Claude: fix prefill tool calling hack with at-depth caching

c3b416b4cfb04883cc288d79737f6b1d8680deef

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +6 -4Ignore whitespace
public/css/toggle-dependent.css+2 -0
@@ -474,6 +474,8 @@ label[for="trim_spaces"]:has(input:checked) i.warning {
474474
475#claude_function_prefill_warning {475#claude_function_prefill_warning {
476 display: none;476 display: none;
477 color: red;
478 font-weight: bold;
477}479}
478480
479#openai_settings:has(#openai_function_calling:checked):has(#claude_assistant_prefill:not(:placeholder-shown), #claude_assistant_impersonation:not(:placeholder-shown)) #claude_function_prefill_warning {481#openai_settings:has(#openai_function_calling:checked):has(#claude_assistant_prefill:not(:placeholder-shown), #claude_assistant_impersonation:not(:placeholder-shown)) #claude_function_prefill_warning {
src/endpoints/backends/chat-completions.js+4 -4
@@ -130,10 +130,6 @@ async function sendClaudeRequest(request, response) {
130 delete requestBody.system;130 delete requestBody.system;
131 }131 }
132 if (useTools) {132 if (useTools) {
133 // Claude doesn't do prefills on function calls, and doesn't allow empty messages
134 if (convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
135 convertedPrompt.messages.push({ role: 'user', content: '.' });
136 }
137 additionalHeaders['anthropic-beta'] = 'tools-2024-05-16';133 additionalHeaders['anthropic-beta'] = 'tools-2024-05-16';
138 requestBody.tool_choice = { type: request.body.tool_choice };134 requestBody.tool_choice = { type: request.body.tool_choice };
139 requestBody.tools = request.body.tools135 requestBody.tools = request.body.tools
@@ -141,6 +137,10 @@ async function sendClaudeRequest(request, response) {
141 .map(tool => tool.function)137 .map(tool => tool.function)
142 .map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));138 .map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));
143139
140 // Claude doesn't do prefills on function calls, and doesn't allow empty messages
141 if (requestBody.tools.length && convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
142 convertedPrompt.messages.push({ role: 'user', content: [{ type: 'text', text: '\u200b' }] });
143 }
144 if (enableSystemPromptCache && requestBody.tools.length) {144 if (enableSystemPromptCache && requestBody.tools.length) {
145 requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };145 requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };
146 }146 }