ai.libx.js 0.2.8 → 0.2.10
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/build/adapters/anthropic.d.ts +2 -0
- package/build/adapters/anthropic.js +156 -24
- package/build/adapters/anthropic.js.map +1 -1
- package/build/adapters/google.js +59 -86
- package/build/adapters/google.js.map +1 -1
- package/build/index.d.ts +2 -1
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/build/models/ai21.d.ts +2 -0
- package/build/models/ai21.js +31 -0
- package/build/models/ai21.js.map +1 -0
- package/build/models/anthropic.d.ts +2 -0
- package/build/models/anthropic.js +83 -0
- package/build/models/anthropic.js.map +1 -0
- package/build/models/cloudflare.d.ts +2 -0
- package/build/models/cloudflare.js +47 -0
- package/build/models/cloudflare.js.map +1 -0
- package/build/models/cohere.d.ts +2 -0
- package/build/models/cohere.js +54 -0
- package/build/models/cohere.js.map +1 -0
- package/build/models/deepseek.d.ts +2 -0
- package/build/models/deepseek.js +38 -0
- package/build/models/deepseek.js.map +1 -0
- package/build/models/google.d.ts +2 -0
- package/build/models/google.js +208 -0
- package/build/models/google.js.map +1 -0
- package/build/models/groq.d.ts +2 -0
- package/build/models/groq.js +115 -0
- package/build/models/groq.js.map +1 -0
- package/build/models/index.d.ts +24 -0
- package/build/models/index.js +173 -0
- package/build/models/index.js.map +1 -0
- package/build/models/mistral.d.ts +2 -0
- package/build/models/mistral.js +51 -0
- package/build/models/mistral.js.map +1 -0
- package/build/models/openai.d.ts +2 -0
- package/build/models/openai.js +261 -0
- package/build/models/openai.js.map +1 -0
- package/build/models/openrouter.d.ts +2 -0
- package/build/models/openrouter.js +49 -0
- package/build/models/openrouter.js.map +1 -0
- package/build/models/types.d.ts +4 -0
- package/build/models/types.js +20 -0
- package/build/models/types.js.map +1 -0
- package/build/models/xai.d.ts +2 -0
- package/build/models/xai.js +38 -0
- package/build/models/xai.js.map +1 -0
- package/build/models.d.ts +14 -623
- package/build/models.js +41 -259
- package/build/models.js.map +1 -1
- package/build/types/index.d.ts +10 -0
- package/build/types/index.js.map +1 -1
- package/build/utils/model-normalization.d.ts +1 -0
- package/build/utils/model-normalization.js +25 -0
- package/build/utils/model-normalization.js.map +1 -1
- package/package.json +1 -1
- package/src/adapters/anthropic.ts +213 -22
- package/src/adapters/google.ts +48 -107
- package/src/index.ts +7 -0
- package/src/models/ai21.ts +32 -0
- package/src/models/anthropic.ts +90 -0
- package/src/models/cloudflare.ts +51 -0
- package/src/models/cohere.ts +57 -0
- package/src/models/deepseek.ts +39 -0
- package/src/models/google.ts +229 -0
- package/src/models/groq.ts +147 -0
- package/src/models/index.ts +236 -0
- package/src/models/mistral.ts +56 -0
- package/src/models/openai.ts +278 -0
- package/src/models/openrouter.ts +53 -0
- package/src/models/types.ts +46 -0
- package/src/models/xai.ts +43 -0
- package/src/models.ts +35 -328
- package/src/types/index.ts +12 -0
- package/src/utils/model-normalization.ts +41 -0
|
@@ -3,6 +3,8 @@ import { ChatOptions, ChatResponse, StreamChunk } from '../types';
|
|
|
3
3
|
export declare class AnthropicAdapter extends BaseAdapter {
|
|
4
4
|
get name(): string;
|
|
5
5
|
chat(options: ChatOptions): Promise<ChatResponse | AsyncIterable<StreamChunk>>;
|
|
6
|
+
private transformTools;
|
|
7
|
+
private transformToolChoice;
|
|
6
8
|
private transformMessages;
|
|
7
9
|
private handleNonStreamResponse;
|
|
8
10
|
private handleStreamResponse;
|
|
@@ -55,6 +55,12 @@ class AnthropicAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
55
55
|
if (systemMessage) {
|
|
56
56
|
request.system = (0, content_helpers_1.contentToString)(systemMessage.content);
|
|
57
57
|
}
|
|
58
|
+
if (options.tools && options.tools.length > 0) {
|
|
59
|
+
request.tools = this.transformTools(options.tools);
|
|
60
|
+
}
|
|
61
|
+
if (options.toolChoice !== undefined) {
|
|
62
|
+
request.tool_choice = this.transformToolChoice(options.toolChoice);
|
|
63
|
+
}
|
|
58
64
|
if (options.temperature !== undefined)
|
|
59
65
|
request.temperature = options.temperature;
|
|
60
66
|
if (options.topP !== undefined)
|
|
@@ -86,18 +92,104 @@ class AnthropicAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
86
92
|
}
|
|
87
93
|
});
|
|
88
94
|
}
|
|
89
|
-
|
|
90
|
-
return
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
transformTools(tools) {
|
|
96
|
+
return tools.map((tool) => ({
|
|
97
|
+
name: tool.function.name,
|
|
98
|
+
description: tool.function.description,
|
|
99
|
+
input_schema: tool.function.parameters,
|
|
93
100
|
}));
|
|
94
101
|
}
|
|
102
|
+
transformToolChoice(toolChoice) {
|
|
103
|
+
var _a;
|
|
104
|
+
if (toolChoice === 'auto') {
|
|
105
|
+
return { type: 'auto' };
|
|
106
|
+
}
|
|
107
|
+
if (toolChoice === 'none') {
|
|
108
|
+
return { type: 'none' };
|
|
109
|
+
}
|
|
110
|
+
if (toolChoice === 'required') {
|
|
111
|
+
return { type: 'any' };
|
|
112
|
+
}
|
|
113
|
+
if (typeof toolChoice === 'object' && ((_a = toolChoice.function) === null || _a === void 0 ? void 0 : _a.name)) {
|
|
114
|
+
return { type: 'tool', name: toolChoice.function.name };
|
|
115
|
+
}
|
|
116
|
+
return { type: 'auto' };
|
|
117
|
+
}
|
|
118
|
+
transformMessages(messages) {
|
|
119
|
+
const result = [];
|
|
120
|
+
for (const msg of messages) {
|
|
121
|
+
if (msg.role === 'tool') {
|
|
122
|
+
const lastMsg = result[result.length - 1];
|
|
123
|
+
if (lastMsg && lastMsg.role === 'user' && Array.isArray(lastMsg.content)) {
|
|
124
|
+
lastMsg.content.push({
|
|
125
|
+
type: 'tool_result',
|
|
126
|
+
tool_use_id: msg.tool_call_id,
|
|
127
|
+
content: (0, content_helpers_1.contentToString)(msg.content),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
result.push({
|
|
132
|
+
role: 'user',
|
|
133
|
+
content: [{
|
|
134
|
+
type: 'tool_result',
|
|
135
|
+
tool_use_id: msg.tool_call_id,
|
|
136
|
+
content: (0, content_helpers_1.contentToString)(msg.content),
|
|
137
|
+
}],
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (msg.role === 'assistant' && msg.tool_calls && msg.tool_calls.length > 0) {
|
|
142
|
+
const content = [];
|
|
143
|
+
const textContent = (0, content_helpers_1.contentToString)(msg.content);
|
|
144
|
+
if (textContent) {
|
|
145
|
+
content.push({ type: 'text', text: textContent });
|
|
146
|
+
}
|
|
147
|
+
for (const tc of msg.tool_calls) {
|
|
148
|
+
content.push({
|
|
149
|
+
type: 'tool_use',
|
|
150
|
+
id: tc.id,
|
|
151
|
+
name: tc.function.name,
|
|
152
|
+
input: JSON.parse(tc.function.arguments || '{}'),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
result.push({ role: 'assistant', content });
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
result.push({
|
|
159
|
+
role: msg.role === 'user' ? 'user' : 'assistant',
|
|
160
|
+
content: (0, content_helpers_1.contentToString)(msg.content),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
95
166
|
handleNonStreamResponse(data, model) {
|
|
96
|
-
|
|
97
|
-
const
|
|
167
|
+
let textContent = '';
|
|
168
|
+
const toolCalls = [];
|
|
169
|
+
if (Array.isArray(data.content)) {
|
|
170
|
+
for (const block of data.content) {
|
|
171
|
+
if (block.type === 'text') {
|
|
172
|
+
textContent += block.text || '';
|
|
173
|
+
}
|
|
174
|
+
else if (block.type === 'tool_use') {
|
|
175
|
+
toolCalls.push({
|
|
176
|
+
id: block.id,
|
|
177
|
+
type: 'function',
|
|
178
|
+
function: {
|
|
179
|
+
name: block.name,
|
|
180
|
+
arguments: JSON.stringify(block.input || {}),
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else if (typeof data.content === 'string') {
|
|
187
|
+
textContent = data.content;
|
|
188
|
+
}
|
|
98
189
|
return {
|
|
99
|
-
content,
|
|
100
|
-
finishReason: data.stop_reason,
|
|
190
|
+
content: textContent,
|
|
191
|
+
finishReason: data.stop_reason === 'tool_use' ? 'tool_calls' : data.stop_reason,
|
|
192
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
101
193
|
usage: data.usage ? {
|
|
102
194
|
promptTokens: data.usage.input_tokens,
|
|
103
195
|
completionTokens: data.usage.output_tokens,
|
|
@@ -110,14 +202,17 @@ class AnthropicAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
110
202
|
handleStreamResponse(response, model) {
|
|
111
203
|
return __asyncGenerator(this, arguments, function* handleStreamResponse_1() {
|
|
112
204
|
var _a, e_1, _b, _c;
|
|
113
|
-
var _d, _e;
|
|
205
|
+
var _d, _e, _f;
|
|
114
206
|
if (!response.body) {
|
|
115
207
|
throw new Error('No response body for streaming');
|
|
116
208
|
}
|
|
209
|
+
const toolCallsInProgress = new Map();
|
|
210
|
+
let currentBlockIndex = -1;
|
|
211
|
+
let currentBlockType = null;
|
|
117
212
|
try {
|
|
118
|
-
for (var
|
|
119
|
-
_c =
|
|
120
|
-
|
|
213
|
+
for (var _g = true, _h = __asyncValues((0, stream_1.streamLines)(response.body)), _j; _j = yield __await(_h.next()), _a = _j.done, !_a; _g = true) {
|
|
214
|
+
_c = _j.value;
|
|
215
|
+
_g = false;
|
|
121
216
|
const line = _c;
|
|
122
217
|
if (!line.startsWith('data: '))
|
|
123
218
|
continue;
|
|
@@ -126,21 +221,58 @@ class AnthropicAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
126
221
|
break;
|
|
127
222
|
try {
|
|
128
223
|
const chunk = JSON.parse(data);
|
|
129
|
-
if (chunk.type === '
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
224
|
+
if (chunk.type === 'content_block_start') {
|
|
225
|
+
currentBlockIndex = chunk.index;
|
|
226
|
+
const block = chunk.content_block;
|
|
227
|
+
currentBlockType = block === null || block === void 0 ? void 0 : block.type;
|
|
228
|
+
if ((block === null || block === void 0 ? void 0 : block.type) === 'tool_use') {
|
|
229
|
+
toolCallsInProgress.set(currentBlockIndex, {
|
|
230
|
+
id: block.id,
|
|
231
|
+
name: block.name,
|
|
232
|
+
arguments: '',
|
|
135
233
|
});
|
|
136
234
|
}
|
|
137
235
|
}
|
|
236
|
+
else if (chunk.type === 'content_block_delta') {
|
|
237
|
+
if (currentBlockType === 'text') {
|
|
238
|
+
const content = ((_d = chunk.delta) === null || _d === void 0 ? void 0 : _d.text) || '';
|
|
239
|
+
if (content) {
|
|
240
|
+
yield yield __await({ content, index: chunk.index });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else if (currentBlockType === 'tool_use') {
|
|
244
|
+
const tc = toolCallsInProgress.get(currentBlockIndex);
|
|
245
|
+
if (tc && ((_e = chunk.delta) === null || _e === void 0 ? void 0 : _e.partial_json)) {
|
|
246
|
+
tc.arguments += chunk.delta.partial_json;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (chunk.type === 'content_block_stop') {
|
|
251
|
+
currentBlockType = null;
|
|
252
|
+
}
|
|
138
253
|
else if (chunk.type === 'message_delta') {
|
|
139
|
-
if ((
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
254
|
+
if ((_f = chunk.delta) === null || _f === void 0 ? void 0 : _f.stop_reason) {
|
|
255
|
+
if (toolCallsInProgress.size > 0) {
|
|
256
|
+
const toolCalls = Array.from(toolCallsInProgress.values()).map(tc => ({
|
|
257
|
+
id: tc.id,
|
|
258
|
+
type: 'function',
|
|
259
|
+
function: {
|
|
260
|
+
name: tc.name,
|
|
261
|
+
arguments: tc.arguments,
|
|
262
|
+
},
|
|
263
|
+
}));
|
|
264
|
+
yield yield __await({
|
|
265
|
+
content: '',
|
|
266
|
+
finishReason: chunk.delta.stop_reason === 'tool_use' ? 'tool_calls' : chunk.delta.stop_reason,
|
|
267
|
+
toolCalls,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
yield yield __await({
|
|
272
|
+
content: '',
|
|
273
|
+
finishReason: chunk.delta.stop_reason,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
144
276
|
}
|
|
145
277
|
}
|
|
146
278
|
}
|
|
@@ -152,7 +284,7 @@ class AnthropicAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
152
284
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
153
285
|
finally {
|
|
154
286
|
try {
|
|
155
|
-
if (!
|
|
287
|
+
if (!_g && !_a && (_b = _h.return)) yield __await(_b.call(_h));
|
|
156
288
|
}
|
|
157
289
|
finally { if (e_1) throw e_1.error; }
|
|
158
290
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAiD;AAEjD,4CAA8C;AAC9C,4CAAsD;AACtD,8DAA2D;AAsB3D,MAAa,gBAAiB,SAAQ,yBAAW;IAChD,IAAI,IAAI;QACP,OAAO,WAAW,CAAC;IACpB,CAAC;IAEK,IAAI,CAAC,OAAoB;;YAC9B,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;gBACvC,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,8BAA8B,CAAC,CAAC;gBAGhE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;gBAGxD,MAAM,aAAa,GAAG,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE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AE,KAAK,CAAC,KAAK,CAAC,WAAW;iCACrC,CAAA,CAAC;4BACH,CAAC;wBACF,CAAC;oBACF,CAAC;oBAAC,OAAO,CAAC,EAAE,CAAC;wBAEZ,SAAS;oBACV,CAAC;gBACF,CAAC;;;;;;;;;QACF,CAAC;KAAA;CACD;AA7HD,4CA6HC","sourcesContent":["import { BaseAdapter } from './base/BaseAdapter';\nimport { ChatOptions, ChatResponse, StreamChunk, Message } from '../types';\nimport { streamLines } from '../utils/stream';\nimport { handleProviderError } from '../utils/errors';\nimport { contentToString } from '../utils/content-helpers';\n\ninterface AnthropicMessage {\n\trole: 'user' | 'assistant';\n\tcontent: string;\n}\n\ninterface AnthropicRequest {\n\tmodel: string;\n\tmessages: AnthropicMessage[];\n\tmax_tokens: number;\n\ttemperature?: number;\n\ttop_p?: number;\n\ttop_k?: number;\n\tstop_sequences?: string[];\n\tstream?: boolean;\n\tsystem?: string;\n}\n\n/**\n * Anthropic Claude API adapter\n */\nexport class AnthropicAdapter extends BaseAdapter {\n\tget name(): string {\n\t\treturn 'anthropic';\n\t}\n\n\tasync chat(options: ChatOptions): Promise<ChatResponse | AsyncIterable<StreamChunk>> {\n\t\ttry {\n\t\t\tconst apiKey = this.getApiKey(options);\n\t\t\tconst baseUrl = this.getBaseUrl('https://api.anthropic.com/v1');\n\n\t\t\t// Strip provider prefix from model if present\n\t\t\tconst model = options.model.replace(/^anthropic\\//, '');\n\n\t\t\t// Extract system message if present\n\t\t\tconst systemMessage = options.messages.find((m) => m.role === 'system');\n\t\t\tconst nonSystemMessages = options.messages.filter((m) => m.role !== 'system');\n\n\t\t\tconst request: AnthropicRequest = {\n\t\t\t\tmodel,\n\t\t\t\tmessages: this.transformMessages(nonSystemMessages),\n\t\t\t\tmax_tokens: options.maxTokens || 4096,\n\t\t\t\tstream: options.stream || false,\n\t\t\t};\n\n\t\t\t// Add system prompt\n\t\t\tif (systemMessage) {\n\t\t\t\trequest.system = contentToString(systemMessage.content);\n\t\t\t}\n\n\t\t\t// Add optional parameters\n\t\t\tif (options.temperature !== undefined) request.temperature = options.temperature;\n\t\t\tif (options.topP !== undefined) request.top_p = options.topP;\n\t\t\tif (options.topK !== undefined) request.top_k = options.topK;\n\t\t\tif (options.stop && Array.isArray(options.stop)) {\n\t\t\t\trequest.stop_sequences = options.stop;\n\t\t\t}\n\n\t\t\t// Merge provider-specific options\n\t\t\tif (options.providerOptions) {\n\t\t\t\tObject.assign(request, options.providerOptions);\n\t\t\t}\n\n\t\t\tconst response = await this.fetchWithErrorHandling(\n\t\t\t\t`${baseUrl}/messages`,\n\t\t\t\t{\n\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t\t'x-api-key': apiKey,\n\t\t\t\t\t\t'anthropic-version': '2023-06-01',\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify(request),\n\t\t\t\t},\n\t\t\t\tthis.name\n\t\t\t);\n\n\t\t\tif (options.stream) {\n\t\t\t\treturn this.handleStreamResponse(response, model);\n\t\t\t}\n\n\t\t\treturn this.handleNonStreamResponse(await response.json(), model);\n\t\t} catch (error) {\n\t\t\thandleProviderError(error, this.name);\n\t\t}\n\t}\n\n\tprivate transformMessages(messages: Message[]): AnthropicMessage[] {\n\t\treturn messages.map((msg) => ({\n\t\t\trole: msg.role === 'user' ? 'user' : 'assistant',\n\t\t\tcontent: contentToString(msg.content),\n\t\t}));\n\t}\n\n\tprivate handleNonStreamResponse(data: any, model: string): ChatResponse {\n\t\tconst content = data.content?.[0]?.text || '';\n\n\t\treturn {\n\t\t\tcontent,\n\t\t\tfinishReason: data.stop_reason,\n\t\t\tusage: data.usage ? {\n\t\t\t\tpromptTokens: data.usage.input_tokens,\n\t\t\t\tcompletionTokens: data.usage.output_tokens,\n\t\t\t\ttotalTokens: data.usage.input_tokens + data.usage.output_tokens,\n\t\t\t} : undefined,\n\t\t\tmodel,\n\t\t\traw: data,\n\t\t};\n\t}\n\n\tprivate async *handleStreamResponse(response: Response, model: string): AsyncIterable<StreamChunk> {\n\t\tif (!response.body) {\n\t\t\tthrow new Error('No response body for streaming');\n\t\t}\n\n\t\tfor await (const line of streamLines(response.body)) {\n\t\t\tif (!line.startsWith('data: ')) continue;\n\n\t\t\tconst data = line.slice(6).trim();\n\t\t\tif (data === '[DONE]') break;\n\n\t\t\ttry {\n\t\t\t\tconst chunk = JSON.parse(data);\n\n\t\t\t\tif (chunk.type === 'content_block_delta') {\n\t\t\t\t\tconst content = chunk.delta?.text || '';\n\t\t\t\t\tif (content) {\n\t\t\t\t\t\tyield {\n\t\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t\tindex: chunk.index,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (chunk.type === 'message_delta') {\n\t\t\t\t\tif (chunk.delta?.stop_reason) {\n\t\t\t\t\t\tyield {\n\t\t\t\t\t\t\tcontent: '',\n\t\t\t\t\t\t\tfinishReason: chunk.delta.stop_reason,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e) {\n\t\t\t\t// Skip invalid JSON\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t}\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAiD;AAEjD,4CAA8C;AAC9C,4CAAsD;AACtD,8DAA2D;AA+C3D,MAAa,gBAAiB,SAAQ,yBAAW;IAChD,IAAI,IAAI;QACP,OAAO,WAAW,CAAC;IACpB,CAAC;IAEK,IAAI,CAAC,OAAoB;;YAC9B,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;gBACvC,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,8BAA8B,CAAC,CAAC;gBAGhE,MA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AAK,0CAAE,WAAW,EAAE,CAAC;gCAE9B,IAAI,mBAAmB,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;oCAClC,MAAM,SAAS,GAAe,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;wCACjF,EAAE,EAAE,EAAE,CAAC,EAAE;wCACT,IAAI,EAAE,UAAmB;wCACzB,QAAQ,EAAE;4CACT,IAAI,EAAE,EAAE,CAAC,IAAI;4CACb,SAAS,EAAE,EAAE,CAAC,SAAS;yCACvB;qCACD,CAAC,CAAC,CAAC;oCAEJ,oBAAM;wCACL,OAAO,EAAE,EAAE;wCACX,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW;wCAC7F,SAAS;qCACT,CAAA,CAAC;gCACH,CAAC;qCAAM,CAAC;oCACP,oBAAM;wCACL,OAAO,EAAE,EAAE;wCACX,YAAY,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW;qCACrC,CAAA,CAAC;gCACH,CAAC;4BACF,CAAC;wBACF,CAAC;oBACF,CAAC;oBAAC,OAAO,CAAC,EAAE,CAAC;wBAEZ,SAAS;oBACV,CAAC;gBACF,CAAC;;;;;;;;;QACF,CAAC;KAAA;CACD;AApSD,4CAoSC","sourcesContent":["import { BaseAdapter } from './base/BaseAdapter';\nimport { ChatOptions, ChatResponse, StreamChunk, Message, Tool, ToolCall, ToolChoice } from '../types';\nimport { streamLines } from '../utils/stream';\nimport { handleProviderError } from '../utils/errors';\nimport { contentToString } from '../utils/content-helpers';\n\n// Anthropic-specific types\ninterface AnthropicContentBlock {\n\ttype: 'text' | 'tool_use' | 'tool_result';\n\ttext?: string;\n\tid?: string;\n\tname?: string;\n\tinput?: Record<string, unknown>;\n\ttool_use_id?: string;\n\tcontent?: string;\n}\n\ninterface AnthropicMessage {\n\trole: 'user' | 'assistant';\n\tcontent: string | AnthropicContentBlock[];\n}\n\ninterface AnthropicTool {\n\tname: string;\n\tdescription?: string;\n\tinput_schema: object;\n}\n\ninterface AnthropicToolChoice {\n\ttype: 'auto' | 'any' | 'tool' | 'none';\n\tname?: string;\n\tdisable_parallel_tool_use?: boolean;\n}\n\ninterface AnthropicRequest {\n\tmodel: string;\n\tmessages: AnthropicMessage[];\n\tmax_tokens: number;\n\ttemperature?: number;\n\ttop_p?: number;\n\ttop_k?: number;\n\tstop_sequences?: string[];\n\tstream?: boolean;\n\tsystem?: string;\n\ttools?: AnthropicTool[];\n\ttool_choice?: AnthropicToolChoice;\n}\n\n/**\n * Anthropic Claude API adapter with full tool support\n */\nexport class AnthropicAdapter extends BaseAdapter {\n\tget name(): string {\n\t\treturn 'anthropic';\n\t}\n\n\tasync chat(options: ChatOptions): Promise<ChatResponse | AsyncIterable<StreamChunk>> {\n\t\ttry {\n\t\t\tconst apiKey = this.getApiKey(options);\n\t\t\tconst baseUrl = this.getBaseUrl('https://api.anthropic.com/v1');\n\n\t\t\t// Strip provider prefix from model if present\n\t\t\tconst model = options.model.replace(/^anthropic\\//, '');\n\n\t\t\t// Extract system message if present\n\t\t\tconst systemMessage = options.messages.find((m) => m.role === 'system');\n\t\t\tconst nonSystemMessages = options.messages.filter((m) => m.role !== 'system');\n\n\t\t\tconst request: AnthropicRequest = {\n\t\t\t\tmodel,\n\t\t\t\tmessages: this.transformMessages(nonSystemMessages),\n\t\t\t\tmax_tokens: options.maxTokens || 4096,\n\t\t\t\tstream: options.stream || false,\n\t\t\t};\n\n\t\t\t// Add system prompt\n\t\t\tif (systemMessage) {\n\t\t\t\trequest.system = contentToString(systemMessage.content);\n\t\t\t}\n\n\t\t\t// Add tools if provided\n\t\t\tif (options.tools && options.tools.length > 0) {\n\t\t\t\trequest.tools = this.transformTools(options.tools);\n\t\t\t}\n\n\t\t\t// Add tool_choice if provided\n\t\t\tif (options.toolChoice !== undefined) {\n\t\t\t\trequest.tool_choice = this.transformToolChoice(options.toolChoice);\n\t\t\t}\n\n\t\t\t// Add optional parameters\n\t\t\tif (options.temperature !== undefined) request.temperature = options.temperature;\n\t\t\tif (options.topP !== undefined) request.top_p = options.topP;\n\t\t\tif (options.topK !== undefined) request.top_k = options.topK;\n\t\t\tif (options.stop && Array.isArray(options.stop)) {\n\t\t\t\trequest.stop_sequences = options.stop;\n\t\t\t}\n\n\t\t\t// Merge provider-specific options\n\t\t\tif (options.providerOptions) {\n\t\t\t\tObject.assign(request, options.providerOptions);\n\t\t\t}\n\n\t\t\tconst response = await this.fetchWithErrorHandling(\n\t\t\t\t`${baseUrl}/messages`,\n\t\t\t\t{\n\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t\t'x-api-key': apiKey,\n\t\t\t\t\t\t'anthropic-version': '2023-06-01',\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify(request),\n\t\t\t\t},\n\t\t\t\tthis.name\n\t\t\t);\n\n\t\t\tif (options.stream) {\n\t\t\t\treturn this.handleStreamResponse(response, model);\n\t\t\t}\n\n\t\t\treturn this.handleNonStreamResponse(await response.json(), model);\n\t\t} catch (error) {\n\t\t\thandleProviderError(error, this.name);\n\t\t}\n\t}\n\n\t/**\n\t * Transform OpenAI-style tools to Anthropic format\n\t */\n\tprivate transformTools(tools: Tool[]): AnthropicTool[] {\n\t\treturn tools.map((tool) => ({\n\t\t\tname: tool.function.name,\n\t\t\tdescription: tool.function.description,\n\t\t\tinput_schema: tool.function.parameters,\n\t\t}));\n\t}\n\n\t/**\n\t * Transform tool choice from OpenAI format to Anthropic format\n\t */\n\tprivate transformToolChoice(toolChoice: ToolChoice): AnthropicToolChoice {\n\t\tif (toolChoice === 'auto') {\n\t\t\treturn { type: 'auto' };\n\t\t}\n\t\tif (toolChoice === 'none') {\n\t\t\treturn { type: 'none' };\n\t\t}\n\t\tif (toolChoice === 'required') {\n\t\t\treturn { type: 'any' };\n\t\t}\n\t\tif (typeof toolChoice === 'object' && toolChoice.function?.name) {\n\t\t\treturn { type: 'tool', name: toolChoice.function.name };\n\t\t}\n\t\treturn { type: 'auto' };\n\t}\n\n\t/**\n\t * Transform messages to Anthropic format, handling tool calls and results\n\t */\n\tprivate transformMessages(messages: Message[]): AnthropicMessage[] {\n\t\tconst result: AnthropicMessage[] = [];\n\n\t\tfor (const msg of messages) {\n\t\t\tif (msg.role === 'tool') {\n\t\t\t\t// Tool result - needs to be a user message with tool_result content\n\t\t\t\t// Find or create the user message to attach this to\n\t\t\t\tconst lastMsg = result[result.length - 1];\n\t\t\t\tif (lastMsg && lastMsg.role === 'user' && Array.isArray(lastMsg.content)) {\n\t\t\t\t\t// Append to existing user message with tool results\n\t\t\t\t\tlastMsg.content.push({\n\t\t\t\t\t\ttype: 'tool_result',\n\t\t\t\t\t\ttool_use_id: msg.tool_call_id!,\n\t\t\t\t\t\tcontent: contentToString(msg.content),\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// Create new user message with tool result\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: 'user',\n\t\t\t\t\t\tcontent: [{\n\t\t\t\t\t\t\ttype: 'tool_result',\n\t\t\t\t\t\t\ttool_use_id: msg.tool_call_id!,\n\t\t\t\t\t\t\tcontent: contentToString(msg.content),\n\t\t\t\t\t\t}],\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else if (msg.role === 'assistant' && msg.tool_calls && msg.tool_calls.length > 0) {\n\t\t\t\t// Assistant message with tool calls\n\t\t\t\tconst content: AnthropicContentBlock[] = [];\n\n\t\t\t\t// Add text content if present\n\t\t\t\tconst textContent = contentToString(msg.content);\n\t\t\t\tif (textContent) {\n\t\t\t\t\tcontent.push({ type: 'text', text: textContent });\n\t\t\t\t}\n\n\t\t\t\t// Add tool_use blocks\n\t\t\t\tfor (const tc of msg.tool_calls) {\n\t\t\t\t\tcontent.push({\n\t\t\t\t\t\ttype: 'tool_use',\n\t\t\t\t\t\tid: tc.id,\n\t\t\t\t\t\tname: tc.function.name,\n\t\t\t\t\t\tinput: JSON.parse(tc.function.arguments || '{}'),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tresult.push({ role: 'assistant', content });\n\t\t\t} else {\n\t\t\t\t// Regular message\n\t\t\t\tresult.push({\n\t\t\t\t\trole: msg.role === 'user' ? 'user' : 'assistant',\n\t\t\t\t\tcontent: contentToString(msg.content),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Handle non-streaming response, extracting tool calls if present\n\t */\n\tprivate handleNonStreamResponse(data: any, model: string): ChatResponse {\n\t\tlet textContent = '';\n\t\tconst toolCalls: ToolCall[] = [];\n\n\t\t// Parse content array\n\t\tif (Array.isArray(data.content)) {\n\t\t\tfor (const block of data.content) {\n\t\t\t\tif (block.type === 'text') {\n\t\t\t\t\ttextContent += block.text || '';\n\t\t\t\t} else if (block.type === 'tool_use') {\n\t\t\t\t\ttoolCalls.push({\n\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\ttype: 'function',\n\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\targuments: JSON.stringify(block.input || {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (typeof data.content === 'string') {\n\t\t\ttextContent = data.content;\n\t\t}\n\n\t\treturn {\n\t\t\tcontent: textContent,\n\t\t\tfinishReason: data.stop_reason === 'tool_use' ? 'tool_calls' : data.stop_reason,\n\t\t\ttoolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n\t\t\tusage: data.usage ? {\n\t\t\t\tpromptTokens: data.usage.input_tokens,\n\t\t\t\tcompletionTokens: data.usage.output_tokens,\n\t\t\t\ttotalTokens: data.usage.input_tokens + data.usage.output_tokens,\n\t\t\t} : undefined,\n\t\t\tmodel,\n\t\t\traw: data,\n\t\t};\n\t}\n\n\t/**\n\t * Handle streaming response with tool call support\n\t */\n\tprivate async *handleStreamResponse(response: Response, model: string): AsyncIterable<StreamChunk> {\n\t\tif (!response.body) {\n\t\t\tthrow new Error('No response body for streaming');\n\t\t}\n\n\t\t// Track tool calls being built\n\t\tconst toolCallsInProgress: Map<number, { id: string; name: string; arguments: string }> = new Map();\n\t\tlet currentBlockIndex = -1;\n\t\tlet currentBlockType: string | null = null;\n\n\t\tfor await (const line of streamLines(response.body)) {\n\t\t\tif (!line.startsWith('data: ')) continue;\n\n\t\t\tconst data = line.slice(6).trim();\n\t\t\tif (data === '[DONE]') break;\n\n\t\t\ttry {\n\t\t\t\tconst chunk = JSON.parse(data);\n\n\t\t\t\tif (chunk.type === 'content_block_start') {\n\t\t\t\t\tcurrentBlockIndex = chunk.index;\n\t\t\t\t\tconst block = chunk.content_block;\n\t\t\t\t\tcurrentBlockType = block?.type;\n\n\t\t\t\t\tif (block?.type === 'tool_use') {\n\t\t\t\t\t\t// Start of a tool call\n\t\t\t\t\t\ttoolCallsInProgress.set(currentBlockIndex, {\n\t\t\t\t\t\t\tid: block.id,\n\t\t\t\t\t\t\tname: block.name,\n\t\t\t\t\t\t\targuments: '',\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else if (chunk.type === 'content_block_delta') {\n\t\t\t\t\tif (currentBlockType === 'text') {\n\t\t\t\t\t\tconst content = chunk.delta?.text || '';\n\t\t\t\t\t\tif (content) {\n\t\t\t\t\t\t\tyield { content, index: chunk.index };\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (currentBlockType === 'tool_use') {\n\t\t\t\t\t\t// Accumulate tool input JSON\n\t\t\t\t\t\tconst tc = toolCallsInProgress.get(currentBlockIndex);\n\t\t\t\t\t\tif (tc && chunk.delta?.partial_json) {\n\t\t\t\t\t\t\ttc.arguments += chunk.delta.partial_json;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (chunk.type === 'content_block_stop') {\n\t\t\t\t\t// Block finished - nothing special needed\n\t\t\t\t\tcurrentBlockType = null;\n\t\t\t\t} else if (chunk.type === 'message_delta') {\n\t\t\t\t\tif (chunk.delta?.stop_reason) {\n\t\t\t\t\t\t// Emit tool calls if any were accumulated\n\t\t\t\t\t\tif (toolCallsInProgress.size > 0) {\n\t\t\t\t\t\t\tconst toolCalls: ToolCall[] = Array.from(toolCallsInProgress.values()).map(tc => ({\n\t\t\t\t\t\t\t\tid: tc.id,\n\t\t\t\t\t\t\t\ttype: 'function' as const,\n\t\t\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\t\t\tname: tc.name,\n\t\t\t\t\t\t\t\t\targuments: tc.arguments,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}));\n\n\t\t\t\t\t\t\tyield {\n\t\t\t\t\t\t\t\tcontent: '',\n\t\t\t\t\t\t\t\tfinishReason: chunk.delta.stop_reason === 'tool_use' ? 'tool_calls' : chunk.delta.stop_reason,\n\t\t\t\t\t\t\t\ttoolCalls,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tyield {\n\t\t\t\t\t\t\t\tcontent: '',\n\t\t\t\t\t\t\t\tfinishReason: chunk.delta.stop_reason,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e) {\n\t\t\t\t// Skip invalid JSON\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
package/build/adapters/google.js
CHANGED
|
@@ -8,6 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
+
};
|
|
11
18
|
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
12
19
|
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
13
20
|
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
@@ -24,6 +31,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
|
|
|
24
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
32
|
exports.GoogleAdapter = void 0;
|
|
26
33
|
const BaseAdapter_1 = require("./base/BaseAdapter");
|
|
34
|
+
const stream_1 = require("../utils/stream");
|
|
27
35
|
const errors_1 = require("../utils/errors");
|
|
28
36
|
const content_helpers_1 = require("../utils/content-helpers");
|
|
29
37
|
const models_1 = require("../models");
|
|
@@ -103,7 +111,8 @@ class GoogleAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
103
111
|
Object.assign(request, options.providerOptions);
|
|
104
112
|
}
|
|
105
113
|
const endpoint = options.stream ? 'streamGenerateContent' : 'generateContent';
|
|
106
|
-
const
|
|
114
|
+
const streamParam = options.stream ? '&alt=sse' : '';
|
|
115
|
+
const url = `${baseUrl}/${model}:${endpoint}?key=${apiKey}${streamParam}`;
|
|
107
116
|
const response = yield this.fetchWithErrorHandling(url, {
|
|
108
117
|
method: 'POST',
|
|
109
118
|
headers: {
|
|
@@ -331,106 +340,70 @@ class GoogleAdapter extends BaseAdapter_1.BaseAdapter {
|
|
|
331
340
|
}
|
|
332
341
|
handleStreamResponse(response, model) {
|
|
333
342
|
return __asyncGenerator(this, arguments, function* handleStreamResponse_1() {
|
|
334
|
-
var _a, _b;
|
|
343
|
+
var _a, e_1, _b, _c;
|
|
344
|
+
var _d, _e;
|
|
335
345
|
if (!response.body) {
|
|
336
346
|
throw new Error('No response body for streaming');
|
|
337
347
|
}
|
|
338
348
|
const accumulatedToolCalls = [];
|
|
339
|
-
const reader = response.body.getReader();
|
|
340
|
-
const decoder = new TextDecoder();
|
|
341
|
-
let buffer = '';
|
|
342
|
-
let depth = 0;
|
|
343
|
-
let inString = false;
|
|
344
|
-
let escapeNext = false;
|
|
345
|
-
let objectStart = -1;
|
|
346
349
|
try {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
350
|
+
for (var _f = true, _g = __asyncValues((0, stream_1.streamLines)(response.body)), _h; _h = yield __await(_g.next()), _a = _h.done, !_a; _f = true) {
|
|
351
|
+
_c = _h.value;
|
|
352
|
+
_f = false;
|
|
353
|
+
const line = _c;
|
|
354
|
+
const trimmed = line.trim();
|
|
355
|
+
if (!trimmed)
|
|
356
|
+
continue;
|
|
357
|
+
let jsonStr = trimmed;
|
|
358
|
+
if (trimmed.startsWith('data:')) {
|
|
359
|
+
jsonStr = trimmed.slice(5).trim();
|
|
351
360
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
if (char === '"') {
|
|
367
|
-
inString = !inString;
|
|
368
|
-
}
|
|
369
|
-
if (!inString) {
|
|
370
|
-
if (char === '{') {
|
|
371
|
-
if (depth === 0) {
|
|
372
|
-
objectStart = i;
|
|
373
|
-
}
|
|
374
|
-
depth++;
|
|
375
|
-
}
|
|
376
|
-
else if (char === '}') {
|
|
377
|
-
depth--;
|
|
378
|
-
if (depth === 0 && objectStart >= 0) {
|
|
379
|
-
const jsonStr = buffer.substring(objectStart, i + 1);
|
|
380
|
-
try {
|
|
381
|
-
const chunk = JSON.parse(jsonStr);
|
|
382
|
-
const candidate = (_a = chunk.candidates) === null || _a === void 0 ? void 0 : _a[0];
|
|
383
|
-
if (candidate) {
|
|
384
|
-
const content = this.extractContentText(candidate.content);
|
|
385
|
-
const finishReason = candidate.finishReason;
|
|
386
|
-
if ((_b = candidate.content) === null || _b === void 0 ? void 0 : _b.parts) {
|
|
387
|
-
candidate.content.parts.forEach((part) => {
|
|
388
|
-
if (part.functionCall) {
|
|
389
|
-
accumulatedToolCalls.push(part.functionCall);
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
if (content || finishReason) {
|
|
394
|
-
const streamChunk = {
|
|
395
|
-
content,
|
|
396
|
-
finishReason,
|
|
397
|
-
};
|
|
398
|
-
if (finishReason && accumulatedToolCalls.length > 0) {
|
|
399
|
-
streamChunk.toolCalls = accumulatedToolCalls.map((fc, idx) => ({
|
|
400
|
-
id: `call_${Date.now()}_${idx}`,
|
|
401
|
-
type: 'function',
|
|
402
|
-
function: {
|
|
403
|
-
name: fc.name,
|
|
404
|
-
arguments: JSON.stringify(fc.args),
|
|
405
|
-
},
|
|
406
|
-
}));
|
|
407
|
-
}
|
|
408
|
-
yield yield __await(streamChunk);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
catch (e) {
|
|
413
|
-
console.warn('Failed to parse chunk:', jsonStr.substring(0, 100));
|
|
361
|
+
if (!jsonStr || jsonStr === '[' || jsonStr === ']')
|
|
362
|
+
continue;
|
|
363
|
+
jsonStr = jsonStr.replace(/,$/, '');
|
|
364
|
+
try {
|
|
365
|
+
const chunk = JSON.parse(jsonStr);
|
|
366
|
+
const candidate = (_d = chunk.candidates) === null || _d === void 0 ? void 0 : _d[0];
|
|
367
|
+
if (candidate) {
|
|
368
|
+
const content = this.extractContentText(candidate.content);
|
|
369
|
+
const finishReason = candidate.finishReason;
|
|
370
|
+
if ((_e = candidate.content) === null || _e === void 0 ? void 0 : _e.parts) {
|
|
371
|
+
candidate.content.parts.forEach((part) => {
|
|
372
|
+
if (part.functionCall) {
|
|
373
|
+
accumulatedToolCalls.push(part.functionCall);
|
|
414
374
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
if (content || finishReason) {
|
|
378
|
+
const streamChunk = {
|
|
379
|
+
content,
|
|
380
|
+
finishReason,
|
|
381
|
+
};
|
|
382
|
+
if (finishReason && accumulatedToolCalls.length > 0) {
|
|
383
|
+
streamChunk.toolCalls = accumulatedToolCalls.map((fc, idx) => ({
|
|
384
|
+
id: `call_${Date.now()}_${idx}`,
|
|
385
|
+
type: 'function',
|
|
386
|
+
function: {
|
|
387
|
+
name: fc.name,
|
|
388
|
+
arguments: JSON.stringify(fc.args),
|
|
389
|
+
},
|
|
390
|
+
}));
|
|
419
391
|
}
|
|
392
|
+
yield yield __await(streamChunk);
|
|
420
393
|
}
|
|
421
394
|
}
|
|
422
|
-
i++;
|
|
423
395
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
buffer = buffer.replace(/^[\[\],\s]+/, '');
|
|
396
|
+
catch (e) {
|
|
397
|
+
continue;
|
|
429
398
|
}
|
|
430
399
|
}
|
|
431
400
|
}
|
|
401
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
432
402
|
finally {
|
|
433
|
-
|
|
403
|
+
try {
|
|
404
|
+
if (!_f && !_a && (_b = _g.return)) yield __await(_b.call(_g));
|
|
405
|
+
}
|
|
406
|
+
finally { if (e_1) throw e_1.error; }
|
|
434
407
|
}
|
|
435
408
|
});
|
|
436
409
|
}
|