@@ -18,15 +18,34 @@ import type {
1818
1919import { Tool } from '@modelcontextprotocol/sdk/types'
2020
21- import { REASONING_EFFORT , ENABLE_THINKING } from '@/renderer/types'
21+ import { ReasoningEffort , REASONING_EFFORT , ENABLE_THINKING } from '@/renderer/types'
2222
23- interface ChatRequestBody {
23+ type ChatRequestBody = {
2424 model ?: string
2525 stream ?: boolean
2626 temperature ?: number
2727 messages ?: ChatCompletionMessage [ ]
2828 top_p ?: number
2929 tools ?: Tool [ ]
30+ reasoning_effort ?: ReasoningEffort
31+ enable_thinking ?: boolean
32+ chat_template_kwargs ?: Record < string , any >
33+ [ key : string ] : unknown
34+ }
35+
36+ type SimpleStreamChoice = {
37+ index : number
38+ delta : AssistantMessage
39+ message ?: AssistantMessage
40+ finish_reason : string | null
41+ }
42+
43+ type SimpleStreamResponse = {
44+ choices : SimpleStreamChoice [ ]
45+ }
46+
47+ type SimpleCfResponse = {
48+ response : AssistantMessage
3049}
3150
3251export type ChatProcessResult = 'aborted' | 'error' | 'done'
@@ -370,17 +389,20 @@ const read = async (
370389 return read ( reader , sessionId , target , buffer , stream )
371390}
372391
373- const parseJson = ( content , target : AssistantMessage ) => {
392+ const parseJson = ( content : string , target : AssistantMessage ) => {
374393 try {
375- const parsed = JSON . parse ( content )
394+ const parsed = JSON . parse ( content as string )
376395 parseChoices ( parsed , target )
377396 } catch ( e ) {
378397 console . log ( e , content )
379398 parseChoice ( content , target )
380399 }
381400}
382401
383- const parseChoices = ( parsed , target ) => {
402+ const parseChoices = (
403+ parsed : SimpleStreamResponse | SimpleCfResponse ,
404+ target : AssistantMessage
405+ ) => {
384406 if ( 'choices' in parsed ) {
385407 return parsed . choices . map ( ( choice ) => {
386408 const content = choice . delta || choice . message
@@ -393,7 +415,7 @@ const parseChoices = (parsed, target) => {
393415 }
394416}
395417
396- const parseChoice = ( choice : AssistantMessage , target : AssistantMessage ) => {
418+ const parseChoice = ( choice : AssistantMessage | string , target : AssistantMessage ) => {
397419 if ( choice ) {
398420 if ( target . role === 'assistant' ) {
399421 if ( typeof choice === 'string' ) {
@@ -408,7 +430,7 @@ const parseChoice = (choice: AssistantMessage, target: AssistantMessage) => {
408430 target . reasoning_content += choice . reasoning_content
409431 }
410432 }
411- parseTool ( choice . tool_calls , target )
433+ parseTool ( ( choice as AssistantMessage ) . tool_calls , target )
412434 }
413435 }
414436}
@@ -453,18 +475,19 @@ const parseTool = (tools: ToolCall[] | undefined, target: AssistantMessage) => {
453475
454476 // Merge each property from source function
455477 Object . keys ( sourceFunc ) . forEach ( ( key ) => {
456- const value = sourceFunc [ key ]
478+ const typedKey = key as 'name' | 'arguments'
479+ const value = sourceFunc [ typedKey ]
457480
458481 // Skip null values (don't overwrite existing values with null)
459482 if ( value === null ) return
460483
461484 // Merge strategy:
462485 // - If target has existing non-empty value: concatenate
463486 // - Otherwise: overwrite
464- if ( targetFunc [ key ] && targetFunc [ key ] !== '{}' ) {
465- targetFunc [ key ] += value
487+ if ( targetFunc [ typedKey ] && targetFunc [ typedKey ] !== '{}' ) {
488+ targetFunc [ typedKey ] += value
466489 } else {
467- targetFunc [ key ] = value
490+ targetFunc [ typedKey ] = value
468491 }
469492 } )
470493 }
0 commit comments