@router-for-me/pi-cliproxyapi-provider 1.4.15 → 1.4.17
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/extensions/codex-stream.ts +170 -12
- package/package.json +1 -1
|
@@ -40,6 +40,154 @@ 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
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Ensure a classic context with top-level systemPrompt/tools is represented as a TranscriptContext.
|
|
155
|
+
* If the context already contains a system message in context.messages, it is returned untouched.
|
|
156
|
+
*/
|
|
157
|
+
export function ensureTranscriptContext(context: Context | { messages?: unknown[]; [key: string]: unknown }): Context {
|
|
158
|
+
if (!context || !Array.isArray(context.messages)) return context as Context;
|
|
159
|
+
const hasSystemMessage = (context.messages as Array<{ role?: string }>).some((m) => m && m.role === "system");
|
|
160
|
+
if (hasSystemMessage) return context as Context;
|
|
161
|
+
|
|
162
|
+
const hasSystemPrompt = typeof context.systemPrompt === "string" && context.systemPrompt.length > 0;
|
|
163
|
+
const hasTools = Array.isArray(context.tools) && context.tools.length > 0;
|
|
164
|
+
if (!hasSystemPrompt && !hasTools) return context as Context;
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
...(context as Record<string, unknown>),
|
|
168
|
+
messages: [
|
|
169
|
+
{
|
|
170
|
+
role: "system",
|
|
171
|
+
content: context.systemPrompt ?? "",
|
|
172
|
+
...(hasTools ? { toolsAdded: context.tools } : {}),
|
|
173
|
+
timestamp: 0,
|
|
174
|
+
},
|
|
175
|
+
...context.messages,
|
|
176
|
+
] as Context["messages"],
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function supportsTranscriptSource(source: string): boolean {
|
|
181
|
+
return source.includes("resolveTranscriptTools") || source.includes("resolveTranscript");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function adaptContextForModule(
|
|
185
|
+
context: Context | { messages?: unknown[]; [key: string]: unknown },
|
|
186
|
+
supportsTranscript: boolean,
|
|
187
|
+
): Context {
|
|
188
|
+
return supportsTranscript ? ensureTranscriptContext(context) : adaptTranscriptContext(context);
|
|
189
|
+
}
|
|
190
|
+
|
|
43
191
|
export function withPriorityServiceTier(payload: unknown): unknown {
|
|
44
192
|
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
45
193
|
return payload;
|
|
@@ -224,6 +372,16 @@ function resolveOriginalCodexModulePath(): { path: string; dir: string } {
|
|
|
224
372
|
// dist file next to the resolved package entry.
|
|
225
373
|
const candidates: string[] = [];
|
|
226
374
|
|
|
375
|
+
// pi bundled Node CLI exposes pi-ai as a virtual module to
|
|
376
|
+
// extensions. Resolve its physical nested dependency from the CLI entry so
|
|
377
|
+
// the source-patching transport can still read the installed implementation.
|
|
378
|
+
if (process.argv[1]) {
|
|
379
|
+
const bundledHostModule = resolveCodexModuleFromNodeEntry(process.argv[1]);
|
|
380
|
+
if (bundledHostModule) {
|
|
381
|
+
candidates.push(bundledHostModule);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
227
385
|
try {
|
|
228
386
|
const subpath = import.meta.resolve("@earendil-works/pi-ai/api/openai-codex-responses");
|
|
229
387
|
candidates.push(fileURLToPath(subpath));
|
|
@@ -240,16 +398,6 @@ function resolveOriginalCodexModulePath(): { path: string; dir: string } {
|
|
|
240
398
|
// ignore
|
|
241
399
|
}
|
|
242
400
|
|
|
243
|
-
// pi 0.84.3's bundled Node CLI exposes pi-ai as a virtual module to
|
|
244
|
-
// extensions. Resolve its physical nested dependency from the CLI entry so
|
|
245
|
-
// the source-patching transport can still read the installed implementation.
|
|
246
|
-
if (process.argv[1]) {
|
|
247
|
-
const bundledHostModule = resolveCodexModuleFromNodeEntry(process.argv[1]);
|
|
248
|
-
if (bundledHostModule) {
|
|
249
|
-
candidates.push(bundledHostModule);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
401
|
for (const path of candidates) {
|
|
254
402
|
if (path && existsSync(path)) {
|
|
255
403
|
return { path, dir: dirname(path) };
|
|
@@ -284,11 +432,21 @@ export async function loadCliproxyCodexStreams(
|
|
|
284
432
|
throw new Error("patched openai-codex-responses module missing streamSimple/stream exports");
|
|
285
433
|
}
|
|
286
434
|
|
|
287
|
-
const
|
|
435
|
+
const supportsTranscript = supportsTranscriptSource(originalSource);
|
|
436
|
+
const adaptContext = (context: Context): Context => adaptContextForModule(context, supportsTranscript);
|
|
437
|
+
|
|
438
|
+
const adaptedStreamSimple: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
|
|
439
|
+
return mod.streamSimple(model, adaptContext(context), streamOptions);
|
|
440
|
+
};
|
|
441
|
+
const adaptedStream: CliproxyCodexStreamSimple = (model, context, streamOptions) => {
|
|
442
|
+
return mod.stream(model, adaptContext(context), streamOptions);
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const streamSimple = wrapStreamSimpleForFast(adaptedStreamSimple, options.shouldUseFast);
|
|
288
446
|
|
|
289
447
|
return {
|
|
290
448
|
api: CLIPROXYAPI_CODEX_API,
|
|
291
449
|
streamSimple,
|
|
292
|
-
stream:
|
|
450
|
+
stream: adaptedStream,
|
|
293
451
|
};
|
|
294
452
|
}
|
package/package.json
CHANGED