-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnode-types.ts
More file actions
431 lines (398 loc) · 11.8 KB
/
node-types.ts
File metadata and controls
431 lines (398 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Types for workflows
import type {
DiscordInteraction,
GeoJSON,
Node,
NodeExecution,
NodeType,
ObjectReference,
QueueMessage,
ScheduledTrigger,
TelegramMessage,
WhatsAppMessage,
} from "@dafthunk/types";
import type { BaseToolRegistry } from "./base-tool-registry";
import type { DatabaseService } from "./database-service";
import type { DatasetService } from "./dataset-service";
import type { ObjectStore } from "./object-store";
import type { QueueService } from "./queue-service";
import type { ToolDefinition, ToolReference } from "./tool-types";
/**
* Generic blob parameter type that accepts any MIME type.
* Semantic types below provide workflow connection validation while allowing
* maximum flexibility in the actual data format.
*/
export type BlobParameter = {
data: Uint8Array;
mimeType: string;
filename?: string;
};
/**
* Semantic blob types - same structure, different meaning in workflow graph.
* The type discriminator enforces connection rules in the visual editor,
* while the unrestricted mimeType allows any format for maximum flexibility.
*/
export type ImageParameter = BlobParameter;
export type AudioParameter = BlobParameter;
export type DocumentParameter = BlobParameter;
export type GltfParameter = BlobParameter;
export type VideoParameter = BlobParameter;
/**
* Serialized blob parameter - allows for JSON-serialized Uint8Array
* (object with numeric keys) in addition to native Uint8Array.
*/
export interface SerializedBlobParameter {
data: Uint8Array | Record<string, number>;
mimeType: string;
}
/**
* Check if a value is an object reference (blob stored in R2).
* Object references have an id and mimeType but no data property.
*/
export function isObjectReference(value: unknown): value is ObjectReference {
if (!value || typeof value !== "object") return false;
const obj = value as Record<string, unknown>;
return (
"id" in obj &&
"mimeType" in obj &&
typeof obj.id === "string" &&
typeof obj.mimeType === "string" &&
!("data" in obj)
);
}
/**
* Check if a value is a blob parameter (native or serialized from JSON).
* Handles both native Uint8Array and serialized format (object with numeric keys).
*/
export function isBlobParameter(
value: unknown
): value is SerializedBlobParameter {
if (!value || typeof value !== "object") return false;
const obj = value as Record<string, unknown>;
if (!("data" in obj) || !("mimeType" in obj)) return false;
// Handle native Uint8Array
if (obj.data instanceof Uint8Array) return true;
// Handle serialized Uint8Array (plain object with numeric keys from JSON)
if (obj.data && typeof obj.data === "object" && !Array.isArray(obj.data)) {
const keys = Object.keys(obj.data as object);
return keys.length > 0 && keys.every((k) => /^\d+$/.test(k));
}
return false;
}
/**
* Convert serialized Uint8Array (from JSON) back to native Uint8Array.
*/
export function toUint8Array(
data: Uint8Array | Record<string, number>
): Uint8Array {
if (data instanceof Uint8Array) return data;
const keys = Object.keys(data)
.map(Number)
.sort((a, b) => a - b);
return new Uint8Array(keys.map((k) => data[k]));
}
export type ParameterType =
| {
type: "string";
value?: string;
}
| {
type: "date";
value?: string; // ISO 8601 timestamp
}
| {
type: "number";
value?: number;
}
| {
type: "boolean";
value?: boolean;
}
| {
type: "blob";
value?: BlobParameter;
}
| {
type: "image";
value?: ImageParameter;
}
| {
type: "json";
value?: any;
}
| {
type: "document";
value?: DocumentParameter;
}
| {
type: "audio";
value?: AudioParameter;
}
| {
type: "gltf";
value?: GltfParameter;
}
| {
type: "video";
value?: VideoParameter;
}
| {
type: "geojson";
value?: GeoJSON;
}
| {
type: "any";
value: any;
};
export type ParameterValue = ParameterType["value"];
export interface HttpRequest {
url?: string;
path?: string;
method?: string;
headers?: Record<string, string>;
query?: Record<string, string>;
queryParams?: Record<string, string>; // Alias for query
body?: BlobParameter; // Raw request body with MIME type
}
export interface EmailMessage {
from: string;
to: string;
headers: Record<string, string>;
raw: string;
}
/**
* Minimal integration information exposed to nodes.
* Token is automatically refreshed if expired when accessed via getIntegration.
*/
export interface IntegrationInfo {
id: string;
name: string;
provider: string;
token: string;
metadata?: Record<string, unknown>;
}
export interface NodeEnv {
DB: D1Database;
AI: Ai;
AI_OPTIONS: AiOptions;
RESSOURCES: R2Bucket;
DATASETS: R2Bucket;
DATASETS_AUTORAG: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
DATABASE: DurableObjectNamespace<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
AGENT_RUNNER: DurableObjectNamespace<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
FFMPEG_CONTAINER?: DurableObjectNamespace<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
DUCKDB_SANDBOX?: DurableObjectNamespace<any>;
WORKFLOW_QUEUE: Queue;
EMAIL_DOMAIN: string;
CLOUDFLARE_ACCOUNT_ID?: string;
CLOUDFLARE_API_TOKEN?: string;
CLOUDFLARE_AI_GATEWAY_ID?: string;
TWILIO_ACCOUNT_SID?: string;
TWILIO_AUTH_TOKEN?: string;
TWILIO_PHONE_NUMBER?: string;
SEND_EMAIL?: SendEmail;
HUGGINGFACE_API_KEY?: string;
REPLICATE_API_TOKEN?: string;
}
export interface NodeContext {
nodeId: string;
workflowId: string;
organizationId: string;
/** Workflow execution instance ID (for async node completion events) */
executionId?: string;
/** Whether the runtime supports async node execution via waitForEvent */
asyncSupported?: boolean;
inputs: Record<string, any>;
onProgress?: (progress: number) => void;
httpRequest?: HttpRequest;
emailMessage?: EmailMessage;
queueMessage?: QueueMessage;
scheduledTrigger?: ScheduledTrigger;
discordInteraction?: DiscordInteraction;
discordBotToken?: string;
telegramMessage?: TelegramMessage;
telegramBotToken?: string;
whatsappMessage?: WhatsAppMessage;
whatsappAccessToken?: string;
whatsappPhoneNumberId?: string;
toolRegistry?: BaseToolRegistry;
objectStore?: ObjectStore;
databaseService?: DatabaseService;
datasetService?: DatasetService;
queueService?: QueueService;
// Callback-based access to sensitive data (improves security and isolation)
getSecret?: (secretName: string) => Promise<string | undefined>;
getIntegration: (integrationId: string) => Promise<IntegrationInfo>;
env: NodeEnv;
// Multi-step execution primitives (populated for MultiStepNode instances)
sleep?: (durationMs: number) => Promise<void>;
doStep?: <T>(fn: () => Promise<T>) => Promise<T>;
}
/**
* Context for multi-step nodes with guaranteed access to step primitives.
* Nodes extending MultiStepNode receive this context instead of NodeContext.
*/
export interface MultiStepNodeContext extends NodeContext {
sleep: (durationMs: number) => Promise<void>;
doStep: <T>(fn: () => Promise<T>) => Promise<T>;
}
/**
* Options for creating a node instance
*/
export interface CreateNodeOptions {
id: string;
name?: string;
position: { x: number; y: number };
description?: string;
inputs?: Record<string, unknown>;
}
/**
* Base class for all executable nodes
*/
export abstract class ExecutableNode {
public readonly node: Node;
public static readonly nodeType: NodeType;
constructor(node: Node) {
this.node = node;
}
/**
* Creates a Node definition from this class's nodeType
*/
static create(options: CreateNodeOptions): Node {
// biome-ignore lint/complexity/noThisInStatic: `this` is the calling subclass, not ExecutableNode
const nodeType = this.nodeType;
const inputs = nodeType.inputs.map((input) => {
const override = options.inputs?.[input.name];
if (override !== undefined) {
return { ...input, value: override };
}
return { ...input };
});
return {
id: options.id,
name: options.name ?? nodeType.name,
type: nodeType.type,
description: options.description ?? nodeType.description,
icon: nodeType.icon,
position: options.position,
inputs,
outputs: nodeType.outputs.map((output) => ({ ...output })),
...(nodeType.functionCalling && { functionCalling: true }),
} as Node;
}
public abstract execute(context: NodeContext): Promise<NodeExecution>;
public createSuccessResult(
outputs: Record<string, ParameterValue>,
usage?: number
): NodeExecution {
const nodeType = (this.constructor as typeof ExecutableNode).nodeType;
return {
nodeId: this.node.id,
status: "completed",
outputs,
usage: usage ?? nodeType.usage ?? 1,
} as NodeExecution;
}
public createErrorResult(error: string, usage?: number): NodeExecution {
return {
nodeId: this.node.id,
status: "error",
error,
usage: usage ?? 0,
} as NodeExecution;
}
/**
* Convert tools input to tool definitions for LLM models
* Returns Cloudflare embedded tool definitions with executable functions
*/
public async convertFunctionCallsToToolDefinitions(
functionCalls: ToolReference[],
context: NodeContext
): Promise<ToolDefinition[]> {
return this.resolveToolDefinitions(functionCalls, context);
}
/**
* Convert tools input to Gemini function declarations format
* Returns Gemini-specific function declarations for function calling
*/
public async convertFunctionCallsToGeminiDeclarations(
functionCalls: ToolReference[],
context: NodeContext
): Promise<
Array<{
name: string;
description: string;
parameters: ToolDefinition["parameters"];
}>
> {
const toolDefinitions = await this.resolveToolDefinitions(
functionCalls,
context
);
return toolDefinitions.map((tool) => ({
name: tool.name,
description: tool.description,
parameters: tool.parameters,
}));
}
/**
* Resolves tool references to tool definitions via the tool registry.
* Shared logic for both Cloudflare and Gemini tool formats.
*/
private async resolveToolDefinitions(
functionCalls: ToolReference[],
context: NodeContext
): Promise<ToolDefinition[]> {
if (
!functionCalls ||
!Array.isArray(functionCalls) ||
functionCalls.length === 0
) {
return [];
}
if (!context.toolRegistry) {
console.warn(
"Tool registry not available in context, cannot resolve tools"
);
return [];
}
try {
for (const item of functionCalls) {
if (
!item ||
typeof item !== "object" ||
!item.type ||
!item.identifier
) {
throw new Error(
`Invalid tool reference format. Expected ToolReference with type and identifier: ${JSON.stringify(item)}`
);
}
}
return await context.toolRegistry.getToolDefinitions(functionCalls);
} catch (error) {
console.error("Failed to resolve tool definitions:", error);
return [];
}
}
}
/**
* Base class for nodes that manage their own durable execution steps.
*
* Multi-step nodes break execution into multiple sub-steps via `doStep()`
* and can sleep durably between them via `sleep()`. The runtime skips
* the outer durable step wrapper, giving the node fine-grained control
* over what gets cached and replayed.
*
* Subclasses implement `execute(context: MultiStepNodeContext)` where
* `sleep` and `doStep` are guaranteed to be present.
*/
export abstract class MultiStepNode extends ExecutableNode {
public abstract override execute(
context: MultiStepNodeContext
): Promise<NodeExecution>;
}