@eko-ai/eko 4.0.3 → 4.0.5

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.
Files changed (38) hide show
  1. package/dist/agent/agent-llm.d.ts +3 -3
  2. package/dist/agent/agent-llm.d.ts.map +1 -1
  3. package/dist/agent/base.d.ts.map +1 -1
  4. package/dist/agent/browser/browser-base.d.ts.map +1 -1
  5. package/dist/agent/browser/utils.d.ts +9 -1
  6. package/dist/agent/browser/utils.d.ts.map +1 -1
  7. package/dist/agent/index.d.ts +2 -2
  8. package/dist/agent/index.d.ts.map +1 -1
  9. package/dist/chat/chat-agent.d.ts.map +1 -1
  10. package/dist/chat/chat-llm.d.ts +1 -2
  11. package/dist/chat/chat-llm.d.ts.map +1 -1
  12. package/dist/chat/tools/deep-action.d.ts.map +1 -1
  13. package/dist/common/utils.d.ts +1 -1
  14. package/dist/common/utils.d.ts.map +1 -1
  15. package/dist/index.cjs.js +685 -644
  16. package/dist/index.cjs.js.map +1 -1
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.esm.js +684 -645
  20. package/dist/index.esm.js.map +1 -1
  21. package/dist/llm/index.d.ts +2 -21
  22. package/dist/llm/index.d.ts.map +1 -1
  23. package/dist/llm/react.d.ts +5 -0
  24. package/dist/llm/react.d.ts.map +1 -0
  25. package/dist/llm/rlm.d.ts +22 -0
  26. package/dist/llm/rlm.d.ts.map +1 -0
  27. package/dist/memory/memory.d.ts.map +1 -1
  28. package/dist/types/agent.types.d.ts +2 -47
  29. package/dist/types/agent.types.d.ts.map +1 -1
  30. package/dist/types/chat.types.d.ts +3 -45
  31. package/dist/types/chat.types.d.ts.map +1 -1
  32. package/dist/types/config.types.d.ts +10 -10
  33. package/dist/types/config.types.d.ts.map +1 -1
  34. package/dist/types/index.d.ts +2 -2
  35. package/dist/types/index.d.ts.map +1 -1
  36. package/dist/types/llm.types.d.ts +51 -1
  37. package/dist/types/llm.types.d.ts.map +1 -1
  38. package/package.json +1 -1
package/dist/index.esm.js CHANGED
@@ -393,9 +393,10 @@ async function compressImageData(imageBase64, imageType, compress, quality) {
393
393
  ctx: canvas.getContext("2d"),
394
394
  exportBase64: async (mime, q) => {
395
395
  const buffer = canvas.toBuffer(mime, { quality: q });
396
- return ((
396
+ const _Buffer =
397
397
  // @ts-ignore
398
- typeof Buffer !== "undefined" ? Buffer.from(buffer) : buffer).toString("base64"));
398
+ typeof Buffer !== "undefined" ? Buffer.from(buffer) : buffer;
399
+ return _Buffer.toString("base64");
399
400
  },
400
401
  };
401
402
  }
@@ -475,13 +476,19 @@ function mergeAgents(agents1, agents2) {
475
476
  }
476
477
  return agents;
477
478
  }
478
- function sub(str, maxLength, appendPoint = true) {
479
+ function sub(str, maxLength, appendPoint = true, showTruncated = true) {
479
480
  if (!str) {
480
481
  return "";
481
482
  }
482
483
  if (str.length > maxLength) {
483
- // return str.substring(0, maxLength) + (appendPoint ? "..." : "");
484
- return (Array.from(str).slice(0, maxLength).join("") + (appendPoint ? "..." : ""));
484
+ const truncatedLength = str.length - maxLength;
485
+ // return str.substring(0, maxLength) + (appendPoint ? showTruncated ? `...(truncated: +${truncatedLength} chars)` : "..." : "");
486
+ return (Array.from(str).slice(0, maxLength).join("") +
487
+ (appendPoint
488
+ ? showTruncated
489
+ ? `...(truncated: +${truncatedLength} chars)`
490
+ : "..."
491
+ : ""));
485
492
  }
486
493
  return str;
487
494
  }
@@ -30021,7 +30028,7 @@ function defaultLLMProviderOptions() {
30021
30028
  },
30022
30029
  openrouter: {
30023
30030
  reasoning: {
30024
- max_tokens: 10,
30031
+ effort: "low",
30025
30032
  },
30026
30033
  },
30027
30034
  };
@@ -30040,12 +30047,12 @@ function defaultMessageProviderOptions() {
30040
30047
  };
30041
30048
  }
