ai 3.0.21 → 3.0.23
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/index.d.mts +42 -1
- package/dist/index.d.ts +42 -1
- package/dist/index.js +104 -177
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -138
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -33
- package/react/dist/index.d.mts +6 -2
- package/react/dist/index.d.ts +6 -2
- package/react/dist/index.js +107 -24
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +107 -24
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +3 -3
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +6 -2
- package/solid/dist/index.d.ts +6 -2
- package/solid/dist/index.js +105 -23
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +105 -23
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +6 -2
- package/svelte/dist/index.d.ts +6 -2
- package/svelte/dist/index.js +107 -24
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +107 -24
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +6 -2
- package/vue/dist/index.d.ts +6 -2
- package/vue/dist/index.js +105 -23
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +105 -23
- package/vue/dist/index.mjs.map +1 -1
- package/anthropic/dist/index.d.mts +0 -51
- package/anthropic/dist/index.d.ts +0 -51
- package/anthropic/dist/index.js +0 -792
- package/anthropic/dist/index.js.map +0 -1
- package/anthropic/dist/index.mjs +0 -760
- package/anthropic/dist/index.mjs.map +0 -1
- package/google/dist/index.d.mts +0 -47
- package/google/dist/index.d.ts +0 -47
- package/google/dist/index.js +0 -796
- package/google/dist/index.js.map +0 -1
- package/google/dist/index.mjs +0 -764
- package/google/dist/index.mjs.map +0 -1
- package/mistral/dist/index.d.mts +0 -52
- package/mistral/dist/index.d.ts +0 -52
- package/mistral/dist/index.js +0 -763
- package/mistral/dist/index.js.map +0 -1
- package/mistral/dist/index.mjs +0 -731
- package/mistral/dist/index.mjs.map +0 -1
- package/openai/dist/index.d.mts +0 -116
- package/openai/dist/index.d.ts +0 -116
- package/openai/dist/index.js +0 -1143
- package/openai/dist/index.js.map +0 -1
- package/openai/dist/index.mjs +0 -1115
- package/openai/dist/index.mjs.map +0 -1
package/mistral/dist/index.mjs
DELETED
@@ -1,731 +0,0 @@
|
|
1
|
-
// spec/util/generate-id.ts
|
2
|
-
import { customAlphabet } from "nanoid/non-secure";
|
3
|
-
var generateId = customAlphabet(
|
4
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
5
|
-
7
|
6
|
-
);
|
7
|
-
|
8
|
-
// spec/util/load-api-key.ts
|
9
|
-
import { LoadAPIKeyError } from "@ai-sdk/provider";
|
10
|
-
function loadApiKey({
|
11
|
-
apiKey,
|
12
|
-
environmentVariableName,
|
13
|
-
apiKeyParameterName = "apiKey",
|
14
|
-
description
|
15
|
-
}) {
|
16
|
-
if (typeof apiKey === "string") {
|
17
|
-
return apiKey;
|
18
|
-
}
|
19
|
-
if (apiKey != null) {
|
20
|
-
throw new LoadAPIKeyError({
|
21
|
-
message: `${description} API key must be a string.`
|
22
|
-
});
|
23
|
-
}
|
24
|
-
if (typeof process === "undefined") {
|
25
|
-
throw new LoadAPIKeyError({
|
26
|
-
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
27
|
-
});
|
28
|
-
}
|
29
|
-
apiKey = process.env[environmentVariableName];
|
30
|
-
if (apiKey == null) {
|
31
|
-
throw new LoadAPIKeyError({
|
32
|
-
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
33
|
-
});
|
34
|
-
}
|
35
|
-
if (typeof apiKey !== "string") {
|
36
|
-
throw new LoadAPIKeyError({
|
37
|
-
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
38
|
-
});
|
39
|
-
}
|
40
|
-
return apiKey;
|
41
|
-
}
|
42
|
-
|
43
|
-
// spec/util/parse-json.ts
|
44
|
-
import { JSONParseError, TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
|
45
|
-
import SecureJSON from "secure-json-parse";
|
46
|
-
|
47
|
-
// spec/util/validate-types.ts
|
48
|
-
import { TypeValidationError } from "@ai-sdk/provider";
|
49
|
-
function validateTypes({
|
50
|
-
value,
|
51
|
-
schema
|
52
|
-
}) {
|
53
|
-
try {
|
54
|
-
return schema.parse(value);
|
55
|
-
} catch (error) {
|
56
|
-
throw new TypeValidationError({ value, cause: error });
|
57
|
-
}
|
58
|
-
}
|
59
|
-
function safeValidateTypes({
|
60
|
-
value,
|
61
|
-
schema
|
62
|
-
}) {
|
63
|
-
try {
|
64
|
-
const validationResult = schema.safeParse(value);
|
65
|
-
if (validationResult.success) {
|
66
|
-
return {
|
67
|
-
success: true,
|
68
|
-
value: validationResult.data
|
69
|
-
};
|
70
|
-
}
|
71
|
-
return {
|
72
|
-
success: false,
|
73
|
-
error: new TypeValidationError({
|
74
|
-
value,
|
75
|
-
cause: validationResult.error
|
76
|
-
})
|
77
|
-
};
|
78
|
-
} catch (error) {
|
79
|
-
return {
|
80
|
-
success: false,
|
81
|
-
error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
|
82
|
-
};
|
83
|
-
}
|
84
|
-
}
|
85
|
-
|
86
|
-
// spec/util/parse-json.ts
|
87
|
-
function parseJSON({
|
88
|
-
text,
|
89
|
-
schema
|
90
|
-
}) {
|
91
|
-
try {
|
92
|
-
const value = SecureJSON.parse(text);
|
93
|
-
if (schema == null) {
|
94
|
-
return value;
|
95
|
-
}
|
96
|
-
return validateTypes({ value, schema });
|
97
|
-
} catch (error) {
|
98
|
-
if (JSONParseError.isJSONParseError(error) || TypeValidationError2.isTypeValidationError(error)) {
|
99
|
-
throw error;
|
100
|
-
}
|
101
|
-
throw new JSONParseError({ text, cause: error });
|
102
|
-
}
|
103
|
-
}
|
104
|
-
function safeParseJSON({
|
105
|
-
text,
|
106
|
-
schema
|
107
|
-
}) {
|
108
|
-
try {
|
109
|
-
const value = SecureJSON.parse(text);
|
110
|
-
if (schema == null) {
|
111
|
-
return {
|
112
|
-
success: true,
|
113
|
-
value
|
114
|
-
};
|
115
|
-
}
|
116
|
-
return safeValidateTypes({ value, schema });
|
117
|
-
} catch (error) {
|
118
|
-
return {
|
119
|
-
success: false,
|
120
|
-
error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
|
121
|
-
};
|
122
|
-
}
|
123
|
-
}
|
124
|
-
|
125
|
-
// spec/util/post-to-api.ts
|
126
|
-
import { APICallError } from "@ai-sdk/provider";
|
127
|
-
var postJsonToApi = async ({
|
128
|
-
url,
|
129
|
-
headers,
|
130
|
-
body,
|
131
|
-
failedResponseHandler,
|
132
|
-
successfulResponseHandler,
|
133
|
-
abortSignal
|
134
|
-
}) => postToApi({
|
135
|
-
url,
|
136
|
-
headers: {
|
137
|
-
...headers,
|
138
|
-
"Content-Type": "application/json"
|
139
|
-
},
|
140
|
-
body: {
|
141
|
-
content: JSON.stringify(body),
|
142
|
-
values: body
|
143
|
-
},
|
144
|
-
failedResponseHandler,
|
145
|
-
successfulResponseHandler,
|
146
|
-
abortSignal
|
147
|
-
});
|
148
|
-
var postToApi = async ({
|
149
|
-
url,
|
150
|
-
headers = {},
|
151
|
-
body,
|
152
|
-
successfulResponseHandler,
|
153
|
-
failedResponseHandler,
|
154
|
-
abortSignal
|
155
|
-
}) => {
|
156
|
-
try {
|
157
|
-
const definedHeaders = Object.fromEntries(
|
158
|
-
Object.entries(headers).filter(([_key, value]) => value != null)
|
159
|
-
);
|
160
|
-
const response = await fetch(url, {
|
161
|
-
method: "POST",
|
162
|
-
headers: definedHeaders,
|
163
|
-
body: body.content,
|
164
|
-
signal: abortSignal
|
165
|
-
});
|
166
|
-
if (!response.ok) {
|
167
|
-
try {
|
168
|
-
throw await failedResponseHandler({
|
169
|
-
response,
|
170
|
-
url,
|
171
|
-
requestBodyValues: body.values
|
172
|
-
});
|
173
|
-
} catch (error) {
|
174
|
-
if (error instanceof Error) {
|
175
|
-
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
176
|
-
throw error;
|
177
|
-
}
|
178
|
-
}
|
179
|
-
throw new APICallError({
|
180
|
-
message: "Failed to process error response",
|
181
|
-
cause: error,
|
182
|
-
statusCode: response.status,
|
183
|
-
url,
|
184
|
-
requestBodyValues: body.values
|
185
|
-
});
|
186
|
-
}
|
187
|
-
}
|
188
|
-
try {
|
189
|
-
return await successfulResponseHandler({
|
190
|
-
response,
|
191
|
-
url,
|
192
|
-
requestBodyValues: body.values
|
193
|
-
});
|
194
|
-
} catch (error) {
|
195
|
-
if (error instanceof Error) {
|
196
|
-
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
197
|
-
throw error;
|
198
|
-
}
|
199
|
-
}
|
200
|
-
throw new APICallError({
|
201
|
-
message: "Failed to process successful response",
|
202
|
-
cause: error,
|
203
|
-
statusCode: response.status,
|
204
|
-
url,
|
205
|
-
requestBodyValues: body.values
|
206
|
-
});
|
207
|
-
}
|
208
|
-
} catch (error) {
|
209
|
-
if (error instanceof Error) {
|
210
|
-
if (error.name === "AbortError") {
|
211
|
-
throw error;
|
212
|
-
}
|
213
|
-
}
|
214
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
215
|
-
const cause = error.cause;
|
216
|
-
if (cause != null) {
|
217
|
-
throw new APICallError({
|
218
|
-
message: `Cannot connect to API: ${cause.message}`,
|
219
|
-
cause,
|
220
|
-
url,
|
221
|
-
requestBodyValues: body.values,
|
222
|
-
isRetryable: true
|
223
|
-
// retry when network error
|
224
|
-
});
|
225
|
-
}
|
226
|
-
}
|
227
|
-
throw error;
|
228
|
-
}
|
229
|
-
};
|
230
|
-
|
231
|
-
// spec/util/response-handler.ts
|
232
|
-
import { APICallError as APICallError2, NoResponseBodyError } from "@ai-sdk/provider";
|
233
|
-
import {
|
234
|
-
EventSourceParserStream
|
235
|
-
} from "eventsource-parser/stream";
|
236
|
-
var createJsonErrorResponseHandler = ({
|
237
|
-
errorSchema,
|
238
|
-
errorToMessage,
|
239
|
-
isRetryable
|
240
|
-
}) => async ({ response, url, requestBodyValues }) => {
|
241
|
-
const responseBody = await response.text();
|
242
|
-
if (responseBody.trim() === "") {
|
243
|
-
return new APICallError2({
|
244
|
-
message: response.statusText,
|
245
|
-
url,
|
246
|
-
requestBodyValues,
|
247
|
-
statusCode: response.status,
|
248
|
-
responseBody,
|
249
|
-
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
250
|
-
});
|
251
|
-
}
|
252
|
-
try {
|
253
|
-
const parsedError = parseJSON({
|
254
|
-
text: responseBody,
|
255
|
-
schema: errorSchema
|
256
|
-
});
|
257
|
-
return new APICallError2({
|
258
|
-
message: errorToMessage(parsedError),
|
259
|
-
url,
|
260
|
-
requestBodyValues,
|
261
|
-
statusCode: response.status,
|
262
|
-
responseBody,
|
263
|
-
data: parsedError,
|
264
|
-
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
265
|
-
});
|
266
|
-
} catch (parseError) {
|
267
|
-
return new APICallError2({
|
268
|
-
message: response.statusText,
|
269
|
-
url,
|
270
|
-
requestBodyValues,
|
271
|
-
statusCode: response.status,
|
272
|
-
responseBody,
|
273
|
-
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
274
|
-
});
|
275
|
-
}
|
276
|
-
};
|
277
|
-
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
278
|
-
if (response.body == null) {
|
279
|
-
throw new NoResponseBodyError();
|
280
|
-
}
|
281
|
-
return response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
282
|
-
new TransformStream({
|
283
|
-
transform({ data }, controller) {
|
284
|
-
if (data === "[DONE]") {
|
285
|
-
return;
|
286
|
-
}
|
287
|
-
controller.enqueue(
|
288
|
-
safeParseJSON({
|
289
|
-
text: data,
|
290
|
-
schema: chunkSchema
|
291
|
-
})
|
292
|
-
);
|
293
|
-
}
|
294
|
-
})
|
295
|
-
);
|
296
|
-
};
|
297
|
-
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
298
|
-
const responseBody = await response.text();
|
299
|
-
const parsedResult = safeParseJSON({
|
300
|
-
text: responseBody,
|
301
|
-
schema: responseSchema
|
302
|
-
});
|
303
|
-
if (!parsedResult.success) {
|
304
|
-
throw new APICallError2({
|
305
|
-
message: "Invalid JSON response",
|
306
|
-
cause: parsedResult.error,
|
307
|
-
statusCode: response.status,
|
308
|
-
responseBody,
|
309
|
-
url,
|
310
|
-
requestBodyValues
|
311
|
-
});
|
312
|
-
}
|
313
|
-
return parsedResult.value;
|
314
|
-
};
|
315
|
-
|
316
|
-
// mistral/mistral-chat-language-model.ts
|
317
|
-
import {
|
318
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
319
|
-
} from "@ai-sdk/provider";
|
320
|
-
import { z as z2 } from "zod";
|
321
|
-
|
322
|
-
// mistral/convert-to-mistral-chat-messages.ts
|
323
|
-
import {
|
324
|
-
UnsupportedFunctionalityError
|
325
|
-
} from "@ai-sdk/provider";
|
326
|
-
function convertToMistralChatMessages(prompt) {
|
327
|
-
const messages = [];
|
328
|
-
for (const { role, content } of prompt) {
|
329
|
-
switch (role) {
|
330
|
-
case "system": {
|
331
|
-
messages.push({ role: "system", content });
|
332
|
-
break;
|
333
|
-
}
|
334
|
-
case "user": {
|
335
|
-
messages.push({
|
336
|
-
role: "user",
|
337
|
-
content: content.map((part) => {
|
338
|
-
switch (part.type) {
|
339
|
-
case "text": {
|
340
|
-
return part.text;
|
341
|
-
}
|
342
|
-
case "image": {
|
343
|
-
throw new UnsupportedFunctionalityError({
|
344
|
-
functionality: "image-part"
|
345
|
-
});
|
346
|
-
}
|
347
|
-
}
|
348
|
-
}).join("")
|
349
|
-
});
|
350
|
-
break;
|
351
|
-
}
|
352
|
-
case "assistant": {
|
353
|
-
let text = "";
|
354
|
-
const toolCalls = [];
|
355
|
-
for (const part of content) {
|
356
|
-
switch (part.type) {
|
357
|
-
case "text": {
|
358
|
-
text += part.text;
|
359
|
-
break;
|
360
|
-
}
|
361
|
-
case "tool-call": {
|
362
|
-
toolCalls.push({
|
363
|
-
id: part.toolCallId,
|
364
|
-
type: "function",
|
365
|
-
function: {
|
366
|
-
name: part.toolName,
|
367
|
-
arguments: JSON.stringify(part.args)
|
368
|
-
}
|
369
|
-
});
|
370
|
-
break;
|
371
|
-
}
|
372
|
-
default: {
|
373
|
-
const _exhaustiveCheck = part;
|
374
|
-
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
375
|
-
}
|
376
|
-
}
|
377
|
-
}
|
378
|
-
messages.push({
|
379
|
-
role: "assistant",
|
380
|
-
content: text,
|
381
|
-
tool_calls: toolCalls.length > 0 ? toolCalls.map(({ function: { name, arguments: args } }) => ({
|
382
|
-
id: "null",
|
383
|
-
type: "function",
|
384
|
-
function: { name, arguments: args }
|
385
|
-
})) : void 0
|
386
|
-
});
|
387
|
-
break;
|
388
|
-
}
|
389
|
-
case "tool": {
|
390
|
-
for (const toolResponse of content) {
|
391
|
-
messages.push({
|
392
|
-
role: "tool",
|
393
|
-
name: toolResponse.toolName,
|
394
|
-
content: JSON.stringify(toolResponse.result)
|
395
|
-
});
|
396
|
-
}
|
397
|
-
break;
|
398
|
-
}
|
399
|
-
default: {
|
400
|
-
const _exhaustiveCheck = role;
|
401
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
402
|
-
}
|
403
|
-
}
|
404
|
-
}
|
405
|
-
return messages;
|
406
|
-
}
|
407
|
-
|
408
|
-
// mistral/map-mistral-finish-reason.ts
|
409
|
-
function mapMistralFinishReason(finishReason) {
|
410
|
-
switch (finishReason) {
|
411
|
-
case "stop":
|
412
|
-
return "stop";
|
413
|
-
case "length":
|
414
|
-
case "model_length":
|
415
|
-
return "length";
|
416
|
-
case "tool_calls":
|
417
|
-
return "tool-calls";
|
418
|
-
default:
|
419
|
-
return "other";
|
420
|
-
}
|
421
|
-
}
|
422
|
-
|
423
|
-
// mistral/mistral-error.ts
|
424
|
-
import { z } from "zod";
|
425
|
-
var mistralErrorDataSchema = z.object({
|
426
|
-
object: z.literal("error"),
|
427
|
-
message: z.string(),
|
428
|
-
type: z.string(),
|
429
|
-
param: z.string().nullable(),
|
430
|
-
code: z.string().nullable()
|
431
|
-
});
|
432
|
-
var mistralFailedResponseHandler = createJsonErrorResponseHandler({
|
433
|
-
errorSchema: mistralErrorDataSchema,
|
434
|
-
errorToMessage: (data) => data.message
|
435
|
-
});
|
436
|
-
|
437
|
-
// mistral/mistral-chat-language-model.ts
|
438
|
-
var MistralChatLanguageModel = class {
|
439
|
-
constructor(modelId, settings, config) {
|
440
|
-
this.specificationVersion = "v1";
|
441
|
-
this.defaultObjectGenerationMode = "json";
|
442
|
-
this.modelId = modelId;
|
443
|
-
this.settings = settings;
|
444
|
-
this.config = config;
|
445
|
-
}
|
446
|
-
get provider() {
|
447
|
-
return this.config.provider;
|
448
|
-
}
|
449
|
-
getArgs({
|
450
|
-
mode,
|
451
|
-
prompt,
|
452
|
-
maxTokens,
|
453
|
-
temperature,
|
454
|
-
topP,
|
455
|
-
frequencyPenalty,
|
456
|
-
presencePenalty,
|
457
|
-
seed
|
458
|
-
}) {
|
459
|
-
var _a;
|
460
|
-
const type = mode.type;
|
461
|
-
const warnings = [];
|
462
|
-
if (frequencyPenalty != null) {
|
463
|
-
warnings.push({
|
464
|
-
type: "unsupported-setting",
|
465
|
-
setting: "frequencyPenalty"
|
466
|
-
});
|
467
|
-
}
|
468
|
-
if (presencePenalty != null) {
|
469
|
-
warnings.push({
|
470
|
-
type: "unsupported-setting",
|
471
|
-
setting: "presencePenalty"
|
472
|
-
});
|
473
|
-
}
|
474
|
-
const baseArgs = {
|
475
|
-
// model id:
|
476
|
-
model: this.modelId,
|
477
|
-
// model specific settings:
|
478
|
-
safe_prompt: this.settings.safePrompt,
|
479
|
-
// standardized settings:
|
480
|
-
max_tokens: maxTokens,
|
481
|
-
temperature,
|
482
|
-
// uses 0..1 scale
|
483
|
-
top_p: topP,
|
484
|
-
random_seed: seed,
|
485
|
-
// messages:
|
486
|
-
messages: convertToMistralChatMessages(prompt)
|
487
|
-
};
|
488
|
-
switch (type) {
|
489
|
-
case "regular": {
|
490
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
491
|
-
return {
|
492
|
-
args: {
|
493
|
-
...baseArgs,
|
494
|
-
tools: tools == null ? void 0 : tools.map((tool) => ({
|
495
|
-
type: "function",
|
496
|
-
function: {
|
497
|
-
name: tool.name,
|
498
|
-
description: tool.description,
|
499
|
-
parameters: tool.parameters
|
500
|
-
}
|
501
|
-
}))
|
502
|
-
},
|
503
|
-
warnings
|
504
|
-
};
|
505
|
-
}
|
506
|
-
case "object-json": {
|
507
|
-
return {
|
508
|
-
args: {
|
509
|
-
...baseArgs,
|
510
|
-
response_format: { type: "json_object" }
|
511
|
-
},
|
512
|
-
warnings
|
513
|
-
};
|
514
|
-
}
|
515
|
-
case "object-tool": {
|
516
|
-
return {
|
517
|
-
args: {
|
518
|
-
...baseArgs,
|
519
|
-
tool_choice: "any",
|
520
|
-
tools: [{ type: "function", function: mode.tool }]
|
521
|
-
},
|
522
|
-
warnings
|
523
|
-
};
|
524
|
-
}
|
525
|
-
case "object-grammar": {
|
526
|
-
throw new UnsupportedFunctionalityError2({
|
527
|
-
functionality: "object-grammar mode"
|
528
|
-
});
|
529
|
-
}
|
530
|
-
default: {
|
531
|
-
const _exhaustiveCheck = type;
|
532
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
533
|
-
}
|
534
|
-
}
|
535
|
-
}
|
536
|
-
async doGenerate(options) {
|
537
|
-
var _a, _b;
|
538
|
-
const { args, warnings } = this.getArgs(options);
|
539
|
-
const response = await postJsonToApi({
|
540
|
-
url: `${this.config.baseUrl}/chat/completions`,
|
541
|
-
headers: this.config.headers(),
|
542
|
-
body: args,
|
543
|
-
failedResponseHandler: mistralFailedResponseHandler,
|
544
|
-
successfulResponseHandler: createJsonResponseHandler(
|
545
|
-
mistralChatResponseSchema
|
546
|
-
),
|
547
|
-
abortSignal: options.abortSignal
|
548
|
-
});
|
549
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
550
|
-
const choice = response.choices[0];
|
551
|
-
return {
|
552
|
-
text: (_a = choice.message.content) != null ? _a : void 0,
|
553
|
-
toolCalls: (_b = choice.message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
|
554
|
-
toolCallType: "function",
|
555
|
-
toolCallId: this.config.generateId(),
|
556
|
-
toolName: toolCall.function.name,
|
557
|
-
args: toolCall.function.arguments
|
558
|
-
})),
|
559
|
-
finishReason: mapMistralFinishReason(choice.finish_reason),
|
560
|
-
usage: {
|
561
|
-
promptTokens: response.usage.prompt_tokens,
|
562
|
-
completionTokens: response.usage.completion_tokens
|
563
|
-
},
|
564
|
-
rawCall: { rawPrompt, rawSettings },
|
565
|
-
warnings
|
566
|
-
};
|
567
|
-
}
|
568
|
-
async doStream(options) {
|
569
|
-
const { args, warnings } = this.getArgs(options);
|
570
|
-
const response = await postJsonToApi({
|
571
|
-
url: `${this.config.baseUrl}/chat/completions`,
|
572
|
-
headers: this.config.headers(),
|
573
|
-
body: {
|
574
|
-
...args,
|
575
|
-
stream: true
|
576
|
-
},
|
577
|
-
failedResponseHandler: mistralFailedResponseHandler,
|
578
|
-
successfulResponseHandler: createEventSourceResponseHandler(
|
579
|
-
mistralChatChunkSchema
|
580
|
-
),
|
581
|
-
abortSignal: options.abortSignal
|
582
|
-
});
|
583
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
584
|
-
let finishReason = "other";
|
585
|
-
let usage = {
|
586
|
-
promptTokens: Number.NaN,
|
587
|
-
completionTokens: Number.NaN
|
588
|
-
};
|
589
|
-
const generateId2 = this.config.generateId;
|
590
|
-
return {
|
591
|
-
stream: response.pipeThrough(
|
592
|
-
new TransformStream({
|
593
|
-
transform(chunk, controller) {
|
594
|
-
if (!chunk.success) {
|
595
|
-
controller.enqueue({ type: "error", error: chunk.error });
|
596
|
-
return;
|
597
|
-
}
|
598
|
-
const value = chunk.value;
|
599
|
-
if (value.usage != null) {
|
600
|
-
usage = {
|
601
|
-
promptTokens: value.usage.prompt_tokens,
|
602
|
-
completionTokens: value.usage.completion_tokens
|
603
|
-
};
|
604
|
-
}
|
605
|
-
const choice = value.choices[0];
|
606
|
-
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
607
|
-
finishReason = mapMistralFinishReason(choice.finish_reason);
|
608
|
-
}
|
609
|
-
if ((choice == null ? void 0 : choice.delta) == null) {
|
610
|
-
return;
|
611
|
-
}
|
612
|
-
const delta = choice.delta;
|
613
|
-
if (delta.content != null) {
|
614
|
-
controller.enqueue({
|
615
|
-
type: "text-delta",
|
616
|
-
textDelta: delta.content
|
617
|
-
});
|
618
|
-
}
|
619
|
-
if (delta.tool_calls != null) {
|
620
|
-
for (const toolCall of delta.tool_calls) {
|
621
|
-
const toolCallId = generateId2();
|
622
|
-
controller.enqueue({
|
623
|
-
type: "tool-call-delta",
|
624
|
-
toolCallType: "function",
|
625
|
-
toolCallId,
|
626
|
-
toolName: toolCall.function.name,
|
627
|
-
argsTextDelta: toolCall.function.arguments
|
628
|
-
});
|
629
|
-
controller.enqueue({
|
630
|
-
type: "tool-call",
|
631
|
-
toolCallType: "function",
|
632
|
-
toolCallId,
|
633
|
-
toolName: toolCall.function.name,
|
634
|
-
args: toolCall.function.arguments
|
635
|
-
});
|
636
|
-
}
|
637
|
-
}
|
638
|
-
},
|
639
|
-
flush(controller) {
|
640
|
-
controller.enqueue({ type: "finish", finishReason, usage });
|
641
|
-
}
|
642
|
-
})
|
643
|
-
),
|
644
|
-
rawCall: { rawPrompt, rawSettings },
|
645
|
-
warnings
|
646
|
-
};
|
647
|
-
}
|
648
|
-
};
|
649
|
-
var mistralChatResponseSchema = z2.object({
|
650
|
-
choices: z2.array(
|
651
|
-
z2.object({
|
652
|
-
message: z2.object({
|
653
|
-
role: z2.literal("assistant"),
|
654
|
-
content: z2.string().nullable(),
|
655
|
-
tool_calls: z2.array(
|
656
|
-
z2.object({
|
657
|
-
function: z2.object({
|
658
|
-
name: z2.string(),
|
659
|
-
arguments: z2.string()
|
660
|
-
})
|
661
|
-
})
|
662
|
-
).optional().nullable()
|
663
|
-
}),
|
664
|
-
index: z2.number(),
|
665
|
-
finish_reason: z2.string().optional().nullable()
|
666
|
-
})
|
667
|
-
),
|
668
|
-
object: z2.literal("chat.completion"),
|
669
|
-
usage: z2.object({
|
670
|
-
prompt_tokens: z2.number(),
|
671
|
-
completion_tokens: z2.number()
|
672
|
-
})
|
673
|
-
});
|
674
|
-
var mistralChatChunkSchema = z2.object({
|
675
|
-
object: z2.literal("chat.completion.chunk"),
|
676
|
-
choices: z2.array(
|
677
|
-
z2.object({
|
678
|
-
delta: z2.object({
|
679
|
-
role: z2.enum(["assistant"]).optional(),
|
680
|
-
content: z2.string().nullable().optional(),
|
681
|
-
tool_calls: z2.array(
|
682
|
-
z2.object({
|
683
|
-
function: z2.object({ name: z2.string(), arguments: z2.string() })
|
684
|
-
})
|
685
|
-
).optional().nullable()
|
686
|
-
}),
|
687
|
-
finish_reason: z2.string().nullable().optional(),
|
688
|
-
index: z2.number()
|
689
|
-
})
|
690
|
-
),
|
691
|
-
usage: z2.object({
|
692
|
-
prompt_tokens: z2.number(),
|
693
|
-
completion_tokens: z2.number()
|
694
|
-
}).optional().nullable()
|
695
|
-
});
|
696
|
-
|
697
|
-
// mistral/mistral-facade.ts
|
698
|
-
var Mistral = class {
|
699
|
-
constructor(options = {}) {
|
700
|
-
var _a;
|
701
|
-
this.baseUrl = options.baseUrl;
|
702
|
-
this.apiKey = options.apiKey;
|
703
|
-
this.generateId = (_a = options.generateId) != null ? _a : generateId;
|
704
|
-
}
|
705
|
-
get baseConfig() {
|
706
|
-
var _a;
|
707
|
-
return {
|
708
|
-
baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.mistral.ai/v1",
|
709
|
-
headers: () => ({
|
710
|
-
Authorization: `Bearer ${loadApiKey({
|
711
|
-
apiKey: this.apiKey,
|
712
|
-
environmentVariableName: "MISTRAL_API_KEY",
|
713
|
-
description: "Mistral"
|
714
|
-
})}`
|
715
|
-
})
|
716
|
-
};
|
717
|
-
}
|
718
|
-
chat(modelId, settings = {}) {
|
719
|
-
return new MistralChatLanguageModel(modelId, settings, {
|
720
|
-
provider: "mistral.chat",
|
721
|
-
...this.baseConfig,
|
722
|
-
generateId: this.generateId
|
723
|
-
});
|
724
|
-
}
|
725
|
-
};
|
726
|
-
var mistral = new Mistral();
|
727
|
-
export {
|
728
|
-
Mistral,
|
729
|
-
mistral
|
730
|
-
};
|
731
|
-
//# sourceMappingURL=index.mjs.map
|