@eko-ai/eko 4.0.4 → 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 (37) 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 +681 -666
  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 +680 -667
  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/types/agent.types.d.ts +2 -47
  28. package/dist/types/agent.types.d.ts.map +1 -1
  29. package/dist/types/chat.types.d.ts +3 -45
  30. package/dist/types/chat.types.d.ts.map +1 -1
  31. package/dist/types/config.types.d.ts +10 -10
  32. package/dist/types/config.types.d.ts.map +1 -1
  33. package/dist/types/index.d.ts +2 -2
  34. package/dist/types/index.d.ts.map +1 -1
  35. package/dist/types/llm.types.d.ts +51 -1
  36. package/dist/types/llm.types.d.ts.map +1 -1
  37. package/package.json +1 -1
package/dist/index.cjs.js CHANGED
@@ -399,9 +399,10 @@ async function compressImageData(imageBase64, imageType, compress, quality) {
399
399
  ctx: canvas.getContext("2d"),
400
400
  exportBase64: async (mime, q) => {
401
401
  const buffer = canvas.toBuffer(mime, { quality: q });
402
- return ((
402
+ const _Buffer =
403
403
  // @ts-ignore
404
- typeof Buffer !== "undefined" ? Buffer.from(buffer) : buffer).toString("base64"));
404
+ typeof Buffer !== "undefined" ? Buffer.from(buffer) : buffer;
405
+ return _Buffer.toString("base64");
405
406
  },
406
407
  };
407
408
  }
@@ -481,13 +482,19 @@ function mergeAgents(agents1, agents2) {
481
482
  }
482
483
  return agents;
483
484
  }
484
- function sub(str, maxLength, appendPoint = true) {
485
+ function sub(str, maxLength, appendPoint = true, showTruncated = true) {
485
486
  if (!str) {
486
487
  return "";
487
488
  }
488
489
  if (str.length > maxLength) {
489
- // return str.substring(0, maxLength) + (appendPoint ? "..." : "");
490
- return (Array.from(str).slice(0, maxLength).join("") + (appendPoint ? "..." : ""));
490
+ const truncatedLength = str.length - maxLength;
491
+ // return str.substring(0, maxLength) + (appendPoint ? showTruncated ? `...(truncated: +${truncatedLength} chars)` : "..." : "");
492
+ return (Array.from(str).slice(0, maxLength).join("") +
493
+ (appendPoint
494
+ ? showTruncated
495
+ ? `...(truncated: +${truncatedLength} chars)`
496
+ : "..."
497
+ : ""));
491
498
  }
492
499
  return str;
493
500
  }
@@ -30196,7 +30203,7 @@ function convertToolResult(toolUse, toolResult, user_messages) {
30196
30203
  output: result,
30197
30204
  };
30198
30205
  }
