@ai-sdk/openai 4.0.108 → 4.0.110
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/CHANGELOG.md +54 -0
- package/dist/index.d.ts +79 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +788 -423
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +389 -9
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +629 -380
- package/dist/internal/index.js.map +1 -1
- package/docs/03-openai.mdx +224 -18
- package/package.json +3 -3
- package/src/chat/convert-to-openai-chat-messages.ts +29 -4
- package/src/chat/map-openai-finish-reason.ts +11 -4
- package/src/chat/openai-chat-api.ts +36 -0
- package/src/chat/openai-chat-language-model-options.ts +13 -0
- package/src/chat/openai-chat-language-model.ts +111 -22
- package/src/chat/openai-chat-prompt.ts +4 -0
- package/src/image/openai-image-model-options.ts +48 -0
- package/src/image/openai-image-model.ts +49 -4
- package/src/index.ts +1 -0
- package/src/internal/index.ts +4 -0
- package/src/live/openai-realtime-model-live.ts +21 -2
- package/src/openai-batch.ts +8 -0
- package/src/openai-model-catalog.ts +3 -1
- package/src/openai-tools.ts +4 -3
- package/src/realtime/openai-realtime-event-mapper.ts +172 -11
- package/src/realtime/openai-realtime-model.ts +57 -3
- package/src/responses/map-openai-responses-finish-reason.ts +7 -1
- package/src/responses/openai-responses-api.ts +90 -49
- package/src/responses/openai-responses-language-model.ts +56 -1
- package/src/responses/openai-responses-prepare-tools.ts +18 -1
- package/src/tool/code-interpreter.ts +38 -2
- package/src/tool/mcp.ts +27 -8
- package/src/transcription/openai-transcription-model.ts +43 -8
package/dist/internal/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APICallError, InvalidPromptError, InvalidResponseDataError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError, isJSONObject } from "@ai-sdk/provider";
|
|
1
|
+
import { APICallError, InvalidArgumentError, InvalidPromptError, InvalidResponseDataError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError, isJSONObject } from "@ai-sdk/provider";
|
|
2
2
|
import { EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, connectToWebSocket, convertBase64ToUint8Array, convertToBase64, convertToFormData, createBinaryResponseHandler, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createProviderExecutedToolFactory, createProviderStreamError, createToolNameMapping, downloadBlob, experimental_toolCaller, generateId, getTopLevelMediaType, isCustomReasoning, isNonNullable, isRecord, lazySchema, mapReasoningToProviderEffort, mediaTypeToExtension, parseJSON, parseProviderOptions, postFormDataToApi, postJsonToApi, resolve, resolveFullMediaType, resolveProviderReference, safeParseJSON, safeValidateTypes, secureJsonParse, serializeModelOptions, stringifyWithBigIntAndCycles, toWebSocketUrl, validateTypes, waitForWebSocketBufferDrain, zodSchema } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { z } from "zod/v4";
|
|
4
4
|
//#region src/openai-error.ts
|
|
@@ -263,6 +263,8 @@ function createGptImageEntry(sizes) {
|
|
|
263
263
|
};
|
|
264
264
|
}
|
|
265
265
|
const openaiImageModelEntries = {
|
|
266
|
+
"gpt-image-2.6-flare": createGptImageEntry(customImageSizes),
|
|
267
|
+
"gpt-image-2.6-sunburst": createGptImageEntry(customImageSizes),
|
|
266
268
|
"gpt-image-2.5-flare": createGptImageEntry(customImageSizes),
|
|
267
269
|
"gpt-image-2.5-sunburst": createGptImageEntry(customImageSizes),
|
|
268
270
|
"gpt-image-2": createGptImageEntry(gptImageSizes),
|
|
@@ -453,7 +455,7 @@ const realtimeModelEntries = {
|
|
|
453
455
|
capabilities: { realtime: {
|
|
454
456
|
conversation: "turn-based",
|
|
455
457
|
transports: ["websocket"],
|
|
456
|
-
connections: ["client-secret-websocket"]
|
|
458
|
+
connections: ["client-secret-websocket", "server-websocket"]
|
|
457
459
|
} },
|
|
458
460
|
requestConfig: { realtime: {
|
|
459
461
|
api: "realtime",
|
|
@@ -716,6 +718,158 @@ function convertOpenAIChatUsage(usage) {
|
|
|
716
718
|
};
|
|
717
719
|
}
|
|
718
720
|
//#endregion
|
|
721
|
+
//#region src/chat/openai-chat-language-model-options.ts
|
|
722
|
+
const openaiLanguageModelChatOptions = lazySchema(() => zodSchema(z.object({
|
|
723
|
+
/**
|
|
724
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
725
|
+
*
|
|
726
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
727
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
728
|
+
*/
|
|
729
|
+
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
730
|
+
/**
|
|
731
|
+
* Return the log probabilities of the tokens.
|
|
732
|
+
*
|
|
733
|
+
* Setting to true will return the log probabilities of the tokens that
|
|
734
|
+
* were generated.
|
|
735
|
+
*
|
|
736
|
+
* Setting to a number will return the log probabilities of the top n
|
|
737
|
+
* tokens that were generated.
|
|
738
|
+
*/
|
|
739
|
+
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
|
740
|
+
/**
|
|
741
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
742
|
+
*/
|
|
743
|
+
parallelToolCalls: z.boolean().optional(),
|
|
744
|
+
/**
|
|
745
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
746
|
+
* monitor and detect abuse.
|
|
747
|
+
*/
|
|
748
|
+
user: z.string().optional(),
|
|
749
|
+
/**
|
|
750
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
751
|
+
*/
|
|
752
|
+
reasoningEffort: z.enum([
|
|
753
|
+
"none",
|
|
754
|
+
"minimal",
|
|
755
|
+
"low",
|
|
756
|
+
"medium",
|
|
757
|
+
"high",
|
|
758
|
+
"xhigh",
|
|
759
|
+
"max"
|
|
760
|
+
]).optional(),
|
|
761
|
+
/**
|
|
762
|
+
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
763
|
+
*/
|
|
764
|
+
maxCompletionTokens: z.number().optional(),
|
|
765
|
+
/**
|
|
766
|
+
* Whether to enable persistence in responses API.
|
|
767
|
+
*/
|
|
768
|
+
store: z.boolean().optional(),
|
|
769
|
+
/**
|
|
770
|
+
* Metadata to associate with the request.
|
|
771
|
+
*/
|
|
772
|
+
metadata: z.record(z.string().max(64), z.string().max(512)).optional(),
|
|
773
|
+
/**
|
|
774
|
+
* Parameters for prediction mode.
|
|
775
|
+
*/
|
|
776
|
+
prediction: z.record(z.string(), z.any()).optional(),
|
|
777
|
+
/**
|
|
778
|
+
* Service tier for the request.
|
|
779
|
+
* - 'auto': Default service tier. The request will be processed with the service tier configured in the
|
|
780
|
+
* Project settings. Unless otherwise configured, the Project will use 'default'.
|
|
781
|
+
* - 'flex': 50% cheaper processing at the cost of increased latency. Only available for o3 and o4-mini models.
|
|
782
|
+
* - 'priority': Higher-speed processing with predictably low latency at premium cost. Available for Enterprise customers.
|
|
783
|
+
* - 'fast': OpenAI's newer name for the 'priority' tier. Interchangeable with it.
|
|
784
|
+
* - 'ultrafast': Access-controlled Ultrafast processing. Only available for gpt-5.6-sol.
|
|
785
|
+
* - 'default': The request will be processed with the standard pricing and performance for the selected model.
|
|
786
|
+
*
|
|
787
|
+
* @default 'auto'
|
|
788
|
+
*/
|
|
789
|
+
serviceTier: z.enum([
|
|
790
|
+
"auto",
|
|
791
|
+
"flex",
|
|
792
|
+
"priority",
|
|
793
|
+
"fast",
|
|
794
|
+
"ultrafast",
|
|
795
|
+
"default"
|
|
796
|
+
]).optional(),
|
|
797
|
+
/**
|
|
798
|
+
* Whether to use strict JSON schema validation.
|
|
799
|
+
*
|
|
800
|
+
* @default true
|
|
801
|
+
*/
|
|
802
|
+
strictJsonSchema: z.boolean().optional(),
|
|
803
|
+
/**
|
|
804
|
+
* Controls the verbosity of the model's responses.
|
|
805
|
+
* Lower values will result in more concise responses, while higher values will result in more verbose responses.
|
|
806
|
+
*/
|
|
807
|
+
textVerbosity: z.enum([
|
|
808
|
+
"low",
|
|
809
|
+
"medium",
|
|
810
|
+
"high"
|
|
811
|
+
]).optional(),
|
|
812
|
+
/**
|
|
813
|
+
* A cache key for prompt caching. Allows manual control over prompt caching behavior.
|
|
814
|
+
* Useful for improving cache hit rates and working around automatic caching issues.
|
|
815
|
+
*/
|
|
816
|
+
promptCacheKey: z.string().optional(),
|
|
817
|
+
/**
|
|
818
|
+
* Prompt cache behavior for GPT-5.6 and later models.
|
|
819
|
+
* `mode` controls whether OpenAI also places an implicit breakpoint.
|
|
820
|
+
* `ttl` sets the minimum cache lifetime and currently only supports 30 minutes.
|
|
821
|
+
*/
|
|
822
|
+
promptCacheOptions: z.object({
|
|
823
|
+
mode: z.enum(["implicit", "explicit"]).optional(),
|
|
824
|
+
ttl: z.literal("30m").optional()
|
|
825
|
+
}).optional(),
|
|
826
|
+
/**
|
|
827
|
+
* The retention policy for the prompt cache.
|
|
828
|
+
* - 'in_memory': Default. Standard prompt caching behavior.
|
|
829
|
+
* - '24h': Extended prompt caching that keeps cached prefixes active for up to 24 hours.
|
|
830
|
+
* Available for models before GPT-5.6 that support extended caching.
|
|
831
|
+
*
|
|
832
|
+
* @deprecated For GPT-5.6 and later models, use `promptCacheOptions.ttl`.
|
|
833
|
+
*
|
|
834
|
+
* @default 'in_memory'
|
|
835
|
+
*/
|
|
836
|
+
promptCacheRetention: z.enum(["in_memory", "24h"]).optional(),
|
|
837
|
+
/**
|
|
838
|
+
* A stable identifier used to help detect users of your application
|
|
839
|
+
* that may be violating OpenAI's usage policies. The IDs should be a
|
|
840
|
+
* string that uniquely identifies each user. We recommend hashing their
|
|
841
|
+
* username or email address, in order to avoid sending us any identifying
|
|
842
|
+
* information.
|
|
843
|
+
*/
|
|
844
|
+
safetyIdentifier: z.string().optional(),
|
|
845
|
+
/**
|
|
846
|
+
* Override the system message mode for this model.
|
|
847
|
+
* - 'system': Use the 'system' role for system messages (default for most models)
|
|
848
|
+
* - 'developer': Use the 'developer' role for system messages (used by reasoning models)
|
|
849
|
+
* - 'remove': Remove system messages entirely
|
|
850
|
+
*
|
|
851
|
+
* If not specified, the mode is automatically determined based on the model.
|
|
852
|
+
*/
|
|
853
|
+
systemMessageMode: z.enum([
|
|
854
|
+
"system",
|
|
855
|
+
"developer",
|
|
856
|
+
"remove"
|
|
857
|
+
]).optional(),
|
|
858
|
+
/**
|
|
859
|
+
* Force treating this model as a reasoning model.
|
|
860
|
+
*
|
|
861
|
+
* This is useful for "stealth" reasoning models (e.g. via a custom baseURL)
|
|
862
|
+
* where the model ID is not recognized by the SDK's allowlist.
|
|
863
|
+
*
|
|
864
|
+
* When enabled, the SDK applies reasoning-model parameter compatibility rules
|
|
865
|
+
* and defaults `systemMessageMode` to `developer` unless overridden.
|
|
866
|
+
*/
|
|
867
|
+
forceReasoning: z.boolean().optional()
|
|
868
|
+
})));
|
|
869
|
+
const openaiChatMessageProviderOptions = lazySchema(() => zodSchema(z.object({
|
|
870
|
+
/** The participant name on a system, developer, user, or assistant message. */
|
|
871
|
+
name: z.string().optional() })));
|
|
872
|
+
//#endregion
|
|
719
873
|
//#region src/chat/convert-to-openai-chat-messages.ts
|
|
720
874
|
function serializeToolCallArguments$1(input) {
|
|
721
875
|
if (typeof input === "string") try {
|
|
@@ -730,197 +884,213 @@ function serializeToolCallArguments$1(input) {
|
|
|
730
884
|
function getPromptCacheBreakpoint$1(providerOptions) {
|
|
731
885
|
return providerOptions?.openai?.promptCacheBreakpoint;
|
|
732
886
|
}
|
|
733
|
-
function convertToOpenAIChatMessages({ prompt, systemMessageMode = "system" }) {
|
|
887
|
+
async function convertToOpenAIChatMessages({ prompt, systemMessageMode = "system" }) {
|
|
734
888
|
const messages = [];
|
|
735
889
|
const warnings = [];
|
|
736
|
-
for (const { role, content, providerOptions } of prompt)
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
890
|
+
for (const { role, content, providerOptions } of prompt) {
|
|
891
|
+
const name = (await parseProviderOptions({
|
|
892
|
+
provider: "openai",
|
|
893
|
+
providerOptions,
|
|
894
|
+
schema: openaiChatMessageProviderOptions
|
|
895
|
+
}))?.name;
|
|
896
|
+
switch (role) {
|
|
897
|
+
case "system":
|
|
898
|
+
switch (systemMessageMode) {
|
|
899
|
+
case "system": {
|
|
900
|
+
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(providerOptions);
|
|
901
|
+
messages.push({
|
|
902
|
+
role: "system",
|
|
903
|
+
...name != null && { name },
|
|
904
|
+
content: promptCacheBreakpoint == null ? content : [{
|
|
905
|
+
type: "text",
|
|
906
|
+
text: content,
|
|
907
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
908
|
+
}]
|
|
909
|
+
});
|
|
910
|
+
break;
|
|
911
|
+
}
|
|
912
|
+
case "developer": {
|
|
913
|
+
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(providerOptions);
|
|
914
|
+
messages.push({
|
|
915
|
+
role: "developer",
|
|
916
|
+
...name != null && { name },
|
|
917
|
+
content: promptCacheBreakpoint == null ? content : [{
|
|
918
|
+
type: "text",
|
|
919
|
+
text: content,
|
|
920
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
921
|
+
}]
|
|
922
|
+
});
|
|
923
|
+
break;
|
|
924
|
+
}
|
|
925
|
+
case "remove":
|
|
926
|
+
warnings.push({
|
|
927
|
+
type: "other",
|
|
928
|
+
message: "system messages are removed for this model"
|
|
929
|
+
});
|
|
930
|
+
break;
|
|
931
|
+
default: throw new Error(`Unsupported system message mode: ${systemMessageMode}`);
|
|
750
932
|
}
|
|
751
|
-
|
|
752
|
-
|
|
933
|
+
break;
|
|
934
|
+
case "user":
|
|
935
|
+
if (content.length === 1 && content[0].type === "text" && getPromptCacheBreakpoint$1(content[0].providerOptions) == null) {
|
|
753
936
|
messages.push({
|
|
754
|
-
role: "
|
|
755
|
-
content:
|
|
756
|
-
|
|
757
|
-
text: content,
|
|
758
|
-
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
759
|
-
}]
|
|
937
|
+
role: "user",
|
|
938
|
+
content: content[0].text,
|
|
939
|
+
...name != null && { name }
|
|
760
940
|
});
|
|
761
941
|
break;
|
|
762
942
|
}
|
|
763
|
-
case "remove":
|
|
764
|
-
warnings.push({
|
|
765
|
-
type: "other",
|
|
766
|
-
message: "system messages are removed for this model"
|
|
767
|
-
});
|
|
768
|
-
break;
|
|
769
|
-
default: throw new Error(`Unsupported system message mode: ${systemMessageMode}`);
|
|
770
|
-
}
|
|
771
|
-
break;
|
|
772
|
-
case "user":
|
|
773
|
-
if (content.length === 1 && content[0].type === "text" && getPromptCacheBreakpoint$1(content[0].providerOptions) == null) {
|
|
774
943
|
messages.push({
|
|
775
944
|
role: "user",
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
case "text": {
|
|
785
|
-
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
786
|
-
return {
|
|
787
|
-
type: "text",
|
|
788
|
-
text: part.text,
|
|
789
|
-
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
790
|
-
};
|
|
791
|
-
}
|
|
792
|
-
case "file": {
|
|
793
|
-
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
794
|
-
switch (part.data.type) {
|
|
795
|
-
case "reference": return {
|
|
796
|
-
type: "file",
|
|
797
|
-
file: { file_id: resolveProviderReference({
|
|
798
|
-
reference: part.data.reference,
|
|
799
|
-
provider: "openai"
|
|
800
|
-
}) },
|
|
945
|
+
...name != null && { name },
|
|
946
|
+
content: content.map((part, index) => {
|
|
947
|
+
switch (part.type) {
|
|
948
|
+
case "text": {
|
|
949
|
+
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
950
|
+
return {
|
|
951
|
+
type: "text",
|
|
952
|
+
text: part.text,
|
|
801
953
|
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
802
954
|
};
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
type: "
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
},
|
|
955
|
+
}
|
|
956
|
+
case "file": {
|
|
957
|
+
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
958
|
+
switch (part.data.type) {
|
|
959
|
+
case "reference": return {
|
|
960
|
+
type: "file",
|
|
961
|
+
file: { file_id: resolveProviderReference({
|
|
962
|
+
reference: part.data.reference,
|
|
963
|
+
provider: "openai"
|
|
964
|
+
}) },
|
|
813
965
|
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
814
966
|
};
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
967
|
+
case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
|
|
968
|
+
case "url":
|
|
969
|
+
case "data": {
|
|
970
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
971
|
+
if (topLevel === "image") return {
|
|
972
|
+
type: "image_url",
|
|
973
|
+
image_url: {
|
|
974
|
+
url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`,
|
|
975
|
+
detail: part.providerOptions?.openai?.imageDetail
|
|
976
|
+
},
|
|
977
|
+
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
978
|
+
};
|
|
979
|
+
else if (topLevel === "audio") {
|
|
980
|
+
if (part.data.type === "url") throw new UnsupportedFunctionalityError({ functionality: "audio file parts with URLs" });
|
|
981
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
982
|
+
switch (fullMediaType) {
|
|
983
|
+
case "audio/wav": return {
|
|
984
|
+
type: "input_audio",
|
|
985
|
+
input_audio: {
|
|
986
|
+
data: convertToBase64(part.data.data),
|
|
987
|
+
format: "wav"
|
|
988
|
+
},
|
|
989
|
+
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
990
|
+
};
|
|
991
|
+
case "audio/mp3":
|
|
992
|
+
case "audio/mpeg": return {
|
|
993
|
+
type: "input_audio",
|
|
994
|
+
input_audio: {
|
|
995
|
+
data: convertToBase64(part.data.data),
|
|
996
|
+
format: "mp3"
|
|
997
|
+
},
|
|
998
|
+
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
999
|
+
};
|
|
1000
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `audio content parts with media type ${fullMediaType}` });
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
{
|
|
1004
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
1005
|
+
if (fullMediaType !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: `file part media type ${fullMediaType}` });
|
|
1006
|
+
if (part.data.type === "url") throw new UnsupportedFunctionalityError({ functionality: "PDF file parts with URLs" });
|
|
1007
|
+
return {
|
|
1008
|
+
type: "file",
|
|
1009
|
+
file: {
|
|
1010
|
+
filename: part.filename ?? `part-${index}.pdf`,
|
|
1011
|
+
file_data: `data:application/pdf;base64,${convertToBase64(part.data.data)}`
|
|
833
1012
|
},
|
|
834
1013
|
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
835
1014
|
};
|
|
836
|
-
default: throw new UnsupportedFunctionalityError({ functionality: `audio content parts with media type ${fullMediaType}` });
|
|
837
1015
|
}
|
|
838
1016
|
}
|
|
839
|
-
{
|
|
840
|
-
const fullMediaType = resolveFullMediaType({ part });
|
|
841
|
-
if (fullMediaType !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: `file part media type ${fullMediaType}` });
|
|
842
|
-
if (part.data.type === "url") throw new UnsupportedFunctionalityError({ functionality: "PDF file parts with URLs" });
|
|
843
|
-
return {
|
|
844
|
-
type: "file",
|
|
845
|
-
file: {
|
|
846
|
-
filename: part.filename ?? `part-${index}.pdf`,
|
|
847
|
-
file_data: `data:application/pdf;base64,${convertToBase64(part.data.data)}`
|
|
848
|
-
},
|
|
849
|
-
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
850
|
-
};
|
|
851
|
-
}
|
|
852
1017
|
}
|
|
853
1018
|
}
|
|
854
1019
|
}
|
|
855
|
-
}
|
|
856
|
-
})
|
|
857
|
-
});
|
|
858
|
-
break;
|
|
859
|
-
case "assistant": {
|
|
860
|
-
let text = "";
|
|
861
|
-
const textParts = [];
|
|
862
|
-
let hasPromptCacheBreakpoint = false;
|
|
863
|
-
const toolCalls = [];
|
|
864
|
-
for (const part of content) switch (part.type) {
|
|
865
|
-
case "text": {
|
|
866
|
-
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
867
|
-
text += part.text;
|
|
868
|
-
textParts.push({
|
|
869
|
-
type: "text",
|
|
870
|
-
text: part.text,
|
|
871
|
-
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
872
|
-
});
|
|
873
|
-
hasPromptCacheBreakpoint ||= promptCacheBreakpoint != null;
|
|
874
|
-
break;
|
|
875
|
-
}
|
|
876
|
-
case "tool-call": toolCalls.push({
|
|
877
|
-
id: part.toolCallId,
|
|
878
|
-
type: "function",
|
|
879
|
-
function: {
|
|
880
|
-
name: part.toolName,
|
|
881
|
-
arguments: serializeToolCallArguments$1(part.input)
|
|
882
|
-
}
|
|
1020
|
+
})
|
|
883
1021
|
});
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
contentValue = output.value;
|
|
901
|
-
break;
|
|
902
|
-
case "error-text":
|
|
903
|
-
case "error-json":
|
|
904
|
-
contentValue = stringifyWithBigIntAndCycles({ error: output.value });
|
|
905
|
-
break;
|
|
906
|
-
case "execution-denied":
|
|
907
|
-
contentValue = output.reason ?? "Tool call execution denied.";
|
|
1022
|
+
break;
|
|
1023
|
+
case "assistant": {
|
|
1024
|
+
let text = "";
|
|
1025
|
+
const textParts = [];
|
|
1026
|
+
let hasPromptCacheBreakpoint = false;
|
|
1027
|
+
const toolCalls = [];
|
|
1028
|
+
for (const part of content) switch (part.type) {
|
|
1029
|
+
case "text": {
|
|
1030
|
+
const promptCacheBreakpoint = getPromptCacheBreakpoint$1(part.providerOptions);
|
|
1031
|
+
text += part.text;
|
|
1032
|
+
textParts.push({
|
|
1033
|
+
type: "text",
|
|
1034
|
+
text: part.text,
|
|
1035
|
+
...promptCacheBreakpoint != null && { prompt_cache_breakpoint: promptCacheBreakpoint }
|
|
1036
|
+
});
|
|
1037
|
+
hasPromptCacheBreakpoint ||= promptCacheBreakpoint != null;
|
|
908
1038
|
break;
|
|
909
|
-
|
|
910
|
-
case "
|
|
1039
|
+
}
|
|
1040
|
+
case "tool-call": toolCalls.push({
|
|
1041
|
+
id: part.toolCallId,
|
|
1042
|
+
type: "function",
|
|
1043
|
+
function: {
|
|
1044
|
+
name: part.toolName,
|
|
1045
|
+
arguments: serializeToolCallArguments$1(part.input)
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
911
1048
|
}
|
|
912
1049
|
messages.push({
|
|
913
|
-
role: "
|
|
914
|
-
|
|
915
|
-
content:
|
|
916
|
-
|
|
917
|
-
text: contentValue,
|
|
918
|
-
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
919
|
-
}]
|
|
1050
|
+
role: "assistant",
|
|
1051
|
+
...name != null && { name },
|
|
1052
|
+
content: hasPromptCacheBreakpoint ? textParts : toolCalls.length > 0 ? text || null : text,
|
|
1053
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
920
1054
|
});
|
|
1055
|
+
break;
|
|
921
1056
|
}
|
|
922
|
-
|
|
923
|
-
|
|
1057
|
+
case "tool":
|
|
1058
|
+
if (name != null) warnings.push({
|
|
1059
|
+
type: "unsupported",
|
|
1060
|
+
feature: "message name on tool messages"
|
|
1061
|
+
});
|
|
1062
|
+
for (const toolResponse of content) {
|
|
1063
|
+
if (toolResponse.type === "tool-approval-response") continue;
|
|
1064
|
+
const output = toolResponse.output;
|
|
1065
|
+
const promptCacheBreakpoint = (output.type === "content" ? output.value.map((part) => getPromptCacheBreakpoint$1(part.providerOptions)).find((breakpoint) => breakpoint != null) : getPromptCacheBreakpoint$1(output.providerOptions)) ?? getPromptCacheBreakpoint$1(toolResponse.providerOptions);
|
|
1066
|
+
let contentValue;
|
|
1067
|
+
switch (output.type) {
|
|
1068
|
+
case "text":
|
|
1069
|
+
contentValue = output.value;
|
|
1070
|
+
break;
|
|
1071
|
+
case "error-text":
|
|
1072
|
+
case "error-json":
|
|
1073
|
+
contentValue = stringifyWithBigIntAndCycles({ error: output.value });
|
|
1074
|
+
break;
|
|
1075
|
+
case "execution-denied":
|
|
1076
|
+
contentValue = output.reason ?? "Tool call execution denied.";
|
|
1077
|
+
break;
|
|
1078
|
+
case "content":
|
|
1079
|
+
case "json": contentValue = stringifyWithBigIntAndCycles(output.value);
|
|
1080
|
+
}
|
|
1081
|
+
messages.push({
|
|
1082
|
+
role: "tool",
|
|
1083
|
+
tool_call_id: toolResponse.toolCallId,
|
|
1084
|
+
content: promptCacheBreakpoint == null ? contentValue : [{
|
|
1085
|
+
type: "text",
|
|
1086
|
+
text: contentValue,
|
|
1087
|
+
prompt_cache_breakpoint: promptCacheBreakpoint
|
|
1088
|
+
}]
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
break;
|
|
1092
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
1093
|
+
}
|
|
924
1094
|
}
|
|
925
1095
|
return {
|
|
926
1096
|
messages,
|
|
@@ -938,9 +1108,9 @@ function getResponseMetadata$1({ id, model, created }) {
|
|
|
938
1108
|
}
|
|
939
1109
|
//#endregion
|
|
940
1110
|
//#region src/chat/map-openai-finish-reason.ts
|
|
941
|
-
function mapOpenAIFinishReason$1(finishReason) {
|
|
1111
|
+
function mapOpenAIFinishReason$1({ finishReason, hasRefusal = false }) {
|
|
942
1112
|
switch (finishReason) {
|
|
943
|
-
case "stop": return "stop";
|
|
1113
|
+
case "stop": return hasRefusal ? "content-filter" : "stop";
|
|
944
1114
|
case "length": return "length";
|
|
945
1115
|
case "content_filter": return "content-filter";
|
|
946
1116
|
case "function_call":
|
|
@@ -958,6 +1128,7 @@ const openaiChatResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
|
958
1128
|
message: z.object({
|
|
959
1129
|
role: z.literal("assistant").nullish(),
|
|
960
1130
|
content: z.string().nullish(),
|
|
1131
|
+
refusal: z.string().nullish(),
|
|
961
1132
|
audio: z.object({ transcript: z.string().nullish() }).nullish(),
|
|
962
1133
|
tool_calls: z.array(z.object({
|
|
963
1134
|
id: z.string().nullish(),
|
|
@@ -978,14 +1149,24 @@ const openaiChatResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
|
978
1149
|
})).nullish()
|
|
979
1150
|
}),
|
|
980
1151
|
index: z.number(),
|
|
981
|
-
logprobs: z.object({
|
|
982
|
-
|
|
983
|
-
logprob: z.number(),
|
|
984
|
-
top_logprobs: z.array(z.object({
|
|
1152
|
+
logprobs: z.object({
|
|
1153
|
+
content: z.array(z.object({
|
|
985
1154
|
token: z.string(),
|
|
986
|
-
logprob: z.number()
|
|
987
|
-
|
|
988
|
-
|
|
1155
|
+
logprob: z.number(),
|
|
1156
|
+
top_logprobs: z.array(z.object({
|
|
1157
|
+
token: z.string(),
|
|
1158
|
+
logprob: z.number()
|
|
1159
|
+
}))
|
|
1160
|
+
})).nullish(),
|
|
1161
|
+
refusal: z.array(z.object({
|
|
1162
|
+
token: z.string(),
|
|
1163
|
+
logprob: z.number(),
|
|
1164
|
+
top_logprobs: z.array(z.object({
|
|
1165
|
+
token: z.string(),
|
|
1166
|
+
logprob: z.number()
|
|
1167
|
+
}))
|
|
1168
|
+
})).nullish()
|
|
1169
|
+
}).nullish(),
|
|
989
1170
|
finish_reason: z.string().nullish()
|
|
990
1171
|
})),
|
|
991
1172
|
usage: z.object({
|
|
@@ -1011,6 +1192,7 @@ const openaiChatChunkSchema = lazySchema(() => zodSchema(z.union([z.object({
|
|
|
1011
1192
|
delta: z.object({
|
|
1012
1193
|
role: z.enum(["assistant"]).nullish(),
|
|
1013
1194
|
content: z.string().nullish(),
|
|
1195
|
+
refusal: z.string().nullish(),
|
|
1014
1196
|
tool_calls: z.array(z.object({
|
|
1015
1197
|
index: z.number(),
|
|
1016
1198
|
id: z.string().nullish(),
|
|
@@ -1030,14 +1212,24 @@ const openaiChatChunkSchema = lazySchema(() => zodSchema(z.union([z.object({
|
|
|
1030
1212
|
})
|
|
1031
1213
|
})).nullish()
|
|
1032
1214
|
}).nullish(),
|
|
1033
|
-
logprobs: z.object({
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1215
|
+
logprobs: z.object({
|
|
1216
|
+
content: z.array(z.object({
|
|
1217
|
+
token: z.string(),
|
|
1218
|
+
logprob: z.number(),
|
|
1219
|
+
top_logprobs: z.array(z.object({
|
|
1220
|
+
token: z.string(),
|
|
1221
|
+
logprob: z.number()
|
|
1222
|
+
}))
|
|
1223
|
+
})).nullish(),
|
|
1224
|
+
refusal: z.array(z.object({
|
|
1225
|
+
token: z.string(),
|
|
1226
|
+
logprob: z.number(),
|
|
1227
|
+
top_logprobs: z.array(z.object({
|
|
1228
|
+
token: z.string(),
|
|
1229
|
+
logprob: z.number()
|
|
1230
|
+
}))
|
|
1231
|
+
})).nullish()
|
|
1232
|
+
}).nullish(),
|
|
1041
1233
|
finish_reason: z.string().nullish(),
|
|
1042
1234
|
index: z.number()
|
|
1043
1235
|
})),
|
|
@@ -1057,155 +1249,6 @@ const openaiChatChunkSchema = lazySchema(() => zodSchema(z.union([z.object({
|
|
|
1057
1249
|
}).nullish()
|
|
1058
1250
|
}), openaiErrorDataSchema()])));
|
|
1059
1251
|
//#endregion
|
|
1060
|
-
//#region src/chat/openai-chat-language-model-options.ts
|
|
1061
|
-
const openaiLanguageModelChatOptions = lazySchema(() => zodSchema(z.object({
|
|
1062
|
-
/**
|
|
1063
|
-
* Modify the likelihood of specified tokens appearing in the completion.
|
|
1064
|
-
*
|
|
1065
|
-
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
1066
|
-
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
1067
|
-
*/
|
|
1068
|
-
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
1069
|
-
/**
|
|
1070
|
-
* Return the log probabilities of the tokens.
|
|
1071
|
-
*
|
|
1072
|
-
* Setting to true will return the log probabilities of the tokens that
|
|
1073
|
-
* were generated.
|
|
1074
|
-
*
|
|
1075
|
-
* Setting to a number will return the log probabilities of the top n
|
|
1076
|
-
* tokens that were generated.
|
|
1077
|
-
*/
|
|
1078
|
-
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
|
1079
|
-
/**
|
|
1080
|
-
* Whether to enable parallel function calling during tool use. Default to true.
|
|
1081
|
-
*/
|
|
1082
|
-
parallelToolCalls: z.boolean().optional(),
|
|
1083
|
-
/**
|
|
1084
|
-
* A unique identifier representing your end-user, which can help OpenAI to
|
|
1085
|
-
* monitor and detect abuse.
|
|
1086
|
-
*/
|
|
1087
|
-
user: z.string().optional(),
|
|
1088
|
-
/**
|
|
1089
|
-
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
1090
|
-
*/
|
|
1091
|
-
reasoningEffort: z.enum([
|
|
1092
|
-
"none",
|
|
1093
|
-
"minimal",
|
|
1094
|
-
"low",
|
|
1095
|
-
"medium",
|
|
1096
|
-
"high",
|
|
1097
|
-
"xhigh",
|
|
1098
|
-
"max"
|
|
1099
|
-
]).optional(),
|
|
1100
|
-
/**
|
|
1101
|
-
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
1102
|
-
*/
|
|
1103
|
-
maxCompletionTokens: z.number().optional(),
|
|
1104
|
-
/**
|
|
1105
|
-
* Whether to enable persistence in responses API.
|
|
1106
|
-
*/
|
|
1107
|
-
store: z.boolean().optional(),
|
|
1108
|
-
/**
|
|
1109
|
-
* Metadata to associate with the request.
|
|
1110
|
-
*/
|
|
1111
|
-
metadata: z.record(z.string().max(64), z.string().max(512)).optional(),
|
|
1112
|
-
/**
|
|
1113
|
-
* Parameters for prediction mode.
|
|
1114
|
-
*/
|
|
1115
|
-
prediction: z.record(z.string(), z.any()).optional(),
|
|
1116
|
-
/**
|
|
1117
|
-
* Service tier for the request.
|
|
1118
|
-
* - 'auto': Default service tier. The request will be processed with the service tier configured in the
|
|
1119
|
-
* Project settings. Unless otherwise configured, the Project will use 'default'.
|
|
1120
|
-
* - 'flex': 50% cheaper processing at the cost of increased latency. Only available for o3 and o4-mini models.
|
|
1121
|
-
* - 'priority': Higher-speed processing with predictably low latency at premium cost. Available for Enterprise customers.
|
|
1122
|
-
* - 'fast': OpenAI's newer name for the 'priority' tier. Interchangeable with it.
|
|
1123
|
-
* - 'ultrafast': Access-controlled Ultrafast processing. Only available for gpt-5.6-sol.
|
|
1124
|
-
* - 'default': The request will be processed with the standard pricing and performance for the selected model.
|
|
1125
|
-
*
|
|
1126
|
-
* @default 'auto'
|
|
1127
|
-
*/
|
|
1128
|
-
serviceTier: z.enum([
|
|
1129
|
-
"auto",
|
|
1130
|
-
"flex",
|
|
1131
|
-
"priority",
|
|
1132
|
-
"fast",
|
|
1133
|
-
"ultrafast",
|
|
1134
|
-
"default"
|
|
1135
|
-
]).optional(),
|
|
1136
|
-
/**
|
|
1137
|
-
* Whether to use strict JSON schema validation.
|
|
1138
|
-
*
|
|
1139
|
-
* @default true
|
|
1140
|
-
*/
|
|
1141
|
-
strictJsonSchema: z.boolean().optional(),
|
|
1142
|
-
/**
|
|
1143
|
-
* Controls the verbosity of the model's responses.
|
|
1144
|
-
* Lower values will result in more concise responses, while higher values will result in more verbose responses.
|
|
1145
|
-
*/
|
|
1146
|
-
textVerbosity: z.enum([
|
|
1147
|
-
"low",
|
|
1148
|
-
"medium",
|
|
1149
|
-
"high"
|
|
1150
|
-
]).optional(),
|
|
1151
|
-
/**
|
|
1152
|
-
* A cache key for prompt caching. Allows manual control over prompt caching behavior.
|
|
1153
|
-
* Useful for improving cache hit rates and working around automatic caching issues.
|
|
1154
|
-
*/
|
|
1155
|
-
promptCacheKey: z.string().optional(),
|
|
1156
|
-
/**
|
|
1157
|
-
* Prompt cache behavior for GPT-5.6 and later models.
|
|
1158
|
-
* `mode` controls whether OpenAI also places an implicit breakpoint.
|
|
1159
|
-
* `ttl` sets the minimum cache lifetime and currently only supports 30 minutes.
|
|
1160
|
-
*/
|
|
1161
|
-
promptCacheOptions: z.object({
|
|
1162
|
-
mode: z.enum(["implicit", "explicit"]).optional(),
|
|
1163
|
-
ttl: z.literal("30m").optional()
|
|
1164
|
-
}).optional(),
|
|
1165
|
-
/**
|
|
1166
|
-
* The retention policy for the prompt cache.
|
|
1167
|
-
* - 'in_memory': Default. Standard prompt caching behavior.
|
|
1168
|
-
* - '24h': Extended prompt caching that keeps cached prefixes active for up to 24 hours.
|
|
1169
|
-
* Available for models before GPT-5.6 that support extended caching.
|
|
1170
|
-
*
|
|
1171
|
-
* @deprecated For GPT-5.6 and later models, use `promptCacheOptions.ttl`.
|
|
1172
|
-
*
|
|
1173
|
-
* @default 'in_memory'
|
|
1174
|
-
*/
|
|
1175
|
-
promptCacheRetention: z.enum(["in_memory", "24h"]).optional(),
|
|
1176
|
-
/**
|
|
1177
|
-
* A stable identifier used to help detect users of your application
|
|
1178
|
-
* that may be violating OpenAI's usage policies. The IDs should be a
|
|
1179
|
-
* string that uniquely identifies each user. We recommend hashing their
|
|
1180
|
-
* username or email address, in order to avoid sending us any identifying
|
|
1181
|
-
* information.
|
|
1182
|
-
*/
|
|
1183
|
-
safetyIdentifier: z.string().optional(),
|
|
1184
|
-
/**
|
|
1185
|
-
* Override the system message mode for this model.
|
|
1186
|
-
* - 'system': Use the 'system' role for system messages (default for most models)
|
|
1187
|
-
* - 'developer': Use the 'developer' role for system messages (used by reasoning models)
|
|
1188
|
-
* - 'remove': Remove system messages entirely
|
|
1189
|
-
*
|
|
1190
|
-
* If not specified, the mode is automatically determined based on the model.
|
|
1191
|
-
*/
|
|
1192
|
-
systemMessageMode: z.enum([
|
|
1193
|
-
"system",
|
|
1194
|
-
"developer",
|
|
1195
|
-
"remove"
|
|
1196
|
-
]).optional(),
|
|
1197
|
-
/**
|
|
1198
|
-
* Force treating this model as a reasoning model.
|
|
1199
|
-
*
|
|
1200
|
-
* This is useful for "stealth" reasoning models (e.g. via a custom baseURL)
|
|
1201
|
-
* where the model ID is not recognized by the SDK's allowlist.
|
|
1202
|
-
*
|
|
1203
|
-
* When enabled, the SDK applies reasoning-model parameter compatibility rules
|
|
1204
|
-
* and defaults `systemMessageMode` to `developer` unless overridden.
|
|
1205
|
-
*/
|
|
1206
|
-
forceReasoning: z.boolean().optional()
|
|
1207
|
-
})));
|
|
1208
|
-
//#endregion
|
|
1209
1252
|
//#region src/normalize-openai-json-schema.ts
|
|
1210
1253
|
/**
|
|
1211
1254
|
* Normalizes JSON Schema for OpenAI structured outputs.
|
|
@@ -1444,7 +1487,7 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1444
1487
|
feature: "reasoningSummary",
|
|
1445
1488
|
details: "reasoningSummary is only supported by the Responses API, not the Chat Completions API"
|
|
1446
1489
|
});
|
|
1447
|
-
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages({
|
|
1490
|
+
const { messages, warnings: messageWarnings } = await convertToOpenAIChatMessages({
|
|
1448
1491
|
prompt,
|
|
1449
1492
|
systemMessageMode: openaiOptions.systemMessageMode ?? (isReasoningModel ? "developer" : modelRequestConfig.systemMessageMode)
|
|
1450
1493
|
});
|
|
@@ -1597,11 +1640,12 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1597
1640
|
}
|
|
1598
1641
|
async doGenerate(options) {
|
|
1599
1642
|
const { args: body, warnings } = await this.getArgs(options);
|
|
1643
|
+
const url = this.config.url({
|
|
1644
|
+
path: "/chat/completions",
|
|
1645
|
+
modelId: this.config.requestModelId ?? this.modelId
|
|
1646
|
+
});
|
|
1600
1647
|
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
1601
|
-
url
|
|
1602
|
-
path: "/chat/completions",
|
|
1603
|
-
modelId: this.config.requestModelId ?? this.modelId
|
|
1604
|
-
}),
|
|
1648
|
+
url,
|
|
1605
1649
|
headers: combineHeaders(await resolve(this.config.headers), options.headers),
|
|
1606
1650
|
body,
|
|
1607
1651
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -1620,6 +1664,12 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1620
1664
|
type: "text",
|
|
1621
1665
|
text
|
|
1622
1666
|
});
|
|
1667
|
+
const refusal = choice.message.refusal;
|
|
1668
|
+
const hasRefusal = refusal != null && refusal.length > 0;
|
|
1669
|
+
if (hasRefusal) content.push({
|
|
1670
|
+
type: "text",
|
|
1671
|
+
text: refusal
|
|
1672
|
+
});
|
|
1623
1673
|
for (const toolCall of choice.message.tool_calls ?? []) content.push({
|
|
1624
1674
|
type: "tool-call",
|
|
1625
1675
|
toolCallId: toolCall.id || generateId(),
|
|
@@ -1637,15 +1687,23 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1637
1687
|
const providerMetadata = { openai: {} };
|
|
1638
1688
|
if (completionTokenDetails?.accepted_prediction_tokens != null) providerMetadata.openai.acceptedPredictionTokens = completionTokenDetails?.accepted_prediction_tokens;
|
|
1639
1689
|
if (completionTokenDetails?.rejected_prediction_tokens != null) providerMetadata.openai.rejectedPredictionTokens = completionTokenDetails?.rejected_prediction_tokens;
|
|
1640
|
-
|
|
1690
|
+
const contentLogprobs = choice.logprobs?.content;
|
|
1691
|
+
const refusalLogprobs = choice.logprobs?.refusal;
|
|
1692
|
+
if (contentLogprobs != null || refusalLogprobs != null) providerMetadata.openai.logprobs = [...contentLogprobs ?? [], ...refusalLogprobs ?? []];
|
|
1641
1693
|
return {
|
|
1642
1694
|
content,
|
|
1643
1695
|
finishReason: {
|
|
1644
|
-
unified: mapOpenAIFinishReason$1(
|
|
1696
|
+
unified: mapOpenAIFinishReason$1({
|
|
1697
|
+
finishReason: choice.finish_reason,
|
|
1698
|
+
hasRefusal
|
|
1699
|
+
}),
|
|
1645
1700
|
raw: choice.finish_reason ?? void 0
|
|
1646
1701
|
},
|
|
1647
1702
|
usage: convertOpenAIChatUsage(response.usage),
|
|
1648
|
-
request: {
|
|
1703
|
+
request: {
|
|
1704
|
+
body,
|
|
1705
|
+
url
|
|
1706
|
+
},
|
|
1649
1707
|
response: {
|
|
1650
1708
|
...getResponseMetadata$1(response),
|
|
1651
1709
|
headers: responseHeaders,
|
|
@@ -1691,6 +1749,10 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1691
1749
|
let usage = void 0;
|
|
1692
1750
|
let metadataExtracted = false;
|
|
1693
1751
|
let isActiveText = false;
|
|
1752
|
+
let hasRefusal = false;
|
|
1753
|
+
let rawFinishReason = void 0;
|
|
1754
|
+
let hasLogprobs = false;
|
|
1755
|
+
const streamedLogprobs = [];
|
|
1694
1756
|
const providerMetadata = { openai: {} };
|
|
1695
1757
|
return {
|
|
1696
1758
|
stream: checkedResponse.pipeThrough(new TransformStream({
|
|
@@ -1714,6 +1776,7 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1714
1776
|
unified: "error",
|
|
1715
1777
|
raw: void 0
|
|
1716
1778
|
};
|
|
1779
|
+
rawFinishReason = void 0;
|
|
1717
1780
|
controller.enqueue({
|
|
1718
1781
|
type: "error",
|
|
1719
1782
|
error: chunk.error
|
|
@@ -1726,6 +1789,7 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1726
1789
|
unified: "error",
|
|
1727
1790
|
raw: void 0
|
|
1728
1791
|
};
|
|
1792
|
+
rawFinishReason = void 0;
|
|
1729
1793
|
controller.enqueue({
|
|
1730
1794
|
type: "error",
|
|
1731
1795
|
error: createOpenAIProviderStreamError(value.error) ?? value.error
|
|
@@ -1748,14 +1812,25 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1748
1812
|
if (value.usage.completion_tokens_details?.rejected_prediction_tokens != null) providerMetadata.openai.rejectedPredictionTokens = value.usage.completion_tokens_details?.rejected_prediction_tokens;
|
|
1749
1813
|
}
|
|
1750
1814
|
const choice = value.choices[0];
|
|
1751
|
-
if (choice?.finish_reason != null)
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1815
|
+
if (choice?.finish_reason != null) rawFinishReason = choice.finish_reason;
|
|
1816
|
+
if (choice?.logprobs != null) {
|
|
1817
|
+
const contentLogprobs = choice.logprobs.content;
|
|
1818
|
+
const refusalLogprobs = choice.logprobs.refusal;
|
|
1819
|
+
if (contentLogprobs != null || refusalLogprobs != null) {
|
|
1820
|
+
hasLogprobs = true;
|
|
1821
|
+
streamedLogprobs.push(...contentLogprobs ?? [], ...refusalLogprobs ?? []);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1756
1824
|
if (choice?.delta == null) return;
|
|
1757
1825
|
const delta = choice.delta;
|
|
1758
|
-
|
|
1826
|
+
const textDeltas = [];
|
|
1827
|
+
if (delta.content != null && delta.content.length > 0) textDeltas.push(delta.content);
|
|
1828
|
+
if (delta.refusal != null && delta.refusal.length > 0) {
|
|
1829
|
+
hasRefusal = true;
|
|
1830
|
+
textDeltas.push(delta.refusal);
|
|
1831
|
+
}
|
|
1832
|
+
if (textDeltas.length === 0 && delta.content === "") textDeltas.push(delta.content);
|
|
1833
|
+
for (const textDelta of textDeltas) {
|
|
1759
1834
|
if (!isActiveText) {
|
|
1760
1835
|
controller.enqueue({
|
|
1761
1836
|
type: "text-start",
|
|
@@ -1766,7 +1841,7 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1766
1841
|
controller.enqueue({
|
|
1767
1842
|
type: "text-delta",
|
|
1768
1843
|
id: "0",
|
|
1769
|
-
delta:
|
|
1844
|
+
delta: textDelta
|
|
1770
1845
|
});
|
|
1771
1846
|
}
|
|
1772
1847
|
if (delta.tool_calls != null) for (const toolCallDelta of delta.tool_calls) toolCallTracker.processDelta(toolCallDelta);
|
|
@@ -1784,6 +1859,14 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1784
1859
|
id: "0"
|
|
1785
1860
|
});
|
|
1786
1861
|
toolCallTracker.flush();
|
|
1862
|
+
if (rawFinishReason != null) finishReason = {
|
|
1863
|
+
unified: mapOpenAIFinishReason$1({
|
|
1864
|
+
finishReason: rawFinishReason,
|
|
1865
|
+
hasRefusal
|
|
1866
|
+
}),
|
|
1867
|
+
raw: rawFinishReason
|
|
1868
|
+
};
|
|
1869
|
+
if (hasLogprobs) providerMetadata.openai.logprobs = streamedLogprobs;
|
|
1787
1870
|
controller.enqueue({
|
|
1788
1871
|
type: "finish",
|
|
1789
1872
|
finishReason,
|
|
@@ -1792,7 +1875,10 @@ var OpenAIChatLanguageModel = class OpenAIChatLanguageModel {
|
|
|
1792
1875
|
});
|
|
1793
1876
|
}
|
|
1794
1877
|
})),
|
|
1795
|
-
request: {
|
|
1878
|
+
request: {
|
|
1879
|
+
body,
|
|
1880
|
+
url
|
|
1881
|
+
},
|
|
1796
1882
|
response: { headers: responseHeaders }
|
|
1797
1883
|
};
|
|
1798
1884
|
}
|
|
@@ -1801,7 +1887,7 @@ function isOpenAIChatOutputChunk(chunk) {
|
|
|
1801
1887
|
if ("error" in chunk) return false;
|
|
1802
1888
|
return chunk.choices.some((choice) => {
|
|
1803
1889
|
const delta = choice.delta;
|
|
1804
|
-
return delta?.content != null && delta.content.length > 0 || delta?.tool_calls != null && delta.tool_calls.length > 0 || delta?.annotations != null && delta.annotations.length > 0;
|
|
1890
|
+
return delta?.content != null && delta.content.length > 0 || delta?.refusal != null && delta.refusal.length > 0 || delta?.tool_calls != null && delta.tool_calls.length > 0 || delta?.annotations != null && delta.annotations.length > 0;
|
|
1805
1891
|
});
|
|
1806
1892
|
}
|
|
1807
1893
|
//#endregion
|
|
@@ -2343,6 +2429,15 @@ const baseImageModelOptionsObject = () => z.object({
|
|
|
2343
2429
|
"webp"
|
|
2344
2430
|
]).optional(),
|
|
2345
2431
|
/**
|
|
2432
|
+
* How much detail the model puts into the image. Supported by GPT Image
|
|
2433
|
+
* 2.6 models. The API defaults to `high`.
|
|
2434
|
+
*/
|
|
2435
|
+
complexity: z.enum([
|
|
2436
|
+
"low",
|
|
2437
|
+
"medium",
|
|
2438
|
+
"high"
|
|
2439
|
+
]).optional(),
|
|
2440
|
+
/**
|
|
2346
2441
|
* Compression level (0-100) for the generated image(s). Applies to the
|
|
2347
2442
|
* `jpeg` and `webp` output formats.
|
|
2348
2443
|
*/
|
|
@@ -2351,7 +2446,13 @@ const baseImageModelOptionsObject = () => z.object({
|
|
|
2351
2446
|
* A unique identifier representing your end-user, which can help OpenAI
|
|
2352
2447
|
* to monitor and detect abuse.
|
|
2353
2448
|
*/
|
|
2354
|
-
user: z.string().optional()
|
|
2449
|
+
user: z.string().optional(),
|
|
2450
|
+
/**
|
|
2451
|
+
* A stable identifier used to help detect users of your application that
|
|
2452
|
+
* may be violating OpenAI's usage policies. Should be a hashed username
|
|
2453
|
+
* or email address.
|
|
2454
|
+
*/
|
|
2455
|
+
safetyIdentifier: z.string().max(128).optional()
|
|
2355
2456
|
});
|
|
2356
2457
|
const openaiImageModelOptions = lazySchema(() => zodSchema(baseImageModelOptionsObject()));
|
|
2357
2458
|
const openaiImageModelGenerationOptions = lazySchema(() => zodSchema(baseImageModelOptionsObject().extend({
|
|
@@ -2362,16 +2463,50 @@ const openaiImageModelGenerationOptions = lazySchema(() => zodSchema(baseImageMo
|
|
|
2362
2463
|
*/
|
|
2363
2464
|
style: z.enum(["vivid", "natural"]).optional(),
|
|
2364
2465
|
/**
|
|
2466
|
+
* Format in which the generated image(s) are returned. `gif` produces
|
|
2467
|
+
* an animated GIF and is supported by GPT Image 2.6 models.
|
|
2468
|
+
*/
|
|
2469
|
+
outputFormat: z.enum([
|
|
2470
|
+
"png",
|
|
2471
|
+
"jpeg",
|
|
2472
|
+
"webp",
|
|
2473
|
+
"gif"
|
|
2474
|
+
]).optional(),
|
|
2475
|
+
/**
|
|
2476
|
+
* Number of frames (2-64) of an animated GIF. Only applies to the `gif`
|
|
2477
|
+
* output format. The API defaults to 9.
|
|
2478
|
+
*/
|
|
2479
|
+
frameCount: z.number().int().min(2).max(64).optional(),
|
|
2480
|
+
/**
|
|
2481
|
+
* Duration of each frame in milliseconds (20-2000) of an animated GIF.
|
|
2482
|
+
* The API rounds it to the nearest 10 ms. Only applies to the `gif`
|
|
2483
|
+
* output format. The API defaults to 200.
|
|
2484
|
+
*/
|
|
2485
|
+
frameDurationMs: z.number().int().min(20).max(2e3).optional(),
|
|
2486
|
+
/**
|
|
2365
2487
|
* Content moderation level for the generated image(s). `low` applies
|
|
2366
2488
|
* less restrictive filtering.
|
|
2367
2489
|
*/
|
|
2368
2490
|
moderation: z.enum(["auto", "low"]).optional()
|
|
2369
2491
|
})));
|
|
2370
|
-
const openaiImageModelEditOptions = lazySchema(() => zodSchema(baseImageModelOptionsObject().extend({
|
|
2371
|
-
/**
|
|
2372
|
-
* Fidelity of the output image(s) to the input image(s).
|
|
2373
|
-
*/
|
|
2374
|
-
inputFidelity: z.enum(["high", "low"]).optional()
|
|
2492
|
+
const openaiImageModelEditOptions = lazySchema(() => zodSchema(baseImageModelOptionsObject().extend({
|
|
2493
|
+
/**
|
|
2494
|
+
* Fidelity of the output image(s) to the input image(s).
|
|
2495
|
+
*/
|
|
2496
|
+
inputFidelity: z.enum(["high", "low"]).optional(),
|
|
2497
|
+
/**
|
|
2498
|
+
* A transparent PNG overlaid on the first input image to mark up the
|
|
2499
|
+
* regions to change, as an HTTPS URL or a base64 data URL. It must match
|
|
2500
|
+
* the dimensions of the first image. Supported by GPT Image 2.5 and 2.6
|
|
2501
|
+
* models. Cannot be combined with a `mask`.
|
|
2502
|
+
*/
|
|
2503
|
+
markup: z.string().optional(),
|
|
2504
|
+
/**
|
|
2505
|
+
* Content moderation level for the edited image(s). `low` applies
|
|
2506
|
+
* less restrictive filtering. Supported by GPT Image models.
|
|
2507
|
+
*/
|
|
2508
|
+
moderation: z.enum(["auto", "low"]).optional()
|
|
2509
|
+
})));
|
|
2375
2510
|
//#endregion
|
|
2376
2511
|
//#region src/image/openai-image-model.ts
|
|
2377
2512
|
var OpenAIImageModel = class OpenAIImageModel {
|
|
@@ -2423,6 +2558,10 @@ var OpenAIImageModel = class OpenAIImageModel {
|
|
|
2423
2558
|
providerOptions,
|
|
2424
2559
|
schema: openaiImageModelEditOptions
|
|
2425
2560
|
}) ?? {};
|
|
2561
|
+
if (mask != null && openaiOptions.markup != null) throw new InvalidArgumentError({
|
|
2562
|
+
argument: "providerOptions.openai.markup",
|
|
2563
|
+
message: "`markup` cannot be combined with `mask`."
|
|
2564
|
+
});
|
|
2426
2565
|
const { value: response, responseHeaders } = await postFormDataToApi({
|
|
2427
2566
|
url: this.config.url({
|
|
2428
2567
|
path: "/images/edits",
|
|
@@ -2434,14 +2573,18 @@ var OpenAIImageModel = class OpenAIImageModel {
|
|
|
2434
2573
|
prompt,
|
|
2435
2574
|
image: await Promise.all(files.map((file) => file.type === "file" ? new Blob([file.data instanceof Uint8Array ? new Blob([file.data], { type: file.mediaType }) : new Blob([convertBase64ToUint8Array(file.data)], { type: file.mediaType })], { type: file.mediaType }) : downloadBlob(file.url, { abortSignal }))),
|
|
2436
2575
|
mask: mask != null ? await fileToBlob(mask, abortSignal) : void 0,
|
|
2576
|
+
markup: openaiOptions.markup != null ? await markupToBlob(openaiOptions.markup, abortSignal) : void 0,
|
|
2437
2577
|
n,
|
|
2438
2578
|
size,
|
|
2439
2579
|
quality: openaiOptions.quality,
|
|
2440
2580
|
background: openaiOptions.background,
|
|
2441
2581
|
output_format: openaiOptions.outputFormat,
|
|
2442
2582
|
output_compression: openaiOptions.outputCompression,
|
|
2583
|
+
complexity: openaiOptions.complexity,
|
|
2443
2584
|
input_fidelity: openaiOptions.inputFidelity,
|
|
2444
|
-
|
|
2585
|
+
moderation: openaiOptions.moderation,
|
|
2586
|
+
user: openaiOptions.user,
|
|
2587
|
+
safety_identifier: openaiOptions.safetyIdentifier
|
|
2445
2588
|
}),
|
|
2446
2589
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
2447
2590
|
successfulResponseHandler: createJsonResponseHandler(openaiImageResponseSchema),
|
|
@@ -2494,7 +2637,11 @@ var OpenAIImageModel = class OpenAIImageModel {
|
|
|
2494
2637
|
moderation: openaiOptions.moderation,
|
|
2495
2638
|
output_format: openaiOptions.outputFormat,
|
|
2496
2639
|
output_compression: openaiOptions.outputCompression,
|
|
2640
|
+
complexity: openaiOptions.complexity,
|
|
2641
|
+
frame_count: openaiOptions.frameCount,
|
|
2642
|
+
frame_duration_ms: openaiOptions.frameDurationMs,
|
|
2497
2643
|
user: openaiOptions.user,
|
|
2644
|
+
safety_identifier: openaiOptions.safetyIdentifier,
|
|
2498
2645
|
...!hasDefaultResponseFormat(this.modelId) ? { response_format: "b64_json" } : {}
|
|
2499
2646
|
},
|
|
2500
2647
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -2547,6 +2694,15 @@ function distributeTokenDetails(details, index, total) {
|
|
|
2547
2694
|
}
|
|
2548
2695
|
return result;
|
|
2549
2696
|
}
|
|
2697
|
+
async function markupToBlob(markup, abortSignal) {
|
|
2698
|
+
const dataUrl = /^data:([^;,]*)(;base64)?,(.*)$/s.exec(markup);
|
|
2699
|
+
if (dataUrl == null) return downloadBlob(markup, { abortSignal });
|
|
2700
|
+
if (dataUrl[2] == null) throw new InvalidArgumentError({
|
|
2701
|
+
argument: "providerOptions.openai.markup",
|
|
2702
|
+
message: "`markup` data URLs must be base64 encoded."
|
|
2703
|
+
});
|
|
2704
|
+
return new Blob([convertBase64ToUint8Array(dataUrl[3])], { type: dataUrl[1] || "image/png" });
|
|
2705
|
+
}
|
|
2550
2706
|
async function fileToBlob(file, abortSignal) {
|
|
2551
2707
|
if (!file) return void 0;
|
|
2552
2708
|
if (file.type === "url") return downloadBlob(file.url, { abortSignal });
|
|
@@ -2740,7 +2896,7 @@ var OpenAITranscriptionModel = class OpenAITranscriptionModel {
|
|
|
2740
2896
|
this.config = config;
|
|
2741
2897
|
this.specificationVersion = "v4";
|
|
2742
2898
|
}
|
|
2743
|
-
async getArgs({ audio, mediaType, providerOptions }) {
|
|
2899
|
+
async getArgs({ audio, mediaType, providerOptions, timestampGranularities: requestedTimestampGranularities }) {
|
|
2744
2900
|
const warnings = [];
|
|
2745
2901
|
const openAIOptions = await parseProviderOptions({
|
|
2746
2902
|
provider: "openai",
|
|
@@ -2754,7 +2910,17 @@ var OpenAITranscriptionModel = class OpenAITranscriptionModel {
|
|
|
2754
2910
|
formData.append("file", new File([blob], "audio", { type: mediaType }), `audio.${fileExtension}`);
|
|
2755
2911
|
const { transcription: requestConfig } = getOpenAITranscriptionModelEntry(this.modelId).requestConfig;
|
|
2756
2912
|
const responseFormat = openAIOptions?.responseFormat ?? requestConfig.responseFormat;
|
|
2757
|
-
const timestampGranularities = openAIOptions?.timestampGranularities ?? (responseFormat === "verbose_json" ? ["segment"] : void 0);
|
|
2913
|
+
const timestampGranularities = openAIOptions?.timestampGranularities ?? requestedTimestampGranularities ?? (responseFormat === "verbose_json" ? ["segment"] : void 0);
|
|
2914
|
+
for (const granularity of requestedTimestampGranularities ?? []) if (openAIOptions?.timestampGranularities != null && !openAIOptions.timestampGranularities.includes(granularity)) warnings.push({
|
|
2915
|
+
type: "unsupported",
|
|
2916
|
+
feature: "timestampGranularities",
|
|
2917
|
+
details: `The "${granularity}" timestamp granularity is excluded by providerOptions.openai.timestampGranularities.`
|
|
2918
|
+
});
|
|
2919
|
+
for (const granularity of timestampGranularities ?? []) if (responseFormat !== "verbose_json" && !(responseFormat === "diarized_json" && granularity === "segment")) warnings.push({
|
|
2920
|
+
type: "unsupported",
|
|
2921
|
+
feature: openAIOptions?.timestampGranularities != null ? "providerOptions.openai.timestampGranularities" : "timestampGranularities",
|
|
2922
|
+
details: `OpenAI transcription with the ${responseFormat ?? "default JSON"} response format does not support ${granularity} timestamps.`
|
|
2923
|
+
});
|
|
2758
2924
|
const chunkingStrategy = openAIOptions?.chunkingStrategy ?? requestConfig.chunkingStrategy;
|
|
2759
2925
|
const transcriptionModelOptions = {
|
|
2760
2926
|
response_format: responseFormat,
|
|
@@ -2764,7 +2930,7 @@ var OpenAITranscriptionModel = class OpenAITranscriptionModel {
|
|
|
2764
2930
|
keywords: openAIOptions?.keywords,
|
|
2765
2931
|
prompt: openAIOptions?.prompt,
|
|
2766
2932
|
temperature: openAIOptions?.temperature,
|
|
2767
|
-
timestamp_granularities: timestampGranularities
|
|
2933
|
+
timestamp_granularities: responseFormat === "verbose_json" ? timestampGranularities : void 0
|
|
2768
2934
|
};
|
|
2769
2935
|
for (const [key, value] of Object.entries(transcriptionModelOptions)) if (value != null) {
|
|
2770
2936
|
if (Array.isArray(value)) for (const item of value) formData.append(`${key}[]`, String(item));
|
|
@@ -2804,17 +2970,19 @@ var OpenAITranscriptionModel = class OpenAITranscriptionModel {
|
|
|
2804
2970
|
endSecond: segment.end,
|
|
2805
2971
|
speaker: segment.speaker
|
|
2806
2972
|
}] : []);
|
|
2973
|
+
const words = response.words?.map((word) => ({
|
|
2974
|
+
text: word.word,
|
|
2975
|
+
startSecond: word.start,
|
|
2976
|
+
endSecond: word.end
|
|
2977
|
+
}));
|
|
2807
2978
|
return {
|
|
2808
2979
|
text: response.text,
|
|
2809
2980
|
segments: response.segments?.map((segment) => ({
|
|
2810
2981
|
text: segment.text,
|
|
2811
2982
|
startSecond: segment.start,
|
|
2812
2983
|
endSecond: segment.end
|
|
2813
|
-
})) ??
|
|
2814
|
-
|
|
2815
|
-
startSecond: word.start,
|
|
2816
|
-
endSecond: word.end
|
|
2817
|
-
})) ?? [],
|
|
2984
|
+
})) ?? words ?? [],
|
|
2985
|
+
...words != null && words.length > 0 ? { words } : {},
|
|
2818
2986
|
language,
|
|
2819
2987
|
durationInSeconds: response.duration ?? (response.usage?.type === "duration" && typeof response.usage.seconds === "number" ? response.usage.seconds : void 0),
|
|
2820
2988
|
warnings,
|
|
@@ -3692,7 +3860,7 @@ const shellArgsSchema = lazySchema(() => zodSchema(z.object({ environment: z.uni
|
|
|
3692
3860
|
})).optional()
|
|
3693
3861
|
})
|
|
3694
3862
|
]).optional() })));
|
|
3695
|
-
createProviderDefinedToolFactoryWithOutputSchema({
|
|
3863
|
+
const shell = createProviderDefinedToolFactoryWithOutputSchema({
|
|
3696
3864
|
id: "openai.shell",
|
|
3697
3865
|
inputSchema: shellInputSchema,
|
|
3698
3866
|
outputSchema: shellOutputSchema
|
|
@@ -4926,10 +5094,12 @@ function mapOpenAIResponsesCitations(annotations, providerOptionsName) {
|
|
|
4926
5094
|
}
|
|
4927
5095
|
//#endregion
|
|
4928
5096
|
//#region src/responses/map-openai-responses-finish-reason.ts
|
|
4929
|
-
function mapOpenAIResponseFinishReason({ finishReason, hasFunctionCall }) {
|
|
5097
|
+
function mapOpenAIResponseFinishReason({ finishReason, hasFunctionCall, hasRefusal = false }) {
|
|
4930
5098
|
switch (finishReason) {
|
|
4931
5099
|
case void 0:
|
|
4932
|
-
case null:
|
|
5100
|
+
case null:
|
|
5101
|
+
if (hasFunctionCall) return "tool-calls";
|
|
5102
|
+
return hasRefusal ? "content-filter" : "stop";
|
|
4933
5103
|
case "max_output_tokens": return "length";
|
|
4934
5104
|
case "content_filter": return "content-filter";
|
|
4935
5105
|
default: return hasFunctionCall ? "tool-calls" : "other";
|
|
@@ -5109,6 +5279,8 @@ const openaiResponsesModeledChunkTypes = /* @__PURE__ */ new Set([
|
|
|
5109
5279
|
"response.output_item.done",
|
|
5110
5280
|
"response.output_text.annotation.added",
|
|
5111
5281
|
"response.output_text.delta",
|
|
5282
|
+
"response.refusal.delta",
|
|
5283
|
+
"response.refusal.done",
|
|
5112
5284
|
"response.reasoning_summary_part.added",
|
|
5113
5285
|
"response.reasoning_summary_part.done",
|
|
5114
5286
|
"response.reasoning_summary_text.delta"
|
|
@@ -5162,6 +5334,20 @@ const openaiResponsesChunkSchema = lazySchema(() => zodSchema(z.union([
|
|
|
5162
5334
|
}))
|
|
5163
5335
|
})).nullish()
|
|
5164
5336
|
}),
|
|
5337
|
+
z.object({
|
|
5338
|
+
type: z.literal("response.refusal.delta"),
|
|
5339
|
+
item_id: z.string(),
|
|
5340
|
+
output_index: z.number().nullish(),
|
|
5341
|
+
content_index: z.number().nullish(),
|
|
5342
|
+
delta: z.string()
|
|
5343
|
+
}),
|
|
5344
|
+
z.object({
|
|
5345
|
+
type: z.literal("response.refusal.done"),
|
|
5346
|
+
item_id: z.string(),
|
|
5347
|
+
output_index: z.number().nullish(),
|
|
5348
|
+
content_index: z.number().nullish(),
|
|
5349
|
+
refusal: z.string()
|
|
5350
|
+
}),
|
|
5165
5351
|
z.object({
|
|
5166
5352
|
type: z.enum(["response.completed", "response.incomplete"]),
|
|
5167
5353
|
response: z.object({
|
|
@@ -5719,7 +5905,7 @@ const openaiResponsesResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
|
5719
5905
|
role: z.literal("assistant"),
|
|
5720
5906
|
id: z.string(),
|
|
5721
5907
|
phase: z.enum(["commentary", "final_answer"]).nullish(),
|
|
5722
|
-
content: z.array(z.object({
|
|
5908
|
+
content: z.array(z.discriminatedUnion("type", [z.object({
|
|
5723
5909
|
type: z.literal("output_text"),
|
|
5724
5910
|
text: z.string(),
|
|
5725
5911
|
logprobs: z.array(z.object({
|
|
@@ -5758,7 +5944,10 @@ const openaiResponsesResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
|
5758
5944
|
index: z.number()
|
|
5759
5945
|
})
|
|
5760
5946
|
]))
|
|
5761
|
-
})
|
|
5947
|
+
}), z.object({
|
|
5948
|
+
type: z.literal("refusal"),
|
|
5949
|
+
refusal: z.string()
|
|
5950
|
+
})]))
|
|
5762
5951
|
}),
|
|
5763
5952
|
z.object({
|
|
5764
5953
|
type: z.literal("web_search_call"),
|
|
@@ -5996,7 +6185,24 @@ const codeInterpreterOutputSchema = lazySchema(() => zodSchema(z.object({ output
|
|
|
5996
6185
|
type: z.literal("image"),
|
|
5997
6186
|
url: z.string()
|
|
5998
6187
|
})])).nullish() })));
|
|
5999
|
-
const codeInterpreterArgsSchema = lazySchema(() => zodSchema(z.object({ container: z.union([z.string(), z.object({
|
|
6188
|
+
const codeInterpreterArgsSchema = lazySchema(() => zodSchema(z.object({ container: z.union([z.string(), z.object({
|
|
6189
|
+
fileIds: z.array(z.string()).optional(),
|
|
6190
|
+
memoryLimit: z.enum([
|
|
6191
|
+
"1g",
|
|
6192
|
+
"4g",
|
|
6193
|
+
"16g",
|
|
6194
|
+
"64g"
|
|
6195
|
+
]).optional(),
|
|
6196
|
+
networkPolicy: z.discriminatedUnion("type", [z.object({ type: z.literal("disabled") }), z.object({
|
|
6197
|
+
type: z.literal("allowlist"),
|
|
6198
|
+
allowedDomains: z.array(z.string()),
|
|
6199
|
+
domainSecrets: z.array(z.object({
|
|
6200
|
+
domain: z.string(),
|
|
6201
|
+
name: z.string(),
|
|
6202
|
+
value: z.string()
|
|
6203
|
+
})).optional()
|
|
6204
|
+
})]).optional()
|
|
6205
|
+
})]).optional() })));
|
|
6000
6206
|
const codeInterpreterToolFactory = createProviderExecutedToolFactory({
|
|
6001
6207
|
id: "openai.code_interpreter",
|
|
6002
6208
|
inputSchema: codeInterpreterInputSchema,
|
|
@@ -6119,10 +6325,11 @@ const customArgsSchema = lazySchema(() => zodSchema(z.object({
|
|
|
6119
6325
|
}), z.object({ type: z.literal("text") })]).optional()
|
|
6120
6326
|
})));
|
|
6121
6327
|
const customInputSchema = lazySchema(() => zodSchema(z.string()));
|
|
6122
|
-
createProviderDefinedToolFactory({
|
|
6328
|
+
const customToolFactory = createProviderDefinedToolFactory({
|
|
6123
6329
|
id: "openai.custom",
|
|
6124
6330
|
inputSchema: customInputSchema
|
|
6125
6331
|
});
|
|
6332
|
+
const customTool = (args) => customToolFactory(args);
|
|
6126
6333
|
//#endregion
|
|
6127
6334
|
//#region src/tool/mcp.ts
|
|
6128
6335
|
let cachedJsonValueSchema;
|
|
@@ -6145,8 +6352,9 @@ const mcpArgsSchema = lazySchema(() => zodSchema(z.object({
|
|
|
6145
6352
|
headers: z.record(z.string(), z.string()).optional(),
|
|
6146
6353
|
requireApproval: z.union([z.enum(["always", "never"]), z.object({ never: z.object({ toolNames: z.array(z.string()).optional() }).optional() })]).optional(),
|
|
6147
6354
|
serverDescription: z.string().optional(),
|
|
6148
|
-
serverUrl: z.string().optional()
|
|
6149
|
-
|
|
6355
|
+
serverUrl: z.string().optional(),
|
|
6356
|
+
tunnelId: z.string().optional()
|
|
6357
|
+
}).refine((v) => v.serverUrl != null || v.connectorId != null || v.tunnelId != null, "One of serverUrl, connectorId, or tunnelId must be provided.").refine((v) => v.tunnelId == null || v.serverUrl == null && v.connectorId == null, "tunnelId cannot be combined with serverUrl or connectorId.")));
|
|
6150
6358
|
const mcpInputSchema = lazySchema(() => zodSchema(z.object({})));
|
|
6151
6359
|
const mcpOutputSchema = lazySchema(() => zodSchema(z.object({
|
|
6152
6360
|
type: z.literal("call"),
|
|
@@ -6355,7 +6563,13 @@ async function prepareResponsesTools({ tools, toolChoice, allowedTools, toolName
|
|
|
6355
6563
|
file_ids: void 0
|
|
6356
6564
|
} : typeof args.container === "string" ? args.container : {
|
|
6357
6565
|
type: "auto",
|
|
6358
|
-
file_ids: args.container.fileIds
|
|
6566
|
+
file_ids: args.container.fileIds,
|
|
6567
|
+
memory_limit: args.container.memoryLimit,
|
|
6568
|
+
network_policy: args.container.networkPolicy == null ? void 0 : args.container.networkPolicy.type === "disabled" ? { type: "disabled" } : {
|
|
6569
|
+
type: "allowlist",
|
|
6570
|
+
allowed_domains: args.container.networkPolicy.allowedDomains,
|
|
6571
|
+
domain_secrets: args.container.networkPolicy.domainSecrets
|
|
6572
|
+
}
|
|
6359
6573
|
}
|
|
6360
6574
|
});
|
|
6361
6575
|
break;
|
|
@@ -6404,7 +6618,8 @@ async function prepareResponsesTools({ tools, toolChoice, allowedTools, toolName
|
|
|
6404
6618
|
headers: args.headers,
|
|
6405
6619
|
require_approval: requireApprovalParam ?? "never",
|
|
6406
6620
|
server_description: args.serverDescription,
|
|
6407
|
-
server_url: args.serverUrl
|
|
6621
|
+
server_url: args.serverUrl,
|
|
6622
|
+
tunnel_id: args.tunnelId
|
|
6408
6623
|
});
|
|
6409
6624
|
break;
|
|
6410
6625
|
}
|
|
@@ -7098,6 +7313,7 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
7098
7313
|
const retrievedFileIds = /* @__PURE__ */ new Set();
|
|
7099
7314
|
let hasFunctionCall = false;
|
|
7100
7315
|
const hostedToolSearchCallIds = [];
|
|
7316
|
+
let hasRefusal = false;
|
|
7101
7317
|
let structuredOutputText;
|
|
7102
7318
|
for (const part of response.output) switch (part.type) {
|
|
7103
7319
|
case "reasoning":
|
|
@@ -7199,8 +7415,20 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
7199
7415
|
});
|
|
7200
7416
|
break;
|
|
7201
7417
|
case "message":
|
|
7202
|
-
if (options.responseFormat?.type === "json" && part.phase === "final_answer") structuredOutputText = part.content.map((contentPart) => contentPart.text).join("");
|
|
7418
|
+
if (options.responseFormat?.type === "json" && part.phase === "final_answer") structuredOutputText = part.content.map((contentPart) => contentPart.type === "output_text" ? contentPart.text : contentPart.refusal).join("");
|
|
7203
7419
|
for (const contentPart of part.content) {
|
|
7420
|
+
if (contentPart.type === "refusal") {
|
|
7421
|
+
hasRefusal = true;
|
|
7422
|
+
content.push({
|
|
7423
|
+
type: "text",
|
|
7424
|
+
text: contentPart.refusal,
|
|
7425
|
+
providerMetadata: { [providerOptionsName]: {
|
|
7426
|
+
itemId: part.id,
|
|
7427
|
+
...part.phase != null && { phase: part.phase }
|
|
7428
|
+
} }
|
|
7429
|
+
});
|
|
7430
|
+
continue;
|
|
7431
|
+
}
|
|
7204
7432
|
if (options.providerOptions?.[providerOptionsName]?.logprobs && contentPart.logprobs) logprobs.push(contentPart.logprobs);
|
|
7205
7433
|
const providerMetadata = {
|
|
7206
7434
|
itemId: part.id,
|
|
@@ -7487,7 +7715,8 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
7487
7715
|
finishReason: {
|
|
7488
7716
|
unified: mapOpenAIResponseFinishReason({
|
|
7489
7717
|
finishReason: response.incomplete_details?.reason,
|
|
7490
|
-
hasFunctionCall
|
|
7718
|
+
hasFunctionCall,
|
|
7719
|
+
hasRefusal
|
|
7491
7720
|
}),
|
|
7492
7721
|
raw: response.incomplete_details?.reason ?? void 0
|
|
7493
7722
|
},
|
|
@@ -7550,6 +7779,7 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
7550
7779
|
const fallbackAnnotations = [];
|
|
7551
7780
|
let activeMessagePhase;
|
|
7552
7781
|
let hasFunctionCall = false;
|
|
7782
|
+
let hasRefusal = false;
|
|
7553
7783
|
const activeReasoning = {};
|
|
7554
7784
|
const activeOutputItemIds = {};
|
|
7555
7785
|
const resolveOutputItemId = ({ itemId, outputIndex }) => outputIndex == null ? itemId : activeOutputItemIds[outputIndex] ?? itemId;
|
|
@@ -8316,7 +8546,19 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
8316
8546
|
delta: value.delta
|
|
8317
8547
|
});
|
|
8318
8548
|
if (options.providerOptions?.[providerOptionsName]?.logprobs && value.logprobs) logprobs.push(value.logprobs);
|
|
8319
|
-
} else if (value
|
|
8549
|
+
} else if (isRefusalDeltaChunk(value)) {
|
|
8550
|
+
hasRefusal = true;
|
|
8551
|
+
const itemId = resolveOutputItemId({
|
|
8552
|
+
itemId: value.item_id,
|
|
8553
|
+
outputIndex: value.output_index
|
|
8554
|
+
});
|
|
8555
|
+
controller.enqueue({
|
|
8556
|
+
type: "text-delta",
|
|
8557
|
+
id: itemId,
|
|
8558
|
+
delta: value.delta
|
|
8559
|
+
});
|
|
8560
|
+
} else if (isRefusalDoneChunk(value)) hasRefusal = true;
|
|
8561
|
+
else if (value.type === "response.reasoning_summary_part.added") {
|
|
8320
8562
|
const itemId = resolveOutputItemId({
|
|
8321
8563
|
itemId: value.item_id,
|
|
8322
8564
|
outputIndex: value.output_index
|
|
@@ -8374,7 +8616,8 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
8374
8616
|
if (!encounteredStreamError) finishReason = {
|
|
8375
8617
|
unified: mapOpenAIResponseFinishReason({
|
|
8376
8618
|
finishReason: value.response.incomplete_details?.reason,
|
|
8377
|
-
hasFunctionCall
|
|
8619
|
+
hasFunctionCall,
|
|
8620
|
+
hasRefusal
|
|
8378
8621
|
}),
|
|
8379
8622
|
raw: value.response.incomplete_details?.reason ?? void 0
|
|
8380
8623
|
};
|
|
@@ -8464,6 +8707,12 @@ var OpenAIResponsesLanguageModel = class OpenAIResponsesLanguageModel {
|
|
|
8464
8707
|
function isTextDeltaChunk(chunk) {
|
|
8465
8708
|
return chunk.type === "response.output_text.delta";
|
|
8466
8709
|
}
|
|
8710
|
+
function isRefusalDeltaChunk(chunk) {
|
|
8711
|
+
return chunk.type === "response.refusal.delta";
|
|
8712
|
+
}
|
|
8713
|
+
function isRefusalDoneChunk(chunk) {
|
|
8714
|
+
return chunk.type === "response.refusal.done";
|
|
8715
|
+
}
|
|
8467
8716
|
function isOpenAIChatCompletionChunk(value) {
|
|
8468
8717
|
const chunk = asRecord(value);
|
|
8469
8718
|
return chunk != null && Array.isArray(chunk.choices) && typeof chunk.type !== "string";
|
|
@@ -8563,6 +8812,6 @@ function escapeJSONDelta(delta) {
|
|
|
8563
8812
|
return JSON.stringify(delta).slice(1, -1);
|
|
8564
8813
|
}
|
|
8565
8814
|
//#endregion
|
|
8566
|
-
export { OpenAIChatLanguageModel, OpenAICompletionLanguageModel, OpenAIEmbeddingModel, OpenAIImageModel, OpenAIResponsesLanguageModel, OpenAISpeechModel, OpenAITranscriptionModel, applyPatch, applyPatchArgsSchema, applyPatchInputSchema, applyPatchOutputSchema, applyPatchToolFactory, codeInterpreter, codeInterpreterArgsSchema, codeInterpreterInputSchema, codeInterpreterOutputSchema, codeInterpreterToolFactory, fileSearch, fileSearchArgsSchema, fileSearchOutputSchema, getMaxImagesPerCall, hasDefaultResponseFormat, imageGeneration, imageGenerationArgsSchema, imageGenerationOutputSchema, mapWebSearchResult, modelMaxImagesPerCall, openAITranscriptionModelOptions, openaiEmbeddingModelOptions, openaiImageModelEditOptions, openaiImageModelGenerationOptions, openaiImageModelOptions, openaiLanguageModelChatOptions, openaiLanguageModelCompletionOptions, openaiResponsesSupportedUrls, openaiSpeechModelOptionsSchema, programmaticToolCalling, programmaticToolCallingInputSchema, programmaticToolCallingOutputSchema, toolSearch, toolSearchArgsSchema, toolSearchInputSchema, toolSearchOutputSchema, webSearch, webSearchArgsSchema, webSearchOutputSchema, webSearchPreview, webSearchPreviewArgsSchema, webSearchPreviewInputSchema, webSearchToolFactory };
|
|
8815
|
+
export { OpenAIChatLanguageModel, OpenAICompletionLanguageModel, OpenAIEmbeddingModel, OpenAIImageModel, OpenAIResponsesLanguageModel, OpenAISpeechModel, OpenAITranscriptionModel, applyPatch, applyPatchArgsSchema, applyPatchInputSchema, applyPatchOutputSchema, applyPatchToolFactory, codeInterpreter, codeInterpreterArgsSchema, codeInterpreterInputSchema, codeInterpreterOutputSchema, codeInterpreterToolFactory, customArgsSchema, customTool, customToolFactory, fileSearch, fileSearchArgsSchema, fileSearchOutputSchema, getMaxImagesPerCall, hasDefaultResponseFormat, imageGeneration, imageGenerationArgsSchema, imageGenerationOutputSchema, mapWebSearchResult, modelMaxImagesPerCall, openAITranscriptionModelOptions, openaiChatMessageProviderOptions, openaiEmbeddingModelOptions, openaiImageModelEditOptions, openaiImageModelGenerationOptions, openaiImageModelOptions, openaiLanguageModelChatOptions, openaiLanguageModelCompletionOptions, openaiModelCatalog, openaiResponsesSupportedUrls, openaiSpeechModelOptionsSchema, programmaticToolCalling, programmaticToolCallingInputSchema, programmaticToolCallingOutputSchema, shell, shellArgsSchema, shellInputSchema, shellOutputSchema, toolSearch, toolSearchArgsSchema, toolSearchInputSchema, toolSearchOutputSchema, webSearch, webSearchArgsSchema, webSearchOutputSchema, webSearchPreview, webSearchPreviewArgsSchema, webSearchPreviewInputSchema, webSearchToolFactory };
|
|
8567
8816
|
|
|
8568
8817
|
//# sourceMappingURL=index.js.map
|