ai 3.1.0-canary.2 → 3.1.0-canary.3
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/ai-model-specification/dist/index.d.mts +76 -9
- package/ai-model-specification/dist/index.d.ts +76 -9
- package/ai-model-specification/dist/index.js +52 -16
- package/ai-model-specification/dist/index.js.map +1 -1
- package/ai-model-specification/dist/index.mjs +49 -15
- package/ai-model-specification/dist/index.mjs.map +1 -1
- package/core/dist/index.d.mts +79 -14
- package/core/dist/index.d.ts +79 -14
- package/core/dist/index.js +246 -338
- package/core/dist/index.js.map +1 -1
- package/core/dist/index.mjs +246 -337
- package/core/dist/index.mjs.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/provider/dist/index.d.mts +124 -45
- package/provider/dist/index.d.ts +124 -45
- package/provider/dist/index.js +131 -93
- package/provider/dist/index.js.map +1 -1
- package/provider/dist/index.mjs +129 -92
- package/provider/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +4 -4
- package/react/dist/index.d.ts +4 -4
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs.map +1 -1
package/provider/dist/index.js
CHANGED
@@ -30,12 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
30
30
|
// provider/index.ts
|
31
31
|
var provider_exports = {};
|
32
32
|
__export(provider_exports, {
|
33
|
-
OpenAI: () => OpenAI
|
33
|
+
OpenAI: () => OpenAI,
|
34
|
+
openai: () => openai
|
34
35
|
});
|
35
36
|
module.exports = __toCommonJS(provider_exports);
|
36
37
|
|
37
38
|
// ai-model-specification/errors/api-call-error.ts
|
38
|
-
var
|
39
|
+
var APICallError = class extends Error {
|
39
40
|
constructor({
|
40
41
|
message,
|
41
42
|
url,
|
@@ -43,7 +44,11 @@ var ApiCallError = class extends Error {
|
|
43
44
|
statusCode,
|
44
45
|
responseBody,
|
45
46
|
cause,
|
46
|
-
isRetryable = statusCode != null && (statusCode ===
|
47
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
48
|
+
statusCode === 409 || // conflict
|
49
|
+
statusCode === 429 || // too many requests
|
50
|
+
statusCode >= 500),
|
51
|
+
// server error
|
47
52
|
data
|
48
53
|
}) {
|
49
54
|
super(message);
|
@@ -291,8 +296,8 @@ var postJsonToApi = async ({
|
|
291
296
|
}) => postToApi({
|
292
297
|
url,
|
293
298
|
headers: {
|
294
|
-
|
295
|
-
|
299
|
+
...headers,
|
300
|
+
"Content-Type": "application/json"
|
296
301
|
},
|
297
302
|
body: {
|
298
303
|
content: JSON.stringify(body),
|
@@ -311,9 +316,12 @@ var postToApi = async ({
|
|
311
316
|
abortSignal
|
312
317
|
}) => {
|
313
318
|
try {
|
319
|
+
const definedHeaders = Object.fromEntries(
|
320
|
+
Object.entries(headers).filter(([_key, value]) => value != null)
|
321
|
+
);
|
314
322
|
const response = await fetch(url, {
|
315
323
|
method: "POST",
|
316
|
-
headers,
|
324
|
+
headers: definedHeaders,
|
317
325
|
body: body.content,
|
318
326
|
signal: abortSignal
|
319
327
|
});
|
@@ -326,11 +334,11 @@ var postToApi = async ({
|
|
326
334
|
});
|
327
335
|
} catch (error) {
|
328
336
|
if (error instanceof Error) {
|
329
|
-
if (error.name === "AbortError" || error instanceof
|
337
|
+
if (error.name === "AbortError" || error instanceof APICallError) {
|
330
338
|
throw error;
|
331
339
|
}
|
332
340
|
}
|
333
|
-
throw new
|
341
|
+
throw new APICallError({
|
334
342
|
message: "Failed to process error response",
|
335
343
|
cause: error,
|
336
344
|
statusCode: response.status,
|
@@ -347,11 +355,11 @@ var postToApi = async ({
|
|
347
355
|
});
|
348
356
|
} catch (error) {
|
349
357
|
if (error instanceof Error) {
|
350
|
-
if (error.name === "AbortError" || error instanceof
|
358
|
+
if (error.name === "AbortError" || error instanceof APICallError) {
|
351
359
|
throw error;
|
352
360
|
}
|
353
361
|
}
|
354
|
-
throw new
|
362
|
+
throw new APICallError({
|
355
363
|
message: "Failed to process successful response",
|
356
364
|
cause: error,
|
357
365
|
statusCode: response.status,
|
@@ -368,12 +376,13 @@ var postToApi = async ({
|
|
368
376
|
if (error instanceof TypeError && error.message === "fetch failed") {
|
369
377
|
const cause = error.cause;
|
370
378
|
if (cause != null) {
|
371
|
-
throw new
|
379
|
+
throw new APICallError({
|
372
380
|
message: `Cannot connect to API: ${cause.message}`,
|
373
381
|
cause,
|
374
382
|
url,
|
375
383
|
requestBodyValues: body.values,
|
376
384
|
isRetryable: true
|
385
|
+
// retry when network error
|
377
386
|
});
|
378
387
|
}
|
379
388
|
}
|
@@ -390,7 +399,7 @@ var createJsonErrorResponseHandler = ({
|
|
390
399
|
}) => async ({ response, url, requestBodyValues }) => {
|
391
400
|
const responseBody = await response.text();
|
392
401
|
if (responseBody.trim() === "") {
|
393
|
-
return new
|
402
|
+
return new APICallError({
|
394
403
|
message: response.statusText,
|
395
404
|
url,
|
396
405
|
requestBodyValues,
|
@@ -404,7 +413,7 @@ var createJsonErrorResponseHandler = ({
|
|
404
413
|
text: responseBody,
|
405
414
|
schema: errorSchema
|
406
415
|
});
|
407
|
-
return new
|
416
|
+
return new APICallError({
|
408
417
|
message: errorToMessage(parsedError),
|
409
418
|
url,
|
410
419
|
requestBodyValues,
|
@@ -414,7 +423,7 @@ var createJsonErrorResponseHandler = ({
|
|
414
423
|
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
415
424
|
});
|
416
425
|
} catch (parseError) {
|
417
|
-
return new
|
426
|
+
return new APICallError({
|
418
427
|
message: response.statusText,
|
419
428
|
url,
|
420
429
|
requestBodyValues,
|
@@ -452,7 +461,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
452
461
|
schema: responseSchema
|
453
462
|
});
|
454
463
|
if (!parsedResult.success) {
|
455
|
-
throw new
|
464
|
+
throw new APICallError({
|
456
465
|
message: "Invalid JSON response",
|
457
466
|
cause: parsedResult.error,
|
458
467
|
statusCode: response.status,
|
@@ -515,7 +524,7 @@ function convertToOpenAIChatMessages(prompt) {
|
|
515
524
|
return {
|
516
525
|
type: "image_url",
|
517
526
|
image_url: {
|
518
|
-
url: `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
|
527
|
+
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
|
519
528
|
}
|
520
529
|
};
|
521
530
|
}
|
@@ -588,25 +597,38 @@ var openAIErrorDataSchema = import_zod.z.object({
|
|
588
597
|
});
|
589
598
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
590
599
|
errorSchema: openAIErrorDataSchema,
|
591
|
-
errorToMessage: (data) => data.error.message
|
592
|
-
isRetryable: (response, error) => response.status >= 500 || response.status === 429 && // insufficient_quota is also reported as a 429, but it's not retryable:
|
593
|
-
(error == null ? void 0 : error.error.type) !== "insufficient_quota"
|
600
|
+
errorToMessage: (data) => data.error.message
|
594
601
|
});
|
595
602
|
|
603
|
+
// provider/openai/map-openai-finish-reason.ts
|
604
|
+
function mapOpenAIFinishReason(finishReason) {
|
605
|
+
switch (finishReason) {
|
606
|
+
case "stop":
|
607
|
+
return "stop";
|
608
|
+
case "length":
|
609
|
+
return "length";
|
610
|
+
case "content-filter":
|
611
|
+
return "content-filter";
|
612
|
+
case "function_call":
|
613
|
+
case "tool-calls":
|
614
|
+
return "tool-calls";
|
615
|
+
default:
|
616
|
+
return "other";
|
617
|
+
}
|
618
|
+
}
|
619
|
+
|
596
620
|
// provider/openai/openai-chat-language-model.ts
|
597
621
|
var OpenAIChatLanguageModel = class {
|
598
|
-
constructor(settings, config) {
|
622
|
+
constructor(modelId, settings, config) {
|
599
623
|
this.specificationVersion = "v1";
|
600
624
|
this.defaultObjectGenerationMode = "tool";
|
625
|
+
this.modelId = modelId;
|
601
626
|
this.settings = settings;
|
602
627
|
this.config = config;
|
603
628
|
}
|
604
629
|
get provider() {
|
605
630
|
return this.config.provider;
|
606
631
|
}
|
607
|
-
get modelId() {
|
608
|
-
return this.settings.id;
|
609
|
-
}
|
610
632
|
getArgs({
|
611
633
|
mode,
|
612
634
|
prompt,
|
@@ -620,8 +642,11 @@ var OpenAIChatLanguageModel = class {
|
|
620
642
|
var _a;
|
621
643
|
const type = mode.type;
|
622
644
|
const baseArgs = {
|
645
|
+
// model id:
|
646
|
+
model: this.modelId,
|
623
647
|
// model specific settings:
|
624
|
-
|
648
|
+
logit_bias: this.settings.logitBias,
|
649
|
+
user: this.settings.user,
|
625
650
|
// standardized settings:
|
626
651
|
max_tokens: maxTokens,
|
627
652
|
temperature: scale({
|
@@ -690,49 +715,54 @@ var OpenAIChatLanguageModel = class {
|
|
690
715
|
}
|
691
716
|
async doGenerate(options) {
|
692
717
|
var _a, _b;
|
718
|
+
const args = this.getArgs(options);
|
693
719
|
const response = await postJsonToApi({
|
694
720
|
url: `${this.config.baseUrl}/chat/completions`,
|
695
|
-
headers:
|
696
|
-
|
697
|
-
},
|
698
|
-
body: {
|
699
|
-
...this.getArgs(options)
|
700
|
-
},
|
721
|
+
headers: this.config.headers(),
|
722
|
+
body: args,
|
701
723
|
failedResponseHandler: openaiFailedResponseHandler,
|
702
724
|
successfulResponseHandler: createJsonResponseHandler(
|
703
725
|
openAIChatResponseSchema
|
704
|
-
)
|
726
|
+
),
|
727
|
+
abortSignal: options.abortSignal
|
705
728
|
});
|
706
|
-
const
|
729
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
730
|
+
const choice = response.choices[0];
|
707
731
|
return {
|
708
|
-
text: (_a = message.content) != null ? _a : void 0,
|
709
|
-
toolCalls: (_b = message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
|
732
|
+
text: (_a = choice.message.content) != null ? _a : void 0,
|
733
|
+
toolCalls: (_b = choice.message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
|
710
734
|
toolCallType: "function",
|
711
735
|
toolCallId: toolCall.id,
|
712
736
|
toolName: toolCall.function.name,
|
713
737
|
args: toolCall.function.arguments
|
714
738
|
})),
|
739
|
+
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
740
|
+
usage: {
|
741
|
+
promptTokens: response.usage.prompt_tokens,
|
742
|
+
completionTokens: response.usage.completion_tokens
|
743
|
+
},
|
744
|
+
rawCall: { rawPrompt, rawSettings },
|
715
745
|
warnings: []
|
716
746
|
};
|
717
747
|
}
|
718
748
|
async doStream(options) {
|
749
|
+
const args = this.getArgs(options);
|
719
750
|
const response = await postJsonToApi({
|
720
751
|
url: `${this.config.baseUrl}/chat/completions`,
|
721
|
-
headers:
|
722
|
-
Authorization: `Bearer ${this.config.apiKey()}`
|
723
|
-
},
|
752
|
+
headers: this.config.headers(),
|
724
753
|
body: {
|
725
|
-
...
|
754
|
+
...args,
|
726
755
|
stream: true
|
727
756
|
},
|
728
757
|
failedResponseHandler: openaiFailedResponseHandler,
|
729
758
|
successfulResponseHandler: createEventSourceResponseHandler(
|
730
759
|
openaiChatChunkSchema
|
731
|
-
)
|
760
|
+
),
|
761
|
+
abortSignal: options.abortSignal
|
732
762
|
});
|
763
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
733
764
|
const toolCalls = [];
|
734
765
|
return {
|
735
|
-
warnings: [],
|
736
766
|
stream: response.pipeThrough(
|
737
767
|
new TransformStream({
|
738
768
|
transform(chunk, controller) {
|
@@ -786,7 +816,9 @@ var OpenAIChatLanguageModel = class {
|
|
786
816
|
}
|
787
817
|
}
|
788
818
|
})
|
789
|
-
)
|
819
|
+
),
|
820
|
+
rawCall: { rawPrompt, rawSettings },
|
821
|
+
warnings: []
|
790
822
|
};
|
791
823
|
}
|
792
824
|
};
|
@@ -932,18 +964,16 @@ ${user}:`]
|
|
932
964
|
|
933
965
|
// provider/openai/openai-completion-language-model.ts
|
934
966
|
var OpenAICompletionLanguageModel = class {
|
935
|
-
constructor(settings, config) {
|
967
|
+
constructor(modelId, settings, config) {
|
936
968
|
this.specificationVersion = "v1";
|
937
969
|
this.defaultObjectGenerationMode = void 0;
|
970
|
+
this.modelId = modelId;
|
938
971
|
this.settings = settings;
|
939
972
|
this.config = config;
|
940
973
|
}
|
941
974
|
get provider() {
|
942
975
|
return this.config.provider;
|
943
976
|
}
|
944
|
-
get modelId() {
|
945
|
-
return this.settings.id;
|
946
|
-
}
|
947
977
|
getArgs({
|
948
978
|
mode,
|
949
979
|
inputFormat,
|
@@ -963,8 +993,13 @@ var OpenAICompletionLanguageModel = class {
|
|
963
993
|
provider: this.provider
|
964
994
|
});
|
965
995
|
const baseArgs = {
|
996
|
+
// model id:
|
997
|
+
model: this.modelId,
|
966
998
|
// model specific settings:
|
967
|
-
|
999
|
+
echo: this.settings.echo,
|
1000
|
+
logit_bias: this.settings.logitBias,
|
1001
|
+
suffix: this.settings.suffix,
|
1002
|
+
user: this.settings.user,
|
968
1003
|
// standardized settings:
|
969
1004
|
max_tokens: maxTokens,
|
970
1005
|
temperature: scale({
|
@@ -1028,30 +1063,35 @@ var OpenAICompletionLanguageModel = class {
|
|
1028
1063
|
}
|
1029
1064
|
}
|
1030
1065
|
async doGenerate(options) {
|
1066
|
+
const args = this.getArgs(options);
|
1031
1067
|
const response = await postJsonToApi({
|
1032
1068
|
url: `${this.config.baseUrl}/completions`,
|
1033
|
-
headers:
|
1034
|
-
|
1035
|
-
},
|
1036
|
-
body: {
|
1037
|
-
...this.getArgs(options)
|
1038
|
-
},
|
1069
|
+
headers: this.config.headers(),
|
1070
|
+
body: args,
|
1039
1071
|
failedResponseHandler: openaiFailedResponseHandler,
|
1040
1072
|
successfulResponseHandler: createJsonResponseHandler(
|
1041
1073
|
openAICompletionResponseSchema
|
1042
|
-
)
|
1074
|
+
),
|
1075
|
+
abortSignal: options.abortSignal
|
1043
1076
|
});
|
1077
|
+
const { prompt: rawPrompt, ...rawSettings } = args;
|
1078
|
+
const choice = response.choices[0];
|
1044
1079
|
return {
|
1045
|
-
text:
|
1080
|
+
text: choice.text,
|
1081
|
+
usage: {
|
1082
|
+
promptTokens: response.usage.prompt_tokens,
|
1083
|
+
completionTokens: response.usage.completion_tokens
|
1084
|
+
},
|
1085
|
+
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
1086
|
+
rawCall: { rawPrompt, rawSettings },
|
1046
1087
|
warnings: []
|
1047
1088
|
};
|
1048
1089
|
}
|
1049
1090
|
async doStream(options) {
|
1091
|
+
const args = this.getArgs(options);
|
1050
1092
|
const response = await postJsonToApi({
|
1051
1093
|
url: `${this.config.baseUrl}/completions`,
|
1052
|
-
headers:
|
1053
|
-
Authorization: `Bearer ${this.config.apiKey()}`
|
1054
|
-
},
|
1094
|
+
headers: this.config.headers(),
|
1055
1095
|
body: {
|
1056
1096
|
...this.getArgs(options),
|
1057
1097
|
stream: true
|
@@ -1059,10 +1099,11 @@ var OpenAICompletionLanguageModel = class {
|
|
1059
1099
|
failedResponseHandler: openaiFailedResponseHandler,
|
1060
1100
|
successfulResponseHandler: createEventSourceResponseHandler(
|
1061
1101
|
openaiCompletionChunkSchema
|
1062
|
-
)
|
1102
|
+
),
|
1103
|
+
abortSignal: options.abortSignal
|
1063
1104
|
});
|
1105
|
+
const { prompt: rawPrompt, ...rawSettings } = args;
|
1064
1106
|
return {
|
1065
|
-
warnings: [],
|
1066
1107
|
stream: response.pipeThrough(
|
1067
1108
|
new TransformStream({
|
1068
1109
|
transform(chunk, controller) {
|
@@ -1080,7 +1121,9 @@ var OpenAICompletionLanguageModel = class {
|
|
1080
1121
|
}
|
1081
1122
|
}
|
1082
1123
|
})
|
1083
|
-
)
|
1124
|
+
),
|
1125
|
+
rawCall: { rawPrompt, rawSettings },
|
1126
|
+
warnings: []
|
1084
1127
|
};
|
1085
1128
|
}
|
1086
1129
|
};
|
@@ -1109,48 +1152,43 @@ var openaiCompletionChunkSchema = import_zod3.z.object({
|
|
1109
1152
|
|
1110
1153
|
// provider/openai/openai-facade.ts
|
1111
1154
|
var OpenAI = class {
|
1112
|
-
constructor(
|
1113
|
-
this.baseUrl = baseUrl;
|
1114
|
-
this.apiKey = apiKey;
|
1155
|
+
constructor(options = {}) {
|
1156
|
+
this.baseUrl = options.baseUrl;
|
1157
|
+
this.apiKey = options.apiKey;
|
1158
|
+
this.organization = options.organization;
|
1115
1159
|
}
|
1116
|
-
|
1160
|
+
get baseConfig() {
|
1117
1161
|
var _a;
|
1118
|
-
return
|
1119
|
-
|
1162
|
+
return {
|
1163
|
+
organization: this.organization,
|
1120
1164
|
baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.openai.com/v1",
|
1121
|
-
|
1122
|
-
|
1123
|
-
environmentVariableName: "OPENAI_API_KEY",
|
1124
|
-
description: "OpenAI"
|
1125
|
-
}),
|
1126
|
-
mapSettings: (settings2) => ({
|
1127
|
-
model: settings2.id,
|
1128
|
-
logit_bias: settings2.logitBias
|
1129
|
-
})
|
1130
|
-
});
|
1131
|
-
}
|
1132
|
-
completion(settings) {
|
1133
|
-
var _a;
|
1134
|
-
return new OpenAICompletionLanguageModel(
|
1135
|
-
settings,
|
1136
|
-
{
|
1137
|
-
provider: "openai.completion",
|
1138
|
-
baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.openai.com/v1",
|
1139
|
-
apiKey: () => loadApiKey({
|
1165
|
+
headers: () => ({
|
1166
|
+
Authorization: `Bearer ${loadApiKey({
|
1140
1167
|
apiKey: this.apiKey,
|
1141
1168
|
environmentVariableName: "OPENAI_API_KEY",
|
1142
1169
|
description: "OpenAI"
|
1143
|
-
})
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1170
|
+
})}`,
|
1171
|
+
"OpenAI-Organization": this.organization
|
1172
|
+
})
|
1173
|
+
};
|
1174
|
+
}
|
1175
|
+
chat(modelId, settings = {}) {
|
1176
|
+
return new OpenAIChatLanguageModel(modelId, settings, {
|
1177
|
+
provider: "openai.chat",
|
1178
|
+
...this.baseConfig
|
1179
|
+
});
|
1180
|
+
}
|
1181
|
+
completion(modelId, settings = {}) {
|
1182
|
+
return new OpenAICompletionLanguageModel(modelId, settings, {
|
1183
|
+
provider: "openai.completion",
|
1184
|
+
...this.baseConfig
|
1185
|
+
});
|
1150
1186
|
}
|
1151
1187
|
};
|
1188
|
+
var openai = new OpenAI();
|
1152
1189
|
// Annotate the CommonJS export names for ESM import in node:
|
1153
1190
|
0 && (module.exports = {
|
1154
|
-
OpenAI
|
1191
|
+
OpenAI,
|
1192
|
+
openai
|
1155
1193
|
});
|
1156
1194
|
//# sourceMappingURL=index.js.map
|