11/**
22 * Minimal LLM API Client
3- * Supports OpenAI / DeepSeek / Anthropic formats
3+ * Supports OpenAI-compatible / Anthropic-compatible formats
44 */
55
66import type { LLMConfig } from './llmModels' ;
@@ -88,6 +88,13 @@ interface LLMResponse {
8888 toolCalls : ToolCall [ ] ;
8989}
9090
91+ function stripThinkTags ( content : string ) : string {
92+ const withoutBlocks = content
93+ . replace ( / < t h i n k \b [ ^ > ] * > [ \s \S ] * ?< \/ t h i n k > / gi, '' )
94+ . replace ( / < \/ ? t h i n k \b [ ^ > ] * > / gi, '' ) ;
95+ return withoutBlocks === content ? content : withoutBlocks . trim ( ) ;
96+ }
97+
9198function hasVersionSuffix ( url : string ) : boolean {
9299 return / \/ v \d + \/ ? $ / . test ( url ) ;
93100}
@@ -162,14 +169,17 @@ async function chatOpenAI(
162169 messageCount : messages . length ,
163170 toolCount : tools . length ,
164171 } ) ;
172+ const headers : Record < string , string > = {
173+ 'Content-Type' : 'application/json' ,
174+ 'X-LLM-Target-URL' : targetUrl ,
175+ ...parseCustomHeaders ( config . customHeaders ) ,
176+ } ;
177+ if ( config . apiKey . trim ( ) ) {
178+ headers . Authorization = `Bearer ${ config . apiKey } ` ;
179+ }
165180 const res = await fetch ( '/api/llm-proxy' , {
166181 method : 'POST' ,
167- headers : {
168- 'Content-Type' : 'application/json' ,
169- Authorization : `Bearer ${ config . apiKey } ` ,
170- 'X-LLM-Target-URL' : targetUrl ,
171- ...parseCustomHeaders ( config . customHeaders ) ,
172- } ,
182+ headers,
173183 body : JSON . stringify ( body ) ,
174184 } ) ;
175185
@@ -195,7 +205,7 @@ async function chatOpenAI(
195205 calledNames ,
196206 ) ;
197207 return {
198- content : choice ?. content || '' ,
208+ content : stripThinkTags ( choice ?. content || '' ) ,
199209 toolCalls,
200210 } ;
201211}
@@ -267,15 +277,18 @@ async function chatAnthropic(
267277 messageCount : anthropicMessages . length ,
268278 toolCount : anthropicTools . length ,
269279 } ) ;
280+ const headers : Record < string , string > = {
281+ 'Content-Type' : 'application/json' ,
282+ 'anthropic-version' : '2023-06-01' ,
283+ 'X-LLM-Target-URL' : targetUrl ,
284+ ...parseCustomHeaders ( config . customHeaders ) ,
285+ } ;
286+ if ( config . apiKey . trim ( ) ) {
287+ headers [ 'x-api-key' ] = config . apiKey ;
288+ }
270289 const res = await fetch ( '/api/llm-proxy' , {
271290 method : 'POST' ,
272- headers : {
273- 'Content-Type' : 'application/json' ,
274- 'x-api-key' : config . apiKey ,
275- 'anthropic-version' : '2023-06-01' ,
276- 'X-LLM-Target-URL' : targetUrl ,
277- ...parseCustomHeaders ( config . customHeaders ) ,
278- } ,
291+ headers,
279292 body : JSON . stringify ( body ) ,
280293 } ) ;
281294
@@ -314,5 +327,5 @@ async function chatAnthropic(
314327 'calledNames=' ,
315328 calledNames ,
316329 ) ;
317- return { content, toolCalls } ;
330+ return { content : stripThinkTags ( content ) , toolCalls } ;
318331}
0 commit comments