@ai-setting/roy-agent-core 1.7.46 → 1.7.48
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/config/index.js +4 -4
- package/dist/env/agent/index.js +7 -7
- package/dist/env/commands/index.js +4 -4
- package/dist/env/debug/index.js +4 -4
- package/dist/env/event-source/index.js +8 -7
- package/dist/env/hook/index.js +2 -2
- package/dist/env/index.js +24 -23
- package/dist/env/llm/index.js +5 -5
- package/dist/env/log-trace/index.js +4 -4
- package/dist/env/mcp/index.js +4 -4
- package/dist/env/memory/index.js +4 -4
- package/dist/env/plugin/index.js +8 -6
- package/dist/env/prompt/index.js +4 -4
- package/dist/env/session/index.js +7 -7
- package/dist/env/session/storage/index.js +3 -3
- package/dist/env/skill/index.js +4 -4
- package/dist/env/task/delegate/index.js +2 -2
- package/dist/env/task/index.js +6 -6
- package/dist/env/task/plugins/index.js +2 -2
- package/dist/env/task/storage/index.js +1 -1
- package/dist/env/tool/index.js +4 -4
- package/dist/env/workflow/engine/index.js +3 -3
- package/dist/env/workflow/index.js +11 -11
- package/dist/env/workflow/tools/index.js +8 -8
- package/dist/index.js +31 -30
- package/dist/shared/@ai-setting/{roy-agent-core-cysnmvcy.js → roy-agent-core-0r90298w.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-qfktfbrr.js → roy-agent-core-1rm32m5t.js} +3 -3
- package/dist/shared/@ai-setting/{roy-agent-core-2f2nqx7g.js → roy-agent-core-3b2ceffs.js} +153 -60
- package/dist/shared/@ai-setting/{roy-agent-core-a236mps6.js → roy-agent-core-3j4by8rj.js} +35 -2
- package/dist/shared/@ai-setting/{roy-agent-core-wybj8emq.js → roy-agent-core-4m02hnqf.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-jawzpf5k.js → roy-agent-core-4nnv0dze.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-cw89yj9d.js → roy-agent-core-54wzmvnb.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-jjw7xcxv.js → roy-agent-core-55hkeft7.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-rmn29nrh.js → roy-agent-core-64f22h1g.js} +1 -11
- package/dist/shared/@ai-setting/{roy-agent-core-12d4qzmw.js → roy-agent-core-7p5zf8ay.js} +4 -4
- package/dist/shared/@ai-setting/{roy-agent-core-qhw8jp6j.js → roy-agent-core-83mk8wrn.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-ajtsk20z.js +2396 -0
- package/dist/shared/@ai-setting/{roy-agent-core-pc8x07p0.js → roy-agent-core-amp8qckb.js} +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-az13yzmc.js +11 -0
- package/dist/shared/@ai-setting/{roy-agent-core-4w5vdzg8.js → roy-agent-core-bjrtwfta.js} +41 -6
- package/dist/shared/@ai-setting/{roy-agent-core-ts56bz5e.js → roy-agent-core-d94ddn0r.js} +3 -3
- package/dist/shared/@ai-setting/{roy-agent-core-rczz7e81.js → roy-agent-core-dabnevwe.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-9t675te0.js → roy-agent-core-ead21w5c.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-xv7k1p2j.js → roy-agent-core-gy1zfe0d.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-4eke3pd4.js → roy-agent-core-h93v6p6y.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-f18jk3cw.js → roy-agent-core-hxazjj4p.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-3h7ajma5.js → roy-agent-core-jfptxs4f.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-4m0t4y9w.js → roy-agent-core-nvcqp6p9.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-jadrj0bz.js → roy-agent-core-p1p9mr8f.js} +13 -0
- package/dist/shared/@ai-setting/{roy-agent-core-ykrcrktp.js → roy-agent-core-pk1b3kvf.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-n1ceexfz.js → roy-agent-core-qvaqmj3f.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-6mnwqcqf.js → roy-agent-core-v5vs1jby.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-9jmzyc8t.js → roy-agent-core-y1fp2mgf.js} +6 -31
- package/dist/shared/@ai-setting/{roy-agent-core-asxe1nt4.js → roy-agent-core-ygnva439.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-8xgk89y4.js → roy-agent-core-yjqj3taz.js} +4 -4
- package/dist/shared/@ai-setting/{roy-agent-core-2ccy0zd2.js → roy-agent-core-zrgaemma.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-r4z6h3ch.js → roy-agent-core-zy3hqfwg.js} +197 -19
- package/package.json +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-0zgcfcnq.js +0 -1720
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
truncateOutputInline
|
|
3
|
-
} from "./roy-agent-core-
|
|
3
|
+
} from "./roy-agent-core-55hkeft7.js";
|
|
4
4
|
import {
|
|
5
5
|
AgentRegistry
|
|
6
6
|
} from "./roy-agent-core-za09t5g3.js";
|
|
@@ -9,11 +9,12 @@ import {
|
|
|
9
9
|
} from "./roy-agent-core-4cj6hram.js";
|
|
10
10
|
import {
|
|
11
11
|
SessionMessageConverter
|
|
12
|
-
} from "./roy-agent-core-
|
|
12
|
+
} from "./roy-agent-core-pk1b3kvf.js";
|
|
13
13
|
import {
|
|
14
|
+
parseToolArgsStrict,
|
|
14
15
|
safeParseToolArgs,
|
|
15
16
|
withTimeout
|
|
16
|
-
} from "./roy-agent-core-
|
|
17
|
+
} from "./roy-agent-core-zy3hqfwg.js";
|
|
17
18
|
import {
|
|
18
19
|
getCurrentBgTaskId
|
|
19
20
|
} from "./roy-agent-core-c1v263jn.js";
|
|
@@ -29,11 +30,11 @@ import {
|
|
|
29
30
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
30
31
|
import {
|
|
31
32
|
BaseComponent
|
|
32
|
-
} from "./roy-agent-core-
|
|
33
|
+
} from "./roy-agent-core-ead21w5c.js";
|
|
33
34
|
import {
|
|
34
35
|
globalHookManager,
|
|
35
36
|
init_global_hook_manager
|
|
36
|
-
} from "./roy-agent-core-
|
|
37
|
+
} from "./roy-agent-core-3j4by8rj.js";
|
|
37
38
|
import {
|
|
38
39
|
init_context
|
|
39
40
|
} from "./roy-agent-core-wn59p0zw.js";
|
|
@@ -1042,7 +1043,37 @@ class AgentComponent extends BaseComponent {
|
|
|
1042
1043
|
}
|
|
1043
1044
|
const func = toolCall.function;
|
|
1044
1045
|
const tcName = func?.name || toolCall.name || "unknown";
|
|
1045
|
-
const
|
|
1046
|
+
const tcArgsParsed = this.parseToolCallArgumentsStrict(toolCall);
|
|
1047
|
+
if (!tcArgsParsed.ok) {
|
|
1048
|
+
const invalidMessage = tcArgsParsed.error ?? "invalid tool arguments";
|
|
1049
|
+
logger2.warn(`[ReAct] 工具参数不可解析,已拒绝执行(未跑任何命令/无副作用): ` + `tool=${tcName} error=${invalidMessage}`);
|
|
1050
|
+
this.pushEnvEvent({
|
|
1051
|
+
type: "tool.error",
|
|
1052
|
+
payload: { error: invalidMessage, toolName: tcName }
|
|
1053
|
+
});
|
|
1054
|
+
this.pushMessage(hookCtx, {
|
|
1055
|
+
role: "tool",
|
|
1056
|
+
content: [{
|
|
1057
|
+
type: "tool-result",
|
|
1058
|
+
toolCallId: toolCall.id,
|
|
1059
|
+
toolName: tcName,
|
|
1060
|
+
output: JSON.stringify({
|
|
1061
|
+
success: false,
|
|
1062
|
+
error: invalidMessage,
|
|
1063
|
+
hint: "上一次调用的 arguments 不是合法 JSON(上游输出可能损坏)。请重新调用,并确保 arguments 是合法 JSON 对象,不要包含任何 XML/标记文本。"
|
|
1064
|
+
})
|
|
1065
|
+
}]
|
|
1066
|
+
});
|
|
1067
|
+
hookCtx.toolResult = {
|
|
1068
|
+
id: toolCall.id,
|
|
1069
|
+
name: tcName,
|
|
1070
|
+
success: false,
|
|
1071
|
+
result: { success: false, output: "", error: invalidMessage }
|
|
1072
|
+
};
|
|
1073
|
+
iterProcessedCount++;
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
const tcArgs = tcArgsParsed.args;
|
|
1046
1077
|
hookCtx.currentToolCall = {
|
|
1047
1078
|
id: toolCall.id,
|
|
1048
1079
|
name: tcName,
|
|
@@ -1493,6 +1524,10 @@ class AgentComponent extends BaseComponent {
|
|
|
1493
1524
|
const rawArgs = toolCall.function?.arguments ?? toolCall.arguments;
|
|
1494
1525
|
return safeParseToolArgs(rawArgs);
|
|
1495
1526
|
}
|
|
1527
|
+
parseToolCallArgumentsStrict(toolCall) {
|
|
1528
|
+
const rawArgs = toolCall.function?.arguments ?? toolCall.arguments;
|
|
1529
|
+
return parseToolArgsStrict(rawArgs);
|
|
1530
|
+
}
|
|
1496
1531
|
async executeTool(ctx) {
|
|
1497
1532
|
const toolCall = ctx.currentToolCall;
|
|
1498
1533
|
const toolName = toolCall.function?.name || toolCall.name || "unknown";
|
|
@@ -10,14 +10,14 @@ import {
|
|
|
10
10
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
11
11
|
import {
|
|
12
12
|
BaseComponent
|
|
13
|
-
} from "./roy-agent-core-
|
|
13
|
+
} from "./roy-agent-core-ead21w5c.js";
|
|
14
14
|
import {
|
|
15
15
|
createHook
|
|
16
|
-
} from "./roy-agent-core-
|
|
16
|
+
} from "./roy-agent-core-54wzmvnb.js";
|
|
17
17
|
import {
|
|
18
18
|
globalHookManager,
|
|
19
19
|
init_global_hook_manager
|
|
20
|
-
} from "./roy-agent-core-
|
|
20
|
+
} from "./roy-agent-core-3j4by8rj.js";
|
|
21
21
|
import {
|
|
22
22
|
createLogger,
|
|
23
23
|
init_logger
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createHook
|
|
3
|
-
} from "./roy-agent-core-
|
|
3
|
+
} from "./roy-agent-core-54wzmvnb.js";
|
|
4
4
|
import {
|
|
5
5
|
HookManager
|
|
6
|
-
} from "./roy-agent-core-
|
|
6
|
+
} from "./roy-agent-core-3j4by8rj.js";
|
|
7
7
|
|
|
8
8
|
// src/env/component.ts
|
|
9
9
|
class BaseComponent {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MemorySessionStore,
|
|
3
3
|
SQLiteSessionStore
|
|
4
|
-
} from "./roy-agent-core-
|
|
4
|
+
} from "./roy-agent-core-h93v6p6y.js";
|
|
5
5
|
import {
|
|
6
6
|
init_search_query_parser,
|
|
7
7
|
matchesQuery,
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
13
13
|
import {
|
|
14
14
|
BaseComponent
|
|
15
|
-
} from "./roy-agent-core-
|
|
15
|
+
} from "./roy-agent-core-ead21w5c.js";
|
|
16
16
|
import {
|
|
17
17
|
TracedAs,
|
|
18
18
|
init_decorator
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SessionMessageConverter
|
|
3
|
-
} from "./roy-agent-core-
|
|
3
|
+
} from "./roy-agent-core-pk1b3kvf.js";
|
|
4
4
|
import {
|
|
5
5
|
canonicalizeParts
|
|
6
|
-
} from "./roy-agent-core-
|
|
6
|
+
} from "./roy-agent-core-zy3hqfwg.js";
|
|
7
7
|
import {
|
|
8
8
|
createLogger,
|
|
9
9
|
init_logger
|
|
@@ -1107,6 +1107,19 @@ var init_sqlite_task_store = __esm(() => {
|
|
|
1107
1107
|
const rows = stmt.all(`%${query}%`, `%${query}%`, limit);
|
|
1108
1108
|
return rows.map((row) => this.rowToTask(row));
|
|
1109
1109
|
}
|
|
1110
|
+
async findTasksByContextValue(value, limit = 50) {
|
|
1111
|
+
await this.initialize();
|
|
1112
|
+
if (!value)
|
|
1113
|
+
return [];
|
|
1114
|
+
const sql = `
|
|
1115
|
+
SELECT * FROM task
|
|
1116
|
+
WHERE context LIKE ?
|
|
1117
|
+
ORDER BY updated_at DESC
|
|
1118
|
+
LIMIT ?
|
|
1119
|
+
`;
|
|
1120
|
+
const rows = this.db.prepare(sql).all(`%${value}%`, limit);
|
|
1121
|
+
return rows.map((row) => this.rowToTask(row));
|
|
1122
|
+
}
|
|
1110
1123
|
rowToTag(row) {
|
|
1111
1124
|
return {
|
|
1112
1125
|
id: row.id,
|
|
@@ -8,8 +8,9 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
buildOpencodeHeaders,
|
|
10
10
|
invoke,
|
|
11
|
-
invokeNonStream
|
|
12
|
-
|
|
11
|
+
invokeNonStream,
|
|
12
|
+
toolParametersToJsonSchema
|
|
13
|
+
} from "./roy-agent-core-zy3hqfwg.js";
|
|
13
14
|
import {
|
|
14
15
|
ContextError,
|
|
15
16
|
ErrorCodes
|
|
@@ -20,12 +21,12 @@ import {
|
|
|
20
21
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
21
22
|
import {
|
|
22
23
|
BaseComponent
|
|
23
|
-
} from "./roy-agent-core-
|
|
24
|
+
} from "./roy-agent-core-ead21w5c.js";
|
|
24
25
|
import {
|
|
25
26
|
LLMHookPoints,
|
|
26
27
|
globalHookManager,
|
|
27
28
|
init_global_hook_manager
|
|
28
|
-
} from "./roy-agent-core-
|
|
29
|
+
} from "./roy-agent-core-3j4by8rj.js";
|
|
29
30
|
import {
|
|
30
31
|
TracedAs,
|
|
31
32
|
init_decorator,
|
|
@@ -50,7 +51,6 @@ import {
|
|
|
50
51
|
stream as piStream,
|
|
51
52
|
getModel
|
|
52
53
|
} from "@earendil-works/pi-ai";
|
|
53
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
54
54
|
var logger = createLogger("llm:adapter-pi-ai");
|
|
55
55
|
var KNOWN_PROVIDER_APIS = {
|
|
56
56
|
anthropic: { api: "anthropic-messages", contextLimit: 200000, maxTokensLimit: 8192 },
|
|
@@ -158,33 +158,8 @@ function safeJson(s) {
|
|
|
158
158
|
return {};
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
|
-
function isZodSchema(obj) {
|
|
162
|
-
return !!obj && typeof obj === "object" && "_def" in obj;
|
|
163
|
-
}
|
|
164
161
|
function convertToolParameters(parameters) {
|
|
165
|
-
|
|
166
|
-
return { type: "object", properties: {} };
|
|
167
|
-
if (typeof parameters !== "object")
|
|
168
|
-
return { type: "object", properties: {} };
|
|
169
|
-
if (isZodSchema(parameters)) {
|
|
170
|
-
try {
|
|
171
|
-
const jsonSchema = zodToJsonSchema(parameters, "zod");
|
|
172
|
-
if ("$ref" in jsonSchema && jsonSchema.definitions) {
|
|
173
|
-
const def = jsonSchema.definitions.zod;
|
|
174
|
-
if (def && def.type === "object" && def.properties) {
|
|
175
|
-
return def;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return jsonSchema;
|
|
179
|
-
} catch {
|
|
180
|
-
return { type: "object", properties: {} };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
const paramsObj = parameters;
|
|
184
|
-
if (paramsObj.type === "object" && paramsObj.properties) {
|
|
185
|
-
return paramsObj;
|
|
186
|
-
}
|
|
187
|
-
return { type: "object", properties: {} };
|
|
162
|
+
return toolParametersToJsonSchema(parameters);
|
|
188
163
|
}
|
|
189
164
|
function toPiTools(tools) {
|
|
190
165
|
if (!tools)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AgentComponentAdapter,
|
|
3
3
|
init_agent_component_adapter
|
|
4
|
-
} from "./roy-agent-core-
|
|
4
|
+
} from "./roy-agent-core-jfptxs4f.js";
|
|
5
5
|
import"./roy-agent-core-1db4vpc6.js";
|
|
6
6
|
import"./roy-agent-core-e25xkv53.js";
|
|
7
|
-
import"./roy-agent-core-
|
|
7
|
+
import"./roy-agent-core-3j4by8rj.js";
|
|
8
8
|
import"./roy-agent-core-wn59p0zw.js";
|
|
9
9
|
import"./roy-agent-core-zbb3sk72.js";
|
|
10
10
|
import"./roy-agent-core-a9pbda78.js";
|
|
@@ -27,11 +27,11 @@ import {
|
|
|
27
27
|
BackgroundTaskManager,
|
|
28
28
|
createDelegateTool,
|
|
29
29
|
createStopTool
|
|
30
|
-
} from "./roy-agent-core-
|
|
30
|
+
} from "./roy-agent-core-nvcqp6p9.js";
|
|
31
31
|
import {
|
|
32
32
|
SQLiteTaskStore,
|
|
33
33
|
getDefaultTaskDbPath
|
|
34
|
-
} from "./roy-agent-core-
|
|
34
|
+
} from "./roy-agent-core-p1p9mr8f.js";
|
|
35
35
|
import {
|
|
36
36
|
TaskHookPoints
|
|
37
37
|
} from "./roy-agent-core-1pg0fepg.js";
|
|
@@ -40,11 +40,11 @@ import {
|
|
|
40
40
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
41
41
|
import {
|
|
42
42
|
BaseComponent
|
|
43
|
-
} from "./roy-agent-core-
|
|
43
|
+
} from "./roy-agent-core-ead21w5c.js";
|
|
44
44
|
import {
|
|
45
45
|
globalHookManager,
|
|
46
46
|
init_global_hook_manager
|
|
47
|
-
} from "./roy-agent-core-
|
|
47
|
+
} from "./roy-agent-core-3j4by8rj.js";
|
|
48
48
|
import {
|
|
49
49
|
getEnvContextOrEmpty,
|
|
50
50
|
init_env_context
|
|
@@ -60,18 +60,212 @@ function buildOpencodeHeaders(providerId, conversationId, baseURL) {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
// src/env/llm/invoke.ts
|
|
63
|
-
init_logger();
|
|
64
63
|
import { randomUUID } from "crypto";
|
|
65
64
|
import { streamText, jsonSchema } from "ai";
|
|
66
65
|
import { createOpenAI } from "@ai-sdk/openai";
|
|
67
66
|
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
68
67
|
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
69
68
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
69
|
+
|
|
70
|
+
// src/env/llm/tool-schema.ts
|
|
70
71
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
72
|
+
function isZodSchema(obj) {
|
|
73
|
+
return !!obj && typeof obj === "object" && "_def" in obj;
|
|
74
|
+
}
|
|
75
|
+
function dereference(node, root, seenRefs = new Set) {
|
|
76
|
+
if (Array.isArray(node)) {
|
|
77
|
+
return node.map((n) => dereference(n, root, seenRefs));
|
|
78
|
+
}
|
|
79
|
+
if (!node || typeof node !== "object")
|
|
80
|
+
return node;
|
|
81
|
+
const obj = node;
|
|
82
|
+
const ref = obj["$ref"];
|
|
83
|
+
if (typeof ref === "string" && ref.startsWith("#/")) {
|
|
84
|
+
if (seenRefs.has(ref)) {
|
|
85
|
+
return { type: "object" };
|
|
86
|
+
}
|
|
87
|
+
const target = resolvePointer(root, ref);
|
|
88
|
+
if (target === undefined) {
|
|
89
|
+
return { type: "object" };
|
|
90
|
+
}
|
|
91
|
+
const nextSeen = new Set(seenRefs);
|
|
92
|
+
nextSeen.add(ref);
|
|
93
|
+
const resolved = dereference(target, root, nextSeen);
|
|
94
|
+
const { $ref: _drop, ...rest } = obj;
|
|
95
|
+
if (resolved && typeof resolved === "object" && !Array.isArray(resolved)) {
|
|
96
|
+
return { ...resolved, ...rest };
|
|
97
|
+
}
|
|
98
|
+
return resolved;
|
|
99
|
+
}
|
|
100
|
+
const out = {};
|
|
101
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
102
|
+
if (k === "definitions" || k === "$defs" || k === "$schema")
|
|
103
|
+
continue;
|
|
104
|
+
out[k] = dereference(v, root, seenRefs);
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
function resolvePointer(root, ref) {
|
|
109
|
+
const path = ref.replace(/^#\//, "").split("/").filter((s) => s.length > 0);
|
|
110
|
+
let cur = root;
|
|
111
|
+
for (const rawSeg of path) {
|
|
112
|
+
const seg = rawSeg.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
113
|
+
if (cur === null || cur === undefined || typeof cur !== "object")
|
|
114
|
+
return;
|
|
115
|
+
cur = cur[seg];
|
|
116
|
+
}
|
|
117
|
+
return cur;
|
|
118
|
+
}
|
|
119
|
+
function toolParametersToJsonSchema(parameters) {
|
|
120
|
+
const EMPTY = {
|
|
121
|
+
type: "object",
|
|
122
|
+
properties: {}
|
|
123
|
+
};
|
|
124
|
+
if (parameters === null || parameters === undefined)
|
|
125
|
+
return EMPTY;
|
|
126
|
+
if (typeof parameters !== "object")
|
|
127
|
+
return EMPTY;
|
|
128
|
+
let raw;
|
|
129
|
+
if (isZodSchema(parameters)) {
|
|
130
|
+
try {
|
|
131
|
+
raw = zodToJsonSchema(parameters, "zod");
|
|
132
|
+
} catch {
|
|
133
|
+
return EMPTY;
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
raw = JSON.parse(JSON.stringify(parameters));
|
|
137
|
+
}
|
|
138
|
+
const deref = dereference(raw, raw);
|
|
139
|
+
if (deref["type"] !== "object") {
|
|
140
|
+
if (deref["properties"] && typeof deref["properties"] === "object") {
|
|
141
|
+
deref["type"] = "object";
|
|
142
|
+
} else {
|
|
143
|
+
return EMPTY;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!deref["properties"] || typeof deref["properties"] !== "object") {
|
|
147
|
+
deref["properties"] = {};
|
|
148
|
+
}
|
|
149
|
+
delete deref["definitions"];
|
|
150
|
+
delete deref["$defs"];
|
|
151
|
+
delete deref["$schema"];
|
|
152
|
+
return deref;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/env/llm/invoke.ts
|
|
156
|
+
init_logger();
|
|
71
157
|
|
|
72
158
|
// src/env/llm/tool-args-parser.ts
|
|
73
159
|
init_logger();
|
|
74
160
|
var logger = createLogger("llm:tool-args-parser");
|
|
161
|
+
var ARG_KEY_RE = /<arg_key>([\s\S]*?)<\/arg_key>\s*<arg_value>([\s\S]*?)<\/arg_value>/g;
|
|
162
|
+
function salvageTextualToolArgs(raw) {
|
|
163
|
+
if (!raw.includes("<arg_key>"))
|
|
164
|
+
return;
|
|
165
|
+
const out = {};
|
|
166
|
+
let matched = false;
|
|
167
|
+
ARG_KEY_RE.lastIndex = 0;
|
|
168
|
+
let m;
|
|
169
|
+
while ((m = ARG_KEY_RE.exec(raw)) !== null) {
|
|
170
|
+
const key = m[1].trim();
|
|
171
|
+
if (!key)
|
|
172
|
+
continue;
|
|
173
|
+
const value = m[2];
|
|
174
|
+
try {
|
|
175
|
+
out[key] = JSON.parse(value);
|
|
176
|
+
} catch {
|
|
177
|
+
out[key] = value;
|
|
178
|
+
}
|
|
179
|
+
matched = true;
|
|
180
|
+
}
|
|
181
|
+
return matched ? out : undefined;
|
|
182
|
+
}
|
|
183
|
+
function stripTextualToolArgsMarkers(raw) {
|
|
184
|
+
return raw.replace(/<\/?arg_key>/g, "").replace(/<\/?arg_value>/g, "").replace(/<\/?tool_call>/g, "");
|
|
185
|
+
}
|
|
186
|
+
function parseToolArgsStrict(args) {
|
|
187
|
+
if (args && typeof args === "object" && !Array.isArray(args)) {
|
|
188
|
+
return { ok: true, args };
|
|
189
|
+
}
|
|
190
|
+
if (typeof args !== "string") {
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
args: {},
|
|
194
|
+
error: `tool arguments must be a JSON object, got ${args === null ? "null" : typeof args}`
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const raw = args.trim();
|
|
198
|
+
if (raw.length === 0) {
|
|
199
|
+
return { ok: true, args: {} };
|
|
200
|
+
}
|
|
201
|
+
const direct = tryParseObject(raw);
|
|
202
|
+
if (direct.ok) {
|
|
203
|
+
if (raw.includes("<arg_key>") || raw.includes("<arg_value>") || raw.includes("<tool_call>")) {
|
|
204
|
+
logger.warn("[parseToolArgsStrict] arguments 里出现上游文本工具调用标记,已清洗值中的 marker");
|
|
205
|
+
return { ok: true, args: sanitizeArgValues(direct.args), salvaged: true };
|
|
206
|
+
}
|
|
207
|
+
return { ok: true, args: direct.args };
|
|
208
|
+
}
|
|
209
|
+
if (raw.includes("<arg_key>") || raw.includes("<arg_value>")) {
|
|
210
|
+
const stripped = stripTextualToolArgsMarkers(raw);
|
|
211
|
+
const afterStrip = tryParseObject(stripped);
|
|
212
|
+
if (afterStrip.ok) {
|
|
213
|
+
logger.warn("[parseToolArgsStrict] 上游把文本工具调用标记混进了 arguments,已剥离 marker 后恢复");
|
|
214
|
+
return { ok: true, args: afterStrip.args, salvaged: true };
|
|
215
|
+
}
|
|
216
|
+
const salvaged = salvageTextualToolArgs(raw);
|
|
217
|
+
if (salvaged) {
|
|
218
|
+
logger.warn("[parseToolArgsStrict] 上游输出的是文本工具调用格式,已按 <arg_key>/<arg_value> 重建参数", { keys: Object.keys(salvaged).join(",") });
|
|
219
|
+
return { ok: true, args: salvaged, salvaged: true };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const nested = tryParseObject(raw);
|
|
223
|
+
if (!nested.ok && raw.length > 1) {
|
|
224
|
+
const rec = safeParseToolArgs(raw);
|
|
225
|
+
if (Object.keys(rec).length > 0) {
|
|
226
|
+
return { ok: true, args: rec, salvaged: true };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
ok: false,
|
|
231
|
+
args: {},
|
|
232
|
+
error: "tool arguments 不是合法 JSON(上游模型输出可能已损坏)。请重新调用该工具,确保 arguments 是合法 JSON 对象。" + ` 原始片段:${raw.slice(0, 200)}`
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function sanitizeArgValues(args) {
|
|
236
|
+
const clean = (v) => {
|
|
237
|
+
if (typeof v === "string")
|
|
238
|
+
return stripTextualToolArgsMarkers(v);
|
|
239
|
+
if (Array.isArray(v))
|
|
240
|
+
return v.map(clean);
|
|
241
|
+
if (v && typeof v === "object") {
|
|
242
|
+
const out = {};
|
|
243
|
+
for (const [k, val] of Object.entries(v)) {
|
|
244
|
+
out[k] = clean(val);
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
return v;
|
|
249
|
+
};
|
|
250
|
+
return clean(args);
|
|
251
|
+
}
|
|
252
|
+
function tryParseObject(raw) {
|
|
253
|
+
try {
|
|
254
|
+
const parsed = JSON.parse(raw);
|
|
255
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
256
|
+
return { ok: true, args: parsed };
|
|
257
|
+
}
|
|
258
|
+
if (typeof parsed === "string") {
|
|
259
|
+
const twice = JSON.parse(parsed);
|
|
260
|
+
if (twice && typeof twice === "object" && !Array.isArray(twice)) {
|
|
261
|
+
return { ok: true, args: twice };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return { ok: false };
|
|
265
|
+
} catch {
|
|
266
|
+
return { ok: false };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
75
269
|
var MAX_SAFE_DEPTH = 5;
|
|
76
270
|
function safeParseToolArgs(args, depth = 0) {
|
|
77
271
|
if (depth >= MAX_SAFE_DEPTH) {
|
|
@@ -448,23 +642,7 @@ function convertToolsToSDK(tools, providerName) {
|
|
|
448
642
|
return result;
|
|
449
643
|
}
|
|
450
644
|
function extractToolSchema(parameters) {
|
|
451
|
-
|
|
452
|
-
const zodSchema = parameters;
|
|
453
|
-
const schema = zodToJsonSchema(zodSchema, "zod");
|
|
454
|
-
if ("$ref" in schema && schema.definitions) {
|
|
455
|
-
const def = schema.definitions.zod;
|
|
456
|
-
if (def && def.type === "object" && def.properties) {
|
|
457
|
-
return {
|
|
458
|
-
type: "object",
|
|
459
|
-
properties: def.properties,
|
|
460
|
-
required: def.required,
|
|
461
|
-
additionalProperties: true
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return schema;
|
|
466
|
-
}
|
|
467
|
-
return parameters;
|
|
645
|
+
return toolParametersToJsonSchema(parameters);
|
|
468
646
|
}
|
|
469
647
|
function generateProviderOptions(providerType, options) {
|
|
470
648
|
const result = {
|
|
@@ -948,4 +1126,4 @@ function createInvokeConfig(model, apiKey, baseURL) {
|
|
|
948
1126
|
};
|
|
949
1127
|
}
|
|
950
1128
|
|
|
951
|
-
export { safeParseToolArgs, generateOpencodeSessionId, isOpencodeProvider, isOpencodeEndpoint, getOrCreateOpencodeSessionId, buildOpencodeHeaders, canonicalizeParts, withTimeout, parseModelString, convertToSDKMessages, invoke, invokeNonStream, createInvokeConfig };
|
|
1129
|
+
export { toolParametersToJsonSchema, parseToolArgsStrict, safeParseToolArgs, generateOpencodeSessionId, isOpencodeProvider, isOpencodeEndpoint, getOrCreateOpencodeSessionId, buildOpencodeHeaders, canonicalizeParts, withTimeout, parseModelString, convertToSDKMessages, invoke, invokeNonStream, createInvokeConfig };
|