30199
- async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, retryNum = 0, callback, requestHandler) {
30206
+ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, callback, requestHandler) {
30200
30207
  await agentContext.context.checkAborted();
30201
30208
  if (!noCompress &&
30202
30209
  (messages.length >= config$1.compressThreshold ||
@@ -30229,314 +30236,47 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
30229
30236
  abortSignal: signal,
30230
30237
  };
30231
30238
  requestHandler && requestHandler(request);
30232
- let streamText = "";
30233
- let thinkText = "";
30234
- let toolArgsText = "";
30235
- let textStreamId = uuidv4();
30236
- let thinkStreamId = uuidv4();
30237
- let textStreamDone = false;
30238
- const toolParts = [];
30239
- let reader = null;
30240
30239
  try {
30241
30240
  agentChain.agentRequest = request;
30242
30241
  context.currentStepControllers.add(stepController);
30243
- const result = await rlm.callStream(request);
30244
- reader = result.stream.getReader();
30245
- let toolPart = null;
30246
- while (true) {
30242
+ const result = await callLLM(rlm, request, async (message) => {
30247
30243
  await context.checkAborted();
30248
- const { done, value } = await reader.read();
30249
- if (done) {
30250
- break;
30244
+ await streamCallback.onMessage({
30245
+ streamType: "agent",
30246
+ chatId: context.chatId,
30247
+ taskId: context.taskId,
30248
+ agentName: agentNode.name,
30249
+ nodeId: agentNode.id,
30250
+ ...message,
30251
+ });
30252
+ }, async (request, error) => {
30253
+ if ((error + "").indexOf("is too long") > -1) {
30254
+ await compressAgentMessages(agentContext, messages, tools);
30251
30255
  }
30252
- const chunk = value;
30253
- switch (chunk.type) {
30254
- case "text-start": {
30255
- textStreamId = uuidv4();
30256
- break;
30257
- }
30258
- case "text-delta": {
30259
- if (toolPart && !chunk.delta) {
30260
- continue;
30261
- }
30262
- streamText += chunk.delta || "";
30263
- await streamCallback.onMessage({
30264
- streamType: "agent",
30265
- chatId: context.chatId,
30266
- taskId: context.taskId,
30267
- agentName: agentNode.name,
30268
- nodeId: agentNode.id,
30269
- type: "text",
30270
- streamId: textStreamId,
30271
- streamDone: false,
30272
- text: streamText,
30273
- }, agentContext);
30274
- if (toolPart) {
30275
- await streamCallback.onMessage({
30276
- streamType: "agent",
30277
- chatId: context.chatId,
30278
- taskId: context.taskId,
30279
- agentName: agentNode.name,
30280
- nodeId: agentNode.id,
30281
- type: "tool_use",
30282
- toolCallId: toolPart.toolCallId,
30283
- toolName: toolPart.toolName,
30284
- params: toolPart.input || {},
30285
- }, agentContext);
30286
- toolPart = null;
30287
- }
30288
- break;
30289
- }
30290
- case "text-end": {
30291
- textStreamDone = true;
30292
- if (streamText) {
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: "text",
30300
- streamId: textStreamId,
30301
- streamDone: true,
30302
- text: streamText,
30303
- }, agentContext);
30304
- }
30305
- break;
30306
- }
30307
- case "reasoning-start": {
30308
- thinkStreamId = uuidv4();
30309
- break;
30310
- }
30311
- case "reasoning-delta": {
30312
- thinkText += chunk.delta || "";
30313
- await streamCallback.onMessage({
30314
- streamType: "agent",
30315
- chatId: context.chatId,
30316
- taskId: context.taskId,
30317
- agentName: agentNode.name,
30318
- nodeId: agentNode.id,
30319
- type: "thinking",
30320
- streamId: thinkStreamId,
30321
- streamDone: false,
30322
- text: thinkText,
30323
- }, agentContext);
30324
- break;
30325
- }
30326
- case "reasoning-end": {
30327
- if (thinkText) {
30328
- await streamCallback.onMessage({
30329
- streamType: "agent",
30330
- chatId: context.chatId,
30331
- taskId: context.taskId,
30332
- agentName: agentNode.name,
30333
- nodeId: agentNode.id,
30334
- type: "thinking",
30335
- streamId: thinkStreamId,
30336
- streamDone: true,
30337
- text: thinkText,
30338
- }, agentContext);
30339
- }
30340
- break;
30341
- }
30342
- case "tool-input-start": {
30343
- if (toolPart && toolPart.toolCallId == chunk.id) {
30344
- toolPart.toolName = chunk.toolName;
30345
- }
30346
- else {
30347
- const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.id)[0];
30348
- if (_toolPart) {
30349
- toolPart = _toolPart;
30350
- toolPart.toolName = _toolPart.toolName || chunk.toolName;
30351
- toolPart.input = _toolPart.input || {};
30352
- }
30353
- else {
30354
- toolPart = {
30355
- type: "tool-call",
30356
- toolCallId: chunk.id,
30357
- toolName: chunk.toolName,
30358
- input: {},
30359
- };
30360
- toolParts.push(toolPart);
30361
- }
30362
- }
30363
- break;
30364
- }
30365
- case "tool-input-delta": {
30366
- if (!textStreamDone) {
30367
- textStreamDone = true;
30368
- await streamCallback.onMessage({
30369
- streamType: "agent",
30370
- chatId: context.chatId,
30371
- taskId: context.taskId,
30372
- agentName: agentNode.name,
30373
- nodeId: agentNode.id,
30374
- type: "text",
30375
- streamId: textStreamId,
30376
- streamDone: true,
30377
- text: streamText,
30378
- }, agentContext);
30379
- }
30380
- toolArgsText += chunk.delta || "";
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: "tool_streaming",
30388
- toolCallId: chunk.id,
30389
- toolName: toolPart?.toolName || "",
30390
- paramsText: toolArgsText,
30391
- }, agentContext);
30392
- break;
30393
- }
30394
- case "tool-call": {
30395
- toolArgsText = "";
30396
- const args = chunk.input ? JSON.parse(chunk.input) : {};
30397
- const message = {
30398
- streamType: "agent",
30399
- chatId: context.chatId,
30400
- taskId: context.taskId,
30401
- agentName: agentNode.name,
30402
- nodeId: agentNode.id,
30403
- type: "tool_use",
30404
- toolCallId: chunk.toolCallId,
30405
- toolName: chunk.toolName,
30406
- params: args,
30407
- };
30408
- await streamCallback.onMessage(message, agentContext);
30409
- if (toolPart == null) {
30410
- const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.toolCallId)[0];
30411
- if (_toolPart) {
30412
- _toolPart.input = message.params || args;
30413
- }
30414
- else {
30415
- toolParts.push({
30416
- type: "tool-call",
30417
- toolCallId: chunk.toolCallId,
30418
- toolName: chunk.toolName,
30419
- input: message.params || args,
30420
- });
30421
- }
30422
- }
30423
- else {
30424
- toolPart.input = message.params || args;
30425
- toolPart = null;
30426
- }
30427
- break;
30428
- }
30429
- case "file": {
30430
- await streamCallback.onMessage({
30431
- streamType: "agent",
30432
- chatId: context.chatId,
30433
- taskId: context.taskId,
30434
- agentName: agentNode.name,
30435
- nodeId: agentNode.id,
30436
- type: "file",
30437
- mimeType: chunk.mediaType,
30438
- data: chunk.data,
30439
- }, agentContext);
30440
- break;
30441
- }
30442
- case "error": {
30443
- Log.error(`${agentNode.name} agent error: `, chunk);
30444
- await streamCallback.onMessage({
30445
- streamType: "agent",
30446
- chatId: context.chatId,
30447
- taskId: context.taskId,
30448
- agentName: agentNode.name,
30449
- nodeId: agentNode.id,
30450
- type: "error",
30451
- error: chunk.error,
30452
- }, agentContext);
30453
- throw new Error("LLM Error: " + chunk.error);
30454
- }
30455
- case "finish": {
30456
- if (!textStreamDone) {
30457
- textStreamDone = true;
30458
- await streamCallback.onMessage({
30459
- streamType: "agent",
30460
- chatId: context.chatId,
30461
- taskId: context.taskId,
30462
- agentName: agentNode.name,
30463
- nodeId: agentNode.id,
30464
- type: "text",
30465
- streamId: textStreamId,
30466
- streamDone: true,
30467
- text: streamText,
30468
- }, agentContext);
30469
- }
30470
- if (chunk.finishReason === "content-filter") {
30471
- throw new Error("LLM error: trigger content filtering violation");
30472
- }
30473
- else if (chunk.finishReason === "other") {
30474
- throw new Error("LLM error: terminated due to other reasons");
30475
- }
30476
- else if (chunk.finishReason === "length" &&
30477
- messages.length >= 3 &&
30478
- !noCompress &&
30479
- retryNum < config$1.maxRetryNum) {
30480
- await compressAgentMessages(agentContext, messages, tools);
30481
- return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
30482
- }
30483
- if (toolPart) {
30484
- await streamCallback.onMessage({
30485
- streamType: "agent",
30486
- chatId: context.chatId,
30487
- taskId: context.taskId,
30488
- agentName: agentNode.name,
30489
- nodeId: agentNode.id,
30490
- type: "tool_use",
30491
- toolCallId: toolPart.toolCallId,
30492
- toolName: toolPart.toolName,
30493
- params: toolPart.input || {},
30494
- }, agentContext);
30495
- toolPart = null;
30496
- }
30497
- await streamCallback.onMessage({
30498
- streamType: "agent",
30499
- chatId: context.chatId,
30500
- taskId: context.taskId,
30501
- agentName: agentNode.name,
30502
- nodeId: agentNode.id,
30503
- type: "finish",
30504
- finishReason: chunk.finishReason,
30505
- usage: {
30506
- promptTokens: chunk.usage.inputTokens || 0,
30507
- completionTokens: chunk.usage.outputTokens || 0,
30508
- totalTokens: chunk.usage.totalTokens ||
30509
- (chunk.usage.inputTokens || 0) +
30510
- (chunk.usage.outputTokens || 0),
30511
- },
30512
- }, agentContext);
30513
- break;
30514
- }
30256
+ }, async (request, finishReason, value, retryNum) => {
30257
+ if (finishReason === "content-filter") {
30258
+ throw new Error("LLM error: trigger content filtering violation");
30515
30259
  }
30516
- }
30517
- }
30518
- catch (e) {
30519
- await context.checkAborted();
30520
- if (retryNum < config$1.maxRetryNum) {
30521
- await sleep(300 * (retryNum + 1) * (retryNum + 1));
30522
- if ((e + "").indexOf("is too long") > -1) {
30260
+ else if (finishReason === "other") {
30261
+ throw new Error("LLM error: terminated due to other reasons");
30262
+ }
30263
+ else if (finishReason === "length" &&
30264
+ messages.length >= 3 &&
30265
+ !noCompress &&
30266
+ retryNum < config$1.maxRetryNum) {
30523
30267
  await compressAgentMessages(agentContext, messages, tools);
30268
+ return "retry";
30524
30269
  }
30525
- return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
30526
- }
30527
- throw e;
30270
+ });
30271
+ agentChain.agentResult = result
30272
+ .filter((s) => s.type == "text")
30273
+ .map((s) => s.text)
30274
+ .join("\n\n");
30275
+ return result;
30528
30276
  }