30042
30049
  function convertTools(tools) {
30043
- return tools.map((tool) => ({
30050
+ return tools.map((tool, index) => ({
30044
30051
  type: "function",
30045
30052
  name: tool.name,
30046
30053
  description: tool.description,
30047
30054
  inputSchema: tool.parameters,
30048
- // providerOptions: defaultMessageProviderOptions()
30055
+ providerOptions: index < 3 ? defaultMessageProviderOptions() : undefined,
30049
30056
  }));
30050
30057
  }
30051
30058
  function getTool(tools, name) {
@@ -30161,7 +30168,7 @@ function convertToolResult(toolUse, toolResult, user_messages) {
30161
30168
  output: result,
30162
30169
  };
30163
30170
  }
30164
- async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, retryNum = 0, callback, requestHandler) {
30171
+ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, callback, requestHandler) {
30165
30172
  await agentContext.context.checkAborted();
30166
30173
  if (!noCompress &&
30167
30174
  (messages.length >= config$1.compressThreshold ||
@@ -30194,300 +30201,47 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
30194
30201
  abortSignal: signal,
30195
30202
  };
30196
30203
  requestHandler && requestHandler(request);
30197
- let streamText = "";
30198
- let thinkText = "";
30199
- let toolArgsText = "";
30200
- let textStreamId = uuidv4();
30201
- let thinkStreamId = uuidv4();
30202
- let textStreamDone = false;
30203
- const toolParts = [];
30204
- let reader = null;
30205
30204
  try {
30206
30205
  agentChain.agentRequest = request;
30207
30206
  context.currentStepControllers.add(stepController);
30208
- const result = await rlm.callStream(request);
30209
- reader = result.stream.getReader();
30210
- let toolPart = null;
30211
- while (true) {
30207
+ const result = await callLLM(rlm, request, async (message) => {
30212
30208
  await context.checkAborted();
30213
- const { done, value } = await reader.read();
30214
- if (done) {
30215
- break;
30209
+ await streamCallback.onMessage({
30210
+ streamType: "agent",
30211
+ chatId: context.chatId,
30212
+ taskId: context.taskId,
30213
+ agentName: agentNode.name,
30214
+ nodeId: agentNode.id,
30215
+ ...message,
30216
+ });
30217
+ }, async (request, error) => {
30218
+ if ((error + "").indexOf("is too long") > -1) {
30219
+ await compressAgentMessages(agentContext, messages, tools);
30216
30220
  }
30217
- const chunk = value;
30218
- switch (chunk.type) {
30219
- case "text-start": {
30220
- textStreamId = uuidv4();
30221
- break;
30222
- }
30223
- case "text-delta": {
30224
- if (toolPart && !chunk.delta) {
30225
- continue;
30226
- }
30227
- streamText += chunk.delta || "";
30228
- await streamCallback.onMessage({
30229
- streamType: "agent",
30230
- chatId: context.chatId,
30231
- taskId: context.taskId,
30232
- agentName: agentNode.name,
30233
- nodeId: agentNode.id,
30234
- type: "text",
30235
- streamId: textStreamId,
30236
- streamDone: false,
30237
- text: streamText,
30238
- }, agentContext);
30239
- if (toolPart) {
30240
- await streamCallback.onMessage({
30241
- streamType: "agent",
30242
- chatId: context.chatId,
30243
- taskId: context.taskId,
30244
- agentName: agentNode.name,
30245
- nodeId: agentNode.id,
30246
- type: "tool_use",
30247
- toolCallId: toolPart.toolCallId,
30248
- toolName: toolPart.toolName,
30249
- params: toolPart.input || {},
30250
- }, agentContext);
30251
- toolPart = null;
30252
- }
30253
- break;
30254
- }
30255
- case "text-end": {
30256
- textStreamDone = true;
30257
- if (streamText) {
30258
- await streamCallback.onMessage({
30259
- streamType: "agent",
30260
- chatId: context.chatId,
30261
- taskId: context.taskId,
30262
- agentName: agentNode.name,
30263
- nodeId: agentNode.id,
30264
- type: "text",
30265
- streamId: textStreamId,
30266
- streamDone: true,
30267
- text: streamText,
30268
- }, agentContext);
30269
- }
30270
- break;
30271
- }
30272
- case "reasoning-start": {
30273
- thinkStreamId = uuidv4();
30274
- break;
30275
- }
30276
- case "reasoning-delta": {
30277
- thinkText += chunk.delta || "";
30278
- await streamCallback.onMessage({
30279
- streamType: "agent",
30280
- chatId: context.chatId,
30281
- taskId: context.taskId,
30282
- agentName: agentNode.name,
30283
- nodeId: agentNode.id,
30284
- type: "thinking",
30285
- streamId: thinkStreamId,
30286
- streamDone: false,
30287
- text: thinkText,
30288
- }, agentContext);
30289
- break;
30290
- }
30291
- case "reasoning-end": {
30292
- if (thinkText) {
30293
- await streamCallback.onMessage({
30294
- streamType: "agent",
30295
- chatId: context.chatId,
30296
- taskId: context.taskId,
30297
- agentName: agentNode.name,
30298
- nodeId: agentNode.id,
30299
- type: "thinking",
30300
- streamId: thinkStreamId,
30301
- streamDone: true,
30302
- text: thinkText,
30303
- }, agentContext);
30304
- }
30305
- break;
30306
- }
30307
- case "tool-input-start": {
30308
- if (toolPart && toolPart.toolCallId == chunk.id) {
30309
- toolPart.toolName = chunk.toolName;
30310
- }
30311
- else {
30312
- toolPart = {
30313
- type: "tool-call",
30314
- toolCallId: chunk.id,
30315
- toolName: chunk.toolName,
30316
- input: {},
30317
- };
30318
- toolParts.push(toolPart);
30319
- }
30320
- break;
30321
- }
30322
- case "tool-input-delta": {
30323
- if (!textStreamDone) {
30324
- textStreamDone = true;
30325
- await streamCallback.onMessage({
30326
- streamType: "agent",
30327
- chatId: context.chatId,
30328
- taskId: context.taskId,
30329
- agentName: agentNode.name,
30330
- nodeId: agentNode.id,
30331
- type: "text",
30332
- streamId: textStreamId,
30333
- streamDone: true,
30334
- text: streamText,
30335
- }, agentContext);
30336
- }
30337
- toolArgsText += chunk.delta || "";
30338
- await streamCallback.onMessage({
30339
- streamType: "agent",
30340
- chatId: context.chatId,
30341
- taskId: context.taskId,
30342
- agentName: agentNode.name,
30343
- nodeId: agentNode.id,
30344
- type: "tool_streaming",
30345
- toolCallId: chunk.id,
30346
- toolName: toolPart?.toolName || "",
30347
- paramsText: toolArgsText,
30348
- }, agentContext);
30349
- break;
30350
- }
30351
- case "tool-call": {
30352
- toolArgsText = "";
30353
- const args = chunk.input ? JSON.parse(chunk.input) : {};
30354
- const message = {
30355
- streamType: "agent",
30356
- chatId: context.chatId,
30357
- taskId: context.taskId,
30358
- agentName: agentNode.name,
30359
- nodeId: agentNode.id,
30360
- type: "tool_use",
30361
- toolCallId: chunk.toolCallId,
30362
- toolName: chunk.toolName,
30363
- params: args,
30364
- };
30365
- await streamCallback.onMessage(message, agentContext);
30366
- if (toolPart == null) {
30367
- toolParts.push({
30368
- type: "tool-call",
30369
- toolCallId: chunk.toolCallId,
30370
- toolName: chunk.toolName,
30371
- input: message.params || args,
30372
- });
30373
- }
30374
- else {
30375
- toolPart.input = message.params || args;
30376
- toolPart = null;
30377
- }
30378
- break;
30379
- }
30380
- case "file": {
30381
- await streamCallback.onMessage({
30382
- streamType: "agent",
30383
- chatId: context.chatId,
30384
- taskId: context.taskId,
30385
- agentName: agentNode.name,
30386
- nodeId: agentNode.id,
30387
- type: "file",
30388
- mimeType: chunk.mediaType,
30389
- data: chunk.data,
30390
- }, agentContext);
30391
- break;
30392
- }
30393
- case "error": {
30394
- Log.error(`${agentNode.name} agent error: `, chunk);
30395
- await streamCallback.onMessage({
30396
- streamType: "agent",
30397
- chatId: context.chatId,
30398
- taskId: context.taskId,
30399
- agentName: agentNode.name,
30400
- nodeId: agentNode.id,
30401
- type: "error",
30402
- error: chunk.error,
30403
- }, agentContext);
30404
- throw new Error("LLM Error: " + chunk.error);
30405
- }
30406
- case "finish": {
30407
- if (!textStreamDone) {
30408
- textStreamDone = true;
30409
- await streamCallback.onMessage({
30410
- streamType: "agent",
30411
- chatId: context.chatId,
30412
- taskId: context.taskId,
30413
- agentName: agentNode.name,
30414
- nodeId: agentNode.id,
30415
- type: "text",
30416
- streamId: textStreamId,
30417
- streamDone: true,
30418
- text: streamText,
30419
- }, agentContext);
30420
- }
30421
- if (chunk.finishReason === "content-filter") {
30422
- throw new Error("LLM error: trigger content filtering violation");
30423
- }
30424
- else if (chunk.finishReason === "other") {
30425
- throw new Error("LLM error: terminated due to other reasons");
30426
- }
30427
- else if (chunk.finishReason === "length" &&
30428
- messages.length >= 3 &&
30429
- !noCompress &&
30430
- retryNum < config$1.maxRetryNum) {
30431
- await compressAgentMessages(agentContext, messages, tools);
30432
- return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
30433
- }
30434
- if (toolPart) {
30435
- await streamCallback.onMessage({
30436
- streamType: "agent",
30437
- chatId: context.chatId,
30438
- taskId: context.taskId,
30439
- agentName: agentNode.name,
30440
- nodeId: agentNode.id,
30441
- type: "tool_use",
30442
- toolCallId: toolPart.toolCallId,
30443
- toolName: toolPart.toolName,
30444
- params: toolPart.input || {},
30445
- }, agentContext);
30446
- toolPart = null;
30447
- }
30448
- await streamCallback.onMessage({
30449
- streamType: "agent",
30450
- chatId: context.chatId,
30451
- taskId: context.taskId,
30452
- agentName: agentNode.name,
30453
- nodeId: agentNode.id,
30454
- type: "finish",
30455
- finishReason: chunk.finishReason,
30456
- usage: {
30457
- promptTokens: chunk.usage.inputTokens || 0,
30458
- completionTokens: chunk.usage.outputTokens || 0,
30459
- totalTokens: chunk.usage.totalTokens ||
30460
- (chunk.usage.inputTokens || 0) +
30461
- (chunk.usage.outputTokens || 0),
30462
- },
30463
- }, agentContext);
30464
- break;
30465
- }
30221
+ }, async (request, finishReason, value, retryNum) => {
30222
+ if (finishReason === "content-filter") {
30223
+ throw new Error("LLM error: trigger content filtering violation");
30466
30224
  }
30467
- }
30468
- }
30469
- catch (e) {
30470
- await context.checkAborted();
30471
- if (retryNum < config$1.maxRetryNum) {
30472
- await sleep(300 * (retryNum + 1) * (retryNum + 1));
30473
- if ((e + "").indexOf("is too long") > -1) {
30225
+ else if (finishReason === "other") {
30226
+ throw new Error("LLM error: terminated due to other reasons");
30227
+ }
30228
+ else if (finishReason === "length" &&
30229
+ messages.length >= 3 &&
30230
+ !noCompress &&
30231
+ retryNum < config$1.maxRetryNum) {
30474
30232
  await compressAgentMessages(agentContext, messages, tools);
30233
+ return "retry";
30475
30234
  }
30476
- return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
30477
- }
30478
- throw e;
30235
+ });
30236
+ agentChain.agentResult = result
30237
+ .filter((s) => s.type == "text")
30238
+ .map((s) => s.text)
30239
+ .join("\n\n");
30240
+ return result;
30479
30241
  }
30480
30242
  finally {
30481
- reader && reader.releaseLock();
30482
30243
  context.currentStepControllers.delete(stepController);
30483
30244
  }
30484
- agentChain.agentResult = streamText;
30485
- return streamText
30486
- ? [
30487
- { type: "text", text: streamText },
30488
- ...toolParts,
30489
- ]
30490
- : toolParts;
30491
30245
  }
30492
30246
  function estimatePromptTokens(messages, tools) {
30493
30247
  let tokens = messages.reduce((total, message) => {
@@ -30988,22 +30742,309 @@ class RetryLanguageModel {
30988
30742
  }
30989
30743
  }
30990
30744
 
30745
+ async function callWithReAct(rlm, request, toolCallCallback, streamCallback, errorHandler, finishHandler, loopControl) {
30746
+ if (!loopControl) {
30747
+ loopControl = async (request, assistantParts, loopNum) => {
30748
+ if (loopNum >= 15) {
30749
+ return false;
30750
+ }
30751
+ return assistantParts.filter((s) => s.type == "tool-call").length > 0;
30752
+ };
30753
+ }
30754
+ let loopNum = 0;
30755
+ let assistantParts = null;
30756
+ while (true) {
30757
+ assistantParts = await callLLM(rlm, request, streamCallback, errorHandler, finishHandler);
30758
+ if (assistantParts.length > 0) {
30759
+ request.messages.push({
30760
+ role: "assistant",
30761
+ content: assistantParts
30762
+ .filter((part) => part.type == "text" || part.type == "tool-call")
30763
+ .map((part) => part.type === "text"
30764
+ ? {
30765
+ type: "text",
30766
+ text: part.text,
30767
+ }
30768
+ : {
30769
+ type: "tool-call",
30770
+ toolCallId: part.toolCallId,
30771
+ toolName: part.toolName,
30772
+ input: JSON.parse((part.input || "{}")),
30773
+ }),
30774
+ });
30775
+ }
30776
+ const continueLoop = await loopControl(request, assistantParts, loopNum);
30777
+ if (!continueLoop) {
30778
+ break;
30779
+ }
30780
+ const toolUses = assistantParts.filter((s) => s.type == "tool-call");
30781
+ const toolResults = await toolCallCallback(request, toolUses);
30782
+ if (toolResults.length > 0) {
30783
+ request.messages.push({
30784
+ role: "tool",
30785
+ content: toolResults.map((result, index) => ({
30786
+ type: "tool-result",
30787
+ toolCallId: toolUses[index].toolCallId,
30788
+ toolName: toolUses[index].toolName,
30789
+ output: result,
30790
+ })),
30791
+ });
30792
+ }
30793
+ loopNum++;
30794
+ }
30795
+ return assistantParts;
30796
+ }
30797
+ async function callLLM(rlm, request, streamCallback, errorHandler, finishHandler, retryNum = 0) {
30798
+ let streamText = "";
30799
+ let thinkText = "";
30800
+ let toolArgsText = "";
30801
+ let textStreamId = uuidv4();
30802
+ let thinkStreamId = uuidv4();
30803
+ let textStreamDone = false;
30804
+ const toolParts = [];
30805
+ let reader = null;
30806
+ try {
30807
+ const result = await rlm.callStream(request);
30808
+ reader = result.stream.getReader();
30809
+ let toolPart = null;
30810
+ while (true) {
30811
+ const { done, value } = await reader.read();
30812
+ if (done) {
30813
+ break;
30814
+ }
30815
+ const chunk = value;
30816
+ switch (chunk.type) {
30817
+ case "text-start": {
30818
+ textStreamId = uuidv4();
30819
+ break;
30820
+ }
30821
+ case "text-delta": {
30822
+ if (toolPart && !chunk.delta) {
30823
+ continue;
30824
+ }
30825
+ streamText += chunk.delta || "";
30826
+ await streamCallback?.({
30827
+ type: "text",
30828
+ streamId: textStreamId,
30829
+ streamDone: false,
30830
+ text: streamText,
30831
+ });
30832
+ if (toolPart) {
30833
+ await streamCallback?.({
30834
+ type: "tool_use",
30835
+ toolCallId: toolPart.toolCallId,
30836
+ toolName: toolPart.toolName,
30837
+ params: toolPart.input || {},
30838
+ });
30839
+ toolPart = null;
30840
+ }
30841
+ break;
30842
+ }
30843
+ case "text-end": {
30844
+ textStreamDone = true;
30845
+ if (streamText) {
30846
+ await streamCallback?.({
30847
+ type: "text",
30848
+ streamId: textStreamId,
30849
+ streamDone: true,
30850
+ text: streamText,
30851
+ });
30852
+ }
30853
+ break;
30854
+ }
30855
+ case "reasoning-start": {
30856
+ thinkStreamId = uuidv4();
30857
+ break;
30858
+ }
30859
+ case "reasoning-delta": {
30860
+ thinkText += chunk.delta || "";
30861
+ await streamCallback?.({
30862
+ type: "thinking",
30863
+ streamId: thinkStreamId,
30864
+ streamDone: false,
30865
+ text: thinkText,
30866
+ });
30867
+ break;
30868
+ }
30869
+ case "reasoning-end": {
30870
+ if (thinkText) {
30871
+ await streamCallback?.({
30872
+ type: "thinking",
30873
+ streamId: thinkStreamId,
30874
+ streamDone: true,
30875
+ text: thinkText,
30876
+ });
30877
+ }
30878
+ break;
30879
+ }
30880
+ case "tool-input-start": {
30881
+ if (toolPart && toolPart.toolCallId == chunk.id) {
30882
+ toolPart.toolName = chunk.toolName;
30883
+ }
30884
+ else {
30885
+ const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.id)[0];
30886
+ if (_toolPart) {
30887
+ toolPart = _toolPart;
30888
+ toolPart.toolName = _toolPart.toolName || chunk.toolName;
30889
+ toolPart.input = _toolPart.input || {};
30890
+ }
30891
+ else {
30892
+ toolPart = {
30893
+ type: "tool-call",
30894
+ toolCallId: chunk.id,
30895
+ toolName: chunk.toolName,
30896
+ input: {},
30897
+ };
30898
+ toolParts.push(toolPart);
30899
+ }
30900
+ }
30901
+ break;
30902
+ }
30903
+ case "tool-input-delta": {
30904
+ if (!textStreamDone) {
30905
+ textStreamDone = true;
30906
+ await streamCallback?.({
30907
+ type: "text",
30908
+ streamId: textStreamId,
30909
+ streamDone: true,
30910
+ text: streamText,
30911
+ });
30912
+ }
30913
+ toolArgsText += chunk.delta || "";
30914
+ await streamCallback?.({
30915
+ type: "tool_streaming",
30916
+ toolCallId: chunk.id,
30917
+ toolName: toolPart?.toolName || "",
30918
+ paramsText: toolArgsText,
30919
+ });
30920
+ break;
30921
+ }
30922
+ case "tool-call": {
30923
+ toolArgsText = "";
30924
+ const args = chunk.input ? JSON.parse(chunk.input) : {};
30925
+ const message = {
30926
+ type: "tool_use",
30927
+ toolCallId: chunk.toolCallId,
30928
+ toolName: chunk.toolName,
30929
+ params: args,
30930
+ };
30931
+ await streamCallback?.(message);
30932
+ if (toolPart == null) {
30933
+ const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.toolCallId)[0];
30934
+ if (_toolPart) {
30935
+ _toolPart.input = message.params || args;
30936
+ }
30937
+ else {
30938
+ toolParts.push({
30939
+ type: "tool-call",
30940
+ toolCallId: chunk.toolCallId,
30941
+ toolName: chunk.toolName,
30942
+ input: message.params || args,
30943
+ });
30944
+ }
30945
+ }
30946
+ else {
30947
+ toolPart.input = message.params || args;
30948
+ toolPart = null;
30949
+ }
30950
+ break;
30951
+ }
30952
+ case "file": {
30953
+ await streamCallback?.({
30954
+ type: "file",
30955
+ mimeType: chunk.mediaType,
30956
+ data: chunk.data,
30957
+ });
30958
+ break;
30959
+ }
30960
+ case "error": {
30961
+ Log.error(`chatLLM error: `, chunk);
30962
+ await streamCallback?.({
30963
+ type: "error",
30964
+ error: chunk.error,
30965
+ });
30966
+ throw new Error("LLM Error: " + chunk.error);
30967
+ }
30968
+ case "finish": {
30969
+ if (!textStreamDone) {
30970
+ textStreamDone = true;
30971
+ await streamCallback?.({
30972
+ type: "text",
30973
+ streamId: textStreamId,
30974
+ streamDone: true,
30975
+ text: streamText,
30976
+ });
30977
+ }
30978
+ if (toolPart) {
30979
+ await streamCallback?.({
30980
+ type: "tool_use",
30981
+ toolCallId: toolPart.toolCallId,
30982
+ toolName: toolPart.toolName,
30983
+ params: toolPart.input || {},
30984
+ });
30985
+ toolPart = null;
30986
+ }
30987
+ if (finishHandler) {
30988
+ const type = await finishHandler(request, chunk.finishReason, chunk, retryNum);
30989
+ if (type == "retry") {
30990
+ await sleep(200 * (retryNum + 1) * (retryNum + 1));
30991
+ return callLLM(rlm, request, streamCallback, errorHandler, finishHandler, ++retryNum);
30992
+ }
30993
+ }
30994
+ await streamCallback?.({
30995
+ type: "finish",
30996
+ finishReason: chunk.finishReason,
30997
+ usage: {
30998
+ promptTokens: chunk.usage.inputTokens || 0,
30999
+ completionTokens: chunk.usage.outputTokens || 0,
31000
+ totalTokens: chunk.usage.totalTokens ||
31001
+ (chunk.usage.inputTokens || 0) +
31002
+ (chunk.usage.outputTokens || 0),
31003
+ },
31004
+ });
31005
+ break;
31006
+ }
31007
+ }
31008
+ }
31009
+ }
31010
+ catch (e) {
31011
+ if (retryNum < config$1.maxRetryNum) {
31012
+ await sleep(200 * (retryNum + 1) * (retryNum + 1));
31013
+ if (errorHandler) {
31014
+ await errorHandler(request, e, retryNum);
31015
+ }
31016
+ return callLLM(rlm, request, streamCallback, errorHandler, finishHandler, ++retryNum);
31017
+ }
31018
+ throw e;
31019
+ }
31020
+ finally {
31021
+ reader && reader.releaseLock();
31022
+ }
31023
+ return streamText
31024
+ ? [
31025
+ { type: "text", text: streamText },
31026
+ ...toolParts,
31027
+ ]
31028
+ : toolParts;
31029
+ }
31030
+
30991
31031
  const global = {
30992
31032
  chatMap: new Map(),
30993
31033
  taskMap: new Map(),
30994
31034
  prompts: new Map(),
30995
31035
  };
30996
31036
 
30997
- const GlobalPromptKey = {
30998
- planner_system: "planner_system",
30999
- planner_example: "planner_example",
31000
- planner_user: "planner_user",
31001
- agent_system: "agent_system",
31002
- chat_system: "chat_system",
31003
- webpage_qa_prompt: "webpage_qa_prompt",
31004
- deep_action_description: "deep_action_description",
31005
- deep_action_param_task_description: "deep_action_param_task_description",
31006
- };
31037
+ var GlobalPromptKey;
31038
+ (function (GlobalPromptKey) {
31039
+ GlobalPromptKey["planner_system"] = "planner_system";
31040
+ GlobalPromptKey["planner_example"] = "planner_example";
31041
+ GlobalPromptKey["planner_user"] = "planner_user";
31042
+ GlobalPromptKey["agent_system"] = "agent_system";
31043
+ GlobalPromptKey["chat_system"] = "chat_system";
31044
+ GlobalPromptKey["webpage_qa_prompt"] = "webpage_qa_prompt";
31045
+ GlobalPromptKey["deep_action_description"] = "deep_action_description";
31046
+ GlobalPromptKey["deep_action_param_task_description"] = "deep_action_param_task_description";
31047
+ })(GlobalPromptKey || (GlobalPromptKey = {}));
31007
31048
 
31008
31049
  class PromptTemplate {
31009
31050
  /**
@@ -32985,7 +33026,7 @@ function buildPreTaskResult(context) {
32985
33026
  for (let i = 0; i < context.chain.agents.length; i++) {
32986
33027
  const agentChain = context.chain.agents[i];
32987
33028
  if (agentChain.agentResult) {
32988
- preTaskResult += `<subtask_result agent="${agentChain.agent.name}">Subtask: ${agentChain.agent.task}\nResult: ${sub(agentChain.agentResult, 600)}</subtask_result>`;
33029
+ preTaskResult += `<subtask_result agent="${agentChain.agent.name}">\nSubtask: ${agentChain.agent.task}\nResult: ${sub(agentChain.agentResult, 600).trim()}\n</subtask_result>`;
32989
33030
  }
32990
33031
  }
32991
33032
  return preTaskResult.trim();
@@ -33047,7 +33088,6 @@ class Agent {
33047
33088
  {
33048
33089
  role: "user",
33049
33090
  content: userPrompt,
33050
- providerOptions: defaultMessageProviderOptions(),
33051
33091
  },
33052
33092
  ];
33053
33093
  agentContext.messages = messages;
@@ -33067,7 +33107,7 @@ class Agent {
33067
33107
  }
33068
33108
  await this.handleMessages(agentContext, messages, tools);
33069
33109
  const llm_tools = convertTools(agentTools);
33070
- const results = await callAgentLLM(agentContext, rlm, messages, llm_tools, false, undefined, 0, this.callback, this.requestHandler);
33110
+ const results = await callAgentLLM(agentContext, rlm, messages, llm_tools, false, undefined, this.callback, this.requestHandler);
33071
33111
  const forceStop = agentContext.variables.get("forceStop");
33072
33112
  if (forceStop) {
33073
33113
  return forceStop;
@@ -33339,123 +33379,344 @@ class Agent {
33339
33379
  }
33340
33380
  }
33341
33381
 
33342
- function extract_page_content(max_url_length = 200, max_content_length = 50000) {
33343
- let result = "";
33344
- max_url_length = max_url_length || 200;
33345
- try {
33346
- function traverse(node) {
33347
- if (node.nodeType === Node.ELEMENT_NODE) {
33348
- const tagName = node.tagName.toLowerCase();
33349
- if (["script", "style", "noscript"].includes(tagName)) {
33350
- return;
33382
+ function extract_page_content(params) {
33383
+ params = params || {};
33384
+ const IGNORED_TAGS = new Set(params.ignored_tags || ["script", "style", "noscript", "svg", "canvas"]);
33385
+ const FORM_TAGS = new Set(["input", "select", "textarea"]);
33386
+ const KEY_ATTRIBUTES = new Set(params.key_attributes || [
33387
+ "id",
33388
+ "title",
33389
+ "name",
33390
+ "alt",
33391
+ "src",
33392
+ "url",
33393
+ "href",
33394
+ "value",
33395
+ "checked",
33396
+ "selected",
33397
+ ]);
33398
+ const urlLimit = params.max_url_length || 200;
33399
+ const contentLimit = params.max_content_length || 50000;
33400
+ const minImageArea = params.min_image_area || 1600;
33401
+ const parts = [];
33402
+ let currentLength = 0;
33403
+ const escapeHtml = (text) => {
33404
+ const map = {
33405
+ "<": "&lt;",
33406
+ ">": "&gt;",
33407
+ };
33408
+ return text.replace(/[<>]/g, (m) => map[m] || m);
33409
+ };
33410
+ const getKeyAttributes = (element) => {
33411
+ const attrs = {};
33412
+ const attributes = element.attributes;
33413
+ for (let i = 0; i < attributes.length; i++) {
33414
+ const attr = attributes[i];
33415
+ const name = attr.name.toLowerCase();
33416
+ if (KEY_ATTRIBUTES.has(name)) {
33417
+ const value = attr.value?.trim();
33418
+ if (value) {
33419
+ attrs[name] = value;
33351
33420
  }
33352
- const style = window.getComputedStyle(node);
33353
- if (style.display == "none" ||
33354
- style.visibility == "hidden" ||
33355
- style.opacity == "0") {
33356
- return;
33421
+ }
33422
+ }
33423
+ if (element instanceof HTMLInputElement) {
33424
+ const inputType = element.type.toLowerCase();
33425
+ if (inputType === "checkbox" || inputType === "radio") {
33426
+ if (element.checked) {
33427
+ attrs.checked = "true";
33357
33428
  }
33358
33429
  }
33359
- if (node.nodeType === Node.TEXT_NODE) {
33360
- // text
33361
- const text = node.textContent.trim();
33362
- if (text) {
33363
- result += text + " ";
33364
- }
33365
- }
33366
- else if (node.nodeType === Node.ELEMENT_NODE) {
33367
- const tagName = node.tagName.toLowerCase();
33368
- if (["input", "select", "textarea"].includes(tagName)) {
33369
- // input / select / textarea
33370
- if (tagName == "input" && node.type == "checkbox") {
33371
- result += node.checked + " ";
33372
- }
33373
- else if (tagName == "input" && node.type == "radio") {
33374
- if (node.checked && node.value) {
33375
- result += node.value + " ";
33376
- }
33377
- }
33378
- else if (node.value) {
33379
- result += node.value + " ";
33430
+ if (element.value && !attrs.value) {
33431
+ attrs.value = element.value;
33432
+ }
33433
+ if (element.name && !attrs.name) {
33434
+ attrs.name = element.name;
33435
+ }
33436
+ if (attrs.value || Object.keys(attrs).length > 0) {
33437
+ attrs.type = inputType;
33438
+ }
33439
+ }
33440
+ else if (element instanceof HTMLSelectElement) {
33441
+ if (element.selectedIndex >= 0) {
33442
+ const selectedOption = element.options[element.selectedIndex];
33443
+ if (selectedOption) {
33444
+ attrs.selected = String(element.selectedIndex);
33445
+ if (selectedOption.value && !attrs.value) {
33446
+ attrs.value = selectedOption.value;
33380
33447
  }
33381
33448
  }
33382
- else if (tagName === "img") {
33383
- // image
33384
- const src = node.src ||
33385
- node.getAttribute("src") ||
33386
- node.getAttribute("data-src");
33387
- const alt = node.alt || node.title || "";
33388
- if (src &&
33389
- src.length <= max_url_length &&
33390
- node.width * node.height >= 10000 &&
33391
- src.startsWith("http")) {
33392
- result += `![${alt ? alt : "image"}](${src.trim()}) `;
33393
- }
33449
+ }
33450
+ if (element.name && !attrs.name) {
33451
+ attrs.name = element.name;
33452
+ }
33453
+ }
33454
+ else if (element instanceof HTMLTextAreaElement) {
33455
+ if (element.value && !attrs.value) {
33456
+ attrs.value = element.value;
33457
+ }
33458
+ if (element.name && !attrs.name) {
33459
+ attrs.name = element.name;
33460
+ }
33461
+ }
33462
+ else if (element instanceof HTMLImageElement) {
33463
+ const src = element.src ||
33464
+ element.getAttribute("src") ||
33465
+ element.getAttribute("data-src");
33466
+ if (src && !attrs.src) {
33467
+ attrs.src = src;
33468
+ }
33469
+ if (element.alt && !attrs.alt) {
33470
+ attrs.alt = element.alt;
33471
+ }
33472
+ }
33473
+ else if (element instanceof HTMLAnchorElement) {
33474
+ if (element.href && !attrs.href) {
33475
+ attrs.href = element.href;
33476
+ }
33477
+ if (element.title && !attrs.title) {
33478
+ attrs.title = element.title;
33479
+ }
33480
+ }
33481
+ else if (element instanceof HTMLVideoElement ||
33482
+ element instanceof HTMLAudioElement) {
33483
+ const src = element.src || element.getAttribute("src");
33484
+ if (src && !attrs.src) {
33485
+ attrs.src = src;
33486
+ }
33487
+ }
33488
+ return attrs;
33489
+ };
33490
+ const buildAttributesString = (attrs) => {
33491
+ if (Object.keys(attrs).length === 0) {
33492
+ return "";
33493
+ }
33494
+ const attrStrings = [];
33495
+ for (const [key, value] of Object.entries(attrs)) {
33496
+ if (value) {
33497
+ attrStrings.push(`${key}="${escapeHtml(value)}"`);
33498
+ }
33499
+ }
33500
+ return attrStrings.length > 0 ? " " + attrStrings.join(" ") : "";
33501
+ };
33502
+ const hasKeyAttributes = (attrs) => {
33503
+ return Object.keys(attrs).length > 0;
33504
+ };
33505
+ const addHtmlContent = (content) => {
33506
+ if (!content || currentLength >= contentLimit) {
33507
+ return false;
33508
+ }
33509
+ const contentLength = content.length;
33510
+ if (currentLength + contentLength > contentLimit) {
33511
+ const remaining = contentLimit - currentLength;
33512
+ if (remaining > 0) {
33513
+ parts.push(content.slice(0, remaining));
33514
+ }
33515
+ return false;
33516
+ }
33517
+ currentLength += contentLength;
33518
+ parts.push(content);
33519
+ return true;
33520
+ };
33521
+ const hasDirectTextChild = (node) => {
33522
+ for (const child of node.childNodes) {
33523
+ if (child.nodeType === Node.TEXT_NODE) {
33524
+ const text = child.textContent || "";
33525
+ if (text.trim()) {
33526
+ return true;
33394
33527
  }
33395
- else if (tagName === "a" && node.children.length == 0) {
33396
- // link
33397
- const href = node.href || node.getAttribute("href");
33398
- const text = node.innerText.trim() || node.title;
33399
- if (text &&
33400
- href &&
33401
- href.length <= max_url_length &&
33402
- href.startsWith("http")) {
33403
- result += `[${text}](${href.trim()}) `;
33404
- }
33405
- else {
33406
- result += text + " ";
33528
+ }
33529
+ }
33530
+ return false;
33531
+ };
33532
+ const traverse = (node) => {
33533
+ if (currentLength >= contentLimit) {
33534
+ return "";
33535
+ }
33536
+ if (node.nodeType === Node.TEXT_NODE) {
33537
+ const text = node.textContent || "";
33538
+ const trimmed = text.trim();
33539
+ if (trimmed) {
33540
+ return escapeHtml(trimmed);
33541
+ }
33542
+ return "";
33543
+ }
33544
+ if (node.nodeType !== Node.ELEMENT_NODE) {
33545
+ return "";
33546
+ }
33547
+ const element = node;
33548
+ const tagName = element.tagName.toLowerCase();
33549
+ if (IGNORED_TAGS.has(tagName)) {
33550
+ return "";
33551
+ }
33552
+ try {
33553
+ const style = window.getComputedStyle(element);
33554
+ if (style.display === "none") {
33555
+ return "";
33556
+ }
33557
+ }
33558
+ catch (e) { }
33559
+ const attrs = getKeyAttributes(element);
33560
+ const attrsString = buildAttributesString(attrs);
33561
+ const hasKeyAttrs = hasKeyAttributes(attrs);
33562
+ const hasDirectText = hasDirectTextChild(node);
33563
+ if (FORM_TAGS.has(tagName)) {
33564
+ let value = "";
33565
+ if (tagName === "input") {
33566
+ const input = element;
33567
+ const inputType = input.type.toLowerCase();
33568
+ if (inputType === "checkbox" || inputType === "radio") {
33569
+ if (!input.checked) {
33570
+ return "";
33407
33571
  }
33572
+ value = input.value || String(input.checked);
33408
33573
  }
33409
- else if (tagName === "video" || tagName == "audio") {
33410
- // video / audio
33411
- let src = node.src || node.getAttribute("src");
33412
- const sources = node.querySelectorAll("source");
33413
- if (sources.length > 0 && sources[0].src) {
33414
- src = sources[0].src;
33415
- if (src && src.startsWith("http") && sources[0].type) {
33416
- result += sources[0].type + " ";
33417
- }
33418
- }
33419
- if (src && src.startsWith("http")) {
33420
- result += src.trim() + " ";
33421
- }
33574
+ else if (inputType === "password") {
33575
+ return "";
33422
33576
  }
33423
- else if (tagName === "br") {
33424
- // br
33425
- result += "\n";
33577
+ else {
33578
+ value = input.value || "";
33426
33579
  }
33427
- else if (["p", "div", "h1", "h2", "h3", "h4", "h5", "h6"].includes(tagName)) {
33428
- // block
33429
- result += "\n";
33430
- for (let child of node.childNodes) {
33431
- traverse(child);
33432
- }
33433
- result += "\n";
33434
- return;
33580
+ }
33581
+ else if (tagName === "select") {
33582
+ const select = element;
33583
+ if (select.selectedIndex >= 0) {
33584
+ const selectedOption = select.options[select.selectedIndex];
33585
+ value = selectedOption
33586
+ ? selectedOption.text || selectedOption.value
33587
+ : "";
33435
33588
  }
33436
- else if (tagName === "hr") {
33437
- // hr
33438
- result += "\n--------\n";
33589
+ }
33590
+ else if (tagName === "textarea") {
33591
+ value = element.value || "";
33592
+ }
33593
+ if (value || hasKeyAttrs || tagName === "textarea") {
33594
+ const escapedValue = escapeHtml(value);
33595
+ return `<${tagName}${attrsString}>${escapedValue}</${tagName}>`;
33596
+ }
33597
+ return "";
33598
+ }
33599
+ if (tagName === "img") {
33600
+ const img = element;
33601
+ const src = img.src || img.getAttribute("src") || img.getAttribute("data-src");
33602
+ if (src &&
33603
+ src.length <= urlLimit &&
33604
+ img.width * img.height >= minImageArea &&
33605
+ src.startsWith("http")) {
33606
+ if (!attrs.alt && (img.alt || img.title)) {
33607
+ attrs.alt = img.alt || img.title || "";
33439
33608
  }
33440
- else {
33441
- // recursive
33442
- for (let child of node.childNodes) {
33443
- traverse(child);
33444
- }
33609
+ if (!attrs.src) {
33610
+ attrs.src = src.trim();
33611
+ }
33612
+ const imgAttrsString = buildAttributesString(attrs);
33613
+ return `<img${imgAttrsString} />`;
33614
+ }
33615
+ return "";
33616
+ }
33617
+ if (tagName === "a") {
33618
+ const anchor = element;
33619
+ const href = anchor.href || anchor.getAttribute("href");
33620
+ const childContent = [];
33621
+ for (const child of node.childNodes) {
33622
+ const content = traverse(child);
33623
+ if (content) {
33624
+ childContent.push(content);
33625
+ }
33626
+ }
33627
+ const innerContent = childContent.join("");
33628
+ if (!innerContent && !hasKeyAttrs) {
33629
+ return "";
33630
+ }
33631
+ if (href && href.length <= urlLimit && href.startsWith("http")) {
33632
+ if (!attrs.href) {
33633
+ attrs.href = href.trim();
33445
33634
  }
33635
+ const linkAttrsString = buildAttributesString(attrs);
33636
+ return `<a${linkAttrsString}>${innerContent}</a>`;
33637
+ }
33638
+ else if (hasKeyAttrs || hasDirectText) {
33639
+ return `<a${attrsString}>${innerContent}</a>`;
33640
+ }
33641
+ return innerContent;
33642
+ }
33643
+ if (tagName === "video" || tagName === "audio") {
33644
+ const media = element;
33645
+ let src = media.src || media.getAttribute("src");
33646
+ const sources = element.querySelectorAll("source");
33647
+ if (sources.length > 0 && sources[0].src) {
33648
+ src = sources[0].src;
33649
+ }
33650
+ if (src && src.startsWith("http") && src.length <= urlLimit) {
33651
+ if (!attrs.src) {
33652
+ attrs.src = src.trim();
33653
+ }
33654
+ const mediaAttrsString = buildAttributesString(attrs);
33655
+ return `<${tagName}${mediaAttrsString}></${tagName}>`;
33656
+ }
33657
+ return "";
33658
+ }
33659
+ const childContent = [];
33660
+ for (const child of node.childNodes) {
33661
+ const content = traverse(child);
33662
+ if (content) {
33663
+ childContent.push(content);
33664
+ }
33665
+ }
33666
+ const innerContent = childContent.join("");
33667
+ if (!innerContent) {
33668
+ return "";
33669
+ }
33670
+ if (hasKeyAttrs || hasDirectText) {
33671
+ return `<${tagName}${attrsString}>${innerContent}</${tagName}>`;
33672
+ }
33673
+ return innerContent;
33674
+ };
33675
+ if (!params.root_element) {
33676
+ if (params.root_selector) {
33677
+ params.root_element = document.querySelector(params.root_selector);
33678
+ if (!params.root_element) {
33679
+ return "";
33680
+ }
33681
+ }
33682
+ else {
33683
+ params.root_element = document.body;
33684
+ }
33685
+ }
33686
+ const rootTabName = params.root_element.tagName.toLowerCase();
33687
+ try {
33688
+ if (params.root_element) {
33689
+ const content = traverse(params.root_element);
33690
+ if (content) {
33691
+ addHtmlContent(content);
33446
33692
  }
33447
33693
  }
33448
- traverse(document.body);
33449
33694
  }
33450
33695
  catch (e) {
33451
- result = document.body.innerText;
33696
+ try {
33697
+ const fallbackText = params.root_element.innerText || "";
33698
+ if (fallbackText) {
33699
+ const escaped = escapeHtml(fallbackText);
33700
+ const truncated = escaped.length > contentLimit
33701
+ ? Array.from(escaped).slice(0, contentLimit).join("").trim() + "..."
33702
+ : escaped;
33703
+ return truncated.startsWith(`<${rootTabName}`)
33704
+ ? truncated
33705
+ : `<${rootTabName}>${truncated}</${rootTabName}>`;
33706
+ }
33707
+ return "";
33708
+ }
33709
+ catch {
33710
+ return "";
33711
+ }
33452
33712
  }
33453
- result = result.replace(/\s*\n/g, "\n").replace(/\n+/g, "\n").trim();
33454
- if (result.length > max_content_length) {
33455
- // result = result.slice(0, max_content_length) + "...";
33456
- result = Array.from(result).slice(0, max_content_length).join("") + "...";
33713
+ let result = parts.join("");
33714
+ if (result.length > contentLimit) {
33715
+ result = Array.from(result).slice(0, contentLimit).join("").trim() + "...";
33457
33716
  }
33458
- return result;
33717
+ return result.startsWith(`<${rootTabName}`)
33718
+ ? result
33719
+ : `<${rootTabName}>${result}</${rootTabName}>`;
33459
33720
  }
33460
33721
  function mark_screenshot_highlight_elements(screenshot, area_map, client_rect) {
33461
33722
  return new Promise(async (resolve, reject) => {
@@ -33463,8 +33724,13 @@ function mark_screenshot_highlight_elements(screenshot, area_map, client_rect) {
33463
33724
  const hasOffscreen = typeof OffscreenCanvas !== "undefined";
33464
33725
  const hasCreateImageBitmap = typeof createImageBitmap !== "undefined";
33465
33726
  const hasDOM = typeof document !== "undefined" && typeof Image !== "undefined";
33466
- // @ts-ignore
33467
- const isNode = typeof window === "undefined" && typeof process !== "undefined" && !!process.versions && !!process.versions.node;
33727
+ const isNode = typeof window === "undefined" &&
33728
+ // @ts-ignore
33729
+ typeof process !== "undefined" &&
33730
+ // @ts-ignore
33731
+ !!process.versions &&
33732
+ // @ts-ignore
33733
+ !!process.versions.node;
33468
33734
  const loadImageAny = async () => {
33469
33735
  if (hasCreateImageBitmap) {
33470
33736
  const base64Data = screenshot.imageBase64;
@@ -35778,7 +36044,6 @@ class EkoMemory {
35778
36044
  };
35779
36045
  }
35780
36046
  }),
35781
- providerOptions: defaultMessageProviderOptions(),
35782
36047
  });
35783
36048
  }
35784
36049
  else if (message.role == "assistant") {
@@ -36295,251 +36560,24 @@ class SimpleHttpMcpClient {
36295
36560
  }
36296
36561
  }
36297
36562
 
36298
- async function callChatLLM(messageId, chatContext, rlm, messages, tools, toolChoice, retryNum = 0, callback, signal) {
36563
+ async function callChatLLM(chatId, messageId, rlm, messages, tools, toolChoice, callback, signal) {
36299
36564
  const streamCallback = callback?.chatCallback || {
36300
36565
  onMessage: async () => { },
36301
36566
  };
36302
36567
  const request = {
36303
- tools: tools,
36568
+ tools,
36569
+ messages,
36304
36570
  toolChoice,
36305
- messages: messages,
36306
36571
  abortSignal: signal,
36307
36572
  };
36308
- let streamText = "";
36309
- let thinkText = "";
36310
- let toolArgsText = "";
36311
- let textStreamId = uuidv4();
36312
- let thinkStreamId = uuidv4();
36313
- let textStreamDone = false;
36314
- const toolParts = [];
36315
- let reader = null;
36316
- try {
36317
- const result = await rlm.callStream(request);
36318
- reader = result.stream.getReader();
36319
- let toolPart = null;
36320
- while (true) {
36321
- const { done, value } = await reader.read();
36322
- if (done) {
36323
- break;
36324
- }
36325
- const chunk = value;
36326
- switch (chunk.type) {
36327
- case "text-start": {
36328
- textStreamId = uuidv4();
36329
- break;
36330
- }
36331
- case "text-delta": {
36332
- if (toolPart && !chunk.delta) {
36333
- continue;
36334
- }
36335
- streamText += chunk.delta || "";
36336
- await streamCallback.onMessage({
36337
- streamType: "chat",
36338
- chatId: chatContext.getChatId(),
36339
- messageId,
36340
- type: "text",
36341
- streamId: textStreamId,
36342
- streamDone: false,
36343
- text: streamText,
36344
- });
36345
- if (toolPart) {
36346
- await streamCallback.onMessage({
36347
- streamType: "chat",
36348
- chatId: chatContext.getChatId(),
36349
- messageId,
36350
- type: "tool_use",
36351
- toolCallId: toolPart.toolCallId,
36352
- toolName: toolPart.toolName,
36353
- params: toolPart.input || {},
36354
- });
36355
- toolPart = null;
36356
- }
36357
- break;
36358
- }
36359
- case "text-end": {
36360
- textStreamDone = true;
36361
- if (streamText) {
36362
- await streamCallback.onMessage({
36363
- streamType: "chat",
36364
- chatId: chatContext.getChatId(),
36365
- messageId,
36366
- type: "text",
36367
- streamId: textStreamId,
36368
- streamDone: true,
36369
- text: streamText,
36370
- });
36371
- }
36372
- break;
36373
- }
36374
- case "reasoning-start": {
36375
- thinkStreamId = uuidv4();
36376
- break;
36377
- }
36378
- case "reasoning-delta": {
36379
- thinkText += chunk.delta || "";
36380
- await streamCallback.onMessage({
36381
- streamType: "chat",
36382
- chatId: chatContext.getChatId(),
36383
- messageId,
36384
- type: "thinking",
36385
- streamId: thinkStreamId,
36386
- streamDone: false,
36387
- text: thinkText,
36388
- });
36389
- break;
36390
- }
36391
- case "reasoning-end": {
36392
- if (thinkText) {
36393
- await streamCallback.onMessage({
36394
- streamType: "chat",
36395
- chatId: chatContext.getChatId(),
36396
- messageId,
36397
- type: "thinking",
36398
- streamId: thinkStreamId,
36399
- streamDone: true,
36400
- text: thinkText,
36401
- });
36402
- }
36403
- break;
36404
- }
36405
- case "tool-input-start": {
36406
- if (toolPart && toolPart.toolCallId == chunk.id) {
36407
- toolPart.toolName = chunk.toolName;
36408
- }
36409
- else {
36410
- toolPart = {
36411
- type: "tool-call",
36412
- toolCallId: chunk.id,
36413
- toolName: chunk.toolName,
36414
- input: {},
36415
- };
36416
- toolParts.push(toolPart);
36417
- }
36418
- break;
36419
- }
36420
- case "tool-input-delta": {
36421
- if (!textStreamDone) {
36422
- textStreamDone = true;
36423
- await streamCallback.onMessage({
36424
- streamType: "chat",
36425
- chatId: chatContext.getChatId(),
36426
- messageId,
36427
- type: "text",
36428
- streamId: textStreamId,
36429
- streamDone: true,
36430
- text: streamText,
36431
- });
36432
- }
36433
- toolArgsText += chunk.delta || "";
36434
- await streamCallback.onMessage({
36435
- streamType: "chat",
36436
- chatId: chatContext.getChatId(),
36437
- messageId,
36438
- type: "tool_streaming",
36439
- toolCallId: chunk.id,
36440
- toolName: toolPart?.toolName || "",
36441
- paramsText: toolArgsText,
36442
- });
36443
- break;
36444
- }
36445
- case "tool-call": {
36446
- toolArgsText = "";
36447
- const args = chunk.input ? JSON.parse(chunk.input) : {};
36448
- const message = {
36449
- streamType: "chat",
36450
- chatId: chatContext.getChatId(),
36451
- messageId,
36452
- type: "tool_use",
36453
- toolCallId: chunk.toolCallId,
36454
- toolName: chunk.toolName,
36455
- params: args,
36456
- };
36457
- await streamCallback.onMessage(message);
36458
- if (toolPart == null) {
36459
- toolParts.push({
36460
- type: "tool-call",
36461
- toolCallId: chunk.toolCallId,
36462
- toolName: chunk.toolName,
36463
- input: message.params || args,
36464
- });
36465
- }
36466
- else {
36467
- toolPart.input = message.params || args;
36468
- toolPart = null;
36469
- }
36470
- break;
36471
- }
36472
- case "error": {
36473
- Log.error(`chatLLM error: `, chunk);
36474
- await streamCallback.onMessage({
36475
- streamType: "chat",
36476
- chatId: chatContext.getChatId(),
36477
- messageId,
36478
- type: "error",
36479
- error: chunk.error,
36480
- });
36481
- throw new Error("LLM Error: " + chunk.error);
36482
- }
36483
- case "finish": {
36484
- if (!textStreamDone) {
36485
- textStreamDone = true;
36486
- await streamCallback.onMessage({
36487
- streamType: "chat",
36488
- chatId: chatContext.getChatId(),
36489
- messageId,
36490
- type: "text",
36491
- streamId: textStreamId,
36492
- streamDone: true,
36493
- text: streamText,
36494
- });
36495
- }
36496
- if (toolPart) {
36497
- await streamCallback.onMessage({
36498
- streamType: "chat",
36499
- chatId: chatContext.getChatId(),
36500
- messageId,
36501
- type: "tool_use",
36502
- toolCallId: toolPart.toolCallId,
36503
- toolName: toolPart.toolName,
36504
- params: toolPart.input || {},
36505
- });
36506
- toolPart = null;
36507
- }
36508
- await streamCallback.onMessage({
36509
- streamType: "chat",
36510
- chatId: chatContext.getChatId(),
36511
- messageId,
36512
- type: "finish",
36513
- finishReason: chunk.finishReason,
36514
- usage: {
36515
- promptTokens: chunk.usage.inputTokens || 0,
36516
- completionTokens: chunk.usage.outputTokens || 0,
36517
- totalTokens: chunk.usage.totalTokens ||
36518
- (chunk.usage.inputTokens || 0) +
36519
- (chunk.usage.outputTokens || 0),
36520
- },
36521
- });
36522
- break;
36523
- }
36524
- }
36525
- }
36526
- }
36527
- catch (e) {
36528
- if (retryNum < config$1.maxRetryNum) {
36529
- await sleep(200 * (retryNum + 1) * (retryNum + 1));
36530
- return callChatLLM(messageId, chatContext, rlm, messages, tools, toolChoice, ++retryNum, callback, signal);
36531
- }
36532
- throw e;
36533
- }
36534
- finally {
36535
- reader && reader.releaseLock();
36536
- }
36537
- return streamText
36538
- ? [
36539
- { type: "text", text: streamText },
36540
- ...toolParts,
36541
- ]
36542
- : toolParts;
36573
+ return await callLLM(rlm, request, async (message) => {
36574
+ await streamCallback.onMessage({
36575
+ streamType: "chat",
36576
+ chatId,
36577
+ messageId,
36578
+ ...message,
36579
+ });
36580
+ });
36543
36581
  }
36544
36582
  function convertAssistantToolResults(results) {
36545
36583
  return results.map((part) => {
@@ -36899,6 +36937,7 @@ class DeepActionTool {
36899
36937
  });
36900
36938
  const taskWebsite = await this.gettaskWebsite(tabIds);
36901
36939
  const workflow = await eko.generate(taskDescription, messageId, {
36940
+ ...(this.params.extra || {}),
36902
36941
  ...globalVariables,
36903
36942
  tabIds: tabIds,
36904
36943
  language: language,
@@ -37142,7 +37181,7 @@ class ChatAgent {
37142
37181
  const rlm = new RetryLanguageModel(config.llms, config.chatLlms);
37143
37182
  for (; reactLoopNum < 15; reactLoopNum++) {
37144
37183
  const messages = this.memory.buildMessages();
37145
- const results = await callChatLLM(params.messageId, this.chatContext, rlm, messages, convertTools(chatTools), undefined, 0, params.callback, params.signal);
37184
+ const results = await callChatLLM(this.chatContext.getChatId(), params.messageId, rlm, messages, convertTools(chatTools), undefined, params.callback, params.signal);
37146
37185
  const finalResult = await this.handleCallResult(params.messageId, chatTools, results, params.callback);
37147
37186
  if (finalResult) {
37148
37187
  return finalResult;
@@ -37332,5 +37371,5 @@ class ChatAgent {
37332
37371
  }
37333
37372
  }
37334
37373
 
37335
- export { Agent, AgentChain, AgentContext, BaseBrowserAgent, BaseBrowserLabelsAgent, BaseBrowserScreenAgent, Chain, ChatAgent, ChatContext, TaskContext as Context, DeepActionTool, Eko, EkoMemory, ForeachTaskTool, HumanInteractTool, Log, Planner, PromptTemplate, RetryLanguageModel, SimpleHttpMcpClient, SimpleSseMcpClient, TaskContext, TaskNodeStatusTool, TaskVariableStorageTool, VariableStorageTool, WatchTriggerTool, WebSearchTool, WebpageQaTool, buildAgentTree, buildSimpleAgentWorkflow, call_timeout, compressImageData, config$1 as config, convertToolSchema, Eko as default, extract_page_content, global, mergeTools, parseWorkflow, resetWorkflowXml, sub, toFile, toImage, uuidv4 };
37374
+ export { Agent, AgentChain, AgentContext, BaseBrowserAgent, BaseBrowserLabelsAgent, BaseBrowserScreenAgent, Chain, ChatAgent, ChatContext, TaskContext as Context, DeepActionTool, Eko, EkoMemory, ForeachTaskTool, HumanInteractTool, Log, Planner, PromptTemplate, RetryLanguageModel, SimpleHttpMcpClient, SimpleSseMcpClient, TaskContext, TaskNodeStatusTool, TaskVariableStorageTool, VariableStorageTool, WatchTriggerTool, WebSearchTool, WebpageQaTool, buildAgentTree, buildSimpleAgentWorkflow, callLLM, callWithReAct, call_timeout, compressImageData, config$1 as config, convertToolSchema, Eko as default, extract_page_content, global, mergeTools, parseWorkflow, resetWorkflowXml, sub, toFile, toImage, uuidv4 };
37336
37375
  //# sourceMappingURL=index.esm.js.map