@sisu-ai/mw-tool-calling 9.0.1 → 9.0.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.js +19 -13
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -40,12 +40,18 @@ export const toolCalling = async (ctx, next) => {
|
|
|
40
40
|
messages: ctx.messages.length,
|
|
41
41
|
});
|
|
42
42
|
const allowTools = i === 0 ? "auto" : "none";
|
|
43
|
-
const genOpts = {
|
|
43
|
+
const genOpts = {
|
|
44
|
+
toolChoice: allowTools,
|
|
45
|
+
signal: ctx.signal,
|
|
46
|
+
};
|
|
44
47
|
if (allowTools !== "none") {
|
|
45
48
|
genOpts.tools = aliasedTools; // Pass renamed tools to adapter
|
|
46
49
|
genOpts.parallelToolCalls = false;
|
|
47
50
|
}
|
|
48
|
-
const out =
|
|
51
|
+
const out = await ctx.model.generate(ctx.messages, genOpts);
|
|
52
|
+
if (!out || typeof out !== "object" || !("message" in out)) {
|
|
53
|
+
throw new Error("[tool-calling] model did not return a message");
|
|
54
|
+
}
|
|
49
55
|
const msg = out.message;
|
|
50
56
|
const toolCalls = msg.tool_calls;
|
|
51
57
|
if (toolCalls && toolCalls.length > 0) {
|
|
@@ -105,11 +111,11 @@ export const toolCalling = async (ctx, next) => {
|
|
|
105
111
|
ctx.log.debug?.("[tool-calling] reusing cached tool result for duplicate call", { name: call.name, id: call.id });
|
|
106
112
|
}
|
|
107
113
|
// Prefer tool_call_id when available (tools API)
|
|
108
|
-
const toolMsg = {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
const toolMsg = {
|
|
115
|
+
role: "tool",
|
|
116
|
+
content: JSON.stringify(result),
|
|
117
|
+
...(call.id ? { tool_call_id: call.id } : { name: call.name }),
|
|
118
|
+
};
|
|
113
119
|
ctx.messages.push(toolMsg);
|
|
114
120
|
ctx.log.debug?.("[tool-calling] tool result appended", {
|
|
115
121
|
name: call.name,
|
|
@@ -143,7 +149,7 @@ export const iterativeToolCalling = async (ctx, next) => {
|
|
|
143
149
|
parallelToolCalls: false,
|
|
144
150
|
signal: ctx.signal,
|
|
145
151
|
};
|
|
146
|
-
const out =
|
|
152
|
+
const out = await ctx.model.generate(ctx.messages, genOpts);
|
|
147
153
|
const msg = out.message;
|
|
148
154
|
const toolCalls = msg.tool_calls;
|
|
149
155
|
if (toolCalls && toolCalls.length > 0) {
|
|
@@ -203,11 +209,11 @@ export const iterativeToolCalling = async (ctx, next) => {
|
|
|
203
209
|
id: call.id,
|
|
204
210
|
});
|
|
205
211
|
}
|
|
206
|
-
const toolMsg = {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
212
|
+
const toolMsg = {
|
|
213
|
+
role: "tool",
|
|
214
|
+
content: JSON.stringify(result),
|
|
215
|
+
...(call.id ? { tool_call_id: call.id } : { name: call.name }),
|
|
216
|
+
};
|
|
211
217
|
ctx.messages.push(toolMsg);
|
|
212
218
|
}
|
|
213
219
|
continue; // next round may call more tools
|