-
Notifications
You must be signed in to change notification settings - Fork 14
fix: strip Claude title reasoning effort #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -896,7 +896,7 @@ function createFetchInterceptor( | |
| headers.set('Authorization', `Bearer ${config.apiKey}`); | ||
| headers.set('Content-Type', 'application/json'); | ||
|
|
||
| const sanitizedBody = await sanitizeGeminiToolSchemas(input, init, url); | ||
| const sanitizedBody = await sanitizeRequestPayload(input, init, url); | ||
|
|
||
| // Clone init to avoid mutating original | ||
| const modifiedInit: RequestInit = { | ||
|
|
@@ -918,8 +918,12 @@ function createFetchInterceptor( | |
| } | ||
|
|
||
| const GEMINI_SCHEMA_KEYS_TO_REMOVE = new Set(['$schema', '$ref', 'ref', 'additionalProperties']); | ||
| const TITLE_PROMPT_REQUIRED_MARKERS = [ | ||
| 'You are a title generator', | ||
| 'thread title', | ||
| ]; | ||
|
|
||
| async function sanitizeGeminiToolSchemas( | ||
| async function sanitizeRequestPayload( | ||
| input: RequestInfo | URL, | ||
| init: RequestInit | undefined, | ||
| url: string, | ||
|
|
@@ -944,24 +948,111 @@ async function sanitizeGeminiToolSchemas( | |
| return undefined; | ||
| } | ||
|
|
||
| const clonedPayload = structuredClone(payload); | ||
| let changed = false; | ||
|
|
||
| changed = stripClaudeTitleReasoningEffort(clonedPayload) || changed; | ||
| changed = sanitizeGeminiToolSchemas(clonedPayload) || changed; | ||
|
|
||
| return changed ? JSON.stringify(clonedPayload) : undefined; | ||
| } | ||
|
|
||
| function stripClaudeTitleReasoningEffort(payload: Record<string, unknown>): boolean { | ||
| const model = payload.model; | ||
| if (!isClaudeModel(model) || !isOpenCodeTitlePrompt(payload)) { | ||
| return false; | ||
| } | ||
|
|
||
| let changed = false; | ||
| if ('reasoning_effort' in payload) { | ||
| delete payload.reasoning_effort; | ||
| changed = true; | ||
| } | ||
| if ('reasoningEffort' in payload) { | ||
| delete payload.reasoningEffort; | ||
| changed = true; | ||
| } | ||
|
|
||
| if (changed) { | ||
| debug('Removed reasoning effort from Claude title request'); | ||
| } | ||
|
|
||
| return changed; | ||
| } | ||
|
|
||
| function isClaudeModel(model: unknown): boolean { | ||
| if (typeof model !== 'string') return false; | ||
| const lower = model.toLowerCase(); | ||
| return ( | ||
| lower.startsWith('claude/') || | ||
| lower.startsWith('claude-') || | ||
| lower.startsWith('anthropic/') || | ||
| lower.startsWith('anthropic:') || | ||
| lower.includes('/claude-') | ||
| ); | ||
| } | ||
|
|
||
| function isOpenCodeTitlePrompt(payload: Record<string, unknown>): boolean { | ||
| if (contentContainsTitlePrompt(payload.instructions)) return true; | ||
|
|
||
| const messages = payload.messages; | ||
| if (Array.isArray(messages)) { | ||
| return messages.some((message) => { | ||
| if (!isRecord(message) || message.role !== 'system') return false; | ||
| return contentContainsTitlePrompt(message.content); | ||
| }); | ||
| } | ||
|
|
||
| const input = payload.input; | ||
| if (Array.isArray(input)) { | ||
| return input.some((item) => { | ||
| if (!isRecord(item) || item.role !== 'system') return false; | ||
| return contentContainsTitlePrompt(item.content); | ||
| }); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
Comment on lines
+995
to
+1015
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If function isOpenCodeTitlePrompt(payload: Record<string, unknown>): boolean {
if (contentContainsTitlePrompt(payload.instructions)) return true;
const messages = payload.messages;
if (Array.isArray(messages)) {
const hasPrompt = messages.some((message) => {
if (!isRecord(message) || message.role !== 'system') return false;
return contentContainsTitlePrompt(message.content);
});
if (hasPrompt) return true;
}
const input = payload.input;
if (Array.isArray(input)) {
const hasPrompt = input.some((item) => {
if (!isRecord(item) || item.role !== 'system') return false;
return contentContainsTitlePrompt(item.content);
});
if (hasPrompt) return true;
}
return false;
} |
||
|
|
||
| function contentContainsTitlePrompt(content: unknown): boolean { | ||
| const text = contentToText(content); | ||
| if (!text) return false; | ||
| return TITLE_PROMPT_REQUIRED_MARKERS.every((marker) => text.includes(marker)); | ||
| } | ||
|
|
||
| function contentToText(content: unknown): string { | ||
| if (typeof content === 'string') return content; | ||
|
|
||
| if (Array.isArray(content)) { | ||
| return content.map(contentToText).filter(Boolean).join('\n'); | ||
| } | ||
|
|
||
| if (!isRecord(content)) return ''; | ||
| const text = content.text; | ||
| if (typeof text === 'string') return text; | ||
| const value = content.value; | ||
| if (typeof value === 'string') return value; | ||
| const contentValue = content.content; | ||
| if (contentValue !== undefined) return contentToText(contentValue); | ||
| return ''; | ||
| } | ||
|
|
||
| function sanitizeGeminiToolSchemas(payload: Record<string, unknown>): boolean { | ||
| const model = payload.model; | ||
| if (typeof model !== 'string' || !model.toLowerCase().includes('gemini')) { | ||
| return undefined; | ||
| return false; | ||
| } | ||
|
|
||
| const tools = payload.tools; | ||
| if (!Array.isArray(tools) || tools.length === 0) { | ||
| return undefined; | ||
| return false; | ||
| } | ||
|
|
||
| const clonedPayload = structuredClone(payload); | ||
| const changed = sanitizeToolSchemaContainer(clonedPayload); | ||
| if (!changed) { | ||
| return undefined; | ||
| const changed = sanitizeToolSchemaContainer(payload); | ||
| if (changed) { | ||
| debug('Sanitized Gemini tool schema keywords'); | ||
| } | ||
|
|
||
| debug('Sanitized Gemini tool schema keywords'); | ||
| return JSON.stringify(clonedPayload); | ||
| return changed; | ||
| } | ||
|
|
||
| async function getRawJsonBody( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating the
changedflag using logical OR (||) with function calls on the left-hand side is correct but highly fragile and non-idiomatic. If a future refactoring reorders the operands (e.g.,changed = changed || ...), the function call will be short-circuited and skipped whenchangedis alreadytrue. It is much safer and more readable to evaluate the functions independently first.