-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathindex.ts
More file actions
560 lines (489 loc) · 17.7 KB
/
index.ts
File metadata and controls
560 lines (489 loc) · 17.7 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import { z } from 'zod/v3';
import { AgentBaseTool, ToolCallResult } from '../base';
import {
workflowPlanSchema,
normalizeWorkflowPlan,
parseWorkflowPlan,
workflowPlanPatchSchema,
parseWorkflowPlanPatch,
} from '@refly/canvas-common';
import { ReflyService } from '../builtin/interface';
import {
User,
WorkflowPlanRecord,
WorkflowPatchOperation,
CanvasNode,
} from '@refly/openapi-schema';
import { RunnableConfig } from '@langchain/core/runnables';
import { truncateContent } from '@refly/utils/token';
interface CopilotToolParams {
user: User;
reflyService: ReflyService;
}
// ============================================================================
// Canvas Drift Detection
// ============================================================================
interface CanvasDrift {
hasDrift: boolean;
planTaskCount: number;
canvasWorkflowNodeCount: number;
missingFromCanvas: Array<{ taskId: string; title: string }>;
addedOnCanvas: Array<{ nodeId: string; title: string; taskId?: string }>;
summary: string;
}
function computeCanvasDrift(plan: WorkflowPlanRecord, canvasNodes: CanvasNode[]): CanvasDrift {
const planTaskIds = new Set((plan.tasks ?? []).map((t) => t.id));
const canvasTaskIds = new Map<string, CanvasNode>();
const canvasWorkflowNodes: CanvasNode[] = [];
for (const node of canvasNodes) {
if (node.type === 'skillResponse') {
canvasWorkflowNodes.push(node);
const taskId = (node.data?.metadata as Record<string, unknown>)?.taskId as string | undefined;
if (taskId) {
canvasTaskIds.set(taskId, node);
}
}
}
const missingFromCanvas = (plan.tasks ?? [])
.filter((t) => !canvasTaskIds.has(t.id))
.map((t) => ({ taskId: t.id, title: t.title }));
const addedOnCanvas = canvasWorkflowNodes
.filter((n) => {
const taskId = (n.data?.metadata as Record<string, unknown>)?.taskId as string | undefined;
return !taskId || !planTaskIds.has(taskId);
})
.map((n) => ({
nodeId: n.id,
title: n.data?.title ?? '',
taskId: (n.data?.metadata as Record<string, unknown>)?.taskId as string | undefined,
}));
const hasDrift = missingFromCanvas.length > 0 || addedOnCanvas.length > 0;
let summary = 'Plan and canvas are in sync.';
if (hasDrift) {
const parts: string[] = [];
if (missingFromCanvas.length > 0) {
parts.push(`${missingFromCanvas.length} plan task(s) removed from canvas`);
}
if (addedOnCanvas.length > 0) {
parts.push(`${addedOnCanvas.length} node(s) added to canvas outside plan`);
}
summary = `Drift detected: ${parts.join('; ')}.`;
}
return {
hasDrift,
planTaskCount: plan.tasks?.length ?? 0,
canvasWorkflowNodeCount: canvasWorkflowNodes.length,
missingFromCanvas,
addedOnCanvas,
summary,
};
}
export class GenerateWorkflow extends AgentBaseTool<CopilotToolParams> {
name = 'generate_workflow';
toolsetKey = 'copilot';
schema = workflowPlanSchema;
description = 'Generate a complete workflow plan from scratch';
protected params: CopilotToolParams;
constructor(params: CopilotToolParams) {
super(params);
this.params = params;
}
async _call(
input: z.infer<typeof this.schema>,
_: unknown,
config: RunnableConfig,
): Promise<ToolCallResult> {
const parsed = parseWorkflowPlan(input);
if (!parsed.success) {
return {
status: 'error',
data: { error: parsed.error },
summary: 'Invalid workflow plan input',
};
}
try {
const { reflyService, user } = this.params;
const { copilotSessionId, resultId, version: resultVersion } = config.configurable ?? {};
if (!copilotSessionId || !resultId || typeof resultVersion !== 'number') {
return {
status: 'error',
data: {
error: `Missing required session context: copilotSessionId=${copilotSessionId}, resultId=${resultId}, resultVersion=${resultVersion}`,
},
summary: 'Missing session context for generating workflow plan',
};
}
const result = await reflyService.generateWorkflowPlan(user, {
data: normalizeWorkflowPlan(parsed.data!),
copilotSessionId,
resultId,
resultVersion,
});
return {
status: 'success',
data: {
planId: result.planId,
version: result.version,
},
summary: `Successfully generated workflow plan with ID: ${result.planId} and version: ${result.version}`,
};
} catch (e) {
return {
status: 'error',
data: { error: (e as Error)?.message },
summary: 'Failed to generate workflow plan',
};
}
}
}
export class PatchWorkflow extends AgentBaseTool<CopilotToolParams> {
name = 'patch_workflow';
toolsetKey = 'copilot';
schema = workflowPlanPatchSchema;
description = `Modify an existing workflow plan using semantic patch operations.
Operations (op field):
- updateTitle: { op, title }
- createTask: { op, task: { id, title, prompt, toolsets, dependentTasks? } }
- updateTask: { op, taskId, data: { title?, prompt?, toolsets?, dependentTasks? } }
- deleteTask: { op, taskId }
- createVariable: { op, variable: { variableId, variableType, name, description, value, required?, resourceTypes?, options?, isSingle? } }
- updateVariable: { op, variableId, data: { name?, description?, value?, required?, variableType?, options?, isSingle?, ... } }
- deleteVariable: { op, variableId }
Variable types:
- string: text input
- resource: file upload (use resourceTypes to specify accepted types)
- option: selection from predefined choices (use options array and isSingle boolean)
Notes:
- planId is optional; if omitted, patches the latest plan in the current session
- Operations are applied in order
- For updates, only include fields that need to change`;
protected params: CopilotToolParams;
constructor(params: CopilotToolParams) {
super(params);
this.params = params;
}
async _call(
input: z.infer<typeof this.schema>,
_: unknown,
config: RunnableConfig,
): Promise<ToolCallResult> {
try {
const { reflyService, user } = this.params;
// Validate the patch input
const parsed = parseWorkflowPlanPatch(input);
if (!parsed.success) {
return {
status: 'error',
data: { error: parsed.error },
summary: 'Invalid workflow plan patch input',
};
}
let planId = input.planId;
if (!planId) {
const copilotSessionId = config.configurable?.copilotSessionId;
let latestPlan: WorkflowPlanRecord | null = null;
// Try current session first
if (copilotSessionId) {
latestPlan = await reflyService.getLatestWorkflowPlan(user, { copilotSessionId });
}
// Fallback: find plan from any previous session on the same canvas
if (!latestPlan) {
const canvasId = config.configurable?.canvasId;
if (canvasId) {
latestPlan = await reflyService.getLatestWorkflowPlanByCanvas(user, {
canvasId,
});
}
}
if (!latestPlan) {
return {
status: 'error',
data: { error: 'No existing workflow plan found for this canvas' },
summary: 'Workflow plan not found',
};
}
planId = latestPlan.planId;
}
// Drift pre-check: ensure targeted tasks still exist on canvas
const canvasId = config.configurable?.canvasId;
if (canvasId) {
try {
const canvasData = await reflyService.getCanvasData(user, { canvasId });
const canvasTaskIds = new Set<string>();
for (const node of canvasData.nodes ?? []) {
if (node.type === 'skillResponse') {
const tid = (node.data?.metadata as Record<string, unknown>)?.taskId as
| string
| undefined;
if (tid) canvasTaskIds.add(tid);
}
}
const taskOps = input.operations.filter(
(op) => (op.op === 'updateTask' || op.op === 'deleteTask') && op.taskId,
);
const staleTaskOps = taskOps.filter((op) => !canvasTaskIds.has(op.taskId!));
if (staleTaskOps.length > 0) {
return {
status: 'error',
data: {
error: `Cannot patch: ${staleTaskOps.length} targeted task(s) no longer exist on canvas. Missing task IDs: ${staleTaskOps.map((op) => op.taskId).join(', ')}. The canvas may have been manually modified. Call get_workflow_summary to see drift details, or use generate_workflow to recreate.`,
},
summary: 'Patch failed due to canvas drift',
};
}
} catch {
// Non-critical: proceed with patch even if drift check fails
}
}
const { resultId, version: resultVersion } = config.configurable ?? {};
if (!resultId || typeof resultVersion !== 'number') {
return {
status: 'error',
data: {
error: `Missing required session context: resultId=${resultId}, resultVersion=${resultVersion}`,
},
summary: 'Missing session context for patching workflow plan',
};
}
const result = await reflyService.patchWorkflowPlan(user, {
planId: planId!,
operations: input.operations as WorkflowPatchOperation[],
resultId,
resultVersion,
});
return {
status: 'success',
data: {
planId: result.planId,
version: result.version,
},
summary: `Successfully patched workflow plan with ID: ${result.planId} and created new version: ${result.version}. Applied ${input.operations.length} operation(s).`,
};
} catch (e) {
return {
status: 'error',
data: { error: (e as Error)?.message ?? String(e) },
summary: 'Failed to patch workflow plan',
};
}
}
}
// Schema for get_workflow_summary tool (no input needed, uses session context)
const getWorkflowSummarySchema = z.object({
planId: z
.string()
.optional()
.describe('Optional plan ID. If not provided, retrieves the latest plan in current session.'),
});
export class GetWorkflowSummary extends AgentBaseTool<CopilotToolParams> {
name = 'get_workflow_summary';
toolsetKey = 'copilot';
schema = getWorkflowSummarySchema;
description = `Retrieve the current workflow plan structure.
Use this tool when you need to:
- Recall current tasks and variables before making modifications
- Verify task/variable IDs for accurate patch operations
- Understand the workflow structure after multiple conversation turns`;
protected params: CopilotToolParams;
constructor(params: CopilotToolParams) {
super(params);
this.params = params;
}
async _call(
input: z.infer<typeof this.schema>,
_: unknown,
config: RunnableConfig,
): Promise<ToolCallResult> {
try {
const { reflyService, user } = this.params;
const { planId } = input;
let plan: WorkflowPlanRecord | null = null;
if (planId) {
plan = await reflyService.getWorkflowPlanById(user, { planId });
if (!plan) {
return {
status: 'error',
data: { error: `Workflow plan with ID ${planId} not found` },
summary: 'Workflow plan not found',
};
}
} else {
// Try current session first
const copilotSessionId = config.configurable?.copilotSessionId;
if (copilotSessionId) {
plan = await reflyService.getLatestWorkflowPlan(user, { copilotSessionId });
}
// Fallback: find plan from any previous session on the same canvas
if (!plan) {
const canvasId = config.configurable?.canvasId;
if (canvasId) {
plan = await reflyService.getLatestWorkflowPlanByCanvas(user, { canvasId });
}
}
}
if (!plan) {
return {
status: 'success',
data: { exists: false },
summary: 'No workflow plan exists in the current session yet.',
};
}
// Drift detection: compare plan tasks with actual canvas nodes
const canvasId = config.configurable?.canvasId;
let canvasDrift: CanvasDrift | undefined;
if (canvasId) {
try {
const canvasData = await this.params.reflyService.getCanvasData(user, { canvasId });
canvasDrift = computeCanvasDrift(plan, canvasData.nodes ?? []);
} catch {
// Non-critical: proceed without drift info
}
}
return {
status: 'success',
data: {
planId: plan.planId,
version: plan.version,
title: plan.title,
taskCount: plan.tasks?.length ?? 0,
variableCount: plan.variables?.length ?? 0,
tasks: plan.tasks?.map((t) => ({
id: t.id,
title: t.title,
dependentTasks: t.dependentTasks,
toolsets: t.toolsets,
prompt: truncateContent(t.prompt, 100),
})),
variables: plan.variables?.map((v) => ({
variableId: v.variableId,
name: v.name,
variableType: v.variableType,
required: v.required,
})),
...(canvasDrift && {
canvasDrift: {
hasDrift: canvasDrift.hasDrift,
summary: canvasDrift.summary,
...(canvasDrift.hasDrift && {
missingFromCanvas: canvasDrift.missingFromCanvas,
addedOnCanvas: canvasDrift.addedOnCanvas,
}),
},
}),
},
summary: canvasDrift?.hasDrift
? `Retrieved workflow plan (${plan.planId} v${plan.version}). WARNING: ${canvasDrift.summary}`
: `Successfully retrieved workflow plan summary for plan ID: ${plan.planId} and version: ${plan.version}`,
};
} catch (e) {
return {
status: 'error',
data: { error: (e as Error)?.message ?? String(e) },
summary: 'Failed to retrieve workflow plan summary',
};
}
}
}
// Schema for get_canvas_snapshot tool (no input needed, uses canvas context)
const getCanvasSnapshotSchema = z.object({});
const MAX_SNAPSHOT_NODES = 50;
const MAX_SNAPSHOT_EDGES = 100;
const MAX_PREVIEW_LENGTH = 80;
export class GetCanvasSnapshot extends AgentBaseTool<CopilotToolParams> {
name = 'get_canvas_snapshot';
toolsetKey = 'copilot';
schema = getCanvasSnapshotSchema;
description = `Retrieve the current canvas state including all nodes and edges.
Use this tool when you need to:
- See what nodes and edges are currently on the canvas
- Understand the canvas layout before generating or modifying workflows
- Answer user questions about the current canvas content`;
protected params: CopilotToolParams;
constructor(params: CopilotToolParams) {
super(params);
this.params = params;
}
async _call(
_input: z.infer<typeof this.schema>,
_: unknown,
config: RunnableConfig,
): Promise<ToolCallResult> {
try {
const { reflyService, user } = this.params;
const canvasId = config.configurable?.canvasId;
if (!canvasId) {
return {
status: 'error',
data: { error: 'canvasId is required to retrieve canvas snapshot' },
summary: 'Missing canvas context',
};
}
const canvasData = await reflyService.getCanvasData(user, { canvasId });
const allNodes: CanvasNode[] = canvasData.nodes ?? [];
const allEdges = canvasData.edges ?? [];
// Build node type counts
const nodeTypeCounts: Record<string, number> = {};
for (const node of allNodes) {
const t = node.type ?? 'unknown';
nodeTypeCounts[t] = (nodeTypeCounts[t] ?? 0) + 1;
}
// Summarize nodes: strip position/style noise, keep key metadata for skillResponse
const truncated = allNodes.length > MAX_SNAPSHOT_NODES;
const nodesToReturn = allNodes.slice(0, MAX_SNAPSHOT_NODES).map((node) => {
const metadata = (node.data as any)?.metadata;
const base: Record<string, any> = {
id: node.id,
type: node.type,
title: node.data?.title,
entityId: node.data?.entityId,
contentPreview: node.data?.contentPreview
? truncateContent(node.data.contentPreview, MAX_PREVIEW_LENGTH)
: undefined,
};
// For skillResponse nodes, include query and toolsets so Agent can understand the workflow
if (node.type === 'skillResponse' && metadata) {
if (metadata.query) {
base.query = truncateContent(metadata.query, 200);
}
if (metadata.selectedToolsets?.length) {
base.toolsets = metadata.selectedToolsets
.map((t: any) => t.id ?? t.toolset?.key)
.filter(Boolean);
}
if (metadata.taskId) {
base.taskId = metadata.taskId;
}
}
return base;
});
// Summarize edges
const edgesTruncated = allEdges.length > MAX_SNAPSHOT_EDGES;
const edgesToReturn = allEdges.slice(0, MAX_SNAPSHOT_EDGES).map((edge) => ({
id: edge.id,
source: edge.source,
target: edge.target,
}));
return {
status: 'success',
data: {
title: canvasData.title,
nodeCount: allNodes.length,
edgeCount: allEdges.length,
nodeTypeCounts,
nodes: nodesToReturn,
edges: edgesToReturn,
variables: canvasData.variables,
truncated,
edgesTruncated,
},
summary: `Canvas "${canvasData.title}" has ${allNodes.length} node(s) and ${allEdges.length} edge(s).`,
};
} catch (e) {
return {
status: 'error',
data: { error: (e as Error)?.message ?? String(e) },
summary: 'Failed to retrieve canvas snapshot',
};
}
}
}