@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/agent/llm.d.ts +4 -2
- package/dist/agent/llm.d.ts.map +1 -1
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/index.cjs.js +132 -11
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +132 -11
- package/dist/index.esm.js.map +1 -1
- package/dist/mcp/http.d.ts.map +1 -1
- package/dist/mcp/sse.d.ts.map +1 -1
- package/dist/prompt/agent.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/agent/llm.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { RetryLanguageModel } from "../llm";
|
|
2
2
|
import { AgentContext } from "../core/context";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { Tool, LLMRequest, ToolResult, DialogueTool, HumanCallback, StreamCallback } from "../types";
|
|
4
|
+
import { LanguageModelV2Prompt, LanguageModelV2TextPart, SharedV2ProviderOptions, LanguageModelV2ToolChoice, LanguageModelV2ToolCallPart, LanguageModelV2FunctionTool, LanguageModelV2ToolResultPart } from "@ai-sdk/provider";
|
|
5
5
|
export declare function defaultLLMProviderOptions(): SharedV2ProviderOptions;
|
|
6
6
|
export declare function defaultMessageProviderOptions(): SharedV2ProviderOptions;
|
|
7
7
|
export declare function convertTools(tools: Tool[] | DialogueTool[]): LanguageModelV2FunctionTool[];
|
|
8
8
|
export declare function getTool<T extends Tool | DialogueTool>(tools: T[], name: string): T | null;
|
|
9
9
|
export declare function convertToolResult(toolUse: LanguageModelV2ToolCallPart, toolResult: ToolResult, user_messages: LanguageModelV2Prompt): LanguageModelV2ToolResultPart;
|
|
10
10
|
export declare function callAgentLLM(agentContext: AgentContext, rlm: RetryLanguageModel, messages: LanguageModelV2Prompt, tools: LanguageModelV2FunctionTool[], noCompress?: boolean, toolChoice?: LanguageModelV2ToolChoice, retryNum?: number, callback?: StreamCallback & HumanCallback, requestHandler?: (request: LLMRequest) => void): Promise<Array<LanguageModelV2TextPart | LanguageModelV2ToolCallPart>>;
|
|
11
|
+
export declare function estimatePromptTokens(messages: LanguageModelV2Prompt, tools?: LanguageModelV2FunctionTool[]): number;
|
|
12
|
+
export declare function estimateTokens(text: string): number;
|
|
11
13
|
//# sourceMappingURL=llm.d.ts.map
|
package/dist/agent/llm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../../src/agent/llm.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../../src/agent/llm.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EACL,IAAI,EACJ,UAAU,EACV,UAAU,EACV,YAAY,EAEZ,aAAa,EACb,cAAc,EAEf,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,EAEzB,2BAA2B,EAC3B,2BAA2B,EAC3B,6BAA6B,EAE9B,MAAM,kBAAkB,CAAC;AAE1B,wBAAgB,yBAAyB,IAAI,uBAAuB,CAanE;AAED,wBAAgB,6BAA6B,IAAI,uBAAuB,CAYvE;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,IAAI,EAAE,GAAG,YAAY,EAAE,GAC7B,2BAA2B,EAAE,CAQ/B;AAED,wBAAgB,OAAO,CAAC,CAAC,SAAS,IAAI,GAAG,YAAY,EACnD,KAAK,EAAE,CAAC,EAAE,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CAAC;IAC9B,iBAAiB,EAAE,OAAO,CAAC;IAC3B,UAAU,EAAE,OAAO,CAAC;IACpB,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAAA;AAED,QAAA,MAAM,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AAAA,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,SAAS,GAAG,KAAK,GAAG,OAAO,CAAC;IACtC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,OAAO,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,uBAAuB,EAAE,MAAM,CAAC;IAChC,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,oBAAoB,EAAE,OAAO,CAAC;IAC9B,iBAAiB,EAAE,OAAO,CAAC;IAC3B,UAAU,EAAE,OAAO,CAAC;IACpB,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAAA;AAED,QAAA,MAAM,MAAM,EAAE,YAgBb,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -12,6 +12,7 @@ const config$1 = {
|
|
|
12
12
|
maxRetryNum: 3,
|
|
13
13
|
agentParallel: false,
|
|
14
14
|
compressThreshold: 80,
|
|
15
|
+
compressTokensThreshold: 100000,
|
|
15
16
|
largeTextLength: 5000,
|
|
16
17
|
fileTextMaxLength: 20000,
|
|
17
18
|
maxDialogueImgFileNum: 1,
|
|
@@ -31604,7 +31605,8 @@ function convertToolResult(toolUse, toolResult, user_messages) {
|
|
|
31604
31605
|
value: "Error",
|
|
31605
31606
|
};
|
|
31606
31607
|
}
|
|
31607
|
-
else if (toolResult.content.length == 1 &&
|
|
31608
|
+
else if (toolResult.content.length == 1 &&
|
|
31609
|
+
toolResult.content[0].type == "text") {
|
|
31608
31610
|
let text = toolResult.content[0].text;
|
|
31609
31611
|
result = {
|
|
31610
31612
|
type: "text",
|
|
@@ -31694,7 +31696,9 @@ function convertToolResult(toolUse, toolResult, user_messages) {
|
|
|
31694
31696
|
}
|
|
31695
31697
|
async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, retryNum = 0, callback, requestHandler) {
|
|
31696
31698
|
await agentContext.context.checkAborted();
|
|
31697
|
-
if (
|
|
31699
|
+
if (!noCompress &&
|
|
31700
|
+
(messages.length >= config$1.compressThreshold || (messages.length >= 10 && estimatePromptTokens(messages, tools) >= config$1.compressTokensThreshold))) {
|
|
31701
|
+
// Compress messages
|
|
31698
31702
|
await compressAgentMessages(agentContext, rlm, messages, tools);
|
|
31699
31703
|
}
|
|
31700
31704
|
if (!toolChoice) {
|
|
@@ -31964,6 +31968,9 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
|
|
|
31964
31968
|
await context.checkAborted();
|
|
31965
31969
|
if (retryNum < config$1.maxRetryNum) {
|
|
31966
31970
|
await sleep(300 * (retryNum + 1) * (retryNum + 1));
|
|
31971
|
+
if ((e + "").indexOf("is too long") > -1) {
|
|
31972
|
+
await compressAgentMessages(agentContext, rlm, messages, tools);
|
|
31973
|
+
}
|
|
31967
31974
|
return callAgentLLM(agentContext, rlm, messages, tools, noCompress, toolChoice, ++retryNum, streamCallback);
|
|
31968
31975
|
}
|
|
31969
31976
|
throw e;
|
|
@@ -31980,6 +31987,100 @@ async function callAgentLLM(agentContext, rlm, messages, tools, noCompress, tool
|
|
|
31980
31987
|
]
|
|
31981
31988
|
: toolParts;
|
|
31982
31989
|
}
|
|
31990
|
+
function estimatePromptTokens(messages, tools) {
|
|
31991
|
+
let tokens = messages.reduce((total, message) => {
|
|
31992
|
+
if (message.role == "system") {
|
|
31993
|
+
return total + estimateTokens(message.content);
|
|
31994
|
+
}
|
|
31995
|
+
else if (message.role == "user") {
|
|
31996
|
+
return (total +
|
|
31997
|
+
estimateTokens(message.content
|
|
31998
|
+
.filter((part) => part.type == "text")
|
|
31999
|
+
.map((part) => part.text)
|
|
32000
|
+
.join("\n")));
|
|
32001
|
+
}
|
|
32002
|
+
else if (message.role == "assistant") {
|
|
32003
|
+
return (total +
|
|
32004
|
+
estimateTokens(message.content
|
|
32005
|
+
.map((part) => {
|
|
32006
|
+
if (part.type == "text") {
|
|
32007
|
+
return part.text;
|
|
32008
|
+
}
|
|
32009
|
+
else if (part.type == "reasoning") {
|
|
32010
|
+
return part.text;
|
|
32011
|
+
}
|
|
32012
|
+
else if (part.type == "tool-call") {
|
|
32013
|
+
return part.toolName + JSON.stringify(part.input || {});
|
|
32014
|
+
}
|
|
32015
|
+
else if (part.type == "tool-result") {
|
|
32016
|
+
return part.toolName + JSON.stringify(part.output || {});
|
|
32017
|
+
}
|
|
32018
|
+
return "";
|
|
32019
|
+
})
|
|
32020
|
+
.join("")));
|
|
32021
|
+
}
|
|
32022
|
+
else if (message.role == "tool") {
|
|
32023
|
+
return (total +
|
|
32024
|
+
estimateTokens(message.content
|
|
32025
|
+
.map((part) => part.toolName + JSON.stringify(part.output || {}))
|
|
32026
|
+
.join("")));
|
|
32027
|
+
}
|
|
32028
|
+
return total;
|
|
32029
|
+
}, 0);
|
|
32030
|
+
if (tools) {
|
|
32031
|
+
tokens += tools.reduce((total, tool) => {
|
|
32032
|
+
return total + estimateTokens(JSON.stringify(tool));
|
|
32033
|
+
}, 0);
|
|
32034
|
+
}
|
|
32035
|
+
return tokens;
|
|
32036
|
+
}
|
|
32037
|
+
function estimateTokens(text) {
|
|
32038
|
+
if (!text) {
|
|
32039
|
+
return 0;
|
|
32040
|
+
}
|
|
32041
|
+
let tokenCount = 0;
|
|
32042
|
+
for (let i = 0; i < text.length; i++) {
|
|
32043
|
+
const char = text[i];
|
|
32044
|
+
const code = char.charCodeAt(0);
|
|
32045
|
+
if ((code >= 0x4e00 && code <= 0x9fff) ||
|
|
32046
|
+
(code >= 0x3400 && code <= 0x4dbf) ||
|
|
32047
|
+
(code >= 0x3040 && code <= 0x309f) ||
|
|
32048
|
+
(code >= 0x30a0 && code <= 0x30ff) ||
|
|
32049
|
+
(code >= 0xac00 && code <= 0xd7af)) {
|
|
32050
|
+
tokenCount += 2;
|
|
32051
|
+
}
|
|
32052
|
+
else if (/\s/.test(char)) {
|
|
32053
|
+
continue;
|
|
32054
|
+
}
|
|
32055
|
+
else if (/[a-zA-Z]/.test(char)) {
|
|
32056
|
+
let word = "";
|
|
32057
|
+
while (i < text.length && /[a-zA-Z]/.test(text[i])) {
|
|
32058
|
+
word += text[i];
|
|
32059
|
+
i++;
|
|
32060
|
+
}
|
|
32061
|
+
i--;
|
|
32062
|
+
if (word.length <= 4) {
|
|
32063
|
+
tokenCount += 1;
|
|
32064
|
+
}
|
|
32065
|
+
else {
|
|
32066
|
+
tokenCount += Math.ceil(word.length / 4);
|
|
32067
|
+
}
|
|
32068
|
+
}
|
|
32069
|
+
else if (/\d/.test(char)) {
|
|
32070
|
+
let number = "";
|
|
32071
|
+
while (i < text.length && /\d/.test(text[i])) {
|
|
32072
|
+
number += text[i];
|
|
32073
|
+
i++;
|
|
32074
|
+
}
|
|
32075
|
+
i--;
|
|
32076
|
+
tokenCount += Math.max(1, Math.ceil(number.length / 3));
|
|
32077
|
+
}
|
|
32078
|
+
else {
|
|
32079
|
+
tokenCount += 1;
|
|
32080
|
+
}
|
|
32081
|
+
}
|
|
32082
|
+
return Math.max(1, tokenCount);
|
|
32083
|
+
}
|
|
31983
32084
|
function appendUserConversation(agentContext, messages) {
|
|
31984
32085
|
const userPrompts = agentContext.context.conversation
|
|
31985
32086
|
.splice(0, agentContext.context.conversation.length)
|
|
@@ -34160,7 +34261,10 @@ class SimpleSseMcpClient {
|
|
|
34160
34261
|
version: "1.0.0",
|
|
34161
34262
|
},
|
|
34162
34263
|
});
|
|
34163
|
-
|
|
34264
|
+
try {
|
|
34265
|
+
await this.request("notifications/initialized", {});
|
|
34266
|
+
}
|
|
34267
|
+
catch (ignored) { }
|
|
34164
34268
|
}
|
|
34165
34269
|
ping() {
|
|
34166
34270
|
this.request("ping", {});
|
|
@@ -34178,7 +34282,7 @@ class SimpleSseMcpClient {
|
|
|
34178
34282
|
return message.result;
|
|
34179
34283
|
}
|
|
34180
34284
|
async request(method, params, signal) {
|
|
34181
|
-
const id = uuidv4();
|
|
34285
|
+
const id = method.startsWith("notifications/") ? null : uuidv4();
|
|
34182
34286
|
try {
|
|
34183
34287
|
const callback = new Promise((resolve, reject) => {
|
|
34184
34288
|
if (signal) {
|
|
@@ -34188,7 +34292,7 @@ class SimpleSseMcpClient {
|
|
|
34188
34292
|
reject(error);
|
|
34189
34293
|
});
|
|
34190
34294
|
}
|
|
34191
|
-
this.requestMap.set(id, resolve);
|
|
34295
|
+
id && this.requestMap.set(id, resolve);
|
|
34192
34296
|
});
|
|
34193
34297
|
Log.debug(`MCP Client, ${method}`, id, params);
|
|
34194
34298
|
const response = await fetch(this.msgUrl, {
|
|
@@ -34235,7 +34339,7 @@ class SimpleSseMcpClient {
|
|
|
34235
34339
|
}
|
|
34236
34340
|
}
|
|
34237
34341
|
finally {
|
|
34238
|
-
this.requestMap.delete(id);
|
|
34342
|
+
id && this.requestMap.delete(id);
|
|
34239
34343
|
}
|
|
34240
34344
|
}
|
|
34241
34345
|
isConnected() {
|
|
@@ -34245,6 +34349,13 @@ class SimpleSseMcpClient {
|
|
|
34245
34349
|
return false;
|
|
34246
34350
|
}
|
|
34247
34351
|
async close() {
|
|
34352
|
+
try {
|
|
34353
|
+
await this.request("notifications/cancelled", {
|
|
34354
|
+
requestId: uuidv4(),
|
|
34355
|
+
reason: "User requested cancellation",
|
|
34356
|
+
});
|
|
34357
|
+
}
|
|
34358
|
+
catch (ignored) { }
|
|
34248
34359
|
this.pingTimer && clearInterval(this.pingTimer);
|
|
34249
34360
|
this.reconnectTimer && clearTimeout(this.reconnectTimer);
|
|
34250
34361
|
this.sseHandler && this.sseHandler.close && this.sseHandler.close();
|
|
@@ -34357,6 +34468,12 @@ class SimpleHttpMcpClient {
|
|
|
34357
34468
|
version: "1.0.0",
|
|
34358
34469
|
},
|
|
34359
34470
|
}, signal);
|
|
34471
|
+
if (this.mcpSessionId) {
|
|
34472
|
+
try {
|
|
34473
|
+
await this.request("notifications/initialized", {});
|
|
34474
|
+
}
|
|
34475
|
+
catch (ignored) { }
|
|
34476
|
+
}
|
|
34360
34477
|
this.connected = true;
|
|
34361
34478
|
}
|
|
34362
34479
|
async listTools(param, signal) {
|
|
@@ -34377,16 +34494,19 @@ class SimpleHttpMcpClient {
|
|
|
34377
34494
|
async close() {
|
|
34378
34495
|
this.connected = false;
|
|
34379
34496
|
if (this.mcpSessionId) {
|
|
34380
|
-
|
|
34381
|
-
|
|
34382
|
-
|
|
34383
|
-
|
|
34497
|
+
try {
|
|
34498
|
+
await this.request("notifications/cancelled", {
|
|
34499
|
+
requestId: uuidv4(),
|
|
34500
|
+
reason: "User requested cancellation",
|
|
34501
|
+
});
|
|
34502
|
+
}
|
|
34503
|
+
catch (ignored) { }
|
|
34384
34504
|
this.mcpSessionId = null;
|
|
34385
34505
|
}
|
|
34386
34506
|
}
|
|
34387
34507
|
async request(method, params, signal) {
|
|
34388
34508
|
try {
|
|
34389
|
-
const id = uuidv4();
|
|
34509
|
+
const id = method.startsWith("notifications/") ? null : uuidv4();
|
|
34390
34510
|
const extHeaders = {};
|
|
34391
34511
|
if (this.mcpSessionId && method !== "initialize") {
|
|
34392
34512
|
extHeaders["Mcp-Session-Id"] = this.mcpSessionId;
|
|
@@ -35332,6 +35452,7 @@ During the task execution process, you can use the \`${TOOL_NAME$5}\` tool to in
|
|
|
35332
35452
|
- When performing dangerous operations such as deleting files, confirmation from humans is required.
|
|
35333
35453
|
- 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.
|
|
35334
35454
|
- Please do not use the \`${TOOL_NAME$5}\` tool frequently.
|
|
35455
|
+
- The \`${TOOL_NAME$5}\` tool does not support parallel calls.
|
|
35335
35456
|
`;
|
|
35336
35457
|
const VARIABLE_PROMPT = `
|
|
35337
35458
|
* VARIABLE STORAGE
|