chore: normalize formatting (2-space → tabs) in stream-error-patterns files
Re-tab only — no logic changes. Follows the repo's tab-based formatting for these files, matching the CommandCode executor/translator style. Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
This commit is contained in:
@@ -4,10 +4,16 @@ import { createErrorResult } from "../../utils/error.js";
|
|||||||
import { HTTP_STATUS } from "../../config/runtimeConfig.js";
|
import { HTTP_STATUS } from "../../config/runtimeConfig.js";
|
||||||
import { FORMATS } from "../../translator/formats.js";
|
import { FORMATS } from "../../translator/formats.js";
|
||||||
import { PROVIDERS } from "../../config/providers.js";
|
import { PROVIDERS } from "../../config/providers.js";
|
||||||
import { buildRequestDetail, extractRequestConfig, saveUsageStats, formatDoneLine } from "./requestDetail.js";
|
import {
|
||||||
|
buildRequestDetail,
|
||||||
|
extractRequestConfig,
|
||||||
|
saveUsageStats,
|
||||||
|
formatDoneLine,
|
||||||
|
} from "./requestDetail.js";
|
||||||
|
|
||||||
// Responses-API providers (e.g. codex) may emit SSE without content-type + use Responses output shape
|
// Responses-API providers (e.g. codex) may emit SSE without content-type + use Responses output shape
|
||||||
const isResponsesProvider = (p) => PROVIDERS[p]?.format === FORMATS.OPENAI_RESPONSES;
|
const isResponsesProvider = (p) =>
|
||||||
|
PROVIDERS[p]?.format === FORMATS.OPENAI_RESPONSES;
|
||||||
import { saveRequestDetail, appendRequestLog } from "@/lib/usageDb.js";
|
import { saveRequestDetail, appendRequestLog } from "@/lib/usageDb.js";
|
||||||
|
|
||||||
function textFromResponsesMessageItem(item) {
|
function textFromResponsesMessageItem(item) {
|
||||||
@@ -52,7 +58,9 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
|||||||
const chunk = JSON.parse(payload);
|
const chunk = JSON.parse(payload);
|
||||||
if (chunk?.error) streamError = chunk.error;
|
if (chunk?.error) streamError = chunk.error;
|
||||||
else chunks.push(chunk);
|
else chunks.push(chunk);
|
||||||
} catch { /* ignore malformed lines */ }
|
} catch {
|
||||||
|
/* ignore malformed lines */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (streamError) return { error: streamError };
|
if (streamError) return { error: streamError };
|
||||||
@@ -68,8 +76,13 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
|||||||
for (const chunk of chunks) {
|
for (const chunk of chunks) {
|
||||||
const choice = chunk?.choices?.[0];
|
const choice = chunk?.choices?.[0];
|
||||||
const delta = choice?.delta || {};
|
const delta = choice?.delta || {};
|
||||||
if (typeof delta.content === "string" && delta.content.length > 0) contentParts.push(delta.content);
|
if (typeof delta.content === "string" && delta.content.length > 0)
|
||||||
if (typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0) reasoningParts.push(delta.reasoning_content);
|
contentParts.push(delta.content);
|
||||||
|
if (
|
||||||
|
typeof delta.reasoning_content === "string" &&
|
||||||
|
delta.reasoning_content.length > 0
|
||||||
|
)
|
||||||
|
reasoningParts.push(delta.reasoning_content);
|
||||||
if (choice?.finish_reason) finishReason = choice.finish_reason;
|
if (choice?.finish_reason) finishReason = choice.finish_reason;
|
||||||
if (chunk?.usage && typeof chunk.usage === "object") usage = chunk.usage;
|
if (chunk?.usage && typeof chunk.usage === "object") usage = chunk.usage;
|
||||||
|
|
||||||
@@ -78,20 +91,31 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
|||||||
for (const tc of delta.tool_calls) {
|
for (const tc of delta.tool_calls) {
|
||||||
const idx = tc.index ?? 0;
|
const idx = tc.index ?? 0;
|
||||||
if (!toolCallMap.has(idx)) {
|
if (!toolCallMap.has(idx)) {
|
||||||
toolCallMap.set(idx, { id: tc.id || "", type: "function", function: { name: "", arguments: "" } });
|
toolCallMap.set(idx, {
|
||||||
|
id: tc.id || "",
|
||||||
|
type: "function",
|
||||||
|
function: { name: "", arguments: "" },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const existing = toolCallMap.get(idx);
|
const existing = toolCallMap.get(idx);
|
||||||
if (tc.id) existing.id = tc.id;
|
if (tc.id) existing.id = tc.id;
|
||||||
if (tc.function?.name) existing.function.name += tc.function.name;
|
if (tc.function?.name) existing.function.name += tc.function.name;
|
||||||
if (tc.function?.arguments) existing.function.arguments += tc.function.arguments;
|
if (tc.function?.arguments)
|
||||||
|
existing.function.arguments += tc.function.arguments;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = { role: "assistant", content: contentParts.join("") || (toolCallMap.size > 0 ? null : "") };
|
const message = {
|
||||||
if (reasoningParts.length > 0) message.reasoning_content = reasoningParts.join("");
|
role: "assistant",
|
||||||
|
content: contentParts.join("") || (toolCallMap.size > 0 ? null : ""),
|
||||||
|
};
|
||||||
|
if (reasoningParts.length > 0)
|
||||||
|
message.reasoning_content = reasoningParts.join("");
|
||||||
if (toolCallMap.size > 0) {
|
if (toolCallMap.size > 0) {
|
||||||
message.tool_calls = [...toolCallMap.entries()].sort((a, b) => a[0] - b[0]).map(([, tc]) => tc);
|
message.tool_calls = [...toolCallMap.entries()]
|
||||||
|
.sort((a, b) => a[0] - b[0])
|
||||||
|
.map(([, tc]) => tc);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
@@ -99,7 +123,7 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
|||||||
object: "chat.completion",
|
object: "chat.completion",
|
||||||
created: first.created || Math.floor(Date.now() / 1000),
|
created: first.created || Math.floor(Date.now() / 1000),
|
||||||
model: first.model || fallbackModel || "unknown",
|
model: first.model || fallbackModel || "unknown",
|
||||||
choices: [{ index: 0, message, finish_reason: finishReason }]
|
choices: [{ index: 0, message, finish_reason: finishReason }],
|
||||||
};
|
};
|
||||||
if (usage) result.usage = usage;
|
if (usage) result.usage = usage;
|
||||||
return result;
|
return result;
|
||||||
@@ -109,45 +133,109 @@ export function parseSSEToOpenAIResponse(rawSSE, fallbackModel) {
|
|||||||
* Handle case: provider forced streaming but client wants JSON.
|
* Handle case: provider forced streaming but client wants JSON.
|
||||||
* Supports both Codex/Responses API SSE and standard Chat Completions SSE.
|
* Supports both Codex/Responses API SSE and standard Chat Completions SSE.
|
||||||
*/
|
*/
|
||||||
export async function handleForcedSSEToJson({ providerResponse, sourceFormat, provider, model, body, stream, translatedBody, finalBody, requestStartTime, connectionId, apiKey, clientRawRequest, onRequestSuccess, trackDone, appendLog, reqTag, log, streamErrorPatterns }) {
|
export async function handleForcedSSEToJson({
|
||||||
|
providerResponse,
|
||||||
|
sourceFormat,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
body,
|
||||||
|
stream,
|
||||||
|
translatedBody,
|
||||||
|
finalBody,
|
||||||
|
requestStartTime,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
clientRawRequest,
|
||||||
|
onRequestSuccess,
|
||||||
|
trackDone,
|
||||||
|
appendLog,
|
||||||
|
reqTag,
|
||||||
|
log,
|
||||||
|
streamErrorPatterns,
|
||||||
|
}) {
|
||||||
const contentType = providerResponse.headers.get("content-type") || "";
|
const contentType = providerResponse.headers.get("content-type") || "";
|
||||||
const isSSE = contentType.includes("text/event-stream") || (contentType === "" && isResponsesProvider(provider));
|
const isSSE =
|
||||||
|
contentType.includes("text/event-stream") ||
|
||||||
|
(contentType === "" && isResponsesProvider(provider));
|
||||||
if (!isSSE) return null; // not handled here
|
if (!isSSE) return null; // not handled here
|
||||||
|
|
||||||
trackDone();
|
trackDone();
|
||||||
|
|
||||||
const ctx = {
|
const ctx = {
|
||||||
provider, model, connectionId,
|
provider,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
request: extractRequestConfig(body, stream),
|
request: extractRequestConfig(body, stream),
|
||||||
providerRequest: finalBody || translatedBody || null
|
providerRequest: finalBody || translatedBody || null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Codex/Responses API SSE path
|
// Codex/Responses API SSE path
|
||||||
const isCodexResponsesApi = isResponsesProvider(provider) || sourceFormat === FORMATS.OPENAI_RESPONSES;
|
const isCodexResponsesApi =
|
||||||
|
isResponsesProvider(provider) || sourceFormat === FORMATS.OPENAI_RESPONSES;
|
||||||
if (isCodexResponsesApi) {
|
if (isCodexResponsesApi) {
|
||||||
try {
|
try {
|
||||||
const jsonResponse = await convertResponsesStreamToJson(providerResponse.body);
|
const jsonResponse = await convertResponsesStreamToJson(
|
||||||
|
providerResponse.body,
|
||||||
|
);
|
||||||
if (onRequestSuccess) await onRequestSuccess();
|
if (onRequestSuccess) await onRequestSuccess();
|
||||||
|
|
||||||
const usage = jsonResponse.usage || {};
|
const usage = jsonResponse.usage || {};
|
||||||
appendLog({ tokens: usage, status: "200 OK" });
|
appendLog({ tokens: usage, status: "200 OK" });
|
||||||
saveUsageStats({ provider, model, tokens: usage, connectionId, apiKey, endpoint: clientRawRequest?.endpoint, silent: true });
|
saveUsageStats({
|
||||||
if (log?.line) log.line(reqTag, "📊", formatDoneLine({ usage, latency: { total: Date.now() - requestStartTime } }));
|
provider,
|
||||||
|
model,
|
||||||
|
tokens: usage,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
endpoint: clientRawRequest?.endpoint,
|
||||||
|
silent: true,
|
||||||
|
});
|
||||||
|
if (log?.line)
|
||||||
|
log.line(
|
||||||
|
reqTag,
|
||||||
|
"📊",
|
||||||
|
formatDoneLine({
|
||||||
|
usage,
|
||||||
|
latency: { total: Date.now() - requestStartTime },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
const { msgItem, textContent } = pickAssistantMessageForChatCompletion(jsonResponse.output);
|
const { msgItem, textContent } = pickAssistantMessageForChatCompletion(
|
||||||
|
jsonResponse.output,
|
||||||
|
);
|
||||||
const totalLatency = Date.now() - requestStartTime;
|
const totalLatency = Date.now() - requestStartTime;
|
||||||
|
|
||||||
saveRequestDetail(buildRequestDetail({
|
saveRequestDetail(
|
||||||
|
buildRequestDetail(
|
||||||
|
{
|
||||||
...ctx,
|
...ctx,
|
||||||
latency: { ttft: totalLatency, total: totalLatency },
|
latency: { ttft: totalLatency, total: totalLatency },
|
||||||
tokens: { prompt_tokens: usage.input_tokens || 0, completion_tokens: usage.output_tokens || 0 },
|
tokens: {
|
||||||
response: { content: textContent, thinking: null, finish_reason: jsonResponse.status || "unknown" },
|
prompt_tokens: usage.input_tokens || 0,
|
||||||
status: "success"
|
completion_tokens: usage.output_tokens || 0,
|
||||||
}, { endpoint: clientRawRequest?.endpoint || null })).catch(() => {});
|
},
|
||||||
|
response: {
|
||||||
|
content: textContent,
|
||||||
|
thinking: null,
|
||||||
|
finish_reason: jsonResponse.status || "unknown",
|
||||||
|
},
|
||||||
|
status: "success",
|
||||||
|
},
|
||||||
|
{ endpoint: clientRawRequest?.endpoint || null },
|
||||||
|
),
|
||||||
|
).catch(() => {});
|
||||||
|
|
||||||
// Client is Responses API → return as-is
|
// Client is Responses API → return as-is
|
||||||
if (sourceFormat === FORMATS.OPENAI_RESPONSES) {
|
if (sourceFormat === FORMATS.OPENAI_RESPONSES) {
|
||||||
return { success: true, response: new Response(JSON.stringify(jsonResponse), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }) };
|
return {
|
||||||
|
success: true,
|
||||||
|
response: new Response(JSON.stringify(jsonResponse), {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build client-format response
|
// Build client-format response
|
||||||
@@ -156,45 +244,90 @@ export async function handleForcedSSEToJson({ providerResponse, sourceFormat, pr
|
|||||||
let finalResp;
|
let finalResp;
|
||||||
|
|
||||||
// Extract tool calls from Responses API output (function_call items)
|
// Extract tool calls from Responses API output (function_call items)
|
||||||
const funcCallItems = (jsonResponse.output || []).filter(item => item.type === "function_call");
|
const funcCallItems = (jsonResponse.output || []).filter(
|
||||||
|
(item) => item.type === "function_call",
|
||||||
|
);
|
||||||
const toolCalls = funcCallItems.map((item, idx) => ({
|
const toolCalls = funcCallItems.map((item, idx) => ({
|
||||||
id: item.call_id || `call_${item.name}_${Date.now()}_${idx}`,
|
id: item.call_id || `call_${item.name}_${Date.now()}_${idx}`,
|
||||||
type: "function",
|
type: "function",
|
||||||
function: {
|
function: {
|
||||||
name: item.name,
|
name: item.name,
|
||||||
arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments || {})
|
arguments:
|
||||||
}
|
typeof item.arguments === "string"
|
||||||
|
? item.arguments
|
||||||
|
: JSON.stringify(item.arguments || {}),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
const hasToolCalls = toolCalls.length > 0;
|
const hasToolCalls = toolCalls.length > 0;
|
||||||
|
|
||||||
if (sourceFormat === FORMATS.ANTIGRAVITY || sourceFormat === FORMATS.GEMINI || sourceFormat === FORMATS.GEMINI_CLI) {
|
if (
|
||||||
|
sourceFormat === FORMATS.ANTIGRAVITY ||
|
||||||
|
sourceFormat === FORMATS.GEMINI ||
|
||||||
|
sourceFormat === FORMATS.GEMINI_CLI
|
||||||
|
) {
|
||||||
finalResp = {
|
finalResp = {
|
||||||
response: {
|
response: {
|
||||||
candidates: [{ content: { role: "model", parts: [{ text: textContent || "" }] }, finishReason: "STOP", index: 0 }],
|
candidates: [
|
||||||
usageMetadata: { promptTokenCount: inTokens, candidatesTokenCount: outTokens, totalTokenCount: inTokens + outTokens },
|
{
|
||||||
|
content: {
|
||||||
|
role: "model",
|
||||||
|
parts: [{ text: textContent || "" }],
|
||||||
|
},
|
||||||
|
finishReason: "STOP",
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usageMetadata: {
|
||||||
|
promptTokenCount: inTokens,
|
||||||
|
candidatesTokenCount: outTokens,
|
||||||
|
totalTokenCount: inTokens + outTokens,
|
||||||
|
},
|
||||||
modelVersion: model,
|
modelVersion: model,
|
||||||
responseId: jsonResponse.id || `resp_${Date.now()}`
|
responseId: jsonResponse.id || `resp_${Date.now()}`,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
const message = { role: "assistant", content: textContent || (hasToolCalls ? null : "") };
|
const message = {
|
||||||
|
role: "assistant",
|
||||||
|
content: textContent || (hasToolCalls ? null : ""),
|
||||||
|
};
|
||||||
if (hasToolCalls) message.tool_calls = toolCalls;
|
if (hasToolCalls) message.tool_calls = toolCalls;
|
||||||
const responseDone = jsonResponse.status === "completed" || jsonResponse.status === "done";
|
const responseDone =
|
||||||
const finishReason = hasToolCalls ? "tool_calls" : (responseDone ? "stop" : (jsonResponse.status || "stop"));
|
jsonResponse.status === "completed" || jsonResponse.status === "done";
|
||||||
|
const finishReason = hasToolCalls
|
||||||
|
? "tool_calls"
|
||||||
|
: responseDone
|
||||||
|
? "stop"
|
||||||
|
: jsonResponse.status || "stop";
|
||||||
finalResp = {
|
finalResp = {
|
||||||
id: jsonResponse.id || `chatcmpl-${Date.now()}`,
|
id: jsonResponse.id || `chatcmpl-${Date.now()}`,
|
||||||
object: "chat.completion",
|
object: "chat.completion",
|
||||||
created: jsonResponse.created_at || Math.floor(Date.now() / 1000),
|
created: jsonResponse.created_at || Math.floor(Date.now() / 1000),
|
||||||
model: jsonResponse.model || model,
|
model: jsonResponse.model || model,
|
||||||
choices: [{ index: 0, message, finish_reason: finishReason }],
|
choices: [{ index: 0, message, finish_reason: finishReason }],
|
||||||
usage: { prompt_tokens: inTokens, completion_tokens: outTokens, total_tokens: inTokens + outTokens }
|
usage: {
|
||||||
|
prompt_tokens: inTokens,
|
||||||
|
completion_tokens: outTokens,
|
||||||
|
total_tokens: inTokens + outTokens,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true, response: new Response(JSON.stringify(finalResp), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }) };
|
return {
|
||||||
|
success: true,
|
||||||
|
response: new Response(JSON.stringify(finalResp), {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[ChatCore] Responses API SSE→JSON failed:", err);
|
console.error("[ChatCore] Responses API SSE→JSON failed:", err);
|
||||||
return createErrorResult(HTTP_STATUS.BAD_GATEWAY, "Failed to convert streaming response to JSON");
|
return createErrorResult(
|
||||||
|
HTTP_STATUS.BAD_GATEWAY,
|
||||||
|
"Failed to convert streaming response to JSON",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,11 +335,15 @@ export async function handleForcedSSEToJson({ providerResponse, sourceFormat, pr
|
|||||||
try {
|
try {
|
||||||
const sseText = await providerResponse.text();
|
const sseText = await providerResponse.text();
|
||||||
const parsed = parseSSEToOpenAIResponse(sseText, model);
|
const parsed = parseSSEToOpenAIResponse(sseText, model);
|
||||||
if (!parsed) return createErrorResult(HTTP_STATUS.BAD_GATEWAY, "Invalid SSE response for non-streaming request");
|
if (!parsed)
|
||||||
|
return createErrorResult(
|
||||||
|
HTTP_STATUS.BAD_GATEWAY,
|
||||||
|
"Invalid SSE response for non-streaming request",
|
||||||
|
);
|
||||||
if (parsed.error) {
|
if (parsed.error) {
|
||||||
return createErrorResult(
|
return createErrorResult(
|
||||||
HTTP_STATUS.BAD_GATEWAY,
|
HTTP_STATUS.BAD_GATEWAY,
|
||||||
parsed.error.message || "Upstream SSE stream failed"
|
parsed.error.message || "Upstream SSE stream failed",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,31 +352,55 @@ export async function handleForcedSSEToJson({ providerResponse, sourceFormat, pr
|
|||||||
// error so account/combo fallback and FAILED logging kick in.
|
// error so account/combo fallback and FAILED logging kick in.
|
||||||
const matchedPattern = matchStreamErrorPatterns(
|
const matchedPattern = matchStreamErrorPatterns(
|
||||||
streamErrorPatterns?.[provider],
|
streamErrorPatterns?.[provider],
|
||||||
parsed.choices?.[0]?.message?.content || ""
|
parsed.choices?.[0]?.message?.content || "",
|
||||||
);
|
);
|
||||||
if (matchedPattern) {
|
if (matchedPattern) {
|
||||||
return createErrorResult(HTTP_STATUS.BAD_GATEWAY, `Stream error pattern matched: ${matchedPattern}`);
|
return createErrorResult(
|
||||||
|
HTTP_STATUS.BAD_GATEWAY,
|
||||||
|
`Stream error pattern matched: ${matchedPattern}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onRequestSuccess) await onRequestSuccess();
|
if (onRequestSuccess) await onRequestSuccess();
|
||||||
|
|
||||||
const usage = parsed.usage || {};
|
const usage = parsed.usage || {};
|
||||||
appendLog({ tokens: usage, status: "200 OK" });
|
appendLog({ tokens: usage, status: "200 OK" });
|
||||||
saveUsageStats({ provider, model, tokens: usage, connectionId, apiKey, endpoint: clientRawRequest?.endpoint, silent: true });
|
saveUsageStats({
|
||||||
if (log?.line) log.line(reqTag, "📊", formatDoneLine({ usage, latency: { total: Date.now() - requestStartTime } }));
|
provider,
|
||||||
|
model,
|
||||||
|
tokens: usage,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
endpoint: clientRawRequest?.endpoint,
|
||||||
|
silent: true,
|
||||||
|
});
|
||||||
|
if (log?.line)
|
||||||
|
log.line(
|
||||||
|
reqTag,
|
||||||
|
"📊",
|
||||||
|
formatDoneLine({
|
||||||
|
usage,
|
||||||
|
latency: { total: Date.now() - requestStartTime },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
const totalLatency = Date.now() - requestStartTime;
|
const totalLatency = Date.now() - requestStartTime;
|
||||||
saveRequestDetail(buildRequestDetail({
|
saveRequestDetail(
|
||||||
|
buildRequestDetail(
|
||||||
|
{
|
||||||
...ctx,
|
...ctx,
|
||||||
latency: { ttft: totalLatency, total: totalLatency },
|
latency: { ttft: totalLatency, total: totalLatency },
|
||||||
tokens: usage,
|
tokens: usage,
|
||||||
response: {
|
response: {
|
||||||
content: parsed.choices?.[0]?.message?.content || null,
|
content: parsed.choices?.[0]?.message?.content || null,
|
||||||
thinking: parsed.choices?.[0]?.message?.reasoning_content || null,
|
thinking: parsed.choices?.[0]?.message?.reasoning_content || null,
|
||||||
finish_reason: parsed.choices?.[0]?.finish_reason || "unknown"
|
finish_reason: parsed.choices?.[0]?.finish_reason || "unknown",
|
||||||
},
|
},
|
||||||
status: "success"
|
status: "success",
|
||||||
}, { endpoint: clientRawRequest?.endpoint || null })).catch(() => {});
|
},
|
||||||
|
{ endpoint: clientRawRequest?.endpoint || null },
|
||||||
|
),
|
||||||
|
).catch(() => {});
|
||||||
|
|
||||||
// Strip reasoning_content only when content is non-empty.
|
// Strip reasoning_content only when content is non-empty.
|
||||||
// When content is empty (e.g. thinking models that used all tokens for reasoning),
|
// When content is empty (e.g. thinking models that used all tokens for reasoning),
|
||||||
@@ -253,9 +414,20 @@ export async function handleForcedSSEToJson({ providerResponse, sourceFormat, pr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true, response: new Response(JSON.stringify(parsed), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }) };
|
return {
|
||||||
|
success: true,
|
||||||
|
response: new Response(JSON.stringify(parsed), {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[ChatCore] Chat Completions SSE→JSON failed:", err);
|
console.error("[ChatCore] Chat Completions SSE→JSON failed:", err);
|
||||||
return createErrorResult(HTTP_STATUS.BAD_GATEWAY, "Failed to convert streaming response to JSON");
|
return createErrorResult(
|
||||||
|
HTTP_STATUS.BAD_GATEWAY,
|
||||||
|
"Failed to convert streaming response to JSON",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
import { FORMATS } from "../../translator/formats.js";
|
import { FORMATS } from "../../translator/formats.js";
|
||||||
import { needsTranslation } from "../../translator/index.js";
|
import { needsTranslation } from "../../translator/index.js";
|
||||||
import { createSSETransformStreamWithLogger, createPassthroughStreamWithLogger } from "../../utils/stream.js";
|
import {
|
||||||
|
createSSETransformStreamWithLogger,
|
||||||
|
createPassthroughStreamWithLogger,
|
||||||
|
} from "../../utils/stream.js";
|
||||||
import { pipeWithDisconnect } from "../../utils/streamHandler.js";
|
import { pipeWithDisconnect } from "../../utils/streamHandler.js";
|
||||||
import { PROVIDERS } from "../../config/providers.js";
|
import { PROVIDERS } from "../../config/providers.js";
|
||||||
import { STREAM_STALL_TIMEOUT_MS } from "../../config/runtimeConfig.js";
|
import { STREAM_STALL_TIMEOUT_MS } from "../../config/runtimeConfig.js";
|
||||||
import { buildAbortedResponsesTerminalBytes } from "../../utils/responsesStreamHelpers.js";
|
import { buildAbortedResponsesTerminalBytes } from "../../utils/responsesStreamHelpers.js";
|
||||||
import { buildRequestDetail, extractRequestConfig, saveUsageStats, formatDoneLine } from "./requestDetail.js";
|
import {
|
||||||
|
buildRequestDetail,
|
||||||
|
extractRequestConfig,
|
||||||
|
saveUsageStats,
|
||||||
|
formatDoneLine,
|
||||||
|
} from "./requestDetail.js";
|
||||||
import { streamStatusForContent } from "../../utils/streamErrorPatterns.js";
|
import { streamStatusForContent } from "../../utils/streamErrorPatterns.js";
|
||||||
import { saveRequestDetail } from "@/lib/usageDb.js";
|
import { saveRequestDetail } from "@/lib/usageDb.js";
|
||||||
import { SSE_HEADERS_CORS as SSE_HEADERS } from "../../utils/sseConstants.js";
|
import { SSE_HEADERS_CORS as SSE_HEADERS } from "../../utils/sseConstants.js";
|
||||||
@@ -23,33 +31,108 @@ const CODEX_SOURCE_TO_TARGET = {
|
|||||||
/**
|
/**
|
||||||
* Determine which SSE transform stream to use based on provider/format.
|
* Determine which SSE transform stream to use based on provider/format.
|
||||||
*/
|
*/
|
||||||
function buildTransformStream({ provider, sourceFormat, targetFormat, userAgent, reqLogger, toolNameMap, model, connectionId, body, onStreamComplete, apiKey }) {
|
function buildTransformStream({
|
||||||
const isDroidCLI = userAgent?.toLowerCase().includes("droid") || userAgent?.toLowerCase().includes("codex-cli");
|
provider,
|
||||||
|
sourceFormat,
|
||||||
|
targetFormat,
|
||||||
|
userAgent,
|
||||||
|
reqLogger,
|
||||||
|
toolNameMap,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
body,
|
||||||
|
onStreamComplete,
|
||||||
|
apiKey,
|
||||||
|
}) {
|
||||||
|
const isDroidCLI =
|
||||||
|
userAgent?.toLowerCase().includes("droid") ||
|
||||||
|
userAgent?.toLowerCase().includes("codex-cli");
|
||||||
// Responses-API providers (e.g. codex) emit Responses SSE → translate into client format
|
// Responses-API providers (e.g. codex) emit Responses SSE → translate into client format
|
||||||
const isResponsesProvider = PROVIDERS[provider]?.format === FORMATS.OPENAI_RESPONSES;
|
const isResponsesProvider =
|
||||||
const needsCodexTranslation = isResponsesProvider && targetFormat === FORMATS.OPENAI_RESPONSES && !isDroidCLI;
|
PROVIDERS[provider]?.format === FORMATS.OPENAI_RESPONSES;
|
||||||
|
const needsCodexTranslation =
|
||||||
|
isResponsesProvider &&
|
||||||
|
targetFormat === FORMATS.OPENAI_RESPONSES &&
|
||||||
|
!isDroidCLI;
|
||||||
|
|
||||||
if (needsCodexTranslation) {
|
if (needsCodexTranslation) {
|
||||||
const codexTarget = CODEX_SOURCE_TO_TARGET[sourceFormat] || FORMATS.OPENAI;
|
const codexTarget = CODEX_SOURCE_TO_TARGET[sourceFormat] || FORMATS.OPENAI;
|
||||||
return createSSETransformStreamWithLogger(FORMATS.OPENAI_RESPONSES, codexTarget, provider, reqLogger, toolNameMap, model, connectionId, body, onStreamComplete, apiKey);
|
return createSSETransformStreamWithLogger(
|
||||||
|
FORMATS.OPENAI_RESPONSES,
|
||||||
|
codexTarget,
|
||||||
|
provider,
|
||||||
|
reqLogger,
|
||||||
|
toolNameMap,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
body,
|
||||||
|
onStreamComplete,
|
||||||
|
apiKey,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needsTranslation(targetFormat, sourceFormat)) {
|
if (needsTranslation(targetFormat, sourceFormat)) {
|
||||||
return createSSETransformStreamWithLogger(targetFormat, sourceFormat, provider, reqLogger, toolNameMap, model, connectionId, body, onStreamComplete, apiKey);
|
return createSSETransformStreamWithLogger(
|
||||||
|
targetFormat,
|
||||||
|
sourceFormat,
|
||||||
|
provider,
|
||||||
|
reqLogger,
|
||||||
|
toolNameMap,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
body,
|
||||||
|
onStreamComplete,
|
||||||
|
apiKey,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return createPassthroughStreamWithLogger(provider, reqLogger, model, connectionId, body, onStreamComplete, apiKey);
|
return createPassthroughStreamWithLogger(
|
||||||
|
provider,
|
||||||
|
reqLogger,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
body,
|
||||||
|
onStreamComplete,
|
||||||
|
apiKey,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle streaming response — pipe provider SSE through transform stream to client.
|
* Handle streaming response — pipe provider SSE through transform stream to client.
|
||||||
*/
|
*/
|
||||||
export async function handleStreamingResponse({ providerResponse, provider, model, sourceFormat, targetFormat, userAgent, body, stream, translatedBody, finalBody, requestStartTime, connectionId, apiKey, clientRawRequest, onRequestSuccess, reqLogger, toolNameMap, streamController, onStreamComplete, streamDetailId, pxpipe, reqTag, log }) {
|
export async function handleStreamingResponse({
|
||||||
|
providerResponse,
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
sourceFormat,
|
||||||
|
targetFormat,
|
||||||
|
userAgent,
|
||||||
|
body,
|
||||||
|
stream,
|
||||||
|
translatedBody,
|
||||||
|
finalBody,
|
||||||
|
requestStartTime,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
clientRawRequest,
|
||||||
|
onRequestSuccess,
|
||||||
|
reqLogger,
|
||||||
|
toolNameMap,
|
||||||
|
streamController,
|
||||||
|
onStreamComplete,
|
||||||
|
streamDetailId,
|
||||||
|
pxpipe,
|
||||||
|
reqTag,
|
||||||
|
log,
|
||||||
|
}) {
|
||||||
if (onRequestSuccess) {
|
if (onRequestSuccess) {
|
||||||
Promise.resolve()
|
Promise.resolve()
|
||||||
.then(onRequestSuccess)
|
.then(onRequestSuccess)
|
||||||
.catch(err => {
|
.catch((err) => {
|
||||||
console.error("[ChatCore] onRequestSuccess failed:", err?.message || err);
|
console.error(
|
||||||
|
"[ChatCore] onRequestSuccess failed:",
|
||||||
|
err?.message || err,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,84 +143,192 @@ export async function handleStreamingResponse({ providerResponse, provider, mode
|
|||||||
// return a clean JSON error instead. The message is stripped of HTML tags
|
// return a clean JSON error instead. The message is stripped of HTML tags
|
||||||
// and clamped so untrusted upstream text never reaches the client verbatim
|
// and clamped so untrusted upstream text never reaches the client verbatim
|
||||||
// (the UI may render error.message as HTML).
|
// (the UI may render error.message as HTML).
|
||||||
const upstreamContentType = (providerResponse.headers.get('content-type') || '').toLowerCase();
|
const upstreamContentType = (
|
||||||
if (upstreamContentType && !upstreamContentType.includes('text/event-stream') && !upstreamContentType.includes('application/json')) {
|
providerResponse.headers.get("content-type") || ""
|
||||||
const bodyText = await providerResponse.text().catch(() => '');
|
).toLowerCase();
|
||||||
|
if (
|
||||||
|
upstreamContentType &&
|
||||||
|
!upstreamContentType.includes("text/event-stream") &&
|
||||||
|
!upstreamContentType.includes("application/json")
|
||||||
|
) {
|
||||||
|
const bodyText = await providerResponse.text().catch(() => "");
|
||||||
const titleMatch = bodyText.match(/<title>([^<]+)<\/title>/i);
|
const titleMatch = bodyText.match(/<title>([^<]+)<\/title>/i);
|
||||||
const sanitizedTitle = (titleMatch?.[1] || '').replace(/<[^>]*>/g, '').replace(/[\r\n]+/g, ' ').trim().slice(0, 160);
|
const sanitizedTitle = (titleMatch?.[1] || "")
|
||||||
const shortMsg = sanitizedTitle
|
.replace(/<[^>]*>/g, "")
|
||||||
|| (bodyText.length < 200 ? bodyText.replace(/<[^>]*>/g, '').trim().slice(0, 160) : `Upstream returned non-SSE response (${upstreamContentType})`);
|
.replace(/[\r\n]+/g, " ")
|
||||||
|
.trim()
|
||||||
|
.slice(0, 160);
|
||||||
|
const shortMsg =
|
||||||
|
sanitizedTitle ||
|
||||||
|
(bodyText.length < 200
|
||||||
|
? bodyText
|
||||||
|
.replace(/<[^>]*>/g, "")
|
||||||
|
.trim()
|
||||||
|
.slice(0, 160)
|
||||||
|
: `Upstream returned non-SSE response (${upstreamContentType})`);
|
||||||
const status = providerResponse.status || 502;
|
const status = providerResponse.status || 502;
|
||||||
if (log?.errorLine) log.errorLine(reqTag, "✗", `BLOCKED ${status} · ${provider}/${model} · non-SSE (${upstreamContentType})\n ${shortMsg}`);
|
if (log?.errorLine)
|
||||||
else console.warn(`[STREAM] ${provider} | ${model} | blocked pipe: ${shortMsg} [${status}]`);
|
log.errorLine(
|
||||||
|
reqTag,
|
||||||
|
"✗",
|
||||||
|
`BLOCKED ${status} · ${provider}/${model} · non-SSE (${upstreamContentType})\n ${shortMsg}`,
|
||||||
|
);
|
||||||
|
else
|
||||||
|
console.warn(
|
||||||
|
`[STREAM] ${provider} | ${model} | blocked pipe: ${shortMsg} [${status}]`,
|
||||||
|
);
|
||||||
streamController?.handleError?.(new Error(`upstream non-SSE: ${status}`));
|
streamController?.handleError?.(new Error(`upstream non-SSE: ${status}`));
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
response: new Response(JSON.stringify({ error: { message: `[${status}]: ${shortMsg}` } }), {
|
response: new Response(
|
||||||
|
JSON.stringify({ error: { message: `[${status}]: ${shortMsg}` } }),
|
||||||
|
{
|
||||||
status,
|
status,
|
||||||
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
|
headers: {
|
||||||
}),
|
"Content-Type": "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const transformStream = buildTransformStream({ provider, sourceFormat, targetFormat, userAgent, reqLogger, toolNameMap, model, connectionId, body, onStreamComplete, apiKey });
|
const transformStream = buildTransformStream({
|
||||||
|
provider,
|
||||||
|
sourceFormat,
|
||||||
|
targetFormat,
|
||||||
|
userAgent,
|
||||||
|
reqLogger,
|
||||||
|
toolNameMap,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
body,
|
||||||
|
onStreamComplete,
|
||||||
|
apiKey,
|
||||||
|
});
|
||||||
|
|
||||||
// Responses passthrough: synthesize response.failed + [DONE] if the stream aborts/stalls before a terminal event
|
// Responses passthrough: synthesize response.failed + [DONE] if the stream aborts/stalls before a terminal event
|
||||||
const isResponsesPassthrough = sourceFormat === FORMATS.OPENAI_RESPONSES && targetFormat === FORMATS.OPENAI_RESPONSES;
|
const isResponsesPassthrough =
|
||||||
const onAbortTerminal = isResponsesPassthrough ? buildAbortedResponsesTerminalBytes : null;
|
sourceFormat === FORMATS.OPENAI_RESPONSES &&
|
||||||
const stallTimeoutMs = PROVIDERS[provider]?.stallTimeoutMs || STREAM_STALL_TIMEOUT_MS;
|
targetFormat === FORMATS.OPENAI_RESPONSES;
|
||||||
const transformedBody = pipeWithDisconnect(providerResponse, transformStream, streamController, onAbortTerminal, stallTimeoutMs);
|
const onAbortTerminal = isResponsesPassthrough
|
||||||
|
? buildAbortedResponsesTerminalBytes
|
||||||
|
: null;
|
||||||
|
const stallTimeoutMs =
|
||||||
|
PROVIDERS[provider]?.stallTimeoutMs || STREAM_STALL_TIMEOUT_MS;
|
||||||
|
const transformedBody = pipeWithDisconnect(
|
||||||
|
providerResponse,
|
||||||
|
transformStream,
|
||||||
|
streamController,
|
||||||
|
onAbortTerminal,
|
||||||
|
stallTimeoutMs,
|
||||||
|
);
|
||||||
|
|
||||||
saveRequestDetail(buildRequestDetail({
|
saveRequestDetail(
|
||||||
provider, model, connectionId,
|
buildRequestDetail(
|
||||||
|
{
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
latency: { ttft: 0, total: Date.now() - requestStartTime },
|
latency: { ttft: 0, total: Date.now() - requestStartTime },
|
||||||
tokens: { prompt_tokens: 0, completion_tokens: 0 },
|
tokens: { prompt_tokens: 0, completion_tokens: 0 },
|
||||||
request: extractRequestConfig(body, stream),
|
request: extractRequestConfig(body, stream),
|
||||||
providerRequest: finalBody || translatedBody || null,
|
providerRequest: finalBody || translatedBody || null,
|
||||||
providerResponse: "[Streaming - raw response not captured]",
|
providerResponse: "[Streaming - raw response not captured]",
|
||||||
response: { content: "[Streaming in progress...]", thinking: null, type: "streaming" },
|
response: {
|
||||||
|
content: "[Streaming in progress...]",
|
||||||
|
thinking: null,
|
||||||
|
type: "streaming",
|
||||||
|
},
|
||||||
pxpipe,
|
pxpipe,
|
||||||
status: "success"
|
status: "success",
|
||||||
}, { id: streamDetailId })).catch(err => {
|
},
|
||||||
console.error("[RequestDetail] Failed to save streaming request:", err.message);
|
{ id: streamDetailId },
|
||||||
|
),
|
||||||
|
).catch((err) => {
|
||||||
|
console.error(
|
||||||
|
"[RequestDetail] Failed to save streaming request:",
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
response: new Response(transformedBody, { headers: SSE_HEADERS })
|
response: new Response(transformedBody, { headers: SSE_HEADERS }),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build onStreamComplete callback for streaming usage tracking.
|
* Build onStreamComplete callback for streaming usage tracking.
|
||||||
*/
|
*/
|
||||||
export function buildOnStreamComplete({ provider, model, connectionId, apiKey, requestStartTime, body, stream, finalBody, translatedBody, clientRawRequest, pxpipe, reqTag, log, streamErrorPatterns }) {
|
export function buildOnStreamComplete({
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
requestStartTime,
|
||||||
|
body,
|
||||||
|
stream,
|
||||||
|
finalBody,
|
||||||
|
translatedBody,
|
||||||
|
clientRawRequest,
|
||||||
|
pxpipe,
|
||||||
|
reqTag,
|
||||||
|
log,
|
||||||
|
streamErrorPatterns,
|
||||||
|
}) {
|
||||||
const streamDetailId = `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
const streamDetailId = `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
||||||
|
|
||||||
const onStreamComplete = (contentObj, usage, ttftAt) => {
|
const onStreamComplete = (contentObj, usage, ttftAt) => {
|
||||||
const latency = {
|
const latency = {
|
||||||
ttft: ttftAt ? ttftAt - requestStartTime : Date.now() - requestStartTime,
|
ttft: ttftAt ? ttftAt - requestStartTime : Date.now() - requestStartTime,
|
||||||
total: Date.now() - requestStartTime
|
total: Date.now() - requestStartTime,
|
||||||
};
|
};
|
||||||
const safeContent = contentObj?.content || "[Empty streaming response]";
|
const safeContent = contentObj?.content || "[Empty streaming response]";
|
||||||
const safeThinking = contentObj?.thinking || null;
|
const safeThinking = contentObj?.thinking || null;
|
||||||
|
|
||||||
saveRequestDetail(buildRequestDetail({
|
saveRequestDetail(
|
||||||
provider, model, connectionId,
|
buildRequestDetail(
|
||||||
|
{
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
connectionId,
|
||||||
latency,
|
latency,
|
||||||
tokens: usage || { prompt_tokens: 0, completion_tokens: 0 },
|
tokens: usage || { prompt_tokens: 0, completion_tokens: 0 },
|
||||||
request: extractRequestConfig(body, stream),
|
request: extractRequestConfig(body, stream),
|
||||||
providerRequest: finalBody || translatedBody || null,
|
providerRequest: finalBody || translatedBody || null,
|
||||||
providerResponse: safeContent,
|
providerResponse: safeContent,
|
||||||
response: { content: safeContent, thinking: safeThinking, type: "streaming" },
|
response: {
|
||||||
|
content: safeContent,
|
||||||
|
thinking: safeThinking,
|
||||||
|
type: "streaming",
|
||||||
|
},
|
||||||
pxpipe,
|
pxpipe,
|
||||||
status: streamStatusForContent(streamErrorPatterns?.[provider], safeContent)
|
status: streamStatusForContent(
|
||||||
}, { id: streamDetailId })).catch(err => {
|
streamErrorPatterns?.[provider],
|
||||||
console.error("[RequestDetail] Failed to update streaming content:", err.message);
|
safeContent,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{ id: streamDetailId },
|
||||||
|
),
|
||||||
|
).catch((err) => {
|
||||||
|
console.error(
|
||||||
|
"[RequestDetail] Failed to update streaming content:",
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Persist stream usage to DB (no console line; the "📊 done" line below is authoritative)
|
// Persist stream usage to DB (no console line; the "📊 done" line below is authoritative)
|
||||||
saveUsageStats({ provider, model, tokens: usage, connectionId, apiKey, endpoint: clientRawRequest?.endpoint, label: "STREAM USAGE", silent: true });
|
saveUsageStats({
|
||||||
|
provider,
|
||||||
|
model,
|
||||||
|
tokens: usage,
|
||||||
|
connectionId,
|
||||||
|
apiKey,
|
||||||
|
endpoint: clientRawRequest?.endpoint,
|
||||||
|
label: "STREAM USAGE",
|
||||||
|
silent: true,
|
||||||
|
});
|
||||||
if (log?.line) log.line(reqTag, "📊", formatDoneLine({ usage, latency }));
|
if (log?.line) log.line(reqTag, "📊", formatDoneLine({ usage, latency }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -447,9 +447,7 @@ export default function ProviderDetailPage() {
|
|||||||
);
|
);
|
||||||
// Load per-provider stream error patterns (one per line)
|
// Load per-provider stream error patterns (one per line)
|
||||||
setStreamErrorPatternsText(
|
setStreamErrorPatternsText(
|
||||||
((settingsData.streamErrorPatterns || {})[providerId] || []).join(
|
((settingsData.streamErrorPatterns || {})[providerId] || []).join("\n"),
|
||||||
"\n",
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
const autoPingSettingsKey = AUTO_PING_SETTINGS_KEYS[providerId];
|
const autoPingSettingsKey = AUTO_PING_SETTINGS_KEYS[providerId];
|
||||||
const apCfg = autoPingSettingsKey
|
const apCfg = autoPingSettingsKey
|
||||||
@@ -2495,9 +2493,9 @@ export default function ProviderDetailPage() {
|
|||||||
<div className="mb-3">
|
<div className="mb-3">
|
||||||
<h2 className="text-lg font-semibold">Stream Error Patterns</h2>
|
<h2 className="text-lg font-semibold">Stream Error Patterns</h2>
|
||||||
<p className="mt-1 text-sm text-text-muted">
|
<p className="mt-1 text-sm text-text-muted">
|
||||||
Treat HTTP-200 streams whose first bytes match one of these
|
Treat HTTP-200 streams whose first bytes match one of these patterns
|
||||||
patterns as failed requests (enables fallback + FAILED logs). One
|
as failed requests (enables fallback + FAILED logs). One pattern per
|
||||||
pattern per line: plain text (case-insensitive substring) or{" "}
|
line: plain text (case-insensitive substring) or{" "}
|
||||||
<code className="rounded bg-background px-1 py-0.5 font-mono text-xs">
|
<code className="rounded bg-background px-1 py-0.5 font-mono text-xs">
|
||||||
/regex/flags
|
/regex/flags
|
||||||
</code>
|
</code>
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ import { getAdapter } from "../driver.js";
|
|||||||
import { parseJson, stringifyJson } from "../helpers/jsonCol.js";
|
import { parseJson, stringifyJson } from "../helpers/jsonCol.js";
|
||||||
|
|
||||||
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
|
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
|
||||||
const DEFAULT_HEADROOM_URL = process.env.HEADROOM_URL || "http://localhost:8787";
|
const DEFAULT_HEADROOM_URL =
|
||||||
|
process.env.HEADROOM_URL || "http://localhost:8787";
|
||||||
|
|
||||||
const DEFAULT_SETTINGS = {
|
const DEFAULT_SETTINGS = {
|
||||||
cloudEnabled: false,
|
cloudEnabled: false,
|
||||||
@@ -92,7 +93,7 @@ export async function updateSettings(updates) {
|
|||||||
next = { ...current, ...updates };
|
next = { ...current, ...updates };
|
||||||
db.run(
|
db.run(
|
||||||
`INSERT INTO settings(id, data) VALUES(1, ?) ON CONFLICT(id) DO UPDATE SET data = excluded.data`,
|
`INSERT INTO settings(id, data) VALUES(1, ?) ON CONFLICT(id) DO UPDATE SET data = excluded.data`,
|
||||||
[stringifyJson(next)]
|
[stringifyJson(next)],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
return mergeWithDefaults(next);
|
return mergeWithDefaults(next);
|
||||||
|
|||||||
@@ -115,7 +115,10 @@ describe("commandcode executor — early-error peek", () => {
|
|||||||
c.close();
|
c.close();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const res = await peekForUpstreamError(new Response(body, { status: 200 }), "m");
|
const res = await peekForUpstreamError(
|
||||||
|
new Response(body, { status: 200 }),
|
||||||
|
"m",
|
||||||
|
);
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
expect(text).toContain("café");
|
expect(text).toContain("café");
|
||||||
expect(text).not.toContain("\uFFFD");
|
expect(text).not.toContain("\uFFFD");
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ const encoder = new TextEncoder();
|
|||||||
const sseResponse = (chunks) => {
|
const sseResponse = (chunks) => {
|
||||||
const body = new ReadableStream({
|
const body = new ReadableStream({
|
||||||
start(c) {
|
start(c) {
|
||||||
for (const ch of chunks) c.enqueue(encoder.encode(`data: ${JSON.stringify(ch)}\n\n`));
|
for (const ch of chunks)
|
||||||
|
c.enqueue(encoder.encode(`data: ${JSON.stringify(ch)}\n\n`));
|
||||||
c.enqueue(encoder.encode("data: [DONE]\n\n"));
|
c.enqueue(encoder.encode("data: [DONE]\n\n"));
|
||||||
c.close();
|
c.close();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } });
|
return new Response(body, {
|
||||||
|
status: 200,
|
||||||
|
headers: { "content-type": "text/event-stream" },
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const mkChunk = (content, finish = null) => ({
|
const mkChunk = (content, finish = null) => ({
|
||||||
@@ -18,7 +22,9 @@ const mkChunk = (content, finish = null) => ({
|
|||||||
object: "chat.completion.chunk",
|
object: "chat.completion.chunk",
|
||||||
created: 1,
|
created: 1,
|
||||||
model: "m",
|
model: "m",
|
||||||
choices: [{ index: 0, delta: content ? { content } : {}, finish_reason: finish }],
|
choices: [
|
||||||
|
{ index: 0, delta: content ? { content } : {}, finish_reason: finish },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const baseCtx = {
|
const baseCtx = {
|
||||||
@@ -48,7 +54,10 @@ describe("Layer 1 — non-streaming stream error patterns", () => {
|
|||||||
const result = await handleForcedSSEToJson({
|
const result = await handleForcedSSEToJson({
|
||||||
...baseCtx,
|
...baseCtx,
|
||||||
streamErrorPatterns: { fakeprovider: ["Network connection lost"] },
|
streamErrorPatterns: { fakeprovider: ["Network connection lost"] },
|
||||||
providerResponse: sseResponse([mkChunk("Network connection lost."), mkChunk(null, "stop")]),
|
providerResponse: sseResponse([
|
||||||
|
mkChunk("Network connection lost."),
|
||||||
|
mkChunk(null, "stop"),
|
||||||
|
]),
|
||||||
});
|
});
|
||||||
expect(result.success).toBe(false);
|
expect(result.success).toBe(false);
|
||||||
expect(result.status).toBe(502);
|
expect(result.status).toBe(502);
|
||||||
@@ -59,7 +68,10 @@ describe("Layer 1 — non-streaming stream error patterns", () => {
|
|||||||
const result = await handleForcedSSEToJson({
|
const result = await handleForcedSSEToJson({
|
||||||
...baseCtx,
|
...baseCtx,
|
||||||
streamErrorPatterns: { fakeprovider: ["Network connection lost"] },
|
streamErrorPatterns: { fakeprovider: ["Network connection lost"] },
|
||||||
providerResponse: sseResponse([mkChunk("hello world"), mkChunk(null, "stop")]),
|
providerResponse: sseResponse([
|
||||||
|
mkChunk("hello world"),
|
||||||
|
mkChunk(null, "stop"),
|
||||||
|
]),
|
||||||
});
|
});
|
||||||
expect(result.success).toBe(true);
|
expect(result.success).toBe(true);
|
||||||
});
|
});
|
||||||
@@ -68,7 +80,10 @@ describe("Layer 1 — non-streaming stream error patterns", () => {
|
|||||||
const result = await handleForcedSSEToJson({
|
const result = await handleForcedSSEToJson({
|
||||||
...baseCtx,
|
...baseCtx,
|
||||||
streamErrorPatterns: { otherprovider: ["hello"] },
|
streamErrorPatterns: { otherprovider: ["hello"] },
|
||||||
providerResponse: sseResponse([mkChunk("hello world"), mkChunk(null, "stop")]),
|
providerResponse: sseResponse([
|
||||||
|
mkChunk("hello world"),
|
||||||
|
mkChunk(null, "stop"),
|
||||||
|
]),
|
||||||
});
|
});
|
||||||
expect(result.success).toBe(true);
|
expect(result.success).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,19 +1,39 @@
|
|||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect } from "vitest";
|
||||||
import { parsePatterns, matchStreamErrorPatterns, streamStatusForContent } from "../../open-sse/utils/streamErrorPatterns.js";
|
import {
|
||||||
|
parsePatterns,
|
||||||
|
matchStreamErrorPatterns,
|
||||||
|
streamStatusForContent,
|
||||||
|
} from "../../open-sse/utils/streamErrorPatterns.js";
|
||||||
|
|
||||||
describe("streamErrorPatterns util", () => {
|
describe("streamErrorPatterns util", () => {
|
||||||
it("matches plain text as case-insensitive substring", () => {
|
it("matches plain text as case-insensitive substring", () => {
|
||||||
expect(matchStreamErrorPatterns(["Network connection lost"], "network CONNECTION LOST.")).toBe("Network connection lost");
|
expect(
|
||||||
expect(matchStreamErrorPatterns(["server_error"], "some normal content")).toBeNull();
|
matchStreamErrorPatterns(
|
||||||
|
["Network connection lost"],
|
||||||
|
"network CONNECTION LOST.",
|
||||||
|
),
|
||||||
|
).toBe("Network connection lost");
|
||||||
|
expect(
|
||||||
|
matchStreamErrorPatterns(["server_error"], "some normal content"),
|
||||||
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("matches /regex/flags", () => {
|
it("matches /regex/flags", () => {
|
||||||
expect(matchStreamErrorPatterns(["/generation failed.*retry/i"], "GENERATION FAILED. please RETRY")).toBe("/generation failed.*retry/i");
|
expect(
|
||||||
expect(matchStreamErrorPatterns(["/\\d+ tokens/"], "used 123 tokens")).toBe("/\\d+ tokens/");
|
matchStreamErrorPatterns(
|
||||||
|
["/generation failed.*retry/i"],
|
||||||
|
"GENERATION FAILED. please RETRY",
|
||||||
|
),
|
||||||
|
).toBe("/generation failed.*retry/i");
|
||||||
|
expect(matchStreamErrorPatterns(["/\\d+ tokens/"], "used 123 tokens")).toBe(
|
||||||
|
"/\\d+ tokens/",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips invalid regex and empty entries", () => {
|
it("skips invalid regex and empty entries", () => {
|
||||||
expect(matchStreamErrorPatterns(["/[unclosed/", "", " "], "anything")).toBeNull();
|
expect(
|
||||||
|
matchStreamErrorPatterns(["/[unclosed/", "", " "], "anything"),
|
||||||
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns null for empty patterns or text", () => {
|
it("returns null for empty patterns or text", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user