@router-for-me/pi-cliproxyapi-provider 1.4.14 → 1.4.16

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.
@@ -86,6 +86,12 @@ export class ProactiveCompactionController {
86
86
  this.pending = undefined;
87
87
  });
88
88
 
89
+ pi.on("session_before_compact", () => {
90
+ // Compaction summaries use the same provider stream. Clear the synthetic
91
+ // overflow before pi invokes the summarizer so it cannot abort compaction.
92
+ this.pending = undefined;
93
+ });
94
+
89
95
  pi.on("session_compact", () => {
90
96
  this.pending = undefined;
91
97
  });
@@ -40,6 +40,116 @@ export interface CliproxyCodexStreamOptions {
40
40
 
41
41
  type PayloadHook = NonNullable<SimpleStreamOptions["onPayload"]>;
42
42
 
43
+ interface TranscriptMessage {
44
+ role?: string;
45
+ content?: unknown;
46
+ sections?: Record<string, string | null | undefined>;
47
+ toolsAdded?: unknown[];
48
+ toolsRemoved?: Array<{ name: string }>;
49
+ [key: string]: unknown;
50
+ }
51
+
52
+ function extractContentText(content: unknown): string {
53
+ if (typeof content === "string") return content.trim();
54
+ if (Array.isArray(content)) {
55
+ return content
56
+ .filter((b) => b && typeof b === "object" && b.type === "text" && typeof b.text === "string")
57
+ .map((b) => b.text.trim())
58
+ .filter(Boolean)
59
+ .join("\n");
60
+ }
61
+ return "";
62
+ }
63
+
64
+ /**
65
+ * Adapt a pi >=0.86.0 TranscriptContext into a classic Context with top-level systemPrompt and tools.
66
+ *
67
+ * In pi >=0.86.0, custom provider stream handlers receive a normalized TranscriptContext where
68
+ * system prompt sections and tool declarations are encoded into system messages inside context.messages.
69
+ * Classic openai-codex-responses expects context.systemPrompt and context.tools, and fails when
70
+ * encountering system messages inside context.messages during token estimation or payload generation.
71
+ */
72
+ export function adaptTranscriptContext(context: Context | { messages?: unknown[]; [key: string]: unknown }): Context {
73
+ if (!context || !Array.isArray(context.messages)) {
74
+ return context as Context;
75
+ }
76
+
77
+ const rawMessages = context.messages as TranscriptMessage[];
78
+ const hasSystemMessage = rawMessages.some((m) => m && m.role === "system");
79
+
80
+ if (!hasSystemMessage) {
81
+ return context as Context;
82
+ }
83
+
84
+ let leadingText = "";
85
+ const sections = new Map<string, string>();
86
+ const toolsMap = new Map<string, unknown>();
87
+
88
+ if (Array.isArray(context.tools)) {
89
+ for (const tool of context.tools) {
90
+ if (tool && typeof tool === "object" && "name" in tool && typeof tool.name === "string") {
91
+ toolsMap.set(tool.name, tool);
92
+ }
93
+ }
94
+ }
95
+
96
+ const nonSystemMessages: unknown[] = [];
97
+
98
+ for (const msg of rawMessages) {
99
+ if (!msg || typeof msg !== "object") continue;
100
+
101
+ if (msg.role === "system") {
102
+ const text = extractContentText(msg.content);
103
+ if (text) {
104
+ leadingText = leadingText ? `${leadingText}\n\n${text}` : text;
105
+ }
106
+ if (msg.sections && typeof msg.sections === "object") {
107
+ for (const [name, sectionText] of Object.entries(msg.sections)) {
108
+ if (sectionText === null || sectionText === undefined) {
109
+ sections.delete(name);
110
+ } else if (typeof sectionText === "string" && sectionText.trim()) {
111
+ sections.set(name, sectionText.trim());
112
+ }
113
+ }
114
+ }
115
+ if (Array.isArray(msg.toolsRemoved)) {
116
+ for (const tool of msg.toolsRemoved) {
117
+ if (tool && typeof tool.name === "string") {
118
+ toolsMap.delete(tool.name);
119
+ }
120
+ }
121
+ }
122
+ if (Array.isArray(msg.toolsAdded)) {
123
+ for (const tool of msg.toolsAdded) {
124
+ if (tool && typeof tool === "object" && "name" in tool && typeof tool.name === "string") {
125
+ toolsMap.set(tool.name, tool);
126
+ }
127
+ }
128
+ }
129
+ } else {
130
+ nonSystemMessages.push(msg);
131
+ }
132
+ }
133
+
134
+ const sectionParts = [...sections.values()];
135
+ const extractedSystemPrompt = [leadingText, ...sectionParts].filter(Boolean).join("\n\n");
136
+ const finalSystemPrompt =
137
+ extractedSystemPrompt || (typeof context.systemPrompt === "string" ? context.systemPrompt : undefined);
138
+ const finalTools =
139
+ toolsMap.size > 0
140
+ ? [...toolsMap.values()]
141
+ : Array.isArray(context.tools) && context.tools.length > 0
142
+ ? context.tools
143
+ : undefined;
144
+
145
+ return {
146
+ ...(context as Record<string, unknown>),
147
+ systemPrompt: finalSystemPrompt,
148
+ tools: finalTools as Context["tools"],
149
+ messages: nonSystemMessages as Context["messages"],
150
+ };
151
+ }
152
+
43
153
  export function withPriorityServiceTier(payload: unknown): unknown {
44
154
  if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
45
155
  return payload;
@@ -66,10 +176,11 @@ export function wrapStreamSimpleForFast(
66
176
  shouldUseFast?: (model: Model<Api>) => boolean,
67
177
  ): CliproxyCodexStreamSimple {
68
178
  return (model, context, streamOptions) => {
179
+ const adaptedContext = adaptTranscriptContext(context);
69
180
  if (!shouldUseFast?.(model)) {
70
- return streamSimple(model, context, streamOptions);
181
+ return streamSimple(model, adaptedContext, streamOptions);
71
182
  }
72
- return streamSimple(model, context, {
183
+ return streamSimple(model, adaptedContext, {
73
184
  ...streamOptions,
74
185
  onPayload: (payload, payloadModel) => applyFastPayloadHook(payload, payloadModel, streamOptions?.onPayload),
75
186
  });
@@ -284,11 +395,18 @@ export async function loadCliproxyCodexStreams(
284
395
  throw new Error("patched openai-codex-responses module missing streamSimple/stream exports");
285
396
  }
286
397
 
287
- const streamSimple = wrapStreamSimpleForFast(mod.streamSimple, options.shouldUseFast);
398
+ const adaptedStreamSimple: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
399
+ return mod.streamSimple(model, adaptTranscriptContext(context), streamOptions);
400
+ };
401
+ const adaptedStream: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
402
+ return mod.stream(model, adaptTranscriptContext(context), streamOptions);
403
+ };
404
+
405
+ const streamSimple = wrapStreamSimpleForFast(adaptedStreamSimple, options.shouldUseFast);
288
406
 
289
407
  return {
290
408
  api: CLIPROXYAPI_CODEX_API,
291
409
  streamSimple,
292
- stream: mod.stream,
410
+ stream: adaptedStream,
293
411
  };
294
412
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@router-for-me/pi-cliproxyapi-provider",
3
- "version": "1.4.14",
3
+ "version": "1.4.16",
4
4
  "description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
5
5
  "type": "module",
6
6
  "keywords": [