@router-for-me/pi-cliproxyapi-provider 1.4.25 → 1.4.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -211,6 +211,10 @@ From CPA catalog entry → pi model:
211
211
 
212
212
  Unsupported pi thinking levels are set to `null` so they are hidden in the UI. When available, prices are matched against canonical model entries in `models.dev`; `cost.tiers[].tier.size` becomes pi's `inputTokensAbove`, including thresholds such as `272000`. The legacy `context_over_200k` field is used only when no explicit tiers are present. Ambiguous reseller prices are not selected arbitrarily and fall back to zero. These are catalog/list prices, not a guarantee of CPA's own markup or billing.
213
213
 
214
+ ## GPT Responses prompt caching
215
+
216
+ For `gpt-` model IDs (ignoring case and surrounding whitespace), the provider sends the base prompt as the first `developer` input message instead of top-level `instructions`. Nonblank text is kept verbatim; blank text defaults to `You are a helpful assistant.` Existing input items stay in order, tools and `prompt_cache_key` are unchanged, and other models keep their existing request shape. This applies to HTTP and WebSocket requests, including Pi background streams that use this provider. A changed developer prefix or model sends the full input instead of a WebSocket continuation delta.
217
+
214
218
  ## Cache-preserving thinking level changes
215
219
 
216
220
  Models whose catalog entry sets `supports_reasoning_effort_updates` (GPT-6 on CLIProxyAPI) get pi's cache-preserving mid-session thinking level changes, matching the behavior pi >=1.3.0 ships for the OpenAI provider, with placement following the OpenAI documentation: the effort of the first turn becomes the fixed request-level `reasoning.effort`, and each later change is carried by a `configuration_update` input item placed immediately before the user message it applies to, so switching levels mid-session keeps the prompt prefix — and the upstream prompt cache — intact instead of rewriting it. WebSocket `previous_response_id` continuation also survives level changes. Histories lacking initial recorded levels (for example, sessions started before this support landed) may still need one full-context request while the baseline is being established. The behavior requires a pi >=1.3.0 host (its shared Responses converter); on older hosts the plugin detects the missing support and falls back to the classic request-level effort.
@@ -6,6 +6,7 @@
6
6
  * - chatgpt-account-id header is omitted when account id is unavailable
7
7
  * - provider id(s) are added to CODEX_TOOL_CALL_PROVIDERS for tool-call id handling
8
8
  * - model/message api id uses cliproxyapi-codex-responses
9
+ * - GPT base instructions become the first developer input item
9
10
  *
10
11
  * The patched module is derived at runtime from the installed
11
12
  * @earendil-works/pi-ai openai-codex-responses implementation so we track
@@ -55,14 +56,17 @@ interface TranscriptMessage {
55
56
  [key: string]: unknown;
56
57
  }
57
58
 
