@sparkleideas/providers 3.0.0-alpha.6-patch.25 → 3.0.0-alpha.6-patch.27
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/dist/anthropic-provider.d.ts +26 -0
- package/dist/anthropic-provider.d.ts.map +1 -0
- package/dist/anthropic-provider.js +513 -0
- package/dist/base-provider.d.ts +174 -0
- package/dist/base-provider.d.ts.map +1 -0
- package/dist/base-provider.js +636 -0
- package/dist/cohere-provider.d.ts +26 -0
- package/dist/cohere-provider.d.ts.map +1 -0
- package/dist/cohere-provider.js +501 -0
- package/dist/google-provider.d.ts +25 -0
- package/dist/google-provider.d.ts.map +1 -0
- package/dist/google-provider.js +498 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/ollama-provider.d.ts +26 -0
- package/dist/ollama-provider.d.ts.map +1 -0
- package/dist/ollama-provider.js +489 -0
- package/dist/openai-provider.d.ts +26 -0
- package/dist/openai-provider.d.ts.map +1 -0
- package/dist/openai-provider.js +543 -0
- package/dist/provider-manager.d.ts +117 -0
- package/dist/provider-manager.d.ts.map +1 -0
- package/dist/provider-manager.js +712 -0
- package/dist/ruvector-provider.d.ts +65 -0
- package/dist/ruvector-provider.d.ts.map +1 -0
- package/dist/ruvector-provider.js +813 -0
- package/dist/types.d.ts +280 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +108 -0
- package/package.json +1 -1
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* V3 Cohere Provider
|
|
4
|
+
*
|
|
5
|
+
* Supports Command R+, Command R, and Command Light models.
|
|
6
|
+
*
|
|
7
|
+
* @module @sparkleideas/providers/cohere-provider
|
|
8
|
+
*/
|
|
9
|
+
var __extends = (this && this.__extends) || (function () {
|
|
10
|
+
var extendStatics = function (d, b) {
|
|
11
|
+
extendStatics = Object.setPrototypeOf ||
|
|
12
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
13
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
14
|
+
return extendStatics(d, b);
|
|
15
|
+
};
|
|
16
|
+
return function (d, b) {
|
|
17
|
+
if (typeof b !== "function" && b !== null)
|
|
18
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
19
|
+
extendStatics(d, b);
|
|
20
|
+
function __() { this.constructor = d; }
|
|
21
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __assign = (this && this.__assign) || function () {
|
|
25
|
+
__assign = Object.assign || function(t) {
|
|
26
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
27
|
+
s = arguments[i];
|
|
28
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
29
|
+
t[p] = s[p];
|
|
30
|
+
}
|
|
31
|
+
return t;
|
|
32
|
+
};
|
|
33
|
+
return __assign.apply(this, arguments);
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
45
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
46
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
47
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
48
|
+
function step(op) {
|
|
49
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
50
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
51
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
52
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
53
|
+
switch (op[0]) {
|
|
54
|
+
case 0: case 1: t = op; break;
|
|
55
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
56
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
57
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
58
|
+
default:
|
|
59
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
60
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
61
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
62
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
63
|
+
if (t[2]) _.ops.pop();
|
|
64
|
+
_.trys.pop(); continue;
|
|
65
|
+
}
|
|
66
|
+
op = body.call(thisArg, _);
|
|
67
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
68
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
72
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
73
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
74
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
75
|
+
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
76
|
+
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
77
|
+
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
78
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
79
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
80
|
+
function fulfill(value) { resume("next", value); }
|
|
81
|
+
function reject(value) { resume("throw", value); }
|
|
82
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
83
|
+
};
|
|
84
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
85
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
86
|
+
if (ar || !(i in from)) {
|
|
87
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
88
|
+
ar[i] = from[i];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
92
|
+
};
|
|
93
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
94
|
+
exports.CohereProvider = void 0;
|
|
95
|
+
var base_provider_js_1 = require("./base-provider.js");
|
|
96
|
+
var types_js_1 = require("./types.js");
|
|
97
|
+
var CohereProvider = /** @class */ (function (_super) {
|
|
98
|
+
__extends(CohereProvider, _super);
|
|
99
|
+
function CohereProvider(options) {
|
|
100
|
+
var _this = _super.call(this, options) || this;
|
|
101
|
+
_this.name = 'cohere';
|
|
102
|
+
_this.capabilities = {
|
|
103
|
+
supportedModels: [
|
|
104
|
+
'command-r-plus',
|
|
105
|
+
'command-r',
|
|
106
|
+
'command-light',
|
|
107
|
+
'command',
|
|
108
|
+
],
|
|
109
|
+
maxContextLength: {
|
|
110
|
+
'command-r-plus': 128000,
|
|
111
|
+
'command-r': 128000,
|
|
112
|
+
'command-light': 4096,
|
|
113
|
+
'command': 4096,
|
|
114
|
+
},
|
|
115
|
+
maxOutputTokens: {
|
|
116
|
+
'command-r-plus': 4096,
|
|
117
|
+
'command-r': 4096,
|
|
118
|
+
'command-light': 4096,
|
|
119
|
+
'command': 4096,
|
|
120
|
+
},
|
|
121
|
+
supportsStreaming: true,
|
|
122
|
+
supportsToolCalling: true,
|
|
123
|
+
supportsSystemMessages: true,
|
|
124
|
+
supportsVision: false,
|
|
125
|
+
supportsAudio: false,
|
|
126
|
+
supportsFineTuning: true,
|
|
127
|
+
supportsEmbeddings: true,
|
|
128
|
+
supportsBatching: false,
|
|
129
|
+
rateLimit: {
|
|
130
|
+
requestsPerMinute: 1000,
|
|
131
|
+
tokensPerMinute: 100000,
|
|
132
|
+
concurrentRequests: 100,
|
|
133
|
+
},
|
|
134
|
+
pricing: {
|
|
135
|
+
'command-r-plus': {
|
|
136
|
+
promptCostPer1k: 0.003,
|
|
137
|
+
completionCostPer1k: 0.015,
|
|
138
|
+
currency: 'USD',
|
|
139
|
+
},
|
|
140
|
+
'command-r': {
|
|
141
|
+
promptCostPer1k: 0.0005,
|
|
142
|
+
completionCostPer1k: 0.0015,
|
|
143
|
+
currency: 'USD',
|
|
144
|
+
},
|
|
145
|
+
'command-light': {
|
|
146
|
+
promptCostPer1k: 0.0003,
|
|
147
|
+
completionCostPer1k: 0.0006,
|
|
148
|
+
currency: 'USD',
|
|
149
|
+
},
|
|
150
|
+
'command': {
|
|
151
|
+
promptCostPer1k: 0.001,
|
|
152
|
+
completionCostPer1k: 0.002,
|
|
153
|
+
currency: 'USD',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
_this.baseUrl = 'https://api.cohere.ai/v1';
|
|
158
|
+
_this.headers = {};
|
|
159
|
+
return _this;
|
|
160
|
+
}
|
|
161
|
+
CohereProvider.prototype.doInitialize = function () {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
163
|
+
return __generator(this, function (_a) {
|
|
164
|
+
if (!this.config.apiKey) {
|
|
165
|
+
throw new types_js_1.AuthenticationError('Cohere API key is required', 'cohere');
|
|
166
|
+
}
|
|
167
|
+
this.baseUrl = this.config.apiUrl || 'https://api.cohere.ai/v1';
|
|
168
|
+
this.headers = {
|
|
169
|
+
Authorization: "Bearer ".concat(this.config.apiKey),
|
|
170
|
+
'Content-Type': 'application/json',
|
|
171
|
+
};
|
|
172
|
+
return [2 /*return*/];
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
CohereProvider.prototype.doComplete = function (request) {
|
|
177
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
178
|
+
var cohereRequest, controller, timeout, response, data, error_1;
|
|
179
|
+
return __generator(this, function (_a) {
|
|
180
|
+
switch (_a.label) {
|
|
181
|
+
case 0:
|
|
182
|
+
cohereRequest = this.buildRequest(request);
|
|
183
|
+
controller = new AbortController();
|
|
184
|
+
timeout = setTimeout(function () { return controller.abort(); }, this.config.timeout || 60000);
|
|
185
|
+
_a.label = 1;
|
|
186
|
+
case 1:
|
|
187
|
+
_a.trys.push([1, 6, , 7]);
|
|
188
|
+
return [4 /*yield*/, fetch("".concat(this.baseUrl, "/chat"), {
|
|
189
|
+
method: 'POST',
|
|
190
|
+
headers: this.headers,
|
|
191
|
+
body: JSON.stringify(cohereRequest),
|
|
192
|
+
signal: controller.signal,
|
|
193
|
+
})];
|
|
194
|
+
case 2:
|
|
195
|
+
response = _a.sent();
|
|
196
|
+
clearTimeout(timeout);
|
|
197
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
|
198
|
+
return [4 /*yield*/, this.handleErrorResponse(response)];
|
|
199
|
+
case 3:
|
|
200
|
+
_a.sent();
|
|
201
|
+
_a.label = 4;
|
|
202
|
+
case 4: return [4 /*yield*/, response.json()];
|
|
203
|
+
case 5:
|
|
204
|
+
data = _a.sent();
|
|
205
|
+
return [2 /*return*/, this.transformResponse(data, request)];
|
|
206
|
+
case 6:
|
|
207
|
+
error_1 = _a.sent();
|
|
208
|
+
clearTimeout(timeout);
|
|
209
|
+
throw this.transformError(error_1);
|
|
210
|
+
case 7: return [2 /*return*/];
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
CohereProvider.prototype.doStreamComplete = function (request) {
|
|
216
|
+
return __asyncGenerator(this, arguments, function doStreamComplete_1() {
|
|
217
|
+
var cohereRequest, controller, timeout, response, reader, decoder, buffer, inputTokens, outputTokens, _a, done, value, lines, _i, lines_1, line, event_1, model, pricing, _b, error_2;
|
|
218
|
+
var _c, _d;
|
|
219
|
+
return __generator(this, function (_e) {
|
|
220
|
+
switch (_e.label) {
|
|
221
|
+
case 0:
|
|
222
|
+
cohereRequest = this.buildRequest(request, true);
|
|
223
|
+
controller = new AbortController();
|
|
224
|
+
timeout = setTimeout(function () { return controller.abort(); }, (this.config.timeout || 60000) * 2);
|
|
225
|
+
_e.label = 1;
|
|
226
|
+
case 1:
|
|
227
|
+
_e.trys.push([1, 19, 20, 21]);
|
|
228
|
+
return [4 /*yield*/, __await(fetch("".concat(this.baseUrl, "/chat"), {
|
|
229
|
+
method: 'POST',
|
|
230
|
+
headers: this.headers,
|
|
231
|
+
body: JSON.stringify(cohereRequest),
|
|
232
|
+
signal: controller.signal,
|
|
233
|
+
}))];
|
|
234
|
+
case 2:
|
|
235
|
+
response = _e.sent();
|
|
236
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
|
237
|
+
return [4 /*yield*/, __await(this.handleErrorResponse(response))];
|
|
238
|
+
case 3:
|
|
239
|
+
_e.sent();
|
|
240
|
+
_e.label = 4;
|
|
241
|
+
case 4:
|
|
242
|
+
reader = response.body.getReader();
|
|
243
|
+
decoder = new TextDecoder();
|
|
244
|
+
buffer = '';
|
|
245
|
+
inputTokens = 0;
|
|
246
|
+
outputTokens = 0;
|
|
247
|
+
_e.label = 5;
|
|
248
|
+
case 5:
|
|
249
|
+
if (!true) return [3 /*break*/, 18];
|
|
250
|
+
return [4 /*yield*/, __await(reader.read())];
|
|
251
|
+
case 6:
|
|
252
|
+
_a = _e.sent(), done = _a.done, value = _a.value;
|
|
253
|
+
if (done)
|
|
254
|
+
return [3 /*break*/, 18];
|
|
255
|
+
buffer += decoder.decode(value, { stream: true });
|
|
256
|
+
lines = buffer.split('\n');
|
|
257
|
+
buffer = lines.pop() || '';
|
|
258
|
+
_i = 0, lines_1 = lines;
|
|
259
|
+
_e.label = 7;
|
|
260
|
+
case 7:
|
|
261
|
+
if (!(_i < lines_1.length)) return [3 /*break*/, 17];
|
|
262
|
+
line = lines_1[_i];
|
|
263
|
+
if (!line.trim())
|
|
264
|
+
return [3 /*break*/, 16];
|
|
265
|
+
_e.label = 8;
|
|
266
|
+
case 8:
|
|
267
|
+
_e.trys.push([8, 15, , 16]);
|
|
268
|
+
event_1 = JSON.parse(line);
|
|
269
|
+
if (!(event_1.event_type === 'text-generation' && event_1.text)) return [3 /*break*/, 11];
|
|
270
|
+
return [4 /*yield*/, __await({
|
|
271
|
+
type: 'content',
|
|
272
|
+
delta: { content: event_1.text },
|
|
273
|
+
})];
|
|
274
|
+
case 9: return [4 /*yield*/, _e.sent()];
|
|
275
|
+
case 10:
|
|
276
|
+
_e.sent();
|
|
277
|
+
return [3 /*break*/, 14];
|
|
278
|
+
case 11:
|
|
279
|
+
if (!(event_1.event_type === 'stream-end')) return [3 /*break*/, 14];
|
|
280
|
+
if ((_d = (_c = event_1.response) === null || _c === void 0 ? void 0 : _c.meta) === null || _d === void 0 ? void 0 : _d.billed_units) {
|
|
281
|
+
inputTokens = event_1.response.meta.billed_units.input_tokens;
|
|
282
|
+
outputTokens = event_1.response.meta.billed_units.output_tokens;
|
|
283
|
+
}
|
|
284
|
+
model = request.model || this.config.model;
|
|
285
|
+
pricing = this.capabilities.pricing[model];
|
|
286
|
+
return [4 /*yield*/, __await({
|
|
287
|
+
type: 'done',
|
|
288
|
+
usage: {
|
|
289
|
+
promptTokens: inputTokens,
|
|
290
|
+
completionTokens: outputTokens,
|
|
291
|
+
totalTokens: inputTokens + outputTokens,
|
|
292
|
+
},
|
|
293
|
+
cost: {
|
|
294
|
+
promptCost: (inputTokens / 1000) * pricing.promptCostPer1k,
|
|
295
|
+
completionCost: (outputTokens / 1000) * pricing.completionCostPer1k,
|
|
296
|
+
totalCost: (inputTokens / 1000) * pricing.promptCostPer1k +
|
|
297
|
+
(outputTokens / 1000) * pricing.completionCostPer1k,
|
|
298
|
+
currency: 'USD',
|
|
299
|
+
},
|
|
300
|
+
})];
|
|
301
|
+
case 12: return [4 /*yield*/, _e.sent()];
|
|
302
|
+
case 13:
|
|
303
|
+
_e.sent();
|
|
304
|
+
_e.label = 14;
|
|
305
|
+
case 14: return [3 /*break*/, 16];
|
|
306
|
+
case 15:
|
|
307
|
+
_b = _e.sent();
|
|
308
|
+
return [3 /*break*/, 16];
|
|
309
|
+
case 16:
|
|
310
|
+
_i++;
|
|
311
|
+
return [3 /*break*/, 7];
|
|
312
|
+
case 17: return [3 /*break*/, 5];
|
|
313
|
+
case 18: return [3 /*break*/, 21];
|
|
314
|
+
case 19:
|
|
315
|
+
error_2 = _e.sent();
|
|
316
|
+
clearTimeout(timeout);
|
|
317
|
+
throw this.transformError(error_2);
|
|
318
|
+
case 20:
|
|
319
|
+
clearTimeout(timeout);
|
|
320
|
+
return [7 /*endfinally*/];
|
|
321
|
+
case 21: return [2 /*return*/];
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
CohereProvider.prototype.listModels = function () {
|
|
327
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
328
|
+
return __generator(this, function (_a) {
|
|
329
|
+
return [2 /*return*/, this.capabilities.supportedModels];
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
CohereProvider.prototype.getModelInfo = function (model) {
|
|
334
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
335
|
+
var descriptions;
|
|
336
|
+
return __generator(this, function (_a) {
|
|
337
|
+
descriptions = {
|
|
338
|
+
'command-r-plus': 'Most capable Cohere model with 128K context',
|
|
339
|
+
'command-r': 'Balanced Cohere model with 128K context',
|
|
340
|
+
'command-light': 'Fast and efficient Cohere model',
|
|
341
|
+
'command': 'Standard Cohere model',
|
|
342
|
+
};
|
|
343
|
+
return [2 /*return*/, {
|
|
344
|
+
model: model,
|
|
345
|
+
name: model,
|
|
346
|
+
description: descriptions[model] || 'Cohere language model',
|
|
347
|
+
contextLength: this.capabilities.maxContextLength[model] || 4096,
|
|
348
|
+
maxOutputTokens: this.capabilities.maxOutputTokens[model] || 4096,
|
|
349
|
+
supportedFeatures: ['chat', 'completion', 'tool_calling', 'rag'],
|
|
350
|
+
pricing: this.capabilities.pricing[model],
|
|
351
|
+
}];
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
CohereProvider.prototype.doHealthCheck = function () {
|
|
356
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
357
|
+
var response, error_3;
|
|
358
|
+
return __generator(this, function (_a) {
|
|
359
|
+
switch (_a.label) {
|
|
360
|
+
case 0:
|
|
361
|
+
_a.trys.push([0, 2, , 3]);
|
|
362
|
+
return [4 /*yield*/, fetch("".concat(this.baseUrl, "/check-api-key"), {
|
|
363
|
+
method: 'POST',
|
|
364
|
+
headers: this.headers,
|
|
365
|
+
})];
|
|
366
|
+
case 1:
|
|
367
|
+
response = _a.sent();
|
|
368
|
+
return [2 /*return*/, __assign({ healthy: response.ok, timestamp: new Date() }, (response.ok ? {} : { error: "HTTP ".concat(response.status) }))];
|
|
369
|
+
case 2:
|
|
370
|
+
error_3 = _a.sent();
|
|
371
|
+
return [2 /*return*/, {
|
|
372
|
+
healthy: false,
|
|
373
|
+
error: error_3 instanceof Error ? error_3.message : 'Unknown error',
|
|
374
|
+
timestamp: new Date(),
|
|
375
|
+
}];
|
|
376
|
+
case 3: return [2 /*return*/];
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
};
|
|
381
|
+
CohereProvider.prototype.buildRequest = function (request, stream) {
|
|
382
|
+
var _a, _b, _c;
|
|
383
|
+
if (stream === void 0) { stream = false; }
|
|
384
|
+
// Get the last user message
|
|
385
|
+
var lastUserMessage = __spreadArray([], request.messages, true).reverse().find(function (m) { return m.role === 'user'; });
|
|
386
|
+
var systemMessage = request.messages.find(function (m) { return m.role === 'system'; });
|
|
387
|
+
// Build chat history (exclude last user message)
|
|
388
|
+
var chatHistory = request.messages
|
|
389
|
+
.filter(function (m) { return m !== lastUserMessage && m.role !== 'system'; })
|
|
390
|
+
.map(function (msg) { return ({
|
|
391
|
+
role: msg.role === 'assistant' ? 'CHATBOT' : 'USER',
|
|
392
|
+
message: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content),
|
|
393
|
+
}); });
|
|
394
|
+
var cohereRequest = {
|
|
395
|
+
model: request.model || this.config.model,
|
|
396
|
+
message: lastUserMessage
|
|
397
|
+
? (typeof lastUserMessage.content === 'string'
|
|
398
|
+
? lastUserMessage.content
|
|
399
|
+
: JSON.stringify(lastUserMessage.content))
|
|
400
|
+
: '',
|
|
401
|
+
stream: stream,
|
|
402
|
+
};
|
|
403
|
+
if (chatHistory.length > 0) {
|
|
404
|
+
cohereRequest.chat_history = chatHistory;
|
|
405
|
+
}
|
|
406
|
+
if (systemMessage) {
|
|
407
|
+
cohereRequest.preamble = typeof systemMessage.content === 'string'
|
|
408
|
+
? systemMessage.content
|
|
409
|
+
: JSON.stringify(systemMessage.content);
|
|
410
|
+
}
|
|
411
|
+
if (request.temperature !== undefined || this.config.temperature !== undefined) {
|
|
412
|
+
cohereRequest.temperature = (_a = request.temperature) !== null && _a !== void 0 ? _a : this.config.temperature;
|
|
413
|
+
}
|
|
414
|
+
if (request.topP !== undefined || this.config.topP !== undefined) {
|
|
415
|
+
cohereRequest.p = (_b = request.topP) !== null && _b !== void 0 ? _b : this.config.topP;
|
|
416
|
+
}
|
|
417
|
+
if (request.topK !== undefined || this.config.topK !== undefined) {
|
|
418
|
+
cohereRequest.k = (_c = request.topK) !== null && _c !== void 0 ? _c : this.config.topK;
|
|
419
|
+
}
|
|
420
|
+
if (request.maxTokens || this.config.maxTokens) {
|
|
421
|
+
cohereRequest.max_tokens = request.maxTokens || this.config.maxTokens;
|
|
422
|
+
}
|
|
423
|
+
if (request.stopSequences || this.config.stopSequences) {
|
|
424
|
+
cohereRequest.stop_sequences = request.stopSequences || this.config.stopSequences;
|
|
425
|
+
}
|
|
426
|
+
if (request.tools) {
|
|
427
|
+
cohereRequest.tools = request.tools.map(function (tool) { return ({
|
|
428
|
+
name: tool.function.name,
|
|
429
|
+
description: tool.function.description,
|
|
430
|
+
parameter_definitions: tool.function.parameters.properties,
|
|
431
|
+
}); });
|
|
432
|
+
}
|
|
433
|
+
return cohereRequest;
|
|
434
|
+
};
|
|
435
|
+
CohereProvider.prototype.transformResponse = function (data, request) {
|
|
436
|
+
var _a;
|
|
437
|
+
var model = request.model || this.config.model;
|
|
438
|
+
var pricing = this.capabilities.pricing[model];
|
|
439
|
+
var inputTokens = data.meta.billed_units.input_tokens;
|
|
440
|
+
var outputTokens = data.meta.billed_units.output_tokens;
|
|
441
|
+
var promptCost = (inputTokens / 1000) * pricing.promptCostPer1k;
|
|
442
|
+
var completionCost = (outputTokens / 1000) * pricing.completionCostPer1k;
|
|
443
|
+
var toolCalls = (_a = data.tool_calls) === null || _a === void 0 ? void 0 : _a.map(function (tc) { return ({
|
|
444
|
+
id: "tool_".concat(Date.now()),
|
|
445
|
+
type: 'function',
|
|
446
|
+
function: {
|
|
447
|
+
name: tc.name,
|
|
448
|
+
arguments: JSON.stringify(tc.parameters),
|
|
449
|
+
},
|
|
450
|
+
}); });
|
|
451
|
+
return {
|
|
452
|
+
id: data.response_id,
|
|
453
|
+
model: model,
|
|
454
|
+
provider: 'cohere',
|
|
455
|
+
content: data.text,
|
|
456
|
+
toolCalls: (toolCalls === null || toolCalls === void 0 ? void 0 : toolCalls.length) ? toolCalls : undefined,
|
|
457
|
+
usage: {
|
|
458
|
+
promptTokens: inputTokens,
|
|
459
|
+
completionTokens: outputTokens,
|
|
460
|
+
totalTokens: inputTokens + outputTokens,
|
|
461
|
+
},
|
|
462
|
+
cost: {
|
|
463
|
+
promptCost: promptCost,
|
|
464
|
+
completionCost: completionCost,
|
|
465
|
+
totalCost: promptCost + completionCost,
|
|
466
|
+
currency: 'USD',
|
|
467
|
+
},
|
|
468
|
+
finishReason: data.finish_reason === 'COMPLETE' ? 'stop' : 'length',
|
|
469
|
+
};
|
|
470
|
+
};
|
|
471
|
+
CohereProvider.prototype.handleErrorResponse = function (response) {
|
|
472
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
473
|
+
var errorText, errorData, message;
|
|
474
|
+
return __generator(this, function (_a) {
|
|
475
|
+
switch (_a.label) {
|
|
476
|
+
case 0: return [4 /*yield*/, response.text()];
|
|
477
|
+
case 1:
|
|
478
|
+
errorText = _a.sent();
|
|
479
|
+
try {
|
|
480
|
+
errorData = JSON.parse(errorText);
|
|
481
|
+
}
|
|
482
|
+
catch (_b) {
|
|
483
|
+
errorData = { message: errorText };
|
|
484
|
+
}
|
|
485
|
+
message = errorData.message || 'Unknown error';
|
|
486
|
+
switch (response.status) {
|
|
487
|
+
case 401:
|
|
488
|
+
throw new types_js_1.AuthenticationError(message, 'cohere', errorData);
|
|
489
|
+
case 429:
|
|
490
|
+
throw new types_js_1.RateLimitError(message, 'cohere', undefined, errorData);
|
|
491
|
+
default:
|
|
492
|
+
throw new types_js_1.LLMProviderError(message, "COHERE_".concat(response.status), 'cohere', response.status, response.status >= 500, errorData);
|
|
493
|
+
}
|
|
494
|
+
return [2 /*return*/];
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
return CohereProvider;
|
|
500
|
+
}(base_provider_js_1.BaseProvider));
|
|
501
|
+
exports.CohereProvider = CohereProvider;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Google (Gemini) Provider
|
|
3
|
+
*
|
|
4
|
+
* Supports Gemini 2.0, 1.5 Pro, and Flash models.
|
|
5
|
+
*
|
|
6
|
+
* @module @sparkleideas/providers/google-provider
|
|
7
|
+
*/
|
|
8
|
+
import { BaseProvider, BaseProviderOptions } from './base-provider.js';
|
|
9
|
+
import { LLMProvider, LLMModel, LLMRequest, LLMResponse, LLMStreamEvent, ModelInfo, ProviderCapabilities, HealthCheckResult } from './types.js';
|
|
10
|
+
export declare class GoogleProvider extends BaseProvider {
|
|
11
|
+
readonly name: LLMProvider;
|
|
12
|
+
readonly capabilities: ProviderCapabilities;
|
|
13
|
+
private baseUrl;
|
|
14
|
+
constructor(options: BaseProviderOptions);
|
|
15
|
+
protected doInitialize(): Promise<void>;
|
|
16
|
+
protected doComplete(request: LLMRequest): Promise<LLMResponse>;
|
|
17
|
+
protected doStreamComplete(request: LLMRequest): AsyncIterable<LLMStreamEvent>;
|
|
18
|
+
listModels(): Promise<LLMModel[]>;
|
|
19
|
+
getModelInfo(model: LLMModel): Promise<ModelInfo>;
|
|
20
|
+
protected doHealthCheck(): Promise<HealthCheckResult>;
|
|
21
|
+
private buildRequest;
|
|
22
|
+
private transformResponse;
|
|
23
|
+
private handleErrorResponse;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=google-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-provider.d.ts","sourceRoot":"","sources":["../src/google-provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,EACL,WAAW,EACX,QAAQ,EACR,UAAU,EACV,WAAW,EACX,cAAc,EACd,SAAS,EACT,oBAAoB,EACpB,iBAAiB,EAIlB,MAAM,YAAY,CAAC;AAyCpB,qBAAa,cAAe,SAAQ,YAAY;IAC9C,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAY;IACtC,QAAQ,CAAC,YAAY,EAAE,oBAAoB,CAsDzC;IAEF,OAAO,CAAC,OAAO,CAA8D;gBAEjE,OAAO,EAAE,mBAAmB;cAIxB,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;cAQ7B,UAAU,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;cA8BpD,gBAAgB,CAAC,OAAO,EAAE,UAAU,GAAG,aAAa,CAAC,cAAc,CAAC;IA2F/E,UAAU,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IAIjC,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC;cAmBvC,aAAa,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAmB3D,OAAO,CAAC,YAAY;IA0DpB,OAAO,CAAC,iBAAiB;YA4CX,mBAAmB;CA6BlC"}
|