@chatcops/core 0.4.0 → 0.5.0
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/chat.d.ts +13 -0
- package/dist/agent/chat.d.ts.map +1 -0
- package/dist/agent/chat.js +42 -0
- package/dist/agent/chat.js.map +1 -0
- package/dist/agent/errors.d.ts +14 -0
- package/dist/agent/errors.d.ts.map +1 -0
- package/dist/agent/errors.js +24 -0
- package/dist/agent/errors.js.map +1 -0
- package/dist/agent/index.d.ts +5 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +5 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/run-turn.d.ts +68 -0
- package/dist/agent/run-turn.d.ts.map +1 -0
- package/dist/agent/run-turn.js +344 -0
- package/dist/agent/run-turn.js.map +1 -0
- package/dist/agent/tool-registry.d.ts +28 -0
- package/dist/agent/tool-registry.d.ts.map +1 -0
- package/dist/agent/tool-registry.js +69 -0
- package/dist/agent/tool-registry.js.map +1 -0
- package/dist/analytics/index.d.ts +1 -0
- package/dist/analytics/index.d.ts.map +1 -1
- package/dist/analytics/index.js +1 -0
- package/dist/analytics/index.js.map +1 -1
- package/dist/analytics/sink.d.ts +24 -0
- package/dist/analytics/sink.d.ts.map +1 -0
- package/dist/analytics/sink.js +106 -0
- package/dist/analytics/sink.js.map +1 -0
- package/dist/conversation/index.d.ts +1 -0
- package/dist/conversation/index.d.ts.map +1 -1
- package/dist/conversation/index.js.map +1 -1
- package/dist/conversation/manager.d.ts +62 -6
- package/dist/conversation/manager.d.ts.map +1 -1
- package/dist/conversation/manager.js +109 -34
- package/dist/conversation/manager.js.map +1 -1
- package/dist/conversation/store.d.ts +6 -5
- package/dist/conversation/store.d.ts.map +1 -1
- package/dist/conversation/store.js +4 -0
- package/dist/conversation/store.js.map +1 -1
- package/dist/index.d.ts +18 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/dist/limits/budget.d.ts +84 -0
- package/dist/limits/budget.d.ts.map +1 -0
- package/dist/limits/budget.js +114 -0
- package/dist/limits/budget.js.map +1 -0
- package/dist/limits/rate-limit.d.ts +53 -0
- package/dist/limits/rate-limit.d.ts.map +1 -0
- package/dist/limits/rate-limit.js +68 -0
- package/dist/limits/rate-limit.js.map +1 -0
- package/dist/parts.d.ts +9 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +17 -0
- package/dist/parts.js.map +1 -0
- package/dist/protocol/ai-sdk.d.ts +27 -0
- package/dist/protocol/ai-sdk.d.ts.map +1 -0
- package/dist/protocol/ai-sdk.js +127 -0
- package/dist/protocol/ai-sdk.js.map +1 -0
- package/dist/protocol/errors.d.ts +10 -0
- package/dist/protocol/errors.d.ts.map +1 -0
- package/dist/protocol/errors.js +42 -0
- package/dist/protocol/errors.js.map +1 -0
- package/dist/protocol/index.d.ts +9 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +6 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/legacy.d.ts +18 -0
- package/dist/protocol/legacy.d.ts.map +1 -0
- package/dist/protocol/legacy.js +41 -0
- package/dist/protocol/legacy.js.map +1 -0
- package/dist/protocol/sse.d.ts +10 -0
- package/dist/protocol/sse.d.ts.map +1 -0
- package/dist/protocol/sse.js +15 -0
- package/dist/protocol/sse.js.map +1 -0
- package/dist/protocol/v1.d.ts +140 -0
- package/dist/protocol/v1.d.ts.map +1 -0
- package/dist/protocol/v1.js +15 -0
- package/dist/protocol/v1.js.map +1 -0
- package/dist/providers/base.d.ts +11 -27
- package/dist/providers/base.d.ts.map +1 -1
- package/dist/providers/base.js +34 -45
- package/dist/providers/base.js.map +1 -1
- package/dist/providers/claude.d.ts +26 -5
- package/dist/providers/claude.d.ts.map +1 -1
- package/dist/providers/claude.js +176 -89
- package/dist/providers/claude.js.map +1 -1
- package/dist/providers/gemini.d.ts +18 -3
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +208 -91
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/history.d.ts +52 -0
- package/dist/providers/history.d.ts.map +1 -0
- package/dist/providers/history.js +212 -0
- package/dist/providers/history.js.map +1 -0
- package/dist/providers/index.d.ts +5 -5
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +5 -4
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/openai.d.ts +27 -6
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +209 -112
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/registry.d.ts +7 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +12 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/schema.d.ts +16 -0
- package/dist/providers/schema.d.ts.map +1 -0
- package/dist/providers/schema.js +80 -0
- package/dist/providers/schema.js.map +1 -0
- package/dist/providers/tool-execution.d.ts +10 -0
- package/dist/providers/tool-execution.d.ts.map +1 -1
- package/dist/providers/tool-execution.js +38 -12
- package/dist/providers/tool-execution.js.map +1 -1
- package/dist/providers/types.d.ts +71 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +2 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/storage/adapt.d.ts +15 -0
- package/dist/storage/adapt.d.ts.map +1 -0
- package/dist/storage/adapt.js +83 -0
- package/dist/storage/adapt.js.map +1 -0
- package/dist/storage/errors.d.ts +18 -0
- package/dist/storage/errors.d.ts.map +1 -0
- package/dist/storage/errors.js +28 -0
- package/dist/storage/errors.js.map +1 -0
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +4 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/memory.d.ts +24 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +265 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/types.d.ts +118 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +2 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/testing/conformance.d.ts +29 -0
- package/dist/testing/conformance.d.ts.map +1 -0
- package/dist/testing/conformance.js +335 -0
- package/dist/testing/conformance.js.map +1 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +5 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/provider.d.ts +45 -0
- package/dist/testing/provider.d.ts.map +1 -0
- package/dist/testing/provider.js +82 -0
- package/dist/testing/provider.js.map +1 -0
- package/dist/tools/base.d.ts +14 -4
- package/dist/tools/base.d.ts.map +1 -1
- package/dist/tools/base.js +4 -2
- package/dist/tools/base.js.map +1 -1
- package/dist/tools/lead-capture.d.ts +3 -1
- package/dist/tools/lead-capture.d.ts.map +1 -1
- package/dist/tools/lead-capture.js +4 -0
- package/dist/tools/lead-capture.js.map +1 -1
- package/dist/types.d.ts +139 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +50 -2
package/dist/providers/openai.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { chatSyncViaTurn, chatViaTurn } from '../agent/chat.js';
|
|
3
|
+
import { joinSystem } from '../agent/run-turn.js';
|
|
4
|
+
import { DEFAULT_MAX_TOKENS, toOpenAITools, warnOnce } from './base.js';
|
|
5
|
+
import { normalizeTurns } from './history.js';
|
|
6
|
+
import { parseToolInput } from './tool-execution.js';
|
|
4
7
|
const DEFAULT_MODEL = 'gpt-5-mini';
|
|
5
8
|
const DEFAULT_EFFORT = 'low';
|
|
6
|
-
let toolCallSequence = 0;
|
|
7
9
|
function createToolCallId() {
|
|
8
|
-
|
|
9
|
-
return `tool_call_${toolCallSequence}`;
|
|
10
|
+
return `call_${crypto.randomUUID()}`;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* OpenAI reasoning models (o-series, GPT-5 and later) take `reasoning_effort`
|
|
@@ -18,25 +19,130 @@ export function isOpenAIReasoningModel(model, baseURL) {
|
|
|
18
19
|
return false;
|
|
19
20
|
return /^(o\d|gpt-5|gpt-6)/.test(model) && !model.includes('chat-latest');
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Stored history as OpenAI chat messages: the system prompt first, then for
|
|
24
|
+
* each step an assistant message with its `tool_calls`, followed by one
|
|
25
|
+
* `tool` message per result in call order. Reasoning isn't sent back; the
|
|
26
|
+
* chat completions API has no field for it.
|
|
27
|
+
*/
|
|
28
|
+
export function toOpenAIStepMessages(systemPrompt, messages) {
|
|
29
|
+
const out = [{ role: 'system', content: systemPrompt }];
|
|
30
|
+
for (const turn of normalizeTurns(messages)) {
|
|
31
|
+
if (turn.role === 'user') {
|
|
32
|
+
for (const part of turn.parts) {
|
|
33
|
+
if (part.type === 'tool-result')
|
|
34
|
+
out.push(toToolMessage(part));
|
|
35
|
+
}
|
|
36
|
+
const text = turn.parts.flatMap((part) => (part.type === 'text' ? [part.text] : [])).join('\n\n');
|
|
37
|
+
if (text)
|
|
38
|
+
out.push({ role: 'user', content: text });
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
let text = '';
|
|
42
|
+
let calls = [];
|
|
43
|
+
const flush = () => {
|
|
44
|
+
if (text || calls.length) {
|
|
45
|
+
out.push({ role: 'assistant', content: text || null, ...(calls.length ? { tool_calls: calls } : {}) });
|
|
46
|
+
}
|
|
47
|
+
text = '';
|
|
48
|
+
calls = [];
|
|
49
|
+
};
|
|
50
|
+
for (const part of turn.parts) {
|
|
51
|
+
if (part.type === 'tool-result') {
|
|
52
|
+
flush();
|
|
53
|
+
out.push(toToolMessage(part));
|
|
54
|
+
}
|
|
55
|
+
else if (part.type === 'text') {
|
|
56
|
+
text += part.text;
|
|
57
|
+
}
|
|
58
|
+
else if (part.type === 'tool-call') {
|
|
59
|
+
calls.push({
|
|
60
|
+
id: part.toolCallId,
|
|
61
|
+
type: 'function',
|
|
62
|
+
function: {
|
|
63
|
+
name: part.toolName,
|
|
64
|
+
arguments: typeof part.input === 'string' ? part.input : JSON.stringify(part.input ?? {}),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
flush();
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
function toToolMessage(part) {
|
|
74
|
+
return {
|
|
75
|
+
role: 'tool',
|
|
76
|
+
tool_call_id: part.toolCallId,
|
|
77
|
+
content: JSON.stringify(part.output ?? { success: false, message: part.error }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Some OpenAI-compatible servers (Ollama among them) end a tool step with
|
|
82
|
+
* `stop` instead of `tool_calls`, so any finished step with calls counts.
|
|
83
|
+
*/
|
|
84
|
+
function finishReason(reason, hasToolCalls, refused) {
|
|
85
|
+
if (refused)
|
|
86
|
+
return 'refusal';
|
|
87
|
+
if (reason === 'length')
|
|
88
|
+
return 'length';
|
|
89
|
+
if (reason === 'content_filter')
|
|
90
|
+
return 'content-filter';
|
|
91
|
+
if (hasToolCalls)
|
|
92
|
+
return 'tool-calls';
|
|
93
|
+
return 'stop';
|
|
94
|
+
}
|
|
95
|
+
function toUsage(usage) {
|
|
96
|
+
const cached = usage?.prompt_tokens_details?.cached_tokens ?? 0;
|
|
97
|
+
return {
|
|
98
|
+
inputTokens: usage?.prompt_tokens ?? 0,
|
|
99
|
+
outputTokens: usage?.completion_tokens ?? 0,
|
|
100
|
+
...(cached ? { cacheReadTokens: cached } : {}),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function rejectsStreamOptions(error) {
|
|
104
|
+
const status = error?.status;
|
|
105
|
+
const message = error instanceof Error ? error.message : '';
|
|
106
|
+
return status === 400 && /stream_options|include_usage/.test(message);
|
|
107
|
+
}
|
|
21
108
|
export class OpenAIProvider {
|
|
22
|
-
name
|
|
109
|
+
name;
|
|
23
110
|
client;
|
|
24
111
|
model;
|
|
25
112
|
reasoning;
|
|
26
113
|
effort;
|
|
27
114
|
maxTokensParam;
|
|
28
|
-
|
|
115
|
+
supportsTools;
|
|
116
|
+
/** Set once a server rejects `stream_options`; usage isn't asked for after that. */
|
|
117
|
+
streamUsage = true;
|
|
118
|
+
constructor(config, options = {}) {
|
|
119
|
+
this.name = options.compatible ? 'openai-compatible' : 'openai';
|
|
120
|
+
if (options.compatible) {
|
|
121
|
+
if (!config.baseURL?.trim())
|
|
122
|
+
throw new Error('openai-compatible requires baseURL');
|
|
123
|
+
if (!config.model?.trim())
|
|
124
|
+
throw new Error('openai-compatible requires model');
|
|
125
|
+
}
|
|
126
|
+
else if (!config.apiKey?.trim()) {
|
|
127
|
+
throw new Error('openai requires apiKey');
|
|
128
|
+
}
|
|
29
129
|
this.client = new OpenAI({
|
|
30
|
-
apiKey: config.apiKey,
|
|
130
|
+
apiKey: config.apiKey || 'chatcops-local',
|
|
31
131
|
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
132
|
+
...(config.headers ? { defaultHeaders: config.headers } : {}),
|
|
32
133
|
...(config.organization ? { organization: config.organization } : {}),
|
|
134
|
+
...(config.fetch ? { fetch: config.fetch } : {}),
|
|
33
135
|
});
|
|
34
136
|
this.model = config.model ?? DEFAULT_MODEL;
|
|
35
137
|
this.reasoning = isOpenAIReasoningModel(this.model, config.baseURL);
|
|
36
138
|
this.effort = config.effort ?? DEFAULT_EFFORT;
|
|
37
|
-
this.maxTokensParam = config.maxTokensParam ?? 'max_completion_tokens';
|
|
139
|
+
this.maxTokensParam = config.maxTokensParam ?? (options.compatible ? 'max_tokens' : 'max_completion_tokens');
|
|
140
|
+
this.supportsTools = config.supportsTools ?? true;
|
|
38
141
|
}
|
|
39
142
|
request(params, messages) {
|
|
143
|
+
if (!this.supportsTools && params.tools?.length) {
|
|
144
|
+
warnOnce(`${this.name}-tools-${this.model}`, `${this.name} model ${this.model} has supportsTools: false, so configured tools are not sent.`);
|
|
145
|
+
}
|
|
40
146
|
let temperature = params.temperature;
|
|
41
147
|
if (temperature != null && this.reasoning) {
|
|
42
148
|
warnOnce(`openai-temperature-${this.model}`, `${this.model} doesn't accept \`temperature\`, so it is not sent. Use \`effort\` to trade speed for depth.`);
|
|
@@ -46,124 +152,115 @@ export class OpenAIProvider {
|
|
|
46
152
|
model: this.model,
|
|
47
153
|
messages,
|
|
48
154
|
[this.maxTokensParam]: params.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
49
|
-
...(params.tools?.length ? { tools: toOpenAITools(params.tools) } : {}),
|
|
155
|
+
...(this.supportsTools && params.tools?.length ? { tools: toOpenAITools(params.tools) } : {}),
|
|
50
156
|
...(temperature != null ? { temperature } : {}),
|
|
51
157
|
...(this.reasoning ? { reasoning_effort: this.effort } : {}),
|
|
52
158
|
};
|
|
53
159
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})),
|
|
61
|
-
];
|
|
62
|
-
}
|
|
63
|
-
async *chat(params) {
|
|
64
|
-
const messages = this.toMessages(params);
|
|
65
|
-
let toolRounds = 0;
|
|
66
|
-
while (true) {
|
|
67
|
-
params.signal?.throwIfAborted();
|
|
68
|
-
const stream = await this.client.chat.completions.create({ ...this.request(params, messages), stream: true }, { signal: params.signal });
|
|
69
|
-
let finishReason = null;
|
|
70
|
-
let assistantText = '';
|
|
71
|
-
const toolCalls = new Map();
|
|
72
|
-
for await (const chunk of stream) {
|
|
73
|
-
const choice = chunk.choices[0];
|
|
74
|
-
if (!choice)
|
|
75
|
-
continue;
|
|
76
|
-
finishReason = choice.finish_reason ?? finishReason;
|
|
77
|
-
const deltaText = choice.delta?.content;
|
|
78
|
-
if (deltaText) {
|
|
79
|
-
assistantText += deltaText;
|
|
80
|
-
yield deltaText;
|
|
81
|
-
}
|
|
82
|
-
for (const toolCallDelta of choice.delta?.tool_calls ?? []) {
|
|
83
|
-
if (toolCallDelta.type && toolCallDelta.type !== 'function')
|
|
84
|
-
continue;
|
|
85
|
-
const existing = toolCalls.get(toolCallDelta.index) ?? {
|
|
86
|
-
id: toolCallDelta.id ?? createToolCallId(),
|
|
87
|
-
type: 'function',
|
|
88
|
-
function: { name: '', arguments: '' },
|
|
89
|
-
};
|
|
90
|
-
if (toolCallDelta.id) {
|
|
91
|
-
existing.id = toolCallDelta.id;
|
|
92
|
-
}
|
|
93
|
-
if (toolCallDelta.function?.name) {
|
|
94
|
-
existing.function.name = toolCallDelta.function.name;
|
|
95
|
-
}
|
|
96
|
-
if (toolCallDelta.function?.arguments) {
|
|
97
|
-
existing.function.arguments += toolCallDelta.function.arguments;
|
|
98
|
-
}
|
|
99
|
-
toolCalls.set(toolCallDelta.index, existing);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const completedToolCalls = Array.from(toolCalls.entries())
|
|
103
|
-
.sort(([left], [right]) => left - right)
|
|
104
|
-
.map(([, toolCall]) => toolCall);
|
|
105
|
-
if (finishReason === 'length') {
|
|
106
|
-
console.warn('[chatcops] OpenAI reply cut off at the token limit. Raise `maxTokens` if this keeps happening.');
|
|
107
|
-
}
|
|
108
|
-
if (finishReason !== 'tool_calls' || completedToolCalls.length === 0) {
|
|
109
|
-
return;
|
|
160
|
+
async openStream(params) {
|
|
161
|
+
const system = joinSystem(params.systemPrompt, params.systemContext);
|
|
162
|
+
const body = { ...this.request(params, toOpenAIStepMessages(system, params.messages)), stream: true };
|
|
163
|
+
if (this.streamUsage) {
|
|
164
|
+
try {
|
|
165
|
+
return await this.client.chat.completions.create({ ...body, stream_options: { include_usage: true } }, { signal: params.signal });
|
|
110
166
|
}
|
|
111
|
-
|
|
112
|
-
|
|
167
|
+
catch (error) {
|
|
168
|
+
if (!rejectsStreamOptions(error))
|
|
169
|
+
throw error;
|
|
170
|
+
this.streamUsage = false;
|
|
171
|
+
warnOnce(`openai-stream-options-${this.model}`, "The server doesn't accept `stream_options`, so token usage isn't reported for this provider.");
|
|
113
172
|
}
|
|
114
|
-
toolRounds += 1;
|
|
115
|
-
messages.push({
|
|
116
|
-
role: 'assistant',
|
|
117
|
-
content: assistantText || null,
|
|
118
|
-
tool_calls: completedToolCalls,
|
|
119
|
-
});
|
|
120
|
-
messages.push(...await this.runTools(params, completedToolCalls));
|
|
121
173
|
}
|
|
174
|
+
return this.client.chat.completions.create(body, { signal: params.signal });
|
|
122
175
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
176
|
+
/** One model request: streams text, then reports tool calls, usage and why it stopped. */
|
|
177
|
+
async *step(params) {
|
|
178
|
+
params.signal?.throwIfAborted();
|
|
179
|
+
const stream = await this.openStream(params);
|
|
180
|
+
let reason = null;
|
|
181
|
+
let usage;
|
|
182
|
+
let text = '';
|
|
183
|
+
let refusal = '';
|
|
184
|
+
const calls = new Map();
|
|
185
|
+
for await (const chunk of stream) {
|
|
186
|
+
// With include_usage, the last chunk carries usage and no choices.
|
|
187
|
+
if (chunk.usage)
|
|
188
|
+
usage = chunk.usage;
|
|
189
|
+
const choice = chunk.choices[0];
|
|
190
|
+
if (!choice)
|
|
191
|
+
continue;
|
|
192
|
+
reason = choice.finish_reason ?? reason;
|
|
193
|
+
const delta = choice.delta;
|
|
194
|
+
if (delta?.content) {
|
|
195
|
+
text += delta.content;
|
|
196
|
+
yield { type: 'text-delta', delta: delta.content };
|
|
134
197
|
}
|
|
135
|
-
if (
|
|
136
|
-
|
|
198
|
+
if (delta?.refusal)
|
|
199
|
+
refusal += delta.refusal;
|
|
200
|
+
for (const callDelta of delta?.tool_calls ?? []) {
|
|
201
|
+
if (callDelta.type && callDelta.type !== 'function')
|
|
202
|
+
continue;
|
|
203
|
+
const call = calls.get(callDelta.index) ?? { id: '', synthetic: true, name: '', arguments: '' };
|
|
204
|
+
if (callDelta.id) {
|
|
205
|
+
call.id = callDelta.id;
|
|
206
|
+
call.synthetic = false;
|
|
207
|
+
}
|
|
208
|
+
if (callDelta.function?.name)
|
|
209
|
+
call.name = callDelta.function.name;
|
|
210
|
+
if (callDelta.function?.arguments)
|
|
211
|
+
call.arguments += callDelta.function.arguments;
|
|
212
|
+
calls.set(callDelta.index, call);
|
|
137
213
|
}
|
|
138
|
-
toolRounds += 1;
|
|
139
|
-
messages.push({
|
|
140
|
-
role: 'assistant',
|
|
141
|
-
content: assistantMessage.content,
|
|
142
|
-
tool_calls: toolCalls,
|
|
143
|
-
});
|
|
144
|
-
messages.push(...await this.runTools(params, toolCalls));
|
|
145
214
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
215
|
+
// The SDK ends the stream quietly when it's aborted, so check here, or a
|
|
216
|
+
// cancelled reply would look like a finished empty one.
|
|
217
|
+
params.signal?.throwIfAborted();
|
|
218
|
+
const parts = [];
|
|
219
|
+
if (text)
|
|
220
|
+
parts.push({ type: 'text', text });
|
|
221
|
+
if (refusal)
|
|
222
|
+
parts.push({ type: 'text', text: refusal });
|
|
223
|
+
const ordered = [...calls.entries()].sort(([a], [b]) => a - b).map(([, call]) => call);
|
|
224
|
+
const events = [];
|
|
225
|
+
for (const call of ordered) {
|
|
226
|
+
const id = call.synthetic ? createToolCallId() : call.id;
|
|
227
|
+
let input = {};
|
|
228
|
+
let inputError;
|
|
151
229
|
try {
|
|
152
|
-
|
|
153
|
-
id: toolCall.id,
|
|
154
|
-
name: toolCall.function.name,
|
|
155
|
-
input: parseToolInput(toolCall.function.arguments),
|
|
156
|
-
});
|
|
230
|
+
input = parseToolInput(call.arguments);
|
|
157
231
|
}
|
|
158
232
|
catch (error) {
|
|
159
|
-
|
|
233
|
+
inputError = error instanceof Error ? error.message : 'Tool arguments must be valid JSON.';
|
|
160
234
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
235
|
+
parts.push({
|
|
236
|
+
type: 'tool-call',
|
|
237
|
+
toolCallId: id,
|
|
238
|
+
toolName: call.name,
|
|
239
|
+
// Keep what the model sent when it isn't JSON, so it can see its mistake.
|
|
240
|
+
input: inputError ? call.arguments : input,
|
|
241
|
+
executor: 'server',
|
|
242
|
+
state: inputError ? 'error' : 'pending',
|
|
243
|
+
});
|
|
244
|
+
events.push({ type: 'tool-call', call: { id, name: call.name, input }, ...(inputError ? { inputError } : {}) });
|
|
245
|
+
}
|
|
246
|
+
yield* events;
|
|
247
|
+
yield {
|
|
248
|
+
type: 'finish',
|
|
249
|
+
reason: finishReason(reason, ordered.length > 0, refusal !== ''),
|
|
250
|
+
usage: toUsage(usage),
|
|
251
|
+
parts,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* A turn with tools run through `params.toolExecutor`, as text.
|
|
256
|
+
* @deprecated Use `runTurn()`, which also returns the reply's parts, status and usage.
|
|
257
|
+
*/
|
|
258
|
+
chat(params) {
|
|
259
|
+
return chatViaTurn(this, params);
|
|
260
|
+
}
|
|
261
|
+
/** @deprecated Use `runTurn()`. */
|
|
262
|
+
chatSync(params) {
|
|
263
|
+
return chatSyncViaTurn(this, params);
|
|
167
264
|
}
|
|
168
265
|
}
|
|
169
266
|
//# sourceMappingURL=openai.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC1F,OAAO,EACL,eAAe,EACf,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,aAAa,GACd,MAAM,qBAAqB,CAAC;AAE7B,MAAM,aAAa,GAAG,YAAY,CAAC;AACnC,MAAM,cA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C,SAAS;aACxC,CAAC,CAAC;YACH,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAClH,CAAC;QAED,KAAK,CAAC,CAAC,MAAM,CAAC;QACd,MAAM;YACJ,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,OAAO,KAAK,EAAE,CAAC;YAChE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;YACrB,KAAK;SACN,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,MAA0B;QAC7B,OAAO,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,mCAAmC;IACnC,QAAQ,CAAC,MAA0B;QACjC,OAAO,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ProviderConfig } from '../types.js';
|
|
2
|
+
import type { AIProvider } from './types.js';
|
|
3
|
+
export type ProviderFactory = (config: ProviderConfig) => AIProvider | Promise<AIProvider>;
|
|
4
|
+
/** Register a custom provider. Re-registering replaces its factory for hot reload. */
|
|
5
|
+
export declare function registerProvider(type: string, factory: ProviderFactory): void;
|
|
6
|
+
export declare function getProviderFactory(type: string): ProviderFactory | undefined;
|
|
7
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/providers/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,cAAc,KAAK,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAK3F,sFAAsF;AACtF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI,CAG7E;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAE5E"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const builtins = new Set(['claude', 'openai', 'gemini', 'openai-compatible']);
|
|
2
|
+
const factories = new Map();
|
|
3
|
+
/** Register a custom provider. Re-registering replaces its factory for hot reload. */
|
|
4
|
+
export function registerProvider(type, factory) {
|
|
5
|
+
if (builtins.has(type))
|
|
6
|
+
throw new Error(`Cannot register reserved provider type: ${type}`);
|
|
7
|
+
factories.set(type, factory);
|
|
8
|
+
}
|
|
9
|
+
export function getProviderFactory(type) {
|
|
10
|
+
return factories.get(type);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/providers/registry.ts"],"names":[],"mappings":"AAKA,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,mBAAmB,CAAC,CAAC,CAAC;AAC9E,MAAM,SAAS,GAAG,IAAI,GAAG,EAA2B,CAAC;AAErD,sFAAsF;AACtF,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,OAAwB;IACrE,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,IAAI,EAAE,CAAC,CAAC;IAC3F,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC/B,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,OAAO,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { JSONSchema, ToolDefinition, ToolParameter } from '../types.js';
|
|
2
|
+
/** The 0.4 flat parameter form as a JSON Schema object. */
|
|
3
|
+
export declare function legacyParametersToSchema(parameters: Record<string, ToolParameter>, required?: string[]): JSONSchema;
|
|
4
|
+
/** The JSON Schema sent to providers for this tool's input. */
|
|
5
|
+
export declare function toolInputSchema(tool: ToolDefinition): JSONSchema;
|
|
6
|
+
export declare class ToolDefinitionError extends Error {
|
|
7
|
+
readonly problems: string[];
|
|
8
|
+
constructor(problems: string[]);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Checks tool definitions before any request, so a mistake fails at startup
|
|
12
|
+
* with a clear message instead of as a provider error on the first message.
|
|
13
|
+
* Throws a `ToolDefinitionError` listing every problem.
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateTools(tools: readonly ToolDefinition[]): void;
|
|
16
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/providers/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAQ7E,2DAA2D;AAC3D,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EACzC,QAAQ,GAAE,MAAM,EAAO,GACtB,UAAU,CAUZ;AAED,+DAA+D;AAC/D,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,GAAG,UAAU,CAGhE;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE;gBAAlB,QAAQ,EAAE,MAAM,EAAE;CAIxC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,GAAG,IAAI,CA6CpE"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names every provider accepts: Claude and OpenAI allow letters, digits, `_`
|
|
3
|
+
* and `-` up to 64 characters, and Gemini also wants a letter or `_` first.
|
|
4
|
+
*/
|
|
5
|
+
const TOOL_NAME = /^[a-zA-Z_][a-zA-Z0-9_-]{0,63}$/;
|
|
6
|
+
/** The 0.4 flat parameter form as a JSON Schema object. */
|
|
7
|
+
export function legacyParametersToSchema(parameters, required = []) {
|
|
8
|
+
const properties = {};
|
|
9
|
+
for (const [key, parameter] of Object.entries(parameters)) {
|
|
10
|
+
properties[key] = {
|
|
11
|
+
type: parameter.type,
|
|
12
|
+
description: parameter.description,
|
|
13
|
+
...(parameter.enum ? { enum: parameter.enum } : {}),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return { type: 'object', properties, required };
|
|
17
|
+
}
|
|
18
|
+
/** The JSON Schema sent to providers for this tool's input. */
|
|
19
|
+
export function toolInputSchema(tool) {
|
|
20
|
+
if (tool.inputSchema)
|
|
21
|
+
return tool.inputSchema;
|
|
22
|
+
return legacyParametersToSchema(tool.parameters ?? {}, tool.required ?? []);
|
|
23
|
+
}
|
|
24
|
+
export class ToolDefinitionError extends Error {
|
|
25
|
+
problems;
|
|
26
|
+
constructor(problems) {
|
|
27
|
+
super(`Invalid tool definitions:\n${problems.map((p) => ` - ${p}`).join('\n')}`);
|
|
28
|
+
this.problems = problems;
|
|
29
|
+
this.name = 'ToolDefinitionError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks tool definitions before any request, so a mistake fails at startup
|
|
34
|
+
* with a clear message instead of as a provider error on the first message.
|
|
35
|
+
* Throws a `ToolDefinitionError` listing every problem.
|
|
36
|
+
*/
|
|
37
|
+
export function validateTools(tools) {
|
|
38
|
+
const problems = [];
|
|
39
|
+
const seen = new Set();
|
|
40
|
+
tools.forEach((tool, index) => {
|
|
41
|
+
const label = typeof tool?.name === 'string' && tool.name ? `"${tool.name}"` : `#${index}`;
|
|
42
|
+
if (typeof tool?.name !== 'string' || !TOOL_NAME.test(tool.name)) {
|
|
43
|
+
problems.push(`tool ${label}: the name must start with a letter or _, and use only letters, digits, _ and - (64 at most)`);
|
|
44
|
+
}
|
|
45
|
+
else if (seen.has(tool.name)) {
|
|
46
|
+
problems.push(`tool ${label}: another tool has the same name`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
seen.add(tool.name);
|
|
50
|
+
}
|
|
51
|
+
if (typeof tool?.description !== 'string') {
|
|
52
|
+
problems.push(`tool ${label}: description must be a string`);
|
|
53
|
+
}
|
|
54
|
+
if (tool?.inputSchema && tool.parameters) {
|
|
55
|
+
problems.push(`tool ${label}: set inputSchema or parameters, not both`);
|
|
56
|
+
}
|
|
57
|
+
if (tool?.inputSchema) {
|
|
58
|
+
const schema = tool.inputSchema;
|
|
59
|
+
if (typeof schema !== 'object' || schema === null || Array.isArray(schema) || schema.type !== 'object') {
|
|
60
|
+
problems.push(`tool ${label}: inputSchema must be a JSON Schema with type: 'object' at the root`);
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
JSON.stringify(schema);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
problems.push(`tool ${label}: inputSchema must be plain JSON (no cycles or functions)`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (tool?.parameters) {
|
|
70
|
+
for (const key of tool.required ?? []) {
|
|
71
|
+
if (!(key in tool.parameters)) {
|
|
72
|
+
problems.push(`tool ${label}: required lists "${key}", which isn't in parameters`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
if (problems.length > 0)
|
|
78
|
+
throw new ToolDefinitionError(probl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AAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC;YAChC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACvG,QAAQ,CAAC,IAAI,CAAC,QAAQ,KAAK,qEAAqE,CAAC,CAAC;YACpG,CAAC;YACD,IAAI,CAAC;gBACH,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;YACzB,CAAC;YAAC,MAAM,CAAC;gBACP,QAAQ,CAAC,IAAI,CAAC,QAAQ,KAAK,2DAA2D,CAAC,CAAC;YAC1F,CAAC;QACH,CAAC;aAAM,IAAI,IAAI,EAAE,UAAU,EAAE,CAAC;YAC5B,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;gBACtC,IAAI,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;oBAC9B,QAAQ,CAAC,IAAI,CAAC,QAAQ,KAAK,qBAAqB,GAAG,8BAA8B,CAAC,CAAC;gBACrF,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAAE,MAAM,IAAI,mBAAmB,CAAC,QAAQ,CAAC,CAAC;AACnE,CAAC"}
|
|
@@ -4,5 +4,15 @@ export declare const DEFAULT_TOOL_TIMEOUT_MS = 30000;
|
|
|
4
4
|
export declare function parseToolInput(rawInput: string): Record<string, unknown>;
|
|
5
5
|
export declare function serializeToolResult(result: ToolResult): Record<string, unknown>;
|
|
6
6
|
export declare function toToolFailure(error: unknown): ToolResult;
|
|
7
|
+
/**
|
|
8
|
+
* Runs a tool with a timeout. The tool gets a signal that aborts when the
|
|
9
|
+
* timeout passes or when `signal` aborts (the visitor left), and the result
|
|
10
|
+
* comes back as a failure right away in both cases, so a tool that ignores
|
|
11
|
+
* its signal can't hold up the reply.
|
|
12
|
+
*/
|
|
13
|
+
export declare function runToolWithTimeout(run: (signal: AbortSignal) => Promise<ToolResult>, options?: {
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}): Promise<ToolResult>;
|
|
7
17
|
export declare function executeToolCall(params: ProviderChatParams, call: ProviderToolCall): Promise<ToolResult>;
|
|
8
18
|
//# sourceMappingURL=tool-execution.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-execution.d.ts","sourceRoot":"","sources":["../../src/providers/tool-execution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEpF,eAAO,MAAM,eAAe,IAAI,CAAC;AACjC,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAexE;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAM/E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAKxD;AAED,wBAAsB,eAAe,CACnC,MAAM,EAAE,kBAAkB,EAC1B,IAAI,EAAE,gBAAgB,GACrB,OAAO,CAAC,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"tool-execution.d.ts","sourceRoot":"","sources":["../../src/providers/tool-execution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEpF,eAAO,MAAM,eAAe,IAAI,CAAC;AACjC,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAexE;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAM/E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAKxD;AAOD;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,UAAU,CAAC,EACjD,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,WAAW,CAAA;CAAO,GACzD,OAAO,CAAC,UAAU,CAAC,CAgCrB;AAED,wBAAsB,eAAe,CACnC,MAAM,EAAE,kBAAkB,EAC1B,IAAI,EAAE,gBAAgB,GACrB,OAAO,CAAC,UAAU,CAAC,CAUrB"}
|
|
@@ -28,23 +28,37 @@ export function toToolFailure(error) {
|
|
|
28
28
|
message: error instanceof Error ? error.message : 'Tool execution failed.',
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const timeoutMs =
|
|
31
|
+
/**
|
|
32
|
+
* Runs a tool with a timeout. The tool gets a signal that aborts when the
|
|
33
|
+
* timeout passes or when `signal` aborts (the visitor left), and the result
|
|
34
|
+
* comes back as a failure right away in both cases, so a tool that ignores
|
|
35
|
+
* its signal can't hold up the reply.
|
|
36
|
+
*/
|
|
37
|
+
export async function runToolWithTimeout(run, options = {}) {
|
|
38
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TOOL_TIMEOUT_MS;
|
|
39
39
|
const timers = globalThis;
|
|
40
|
+
const controller = new AbortController();
|
|
40
41
|
let timeoutId;
|
|
42
|
+
let onAbort;
|
|
41
43
|
try {
|
|
42
44
|
return await Promise.race([
|
|
43
|
-
|
|
45
|
+
run(controller.signal),
|
|
44
46
|
new Promise((_, reject) => {
|
|
45
47
|
timeoutId = timers.setTimeout(() => {
|
|
46
|
-
|
|
48
|
+
const error = new Error(`Tool execution timed out after ${timeoutMs}ms.`);
|
|
49
|
+
controller.abort(error);
|
|
50
|
+
reject(error);
|
|
47
51
|
}, timeoutMs);
|
|
52
|
+
if (options.signal) {
|
|
53
|
+
onAbort = () => {
|
|
54
|
+
controller.abort(options.signal?.reason);
|
|
55
|
+
reject(new Error('The reply was cancelled before the tool finished.'));
|
|
56
|
+
};
|
|
57
|
+
if (options.signal.aborted)
|
|
58
|
+
onAbort();
|
|
59
|
+
else
|
|
60
|
+
options.signal.addEventListener('abort', onAbort, { once: true });
|
|
61
|
+
}
|
|
48
62
|
}),
|
|
49
63
|
]);
|
|
50
64
|
}
|
|
@@ -52,9 +66,21 @@ export async function executeToolCall(params, call) {
|
|
|
52
66
|
return toToolFailure(error);
|
|
53
67
|
}
|
|
54
68
|
finally {
|
|
55
|
-
if (timeoutId !== undefined)
|
|
69
|
+
if (timeoutId !== undefined)
|
|
56
70
|
timers.clearTimeout(timeoutId);
|
|
57
|
-
|
|
71
|
+
if (onAbort)
|
|
72
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export async function executeToolCall(params, call) {
|
|
76
|
+
const executor = params.toolExecutor;
|
|
77
|
+
if (!executor) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
message: `No tool executor is configured for "${call.name}".`,
|
|
81
|
+
};
|
|
58
82
|
}
|
|
83
|
+
// The executor gets the call alone, as in 0.4.
|
|
84
|
+
return runToolWithTimeout(() => executor(call), { timeoutMs: params.toolTimeoutMs });
|
|
59
85
|
}
|
|
60
86
|
//# sourceMappingURL=tool-execution.js.map
|