@eko-ai/eko 3.0.7 → 3.0.8-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -6,6 +6,7 @@ const config$1 = {
6
6
  maxRetryNum: 3,
7
7
  agentParallel: false,
8
8
  compressThreshold: 80,
9
+ compressTokensThreshold: 100000,
9
10
  largeTextLength: 5000,
10
11
  fileTextMaxLength: 20000,
11
12
  maxDialogueImgFileNum: 1,
@@ -31569,7 +31570,8 @@ function convertToolResult(toolUse, toolResult, user_messages) {
31569
31570
  value: "Error",
31570
31571
  };
31571
31572
  }
31572
- else if (toolResult.content.length == 1 && toolResult.content[0].type == "text") {
31573
+ else if (toolResult.content.length == 1 &&
31574
+ toolResult.content[0].type == "text") {
31573
31575
  let text = toolResult.content[0].text;
31574
31576
  result = {
31575
31577
  type: "text",
@@ -31659,7 +31661,9 @@ function convertToolResult(toolUse, toolResult, user_messages) {
31659
31661
  }
31660
31662
  async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, retryNum = 0, callback, requestHandler) {
31661
31663
  await agentContext.context.checkAborted();
31662
- if (messages.length >= config$1.compressThreshold && !noCompress) {
31664
+ if (!noCompress &&
31665
+ (messages.length >= config$1.compressThreshold || (messages.length >= 10 && estimatePromptTokens(messages, tools) >= config$1.compressTokensThreshold))) {
31666
+ // Compress messages
31663
31667
  await compressAgentMessages(agentContext, rlm, messages, tools);
31664
31668
  }
31665
31669
  if (!toolChoice) {
@@ -31929,6 +31933,9 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
31929
31933
  await context.checkAborted();
31930
31934
  if (retryNum < config$1.maxRetryNum) {
31931
31935
  await sleep(300 * (retryNum + 1) * (retryNum + 1));
31936
+ if ((e + "").indexOf("is too long") > -1) {
31937
+ await compressAgentMessages(agentContext, rlm, messages, tools);
31938
+ }
31932
31939
  return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
31933
31940
  }
31934
31941
  throw e;
@@ -31945,6 +31952,100 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
31945
31952
  ]
31946
31953
  : toolParts;
31947
31954
  }
31955
+ function estimatePromptTokens(messages, tools) {
31956
+ let tokens = messages.reduce((total, message) => {
31957
+ if (message.role == "system") {
31958
+ return total + estimateTokens(message.content);
31959
+ }
31960
+ else if (message.role == "user") {
31961
+ return (total +
31962
+ estimateTokens(message.content
31963
+ .filter((part) => part.type == "text")
31964
+ .map((part) => part.text)
31965
+ .join("\n")));
31966
+ }
31967
+ else if (message.role == "assistant") {
31968
+ return (total +
31969
+ estimateTokens(message.content
31970
+ .map((part) => {
31971
+ if (part.type == "text") {
31972
+ return part.text;
31973
+ }
31974
+ else if (part.type == "reasoning") {
31975
+ return part.text;
31976
+ }
31977
+ else if (part.type == "tool-call") {
31978
+ return part.toolName + JSON.stringify(part.input || {});
31979
+ }
31980
+ else if (part.type == "tool-result") {
31981
+ return part.toolName + JSON.stringify(part.output || {});
31982
+ }
31983
+ return "";
31984
+ })
31985
+ .join("")));
31986
+ }
31987
+ else if (message.role == "tool") {
31988
+ return (total +
31989
+ estimateTokens(message.content
31990
+ .map((part) => part.toolName + JSON.stringify(part.output || {}))
31991
+ .join("")));
31992
+ }
31993
+ return total;
31994
+ }, 0);
31995
+ if (tools) {
31996
+ tokens += tools.reduce((total, tool) => {
31997
+ return total + estimateTokens(JSON.stringify(tool));
31998
+ }, 0);
31999
+ }
32000
+ return tokens;
32001
+ }
32002
+ function estimateTokens(text) {
32003
+ if (!text) {
32004
+ return 0;
32005
+ }
32006
+ let tokenCount = 0;
32007
+ for (let i = 0; i < text.length; i++) {
32008
+ const char = text[i];
32009
+ const code = char.charCodeAt(0);
32010
+ if ((code >= 0x4e00 && code <= 0x9fff) ||
32011
+ (code >= 0x3400 && code <= 0x4dbf) ||
32012
+ (code >= 0x3040 && code <= 0x309f) ||
32013
+ (code >= 0x30a0 && code <= 0x30ff) ||
32014
+ (code >= 0xac00 && code <= 0xd7af)) {
32015
+ tokenCount += 2;
32016
+ }
32017
+ else if (/\s/.test(char)) {
32018
+ continue;
32019
+ }
32020
+ else if (/[a-zA-Z]/.test(char)) {
32021
+ let word = "";
32022
+ while (i < text.length && /[a-zA-Z]/.test(text[i])) {
32023
+ word += text[i];
32024
+ i++;
32025
+ }
32026
+ i--;
32027
+ if (word.length <= 4) {
32028
+ tokenCount += 1;
32029
+ }
32030
+ else {
32031
+ tokenCount += Math.ceil(word.length / 4);
32032
+ }
32033
+ }
32034
+ else if (/\d/.test(char)) {
32035
+ let number = "";
32036
+ while (i < text.length && /\d/.test(text[i])) {
32037
+ number += text[i];
32038
+ i++;
32039
+ }
32040
+ i--;
32041
+ tokenCount += Math.max(1, Math.ceil(number.length / 3));
32042
+ }
32043
+ else {
32044
+ tokenCount += 1;
32045
+ }
32046
+ }
32047
+ return Math.max(1, tokenCount);
32048
+ }
31948
32049
  function appendUserConversation(agentContext, messages) {
31949
32050
  const userPrompts = agentContext.context.conversation
31950
32051
  .splice(0, agentContext.context.conversation.length)
@@ -34125,7 +34226,10 @@ class SimpleSseMcpClient {
34125
34226
  version: "1.0.0",
34126
34227
  },
34127
34228
  });
34128
- // this.request("notifications/initialized", {});
34229
+ try {
34230
+ await this.request("notifications/initialized", {});
34231
+ }
34232
+ catch (ignored) { }
34129
34233
  }
34130
34234
  ping() {
34131
34235
  this.request("ping", {});
@@ -34143,7 +34247,7 @@ class SimpleSseMcpClient {
34143
34247
  return message.result;
34144
34248
  }
34145
34249
  async request(method, params, signal) {
34146
- const id = uuidv4();
34250
+ const id = method.startsWith("notifications/") ? null : uuidv4();
34147
34251
  try {
34148
34252
  const callback = new Promise((resolve, reject) => {
34149
34253
  if (signal) {
@@ -34153,7 +34257,7 @@ class SimpleSseMcpClient {
34153
34257
  reject(error);
34154
34258
  });
34155
34259
  }
34156
- this.requestMap.set(id, resolve);
34260
+ id && this.requestMap.set(id, resolve);
34157
34261
  });
34158
34262
  Log.debug(`MCP Client, ${method}`, id, params);
34159
34263
  const response = await fetch(this.msgUrl, {
@@ -34200,7 +34304,7 @@ class SimpleSseMcpClient {
34200
34304
  }
34201
34305
  }
34202
34306
  finally {
34203
- this.requestMap.delete(id);
34307
+ id && this.requestMap.delete(id);
34204
34308
  }
34205
34309
  }
34206
34310
  isConnected() {
@@ -34210,6 +34314,13 @@ class SimpleSseMcpClient {
34210
34314
  return false;
34211
34315
  }
34212
34316
  async close() {
34317
+ try {
34318
+ await this.request("notifications/cancelled", {
34319
+ requestId: uuidv4(),
34320
+ reason: "User requested cancellation",
34321
+ });
34322
+ }
34323
+ catch (ignored) { }
34213
34324
  this.pingTimer && clearInterval(this.pingTimer);
34214
34325
  this.reconnectTimer && clearTimeout(this.reconnectTimer);
34215
34326
  this.sseHandler && this.sseHandler.close && this.sseHandler.close();
@@ -34322,6 +34433,12 @@ class SimpleHttpMcpClient {
34322
34433
  version: "1.0.0",
34323
34434
  },
34324
34435
  }, signal);
34436
+ if (this.mcpSessionId) {
34437
+ try {
34438
+ await this.request("notifications/initialized", {});
34439
+ }
34440
+ catch (ignored) { }
34441
+ }
34325
34442
  this.connected = true;
34326
34443
  }
34327
34444
  async listTools(param, signal) {
@@ -34342,16 +34459,19 @@ class SimpleHttpMcpClient {
34342
34459
  async close() {
34343
34460
  this.connected = false;
34344
34461
  if (this.mcpSessionId) {
34345
- this.request("notifications/cancelled", {
34346
- requestId: uuidv4(),
34347
- reason: "User requested cancellation",
34348
- });
34462
+ try {
34463
+ await this.request("notifications/cancelled", {
34464
+ requestId: uuidv4(),
34465
+ reason: "User requested cancellation",
34466
+ });
34467
+ }
34468
+ catch (ignored) { }
34349
34469
  this.mcpSessionId = null;
34350
34470
  }
34351
34471
  }
34352
34472
  async request(method, params, signal) {
34353
34473
  try {
34354
- const id = uuidv4();
34474
+ const id = method.startsWith("notifications/") ? null : uuidv4();
34355
34475
  const extHeaders = {};
34356
34476
  if (this.mcpSessionId && method !== "initialize") {
34357
34477
  extHeaders["Mcp-Session-Id"] = this.mcpSessionId;
@@ -35297,6 +35417,7 @@ During the task execution process, you can use the \`${TOOL_NAME$5}\` tool to in
35297
35417
  - When performing dangerous operations such as deleting files, confirmation from humans is required.
35298
35418
  - When encountering obstacles while accessing websites, such as requiring user login, captcha verification, QR code scanning, or human verification, you need to request manual assistance.
35299
35419
  - Please do not use the \`${TOOL_NAME$5}\` tool frequently.
35420
+ - The \`${TOOL_NAME$5}\` tool does not support parallel calls.
35300
35421
  `;
35301
35422
  const VARIABLE_PROMPT = `
35302
35423
  * VARIABLE STORAGE