thebird 1.2.35 → 1.2.37

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/index.js CHANGED
@@ -51,7 +51,7 @@ async function* createFullStream({ model, system, messages, tools, onStepFinish,
51
51
  catch (e) { result = { error: true, message: e.message }; }
52
52
  }
53
53
  yield { type: 'tool-result', toolCallId: toolId, toolName: name, args, result };
54
- toolResultParts.push({ functionResponse: { name, response: result || {} } });
54
+ toolResultParts.push({ functionResponse: { name, response: typeof result === 'string' ? { output: result } : (result || {}) } });
55
55
  }
56
56
  yield { type: 'finish-step', finishReason: 'tool-calls' };
57
57
  if (onStepFinish) await onStepFinish();
@@ -91,7 +91,7 @@ async function generateGemini({ model, system, messages, tools, apiKey, temperat
91
91
  try { result = await toolDef.execute(args); }
92
92
  catch (e) { result = { error: true, message: e.message }; }
93
93
  }
94
- toolResultParts.push({ functionResponse: { name, response: result || {} } });
94
+ toolResultParts.push({ functionResponse: { name, response: typeof result === 'string' ? { output: result } : (result || {}) } });
95
95
  }
96
96
  contents.push({ role: 'model', parts: allParts });
97
97
  contents.push({ role: 'user', parts: toolResultParts });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thebird",
3
- "version": "1.2.35",
3
+ "version": "1.2.37",
4
4
  "description": "Anthropic SDK to Gemini streaming bridge — drop-in proxy that translates Anthropic message format and tool calls to Google Gemini",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,3 @@
1
+ import tb from './thebird-browser-entry.js';
2
+ export const streamGemini = tb.streamGemini;
3
+ export const generateGemini = tb.generateGemini;
@@ -0,0 +1,93 @@
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
+ module.exports = { streamGemini, generateGemini };