@oh-my-pi/pi-ai 18.2.7 → 18.2.9

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 (133) hide show
  1. package/CHANGELOG.md +49 -18
  2. package/dist/types/auth-gateway/dispatch.d.ts +80 -0
  3. package/dist/types/auth-gateway/http.d.ts +5 -6
  4. package/dist/types/auth-gateway/index.d.ts +1 -0
  5. package/dist/types/auth-gateway/routes/embeddings.d.ts +3 -0
  6. package/dist/types/auth-gateway/routes/images.d.ts +3 -0
  7. package/dist/types/auth-gateway/routes/rerank.d.ts +3 -0
  8. package/dist/types/auth-gateway/routes/speech.d.ts +2 -0
  9. package/dist/types/auth-gateway/routes/systemone.d.ts +2 -0
  10. package/dist/types/auth-gateway/routes/transcriptions.d.ts +3 -0
  11. package/dist/types/auth-gateway/routes/video.d.ts +7 -0
  12. package/dist/types/auth-gateway/server.d.ts +10 -16
  13. package/dist/types/auth-gateway/types.d.ts +5 -0
  14. package/dist/types/auth-storage.d.ts +35 -32
  15. package/dist/types/embeddings/index.d.ts +7 -0
  16. package/dist/types/embeddings/openai-embeddings.d.ts +15 -0
  17. package/dist/types/embeddings/types.d.ts +15 -0
  18. package/dist/types/images/google-antigravity.d.ts +9 -0
  19. package/dist/types/images/google-generative-ai.d.ts +3 -0
  20. package/dist/types/images/index.d.ts +14 -0
  21. package/dist/types/images/openai-hosted.d.ts +3 -0
  22. package/dist/types/images/openai-images.d.ts +5 -0
  23. package/dist/types/images/openrouter-images.d.ts +3 -0
  24. package/dist/types/images/shared.d.ts +34 -0
  25. package/dist/types/images/types.d.ts +31 -0
  26. package/dist/types/index.d.ts +8 -1
  27. package/dist/types/judgment/typesafe.d.ts +2 -0
  28. package/dist/types/providers/amazon-bedrock.d.ts +7 -0
  29. package/dist/types/providers/claude-code-fingerprint.d.ts +22 -3
  30. package/dist/types/providers/embeddings-server.d.ts +32 -0
  31. package/dist/types/providers/google-gemini-cli.d.ts +0 -2
  32. package/dist/types/providers/images-server.d.ts +22 -0
  33. package/dist/types/providers/openai-chat-server-schema.d.ts +2 -2
  34. package/dist/types/providers/rerank-server.d.ts +35 -0
  35. package/dist/types/providers/speech-server.d.ts +8 -0
  36. package/dist/types/providers/systemone-server.d.ts +26 -0
  37. package/dist/types/providers/transcriptions-server.d.ts +32 -0
  38. package/dist/types/providers/video-server.d.ts +39 -0
  39. package/dist/types/rerank/index.d.ts +7 -0
  40. package/dist/types/rerank/openrouter-rerank.d.ts +15 -0
  41. package/dist/types/rerank/types.d.ts +17 -0
  42. package/dist/types/speech/index.d.ts +13 -0
  43. package/dist/types/speech/openai-speech.d.ts +3 -0
  44. package/dist/types/speech/transport.d.ts +7 -0
  45. package/dist/types/speech/types.d.ts +24 -0
  46. package/dist/types/speech/xai-tts.d.ts +7 -0
  47. package/dist/types/transcription/index.d.ts +7 -0
  48. package/dist/types/transcription/openai-transcriptions.d.ts +15 -0
  49. package/dist/types/transcription/types.d.ts +41 -0
  50. package/dist/types/usage/claude-api.d.ts +22 -0
  51. package/dist/types/usage/claude-reset.d.ts +44 -0
  52. package/dist/types/usage.d.ts +111 -5
  53. package/dist/types/utils/schema/json-schema-validator.d.ts +5 -2
  54. package/dist/types/utils/tool-call-loop-guard.d.ts +1 -1
  55. package/dist/types/video/index.d.ts +11 -0
  56. package/dist/types/video/openrouter-video.d.ts +19 -0
  57. package/dist/types/video/types.d.ts +62 -0
  58. package/package.json +30 -6
  59. package/src/auth/sqlite-credential-store.ts +44 -1
  60. package/src/auth-broker/remote-store.ts +6 -6
  61. package/src/auth-broker/wire-schemas.ts +14 -0
  62. package/src/auth-gateway/dispatch.ts +273 -0
  63. package/src/auth-gateway/http.ts +6 -7
  64. package/src/auth-gateway/index.ts +1 -0
  65. package/src/auth-gateway/routes/embeddings.ts +98 -0
  66. package/src/auth-gateway/routes/images.ts +131 -0
  67. package/src/auth-gateway/routes/rerank.ts +87 -0
  68. package/src/auth-gateway/routes/speech.ts +101 -0
  69. package/src/auth-gateway/routes/systemone.ts +116 -0
  70. package/src/auth-gateway/routes/transcriptions.ts +98 -0
  71. package/src/auth-gateway/routes/video.ts +243 -0
  72. package/src/auth-gateway/server.ts +127 -260
  73. package/src/auth-gateway/types.ts +5 -0
  74. package/src/auth-storage.ts +263 -140
  75. package/src/embeddings/index.ts +17 -0
  76. package/src/embeddings/openai-embeddings.ts +141 -0
  77. package/src/embeddings/types.ts +14 -0
  78. package/src/error/flags.ts +10 -0
  79. package/src/error/rate-limit.ts +1 -1
  80. package/src/images/google-antigravity.ts +180 -0
  81. package/src/images/google-generative-ai.ts +92 -0
  82. package/src/images/index.ts +59 -0
  83. package/src/images/openai-hosted.ts +185 -0
  84. package/src/images/openai-images.ts +110 -0
  85. package/src/images/openrouter-images.ts +33 -0
  86. package/src/images/shared.ts +193 -0
  87. package/src/images/types.ts +36 -0
  88. package/src/index.ts +8 -1
  89. package/src/judgment/typesafe.ts +5 -0
  90. package/src/providers/amazon-bedrock.ts +55 -5
  91. package/src/providers/anthropic.ts +45 -11
  92. package/src/providers/aws-credentials.ts +124 -11
  93. package/src/providers/claude-code-fingerprint.ts +55 -3
  94. package/src/providers/embeddings-server.ts +151 -0
  95. package/src/providers/gitlab-duo.ts +20 -4
  96. package/src/providers/google-gemini-cli.ts +0 -8
  97. package/src/providers/google-shared.ts +1 -18
  98. package/src/providers/images-server.ts +159 -0
  99. package/src/providers/openai-chat-server-schema.ts +1 -1
  100. package/src/providers/openai-chat-server.ts +3 -1
  101. package/src/providers/openai-codex-responses.ts +27 -5
  102. package/src/providers/openai-completions.ts +122 -19
  103. package/src/providers/pi-native-server.ts +1 -0
  104. package/src/providers/rerank-server.ts +166 -0
  105. package/src/providers/speech-server.ts +53 -0
  106. package/src/providers/systemone-server.ts +73 -0
  107. package/src/providers/transcriptions-server.ts +243 -0
  108. package/src/providers/video-server.ts +286 -0
  109. package/src/registry/oauth/anthropic.ts +2 -3
  110. package/src/rerank/index.ts +13 -0
  111. package/src/rerank/openrouter-rerank.ts +136 -0
  112. package/src/rerank/types.ts +20 -0
  113. package/src/speech/index.ts +35 -0
  114. package/src/speech/openai-speech.ts +26 -0
  115. package/src/speech/transport.ts +66 -0
  116. package/src/speech/types.ts +37 -0
  117. package/src/speech/xai-tts.ts +41 -0
  118. package/src/stream.ts +13 -4
  119. package/src/transcription/index.ts +17 -0
  120. package/src/transcription/openai-transcriptions.ts +133 -0
  121. package/src/transcription/types.ts +46 -0
  122. package/src/usage/alibaba-token-plan.ts +7 -1
  123. package/src/usage/claude-api.ts +66 -0
  124. package/src/usage/claude-reset.ts +638 -0
  125. package/src/usage/claude.ts +37 -59
  126. package/src/usage/kimi.ts +32 -1
  127. package/src/usage.ts +52 -5
  128. package/src/utils/schema/json-schema-validator.ts +23 -10
  129. package/src/utils/tool-call-loop-guard.ts +2 -2
  130. package/src/utils/validation.ts +145 -50
  131. package/src/video/index.ts +34 -0
  132. package/src/video/openrouter-video.ts +210 -0
  133. package/src/video/types.ts +72 -0
