Enhance chat handling.

This commit is contained in:
decolua
2026-01-14 15:42:38 +07:00
parent c39eca6d4e
commit c208f244ee
7 changed files with 145 additions and 137 deletions

View File

@@ -63,13 +63,13 @@ function extractUsageFromResponse(responseBody, provider) {
* @param {function} options.onDisconnect - Callback when client disconnects
* @param {string} options.connectionId - Connection ID for usage tracking
*/
export async function handleChatCore({ body, modelInfo, credentials, log, onCredentialsRefreshed, onRequestSuccess, onDisconnect, clientRawRequest, connectionId }) {
export async function handleChatCore({ body, modelInfo, credentials, log, onCredentialsRefreshed, onRequestSuccess, onDisconnect, clientRawRequest, connectionId, userAgent }) {
const { provider, model } = modelInfo;
const sourceFormat = detectFormat(body);
// Check for bypass patterns (warmup, skip) - return fake response
const bypassResponse = handleBypassRequest(body, model);
const bypassResponse = handleBypassRequest(body, model, userAgent);
if (bypassResponse) {
return bypassResponse;
}
@@ -173,17 +173,6 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
return createErrorResult(502, errMsg);
}
// Log headers (mask sensitive values)
const safeHeaders = {};
for (const [key, value] of Object.entries(providerHeaders || {})) {
if (key.toLowerCase().includes("auth") || key.toLowerCase().includes("key") || key.toLowerCase().includes("token")) {
safeHeaders[key] = value ? `${value.slice(0, 10)}...` : "";
} else {
safeHeaders[key] = value;
}
}
log?.debug?.("HEADERS", JSON.stringify(safeHeaders));
// Handle 401/403 - try token refresh using executor
if (providerResponse.status === 401 || providerResponse.status === 403) {
const newCredentials = await refreshWithRetry(

View File

@@ -5,19 +5,14 @@ import { SKIP_PATTERNS } from "../config/constants.js";
import { formatSSE } from "./stream.js";
/**
* Check for bypass patterns (warmup, skip) - return fake response without calling provider
* Supports both streaming and non-streaming responses
* Returns response in the correct sourceFormat using translator
*
* @param {object} body - Request body
* @param {string} model - Model name
* @returns {object|null} { success: true, response: Response } or null if not bypass
* Check for bypass patterns - return fake response without calling provider
* Only works for Claude CLI requests
*/
export function handleBypassRequest(body, model) {
const messages = body.messages;
if (!messages?.length) return null;
export function handleBypassRequest(body, model, userAgent = "") {
if (!userAgent.includes("claude-cli")) return null;
if (!body.messages?.length) return null;
// Helper to extract text from content
const messages = body.messages;
const getText = (content) => {
if (typeof content === "string") return content;
if (Array.isArray(content)) {
@@ -28,43 +23,45 @@ export function handleBypassRequest(body, model) {
let shouldBypass = false;
// Check warmup: first message "Warmup"
const firstText = getText(messages[0]?.content);
if (firstText === "Warmup") {
// Pattern 1: Title extraction (assistant message = "{")
const lastMsg = messages[messages.length - 1];
if (lastMsg?.role === "assistant" && lastMsg.content?.[0]?.text === "{") {
shouldBypass = true;
}
// Check count pattern: [{"role":"user","content":"count"}]
// if (!shouldBypass &&
// messages.length === 1 &&
// messages[0]?.role === "user" &&
// firstText === "count") {
// shouldBypass = true;
// }
// Pattern 2: Warmup
if (!shouldBypass) {
const firstText = getText(messages[0]?.content);
if (firstText === "Warmup") {
shouldBypass = true;
}
}
// Check skip patterns - only check user messages, not system prompt
// Pattern 3: Count
if (!shouldBypass && messages.length === 1 && messages[0]?.role === "user") {
const firstText = getText(messages[0]?.content);
if (firstText === "count") {
shouldBypass = true;
}
}
// Pattern 4: Skip patterns
if (!shouldBypass && SKIP_PATTERNS?.length) {
// Only check user messages, skip system/assistant messages to avoid matching system prompts
const userMessages = messages.filter(m => m.role === "user");
const userText = userMessages.map(m => getText(m.content)).join(" ");
const matchedPattern = SKIP_PATTERNS.find(p => userText.includes(p));
if (matchedPattern) {
if (SKIP_PATTERNS.some(p => userText.includes(p))) {
shouldBypass = true;
}
}
if (!shouldBypass) return null;
// Detect source format and stream mode
const sourceFormat = detectFormat(body);
const stream = body.stream !== false;
// Create bypass response using translator
if (stream) {
return createStreamingResponse(sourceFormat, model);
} else {
return createNonStreamingResponse(sourceFormat, model);
}
return stream
? createStreamingResponse(sourceFormat, model)
: createNonStreamingResponse(sourceFormat, model);
}
/**
@@ -102,7 +99,7 @@ function createOpenAIResponse(model) {
*/
function createNonStreamingResponse(sourceFormat, model) {
const openaiResponse = createOpenAIResponse(model);
// If sourceFormat is OpenAI, return directly
if (sourceFormat === FORMATS.OPENAI) {
return {
@@ -119,26 +116,26 @@ function createNonStreamingResponse(sourceFormat, model) {
// Use translator to convert: simulate streaming then collect all chunks
const state = initState(sourceFormat);
state.model = model;
const openaiChunks = createOpenAIStreamingChunks(openaiResponse);
const allTranslated = [];
for (const chunk of openaiChunks) {
const translated = translateResponse(FORMATS.OPENAI, sourceFormat, chunk, state);
if (translated?.length > 0) {
allTranslated.push(...translated);
}
}
// Flush remaining
const flushed = translateResponse(FORMATS.OPENAI, sourceFormat, null, state);
if (flushed?.length > 0) {
allTranslated.push(...flushed);
}
// For non-streaming, merge all chunks into final response
const finalResponse = mergeChunksToResponse(allTranslated, sourceFormat);
return {
success: true,
response: new Response(JSON.stringify(finalResponse), {
@@ -164,7 +161,7 @@ function createStreamingResponse(sourceFormat, model) {
// Translate each chunk to sourceFormat using translator
const translatedChunks = [];
for (const chunk of openaiChunks) {
const translated = translateResponse(FORMATS.OPENAI, sourceFormat, chunk, state);
if (translated?.length > 0) {
@@ -206,11 +203,11 @@ function mergeChunksToResponse(chunks, sourceFormat) {
if (!chunks || chunks.length === 0) {
return createOpenAIResponse("unknown");
}
// For most formats, the last chunk before done contains the complete response
// Find the most complete chunk (usually the last one with content)
let finalChunk = chunks[chunks.length - 1];
// For Claude format, find the message_stop or final message
if (sourceFormat === FORMATS.CLAUDE) {
const messageStop = chunks.find(c => c.type === "message_stop");
@@ -219,7 +216,7 @@ function mergeChunksToResponse(chunks, sourceFormat) {
const contentDelta = chunks.find(c => c.type === "content_block_delta");
const messageDelta = chunks.find(c => c.type === "message_delta");
const messageStart = chunks.find(c => c.type === "message_start");
if (messageStart?.message) {
finalChunk = messageStart.message;
// Merge usage if available
@@ -229,7 +226,7 @@ function mergeChunksToResponse(chunks, sourceFormat) {
}
}
}
return finalChunk;
}

View File

@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { Card, Button, ModelSelectModal } from "@/shared/components";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
const CLOUD_URL = process.env.NEXT_PUBLIC_CLOUD_URL;
@@ -27,8 +27,8 @@ export default function ClaudeToolCard({
const [modalOpen, setModalOpen] = useState(false);
const [currentEditingAlias, setCurrentEditingAlias] = useState(null);
const [selectedApiKey, setSelectedApiKey] = useState("");
const [copiedConfig, setCopiedConfig] = useState(false);
const [modelAliases, setModelAliases] = useState({});
const [showManualConfigModal, setShowManualConfigModal] = useState(false);
const getConfigStatus = () => {
if (!claudeStatus?.installed) return null;
@@ -163,7 +163,7 @@ export default function ClaudeToolCard({
};
// Generate settings.json content for manual copy
const getSettingsContent = () => {
const getManualConfigs = () => {
const keyToUse = (selectedApiKey && selectedApiKey.trim())
? selectedApiKey
: (!cloudEnabled ? "sk_9router" : "<API_KEY_FROM_DASHBOARD>");
@@ -172,17 +172,13 @@ export default function ClaudeToolCard({
const targetModel = modelMappings[model.alias];
if (targetModel && model.envKey) env[model.envKey] = targetModel;
});
return JSON.stringify({ env }, null, 2);
};
const copyToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text);
setCopiedConfig(true);
setTimeout(() => setCopiedConfig(false), 2000);
} catch (err) {
console.log("Failed to copy:", err);
}
return [
{
filename: "~/.claude/settings.json",
content: JSON.stringify({ env }, null, 2),
},
];
};
return (
@@ -289,31 +285,23 @@ export default function ClaudeToolCard({
<Button variant="outline" size="sm" onClick={handleResetSettings} disabled={!claudeStatus?.has9Router} loading={restoring}>
<span className="material-symbols-outlined text-[14px] mr-1">restore</span>Reset
</Button>
<Button variant="ghost" size="sm" onClick={checkClaudeStatus}>
<span className="material-symbols-outlined text-[14px]">refresh</span>
<Button variant="ghost" size="sm" onClick={() => setShowManualConfigModal(true)}>
<span className="material-symbols-outlined text-[14px] mr-1">content_copy</span>Manual Config
</Button>
</div>
{/* Manual Config Section */}
<div className="pt-4 border-t border-border flex flex-col gap-3">
<p className="text-xs text-text-muted">Or copy config manually:</p>
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-text-main">~/.claude/settings.json</span>
<Button variant="ghost" size="sm" onClick={() => copyToClipboard(getSettingsContent())}>
<span className="material-symbols-outlined text-[14px] mr-1">{copiedConfig ? "check" : "content_copy"}</span>
{copiedConfig ? "Copied!" : "Copy"}
</Button>
</div>
<pre className="px-3 py-2 bg-black/5 dark:bg-white/5 rounded font-mono text-xs overflow-x-auto whitespace-pre-wrap break-all max-h-40 overflow-y-auto">{getSettingsContent()}</pre>
</div>
</div>
</>
)}
</div>
)}
<ModelSelectModal isOpen={modalOpen} onClose={() => setModalOpen(false)} onSelect={handleModelSelect} selectedModel={currentEditingAlias ? modelMappings[currentEditingAlias] : null} activeProviders={activeProviders} modelAliases={modelAliases} title={`Select model for ${currentEditingAlias}`} />
<ManualConfigModal
isOpen={showManualConfigModal}
onClose={() => setShowManualConfigModal(false)}
title="Claude CLI - Manual Configuration"
configs={getManualConfigs()}
/>
</Card>
);
}

View File

@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { Card, Button, ModelSelectModal } from "@/shared/components";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import Image from "next/image";
export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, apiKeys, activeProviders, cloudEnabled }) {
@@ -13,9 +13,9 @@ export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, api
const [showInstallGuide, setShowInstallGuide] = useState(false);
const [selectedApiKey, setSelectedApiKey] = useState("");
const [selectedModel, setSelectedModel] = useState("");
const [copiedConfig, setCopiedConfig] = useState(false);
const [modalOpen, setModalOpen] = useState(false);
const [modelAliases, setModelAliases] = useState({});
const [showManualConfigModal, setShowManualConfigModal] = useState(false);
useEffect(() => {
if (apiKeys?.length > 0 && !selectedApiKey) {
@@ -123,7 +123,12 @@ export default function CodexToolCard({ tool, isExpanded, onToggle, baseUrl, api
setModalOpen(false);
};
const configContent = `# 9Router Configuration for Codex CLI
const getManualConfigs = () => {
const keyToUse = (selectedApiKey && selectedApiKey.trim())
? selectedApiKey
: (!cloudEnabled ? "sk_9router" : "<API_KEY_FROM_DASHBOARD>");
const configContent = `# 9Router Configuration for Codex CLI
model = "${selectedModel}"
model_provider = "9router"
@@ -133,22 +138,20 @@ base_url = "${baseUrl}/v1"
wire_api = "responses"
`;
const keyToUse = (selectedApiKey && selectedApiKey.trim())
? selectedApiKey
: (!cloudEnabled ? "sk_9router" : "<API_KEY_FROM_DASHBOARD>");
const authContent = JSON.stringify({
OPENAI_API_KEY: keyToUse
}, null, 2);
const authContent = JSON.stringify({
OPENAI_API_KEY: keyToUse
}, null, 2);
const copyToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text);
setCopiedConfig(true);
setTimeout(() => setCopiedConfig(false), 2000);
} catch (err) {
console.log("Failed to copy:", err);
}
return [
{
filename: "~/.codex/config.toml",
content: configContent,
},
{
filename: "~/.codex/auth.json",
content: authContent,
},
];
};
return (
@@ -257,39 +260,12 @@ wire_api = "responses"
<Button variant="outline" size="sm" onClick={handleResetSettings} disabled={!codexStatus.has9Router} loading={restoring}>
<span className="material-symbols-outlined text-[14px] mr-1">restore</span>Reset
</Button>
<Button variant="ghost" size="sm" onClick={checkCodexStatus}>
<span className="material-symbols-outlined text-[14px]">refresh</span>
<Button variant="ghost" size="sm" onClick={() => setShowManualConfigModal(true)}>
<span className="material-symbols-outlined text-[14px] mr-1">content_copy</span>Manual Config
</Button>
</div>
</>
)}
{/* Manual Config Section */}
<div className="pt-4 border-t border-border flex flex-col gap-3">
<p className="text-xs text-text-muted">Or copy config manually:</p>
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-text-main">~/.codex/config.toml</span>
<Button variant="ghost" size="sm" onClick={() => copyToClipboard(configContent)}>
<span className="material-symbols-outlined text-[14px] mr-1">{copiedConfig ? "check" : "content_copy"}</span>
{copiedConfig ? "Copied!" : "Copy"}
</Button>
</div>
<pre className="px-3 py-2 bg-black/5 dark:bg-white/5 rounded font-mono text-xs overflow-x-auto whitespace-pre-wrap break-all max-h-32 overflow-y-auto">{configContent}</pre>
</div>
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-text-main">~/.codex/auth.json</span>
<Button variant="ghost" size="sm" onClick={() => copyToClipboard(authContent)}>
<span className="material-symbols-outlined text-[14px] mr-1">{copiedConfig ? "check" : "content_copy"}</span>
{copiedConfig ? "Copied!" : "Copy"}
</Button>
</div>
<pre className="px-3 py-2 bg-black/5 dark:bg-white/5 rounded font-mono text-xs overflow-x-auto whitespace-pre-wrap break-all max-h-32 overflow-y-auto">{authContent}</pre>
</div>
</div>
</div>
)}
@@ -302,6 +278,13 @@ wire_api = "responses"
modelAliases={modelAliases}
title="Select Model for Codex"
/>
<ManualConfigModal
isOpen={showManualConfigModal}
onClose={() => setShowManualConfigModal(false)}
title="Codex CLI - Manual Configuration"
configs={getManualConfigs()}
/>
</Card>
);
}

View File

@@ -0,0 +1,46 @@
"use client";
import { useState } from "react";
import Modal from "./Modal";
import Button from "./Button";
export default function ManualConfigModal({ isOpen, onClose, title = "Manual Configuration", configs = [] }) {
const [copiedIndex, setCopiedIndex] = useState(null);
const copyToClipboard = async (text, index) => {
try {
await navigator.clipboard.writeText(text);
setCopiedIndex(index);
setTimeout(() => setCopiedIndex(null), 2000);
} catch (err) {
console.log("Failed to copy:", err);
}
};
return (
<Modal isOpen={isOpen} onClose={onClose} title={title} size="xl">
<div className="flex flex-col gap-4">
{configs.map((config, index) => (
<div key={index} className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-sm font-medium text-text-main">{config.filename}</span>
<Button
variant="ghost"
size="sm"
onClick={() => copyToClipboard(config.content, index)}
>
<span className="material-symbols-outlined text-[14px] mr-1">
{copiedIndex === index ? "check" : "content_copy"}
</span>
{copiedIndex === index ? "Copied!" : "Copy"}
</Button>
</div>
<pre className="px-3 py-2 bg-black/5 dark:bg-white/5 rounded font-mono text-xs overflow-x-auto whitespace-pre-wrap break-all max-h-60 overflow-y-auto border border-border">
{config.content}
</pre>
</div>
))}
</div>
</Modal>
);
}

View File

@@ -15,6 +15,7 @@ export { default as Header } from "./Header";
export { default as Footer } from "./Footer";
export { default as OAuthModal } from "./OAuthModal";
export { default as ModelSelectModal } from "./ModelSelectModal";
export { default as ManualConfigModal } from "./ManualConfigModal";
export { default as UsageStats } from "./UsageStats";
export { default as RequestLogger } from "./RequestLogger";

View File

@@ -61,19 +61,19 @@ export async function handleChat(request, clientRawRequest = null) {
return handleComboChat({
body,
models: comboModels,
handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest),
handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest, request),
log
});
}
// Single model request
return handleSingleModelChat(body, modelStr, clientRawRequest);
return handleSingleModelChat(body, modelStr, clientRawRequest, request);
}
/**
* Handle single model chat request
*/
async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
async function handleSingleModelChat(body, modelStr, clientRawRequest = null, request = null) {
const modelInfo = await getModelInfo(modelStr);
if (!modelInfo.provider) {
log.warn("CHAT", "Invalid model format", { model: modelStr });
@@ -89,6 +89,9 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
log.info("ROUTING", `Provider: ${provider}, Model: ${model}`);
}
// Extract userAgent from request
const userAgent = request?.headers?.get("user-agent") || "";
// Try with available accounts (fallback on errors)
let excludeConnectionId = null;
let lastError = null;
@@ -121,6 +124,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
log,
clientRawRequest,
connectionId: credentials.connectionId,
userAgent,
onCredentialsRefreshed: async (newCreds) => {
await updateProviderCredentials(credentials.connectionId, {
accessToken: newCreds.accessToken,