@openclaw/ai 2026.7.2-beta.4 → 2026.7.2-beta.5

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.
Files changed (55) hide show
  1. package/dist/{anthropic-CrUDIBpM.mjs → anthropic-CQVj3le6.mjs} +53 -19
  2. package/dist/{anthropic-BoTnz8cv.d.mts → anthropic-SrGtwsJu.d.mts} +27 -1
  3. package/dist/{api-registry-BMphkFf1.d.mts → api-registry-DlMgPR39.d.mts} +1 -1
  4. package/dist/{azure-openai-responses-CbrpVeEv.mjs → azure-openai-responses-DIYgsqFM.mjs} +3 -4
  5. package/dist/error-coercion-DgxlWC0n.mjs +15 -0
  6. package/dist/{event-stream-Douf9dob.d.mts → event-stream-YjaPW20U.d.mts} +1 -1
  7. package/dist/event-stream.d.mts +1 -1
  8. package/dist/{tool-schema-json-projection-BwNu3nDi.mjs → github-copilot-headers-BCoBNmL7.mjs} +48 -1
  9. package/dist/{google-Cs62KA7t.mjs → google-f-A8xrae.mjs} +2 -2
  10. package/dist/{google-shared-CMLI-tCZ.mjs → google-shared-J6qvYINH.mjs} +5 -4
  11. package/dist/{google-vertex-B4SD3U0f.mjs → google-vertex-D3yMVXIY.mjs} +2 -2
  12. package/dist/{host-WvWBo4h8.d.mts → host-B9GUmcra.d.mts} +2 -2
  13. package/dist/index.d.mts +7 -7
  14. package/dist/index.mjs +2 -2
  15. package/dist/internal/anthropic.d.mts +15 -11
  16. package/dist/internal/anthropic.mjs +4 -4
  17. package/dist/internal/openai.d.mts +2 -1
  18. package/dist/internal/openai.mjs +4 -6
  19. package/dist/internal/runtime.d.mts +8 -32
  20. package/dist/internal/runtime.mjs +2 -3
  21. package/dist/internal/shared.d.mts +13 -8
  22. package/dist/internal/shared.mjs +2 -2
  23. package/dist/{mistral-D5Ps7Led.mjs → mistral-CKV-TOQj.mjs} +4 -3
  24. package/dist/number-coercion-DvG7SNMg.mjs +129 -0
  25. package/dist/{openai-CoGicoDt.d.mts → openai-BPor_3WI.d.mts} +35 -9
  26. package/dist/{openai-responses-shared-CzCurmY1.mjs → openai-D3PD6PE-.mjs} +1771 -745
  27. package/dist/{openai-chatgpt-responses-h0o5yV8Y.mjs → openai-chatgpt-responses-CedIj0hk.mjs} +19 -20
  28. package/dist/{openai-completions-exO8NFQf.mjs → openai-completions-CiSutyu0.mjs} +366 -227
  29. package/dist/openai-prompt-cache-2uo_1OR1.d.mts +7 -0
  30. package/dist/openai-prompt-cache-mZTCdRPo.mjs +12 -0
  31. package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +13 -0
  32. package/dist/{provider-error-C4VvV_3t.mjs → provider-error-apVOZI6G.mjs} +8 -2
  33. package/dist/providers.d.mts +3 -2
  34. package/dist/providers.mjs +10 -9
  35. package/dist/{system-prompt-cache-boundary-CbHeV4_l.mjs → shared-CdjNZd35.mjs} +117 -9
  36. package/dist/{src-CXno1H5g.mjs → src-QkygScBs.mjs} +23 -4
  37. package/dist/stream-first-event-timeout-C3OgBjIk.mjs +12294 -0
  38. package/dist/stream-first-event-timeout-DvDeSucC.d.mts +29 -0
  39. package/dist/transport-stream-shared-BbMELSI4.mjs +297 -0
  40. package/dist/transports.d.mts +105 -102
  41. package/dist/transports.mjs +334 -1966
  42. package/dist/{types-AFwwWium.d.mts → types-bzp5k29J.d.mts} +7 -0
  43. package/dist/types.d.mts +5 -5
  44. package/dist/types.mjs +2 -2
  45. package/dist/{validation-sxvxC8J-.d.mts → validation-B-j7cOYp.d.mts} +1 -1
  46. package/dist/validation.d.mts +1 -1
  47. package/package.json +1 -2
  48. package/dist/azure-openai-responses-client-compat-C7K7QfUE.mjs +0 -62
  49. package/dist/github-copilot-headers-BsH5cqGj.mjs +0 -48
  50. package/dist/model-utils-1GiZ2_rr.mjs +0 -64
  51. package/dist/openai-responses-BHpmtUKo.mjs +0 -136
  52. package/dist/openai-tool-projection-ITOU9bG1.mjs +0 -200
  53. package/dist/reasoning-tag-text-partitioner-BlVe67g6.mjs +0 -400
  54. package/dist/stream-first-event-timeout-DP4xEyBY.mjs +0 -150
  55. package/npm-shrinkwrap.json +0 -638
@@ -1,13 +1,57 @@
1
+ import { n as getEnvApiKey, r as __exportAll } from "./env-api-keys-DrgeBuva.mjs";
2
+ import { t as AssistantMessageEventStream } from "./event-stream-D8n2uFee.mjs";
1
3
  import { n as getAiTransportHost } from "./host-XYGZcgO8.mjs";
2
- import { C as isImageWithMediaPayload, T as isRecord, _ as normalizeOptionalString, a as stripSystemPromptCacheBoundary, c as transformMessages, g as normalizeLowercaseStringOrEmpty, x as extractToolResultText, y as describeToolResultMediaPlaceholder } from "./system-prompt-cache-boundary-CbHeV4_l.mjs";
4
+ import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
5
+ import { A as buildBaseOptions, D as truncateUtf16Safe, E as isImageWithMediaPayload, O as isRecord, S as describeToolResultMediaPlaceholder, b as normalizeOptionalString, l as sortPromptCacheToolsByName, o as stripSystemPromptCacheBoundary, u as transformMessages, w as extractToolResultText, y as normalizeLowercaseStringOrEmpty } from "./shared-CdjNZd35.mjs";
3
6
  import { t as sanitizeSurrogates } from "./sanitize-unicode-DT5o51ur.mjs";
4
- import { n as calculateCost, r as clampThinkingLevel } from "./model-utils-1GiZ2_rr.mjs";
7
+ import { C as transformTransportMessages, a as projectOpenAITools } from "./openai-completions-CiSutyu0.mjs";
8
+ import { c as calculateCost, l as clampThinkingLevel } from "./number-coercion-DvG7SNMg.mjs";
9
+ import { b as redactIdentifier, d as transportAbortError, l as sanitizeNonEmptyTransportPayloadText, u as sanitizeTransportPayloadText, x as redactSensitiveText } from "./transport-stream-shared-BbMELSI4.mjs";
5
10
  import { t as headersToRecord } from "./headers-B_e4-1J0.mjs";
6
11
  import { n as parseStreamingJson } from "./json-parse-BvXNt1-7.mjs";