58
- function extractContentText(content: unknown): string {
59
- if (typeof content === "string") return content.trim();
59
+ function isGptModelId(id: unknown): boolean {
60
+ return typeof id === "string" && id.trim().toLowerCase().startsWith("gpt-");
61
+ }
62
+
63
+ function extractContentText(content: unknown, preserveWhitespace: boolean): string {
64
+ if (typeof content === "string") return preserveWhitespace ? content : content.trim();
60
65
  if (Array.isArray(content)) {
61
- return content
66
+ const parts = content
62
67
  .filter((b) => b && typeof b === "object" && b.type === "text" && typeof b.text === "string")
63
- .map((b) => b.text.trim())
64
- .filter(Boolean)
65
- .join("\n");
68
+ .map((b) => b.text as string);
69
+ return (preserveWhitespace ? parts : parts.map((text) => text.trim()).filter(Boolean)).join("\n");
66
70
  }
67
71
  return "";
68
72
  }
@@ -75,7 +79,10 @@ function extractContentText(content: unknown): string {
75
79
  * Classic openai-codex-responses expects context.systemPrompt and context.tools, and fails when
76
80
  * encountering system messages inside context.messages during token estimation or payload generation.
77
81
  */
78
- export function adaptTranscriptContext(context: Context | { messages?: unknown[]; [key: string]: unknown }): Context {
82
+ export function adaptTranscriptContext(
83
+ context: Context | { messages?: unknown[]; [key: string]: unknown },
84
+ preserveSystemWhitespace = false,
85
+ ): Context {
79
86
  if (!context || !Array.isArray(context.messages)) {
80
87
  return context as Context;
81
88
  }
@@ -105,7 +112,7 @@ export function adaptTranscriptContext(context: Context | { messages?: unknown[]
105
112
  if (!msg || typeof msg !== "object") continue;
106
113
 
107
114
  if (msg.role === "system") {
108
- const text = extractContentText(msg.content);
115
+ const text = extractContentText(msg.content, preserveSystemWhitespace);
109
116
  if (text) {
110
117
  leadingText = leadingText ? `${leadingText}\n\n${text}` : text;
111
118
  }
@@ -113,8 +120,9 @@ export function adaptTranscriptContext(context: Context | { messages?: unknown[]
113
120
  for (const [name, sectionText] of Object.entries(msg.sections)) {
114
121
  if (sectionText === null || sectionText === undefined) {
115
122
  sections.delete(name);
116
- } else if (typeof sectionText === "string" && sectionText.trim()) {
117
- sections.set(name, sectionText.trim());
123
+ } else if (typeof sectionText === "string") {
124
+ const section = preserveSystemWhitespace ? sectionText : sectionText.trim();
125
+ if (section) sections.set(name, section);
118
126
  }
119
127
  }
120
128
  }
@@ -194,6 +202,52 @@ export function adaptContextForModule(
194
202
  return supportsTranscript ? ensureTranscriptContext(context) : adaptTranscriptContext(context);
195
203
  }
196
204
 
205
+ function matchesInputText(content: unknown, text: string): boolean {
206
+ if (typeof content === "string") return content === text;
207
+ return (
208
+ Array.isArray(content) &&
209
+ content.every((part) => part?.type === "input_text" && typeof part.text === "string") &&
210
+ content.map((part) => part.text).join("") === text
211
+ );
212
+ }
213
+
214
+ function moveGptInstructionsToInput(
215
+ body: Record<string, unknown>,
216
+ builtInstructions: unknown,
217
+ legacyContext?: Context,
218
+ ): Record<string, unknown> {
219
+ if (!isGptModelId(body.model)) return body;
220
+
221
+ const { input, instructions, ...rest } = body;
222
+ if (input !== undefined && typeof input !== "string" && !Array.isArray(input)) return body;
223
+ const messages =
224
+ typeof input === "string" ? [{ role: "user", content: [{ type: "input_text", text: input }] }] : (input ?? []);
225
+ let prompt = instructions === undefined ? builtInstructions : instructions;
226
+ if (legacyContext && (instructions === undefined || instructions === builtInstructions)) {
227
+ // Recover whitespace only for the outgoing GPT prompt, not the classic context or hook replacements.
228
+ const legacyPrompt = adaptTranscriptContext(legacyContext, true).systemPrompt;
229
+ if (legacyPrompt?.trim()) prompt = legacyPrompt;
230
+ }
231
+ const text = typeof prompt === "string" && prompt.trim() ? prompt : "You are a helpful assistant.";
232
+
233
+ // An unrelated leading developer message must not replace the base prompt.
234
+ const first = messages[0];
235
+ if (
236
+ instructions === undefined &&
237
+ first?.role === "developer" &&
238
+ (first.type === undefined || first.type === "message") &&
239
+ (typeof first.content === "string" || Array.isArray(first.content)) &&
240
+ (builtInstructions === undefined || matchesInputText(first.content, text))
241
+ ) {
242
+ return body;
243
+ }
244
+
245
+ return {
246
+ ...rest,
247
+ input: [{ type: "message", role: "developer", content: [{ type: "input_text", text }] }, ...messages],
248
+ };
249
+ }
250
+
197
251
  export function withPriorityServiceTier(payload: unknown): unknown {
198
252
  if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
199
253
  return payload;
@@ -1413,25 +1467,30 @@ export async function loadCliproxyCodexStreams(
1413
1467
  // follows pi's converter layout; reshape payloads to the documented layout
1414
1468
  // in the wrapper instead of inside the patched module.
1415
1469
  const nativeMidConvoEffort = hasNativeMidConvoEffort(originalSource);
1416
- const adaptOptions = (streamOptions?: SimpleStreamOptions): SimpleStreamOptions | undefined => {
1417
- if (!nativeMidConvoEffort) {
1418
- return streamOptions;
1419
- }
1420
- return {
1421
- ...streamOptions,
1422
- onPayload: async (payload, payloadModel) => {
1423
- const nextPayload = await streamOptions?.onPayload?.(payload, payloadModel);
1424
- const effective = (nextPayload === undefined ? payload : nextPayload) as Record<string, unknown>;
1425
- return effective && typeof effective === "object" ? reshapeConfigurationUpdates(effective) : effective;
1426
- },
1427
- };
1428
- };
1470
+ const adaptOptions = (context: Context, streamOptions?: SimpleStreamOptions): SimpleStreamOptions => ({
1471
+ ...streamOptions,
1472
+ onPayload: async (payload, payloadModel) => {
1473
+ const builtInstructions =
1474
+ payload && typeof payload === "object" && "instructions" in payload ? payload.instructions : undefined;
1475
+ const nextPayload = await streamOptions?.onPayload?.(payload, payloadModel);
1476
+ const effective = nextPayload === undefined ? payload : nextPayload;
1477
+ if (!effective || typeof effective !== "object" || Array.isArray(effective)) return effective;
1478
+
1479
+ const body = effective as Record<string, unknown>;
1480
+ // Prefix the final full body before either transport, including WebSocket delta detection.
1481
+ return moveGptInstructionsToInput(
1482
+ nativeMidConvoEffort ? reshapeConfigurationUpdates(body) : body,
1483
+ builtInstructions,
1484
+ supportsTranscript ? undefined : context,
1485
+ );
1486
+ },
1487
+ });
1429
1488
 
1430
1489
  const adaptedStreamSimple: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
1431
- return mod.streamSimple(adaptModel(model), adaptContext(context), adaptOptions(streamOptions));
1490
+ return mod.streamSimple(adaptModel(model), adaptContext(context), adaptOptions(context, streamOptions));
1432
1491
  };
1433
1492
  const adaptedStream: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
1434
- return mod.stream(adaptModel(model), adaptContext(context), adaptOptions(streamOptions));
1493
+ return mod.stream(adaptModel(model), adaptContext(context), adaptOptions(context, streamOptions));
1435
1494
  };
1436
1495
 
1437
1496
  const streamSimple = wrapStreamSimpleForFast(adaptedStreamSimple, options.shouldUseFast);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@router-for-me/pi-cliproxyapi-provider",
3
- "version": "1.4.25",
3
+ "version": "1.4.26",
4
4
  "description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
5
5
  "type": "module",
6
6
  "keywords": [