@eko-ai/eko 1.0.8 → 1.0.9

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/dist/index.esm.js CHANGED
@@ -1,11 +1,216 @@
1
+ /**
2
+ * Manages logging for action execution, providing a cleaner view of the execution
3
+ * flow while maintaining important context and history.
4
+ */
5
+ class ExecutionLogger {
6
+ constructor(options = {}) {
7
+ var _a;
8
+ this.history = [];
9
+ this.maxHistoryLength = options.maxHistoryLength || 10;
10
+ this.logLevel = options.logLevel || 'info';
11
+ this.includeTimestamp = (_a = options.includeTimestamp) !== null && _a !== void 0 ? _a : true;
12
+ this.debugImagePath = options.debugImagePath;
13
+ this.imageSaver = options.imageSaver;
14
+ // Check if running in Node.js environment
15
+ this.isNode =
16
+ typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
17
+ }
18
+ /**
19
+ * Logs a message with execution context
20
+ */
21
+ log(level, message, context) {
22
+ if (this.shouldLog(level)) {
23
+ const timestamp = this.includeTimestamp ? new Date().toISOString() : '';
24
+ const contextSummary = this.summarizeContext(context);
25
+ console.log(`${timestamp} [${level.toUpperCase()}] ${message}${contextSummary}`);
26
+ }
27
+ }
28
+ /**
29
+ * Updates conversation history while maintaining size limit
30
+ */
31
+ updateHistory(messages) {
32
+ // Keep system messages and last N messages
33
+ const systemMessages = messages.filter((m) => m.role === 'system');
34
+ const nonSystemMessages = messages.filter((m) => m.role !== 'system');
35
+ const recentMessages = nonSystemMessages.slice(-this.maxHistoryLength);
36
+ this.history = [...systemMessages, ...recentMessages];
37
+ }
38
+ /**
39
+ * Gets current conversation history
40
+ */
41
+ getHistory() {
42
+ return this.history;
43
+ }
44
+ /**
45
+ * Summarizes the execution context for logging
46
+ */
47
+ summarizeContext(context) {
48
+ if (!context)
49
+ return '';
50
+ const summary = {
51
+ variables: Object.fromEntries(context.variables),
52
+ tools: context.tools ? Array.from(context.tools.keys()) : [],
53
+ };
54
+ return `\nContext: ${JSON.stringify(summary, null, 2)}`;
55
+ }
56
+ /**
57
+ * Checks if message should be logged based on log level
58
+ */
59
+ shouldLog(level) {
60
+ const levels = {
61
+ error: 0,
62
+ warn: 1,
63
+ info: 2,
64
+ debug: 3,
65
+ };
66
+ return levels[level] <= levels[this.logLevel];
67
+ }
68
+ /**
69
+ * Logs the start of an action execution
70
+ */
71
+ logActionStart(actionName, input, context) {
72
+ this.log('info', `Starting action: ${actionName}`, context);
73
+ this.log('info', `Input: ${JSON.stringify(input, null, 2)}`);
74
+ }
75
+ /**
76
+ * Logs the completion of an action execution
77
+ */
78
+ logActionComplete(actionName, result, context) {
79
+ this.log('info', `Completed action: ${actionName}`, context);
80
+ this.log('info', `Result: ${JSON.stringify(result, null, 2)}`);
81
+ }
82
+ /**
83
+ * Logs a tool execution
84
+ */
85
+ logToolExecution(toolName, input, context) {
86
+ this.log('info', `Executing tool: ${toolName}`);
87
+ this.log('info', `Tool input: ${JSON.stringify(input, null, 2)}`);
88
+ }
89
+ /**
90
+ * Logs an error that occurred during execution
91
+ */
92
+ logError(error, context) {
93
+ this.log('error', `Error occurred: ${error.message}`, context);
94
+ if (error.stack) {
95
+ this.log('debug', `Stack trace: ${error.stack}`);
96
+ }
97
+ }
98
+ extractFromDataUrl(dataUrl) {
99
+ const matches = dataUrl.match(/^data:image\/([a-zA-Z0-9]+);base64,(.+)$/);
100
+ if (!matches) {
101
+ throw new Error('Invalid data URL format');
102
+ }
103
+ return {
104
+ extension: matches[1],
105
+ base64Data: matches[2],
106
+ };
107
+ }
108
+ async saveDebugImage(imageData, toolName) {
109
+ try {
110
+ let extension;
111
+ let base64Data;
112
+ // Handle both data URL strings and ImageData objects
113
+ if (typeof imageData === 'string' && imageData.startsWith('data:')) {
114
+ const extracted = this.extractFromDataUrl(imageData);
115
+ extension = extracted.extension;
116
+ base64Data = extracted.base64Data;
117
+ }
118
+ else if (typeof imageData === 'object' && 'type' in imageData) {
119
+ extension = imageData.media_type.split('/')[1] || 'png';
120
+ base64Data = imageData.data;
121
+ }
122
+ else {
123
+ return '[image]';
124
+ }
125
+ // If custom image saver is provided, use it
126
+ if (this.imageSaver) {
127
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
128
+ const filename = `${toolName}_${timestamp}.${extension}`;
129
+ return await this.imageSaver({ type: 'base64', media_type: `image/${extension}`, data: base64Data }, filename);
130
+ }
131
+ // If in Node.js environment and debugImagePath is set
132
+ if (this.isNode && this.debugImagePath) {
133
+ // Dynamically import Node.js modules only when needed
134
+ const { promises: fs } = await import('fs');
135
+ const { join } = await import('path');
136
+ await fs.mkdir(this.debugImagePath, { recursive: true });
137
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
138
+ const filename = `${toolName}_${timestamp}.${extension}`;
139
+ const filepath = join(this.debugImagePath, filename);
140
+ const buffer = Buffer.from(base64Data, 'base64');
141
+ await fs.writeFile(filepath, buffer);
142
+ return `[image saved to: ${filepath}]`;
143
+ }
144
+ // Default case - just return placeholder
145
+ return '[image]';
146
+ }
147
+ catch (error) {
148
+ console.warn('Failed to save debug image:', error);
149
+ return '[image]';
150
+ }
151
+ }
152
+ async formatToolResult(result) {
153
+ // Handle null/undefined
154
+ if (result == null) {
155
+ return 'null';
156
+ }
157
+ // Handle direct image result
158
+ if (result.image) {
159
+ const imagePlaceholder = await this.saveDebugImage(result.image, 'tool');
160
+ const modifiedResult = { ...result, image: imagePlaceholder };
161
+ return JSON.stringify(modifiedResult);
162
+ }
163
+ // Handle nested images in result object
164
+ if (typeof result === 'object') {
165
+ const formatted = { ...result };
166
+ for (const [key, value] of Object.entries(formatted)) {
167
+ if (value && typeof value === 'string' && value.startsWith('data:image/')) {
168
+ formatted[key] = await this.saveDebugImage(value, key);
169
+ }
170
+ else if (value &&
171
+ typeof value === 'object' &&
172
+ 'type' in value &&
173
+ value.type === 'base64') {
174
+ formatted[key] = await this.saveDebugImage(value, key);
175
+ }
176
+ }
177
+ return JSON.stringify(formatted);
178
+ }
179
+ // Handle primitive values
180
+ return String(result);
181
+ }
182
+ async logToolResult(toolName, result, context) {
183
+ if (this.shouldLog('info')) {
184
+ const timestamp = this.includeTimestamp ? new Date().toISOString() : '';
185
+ const contextSummary = this.summarizeContext(context);
186
+ const formattedResult = await this.formatToolResult(result);
187
+ console.log(`${timestamp} [INFO] Tool executed: ${toolName}\n` +
188
+ `${timestamp} [INFO] Tool result: ${formattedResult}${contextSummary}`);
189
+ }
190
+ }
191
+ }
192
+
1
193
  class WorkflowImpl {
2
- constructor(id, name, description, nodes = [], variables = new Map(), llmProvider) {
194
+ constructor(id, name, description, nodes = [], variables = new Map(), llmProvider, loggerOptions) {
3
195
  this.id = id;
4
196
  this.name = name;
5
197
  this.description = description;
6
198
  this.nodes = nodes;
7
199
  this.variables = variables;
8
200
  this.llmProvider = llmProvider;
201
+ this.abortControllers = new Map();
202
+ if (loggerOptions) {
203
+ this.logger = new ExecutionLogger(loggerOptions);
204
+ }
205
+ }
206
+ setLogger(logger) {
207
+ this.logger = logger;
208
+ }
209
+ async cancel() {
210
+ this.abort = true;
211
+ for (const controller of this.abortControllers.values()) {
212
+ controller.abort("Workflow cancelled");
213
+ }
9
214
  }
10
215
  async execute(callback) {
11
216
  var _a, _b, _c, _d;
@@ -28,37 +233,49 @@ class WorkflowImpl {
28
233
  throw new Error(`Circular dependency detected at node: ${nodeId}`);
29
234
  }
30
235
  const node = this.getNode(nodeId);
236
+ const abortController = new AbortController();
237
+ this.abortControllers.set(nodeId, abortController);
31
238
  // Execute the node's action
32
239
  const context = {
33
240
  __skip: false,
34
241
  __abort: false,
242
+ workflow: this,
35
243
  variables: this.variables,
36
244
  llmProvider: this.llmProvider,
37
245
  tools: new Map(node.action.tools.map(tool => [tool.name, tool])),
38
246
  callback,
247
+ logger: this.logger,
39
248
  next: () => context.__skip = true,
40
- abortAll: () => this.abort = context.__abort = true,
249
+ abortAll: () => {
250
+ this.abort = context.__abort = true;
251
+ // Abort all running tasks
252
+ for (const controller of this.abortControllers.values()) {
253
+ controller.abort("Workflow cancelled");
254
+ }
255
+ },
256
+ signal: abortController.signal
41
257
  };
42
- callback && await ((_b = (_a = callback.hooks).beforeSubtask) === null || _b === void 0 ? void 0 : _b.call(_a, node, context));
43
- if (context.__abort) {
44
- throw new Error("Abort");
45
- }
46
- else if (context.__skip) {
47
- return;
48
- }
49
258
  executing.add(nodeId);
50
259
  // Execute dependencies first
51
260
  for (const depId of node.dependencies) {
52
261
  await executeNode(depId);
53
262
  }
54
263
  // Prepare input by gathering outputs from dependencies
55
- const input = {};
264
+ const input = { items: [] };
56
265
  for (const depId of node.dependencies) {
57
266
  const depNode = this.getNode(depId);
58
- input[depId] = depNode.output.value;
267
+ input.items.push(depNode.output);
59
268
  }
60
- node.input.value = input;
61
- node.output.value = await node.action.execute(node.input.value, context);
269
+ node.input = input;
270
+ // Run pre-execution hooks and execute action
271
+ callback && await ((_b = (_a = callback.hooks).beforeSubtask) === null || _b === void 0 ? void 0 : _b.call(_a, node, context));
272
+ if (context.__abort) {
273
+ throw new Error("Abort");
274
+ }
275
+ else if (context.__skip) {
276
+ return;
277
+ }
278
+ node.output.value = await node.action.execute(node.input, node.output, context);
62
279
  executing.delete(nodeId);
63
280
  executed.add(nodeId);
64
281
  callback && await ((_d = (_c = callback.hooks).afterSubtask) === null || _d === void 0 ? void 0 : _d.call(_c, node, context, (_e = node.output) === null || _e === void 0 ? void 0 : _e.value));
@@ -67,6 +284,7 @@ class WorkflowImpl {
67
284
  const terminalNodes = this.nodes.filter(node => !this.nodes.some(n => n.dependencies.includes(node.id)));
68
285
  await Promise.all(terminalNodes.map(node => executeNode(node.id)));
69
286
  callback && await ((_d = (_c = callback.hooks).afterWorkflow) === null || _d === void 0 ? void 0 : _d.call(_c, this, this.variables));
287
+ return terminalNodes.map(node => node.output);
70
288
  }
71
289
  addNode(node) {
72
290
  if (this.nodes.some(n => n.id === node.id)) {
@@ -125,7 +343,7 @@ class WorkflowImpl {
125
343
  class WriteContextTool {
126
344
  constructor() {
127
345
  this.name = 'write_context';
128
- this.description = 'Write a value to the workflow context. Use this to store intermediate results or outputs.';
346
+ this.description = 'Write a value to the global workflow context. Use this to store important intermediate results, but only when a piece of information is essential for future reference but missing from the final output specification of the current action.';
129
347
  this.input_schema = {
130
348
  type: 'object',
131
349
  properties: {
@@ -155,25 +373,32 @@ class WriteContextTool {
155
373
  return { success: true, key, value };
156
374
  }
157
375
  }
158
- function createReturnTool(outputSchema) {
376
+ function createReturnTool(actionName, outputDescription, outputSchema) {
159
377
  return {
160
378
  name: 'return_output',
161
- description: 'Return the final output of this action. Use this to return a value matching the required output schema.',
379
+ description: `Return the final output of this action. Use this to return a value matching the required output schema (if specified) and the following description:
380
+ ${outputDescription}
381
+
382
+ You can either set 'use_tool_result=true' to return the result of a previous tool call, or explicitly specify 'value' with 'use_tool_result=false' to return a value according to your own understanding. Whenever possible, reuse tool results to avoid redundancy.
383
+ `,
162
384
  input_schema: {
163
385
  type: 'object',
164
386
  properties: {
387
+ use_tool_result: {
388
+ type: ['boolean'],
389
+ description: `Whether to use the latest tool result as output. When set to true, the 'value' parameter is ignored.`,
390
+ },
165
391
  value: outputSchema || {
166
392
  // Default to accepting any JSON value
167
393
  type: ['string', 'number', 'boolean', 'object', 'null'],
168
- description: 'The output value',
394
+ description: 'The output value. Only provide a value if the previous tool result is not suitable for the output description. Otherwise, leave this as null.',
169
395
  },
170
396
  },
171
- required: ['value'],
397
+ required: ['use_tool_result', 'value'],
172
398
  },
173
399
  async execute(context, params) {
174
- const { value } = params;
175
- context.variables.set('__action_output', value);
176
- return { returned: value };
400
+ context.variables.set(`__action_${actionName}_output`, params);
401
+ return { success: true };
177
402
  },
178
403
  };
179
404
  }
@@ -187,6 +412,8 @@ class ActionImpl {
187
412
  this.llmProvider = llmProvider;
188
413
  this.llmConfig = llmConfig;
189
414
  this.maxRounds = 10; // Default max rounds
415
+ this.toolResults = new Map();
416
+ this.logger = new ExecutionLogger();
190
417
  this.writeContextTool = new WriteContextTool();
191
418
  this.tools = [...tools, this.writeContextTool];
192
419
  if (config === null || config === void 0 ? void 0 : config.maxRounds) {
@@ -194,6 +421,7 @@ class ActionImpl {
194
421
  }
195
422
  }
196
423
  async executeSingleRound(messages, params, toolMap, context) {
424
+ this.logger = context.logger;
197
425
  const roundMessages = [];
198
426
  let hasToolUse = false;
199
427
  let response = null;
@@ -203,6 +431,12 @@ class ActionImpl {
203
431
  let toolResultMessage = null;
204
432
  // Track tool execution promise
205
433
  let toolExecutionPromise = null;
434
+ // Listen for abort signal
435
+ if (context.signal) {
436
+ context.signal.addEventListener('abort', () => {
437
+ context.__abort = true;
438
+ });
439
+ }
206
440
  const handler = {
207
441
  onContent: (content) => {
208
442
  if (content.trim()) {
@@ -210,7 +444,8 @@ class ActionImpl {
210
444
  }
211
445
  },
212
446
  onToolUse: async (toolCall) => {
213
- console.log('Tool Call:', toolCall.name, toolCall.input);
447
+ this.logger.log('info', `Assistant: ${assistantTextMessage}`);
448
+ this.logger.logToolExecution(toolCall.name, toolCall.input, context);
214
449
  hasToolUse = true;
215
450
  const tool = toolMap.get(toolCall.name);
216
451
  if (!tool) {
@@ -229,6 +464,7 @@ class ActionImpl {
229
464
  };
230
465
  // Store the promise of tool execution
231
466
  toolExecutionPromise = (async () => {
467
+ var _a;
232
468
  try {
233
469
  // beforeToolUse
234
470
  context.__skip = false;
@@ -238,7 +474,7 @@ class ActionImpl {
238
474
  toolCall.input = modified_input;
239
475
  }
240
476
  }
241
- if (context.__skip || context.__abort) {
477
+ if (context.__skip || context.__abort || ((_a = context.signal) === null || _a === void 0 ? void 0 : _a.aborted)) {
242
478
  toolResultMessage = {
243
479
  role: 'user',
244
480
  content: [
@@ -260,31 +496,42 @@ class ActionImpl {
260
496
  result = modified_result;
261
497
  }
262
498
  }
499
+ const result_has_image = result && "image" in result;
500
+ const resultContent = result_has_image
501
+ ? {
502
+ type: 'tool_result',
503
+ tool_use_id: toolCall.id,
504
+ content: result.text
505
+ ? [
506
+ { type: 'image', source: result.image },
507
+ { type: 'text', text: result.text },
508
+ ]
509
+ : [{ type: 'image', source: result.image }],
510
+ }
511
+ : {
512
+ type: 'tool_result',
513
+ tool_use_id: toolCall.id,
514
+ content: [{ type: 'text', text: JSON.stringify(result) }],
515
+ };
516
+ const resultContentText = result_has_image
517
+ ? result.text
518
+ ? result.text + ' [Image]'
519
+ : '[Image]'
520
+ : JSON.stringify(result);
263
521
  const resultMessage = {
264
522
  role: 'user',
265
- content: [
266
- result.image && result.image.type
267
- ? {
268
- type: 'tool_result',
269
- tool_use_id: toolCall.id,
270
- content: result.text
271
- ? [
272
- { type: 'image', source: result.image },
273
- { type: 'text', text: result.text },
274
- ]
275
- : [{ type: 'image', source: result.image }],
276
- }
277
- : {
278
- type: 'tool_result',
279
- tool_use_id: toolCall.id,
280
- content: [{ type: 'text', text: JSON.stringify(result) }],
281
- },
282
- ],
523
+ content: [resultContent],
283
524
  };
284
525
  toolResultMessage = resultMessage;
285
- console.log('Tool Result:', result);
526
+ this.logger.logToolResult(tool.name, result, context);
527
+ // Store tool results except for the return_output tool
528
+ if (tool.name !== 'return_output') {
529
+ this.toolResults.set(toolCall.id, resultContentText);
530
+ }
286
531
  }
287
532
  catch (err) {
533
+ console.log("An error occurred when calling tool:");
534
+ console.log(err);
288
535
  const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
289
536
  const errorResult = {
290
537
  role: 'user',
@@ -298,7 +545,7 @@ class ActionImpl {
298
545
  ],
299
546
  };
300
547
  toolResultMessage = errorResult;
301
- console.error('Tool Error:', err);
548
+ this.logger.logError(err, context);
302
549
  }
303
550
  })();
304
551
  },
@@ -307,10 +554,14 @@ class ActionImpl {
307
554
  },
308
555
  onError: (error) => {
309
556
  console.error('Stream Error:', error);
557
+ console.log('Last message array sent to LLM:', JSON.stringify(messages, null, 2));
310
558
  },
311
559
  };
312
560
  this.handleHistoryImageMessages(messages);
313
561
  // Wait for stream to complete
562
+ if (!this.llmProvider) {
563
+ throw new Error('LLM provider not set');
564
+ }
314
565
  await this.llmProvider.generateStream(messages, params, handler);
315
566
  // Wait for tool execution to complete if it was started
316
567
  if (toolExecutionPromise) {
@@ -332,40 +583,58 @@ class ActionImpl {
332
583
  return { response, hasToolUse, roundMessages };
333
584
  }
334
585
  handleHistoryImageMessages(messages) {
335
- // Remove all images of the historical tool call results, except for the last one.
336
- let last_user = true;
586
+ // Remove all images from historical tool results except the most recent user message
587
+ const initialImageCount = this.countImages(messages);
588
+ let foundFirstUser = false;
337
589
  for (let i = messages.length - 1; i >= 0; i--) {
338
590
  const message = messages[i];
339
591
  if (message.role === 'user') {
340
- if (last_user) {
341
- last_user = false;
592
+ if (!foundFirstUser) {
593
+ foundFirstUser = true;
342
594
  continue;
343
595
  }
344
- if (message.content instanceof Array) {
345
- let content = message.content;
346
- for (let j = 0; j < content.length; j++) {
347
- if (content[j].type === 'tool_result' && content[j].content instanceof Array) {
348
- let tool_content = content[j].content;
349
- if (tool_content.length > 0) {
350
- for (let k = tool_content.length - 1; k >= 0; k--) {
351
- if (tool_content[k].type === 'image') {
352
- tool_content.splice(k, 1);
353
- }
596
+ if (Array.isArray(message.content)) {
597
+ // Directly modify the message content array
598
+ message.content = message.content.map((item) => {
599
+ if (item.type === 'tool_result' && Array.isArray(item.content)) {
600
+ // Create a new content array without images
601
+ if (item.content.length > 0) {
602
+ item.content = item.content.filter((c) => c.type !== 'image');
603
+ // If all content was images and got filtered out, replace with ok message
604
+ if (item.content.length === 0) {
605
+ item.content = [{ type: 'text', text: 'ok' }];
354
606
  }
355
607
  }
356
- else if (tool_content[0].type === 'image') {
357
- tool_content = [{ type: 'text', text: 'ok' }];
358
- }
359
608
  }
360
- }
609
+ return item;
610
+ });
361
611
  }
362
612
  }
363
613
  }
614
+ const finalImageCount = this.countImages(messages);
615
+ if (initialImageCount !== finalImageCount) {
616
+ this.logger.log("info", `Removed ${initialImageCount - finalImageCount} images from history`);
617
+ }
364
618
  }
365
- async execute(input, context, outputSchema) {
366
- var _a;
619
+ countImages(messages) {
620
+ let count = 0;
621
+ messages.forEach(msg => {
622
+ if (Array.isArray(msg.content)) {
623
+ msg.content.forEach((item) => {
624
+ if (item.type === 'tool_result' && Array.isArray(item.content)) {
625
+ count += item.content.filter((c) => c.type === 'image').length;
626
+ }
627
+ });
628
+ }
629
+ });
630
+ return count;
631
+ }
632
+ async execute(input, output, context, outputSchema) {
633
+ var _a, _b, _c, _d, _e;
634
+ this.logger = context.logger;
635
+ console.log(`Executing action started: ${this.name}`);
367
636
  // Create return tool with output schema
368
- const returnTool = createReturnTool(outputSchema);
637
+ const returnTool = createReturnTool(this.name, output.description, outputSchema);
369
638
  // Create tool map combining context tools, action tools, and return tool
370
639
  const toolMap = new Map();
371
640
  this.tools.forEach((tool) => toolMap.set(tool.name, tool));
@@ -376,9 +645,7 @@ class ActionImpl {
376
645
  { role: 'system', content: this.formatSystemPrompt() },
377
646
  { role: 'user', content: this.formatUserPrompt(context, input) },
378
647
  ];
379
- console.log('Starting LLM conversation...');
380
- console.log('Initial messages:', messages);
381
- console.log('Output schema:', outputSchema);
648
+ this.logger.logActionStart(this.name, input, context);
382
649
  // Configure tool parameters
383
650
  const params = {
384
651
  ...this.llmConfig,
@@ -390,17 +657,24 @@ class ActionImpl {
390
657
  };
391
658
  let roundCount = 0;
392
659
  while (roundCount < this.maxRounds) {
660
+ // Check for abort signal
661
+ if ((_b = context.signal) === null || _b === void 0 ? void 0 : _b.aborted) {
662
+ throw new Error('Workflow cancelled');
663
+ }
393
664
  roundCount++;
394
- console.log(`Starting round ${roundCount} of ${this.maxRounds}`);
395
- console.log('Current conversation status:', JSON.stringify(messages, null, 2));
665
+ this.logger.log('info', `Starting round ${roundCount} of ${this.maxRounds}`, context);
396
666
  const { response, hasToolUse, roundMessages } = await this.executeSingleRound(messages, params, toolMap, context);
667
+ if (response === null || response === void 0 ? void 0 : response.textContent) {
668
+ (_e = (_d = (_c = context.callback) === null || _c === void 0 ? void 0 : _c.hooks) === null || _d === void 0 ? void 0 : _d.onLlmMessage) === null || _e === void 0 ? void 0 : _e.call(_d, response.textContent);
669
+ }
397
670
  // Add round messages to conversation history
398
671
  messages.push(...roundMessages);
672
+ this.logger.log('debug', `Round ${roundCount} messages: ${JSON.stringify(roundMessages)}`, context);
399
673
  // Check termination conditions
400
674
  if (!hasToolUse && response) {
401
675
  // LLM sent a message without using tools - request explicit return
402
- console.log('No tool use detected, requesting explicit return');
403
- console.log('Response:', response);
676
+ this.logger.log('info', `Assistant: ${response.textContent}`);
677
+ this.logger.log('warn', 'LLM sent a message without using tools; requesting explicit return');
404
678
  const returnOnlyParams = {
405
679
  ...params,
406
680
  tools: [
@@ -420,12 +694,11 @@ class ActionImpl {
420
694
  break;
421
695
  }
422
696
  if (response === null || response === void 0 ? void 0 : response.toolCalls.some((call) => call.name === 'return_output')) {
423
- console.log('Task completed with return_output tool');
424
697
  break;
425
698
  }
426
699
  // If this is the last round, force an explicit return
427
700
  if (roundCount === this.maxRounds) {
428
- console.log('Max rounds reached, requesting explicit return');
701
+ this.logger.log('warn', 'Max rounds reached, requesting explicit return');
429
702
  const returnOnlyParams = {
430
703
  ...params,
431
704
  tools: [
@@ -445,33 +718,50 @@ class ActionImpl {
445
718
  }
446
719
  }
447
720
  // Get and clean up output value
448
- const output = context.variables.get('__action_output');
449
- context.variables.delete('__action_output');
450
- if (output === undefined) {
721
+ const outputKey = `__action_${this.name}_output`;
722
+ const outputParams = context.variables.get(outputKey);
723
+ context.variables.delete(outputKey);
724
+ // Get output value, first checking for use_tool_result
725
+ const outputValue = outputParams.use_tool_result
726
+ ? Array.from(this.toolResults.values()).pop()
727
+ : outputParams === null || outputParams === void 0 ? void 0 : outputParams.value;
728
+ if (outputValue === undefined) {
451
729
  console.warn('Action completed without returning a value');
452
730
  return {};
453
731
  }
454
- return output;
732
+ return outputValue;
455
733
  }
456
734
  formatSystemPrompt() {
457
- return `You are a task executor. You need to complete the task specified by the user, using the tools provided. When you need to store results or outputs, use the write_context tool. When you are ready to return the final output, use the return_output tool.
735
+ return `You are a subtask executor. You need to complete the subtask specified by the user, which is a consisting part of the overall task. Help the user by calling the tools provided.
458
736
 
459
737
  Remember to:
460
738
  1. Use tools when needed to accomplish the task
461
- 2. Store important results using write_context, including intermediate and final results
462
- 3. Think step by step about what needs to be done`;
739
+ 2. Think step by step about what needs to be done
740
+ 3. Return the output of the subtask using the 'return_output' tool when you are done; prefer using the 'tool_use_id' parameter to refer to the output of a tool call over providing a long text as the value
741
+ 4. Use the context to store important information for later reference, but use it sparingly: most of the time, the output of the subtask should be sufficient for the next steps
742
+ 5. If there are any unclear points during the task execution, please use the human-related tool to inquire with the user
743
+ 6. If user intervention is required during the task execution, please use the human-related tool to transfer the operation rights to the user
744
+ `;
463
745
  }
464
746
  formatUserPrompt(context, input) {
465
- // Create a description of the current context
466
- const contextDescription = Array.from(context.variables.entries())
747
+ var _a;
748
+ const workflowDescription = ((_a = context.workflow) === null || _a === void 0 ? void 0 : _a.description) || null;
749
+ const actionDescription = `${this.name} -- ${this.description}`;
750
+ const inputDescription = JSON.stringify(input, null, 2) || null;
751
+ const contextVariables = Array.from(context.variables.entries())
467
752
  .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
468
753
  .join('\n');
469
- return `You are executing the action "${this.name}". The specific instructions are: "${this.description}". You have access to the following context:
754
+ return `You are executing a subtask in the workflow. The workflow description is as follows:
755
+ ${workflowDescription}
470
756
 
471
- ${contextDescription || 'No context variables set'}
757
+ The subtask description is as follows:
758
+ ${actionDescription}
472
759
 
473
- You have been provided with the following input:
474
- ${(typeof input === 'string' ? input : JSON.stringify(input, null, 2)) || 'No additional input provided'}
760
+ The input to the subtask is as follows:
761
+ ${inputDescription}
762
+
763
+ There are some variables stored in the context that you can use for reference:
764
+ ${contextVariables}
475
765
  `;
476
766
  }
477
767
  // Static factory method
@@ -501,7 +791,8 @@ Generate a complete workflow that:
501
791
  2. Properly sequences tool usage based on dependencies
502
792
  3. Ensures each action has appropriate input/output schemas, and that the "tools" field in each action is populated with the sufficient subset of all available tools needed to complete the action
503
793
  4. Creates a clear, logical flow to accomplish the user's goal
504
- 5. Includes detailed descriptions for each action, ensuring that the actions, when combined, is a complete solution to the user's problem`;
794
+ 5. Includes detailed descriptions for each action, ensuring that the actions, when combined, is a complete solution to the user's problem
795
+ 6. You should always add a SubTask at the end of the workflow to summarize it, and this SubTask should always call the "summary_workflow" tool. It's dependencies should be all of the SubTasks`;
505
796
  },
506
797
  formatUserPrompt: (requirement) => `Create a workflow for the following requirement: ${requirement}`,
507
798
  modifyUserPrompt: (prompt) => `Modify workflow: ${prompt}`,
@@ -642,6 +933,19 @@ class WorkflowGenerator {
642
933
  ],
643
934
  });
644
935
  const workflowData = response.toolCalls[0].input.workflow;
936
+ // Forcibly add special tools
937
+ const specialTools = [
938
+ "cancel_workflow",
939
+ "human_input_text",
940
+ "human_operate",
941
+ ];
942
+ for (const node of workflowData.nodes) {
943
+ for (const tool of specialTools) {
944
+ if (!node.action.tools.includes(tool)) {
945
+ node.action.tools.push(tool);
946
+ }
947
+ }
948
+ }
645
949
  // Validate all tools exist
646
950
  for (const node of workflowData.nodes) {
647
951
  if (!this.toolRegistry.hasTools(node.action.tools)) {
@@ -652,15 +956,22 @@ class WorkflowGenerator {
652
956
  if (!workflowData.id) {
653
957
  workflowData.id = v4();
654
958
  }
959
+ // debug
960
+ console.log("Debug the workflow...");
961
+ console.log(workflowData);
962
+ console.log("Debug the workflow...Done");
655
963
  return this.createWorkflowFromData(workflowData);
656
964
  }
657
965
  createWorkflowFromData(data) {
658
- const workflow = new WorkflowImpl(data.id, data.name, data.description || '', [], new Map(Object.entries(data.variables || {})), this.llmProvider);
966
+ const workflow = new WorkflowImpl(data.id, data.name, data.description || '', [], new Map(Object.entries(data.variables || {})), this.llmProvider, {
967
+ logLevel: 'info',
968
+ includeTimestamp: true,
969
+ });
659
970
  // Add nodes to workflow
660
971
  if (Array.isArray(data.nodes)) {
661
972
  data.nodes.forEach((nodeData) => {
662
973
  const tools = nodeData.action.tools.map((toolName) => this.toolRegistry.getTool(toolName));
663
- const action = ActionImpl.createPromptAction(nodeData.action.name, nodeData.action.description, tools, this.llmProvider, { maxTokens: 1000 });
974
+ const action = ActionImpl.createPromptAction(nodeData.action.name, nodeData.action.description, tools, this.llmProvider, { maxTokens: 8192 });
664
975
  const node = {
665
976
  id: nodeData.id,
666
977
  name: nodeData.name || nodeData.id,
@@ -3404,8 +3715,13 @@ class ClaudeProvider {
3404
3715
  ...options,
3405
3716
  });
3406
3717
  }
3718
+ else if (param.messages && param.completions) {
3719
+ this.client = param;
3720
+ }
3407
3721
  else {
3408
- this.client = new Anthropic(param);
3722
+ let options = param;
3723
+ options.dangerouslyAllowBrowser = true;
3724
+ this.client = new Anthropic(options);
3409
3725
  }
3410
3726
  }
3411
3727
  processResponse(response) {
@@ -8824,8 +9140,13 @@ class OpenaiProvider {
8824
9140
  ...options,
8825
9141
  });
8826
9142
  }
9143
+ else if (param.chat && param.chat.completions) {
9144
+ this.client = param;
9145
+ }
8827
9146
  else {
8828
- this.client = new OpenAI(param);
9147
+ let options = param;
9148
+ options.dangerouslyAllowBrowser = true;
9149
+ this.client = new OpenAI(options);
8829
9150
  }
8830
9151
  }
8831
9152
  buildParams(messages, params, stream) {
@@ -9136,18 +9457,11 @@ const workflowSchema = {
9136
9457
  type: "array",
9137
9458
  items: { type: "string" },
9138
9459
  },
9139
- input: {
9140
- type: "object",
9141
- properties: {
9142
- type: { type: "string" },
9143
- schema: { type: "object" },
9144
- },
9145
- },
9146
9460
  output: {
9147
9461
  type: "object",
9148
9462
  properties: {
9149
- type: { type: "string" },
9150
- schema: { type: "object" },
9463
+ name: { type: "string" },
9464
+ description: { type: "string" },
9151
9465
  },
9152
9466
  },
9153
9467
  action: {
@@ -9276,8 +9590,27 @@ class Eko {
9276
9590
  return workflow;
9277
9591
  }
9278
9592
  async execute(workflow, callback) {
9593
+ // Inject LLM provider at workflow level
9594
+ workflow.llmProvider = this.llmProvider;
9595
+ // Process each node's action
9596
+ for (const node of workflow.nodes) {
9597
+ if (node.action.type === 'prompt') {
9598
+ // Inject LLM provider
9599
+ node.action.llmProvider = this.llmProvider;
9600
+ // Resolve tools
9601
+ node.action.tools = node.action.tools.map(tool => {
9602
+ if (typeof tool === 'string') {
9603
+ return this.toolRegistry.getTool(tool);
9604
+ }
9605
+ return tool;
9606
+ });
9607
+ }
9608
+ }
9279
9609
  return await workflow.execute(callback);
9280
9610
  }
9611
+ async cancel(workflow) {
9612
+ return await workflow.cancel();
9613
+ }
9281
9614
  async modify(workflow, prompt) {
9282
9615
  const generator = this.workflowGeneratorMap.get(workflow);
9283
9616
  workflow = await generator.modifyWorkflow(prompt);
@@ -9461,31 +9794,30 @@ class WorkflowParser {
9461
9794
  errors,
9462
9795
  };
9463
9796
  }
9464
- /**
9465
- * Convert parsed JSON to runtime Workflow object
9466
- */
9467
9797
  static toRuntime(json) {
9468
9798
  const variables = new Map(Object.entries(json.variables || {}));
9469
- const workflow = new WorkflowImpl(json.id, json.name, json.description, [], variables);
9799
+ const workflow = new WorkflowImpl(json.id, json.name, json.description, [], variables, undefined, {
9800
+ logLevel: 'info',
9801
+ includeTimestamp: true,
9802
+ });
9470
9803
  // Convert nodes
9471
9804
  json.nodes.forEach((nodeJson) => {
9805
+ const action = ActionImpl.createPromptAction(nodeJson.action.name, nodeJson.action.description,
9806
+ // Pass tool names as strings, they'll be resolved at execution time
9807
+ nodeJson.action.tools || [], undefined, // LLM provider will be injected at execution time
9808
+ { maxTokens: 1000 });
9472
9809
  const node = {
9473
9810
  id: nodeJson.id,
9474
9811
  name: nodeJson.name || nodeJson.id,
9475
9812
  description: nodeJson.description,
9476
9813
  dependencies: nodeJson.dependencies || [],
9477
- input: this.convertIO(nodeJson.input),
9478
- output: this.convertIO(nodeJson.output),
9479
- action: {
9480
- type: nodeJson.action.type,
9481
- name: nodeJson.action.name,
9482
- description: nodeJson.action.description,
9483
- tools: nodeJson.action.tools || [],
9484
- execute: async (input, context) => {
9485
- // Default implementation - should be overridden by specific action types
9486
- return input;
9487
- },
9814
+ input: { items: [] },
9815
+ output: nodeJson.output || {
9816
+ name: `${nodeJson.name || nodeJson.id}_output`,
9817
+ description: `Output of node ${nodeJson.name || nodeJson.id}`,
9818
+ value: null,
9488
9819
  },
9820
+ action: action,
9489
9821
  };
9490
9822
  workflow.addNode(node);
9491
9823
  });
@@ -9505,28 +9837,19 @@ class WorkflowParser {
9505
9837
  name: node.name,
9506
9838
  description: node.description,
9507
9839
  dependencies: node.dependencies,
9508
- input: node.input,
9509
9840
  output: node.output,
9510
9841
  action: {
9511
9842
  type: node.action.type,
9512
9843
  name: node.action.name,
9513
9844
  description: node.action.description,
9514
- tools: node.action.tools,
9845
+ tools: node.action.tools
9846
+ .map((tool) => (typeof tool === 'string' ? tool : tool.name))
9847
+ .filter((tool) => tool !== 'write_context'),
9515
9848
  },
9516
9849
  })),
9517
9850
  variables: Object.fromEntries(workflow.variables),
9518
9851
  };
9519
9852
  }
9520
- /**
9521
- * Helper to convert IO definitions
9522
- */
9523
- static convertIO(io) {
9524
- return {
9525
- type: (io === null || io === void 0 ? void 0 : io.type) || 'object',
9526
- schema: (io === null || io === void 0 ? void 0 : io.schema) || {},
9527
- value: null,
9528
- };
9529
- }
9530
9853
  }
9531
9854
 
9532
- export { ClaudeProvider, Eko, OpenaiProvider, ToolRegistry, WorkflowGenerator, WorkflowParser, Eko as default };
9855
+ export { ClaudeProvider, Eko, ExecutionLogger, OpenaiProvider, ToolRegistry, WorkflowGenerator, WorkflowParser, Eko as default };