-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathconstants.ts
More file actions
401 lines (331 loc) · 9.88 KB
/
constants.ts
File metadata and controls
401 lines (331 loc) · 9.88 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
export enum BlockType {
PARALLEL = 'parallel',
LOOP = 'loop',
ROUTER = 'router',
CONDITION = 'condition',
START_TRIGGER = 'start_trigger',
STARTER = 'starter',
TRIGGER = 'trigger',
FUNCTION = 'function',
AGENT = 'agent',
API = 'api',
EVALUATOR = 'evaluator',
VARIABLES = 'variables',
RESPONSE = 'response',
HUMAN_IN_THE_LOOP = 'human_in_the_loop',
WORKFLOW = 'workflow',
WORKFLOW_INPUT = 'workflow_input',
WAIT = 'wait',
NOTE = 'note',
SENTINEL_START = 'sentinel_start',
SENTINEL_END = 'sentinel_end',
}
export const TRIGGER_BLOCK_TYPES = [
BlockType.START_TRIGGER,
BlockType.STARTER,
BlockType.TRIGGER,
] as const
export const METADATA_ONLY_BLOCK_TYPES = [
BlockType.LOOP,
BlockType.PARALLEL,
BlockType.NOTE,
] as const
export type LoopType = 'for' | 'forEach' | 'while' | 'doWhile'
export type SentinelType = 'start' | 'end'
export type ParallelType = 'collection' | 'count'
export const EDGE = {
CONDITION_PREFIX: 'condition-',
CONDITION_TRUE: 'condition-true',
CONDITION_FALSE: 'condition-false',
ROUTER_PREFIX: 'router-',
LOOP_CONTINUE: 'loop_continue',
LOOP_CONTINUE_ALT: 'loop-continue-source',
LOOP_EXIT: 'loop_exit',
ERROR: 'error',
SOURCE: 'source',
DEFAULT: 'default',
} as const
export const LOOP = {
TYPE: {
FOR: 'for' as LoopType,
FOR_EACH: 'forEach' as LoopType,
WHILE: 'while' as LoopType,
DO_WHILE: 'doWhile',
},
SENTINEL: {
PREFIX: 'loop-',
START_SUFFIX: '-sentinel-start',
END_SUFFIX: '-sentinel-end',
START_TYPE: 'start' as SentinelType,
END_TYPE: 'end' as SentinelType,
START_NAME_PREFIX: 'Loop Start',
END_NAME_PREFIX: 'Loop End',
},
} as const
export const PARALLEL = {
TYPE: {
COLLECTION: 'collection' as ParallelType,
COUNT: 'count' as ParallelType,
},
BRANCH: {
PREFIX: '₍',
SUFFIX: '₎',
},
DEFAULT_COUNT: 1,
} as const
export const REFERENCE = {
START: '<',
END: '>',
PATH_DELIMITER: '.',
ENV_VAR_START: '{{',
ENV_VAR_END: '}}',
PREFIX: {
LOOP: 'loop',
PARALLEL: 'parallel',
VARIABLE: 'variable',
},
} as const
export const SPECIAL_REFERENCE_PREFIXES = [
REFERENCE.PREFIX.LOOP,
REFERENCE.PREFIX.PARALLEL,
REFERENCE.PREFIX.VARIABLE,
] as const
export const LOOP_REFERENCE = {
ITERATION: 'iteration',
INDEX: 'index',
ITEM: 'item',
INDEX_PATH: 'loop.index',
} as const
export const PARALLEL_REFERENCE = {
INDEX: 'index',
CURRENT_ITEM: 'currentItem',
ITEMS: 'items',
} as const
export const DEFAULTS = {
BLOCK_TYPE: 'unknown',
BLOCK_TITLE: 'Untitled Block',
WORKFLOW_NAME: 'Workflow',
MAX_LOOP_ITERATIONS: 1000,
MAX_FOREACH_ITEMS: 1000,
MAX_PARALLEL_BRANCHES: 20,
MAX_WORKFLOW_DEPTH: 10,
EXECUTION_TIME: 0,
TOKENS: {
PROMPT: 0,
COMPLETION: 0,
TOTAL: 0,
},
COST: {
INPUT: 0,
OUTPUT: 0,
TOTAL: 0,
},
} as const
export const HTTP = {
STATUS: {
OK: 200,
FORBIDDEN: 403,
NOT_FOUND: 404,
TOO_MANY_REQUESTS: 429,
SERVER_ERROR: 500,
},
CONTENT_TYPE: {
JSON: 'application/json',
EVENT_STREAM: 'text/event-stream',
},
} as const
export const AGENT = {
DEFAULT_MODEL: 'claude-sonnet-4-5',
DEFAULT_FUNCTION_TIMEOUT: 600000,
REQUEST_TIMEOUT: 600000,
CUSTOM_TOOL_PREFIX: 'custom_',
} as const
export const MCP = {
TOOL_PREFIX: 'mcp-',
} as const
export const MEMORY = {
DEFAULT_SLIDING_WINDOW_SIZE: 10,
DEFAULT_SLIDING_WINDOW_TOKENS: 4000,
CONTEXT_WINDOW_UTILIZATION: 0.9,
MAX_CONVERSATION_ID_LENGTH: 255,
MAX_MESSAGE_CONTENT_BYTES: 100 * 1024,
} as const
export const ROUTER = {
DEFAULT_MODEL: 'claude-sonnet-4-5',
DEFAULT_TEMPERATURE: 0,
INFERENCE_TEMPERATURE: 0.1,
} as const
export const EVALUATOR = {
DEFAULT_MODEL: 'claude-sonnet-4-5',
DEFAULT_TEMPERATURE: 0.1,
RESPONSE_SCHEMA_NAME: 'evaluation_response',
JSON_INDENT: 2,
} as const
export const CONDITION = {
ELSE_LABEL: 'else',
ELSE_TITLE: 'else',
} as const
export const PAUSE_RESUME = {
OPERATION: {
HUMAN: 'human',
API: 'api',
},
PATH: {
API_RESUME: '/api/resume',
UI_RESUME: '/resume',
},
} as const
export function buildResumeApiUrl(
baseUrl: string | undefined,
workflowId: string,
executionId: string,
contextId: string
): string {
const prefix = baseUrl ?? ''
return `${prefix}${PAUSE_RESUME.PATH.API_RESUME}/${workflowId}/${executionId}/${contextId}`
}
export function buildResumeUiUrl(
baseUrl: string | undefined,
workflowId: string,
executionId: string
): string {
const prefix = baseUrl ?? ''
return `${prefix}${PAUSE_RESUME.PATH.UI_RESUME}/${workflowId}/${executionId}`
}
export const PARSING = {
JSON_RADIX: 10,
PREVIEW_LENGTH: 200,
PREVIEW_SUFFIX: '...',
} as const
export type FieldType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'files' | 'plain'
export interface ConditionConfig {
id: string
label?: string
condition: string
}
export function isTriggerBlockType(blockType: string | undefined): boolean {
return TRIGGER_BLOCK_TYPES.includes(blockType as any)
}
export function isMetadataOnlyBlockType(blockType: string | undefined): boolean {
return METADATA_ONLY_BLOCK_TYPES.includes(blockType as any)
}
export function isWorkflowBlockType(blockType: string | undefined): boolean {
return blockType === BlockType.WORKFLOW || blockType === BlockType.WORKFLOW_INPUT
}
export function isSentinelBlockType(blockType: string | undefined): boolean {
return blockType === BlockType.SENTINEL_START || blockType === BlockType.SENTINEL_END
}
export function isConditionBlockType(blockType: string | undefined): boolean {
return blockType === BlockType.CONDITION
}
export function isRouterBlockType(blockType: string | undefined): boolean {
return blockType === BlockType.ROUTER
}
export function isAgentBlockType(blockType: string | undefined): boolean {
return blockType === BlockType.AGENT
}
export function isAnnotationOnlyBlock(blockType: string | undefined): boolean {
return blockType === BlockType.NOTE
}
export function supportsHandles(blockType: string | undefined): boolean {
return !isAnnotationOnlyBlock(blockType)
}
export function getDefaultTokens() {
return {
input: DEFAULTS.TOKENS.PROMPT,
output: DEFAULTS.TOKENS.COMPLETION,
total: DEFAULTS.TOKENS.TOTAL,
}
}
export function getDefaultCost() {
return {
input: DEFAULTS.COST.INPUT,
output: DEFAULTS.COST.OUTPUT,
total: DEFAULTS.COST.TOTAL,
}
}
export function buildReference(path: string): string {
return `${REFERENCE.START}${path}${REFERENCE.END}`
}
export function buildLoopReference(property: string): string {
return buildReference(`${REFERENCE.PREFIX.LOOP}${REFERENCE.PATH_DELIMITER}${property}`)
}
export function buildParallelReference(property: string): string {
return buildReference(`${REFERENCE.PREFIX.PARALLEL}${REFERENCE.PATH_DELIMITER}${property}`)
}
export function buildVariableReference(variableName: string): string {
return buildReference(`${REFERENCE.PREFIX.VARIABLE}${REFERENCE.PATH_DELIMITER}${variableName}`)
}
export function buildBlockReference(blockId: string, path?: string): string {
return buildReference(path ? `${blockId}${REFERENCE.PATH_DELIMITER}${path}` : blockId)
}
export function buildLoopIndexCondition(maxIterations: number): string {
return `${buildLoopReference(LOOP_REFERENCE.INDEX)} < ${maxIterations}`
}
export function buildEnvVarReference(varName: string): string {
return `${REFERENCE.ENV_VAR_START}${varName}${REFERENCE.ENV_VAR_END}`
}
export function isReference(value: string): boolean {
return value.startsWith(REFERENCE.START) && value.endsWith(REFERENCE.END)
}
export function isEnvVarReference(value: string): boolean {
return value.startsWith(REFERENCE.ENV_VAR_START) && value.endsWith(REFERENCE.ENV_VAR_END)
}
export function extractEnvVarName(reference: string): string {
return reference.substring(
REFERENCE.ENV_VAR_START.length,
reference.length - REFERENCE.ENV_VAR_END.length
)
}
export function extractReferenceContent(reference: string): string {
return reference.substring(REFERENCE.START.length, reference.length - REFERENCE.END.length)
}
export function parseReferencePath(reference: string): string[] {
const content = extractReferenceContent(reference)
return content.split(REFERENCE.PATH_DELIMITER)
}
export const PATTERNS = {
UUID: /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i,
UUID_V4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
UUID_PREFIX: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i,
ENV_VAR_NAME: /^[A-Za-z_][A-Za-z0-9_]*$/,
} as const
export function isUuid(value: string): boolean {
return PATTERNS.UUID.test(value)
}
export function isUuidV4(value: string): boolean {
return PATTERNS.UUID_V4.test(value)
}
export function startsWithUuid(value: string): boolean {
return PATTERNS.UUID_PREFIX.test(value)
}
export function isValidEnvVarName(name: string): boolean {
return PATTERNS.ENV_VAR_NAME.test(name)
}
export function sanitizeFileName(fileName: string): string {
return fileName.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9.-]/g, '_')
}
export function isCustomTool(toolId: string): boolean {
return toolId.startsWith(AGENT.CUSTOM_TOOL_PREFIX)
}
export function isMcpTool(toolId: string): boolean {
return toolId.startsWith(MCP.TOOL_PREFIX)
}
export function stripCustomToolPrefix(name: string): string {
return name.startsWith(AGENT.CUSTOM_TOOL_PREFIX)
? name.slice(AGENT.CUSTOM_TOOL_PREFIX.length)
: name
}
export function stripMcpToolPrefix(name: string): string {
return name.startsWith(MCP.TOOL_PREFIX) ? name.slice(MCP.TOOL_PREFIX.length) : name
}
export function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/**
* Normalizes a name for comparison by converting to lowercase and removing spaces.
* Used for both block names and variable names to ensure consistent matching.
*/
export function normalizeName(name: string): string {
return name.toLowerCase().replace(/\s+/g, '')
}