@@ -89,6 +89,24 @@ function createMoonshotRequest(params: {
8989 fetch : typeof globalThis . fetch
9090} ) {
9191 const { body, originalModel, fetch } = params
92+ const moonshotBody = buildMoonshotRequestBody ( body , originalModel )
93+
94+ return fetch ( `${ MOONSHOT_BASE_URL } /chat/completions` , {
95+ method : 'POST' ,
96+ headers : {
97+ Authorization : `Bearer ${ getMoonshotApiKey ( ) } ` ,
98+ 'Content-Type' : 'application/json' ,
99+ } ,
100+ body : JSON . stringify ( moonshotBody ) ,
101+ // @ts -expect-error - dispatcher is a valid undici option not in fetch types
102+ dispatcher : moonshotAgent ,
103+ } )
104+ }
105+
106+ export function buildMoonshotRequestBody (
107+ body : ChatCompletionRequestBody ,
108+ originalModel : string ,
109+ ) : Record < string , unknown > {
92110 const moonshotCompatibleBody = addKimiToolCompatibilityFields ( body )
93111 const moonshotBody : Record < string , unknown > = {
94112 ...moonshotCompatibleBody ,
@@ -97,12 +115,7 @@ function createMoonshotRequest(params: {
97115 model : getMoonshotModelId ( originalModel ) ,
98116 }
99117
100- if ( moonshotBody . reasoning && typeof moonshotBody . reasoning === 'object' ) {
101- const reasoning = moonshotBody . reasoning as { enabled ?: boolean }
102- moonshotBody . thinking = {
103- type : reasoning . enabled === false ? 'disabled' : 'enabled' ,
104- }
105- }
118+ moonshotBody . thinking = createMoonshotThinking ( moonshotBody )
106119
107120 delete moonshotBody . reasoning
108121 delete moonshotBody . reasoning_effort
@@ -115,16 +128,33 @@ function createMoonshotRequest(params: {
115128 moonshotBody . stream_options = { include_usage : true }
116129 }
117130
118- return fetch ( `${ MOONSHOT_BASE_URL } /chat/completions` , {
119- method : 'POST' ,
120- headers : {
121- Authorization : `Bearer ${ getMoonshotApiKey ( ) } ` ,
122- 'Content-Type' : 'application/json' ,
123- } ,
124- body : JSON . stringify ( moonshotBody ) ,
125- // @ts -expect-error - dispatcher is a valid undici option not in fetch types
126- dispatcher : moonshotAgent ,
127- } )
131+ return moonshotBody
132+ }
133+
134+ function createMoonshotThinking (
135+ moonshotBody : Record < string , unknown > ,
136+ ) : Record < string , unknown > {
137+ const reasoning =
138+ moonshotBody . reasoning && typeof moonshotBody . reasoning === 'object'
139+ ? ( moonshotBody . reasoning as { enabled ?: boolean } )
140+ : undefined
141+ if ( reasoning ?. enabled === false ) {
142+ return { type : 'disabled' }
143+ }
144+
145+ const existingThinking =
146+ moonshotBody . thinking && typeof moonshotBody . thinking === 'object'
147+ ? ( moonshotBody . thinking as Record < string , unknown > )
148+ : { }
149+ if ( existingThinking . type === 'disabled' ) {
150+ return { type : 'disabled' }
151+ }
152+
153+ return {
154+ ...existingThinking ,
155+ type : 'enabled' ,
156+ keep : 'all' ,
157+ }
128158}
129159
130160function normalizeMoonshotMessages (
0 commit comments