30529
30277
  finally {
30530
- reader && reader.releaseLock();
30531
30278
  context.currentStepControllers.delete(stepController);
30532
30279
  }
30533
- agentChain.agentResult = streamText;
30534
- return streamText
30535
- ? [
30536
- { type: "text", text: streamText },
30537
- ...toolParts,
30538
- ]
30539
- : toolParts;
30540
30280
  }
30541
30281
  function estimatePromptTokens(messages, tools) {
30542
30282
  let tokens = messages.reduce((total, message) => {
@@ -31037,22 +30777,309 @@ class RetryLanguageModel {
31037
30777
  }
31038
30778
  }
31039
30779
 
30780
+ async function callWithReAct(rlm, request, toolCallCallback, streamCallback, errorHandler, finishHandler, loopControl) {
30781
+ if (!loopControl) {
30782
+ loopControl = async (request, assistantParts, loopNum) => {
30783
+ if (loopNum >= 15) {
30784
+ return false;
30785
+ }
30786
+ return assistantParts.filter((s) => s.type == "tool-call").length > 0;
30787
+ };
30788
+ }
30789
+ let loopNum = 0;
30790
+ let assistantParts = null;
30791
+ while (true) {
30792
+ assistantParts = await callLLM(rlm, request, streamCallback, errorHandler, finishHandler);
30793
+ if (assistantParts.length > 0) {
30794
+ request.messages.push({
30795
+ role: "assistant",
30796
+ content: assistantParts
30797
+ .filter((part) => part.type == "text" || part.type == "tool-call")
30798
+ .map((part) => part.type === "text"
30799
+ ? {
30800
+ type: "text",
30801
+ text: part.text,
30802
+ }
30803
+ : {
30804
+ type: "tool-call",
30805
+ toolCallId: part.toolCallId,
30806
+ toolName: part.toolName,
30807
+ input: JSON.parse((part.input || "{}")),
30808
+ }),
30809
+ });
30810
+ }
30811
+ const continueLoop = await loopControl(request, assistantParts, loopNum);
30812
+ if (!continueLoop) {
30813
+ break;
30814
+ }
30815
+ const toolUses = assistantParts.filter((s) => s.type == "tool-call");
30816
+ const toolResults = await toolCallCallback(request, toolUses);
30817
+ if (toolResults.length > 0) {
30818
+ request.messages.push({
30819
+ role: "tool",
30820
+ content: toolResults.map((result, index) => ({
30821
+ type: "tool-result",
30822
+ toolCallId: toolUses[index].toolCallId,
30823
+ toolName: toolUses[index].toolName,
30824
+ output: result,
30825
+ })),
30826
+ });
30827
+ }
30828
+ loopNum++;
30829
+ }
30830
+ return assistantParts;
30831
+ }
30832
+ async function callLLM(rlm, request, streamCallback, errorHandler, finishHandler, retryNum = 0) {
30833
+ let streamText = "";
30834
+ let thinkText = "";
30835
+ let toolArgsText = "";
30836
+ let textStreamId = uuidv4();
30837
+ let thinkStreamId = uuidv4();
30838
+ let textStreamDone = false;
30839
+ const toolParts = [];
30840
+ let reader = null;
30841
+ try {
30842
+ const result = await rlm.callStream(request);
30843
+ reader = result.stream.getReader();
30844
+ let toolPart = null;
30845
+ while (true) {
30846
+ const { done, value } = await reader.read();
30847
+ if (done) {
30848
+ break;
30849
+ }
30850
+ const chunk = value;
30851
+ switch (chunk.type) {
30852
+ case "text-start": {
30853
+ textStreamId = uuidv4();
30854
+ break;
30855
+ }
30856
+ case "text-delta": {
30857
+ if (toolPart && !chunk.delta) {
30858
+ continue;
30859
+ }
30860
+ streamText += chunk.delta || "";
30861
+ await streamCallback?.({
30862
+ type: "text",
30863
+ streamId: textStreamId,
30864
+ streamDone: false,
30865
+ text: streamText,
30866
+ });
30867
+ if (toolPart) {
30868
+ await streamCallback?.({
30869
+ type: "tool_use",
30870
+ toolCallId: toolPart.toolCallId,
30871
+ toolName: toolPart.toolName,
30872
+ params: toolPart.input || {},
30873
+ });
30874
+ toolPart = null;
30875
+ }
30876
+ break;
30877
+ }
30878
+ case "text-end": {
30879
+ textStreamDone = true;
30880
+ if (streamText) {
30881
+ await streamCallback?.({
30882
+ type: "text",
30883
+ streamId: textStreamId,
30884
+ streamDone: true,
30885
+ text: streamText,
30886
+ });
30887
+ }
30888
+ break;
30889
+ }
30890
+ case "reasoning-start": {
30891
+ thinkStreamId = uuidv4();
30892
+ break;
30893
+ }
30894
+ case "reasoning-delta": {
30895
+ thinkText += chunk.delta || "";
30896
+ await streamCallback?.({
30897
+ type: "thinking",
30898
+ streamId: thinkStreamId,
30899
+ streamDone: false,
30900
+ text: thinkText,
30901
+ });
30902
+ break;
30903
+ }
30904
+ case "reasoning-end": {
30905
+ if (thinkText) {
30906
+ await streamCallback?.({
30907
+ type: "thinking",
30908
+ streamId: thinkStreamId,
30909
+ streamDone: true,
30910
+ text: thinkText,
30911
+ });
30912
+ }
30913
+ break;
30914
+ }
30915
+ case "tool-input-start": {
30916
+ if (toolPart && toolPart.toolCallId == chunk.id) {
30917
+ toolPart.toolName = chunk.toolName;
30918
+ }
30919
+ else {
30920
+ const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.id)[0];
30921
+ if (_toolPart) {
30922
+ toolPart = _toolPart;
30923
+ toolPart.toolName = _toolPart.toolName || chunk.toolName;
30924
+ toolPart.input = _toolPart.input || {};
30925
+ }
30926
+ else {
30927
+ toolPart = {
30928
+ type: "tool-call",
30929
+ toolCallId: chunk.id,
30930
+ toolName: chunk.toolName,
30931
+ input: {},
30932
+ };
30933
+ toolParts.push(toolPart);
30934
+ }
30935
+ }
30936
+ break;
30937
+ }
30938
+ case "tool-input-delta": {
30939
+ if (!textStreamDone) {
30940
+ textStreamDone = true;
30941
+ await streamCallback?.({
30942
+ type: "text",
30943
+ streamId: textStreamId,
30944
+ streamDone: true,
30945
+ text: streamText,
30946
+ });
30947
+ }
30948
+ toolArgsText += chunk.delta || "";
30949
+ await streamCallback?.({
30950
+ type: "tool_streaming",
30951
+ toolCallId: chunk.id,
30952
+ toolName: toolPart?.toolName || "",
30953
+ paramsText: toolArgsText,
30954
+ });
30955
+ break;
30956
+ }
30957
+ case "tool-call": {
30958
+ toolArgsText = "";
30959
+ const args = chunk.input ? JSON.parse(chunk.input) : {};
30960
+ const message = {
30961
+ type: "tool_use",
30962
+ toolCallId: chunk.toolCallId,
30963
+ toolName: chunk.toolName,
30964
+ params: args,
30965
+ };
30966
+ await streamCallback?.(message);
30967
+ if (toolPart == null) {
30968
+ const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.toolCallId)[0];
30969
+ if (_toolPart) {
30970
+ _toolPart.input = message.params || args;
30971
+ }
30972
+ else {
30973
+ toolParts.push({
30974
+ type: "tool-call",
30975
+ toolCallId: chunk.toolCallId,
30976
+ toolName: chunk.toolName,
30977
+ input: message.params || args,
30978
+ });
30979
+ }
30980
+ }
30981
+ else {
30982
+ toolPart.input = message.params || args;
30983
+ toolPart = null;
30984
+ }
30985
+ break;
30986
+ }
30987
+ case "file": {
30988
+ await streamCallback?.({
30989
+ type: "file",
30990
+ mimeType: chunk.mediaType,
30991
+ data: chunk.data,
30992
+ });
30993
+ break;
30994
+ }
30995
+ case "error": {
30996
+ Log.error(`chatLLM error: `, chunk);
30997
+ await streamCallback?.({
30998
+ type: "error",
30999
+ error: chunk.error,
31000
+ });
31001
+ throw new Error("LLM Error: " + chunk.error);
31002
+ }
31003
+ case "finish": {
31004
+ if (!textStreamDone) {
31005
+ textStreamDone = true;
31006
+ await streamCallback?.({
31007
+ type: "text",
31008
+ streamId: textStreamId,
31009
+ streamDone: true,
31010
+ text: streamText,
31011
+ });
31012
+ }
31013
+ if (toolPart) {
31014
+ await streamCallback?.({
31015
+ type: "tool_use",
31016
+ toolCallId: toolPart.toolCallId,
31017
+ toolName: toolPart.toolName,
31018
+ params: toolPart.input || {},
31019
+ });
31020
+ toolPart = null;
31021
+ }
31022
+ if (finishHandler) {
31023
+ const type = await finishHandler(request, chunk.finishReason, chunk, retryNum);
31024
+ if (type == "retry") {
31025
+ await sleep(200 * (retryNum + 1) * (retryNum + 1));
31026
+ return callLLM(rlm, request, streamCallback, errorHandler, finishHandler, ++retryNum);
31027
+ }
31028
+ }
31029
+ await streamCallback?.({
31030
+ type: "finish",
31031
+ finishReason: chunk.finishReason,
31032
+ usage: {
31033
+ promptTokens: chunk.usage.inputTokens || 0,
31034
+ completionTokens: chunk.usage.outputTokens || 0,
31035
+ totalTokens: chunk.usage.totalTokens ||
31036
+ (chunk.usage.inputTokens || 0) +
31037
+ (chunk.usage.outputTokens || 0),
31038
+ },
31039
+ });
31040
+ break;
31041
+ }
31042
+ }
31043
+ }
31044
+ }
31045
+ catch (e) {
31046
+ if (retryNum < config$1.maxRetryNum) {
31047
+ await sleep(200 * (retryNum + 1) * (retryNum + 1));
31048
+ if (errorHandler) {
31049
+ await errorHandler(request, e, retryNum);
31050
+ }
31051
+ return callLLM(rlm, request, streamCallback, errorHandler, finishHandler, ++retryNum);
31052
+ }
31053
+ throw e;
31054
+ }
31055
+ finally {
31056
+ reader && reader.releaseLock();
31057
+ }
31058
+ return streamText
31059
+ ? [
31060
+ { type: "text", text: streamText },
31061
+ ...toolParts,
31062
+ ]
31063
+ : toolParts;
31064
+ }
31065
+
31040
31066
  const global = {
31041
31067
  chatMap: new Map(),
31042
31068
  taskMap: new Map(),
31043
31069
  prompts: new Map(),
31044
31070
  };
