Compare commits
3 Commits
v0.5.1
...
fix/3p-pro
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02599e0b6f | ||
|
|
b09972f223 | ||
|
|
336ddcc50d |
@@ -1,3 +1,3 @@
|
||||
{
|
||||
".": "0.5.1"
|
||||
".": "0.5.2"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
## [0.5.2](https://github.com/Gitlawb/openclaude/compare/v0.5.1...v0.5.2) (2026-04-20)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **api:** replace phrase-based reasoning sanitizer with tag-based filter ([#779](https://github.com/Gitlawb/openclaude/issues/779)) ([336ddcc](https://github.com/Gitlawb/openclaude/commit/336ddcc50d59d79ebff50993f2673652aecb0d7d))
|
||||
|
||||
## [0.5.1](https://github.com/Gitlawb/openclaude/compare/v0.5.0...v0.5.1) (2026-04-20)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@gitlawb/openclaude",
|
||||
"version": "0.5.1",
|
||||
"version": "0.5.2",
|
||||
"description": "Claude Code opened to any LLM — OpenAI, Gemini, DeepSeek, Ollama, and 200+ models",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -21,11 +21,11 @@ describe('Gemini store field fix', () => {
|
||||
test('isGeminiMode is imported and used in openaiShim', async () => {
|
||||
const content = await file('services/api/openaiShim.ts').text()
|
||||
|
||||
// Verify the fix: store deletion should check for Gemini mode
|
||||
// Verify the fix: store deletion should check for Gemini mode and local providers
|
||||
expect(content).toContain('isGeminiMode()')
|
||||
expect(content).toContain("mistral and gemini don't recognize body.store")
|
||||
// Ensure the delete body.store is guarded for both Mistral and Gemini
|
||||
expect(content).toMatch(/isMistral\s*\|\|\s*isGeminiMode\(\)/)
|
||||
expect(content).toContain("Strip store for providers that don't recognize it")
|
||||
// Ensure the delete body.store is guarded for Mistral, Gemini, and local providers
|
||||
expect(content).toMatch(/isMistral\s*\|\|\s*isGeminiMode\(\)\s*\|\|\s*isLocal/)
|
||||
})
|
||||
|
||||
test('store: false is still set by default (OpenAI needs it)', async () => {
|
||||
|
||||
@@ -547,7 +547,7 @@ describe('Codex request translation', () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('strips leaked reasoning preamble from completed Codex text responses', () => {
|
||||
test('strips <think> tag block from completed Codex text responses', () => {
|
||||
const message = convertCodexResponseToAnthropicMessage(
|
||||
{
|
||||
id: 'resp_1',
|
||||
@@ -560,7 +560,7 @@ describe('Codex request translation', () => {
|
||||
{
|
||||
type: 'output_text',
|
||||
text:
|
||||
'The user just said "hey" - a simple greeting. I should respond briefly and friendly.\n\nHey! How can I help you today?',
|
||||
'<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?',
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -578,6 +578,37 @@ describe('Codex request translation', () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('strips unterminated <think> tag at block boundary in Codex completed response', () => {
|
||||
const message = convertCodexResponseToAnthropicMessage(
|
||||
{
|
||||
id: 'resp_1',
|
||||
model: 'gpt-5.4',
|
||||
output: [
|
||||
{
|
||||
type: 'message',
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{
|
||||
type: 'output_text',
|
||||
text:
|
||||
'Here is the answer.\n<think>wait, let me reconsider the user request',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
usage: { input_tokens: 12, output_tokens: 4 },
|
||||
},
|
||||
'gpt-5.4',
|
||||
)
|
||||
|
||||
expect(message.content).toEqual([
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Here is the answer.',
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
test('translates Codex SSE text stream into Anthropic events', async () => {
|
||||
const responseText = [
|
||||
'event: response.output_item.added',
|
||||
@@ -609,7 +640,7 @@ describe('Codex request translation', () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('strips leaked reasoning preamble from Codex SSE text stream', async () => {
|
||||
test('strips <think> tag block from Codex SSE text stream', async () => {
|
||||
const responseText = [
|
||||
'event: response.output_item.added',
|
||||
'data: {"type":"response.output_item.added","item":{"id":"msg_1","type":"message","status":"in_progress","content":[],"role":"assistant"},"output_index":0,"sequence_number":0}',
|
||||
@@ -618,13 +649,13 @@ describe('Codex request translation', () => {
|
||||
'data: {"type":"response.content_part.added","content_index":0,"item_id":"msg_1","output_index":0,"part":{"type":"output_text","text":""},"sequence_number":1}',
|
||||
'',
|
||||
'event: response.output_text.delta',
|
||||
'data: {"type":"response.output_text.delta","content_index":0,"delta":"The user just said \\"hey\\" - a simple greeting. I should respond briefly and friendly.\\n\\nHey! How can I help you today?","item_id":"msg_1","output_index":0,"sequence_number":2}',
|
||||
'data: {"type":"response.output_text.delta","content_index":0,"delta":"<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?","item_id":"msg_1","output_index":0,"sequence_number":2}',
|
||||
'',
|
||||
'event: response.output_item.done',
|
||||
'data: {"type":"response.output_item.done","item":{"id":"msg_1","type":"message","status":"completed","content":[{"type":"output_text","text":"The user just said \\"hey\\" - a simple greeting. I should respond briefly and friendly.\\n\\nHey! How can I help you today?"}],"role":"assistant"},"output_index":0,"sequence_number":3}',
|
||||
'data: {"type":"response.output_item.done","item":{"id":"msg_1","type":"message","status":"completed","content":[{"type":"output_text","text":"<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?"}],"role":"assistant"},"output_index":0,"sequence_number":3}',
|
||||
'',
|
||||
'event: response.completed',
|
||||
'data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","model":"gpt-5.4","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"The user just said \\"hey\\" - a simple greeting. I should respond briefly and friendly.\\n\\nHey! How can I help you today?"}]}],"usage":{"input_tokens":2,"output_tokens":1}},"sequence_number":4}',
|
||||
'data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","model":"gpt-5.4","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?"}]}],"usage":{"input_tokens":2,"output_tokens":1}},"sequence_number":4}',
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
@@ -646,6 +677,50 @@ describe('Codex request translation', () => {
|
||||
}
|
||||
}
|
||||
|
||||
expect(textDeltas).toEqual(['Hey! How can I help you today?'])
|
||||
expect(textDeltas.join('')).toBe('Hey! How can I help you today?')
|
||||
})
|
||||
|
||||
test('preserves prose without tags (no phrase-based false positive)', async () => {
|
||||
// Regression test: older phrase-based sanitizer would incorrectly strip text
|
||||
// starting with "I should" or "The user". The tag-based approach leaves it alone.
|
||||
const responseText = [
|
||||
'event: response.output_item.added',
|
||||
'data: {"type":"response.output_item.added","item":{"id":"msg_1","type":"message","status":"in_progress","content":[],"role":"assistant"},"output_index":0,"sequence_number":0}',
|
||||
'',
|
||||
'event: response.content_part.added',
|
||||
'data: {"type":"response.content_part.added","content_index":0,"item_id":"msg_1","output_index":0,"part":{"type":"output_text","text":""},"sequence_number":1}',
|
||||
'',
|
||||
'event: response.output_text.delta',
|
||||
'data: {"type":"response.output_text.delta","content_index":0,"delta":"I should note that the user role requires a briefly concise friendly response format.","item_id":"msg_1","output_index":0,"sequence_number":2}',
|
||||
'',
|
||||
'event: response.output_item.done',
|
||||
'data: {"type":"response.output_item.done","item":{"id":"msg_1","type":"message","status":"completed","content":[{"type":"output_text","text":"I should note that the user role requires a briefly concise friendly response format."}],"role":"assistant"},"output_index":0,"sequence_number":3}',
|
||||
'',
|
||||
'event: response.completed',
|
||||
'data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","model":"gpt-5.4","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"I should note that the user role requires a briefly concise friendly response format."}]}],"usage":{"input_tokens":2,"output_tokens":1}},"sequence_number":4}',
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode(responseText))
|
||||
controller.close()
|
||||
},
|
||||
})
|
||||
|
||||
const textDeltas: string[] = []
|
||||
for await (const event of codexStreamToAnthropic(
|
||||
new Response(stream),
|
||||
'gpt-5.4',
|
||||
)) {
|
||||
const delta = (event as { delta?: { type?: string; text?: string } }).delta
|
||||
if (delta?.type === 'text_delta' && typeof delta.text === 'string') {
|
||||
textDeltas.push(delta.text)
|
||||
}
|
||||
}
|
||||
|
||||
expect(textDeltas.join('')).toBe(
|
||||
'I should note that the user role requires a briefly concise friendly response format.',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,10 +6,9 @@ import type {
|
||||
} from './providerConfig.js'
|
||||
import { sanitizeSchemaForOpenAICompat } from './openaiSchemaSanitizer.js'
|
||||
import {
|
||||
looksLikeLeakedReasoningPrefix,
|
||||
shouldBufferPotentialReasoningPrefix,
|
||||
stripLeakedReasoningPreamble,
|
||||
} from './reasoningLeakSanitizer.js'
|
||||
createThinkTagFilter,
|
||||
stripThinkTags,
|
||||
} from './thinkTagSanitizer.js'
|
||||
|
||||
export interface AnthropicUsage {
|
||||
input_tokens: number
|
||||
@@ -734,34 +733,29 @@ export async function* codexStreamToAnthropic(
|
||||
{ index: number; toolUseId: string }
|
||||
>()
|
||||
let activeTextBlockIndex: number | null = null
|
||||
let activeTextBuffer = ''
|
||||
let textBufferMode: 'none' | 'pending' | 'strip' = 'none'
|
||||
const thinkFilter = createThinkTagFilter()
|
||||
let nextContentBlockIndex = 0
|
||||
let sawToolUse = false
|
||||
let finalResponse: Record<string, any> | undefined
|
||||
|
||||
const closeActiveTextBlock = async function* () {
|
||||
if (activeTextBlockIndex === null) return
|
||||
if (textBufferMode !== 'none') {
|
||||
const sanitized = stripLeakedReasoningPreamble(activeTextBuffer)
|
||||
if (sanitized) {
|
||||
const tail = thinkFilter.flush()
|
||||
if (tail) {
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: activeTextBlockIndex,
|
||||
delta: {
|
||||
type: 'text_delta',
|
||||
text: sanitized,
|
||||
text: tail,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
yield {
|
||||
type: 'content_block_stop',
|
||||
index: activeTextBlockIndex,
|
||||
}
|
||||
activeTextBlockIndex = null
|
||||
activeTextBuffer = ''
|
||||
textBufferMode = 'none'
|
||||
}
|
||||
|
||||
const startTextBlockIfNeeded = async function* () {
|
||||
@@ -837,43 +831,17 @@ export async function* codexStreamToAnthropic(
|
||||
|
||||
if (event.event === 'response.output_text.delta') {
|
||||
yield* startTextBlockIfNeeded()
|
||||
activeTextBuffer += payload.delta ?? ''
|
||||
if (activeTextBlockIndex !== null) {
|
||||
if (
|
||||
textBufferMode === 'strip' ||
|
||||
looksLikeLeakedReasoningPrefix(activeTextBuffer)
|
||||
) {
|
||||
textBufferMode = 'strip'
|
||||
continue
|
||||
}
|
||||
|
||||
if (textBufferMode === 'pending') {
|
||||
if (shouldBufferPotentialReasoningPrefix(activeTextBuffer)) {
|
||||
continue
|
||||
}
|
||||
const visible = thinkFilter.feed(payload.delta ?? '')
|
||||
if (visible) {
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: activeTextBlockIndex,
|
||||
delta: {
|
||||
type: 'text_delta',
|
||||
text: activeTextBuffer,
|
||||
text: visible,
|
||||
},
|
||||
}
|
||||
textBufferMode = 'none'
|
||||
continue
|
||||
}
|
||||
|
||||
if (shouldBufferPotentialReasoningPrefix(activeTextBuffer)) {
|
||||
textBufferMode = 'pending'
|
||||
continue
|
||||
}
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: activeTextBlockIndex,
|
||||
delta: {
|
||||
type: 'text_delta',
|
||||
text: payload.delta ?? '',
|
||||
},
|
||||
}
|
||||
}
|
||||
continue
|
||||
@@ -969,7 +937,7 @@ export function convertCodexResponseToAnthropicMessage(
|
||||
if (part?.type === 'output_text') {
|
||||
content.push({
|
||||
type: 'text',
|
||||
text: stripLeakedReasoningPreamble(part.text ?? ''),
|
||||
text: stripThinkTags(part.text ?? ''),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2513,7 +2513,7 @@ test('non-streaming: real content takes precedence over reasoning_content', asyn
|
||||
])
|
||||
})
|
||||
|
||||
test('non-streaming: strips leaked reasoning preamble from assistant content', async () => {
|
||||
test('non-streaming: strips <think> tag block from assistant content', async () => {
|
||||
globalThis.fetch = (async () => {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
@@ -2524,7 +2524,7 @@ test('non-streaming: strips leaked reasoning preamble from assistant content', a
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content:
|
||||
'The user just said "hey" - a simple greeting. I should respond briefly and friendly.\n\nHey! How can I help you today?',
|
||||
'<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?',
|
||||
},
|
||||
finish_reason: 'stop',
|
||||
},
|
||||
@@ -2645,7 +2645,7 @@ test('streaming: thinking block closed before tool call', async () => {
|
||||
expect(thinkingStart?.content_block?.type).toBe('thinking')
|
||||
})
|
||||
|
||||
test('streaming: strips leaked reasoning preamble from assistant content deltas', async () => {
|
||||
test('streaming: strips <think> tag block from assistant content deltas', async () => {
|
||||
globalThis.fetch = (async () => {
|
||||
const chunks = makeStreamChunks([
|
||||
{
|
||||
@@ -2658,7 +2658,7 @@ test('streaming: strips leaked reasoning preamble from assistant content deltas'
|
||||
delta: {
|
||||
role: 'assistant',
|
||||
content:
|
||||
'The user just said "hey" - a simple greeting. I should respond briefly and friendly.\n\nHey! How can I help you today?',
|
||||
'<think>user wants a greeting, respond briefly</think>Hey! How can I help you today?',
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
@@ -2700,10 +2700,10 @@ test('streaming: strips leaked reasoning preamble from assistant content deltas'
|
||||
}
|
||||
}
|
||||
|
||||
expect(textDeltas).toEqual(['Hey! How can I help you today?'])
|
||||
expect(textDeltas.join('')).toBe('Hey! How can I help you today?')
|
||||
})
|
||||
|
||||
test('streaming: strips leaked reasoning preamble when split across multiple content chunks', async () => {
|
||||
test('streaming: strips <think> tag split across multiple content chunks', async () => {
|
||||
globalThis.fetch = (async () => {
|
||||
const chunks = makeStreamChunks([
|
||||
{
|
||||
@@ -2715,7 +2715,7 @@ test('streaming: strips leaked reasoning preamble when split across multiple con
|
||||
index: 0,
|
||||
delta: {
|
||||
role: 'assistant',
|
||||
content: 'The user said "hey" - this is a simple greeting. ',
|
||||
content: '<think>user wants a greeting,',
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
@@ -2729,8 +2729,21 @@ test('streaming: strips leaked reasoning preamble when split across multiple con
|
||||
{
|
||||
index: 0,
|
||||
delta: {
|
||||
content:
|
||||
'I should respond in a friendly, concise way.\n\nHey! How can I help you today?',
|
||||
content: ' respond briefly</th',
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion.chunk',
|
||||
model: 'gpt-5-mini',
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: {
|
||||
content: 'ink>Hey! How can I help you today?',
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
@@ -2773,7 +2786,69 @@ test('streaming: strips leaked reasoning preamble when split across multiple con
|
||||
}
|
||||
}
|
||||
|
||||
expect(textDeltas).toEqual(['Hey! How can I help you today?'])
|
||||
expect(textDeltas.join('')).toBe('Hey! How can I help you today?')
|
||||
})
|
||||
|
||||
test('streaming: preserves prose without tags (no phrase-based false positive)', async () => {
|
||||
// Regression: older phrase-based sanitizer would strip "I should..." prose.
|
||||
// The tag-based approach leaves legitimate assistant output alone.
|
||||
globalThis.fetch = (async () => {
|
||||
const chunks = makeStreamChunks([
|
||||
{
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion.chunk',
|
||||
model: 'gpt-5-mini',
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: {
|
||||
role: 'assistant',
|
||||
content:
|
||||
'I should note that the user role requires a briefly concise friendly response format.',
|
||||
},
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion.chunk',
|
||||
model: 'gpt-5-mini',
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: {},
|
||||
finish_reason: 'stop',
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
return makeSseResponse(chunks)
|
||||
}) as FetchType
|
||||
|
||||
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||
const result = await client.beta.messages
|
||||
.create({
|
||||
model: 'gpt-5-mini',
|
||||
system: 'test system',
|
||||
messages: [{ role: 'user', content: 'hey' }],
|
||||
max_tokens: 64,
|
||||
stream: true,
|
||||
})
|
||||
.withResponse()
|
||||
|
||||
const textDeltas: string[] = []
|
||||
for await (const event of result.data) {
|
||||
const delta = (event as { delta?: { type?: string; text?: string } }).delta
|
||||
if (delta?.type === 'text_delta' && typeof delta.text === 'string') {
|
||||
textDeltas.push(delta.text)
|
||||
}
|
||||
}
|
||||
|
||||
expect(textDeltas.join('')).toBe(
|
||||
'I should note that the user role requires a briefly concise friendly response format.',
|
||||
)
|
||||
})
|
||||
|
||||
test('classifies localhost transport failures with actionable category marker', async () => {
|
||||
@@ -2944,3 +3019,123 @@ test('preserves valid tool_result and drops orphan tool_result', async () => {
|
||||
const orphanMessage = toolMessages.find(m => m.tool_call_id === 'orphan_call_2')
|
||||
expect(orphanMessage).toBeUndefined()
|
||||
})
|
||||
|
||||
test('request body does not contain store field for local providers', async () => {
|
||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
||||
let requestBody: Record<string, unknown> | undefined
|
||||
|
||||
globalThis.fetch = (async (_input, init) => {
|
||||
requestBody = JSON.parse(String(init?.body))
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion',
|
||||
model: 'test-model',
|
||||
choices: [{ index: 0, message: { role: 'assistant', content: 'ok' }, finish_reason: 'stop' }],
|
||||
usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } },
|
||||
)
|
||||
}) as FetchType
|
||||
|
||||
const client = createOpenAIShimClient({ defaultHeaders: {} }) as unknown as OpenAIShimClient
|
||||
await client.beta.messages.create({
|
||||
model: 'some-model',
|
||||
messages: [{ role: 'user', content: [{ type: 'text', text: 'hi' }] }],
|
||||
max_tokens: 64,
|
||||
stream: false,
|
||||
})
|
||||
|
||||
expect(requestBody).toBeDefined()
|
||||
expect('store' in requestBody!).toBe(false)
|
||||
})
|
||||
|
||||
test('preserves reasoning_content on assistant messages with tool_calls during replay', async () => {
|
||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||
let requestBody: Record<string, unknown> | undefined
|
||||
|
||||
globalThis.fetch = (async (_input, init) => {
|
||||
requestBody = JSON.parse(String(init?.body))
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion',
|
||||
model: 'test-model',
|
||||
choices: [{ index: 0, message: { role: 'assistant', content: 'done' }, finish_reason: 'stop' }],
|
||||
usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } },
|
||||
)
|
||||
}) as FetchType
|
||||
|
||||
const client = createOpenAIShimClient({ defaultHeaders: {} }) as unknown as OpenAIShimClient
|
||||
await client.beta.messages.create({
|
||||
model: 'kimi-k2.5',
|
||||
messages: [
|
||||
{ role: 'user', content: [{ type: 'text', text: 'read file' }] },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'thinking', thinking: 'I should use the read tool' },
|
||||
{ type: 'tool_use', id: 'call_1', name: 'Read', input: { file_path: 'test.ts' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'tool_result', tool_use_id: 'call_1', content: 'file contents here' },
|
||||
],
|
||||
},
|
||||
],
|
||||
max_tokens: 64,
|
||||
stream: false,
|
||||
})
|
||||
|
||||
const messages = requestBody?.messages as Array<Record<string, unknown>>
|
||||
const assistantMsg = messages.find(m => m.role === 'assistant' && m.tool_calls)
|
||||
expect(assistantMsg).toBeDefined()
|
||||
expect(assistantMsg!.reasoning_content).toBe('I should use the read tool')
|
||||
})
|
||||
|
||||
test('does not add reasoning_content on assistant messages without tool_calls', async () => {
|
||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||
let requestBody: Record<string, unknown> | undefined
|
||||
|
||||
globalThis.fetch = (async (_input, init) => {
|
||||
requestBody = JSON.parse(String(init?.body))
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: 'chatcmpl-1',
|
||||
object: 'chat.completion',
|
||||
model: 'test-model',
|
||||
choices: [{ index: 0, message: { role: 'assistant', content: 'ok' }, finish_reason: 'stop' }],
|
||||
usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } },
|
||||
)
|
||||
}) as FetchType
|
||||
|
||||
const client = createOpenAIShimClient({ defaultHeaders: {} }) as unknown as OpenAIShimClient
|
||||
await client.beta.messages.create({
|
||||
model: 'deepseek-reasoner',
|
||||
messages: [
|
||||
{ role: 'user', content: [{ type: 'text', text: 'explain' }] },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'thinking', thinking: 'Let me think about this' },
|
||||
{ type: 'text', text: 'Here is the explanation' },
|
||||
],
|
||||
},
|
||||
{ role: 'user', content: [{ type: 'text', text: 'thanks' }] },
|
||||
],
|
||||
max_tokens: 64,
|
||||
stream: false,
|
||||
})
|
||||
|
||||
const messages = requestBody?.messages as Array<Record<string, unknown>>
|
||||
const assistantMsg = messages.find(m => m.role === 'assistant' && !m.tool_calls)
|
||||
expect(assistantMsg).toBeDefined()
|
||||
expect(assistantMsg!.reasoning_content).toBeUndefined()
|
||||
})
|
||||
@@ -32,10 +32,9 @@ import { resolveGeminiCredential } from '../../utils/geminiAuth.js'
|
||||
import { hydrateGeminiAccessTokenFromSecureStorage } from '../../utils/geminiCredentials.js'
|
||||
import { hydrateGithubModelsTokenFromSecureStorage } from '../../utils/githubModelsCredentials.js'
|
||||
import {
|
||||
looksLikeLeakedReasoningPrefix,
|
||||
shouldBufferPotentialReasoningPrefix,
|
||||
stripLeakedReasoningPreamble,
|
||||
} from './reasoningLeakSanitizer.js'
|
||||
createThinkTagFilter,
|
||||
stripThinkTags,
|
||||
} from './thinkTagSanitizer.js'
|
||||
import {
|
||||
codexStreamToAnthropic,
|
||||
collectCodexCompletedResponse,
|
||||
@@ -193,6 +192,7 @@ function sleepMs(ms: number): Promise<void> {
|
||||
interface OpenAIMessage {
|
||||
role: 'system' | 'user' | 'assistant' | 'tool'
|
||||
content?: string | Array<{ type: string; text?: string; image_url?: { url: string } }>
|
||||
reasoning_content?: string
|
||||
tool_calls?: Array<{
|
||||
id: string
|
||||
type: 'function'
|
||||
@@ -417,6 +417,16 @@ function convertMessages(
|
||||
}
|
||||
|
||||
if (toolUses.length > 0) {
|
||||
// Preserve thinking text as reasoning_content for providers that
|
||||
// require it on replayed assistant tool-call messages (e.g. Kimi,
|
||||
// DeepSeek). Without this, follow-up requests fail with 400:
|
||||
// "reasoning_content is missing in assistant tool call message".
|
||||
// Note: only the first thinking block per turn is captured (.find);
|
||||
// Anthropic's API typically produces one thinking block per turn.
|
||||
if (thinkingBlock) {
|
||||
assistantMsg.reasoning_content = (thinkingBlock as { thinking?: string }).thinking ?? ''
|
||||
}
|
||||
|
||||
assistantMsg.tool_calls = toolUses.map(
|
||||
(tu: {
|
||||
id?: string
|
||||
@@ -718,8 +728,7 @@ async function* openaiStreamToAnthropic(
|
||||
let hasEmittedContentStart = false
|
||||
let hasEmittedThinkingStart = false
|
||||
let hasClosedThinking = false
|
||||
let activeTextBuffer = ''
|
||||
let textBufferMode: 'none' | 'pending' | 'strip' = 'none'
|
||||
const thinkFilter = createThinkTagFilter()
|
||||
let lastStopReason: 'tool_use' | 'max_tokens' | 'end_turn' | null = null
|
||||
let hasEmittedFinalUsage = false
|
||||
let hasProcessedFinishReason = false
|
||||
@@ -798,14 +807,12 @@ async function* openaiStreamToAnthropic(
|
||||
const closeActiveContentBlock = async function* () {
|
||||
if (!hasEmittedContentStart) return
|
||||
|
||||
if (textBufferMode !== 'none') {
|
||||
const sanitized = stripLeakedReasoningPreamble(activeTextBuffer)
|
||||
if (sanitized) {
|
||||
const tail = thinkFilter.flush()
|
||||
if (tail) {
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: contentBlockIndex,
|
||||
delta: { type: 'text_delta', text: sanitized },
|
||||
}
|
||||
delta: { type: 'text_delta', text: tail },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -815,8 +822,6 @@ async function* openaiStreamToAnthropic(
|
||||
}
|
||||
contentBlockIndex++
|
||||
hasEmittedContentStart = false
|
||||
activeTextBuffer = ''
|
||||
textBufferMode = 'none'
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -873,7 +878,6 @@ async function* openaiStreamToAnthropic(
|
||||
contentBlockIndex++
|
||||
hasClosedThinking = true
|
||||
}
|
||||
activeTextBuffer += delta.content
|
||||
if (!hasEmittedContentStart) {
|
||||
yield {
|
||||
type: 'content_block_start',
|
||||
@@ -883,38 +887,13 @@ async function* openaiStreamToAnthropic(
|
||||
hasEmittedContentStart = true
|
||||
}
|
||||
|
||||
if (
|
||||
textBufferMode === 'strip' ||
|
||||
looksLikeLeakedReasoningPrefix(activeTextBuffer)
|
||||
) {
|
||||
textBufferMode = 'strip'
|
||||
continue
|
||||
}
|
||||
|
||||
if (textBufferMode === 'pending') {
|
||||
if (shouldBufferPotentialReasoningPrefix(activeTextBuffer)) {
|
||||
continue
|
||||
}
|
||||
const visible = thinkFilter.feed(delta.content)
|
||||
if (visible) {
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: contentBlockIndex,
|
||||
delta: {
|
||||
type: 'text_delta',
|
||||
text: activeTextBuffer,
|
||||
},
|
||||
delta: { type: 'text_delta', text: visible },
|
||||
}
|
||||
textBufferMode = 'none'
|
||||
continue
|
||||
}
|
||||
|
||||
if (shouldBufferPotentialReasoningPrefix(activeTextBuffer)) {
|
||||
textBufferMode = 'pending'
|
||||
continue
|
||||
}
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
index: contentBlockIndex,
|
||||
delta: { type: 'text_delta', text: delta.content },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1377,9 +1356,10 @@ class OpenAIShimMessages {
|
||||
delete body.max_completion_tokens
|
||||
}
|
||||
|
||||
// mistral and gemini don't recognize body.store — Gemini returns 400
|
||||
// "Invalid JSON payload received. Unknown name 'store': Cannot find field."
|
||||
if (isMistral || isGeminiMode()) {
|
||||
// Strip store for providers that don't recognize it. Only OpenAI's own
|
||||
// API supports this field — Gemini returns 400, local servers (vLLM,
|
||||
// Ollama) reject unknown fields, and other providers silently ignore it.
|
||||
if (isMistral || isGeminiMode() || isLocal) {
|
||||
delete body.store
|
||||
}
|
||||
|
||||
@@ -1742,7 +1722,7 @@ class OpenAIShimMessages {
|
||||
if (typeof rawContent === 'string' && rawContent) {
|
||||
content.push({
|
||||
type: 'text',
|
||||
text: stripLeakedReasoningPreamble(rawContent),
|
||||
text: stripThinkTags(rawContent),
|
||||
})
|
||||
} else if (Array.isArray(rawContent) && rawContent.length > 0) {
|
||||
const parts: string[] = []
|
||||
@@ -1760,7 +1740,7 @@ class OpenAIShimMessages {
|
||||
if (joined) {
|
||||
content.push({
|
||||
type: 'text',
|
||||
text: stripLeakedReasoningPreamble(joined),
|
||||
text: stripThinkTags(joined),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
import {
|
||||
looksLikeLeakedReasoningPrefix,
|
||||
shouldBufferPotentialReasoningPrefix,
|
||||
stripLeakedReasoningPreamble,
|
||||
} from './reasoningLeakSanitizer.ts'
|
||||
|
||||
describe('reasoning leak sanitizer', () => {
|
||||
test('strips explicit internal reasoning preambles', () => {
|
||||
const text =
|
||||
'The user just said "hey" - a simple greeting. I should respond briefly and friendly.\n\nHey! How can I help you today?'
|
||||
|
||||
expect(looksLikeLeakedReasoningPrefix(text)).toBe(true)
|
||||
expect(stripLeakedReasoningPreamble(text)).toBe(
|
||||
'Hey! How can I help you today?',
|
||||
)
|
||||
})
|
||||
|
||||
test('does not strip normal user-facing advice that mentions "the user should"', () => {
|
||||
const text =
|
||||
'The user should reset their password immediately.\n\nHere are the steps...'
|
||||
|
||||
expect(looksLikeLeakedReasoningPrefix(text)).toBe(false)
|
||||
expect(shouldBufferPotentialReasoningPrefix(text)).toBe(false)
|
||||
expect(stripLeakedReasoningPreamble(text)).toBe(text)
|
||||
})
|
||||
|
||||
test('does not strip legitimate first-person advice about responding to an incident', () => {
|
||||
const text =
|
||||
'I need to respond to this security incident immediately. The system is compromised.\n\nHere are the remediation steps...'
|
||||
|
||||
expect(looksLikeLeakedReasoningPrefix(text)).toBe(false)
|
||||
expect(shouldBufferPotentialReasoningPrefix(text)).toBe(false)
|
||||
expect(stripLeakedReasoningPreamble(text)).toBe(text)
|
||||
})
|
||||
|
||||
test('does not strip legitimate first-person advice about answering a support ticket', () => {
|
||||
const text =
|
||||
'I need to answer the support ticket before end of day. The customer is waiting.\n\nHere is the response I drafted...'
|
||||
|
||||
expect(looksLikeLeakedReasoningPrefix(text)).toBe(false)
|
||||
expect(shouldBufferPotentialReasoningPrefix(text)).toBe(false)
|
||||
expect(stripLeakedReasoningPreamble(text)).toBe(text)
|
||||
})
|
||||
})
|
||||
@@ -1,54 +0,0 @@
|
||||
const EXPLICIT_REASONING_START_RE =
|
||||
/^\s*(i should\b|i need to\b|let me think\b|the task\b|the request\b)/i
|
||||
|
||||
const EXPLICIT_REASONING_META_RE =
|
||||
/\b(user|request|question|prompt|message|task|greeting|small talk|briefly|friendly|concise)\b/i
|
||||
|
||||
const USER_META_START_RE =
|
||||
/^\s*the user\s+(just\s+)?(said|asked|is asking|wants|wanted|mentioned|seems|appears)\b/i
|
||||
|
||||
const USER_REASONING_RE =
|
||||
/^\s*the user\s+(just\s+)?(said|asked|is asking|wants|wanted|mentioned|seems|appears)\b[\s\S]*\b(i should|i need to|let me think|respond|reply|answer|greeting|small talk|briefly|friendly|concise)\b/i
|
||||
|
||||
export function shouldBufferPotentialReasoningPrefix(text: string): boolean {
|
||||
const normalized = text.trim()
|
||||
if (!normalized) return false
|
||||
|
||||
if (looksLikeLeakedReasoningPrefix(normalized)) {
|
||||
return true
|
||||
}
|
||||
|
||||
const hasParagraphBoundary = /\n\s*\n/.test(normalized)
|
||||
if (hasParagraphBoundary) {
|
||||
return false
|
||||
}
|
||||
|
||||
return (
|
||||
EXPLICIT_REASONING_START_RE.test(normalized) ||
|
||||
USER_META_START_RE.test(normalized)
|
||||
)
|
||||
}
|
||||
|
||||
export function looksLikeLeakedReasoningPrefix(text: string): boolean {
|
||||
const normalized = text.trim()
|
||||
if (!normalized) return false
|
||||
return (
|
||||
(EXPLICIT_REASONING_START_RE.test(normalized) &&
|
||||
EXPLICIT_REASONING_META_RE.test(normalized)) ||
|
||||
USER_REASONING_RE.test(normalized)
|
||||
)
|
||||
}
|
||||
|
||||
export function stripLeakedReasoningPreamble(text: string): string {
|
||||
const normalized = text.replace(/\r\n/g, '\n')
|
||||
const parts = normalized.split(/\n\s*\n/)
|
||||
if (parts.length < 2) return text
|
||||
|
||||
const first = parts[0]?.trim() ?? ''
|
||||
if (!looksLikeLeakedReasoningPrefix(first)) {
|
||||
return text
|
||||
}
|
||||
|
||||
const remainder = parts.slice(1).join('\n\n').trim()
|
||||
return remainder || text
|
||||
}
|
||||
183
src/services/api/thinkTagSanitizer.test.ts
Normal file
183
src/services/api/thinkTagSanitizer.test.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
import {
|
||||
createThinkTagFilter,
|
||||
stripThinkTags,
|
||||
} from './thinkTagSanitizer.ts'
|
||||
|
||||
describe('stripThinkTags — whole-text cleanup', () => {
|
||||
test('strips closed think pair', () => {
|
||||
expect(stripThinkTags('<think>reasoning</think>Hello')).toBe('Hello')
|
||||
})
|
||||
|
||||
test('strips closed thinking pair', () => {
|
||||
expect(stripThinkTags('<thinking>x</thinking>Out')).toBe('Out')
|
||||
})
|
||||
|
||||
test('strips closed reasoning pair', () => {
|
||||
expect(stripThinkTags('<reasoning>x</reasoning>Out')).toBe('Out')
|
||||
})
|
||||
|
||||
test('strips REASONING_SCRATCHPAD pair', () => {
|
||||
expect(stripThinkTags('<REASONING_SCRATCHPAD>plan</REASONING_SCRATCHPAD>Answer'))
|
||||
.toBe('Answer')
|
||||
})
|
||||
|
||||
test('is case-insensitive', () => {
|
||||
expect(stripThinkTags('<THINKING>x</THINKING>out')).toBe('out')
|
||||
expect(stripThinkTags('<Think>x</Think>out')).toBe('out')
|
||||
})
|
||||
|
||||
test('handles attributes on open tag', () => {
|
||||
expect(stripThinkTags('<think id="plan-1">reason</think>ok')).toBe('ok')
|
||||
})
|
||||
|
||||
test('strips unterminated open tag at block boundary', () => {
|
||||
expect(stripThinkTags('<think>reasoning that never closes')).toBe('')
|
||||
})
|
||||
|
||||
test('strips unterminated open tag after newline', () => {
|
||||
// Block-boundary match consumes the leading newline, same as hermes.
|
||||
expect(stripThinkTags('Answer: 42\n<think>second-guess myself'))
|
||||
.toBe('Answer: 42')
|
||||
})
|
||||
|
||||
test('strips orphan close tag', () => {
|
||||
expect(stripThinkTags('trailing </think>done')).toBe('trailing done')
|
||||
})
|
||||
|
||||
test('strips multiple blocks', () => {
|
||||
expect(stripThinkTags('<think>a</think>B<think>c</think>D')).toBe('BD')
|
||||
})
|
||||
|
||||
test('handles reasoning mid-response after content', () => {
|
||||
expect(stripThinkTags('Answer: 42\n<think>double-check</think>\nDone'))
|
||||
.toBe('Answer: 42\n\nDone')
|
||||
})
|
||||
|
||||
test('handles nested-looking tags (lazy match + orphan cleanup)', () => {
|
||||
expect(stripThinkTags('<think><think>x</think></think>y')).toBe('y')
|
||||
})
|
||||
|
||||
test('preserves legitimate non-think tags', () => {
|
||||
expect(stripThinkTags('use <div> and <span>')).toBe('use <div> and <span>')
|
||||
})
|
||||
|
||||
test('preserves text without any tags', () => {
|
||||
expect(stripThinkTags('Hello, world. I should respond briefly.')).toBe(
|
||||
'Hello, world. I should respond briefly.',
|
||||
)
|
||||
})
|
||||
|
||||
test('handles empty input', () => {
|
||||
expect(stripThinkTags('')).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('createThinkTagFilter — streaming state machine', () => {
|
||||
test('passes through plain text', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('Hello, ')).toBe('Hello, ')
|
||||
expect(f.feed('world!')).toBe('world!')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('strips a complete think block in one chunk', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('pre<think>reason</think>post')).toBe('prepost')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('handles open tag split across deltas', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('before<th')).toBe('before')
|
||||
expect(f.feed('ink>reason</think>after')).toBe('after')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('handles close tag split across deltas', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('<think>reason</th')).toBe('')
|
||||
expect(f.feed('ink>keep')).toBe('keep')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('handles tag split on bare < boundary', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('leading <')).toBe('leading ')
|
||||
expect(f.feed('think>inner</think>tail')).toBe('tail')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('preserves partial non-tag < at boundary when next char rules it out', () => {
|
||||
const f = createThinkTagFilter()
|
||||
// "<d" — 'd' cannot start any of our tag names, so emit immediately
|
||||
expect(f.feed('pre<d')).toBe('pre<d')
|
||||
expect(f.feed('iv>rest')).toBe('iv>rest')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('case-insensitive streaming', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('<THINKING>x</THINKING>out')).toBe('out')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('unterminated open tag — flush drops remainder', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('<think>reasoning with no close ')).toBe('')
|
||||
expect(f.feed('and more reasoning')).toBe('')
|
||||
expect(f.flush()).toBe('')
|
||||
expect(f.isInsideBlock()).toBe(false)
|
||||
})
|
||||
|
||||
test('multiple blocks in single feed', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('<think>a</think>B<think>c</think>D')).toBe('BD')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('flush after clean stream emits nothing extra', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('complete message')).toBe('complete message')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('flush of bare < at end emits it (not a tag prefix)', () => {
|
||||
const f = createThinkTagFilter()
|
||||
// bare '<' held back; flush emits it since it has no tag-name chars
|
||||
expect(f.feed('x <')).toBe('x ')
|
||||
expect(f.flush()).toBe('<')
|
||||
})
|
||||
|
||||
test('flush of partial tag-name prefix at end drops it', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('x <thi')).toBe('x ')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('handles attributes on streaming open tag', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('<think type="plan">reason</think>ok')).toBe('ok')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('mid-delta transition: content, reasoning, content', () => {
|
||||
const f = createThinkTagFilter()
|
||||
expect(f.feed('Answer: 42\n<think>')).toBe('Answer: 42\n')
|
||||
expect(f.feed('double-check')).toBe('')
|
||||
expect(f.feed('</think>\nDone')).toBe('\nDone')
|
||||
expect(f.flush()).toBe('')
|
||||
})
|
||||
|
||||
test('orphan close tag mid-stream is stripped on flush via safety-net behavior', () => {
|
||||
// Filter alone treats orphan close as "we're not inside", so it emits as-is.
|
||||
// Safety net (stripThinkTags on final text) removes orphans.
|
||||
const f = createThinkTagFilter()
|
||||
const chunk1 = f.feed('trailing ')
|
||||
const chunk2 = f.feed('</think>done')
|
||||
const final = chunk1 + chunk2 + f.flush()
|
||||
// Orphan close appears in stream output; safety net cleans it
|
||||
expect(stripThinkTags(final)).toBe('trailing done')
|
||||
})
|
||||
})
|
||||
162
src/services/api/thinkTagSanitizer.ts
Normal file
162
src/services/api/thinkTagSanitizer.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Think-tag sanitizer for reasoning content leaks.
|
||||
*
|
||||
* Some OpenAI-compatible reasoning models (MiniMax M2.7, GLM-4.5/5, DeepSeek, Kimi K2,
|
||||
* self-hosted vLLM builds) emit chain-of-thought inline inside the `content` field using
|
||||
* XML-like tags instead of the separate `reasoning_content` channel. Example:
|
||||
*
|
||||
* <think>the user wants foo, let me check bar</think>Here is the answer: ...
|
||||
*
|
||||
* This module strips those blocks structurally (tag-based), independent of English
|
||||
* phrasings. Three layers:
|
||||
*
|
||||
* 1. `createThinkTagFilter()` — streaming state machine. Feeds deltas, emits only
|
||||
* the visible (non-reasoning) portion, and buffers partial tags across chunk
|
||||
* boundaries so `</th` + `ink>` still parses correctly.
|
||||
*
|
||||
* 2. `stripThinkTags()` — whole-text cleanup. Removes closed pairs, unterminated
|
||||
* opens at block boundaries, and orphan open/close tags. Used for non-streaming
|
||||
* responses and as a safety net after stream close.
|
||||
*
|
||||
* 3. Flush discards buffered partial tags at stream end (false-negative bias —
|
||||
* prefer losing a partial reasoning fragment over leaking it).
|
||||
*/
|
||||
|
||||
const TAG_NAMES = [
|
||||
'think',
|
||||
'thinking',
|
||||
'reasoning',
|
||||
'thought',
|
||||
'reasoning_scratchpad',
|
||||
] as const
|
||||
|
||||
const TAG_ALT = TAG_NAMES.join('|')
|
||||
|
||||
const OPEN_TAG_RE = new RegExp(`<\\s*(?:${TAG_ALT})\\b[^>]*>`, 'i')
|
||||
const CLOSE_TAG_RE = new RegExp(`<\\s*/\\s*(?:${TAG_ALT})\\s*>`, 'i')
|
||||
|
||||
const CLOSED_PAIR_RE_G = new RegExp(
|
||||
`<\\s*(${TAG_ALT})\\b[^>]*>[\\s\\S]*?<\\s*/\\s*\\1\\s*>`,
|
||||
'gi',
|
||||
)
|
||||
const UNTERMINATED_OPEN_RE = new RegExp(
|
||||
`(?:^|\\n)[ \\t]*<\\s*(?:${TAG_ALT})\\b[^>]*>[\\s\\S]*$`,
|
||||
'i',
|
||||
)
|
||||
const ORPHAN_TAG_RE_G = new RegExp(
|
||||
`<\\s*/?\\s*(?:${TAG_ALT})\\b[^>]*>\\s*`,
|
||||
'gi',
|
||||
)
|
||||
|
||||
const MAX_PARTIAL_TAG = 64
|
||||
|
||||
/**
|
||||
* Remove reasoning/thinking blocks from a complete text body.
|
||||
*
|
||||
* Handles:
|
||||
* - Closed pairs: <think>...</think> (lazy match, anywhere in text)
|
||||
* - Unterminated open tags at a block boundary: strips from the tag to end of string
|
||||
* - Orphan open or close tags (no matching partner)
|
||||
*
|
||||
* False-negative bias: prefers leaving a few tag characters in rare edge cases over
|
||||
* stripping legitimate content.
|
||||
*/
|
||||
export function stripThinkTags(text: string): string {
|
||||
if (!text) return text
|
||||
let out = text
|
||||
out = out.replace(CLOSED_PAIR_RE_G, '')
|
||||
out = out.replace(UNTERMINATED_OPEN_RE, '')
|
||||
out = out.replace(ORPHAN_TAG_RE_G, '')
|
||||
return out
|
||||
}
|
||||
|
||||
export interface ThinkTagFilter {
|
||||
feed(chunk: string): string
|
||||
flush(): string
|
||||
isInsideBlock(): boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Streaming state machine. Feed deltas, emits visible (non-reasoning) text.
|
||||
* Handles tags split across chunk boundaries by holding back a short tail buffer
|
||||
* whenever the current buffer ends with what looks like a partial tag.
|
||||
*/
|
||||
export function createThinkTagFilter(): ThinkTagFilter {
|
||||
let inside = false
|
||||
let buffer = ''
|
||||
|
||||
function findPartialTagStart(s: string): number {
|
||||
const lastLt = s.lastIndexOf('<')
|
||||
if (lastLt === -1) return -1
|
||||
if (s.indexOf('>', lastLt) !== -1) return -1
|
||||
const tail = s.slice(lastLt)
|
||||
if (tail.length > MAX_PARTIAL_TAG) return -1
|
||||
|
||||
const m = /^<\s*\/?\s*([a-zA-Z_]\w*)?\s*$/.exec(tail)
|
||||
if (!m) return -1
|
||||
const partialName = (m[1] ?? '').toLowerCase()
|
||||
if (!partialName) return lastLt
|
||||
if (TAG_NAMES.some(name => name.startsWith(partialName))) return lastLt
|
||||
return -1
|
||||
}
|
||||
|
||||
function feed(chunk: string): string {
|
||||
if (!chunk) return ''
|
||||
buffer += chunk
|
||||
let out = ''
|
||||
|
||||
while (buffer.length > 0) {
|
||||
if (!inside) {
|
||||
const open = OPEN_TAG_RE.exec(buffer)
|
||||
if (open) {
|
||||
out += buffer.slice(0, open.index)
|
||||
buffer = buffer.slice(open.index + open[0].length)
|
||||
inside = true
|
||||
continue
|
||||
}
|
||||
|
||||
const partialStart = findPartialTagStart(buffer)
|
||||
if (partialStart === -1) {
|
||||
out += buffer
|
||||
buffer = ''
|
||||
} else {
|
||||
out += buffer.slice(0, partialStart)
|
||||
buffer = buffer.slice(partialStart)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
const close = CLOSE_TAG_RE.exec(buffer)
|
||||
if (close) {
|
||||
buffer = buffer.slice(close.index + close[0].length)
|
||||
inside = false
|
||||
continue
|
||||
}
|
||||
|
||||
const partialStart = findPartialTagStart(buffer)
|
||||
if (partialStart === -1) {
|
||||
buffer = ''
|
||||
} else {
|
||||
buffer = buffer.slice(partialStart)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
function flush(): string {
|
||||
const held = buffer
|
||||
const wasInside = inside
|
||||
buffer = ''
|
||||
inside = false
|
||||
|
||||
if (wasInside) return ''
|
||||
if (!held) return ''
|
||||
|
||||
if (/^<\s*\/?\s*[a-zA-Z_]/.test(held)) return ''
|
||||
return held
|
||||
}
|
||||
|
||||
return { feed, flush, isInsideBlock: () => inside }
|
||||
}
|
||||
@@ -190,16 +190,20 @@ export function getModelMaxOutputTokens(model: string): {
|
||||
}
|
||||
|
||||
// OpenAI-compatible provider — use known output limits to avoid 400 errors
|
||||
if (
|
||||
const isOpenAICompatProvider =
|
||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_OPENAI) ||
|
||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB) ||
|
||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_MISTRAL)
|
||||
) {
|
||||
if (isOpenAICompatProvider) {
|
||||
const openaiMax = getOpenAIMaxOutputTokens(model)
|
||||
if (openaiMax !== undefined) {
|
||||
return { default: openaiMax, upperLimit: openaiMax }
|
||||
}
|
||||
// Unknown 3P model — use conservative default to avoid vLLM/Ollama 400
|
||||
// errors when the default 32k exceeds the model's max_model_len.
|
||||
// Users can override with CLAUDE_CODE_MAX_OUTPUT_TOKENS.
|
||||
return { default: 4_096, upperLimit: 16_384 }
|
||||
}
|
||||
|
||||
const m = getCanonicalName(model)
|
||||
|
||||
@@ -179,12 +179,16 @@ const OPENAI_CONTEXT_WINDOWS: Record<string, number> = {
|
||||
// Google (via OpenRouter)
|
||||
'google/gemini-2.0-flash': 1_048_576,
|
||||
'google/gemini-2.5-pro': 1_048_576,
|
||||
'google/gemini-3-flash-preview': 1_048_576,
|
||||
'google/gemini-3.1-pro-preview': 1_048_576,
|
||||
|
||||
// Google (native via CLAUDE_CODE_USE_GEMINI)
|
||||
'gemini-2.0-flash': 1_048_576,
|
||||
'gemini-2.5-pro': 1_048_576,
|
||||
'gemini-2.5-flash': 1_048_576,
|
||||
'gemini-3-flash-preview': 1_048_576,
|
||||
'gemini-3.1-pro': 1_048_576,
|
||||
'gemini-3.1-pro-preview': 1_048_576,
|
||||
'gemini-3.1-flash-lite-preview': 1_048_576,
|
||||
|
||||
// Ollama local models
|
||||
@@ -331,12 +335,16 @@ const OPENAI_MAX_OUTPUT_TOKENS: Record<string, number> = {
|
||||
// Google (via OpenRouter)
|
||||
'google/gemini-2.0-flash': 8_192,
|
||||
'google/gemini-2.5-pro': 65_536,
|
||||
'google/gemini-3-flash-preview': 65_536,
|
||||
'google/gemini-3.1-pro-preview': 65_536,
|
||||
|
||||
// Google (native via CLAUDE_CODE_USE_GEMINI)
|
||||
'gemini-2.0-flash': 8_192,
|
||||
'gemini-2.5-pro': 65_536,
|
||||
'gemini-2.5-flash': 65_536,
|
||||
'gemini-3-flash-preview': 65_536,
|
||||
'gemini-3.1-pro': 65_536,
|
||||
'gemini-3.1-pro-preview': 65_536,
|
||||
'gemini-3.1-flash-lite-preview': 65_536,
|
||||
|
||||
// Ollama local models (conservative safe defaults)
|
||||
|
||||
Reference in New Issue
Block a user