|
| 1 | +import type { ProviderActionDefinition } from "../../core/provider-definition.ts"; |
| 2 | +import type { ActionDefinition, JsonSchema } from "../../core/types.ts"; |
| 3 | +import type { FusionApiInputField, FusionApiInputFieldKind, FusionApiOperation } from "./operations.ts"; |
| 4 | + |
| 5 | +import { s } from "../../core/json-schema.ts"; |
| 6 | +import { defineProviderAction } from "../../core/provider-definition.ts"; |
| 7 | +import { fusionApiOperations } from "./operations.ts"; |
| 8 | + |
| 9 | +const service = "fusion-api"; |
| 10 | +const fusionApiActionsById = new Set(fusionApiOperations.map((operation) => `${service}.${operation.actionName}`)); |
| 11 | + |
| 12 | +const urlArrayItemSchema = s.url("One URL accepted by Fusion API."); |
| 13 | + |
| 14 | +const completedStateSchema = s.object("The Fusion API task completed.", { |
| 15 | + state: s.literal("completed", { description: "Task state." }), |
| 16 | +}); |
| 17 | +const processingStateSchema = s.object("The Fusion API task is still processing.", { |
| 18 | + state: s.literal("processing", { description: "Task state." }), |
| 19 | + progress: s.number("Task progress reported by Fusion API."), |
| 20 | +}); |
| 21 | +const missingStateSchema = s.object("The Fusion API task was not found.", { |
| 22 | + state: s.literal("not_found", { description: "Task state." }), |
| 23 | + error: s.string("Error message returned by Fusion API."), |
| 24 | +}); |
| 25 | +const taskResultSchema = s.union( |
| 26 | + [ |
| 27 | + s.object("The Fusion API task completed with result data.", { |
| 28 | + state: s.literal("completed", { description: "Task state." }), |
| 29 | + data: s.unknown("Task result data returned by Fusion API."), |
| 30 | + }), |
| 31 | + processingStateSchema, |
| 32 | + missingStateSchema, |
| 33 | + ], |
| 34 | + { description: "The normalized Fusion API task result." }, |
| 35 | +); |
| 36 | +const taskStateSchema = s.union([completedStateSchema, processingStateSchema, missingStateSchema], { |
| 37 | + description: "The normalized Fusion API task state.", |
| 38 | +}); |
| 39 | +const submitOutputSchema = s.object("The Fusion API task submission handle.", { |
| 40 | + sessionId: s.nonEmptyString("Task session ID used by result and state actions."), |
| 41 | +}); |
| 42 | +const syncActionOutputSchema = s.unknown("The normalized Fusion API action result."); |
| 43 | + |
| 44 | +export const fusionApiActions: ProviderActionDefinition<string>[] = fusionApiOperations.map((operation) => |
| 45 | + defineProviderAction(service, { |
| 46 | + name: operation.actionName, |
| 47 | + description: operation.description, |
| 48 | + inputSchema: buildInputSchema(operation), |
| 49 | + outputSchema: buildOutputSchema(operation), |
| 50 | + followUpActions: buildFollowUpActions(operation), |
| 51 | + asyncLifecycle: buildAsyncLifecycle(operation), |
| 52 | + }), |
| 53 | +); |
| 54 | + |
| 55 | +function buildInputSchema(operation: FusionApiOperation): JsonSchema { |
| 56 | + const properties = Object.fromEntries( |
| 57 | + operation.inputFields.map((field) => [field.name, buildFieldSchema(field)] as const), |
| 58 | + ); |
| 59 | + const required = operation.inputFields.filter((field) => field.required).map((field) => field.name); |
| 60 | + return s.object("The Fusion API request payload.", properties, { |
| 61 | + required, |
| 62 | + additionalProperties: true, |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function buildFieldSchema(field: FusionApiInputField): JsonSchema { |
| 67 | + const options = buildFieldOptions(field); |
| 68 | + |
| 69 | + if (field.enumValues && field.enumValues.length > 0) { |
| 70 | + return s.stringEnum(field.enumValues, options); |
| 71 | + } |
| 72 | + if (isUrlStringField(field)) { |
| 73 | + return s.url(field.description); |
| 74 | + } |
| 75 | + if (isUrlArrayField(field)) { |
| 76 | + return s.array(urlArrayItemSchema, options); |
| 77 | + } |
| 78 | + |
| 79 | + switch (field.kind satisfies FusionApiInputFieldKind) { |
| 80 | + case "string": |
| 81 | + return s.string(options); |
| 82 | + case "number": |
| 83 | + return s.number({ ...options, minimum: field.minimum, maximum: field.maximum }); |
| 84 | + case "integer": |
| 85 | + return s.integer({ ...options, minimum: field.minimum, maximum: field.maximum }); |
| 86 | + case "boolean": |
| 87 | + return s.boolean(options); |
| 88 | + case "array": |
| 89 | + return s.array(s.unknown("One array item."), options); |
| 90 | + case "object": |
| 91 | + return s.record(field.description, s.unknown("One object property value.")); |
| 92 | + case "unknown": |
| 93 | + return s.unknown(field.description); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function buildFieldOptions(field: FusionApiInputField): { |
| 98 | + description: string; |
| 99 | + default?: string | number | boolean; |
| 100 | + format?: string; |
| 101 | +} { |
| 102 | + return { |
| 103 | + description: field.description, |
| 104 | + default: field.defaultValue, |
| 105 | + format: field.format, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +function isUrlStringField(field: FusionApiInputField): boolean { |
| 110 | + return field.kind === "string" && fieldNameLooksLikeUrl(field.name); |
| 111 | +} |
| 112 | + |
| 113 | +function isUrlArrayField(field: FusionApiInputField): boolean { |
| 114 | + return field.kind === "array" && fieldNameLooksLikeUrls(field.name); |
| 115 | +} |
| 116 | + |
| 117 | +function fieldNameLooksLikeUrl(name: string): boolean { |
| 118 | + return name.toLowerCase().endsWith("url"); |
| 119 | +} |
| 120 | + |
| 121 | +function fieldNameLooksLikeUrls(name: string): boolean { |
| 122 | + return name.toLowerCase().endsWith("urls"); |
| 123 | +} |
| 124 | + |
| 125 | +function buildOutputSchema(operation: FusionApiOperation): JsonSchema { |
| 126 | + if (operation.actionName.endsWith("_submit")) { |
| 127 | + return submitOutputSchema; |
| 128 | + } |
| 129 | + if (operation.actionName.endsWith("_result")) { |
| 130 | + return taskResultSchema; |
| 131 | + } |
| 132 | + if (operation.actionName.endsWith("_state")) { |
| 133 | + return taskStateSchema; |
| 134 | + } |
| 135 | + return syncActionOutputSchema; |
| 136 | +} |
| 137 | + |
| 138 | +function buildFollowUpActions(operation: FusionApiOperation): string[] | undefined { |
| 139 | + if (!operation.actionName.endsWith("_submit")) { |
| 140 | + return undefined; |
| 141 | + } |
| 142 | + |
| 143 | + const prefix = operation.actionName.slice(0, -"_submit".length); |
| 144 | + const followUps = [`${service}.${prefix}_result`, `${service}.${prefix}_state`].filter((actionId) => |
| 145 | + fusionApiActionsById.has(actionId), |
| 146 | + ); |
| 147 | + return followUps.length > 0 ? followUps : undefined; |
| 148 | +} |
| 149 | + |
| 150 | +function buildAsyncLifecycle(operation: FusionApiOperation): ActionDefinition["asyncLifecycle"] { |
| 151 | + if (!operation.actionName.endsWith("_submit")) { |
| 152 | + return undefined; |
| 153 | + } |
| 154 | + |
| 155 | + const prefix = operation.actionName.slice(0, -"_submit".length); |
| 156 | + const resultActionId = `${service}.${prefix}_result`; |
| 157 | + if (!fusionApiActionsById.has(resultActionId)) { |
| 158 | + return undefined; |
| 159 | + } |
| 160 | + return { |
| 161 | + startActionId: `${service}.${operation.actionName}`, |
| 162 | + statusActionId: resultActionId, |
| 163 | + }; |
| 164 | +} |
0 commit comments