@charivo/llm 0.10.7 → 0.11.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/README.md +6 -0
- package/dist/gemini/index.d.mts +35 -0
- package/dist/gemini/index.d.ts +35 -0
- package/dist/gemini/index.js +246 -0
- package/dist/gemini/index.js.map +1 -0
- package/dist/gemini/index.mjs +136 -0
- package/dist/gemini/index.mjs.map +1 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -161,6 +161,12 @@ modalities can reuse.
|
|
|
161
161
|
history, not rotate a pinned gateway session, so a client-side `sessionKey`
|
|
162
162
|
would silently replay the old transcript after a reset. Server routes that
|
|
163
163
|
construct the provider directly can rotate `sessionKey` themselves.
|
|
164
|
+
- `@charivo/llm/gemini`: `createGeminiLLMClient(config)` (browser client,
|
|
165
|
+
dev/testing only) and, for server-side use, `createGeminiLLMProvider(config)`,
|
|
166
|
+
`GeminiLLMProvider`, `type GeminiLLMConfig`. Tool-call history is resent with
|
|
167
|
+
Gemini's documented `skip_thought_signature_validator` placeholder because
|
|
168
|
+
`LLMToolCall` carries no thought signature, so reasoning continuity across
|
|
169
|
+
tool rounds is lost.
|
|
164
170
|
|
|
165
171
|
## Manager API
|
|
166
172
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { LLMProvider, LLMMessage, ToolDefinition, LLMToolResponse, LLMClient } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface GeminiLLMConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
model?: string;
|
|
6
|
+
temperature?: number;
|
|
7
|
+
maxTokens?: number;
|
|
8
|
+
dangerouslyAllowBrowser?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Gemini 3 answers HTTP 400 ("Function call is missing a thought_signature in
|
|
12
|
+
* functionCall parts") when a resent assistant `tool_calls` turn lacks the
|
|
13
|
+
* `thought_signature` Gemini attached to its first call. `LLMToolCall` has no
|
|
14
|
+
* field for the real signature Gemini returned, so this provider sends a
|
|
15
|
+
* documented skip placeholder instead and loses reasoning continuity across
|
|
16
|
+
* tool rounds.
|
|
17
|
+
*/
|
|
18
|
+
declare class GeminiLLMProvider implements LLMProvider {
|
|
19
|
+
private openai;
|
|
20
|
+
private model;
|
|
21
|
+
private temperature?;
|
|
22
|
+
private maxTokens;
|
|
23
|
+
constructor(config: GeminiLLMConfig);
|
|
24
|
+
generateResponse(messages: Array<{
|
|
25
|
+
role: string;
|
|
26
|
+
content: string;
|
|
27
|
+
}>): Promise<string>;
|
|
28
|
+
generateResponseWithTools(messages: LLMMessage[], tools: ToolDefinition[]): Promise<LLMToolResponse>;
|
|
29
|
+
}
|
|
30
|
+
declare function createGeminiLLMProvider(config: GeminiLLMConfig): GeminiLLMProvider;
|
|
31
|
+
|
|
32
|
+
type GeminiLLMClientConfig = GeminiLLMConfig;
|
|
33
|
+
declare function createGeminiLLMClient(config: GeminiLLMClientConfig): LLMClient;
|
|
34
|
+
|
|
35
|
+
export { type GeminiLLMClientConfig, type GeminiLLMConfig, GeminiLLMProvider, createGeminiLLMClient, createGeminiLLMProvider };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { LLMProvider, LLMMessage, ToolDefinition, LLMToolResponse, LLMClient } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface GeminiLLMConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
model?: string;
|
|
6
|
+
temperature?: number;
|
|
7
|
+
maxTokens?: number;
|
|
8
|
+
dangerouslyAllowBrowser?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Gemini 3 answers HTTP 400 ("Function call is missing a thought_signature in
|
|
12
|
+
* functionCall parts") when a resent assistant `tool_calls` turn lacks the
|
|
13
|
+
* `thought_signature` Gemini attached to its first call. `LLMToolCall` has no
|
|
14
|
+
* field for the real signature Gemini returned, so this provider sends a
|
|
15
|
+
* documented skip placeholder instead and loses reasoning continuity across
|
|
16
|
+
* tool rounds.
|
|
17
|
+
*/
|
|
18
|
+
declare class GeminiLLMProvider implements LLMProvider {
|
|
19
|
+
private openai;
|
|
20
|
+
private model;
|
|
21
|
+
private temperature?;
|
|
22
|
+
private maxTokens;
|
|
23
|
+
constructor(config: GeminiLLMConfig);
|
|
24
|
+
generateResponse(messages: Array<{
|
|
25
|
+
role: string;
|
|
26
|
+
content: string;
|
|
27
|
+
}>): Promise<string>;
|
|
28
|
+
generateResponseWithTools(messages: LLMMessage[], tools: ToolDefinition[]): Promise<LLMToolResponse>;
|
|
29
|
+
}
|
|
30
|
+
declare function createGeminiLLMProvider(config: GeminiLLMConfig): GeminiLLMProvider;
|
|
31
|
+
|
|
32
|
+
type GeminiLLMClientConfig = GeminiLLMConfig;
|
|
33
|
+
declare function createGeminiLLMClient(config: GeminiLLMClientConfig): LLMClient;
|
|
34
|
+
|
|
35
|
+
export { type GeminiLLMClientConfig, type GeminiLLMConfig, GeminiLLMProvider, createGeminiLLMClient, createGeminiLLMProvider };
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var OpenAI = require('openai');
|
|
4
|
+
var core = require('@charivo/core');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var OpenAI__default = /*#__PURE__*/_interopDefault(OpenAI);
|
|
9
|
+
|
|
10
|
+
// src/gemini/provider.ts
|
|
11
|
+
function toOpenAIChatMessages(messages) {
|
|
12
|
+
return messages.map((message) => {
|
|
13
|
+
switch (message.role) {
|
|
14
|
+
case "tool":
|
|
15
|
+
return {
|
|
16
|
+
role: "tool",
|
|
17
|
+
tool_call_id: message.toolCallId,
|
|
18
|
+
content: message.content
|
|
19
|
+
};
|
|
20
|
+
case "assistant":
|
|
21
|
+
return {
|
|
22
|
+
role: "assistant",
|
|
23
|
+
content: message.content,
|
|
24
|
+
...message.toolCalls && message.toolCalls.length > 0 ? { tool_calls: message.toolCalls.map(toOpenAIToolCall) } : {}
|
|
25
|
+
};
|
|
26
|
+
case "system":
|
|
27
|
+
case "user":
|
|
28
|
+
return { role: message.role, content: message.content };
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function toOpenAITools(tools) {
|
|
33
|
+
if (tools.length === 0) {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
return tools.map((tool) => ({
|
|
37
|
+
type: "function",
|
|
38
|
+
function: {
|
|
39
|
+
name: tool.name,
|
|
40
|
+
description: tool.description,
|
|
41
|
+
parameters: tool.parameters
|
|
42
|
+
}
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
function toLLMToolResponse(message) {
|
|
46
|
+
const toolCalls = readToolCalls(message);
|
|
47
|
+
return {
|
|
48
|
+
content: message?.content ?? "",
|
|
49
|
+
...toolCalls.length > 0 ? { toolCalls } : {}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function readToolCalls(message) {
|
|
53
|
+
const rawToolCalls = message?.tool_calls;
|
|
54
|
+
if (!rawToolCalls || rawToolCalls.length === 0) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
return rawToolCalls.map(toLLMToolCall);
|
|
58
|
+
}
|
|
59
|
+
function toOpenAIToolCall(toolCall) {
|
|
60
|
+
return {
|
|
61
|
+
id: toolCall.id,
|
|
62
|
+
type: "function",
|
|
63
|
+
function: {
|
|
64
|
+
name: toolCall.name,
|
|
65
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function toLLMToolCall(raw) {
|
|
70
|
+
if (!isPlainObject(raw)) {
|
|
71
|
+
throw new core.CharivoProviderError("LLM tool call must be an object");
|
|
72
|
+
}
|
|
73
|
+
const id = raw.id;
|
|
74
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
75
|
+
throw new core.CharivoProviderError(
|
|
76
|
+
'LLM tool call is missing a non-empty string "id"'
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const name = raw.function?.name;
|
|
80
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
81
|
+
throw new core.CharivoProviderError(
|
|
82
|
+
`LLM tool call "${id}" is missing a non-empty string "function.name"`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
id,
|
|
87
|
+
name,
|
|
88
|
+
arguments: parseToolArguments(name, raw.function.arguments)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function parseToolArguments(name, rawArguments) {
|
|
92
|
+
let parsed;
|
|
93
|
+
try {
|
|
94
|
+
parsed = JSON.parse(rawArguments);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
throw new core.CharivoProviderError(
|
|
97
|
+
`LLM tool call "${name}" has unparsable "function.arguments"`,
|
|
98
|
+
{ cause: error instanceof Error ? error : void 0 }
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
102
|
+
throw new core.CharivoProviderError(
|
|
103
|
+
`LLM tool call "${name}" has "function.arguments" that are not a JSON object`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return parsed;
|
|
107
|
+
}
|
|
108
|
+
function isPlainObject(value) {
|
|
109
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/gemini/provider.ts
|
|
113
|
+
var GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/";
|
|
114
|
+
var DEFAULT_MODEL = "gemini-3.5-flash-lite";
|
|
115
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
116
|
+
var THOUGHT_SIGNATURE_SKIP = "skip_thought_signature_validator";
|
|
117
|
+
var GeminiLLMProvider = class {
|
|
118
|
+
openai;
|
|
119
|
+
model;
|
|
120
|
+
temperature;
|
|
121
|
+
maxTokens;
|
|
122
|
+
constructor(config) {
|
|
123
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
124
|
+
throw new core.CharivoStateError(
|
|
125
|
+
"Gemini LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
this.openai = new OpenAI__default.default({
|
|
129
|
+
apiKey: config.apiKey,
|
|
130
|
+
baseURL: GEMINI_BASE_URL,
|
|
131
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
132
|
+
});
|
|
133
|
+
this.model = config.model || DEFAULT_MODEL;
|
|
134
|
+
this.temperature = config.temperature;
|
|
135
|
+
this.maxTokens = config.maxTokens || 1e3;
|
|
136
|
+
}
|
|
137
|
+
async generateResponse(messages) {
|
|
138
|
+
try {
|
|
139
|
+
const openAIMessages = messages.map((msg) => ({
|
|
140
|
+
role: msg.role,
|
|
141
|
+
content: msg.content
|
|
142
|
+
}));
|
|
143
|
+
const completion = await withTimeout(
|
|
144
|
+
(signal) => this.openai.chat.completions.create(
|
|
145
|
+
{
|
|
146
|
+
model: this.model,
|
|
147
|
+
messages: openAIMessages,
|
|
148
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
149
|
+
max_tokens: this.maxTokens
|
|
150
|
+
},
|
|
151
|
+
{ signal }
|
|
152
|
+
),
|
|
153
|
+
`Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
154
|
+
);
|
|
155
|
+
return completion.choices[0]?.message?.content || "";
|
|
156
|
+
} catch (error) {
|
|
157
|
+
throw core.toCharivoError("provider", error, "Gemini LLM request failed");
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async generateResponseWithTools(messages, tools) {
|
|
161
|
+
try {
|
|
162
|
+
const openAITools = toOpenAITools(tools);
|
|
163
|
+
const completion = await withTimeout(
|
|
164
|
+
(signal) => this.openai.chat.completions.create(
|
|
165
|
+
{
|
|
166
|
+
model: this.model,
|
|
167
|
+
messages: toGeminiChatMessages(messages),
|
|
168
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
169
|
+
max_tokens: this.maxTokens,
|
|
170
|
+
...openAITools ? { tools: openAITools } : {}
|
|
171
|
+
},
|
|
172
|
+
{ signal }
|
|
173
|
+
),
|
|
174
|
+
`Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
175
|
+
);
|
|
176
|
+
return toLLMToolResponse(completion.choices[0]?.message);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
throw core.toCharivoError("provider", error, "Gemini LLM request failed");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
function createGeminiLLMProvider(config) {
|
|
183
|
+
return new GeminiLLMProvider(config);
|
|
184
|
+
}
|
|
185
|
+
function toGeminiChatMessages(messages) {
|
|
186
|
+
return toOpenAIChatMessages(messages).map((message) => {
|
|
187
|
+
if (message.role !== "assistant" || !message.tool_calls?.length) {
|
|
188
|
+
return message;
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
...message,
|
|
192
|
+
tool_calls: message.tool_calls.map(
|
|
193
|
+
(toolCall, index) => index === 0 ? {
|
|
194
|
+
...toolCall,
|
|
195
|
+
extra_content: {
|
|
196
|
+
google: { thought_signature: THOUGHT_SIGNATURE_SKIP }
|
|
197
|
+
}
|
|
198
|
+
} : toolCall
|
|
199
|
+
)
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async function withTimeout(makeRequest, timeoutMessage) {
|
|
204
|
+
const controller = new AbortController();
|
|
205
|
+
let timeoutId;
|
|
206
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
207
|
+
timeoutId = setTimeout(() => {
|
|
208
|
+
controller.abort();
|
|
209
|
+
reject(new core.CharivoTimeoutError(timeoutMessage));
|
|
210
|
+
}, REQUEST_TIMEOUT_MS);
|
|
211
|
+
});
|
|
212
|
+
try {
|
|
213
|
+
return await Promise.race([makeRequest(controller.signal), timeoutPromise]);
|
|
214
|
+
} finally {
|
|
215
|
+
if (timeoutId) {
|
|
216
|
+
clearTimeout(timeoutId);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/gemini/index.ts
|
|
222
|
+
var GeminiLLMClient = class {
|
|
223
|
+
provider;
|
|
224
|
+
constructor(config) {
|
|
225
|
+
this.provider = createGeminiLLMProvider({
|
|
226
|
+
...config,
|
|
227
|
+
dangerouslyAllowBrowser: true
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
async call(messages) {
|
|
231
|
+
const assistantMessage = await this.provider.generateResponse(messages);
|
|
232
|
+
return assistantMessage;
|
|
233
|
+
}
|
|
234
|
+
async callWithTools(messages, tools) {
|
|
235
|
+
return this.provider.generateResponseWithTools(messages, tools);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
function createGeminiLLMClient(config) {
|
|
239
|
+
return new GeminiLLMClient(config);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
exports.GeminiLLMProvider = GeminiLLMProvider;
|
|
243
|
+
exports.createGeminiLLMClient = createGeminiLLMClient;
|
|
244
|
+
exports.createGeminiLLMProvider = createGeminiLLMProvider;
|
|
245
|
+
//# sourceMappingURL=index.js.map
|
|
246
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/openai-tool-format.ts","../../src/gemini/provider.ts","../../src/gemini/index.ts"],"names":["CharivoProviderError","CharivoStateError","OpenAI","toCharivoError","CharivoTimeoutError"],"mappings":";;;;;;;;;;AAcO,SAAS,qBACd,QAAA,EACqC;AACrC,EAAA,OAAO,QAAA,CAAS,GAAA,CAAI,CAAC,OAAA,KAAY;AAC/B,IAAA,QAAQ,QAAQ,IAAA;AAAM,MACpB,KAAK,MAAA;AACH,QAAA,OAAO;AAAA,UACL,IAAA,EAAM,MAAA;AAAA,UACN,cAAc,OAAA,CAAQ,UAAA;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,EAAA,IAAI,SAAA;AAEJ,EAAA,MAAM,cAAA,GAAiB,IAAI,OAAA,CAAW,CAAC,GAAG,MAAA,KAAW;AACnD,IAAA,SAAA,GAAY,WAAW,MAAM;AAC3B,MAAA,UAAA,CAAW,KAAA,EAAM;AACjB,MAAA,MAAA,CAAO,IAAIC,wBAAA,CAAoB,cAAc,CAAC,CAAA;AAAA,IAChD,GAAG,kBAAkB,CAAA;AAAA,EACvB,CAAC,CAAA;AAED,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,QAAQ,IAAA,CAAK,CAAC,YAAY,UAAA,CAAW,MAAM,CAAA,EAAG,cAAc,CAAC,CAAA;AAAA,EAC5E,CAAA,SAAE;AACA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,YAAA,CAAa,SAAS,CAAA;AAAA,IACxB;AAAA,EACF;AACF;;;ACpLA,IAAM,kBAAN,MAA2C;AAAA,EACjC,QAAA;AAAA,EAER,YAAY,MAAA,EAA+B;AAGzC,IAAA,IAAA,CAAK,WAAW,uBAAA,CAAwB;AAAA,MACtC,GAAG,MAAA;AAAA,MACH,uBAAA,EAAyB;AAAA,KAC1B,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KACJ,QAAA,EACiB;AAEjB,IAAA,MAAM,gBAAA,GAAmB,MAAM,IAAA,CAAK,QAAA,CAAS,iBAAiB,QAAQ,CAAA;AAEtE,IAAA,OAAO,gBAAA;AAAA,EACT;AAAA,EAEA,MAAM,aAAA,CACJ,QAAA,EACA,KAAA,EAC0B;AAC1B,IAAA,OAAO,IAAA,CAAK,QAAA,CAAS,yBAAA,CAA0B,QAAA,EAAU,KAAK,CAAA;AAAA,EAChE;AACF,CAAA;AAEO,SAAS,sBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC","file":"index.js","sourcesContent":["/**\n * Mapping between the neutral LLM tool contracts and the OpenAI\n * chat.completions wire format. Shared by the OpenAI provider and the OpenClaw\n * gateway provider, which speaks the same API.\n */\nimport {\n CharivoProviderError,\n type LLMMessage,\n type LLMToolCall,\n type LLMToolResponse,\n type ToolDefinition,\n} from \"@charivo/core\";\nimport type OpenAI from \"openai\";\n\nexport function toOpenAIChatMessages(\n messages: LLMMessage[],\n): OpenAI.ChatCompletionMessageParam[] {\n return messages.map((message) => {\n switch (message.role) {\n case \"tool\":\n return {\n role: \"tool\",\n tool_call_id: message.toolCallId,\n content: message.content,\n };\n case \"assistant\":\n return {\n role: \"assistant\",\n content: message.content,\n ...(message.toolCalls && message.toolCalls.length > 0\n ? { tool_calls: message.toolCalls.map(toOpenAIToolCall) }\n : {}),\n };\n case \"system\":\n case \"user\":\n return { role: message.role, content: message.content };\n }\n });\n}\n\n/**\n * Returns undefined for an empty list so callers can spread the result and\n * leave the `tools` param out entirely: an empty array would still switch the\n * model into tool mode.\n */\nexport function toOpenAITools(\n tools: ToolDefinition[],\n): OpenAI.ChatCompletionTool[] | undefined {\n if (tools.length === 0) {\n return undefined;\n }\n\n return tools.map((tool) => ({\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters,\n },\n }));\n}\n\n/**\n * Builds the neutral tool response from a completion message. Shared by the\n * OpenAI and OpenClaw providers, which both read from the same wire shape.\n */\nexport function toLLMToolResponse(\n message: OpenAI.ChatCompletionMessage | undefined,\n): LLMToolResponse {\n const toolCalls = readToolCalls(message);\n\n return {\n content: message?.content ?? \"\",\n ...(toolCalls.length > 0 ? { toolCalls } : {}),\n };\n}\n\n/**\n * Read the tool calls off a completion message. The SDK types promise more than\n * the wire delivers - especially through a gateway - so every field is checked\n * before it reaches the neutral contract.\n */\nfunction readToolCalls(\n message: OpenAI.ChatCompletionMessage | undefined,\n): LLMToolCall[] {\n const rawToolCalls = message?.tool_calls;\n if (!rawToolCalls || rawToolCalls.length === 0) {\n return [];\n }\n\n return rawToolCalls.map(toLLMToolCall);\n}\n\nfunction toOpenAIToolCall(\n toolCall: LLMToolCall,\n): OpenAI.ChatCompletionMessageToolCall {\n return {\n id: toolCall.id,\n type: \"function\",\n function: {\n name: toolCall.name,\n arguments: JSON.stringify(toolCall.arguments),\n },\n };\n}\n\nfunction toLLMToolCall(raw: OpenAI.ChatCompletionMessageToolCall): LLMToolCall {\n if (!isPlainObject(raw)) {\n throw new CharivoProviderError(\"LLM tool call must be an object\");\n }\n\n const id = raw.id;\n if (typeof id !== \"string\" || id.length === 0) {\n throw new CharivoProviderError(\n 'LLM tool call is missing a non-empty string \"id\"',\n );\n }\n\n const name = raw.function?.name;\n if (typeof name !== \"string\" || name.length === 0) {\n throw new CharivoProviderError(\n `LLM tool call \"${id}\" is missing a non-empty string \"function.name\"`,\n );\n }\n\n return {\n id,\n name,\n arguments: parseToolArguments(name, raw.function.arguments),\n };\n}\n\nfunction parseToolArguments(\n name: string,\n rawArguments: string,\n): Record<string, unknown> {\n let parsed: unknown;\n\n try {\n parsed = JSON.parse(rawArguments);\n } catch (error) {\n throw new CharivoProviderError(\n `LLM tool call \"${name}\" has unparsable \"function.arguments\"`,\n { cause: error instanceof Error ? error : undefined },\n );\n }\n\n if (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n throw new CharivoProviderError(\n `LLM tool call \"${name}\" has \"function.arguments\" that are not a JSON object`,\n );\n }\n\n return parsed as Record<string, unknown>;\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n","import OpenAI from \"openai\";\nimport {\n CharivoStateError,\n CharivoTimeoutError,\n LLMProvider,\n toCharivoError,\n type LLMMessage,\n type LLMToolResponse,\n type ToolDefinition,\n} from \"@charivo/core\";\nimport {\n toLLMToolResponse,\n toOpenAIChatMessages,\n toOpenAITools,\n} from \"../openai-tool-format\";\n\nconst GEMINI_BASE_URL =\n \"https://generativelanguage.googleapis.com/v1beta/openai/\";\nconst DEFAULT_MODEL = \"gemini-3.5-flash-lite\";\nconst REQUEST_TIMEOUT_MS = 30_000;\n\n/** Sent in place of the real thought signature Gemini returned; see `toGeminiChatMessages`. */\nconst THOUGHT_SIGNATURE_SKIP = \"skip_thought_signature_validator\";\n\nexport interface GeminiLLMConfig {\n apiKey: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n dangerouslyAllowBrowser?: boolean;\n}\n\ntype GeminiToolCall = OpenAI.ChatCompletionMessageToolCall & {\n extra_content?: { google: { thought_signature: string } };\n};\n\n/**\n * `toOpenAIChatMessages`'s output widened so an assistant tool-call message can\n * carry Gemini's `extra_content.google.thought_signature` on its first tool\n * call, a field `openai`'s `ChatCompletionMessageToolCall` type does not model.\n */\ntype GeminiChatMessage =\n | Exclude<\n OpenAI.ChatCompletionMessageParam,\n OpenAI.ChatCompletionAssistantMessageParam\n >\n | (Omit<OpenAI.ChatCompletionAssistantMessageParam, \"tool_calls\"> & {\n tool_calls?: GeminiToolCall[];\n });\n\n/**\n * Gemini 3 answers HTTP 400 (\"Function call is missing a thought_signature in\n * functionCall parts\") when a resent assistant `tool_calls` turn lacks the\n * `thought_signature` Gemini attached to its first call. `LLMToolCall` has no\n * field for the real signature Gemini returned, so this provider sends a\n * documented skip placeholder instead and loses reasoning continuity across\n * tool rounds.\n */\nexport class GeminiLLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature?: number;\n private maxTokens: number;\n\n constructor(config: GeminiLLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"Gemini LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.apiKey,\n baseURL: GEMINI_BASE_URL,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || DEFAULT_MODEL;\n this.temperature = config.temperature;\n this.maxTokens = config.maxTokens || 1000;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = messages.map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await withTimeout(\n (signal) =>\n this.openai.chat.completions.create(\n {\n model: this.model,\n messages: openAIMessages,\n ...(this.temperature !== undefined\n ? { temperature: this.temperature }\n : {}),\n max_tokens: this.maxTokens,\n },\n { signal },\n ),\n `Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"Gemini LLM request failed\");\n }\n }\n\n async generateResponseWithTools(\n messages: LLMMessage[],\n tools: ToolDefinition[],\n ): Promise<LLMToolResponse> {\n try {\n const openAITools = toOpenAITools(tools);\n\n const completion = await withTimeout(\n (signal) =>\n this.openai.chat.completions.create(\n {\n model: this.model,\n messages: toGeminiChatMessages(messages),\n ...(this.temperature !== undefined\n ? { temperature: this.temperature }\n : {}),\n max_tokens: this.maxTokens,\n ...(openAITools ? { tools: openAITools } : {}),\n },\n { signal },\n ),\n `Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return toLLMToolResponse(completion.choices[0]?.message);\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"Gemini LLM request failed\");\n }\n }\n}\n\nexport function createGeminiLLMProvider(\n config: GeminiLLMConfig,\n): GeminiLLMProvider {\n return new GeminiLLMProvider(config);\n}\n\n/**\n * Stamps the documented skip placeholder for `thought_signature` onto only\n * `tool_calls[0]` of each assistant tool-call message: Gemini attaches the real\n * signature to the first call of a parallel group, and every other message\n * (including `tool_calls[1..]`) is left as `toOpenAIChatMessages` produced it.\n */\nfunction toGeminiChatMessages(messages: LLMMessage[]): GeminiChatMessage[] {\n return toOpenAIChatMessages(messages).map((message) => {\n // `role: \"tool\"` messages pass through unchanged, with no `name` field: the\n // OpenAI-compatible endpoint resolves the function from `tool_call_id`\n // (verified across sequential and parallel tool rounds). `name` is a\n // requirement of the native `functionResponse` / Interactions surfaces, not this one.\n if (message.role !== \"assistant\" || !message.tool_calls?.length) {\n return message;\n }\n\n return {\n ...message,\n tool_calls: message.tool_calls.map((toolCall, index) =>\n index === 0\n ? {\n ...toolCall,\n extra_content: {\n google: { thought_signature: THOUGHT_SIGNATURE_SKIP },\n },\n }\n : toolCall,\n ),\n };\n });\n}\n\n/**\n * Aborts the underlying SDK request on timeout instead of only abandoning the\n * wrapper promise: the SDK stops waiting and stops retrying, and the aborted\n * request becomes a cancellation request to the server. Work Gemini already\n * accepted before the abort may still be billed.\n */\nasync function withTimeout<T>(\n makeRequest: (signal: AbortSignal) => Promise<T>,\n timeoutMessage: string,\n): Promise<T> {\n const controller = new AbortController();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const timeoutPromise = new Promise<T>((_, reject) => {\n timeoutId = setTimeout(() => {\n controller.abort();\n reject(new CharivoTimeoutError(timeoutMessage));\n }, REQUEST_TIMEOUT_MS);\n });\n\n try {\n return await Promise.race([makeRequest(controller.signal), timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n}\n","import type {\n LLMClient,\n LLMMessage,\n LLMToolResponse,\n ToolDefinition,\n} from \"@charivo/core\";\nimport {\n createGeminiLLMProvider,\n GeminiLLMConfig,\n GeminiLLMProvider,\n} from \"./provider\";\n\nexport {\n createGeminiLLMProvider,\n GeminiLLMProvider,\n type GeminiLLMConfig,\n} from \"./provider\";\n\n// Use GeminiLLMConfig directly\nexport type GeminiLLMClientConfig = GeminiLLMConfig;\n\n/**\n * Gemini LLM Client - Stateless client that wraps the Gemini provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The API key is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass GeminiLLMClient implements LLMClient {\n private provider: GeminiLLMProvider;\n\n constructor(config: GeminiLLMClientConfig) {\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createGeminiLLMProvider({\n ...config,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n // Generate a response using the provider\n const assistantMessage = await this.provider.generateResponse(messages);\n\n return assistantMessage;\n }\n\n async callWithTools(\n messages: LLMMessage[],\n tools: ToolDefinition[],\n ): Promise<LLMToolResponse> {\n return this.provider.generateResponseWithTools(messages, tools);\n }\n}\n\nexport function createGeminiLLMClient(\n config: GeminiLLMClientConfig,\n): LLMClient {\n return new GeminiLLMClient(config);\n}\n"]}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { toOpenAITools, toLLMToolResponse, toOpenAIChatMessages } from '../chunk-N2ILPUIF.mjs';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import { CharivoStateError, toCharivoError, CharivoTimeoutError } from '@charivo/core';
|
|
4
|
+
|
|
5
|
+
var GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/";
|
|
6
|
+
var DEFAULT_MODEL = "gemini-3.5-flash-lite";
|
|
7
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
8
|
+
var THOUGHT_SIGNATURE_SKIP = "skip_thought_signature_validator";
|
|
9
|
+
var GeminiLLMProvider = class {
|
|
10
|
+
openai;
|
|
11
|
+
model;
|
|
12
|
+
temperature;
|
|
13
|
+
maxTokens;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
16
|
+
throw new CharivoStateError(
|
|
17
|
+
"Gemini LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
this.openai = new OpenAI({
|
|
21
|
+
apiKey: config.apiKey,
|
|
22
|
+
baseURL: GEMINI_BASE_URL,
|
|
23
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
24
|
+
});
|
|
25
|
+
this.model = config.model || DEFAULT_MODEL;
|
|
26
|
+
this.temperature = config.temperature;
|
|
27
|
+
this.maxTokens = config.maxTokens || 1e3;
|
|
28
|
+
}
|
|
29
|
+
async generateResponse(messages) {
|
|
30
|
+
try {
|
|
31
|
+
const openAIMessages = messages.map((msg) => ({
|
|
32
|
+
role: msg.role,
|
|
33
|
+
content: msg.content
|
|
34
|
+
}));
|
|
35
|
+
const completion = await withTimeout(
|
|
36
|
+
(signal) => this.openai.chat.completions.create(
|
|
37
|
+
{
|
|
38
|
+
model: this.model,
|
|
39
|
+
messages: openAIMessages,
|
|
40
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
41
|
+
max_tokens: this.maxTokens
|
|
42
|
+
},
|
|
43
|
+
{ signal }
|
|
44
|
+
),
|
|
45
|
+
`Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
46
|
+
);
|
|
47
|
+
return completion.choices[0]?.message?.content || "";
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw toCharivoError("provider", error, "Gemini LLM request failed");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async generateResponseWithTools(messages, tools) {
|
|
53
|
+
try {
|
|
54
|
+
const openAITools = toOpenAITools(tools);
|
|
55
|
+
const completion = await withTimeout(
|
|
56
|
+
(signal) => this.openai.chat.completions.create(
|
|
57
|
+
{
|
|
58
|
+
model: this.model,
|
|
59
|
+
messages: toGeminiChatMessages(messages),
|
|
60
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
61
|
+
max_tokens: this.maxTokens,
|
|
62
|
+
...openAITools ? { tools: openAITools } : {}
|
|
63
|
+
},
|
|
64
|
+
{ signal }
|
|
65
|
+
),
|
|
66
|
+
`Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
67
|
+
);
|
|
68
|
+
return toLLMToolResponse(completion.choices[0]?.message);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
throw toCharivoError("provider", error, "Gemini LLM request failed");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
function createGeminiLLMProvider(config) {
|
|
75
|
+
return new GeminiLLMProvider(config);
|
|
76
|
+
}
|
|
77
|
+
function toGeminiChatMessages(messages) {
|
|
78
|
+
return toOpenAIChatMessages(messages).map((message) => {
|
|
79
|
+
if (message.role !== "assistant" || !message.tool_calls?.length) {
|
|
80
|
+
return message;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
...message,
|
|
84
|
+
tool_calls: message.tool_calls.map(
|
|
85
|
+
(toolCall, index) => index === 0 ? {
|
|
86
|
+
...toolCall,
|
|
87
|
+
extra_content: {
|
|
88
|
+
google: { thought_signature: THOUGHT_SIGNATURE_SKIP }
|
|
89
|
+
}
|
|
90
|
+
} : toolCall
|
|
91
|
+
)
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async function withTimeout(makeRequest, timeoutMessage) {
|
|
96
|
+
const controller = new AbortController();
|
|
97
|
+
let timeoutId;
|
|
98
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
99
|
+
timeoutId = setTimeout(() => {
|
|
100
|
+
controller.abort();
|
|
101
|
+
reject(new CharivoTimeoutError(timeoutMessage));
|
|
102
|
+
}, REQUEST_TIMEOUT_MS);
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
return await Promise.race([makeRequest(controller.signal), timeoutPromise]);
|
|
106
|
+
} finally {
|
|
107
|
+
if (timeoutId) {
|
|
108
|
+
clearTimeout(timeoutId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/gemini/index.ts
|
|
114
|
+
var GeminiLLMClient = class {
|
|
115
|
+
provider;
|
|
116
|
+
constructor(config) {
|
|
117
|
+
this.provider = createGeminiLLMProvider({
|
|
118
|
+
...config,
|
|
119
|
+
dangerouslyAllowBrowser: true
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async call(messages) {
|
|
123
|
+
const assistantMessage = await this.provider.generateResponse(messages);
|
|
124
|
+
return assistantMessage;
|
|
125
|
+
}
|
|
126
|
+
async callWithTools(messages, tools) {
|
|
127
|
+
return this.provider.generateResponseWithTools(messages, tools);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function createGeminiLLMClient(config) {
|
|
131
|
+
return new GeminiLLMClient(config);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { GeminiLLMProvider, createGeminiLLMClient, createGeminiLLMProvider };
|
|
135
|
+
//# sourceMappingURL=index.mjs.map
|
|
136
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/gemini/provider.ts","../../src/gemini/index.ts"],"names":[],"mappings":";;;;AAgBA,IAAM,eAAA,GACJ,0DAAA;AACF,IAAM,aAAA,GAAgB,uBAAA;AACtB,IAAM,kBAAA,GAAqB,GAAA;AAG3B,IAAM,sBAAA,GAAyB,kCAAA;AAoCxB,IAAM,oBAAN,MAA+C;AAAA,EAC5C,MAAA;AAAA,EACA,KAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EAER,YAAY,MAAA,EAAyB;AACnC,IAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,CAAC,OAAO,uBAAA,EAAyB;AACpE,MAAA,MAAM,IAAI,iBAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAEA,IAAA,IAAA,CAAK,MAAA,GAAS,IAAI,MAAA,CAAO;AAAA,MACvB,QAAQ,MAAA,CAAO,MAAA;AAAA,MACf,OAAA,EAAS,eAAA;AAAA,MACT,yBAAyB,MAAA,CAAO;AAAA,KACjC,CAAA;AAED,IAAA,IAAA,CAAK,KAAA,GAAQ,OAAO,KAAA,IAAS,aAAA;AAC7B,IAAA,IAAA,CAAK,cAAc,MAAA,CAAO,WAAA;AAC1B,IAAA,IAAA,CAAK,SAAA,GAAY,OAAO,SAAA,IAAa,GAAA;AAAA,EACvC;AAAA,EAEA,MAAM,iBACJ,QAAA,E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AC,GAAG,MAAA,KAAW;AACnD,IAAA,SAAA,GAAY,WAAW,MAAM;AAC3B,MAAA,UAAA,CAAW,KAAA,EAAM;AACjB,MAAA,MAAA,CAAO,IAAI,mBAAA,CAAoB,cAAc,CAAC,CAAA;AAAA,IAChD,GAAG,kBAAkB,CAAA;AAAA,EACvB,CAAC,CAAA;AAED,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,QAAQ,IAAA,CAAK,CAAC,YAAY,UAAA,CAAW,MAAM,CAAA,EAAG,cAAc,CAAC,CAAA;AAAA,EAC5E,CAAA,SAAE;AACA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,YAAA,CAAa,SAAS,CAAA;AAAA,IACxB;AAAA,EACF;AACF;;;ACpLA,IAAM,kBAAN,MAA2C;AAAA,EACjC,QAAA;AAAA,EAER,YAAY,MAAA,EAA+B;AAGzC,IAAA,IAAA,CAAK,WAAW,uBAAA,CAAwB;AAAA,MACtC,GAAG,MAAA;AAAA,MACH,uBAAA,EAAyB;AAAA,KAC1B,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KACJ,QAAA,EACiB;AAEjB,IAAA,MAAM,gBAAA,GAAmB,MAAM,IAAA,CAAK,QAAA,CAAS,iBAAiB,QAAQ,CAAA;AAEtE,IAAA,OAAO,gBAAA;AAAA,EACT;AAAA,EAEA,MAAM,aAAA,CACJ,QAAA,EACA,KAAA,EAC0B;AAC1B,IAAA,OAAO,IAAA,CAAK,QAAA,CAAS,yBAAA,CAA0B,QAAA,EAAU,KAAK,CAAA;AAAA,EAChE;AACF,CAAA;AAEO,SAAS,sBACd,MAAA,EACW;AACX,EAAA,OAAO,IAAI,gBAAgB,MAAM,CAAA;AACnC","file":"index.mjs","sourcesContent":["import OpenAI from \"openai\";\nimport {\n CharivoStateError,\n CharivoTimeoutError,\n LLMProvider,\n toCharivoError,\n type LLMMessage,\n type LLMToolResponse,\n type ToolDefinition,\n} from \"@charivo/core\";\nimport {\n toLLMToolResponse,\n toOpenAIChatMessages,\n toOpenAITools,\n} from \"../openai-tool-format\";\n\nconst GEMINI_BASE_URL =\n \"https://generativelanguage.googleapis.com/v1beta/openai/\";\nconst DEFAULT_MODEL = \"gemini-3.5-flash-lite\";\nconst REQUEST_TIMEOUT_MS = 30_000;\n\n/** Sent in place of the real thought signature Gemini returned; see `toGeminiChatMessages`. */\nconst THOUGHT_SIGNATURE_SKIP = \"skip_thought_signature_validator\";\n\nexport interface GeminiLLMConfig {\n apiKey: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n dangerouslyAllowBrowser?: boolean;\n}\n\ntype GeminiToolCall = OpenAI.ChatCompletionMessageToolCall & {\n extra_content?: { google: { thought_signature: string } };\n};\n\n/**\n * `toOpenAIChatMessages`'s output widened so an assistant tool-call message can\n * carry Gemini's `extra_content.google.thought_signature` on its first tool\n * call, a field `openai`'s `ChatCompletionMessageToolCall` type does not model.\n */\ntype GeminiChatMessage =\n | Exclude<\n OpenAI.ChatCompletionMessageParam,\n OpenAI.ChatCompletionAssistantMessageParam\n >\n | (Omit<OpenAI.ChatCompletionAssistantMessageParam, \"tool_calls\"> & {\n tool_calls?: GeminiToolCall[];\n });\n\n/**\n * Gemini 3 answers HTTP 400 (\"Function call is missing a thought_signature in\n * functionCall parts\") when a resent assistant `tool_calls` turn lacks the\n * `thought_signature` Gemini attached to its first call. `LLMToolCall` has no\n * field for the real signature Gemini returned, so this provider sends a\n * documented skip placeholder instead and loses reasoning continuity across\n * tool rounds.\n */\nexport class GeminiLLMProvider implements LLMProvider {\n private openai: OpenAI;\n private model: string;\n private temperature?: number;\n private maxTokens: number;\n\n constructor(config: GeminiLLMConfig) {\n if (typeof window !== \"undefined\" && !config.dangerouslyAllowBrowser) {\n throw new CharivoStateError(\n \"Gemini LLM provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing\",\n );\n }\n\n this.openai = new OpenAI({\n apiKey: config.apiKey,\n baseURL: GEMINI_BASE_URL,\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser,\n });\n\n this.model = config.model || DEFAULT_MODEL;\n this.temperature = config.temperature;\n this.maxTokens = config.maxTokens || 1000;\n }\n\n async generateResponse(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n try {\n const openAIMessages = messages.map((msg) => ({\n role: msg.role as \"system\" | \"user\" | \"assistant\",\n content: msg.content,\n }));\n\n const completion = await withTimeout(\n (signal) =>\n this.openai.chat.completions.create(\n {\n model: this.model,\n messages: openAIMessages,\n ...(this.temperature !== undefined\n ? { temperature: this.temperature }\n : {}),\n max_tokens: this.maxTokens,\n },\n { signal },\n ),\n `Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return completion.choices[0]?.message?.content || \"\";\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"Gemini LLM request failed\");\n }\n }\n\n async generateResponseWithTools(\n messages: LLMMessage[],\n tools: ToolDefinition[],\n ): Promise<LLMToolResponse> {\n try {\n const openAITools = toOpenAITools(tools);\n\n const completion = await withTimeout(\n (signal) =>\n this.openai.chat.completions.create(\n {\n model: this.model,\n messages: toGeminiChatMessages(messages),\n ...(this.temperature !== undefined\n ? { temperature: this.temperature }\n : {}),\n max_tokens: this.maxTokens,\n ...(openAITools ? { tools: openAITools } : {}),\n },\n { signal },\n ),\n `Gemini LLM request timed out after ${REQUEST_TIMEOUT_MS}ms`,\n );\n\n return toLLMToolResponse(completion.choices[0]?.message);\n } catch (error) {\n throw toCharivoError(\"provider\", error, \"Gemini LLM request failed\");\n }\n }\n}\n\nexport function createGeminiLLMProvider(\n config: GeminiLLMConfig,\n): GeminiLLMProvider {\n return new GeminiLLMProvider(config);\n}\n\n/**\n * Stamps the documented skip placeholder for `thought_signature` onto only\n * `tool_calls[0]` of each assistant tool-call message: Gemini attaches the real\n * signature to the first call of a parallel group, and every other message\n * (including `tool_calls[1..]`) is left as `toOpenAIChatMessages` produced it.\n */\nfunction toGeminiChatMessages(messages: LLMMessage[]): GeminiChatMessage[] {\n return toOpenAIChatMessages(messages).map((message) => {\n // `role: \"tool\"` messages pass through unchanged, with no `name` field: the\n // OpenAI-compatible endpoint resolves the function from `tool_call_id`\n // (verified across sequential and parallel tool rounds). `name` is a\n // requirement of the native `functionResponse` / Interactions surfaces, not this one.\n if (message.role !== \"assistant\" || !message.tool_calls?.length) {\n return message;\n }\n\n return {\n ...message,\n tool_calls: message.tool_calls.map((toolCall, index) =>\n index === 0\n ? {\n ...toolCall,\n extra_content: {\n google: { thought_signature: THOUGHT_SIGNATURE_SKIP },\n },\n }\n : toolCall,\n ),\n };\n });\n}\n\n/**\n * Aborts the underlying SDK request on timeout instead of only abandoning the\n * wrapper promise: the SDK stops waiting and stops retrying, and the aborted\n * request becomes a cancellation request to the server. Work Gemini already\n * accepted before the abort may still be billed.\n */\nasync function withTimeout<T>(\n makeRequest: (signal: AbortSignal) => Promise<T>,\n timeoutMessage: string,\n): Promise<T> {\n const controller = new AbortController();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const timeoutPromise = new Promise<T>((_, reject) => {\n timeoutId = setTimeout(() => {\n controller.abort();\n reject(new CharivoTimeoutError(timeoutMessage));\n }, REQUEST_TIMEOUT_MS);\n });\n\n try {\n return await Promise.race([makeRequest(controller.signal), timeoutPromise]);\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n}\n","import type {\n LLMClient,\n LLMMessage,\n LLMToolResponse,\n ToolDefinition,\n} from \"@charivo/core\";\nimport {\n createGeminiLLMProvider,\n GeminiLLMConfig,\n GeminiLLMProvider,\n} from \"./provider\";\n\nexport {\n createGeminiLLMProvider,\n GeminiLLMProvider,\n type GeminiLLMConfig,\n} from \"./provider\";\n\n// Use GeminiLLMConfig directly\nexport type GeminiLLMClientConfig = GeminiLLMConfig;\n\n/**\n * Gemini LLM Client - Stateless client that wraps the Gemini provider for direct use on the client\n *\n * For use in local development or test environments. Not recommended for production for security reasons.\n * The API key is exposed to the client, so use it only in a server environment or for testing purposes.\n *\n * Stateless design: session management is handled externally, and this client only handles API calls\n */\nclass GeminiLLMClient implements LLMClient {\n private provider: GeminiLLMProvider;\n\n constructor(config: GeminiLLMClientConfig) {\n // Intentional dev/test escape hatch: this direct browser client exposes\n // credentials. For production, see docs/guide/choosing-packages.md#remote.\n this.provider = createGeminiLLMProvider({\n ...config,\n dangerouslyAllowBrowser: true,\n });\n }\n\n async call(\n messages: Array<{ role: string; content: string }>,\n ): Promise<string> {\n // Generate a response using the provider\n const assistantMessage = await this.provider.generateResponse(messages);\n\n return assistantMessage;\n }\n\n async callWithTools(\n messages: LLMMessage[],\n tools: ToolDefinition[],\n ): Promise<LLMToolResponse> {\n return this.provider.generateResponseWithTools(messages, tools);\n }\n}\n\nexport function createGeminiLLMClient(\n config: GeminiLLMClientConfig,\n): LLMClient {\n return new GeminiLLMClient(config);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charivo/llm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "LLM manager and browser adapters for Charivo",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -26,6 +26,11 @@
|
|
|
26
26
|
"import": "./dist/openclaw/index.mjs",
|
|
27
27
|
"require": "./dist/openclaw/index.js"
|
|
28
28
|
},
|
|
29
|
+
"./gemini": {
|
|
30
|
+
"types": "./dist/gemini/index.d.ts",
|
|
31
|
+
"import": "./dist/gemini/index.mjs",
|
|
32
|
+
"require": "./dist/gemini/index.js"
|
|
33
|
+
},
|
|
29
34
|
"./stub": {
|
|
30
35
|
"types": "./dist/stub/index.d.ts",
|
|
31
36
|
"import": "./dist/stub/index.mjs",
|