@arki-moe/agent-ts 2.2.1 → 2.2.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/adapter/openai.js +37 -0
- package/dist/adapter/openrouter.js +37 -0
- package/package.json +1 -1
package/dist/adapter/openai.js
CHANGED
|
@@ -66,6 +66,24 @@ async function openaiAdapter(config, context, tools) {
|
|
|
66
66
|
}
|
|
67
67
|
if (onStream) {
|
|
68
68
|
let content = "";
|
|
69
|
+
const toolCalls = new Map();
|
|
70
|
+
const upsertToolCall = (tc) => {
|
|
71
|
+
const index = typeof tc?.index === "number" ? tc.index : toolCalls.size;
|
|
72
|
+
let entry = toolCalls.get(index);
|
|
73
|
+
if (!entry) {
|
|
74
|
+
entry = { args: "" };
|
|
75
|
+
toolCalls.set(index, entry);
|
|
76
|
+
}
|
|
77
|
+
if (typeof tc?.id === "string")
|
|
78
|
+
entry.id = tc.id;
|
|
79
|
+
const fn = tc?.function;
|
|
80
|
+
if (fn) {
|
|
81
|
+
if (typeof fn.name === "string")
|
|
82
|
+
entry.name = fn.name;
|
|
83
|
+
if (typeof fn.arguments === "string")
|
|
84
|
+
entry.args += fn.arguments;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
69
87
|
await (0, sse_1.readSse)(res, async (dataLine) => {
|
|
70
88
|
if (dataLine === "[DONE]")
|
|
71
89
|
return;
|
|
@@ -79,11 +97,30 @@ async function openaiAdapter(config, context, tools) {
|
|
|
79
97
|
const delta = parsed?.choices?.[0]?.delta;
|
|
80
98
|
if (!delta)
|
|
81
99
|
return;
|
|
100
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
101
|
+
for (const tc of delta.tool_calls) {
|
|
102
|
+
upsertToolCall(tc);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
82
105
|
if (typeof delta.content === "string") {
|
|
83
106
|
content += delta.content;
|
|
84
107
|
await Promise.resolve(onStream(delta.content));
|
|
85
108
|
}
|
|
86
109
|
});
|
|
110
|
+
if (toolCalls.size > 0) {
|
|
111
|
+
return [...toolCalls.entries()]
|
|
112
|
+
.sort((a, b) => a[0] - b[0])
|
|
113
|
+
.map(([index, tc]) => {
|
|
114
|
+
if (!tc.name)
|
|
115
|
+
throw new Error(`OpenAI streaming tool call missing function name at index ${index}`);
|
|
116
|
+
return {
|
|
117
|
+
role: types_1.Role.ToolCall,
|
|
118
|
+
toolName: tc.name,
|
|
119
|
+
callId: tc.id ?? `call_${index}`,
|
|
120
|
+
argsText: tc.args.length ? tc.args : "{}",
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
87
124
|
return [{ role: types_1.Role.Ai, content }];
|
|
88
125
|
}
|
|
89
126
|
const text = await res.text();
|
|
@@ -76,6 +76,24 @@ async function openrouterAdapter(config, context, tools) {
|
|
|
76
76
|
}
|
|
77
77
|
if (onStream) {
|
|
78
78
|
let content = "";
|
|
79
|
+
const toolCalls = new Map();
|
|
80
|
+
const upsertToolCall = (tc) => {
|
|
81
|
+
const index = typeof tc?.index === "number" ? tc.index : toolCalls.size;
|
|
82
|
+
let entry = toolCalls.get(index);
|
|
83
|
+
if (!entry) {
|
|
84
|
+
entry = { args: "" };
|
|
85
|
+
toolCalls.set(index, entry);
|
|
86
|
+
}
|
|
87
|
+
if (typeof tc?.id === "string")
|
|
88
|
+
entry.id = tc.id;
|
|
89
|
+
const fn = tc?.function;
|
|
90
|
+
if (fn) {
|
|
91
|
+
if (typeof fn.name === "string")
|
|
92
|
+
entry.name = fn.name;
|
|
93
|
+
if (typeof fn.arguments === "string")
|
|
94
|
+
entry.args += fn.arguments;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
79
97
|
await (0, sse_1.readSse)(res, async (dataLine) => {
|
|
80
98
|
if (dataLine === "[DONE]")
|
|
81
99
|
return;
|
|
@@ -89,11 +107,30 @@ async function openrouterAdapter(config, context, tools) {
|
|
|
89
107
|
const delta = parsed?.choices?.[0]?.delta;
|
|
90
108
|
if (!delta)
|
|
91
109
|
return;
|
|
110
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
111
|
+
for (const tc of delta.tool_calls) {
|
|
112
|
+
upsertToolCall(tc);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
92
115
|
if (typeof delta.content === "string") {
|
|
93
116
|
content += delta.content;
|
|
94
117
|
await Promise.resolve(onStream(delta.content));
|
|
95
118
|
}
|
|
96
119
|
});
|
|
120
|
+
if (toolCalls.size > 0) {
|
|
121
|
+
return [...toolCalls.entries()]
|
|
122
|
+
.sort((a, b) => a[0] - b[0])
|
|
123
|
+
.map(([index, tc]) => {
|
|
124
|
+
if (!tc.name)
|
|
125
|
+
throw new Error(`OpenRouter streaming tool call missing function name at index ${index}`);
|
|
126
|
+
return {
|
|
127
|
+
role: types_1.Role.ToolCall,
|
|
128
|
+
toolName: tc.name,
|
|
129
|
+
callId: tc.id ?? `call_${index}`,
|
|
130
|
+
argsText: tc.args.length ? tc.args : "{}",
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
}
|
|
97
134
|
return [{ role: types_1.Role.Ai, content }];
|
|
98
135
|
}
|
|
99
136
|
const text = await res.text();
|