@router-for-me/pi-cliproxyapi-provider 1.4.6 → 1.4.7
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 +58 -1
- package/package.json +1 -1
|
@@ -98,7 +98,60 @@ function rewriteRelativeImports(source: string, originalDir: string): string {
|
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
function
|
|
101
|
+
function patchWebSocketOnlyTransport(source: string): string {
|
|
102
|
+
const disabledForSession =
|
|
103
|
+
/const websocketDisabledForSession\s*=\s*transport !== "sse" && isWebSocketSseFallbackActive\(options\?\.sessionId\);/;
|
|
104
|
+
const retryVariables = /let retriedWebSocketConnectionLimit\s*=\s*false;/;
|
|
105
|
+
const websocketFailureHandling =
|
|
106
|
+
/const connectionLimitBeforeStart = !websocketStarted && isWebSocketConnectionLimitReachedError\(error\);[\s\S]*?recordWebSocketSseFallback\(options\?\.sessionId\);\s*break;/;
|
|
107
|
+
const fallbackSessionRecord = "websocketSseFallbackSessions.add(sessionId);";
|
|
108
|
+
const fallbackActiveRecord = "stats.websocketFallbackActive = true;";
|
|
109
|
+
|
|
110
|
+
for (const fragment of [fallbackSessionRecord, fallbackActiveRecord]) {
|
|
111
|
+
if (!source.includes(fragment)) {
|
|
112
|
+
throw new Error("openai-codex-responses source no longer supports the WebSocket-only transport patch");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const pattern of [disabledForSession, retryVariables, websocketFailureHandling]) {
|
|
116
|
+
if (!pattern.test(source)) {
|
|
117
|
+
throw new Error("openai-codex-responses source no longer supports the WebSocket-only transport patch");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return source
|
|
122
|
+
.replace(disabledForSession, "const websocketDisabledForSession = false;")
|
|
123
|
+
.replace(
|
|
124
|
+
retryVariables,
|
|
125
|
+
`let websocketRetries = 0;
|
|
126
|
+
const maxWebSocketRetries = Number.isFinite(options?.maxRetries)
|
|
127
|
+
? Math.min(Math.max(0, Math.floor(options.maxRetries)), 5)
|
|
128
|
+
: 3;`,
|
|
129
|
+
)
|
|
130
|
+
.replace(
|
|
131
|
+
websocketFailureHandling,
|
|
132
|
+
`const connectionLimitBeforeStart = !websocketStarted && isWebSocketConnectionLimitReachedError(error);
|
|
133
|
+
if (aborted || (isCodexNonTransportError(error) && !connectionLimitBeforeStart)) {
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
if (!websocketStarted && websocketRetries < maxWebSocketRetries) {
|
|
137
|
+
websocketRetries++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
appendAssistantMessageDiagnostic(output, createAssistantMessageDiagnostic("provider_transport_failure", error, {
|
|
141
|
+
configuredTransport: transport,
|
|
142
|
+
fallbackTransport: undefined,
|
|
143
|
+
eventsEmitted: websocketStarted,
|
|
144
|
+
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
145
|
+
requestBytes: new TextEncoder().encode(bodyJson).byteLength,
|
|
146
|
+
}));
|
|
147
|
+
recordWebSocketFailure(options?.sessionId, error);
|
|
148
|
+
throw error;`,
|
|
149
|
+
)
|
|
150
|
+
.replace(fallbackSessionRecord, "")
|
|
151
|
+
.replace(fallbackActiveRecord, "stats.websocketFallbackActive = false;");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function patchCodexSource(source: string, providerIds: string[]): string {
|
|
102
155
|
let src = source;
|
|
103
156
|
|
|
104
157
|
if (!/function extractAccountId\(token\) \{/.test(src)) {
|
|
@@ -131,6 +184,10 @@ function patchCodexSource(source: string, providerIds: string[]): string {
|
|
|
131
184
|
// Keep assistant message api metadata aligned with the registered custom api id.
|
|
132
185
|
src = src.replaceAll(`api: "openai-codex-responses"`, `api: ${JSON.stringify(CLIPROXYAPI_CODEX_API)}`);
|
|
133
186
|
|
|
187
|
+
// CLIProxyAPI needs a persistent WebSocket transport. Reconnect before the
|
|
188
|
+
// response starts and surface a failure rather than silently switching to SSE.
|
|
189
|
+
src = patchWebSocketOnlyTransport(src);
|
|
190
|
+
|
|
134
191
|
// The generated module lives outside the original source map directory.
|
|
135
192
|
src = src.replace(/^\/\/# sourceMappingURL=.*$/gm, "");
|
|
136
193
|
|
package/package.json
CHANGED