31045
31071
 
31046
- const GlobalPromptKey = {
31047
- planner_system: "planner_system",
31048
- planner_example: "planner_example",
31049
- planner_user: "planner_user",
31050
- agent_system: "agent_system",
31051
- chat_system: "chat_system",
31052
- webpage_qa_prompt: "webpage_qa_prompt",
31053
- deep_action_description: "deep_action_description",
31054
- deep_action_param_task_description: "deep_action_param_task_description",
31055
- };
31072
+ var GlobalPromptKey;
31073
+ (function (GlobalPromptKey) {
31074
+ GlobalPromptKey["planner_system"] = "planner_system";
31075
+ GlobalPromptKey["planner_example"] = "planner_example";
31076
+ GlobalPromptKey["planner_user"] = "planner_user";
31077
+ GlobalPromptKey["agent_system"] = "agent_system";
31078
+ GlobalPromptKey["chat_system"] = "chat_system";
31079
+ GlobalPromptKey["webpage_qa_prompt"] = "webpage_qa_prompt";
31080
+ GlobalPromptKey["deep_action_description"] = "deep_action_description";
31081
+ GlobalPromptKey["deep_action_param_task_description"] = "deep_action_param_task_description";
31082
+ })(GlobalPromptKey || (GlobalPromptKey = {}));
31056
31083
 
