@ai-sdk/open-responses 2.0.51 → 2.0.52
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 +8 -0
- package/dist/index.d.ts +105 -4
- package/dist/index.js +221 -78
- package/dist/index.js.map +1 -1
- package/docs/06-open-responses.mdx +66 -2
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/open-responses-provider.ts +50 -3
- package/src/open-responses-tools.ts +52 -0
- package/src/responses/convert-to-open-responses-input.ts +24 -8
- package/src/responses/open-responses-api.ts +83 -4
- package/src/responses/open-responses-config.ts +10 -1
- package/src/responses/open-responses-language-model.ts +175 -18
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var VERSION = true ? "2.0.
|
|
2
|
+
var VERSION = true ? "2.0.52" : "0.0.0-test";
|
|
3
3
|
|
|
4
4
|
// src/open-responses-provider.ts
|
|
5
5
|
import {
|
|
@@ -188,6 +188,30 @@ function isOpenResponsesJSONValue(value) {
|
|
|
188
188
|
return isOpenResponsesJSONObject(value);
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
+
// src/open-responses-tools.ts
|
|
192
|
+
import {
|
|
193
|
+
createProviderDefinedToolFactory,
|
|
194
|
+
lazySchema,
|
|
195
|
+
zodSchema
|
|
196
|
+
} from "@ai-sdk/provider-utils";
|
|
197
|
+
import { z } from "zod/v4";
|
|
198
|
+
var customInputSchema = lazySchema(() => zodSchema(z.string()));
|
|
199
|
+
function createOpenResponsesTools({
|
|
200
|
+
customToolId
|
|
201
|
+
}) {
|
|
202
|
+
const customToolFactory = createProviderDefinedToolFactory({
|
|
203
|
+
id: customToolId,
|
|
204
|
+
inputSchema: customInputSchema
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
/**
|
|
208
|
+
* Creates a caller-executed OpenAI-compatible custom tool. Custom tools accept
|
|
209
|
+
* a raw string, optionally constrained by a grammar. Requires endpoint support.
|
|
210
|
+
*/
|
|
211
|
+
customTool: (options) => customToolFactory(options)
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
191
215
|
// src/responses/open-responses-language-model.ts
|
|
192
216
|
import {
|
|
193
217
|
APICallError
|
|
@@ -208,7 +232,7 @@ import {
|
|
|
208
232
|
WORKFLOW_SERIALIZE,
|
|
209
233
|
WORKFLOW_DESERIALIZE
|
|
210
234
|
} from "@ai-sdk/provider-utils";
|
|
211
|
-
import { z as
|
|
235
|
+
import { z as z4 } from "zod/v4";
|
|
212
236
|
|
|
213
237
|
// src/responses/convert-to-open-responses-input.ts
|
|
214
238
|
import {
|
|
@@ -224,7 +248,8 @@ async function convertToOpenResponsesInput({
|
|
|
224
248
|
providerOptionsName = "open-responses",
|
|
225
249
|
extensionRegistry,
|
|
226
250
|
providerToolsByName = /* @__PURE__ */ new Map(),
|
|
227
|
-
strictResponseInput = false
|
|
251
|
+
strictResponseInput = false,
|
|
252
|
+
customToolId
|
|
228
253
|
}) {
|
|
229
254
|
var _a, _b, _c, _d;
|
|
230
255
|
const input = [];
|
|
@@ -437,13 +462,24 @@ async function convertToOpenResponsesInput({
|
|
|
437
462
|
const argumentsValue = typeof part.input === "string" ? part.input : JSON.stringify(part.input);
|
|
438
463
|
const providerData = getProviderData(part, providerOptionsName);
|
|
439
464
|
const itemId = typeof (providerData == null ? void 0 : providerData.itemId) === "string" ? providerData.itemId : void 0;
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
465
|
+
const providerTool = providerToolsByName.get(part.toolName);
|
|
466
|
+
if (customToolId != null && (providerTool == null ? void 0 : providerTool.id) === customToolId) {
|
|
467
|
+
input.push({
|
|
468
|
+
type: "custom_tool_call",
|
|
469
|
+
...itemId != null && { id: itemId },
|
|
470
|
+
call_id: part.toolCallId,
|
|
471
|
+
name: part.toolName,
|
|
472
|
+
input: argumentsValue
|
|
473
|
+
});
|
|
474
|
+
} else {
|
|
475
|
+
input.push({
|
|
476
|
+
type: "function_call",
|
|
477
|
+
...itemId != null && { id: itemId },
|
|
478
|
+
call_id: part.toolCallId,
|
|
479
|
+
name: part.toolName,
|
|
480
|
+
arguments: argumentsValue
|
|
481
|
+
});
|
|
482
|
+
}
|
|
447
483
|
break;
|
|
448
484
|
}
|
|
449
485
|
}
|
|
@@ -567,7 +603,7 @@ async function convertToOpenResponsesInput({
|
|
|
567
603
|
}
|
|
568
604
|
}
|
|
569
605
|
input.push({
|
|
570
|
-
type: "function_call_output",
|
|
606
|
+
type: customToolId != null && (providerTool == null ? void 0 : providerTool.id) === customToolId ? "custom_tool_call_output" : "function_call_output",
|
|
571
607
|
call_id: part.toolCallId,
|
|
572
608
|
output: contentValue
|
|
573
609
|
});
|
|
@@ -686,16 +722,16 @@ function parseOutputTextAnnotations(value) {
|
|
|
686
722
|
}
|
|
687
723
|
|
|
688
724
|
// src/responses/open-responses-api.ts
|
|
689
|
-
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
690
|
-
import { z } from "zod/v4";
|
|
691
|
-
var openResponsesErrorSchema =
|
|
692
|
-
() =>
|
|
693
|
-
|
|
694
|
-
error:
|
|
695
|
-
message:
|
|
696
|
-
type:
|
|
697
|
-
param:
|
|
698
|
-
code:
|
|
725
|
+
import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
|
|
726
|
+
import { z as z2 } from "zod/v4";
|
|
727
|
+
var openResponsesErrorSchema = lazySchema2(
|
|
728
|
+
() => zodSchema2(
|
|
729
|
+
z2.object({
|
|
730
|
+
error: z2.object({
|
|
731
|
+
message: z2.string(),
|
|
732
|
+
type: z2.string().nullish(),
|
|
733
|
+
param: z2.string().nullish(),
|
|
734
|
+
code: z2.string().nullish()
|
|
699
735
|
})
|
|
700
736
|
})
|
|
701
737
|
)
|
|
@@ -721,28 +757,32 @@ function mapOpenResponsesFinishReason({
|
|
|
721
757
|
|
|
722
758
|
// src/responses/open-responses-language-model-options.ts
|
|
723
759
|
import {
|
|
724
|
-
lazySchema as
|
|
725
|
-
zodSchema as
|
|
760
|
+
lazySchema as lazySchema3,
|
|
761
|
+
zodSchema as zodSchema3
|
|
726
762
|
} from "@ai-sdk/provider-utils";
|
|
727
|
-
import { z as
|
|
728
|
-
var openResponsesLanguageModelOptions =
|
|
729
|
-
() =>
|
|
730
|
-
|
|
763
|
+
import { z as z3 } from "zod/v4";
|
|
764
|
+
var openResponsesLanguageModelOptions = lazySchema3(
|
|
765
|
+
() => zodSchema3(
|
|
766
|
+
z3.object({
|
|
731
767
|
/**
|
|
732
768
|
* Provider-native reasoning effort. The value is passed through to the
|
|
733
769
|
* endpoint and takes precedence over the top-level `reasoning` setting.
|
|
734
770
|
*/
|
|
735
|
-
reasoningEffort:
|
|
771
|
+
reasoningEffort: z3.string().nullish(),
|
|
736
772
|
/**
|
|
737
773
|
* Controls reasoning summary output from the model.
|
|
738
774
|
* Valid values: 'concise', 'detailed', 'auto'.
|
|
739
775
|
*/
|
|
740
|
-
reasoningSummary:
|
|
776
|
+
reasoningSummary: z3.enum(["concise", "detailed", "auto"]).nullish()
|
|
741
777
|
})
|
|
742
778
|
)
|
|
743
779
|
);
|
|
744
780
|
|
|
745
781
|
// src/responses/open-responses-language-model.ts
|
|
782
|
+
var defaultFailedResponseHandler = createJsonErrorResponseHandler({
|
|
783
|
+
errorSchema: openResponsesErrorSchema,
|
|
784
|
+
errorToMessage: (error) => error.error.message
|
|
785
|
+
});
|
|
746
786
|
var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
747
787
|
constructor(modelId, config) {
|
|
748
788
|
this.specificationVersion = "v4";
|
|
@@ -787,7 +827,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
787
827
|
toolChoice,
|
|
788
828
|
responseFormat
|
|
789
829
|
}) {
|
|
790
|
-
var _a, _b;
|
|
830
|
+
var _a, _b, _c;
|
|
791
831
|
const warnings = [];
|
|
792
832
|
if (stopSequences != null) {
|
|
793
833
|
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
@@ -810,13 +850,28 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
810
850
|
providerOptionsName: this.config.providerOptionsName,
|
|
811
851
|
extensionRegistry: this.extensionRegistry,
|
|
812
852
|
providerToolsByName,
|
|
813
|
-
strictResponseInput: this.config.strictResponseInput
|
|
853
|
+
strictResponseInput: this.config.strictResponseInput,
|
|
854
|
+
customToolId: this.config.customToolId
|
|
814
855
|
});
|
|
815
856
|
warnings.push(...inputWarnings);
|
|
816
857
|
const convertedTools = [];
|
|
817
858
|
const encodedProviderToolsByName = /* @__PURE__ */ new Map();
|
|
818
859
|
for (const tool of tools != null ? tools : []) {
|
|
819
860
|
if (tool.type === "provider") {
|
|
861
|
+
if (this.config.customToolId != null && tool.id === this.config.customToolId) {
|
|
862
|
+
const format = tool.args.format != null && typeof tool.args.format === "object" && !Array.isArray(tool.args.format) ? tool.args.format : void 0;
|
|
863
|
+
convertedTools.push({
|
|
864
|
+
type: "custom",
|
|
865
|
+
name: tool.name,
|
|
866
|
+
description: typeof tool.args.description === "string" ? tool.args.description : void 0,
|
|
867
|
+
format: format != null && "type" in format && format.type === "grammar" && "syntax" in format && (format.syntax === "regex" || format.syntax === "lark") && "definition" in format && typeof format.definition === "string" ? {
|
|
868
|
+
type: "grammar",
|
|
869
|
+
syntax: format.syntax,
|
|
870
|
+
definition: format.definition
|
|
871
|
+
} : format != null && "type" in format && format.type === "text" ? { type: "text" } : void 0
|
|
872
|
+
});
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
820
875
|
const extension = this.extensionRegistry.byProviderToolId.get(tool.id);
|
|
821
876
|
let encoded;
|
|
822
877
|
if (extension != null) {
|
|
@@ -863,7 +918,12 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
863
918
|
toolChoice.toolName
|
|
864
919
|
);
|
|
865
920
|
if (registeredTool == null) {
|
|
866
|
-
if (
|
|
921
|
+
if (this.config.customToolId != null && ((_a = providerToolsByName.get(toolChoice.toolName)) == null ? void 0 : _a.id) === this.config.customToolId) {
|
|
922
|
+
convertedToolChoice = {
|
|
923
|
+
type: "custom",
|
|
924
|
+
name: toolChoice.toolName
|
|
925
|
+
};
|
|
926
|
+
} else if (!providerToolsByName.has(toolChoice.toolName)) {
|
|
867
927
|
convertedToolChoice = {
|
|
868
928
|
type: "function",
|
|
869
929
|
name: toolChoice.toolName
|
|
@@ -895,19 +955,22 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
895
955
|
} else {
|
|
896
956
|
convertedToolChoice = toolChoice == null ? void 0 : toolChoice.type;
|
|
897
957
|
}
|
|
898
|
-
const textFormat = (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
958
|
+
const textFormat = (responseFormat == null ? void 0 : responseFormat.type) === "json" && this.config.structuredOutputs !== false ? responseFormat.schema != null ? {
|
|
899
959
|
type: "json_schema",
|
|
900
|
-
name: (
|
|
960
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
901
961
|
description: responseFormat.description,
|
|
902
962
|
schema: responseFormat.schema,
|
|
903
963
|
strict: true
|
|
904
964
|
} : { type: "json_object" } : void 0;
|
|
965
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && this.config.structuredOutputs === false) {
|
|
966
|
+
warnings.push({ type: "unsupported", feature: "responseFormat" });
|
|
967
|
+
}
|
|
905
968
|
const openResponsesOptions = await parseProviderOptions({
|
|
906
969
|
provider: this.config.providerOptionsName,
|
|
907
970
|
providerOptions,
|
|
908
971
|
schema: openResponsesLanguageModelOptions
|
|
909
972
|
});
|
|
910
|
-
const resolvedReasoningEffort = (
|
|
973
|
+
const resolvedReasoningEffort = (_c = openResponsesOptions == null ? void 0 : openResponsesOptions.reasoningEffort) != null ? _c : isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
911
974
|
reasoning,
|
|
912
975
|
effortMap: {
|
|
913
976
|
minimal: "low",
|
|
@@ -944,7 +1007,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
944
1007
|
};
|
|
945
1008
|
}
|
|
946
1009
|
async doGenerate(options) {
|
|
947
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
1010
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
948
1011
|
const { body, warnings } = await this.getArgs(options);
|
|
949
1012
|
const {
|
|
950
1013
|
responseHeaders,
|
|
@@ -954,10 +1017,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
954
1017
|
url: this.config.url,
|
|
955
1018
|
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
956
1019
|
body,
|
|
957
|
-
failedResponseHandler:
|
|
958
|
-
errorSchema: openResponsesErrorSchema,
|
|
959
|
-
errorToMessage: (error) => error.error.message
|
|
960
|
-
}),
|
|
1020
|
+
failedResponseHandler: (_c = this.config.failedResponseHandler) != null ? _c : defaultFailedResponseHandler,
|
|
961
1021
|
successfulResponseHandler: createJsonResponseHandler(
|
|
962
1022
|
// do not validate the response body, only apply types to the response body
|
|
963
1023
|
jsonSchema(() => {
|
|
@@ -968,18 +1028,23 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
968
1028
|
fetch: this.config.fetch
|
|
969
1029
|
});
|
|
970
1030
|
if (response.error) {
|
|
1031
|
+
const errorMetadata = (_e = (_d = this.config).getResponseErrorMetadata) == null ? void 0 : _e.call(
|
|
1032
|
+
_d,
|
|
1033
|
+
response.error
|
|
1034
|
+
);
|
|
971
1035
|
throw new APICallError({
|
|
972
1036
|
message: response.error.message,
|
|
973
1037
|
url: this.config.url,
|
|
974
1038
|
requestBodyValues: body,
|
|
975
|
-
statusCode: 400,
|
|
1039
|
+
statusCode: (_f = errorMetadata == null ? void 0 : errorMetadata.statusCode) != null ? _f : 400,
|
|
1040
|
+
isRetryable: errorMetadata == null ? void 0 : errorMetadata.isRetryable,
|
|
976
1041
|
responseHeaders,
|
|
977
1042
|
responseBody: rawResponse,
|
|
978
|
-
|
|
1043
|
+
data: response.error
|
|
979
1044
|
});
|
|
980
1045
|
}
|
|
981
1046
|
if (response.output == null) {
|
|
982
|
-
const detail = (
|
|
1047
|
+
const detail = (_h = (_g = response.incomplete_details) == null ? void 0 : _g.reason) != null ? _h : response.status;
|
|
983
1048
|
throw new APICallError({
|
|
984
1049
|
message: detail ? `Responses API returned no output (${detail})` : "Responses API returned no output",
|
|
985
1050
|
url: this.config.url,
|
|
@@ -996,7 +1061,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
996
1061
|
switch (part.type) {
|
|
997
1062
|
// TODO AI SDK 7 adjust reasoning in the specification to better support the reasoning structure from open responses.
|
|
998
1063
|
case "reasoning": {
|
|
999
|
-
if (((
|
|
1064
|
+
if (((_j = (_i = part.content) == null ? void 0 : _i.length) != null ? _j : 0) > 0) {
|
|
1000
1065
|
for (const contentPart of part.content) {
|
|
1001
1066
|
content.push({
|
|
1002
1067
|
type: "reasoning",
|
|
@@ -1049,6 +1114,19 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1049
1114
|
});
|
|
1050
1115
|
break;
|
|
1051
1116
|
}
|
|
1117
|
+
case "custom_tool_call": {
|
|
1118
|
+
hasToolCalls = true;
|
|
1119
|
+
content.push({
|
|
1120
|
+
type: "tool-call",
|
|
1121
|
+
toolCallId: part.call_id,
|
|
1122
|
+
toolName: part.name,
|
|
1123
|
+
input: JSON.stringify(part.input),
|
|
1124
|
+
providerMetadata: {
|
|
1125
|
+
[this.config.providerOptionsName]: { itemId: part.id }
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1052
1130
|
default: {
|
|
1053
1131
|
if (!isOpenResponsesExtensionItem(part)) {
|
|
1054
1132
|
break;
|
|
@@ -1069,24 +1147,25 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1069
1147
|
}
|
|
1070
1148
|
const usage = response.usage;
|
|
1071
1149
|
const inputTokens = usage == null ? void 0 : usage.input_tokens;
|
|
1072
|
-
const cachedInputTokens = (
|
|
1150
|
+
const cachedInputTokens = (_k = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _k.cached_tokens;
|
|
1151
|
+
const cacheWriteTokens = (_l = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _l.cache_write_tokens;
|
|
1073
1152
|
const outputTokens = usage == null ? void 0 : usage.output_tokens;
|
|
1074
|
-
const reasoningTokens = (
|
|
1153
|
+
const reasoningTokens = (_m = usage == null ? void 0 : usage.output_tokens_details) == null ? void 0 : _m.reasoning_tokens;
|
|
1075
1154
|
return {
|
|
1076
1155
|
content,
|
|
1077
1156
|
finishReason: {
|
|
1078
1157
|
unified: mapOpenResponsesFinishReason({
|
|
1079
|
-
finishReason: (
|
|
1158
|
+
finishReason: (_n = response.incomplete_details) == null ? void 0 : _n.reason,
|
|
1080
1159
|
hasToolCalls
|
|
1081
1160
|
}),
|
|
1082
|
-
raw: (
|
|
1161
|
+
raw: (_p = (_o = response.incomplete_details) == null ? void 0 : _o.reason) != null ? _p : void 0
|
|
1083
1162
|
},
|
|
1084
1163
|
usage: {
|
|
1085
1164
|
inputTokens: {
|
|
1086
1165
|
total: inputTokens,
|
|
1087
|
-
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0),
|
|
1166
|
+
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0) - (cacheWriteTokens != null ? cacheWriteTokens : 0),
|
|
1088
1167
|
cacheRead: cachedInputTokens,
|
|
1089
|
-
cacheWrite:
|
|
1168
|
+
cacheWrite: cacheWriteTokens
|
|
1090
1169
|
},
|
|
1091
1170
|
outputTokens: {
|
|
1092
1171
|
total: outputTokens,
|
|
@@ -1108,7 +1187,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1108
1187
|
};
|
|
1109
1188
|
}
|
|
1110
1189
|
async doStream(options) {
|
|
1111
|
-
var _a, _b;
|
|
1190
|
+
var _a, _b, _c;
|
|
1112
1191
|
const { body, warnings } = await this.getArgs(options);
|
|
1113
1192
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1114
1193
|
url: this.config.url,
|
|
@@ -1117,11 +1196,8 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1117
1196
|
...body,
|
|
1118
1197
|
stream: true
|
|
1119
1198
|
},
|
|
1120
|
-
failedResponseHandler:
|
|
1121
|
-
|
|
1122
|
-
errorToMessage: (error) => error.error.message
|
|
1123
|
-
}),
|
|
1124
|
-
successfulResponseHandler: createEventSourceResponseHandler(z3.any()),
|
|
1199
|
+
failedResponseHandler: (_c = this.config.failedResponseHandler) != null ? _c : defaultFailedResponseHandler,
|
|
1200
|
+
successfulResponseHandler: createEventSourceResponseHandler(z4.any()),
|
|
1125
1201
|
abortSignal: options.abortSignal,
|
|
1126
1202
|
fetch: this.config.fetch
|
|
1127
1203
|
});
|
|
@@ -1139,19 +1215,20 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1139
1215
|
}
|
|
1140
1216
|
};
|
|
1141
1217
|
const updateUsage = (responseUsage) => {
|
|
1142
|
-
var _a2, _b2;
|
|
1218
|
+
var _a2, _b2, _c2;
|
|
1143
1219
|
if (!responseUsage) {
|
|
1144
1220
|
return;
|
|
1145
1221
|
}
|
|
1146
1222
|
const inputTokens = responseUsage.input_tokens;
|
|
1147
1223
|
const cachedInputTokens = (_a2 = responseUsage.input_tokens_details) == null ? void 0 : _a2.cached_tokens;
|
|
1224
|
+
const cacheWriteTokens = (_b2 = responseUsage.input_tokens_details) == null ? void 0 : _b2.cache_write_tokens;
|
|
1148
1225
|
const outputTokens = responseUsage.output_tokens;
|
|
1149
|
-
const reasoningTokens = (
|
|
1226
|
+
const reasoningTokens = (_c2 = responseUsage.output_tokens_details) == null ? void 0 : _c2.reasoning_tokens;
|
|
1150
1227
|
usage.inputTokens = {
|
|
1151
1228
|
total: inputTokens,
|
|
1152
|
-
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0),
|
|
1229
|
+
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0) - (cacheWriteTokens != null ? cacheWriteTokens : 0),
|
|
1153
1230
|
cacheRead: cachedInputTokens,
|
|
1154
|
-
cacheWrite:
|
|
1231
|
+
cacheWrite: cacheWriteTokens
|
|
1155
1232
|
};
|
|
1156
1233
|
usage.outputTokens = {
|
|
1157
1234
|
total: outputTokens,
|
|
@@ -1167,9 +1244,11 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1167
1244
|
raw: void 0
|
|
1168
1245
|
};
|
|
1169
1246
|
const toolCallsByItemId = /* @__PURE__ */ new Map();
|
|
1247
|
+
const customToolCallsByItemId = /* @__PURE__ */ new Map();
|
|
1170
1248
|
const providerOptionsName = this.config.providerOptionsName;
|
|
1171
1249
|
const extensionRegistry = this.extensionRegistry;
|
|
1172
1250
|
const extensionStreamState = /* @__PURE__ */ new Map();
|
|
1251
|
+
const getResponseErrorMetadata = this.config.getResponseErrorMetadata;
|
|
1173
1252
|
return {
|
|
1174
1253
|
stream: response.pipeThrough(
|
|
1175
1254
|
new TransformStream({
|
|
@@ -1177,7 +1256,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1177
1256
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1178
1257
|
},
|
|
1179
1258
|
async transform(parseResult, controller) {
|
|
1180
|
-
var _a2, _b2,
|
|
1259
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
1181
1260
|
if (options.includeRawChunks) {
|
|
1182
1261
|
controller.enqueue({
|
|
1183
1262
|
type: "raw",
|
|
@@ -1236,8 +1315,8 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1236
1315
|
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "function_call") {
|
|
1237
1316
|
const toolCall = toolCallsByItemId.get(chunk.item.id);
|
|
1238
1317
|
const toolName = (_b2 = toolCall == null ? void 0 : toolCall.toolName) != null ? _b2 : chunk.item.name;
|
|
1239
|
-
const toolCallId = (
|
|
1240
|
-
const input =
|
|
1318
|
+
const toolCallId = (_c2 = toolCall == null ? void 0 : toolCall.toolCallId) != null ? _c2 : chunk.item.call_id;
|
|
1319
|
+
const input = chunk.item.arguments !== "" ? chunk.item.arguments : (_d = toolCall == null ? void 0 : toolCall.arguments) != null ? _d : "";
|
|
1241
1320
|
controller.enqueue({
|
|
1242
1321
|
type: "tool-call",
|
|
1243
1322
|
toolCallId,
|
|
@@ -1251,6 +1330,56 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1251
1330
|
});
|
|
1252
1331
|
hasToolCalls = true;
|
|
1253
1332
|
toolCallsByItemId.delete(chunk.item.id);
|
|
1333
|
+
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "custom_tool_call") {
|
|
1334
|
+
customToolCallsByItemId.set(chunk.item.id, {
|
|
1335
|
+
toolName: chunk.item.name,
|
|
1336
|
+
toolCallId: chunk.item.call_id,
|
|
1337
|
+
input: chunk.item.input
|
|
1338
|
+
});
|
|
1339
|
+
controller.enqueue({
|
|
1340
|
+
type: "tool-input-start",
|
|
1341
|
+
id: chunk.item.call_id,
|
|
1342
|
+
toolName: chunk.item.name
|
|
1343
|
+
});
|
|
1344
|
+
} else if (chunk.type === "response.custom_tool_call_input.delta") {
|
|
1345
|
+
const toolCall = customToolCallsByItemId.get(chunk.item_id);
|
|
1346
|
+
if (toolCall == null) {
|
|
1347
|
+
customToolCallsByItemId.set(chunk.item_id, {
|
|
1348
|
+
input: chunk.delta
|
|
1349
|
+
});
|
|
1350
|
+
} else {
|
|
1351
|
+
toolCall.input = ((_e = toolCall.input) != null ? _e : "") + chunk.delta;
|
|
1352
|
+
}
|
|
1353
|
+
controller.enqueue({
|
|
1354
|
+
type: "tool-input-delta",
|
|
1355
|
+
id: (_g = (_f = customToolCallsByItemId.get(chunk.item_id)) == null ? void 0 : _f.toolCallId) != null ? _g : chunk.item_id,
|
|
1356
|
+
delta: chunk.delta
|
|
1357
|
+
});
|
|
1358
|
+
} else if (chunk.type === "response.custom_tool_call_input.done") {
|
|
1359
|
+
const toolCall = customToolCallsByItemId.get(chunk.item_id);
|
|
1360
|
+
if (toolCall == null) {
|
|
1361
|
+
customToolCallsByItemId.set(chunk.item_id, {
|
|
1362
|
+
input: chunk.input
|
|
1363
|
+
});
|
|
1364
|
+
} else {
|
|
1365
|
+
toolCall.input = chunk.input;
|
|
1366
|
+
}
|
|
1367
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "custom_tool_call") {
|
|
1368
|
+
const toolCall = customToolCallsByItemId.get(chunk.item.id);
|
|
1369
|
+
const toolCallId = (_h = toolCall == null ? void 0 : toolCall.toolCallId) != null ? _h : chunk.item.call_id;
|
|
1370
|
+
const input = chunk.item.input !== "" ? chunk.item.input : (_i = toolCall == null ? void 0 : toolCall.input) != null ? _i : "";
|
|
1371
|
+
controller.enqueue({ type: "tool-input-end", id: toolCallId });
|
|
1372
|
+
controller.enqueue({
|
|
1373
|
+
type: "tool-call",
|
|
1374
|
+
toolCallId,
|
|
1375
|
+
toolName: (_j = toolCall == null ? void 0 : toolCall.toolName) != null ? _j : chunk.item.name,
|
|
1376
|
+
input: JSON.stringify(input),
|
|
1377
|
+
providerMetadata: {
|
|
1378
|
+
[providerOptionsName]: { itemId: chunk.item.id }
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
hasToolCalls = true;
|
|
1382
|
+
customToolCallsByItemId.delete(chunk.item.id);
|
|
1254
1383
|
} else if (chunk.type === "response.output_item.done" && isOpenResponsesExtensionItem(chunk.item)) {
|
|
1255
1384
|
try {
|
|
1256
1385
|
const decoded = await decodeExtensionItem({
|
|
@@ -1314,7 +1443,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1314
1443
|
}
|
|
1315
1444
|
});
|
|
1316
1445
|
} else if (chunk.type === "response.completed" || chunk.type === "response.incomplete") {
|
|
1317
|
-
const reason = (
|
|
1446
|
+
const reason = (_k = chunk.response.incomplete_details) == null ? void 0 : _k.reason;
|
|
1318
1447
|
finishReason = {
|
|
1319
1448
|
unified: mapOpenResponsesFinishReason({
|
|
1320
1449
|
finishReason: reason,
|
|
@@ -1326,7 +1455,7 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1326
1455
|
} else if (chunk.type === "response.failed") {
|
|
1327
1456
|
finishReason = {
|
|
1328
1457
|
unified: "error",
|
|
1329
|
-
raw: (
|
|
1458
|
+
raw: (_m = (_l = chunk.response.error) == null ? void 0 : _l.code) != null ? _m : chunk.response.status
|
|
1330
1459
|
};
|
|
1331
1460
|
updateUsage(chunk.response.usage);
|
|
1332
1461
|
if (chunk.response.error != null) {
|
|
@@ -1335,7 +1464,8 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1335
1464
|
error: createOpenResponsesStreamError({
|
|
1336
1465
|
type: chunk.type,
|
|
1337
1466
|
error: chunk.response.error,
|
|
1338
|
-
data: chunk
|
|
1467
|
+
data: chunk,
|
|
1468
|
+
getResponseErrorMetadata
|
|
1339
1469
|
})
|
|
1340
1470
|
});
|
|
1341
1471
|
}
|
|
@@ -1349,7 +1479,8 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1349
1479
|
error: createOpenResponsesStreamError({
|
|
1350
1480
|
type: chunk.type,
|
|
1351
1481
|
error: chunk.error,
|
|
1352
|
-
data: chunk
|
|
1482
|
+
data: chunk,
|
|
1483
|
+
getResponseErrorMetadata
|
|
1353
1484
|
})
|
|
1354
1485
|
});
|
|
1355
1486
|
}
|
|
@@ -1378,12 +1509,14 @@ var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
|
1378
1509
|
function createOpenResponsesStreamError({
|
|
1379
1510
|
type,
|
|
1380
1511
|
error,
|
|
1381
|
-
data
|
|
1512
|
+
data,
|
|
1513
|
+
getResponseErrorMetadata
|
|
1382
1514
|
}) {
|
|
1383
1515
|
return createProviderStreamError({
|
|
1384
1516
|
message: error.message,
|
|
1385
1517
|
type,
|
|
1386
1518
|
code: error.code,
|
|
1519
|
+
...getResponseErrorMetadata == null ? void 0 : getResponseErrorMetadata(error),
|
|
1387
1520
|
data
|
|
1388
1521
|
});
|
|
1389
1522
|
}
|
|
@@ -1496,19 +1629,24 @@ function getOutputTextAnnotations(value) {
|
|
|
1496
1629
|
|
|
1497
1630
|
// src/open-responses-provider.ts
|
|
1498
1631
|
function createOpenResponses(options) {
|
|
1632
|
+
var _a;
|
|
1499
1633
|
const providerName = options.name;
|
|
1634
|
+
const customToolId = (_a = options.customToolId) != null ? _a : "open-responses.custom";
|
|
1500
1635
|
const extensionRegistry = createOpenResponsesExtensionRegistry(
|
|
1501
1636
|
options.experimental_extensions
|
|
1502
1637
|
);
|
|
1503
|
-
const getHeaders = () =>
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1638
|
+
const getHeaders = () => {
|
|
1639
|
+
var _a2;
|
|
1640
|
+
return withUserAgentSuffix(
|
|
1641
|
+
{
|
|
1642
|
+
...options.apiKey ? {
|
|
1643
|
+
Authorization: `Bearer ${options.apiKey}`
|
|
1644
|
+
} : {},
|
|
1645
|
+
...typeof options.headers === "function" ? options.headers() : options.headers
|
|
1646
|
+
},
|
|
1647
|
+
(_a2 = options.userAgentSuffix) != null ? _a2 : `ai-sdk/open-responses/${VERSION}`
|
|
1648
|
+
);
|
|
1649
|
+
};
|
|
1512
1650
|
const createResponsesModel = (modelId) => {
|
|
1513
1651
|
return new OpenResponsesLanguageModel(modelId, {
|
|
1514
1652
|
provider: `${providerName}.responses`,
|
|
@@ -1516,9 +1654,13 @@ function createOpenResponses(options) {
|
|
|
1516
1654
|
headers: getHeaders,
|
|
1517
1655
|
url: options.url,
|
|
1518
1656
|
fetch: options.fetch,
|
|
1657
|
+
failedResponseHandler: options.failedResponseHandler,
|
|
1658
|
+
getResponseErrorMetadata: options.getResponseErrorMetadata,
|
|
1519
1659
|
generateId: () => generateId(),
|
|
1520
1660
|
extensionRegistry,
|
|
1521
|
-
strictResponseInput: options.strictResponseInput
|
|
1661
|
+
strictResponseInput: options.strictResponseInput,
|
|
1662
|
+
customToolId,
|
|
1663
|
+
structuredOutputs: options.structuredOutputs
|
|
1522
1664
|
});
|
|
1523
1665
|
};
|
|
1524
1666
|
const createLanguageModel = (modelId) => {
|
|
@@ -1534,6 +1676,7 @@ function createOpenResponses(options) {
|
|
|
1534
1676
|
};
|
|
1535
1677
|
provider.specificationVersion = "v4";
|
|
1536
1678
|
provider.languageModel = createLanguageModel;
|
|
1679
|
+
provider.tools = createOpenResponsesTools({ customToolId });
|
|
1537
1680
|
provider.embeddingModel = (modelId) => {
|
|
1538
1681
|
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
1539
1682
|
};
|