@@ -420,9 +420,7 @@ interface CloudCodeAssistRequest {
420
420
  temperature?: number;
421
421
  topP?: number;
422
422
  topK?: number;
423
- minP?: number;
424
423
  presencePenalty?: number;
425
- repetitionPenalty?: number;
426
424
  thinkingConfig?: ThinkingConfig;
427
425
  };
428
426
  tools?: { functionDeclarations: Record<string, unknown>[] }[] | undefined;
@@ -1275,15 +1273,9 @@ export function buildRequest(
1275
1273
  if (options.topK !== undefined) {
1276
1274
  generationConfig.topK = options.topK;
1277
1275
  }
1278
- if (options.minP !== undefined) {
1279
- generationConfig.minP = options.minP;
1280
- }
1281
1276
  if (options.presencePenalty !== undefined) {
1282
1277
  generationConfig.presencePenalty = options.presencePenalty;
1283
1278
  }
1284
- if (options.repetitionPenalty !== undefined) {
1285
- generationConfig.repetitionPenalty = options.repetitionPenalty;
1286
- }
1287
1279
 
1288
1280
  // Thinking config
1289
1281
  if (options.thinking?.enabled && model.reasoning) {
@@ -780,18 +780,6 @@ export async function consumeGoogleStream<T extends GoogleApiType>(args: {
780
780
  }
781
781
  }
782
782
 
783
- /**
784
- * Generation/sampling fields that map directly onto Gemini's `GenerateContentConfig`.
785
- * Excludes any provider-specific extensions (`topP`/`topK`/etc are all forwarded as-is).
786
- */
787
- interface GoogleGenerationConfig extends GenerateContentConfig {
788
- topP?: number;
789
- topK?: number;
790
- minP?: number;
791
- presencePenalty?: number;
792
- repetitionPenalty?: number;
793
- }
794
-
795
783
  /**
796
784
  * Build the `GenerateContentParameters` payload for the public Gemini API and Vertex AI.
797
785
  * Both surfaces accept the same `GenerateContentConfig` shape — every numeric/string knob,
@@ -809,14 +797,12 @@ export function buildGoogleGenerateContentParams<T extends "google-generative-ai
809
797
  const systemPrompts = normalizeSystemPrompts(context.systemPrompt);
810
798
  const contents = convertMessages(model, context);
811
799
 
812
- const generationConfig: GoogleGenerationConfig = {};
800
+ const generationConfig: GenerateContentConfig = {};
813
801
  if (options.temperature !== undefined) generationConfig.temperature = options.temperature;
814
802
  if (options.maxTokens !== undefined) generationConfig.maxOutputTokens = options.maxTokens;
815
803
  if (options.topP !== undefined) generationConfig.topP = options.topP;
816
804
  if (options.topK !== undefined) generationConfig.topK = options.topK;
817
- if (options.minP !== undefined) generationConfig.minP = options.minP;
818
805
  if (options.presencePenalty !== undefined) generationConfig.presencePenalty = options.presencePenalty;
819
- if (options.repetitionPenalty !== undefined) generationConfig.repetitionPenalty = options.repetitionPenalty;
820
806
 
821
807
  const config: GenerateContentConfig = {
822
808
  ...(Object.keys(generationConfig).length > 0 && generationConfig),
@@ -1111,9 +1097,6 @@ function paramsToWireBody(params: GenerateContentParameters): Record<string, unk
1111
1097
  if (config.responseJsonSchema !== undefined) gen.responseJsonSchema = config.responseJsonSchema;
1112
1098
  if (config.responseModalities !== undefined) gen.responseModalities = config.responseModalities;
1113
1099
  if (config.thinkingConfig !== undefined) gen.thinkingConfig = config.thinkingConfig;
1114
- const generationConfig = config as unknown as { minP?: number; repetitionPenalty?: number };
1115
- if (generationConfig.minP !== undefined) gen.minP = generationConfig.minP;
1116
- if (generationConfig.repetitionPenalty !== undefined) gen.repetitionPenalty = generationConfig.repetitionPenalty;
1117
1100
  if (Object.keys(gen).length > 0) body.generationConfig = gen;
1118
1101
  return body;
1119
1102
  }
@@ -0,0 +1,159 @@
1
+ import { type } from "@oh-my-pi/omptype";
2
+ import * as AIError from "../error";
3
+ import type { ImageGenerationRequest, ImageGenerationResult } from "../images/types";
4
+
5
+ const imageJsonRequestSchema = type({
6
+ model: "string > 0",
7
+ prompt: "string > 0",
8
+ "n?": "unknown",
9
+ "size?": "unknown",
10
+ "image_size?": "unknown",
11
+ "aspect_ratio?": "unknown",
12
+ "response_format?": "unknown",
13
+ "stream?": "unknown",
14
+ "input_references?": "unknown",
15
+ "images?": "unknown",
16
+ "image?": "unknown",
17
+ });
18
+
19
+ export type ImageRequestKind = "generations" | "edits";
20
+
21
+ export interface ImagesParsedRequest {
22
+ modelId: string;
23
+ request: ImageGenerationRequest;
24
+ }
25
+
26
+ export interface ImagesResponseBody {
27
+ created: number;
28
+ data: Array<{ b64_json: string; revised_prompt?: string }>;
29
+ usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; cost: number };
30
+ }
31
+
32
+ function validation(message: string): never {
33
+ throw new AIError.ValidationError(`images: ${message}`);
34
+ }
35
+
36
+ function optionalString(value: unknown, field: string): string | undefined {
37
+ if (value === undefined || value === null) return undefined;
38
+ if (typeof value !== "string" || value.length === 0) validation(`${field} must be a non-empty string`);
39
+ return value;
40
+ }
41
+
42
+ function optionalCount(value: unknown): number | undefined {
43
+ if (value === undefined || value === null) return undefined;
44
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1) validation("n must be a positive integer");
45
+ return value;
46
+ }
47
+
48
+ function decodeInputReference(value: unknown): { data: string; mimeType: string } {
49
+ let raw: unknown = value;
50
+ if (value !== null && typeof value === "object") {
51
+ raw = "url" in value ? value.url : undefined;
52
+ const imageUrl = "image_url" in value ? value.image_url : undefined;
53
+ if (raw === undefined && imageUrl !== null && typeof imageUrl === "object" && "url" in imageUrl) {
54
+ raw = imageUrl.url;
55
+ }
56
+ }
57
+ if (typeof raw !== "string" || raw.length === 0) validation("input image must be base64 or a data URL");
58
+ const match = raw.match(/^data:([^;]+);base64,(.+)$/s);
59
+ if (match) return { data: match[2] ?? "", mimeType: match[1] ?? "image/png" };
60
+ if (/^[A-Za-z0-9+/]+={0,2}$/.test(raw)) return { data: raw, mimeType: "image/png" };
61
+ validation("input image URLs are not supported; send a data URL or base64 bytes");
62
+ }
63
+
64
+ function parseJson(body: unknown, kind: ImageRequestKind): ImagesParsedRequest {
65
+ const parsed = imageJsonRequestSchema(body);
66
+ if (parsed instanceof type.errors) validation(parsed.summary);
67
+ if (parsed.stream === true) validation("streaming image responses are not supported");
68
+ if (parsed.response_format !== undefined && parsed.response_format !== "b64_json") {
69
+ validation('response_format must be "b64_json"; URL responses are not supported');
70
+ }
71
+ const input = parsed.input_references ?? parsed.images ?? parsed.image;
72
+ const values = input === undefined ? [] : Array.isArray(input) ? input : [input];
73
+ if (kind === "edits" && values.length === 0) validation("image edit requires at least one input image");
74
+ const aspectRatio = optionalString(parsed.aspect_ratio, "aspect_ratio");
75
+ const imageSize = optionalString(
76
+ parsed.image_size ?? parsed.size,
77
+ parsed.image_size === undefined ? "size" : "image_size",
78
+ );
79
+ const count = optionalCount(parsed.n);
80
+ return {
81
+ modelId: parsed.model,
82
+ request: {
83
+ prompt: parsed.prompt,
84
+ ...(values.length > 0 ? { inputImages: values.map(decodeInputReference) } : {}),
85
+ ...(aspectRatio ? { aspectRatio } : {}),
86
+ ...(imageSize ? { imageSize } : {}),
87
+ ...(count ? { count } : {}),
88
+ },
89
+ };
90
+ }
91
+
92
+ async function parseMultipart(form: FormData): Promise<ImagesParsedRequest> {
93
+ const model = form.get("model");
94
+ const prompt = form.get("prompt");
95
+ if (typeof model !== "string" || model.length === 0) validation("model must be a non-empty string");
96
+ if (typeof prompt !== "string" || prompt.length === 0) validation("prompt must be a non-empty string");
97
+ const responseFormat = form.get("response_format");
98
+ if (responseFormat !== null && responseFormat !== "b64_json") {
99
+ validation('response_format must be "b64_json"; URL responses are not supported');
100
+ }
101
+ const stream = form.get("stream");
102
+ if (stream === "true") validation("streaming image responses are not supported");
103
+ const entries = [...form.getAll("image"), ...form.getAll("image[]")];
104
+ if (entries.length === 0) validation("image edit requires at least one image file");
105
+ const inputImages: Array<{ data: string; mimeType: string }> = [];
106
+ for (const entry of entries) {
107
+ if (typeof entry === "string") validation("image must be a file part");
108
+ const bytes = new Uint8Array(await entry.arrayBuffer());
109
+ inputImages.push({ data: bytes.toBase64(), mimeType: entry.type || "image/png" });
110
+ }
111
+ const nValue = form.get("n");
112
+ let count: number | undefined;
113
+ if (nValue !== null) {
114
+ if (typeof nValue !== "string" || !/^\d+$/.test(nValue)) validation("n must be a positive integer");
115
+ count = optionalCount(Number(nValue));
116
+ }
117
+ const size = form.get("size");
118
+ if (size !== null && typeof size !== "string") validation("size must be a string");
119
+ return {
120
+ modelId: model,
121
+ request: {
122
+ prompt,
123
+ inputImages,
124
+ ...(size ? { imageSize: size } : {}),
125
+ ...(count ? { count } : {}),
126
+ },
127
+ };
128
+ }
129
+
130
+ export async function parseRequest(body: unknown | FormData, kind: ImageRequestKind): Promise<ImagesParsedRequest> {
131
+ if (body instanceof FormData) {
132
+ if (kind !== "edits") validation("multipart requests are supported only for image edits");
133
+ return parseMultipart(body);
134
+ }
135
+ return parseJson(body, kind);
136
+ }
137
+
138
+ export function encodeResponse(result: ImageGenerationResult, _requestedModelId: string): ImagesResponseBody {
139
+ return {
140
+ created: Math.floor(Date.now() / 1000),
141
+ data: result.images.map(image => ({ b64_json: image.data })),
142
+ usage: {
143
+ prompt_tokens: result.usage.input,
144
+ completion_tokens: result.usage.output,
145
+ total_tokens: result.usage.totalTokens,
146
+ cost: result.usage.cost.total,
147
+ },
148
+ };
149
+ }
150
+
151
+ export function formatError(status: number, errorType: string, message: string): Response {
152
+ return new Response(JSON.stringify({ error: { code: status, type: errorType, message } }), {
153
+ status,
154
+ headers: {
155
+ "Content-Type": "application/json; charset=utf-8",
156
+ "Cache-Control": "no-store",
157
+ },
158
+ });
159
+ }
@@ -210,7 +210,7 @@ export const openaiChatRequestSchema = type({
210
210
  "frequency_penalty?": "number",
211
211
  "logit_bias?": type({ "[string]": "number" }),
212
212
  "user?": "string",
213
- "reasoning_effort?": "'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'",
213
+ "reasoning_effort?": "'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'",
214
214
  "parallel_tool_calls?": "boolean",
215
215
  "service_tier?": "'auto' | 'default' | 'flex' | 'scale' | 'priority'",
216
216
  "metadata?": type({ "[string]": "unknown" }),
@@ -200,7 +200,9 @@ export function parseRequest(body: unknown, headers?: Headers): ParsedRequest {
200
200
  if (data.user !== undefined) options.user = data.user;
201
201
  if (data.response_format !== undefined) options.responseFormat = data.response_format;
202
202
  if (data.parallel_tool_calls !== undefined) options.parallelToolCalls = data.parallel_tool_calls;
203
- if (data.reasoning_effort !== undefined && isReasoningEffort(data.reasoning_effort)) {
203
+ if (data.reasoning_effort === "none") {
204
+ options.forceReasoningOff = true;
205
+ } else if (data.reasoning_effort !== undefined && isReasoningEffort(data.reasoning_effort)) {
204
206
  options.reasoning = data.reasoning_effort;
205
207
  }
206
208
  if (data.service_tier !== undefined && isServiceTier(data.service_tier)) {
@@ -4290,11 +4290,22 @@ async function openCodexSseEventStream(
4290
4290
  // an internal timeout stays retryable while an explicit abort fails fast.
4291
4291
  let clearPreResponseTimeout: (() => void) | undefined;
4292
4292
  const fetchAttempt: FetchImpl = async (input, init) => {
4293
+ let response: Response | undefined;
4293
4294
  try {
4294
- return await (fetchOverride ?? fetch)(input, init);
4295
+ response = await (fetchOverride ?? fetch)(input, init);
4296
+ return response;
4295
4297
  } finally {
4296
- clearPreResponseTimeout?.();
4297
- clearPreResponseTimeout = undefined;
4298
+ // A successful streaming body is governed by the iterator-level idle
4299
+ // watchdog, so disarm the pre-response guard the instant headers arrive.
4300
+ // Keep it armed for a non-2xx response: the error body is still
4301
+ // consumed under this deadline — by fetchWithRetry's
4302
+ // retry-status inspection and by CodexApiError.fromResponse — otherwise a
4303
+ // server that sends headers then stalls the body hangs the turn past every
4304
+ // configured first-event/idle deadline (issue #12664).
4305
+ if (!response || response.ok) {
4306
+ clearPreResponseTimeout?.();
4307
+ clearPreResponseTimeout = undefined;
4308
+ }
4298
4309
  }
4299
4310
  };
4300
4311
  const bodyJson = JSON.stringify(body);
@@ -4318,6 +4329,9 @@ async function openCodexSseEventStream(
4318
4329
  body: requestBody,
4319
4330
  signal,
4320
4331
  prepareInit: () => {
4332
+ // A retried non-2xx attempt leaves its guard armed for the retry-status
4333
+ // body read; disarm it before arming the next attempt's guard.
4334
+ clearPreResponseTimeout?.();
4321
4335
  const watchdog = armPreResponseTimeout(signal, firstEventTimeoutMs);
4322
4336
  clearPreResponseTimeout = watchdog.clear;
4323
4337
  return { signal: watchdog.signal };
@@ -4342,8 +4356,9 @@ async function openCodexSseEventStream(
4342
4356
  });
4343
4357
  response = await send(bodyJson);
4344
4358
  }
4345
- } finally {
4359
+ } catch (error) {
4346
4360
  clearPreResponseTimeout?.();
4361
+ throw error;
4347
4362
  }
4348
4363
  CODEX_DEBUG &&
4349
4364
  logger.debug("[codex] codex response", {
@@ -4354,7 +4369,14 @@ async function openCodexSseEventStream(
4354
4369
  cfRay: response.headers.get("cf-ray") || null,
4355
4370
  });
4356
4371
  if (!response.ok) {
4357
- throw await CodexApiError.fromResponse(response);
4372
+ // The pre-response guard is still armed for a non-2xx response; keep it live
4373
+ // across the error-body read so a stalled body is bounded, then disarm once
4374
+ // the read settles (issue #12664).
4375
+ try {
4376
+ throw await CodexApiError.fromResponse(response);
4377
+ } finally {
4378
+ clearPreResponseTimeout?.();
4379
+ }
4358
4380
  }
4359
4381
  updateCodexSessionMetadataFromHeaders(turnState, state, response.headers);
4360
4382
  if (!response.body) {
@@ -1,3 +1,4 @@
1
+ import { resolveModelPolicy } from "@oh-my-pi/pi-catalog/compat/resolve";
1
2
  import type { Effort } from "@oh-my-pi/pi-catalog/effort";
2
3
  import { resolveWireModelId } from "@oh-my-pi/pi-catalog/model-thinking";
3
4
  import { calculateCost } from "@oh-my-pi/pi-catalog/models";
@@ -133,6 +134,13 @@ type OpenAICompletionsDeltaWithReasoningDetails = ChatCompletionChunk.Choice["de
133
134
  reasoning_details?: unknown;
134
135
  };
135
136
 
137
+ type GeminiMessageThoughtSignatureField = "thinking_signature" | "thought_signature";
138
+
139
+ type GeminiMessageThoughtSignature = {
140
+ field: GeminiMessageThoughtSignatureField;
141
+ signature: string;
142
+ };
143
+
136
144
  type GeminiThoughtSignatureNamespace = "google" | "vertex";
137
145
 
138
146
  type GeminiThoughtSignatureExtraContent = Partial<
@@ -145,6 +153,11 @@ type OpenAICompletionsFunctionToolCall = ChatCompletionMessageFunctionToolCall &
145
153
 
146
154
  const GEMINI_THOUGHT_SIGNATURE_NAMESPACES: readonly GeminiThoughtSignatureNamespace[] = ["google", "vertex"];
147
155
 
156
+ const GEMINI_MESSAGE_THOUGHT_SIGNATURE_FIELDS: readonly GeminiMessageThoughtSignatureField[] = [
157
+ "thinking_signature",
158
+ "thought_signature",
159
+ ];
160
+
148
161
  function getGeminiThoughtSignatureExtraContent(value: unknown): GeminiThoughtSignatureExtraContent | undefined {
149
162
  if (typeof value !== "object" || value === null) return undefined;
150
163
  for (const namespace of GEMINI_THOUGHT_SIGNATURE_NAMESPACES) {
@@ -159,20 +172,66 @@ function getGeminiThoughtSignatureExtraContent(value: unknown): GeminiThoughtSig
159
172
  return undefined;
160
173
  }
161
174
 
162
- function parseGeminiThoughtSignatureExtraContent(
163
- thoughtSignature: string | undefined,
164
- ): GeminiThoughtSignatureExtraContent | undefined {
175
+ function getGeminiMessageThoughtSignature(value: unknown): GeminiMessageThoughtSignature | undefined {
176
+ if (typeof value !== "object" || value === null) return undefined;
177
+ for (const field of GEMINI_MESSAGE_THOUGHT_SIGNATURE_FIELDS) {
178
+ const signature = Reflect.get(value, field);
179
+ if (typeof signature === "string" && signature.length > 0) return { field, signature };
180
+ }
181
+ return undefined;
182
+ }
183
+
184
+ function parseStoredThoughtSignature(thoughtSignature: string | undefined): unknown {
165
185
  if (!thoughtSignature) return undefined;
166
186
  try {
167
- const parsed: unknown = JSON.parse(thoughtSignature);
168
- return getGeminiThoughtSignatureExtraContent(parsed);
187
+ return JSON.parse(thoughtSignature);
169
188
  } catch {
170
189
  return undefined;
171
190
  }
172
191
  }
173
192
 
193
+ // A single tool-call turn on an OpenAI-compatible Gemini wire can carry two
194
+ // independent signatures: a per-call one (`extra_content.google|vertex` or an
195
+ // encrypted `reasoning_details` entry) and a message-level `thinking_signature`
196
+ // / `thought_signature`. Both are stashed together on the originating tool
197
+ // call's `thoughtSignature` so persistence and replay preserve each field.
198
+ // `perCall` holds the raw extra_content object or reasoning detail; `message`
199
+ // holds the message-level signature in its wire shape.
200
+ type StoredGeminiSignature = {
201
+ perCall?: unknown;
202
+ message?: Partial<Record<GeminiMessageThoughtSignatureField, string>>;
203
+ };
204
+
205
+ // Reads the stored envelope, also accepting the legacy raw shapes emitted before
206
+ // the envelope existed (a bare extra_content object, reasoning detail, or
207
+ // message-level signature) so persisted history keeps replaying.
208
+ function normalizeStoredGeminiSignature(value: unknown): StoredGeminiSignature | undefined {
209
+ if (typeof value !== "object" || value === null) return undefined;
210
+ const perCall = Reflect.get(value, "perCall");
211
+ const envelopeMessage = getGeminiMessageThoughtSignature(Reflect.get(value, "message"));
212
+ if (perCall !== undefined || envelopeMessage) {
213
+ const normalized: StoredGeminiSignature = {};
214
+ if (perCall !== undefined) normalized.perCall = perCall;
215
+ if (envelopeMessage) normalized.message = { [envelopeMessage.field]: envelopeMessage.signature };
216
+ return normalized;
217
+ }
218
+ const legacyMessage = getGeminiMessageThoughtSignature(value);
219
+ if (legacyMessage) return { message: { [legacyMessage.field]: legacyMessage.signature } };
220
+ return { perCall: value };
221
+ }
222
+
223
+ // Merges a new per-call or message-level signature into whatever is already
224
+ // stored, so a later message-level signature never clobbers an earlier per-call
225
+ // one (and vice versa).
226
+ function mergeStoredGeminiSignature(existing: string | undefined, update: StoredGeminiSignature): string {
227
+ const merged = normalizeStoredGeminiSignature(parseStoredThoughtSignature(existing)) ?? {};
228
+ if (update.perCall !== undefined) merged.perCall = update.perCall;
229
+ if (update.message) merged.message = update.message;
230
+ return JSON.stringify(merged);
231
+ }
232
+
174
233
  type OpenAICompletionsAssistantMessageParam = ChatCompletionAssistantMessageParam &
175
- Partial<Record<OpenAICompletionsReasoningField, string>> & {
234
+ Partial<Record<OpenAICompletionsReasoningField | GeminiMessageThoughtSignatureField, string>> & {
176
235
  reasoning_details?: unknown[];
177
236
  };
178
237
 
@@ -919,6 +978,7 @@ const streamOpenAICompletionsOnce = (
919
978
  }
920
979
  };
921
980
  let currentBlock: OpenAIStreamBlock | undefined;
981
+ let messageThoughtSignature: GeminiMessageThoughtSignature | undefined;
922
982
  const blockIndex = (block: OpenAIStreamBlock | undefined): number => {
923
983
  if (!block) return Math.max(0, output.content.length - 1);
924
984
  return output.content.indexOf(block);
@@ -1347,7 +1407,11 @@ const streamOpenAICompletionsOnce = (
1347
1407
  if (toolCall.id) block.id = toolCall.id;
1348
1408
  if (incomingName) block.name = incomingName;
1349
1409
  const extraContent = getGeminiThoughtSignatureExtraContent(Reflect.get(toolCall, "extra_content"));
1350
- if (extraContent) block.thoughtSignature = JSON.stringify(extraContent);
1410
+ if (extraContent) {
1411
+ block.thoughtSignature = mergeStoredGeminiSignature(block.thoughtSignature, {
1412
+ perCall: extraContent,
1413
+ });
1414
+ }
1351
1415
  let delta = "";
1352
1416
  // The OpenAI SDK types `function.arguments` as a JSON string, but MiniMax-compatible
1353
1417
  // hosts stream a fully-formed object instead. Model both shapes so the branches below
@@ -1411,11 +1475,29 @@ const streamOpenAICompletionsOnce = (
1411
1475
  b => b.type === "toolCall" && b.id === detailObject.id,
1412
1476
  ) as ToolCall | undefined;
1413
1477
  if (matchingToolCall) {
1414
- matchingToolCall.thoughtSignature = JSON.stringify(detailObject);
1478
+ matchingToolCall.thoughtSignature = mergeStoredGeminiSignature(
1479
+ matchingToolCall.thoughtSignature,
1480
+ { perCall: detailObject },
1481
+ );
1415
1482
  }
1416
1483
  }
1417
1484
  }
1418
1485
  }
1486
+
1487
+ const incomingMessageThoughtSignature = getGeminiMessageThoughtSignature(choice.delta);
1488
+ if (incomingMessageThoughtSignature) messageThoughtSignature = incomingMessageThoughtSignature;
1489
+ if (messageThoughtSignature) {
1490
+ for (const block of output.content) {
1491
+ if (block.type !== "toolCall") continue;
1492
+ block.thoughtSignature = mergeStoredGeminiSignature(block.thoughtSignature, {
1493
+ message: {
1494
+ [messageThoughtSignature.field]: messageThoughtSignature.signature,
1495
+ },
1496
+ });
1497
+ messageThoughtSignature = undefined;
1498
+ break;
1499
+ }
1500
+ }
1419
1501
  }
1420
1502
 
1421
1503
  // If usage arrived on the finish chunk without cache-read fields,
@@ -1542,16 +1624,33 @@ const streamOpenAICompletionsOnce = (
1542
1624
  return stream;
1543
1625
  };
1544
1626
 
1627
+ /**
1628
+ * Custom APIs deliberately have no catalog compat type. Once an extension
1629
+ * explicitly delegates to this streamer, resolve the OpenAI wire policy on a
1630
+ * request-local clone while preserving the custom API id on the original model.
1631
+ */
1632
+ function resolveOpenAICompletionsCompat(model: Model<"openai-completions">): Model<"openai-completions"> {
1633
+ if (model.compat !== undefined) return model;
1634
+ const compat = resolveModelPolicy({
1635
+ ...model,
1636
+ api: "openai-completions",
1637
+ compat: model.compatConfig,
1638
+ }).compat;
1639
+ return { ...model, compat };
1640
+ }
1641
+
1545
1642
  /**
1546
1643
  * Retries benign empty completions and transient provider failures only before
1547
1644
  * assistant output commits the attempt.
1548
1645
  */
1549
- export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (model, context, options) =>
1550
- withReplaySafeStreamRetry(model, context, options, streamOpenAICompletionsOnce, {
1646
+ export const streamOpenAICompletions: StreamFunction<"openai-completions"> = (model, context, options) => {
1647
+ const resolvedModel = resolveOpenAICompletionsCompat(model);
1648
+ return withReplaySafeStreamRetry(resolvedModel, context, options, streamOpenAICompletionsOnce, {
1551
1649
  retryEmptyCompletion: true,
1552
1650
  retryProviderErrors: true,
1553
1651
  maxProviderErrorRetries: 1,
1554
1652
  });
1653
+ };
1555
1654
 
1556
1655
  function createRequestSetup(
1557
1656
  model: Model<"openai-completions">,
@@ -2350,19 +2449,23 @@ export function convertMessages(
2350
2449
  arguments: serializeToolArguments(tc.arguments),
2351
2450
  },
2352
2451
  };
2353
- const extraContent = parseGeminiThoughtSignatureExtraContent(tc.thoughtSignature);
2452
+ const stored = normalizeStoredGeminiSignature(parseStoredThoughtSignature(tc.thoughtSignature));
2453
+ const extraContent = getGeminiThoughtSignatureExtraContent(stored?.perCall);
2354
2454
  if (extraContent) replayedToolCall.extra_content = extraContent;
2355
2455
  return replayedToolCall;
2356
2456
  });
2457
+ for (const toolCall of toolCalls) {
2458
+ const stored = normalizeStoredGeminiSignature(parseStoredThoughtSignature(toolCall.thoughtSignature));
2459
+ const messageSignature = getGeminiMessageThoughtSignature(stored?.message);
2460
+ if (!messageSignature) continue;
2461
+ assistantMsg[messageSignature.field] = messageSignature.signature;
2462
+ break;
2463
+ }
2357
2464
  const reasoningDetails = toolCalls.flatMap(tc => {
2358
- const thoughtSignature = tc.thoughtSignature;
2359
- if (!thoughtSignature) return [];
2360
- try {
2361
- const parsed: unknown = JSON.parse(thoughtSignature);
2362
- return getGeminiThoughtSignatureExtraContent(parsed) ? [] : [parsed];
2363
- } catch {
2364
- return [];
2365
- }
2465
+ const stored = normalizeStoredGeminiSignature(parseStoredThoughtSignature(tc.thoughtSignature));
2466
+ const perCall = stored?.perCall;
2467
+ if (perCall === undefined || getGeminiThoughtSignatureExtraContent(perCall)) return [];
2468
+ return [perCall];
2366
2469
  });
2367
2470
  if (reasoningDetails.length > 0) {
2368
2471
  assistantMsg.reasoning_details = reasoningDetails;
@@ -69,6 +69,7 @@ const ALLOWED_OPTION_KEYS: ReadonlySet<keyof SimpleStreamOptions> = new Set([
69
69
  "streamIdleTimeoutMs",
70
70
  "reasoning",
71
71
  "disableReasoning",
72
+ "forceReasoningOff",
72
73
  "hideThinkingSummary",
73
74
  "thinkingBudgets",
74
75
  "toolChoice",