thebird 1.2.99 → 1.2.101
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/.gm/lastskill +1 -0
- package/docs/index.html +4 -4
- package/index.js +1 -104
- package/package.json +2 -3
- package/server.js +1 -1
- package/examples/basic-chat.js +0 -34
- package/examples/multi-turn.js +0 -45
- package/examples/sdk-validate.js +0 -31
- package/examples/streaming.js +0 -81
- package/examples/tool-use.js +0 -77
- package/examples/vision.js +0 -84
- package/index.d.ts +0 -126
- package/lib/capabilities.js +0 -50
- package/lib/circuit-breaker.js +0 -36
- package/lib/client.js +0 -10
- package/lib/cloud-generate.js +0 -119
- package/lib/config.js +0 -24
- package/lib/convert.js +0 -87
- package/lib/errors.js +0 -140
- package/lib/oauth.js +0 -133
- package/lib/providers/acp.js +0 -88
- package/lib/providers/openai.js +0 -134
- package/lib/router-stream.js +0 -95
- package/lib/router.js +0 -51
- package/lib/stream-guard.js +0 -35
- package/lib/transformers.js +0 -93
- package/thebird-browser-entry-esm.js +0 -4
- package/thebird-browser-entry.js +0 -196
package/thebird-browser-entry.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
const { getClient } = require('./lib/client.js');
|
|
2
|
-
const { GeminiError, withRetry } = require('./lib/errors.js');
|
|
3
|
-
const { convertMessages, convertTools, cleanSchema, extractModelId, buildConfig } = require('./lib/convert.js');
|
|
4
|
-
|
|
5
|
-
function streamGemini({ model, system, messages, tools, onStepFinish, apiKey, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities }) {
|
|
6
|
-
return { fullStream: createFullStream({ model, system, messages, tools, onStepFinish, apiKey, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities }), warnings: Promise.resolve([]) };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
async function* createFullStream({ model, system, messages, tools, onStepFinish, apiKey, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities }) {
|
|
10
|
-
const client = getClient(apiKey);
|
|
11
|
-
const modelId = extractModelId(model);
|
|
12
|
-
let contents = convertMessages(messages);
|
|
13
|
-
const { config } = buildConfig({ system, tools, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities });
|
|
14
|
-
while (true) {
|
|
15
|
-
yield { type: 'start-step' };
|
|
16
|
-
try {
|
|
17
|
-
const stream = await withRetry(() => client.models.generateContentStream({ model: modelId, contents, config }));
|
|
18
|
-
const allParts = [];
|
|
19
|
-
for await (const chunk of stream) {
|
|
20
|
-
for (const candidate of (chunk.candidates || [])) {
|
|
21
|
-
for (const part of (candidate.content?.parts || [])) {
|
|
22
|
-
allParts.push(part);
|
|
23
|
-
if (part.text && !part.thought) yield { type: 'text-delta', textDelta: part.text };
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
const fcParts = allParts.filter(p => p.functionCall);
|
|
28
|
-
if (fcParts.length === 0) {
|
|
29
|
-
yield { type: 'finish-step', finishReason: 'stop' };
|
|
30
|
-
if (onStepFinish) await onStepFinish();
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const toolResultParts = [];
|
|
34
|
-
for (const part of fcParts) {
|
|
35
|
-
const name = part.functionCall.name;
|
|
36
|
-
const args = part.functionCall.args || {};
|
|
37
|
-
const toolId = 'toolu_' + Math.random().toString(36).slice(2, 10);
|
|
38
|
-
yield { type: 'tool-call', toolCallId: toolId, toolName: name, args };
|
|
39
|
-
const toolDef = tools?.[name];
|
|
40
|
-
let result = toolDef ? null : { error: true, message: 'Tool not found: ' + name };
|
|
41
|
-
if (toolDef?.execute) {
|
|
42
|
-
try { result = await toolDef.execute(args, { toolCallId: toolId }); }
|
|
43
|
-
catch (e) { result = { error: true, message: e.message }; }
|
|
44
|
-
}
|
|
45
|
-
yield { type: 'tool-result', toolCallId: toolId, toolName: name, args, result };
|
|
46
|
-
toolResultParts.push({ functionResponse: { name, response: typeof result === 'string' ? { output: result } : (result || {}) } });
|
|
47
|
-
}
|
|
48
|
-
yield { type: 'finish-step', finishReason: 'tool-calls' };
|
|
49
|
-
if (onStepFinish) await onStepFinish();
|
|
50
|
-
contents.push({ role: 'model', parts: allParts });
|
|
51
|
-
contents.push({ role: 'user', parts: toolResultParts });
|
|
52
|
-
} catch (err) {
|
|
53
|
-
yield { type: 'error', error: err };
|
|
54
|
-
yield { type: 'finish-step', finishReason: 'error' };
|
|
55
|
-
if (onStepFinish) await onStepFinish();
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function generateGemini({ model, system, messages, tools, apiKey, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities }) {
|
|
62
|
-
const client = getClient(apiKey);
|
|
63
|
-
const modelId = extractModelId(model);
|
|
64
|
-
let contents = convertMessages(messages);
|
|
65
|
-
const { config } = buildConfig({ system, tools, temperature, maxOutputTokens, topP, topK, safetySettings, responseModalities });
|
|
66
|
-
while (true) {
|
|
67
|
-
const response = await withRetry(() => client.models.generateContent({ model: modelId, contents, config }));
|
|
68
|
-
const candidate = response.candidates?.[0];
|
|
69
|
-
if (!candidate) throw new GeminiError('No candidates returned', { retryable: false });
|
|
70
|
-
const allParts = candidate.content?.parts || [];
|
|
71
|
-
const fcParts = allParts.filter(p => p.functionCall);
|
|
72
|
-
if (fcParts.length === 0) {
|
|
73
|
-
const text = allParts.filter(p => p.text && !p.thought).map(p => p.text).join('');
|
|
74
|
-
return { text, parts: allParts, response };
|
|
75
|
-
}
|
|
76
|
-
const toolResultParts = [];
|
|
77
|
-
for (const part of fcParts) {
|
|
78
|
-
const name = part.functionCall.name;
|
|
79
|
-
const args = part.functionCall.args || {};
|
|
80
|
-
const toolDef = tools?.[name];
|
|
81
|
-
let result = toolDef ? null : { error: true, message: 'Tool not found: ' + name };
|
|
82
|
-
if (toolDef?.execute) {
|
|
83
|
-
try { result = await toolDef.execute(args); }
|
|
84
|
-
catch (e) { result = { error: true, message: e.message }; }
|
|
85
|
-
}
|
|
86
|
-
toolResultParts.push({ functionResponse: { name, response: typeof result === 'string' ? { output: result } : (result || {}) } });
|
|
87
|
-
}
|
|
88
|
-
contents.push({ role: 'model', parts: allParts });
|
|
89
|
-
contents.push({ role: 'user', parts: toolResultParts });
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function convertMessagesOAI(messages, system) {
|
|
94
|
-
const result = [];
|
|
95
|
-
if (system) result.push({ role: 'system', content: typeof system === 'string' ? system : JSON.stringify(system) });
|
|
96
|
-
for (const m of messages) {
|
|
97
|
-
if (typeof m.content === 'string') { result.push({ role: m.role, content: m.content }); continue; }
|
|
98
|
-
if (!Array.isArray(m.content)) continue;
|
|
99
|
-
const toolCalls = m.content.filter(b => b.type === 'tool_use');
|
|
100
|
-
const toolResults = m.content.filter(b => b.type === 'tool_result');
|
|
101
|
-
if (toolResults.length) {
|
|
102
|
-
for (const b of toolResults) {
|
|
103
|
-
const c = typeof b.content === 'string' ? b.content : JSON.stringify(b.content || '');
|
|
104
|
-
result.push({ role: 'tool', tool_call_id: b.tool_use_id || b.id || b.name, content: c });
|
|
105
|
-
}
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const textParts = m.content.filter(b => b.type === 'text').map(b => b.text).join('');
|
|
109
|
-
if (toolCalls.length) {
|
|
110
|
-
result.push({ role: 'assistant', content: textParts || null,
|
|
111
|
-
tool_calls: toolCalls.map(b => ({ id: b.id || ('call_' + Math.random().toString(36).slice(2,8)), type: 'function',
|
|
112
|
-
function: { name: b.name, arguments: JSON.stringify(b.input || {}) } })) });
|
|
113
|
-
} else {
|
|
114
|
-
result.push({ role: m.role, content: textParts });
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return result;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function convertToolsOAI(tools) {
|
|
121
|
-
if (!tools || typeof tools !== 'object') return undefined;
|
|
122
|
-
const list = Object.entries(tools).map(([name, t]) => ({
|
|
123
|
-
type: 'function', function: { name, description: t.description || '',
|
|
124
|
-
parameters: t.parameters?.jsonSchema || t.parameters || { type: 'object' } }
|
|
125
|
-
}));
|
|
126
|
-
return list.length ? list : undefined;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async function* streamOpenAI({ url, apiKey, messages, system, model, tools, maxOutputTokens, temperature, onStepFinish }) {
|
|
130
|
-
const oaiMsgs = convertMessagesOAI(messages, system);
|
|
131
|
-
const oaiTools = convertToolsOAI(tools);
|
|
132
|
-
let body = { messages: oaiMsgs, model, max_tokens: maxOutputTokens || 8192, temperature: temperature ?? 0.5 };
|
|
133
|
-
if (oaiTools) body.tools = oaiTools;
|
|
134
|
-
while (true) {
|
|
135
|
-
yield { type: 'start-step' };
|
|
136
|
-
const res = await fetch(url, { method: 'POST',
|
|
137
|
-
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` },
|
|
138
|
-
body: JSON.stringify({ ...body, stream: true }) });
|
|
139
|
-
if (!res.ok) { const t = await res.text(); throw new Error(t); }
|
|
140
|
-
const reader = res.body.getReader();
|
|
141
|
-
const dec = new TextDecoder();
|
|
142
|
-
let buf = '', toolCallsMap = {};
|
|
143
|
-
try {
|
|
144
|
-
while (true) {
|
|
145
|
-
const { done, value } = await reader.read();
|
|
146
|
-
if (done) break;
|
|
147
|
-
buf += dec.decode(value, { stream: true });
|
|
148
|
-
const lines = buf.split('\n');
|
|
149
|
-
buf = lines.pop();
|
|
150
|
-
for (const line of lines) {
|
|
151
|
-
if (!line.startsWith('data: ')) continue;
|
|
152
|
-
const d = line.slice(6).trim();
|
|
153
|
-
if (d === '[DONE]') break;
|
|
154
|
-
let chunk; try { chunk = JSON.parse(d); } catch { continue; }
|
|
155
|
-
const delta = chunk.choices?.[0]?.delta;
|
|
156
|
-
if (!delta) continue;
|
|
157
|
-
if (delta.content) yield { type: 'text-delta', textDelta: delta.content };
|
|
158
|
-
if (delta.tool_calls) {
|
|
159
|
-
for (const tc of delta.tool_calls) {
|
|
160
|
-
const idx = tc.index ?? 0;
|
|
161
|
-
if (!toolCallsMap[idx]) toolCallsMap[idx] = { id: tc.id || '', name: '', args: '' };
|
|
162
|
-
if (tc.id) toolCallsMap[idx].id = tc.id;
|
|
163
|
-
if (tc.function?.name) toolCallsMap[idx].name += tc.function.name;
|
|
164
|
-
if (tc.function?.arguments) toolCallsMap[idx].args += tc.function.arguments;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
} finally { reader.releaseLock(); }
|
|
170
|
-
const pending = Object.values(toolCallsMap);
|
|
171
|
-
if (!pending.length) {
|
|
172
|
-
yield { type: 'finish-step', finishReason: 'stop' };
|
|
173
|
-
if (onStepFinish) await onStepFinish();
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const toolResultMsgs = [];
|
|
177
|
-
for (const tc of pending) {
|
|
178
|
-
let args; try { args = JSON.parse(tc.args || '{}'); } catch { args = {}; }
|
|
179
|
-
const toolDef = tools?.[tc.name];
|
|
180
|
-
let result = toolDef ? null : { error: true, message: 'Tool not found: ' + tc.name };
|
|
181
|
-
if (toolDef?.execute) try { result = await toolDef.execute(args, { toolCallId: tc.id }); } catch(e) { result = { error: true, message: e.message }; }
|
|
182
|
-
yield { type: 'tool-call', toolCallId: tc.id, toolName: tc.name, args };
|
|
183
|
-
yield { type: 'tool-result', toolCallId: tc.id, toolName: tc.name, args, result };
|
|
184
|
-
toolResultMsgs.push({ role: 'tool', tool_call_id: tc.id, content: JSON.stringify(result ?? '') });
|
|
185
|
-
}
|
|
186
|
-
yield { type: 'finish-step', finishReason: 'tool-calls' };
|
|
187
|
-
if (onStepFinish) await onStepFinish();
|
|
188
|
-
body = { ...body, messages: [...body.messages,
|
|
189
|
-
{ role: 'assistant', content: null, tool_calls: pending.map(tc => ({ id: tc.id, type: 'function', function: { name: tc.name, arguments: tc.args } })) },
|
|
190
|
-
...toolResultMsgs
|
|
191
|
-
]};
|
|
192
|
-
toolCallsMap = {};
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
module.exports = { streamGemini, generateGemini, streamOpenAI };
|