@decocms/bindings 1.4.7 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -2
- package/src/index.ts +0 -3
- package/src/well-known/workflow.ts +0 -744
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/bindings",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"check": "tsc --noEmit",
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
"./mcp": "./src/well-known/mcp.ts",
|
|
29
29
|
"./assistant": "./src/well-known/assistant.ts",
|
|
30
30
|
"./prompt": "./src/well-known/prompt.ts",
|
|
31
|
-
"./workflow": "./src/well-known/workflow.ts",
|
|
32
31
|
"./plugins": "./src/core/plugins.ts",
|
|
33
32
|
"./plugin-router": "./src/core/plugin-router.tsx",
|
|
34
33
|
"./ai-gateway": "./src/well-known/ai-gateway.ts",
|
package/src/index.ts
CHANGED
|
@@ -121,9 +121,6 @@ export {
|
|
|
121
121
|
type DeleteObjectsOutput,
|
|
122
122
|
} from "./well-known/object-storage";
|
|
123
123
|
|
|
124
|
-
// Re-export workflow binding types
|
|
125
|
-
export { WORKFLOWS_COLLECTION_BINDING } from "./well-known/workflow";
|
|
126
|
-
|
|
127
124
|
// Re-export brand binding types (for reading org brand context)
|
|
128
125
|
export {
|
|
129
126
|
BrandColorsSchema,
|
|
@@ -1,744 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workflows Well-Known Binding
|
|
3
|
-
*
|
|
4
|
-
* Defines the interface for workflow providers.
|
|
5
|
-
* Any MCP that implements this binding can expose configurable workflows,
|
|
6
|
-
* executions, step results, and events via collection bindings.
|
|
7
|
-
*
|
|
8
|
-
* This binding uses collection bindings for LIST and GET operations (read-only).
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { z } from "zod";
|
|
12
|
-
import { type Binder, bindingClient, type ToolBinder } from "../core/binder";
|
|
13
|
-
import {
|
|
14
|
-
BaseCollectionEntitySchema,
|
|
15
|
-
createCollectionBindings,
|
|
16
|
-
} from "./collections";
|
|
17
|
-
export const ToolCallActionSchema = z.object({
|
|
18
|
-
toolName: z
|
|
19
|
-
.string()
|
|
20
|
-
.describe("Name of the tool to invoke on that connection"),
|
|
21
|
-
transformCode: z
|
|
22
|
-
.string()
|
|
23
|
-
.optional()
|
|
24
|
-
.describe(`Pure TypeScript function for data transformation of the tool call result. Must be a TypeScript file that declares the Output interface and exports a default function: \`interface Output { ... } export default async function(stepInput): Output { ... }\`
|
|
25
|
-
The stepInput will match with the tool call outputSchema. If transformCode is not provided, the tool call result will be used as the step output.
|
|
26
|
-
Providing an transformCode is recommended because it both allows you to transform the data and validate it against a JSON Schema - tools are ephemeral and may return unexpected data.`),
|
|
27
|
-
});
|
|
28
|
-
export type ToolCallAction = z.infer<typeof ToolCallActionSchema>;
|
|
29
|
-
|
|
30
|
-
export const CodeActionSchema = z.object({
|
|
31
|
-
code: z.string().describe(
|
|
32
|
-
`Pure TypeScript function for data transformation. Useful to merge data from multiple steps and transform it. Must be a TypeScript file that declares the Output interface and exports a default function: \`interface Output { ... } export default async function(stepInput): Output { ... }\`
|
|
33
|
-
The stepInput is the resolved value of the references in the input field. Example:
|
|
34
|
-
{
|
|
35
|
-
"input": {
|
|
36
|
-
"name": "@Step_1.name",
|
|
37
|
-
"age": "@Step_2.age"
|
|
38
|
-
},
|
|
39
|
-
"code": "export default function(stepInput): Output { return { result: \`\${stepInput.name} is \${stepInput.age} years old.\` } }"
|
|
40
|
-
}
|
|
41
|
-
`,
|
|
42
|
-
),
|
|
43
|
-
});
|
|
44
|
-
export type CodeAction = z.infer<typeof CodeActionSchema>;
|
|
45
|
-
|
|
46
|
-
export const WaitForSignalActionSchema = z.object({
|
|
47
|
-
signalName: z
|
|
48
|
-
.string()
|
|
49
|
-
.describe(
|
|
50
|
-
"Signal name to wait for (e.g., 'approval'). Execution pauses until SEND_SIGNAL is called with this name.",
|
|
51
|
-
),
|
|
52
|
-
});
|
|
53
|
-
export type WaitForSignalAction = z.infer<typeof WaitForSignalActionSchema>;
|
|
54
|
-
|
|
55
|
-
export const StepActionSchema = z.union([
|
|
56
|
-
ToolCallActionSchema.describe("Call an external tool via MCP connection. "),
|
|
57
|
-
CodeActionSchema.describe(
|
|
58
|
-
"Run pure TypeScript code for data transformation. Useful to merge data from multiple steps and transform it.",
|
|
59
|
-
),
|
|
60
|
-
]);
|
|
61
|
-
export type StepAction = z.infer<typeof StepActionSchema>;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Step Config Schema - Optional configuration for retry, timeout, and looping
|
|
65
|
-
*/
|
|
66
|
-
/**
|
|
67
|
-
* Step Condition Schema - Structured condition for bail and future `when` support.
|
|
68
|
-
* Evaluates a resolved @ref against an optional operator.
|
|
69
|
-
* If no operator is specified, checks for truthiness.
|
|
70
|
-
*/
|
|
71
|
-
export const StepConditionSchema = z.object({
|
|
72
|
-
ref: z
|
|
73
|
-
.string()
|
|
74
|
-
.describe(
|
|
75
|
-
"@ref to resolve and evaluate, e.g. '@validate.shouldStop' or '@input.skipProcessing'",
|
|
76
|
-
),
|
|
77
|
-
eq: z
|
|
78
|
-
.unknown()
|
|
79
|
-
.optional()
|
|
80
|
-
.describe("Step bails if resolved value equals this"),
|
|
81
|
-
neq: z
|
|
82
|
-
.unknown()
|
|
83
|
-
.optional()
|
|
84
|
-
.describe("Step bails if resolved value does NOT equal this"),
|
|
85
|
-
gt: z
|
|
86
|
-
.number()
|
|
87
|
-
.optional()
|
|
88
|
-
.describe("Step bails if resolved value is greater than this"),
|
|
89
|
-
lt: z
|
|
90
|
-
.number()
|
|
91
|
-
.optional()
|
|
92
|
-
.describe("Step bails if resolved value is less than this"),
|
|
93
|
-
});
|
|
94
|
-
export type StepCondition = z.infer<typeof StepConditionSchema>;
|
|
95
|
-
|
|
96
|
-
export const StepConfigSchema = z.object({
|
|
97
|
-
maxAttempts: z
|
|
98
|
-
.number()
|
|
99
|
-
.int()
|
|
100
|
-
.min(1)
|
|
101
|
-
.max(10)
|
|
102
|
-
.optional()
|
|
103
|
-
.describe("Max retry attempts on failure (default: 1, no retries)"),
|
|
104
|
-
backoffMs: z
|
|
105
|
-
.number()
|
|
106
|
-
.int()
|
|
107
|
-
.min(100)
|
|
108
|
-
.max(60_000)
|
|
109
|
-
.optional()
|
|
110
|
-
.describe("Initial delay between retries in ms (doubles each attempt)"),
|
|
111
|
-
timeoutMs: z
|
|
112
|
-
.number()
|
|
113
|
-
.int()
|
|
114
|
-
.min(100)
|
|
115
|
-
.max(86_400_000)
|
|
116
|
-
.optional()
|
|
117
|
-
.describe(
|
|
118
|
-
"Max execution time in ms before step fails (default: 30000). Upper bound 24h.",
|
|
119
|
-
),
|
|
120
|
-
onError: z
|
|
121
|
-
.enum(["fail", "continue"])
|
|
122
|
-
.optional()
|
|
123
|
-
.describe(
|
|
124
|
-
"What to do when this step fails: 'fail' aborts the workflow, 'continue' skips the error and proceeds",
|
|
125
|
-
),
|
|
126
|
-
});
|
|
127
|
-
export type StepConfig = z.infer<typeof StepConfigSchema>;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Step Schema - A single unit of work in a workflow
|
|
131
|
-
*
|
|
132
|
-
* Action types:
|
|
133
|
-
* - Tool call: Invoke an external tool via MCP connection
|
|
134
|
-
* - Code: Run pure TypeScript for data transformation
|
|
135
|
-
* - Wait for signal: Pause until external input (human-in-the-loop)
|
|
136
|
-
*
|
|
137
|
-
* Data flow uses @ref syntax:
|
|
138
|
-
* - @input.field → workflow input
|
|
139
|
-
* - @stepName.field → output from a previous step
|
|
140
|
-
* - @ctx.execution_id → current workflow execution ID
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
export type JsonSchema = {
|
|
144
|
-
type?: string;
|
|
145
|
-
properties?: Record<string, JsonSchema>;
|
|
146
|
-
required?: string[];
|
|
147
|
-
description?: string;
|
|
148
|
-
additionalProperties?: boolean | Record<string, unknown>;
|
|
149
|
-
additionalItems?: boolean | Record<string, unknown>;
|
|
150
|
-
items?: JsonSchema;
|
|
151
|
-
format?: string;
|
|
152
|
-
enum?: string[];
|
|
153
|
-
maxLength?: number;
|
|
154
|
-
anyOf?: JsonSchema[];
|
|
155
|
-
[key: string]: unknown;
|
|
156
|
-
};
|
|
157
|
-
export const JsonSchemaSchema: z.ZodType<JsonSchema> = z.lazy(() =>
|
|
158
|
-
z
|
|
159
|
-
.object({
|
|
160
|
-
type: z.string().optional(),
|
|
161
|
-
properties: z.record(z.string(), z.unknown()).optional(),
|
|
162
|
-
required: z.array(z.string()).optional(),
|
|
163
|
-
description: z.string().optional(),
|
|
164
|
-
additionalProperties: z
|
|
165
|
-
.union([z.boolean(), z.record(z.string(), z.unknown())])
|
|
166
|
-
.optional(),
|
|
167
|
-
additionalItems: z
|
|
168
|
-
.union([z.boolean(), z.record(z.string(), z.unknown())])
|
|
169
|
-
.optional(),
|
|
170
|
-
items: JsonSchemaSchema.optional(),
|
|
171
|
-
})
|
|
172
|
-
.passthrough(),
|
|
173
|
-
) as unknown as z.ZodType<JsonSchema>;
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Step names that are reserved by the @ref system and cannot be used as step names.
|
|
177
|
-
* These are intercepted before step lookup in the ref resolver.
|
|
178
|
-
*/
|
|
179
|
-
export const RESERVED_STEP_NAMES = ["input", "item", "index", "ctx"] as const;
|
|
180
|
-
|
|
181
|
-
export const StepSchema = z.object({
|
|
182
|
-
name: z
|
|
183
|
-
.string()
|
|
184
|
-
.min(1)
|
|
185
|
-
.refine(
|
|
186
|
-
(name) => !(RESERVED_STEP_NAMES as readonly string[]).includes(name),
|
|
187
|
-
{
|
|
188
|
-
message: `Step name is reserved. Reserved names: ${RESERVED_STEP_NAMES.join(", ")}`,
|
|
189
|
-
},
|
|
190
|
-
)
|
|
191
|
-
.describe(
|
|
192
|
-
"Unique identifier for this step. Other steps reference its output as @name.field",
|
|
193
|
-
),
|
|
194
|
-
description: z.string().optional().describe("What this step does"),
|
|
195
|
-
action: StepActionSchema,
|
|
196
|
-
input: z
|
|
197
|
-
.record(z.string(), z.unknown())
|
|
198
|
-
.optional()
|
|
199
|
-
.describe(
|
|
200
|
-
"Data passed to the action. Use @ref for dynamic values: @input.field (workflow input), @stepName.field (previous step output), @item/@index (loop context), @ctx.execution_id (current execution ID). Example: { 'userId': '@input.user_id', 'data': '@fetch.result', 'executionId': '@ctx.execution_id' }",
|
|
201
|
-
),
|
|
202
|
-
outputSchema: JsonSchemaSchema.optional().describe(
|
|
203
|
-
"Optional JSON Schema describing the expected output of the step.",
|
|
204
|
-
),
|
|
205
|
-
config: StepConfigSchema.optional().describe("Retry and timeout settings"),
|
|
206
|
-
forEach: z
|
|
207
|
-
.object({
|
|
208
|
-
ref: z.string().describe("@ ref to the step to iterate over"),
|
|
209
|
-
concurrency: z
|
|
210
|
-
.number()
|
|
211
|
-
.optional()
|
|
212
|
-
.default(1)
|
|
213
|
-
.describe("max parallel iterations. default is 1 (sequential)"),
|
|
214
|
-
})
|
|
215
|
-
.optional(),
|
|
216
|
-
bail: z
|
|
217
|
-
.union([z.literal(true), StepConditionSchema])
|
|
218
|
-
.optional()
|
|
219
|
-
.describe(
|
|
220
|
-
"Early return: if true, the workflow exits with this step's output on successful completion. If a condition object, the workflow exits only when the condition is met. Bail is ignored on step error.",
|
|
221
|
-
),
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
export type Step = z.infer<typeof StepSchema>;
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Workflow Execution Status
|
|
228
|
-
*
|
|
229
|
-
* States:
|
|
230
|
-
* - pending: Created but not started
|
|
231
|
-
* - running: Currently executing
|
|
232
|
-
* - completed: Successfully finished
|
|
233
|
-
* - cancelled: Manually cancelled
|
|
234
|
-
*/
|
|
235
|
-
|
|
236
|
-
const WorkflowExecutionStatusEnum = z
|
|
237
|
-
.enum(["enqueued", "running", "success", "error", "failed", "cancelled"])
|
|
238
|
-
.default("enqueued");
|
|
239
|
-
export type WorkflowExecutionStatus = z.infer<
|
|
240
|
-
typeof WorkflowExecutionStatusEnum
|
|
241
|
-
>;
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Workflow Execution Schema
|
|
245
|
-
*
|
|
246
|
-
* Includes lock columns and retry tracking.
|
|
247
|
-
*/
|
|
248
|
-
export const WorkflowExecutionSchema = BaseCollectionEntitySchema.extend({
|
|
249
|
-
virtual_mcp_id: z
|
|
250
|
-
.string()
|
|
251
|
-
.describe(
|
|
252
|
-
"ID of the virtual MCP (agent) that will be used to execute the workflow",
|
|
253
|
-
),
|
|
254
|
-
status: WorkflowExecutionStatusEnum.describe(
|
|
255
|
-
"Current status of the workflow execution",
|
|
256
|
-
),
|
|
257
|
-
input: z
|
|
258
|
-
.record(z.string(), z.unknown())
|
|
259
|
-
.optional()
|
|
260
|
-
.describe("Input data for the workflow execution"),
|
|
261
|
-
output: z
|
|
262
|
-
.unknown()
|
|
263
|
-
.optional()
|
|
264
|
-
.describe("Output data for the workflow execution"),
|
|
265
|
-
completed_at_epoch_ms: z
|
|
266
|
-
.number()
|
|
267
|
-
.nullish()
|
|
268
|
-
.describe("Timestamp of when the workflow execution completed"),
|
|
269
|
-
start_at_epoch_ms: z
|
|
270
|
-
.number()
|
|
271
|
-
.nullish()
|
|
272
|
-
.describe("Timestamp of when the workflow execution started or will start"),
|
|
273
|
-
timeout_ms: z
|
|
274
|
-
.number()
|
|
275
|
-
.nullish()
|
|
276
|
-
.describe("Timeout in milliseconds for the workflow execution"),
|
|
277
|
-
deadline_at_epoch_ms: z
|
|
278
|
-
.number()
|
|
279
|
-
.nullish()
|
|
280
|
-
.describe(
|
|
281
|
-
"Deadline for the workflow execution - when the workflow execution will be cancelled if it is not completed. This is read-only and is set by the workflow engine when an execution is created.",
|
|
282
|
-
),
|
|
283
|
-
error: z
|
|
284
|
-
.unknown()
|
|
285
|
-
.nullish()
|
|
286
|
-
.describe("Error that occurred during the workflow execution"),
|
|
287
|
-
completed_steps: z
|
|
288
|
-
.object({
|
|
289
|
-
success: z
|
|
290
|
-
.array(
|
|
291
|
-
z.object({
|
|
292
|
-
name: z.string(),
|
|
293
|
-
completed_at_epoch_ms: z.number(),
|
|
294
|
-
}),
|
|
295
|
-
)
|
|
296
|
-
.describe("Names of the steps that were completed successfully"),
|
|
297
|
-
error: z.array(z.string()).describe("Names of the steps that errored"),
|
|
298
|
-
})
|
|
299
|
-
.optional()
|
|
300
|
-
.describe("Names of the steps that were completed and their status"),
|
|
301
|
-
running_steps: z
|
|
302
|
-
.array(z.string())
|
|
303
|
-
.optional()
|
|
304
|
-
.describe("Names of the steps that are currently running"),
|
|
305
|
-
});
|
|
306
|
-
export type WorkflowExecution = z.infer<typeof WorkflowExecutionSchema>;
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Event Type Enum
|
|
310
|
-
*
|
|
311
|
-
* Event types for the unified events table:
|
|
312
|
-
* - signal: External signal (human-in-the-loop)
|
|
313
|
-
* - timer: Durable sleep wake-up
|
|
314
|
-
* - message: Inter-workflow communication (send/recv)
|
|
315
|
-
* - output: Published value (setEvent/getEvent)
|
|
316
|
-
* - step_started: Observability - step began
|
|
317
|
-
* - step_completed: Observability - step finished
|
|
318
|
-
* - workflow_started: Workflow began execution
|
|
319
|
-
* - workflow_completed: Workflow finished
|
|
320
|
-
*/
|
|
321
|
-
export const EventTypeEnum = z.enum([
|
|
322
|
-
"signal",
|
|
323
|
-
"timer",
|
|
324
|
-
"message",
|
|
325
|
-
"output",
|
|
326
|
-
"step_started",
|
|
327
|
-
"step_completed",
|
|
328
|
-
"workflow_started",
|
|
329
|
-
"workflow_completed",
|
|
330
|
-
]);
|
|
331
|
-
|
|
332
|
-
export type EventType = z.infer<typeof EventTypeEnum>;
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Workflow Event Schema
|
|
336
|
-
*
|
|
337
|
-
* Unified events table for signals, timers, messages, and observability.
|
|
338
|
-
*/
|
|
339
|
-
export const WorkflowEventSchema = BaseCollectionEntitySchema.extend({
|
|
340
|
-
execution_id: z.string(),
|
|
341
|
-
type: EventTypeEnum,
|
|
342
|
-
name: z.string().nullish(),
|
|
343
|
-
payload: z.unknown().optional(),
|
|
344
|
-
visible_at: z.number().nullish(),
|
|
345
|
-
consumed_at: z.number().nullish(),
|
|
346
|
-
source_execution_id: z.string().nullish(),
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
export type WorkflowEvent = z.infer<typeof WorkflowEventSchema>;
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Workflow Schema - A sequence of steps that execute with data flowing between them
|
|
353
|
-
*
|
|
354
|
-
* Key concepts:
|
|
355
|
-
* - Steps run in parallel unless they reference each other via @ref
|
|
356
|
-
* - Use @ref to wire data: @input.field, @stepName.field, @item (in loops)
|
|
357
|
-
* - Execution order is auto-determined from @ref dependencies
|
|
358
|
-
*
|
|
359
|
-
* Example: 2 parallel fetches + 1 merge step
|
|
360
|
-
* {
|
|
361
|
-
* "title": "Fetch and Merge",
|
|
362
|
-
* "steps": [
|
|
363
|
-
* { "name": "fetch_users", "action": { "connectionId": "api", "toolName": "getUsers" } },
|
|
364
|
-
* { "name": "fetch_orders", "action": { "connectionId": "api", "toolName": "getOrders" } },
|
|
365
|
-
* { "name": "merge", "action": { "code": "..." }, "input": { "users": "@fetch_users.data", "orders": "@fetch_orders.data" } }
|
|
366
|
-
* ]
|
|
367
|
-
* }
|
|
368
|
-
* → fetch_users and fetch_orders run in parallel; merge waits for both
|
|
369
|
-
*/
|
|
370
|
-
export const WorkflowSchema = BaseCollectionEntitySchema.extend({
|
|
371
|
-
description: z
|
|
372
|
-
.string()
|
|
373
|
-
.optional()
|
|
374
|
-
.describe("Human-readable summary of what this workflow does"),
|
|
375
|
-
|
|
376
|
-
input_schema: JsonSchemaSchema.optional().describe(
|
|
377
|
-
"Optional JSON Schema describing the expected input for this workflow.",
|
|
378
|
-
),
|
|
379
|
-
|
|
380
|
-
steps: z
|
|
381
|
-
.array(StepSchema)
|
|
382
|
-
.describe(
|
|
383
|
-
"Ordered list of steps. Execution order is auto-determined by @ref dependencies: steps with no @ref dependencies run in parallel; steps referencing @stepName wait for that step to complete.",
|
|
384
|
-
),
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
export type Workflow = z.infer<typeof WorkflowSchema>;
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* WORKFLOW Collection Binding
|
|
391
|
-
*
|
|
392
|
-
* Collection bindings for workflows (read-only).
|
|
393
|
-
* Provides LIST and GET operations for workflows.
|
|
394
|
-
*/
|
|
395
|
-
export const WORKFLOWS_COLLECTION_BINDING = createCollectionBindings(
|
|
396
|
-
"workflow",
|
|
397
|
-
WorkflowSchema,
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
const DEFAULT_STEP_CONFIG: StepConfig = {
|
|
401
|
-
maxAttempts: 1,
|
|
402
|
-
timeoutMs: 30000,
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
// export const DEFAULT_WAIT_FOR_SIGNAL_STEP: Omit<Step, "name"> = {
|
|
406
|
-
// action: {
|
|
407
|
-
// signalName: "approve_output",
|
|
408
|
-
// },
|
|
409
|
-
// outputSchema: {
|
|
410
|
-
// type: "object",
|
|
411
|
-
// properties: {
|
|
412
|
-
// approved: {
|
|
413
|
-
// type: "boolean",
|
|
414
|
-
// description: "Whether the output was approved",
|
|
415
|
-
// },
|
|
416
|
-
// },
|
|
417
|
-
// },
|
|
418
|
-
// };
|
|
419
|
-
export const DEFAULT_TOOL_STEP: Omit<Step, "name"> = {
|
|
420
|
-
action: {
|
|
421
|
-
toolName: "LLM_DO_GENERATE",
|
|
422
|
-
transformCode: `
|
|
423
|
-
interface Input {
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
export default function(stepInput) { return stepInput.result }`,
|
|
427
|
-
},
|
|
428
|
-
input: {
|
|
429
|
-
modelId: "anthropic/claude-4.5-haiku",
|
|
430
|
-
prompt: "Write a haiku about the weather.",
|
|
431
|
-
},
|
|
432
|
-
|
|
433
|
-
config: DEFAULT_STEP_CONFIG,
|
|
434
|
-
outputSchema: {
|
|
435
|
-
type: "object",
|
|
436
|
-
properties: {
|
|
437
|
-
result: {
|
|
438
|
-
type: "string",
|
|
439
|
-
description: "The result of the step",
|
|
440
|
-
},
|
|
441
|
-
},
|
|
442
|
-
},
|
|
443
|
-
};
|
|
444
|
-
export const DEFAULT_CODE_STEP: Step = {
|
|
445
|
-
name: "Initial Step",
|
|
446
|
-
action: {
|
|
447
|
-
code: `
|
|
448
|
-
interface Input {
|
|
449
|
-
example: string;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
interface Output {
|
|
453
|
-
result: unknown;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
export default async function(stepInput: Input): Promise<Output> {
|
|
457
|
-
return {
|
|
458
|
-
result: stepInput.example
|
|
459
|
-
}
|
|
460
|
-
}`,
|
|
461
|
-
},
|
|
462
|
-
config: DEFAULT_STEP_CONFIG,
|
|
463
|
-
outputSchema: {
|
|
464
|
-
type: "object",
|
|
465
|
-
properties: {
|
|
466
|
-
result: {
|
|
467
|
-
type: "string",
|
|
468
|
-
description: "The result of the step",
|
|
469
|
-
},
|
|
470
|
-
},
|
|
471
|
-
required: ["result"],
|
|
472
|
-
description:
|
|
473
|
-
"The output of the step. This is a JSON Schema describing the expected output of the step.",
|
|
474
|
-
},
|
|
475
|
-
};
|
|
476
|
-
|
|
477
|
-
export const createDefaultWorkflow = (id?: string): Workflow => ({
|
|
478
|
-
id: id || crypto.randomUUID(),
|
|
479
|
-
title: "Default Workflow",
|
|
480
|
-
description: "The default workflow for the toolkit",
|
|
481
|
-
steps: [DEFAULT_CODE_STEP],
|
|
482
|
-
created_at: new Date().toISOString(),
|
|
483
|
-
updated_at: new Date().toISOString(),
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
export const WORKFLOW_EXECUTIONS_COLLECTION_BINDING = createCollectionBindings(
|
|
487
|
-
"workflow_execution",
|
|
488
|
-
WorkflowExecutionSchema,
|
|
489
|
-
);
|
|
490
|
-
|
|
491
|
-
/**
|
|
492
|
-
* WORKFLOWS Binding
|
|
493
|
-
*
|
|
494
|
-
* Defines the interface for workflow providers.
|
|
495
|
-
* Any MCP that implements this binding can provide configurable workflows.
|
|
496
|
-
*
|
|
497
|
-
* Required tools:
|
|
498
|
-
* - COLLECTION_WORKFLOW_LIST: List available workflows with their configurations
|
|
499
|
-
* - COLLECTION_WORKFLOW_GET: Get a single workflow by ID (includes steps and triggers)
|
|
500
|
-
*/
|
|
501
|
-
export const WORKFLOW_COLLECTIONS_BINDINGS = [
|
|
502
|
-
...WORKFLOWS_COLLECTION_BINDING,
|
|
503
|
-
...WORKFLOW_EXECUTIONS_COLLECTION_BINDING,
|
|
504
|
-
] as const satisfies Binder;
|
|
505
|
-
|
|
506
|
-
export const WORKFLOW_BINDING = [
|
|
507
|
-
...WORKFLOW_COLLECTIONS_BINDINGS,
|
|
508
|
-
] satisfies ToolBinder[];
|
|
509
|
-
|
|
510
|
-
export const WorkflowBinding = bindingClient(WORKFLOW_BINDING);
|
|
511
|
-
|
|
512
|
-
export const WORKFLOW_EXECUTION_BINDING = createCollectionBindings(
|
|
513
|
-
"workflow_execution",
|
|
514
|
-
WorkflowExecutionSchema,
|
|
515
|
-
);
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* DAG (Directed Acyclic Graph) utilities for workflow step execution
|
|
519
|
-
*
|
|
520
|
-
* Pure TypeScript functions for analyzing step dependencies and grouping
|
|
521
|
-
* steps into execution levels for parallel execution.
|
|
522
|
-
*
|
|
523
|
-
* Can be used in both frontend (visualization) and backend (execution).
|
|
524
|
-
*/
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* Minimal step interface for DAG computation.
|
|
528
|
-
* This allows the DAG utilities to work with any step-like object.
|
|
529
|
-
*/
|
|
530
|
-
export interface DAGStep {
|
|
531
|
-
name: string;
|
|
532
|
-
input?: unknown;
|
|
533
|
-
bail?: true | StepCondition;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
/**
|
|
537
|
-
* Extract all @ref references from a value recursively.
|
|
538
|
-
* Finds patterns like @stepName or @stepName.field
|
|
539
|
-
*
|
|
540
|
-
* @param input - Any value that might contain @ref strings
|
|
541
|
-
* @returns Array of unique reference names (without @ prefix)
|
|
542
|
-
*/
|
|
543
|
-
export function getAllRefs(input: unknown): string[] {
|
|
544
|
-
const refs: string[] = [];
|
|
545
|
-
|
|
546
|
-
function traverse(value: unknown) {
|
|
547
|
-
if (typeof value === "string") {
|
|
548
|
-
const matches = value.match(/@([a-zA-Z_][a-zA-Z0-9_-]*)/g);
|
|
549
|
-
if (matches) {
|
|
550
|
-
refs.push(...matches.map((m) => m.substring(1))); // Remove @ prefix
|
|
551
|
-
}
|
|
552
|
-
} else if (Array.isArray(value)) {
|
|
553
|
-
value.forEach(traverse);
|
|
554
|
-
} else if (typeof value === "object" && value !== null) {
|
|
555
|
-
Object.values(value).forEach(traverse);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
traverse(input);
|
|
560
|
-
return [...new Set(refs)].sort(); // Dedupe and sort for consistent results
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
/**
|
|
564
|
-
* Get the dependencies of a step (other steps it references).
|
|
565
|
-
* Only returns dependencies that are actual step names (filters out built-ins like "item", "index", "input").
|
|
566
|
-
*
|
|
567
|
-
* @param step - The step to analyze
|
|
568
|
-
* @param allStepNames - Set of all step names in the workflow
|
|
569
|
-
* @returns Array of step names this step depends on
|
|
570
|
-
*/
|
|
571
|
-
export function getStepDependencies(
|
|
572
|
-
step: DAGStep,
|
|
573
|
-
allStepNames: Set<string>,
|
|
574
|
-
): string[] {
|
|
575
|
-
const deps: string[] = [];
|
|
576
|
-
|
|
577
|
-
function traverse(value: unknown) {
|
|
578
|
-
if (typeof value === "string") {
|
|
579
|
-
// Match @stepName or @stepName.something patterns
|
|
580
|
-
const matches = value.match(/@([a-zA-Z_][a-zA-Z0-9_-]*)/g);
|
|
581
|
-
if (matches) {
|
|
582
|
-
for (const match of matches) {
|
|
583
|
-
const refName = match.substring(1); // Remove @
|
|
584
|
-
// Only count as dependency if it references another step
|
|
585
|
-
// (not "item", "index", "input" from forEach or workflow input)
|
|
586
|
-
if (allStepNames.has(refName) && refName !== step.name) {
|
|
587
|
-
deps.push(refName);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
} else if (Array.isArray(value)) {
|
|
592
|
-
value.forEach(traverse);
|
|
593
|
-
} else if (typeof value === "object" && value !== null) {
|
|
594
|
-
Object.values(value).forEach(traverse);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
traverse(step.input);
|
|
599
|
-
|
|
600
|
-
// Extract dependency from bail condition ref
|
|
601
|
-
if (step.bail && step.bail !== true) {
|
|
602
|
-
traverse(step.bail.ref);
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
return [...new Set(deps)];
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
* Build edges for the DAG: [fromStep, toStep][]
|
|
610
|
-
*/
|
|
611
|
-
export function buildDagEdges(steps: Step[]): [string, string][] {
|
|
612
|
-
const stepNames = new Set(steps.map((s) => s.name));
|
|
613
|
-
const edges: [string, string][] = [];
|
|
614
|
-
|
|
615
|
-
for (const step of steps) {
|
|
616
|
-
const deps = getStepDependencies(step, stepNames);
|
|
617
|
-
for (const dep of deps) {
|
|
618
|
-
edges.push([dep, step.name]);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
return edges;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* Compute topological levels for all steps.
|
|
627
|
-
* Level 0 = no dependencies on other steps
|
|
628
|
-
* Level N = depends on at least one step at level N-1
|
|
629
|
-
*
|
|
630
|
-
* @param steps - Array of steps to analyze
|
|
631
|
-
* @returns Map from step name to level number
|
|
632
|
-
*/
|
|
633
|
-
export function computeStepLevels<T extends DAGStep>(
|
|
634
|
-
steps: T[],
|
|
635
|
-
): Map<string, number> {
|
|
636
|
-
const stepNames = new Set(steps.map((s) => s.name));
|
|
637
|
-
const levels = new Map<string, number>();
|
|
638
|
-
|
|
639
|
-
// Build dependency map
|
|
640
|
-
const depsMap = new Map<string, string[]>();
|
|
641
|
-
for (const step of steps) {
|
|
642
|
-
depsMap.set(step.name, getStepDependencies(step, stepNames));
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// Compute level for each step (with memoization)
|
|
646
|
-
function getLevel(stepName: string, visited: Set<string>): number {
|
|
647
|
-
if (levels.has(stepName)) return levels.get(stepName)!;
|
|
648
|
-
if (visited.has(stepName)) return 0; // Cycle detection
|
|
649
|
-
|
|
650
|
-
visited.add(stepName);
|
|
651
|
-
const deps = depsMap.get(stepName) || [];
|
|
652
|
-
|
|
653
|
-
if (deps.length === 0) {
|
|
654
|
-
levels.set(stepName, 0);
|
|
655
|
-
return 0;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
const maxDepLevel = Math.max(...deps.map((d) => getLevel(d, visited)));
|
|
659
|
-
const level = maxDepLevel + 1;
|
|
660
|
-
levels.set(stepName, level);
|
|
661
|
-
return level;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
for (const step of steps) {
|
|
665
|
-
getLevel(step.name, new Set());
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
return levels;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
/**
|
|
672
|
-
* Group steps by their execution level.
|
|
673
|
-
* Steps at the same level have no dependencies on each other and can run in parallel.
|
|
674
|
-
*
|
|
675
|
-
* @param steps - Array of steps to group
|
|
676
|
-
* @returns Array of step arrays, where index is the level
|
|
677
|
-
*/
|
|
678
|
-
export function groupStepsByLevel<T extends DAGStep>(steps: T[]): T[][] {
|
|
679
|
-
const levels = computeStepLevels(steps);
|
|
680
|
-
const maxLevel = Math.max(...Array.from(levels.values()), -1);
|
|
681
|
-
|
|
682
|
-
const grouped: T[][] = [];
|
|
683
|
-
for (let level = 0; level <= maxLevel; level++) {
|
|
684
|
-
const stepsAtLevel = steps.filter((s) => levels.get(s.name) === level);
|
|
685
|
-
if (stepsAtLevel.length > 0) {
|
|
686
|
-
grouped.push(stepsAtLevel);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
return grouped;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
/**
|
|
694
|
-
* Validate that there are no cycles in the step dependencies.
|
|
695
|
-
*
|
|
696
|
-
* @param steps - Array of steps to validate
|
|
697
|
-
* @returns Object with isValid and optional error message
|
|
698
|
-
*/
|
|
699
|
-
export function validateNoCycles<T extends DAGStep>(
|
|
700
|
-
steps: T[],
|
|
701
|
-
): { isValid: boolean; error?: string } {
|
|
702
|
-
const stepNames = new Set(steps.map((s) => s.name));
|
|
703
|
-
const depsMap = new Map<string, string[]>();
|
|
704
|
-
|
|
705
|
-
for (const step of steps) {
|
|
706
|
-
depsMap.set(step.name, getStepDependencies(step, stepNames));
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
const visited = new Set<string>();
|
|
710
|
-
const recursionStack = new Set<string>();
|
|
711
|
-
|
|
712
|
-
function hasCycle(stepName: string, path: string[]): string[] | null {
|
|
713
|
-
if (recursionStack.has(stepName)) {
|
|
714
|
-
return [...path, stepName];
|
|
715
|
-
}
|
|
716
|
-
if (visited.has(stepName)) {
|
|
717
|
-
return null;
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
visited.add(stepName);
|
|
721
|
-
recursionStack.add(stepName);
|
|
722
|
-
|
|
723
|
-
const deps = depsMap.get(stepName) || [];
|
|
724
|
-
for (const dep of deps) {
|
|
725
|
-
const cycle = hasCycle(dep, [...path, stepName]);
|
|
726
|
-
if (cycle) return cycle;
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
recursionStack.delete(stepName);
|
|
730
|
-
return null;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
for (const step of steps) {
|
|
734
|
-
const cycle = hasCycle(step.name, []);
|
|
735
|
-
if (cycle) {
|
|
736
|
-
return {
|
|
737
|
-
isValid: false,
|
|
738
|
-
error: `Circular dependency detected: ${cycle.join(" -> ")}`,
|
|
739
|
-
};
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
return { isValid: true };
|
|
744
|
-
}
|