31057
31084
  class PromptTemplate {
31058
31085
  /**
@@ -33115,7 +33142,7 @@ class Agent {
33115
33142
  }
33116
33143
  await this.handleMessages(agentContext, messages, tools);
33117
33144
  const llm_tools = convertTools(agentTools);
33118
- const results = await callAgentLLM(agentContext, rlm, messages, llm_tools, false, undefined, 0, this.callback, this.requestHandler);
33145
+ const results = await callAgentLLM(agentContext, rlm, messages, llm_tools, false, undefined, this.callback, this.requestHandler);
33119
33146
  const forceStop = agentContext.variables.get("forceStop");
33120
33147
  if (forceStop) {
33121
33148
  return forceStop;
@@ -33387,123 +33414,344 @@ class Agent {
33387
33414
  }
33388
33415
  }
33389
33416
 
33390
- function extract_page_content(max_url_length = 200, max_content_length = 50000) {
33391
- let result = "";
33392
- max_url_length = max_url_length || 200;
33393
- try {
33394
- function traverse(node) {
33395
- if (node.nodeType === Node.ELEMENT_NODE) {
33396
- const tagName = node.tagName.toLowerCase();
33397
- if (["script", "style", "noscript"].includes(tagName)) {
33398
- return;
33417
+ function extract_page_content(params) {
33418
+ params = params || {};
33419
+ const IGNORED_TAGS = new Set(params.ignored_tags || ["script", "style", "noscript", "svg", "canvas"]);
33420
+ const FORM_TAGS = new Set(["input", "select", "textarea"]);
33421
+ const KEY_ATTRIBUTES = new Set(params.key_attributes || [
33422
+ "id",
33423
+ "title",
33424
+ "name",
33425
+ "alt",
33426
+ "src",
33427
+ "url",
33428
+ "href",
33429
+ "value",
33430
+ "checked",
33431
+ "selected",
33432
+ ]);
33433
+ const urlLimit = params.max_url_length || 200;
33434
+ const contentLimit = params.max_content_length || 50000;
33435
+ const minImageArea = params.min_image_area || 1600;
33436
+ const parts = [];
33437
+ let currentLength = 0;
33438
+ const escapeHtml = (text) => {
33439
+ const map = {
33440
+ "<": "&lt;",
33441
+ ">": "&gt;",
33442
+ };
33443
+ return text.replace(/[<>]/g, (m) => map[m] || m);
33444
+ };
33445
+ const getKeyAttributes = (element) => {
33446
+ const attrs = {};
33447
+ const attributes = element.attributes;
33448
+ for (let i = 0; i < attributes.length; i++) {
33449
+ const attr = attributes[i];
33450
+ const name = attr.name.toLowerCase();
33451
+ if (KEY_ATTRIBUTES.has(name)) {
33452
+ const value = attr.value?.trim();
33453
+ if (value) {
33454
+ attrs[name] = value;
33399
33455
  }
33400
- const style = window.getComputedStyle(node);
33401
- if (style.display == "none" ||
33402
- style.visibility == "hidden" ||
33403
- style.opacity == "0") {
33404
- return;
33456
+ }
33457
+ }
33458
+ if (element instanceof HTMLInputElement) {
33459
+ const inputType = element.type.toLowerCase();
33460
+ if (inputType === "checkbox" || inputType === "radio") {
33461
+ if (element.checked) {
33462
+ attrs.checked = "true";
33405
33463
  }
33406
33464
  }
33407
- if (node.nodeType === Node.TEXT_NODE) {
33408
- // text
33409
- const text = node.textContent.trim();
33410
- if (text) {
33411
- result += text + " ";
33412
- }
33413
- }
33414
- else if (node.nodeType === Node.ELEMENT_NODE) {
33415
- const tagName = node.tagName.toLowerCase();
33416
- if (["input", "select", "textarea"].includes(tagName)) {
33417
- // input / select / textarea
33418
- if (tagName == "input" && node.type == "checkbox") {
33419
- result += node.checked + " ";
33420
- }
33421
- else if (tagName == "input" && node.type == "radio") {
33422
- if (node.checked && node.value) {
33423
- result += node.value + " ";
33424
- }
33425
- }
33426
- else if (node.value) {
33427
- result += node.value + " ";
33465
+ if (element.value && !attrs.value) {
33466
+ attrs.value = element.value;
33467
+ }
33468
+ if (element.name && !attrs.name) {
33469
+ attrs.name = element.name;
33470
+ }
33471
+ if (attrs.value || Object.keys(attrs).length > 0) {
33472
+ attrs.type = inputType;
33473
+ }
33474
+ }
33475
+ else if (element instanceof HTMLSelectElement) {
33476
+ if (element.selectedIndex >= 0) {
33477
+ const selectedOption = element.options[element.selectedIndex];
33478
+ if (selectedOption) {
33479
+ attrs.selected = String(element.selectedIndex);
33480
+ if (selectedOption.value && !attrs.value) {
33481
+ attrs.value = selectedOption.value;
33428
33482
  }
33429
33483
  }
33430
- else if (tagName === "img") {
33431
- // image
33432
- const src = node.src ||
33433
- node.getAttribute("src") ||
33434
- node.getAttribute("data-src");
33435
- const alt = node.alt || node.title || "";
33436
- if (src &&
33437
- src.length <= max_url_length &&
33438
- node.width * node.height >= 10000 &&
33439
- src.startsWith("http")) {
33440
- result += `![${alt ? alt : "image"}](${src.trim()}) `;
33441
- }
33484
+ }
33485
+ if (element.name && !attrs.name) {
33486
+ attrs.name = element.name;
33487
+ }
33488
+ }
33489
+ else if (element instanceof HTMLTextAreaElement) {
33490
+ if (element.value && !attrs.value) {
33491
+ attrs.value = element.value;
33492
+ }
33493
+ if (element.name && !attrs.name) {
33494
+ attrs.name = element.name;
33495
+ }
33496
+ }
33497
+ else if (element instanceof HTMLImageElement) {
33498
+ const src = element.src ||
33499
+ element.getAttribute("src") ||
33500
+ element.getAttribute("data-src");
33501
+ if (src && !attrs.src) {
33502
+ attrs.src = src;
33503
+ }
33504
+ if (element.alt && !attrs.alt) {
33505
+ attrs.alt = element.alt;
33506
+ }
33507
+ }
33508
+ else if (element instanceof HTMLAnchorElement) {
33509
+ if (element.href && !attrs.href) {
33510
+ attrs.href = element.href;
33511
+ }
33512
+ if (element.title && !attrs.title) {
33513
+ attrs.title = element.title;
33514
+ }
33515
+ }
33516
+ else if (element instanceof HTMLVideoElement ||
33517
+ element instanceof HTMLAudioElement) {
33518
+ const src = element.src || element.getAttribute("src");
33519
+ if (src && !attrs.src) {
33520
+ attrs.src = src;
33521
+ }
33522
+ }
33523
+ return attrs;
33524
+ };
33525
+ const buildAttributesString = (attrs) => {
33526
+ if (Object.keys(attrs).length === 0) {
33527
+ return "";
33528
+ }
33529
+ const attrStrings = [];
33530
+ for (const [key, value] of Object.entries(attrs)) {
33531
+ if (value) {
33532
+ attrStrings.push(`${key}="${escapeHtml(value)}"`);
33533
+ }
33534
+ }
33535
+ return attrStrings.length > 0 ? " " + attrStrings.join(" ") : "";
33536
+ };
33537
+ const hasKeyAttributes = (attrs) => {
33538
+ return Object.keys(attrs).length > 0;
33539
+ };
33540
+ const addHtmlContent = (content) => {
33541
+ if (!content || currentLength >= contentLimit) {
33542
+ return false;
33543
+ }
33544
+ const contentLength = content.length;
33545
+ if (currentLength + contentLength > contentLimit) {
33546
+ const remaining = contentLimit - currentLength;
33547
+ if (remaining > 0) {
33548
+ parts.push(content.slice(0, remaining));
33549
+ }
33550
+ return false;
33551
+ }
33552
+ currentLength += contentLength;
33553
+ parts.push(content);
33554
+ return true;
33555
+ };
33556
+ const hasDirectTextChild = (node) => {
33557
+ for (const child of node.childNodes) {
33558
+ if (child.nodeType === Node.TEXT_NODE) {
33559
+ const text = child.textContent || "";
33560
+ if (text.trim()) {
33561
+ return true;
33442
33562
  }
33443
- else if (tagName === "a" && node.children.length == 0) {
33444
- // link
33445
- const href = node.href || node.getAttribute("href");
33446
- const text = node.innerText.trim() || node.title;
33447
- if (text &&
33448
- href &&
33449
- href.length <= max_url_length &&
33450
- href.startsWith("http")) {
33451
- result += `[${text}](${href.trim()}) `;
33452
- }
33453
- else {
33454
- result += text + " ";
33563
+ }
33564
+ }
33565
+ return false;
33566
+ };
33567
+ const traverse = (node) => {
33568
+ if (currentLength >= contentLimit) {
33569
+ return "";
33570
+ }
33571
+ if (node.nodeType === Node.TEXT_NODE) {
33572
+ const text = node.textContent || "";
33573
+ const trimmed = text.trim();
33574
+ if (trimmed) {
33575
+ return escapeHtml(trimmed);
33576
+ }
33577
+ return "";
33578
+ }
33579
+ if (node.nodeType !== Node.ELEMENT_NODE) {
33580
+ return "";
33581
+ }
33582
+ const element = node;
33583
+ const tagName = element.tagName.toLowerCase();
33584
+ if (IGNORED_TAGS.has(tagName)) {
33585
+ return "";
33586
+ }
33587
+ try {
33588
+ const style = window.getComputedStyle(element);
33589
+ if (style.display === "none") {
33590
+ return "";
33591
+ }
33592
+ }
33593
+ catch (e) { }
33594
+ const attrs = getKeyAttributes(element);
33595
+ const attrsString = buildAttributesString(attrs);
33596
+ const hasKeyAttrs = hasKeyAttributes(attrs);
33597
+ const hasDirectText = hasDirectTextChild(node);
33598
+ if (FORM_TAGS.has(tagName)) {
33599
+ let value = "";
33600
+ if (tagName === "input") {
33601
+ const input = element;
33602
+ const inputType = input.type.toLowerCase();
33603
+ if (inputType === "checkbox" || inputType === "radio") {
33604
+ if (!input.checked) {
33605
+ return "";
33455
33606
  }
33607
+ value = input.value || String(input.checked);
33456
33608
  }
33457
- else if (tagName === "video" || tagName == "audio") {
33458
- // video / audio
33459
- let src = node.src || node.getAttribute("src");
33460
- const sources = node.querySelectorAll("source");
33461
- if (sources.length > 0 && sources[0].src) {
33462
- src = sources[0].src;
33463
- if (src && src.startsWith("http") && sources[0].type) {
33464
- result += sources[0].type + " ";
33465
- }
33466
- }
33467
- if (src && src.startsWith("http")) {
33468
- result += src.trim() + " ";
33469
- }
33609
+ else if (inputType === "password") {
33610
+ return "";
33470
33611
  }
33471
- else if (tagName === "br") {
33472
- // br
33473
- result += "\n";
33612
+ else {
33613
+ value = input.value || "";
33474
33614
  }
33475
- else if (["p", "div", "h1", "h2", "h3", "h4", "h5", "h6"].includes(tagName)) {
33476
- // block
33477
- result += "\n";
33478
- for (let child of node.childNodes) {
33479
- traverse(child);
33480
- }
33481
- result += "\n";
33482
- return;
33615
+ }
33616
+ else if (tagName === "select") {
33617
+ const select = element;
33618
+ if (select.selectedIndex >= 0) {
33619
+ const selectedOption = select.options[select.selectedIndex];
33620
+ value = selectedOption
33621
+ ? selectedOption.text || selectedOption.value
33622
+ : "";
33483
33623
  }
33484
- else if (tagName === "hr") {
33485
- // hr
33486
- result += "\n--------\n";
33624
+ }
33625
+ else if (tagName === "textarea") {
33626
+ value = element.value || "";
33627
+ }
33628
+ if (value || hasKeyAttrs || tagName === "textarea") {
33629
+ const escapedValue = escapeHtml(value);
33630
+ return `<${tagName}${attrsString}>${escapedValue}</${tagName}>`;
33631
+ }
33632
+ return "";
33633
+ }
33634
+ if (tagName === "img") {
33635
+ const img = element;
33636
+ const src = img.src || img.getAttribute("src") || img.getAttribute("data-src");
33637
+ if (src &&
33638
+ src.length <= urlLimit &&
33639
+ img.width * img.height >= minImageArea &&
33640
+ src.startsWith("http")) {
33641
+ if (!attrs.alt && (img.alt || img.title)) {
33642
+ attrs.alt = img.alt || img.title || "";
33487
33643
  }
33488
- else {
33489
- // recursive
33490
- for (let child of node.childNodes) {
33491
- traverse(child);
33492
- }
33644
+ if (!attrs.src) {
33645
+ attrs.src = src.trim();
33493
33646
  }
33647
+ const imgAttrsString = buildAttributesString(attrs);
33648
+ return `<img${imgAttrsString} />`;
33649
+ }
33650
+ return "";
33651
+ }
33652
+ if (tagName === "a") {
33653
+ const anchor = element;
33654
+ const href = anchor.href || anchor.getAttribute("href");
33655
+ const childContent = [];
33656
+ for (const child of node.childNodes) {
33657
+ const content = traverse(child);
33658
+ if (content) {
33659
+ childContent.push(content);
33660
+ }
33661
+ }
33662
+ const innerContent = childContent.join("");
33663
+ if (!innerContent && !hasKeyAttrs) {
33664
+ return "";
33665
+ }
33666
+ if (href && href.length <= urlLimit && href.startsWith("http")) {
33667
+ if (!attrs.href) {
33668
+ attrs.href = href.trim();
33669
+ }
33670
+ const linkAttrsString = buildAttributesString(attrs);
33671
+ return `<a${linkAttrsString}>${innerContent}</a>`;
33672
+ }
33673
+ else if (hasKeyAttrs || hasDirectText) {
33674
+ return `<a${attrsString}>${innerContent}</a>`;
33675
+ }
33676
+ return innerContent;
33677
+ }
33678
+ if (tagName === "video" || tagName === "audio") {
33679
+ const media = element;
33680
+ let src = media.src || media.getAttribute("src");
33681
+ const sources = element.querySelectorAll("source");
33682
+ if (sources.length > 0 && sources[0].src) {
33683
+ src = sources[0].src;
33684
+ }
33685
+ if (src && src.startsWith("http") && src.length <= urlLimit) {
33686
+ if (!attrs.src) {
33687
+ attrs.src = src.trim();
33688
+ }
33689
+ const mediaAttrsString = buildAttributesString(attrs);
33690
+ return `<${tagName}${mediaAttrsString}></${tagName}>`;
33691
+ }
33692
+ return "";
33693
+ }
33694
+ const childContent = [];
33695
+ for (const child of node.childNodes) {
33696
+ const content = traverse(child);
33697
+ if (content) {
33698
+ childContent.push(content);
33699
+ }
33700
+ }
33701
+ const innerContent = childContent.join("");
33702
+ if (!innerContent) {
33703
+ return "";
33704
+ }
33705
+ if (hasKeyAttrs || hasDirectText) {
33706
+ return `<${tagName}${attrsString}>${innerContent}</${tagName}>`;
33707
+ }
33708
+ return innerContent;
33709
+ };
33710
+ if (!params.root_element) {
33711
+ if (params.root_selector) {
33712
+ params.root_element = document.querySelector(params.root_selector);
33713
+ if (!params.root_element) {
33714
+ return "";
33715
+ }
33716
+ }
33717
+ else {
33718
+ params.root_element = document.body;
33719
+ }
33720
+ }
33721
+ const rootTabName = params.root_element.tagName.toLowerCase();
33722
+ try {
33723
+ if (params.root_element) {
33724
+ const content = traverse(params.root_element);
33725
+ if (content) {
33726
+ addHtmlContent(content);
33494
33727
  }
33495
33728
  }
33496
- traverse(document.body);
33497
33729
  }
33498
33730
  catch (e) {
33499
- result = document.body.innerText;
33731
+ try {
33732
+ const fallbackText = params.root_element.innerText || "";
33733
+ if (fallbackText) {
33734
+ const escaped = escapeHtml(fallbackText);
33735
+ const truncated = escaped.length > contentLimit
33736
+ ? Array.from(escaped).slice(0, contentLimit).join("").trim() + "..."
33737
+ : escaped;
33738
+ return truncated.startsWith(`<${rootTabName}`)
33739
+ ? truncated
33740
+ : `<${rootTabName}>${truncated}</${rootTabName}>`;
33741
+ }
33742
+ return "";
33743
+ }
33744
+ catch {
33745
+ return "";
33746
+ }
33500
33747
  }
33501
- result = result.replace(/\s*\n/g, "\n").replace(/\n+/g, "\n").trim();
33502
- if (result.length > max_content_length) {
33503
- // result = result.slice(0, max_content_length) + "...";
33504
- result = Array.from(result).slice(0, max_content_length).join("") + "...";
33748
+ let result = parts.join("");
33749
+ if (result.length > contentLimit) {
33750
+ result = Array.from(result).slice(0, contentLimit).join("").trim() + "...";
33505
33751
  }
33506
- return result;
33752
+ return result.startsWith(`<${rootTabName}`)
33753
+ ? result
33754
+ : `<${rootTabName}>${result}</${rootTabName}>`;
33507
33755
  }
33508
33756
  function mark_screenshot_highlight_elements(screenshot, area_map, client_rect) {
33509
33757
  return new Promise(async (resolve, reject) => {
@@ -33511,8 +33759,13 @@ function mark_screenshot_highlight_elements(screenshot, area_map, client_rect) {
33511
33759
  const hasOffscreen = typeof OffscreenCanvas !== "undefined";
33512
33760
  const hasCreateImageBitmap = typeof createImageBitmap !== "undefined";
33513
33761
  const hasDOM = typeof document !== "undefined" && typeof Image !== "undefined";
33514
- // @ts-ignore
33515
- const isNode = typeof window === "undefined" && typeof process !== "undefined" && !!process.versions && !!process.versions.node;
33762
+ const isNode = typeof window === "undefined" &&
33763
+ // @ts-ignore
33764
+ typeof process !== "undefined" &&
33765
+ // @ts-ignore
33766
+ !!process.versions &&
33767
+ // @ts-ignore
33768
+ !!process.versions.node;
33516
33769
  const loadImageAny = async () => {
33517
33770
  if (hasCreateImageBitmap) {
33518
33771
  const base64Data = screenshot.imageBase64;
@@ -36342,265 +36595,24 @@ class SimpleHttpMcpClient {
36342
36595
  }
36343
36596
  }
36344
36597
 
36345
- async function callChatLLM(messageId, chatContext, rlm, messages, tools, toolChoice, retryNum = 0, callback, signal) {
36598
+ async function callChatLLM(chatId, messageId, rlm, messages, tools, toolChoice, callback, signal) {
36346
36599
  const streamCallback = callback?.chatCallback || {
36347
36600
  onMessage: async () => { },
36348
36601
  };
36349
36602
  const request = {
36350
- tools: tools,
36603
+ tools,
36604
+ messages,
36351
36605
  toolChoice,
36352
- messages: messages,
36353
36606
  abortSignal: signal,
36354
36607
  };
36355
- let streamText = "";
36356
- let thinkText = "";
36357
- let toolArgsText = "";
36358
- let textStreamId = uuidv4();
36359
- let thinkStreamId = uuidv4();
36360
- let textStreamDone = false;
36361
- const toolParts = [];
36362
- let reader = null;
36363
- try {
36364
- const result = await rlm.callStream(request);
36365
- reader = result.stream.getReader();
36366
- let toolPart = null;
36367
- while (true) {
36368
- const { done, value } = await reader.read();
36369
- if (done) {
36370
- break;
36371
- }
36372
- const chunk = value;
36373
- switch (chunk.type) {
36374
- case "text-start": {
36375
- textStreamId = uuidv4();
36376
- break;
36377
- }
36378
- case "text-delta": {
36379
- if (toolPart && !chunk.delta) {
36380
- continue;
36381
- }
36382
- streamText += chunk.delta || "";
36383
- await streamCallback.onMessage({
36384
- streamType: "chat",
36385
- chatId: chatContext.getChatId(),
36386
- messageId,
36387
- type: "text",
36388
- streamId: textStreamId,
36389
- streamDone: false,
36390
- text: streamText,
36391
- });
36392
- if (toolPart) {
36393
- await streamCallback.onMessage({
36394
- streamType: "chat",
36395
- chatId: chatContext.getChatId(),
36396
- messageId,
36397
- type: "tool_use",
36398
- toolCallId: toolPart.toolCallId,
36399
- toolName: toolPart.toolName,
36400
- params: toolPart.input || {},
36401
- });
36402
- toolPart = null;
36403
- }
36404
- break;
36405
- }
36406
- case "text-end": {
36407
- textStreamDone = true;
36408
- if (streamText) {
36409
- await streamCallback.onMessage({
36410
- streamType: "chat",
36411
- chatId: chatContext.getChatId(),
36412
- messageId,
36413
- type: "text",
36414
- streamId: textStreamId,
36415
- streamDone: true,
36416
- text: streamText,
36417
- });
36418
- }
36419
- break;
36420
- }
36421
- case "reasoning-start": {
36422
- thinkStreamId = uuidv4();
36423
- break;
36424
- }
36425
- case "reasoning-delta": {
36426
- thinkText += chunk.delta || "";
36427
- await streamCallback.onMessage({
36428
- streamType: "chat",
36429
- chatId: chatContext.getChatId(),
36430
- messageId,
36431
- type: "thinking",
36432
- streamId: thinkStreamId,
36433
- streamDone: false,
36434
- text: thinkText,
36435
- });
36436
- break;
36437
- }
36438
- case "reasoning-end": {
36439
- if (thinkText) {
36440
- await streamCallback.onMessage({
36441
- streamType: "chat",
36442
- chatId: chatContext.getChatId(),
36443
- messageId,
36444
- type: "thinking",
36445
- streamId: thinkStreamId,
36446
- streamDone: true,
36447
- text: thinkText,
36448
- });
36449
- }
36450
- break;
36451
- }
36452
- case "tool-input-start": {
36453
- if (toolPart && toolPart.toolCallId == chunk.id) {
36454
- toolPart.toolName = chunk.toolName;
36455
- }
36456
- else {
36457
- const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.id)[0];
36458
- if (_toolPart) {
36459
- toolPart = _toolPart;
36460
- toolPart.toolName = _toolPart.toolName || chunk.toolName;
36461
- toolPart.input = _toolPart.input || {};
36462
- }
36463
- else {
36464
- toolPart = {
36465
- type: "tool-call",
36466
- toolCallId: chunk.id,
36467
- toolName: chunk.toolName,
36468
- input: {},
36469
- };
36470
- toolParts.push(toolPart);
36471
- }
36472
- }
36473
- break;
36474
- }
36475
- case "tool-input-delta": {
36476
- if (!textStreamDone) {
36477
- textStreamDone = true;
36478
- await streamCallback.onMessage({
36479
- streamType: "chat",
36480
- chatId: chatContext.getChatId(),
36481
- messageId,
36482
- type: "text",
36483
- streamId: textStreamId,
36484
- streamDone: true,
36485
- text: streamText,
36486
- });
36487
- }
36488
- toolArgsText += chunk.delta || "";
36489
- await streamCallback.onMessage({
36490
- streamType: "chat",
36491
- chatId: chatContext.getChatId(),
36492
- messageId,
36493
- type: "tool_streaming",
36494
- toolCallId: chunk.id,
36495
- toolName: toolPart?.toolName || "",
36496
- paramsText: toolArgsText,
36497
- });
36498
- break;
36499
- }
36500
- case "tool-call": {
36501
- toolArgsText = "";
36502
- const args = chunk.input ? JSON.parse(chunk.input) : {};
36503
- const message = {
36504
- streamType: "chat",
36505
- chatId: chatContext.getChatId(),
36506
- messageId,
36507
- type: "tool_use",
36508
- toolCallId: chunk.toolCallId,
36509
- toolName: chunk.toolName,
36510
- params: args,
36511
- };
36512
- await streamCallback.onMessage(message);
36513
- if (toolPart == null) {
36514
- const _toolPart = toolParts.filter((s) => s.toolCallId == chunk.toolCallId)[0];
36515
- if (_toolPart) {
36516
- _toolPart.input = message.params || args;
36517
- }
36518
- else {
36519
- toolParts.push({
36520
- type: "tool-call",
36521
- toolCallId: chunk.toolCallId,
36522
- toolName: chunk.toolName,
36523
- input: message.params || args,
36524
- });
36525
- }
36526
- }
36527
- else {
36528
- toolPart.input = message.params || args;
36529
- toolPart = null;
36530
- }
36531
- break;
36532
- }
36533
- case "error": {
36534
- Log.error(`chatLLM error: `, chunk);
36535
- await streamCallback.onMessage({
36536
- streamType: "chat",
36537
- chatId: chatContext.getChatId(),
36538
- messageId,
36539
- type: "error",
36540
- error: chunk.error,
36541
- });
36542
- throw new Error("LLM Error: " + chunk.error);
36543
- }
36544
- case "finish": {
36545
- if (!textStreamDone) {
36546
- textStreamDone = true;
36547
- await streamCallback.onMessage({
36548
- streamType: "chat",
36549
- chatId: chatContext.getChatId(),
36550
- messageId,
36551
- type: "text",
36552
- streamId: textStreamId,
36553
- streamDone: true,
36554
- text: streamText,
36555
- });
36556
- }
36557
- if (toolPart) {
36558
- await streamCallback.onMessage({
36559
- streamType: "chat",
36560
- chatId: chatContext.getChatId(),
36561
- messageId,
36562
- type: "tool_use",
36563
- toolCallId: toolPart.toolCallId,
36564
- toolName: toolPart.toolName,
36565
- params: toolPart.input || {},
36566
- });
36567
- toolPart = null;
36568
- }
36569
- await streamCallback.onMessage({
36570
- streamType: "chat",
36571
- chatId: chatContext.getChatId(),
36572
- messageId,
36573
- type: "finish",
36574
- finishReason: chunk.finishReason,
36575
- usage: {
36576
- promptTokens: chunk.usage.inputTokens || 0,
36577
- completionTokens: chunk.usage.outputTokens || 0,
36578
- totalTokens: chunk.usage.totalTokens ||
36579
- (chunk.usage.inputTokens || 0) +
36580
- (chunk.usage.outputTokens || 0),
36581
- },
36582
- });
36583
- break;
36584
- }
36585
- }
36586
- }
36587
- }
36588
- catch (e) {
36589
- if (retryNum < config$1.maxRetryNum) {
36590
- await sleep(200 * (retryNum + 1) * (retryNum + 1));
36591
- return callChatLLM(messageId, chatContext, rlm, messages, tools, toolChoice, ++retryNum, callback, signal);
36592
- }
36593
- throw e;
36594
- }
36595
- finally {
36596
- reader && reader.releaseLock();
36597
- }
36598
- return streamText
36599
- ? [
36600
- { type: "text", text: streamText },
36601
- ...toolParts,
36602
- ]
36603
- : toolParts;
36608
+ return await callLLM(rlm, request, async (message) => {
36609
+ await streamCallback.onMessage({
36610
+ streamType: "chat",
36611
+ chatId,
36612
+ messageId,
36613
+ ...message,
36614
+ });
36615
+ });
36604
36616
  }
36605
36617
  function convertAssistantToolResults(results) {
36606
36618
  return results.map((part) => {
@@ -36960,6 +36972,7 @@ class DeepActionTool {
36960
36972
  });
36961
36973
  const taskWebsite = await this.gettaskWebsite(tabIds);
36962
36974
  const workflow = await eko.generate(taskDescription, messageId, {
36975
+ ...(this.params.extra || {}),
36963
36976
  ...globalVariables,
36964
36977
  tabIds: tabIds,
36965
36978
  language: language,
@@ -37203,7 +37216,7 @@ class ChatAgent {
37203
37216
  const rlm = new RetryLanguageModel(config.llms, config.chatLlms);
37204
37217
  for (; reactLoopNum < 15; reactLoopNum++) {
37205
37218
  const messages = this.memory.buildMessages();
37206
- const results = await callChatLLM(params.messageId, this.chatContext, rlm, messages, convertTools(chatTools), undefined, 0, params.callback, params.signal);
37219
+ const results = await callChatLLM(this.chatContext.getChatId(), params.messageId, rlm, messages, convertTools(chatTools), undefined, params.callback, params.signal);
37207
37220
  const finalResult = await this.handleCallResult(params.messageId, chatTools, results, params.callback);
37208
37221
  if (finalResult) {
37209
37222
  return finalResult;
@@ -37423,6 +37436,8 @@ exports.WebSearchTool = WebSearchTool;
37423
37436
  exports.WebpageQaTool = WebpageQaTool;
37424
37437
  exports.buildAgentTree = buildAgentTree;
37425
37438
  exports.buildSimpleAgentWorkflow = buildSimpleAgentWorkflow;
37439
+ exports.callLLM = callLLM;
37440
+ exports.callWithReAct = callWithReAct;
37426
37441
  exports.call_timeout = call_timeout;
37427
37442
  exports.compressImageData = compressImageData;
37428
37443
  exports.config = config$1;