12
+ import { a as resolveCacheRetention$1, i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, r as isCloudflareProvider, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-BCoBNmL7.mjs";
7
13
  import { t as shortHash } from "./hash-CHgqbJmD.mjs";
8
- import { a as withFirstStreamEventTimeout, i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-DP4xEyBY.mjs";
9
- import { t as projectOpenAITools } from "./openai-tool-projection-ITOU9bG1.mjs";
14
+ import { a as withFirstStreamEventTimeout, i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-C3OgBjIk.mjs";
10
15
  import { createHash, randomUUID } from "node:crypto";
16
+ import OpenAI from "openai";
17
+ //#region packages/ai/src/transports/model-transport-debug.ts
18
+ function normalizeEnv(value) {
19
+ return typeof value === "string" ? value.trim().toLowerCase() : "";
20
+ }
21
+ function isTruthyEnv(value) {
22
+ const normalized = normalizeEnv(value);
23
+ return normalized.length > 0 && normalized !== "0" && normalized !== "false" && normalized !== "off" && normalized !== "no";
24
+ }
25
+ /** Resolves model payload debug verbosity from `OPENCLAW_DEBUG_MODEL_PAYLOAD`. */
26
+ function resolveModelPayloadDebugMode(env = process.env) {
27
+ const normalized = normalizeEnv(env.OPENCLAW_DEBUG_MODEL_PAYLOAD);
28
+ if (normalized === "tools" || normalized === "full-redacted") return normalized;
29
+ if (normalized === "summary") return "summary";
30
+ return "off";
31
+ }
32
+ /** Resolves SSE stream debug verbosity from `OPENCLAW_DEBUG_SSE`. */
33
+ function resolveModelSseDebugMode(env = process.env) {
34
+ const normalized = normalizeEnv(env.OPENCLAW_DEBUG_SSE);
35
+ if (normalized === "peek") return "peek";
36
+ if (normalized === "events" || isTruthyEnv(normalized)) return "events";
37
+ return "off";
38
+ }
39
+ /** Returns whether any model transport debug channel is enabled. */
40
+ function isModelTransportDebugEnabled(env = process.env) {
41
+ return isTruthyEnv(env.OPENCLAW_DEBUG_MODEL_TRANSPORT) || resolveModelPayloadDebugMode(env) !== "off" || resolveModelSseDebugMode(env) !== "off" || isTruthyEnv(env.OPENCLAW_DEBUG_CODE_MODE);
42
+ }
43
+ function isModelFetchMetadataMessage(message) {
44
+ return message.startsWith("[model-fetch]");
45
+ }
46
+ /** Emits model-fetch metadata at info level by default; other diagnostics require debug env. */
47
+ function emitModelTransportDebug(log, message) {
48
+ if (isModelFetchMetadataMessage(message) || isModelTransportDebugEnabled()) {
49
+ log.info(message);
50
+ return;
51
+ }
52
+ log.debug(message);
53
+ }
54
+ //#endregion
11
55
  //#region packages/normalization-core/src/string-normalization.ts
12
56
  /** Coerces entries to strings, trims them, and drops empty results. */
13
57
  function normalizeStringEntries(list) {
@@ -896,6 +940,67 @@ function normalizeToolParameterSchema(schema, options) {
896
940
  return rememberCachedToolParameterSchema(schema, cacheKey, normalizeToolParameterSchemaUncached(schema, options));
897
941
  }
898
942
  //#endregion
943
+ //#region packages/ai/src/providers/azure-deployment-map.ts
944
+ /** Parses AZURE_OPENAI_DEPLOYMENT_MAP-style model=deployment entries. */
945
+ function parseAzureDeploymentNameMap(value) {
946
+ const map = /* @__PURE__ */ new Map();
947
+ if (!value) return map;
948
+ for (const entry of value.split(",")) {
949
+ const trimmed = entry.trim();
950
+ if (!trimmed) continue;
951
+ const separator = trimmed.indexOf("=");
952
+ if (separator <= 0) continue;
953
+ const modelId = trimmed.slice(0, separator).trim();
954
+ const deploymentName = trimmed.slice(separator + 1).trim();
955
+ if (!modelId || !deploymentName) continue;
956
+ map.set(modelId, deploymentName);
957
+ }
958
+ return map;
959
+ }
960
+ let cachedDeploymentLookup;
961
+ function getDeploymentLookup(source) {
962
+ const cached = cachedDeploymentLookup;
963
+ if (cached && cached.source === source) return cached;
964
+ const exact = parseAzureDeploymentNameMap(source);
965
+ const folded = /* @__PURE__ */ new Map();
966
+ for (const [modelId, deploymentName] of exact) folded.set(modelId.toLowerCase(), deploymentName);
967
+ cachedDeploymentLookup = {
968
+ source,
969
+ exact,
970
+ folded
971
+ };
972
+ return cachedDeploymentLookup;
973
+ }
974
+ /**
975
+ * Resolves the Azure deployment name for a model id, falling back to the model id.
976
+ *
977
+ * An exact-case match always wins, so configs that intentionally distinguish keys by
978
+ * case keep their exact mappings; a case-insensitive match is only used as a fallback
979
+ * (e.g. `GPT-4o` against a `gpt-4o=...` map) to avoid 404s from casing differences.
980
+ */
981
+ function resolveAzureDeploymentNameFromMap(params) {
982
+ const { exact, folded } = getDeploymentLookup(params.deploymentMap);
983
+ return exact.get(params.modelId) ?? folded.get(params.modelId.toLowerCase()) ?? params.modelId;
984
+ }
985
+ //#endregion
986
+ //#region packages/ai/src/providers/azure-openai-responses-client-compat.ts
987
+ function isTraditionalAzureOpenAIHost(hostname) {
988
+ return hostname.endsWith(".openai.azure.com") || hostname.endsWith(".cognitiveservices.azure.com");
989
+ }
990
+ function isOpenAICompatibleAzureResponsesBaseUrl(baseUrl) {
991
+ let url;
992
+ try {
993
+ url = new URL(baseUrl);
994
+ } catch {
995
+ return false;
996
+ }
997
+ if (isTraditionalAzureOpenAIHost(url.hostname)) return false;
998
+ const hostname = url.hostname.toLowerCase();
999
+ if (!(hostname.endsWith(".services.ai.azure.com") || hostname.endsWith(".api.cognitive.microsoft.com"))) return false;
1000
+ const normalizedPath = url.pathname.replace(/\/+$/, "");
1001
+ return normalizedPath === "/openai/v1" || normalizedPath.endsWith("/openai/v1");
1002
+ }
1003
+ //#endregion
899
1004
  //#region packages/ai/src/providers/openai-reasoning-effort.ts
900
1005
  /**
901
1006
  * OpenAI-compatible reasoning-effort normalization. Different GPT families
@@ -1076,66 +1181,6 @@ function resolveResponsesMessageSnapshotCollapse(params) {
1076
1181
  return { kind: "keep" };
1077
1182
  }
1078
1183
  //#endregion
1079
- //#region packages/ai/src/providers/openai-responses-terminal-usage.ts
1080
- function readCount(value) {
1081
- return typeof value === "number" && Number.isFinite(value) ? value : 0;
1082
- }
1083
- /**
1084
- * Split a terminal usage payload into the priced buckets.
1085
- *
1086
- * OpenAI includes cache reads and writes in `input_tokens`, so both are subtracted out of the
1087
- * billable input bucket. `total_tokens` comes from the payload, but never below the sum of the
1088
- * split buckets: proxies routinely omit it (reporting 0 would understate the turn), and a payload
1089
- * whose `cached_tokens` exceeds `input_tokens` clamps the input bucket, leaving the reported total
1090
- * short of what the buckets actually price.
1091
- */
1092
- function mapResponsesTerminalUsage(usage) {
1093
- if (!usage) return;
1094
- const cacheRead = readCount(usage.input_tokens_details?.cached_tokens);
1095
- const cacheWrite = readCount(usage.input_tokens_details?.cache_write_tokens);
1096
- const input = Math.max(0, readCount(usage.input_tokens) - cacheRead - cacheWrite);
1097
- const output = readCount(usage.output_tokens);
1098
- const bucketTotal = input + output + cacheRead + cacheWrite;
1099
- return {
1100
- input,
1101
- output,
1102
- cacheRead,
1103
- cacheWrite,
1104
- totalTokens: Math.max(bucketTotal, readCount(usage.total_tokens))
1105
- };
1106
- }
1107
- /** Reasoning tokens are reported by the agent path only; the package path does not track them. */
1108
- function readResponsesReasoningTokens(usage) {
1109
- const reasoningTokens = usage?.output_tokens_details?.reasoning_tokens;
1110
- return typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? reasoningTokens : void 0;
1111
- }
1112
- function mapResponsesTerminalStopReason(status) {
1113
- if (!status) return "stop";
1114
- switch (status) {
1115
- case "completed": return "stop";
1116
- case "incomplete": return "length";
1117
- case "failed":
1118
- case "cancelled": return "error";
1119
- case "in_progress":
1120
- case "queued": return "stop";
1121
- default: throw new Error(`Unhandled stop reason: ${String(status)}`);
1122
- }
1123
- }
1124
- /**
1125
- * Resolve the terminal stop reason, including the two overrides every Responses path shares: a
1126
- * content-filtered turn is a provider error rather than a truncated answer, and a turn that
1127
- * produced tool calls reports `toolUse` instead of a plain stop.
1128
- */
1129
- function resolveResponsesTerminalStopReason(params) {
1130
- if (params.status === "incomplete" && params.incompleteReason === "content_filter") return {
1131
- stopReason: "error",
1132
- errorMessage: "Provider incomplete_reason: content_filter"
1133
- };
1134
- const stopReason = mapResponsesTerminalStopReason(params.status);
1135
- if (stopReason === "stop" && params.hasToolCall) return { stopReason: "toolUse" };
1136
- return { stopReason };
1137
- }
1138
- //#endregion
1139
1184
  //#region packages/ai/src/providers/openai-responses-tool-call-tracker.ts
1140
1185
  function readIdentityValue(value) {
1141
1186
  return (typeof value === "string" ? value.trim() : "") || void 0;
@@ -1211,175 +1256,1556 @@ function createResponsesToolCallTracker() {
1211
1256
  }
1212
1257
  };
1213
1258
  }
1259
+ const OPENAI_CODEX_RESPONSES_DEFAULT_INSTRUCTIONS = "Follow the user request.";
1260
+ const AZURE_RESPONSES_FIRST_EVENT_TIMEOUT_MS = 3e4;
1261
+ const RESPONSE_FAILED_NO_DETAILS_MESSAGE = "Unknown error (no error details in response)";
1262
+ const OPENAI_RESPONSES_REASONING_REPLAY_META_KEY = "__openclaw_replay";
1263
+ const OPENAI_RESPONSES_REASONING_REPLAY_BLOCK_META_KEY = "openclawReasoningReplay";
1214
1264
  //#endregion
1215
- //#region packages/ai/src/providers/openai-tool-schema-compat.ts
1216
- const OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS = /* @__PURE__ */ new Set([
1217
- "$defs",
1218
- "definitions",
1219
- "dependentSchemas",
1220
- "dependencies",
1221
- "patternProperties",
1222
- "properties"
1223
- ]);
1224
- const OPENAI_NULLABLE_ANNOTATION_KEYS = /* @__PURE__ */ new Set([
1225
- "default",
1226
- "description",
1227
- "examples",
1228
- "format",
1229
- "title"
1230
- ]);
1231
- const OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS = /* @__PURE__ */ new Set([
1232
- "additionalItems",
1233
- "additionalProperties",
1234
- "allOf",
1235
- "anyOf",
1236
- "contains",
1237
- "contentSchema",
1238
- "else",
1239
- "if",
1240
- "items",
1241
- "not",
1242
- "oneOf",
1243
- "prefixItems",
1244
- "propertyNames",
1245
- "then",
1246
- "unevaluatedItems",
1247
- "unevaluatedProperties"
1248
- ]);
1249
- function normalizeOpenAIStrictCompatSchemaMap(schema) {
1250
- if (!schema || typeof schema !== "object" || Array.isArray(schema)) return schema;
1251
- let changed = false;
1252
- const normalized = {};
1253
- for (const [key, value] of Object.entries(schema)) {
1254
- const next = normalizeOpenAIStrictCompatSchemaRecursive(value, { promoteEmptyObject: false });
1255
- normalized[key] = next;
1256
- changed ||= next !== value;
1265
+ //#region packages/ai/src/transports/openai-transport-shared.ts
1266
+ /** Shared options, usage shape, cache identity, ordering, and stream scheduling for OpenAI APIs. */
1267
+ const MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS = 12;
1268
+ const MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS = 64;
1269
+ const GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP = "skip_thought_signature_validator";
1270
+ const log = {
1271
+ debug(message, data) {
1272
+ getAiTransportHost().logDebug("openai-transport", () => ({
1273
+ message,
1274
+ data
1275
+ }));
1276
+ },
1277
+ info(message, data) {
1278
+ getAiTransportHost().logInfo("openai-transport", message, data);
1279
+ },
1280
+ warn(message, data) {
1281
+ getAiTransportHost().logWarn("openai-transport", message, data);
1257
1282
  }
1258
- return changed ? normalized : schema;
1259
- }
1260
- function normalizeOpenAIStrictCompatSchemaRecursive(schema, options) {
1261
- if (Array.isArray(schema)) {
1262
- let changed = false;
1263
- const normalized = schema.map((entry) => {
1264
- const next = normalizeOpenAIStrictCompatSchemaRecursive(entry, { promoteEmptyObject: false });
1265
- changed ||= next !== entry;
1266
- return next;
1283
+ };
1284
+ function throwIfModelStreamAborted(signal) {
1285
+ if (signal?.aborted) throw transportAbortError(signal);
1286
+ }
1287
+ function createModelStreamCooperativeScheduler(signal) {
1288
+ let lastYieldedAt = Date.now();
1289
+ let eventsSinceYield = 0;
1290
+ return { async afterEvent() {
1291
+ throwIfModelStreamAborted(signal);
1292
+ eventsSinceYield += 1;
1293
+ const now = Date.now();
1294
+ if (eventsSinceYield < MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS && now - lastYieldedAt < MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS) return;
1295
+ eventsSinceYield = 0;
1296
+ lastYieldedAt = now;
1297
+ await new Promise((resolve) => {
1298
+ setTimeout(resolve, 0);
1267
1299
  });
1268
- return changed ? normalized : schema;
1269
- }
1270
- if (!schema || typeof schema !== "object") return schema;
1271
- const record = schema;
1272
- let changed = false;
1273
- let hadNullType = false;
1274
- const normalized = {};
1275
- for (const [key, value] of Object.entries(record)) {
1276
- if (value === null && OPENAI_NULLABLE_ANNOTATION_KEYS.has(key)) {
1277
- changed = true;
1278
- continue;
1279
- }
1280
- if (value === null && key === "type") {
1281
- hadNullType = true;
1282
- changed = true;
1283
- continue;
1284
- }
1285
- const next = OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS.has(key) ? normalizeOpenAIStrictCompatSchemaMap(value) : OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS.has(key) ? normalizeOpenAIStrictCompatSchemaRecursive(value, { promoteEmptyObject: false }) : value;
1286
- normalized[key] = next;
1287
- changed ||= next !== value;
1300
+ throwIfModelStreamAborted(signal);
1301
+ } };
1302
+ }
1303
+ function resolveCacheRetention(cacheRetention) {
1304
+ if (cacheRetention === "short" || cacheRetention === "long" || cacheRetention === "none") return cacheRetention;
1305
+ if (typeof process !== "undefined" && process.env.OPENCLAW_CACHE_RETENTION === "long") return "long";
1306
+ return "short";
1307
+ }
1308
+ function resolvePromptCacheKey(options, cacheRetention) {
1309
+ if (cacheRetention === "none") return;
1310
+ return clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId);
1311
+ }
1312
+ //#endregion
1313
+ //#region packages/ai/src/transports/openai-responses-debug.ts
1314
+ function stringifyUnknown(value, fallback = "") {
1315
+ if (typeof value === "string") return value;
1316
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
1317
+ return fallback;
1318
+ }
1319
+ function getServiceTierCostMultiplier$1(serviceTier) {
1320
+ switch (serviceTier) {
1321
+ case "flex": return .5;
1322
+ case "priority": return 2;
1323
+ default: return 1;
1288
1324
  }
1289
- if (Object.keys(normalized).length === 0) {
1290
- if (!options.promoteEmptyObject) return schema;
1291
- return {
1292
- type: "object",
1293
- properties: {},
1294
- required: [],
1295
- additionalProperties: false
1296
- };
1325
+ }
1326
+ function applyServiceTierPricing$1(usage, serviceTier) {
1327
+ const multiplier = getServiceTierCostMultiplier$1(serviceTier);
1328
+ if (multiplier === 1) return;
1329
+ usage.cost.input *= multiplier;
1330
+ usage.cost.output *= multiplier;
1331
+ usage.cost.cacheRead *= multiplier;
1332
+ usage.cost.cacheWrite *= multiplier;
1333
+ usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
1334
+ }
1335
+ function safeDebugValue(value) {
1336
+ if (typeof value === "string") return value;
1337
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
1338
+ if (value === null) return "null";
1339
+ if (value === void 0) return "undefined";
1340
+ return Array.isArray(value) ? "array" : typeof value;
1341
+ }
1342
+ function responseInputTextChars(input) {
1343
+ if (typeof input === "string") return input.length;
1344
+ if (Array.isArray(input)) return input.reduce((total, item) => total + responseInputTextChars(item), 0);
1345
+ if (!input || typeof input !== "object") return 0;
1346
+ const record = input;
1347
+ let total = 0;
1348
+ if (typeof record.text === "string") total += record.text.length;
1349
+ if (typeof record.content === "string") total += record.content.length;
1350
+ else if (Array.isArray(record.content)) total += responseInputTextChars(record.content);
1351
+ return total;
1352
+ }
1353
+ function responseInputRoles(input) {
1354
+ if (!Array.isArray(input)) return "";
1355
+ const roles = /* @__PURE__ */ new Set();
1356
+ for (const item of input) if (item && typeof item === "object") {
1357
+ const role = item.role;
1358
+ if (typeof role === "string" && role.trim()) roles.add(role.trim());
1297
1359
  }
1298
- const hasObjectShapeHints = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) || Array.isArray(normalized.required);
1299
- const hasArrayShapeHints = "items" in normalized;
1300
- if (!("type" in normalized) && hasObjectShapeHints !== hasArrayShapeHints) {
1301
- normalized.type = hasObjectShapeHints ? "object" : "array";
1302
- changed = true;
1303
- } else if (hadNullType && !("type" in normalized)) normalized.type = null;
1304
- if (normalized.type === "object" && !("properties" in normalized)) {
1305
- normalized.properties = {};
1306
- changed = true;
1360
+ return [...roles].toSorted().join(",");
1361
+ }
1362
+ function readToolPayloadField(record, field) {
1363
+ try {
1364
+ return record[field];
1365
+ } catch {
1366
+ return;
1307
1367
  }
1308
- const hasEmptyProperties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) && Object.keys(normalized.properties).length === 0;
1309
- if (normalized.type === "object" && !Array.isArray(normalized.required) && hasEmptyProperties) {
1310
- normalized.required = [];
1311
- changed = true;
1368
+ }
1369
+ function readResponsesToolDisplayName(tool) {
1370
+ if (!tool || typeof tool !== "object") return "";
1371
+ const record = tool;
1372
+ const name = readToolPayloadField(record, "name");
1373
+ if (typeof name === "string") return name;
1374
+ const fn = readToolPayloadField(record, "function");
1375
+ if (fn && typeof fn === "object") {
1376
+ const fnName = readToolPayloadField(fn, "name");
1377
+ if (typeof fnName === "string") return fnName;
1312
1378
  }
1313
- if (normalized.type === "object" && hasEmptyProperties && !("additionalProperties" in normalized)) {
1314
- normalized.additionalProperties = false;
1315
- changed = true;
1379
+ const type = readToolPayloadField(record, "type");
1380
+ return typeof type === "string" && type !== "function" ? type : "";
1381
+ }
1382
+ function summarizeResponsesTools(tools) {
1383
+ if (!Array.isArray(tools)) return "count=0";
1384
+ const names = tools.map(readResponsesToolDisplayName).filter(Boolean);
1385
+ const mode = resolveModelPayloadDebugMode();
1386
+ const maxNames = mode === "tools" || mode === "full-redacted" ? names.length : 12;
1387
+ const label = maxNames >= names.length ? "names" : "sample";
1388
+ const shown = names.slice(0, maxNames).join(",");
1389
+ return `count=${tools.length}${shown ? ` ${label}=${shown}` : ""}`;
1390
+ }
1391
+ function stringifyRedactedPayload(value) {
1392
+ try {
1393
+ const encoded = JSON.stringify(value);
1394
+ if (!encoded) return "<empty>";
1395
+ const redacted = redactSensitiveText(encoded, { mode: "tools" });
1396
+ return redacted.length > 8e3 ? `${truncateUtf16Safe(redacted, 8e3)}…<truncated>` : redacted;
1397
+ } catch {
1398
+ return "<unserializable>";
1316
1399
  }
1317
- return changed ? normalized : schema;
1318
- }
1319
- /** Repairs recoverable OpenAI tool-schema shapes before canonical normalization. */
1320
- function normalizeOpenAIStrictCompatSchema(schema) {
1321
- return normalizeOpenAIStrictCompatSchemaRecursive(schema, { promoteEmptyObject: true });
1322
1400
  }
1323
- /** Finds schema paths that violate OpenAI strict tool-schema requirements. */
1324
- function findOpenAIStrictSchemaViolations(schema, path, options) {
1325
- if (Array.isArray(schema)) {
1326
- if (options?.requireObjectRoot) return [`${path}.type`];
1327
- return schema.flatMap((item, index) => findOpenAIStrictSchemaViolations(item, `${path}[${index}]`));
1328
- }
1329
- if (!schema || typeof schema !== "object") return options?.requireObjectRoot ? [`${path}.type`] : [];
1330
- const record = schema;
1331
- const violations = [];
1332
- for (const key of [
1333
- "anyOf",
1334
- "oneOf",
1335
- "allOf"
1336
- ]) if (key in record) violations.push(`${path}.${key}`);
1337
- if (Array.isArray(record.type)) violations.push(`${path}.type`);
1338
- const properties = record.properties && typeof record.properties === "object" && !Array.isArray(record.properties) ? record.properties : void 0;
1339
- if (record.type === "object") {
1340
- if (record.additionalProperties !== false) violations.push(`${path}.additionalProperties`);
1341
- const required = Array.isArray(record.required) ? record.required.filter((entry) => typeof entry === "string") : void 0;
1342
- if (!required) violations.push(`${path}.required`);
1343
- else if (properties) {
1344
- const requiredSet = new Set(required);
1345
- for (const key of Object.keys(properties)) if (!requiredSet.has(key)) violations.push(`${path}.required.${key}`);
1401
+ function stringifyRedactedEvent(value) {
1402
+ const redacted = stringifyRedactedPayload(value);
1403
+ return redacted.length > 2e3 ? `${truncateUtf16Safe(redacted, 2e3)}…<truncated>` : redacted;
1404
+ }
1405
+ const RESPONSE_FAILED_FAILURE_FIELD_KEYS = [
1406
+ "error",
1407
+ "incomplete_details",
1408
+ "status_details",
1409
+ "failure_reason",
1410
+ "last_error",
1411
+ "provider_error",
1412
+ "error_details"
1413
+ ];
1414
+ function readResponseFailedString(record, key) {
1415
+ return stringifyUnknown(record?.[key]);
1416
+ }
1417
+ function buildResponsesFailedEventSummary(message, responseId, observation) {
1418
+ const summary = { message };
1419
+ if (responseId) summary.responseId = responseId;
1420
+ if (observation) summary.observation = observation;
1421
+ return summary;
1422
+ }
1423
+ function isResponseFailedIdentifierKey(key) {
1424
+ const normalized = key.replace(/[-_\s]/g, "").toLowerCase();
1425
+ return normalized === "requestid" || normalized === "xrequestid" || normalized === "providerrequestid" || normalized === "providerresponseid" || normalized === "litellmrequestid" || normalized.includes("request") && normalized.endsWith("id") || normalized.includes("provider") && normalized.endsWith("id");
1426
+ }
1427
+ function collectResponseFailedIdentifierHashes(value, opts = {}) {
1428
+ const path = opts.path ?? "";
1429
+ const depth = opts.depth ?? 0;
1430
+ const identifierKey = opts.identifierKey ?? "";
1431
+ const out = opts.out ?? [];
1432
+ const seen = opts.seen ?? /* @__PURE__ */ new WeakSet();
1433
+ if (out.length >= 12 || depth > 4 || !value || typeof value !== "object") return out;
1434
+ if (seen.has(value)) return out;
1435
+ seen.add(value);
1436
+ if (Array.isArray(value)) {
1437
+ for (const [index, item] of value.entries()) {
1438
+ if (index >= 8 || out.length >= 12) break;
1439
+ const itemString = typeof item === "string" || typeof item === "number" ? String(item).trim() : "";
1440
+ if (identifierKey && isResponseFailedIdentifierKey(identifierKey) && itemString) {
1441
+ out.push(`${path}[${index}]=${redactIdentifier(itemString, { len: 12 })}`);
1442
+ continue;
1443
+ }
1444
+ collectResponseFailedIdentifierHashes(item, {
1445
+ path: `${path}[${index}]`,
1446
+ depth: depth + 1,
1447
+ identifierKey,
1448
+ out,
1449
+ seen
1450
+ });
1346
1451
  }
1452
+ return out;
1347
1453
  }
1348
- for (const key of OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS) {
1349
- const schemaMap = record[key];
1350
- if (!schemaMap || typeof schemaMap !== "object" || Array.isArray(schemaMap)) continue;
1351
- for (const [entryKey, value] of Object.entries(schemaMap)) violations.push(...findOpenAIStrictSchemaViolations(value, `${path}.${key}.${entryKey}`));
1352
- }
1353
- for (const key of OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS) {
1354
- const value = record[key];
1355
- if (value && typeof value === "object") violations.push(...findOpenAIStrictSchemaViolations(value, `${path}.${key}`));
1454
+ for (const [key, child] of Object.entries(value)) {
1455
+ if (out.length >= 12) break;
1456
+ const childPath = path ? `${path}.${key}` : key;
1457
+ const childString = typeof child === "string" || typeof child === "number" ? String(child).trim() : "";
1458
+ if (isResponseFailedIdentifierKey(key) && childString) {
1459
+ out.push(`${childPath}=${redactIdentifier(childString, { len: 12 })}`);
1460
+ continue;
1461
+ }
1462
+ collectResponseFailedIdentifierHashes(child, {
1463
+ path: childPath,
1464
+ depth: depth + 1,
1465
+ identifierKey: isResponseFailedIdentifierKey(key) ? key : void 0,
1466
+ out,
1467
+ seen
1468
+ });
1356
1469
  }
1357
- return violations;
1358
- }
1359
- //#endregion
1360
- //#region packages/ai/src/providers/openai-tool-schema.ts
1361
- /**
1362
- * OpenAI strict JSON-schema normalization for tool inventories and request payloads.
1363
- *
1364
- * Caches normalized object inputs by provider compatibility so repeated inventory builds preserve identity.
1365
- */
1366
- const MAX_STRICT_SCHEMA_CACHE_ENTRIES_PER_SCHEMA = 8;
1367
- let strictOpenAISchemaCache = /* @__PURE__ */ new WeakMap();
1368
- function resolveToolSchemaModelCompat(compat) {
1369
- if (!compat) return;
1370
- const unsupportedToolSchemaKeywords = Array.isArray(compat.unsupportedToolSchemaKeywords) ? compat.unsupportedToolSchemaKeywords.filter((keyword) => typeof keyword === "string") : [];
1371
- if (unsupportedToolSchemaKeywords.length === 0 && compat.omitEmptyArrayItems !== true) return;
1470
+ return out;
1471
+ }
1472
+ function redactResponseFailedDiagnosticValue(value, opts = {}) {
1473
+ const key = opts.key ?? "";
1474
+ const depth = opts.depth ?? 0;
1475
+ if (typeof value === "string" || typeof value === "number") return key && isResponseFailedIdentifierKey(key) ? redactIdentifier(String(value), { len: 12 }) : value;
1476
+ if (depth > 6 || !value || typeof value !== "object") return value;
1477
+ const seen = opts.seen ?? /* @__PURE__ */ new WeakSet();
1478
+ if (seen.has(value)) return "<circular>";
1479
+ seen.add(value);
1480
+ if (Array.isArray(value)) return value.slice(0, 16).map((item) => redactResponseFailedDiagnosticValue(item, {
1481
+ key,
1482
+ depth: depth + 1,
1483
+ seen
1484
+ }));
1485
+ const out = {};
1486
+ for (const [childKey, child] of Object.entries(value)) out[childKey] = redactResponseFailedDiagnosticValue(child, {
1487
+ key: childKey,
1488
+ depth: depth + 1,
1489
+ seen
1490
+ });
1491
+ return out;
1492
+ }
1493
+ function buildResponsesFailedFailureFields(response) {
1494
+ if (!response) return {};
1495
+ const fields = {};
1496
+ for (const key of RESPONSE_FAILED_FAILURE_FIELD_KEYS) if (response[key] !== void 0 && response[key] !== null) fields[key] = response[key];
1497
+ return fields;
1498
+ }
1499
+ function buildResponsesFailedNoDetailsObservation(event, model, response = isRecord(event.response) ? event.response : void 0) {
1500
+ const failureFields = redactResponseFailedDiagnosticValue(buildResponsesFailedFailureFields(response));
1501
+ const metadataKeys = isRecord(response?.metadata) ? Object.keys(response.metadata).toSorted() : [];
1502
+ const responsePreview = {
1503
+ id: readResponseFailedString(response, "id"),
1504
+ status: readResponseFailedString(response, "status"),
1505
+ model: readResponseFailedString(response, "model"),
1506
+ object: readResponseFailedString(response, "object"),
1507
+ failureFields,
1508
+ metadataKeys
1509
+ };
1372
1510
  return {
1373
- ...unsupportedToolSchemaKeywords.length > 0 ? { unsupportedToolSchemaKeywords } : {},
1374
- ...compat.omitEmptyArrayItems === true ? { omitEmptyArrayItems: true } : {}
1511
+ event: "openai_responses_response_failed_without_details",
1512
+ provider: model.provider,
1513
+ api: model.api,
1514
+ transportModel: model.id,
1515
+ providerRuntimeFailureKind: "no_error_details",
1516
+ responseId: responsePreview.id,
1517
+ responseStatus: responsePreview.status,
1518
+ responseModel: responsePreview.model,
1519
+ responseObject: responsePreview.object,
1520
+ metadataKeys,
1521
+ requestIdHashes: collectResponseFailedIdentifierHashes(event),
1522
+ failureFieldsPreview: stringifyRedactedEvent(failureFields),
1523
+ responsePreview: stringifyRedactedEvent(responsePreview)
1375
1524
  };
1376
1525
  }
1377
- function resolveStrictOpenAISchemaCacheKey(modelCompat) {
1378
- const compat = resolveToolSchemaModelCompat(modelCompat);
1379
- return JSON.stringify([[...compat?.unsupportedToolSchemaKeywords ?? []].toSorted(), shouldOmitEmptyArrayItems(compat)]);
1526
+ function summarizeResponsesFailedNoDetailsObservation(observation) {
1527
+ const requestIds = observation.requestIdHashes.join(",");
1528
+ const metadataKeys = observation.metadataKeys.join(",");
1529
+ return `responseId=${safeDebugValue(observation.responseId || void 0)} responseStatus=${safeDebugValue(observation.responseStatus || void 0)} responseModel=${safeDebugValue(observation.responseModel || void 0)} requestIds=${requestIds || "none"} metadataKeys=${metadataKeys || "none"} failureFields=${observation.failureFieldsPreview}`;
1530
+ }
1531
+ function normalizeResponsesFailedEvent(event, model) {
1532
+ const response = isRecord(event.response) ? event.response : void 0;
1533
+ const responseId = readResponseFailedString(response, "id") || void 0;
1534
+ const error = isRecord(response?.error) ? response.error : void 0;
1535
+ if (error) {
1536
+ const code = readResponseFailedString(error, "code").trim();
1537
+ const message = readResponseFailedString(error, "message").trim();
1538
+ if (code || message) return buildResponsesFailedEventSummary(`${code || "unknown"}: ${message || "no message"}`, responseId);
1539
+ }
1540
+ const incompleteReason = readResponseFailedString(isRecord(response?.incomplete_details) ? response.incomplete_details : void 0, "reason");
1541
+ if (incompleteReason) return buildResponsesFailedEventSummary(`incomplete: ${incompleteReason}`, responseId);
1542
+ return buildResponsesFailedEventSummary(RESPONSE_FAILED_NO_DETAILS_MESSAGE, responseId, buildResponsesFailedNoDetailsObservation(event, model, response));
1543
+ }
1544
+ function logResponsesFailedNoDetails(observation) {
1545
+ log.warn(`[responses] response.failed missing error details provider=${observation.provider} api=${observation.api} model=${observation.transportModel} ` + summarizeResponsesFailedNoDetailsObservation(observation), observation);
1546
+ }
1547
+ function summarizeResponsesPayload(params) {
1548
+ if (!params || typeof params !== "object") return "payload=non-object";
1549
+ const record = params;
1550
+ const input = record.input;
1551
+ const reasoning = record.reasoning && typeof record.reasoning === "object" ? record.reasoning : void 0;
1552
+ const text = record.text && typeof record.text === "object" ? record.text : void 0;
1553
+ const parts = [
1554
+ `fields=${Object.keys(record).toSorted().join(",")}`,
1555
+ `model=${safeDebugValue(record.model)}`,
1556
+ `stream=${safeDebugValue(record.stream)}`,
1557
+ `inputItems=${Array.isArray(input) ? input.length : typeof input}`,
1558
+ `inputRoles=${responseInputRoles(input) || "none"}`,
1559
+ `inputTextChars=${responseInputTextChars(input)}`,
1560
+ `tools=${summarizeResponsesTools(record.tools)}`,
1561
+ `reasoningEffort=${safeDebugValue(reasoning?.effort)}`,
1562
+ `reasoningSummary=${safeDebugValue(reasoning?.summary)}`,
1563
+ `textVerbosity=${safeDebugValue(text?.verbosity)}`,
1564
+ `serviceTier=${safeDebugValue(record.service_tier)}`,
1565
+ `store=${safeDebugValue(record.store)}`,
1566
+ `promptCacheKey=${record.prompt_cache_key === void 0 ? "absent" : "present"}`,
1567
+ `metadataKeys=${record.metadata && typeof record.metadata === "object" ? Object.keys(record.metadata).toSorted().join(",") : "none"}`
1568
+ ];
1569
+ if (resolveModelPayloadDebugMode() === "full-redacted") parts.push(`payload=${stringifyRedactedPayload(record)}`);
1570
+ return parts.join(" ");
1571
+ }
1572
+ function summarizeOpenAITransportError(error) {
1573
+ if (!error || typeof error !== "object") return `type=${typeof error} message=${safeDebugValue(error)}`;
1574
+ const record = error;
1575
+ const cause = record.cause && typeof record.cause === "object" ? record.cause : void 0;
1576
+ return [
1577
+ `name=${safeDebugValue(record.name)}`,
1578
+ `status=${safeDebugValue(record.status)}`,
1579
+ `code=${safeDebugValue(record.code)}`,
1580
+ `type=${safeDebugValue(record.type)}`,
1581
+ `causeName=${safeDebugValue(cause?.name)}`,
1582
+ `causeCode=${safeDebugValue(cause?.code)}`,
1583
+ `message=${error instanceof Error ? error.message : safeDebugValue(error)}`
1584
+ ].join(" ");
1380
1585
  }
1381
- function readCachedStrictOpenAISchema(schema, key) {
1382
- return strictOpenAISchemaCache.get(schema)?.find((entry) => entry.key === key)?.value;
1586
+ //#endregion
1587
+ //#region packages/ai/src/transports/openai-responses-replay.ts
1588
+ /** Resolves the assistant message id that can be replayed to OpenAI Responses. */
1589
+ function resolveReplayableResponsesMessageId$1(params) {
1590
+ if (!params.replayResponsesItemIds) return;
1591
+ if (!params.textSignatureId) return params.fallbackOrdinal === 0 ? params.fallbackId : `${params.fallbackId}_${params.fallbackOrdinal}`;
1592
+ return params.previousReplayItemWasReasoning ? params.textSignatureId : void 0;
1593
+ }
1594
+ //#endregion
1595
+ //#region packages/ai/src/transports/openai-responses-replay-internal.ts
1596
+ function isInvalidEncryptedContentError(error) {
1597
+ if (!error || typeof error !== "object") return false;
1598
+ const record = error;
1599
+ if (record.code === "invalid_encrypted_content" || record.code === "thinking_signature_invalid") return true;
1600
+ const message = typeof record.message === "string" ? record.message : "";
1601
+ return message.includes("invalid_encrypted_content") || message.includes("thinking_signature_invalid") || record.status === 400 && message.toLowerCase().includes("could not decrypt the provided encrypted_content");
1602
+ }
1603
+ function stripEncryptedContentFields(value) {
1604
+ if (!value || typeof value !== "object") return {
1605
+ value,
1606
+ changed: false
1607
+ };
1608
+ if (Array.isArray(value)) {
1609
+ let changed = false;
1610
+ const next = value.map((item) => {
1611
+ const stripped = stripEncryptedContentFields(item);
1612
+ changed ||= stripped.changed;
1613
+ return stripped.value;
1614
+ });
1615
+ return changed ? {
1616
+ value: next,
1617
+ changed: true
1618
+ } : {
1619
+ value,
1620
+ changed: false
1621
+ };
1622
+ }
1623
+ let changed = false;
1624
+ const next = {};
1625
+ for (const [key, child] of Object.entries(value)) {
1626
+ if (key === "encrypted_content") {
1627
+ changed = true;
1628
+ continue;
1629
+ }
1630
+ const stripped = stripEncryptedContentFields(child);
1631
+ changed ||= stripped.changed;
1632
+ next[key] = stripped.value;
1633
+ }
1634
+ return changed ? {
1635
+ value: next,
1636
+ changed: true
1637
+ } : {
1638
+ value,
1639
+ changed: false
1640
+ };
1641
+ }
1642
+ function stripResponsesRequestEncryptedContent(params) {
1643
+ const stripped = stripEncryptedContentFields(params.input);
1644
+ if (!stripped.changed) return params;
1645
+ return {
1646
+ ...params,
1647
+ input: stripped.value
1648
+ };
1649
+ }
1650
+ function hashOptionalReplayContextValue(value) {
1651
+ const normalized = value?.trim();
1652
+ return normalized ? shortHash(normalized) : void 0;
1653
+ }
1654
+ function buildOpenAIResponsesReplayContext(model, options) {
1655
+ return {
1656
+ provider: model.provider,
1657
+ api: model.api,
1658
+ model: model.id,
1659
+ baseUrlHash: hashOptionalReplayContextValue(model.baseUrl),
1660
+ sessionHash: hashOptionalReplayContextValue(options?.sessionId),
1661
+ authProfileHash: hashOptionalReplayContextValue(options?.authProfileId)
1662
+ };
1663
+ }
1664
+ function buildOpenAIResponsesReasoningReplayMetadata(model, options) {
1665
+ return {
1666
+ v: 1,
1667
+ source: "openai-responses",
1668
+ ...buildOpenAIResponsesReplayContext(model, options)
1669
+ };
1670
+ }
1671
+ function tagOpenAIResponsesReasoningReplayItem(item, model, options) {
1672
+ if (!("encrypted_content" in item)) return item;
1673
+ return {
1674
+ ...item,
1675
+ [OPENAI_RESPONSES_REASONING_REPLAY_META_KEY]: buildOpenAIResponsesReasoningReplayMetadata(model, options)
1676
+ };
1677
+ }
1678
+ function isOpenAIResponsesReasoningReplayMetadata(value) {
1679
+ if (!value || typeof value !== "object") return false;
1680
+ const record = value;
1681
+ return record.v === 1 && record.source === "openai-responses" && typeof record.provider === "string" && typeof record.api === "string" && typeof record.model === "string" && (record.baseUrlHash === void 0 || typeof record.baseUrlHash === "string") && (record.sessionHash === void 0 || typeof record.sessionHash === "string") && (record.authProfileHash === void 0 || typeof record.authProfileHash === "string");
1682
+ }
1683
+ function encryptedReasoningReplayMetadataMatches(metadata, context) {
1684
+ if (!metadata) return false;
1685
+ return metadata.provider === context.provider && metadata.api === context.api && metadata.model === context.model && metadata.baseUrlHash === context.baseUrlHash && metadata.sessionHash === context.sessionHash && metadata.authProfileHash === context.authProfileHash;
1686
+ }
1687
+ function readOpenAIResponsesReasoningReplayBlockMetadata(block) {
1688
+ const value = block[OPENAI_RESPONSES_REASONING_REPLAY_BLOCK_META_KEY];
1689
+ return isOpenAIResponsesReasoningReplayMetadata(value) ? value : void 0;
1690
+ }
1691
+ function normalizeOpenAIResponsesReasoningReplayItem(item) {
1692
+ const record = item;
1693
+ if (record.type !== "reasoning" || Array.isArray(record.summary)) return item;
1694
+ return {
1695
+ ...record,
1696
+ summary: []
1697
+ };
1698
+ }
1699
+ function prepareOpenAIResponsesReasoningItemForReplay(item, context, blockMetadata) {
1700
+ const { [OPENAI_RESPONSES_REASONING_REPLAY_META_KEY]: rawMetadata, ...rest } = item;
1701
+ if (!("encrypted_content" in rest)) return normalizeOpenAIResponsesReasoningReplayItem(rest);
1702
+ if (encryptedReasoningReplayMetadataMatches(blockMetadata ?? (isOpenAIResponsesReasoningReplayMetadata(rawMetadata) ? rawMetadata : void 0), context)) return normalizeOpenAIResponsesReasoningReplayItem(rest);
1703
+ return normalizeOpenAIResponsesReasoningReplayItem(stripEncryptedContentFields(rest).value);
1704
+ }
1705
+ async function createResponsesStreamWithEncryptedContentRetry(params) {
1706
+ try {
1707
+ return await params.client.responses.create(params.request, params.requestOptions);
1708
+ } catch (error) {
1709
+ const retryRequest = stripResponsesRequestEncryptedContent(params.request);
1710
+ if (!isInvalidEncryptedContentError(error) || retryRequest === params.request) throw error;
1711
+ log.warn(`[responses] retrying without encrypted reasoning content provider=${params.model.provider} api=${params.model.api} model=${params.model.id}`);
1712
+ return await params.client.responses.create(retryRequest, params.requestOptions);
1713
+ }
1714
+ }
1715
+ function resolveAzureOpenAIApiVersion(env = process.env) {
1716
+ return env.AZURE_OPENAI_API_VERSION?.trim() || "preview";
1717
+ }
1718
+ function normalizeResponsesReplayItemId(id, prefix) {
1719
+ if (!id) return;
1720
+ if (id.length <= 64) return id;
1721
+ return `${prefix}_${shortHash(id)}`;
1722
+ }
1723
+ function isSafeResponsesReplayItemId(id) {
1724
+ return typeof id === "string" && id.length > 0 && id.length <= 64;
1725
+ }
1726
+ function encodeTextSignatureV1(id, phase) {
1727
+ return JSON.stringify({
1728
+ v: 1,
1729
+ id,
1730
+ ...phase ? { phase } : {}
1731
+ });
1732
+ }
1733
+ function parseTextSignature$1(signature) {
1734
+ if (!signature) return;
1735
+ if (signature.startsWith("{")) try {
1736
+ const parsed = JSON.parse(signature);
1737
+ if (parsed.v === 1) {
1738
+ const id = typeof parsed.id === "string" ? parsed.id : void 0;
1739
+ const phase = parsed.phase === "commentary" || parsed.phase === "final_answer" ? parsed.phase : void 0;
1740
+ if (id !== void 0 || phase !== void 0) return {
1741
+ id,
1742
+ phase
1743
+ };
1744
+ return;
1745
+ }
1746
+ } catch {}
1747
+ return { id: signature };
1748
+ }
1749
+ function buildResponsesInputMessage(role, content) {
1750
+ return {
1751
+ type: "message",
1752
+ role,
1753
+ content
1754
+ };
1755
+ }
1756
+ function convertResponsesMessages$1(model, context, allowedToolCallProviders, options) {
1757
+ const messages = [];
1758
+ const shouldReplayReasoningItems = options?.replayReasoningItems ?? true;
1759
+ const shouldReplayResponsesItemIds = options?.replayResponsesItemIds ?? true;
1760
+ const replayContext = buildOpenAIResponsesReplayContext(model, {
1761
+ sessionId: options?.sessionId,
1762
+ authProfileId: options?.authProfileId
1763
+ });
1764
+ const shouldNormalizeSameModelToolCallIds = model.provider === "github-copilot";
1765
+ const sanitizeIdPart = (part) => part.replace(/[^a-zA-Z0-9_-]/g, "_").replace(/_+$/, "");
1766
+ const normalizeIdPart = (part) => {
1767
+ const sanitized = sanitizeIdPart(part);
1768
+ return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
1769
+ };
1770
+ const buildForeignResponsesItemId = (itemId) => {
1771
+ const normalized = `fc_${shortHash(itemId)}`;
1772
+ return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
1773
+ };
1774
+ const buildSameProviderCopilotResponsesItemId = (itemId) => {
1775
+ const sanitized = sanitizeIdPart(itemId);
1776
+ const candidate = sanitized.startsWith("fc_") ? sanitized : `fc_${sanitized}`;
1777
+ return candidate.length > 64 ? buildForeignResponsesItemId(itemId) : candidate;
1778
+ };
1779
+ const normalizeToolCallId = (id, _targetModel, source) => {
1780
+ if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
1781
+ if (!id.includes("|")) return normalizeIdPart(id);
1782
+ const separatorIndex = id.indexOf("|");
1783
+ const callId = id.slice(0, separatorIndex);
1784
+ const itemId = id.slice(separatorIndex + 1);
1785
+ const normalizedCallId = normalizeIdPart(callId);
1786
+ let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : model.provider === "github-copilot" ? buildSameProviderCopilotResponsesItemId(itemId) : normalizeIdPart(itemId);
1787
+ if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
1788
+ return `${normalizedCallId}|${normalizedItemId}`;
1789
+ };
1790
+ const transformedMessages = transformTransportMessages(context.messages, model, normalizeToolCallId, { normalizeSameModelToolCallIds: shouldNormalizeSameModelToolCallIds });
1791
+ if ((options?.includeSystemPrompt ?? true) && context.systemPrompt) messages.push(buildResponsesInputMessage(model.reasoning && options?.supportsDeveloperRole !== false ? "developer" : "system", [{
1792
+ type: "input_text",
1793
+ text: sanitizeTransportPayloadText(stripSystemPromptCacheBoundary(context.systemPrompt))
1794
+ }]));
1795
+ let msgIndex = 0;
1796
+ for (const msg of transformedMessages) {
1797
+ if (msg.role === "user") if (typeof msg.content === "string") messages.push(buildResponsesInputMessage("user", [{
1798
+ type: "input_text",
1799
+ text: sanitizeTransportPayloadText(msg.content)
1800
+ }]));
1801
+ else {
1802
+ const content = msg.content.map((item) => item.type === "text" ? {
1803
+ type: "input_text",
1804
+ text: sanitizeTransportPayloadText(item.text)
1805
+ } : {
1806
+ type: "input_image",
1807
+ detail: "auto",
1808
+ image_url: `data:${item.mimeType};base64,${item.data}`
1809
+ }).filter((item) => model.input.includes("image") || item.type !== "input_image");
1810
+ if (content.length > 0) messages.push(buildResponsesInputMessage("user", content));
1811
+ }
1812
+ else if (msg.role === "assistant") {
1813
+ const output = [];
1814
+ let textFallbackOrdinal = 0;
1815
+ let previousReplayItemWasReasoning = false;
1816
+ const isDifferentModel = msg.model !== model.id && msg.provider === model.provider && msg.api === model.api;
1817
+ for (const block of msg.content) if (block.type === "thinking") {
1818
+ if (shouldReplayReasoningItems && block.thinkingSignature && block.thinkingSignature.startsWith("{")) {
1819
+ const replayableReasoningItem = prepareOpenAIResponsesReasoningItemForReplay(JSON.parse(block.thinkingSignature), replayContext, readOpenAIResponsesReasoningReplayBlockMetadata(block));
1820
+ if (!shouldReplayResponsesItemIds) delete replayableReasoningItem.id;
1821
+ if (shouldReplayResponsesItemIds && model.provider === "github-copilot" && !isSafeResponsesReplayItemId(replayableReasoningItem.id)) continue;
1822
+ output.push(replayableReasoningItem);
1823
+ previousReplayItemWasReasoning = true;
1824
+ }
1825
+ } else if (block.type === "text") {
1826
+ const textSignature = parseTextSignature$1(block.textSignature);
1827
+ let msgId = resolveReplayableResponsesMessageId$1({
1828
+ replayResponsesItemIds: shouldReplayResponsesItemIds,
1829
+ textSignatureId: textSignature?.id,
1830
+ fallbackId: `msg_${msgIndex}`,
1831
+ fallbackOrdinal: textFallbackOrdinal,
1832
+ previousReplayItemWasReasoning
1833
+ });
1834
+ if (!textSignature?.id) textFallbackOrdinal += 1;
1835
+ msgId = normalizeResponsesReplayItemId(msgId, "msg");
1836
+ const messageItem = {
1837
+ type: "message",
1838
+ role: "assistant",
1839
+ content: [{
1840
+ type: "output_text",
1841
+ text: sanitizeTransportPayloadText(block.text),
1842
+ annotations: []
1843
+ }],
1844
+ status: "completed",
1845
+ ...msgId ? { id: msgId } : {},
1846
+ phase: textSignature?.phase
1847
+ };
1848
+ output.push(messageItem);
1849
+ previousReplayItemWasReasoning = false;
1850
+ } else if (block.type === "toolCall") {
1851
+ const separatorIndex = block.id.indexOf("|");
1852
+ const callId = separatorIndex === -1 ? block.id : block.id.slice(0, separatorIndex);
1853
+ const itemIdRaw = separatorIndex === -1 ? void 0 : block.id.slice(separatorIndex + 1);
1854
+ const itemId = shouldReplayResponsesItemIds && !(isDifferentModel && itemIdRaw?.startsWith("fc_")) ? itemIdRaw : void 0;
1855
+ output.push({
1856
+ type: "function_call",
1857
+ ...itemId ? { id: itemId } : {},
1858
+ call_id: callId,
1859
+ name: block.name,
1860
+ arguments: typeof block.arguments === "string" ? block.arguments : JSON.stringify(block.arguments ?? {})
1861
+ });
1862
+ previousReplayItemWasReasoning = false;
1863
+ }
1864
+ if (output.length > 0) messages.push(...output);
1865
+ } else if (msg.role === "toolResult") {
1866
+ const textResult = extractToolResultText(msg.content);
1867
+ const sanitizedTextResult = sanitizeTransportPayloadText(textResult);
1868
+ const hasText = sanitizedTextResult.trim().length > 0;
1869
+ const mediaPlaceholder = describeToolResultMediaPlaceholder(msg.content);
1870
+ const hasImages = msg.content.some(isImageWithMediaPayload);
1871
+ const separatorIndex = msg.toolCallId.indexOf("|");
1872
+ const callId = separatorIndex === -1 ? msg.toolCallId : msg.toolCallId.slice(0, separatorIndex);
1873
+ messages.push({
1874
+ type: "function_call_output",
1875
+ call_id: callId,
1876
+ output: hasImages && model.input.includes("image") ? [...hasText ? [{
1877
+ type: "input_text",
1878
+ text: sanitizedTextResult
1879
+ }] : mediaPlaceholder === "(see attached media)" ? [{
1880
+ type: "input_text",
1881
+ text: mediaPlaceholder
1882
+ }] : [], ...msg.content.filter(isImageWithMediaPayload).map((item) => ({
1883
+ type: "input_image",
1884
+ detail: "auto",
1885
+ image_url: `data:${item.mimeType};base64,${item.data}`
1886
+ }))] : sanitizeNonEmptyTransportPayloadText(textResult, mediaPlaceholder ?? "(no output)")
1887
+ });
1888
+ }
1889
+ msgIndex += 1;
1890
+ }
1891
+ return messages;
1892
+ }
1893
+ //#endregion
1894
+ //#region packages/ai/src/transports/openai-responses-stream-observer-internal.ts
1895
+ const STRING_DELTA_EVENTS = /* @__PURE__ */ new Set([
1896
+ "response.function_call_arguments.delta",
1897
+ "response.output_text.delta",
1898
+ "response.reasoning_summary_text.delta",
1899
+ "response.reasoning_text.delta",
1900
+ "response.refusal.delta",
1901
+ "response.text.delta"
1902
+ ]);
1903
+ async function* adaptResponsesStream(stream, signal) {
1904
+ const scheduler = createModelStreamCooperativeScheduler(signal);
1905
+ for await (const event of stream) {
1906
+ if (signal?.aborted) throw transportAbortError(signal);
1907
+ if (!isRecord(event) || typeof event.type !== "string") throw new Error("Responses stream delivered a malformed event without a string type");
1908
+ if (STRING_DELTA_EVENTS.has(event.type) && typeof event.delta !== "string") throw new Error(`Responses stream delivered malformed ${event.type} delta`);
1909
+ if ((event.type === "response.output_item.added" || event.type === "response.output_item.done") && !isRecord(event.item)) throw new Error(`Responses stream delivered malformed ${event.type} item`);
1910
+ if ((event.type === "response.created" || event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && !isRecord(event.response)) throw new Error(`Responses stream delivered malformed ${event.type} response`);
1911
+ yield event;
1912
+ await scheduler.afterEvent();
1913
+ }
1914
+ }
1915
+ async function* observeResponsesStream(stream, model) {
1916
+ const startedAt = Date.now();
1917
+ const eventTypes = /* @__PURE__ */ new Map();
1918
+ const debugMode = resolveModelSseDebugMode();
1919
+ let eventCount = 0;
1920
+ try {
1921
+ for await (const event of stream) {
1922
+ const type = isRecord(event) && typeof event.type === "string" ? event.type : "unknown";
1923
+ eventCount += 1;
1924
+ eventTypes.set(type, (eventTypes.get(type) ?? 0) + 1);
1925
+ if (eventCount === 1) emitModelTransportDebug(log, `[responses] first_event provider=${model.provider} api=${model.api} model=${model.id} elapsedMs=${Date.now() - startedAt} type=${type}`);
1926
+ if (debugMode === "peek" && eventCount <= 5) emitModelTransportDebug(log, `[responses] event_peek provider=${model.provider} api=${model.api} model=${model.id} index=${eventCount} type=${type} event=${stringifyRedactedEvent(event)}`);
1927
+ yield event;
1928
+ }
1929
+ } finally {
1930
+ const types = [...eventTypes].map(([type, count]) => `${type}:${count}`).join(",");
1931
+ emitModelTransportDebug(log, `[responses] stream_done provider=${model.provider} api=${model.api} model=${model.id} elapsedMs=${Date.now() - startedAt} events=${eventCount} types=${types}`);
1932
+ }
1933
+ }
1934
+ //#endregion
1935
+ //#region packages/ai/src/transports/openai-responses-stream-slots-internal.ts
1936
+ function readResponsesOutputIndex(event) {
1937
+ const outputIndex = event.output_index;
1938
+ return typeof outputIndex === "number" && Number.isInteger(outputIndex) && outputIndex >= 0 ? outputIndex : void 0;
1939
+ }
1940
+ function createResponsesOutputSlotTracker() {
1941
+ const indexed = /* @__PURE__ */ new Map();
1942
+ let unindexed;
1943
+ return {
1944
+ register(event, slot) {
1945
+ const outputIndex = readResponsesOutputIndex(event);
1946
+ if (outputIndex === void 0) {
1947
+ if (unindexed) throw new Error("Responses stream added overlapping unindexed output items");
1948
+ unindexed = slot;
1949
+ return;
1950
+ }
1951
+ if (indexed.has(outputIndex)) throw new Error(`Responses stream reused active output index ${outputIndex}`);
1952
+ indexed.set(outputIndex, slot);
1953
+ },
1954
+ resolve(event, type) {
1955
+ const outputIndex = readResponsesOutputIndex(event);
1956
+ let slot = outputIndex === void 0 ? unindexed : indexed.get(outputIndex);
1957
+ if (outputIndex === void 0 && !slot) {
1958
+ const matches = [...indexed.values()].filter((candidate) => candidate.type === type);
1959
+ slot = matches.length === 1 ? matches[0] : void 0;
1960
+ }
1961
+ return slot?.type === type ? slot : void 0;
1962
+ },
1963
+ get(event) {
1964
+ const outputIndex = readResponsesOutputIndex(event);
1965
+ return outputIndex === void 0 ? unindexed : indexed.get(outputIndex);
1966
+ },
1967
+ values() {
1968
+ return [.../* @__PURE__ */ new Set([...indexed.values(), ...unindexed ? [unindexed] : []])];
1969
+ },
1970
+ forget(slot) {
1971
+ if (unindexed === slot) unindexed = void 0;
1972
+ for (const [outputIndex, candidate] of indexed) if (candidate === slot) indexed.delete(outputIndex);
1973
+ }
1974
+ };
1975
+ }
1976
+ //#endregion
1977
+ //#region packages/ai/src/providers/openai-responses-terminal-usage.ts
1978
+ function readCount(value) {
1979
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
1980
+ }
1981
+ /**
1982
+ * Split a terminal usage payload into the priced buckets.
1983
+ *
1984
+ * OpenAI includes cache reads and writes in `input_tokens`, so both are subtracted out of the
1985
+ * billable input bucket. `total_tokens` comes from the payload, but never below the sum of the
1986
+ * split buckets: proxies routinely omit it (reporting 0 would understate the turn), and a payload
1987
+ * whose `cached_tokens` exceeds `input_tokens` clamps the input bucket, leaving the reported total
1988
+ * short of what the buckets actually price.
1989
+ */
1990
+ function mapResponsesTerminalUsage(usage) {
1991
+ if (!usage) return;
1992
+ const cacheRead = readCount(usage.input_tokens_details?.cached_tokens);
1993
+ const cacheWrite = readCount(usage.input_tokens_details?.cache_write_tokens);
1994
+ const input = Math.max(0, readCount(usage.input_tokens) - cacheRead - cacheWrite);
1995
+ const output = readCount(usage.output_tokens);
1996
+ const bucketTotal = input + output + cacheRead + cacheWrite;
1997
+ return {
1998
+ input,
1999
+ output,
2000
+ cacheRead,
2001
+ cacheWrite,
2002
+ totalTokens: Math.max(bucketTotal, readCount(usage.total_tokens))
2003
+ };
2004
+ }
2005
+ /** Reasoning tokens are reported by the agent path only; the package path does not track them. */
2006
+ function readResponsesReasoningTokens(usage) {
2007
+ const reasoningTokens = usage?.output_tokens_details?.reasoning_tokens;
2008
+ return typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? reasoningTokens : void 0;
2009
+ }
2010
+ function mapResponsesTerminalStopReason(status) {
2011
+ if (!status) return "stop";
2012
+ switch (status) {
2013
+ case "completed": return "stop";
2014
+ case "incomplete": return "length";
2015
+ case "failed":
2016
+ case "cancelled": return "error";
2017
+ case "in_progress":
2018
+ case "queued": return "stop";
2019
+ default: throw new Error(`Unhandled stop reason: ${String(status)}`);
2020
+ }
2021
+ }
2022
+ /**
2023
+ * Resolve the terminal stop reason, including the two overrides every Responses path shares: a
2024
+ * content-filtered turn is a provider error rather than a truncated answer, and a turn that
2025
+ * produced tool calls reports `toolUse` instead of a plain stop.
2026
+ */
2027
+ function resolveResponsesTerminalStopReason(params) {
2028
+ if (params.status === "incomplete" && params.incompleteReason === "content_filter") return {
2029
+ stopReason: "error",
2030
+ errorMessage: "Provider incomplete_reason: content_filter"
2031
+ };
2032
+ const stopReason = mapResponsesTerminalStopReason(params.status);
2033
+ if (stopReason === "stop" && params.hasToolCall) return { stopReason: "toolUse" };
2034
+ return { stopReason };
2035
+ }
2036
+ //#endregion
2037
+ //#region packages/ai/src/transports/openai-responses-stream-terminal-internal.ts
2038
+ function splitToolCallId(id) {
2039
+ const separator = id.indexOf("|");
2040
+ return separator === -1 ? [id, void 0] : [id.slice(0, separator), id.slice(separator + 1)];
2041
+ }
2042
+ function resolveResponsesToolCallId(item, fallbackId) {
2043
+ const callId = typeof item.call_id === "string" ? item.call_id.trim() : "";
2044
+ const itemId = typeof item.id === "string" ? item.id.trim() : "";
2045
+ const [fallbackCallId, fallbackItemId = ""] = splitToolCallId(fallbackId ?? "");
2046
+ const resolvedCallId = callId || fallbackCallId;
2047
+ const resolvedItemId = itemId || fallbackItemId;
2048
+ if (resolvedCallId) return resolvedItemId ? `${resolvedCallId}|${resolvedItemId}` : resolvedCallId;
2049
+ const generated = `call_${randomUUID().replaceAll("-", "").slice(0, 24)}`;
2050
+ return resolvedItemId ? `${generated}|${resolvedItemId}` : generated;
2051
+ }
2052
+ function resolveCompletedToolCallName(toolCall, value) {
2053
+ const streamedName = toolCall?.block.name.trim() || void 0;
2054
+ const completedName = typeof value === "string" ? value.trim() || void 0 : void 0;
2055
+ if (streamedName && completedName && streamedName !== completedName) throw new Error(`Responses stream changed tool-call function name from ${streamedName} to ${completedName}`);
2056
+ const name = completedName ?? streamedName;
2057
+ if (!name) throw new Error("Responses stream completed tool call without a function name");
2058
+ return name;
2059
+ }
2060
+ function createResponsesTerminalController(params) {
2061
+ const { output, stream, model, options } = params;
2062
+ const blocks = output.content;
2063
+ const backfillReasoning = (items) => {
2064
+ for (const item of items) {
2065
+ if (item.type !== "reasoning" || !item.encrypted_content) continue;
2066
+ const block = params.reasoningBlocksById.get(item.id);
2067
+ if (!block?.thinkingSignature) continue;
2068
+ const stored = JSON.parse(block.thinkingSignature);
2069
+ if (!stored.encrypted_content) block.thinkingSignature = JSON.stringify({
2070
+ ...stored,
2071
+ encrypted_content: item.encrypted_content
2072
+ });
2073
+ if (options?.reasoningReplayMetadata) block[OPENAI_RESPONSES_REASONING_REPLAY_BLOCK_META_KEY] = options.reasoningReplayMetadata;
2074
+ }
2075
+ };
2076
+ const appendText = (item) => {
2077
+ const text = (Array.isArray(item.content) ? item.content : []).map((part) => {
2078
+ const content = part;
2079
+ return content.type === "output_text" || content.type === "text" ? content.text ?? "" : content.refusal ?? "";
2080
+ }).join("");
2081
+ if (!text) return;
2082
+ const phase = item.phase ?? void 0;
2083
+ const previous = params.getLastTextBlock();
2084
+ const collapse = resolveResponsesMessageSnapshotCollapse({
2085
+ prior: previous && {
2086
+ text: previous.block.text,
2087
+ phase: previous.phase
2088
+ },
2089
+ nextText: text,
2090
+ nextPhase: phase
2091
+ });
2092
+ if (collapse.kind === "extend" && previous) {
2093
+ previous.block.text = collapse.text;
2094
+ previous.block.textSignature = encodeTextSignatureV1(item.id, phase);
2095
+ stream.push({
2096
+ type: "text_end",
2097
+ contentIndex: previous.index,
2098
+ content: collapse.text,
2099
+ partial: output
2100
+ });
2101
+ return;
2102
+ }
2103
+ const block = {
2104
+ type: "text",
2105
+ text,
2106
+ textSignature: encodeTextSignatureV1(item.id, phase)
2107
+ };
2108
+ blocks.push(block);
2109
+ const index = blocks.length - 1;
2110
+ params.setLastTextBlock({
2111
+ block,
2112
+ index,
2113
+ phase
2114
+ });
2115
+ stream.push({
2116
+ type: "text_start",
2117
+ contentIndex: index,
2118
+ partial: output
2119
+ });
2120
+ stream.push({
2121
+ type: "text_end",
2122
+ contentIndex: index,
2123
+ content: text,
2124
+ partial: output
2125
+ });
2126
+ };
2127
+ const appendToolCall = (item) => {
2128
+ const toolCall = {
2129
+ type: "toolCall",
2130
+ id: resolveResponsesToolCallId(item),
2131
+ name: resolveCompletedToolCallName(void 0, item.name),
2132
+ arguments: parseStreamingJson(item.arguments || "{}")
2133
+ };
2134
+ blocks.push(toolCall);
2135
+ const contentIndex = blocks.length - 1;
2136
+ stream.push({
2137
+ type: "toolcall_start",
2138
+ contentIndex,
2139
+ partial: output
2140
+ });
2141
+ stream.push({
2142
+ type: "toolcall_end",
2143
+ contentIndex,
2144
+ toolCall,
2145
+ partial: output
2146
+ });
2147
+ };
2148
+ const recoverTerminalOutput = (items, includeToolCalls) => {
2149
+ if (blocks.length > 0) return;
2150
+ for (const item of items) if (item.type === "message") appendText(item);
2151
+ else {
2152
+ params.setLastTextBlock(null);
2153
+ if (includeToolCalls && item.type === "function_call") appendToolCall(item);
2154
+ }
2155
+ };
2156
+ const finalizeResponse = (response) => {
2157
+ params.markFinalized();
2158
+ backfillReasoning(response.output ?? []);
2159
+ output.responseId = response.id || output.responseId;
2160
+ const usage = mapResponsesTerminalUsage(response.usage);
2161
+ const reasoningTokens = readResponsesReasoningTokens(response.usage);
2162
+ if (usage) output.usage = {
2163
+ ...usage,
2164
+ ...reasoningTokens === void 0 ? {} : { reasoningTokens },
2165
+ cost: {
2166
+ input: 0,
2167
+ output: 0,
2168
+ cacheRead: 0,
2169
+ cacheWrite: 0,
2170
+ total: 0
2171
+ }
2172
+ };
2173
+ calculateCost(model, output.usage);
2174
+ if (options?.applyServiceTierPricing) {
2175
+ const tier = options.resolveServiceTier ? options.resolveServiceTier(response.service_tier, options.serviceTier) : response.service_tier ?? options.serviceTier;
2176
+ options.applyServiceTierPricing(output.usage, tier);
2177
+ }
2178
+ const terminal = resolveResponsesTerminalStopReason({
2179
+ status: response.status,
2180
+ incompleteReason: response.incomplete_details?.reason,
2181
+ hasToolCall: blocks.some((block) => block.type === "toolCall")
2182
+ });
2183
+ output.stopReason = terminal.stopReason;
2184
+ output.errorMessage = terminal.errorMessage;
2185
+ };
2186
+ return {
2187
+ finalizeResponse,
2188
+ recoverTerminalOutput
2189
+ };
2190
+ }
2191
+ //#endregion
2192
+ //#region packages/ai/src/transports/openai-responses-stream-internal.ts
2193
+ var ResponsesStreamFailure = class extends Error {
2194
+ constructor(failure, response) {
2195
+ super(failure.message);
2196
+ this.name = "ResponsesStreamFailure";
2197
+ this.responseId = failure.responseId;
2198
+ this.response = response;
2199
+ this.observation = failure.observation;
2200
+ }
2201
+ };
2202
+ async function processResponsesStream(openaiStream, output, stream, model, options) {
2203
+ const streamingToolCalls = createResponsesToolCallTracker();
2204
+ const outputSlots = createResponsesOutputSlotTracker();
2205
+ const reasoningBlocksById = /* @__PURE__ */ new Map();
2206
+ let terminalResponseEvent;
2207
+ let lastTextBlock = null;
2208
+ const blocks = output.content;
2209
+ const blockIndex = () => blocks.length - 1;
2210
+ const createOutputSlot = (event, item) => {
2211
+ if (item.type === "reasoning") {
2212
+ const block = {
2213
+ type: "thinking",
2214
+ thinking: ""
2215
+ };
2216
+ const slot = {
2217
+ type: "thinking",
2218
+ item,
2219
+ block,
2220
+ contentIndex: blocks.length
2221
+ };
2222
+ blocks.push(block);
2223
+ outputSlots.register(event, slot);
2224
+ stream.push({
2225
+ type: "thinking_start",
2226
+ contentIndex: slot.contentIndex,
2227
+ partial: output
2228
+ });
2229
+ return slot;
2230
+ }
2231
+ if (item.type === "message") {
2232
+ const messageItem = item;
2233
+ const collapseCandidate = lastTextBlock;
2234
+ const block = collapseCandidate ? null : {
2235
+ type: "text",
2236
+ text: "",
2237
+ ...messageItem.phase ? { textSignature: encodeTextSignatureV1(messageItem.id, messageItem.phase) } : {}
2238
+ };
2239
+ const slot = {
2240
+ type: "text",
2241
+ item: messageItem,
2242
+ block,
2243
+ contentIndex: block ? blocks.length : void 0,
2244
+ pendingText: collapseCandidate ? "" : null,
2245
+ collapseCandidate
2246
+ };
2247
+ if (block) blocks.push(block);
2248
+ outputSlots.register(event, slot);
2249
+ if (slot.contentIndex !== void 0) stream.push({
2250
+ type: "text_start",
2251
+ contentIndex: slot.contentIndex,
2252
+ partial: output
2253
+ });
2254
+ return slot;
2255
+ }
2256
+ };
2257
+ const resolveOutputItemSlot = (event, item) => {
2258
+ if (item.type === "reasoning") return outputSlots.resolve(event, "thinking");
2259
+ if (item.type === "message") return outputSlots.resolve(event, "text");
2260
+ return readResponsesOutputIndex(event) === void 0 ? void 0 : outputSlots.get(event);
2261
+ };
2262
+ const getOrCreateOutputSlot = (event, item) => {
2263
+ return resolveOutputItemSlot(event, item) ?? createOutputSlot(event, item);
2264
+ };
2265
+ const materializeDeferredTextSlot = (slot) => {
2266
+ if (slot.block || slot.pendingText === null) return;
2267
+ const text = slot.pendingText;
2268
+ slot.block = {
2269
+ type: "text",
2270
+ text,
2271
+ ...slot.item.phase ? { textSignature: encodeTextSignatureV1(slot.item.id, slot.item.phase) } : {}
2272
+ };
2273
+ blocks.push(slot.block);
2274
+ slot.contentIndex = blockIndex();
2275
+ stream.push({
2276
+ type: "text_start",
2277
+ contentIndex: slot.contentIndex,
2278
+ partial: output
2279
+ });
2280
+ if (text) stream.push({
2281
+ type: "text_delta",
2282
+ contentIndex: slot.contentIndex,
2283
+ delta: text
2284
+ });
2285
+ if (lastTextBlock === slot.collapseCandidate) lastTextBlock = null;
2286
+ slot.pendingText = null;
2287
+ slot.collapseCandidate = null;
2288
+ };
2289
+ const materializeDeferredTextSlots = (except) => {
2290
+ for (const slot of outputSlots.values()) if (slot !== except && slot.type === "text") materializeDeferredTextSlot(slot);
2291
+ };
2292
+ const appendPendingMessageDelta = (slot, delta) => {
2293
+ slot.pendingText = `${slot.pendingText ?? ""}${delta}`;
2294
+ const priorText = slot.collapseCandidate?.block.text ?? "";
2295
+ if (priorText.startsWith(slot.pendingText) || slot.pendingText.startsWith(priorText)) return;
2296
+ materializeDeferredTextSlot(slot);
2297
+ };
2298
+ const { finalizeResponse, recoverTerminalOutput } = createResponsesTerminalController({
2299
+ output,
2300
+ stream,
2301
+ model,
2302
+ options,
2303
+ reasoningBlocksById,
2304
+ getLastTextBlock: () => lastTextBlock,
2305
+ setLastTextBlock: (block) => {
2306
+ lastTextBlock = block;
2307
+ },
2308
+ markFinalized: () => {
2309
+ terminalResponseEvent = "finalized";
2310
+ }
2311
+ });
2312
+ const guardedStream = adaptResponsesStream(withFirstStreamEventTimeout(openaiStream, {
2313
+ provider: model.provider,
2314
+ api: model.api,
2315
+ model: model.id,
2316
+ timeoutMs: options?.firstEventTimeoutMs ?? 0,
2317
+ stage: "responses",
2318
+ abort: options?.abortFirstEventStream,
2319
+ onTimeout: options?.onFirstEventTimeout,
2320
+ hint: "The provider may be stalled while parsing the tool payload; retry with a smaller tool surface or enable OPENCLAW_DEBUG_MODEL_PAYLOAD=tools to inspect exposed tools."
2321
+ }), options?.signal);
2322
+ try {
2323
+ for await (const event of guardedStream) if (event.type === "response.created") output.responseId = event.response.id;
2324
+ else if (event.type === "response.output_item.added") {
2325
+ materializeDeferredTextSlots();
2326
+ const item = event.item;
2327
+ if (item.type !== "message") lastTextBlock = null;
2328
+ if (item.type === "reasoning" || item.type === "message") createOutputSlot(event, item);
2329
+ else if (item.type === "function_call") {
2330
+ const toolCallBlock = {
2331
+ type: "toolCall",
2332
+ id: resolveResponsesToolCallId(item),
2333
+ name: typeof item.name === "string" ? item.name.trim() : "",
2334
+ arguments: {},
2335
+ partialJson: item.arguments || ""
2336
+ };
2337
+ const contentIndex = output.content.length;
2338
+ const toolCallState = {
2339
+ block: toolCallBlock,
2340
+ contentIndex,
2341
+ argumentStreamReliable: true,
2342
+ ...readResponsesToolCallItemIdentity(item)
2343
+ };
2344
+ streamingToolCalls.register(event, toolCallState);
2345
+ if (readResponsesOutputIndex(event) !== void 0) outputSlots.register(event, {
2346
+ type: "toolCall",
2347
+ toolCall: toolCallState
2348
+ });
2349
+ output.content.push(toolCallBlock);
2350
+ stream.push({
2351
+ type: "toolcall_start",
2352
+ contentIndex,
2353
+ partial: output
2354
+ });
2355
+ }
2356
+ } else if (event.type === "response.reasoning_summary_part.added") {
2357
+ const slot = outputSlots.resolve(event, "thinking");
2358
+ if (!slot) continue;
2359
+ slot.item.summary = slot.item.summary || [];
2360
+ slot.item.summary.push(event.part);
2361
+ } else if (event.type === "response.reasoning_summary_text.delta") {
2362
+ const slot = outputSlots.resolve(event, "thinking");
2363
+ if (!slot) continue;
2364
+ slot.item.summary = slot.item.summary || [];
2365
+ const lastPart = slot.item.summary[slot.item.summary.length - 1];
2366
+ if (!lastPart) continue;
2367
+ slot.block.thinking += event.delta;
2368
+ lastPart.text += event.delta;
2369
+ stream.push({
2370
+ type: "thinking_delta",
2371
+ contentIndex: slot.contentIndex,
2372
+ delta: event.delta,
2373
+ partial: output
2374
+ });
2375
+ } else if (event.type === "response.reasoning_summary_part.done") {
2376
+ const slot = outputSlots.resolve(event, "thinking");
2377
+ if (!slot) continue;
2378
+ slot.item.summary = slot.item.summary || [];
2379
+ const lastPart = slot.item.summary[slot.item.summary.length - 1];
2380
+ if (!lastPart) continue;
2381
+ slot.block.thinking += "\n\n";
2382
+ lastPart.text += "\n\n";
2383
+ stream.push({
2384
+ type: "thinking_delta",
2385
+ contentIndex: slot.contentIndex,
2386
+ delta: "\n\n",
2387
+ partial: output
2388
+ });
2389
+ } else if (event.type === "response.reasoning_text.delta") {
2390
+ const slot = outputSlots.resolve(event, "thinking");
2391
+ if (!slot) continue;
2392
+ slot.block.thinking += event.delta;
2393
+ stream.push({
2394
+ type: "thinking_delta",
2395
+ contentIndex: slot.contentIndex,
2396
+ delta: event.delta,
2397
+ partial: output
2398
+ });
2399
+ } else if (event.type === "response.content_part.added") {
2400
+ const slot = outputSlots.resolve(event, "text");
2401
+ if (!slot) continue;
2402
+ slot.item.content = slot.item.content || [];
2403
+ if (event.part.type === "output_text" || event.part.type === "text" || event.part.type === "refusal") slot.item.content.push(event.part);
2404
+ } else if (event.type === "response.output_text.delta") {
2405
+ const slot = outputSlots.resolve(event, "text");
2406
+ if (!slot) continue;
2407
+ slot.item.content ||= [];
2408
+ let lastPart = slot.item.content[slot.item.content.length - 1];
2409
+ if (!isResponsesTextContentPartType(lastPart?.type)) {
2410
+ lastPart = {
2411
+ type: "output_text",
2412
+ text: "",
2413
+ annotations: []
2414
+ };
2415
+ slot.item.content.push(lastPart);
2416
+ }
2417
+ lastPart.text += event.delta;
2418
+ if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2419
+ else if (slot.block && slot.contentIndex !== void 0) {
2420
+ slot.block.text += event.delta;
2421
+ stream.push({
2422
+ type: "text_delta",
2423
+ contentIndex: slot.contentIndex,
2424
+ delta: event.delta
2425
+ });
2426
+ }
2427
+ } else if (isAzureResponsesTextDeltaEvent(event)) {
2428
+ const slot = outputSlots.resolve(event, "text");
2429
+ if (!slot) continue;
2430
+ slot.item.content = slot.item.content || [];
2431
+ let lastPart = slot.item.content[slot.item.content.length - 1];
2432
+ if (lastPart?.type !== "text") {
2433
+ lastPart = {
2434
+ type: "text",
2435
+ text: ""
2436
+ };
2437
+ slot.item.content.push(lastPart);
2438
+ }
2439
+ lastPart.text += event.delta;
2440
+ if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2441
+ else if (slot.block && slot.contentIndex !== void 0) {
2442
+ slot.block.text += event.delta;
2443
+ stream.push({
2444
+ type: "text_delta",
2445
+ contentIndex: slot.contentIndex,
2446
+ delta: event.delta
2447
+ });
2448
+ }
2449
+ } else if (event.type === "response.refusal.delta") {
2450
+ const slot = outputSlots.resolve(event, "text");
2451
+ if (!slot) continue;
2452
+ slot.item.content ||= [];
2453
+ let lastPart = slot.item.content[slot.item.content.length - 1];
2454
+ if (lastPart?.type !== "refusal") {
2455
+ lastPart = {
2456
+ type: "refusal",
2457
+ refusal: ""
2458
+ };
2459
+ slot.item.content.push(lastPart);
2460
+ }
2461
+ lastPart.refusal += event.delta;
2462
+ if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2463
+ else if (slot.block && slot.contentIndex !== void 0) {
2464
+ slot.block.text += event.delta;
2465
+ stream.push({
2466
+ type: "text_delta",
2467
+ contentIndex: slot.contentIndex,
2468
+ delta: event.delta
2469
+ });
2470
+ }
2471
+ } else if (event.type === "response.function_call_arguments.delta") {
2472
+ const toolCall = streamingToolCalls.resolve(event);
2473
+ if (toolCall) {
2474
+ toolCall.block.partialJson += event.delta;
2475
+ toolCall.block.arguments = parseStreamingJson(toolCall.block.partialJson);
2476
+ stream.push({
2477
+ type: "toolcall_delta",
2478
+ contentIndex: toolCall.contentIndex,
2479
+ delta: event.delta,
2480
+ partial: output
2481
+ });
2482
+ } else if (streamingToolCalls.hasActive()) streamingToolCalls.markArgumentsUnreliable();
2483
+ } else if (event.type === "response.function_call_arguments.done") {
2484
+ const toolCall = streamingToolCalls.resolve(event);
2485
+ if (toolCall) {
2486
+ const previousPartialJson = toolCall.block.partialJson;
2487
+ const doneArguments = typeof event.arguments === "string" ? event.arguments : void 0;
2488
+ if (doneArguments !== void 0 && (doneArguments.length > 0 || previousPartialJson === "")) {
2489
+ toolCall.block.partialJson = doneArguments;
2490
+ toolCall.block.arguments = parseStreamingJson(toolCall.block.partialJson);
2491
+ toolCall.argumentStreamReliable = true;
2492
+ }
2493
+ if (doneArguments?.startsWith(previousPartialJson)) {
2494
+ const delta = doneArguments.slice(previousPartialJson.length);
2495
+ if (delta.length > 0) stream.push({
2496
+ type: "toolcall_delta",
2497
+ contentIndex: toolCall.contentIndex,
2498
+ delta,
2499
+ partial: output
2500
+ });
2501
+ }
2502
+ } else if (streamingToolCalls.hasActive()) streamingToolCalls.markArgumentsUnreliable();
2503
+ } else if (event.type === "response.output_item.done") {
2504
+ const item = event.item;
2505
+ if (item.type !== "message") lastTextBlock = null;
2506
+ const existingOutputSlot = resolveOutputItemSlot(event, item);
2507
+ materializeDeferredTextSlots(existingOutputSlot);
2508
+ const outputSlot = existingOutputSlot ?? getOrCreateOutputSlot(event, item);
2509
+ if (item.type === "reasoning" && outputSlot?.type === "thinking") {
2510
+ const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
2511
+ const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
2512
+ outputSlot.block.thinking = summaryText || contentText || outputSlot.block.thinking;
2513
+ outputSlot.block.thinkingSignature = JSON.stringify(item);
2514
+ if (item.encrypted_content && options?.reasoningReplayMetadata) outputSlot.block[OPENAI_RESPONSES_REASONING_REPLAY_BLOCK_META_KEY] = options.reasoningReplayMetadata;
2515
+ if (typeof item.id === "string") reasoningBlocksById.set(item.id, outputSlot.block);
2516
+ stream.push({
2517
+ type: "thinking_end",
2518
+ contentIndex: outputSlot.contentIndex,
2519
+ content: outputSlot.block.thinking,
2520
+ partial: output
2521
+ });
2522
+ outputSlots.forget(outputSlot);
2523
+ } else if (item.type === "message" && outputSlot?.type === "text" && (outputSlot.block || outputSlot.pendingText !== null)) {
2524
+ const streamedText = outputSlot.pendingText ?? outputSlot.block?.text ?? "";
2525
+ const finalText = item.content == null ? streamedText : item.content.map((c) => c.type === "output_text" || c.type === "text" ? c.text : c.refusal).join("");
2526
+ const phase = item.phase ?? void 0;
2527
+ const collapse = outputSlot.pendingText !== null ? resolveResponsesMessageSnapshotCollapse({
2528
+ prior: outputSlot.collapseCandidate && {
2529
+ text: outputSlot.collapseCandidate.block.text,
2530
+ phase: outputSlot.collapseCandidate.phase
2531
+ },
2532
+ nextText: finalText,
2533
+ nextPhase: phase
2534
+ }) : { kind: "keep" };
2535
+ outputSlot.pendingText = null;
2536
+ if (collapse.kind === "extend" && outputSlot.collapseCandidate) {
2537
+ outputSlot.collapseCandidate.block.text = collapse.text;
2538
+ outputSlot.collapseCandidate.block.textSignature = encodeTextSignatureV1(item.id, phase);
2539
+ stream.push({
2540
+ type: "text_end",
2541
+ contentIndex: outputSlot.collapseCandidate.index,
2542
+ content: collapse.text,
2543
+ partial: output
2544
+ });
2545
+ lastTextBlock = outputSlot.collapseCandidate;
2546
+ } else {
2547
+ if (!outputSlot.block) {
2548
+ outputSlot.block = {
2549
+ type: "text",
2550
+ text: "",
2551
+ ...phase ? { textSignature: encodeTextSignatureV1(item.id, phase) } : {}
2552
+ };
2553
+ blocks.push(outputSlot.block);
2554
+ outputSlot.contentIndex = blockIndex();
2555
+ stream.push({
2556
+ type: "text_start",
2557
+ contentIndex: outputSlot.contentIndex,
2558
+ partial: output
2559
+ });
2560
+ }
2561
+ outputSlot.block.text = finalText;
2562
+ outputSlot.block.textSignature = encodeTextSignatureV1(item.id, phase);
2563
+ const contentIndex = outputSlot.contentIndex;
2564
+ if (contentIndex === void 0) throw new Error("Responses stream finalized text without a content index");
2565
+ lastTextBlock = {
2566
+ block: outputSlot.block,
2567
+ index: contentIndex,
2568
+ phase
2569
+ };
2570
+ stream.push({
2571
+ type: "text_end",
2572
+ contentIndex,
2573
+ content: outputSlot.block.text,
2574
+ partial: output
2575
+ });
2576
+ }
2577
+ outputSlots.forget(outputSlot);
2578
+ } else if (item.type === "function_call") {
2579
+ const streamingToolCall = streamingToolCalls.resolve(event, readResponsesToolCallItemIdentity(item));
2580
+ if (!streamingToolCall && streamingToolCalls.hasActive()) continue;
2581
+ const completedName = resolveCompletedToolCallName(streamingToolCall, item.name);
2582
+ const streamedArguments = streamingToolCall?.block.partialJson ?? "";
2583
+ const completedArguments = typeof item.arguments === "string" ? item.arguments : void 0;
2584
+ if (streamingToolCall && !streamingToolCall.argumentStreamReliable && !completedArguments) continue;
2585
+ const args = parseStreamingJson(completedArguments !== void 0 && (completedArguments.length > 0 || !streamedArguments) ? completedArguments : streamedArguments || "{}");
2586
+ let toolCall;
2587
+ let contentIndex;
2588
+ if (streamingToolCall) {
2589
+ const block = streamingToolCall.block;
2590
+ block.id = resolveResponsesToolCallId(item, block.id);
2591
+ block.name = completedName;
2592
+ block.arguments = args;
2593
+ delete block.partialJson;
2594
+ toolCall = block;
2595
+ contentIndex = streamingToolCall.contentIndex;
2596
+ } else {
2597
+ toolCall = {
2598
+ type: "toolCall",
2599
+ id: resolveResponsesToolCallId(item),
2600
+ name: completedName,
2601
+ arguments: args
2602
+ };
2603
+ blocks.push(toolCall);
2604
+ contentIndex = blockIndex();
2605
+ stream.push({
2606
+ type: "toolcall_start",
2607
+ contentIndex,
2608
+ partial: output
2609
+ });
2610
+ }
2611
+ if (streamingToolCall) {
2612
+ streamingToolCalls.forget(streamingToolCall);
2613
+ for (const slot of outputSlots.values()) if (slot.type === "toolCall" && slot.toolCall === streamingToolCall) outputSlots.forget(slot);
2614
+ }
2615
+ stream.push({
2616
+ type: "toolcall_end",
2617
+ contentIndex,
2618
+ toolCall,
2619
+ partial: output
2620
+ });
2621
+ }
2622
+ } else if (event.type === "response.completed" || event.type === "response.incomplete") {
2623
+ if (streamingToolCalls.hasActive()) throw new Error("Responses stream completed with unresolved tool calls");
2624
+ finalizeResponse(event.response);
2625
+ if (event.type === "response.completed" || output.stopReason === "length") recoverTerminalOutput(event.response.output ?? [], event.type === "response.completed");
2626
+ if (output.stopReason === "stop" && output.content.some((block) => block.type === "toolCall")) output.stopReason = "toolUse";
2627
+ break;
2628
+ } else if (event.type === "error") throw new Error(event.message ? `Error Code ${event.code}: ${event.message}` : "Unknown error");
2629
+ else if (event.type === "response.failed") {
2630
+ const failure = normalizeResponsesFailedEvent(event, model);
2631
+ if (failure.responseId) output.responseId = failure.responseId;
2632
+ throw new ResponsesStreamFailure(failure, event.response);
2633
+ }
2634
+ if (streamingToolCalls.hasActive()) throw new Error("Responses stream ended with unresolved tool calls");
2635
+ if (!terminalResponseEvent) throw new Error("OpenAI Responses stream ended before a terminal response event");
2636
+ } finally {
2637
+ for (const block of output.content) delete block.partialJson;
2638
+ }
2639
+ }
2640
+ //#endregion
2641
+ //#region packages/ai/src/providers/openai-tool-schema-compat.ts
2642
+ const OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS = /* @__PURE__ */ new Set([
2643
+ "$defs",
2644
+ "definitions",
2645
+ "dependentSchemas",
2646
+ "dependencies",
2647
+ "patternProperties",
2648
+ "properties"
2649
+ ]);
2650
+ const OPENAI_NULLABLE_ANNOTATION_KEYS = /* @__PURE__ */ new Set([
2651
+ "default",
2652
+ "description",
2653
+ "examples",
2654
+ "format",
2655
+ "title"
2656
+ ]);
2657
+ const OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS = /* @__PURE__ */ new Set([
2658
+ "additionalItems",
2659
+ "additionalProperties",
2660
+ "allOf",
2661
+ "anyOf",
2662
+ "contains",
2663
+ "contentSchema",
2664
+ "else",
2665
+ "if",
2666
+ "items",
2667
+ "not",
2668
+ "oneOf",
2669
+ "prefixItems",
2670
+ "propertyNames",
2671
+ "then",
2672
+ "unevaluatedItems",
2673
+ "unevaluatedProperties"
2674
+ ]);
2675
+ function normalizeOpenAIStrictCompatSchemaMap(schema) {
2676
+ if (!schema || typeof schema !== "object" || Array.isArray(schema)) return schema;
2677
+ let changed = false;
2678
+ const normalized = {};
2679
+ for (const [key, value] of Object.entries(schema)) {
2680
+ const next = normalizeOpenAIStrictCompatSchemaRecursive(value, { promoteEmptyObject: false });
2681
+ normalized[key] = next;
2682
+ changed ||= next !== value;
2683
+ }
2684
+ return changed ? normalized : schema;
2685
+ }
2686
+ function normalizeOpenAIStrictCompatSchemaRecursive(schema, options) {
2687
+ if (Array.isArray(schema)) {
2688
+ let changed = false;
2689
+ const normalized = schema.map((entry) => {
2690
+ const next = normalizeOpenAIStrictCompatSchemaRecursive(entry, { promoteEmptyObject: false });
2691
+ changed ||= next !== entry;
2692
+ return next;
2693
+ });
2694
+ return changed ? normalized : schema;
2695
+ }
2696
+ if (!schema || typeof schema !== "object") return schema;
2697
+ const record = schema;
2698
+ let changed = false;
2699
+ let hadNullType = false;
2700
+ const normalized = {};
2701
+ for (const [key, value] of Object.entries(record)) {
2702
+ if (value === null && OPENAI_NULLABLE_ANNOTATION_KEYS.has(key)) {
2703
+ changed = true;
2704
+ continue;
2705
+ }
2706
+ if (value === null && key === "type") {
2707
+ hadNullType = true;
2708
+ changed = true;
2709
+ continue;
2710
+ }
2711
+ const next = OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS.has(key) ? normalizeOpenAIStrictCompatSchemaMap(value) : OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS.has(key) ? normalizeOpenAIStrictCompatSchemaRecursive(value, { promoteEmptyObject: false }) : value;
2712
+ normalized[key] = next;
2713
+ changed ||= next !== value;
2714
+ }
2715
+ if (Object.keys(normalized).length === 0) {
2716
+ if (!options.promoteEmptyObject) return schema;
2717
+ return {
2718
+ type: "object",
2719
+ properties: {},
2720
+ required: [],
2721
+ additionalProperties: false
2722
+ };
2723
+ }
2724
+ const hasObjectShapeHints = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) || Array.isArray(normalized.required);
2725
+ const hasArrayShapeHints = "items" in normalized;
2726
+ if (!("type" in normalized) && hasObjectShapeHints !== hasArrayShapeHints) {
2727
+ normalized.type = hasObjectShapeHints ? "object" : "array";
2728
+ changed = true;
2729
+ } else if (hadNullType && !("type" in normalized)) normalized.type = null;
2730
+ if (normalized.type === "object" && !("properties" in normalized)) {
2731
+ normalized.properties = {};
2732
+ changed = true;
2733
+ }
2734
+ const hasEmptyProperties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) && Object.keys(normalized.properties).length === 0;
2735
+ if (normalized.type === "object" && !Array.isArray(normalized.required) && hasEmptyProperties) {
2736
+ normalized.required = [];
2737
+ changed = true;
2738
+ }
2739
+ if (normalized.type === "object" && hasEmptyProperties && !("additionalProperties" in normalized)) {
2740
+ normalized.additionalProperties = false;
2741
+ changed = true;
2742
+ }
2743
+ return changed ? normalized : schema;
2744
+ }
2745
+ /** Repairs recoverable OpenAI tool-schema shapes before canonical normalization. */
2746
+ function normalizeOpenAIStrictCompatSchema(schema) {
2747
+ return normalizeOpenAIStrictCompatSchemaRecursive(schema, { promoteEmptyObject: true });
2748
+ }
2749
+ /** Finds schema paths that violate OpenAI strict tool-schema requirements. */
2750
+ function findOpenAIStrictSchemaViolations(schema, path, options) {
2751
+ if (Array.isArray(schema)) {
2752
+ if (options?.requireObjectRoot) return [`${path}.type`];
2753
+ return schema.flatMap((item, index) => findOpenAIStrictSchemaViolations(item, `${path}[${index}]`));
2754
+ }
2755
+ if (!schema || typeof schema !== "object") return options?.requireObjectRoot ? [`${path}.type`] : [];
2756
+ const record = schema;
2757
+ const violations = [];
2758
+ for (const key of [
2759
+ "anyOf",
2760
+ "oneOf",
2761
+ "allOf"
2762
+ ]) if (key in record) violations.push(`${path}.${key}`);
2763
+ if (Array.isArray(record.type)) violations.push(`${path}.type`);
2764
+ const properties = record.properties && typeof record.properties === "object" && !Array.isArray(record.properties) ? record.properties : void 0;
2765
+ if (record.type === "object") {
2766
+ if (record.additionalProperties !== false) violations.push(`${path}.additionalProperties`);
2767
+ const required = Array.isArray(record.required) ? record.required.filter((entry) => typeof entry === "string") : void 0;
2768
+ if (!required) violations.push(`${path}.required`);
2769
+ else if (properties) {
2770
+ const requiredSet = new Set(required);
2771
+ for (const key of Object.keys(properties)) if (!requiredSet.has(key)) violations.push(`${path}.required.${key}`);
2772
+ }
2773
+ }
2774
+ for (const key of OPENAI_STRICT_COMPAT_SCHEMA_MAP_KEYS) {
2775
+ const schemaMap = record[key];
2776
+ if (!schemaMap || typeof schemaMap !== "object" || Array.isArray(schemaMap)) continue;
2777
+ for (const [entryKey, value] of Object.entries(schemaMap)) violations.push(...findOpenAIStrictSchemaViolations(value, `${path}.${key}.${entryKey}`));
2778
+ }
2779
+ for (const key of OPENAI_STRICT_COMPAT_SCHEMA_NESTED_KEYS) {
2780
+ const value = record[key];
2781
+ if (value && typeof value === "object") violations.push(...findOpenAIStrictSchemaViolations(value, `${path}.${key}`));
2782
+ }
2783
+ return violations;
2784
+ }
2785
+ //#endregion
2786
+ //#region packages/ai/src/providers/openai-tool-schema.ts
2787
+ /**
2788
+ * OpenAI strict JSON-schema normalization for tool inventories and request payloads.
2789
+ *
2790
+ * Caches normalized object inputs by provider compatibility so repeated inventory builds preserve identity.
2791
+ */
2792
+ const MAX_STRICT_SCHEMA_CACHE_ENTRIES_PER_SCHEMA = 8;
2793
+ let strictOpenAISchemaCache = /* @__PURE__ */ new WeakMap();
2794
+ function resolveToolSchemaModelCompat(compat) {
2795
+ if (!compat) return;
2796
+ const unsupportedToolSchemaKeywords = Array.isArray(compat.unsupportedToolSchemaKeywords) ? compat.unsupportedToolSchemaKeywords.filter((keyword) => typeof keyword === "string") : [];
2797
+ if (unsupportedToolSchemaKeywords.length === 0 && compat.omitEmptyArrayItems !== true) return;
2798
+ return {
2799
+ ...unsupportedToolSchemaKeywords.length > 0 ? { unsupportedToolSchemaKeywords } : {},
2800
+ ...compat.omitEmptyArrayItems === true ? { omitEmptyArrayItems: true } : {}
2801
+ };
2802
+ }
2803
+ function resolveStrictOpenAISchemaCacheKey(modelCompat) {
2804
+ const compat = resolveToolSchemaModelCompat(modelCompat);
2805
+ return JSON.stringify([[...compat?.unsupportedToolSchemaKeywords ?? []].toSorted(), shouldOmitEmptyArrayItems(compat)]);
2806
+ }
2807
+ function readCachedStrictOpenAISchema(schema, key) {
2808
+ return strictOpenAISchemaCache.get(schema)?.find((entry) => entry.key === key)?.value;
1383
2809
  }
1384
2810
  function rememberStrictOpenAISchema(schema, key, value) {
1385
2811
  const entries = strictOpenAISchemaCache.get(schema) ?? [];
@@ -1493,7 +2919,7 @@ function convertResponsesToolPayload(tools, options) {
1493
2919
  const strict = resolveResponsesStrictToolFlag(projection, resolveResponsesStrictToolSetting(options), options?.model);
1494
2920
  return {
1495
2921
  projection,
1496
- tools: sortResponsesToolsByName(projection.tools).map((tool) => {
2922
+ tools: sortPromptCacheToolsByName(projection.tools).map((tool) => {
1497
2923
  const result = {
1498
2924
  type: "function",
1499
2925
  name: tool.name,
@@ -1549,16 +2975,6 @@ function shouldLogStrictToolDowngradeDiagnostic(diagnostics, model) {
1549
2975
  loggedStrictToolDowngradeDiagnosticKeys.add(key);
1550
2976
  return true;
1551
2977
  }
1552
- function compareToolText(left, right) {
1553
- const leftText = left ?? "";
1554
- const rightText = right ?? "";
1555
- if (leftText < rightText) return -1;
1556
- if (leftText > rightText) return 1;
1557
- return 0;
1558
- }
1559
- function sortResponsesToolsByName(tools) {
1560
- return tools.toSorted((left, right) => compareToolText(left.name, right.name) || compareToolText(left.description, right.description));
1561
- }
1562
2978
  //#endregion
1563
2979
  //#region packages/ai/src/providers/openai-responses-shared.ts
1564
2980
  const EMPTY_TOOL_RESULT_TEXT = "(no output)";
@@ -1566,16 +2982,6 @@ function splitResponsesToolCallId(id) {
1566
2982
  const separatorIndex = id.indexOf("|");
1567
2983
  return separatorIndex === -1 ? [id, void 0] : [id.slice(0, separatorIndex), id.slice(separatorIndex + 1)];
1568
2984
  }
1569
- function resolveResponsesToolCallId(item, fallbackId) {
1570
- const callId = typeof item.call_id === "string" ? item.call_id.trim() : "";
1571
- const itemId = typeof item.id === "string" ? item.id.trim() : "";
1572
- const [fallbackCallId, fallbackItemId = ""] = splitResponsesToolCallId(fallbackId ?? "");
1573
- const resolvedCallId = callId || fallbackCallId;
1574
- const resolvedItemId = itemId || fallbackItemId;
1575
- if (resolvedCallId) return resolvedItemId ? `${resolvedCallId}|${resolvedItemId}` : resolvedCallId;
1576
- const generatedCallId = `call_${randomUUID().replaceAll("-", "").slice(0, 24)}`;
1577
- return resolvedItemId ? `${generatedCallId}|${resolvedItemId}` : generatedCallId;
1578
- }
1579
2985
  function sanitizeToolResultText(text, fallback) {
1580
2986
  const sanitized = sanitizeSurrogates(text);
1581
2987
  return sanitized.trim().length > 0 ? sanitized : fallback;
@@ -1586,14 +2992,6 @@ function normalizeResponsesReasoningReplayItem(params) {
1586
2992
  if (!params.replayResponsesItemIds) delete next.id;
1587
2993
  return next;
1588
2994
  }
1589
- function encodeTextSignatureV1(id, phase) {
1590
- const payload = {
1591
- v: 1,
1592
- id
1593
- };
1594
- if (phase) payload.phase = phase;
1595
- return JSON.stringify(payload);
1596
- }
1597
2995
  function parseTextSignature(signature) {
1598
2996
  if (!signature) return;
1599
2997
  if (signature.startsWith("{")) try {
@@ -1864,9 +3262,10 @@ async function runResponsesStreamLifecycle(params) {
1864
3262
  ...params.processStreamOptions,
1865
3263
  firstEventTimeoutMs: params.processStreamOptions?.firstEventTimeoutMs ?? firstEventTimeoutMs,
1866
3264
  abortFirstEventStream: params.processStreamOptions?.abortFirstEventStream ?? firstEventAbort.abort,
1867
- onFirstEventTimeout: params.processStreamOptions?.onFirstEventTimeout ?? onFirstEventTimeout
3265
+ onFirstEventTimeout: params.processStreamOptions?.onFirstEventTimeout ?? onFirstEventTimeout,
3266
+ signal: params.processStreamOptions?.signal ?? options?.signal
1868
3267
  } : void 0);
1869
- if (options?.signal?.aborted) throw new Error("Request was aborted");
3268
+ if (options?.signal?.aborted) throw transportAbortError(options.signal);
1870
3269
  if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
1871
3270
  stream.push({
1872
3271
  type: "done",
@@ -1888,505 +3287,132 @@ async function runResponsesStreamLifecycle(params) {
1888
3287
  firstEventAbort?.dispose();
1889
3288
  }
1890
3289
  }
1891
- async function processResponsesStream(openaiStream, output, stream, model, options) {
1892
- const streamingToolCalls = createResponsesToolCallTracker();
1893
- const outputSlots = /* @__PURE__ */ new Map();
1894
- const reasoningBlocksById = /* @__PURE__ */ new Map();
1895
- let unindexedOutputSlot;
1896
- let terminalResponseEvent;
1897
- let lastTextBlock = null;
1898
- const blocks = output.content;
1899
- const blockIndex = () => blocks.length - 1;
1900
- const readOutputIndex = (event) => {
1901
- const outputIndex = event.output_index;
1902
- return typeof outputIndex === "number" && Number.isInteger(outputIndex) && outputIndex >= 0 ? outputIndex : void 0;
1903
- };
1904
- const registerOutputSlot = (event, slot) => {
1905
- const outputIndex = readOutputIndex(event);
1906
- if (outputIndex === void 0) {
1907
- if (unindexedOutputSlot) throw new Error("Responses stream added overlapping unindexed output items");
1908
- unindexedOutputSlot = slot;
1909
- return;
1910
- }
1911
- if (outputSlots.has(outputIndex)) throw new Error(`Responses stream reused active output index ${outputIndex}`);
1912
- outputSlots.set(outputIndex, slot);
1913
- };
1914
- const resolveOutputSlot = (event, type) => {
1915
- const outputIndex = readOutputIndex(event);
1916
- let slot = outputIndex === void 0 ? unindexedOutputSlot : outputSlots.get(outputIndex);
1917
- if (outputIndex === void 0 && !slot) {
1918
- const matchingSlots = [...outputSlots.values()].filter((candidate) => candidate.type === type);
1919
- slot = matchingSlots.length === 1 ? matchingSlots[0] : void 0;
1920
- }
1921
- return slot?.type === type ? slot : void 0;
1922
- };
1923
- const forgetOutputSlot = (event, slot) => {
1924
- const outputIndex = readOutputIndex(event);
1925
- if (outputIndex === void 0) {
1926
- if (unindexedOutputSlot === slot) unindexedOutputSlot = void 0;
1927
- else for (const [indexedOutput, indexedSlot] of outputSlots) if (indexedSlot === slot) outputSlots.delete(indexedOutput);
1928
- return;
1929
- }
1930
- if (outputSlots.get(outputIndex) === slot) outputSlots.delete(outputIndex);
1931
- };
1932
- const forgetToolCallOutputSlot = (toolCall) => {
1933
- for (const [outputIndex, slot] of outputSlots) if (slot.type === "toolCall" && slot.toolCall === toolCall) outputSlots.delete(outputIndex);
1934
- };
1935
- const readIdentityValue = (value) => {
1936
- return (typeof value === "string" ? value.trim() : "") || void 0;
1937
- };
1938
- const resolveCompletedToolCallName = (toolCall, value) => {
1939
- const streamedName = readIdentityValue(toolCall?.block.name);
1940
- const completedName = readIdentityValue(value);
1941
- if (streamedName && completedName && streamedName !== completedName) throw new Error(`Responses stream changed tool-call function name from ${streamedName} to ${completedName}`);
1942
- const name = completedName ?? streamedName;
1943
- if (!name) throw new Error("Responses stream completed tool call without a function name");
1944
- return name;
1945
- };
1946
- const createOutputSlot = (event, item) => {
1947
- if (item.type === "reasoning") {
1948
- const block = {
1949
- type: "thinking",
1950
- thinking: ""
1951
- };
1952
- const slot = {
1953
- type: "thinking",
1954
- item,
1955
- block,
1956
- contentIndex: blocks.length
1957
- };
1958
- blocks.push(block);
1959
- registerOutputSlot(event, slot);
1960
- stream.push({
1961
- type: "thinking_start",
1962
- contentIndex: slot.contentIndex,
1963
- partial: output
1964
- });
1965
- return slot;
1966
- }
1967
- if (item.type === "message") {
1968
- const messageItem = item;
1969
- const collapseCandidate = lastTextBlock;
1970
- const block = collapseCandidate ? null : {
1971
- type: "text",
1972
- text: "",
1973
- ...messageItem.phase ? { textSignature: encodeTextSignatureV1(messageItem.id, messageItem.phase) } : {}
1974
- };
1975
- const slot = {
1976
- type: "text",
1977
- item: messageItem,
1978
- block,
1979
- contentIndex: block ? blocks.length : void 0,
1980
- pendingText: collapseCandidate ? "" : null,
1981
- collapseCandidate
1982
- };
1983
- if (block) blocks.push(block);
1984
- registerOutputSlot(event, slot);
1985
- if (slot.contentIndex !== void 0) stream.push({
1986
- type: "text_start",
1987
- contentIndex: slot.contentIndex,
1988
- partial: output
1989
- });
1990
- return slot;
1991
- }
1992
- };
1993
- const resolveOutputItemSlot = (event, item) => {
1994
- if (item.type === "reasoning") return resolveOutputSlot(event, "thinking");
1995
- if (item.type === "message") return resolveOutputSlot(event, "text");
1996
- const outputIndex = readOutputIndex(event);
1997
- return outputIndex === void 0 ? void 0 : outputSlots.get(outputIndex);
1998
- };
1999
- const getOrCreateOutputSlot = (event, item) => {
2000
- return resolveOutputItemSlot(event, item) ?? createOutputSlot(event, item);
2001
- };
2002
- const materializeDeferredTextSlot = (slot) => {
2003
- if (slot.block || slot.pendingText === null) return;
2004
- const text = slot.pendingText;
2005
- slot.block = {
2006
- type: "text",
2007
- text,
2008
- ...slot.item.phase ? { textSignature: encodeTextSignatureV1(slot.item.id, slot.item.phase) } : {}
2009
- };
2010
- blocks.push(slot.block);
2011
- slot.contentIndex = blockIndex();
2012
- stream.push({
2013
- type: "text_start",
2014
- contentIndex: slot.contentIndex,
2015
- partial: output
2016
- });
2017
- if (text) stream.push({
2018
- type: "text_delta",
2019
- contentIndex: slot.contentIndex,
2020
- delta: text,
2021
- partial: output
2022
- });
2023
- if (lastTextBlock === slot.collapseCandidate) lastTextBlock = null;
2024
- slot.pendingText = null;
2025
- slot.collapseCandidate = null;
2026
- };
2027
- const materializeDeferredTextSlots = (except) => {
2028
- for (const slot of outputSlots.values()) if (slot !== except && slot.type === "text") materializeDeferredTextSlot(slot);
2029
- if (unindexedOutputSlot !== except && unindexedOutputSlot?.type === "text") materializeDeferredTextSlot(unindexedOutputSlot);
2030
- };
2031
- const appendPendingMessageDelta = (slot, delta) => {
2032
- slot.pendingText = `${slot.pendingText ?? ""}${delta}`;
2033
- const priorText = slot.collapseCandidate?.block.text ?? "";
2034
- if (priorText.startsWith(slot.pendingText) || slot.pendingText.startsWith(priorText)) return;
2035
- materializeDeferredTextSlot(slot);
2036
- };
2037
- const backfillReasoningSignatures = (responseOutput) => {
2038
- for (const item of responseOutput) {
2039
- if (item.type !== "reasoning" || !item.encrypted_content) continue;
2040
- const block = reasoningBlocksById.get(item.id);
2041
- if (!block?.thinkingSignature) continue;
2042
- const storedItem = JSON.parse(block.thinkingSignature);
2043
- if (storedItem.encrypted_content) continue;
2044
- block.thinkingSignature = JSON.stringify({
2045
- ...storedItem,
2046
- encrypted_content: item.encrypted_content
2047
- });
2048
- }
2049
- };
2050
- const finalizeResponse = (response) => {
2051
- terminalResponseEvent = "finalized";
2052
- backfillReasoningSignatures(response.output ?? []);
2053
- if (response.id) output.responseId = response.id;
2054
- const mappedUsage = mapResponsesTerminalUsage(response.usage);
2055
- if (mappedUsage) output.usage = {
2056
- ...mappedUsage,
2057
- cost: {
2058
- input: 0,
2059
- output: 0,
2060
- cacheRead: 0,
2061
- cacheWrite: 0,
2062
- total: 0
2063
- }
2064
- };
2065
- calculateCost(model, output.usage);
2066
- if (options?.applyServiceTierPricing) {
2067
- const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response.service_tier, options.serviceTier) : response.service_tier ?? options.serviceTier;
2068
- options.applyServiceTierPricing(output.usage, serviceTier);
2069
- }
2070
- const terminal = resolveResponsesTerminalStopReason({
2071
- status: response.status,
2072
- incompleteReason: response.incomplete_details?.reason,
2073
- hasToolCall: output.content.some((block) => block.type === "toolCall")
2074
- });
2075
- output.stopReason = terminal.stopReason;
2076
- if (terminal.errorMessage) output.errorMessage = terminal.errorMessage;
3290
+ //#endregion
3291
+ //#region packages/ai/src/providers/openai-responses.ts
3292
+ var openai_responses_exports = /* @__PURE__ */ __exportAll({
3293
+ streamOpenAIResponses: () => streamOpenAIResponses,
3294
+ streamSimpleOpenAIResponses: () => streamSimpleOpenAIResponses
3295
+ });
3296
+ const OPENAI_TOOL_CALL_PROVIDERS = /* @__PURE__ */ new Set(["openai", "opencode"]);
3297
+ function getCompat(model) {
3298
+ return {
3299
+ sendSessionIdHeader: model.compat?.sendSessionIdHeader ?? true,
3300
+ supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true
2077
3301
  };
2078
- const guardedStream = withFirstStreamEventTimeout(openaiStream, {
2079
- provider: model.provider,
2080
- api: model.api,
2081
- model: model.id,
2082
- timeoutMs: options?.firstEventTimeoutMs ?? 0,
2083
- stage: "responses",
2084
- abort: options?.abortFirstEventStream,
2085
- onTimeout: options?.onFirstEventTimeout,
2086
- hint: "The provider may be stalled while parsing the tool payload; retry with a smaller tool surface or enable OPENCLAW_DEBUG_MODEL_PAYLOAD=tools to inspect exposed tools."
3302
+ }
3303
+ function getPromptCacheRetention(compat, cacheRetention) {
3304
+ return cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0;
3305
+ }
3306
+ function formatOpenAIResponsesError(error) {
3307
+ if (error instanceof Error) {
3308
+ const status = error.status;
3309
+ const statusCode = typeof status === "number" ? status : void 0;
3310
+ if (statusCode !== void 0) return `OpenAI API error (${statusCode}): ${error.message}`;
3311
+ return error.message;
3312
+ }
3313
+ try {
3314
+ return JSON.stringify(error);
3315
+ } catch {
3316
+ return String(error);
3317
+ }
3318
+ }
3319
+ /**
3320
+ * Generate function for OpenAI Responses API
3321
+ */
3322
+ const streamOpenAIResponses = (model, context, options) => {
3323
+ const stream = new AssistantMessageEventStream();
3324
+ runResponsesStreamLifecycle({
3325
+ stream,
3326
+ model,
3327
+ output: createResponsesAssistantOutput(model),
3328
+ options,
3329
+ createClient: () => {
3330
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
3331
+ const cacheSessionId = resolveCacheRetention$1(options?.cacheRetention) === "none" ? void 0 : options?.sessionId;
3332
+ return createClient(model, context, apiKey, options?.headers, cacheSessionId);
3333
+ },
3334
+ buildParams: () => buildParams(model, context, options),
3335
+ processStreamOptions: {
3336
+ serviceTier: options?.serviceTier,
3337
+ applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model)
3338
+ },
3339
+ formatError: formatOpenAIResponsesError
2087
3340
  });
2088
- for await (const event of guardedStream) if (event.type === "response.created") output.responseId = event.response.id;
2089
- else if (event.type === "response.output_item.added") {
2090
- materializeDeferredTextSlots();
2091
- const item = event.item;
2092
- if (item.type !== "message") lastTextBlock = null;
2093
- if (item.type === "reasoning" || item.type === "message") createOutputSlot(event, item);
2094
- else if (item.type === "function_call") {
2095
- const toolCallBlock = {
2096
- type: "toolCall",
2097
- id: resolveResponsesToolCallId(item),
2098
- name: readIdentityValue(item.name) ?? "",
2099
- arguments: {},
2100
- partialJson: item.arguments || ""
2101
- };
2102
- const contentIndex = output.content.length;
2103
- const toolCallState = {
2104
- block: toolCallBlock,
2105
- contentIndex,
2106
- argumentStreamReliable: true,
2107
- ...readResponsesToolCallItemIdentity(item)
2108
- };
2109
- streamingToolCalls.register(event, toolCallState);
2110
- if (readOutputIndex(event) !== void 0) registerOutputSlot(event, {
2111
- type: "toolCall",
2112
- toolCall: toolCallState
2113
- });
2114
- output.content.push(toolCallBlock);
2115
- stream.push({
2116
- type: "toolcall_start",
2117
- contentIndex,
2118
- partial: output
2119
- });
2120
- }
2121
- } else if (event.type === "response.reasoning_summary_part.added") {
2122
- const slot = resolveOutputSlot(event, "thinking");
2123
- if (!slot) continue;
2124
- slot.item.summary = slot.item.summary || [];
2125
- slot.item.summary.push(event.part);
2126
- } else if (event.type === "response.reasoning_summary_text.delta") {
2127
- const slot = resolveOutputSlot(event, "thinking");
2128
- if (!slot) continue;
2129
- slot.item.summary = slot.item.summary || [];
2130
- const lastPart = slot.item.summary[slot.item.summary.length - 1];
2131
- if (!lastPart) continue;
2132
- slot.block.thinking += event.delta;
2133
- lastPart.text += event.delta;
2134
- stream.push({
2135
- type: "thinking_delta",
2136
- contentIndex: slot.contentIndex,
2137
- delta: event.delta,
2138
- partial: output
2139
- });
2140
- } else if (event.type === "response.reasoning_summary_part.done") {
2141
- const slot = resolveOutputSlot(event, "thinking");
2142
- if (!slot) continue;
2143
- slot.item.summary = slot.item.summary || [];
2144
- const lastPart = slot.item.summary[slot.item.summary.length - 1];
2145
- if (!lastPart) continue;
2146
- slot.block.thinking += "\n\n";
2147
- lastPart.text += "\n\n";
2148
- stream.push({
2149
- type: "thinking_delta",
2150
- contentIndex: slot.contentIndex,
2151
- delta: "\n\n",
2152
- partial: output
2153
- });
2154
- } else if (event.type === "response.reasoning_text.delta") {
2155
- const slot = resolveOutputSlot(event, "thinking");
2156
- if (!slot) continue;
2157
- slot.block.thinking += event.delta;
2158
- stream.push({
2159
- type: "thinking_delta",
2160
- contentIndex: slot.contentIndex,
2161
- delta: event.delta,
2162
- partial: output
3341
+ return stream;
3342
+ };
3343
+ const streamSimpleOpenAIResponses = (model, context, options) => {
3344
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
3345
+ if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
3346
+ const base = buildBaseOptions(model, options, apiKey);
3347
+ return streamOpenAIResponses(model, context, {
3348
+ ...base,
3349
+ reasoningEffort: resolveResponsesReasoningEffort(model, options?.reasoning),
3350
+ replayResponsesItemIds: options?.replayResponsesItemIds
3351
+ });
3352
+ };
3353
+ function createClient(model, context, apiKey, optionsHeaders, sessionId) {
3354
+ if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
3355
+ const compat = getCompat(model);
3356
+ const headers = { ...model.headers };
3357
+ if (model.provider === "github-copilot") {
3358
+ const hasImages = hasCopilotVisionInput(context.messages);
3359
+ const copilotHeaders = buildCopilotDynamicHeaders({
3360
+ messages: context.messages,
3361
+ hasImages
2163
3362
  });
2164
- } else if (event.type === "response.content_part.added") {
2165
- const slot = resolveOutputSlot(event, "text");
2166
- if (!slot) continue;
2167
- slot.item.content = slot.item.content || [];
2168
- if (event.part.type === "output_text" || event.part.type === "text" || event.part.type === "refusal") slot.item.content.push(event.part);
2169
- } else if (event.type === "response.output_text.delta") {
2170
- const slot = resolveOutputSlot(event, "text");
2171
- if (!slot?.item.content || slot.item.content.length === 0) continue;
2172
- const lastPart = slot.item.content[slot.item.content.length - 1];
2173
- if (!isResponsesTextContentPartType(lastPart?.type)) continue;
2174
- lastPart.text += event.delta;
2175
- if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2176
- else if (slot.block && slot.contentIndex !== void 0) {
2177
- slot.block.text += event.delta;
2178
- stream.push({
2179
- type: "text_delta",
2180
- contentIndex: slot.contentIndex,
2181
- delta: event.delta,
2182
- partial: output
2183
- });
2184
- }
2185
- } else if (isAzureResponsesTextDeltaEvent(event)) {
2186
- const slot = resolveOutputSlot(event, "text");
2187
- if (!slot) continue;
2188
- slot.item.content = slot.item.content || [];
2189
- let lastPart = slot.item.content[slot.item.content.length - 1];
2190
- if (lastPart?.type !== "text") {
2191
- lastPart = {
2192
- type: "text",
2193
- text: ""
2194
- };
2195
- slot.item.content.push(lastPart);
2196
- }
2197
- lastPart.text += event.delta;
2198
- if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2199
- else if (slot.block && slot.contentIndex !== void 0) {
2200
- slot.block.text += event.delta;
2201
- stream.push({
2202
- type: "text_delta",
2203
- contentIndex: slot.contentIndex,
2204
- delta: event.delta,
2205
- partial: output
2206
- });
2207
- }
2208
- } else if (event.type === "response.refusal.delta") {
2209
- const slot = resolveOutputSlot(event, "text");
2210
- if (!slot?.item.content || slot.item.content.length === 0) continue;
2211
- const lastPart = slot.item.content[slot.item.content.length - 1];
2212
- if (lastPart?.type !== "refusal") continue;
2213
- lastPart.refusal += event.delta;
2214
- if (slot.pendingText !== null) appendPendingMessageDelta(slot, event.delta);
2215
- else if (slot.block && slot.contentIndex !== void 0) {
2216
- slot.block.text += event.delta;
2217
- stream.push({
2218
- type: "text_delta",
2219
- contentIndex: slot.contentIndex,
2220
- delta: event.delta,
2221
- partial: output
2222
- });
2223
- }
2224
- } else if (event.type === "response.function_call_arguments.delta") {
2225
- const toolCall = streamingToolCalls.resolve(event);
2226
- if (toolCall) {
2227
- toolCall.block.partialJson += event.delta;
2228
- toolCall.block.arguments = parseStreamingJson(toolCall.block.partialJson);
2229
- stream.push({
2230
- type: "toolcall_delta",
2231
- contentIndex: toolCall.contentIndex,
2232
- delta: event.delta,
2233
- partial: output
2234
- });
2235
- } else if (streamingToolCalls.hasActive()) streamingToolCalls.markArgumentsUnreliable();
2236
- } else if (event.type === "response.function_call_arguments.done") {
2237
- const toolCall = streamingToolCalls.resolve(event);
2238
- if (toolCall) {
2239
- const previousPartialJson = toolCall.block.partialJson;
2240
- const doneArguments = typeof event.arguments === "string" ? event.arguments : void 0;
2241
- if (doneArguments !== void 0 && (doneArguments.length > 0 || previousPartialJson === "")) {
2242
- toolCall.block.partialJson = doneArguments;
2243
- toolCall.block.arguments = parseStreamingJson(toolCall.block.partialJson);
2244
- toolCall.argumentStreamReliable = true;
2245
- }
2246
- if (doneArguments?.startsWith(previousPartialJson)) {
2247
- const delta = doneArguments.slice(previousPartialJson.length);
2248
- if (delta.length > 0) stream.push({
2249
- type: "toolcall_delta",
2250
- contentIndex: toolCall.contentIndex,
2251
- delta,
2252
- partial: output
2253
- });
2254
- }
2255
- } else if (streamingToolCalls.hasActive()) streamingToolCalls.markArgumentsUnreliable();
2256
- } else if (event.type === "response.output_item.done") {
2257
- const item = event.item;
2258
- if (item.type !== "message") lastTextBlock = null;
2259
- const existingOutputSlot = resolveOutputItemSlot(event, item);
2260
- materializeDeferredTextSlots(existingOutputSlot);
2261
- const outputSlot = existingOutputSlot ?? getOrCreateOutputSlot(event, item);
2262
- if (item.type === "reasoning" && outputSlot?.type === "thinking") {
2263
- const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
2264
- const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
2265
- outputSlot.block.thinking = summaryText || contentText || outputSlot.block.thinking;
2266
- outputSlot.block.thinkingSignature = JSON.stringify(item);
2267
- if (typeof item.id === "string") reasoningBlocksById.set(item.id, outputSlot.block);
2268
- stream.push({
2269
- type: "thinking_end",
2270
- contentIndex: outputSlot.contentIndex,
2271
- content: outputSlot.block.thinking,
2272
- partial: output
2273
- });
2274
- forgetOutputSlot(event, outputSlot);
2275
- } else if (item.type === "message" && outputSlot?.type === "text" && (outputSlot.block || outputSlot.pendingText !== null)) {
2276
- const streamedText = outputSlot.pendingText ?? outputSlot.block?.text ?? "";
2277
- const finalText = item.content == null ? streamedText : item.content.map((c) => c.type === "output_text" || c.type === "text" ? c.text : c.refusal).join("");
2278
- const phase = item.phase ?? void 0;
2279
- const collapse = outputSlot.pendingText !== null ? resolveResponsesMessageSnapshotCollapse({
2280
- prior: outputSlot.collapseCandidate && {
2281
- text: outputSlot.collapseCandidate.block.text,
2282
- phase: outputSlot.collapseCandidate.phase
2283
- },
2284
- nextText: finalText,
2285
- nextPhase: phase
2286
- }) : { kind: "keep" };
2287
- outputSlot.pendingText = null;
2288
- if (collapse.kind === "extend" && outputSlot.collapseCandidate) {
2289
- outputSlot.collapseCandidate.block.text = collapse.text;
2290
- outputSlot.collapseCandidate.block.textSignature = encodeTextSignatureV1(item.id, phase);
2291
- stream.push({
2292
- type: "text_end",
2293
- contentIndex: outputSlot.collapseCandidate.index,
2294
- content: collapse.text,
2295
- partial: output
2296
- });
2297
- lastTextBlock = outputSlot.collapseCandidate;
2298
- } else {
2299
- if (!outputSlot.block) {
2300
- outputSlot.block = {
2301
- type: "text",
2302
- text: "",
2303
- ...phase ? { textSignature: encodeTextSignatureV1(item.id, phase) } : {}
2304
- };
2305
- blocks.push(outputSlot.block);
2306
- outputSlot.contentIndex = blockIndex();
2307
- stream.push({
2308
- type: "text_start",
2309
- contentIndex: outputSlot.contentIndex,
2310
- partial: output
2311
- });
2312
- }
2313
- outputSlot.block.text = finalText;
2314
- outputSlot.block.textSignature = encodeTextSignatureV1(item.id, phase);
2315
- const contentIndex = outputSlot.contentIndex;
2316
- if (contentIndex === void 0) throw new Error("Responses stream finalized text without a content index");
2317
- lastTextBlock = {
2318
- block: outputSlot.block,
2319
- index: contentIndex,
2320
- phase
2321
- };
2322
- stream.push({
2323
- type: "text_end",
2324
- contentIndex,
2325
- content: outputSlot.block.text,
2326
- partial: output
2327
- });
2328
- }
2329
- forgetOutputSlot(event, outputSlot);
2330
- } else if (item.type === "function_call") {
2331
- const streamingToolCall = streamingToolCalls.resolve(event, readResponsesToolCallItemIdentity(item));
2332
- if (!streamingToolCall && streamingToolCalls.hasActive()) continue;
2333
- const completedName = resolveCompletedToolCallName(streamingToolCall, item.name);
2334
- const streamedArguments = streamingToolCall?.block.partialJson ?? "";
2335
- const completedArguments = typeof item.arguments === "string" ? item.arguments : void 0;
2336
- if (streamingToolCall && !streamingToolCall.argumentStreamReliable && !completedArguments) continue;
2337
- const args = parseStreamingJson(completedArguments !== void 0 && (completedArguments.length > 0 || !streamedArguments) ? completedArguments : streamedArguments || "{}");
2338
- let toolCall;
2339
- let contentIndex;
2340
- if (streamingToolCall) {
2341
- const block = streamingToolCall.block;
2342
- block.id = resolveResponsesToolCallId(item, block.id);
2343
- block.name = completedName;
2344
- block.arguments = args;
2345
- delete block.partialJson;
2346
- toolCall = block;
2347
- contentIndex = streamingToolCall.contentIndex;
2348
- } else {
2349
- toolCall = {
2350
- type: "toolCall",
2351
- id: resolveResponsesToolCallId(item),
2352
- name: completedName,
2353
- arguments: args
2354
- };
2355
- blocks.push(toolCall);
2356
- contentIndex = blockIndex();
2357
- stream.push({
2358
- type: "toolcall_start",
2359
- contentIndex,
2360
- partial: output
2361
- });
2362
- }
2363
- if (streamingToolCall) {
2364
- streamingToolCalls.forget(streamingToolCall);
2365
- forgetToolCallOutputSlot(streamingToolCall);
2366
- }
2367
- stream.push({
2368
- type: "toolcall_end",
2369
- contentIndex,
2370
- toolCall,
2371
- partial: output
2372
- });
2373
- }
2374
- } else if (event.type === "response.completed" || event.type === "response.incomplete") {
2375
- if (streamingToolCalls.hasActive()) throw new Error("Responses stream completed with unresolved tool calls");
2376
- finalizeResponse(event.response);
2377
- } else if (event.type === "error") throw new Error(event.message ? `Error Code ${event.code}: ${event.message}` : "Unknown error");
2378
- else if (event.type === "response.failed") {
2379
- const error = event.response?.error;
2380
- const details = event.response?.incomplete_details;
2381
- output.responseId = event.response.id;
2382
- output.stopReason = "error";
2383
- output.errorMessage = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
2384
- terminalResponseEvent = "failed";
2385
- break;
2386
- }
2387
- if (terminalResponseEvent === "failed") return;
2388
- if (streamingToolCalls.hasActive()) throw new Error("Responses stream ended with unresolved tool calls");
2389
- if (!terminalResponseEvent) throw new Error("OpenAI Responses stream ended before a terminal response event");
3363
+ Object.assign(headers, copilotHeaders);
3364
+ }
3365
+ if (sessionId) {
3366
+ if (compat.sendSessionIdHeader) headers.session_id = sessionId;
3367
+ headers["x-client-request-id"] = sessionId;
3368
+ }
3369
+ if (optionsHeaders) Object.assign(headers, optionsHeaders);
3370
+ const defaultHeaders = model.provider === "cloudflare-ai-gateway" ? {
3371
+ ...headers,
3372
+ Authorization: headers.Authorization ?? null,
3373
+ "cf-aig-authorization": `Bearer ${apiKey}`
3374
+ } : headers;
3375
+ return new OpenAI({
3376
+ apiKey,
3377
+ baseURL: isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model) : model.baseUrl,
3378
+ dangerouslyAllowBrowser: true,
3379
+ defaultHeaders,
3380
+ fetch: getAiTransportHost().buildModelFetch(model)
3381
+ });
3382
+ }
3383
+ function buildParams(model, context, options) {
3384
+ const messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS, { replayResponsesItemIds: options?.replayResponsesItemIds ?? false });
3385
+ const cacheRetention = resolveCacheRetention$1(options?.cacheRetention);
3386
+ const compat = getCompat(model);
3387
+ const params = {
3388
+ model: model.id,
3389
+ input: messages,
3390
+ stream: true,
3391
+ prompt_cache_key: cacheRetention === "none" ? void 0 : clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId),
3392
+ prompt_cache_retention: getPromptCacheRetention(compat, cacheRetention),
3393
+ store: false
3394
+ };
3395
+ if (options?.maxTokens) params.max_output_tokens = options?.maxTokens;
3396
+ if (options?.temperature !== void 0 && supportsOpenAITemperature(model)) params.temperature = options?.temperature;
3397
+ if (options?.serviceTier !== void 0) params.service_tier = options.serviceTier;
3398
+ applyCommonResponsesParams(params, model, context, options, { setDefaultReasoningOff: model.provider !== "github-copilot" });
3399
+ return params;
3400
+ }
3401
+ function getServiceTierCostMultiplier(model, serviceTier) {
3402
+ switch (serviceTier) {
3403
+ case "flex": return .5;
3404
+ case "priority": return model.id === "gpt-5.5" ? 2.5 : 2;
3405
+ default: return 1;
3406
+ }
3407
+ }
3408
+ function applyServiceTierPricing(usage, serviceTier, model) {
3409
+ const multiplier = getServiceTierCostMultiplier(model, serviceTier);
3410
+ if (multiplier === 1) return;
3411
+ usage.cost.input *= multiplier;
3412
+ usage.cost.output *= multiplier;
3413
+ usage.cost.cacheRead *= multiplier;
3414
+ usage.cost.cacheWrite *= multiplier;
3415
+ usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
2390
3416
  }
2391
3417
  //#endregion
2392
- export { isOpenAIGpt54MiniModel as A, resolveUnsupportedToolSchemaKeywords as B, OPENAI_RESPONSES_OUTPUT_TEXT_CONTENT_PART_TYPE as C, isResponsesTextContentPartType as D, isAzureResponsesTextDeltaEventType as E, resolveOpenAISupportedReasoningEfforts as F, uniqueStrings as G, stripUnsupportedSchemaKeywords as H, supportsOpenAIReasoningEffort as I, supportsOpenAITemperature as L, isOpenAIGpt56Model as M, normalizeOpenAIReasoningEffort as N, isResponsesTextDeltaEventType as O, resolveOpenAIReasoningEffortForModel as P, extractToolSchemaModelCompat as R, AZURE_RESPONSES_TEXT_DELTA_EVENT_TYPE as S, isAzureResponsesTextDeltaEvent as T, GEMINI_UNSUPPORTED_SCHEMA_KEYWORDS as U, shouldOmitEmptyArrayItems as V, cleanSchemaForGemini as W, readResponsesToolCallItemIdentity as _, resolveResponsesReasoningEffort as a, resolveResponsesTerminalStopReason as b, clearOpenAIToolSchemaCacheForTest as c, normalizeOpenAIStrictToolParameters as d, normalizeStrictOpenAIJsonSchema as f, createResponsesToolCallTracker as g, normalizeOpenAIStrictCompatSchema as h, processResponsesStream as i, isOpenAIGpt55Model as j, resolveResponsesMessageSnapshotCollapse as k, findOpenAIStrictToolProjectionDiagnostics as l, findOpenAIStrictSchemaViolations as m, convertResponsesMessages as n, runResponsesStreamLifecycle as o, resolveOpenAIProjectedToolsStrictToolFlag as p, createResponsesAssistantOutput as r, convertResponsesToolPayload as s, applyCommonResponsesParams as t, isStrictOpenAIJsonSchemaCompatible as u, mapResponsesTerminalUsage as v, OPENAI_RESPONSES_OUTPUT_TEXT_DELTA_EVENT_TYPE as w, AZURE_RESPONSES_TEXT_CONTENT_PART_TYPE as x, readResponsesReasoningTokens as y, normalizeToolParameterSchema as z };
3418
+ export { createResponsesToolCallTracker as $, resolveAzureOpenAIApiVersion as A, emitModelTransportDebug as At, stringifyRedactedPayload as B, observeResponsesStream as C, normalizeToolParameterSchema as Ct, createResponsesStreamWithEncryptedContentRetry as D, GEMINI_UNSUPPORTED_SCHEMA_KEYWORDS as Dt, convertResponsesMessages$1 as E, stripUnsupportedSchemaKeywords as Et, buildResponsesFailedNoDetailsObservation as F, GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP as G, summarizeResponsesFailedNoDetailsObservation as H, logResponsesFailedNoDetails as I, resolveCacheRetention as J, createModelStreamCooperativeScheduler as K, normalizeResponsesFailedEvent as L, tagOpenAIResponsesReasoningReplayItem as M, resolveModelSseDebugMode as Mt, resolveReplayableResponsesMessageId$1 as N, isInvalidEncryptedContentError as O, cleanSchemaForGemini as Ot, applyServiceTierPricing$1 as P, OPENAI_CODEX_RESPONSES_DEFAULT_INSTRUCTIONS as Q, safeDebugValue as R, resolveResponsesTerminalStopReason as S, extractToolSchemaModelCompat as St, buildResponsesInputMessage as T, shouldOmitEmptyArrayItems as Tt, summarizeResponsesPayload as U, summarizeOpenAITransportError as V, summarizeResponsesTools as W, throwIfModelStreamAborted as X, resolvePromptCacheKey as Y, AZURE_RESPONSES_FIRST_EVENT_TIMEOUT_MS as Z, normalizeOpenAIStrictCompatSchema as _, supportsOpenAITemperature as _t, convertResponsesMessages as a, isAzureResponsesTextDeltaEvent as at, mapResponsesTerminalUsage as b, parseAzureDeploymentNameMap as bt, runResponsesStreamLifecycle as c, isResponsesTextDeltaEventType as ct, findOpenAIStrictToolProjectionDiagnostics as d, isOpenAIGpt55Model as dt, readResponsesToolCallItemIdentity as et, isStrictOpenAIJsonSchemaCompatible as f, isOpenAIGpt56Model as ft, findOpenAIStrictSchemaViolations as g, supportsOpenAIReasoningEffort as gt, resolveOpenAIProjectedToolsStrictToolFlag as h, resolveOpenAISupportedReasoningEfforts as ht, applyCommonResponsesParams as i, OPENAI_RESPONSES_OUTPUT_TEXT_DELTA_EVENT_TYPE as it, stripResponsesRequestEncryptedContent as j, resolveModelPayloadDebugMode as jt, prepareOpenAIResponsesReasoningItemForReplay as k, uniqueStrings as kt, convertResponsesToolPayload as l, resolveResponsesMessageSnapshotCollapse as lt, normalizeStrictOpenAIJsonSchema as m, resolveOpenAIReasoningEffortForModel as mt, streamOpenAIResponses as n, AZURE_RESPONSES_TEXT_DELTA_EVENT_TYPE as nt, createResponsesAssistantOutput as o, isAzureResponsesTextDeltaEventType as ot, normalizeOpenAIStrictToolParameters as p, normalizeOpenAIReasoningEffort as pt, log as q, streamSimpleOpenAIResponses as r, OPENAI_RESPONSES_OUTPUT_TEXT_CONTENT_PART_TYPE as rt, resolveResponsesReasoningEffort as s, isResponsesTextContentPartType as st, openai_responses_exports as t, AZURE_RESPONSES_TEXT_CONTENT_PART_TYPE as tt, clearOpenAIToolSchemaCacheForTest as u, isOpenAIGpt54MiniModel as ut, ResponsesStreamFailure as v, isOpenAICompatibleAzureResponsesBaseUrl as vt, buildOpenAIResponsesReasoningReplayMetadata as w, resolveUnsupportedToolSchemaKeywords as wt, readResponsesReasoningTokens as x, resolveAzureDeploymentNameFromMap as xt, processResponsesStream as y, isTraditionalAzureOpenAIHost as yt, stringifyRedactedEvent as z };