thebird 1.2.37 → 1.2.39
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/docs/acp-stream.js +83 -0
- package/docs/agent-chat.js +16 -4
- package/docs/app.js +75 -19
- package/docs/defaults.json +1 -1
- package/docs/index.html +0 -4
- package/docs/terminal.js +0 -12
- package/docs/vendor/acp-sdk.js +16416 -0
- package/docs/vendor/thebird-browser.js +18562 -13
- package/package.json +1 -1
- package/thebird-browser-entry-esm.js +1 -0
- package/thebird-browser-entry.js +104 -1
package/package.json
CHANGED
package/thebird-browser-entry.js
CHANGED
|
@@ -90,4 +90,107 @@ async function generateGemini({ model, system, messages, tools, apiKey, temperat
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
|
|
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 };
|