feat(chat): Enhance bypass handling and introduce CC filter naming feature

Fix : Ollam Provider response
This commit is contained in:
decolua
2026-03-13 09:41:40 +07:00
parent 4b5111974a
commit 373b10ebb5
7 changed files with 94 additions and 12 deletions

View File

@@ -23,14 +23,14 @@ import { handleStreamingResponse, buildOnStreamComplete } from "./chatCore/strea
* @param {object} options.credentials - Provider credentials
* @param {string} options.sourceFormatOverride - Override detected source format (e.g. "openai-responses")
*/
export async function handleChatCore({ body, modelInfo, credentials, log, onCredentialsRefreshed, onRequestSuccess, onDisconnect, clientRawRequest, connectionId, userAgent, apiKey, sourceFormatOverride }) {
export async function handleChatCore({ body, modelInfo, credentials, log, onCredentialsRefreshed, onRequestSuccess, onDisconnect, clientRawRequest, connectionId, userAgent, apiKey, ccFilterNaming, sourceFormatOverride }) {
const { provider, model } = modelInfo;
const requestStartTime = Date.now();
const sourceFormat = sourceFormatOverride || detectFormat(body);
// Check for bypass patterns (warmup, skip)
const bypassResponse = handleBypassRequest(body, model, userAgent);
// Check for bypass patterns (warmup, skip, cc naming)
const bypassResponse = handleBypassRequest(body, model, userAgent, ccFilterNaming);
if (bypassResponse) return bypassResponse;
const alias = PROVIDER_ID_TO_ALIAS[provider] || provider;

View File

@@ -8,7 +8,7 @@ import { formatSSE } from "./stream.js";
* Check for bypass patterns - return fake response without calling provider
* Only works for Claude CLI requests
*/
export function handleBypassRequest(body, model, userAgent = "") {
export function handleBypassRequest(body, model, userAgent = "", ccFilterNaming = false) {
if (!userAgent.includes("claude-cli")) return null;
if (!body.messages?.length) return null;
@@ -22,6 +22,7 @@ export function handleBypassRequest(body, model, userAgent = "") {
};
let shouldBypass = false;
let namingBypass = false;
// Pattern 1: Title extraction (assistant message = "{")
const lastMsg = messages[messages.length - 1];
@@ -54,23 +55,50 @@ export function handleBypassRequest(body, model, userAgent = "") {
}
}
// Pattern 5: CC naming request (topic title extraction by Claude Code CLI)
// Claude format: system is top-level body.system field, not inside messages
if (!shouldBypass && ccFilterNaming) {
const systemMsg = messages.find(m => m.role === "system");
const systemFromMessages = getText(systemMsg?.content);
const systemFromBody = Array.isArray(body.system)
? body.system.filter(s => s.type === "text").map(s => s.text).join(" ")
: (typeof body.system === "string" ? body.system : "");
const systemText = systemFromMessages || systemFromBody;
if (systemText.includes("isNewTopic")) {
shouldBypass = true;
namingBypass = true;
}
}
if (!shouldBypass) return null;
const sourceFormat = detectFormat(body);
const stream = body.stream !== false;
// For naming bypass, generate title from user message
if (namingBypass) {
const userMsg = messages.find(m => m.role === "user");
const userText = getText(userMsg?.content);
const title = userText.trim().split(/\s+/).slice(0, 3).join(" ");
const namingText = JSON.stringify({ isNewTopic: true, title });
return stream
? createStreamingResponse(sourceFormat, model, namingText)
: createNonStreamingResponse(sourceFormat, model, namingText);
}
return stream
? createStreamingResponse(sourceFormat, model)
: createNonStreamingResponse(sourceFormat, model);
}
const DEFAULT_BYPASS_TEXT = "CLI Command Execution: Clear Terminal";
/**
* Create OpenAI standard format response
*/
function createOpenAIResponse(model) {
function createOpenAIResponse(model, text = DEFAULT_BYPASS_TEXT) {
const id = `chatcmpl-${Date.now()}`;
const created = Math.floor(Date.now() / 1000);
const text = "CLI Command Execution: Clear Terminal";
return {
id,
@@ -97,8 +125,8 @@ function createOpenAIResponse(model) {
* Create non-streaming response with translation
* Use translator to convert OpenAI → sourceFormat
*/
function createNonStreamingResponse(sourceFormat, model) {
const openaiResponse = createOpenAIResponse(model);
function createNonStreamingResponse(sourceFormat, model, text) {
const openaiResponse = createOpenAIResponse(model, text);
// If sourceFormat is OpenAI, return directly
if (sourceFormat === FORMATS.OPENAI) {
@@ -151,8 +179,8 @@ function createNonStreamingResponse(sourceFormat, model) {
* Create streaming response with translation
* Use translator to convert OpenAI chunks → sourceFormat
*/
function createStreamingResponse(sourceFormat, model) {
const openaiResponse = createOpenAIResponse(model);
function createStreamingResponse(sourceFormat, model, text) {
const openaiResponse = createOpenAIResponse(model, text);
const state = initState(sourceFormat);
state.model = model;

View File

@@ -162,7 +162,9 @@ export function createSSEStream(options = {}) {
const parsed = parseSSELine(trimmed, targetFormat);
if (!parsed) continue;
if (parsed && parsed.done) {
// For Ollama: done=true is the final chunk with finish_reason/usage, must translate
// For other formats: done=true is the [DONE] sentinel, skip
if (parsed && parsed.done && targetFormat !== FORMATS.OLLAMA) {
const output = "data: [DONE]\n\n";
reqLogger?.appendConvertedChunk?.(output);
controller.enqueue(sharedEncoder.encode(output));

View File

@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect, useRef } from "react";
import { Card, Button, ModelSelectModal, ManualConfigModal } from "@/shared/components";
import { Card, Button, ModelSelectModal, ManualConfigModal, Tooltip } from "@/shared/components";
import Image from "next/image";
const CLOUD_URL = process.env.NEXT_PUBLIC_CLOUD_URL;
@@ -31,6 +31,7 @@ export default function ClaudeToolCard({
const [modelAliases, setModelAliases] = useState({});
const [showManualConfigModal, setShowManualConfigModal] = useState(false);
const [customBaseUrl, setCustomBaseUrl] = useState("");
const [ccFilterNaming, setCcFilterNaming] = useState(false);
const hasInitializedModels = useRef(false);
const getConfigStatus = () => {
@@ -64,6 +65,22 @@ export default function ClaudeToolCard({
if (isExpanded) fetchModelAliases();
}, [isExpanded]);
useEffect(() => {
fetch("/api/settings").then(r => r.json()).then(data => {
setCcFilterNaming(!!data.ccFilterNaming);
}).catch(() => {});
}, []);
const handleCcFilterNamingToggle = async (e) => {
const value = e.target.checked;
setCcFilterNaming(value);
await fetch("/api/settings", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ccFilterNaming: value }),
}).catch(() => {});
};
const fetchModelAliases = async () => {
try {
const res = await fetch("/api/models/alias");
@@ -319,6 +336,19 @@ export default function ClaudeToolCard({
{modelMappings[model.alias] && <button onClick={() => onModelMappingChange(model.alias, "")} className="p-1 text-text-muted hover:text-red-500 rounded transition-colors" title="Clear"><span className="material-symbols-outlined text-[14px]">close</span></button>}
</div>
))}
{/* CC Filter Naming */}
<div className="flex items-center gap-2">
<span className="w-32 shrink-0 text-sm font-semibold text-text-main text-right">Filter naming</span>
<span className="material-symbols-outlined text-text-muted text-[14px]">arrow_forward</span>
<label className="flex items-center gap-1.5 cursor-pointer select-none">
<input type="checkbox" checked={ccFilterNaming} onChange={handleCcFilterNamingToggle} className="w-3.5 h-3.5 accent-primary cursor-pointer" />
<span className="text-xs text-text-muted">Filter naming requests</span>
</label>
<Tooltip text="Intercepts Claude Code's topic-naming requests and returns a fake response locally, saving API tokens.">
<span className="material-symbols-outlined text-text-muted text-[14px] cursor-help">info</span>
</Tooltip>
</div>
</div>
{message && (

View File

@@ -0,0 +1,19 @@
"use client";
export default function Tooltip({ text, children, position = "top" }) {
const posClass = {
top: "bottom-full left-1/2 -translate-x-1/2 mb-1.5",
bottom: "top-full left-1/2 -translate-x-1/2 mt-1.5",
left: "right-full top-1/2 -translate-y-1/2 mr-1.5",
right: "left-full top-1/2 -translate-y-1/2 ml-1.5",
}[position];
return (
<div className="relative inline-flex group">
{children}
<div className={`pointer-events-none absolute ${posClass} z-50 w-max max-w-56 rounded px-2 py-1 text-[11px] leading-snug bg-gray-900 text-white opacity-0 group-hover:opacity-100 transition-opacity duration-150 whitespace-normal`}>
{text}
</div>
</div>
);
}

View File

@@ -25,6 +25,7 @@ export { default as KiroSocialOAuthModal } from "./KiroSocialOAuthModal";
export { default as CursorAuthModal } from "./CursorAuthModal";
export { default as IFlowCookieModal } from "./IFlowCookieModal";
export { default as SegmentedControl } from "./SegmentedControl";
export { default as Tooltip } from "./Tooltip";
// Layouts
export * from "./layouts";

View File

@@ -171,6 +171,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
}
// Use shared chatCore
const chatSettings = await getSettings();
const result = await handleChatCore({
body: { ...body, model: `${provider}/${model}` },
modelInfo: { provider, model },
@@ -180,6 +181,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
connectionId: credentials.connectionId,
userAgent,
apiKey,
ccFilterNaming: !!chatSettings.ccFilterNaming,
// Detect source format by endpoint + body
sourceFormatOverride: request?.url ? detectFormatByEndpoint(new URL(request.url).pathname, body) : null,
onCredentialsRefreshed: async (newCreds) => {