@imenam/unified-llm-client 1.2.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/LICENSE +21 -0
- package/README.md +233 -0
- package/dist/adapters/anthropic/AnthropicLogAdapter.d.ts +6 -0
- package/dist/adapters/anthropic/AnthropicLogAdapter.d.ts.map +1 -0
- package/dist/adapters/anthropic/AnthropicLogAdapter.js +41 -0
- package/dist/adapters/anthropic/AnthropicOutputAdapter.d.ts +6 -0
- package/dist/adapters/anthropic/AnthropicOutputAdapter.d.ts.map +1 -0
- package/dist/adapters/anthropic/AnthropicOutputAdapter.js +41 -0
- package/dist/adapters/anthropic/AnthropicPromptAdapter.d.ts +12 -0
- package/dist/adapters/anthropic/AnthropicPromptAdapter.d.ts.map +1 -0
- package/dist/adapters/anthropic/AnthropicPromptAdapter.js +249 -0
- package/dist/adapters/anthropic/AnthropicToolAdapter.d.ts +6 -0
- package/dist/adapters/anthropic/AnthropicToolAdapter.d.ts.map +1 -0
- package/dist/adapters/anthropic/AnthropicToolAdapter.js +20 -0
- package/dist/adapters/gemini/GeminiAudioAdapter.d.ts +40 -0
- package/dist/adapters/gemini/GeminiAudioAdapter.d.ts.map +1 -0
- package/dist/adapters/gemini/GeminiAudioAdapter.js +273 -0
- package/dist/adapters/gemini/GeminiLogAdapter.d.ts +6 -0
- package/dist/adapters/gemini/GeminiLogAdapter.d.ts.map +1 -0
- package/dist/adapters/gemini/GeminiLogAdapter.js +43 -0
- package/dist/adapters/gemini/GeminiOutputAdapter.d.ts +6 -0
- package/dist/adapters/gemini/GeminiOutputAdapter.d.ts.map +1 -0
- package/dist/adapters/gemini/GeminiOutputAdapter.js +38 -0
- package/dist/adapters/gemini/GeminiPromptAdapter.d.ts +26 -0
- package/dist/adapters/gemini/GeminiPromptAdapter.d.ts.map +1 -0
- package/dist/adapters/gemini/GeminiPromptAdapter.js +334 -0
- package/dist/adapters/gemini/GeminiToolAdapter.d.ts +8 -0
- package/dist/adapters/gemini/GeminiToolAdapter.d.ts.map +1 -0
- package/dist/adapters/gemini/GeminiToolAdapter.js +42 -0
- package/dist/adapters/gemini/types.d.ts +14 -0
- package/dist/adapters/gemini/types.d.ts.map +1 -0
- package/dist/adapters/gemini/types.js +2 -0
- package/dist/adapters/openai/OpenAIOutputAdapter.d.ts +6 -0
- package/dist/adapters/openai/OpenAIOutputAdapter.d.ts.map +1 -0
- package/dist/adapters/openai/OpenAIOutputAdapter.js +18 -0
- package/dist/adapters/openai/OpenAIPromptAdapter.d.ts +13 -0
- package/dist/adapters/openai/OpenAIPromptAdapter.d.ts.map +1 -0
- package/dist/adapters/openai/OpenAIPromptAdapter.js +215 -0
- package/dist/adapters/openai/OpenAiLogAdapter.d.ts +6 -0
- package/dist/adapters/openai/OpenAiLogAdapter.d.ts.map +1 -0
- package/dist/adapters/openai/OpenAiLogAdapter.js +37 -0
- package/dist/adapters/openai/OpenAiToolAdapter.d.ts +6 -0
- package/dist/adapters/openai/OpenAiToolAdapter.d.ts.map +1 -0
- package/dist/adapters/openai/OpenAiToolAdapter.js +20 -0
- package/dist/adapters/qwen/QwenLogAdapter.d.ts +7 -0
- package/dist/adapters/qwen/QwenLogAdapter.d.ts.map +1 -0
- package/dist/adapters/qwen/QwenLogAdapter.js +49 -0
- package/dist/adapters/qwen/QwenOutputAdapter.d.ts +6 -0
- package/dist/adapters/qwen/QwenOutputAdapter.d.ts.map +1 -0
- package/dist/adapters/qwen/QwenOutputAdapter.js +9 -0
- package/dist/adapters/qwen/QwenPromptAdapter.d.ts +13 -0
- package/dist/adapters/qwen/QwenPromptAdapter.d.ts.map +1 -0
- package/dist/adapters/qwen/QwenPromptAdapter.js +313 -0
- package/dist/adapters/qwen/QwenToolAdapter.d.ts +6 -0
- package/dist/adapters/qwen/QwenToolAdapter.d.ts.map +1 -0
- package/dist/adapters/qwen/QwenToolAdapter.js +20 -0
- package/dist/common/types.d.ts +106 -0
- package/dist/common/types.d.ts.map +1 -0
- package/dist/common/types.js +2 -0
- package/dist/core/LLMAdapter.d.ts +74 -0
- package/dist/core/LLMAdapter.d.ts.map +1 -0
- package/dist/core/LLMAdapter.js +70 -0
- package/dist/core/Models.d.ts +16 -0
- package/dist/core/Models.d.ts.map +1 -0
- package/dist/core/Models.js +118 -0
- package/dist/core/UnifiedLLMClient.d.ts +68 -0
- package/dist/core/UnifiedLLMClient.d.ts.map +1 -0
- package/dist/core/UnifiedLLMClient.js +185 -0
- package/dist/examples/anthropic-example.d.ts +2 -0
- package/dist/examples/anthropic-example.d.ts.map +1 -0
- package/dist/examples/anthropic-example.js +243 -0
- package/dist/examples/audio-example.d.ts +14 -0
- package/dist/examples/audio-example.d.ts.map +1 -0
- package/dist/examples/audio-example.js +204 -0
- package/dist/examples/example-logging.d.ts +2 -0
- package/dist/examples/example-logging.d.ts.map +1 -0
- package/dist/examples/example-logging.js +53 -0
- package/dist/examples/example.d.ts +2 -0
- package/dist/examples/example.d.ts.map +1 -0
- package/dist/examples/example.js +373 -0
- package/dist/examples/qwen-vl-example.d.ts +26 -0
- package/dist/examples/qwen-vl-example.d.ts.map +1 -0
- package/dist/examples/qwen-vl-example.js +314 -0
- package/dist/examples/test-multiturn-tools.d.ts +2 -0
- package/dist/examples/test-multiturn-tools.d.ts.map +1 -0
- package/dist/examples/test-multiturn-tools.js +139 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/utils/audioUtils.d.ts +41 -0
- package/dist/utils/audioUtils.d.ts.map +1 -0
- package/dist/utils/audioUtils.js +185 -0
- package/package.json +64 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
45
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
46
|
+
var m = o[Symbol.asyncIterator], i;
|
|
47
|
+
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);
|
|
48
|
+
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); }); }; }
|
|
49
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
exports.GeminiPromptAdapter = void 0;
|
|
53
|
+
const genai_1 = require("@google/genai");
|
|
54
|
+
const mime = __importStar(require("mime-types"));
|
|
55
|
+
class GeminiPromptAdapter {
|
|
56
|
+
constructor(opts) {
|
|
57
|
+
this.opts = opts;
|
|
58
|
+
if (!opts.apiKey) {
|
|
59
|
+
throw new Error("Google API key is missing in options. Please provide it via opts.apiKey.");
|
|
60
|
+
}
|
|
61
|
+
this.genAI = new genai_1.GoogleGenAI({ apiKey: opts.apiKey });
|
|
62
|
+
}
|
|
63
|
+
formatMessage(message) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
var _a, _b, _c;
|
|
66
|
+
if (message.role === "function" || message.parts) {
|
|
67
|
+
return message;
|
|
68
|
+
}
|
|
69
|
+
// Assistant message with tool calls — inject dummy thought_signature on the first
|
|
70
|
+
// functionCall part to satisfy Gemini 3 validation when history is rebuilt manually.
|
|
71
|
+
// Ref: https://ai.google.dev/gemini-api/docs/thought-signatures (FAQ section)
|
|
72
|
+
if (message.role === "assistant" && message.tool_calls && message.tool_calls.length > 0) {
|
|
73
|
+
return {
|
|
74
|
+
role: "model",
|
|
75
|
+
parts: message.tool_calls.map((tc, index) => (Object.assign(Object.assign({}, (index === 0 ? { thoughtSignature: "skip_thought_signature_validator" } : {})), { functionCall: {
|
|
76
|
+
name: tc.name,
|
|
77
|
+
args: tc.arguments,
|
|
78
|
+
} }))),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// Tool result message → function turn with functionResponse parts
|
|
82
|
+
// For Gemini, tool_call_id must contain the function name
|
|
83
|
+
if (message.role === "tool") {
|
|
84
|
+
return {
|
|
85
|
+
role: "function",
|
|
86
|
+
parts: [{
|
|
87
|
+
functionResponse: {
|
|
88
|
+
name: (_a = message.tool_call_id) !== null && _a !== void 0 ? _a : "",
|
|
89
|
+
response: { result: (_b = message.content) !== null && _b !== void 0 ? _b : "" },
|
|
90
|
+
},
|
|
91
|
+
}],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const parts = [];
|
|
95
|
+
if (message.content) {
|
|
96
|
+
parts.push({ text: message.content || "" });
|
|
97
|
+
}
|
|
98
|
+
if (message.attachedFile) {
|
|
99
|
+
const files = Array.isArray(message.attachedFile) ? message.attachedFile : [message.attachedFile];
|
|
100
|
+
for (const file of files) {
|
|
101
|
+
if (typeof file === 'string') {
|
|
102
|
+
const determinedMimeType = mime.lookup(file) || 'application/octet-stream';
|
|
103
|
+
const uploadedFile = yield this.genAI.files.upload({
|
|
104
|
+
file: file,
|
|
105
|
+
config: { mimeType: determinedMimeType },
|
|
106
|
+
});
|
|
107
|
+
if (typeof uploadedFile.uri === 'string') {
|
|
108
|
+
parts.push((0, genai_1.createPartFromUri)(uploadedFile.uri, (_c = uploadedFile.mimeType) !== null && _c !== void 0 ? _c : determinedMimeType));
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
console.warn("Uploaded file URI is not a string, skipping file part:", uploadedFile);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
role: message.role === "assistant" ? "model" : message.role,
|
|
118
|
+
parts: parts,
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
sendToProvider(messages_1) {
|
|
123
|
+
return __awaiter(this, arguments, void 0, function* (messages, tools = [], toolChoice = "auto", attachedFile, model, config) {
|
|
124
|
+
var _a, _b, _c;
|
|
125
|
+
const contents = messages.filter((m) => m !== undefined);
|
|
126
|
+
let sdkTools = undefined;
|
|
127
|
+
if ((tools === null || tools === void 0 ? void 0 : tools.length) > 0) {
|
|
128
|
+
sdkTools = [{
|
|
129
|
+
functionDeclarations: tools === null || tools === void 0 ? void 0 : tools.map(t => ({
|
|
130
|
+
name: t.name,
|
|
131
|
+
description: t.description,
|
|
132
|
+
parameters: t.parameters,
|
|
133
|
+
})),
|
|
134
|
+
}];
|
|
135
|
+
}
|
|
136
|
+
let toolConfig = undefined;
|
|
137
|
+
if (sdkTools && sdkTools.length > 0) {
|
|
138
|
+
if (toolChoice === "none") {
|
|
139
|
+
toolConfig = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.NONE } };
|
|
140
|
+
}
|
|
141
|
+
else if (toolChoice === "any") {
|
|
142
|
+
toolConfig = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.ANY } };
|
|
143
|
+
}
|
|
144
|
+
else if (toolChoice !== "auto" && typeof toolChoice === 'string') {
|
|
145
|
+
toolConfig = {
|
|
146
|
+
functionCallingConfig: {
|
|
147
|
+
mode: genai_1.FunctionCallingConfigMode.ANY,
|
|
148
|
+
allowedFunctionNames: [toolChoice],
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
else if (toolChoice === "auto") {
|
|
153
|
+
toolConfig = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.AUTO } };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const result = yield this.genAI.models.generateContent({
|
|
157
|
+
model: model || this.opts.model,
|
|
158
|
+
contents,
|
|
159
|
+
config: {
|
|
160
|
+
thinkingConfig: {
|
|
161
|
+
thinkingBudget: (_a = config === null || config === void 0 ? void 0 : config.thinkingBudget) !== null && _a !== void 0 ? _a : 128,
|
|
162
|
+
},
|
|
163
|
+
maxOutputTokens: 65535,
|
|
164
|
+
tools: sdkTools,
|
|
165
|
+
toolConfig,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
const sdkResponse = result;
|
|
169
|
+
const responseCandidate = (_b = sdkResponse.candidates) === null || _b === void 0 ? void 0 : _b[0];
|
|
170
|
+
let responseText = null;
|
|
171
|
+
if ((_c = responseCandidate === null || responseCandidate === void 0 ? void 0 : responseCandidate.content) === null || _c === void 0 ? void 0 : _c.parts) {
|
|
172
|
+
for (const part of responseCandidate.content.parts) {
|
|
173
|
+
if (part.text) {
|
|
174
|
+
responseText = (responseText !== null && responseText !== void 0 ? responseText : "") + part.text;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
content: responseText,
|
|
180
|
+
raw: sdkResponse,
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Initiates a streaming chat session with the Gemini model.
|
|
186
|
+
*
|
|
187
|
+
* This method sends the conversation history and any specified tools to the Gemini API
|
|
188
|
+
* and returns an asynchronous iterable that yields chunks of the response as they become available.
|
|
189
|
+
* It also provides a way to retrieve the final aggregated response metadata once the stream completes.
|
|
190
|
+
*
|
|
191
|
+
* @param messages An array of `ChatMessage` objects representing the conversation history.
|
|
192
|
+
* @param tools Optional. An array of `ToolDefinition` objects that the LLM can use.
|
|
193
|
+
* @param toolChoice Optional. Controls how the LLM uses tools: "auto", "none", or a specific tool name.
|
|
194
|
+
* @returns A `StreamingChatResult` object containing the stream of `StreamedChatChunk` and a function `getFinalResponse`
|
|
195
|
+
* to retrieve the final response metadata (excluding content, which is streamed).
|
|
196
|
+
* @throws Error if the Gemini API key is not configured or if the underlying SDK call fails.
|
|
197
|
+
*/
|
|
198
|
+
streamFromProvider(_a) {
|
|
199
|
+
return __awaiter(this, arguments, void 0, function* ({ messages, tools, toolChoice, model, onChunk, onFinalResponse, config, }) {
|
|
200
|
+
var _b, e_1, _c, _d;
|
|
201
|
+
var _e, _f, _g, _h, _j, _k;
|
|
202
|
+
if (!this.genAI) {
|
|
203
|
+
throw new Error("Gemini API not configured");
|
|
204
|
+
}
|
|
205
|
+
const contents = messages.filter((m) => m !== undefined);
|
|
206
|
+
let sdkTools = undefined;
|
|
207
|
+
if (tools && tools.length > 0) {
|
|
208
|
+
sdkTools = [{
|
|
209
|
+
functionDeclarations: tools === null || tools === void 0 ? void 0 : tools.map(t => ({
|
|
210
|
+
name: t.name,
|
|
211
|
+
description: t.description,
|
|
212
|
+
parameters: t.parameters,
|
|
213
|
+
})),
|
|
214
|
+
}];
|
|
215
|
+
}
|
|
216
|
+
let toolConfigValue = undefined;
|
|
217
|
+
if (sdkTools && sdkTools.length > 0) {
|
|
218
|
+
if (toolChoice === "none") {
|
|
219
|
+
toolConfigValue = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.NONE } };
|
|
220
|
+
}
|
|
221
|
+
else if (toolChoice === "any") {
|
|
222
|
+
toolConfigValue = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.ANY } };
|
|
223
|
+
}
|
|
224
|
+
else if (toolChoice !== "auto" && typeof toolChoice === 'string') {
|
|
225
|
+
toolConfigValue = {
|
|
226
|
+
functionCallingConfig: {
|
|
227
|
+
mode: genai_1.FunctionCallingConfigMode.ANY,
|
|
228
|
+
allowedFunctionNames: [toolChoice],
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
else if (toolChoice === "auto") {
|
|
233
|
+
toolConfigValue = { functionCallingConfig: { mode: genai_1.FunctionCallingConfigMode.AUTO } };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const finalResponseData = {
|
|
237
|
+
content: "",
|
|
238
|
+
raw: null,
|
|
239
|
+
toolCalls: [],
|
|
240
|
+
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0, promptTokensDetails: [], thoughtsToken: 0 },
|
|
241
|
+
finishReason: undefined
|
|
242
|
+
};
|
|
243
|
+
const formattedContents = contents;
|
|
244
|
+
const streamResult = yield this.genAI.models.generateContentStream({
|
|
245
|
+
model: model || this.opts.model,
|
|
246
|
+
contents: formattedContents,
|
|
247
|
+
config: {
|
|
248
|
+
tools: sdkTools,
|
|
249
|
+
toolConfig: toolConfigValue,
|
|
250
|
+
thinkingConfig: {
|
|
251
|
+
thinkingBudget: (config === null || config === void 0 ? void 0 : config.thinkingBudget) || 0,
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
let aggregatedContent = "";
|
|
256
|
+
const aggregatedToolCalls = [];
|
|
257
|
+
let lastChunk = null;
|
|
258
|
+
try {
|
|
259
|
+
for (var _l = true, streamResult_1 = __asyncValues(streamResult), streamResult_1_1; streamResult_1_1 = yield streamResult_1.next(), _b = streamResult_1_1.done, !_b; _l = true) {
|
|
260
|
+
_d = streamResult_1_1.value;
|
|
261
|
+
_l = false;
|
|
262
|
+
const chunk = _d;
|
|
263
|
+
lastChunk = chunk; // Keep track of the last chunk
|
|
264
|
+
const candidate = (_e = chunk.candidates) === null || _e === void 0 ? void 0 : _e[0];
|
|
265
|
+
if (!candidate)
|
|
266
|
+
continue;
|
|
267
|
+
let contentChunkText = "";
|
|
268
|
+
if ((_f = candidate.content) === null || _f === void 0 ? void 0 : _f.parts) {
|
|
269
|
+
for (const part of candidate.content.parts) {
|
|
270
|
+
if (part.text) {
|
|
271
|
+
contentChunkText += part.text;
|
|
272
|
+
}
|
|
273
|
+
if (part.functionCall) {
|
|
274
|
+
const fc = part.functionCall;
|
|
275
|
+
const toolCall = {
|
|
276
|
+
id: fc.name || "",
|
|
277
|
+
name: fc.name || "",
|
|
278
|
+
arguments: fc.args || {},
|
|
279
|
+
};
|
|
280
|
+
aggregatedToolCalls.push(toolCall);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (contentChunkText) {
|
|
285
|
+
aggregatedContent += contentChunkText;
|
|
286
|
+
onChunk === null || onChunk === void 0 ? void 0 : onChunk({ contentChunk: contentChunkText, rawChunk: chunk });
|
|
287
|
+
}
|
|
288
|
+
if (((_h = (_g = candidate.content) === null || _g === void 0 ? void 0 : _g.parts) === null || _h === void 0 ? void 0 : _h.some((p) => p.functionCall)) && aggregatedToolCalls.length > 0) {
|
|
289
|
+
}
|
|
290
|
+
if (chunk.usageMetadata) {
|
|
291
|
+
finalResponseData.usage = {
|
|
292
|
+
promptTokens: chunk.usageMetadata.promptTokenCount || 0,
|
|
293
|
+
completionTokens: chunk.usageMetadata.candidatesTokenCount || 0,
|
|
294
|
+
totalTokens: chunk.usageMetadata.totalTokenCount || 0,
|
|
295
|
+
promptTokensDetails: chunk.usageMetadata.promptTokensDetails || [],
|
|
296
|
+
thoughtsToken: chunk.usageMetadata.thoughtsTokenCount || 0,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
if (candidate.finishReason) {
|
|
300
|
+
finalResponseData.finishReason = candidate.finishReason;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
305
|
+
finally {
|
|
306
|
+
try {
|
|
307
|
+
if (!_l && !_b && (_c = streamResult_1.return)) yield _c.call(streamResult_1);
|
|
308
|
+
}
|
|
309
|
+
finally { if (e_1) throw e_1.error; }
|
|
310
|
+
}
|
|
311
|
+
// If streamResult is the generator, there's no separate .response object.
|
|
312
|
+
// We derive final data from the stream's completion and the last chunk seen.
|
|
313
|
+
finalResponseData.raw = lastChunk; // The last chunk might contain some aggregated data or be the closest to a "final response"
|
|
314
|
+
if ((_k = (_j = lastChunk === null || lastChunk === void 0 ? void 0 : lastChunk.candidates) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.finishReason) {
|
|
315
|
+
finalResponseData.finishReason = lastChunk.candidates[0].finishReason;
|
|
316
|
+
}
|
|
317
|
+
if (lastChunk === null || lastChunk === void 0 ? void 0 : lastChunk.usageMetadata) {
|
|
318
|
+
finalResponseData.usage = {
|
|
319
|
+
promptTokens: lastChunk.usageMetadata.promptTokenCount || 0,
|
|
320
|
+
completionTokens: lastChunk.usageMetadata.candidatesTokenCount || 0,
|
|
321
|
+
totalTokens: lastChunk.usageMetadata.totalTokenCount || 0,
|
|
322
|
+
promptTokensDetails: lastChunk.usageMetadata.promptTokensDetails || [],
|
|
323
|
+
thoughtsToken: lastChunk.usageMetadata.thoughtsTokenCount || 0,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
// Tool calls are aggregated during the stream. Ensure they are set.
|
|
327
|
+
finalResponseData.toolCalls = aggregatedToolCalls.length > 0 ? aggregatedToolCalls : undefined;
|
|
328
|
+
onChunk === null || onChunk === void 0 ? void 0 : onChunk({ isFinal: true, contentChunk: "" });
|
|
329
|
+
onFinalResponse === null || onFinalResponse === void 0 ? void 0 : onFinalResponse(finalResponseData);
|
|
330
|
+
return finalResponseData;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
exports.GeminiPromptAdapter = GeminiPromptAdapter;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ToolCall } from "../../common/types";
|
|
2
|
+
import { ToolAdapter } from "../../core/LLMAdapter";
|
|
3
|
+
import { ToolDefinition } from "../../common/types";
|
|
4
|
+
export declare class GeminiToolAdapter implements ToolAdapter {
|
|
5
|
+
formatToolCallResult(toolCall: ToolCall): any;
|
|
6
|
+
formatTool(tool: ToolDefinition): any;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=GeminiToolAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeminiToolAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/gemini/GeminiToolAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,qBAAa,iBAAkB,YAAW,WAAW;IACnD,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,GAAG,GAAG;IA6B7C,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,GAAG;CAOtC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeminiToolAdapter = void 0;
|
|
4
|
+
class GeminiToolAdapter {
|
|
5
|
+
formatToolCallResult(toolCall) {
|
|
6
|
+
let result_content;
|
|
7
|
+
if (typeof toolCall.result === 'string') {
|
|
8
|
+
// Si le résultat est une simple chaîne, on essaie de la parser comme JSON
|
|
9
|
+
// au cas où ce serait une chaîne JSON. Sinon, on l'utilise telle quelle.
|
|
10
|
+
try {
|
|
11
|
+
result_content = JSON.parse(toolCall.result);
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
result_content = toolCall.result;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
result_content = toolCall.result;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
role: "function",
|
|
22
|
+
parts: [
|
|
23
|
+
{
|
|
24
|
+
functionResponse: {
|
|
25
|
+
name: toolCall.name,
|
|
26
|
+
response: {
|
|
27
|
+
output: result_content,
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
formatTool(tool) {
|
|
35
|
+
return {
|
|
36
|
+
name: tool.name,
|
|
37
|
+
description: tool.description,
|
|
38
|
+
parameters: tool.parameters,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.GeminiToolAdapter = GeminiToolAdapter;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface GeminiModalityTokenCount {
|
|
2
|
+
modality: 'TEXT' | 'IMAGE' | 'AUDIO' | 'VIDEO';
|
|
3
|
+
tokenCount: number;
|
|
4
|
+
}
|
|
5
|
+
export interface GeminiUsageMetadata {
|
|
6
|
+
promptTokenCount: number;
|
|
7
|
+
candidatesTokenCount: number;
|
|
8
|
+
totalTokenCount: number;
|
|
9
|
+
promptTokensDetails?: GeminiModalityTokenCount[];
|
|
10
|
+
candidatesTokensDetails?: GeminiModalityTokenCount[];
|
|
11
|
+
cachedContentTokenCount?: number;
|
|
12
|
+
thoughtsTokenCount?: number;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/adapters/gemini/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,mBAAmB;IAClC,gBAAgB,EAAE,MAAM,CAAC;IACzB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,eAAe,EAAE,MAAM,CAAC;IACxB,mBAAmB,CAAC,EAAE,wBAAwB,EAAE,CAAC;IACjD,uBAAuB,CAAC,EAAE,wBAAwB,EAAE,CAAC;IACrD,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChatResponse } from "../../common/types";
|
|
2
|
+
import { OutputAdapter } from "../../core/LLMAdapter";
|
|
3
|
+
export declare class OpenAIOutputAdapter implements OutputAdapter {
|
|
4
|
+
parse(providerResponse: any): ChatResponse;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=OpenAIOutputAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAIOutputAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/OpenAIOutputAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,qBAAa,mBAAoB,YAAW,aAAa;IAChD,KAAK,CAAC,gBAAgB,EAAE,GAAG,GAAG,YAAY;CAclD"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIOutputAdapter = void 0;
|
|
4
|
+
class OpenAIOutputAdapter {
|
|
5
|
+
parse(providerResponse) {
|
|
6
|
+
var _a, _b, _c, _d, _e;
|
|
7
|
+
const usage = providerResponse.raw.usage;
|
|
8
|
+
let output = Object.assign(Object.assign({}, providerResponse), { usage: {
|
|
9
|
+
promptTokens: (_a = usage.promptTokens) !== null && _a !== void 0 ? _a : usage.prompt_tokens,
|
|
10
|
+
completionTokens: (_b = usage.completionTokens) !== null && _b !== void 0 ? _b : usage.completion_tokens,
|
|
11
|
+
totalTokens: (_c = usage.totalTokens) !== null && _c !== void 0 ? _c : usage.total_tokens,
|
|
12
|
+
promptTokensDetails: (_d = usage.promptTokensDetails) !== null && _d !== void 0 ? _d : usage.prompt_tokens_details,
|
|
13
|
+
completionTokensDetails: (_e = usage.completionTokensDetails) !== null && _e !== void 0 ? _e : usage.completion_tokens_details,
|
|
14
|
+
} });
|
|
15
|
+
return output;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.OpenAIOutputAdapter = OpenAIOutputAdapter;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PromptAdapterOptions, PromptAdapter } from "../../core/LLMAdapter";
|
|
2
|
+
import { ChatMessage, ToolDefinition, ChatResponse } from "../../common/types";
|
|
3
|
+
export declare class OpenAIPromptAdapter implements PromptAdapter {
|
|
4
|
+
private readonly opts;
|
|
5
|
+
private readonly endpoint;
|
|
6
|
+
private messages;
|
|
7
|
+
constructor(opts: PromptAdapterOptions);
|
|
8
|
+
formatMessage(message: ChatMessage): any;
|
|
9
|
+
sendToProvider(messages: any, tools?: ToolDefinition[], toolChoice?: "auto" | "none" | string, attachedFile?: string | string[], model?: string): Promise<ChatResponse>;
|
|
10
|
+
private processFiles;
|
|
11
|
+
private request;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=OpenAIPromptAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAIPromptAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/OpenAIPromptAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAY,cAAc,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAKzF,qBAAa,mBAAoB,YAAW,aAAa;IAI3C,OAAO,CAAC,QAAQ,CAAC,IAAI;IAHjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAa;gBAEA,IAAI,EAAE,oBAAoB;IAMhD,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,GAAG;IA4CzC,cAAc,CAClB,QAAQ,EAAE,GAAG,EACb,KAAK,GAAE,cAAc,EAAO,EAC5B,UAAU,GAAE,MAAM,GAAG,MAAM,GAAG,MAAe,EAC7C,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,EAChC,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,YAAY,CAAC;IAmDxB,OAAO,CAAC,YAAY;YAmCN,OAAO;CAetB"}
|