@@ -45,7 +45,7 @@ class ApiEP {
4545 */
4646
4747/**
48- * @typedef {{role: string, content: string, tool_calls: Array<NSToolCalls>} } NSChatMessage
48+ * @typedef {{role: string, content: string, reasoning_content: string, tool_calls: Array<NSToolCalls>} } NSChatMessage
4949 */
5050
5151class ChatMessageEx {
@@ -54,12 +54,13 @@ class ChatMessageEx {
5454 * Represent a Message in the Chat
5555 * @param {string } role
5656 * @param {string } content
57+ * @param {string } reasoning_content
5758 * @param {Array<any> } tool_calls
5859 * @param {string } trimmedContent
5960 */
60- constructor ( role = "" , content = "" , tool_calls = [ ] , trimmedContent = "" ) {
61+ constructor ( role = "" , content = "" , reasoning_content = "" , tool_calls = [ ] , trimmedContent = "" ) {
6162 /** @type {NSChatMessage } */
62- this . ns = { role : role , content : content , tool_calls : tool_calls }
63+ this . ns = { role : role , content : content , tool_calls : tool_calls , reasoning_content : reasoning_content }
6364 this . trimmedContent = trimmedContent ;
6465 }
6566
@@ -68,12 +69,13 @@ class ChatMessageEx {
6869 * @param {ChatMessageEx } old
6970 */
7071 static newFrom ( old ) {
71- return new ChatMessageEx ( old . ns . role , old . ns . content , old . ns . tool_calls , old . trimmedContent )
72+ return new ChatMessageEx ( old . ns . role , old . ns . content , old . ns . reasoning_content , old . ns . tool_calls , old . trimmedContent )
7273 }
7374
7475 clear ( ) {
7576 this . ns . role = "" ;
7677 this . ns . content = "" ;
78+ this . ns . reasoning_content = "" ;
7779 this . ns . tool_calls = [ ] ;
7880 this . trimmedContent = "" ;
7981 }
@@ -182,6 +184,7 @@ class ChatMessageEx {
182184 }
183185 } else {
184186 let toolCalls = nwo [ "choices" ] [ 0 ] [ "delta" ] [ "tool_calls" ] ;
187+ let reasoningContent = nwo [ "choices" ] [ 0 ] [ "delta" ] [ "reasoning_content" ] ;
185188 if ( toolCalls !== undefined ) {
186189 if ( toolCalls [ 0 ] [ "function" ] [ "name" ] !== undefined ) {
187190 this . ns . tool_calls . push ( toolCalls [ 0 ] ) ;
@@ -197,6 +200,9 @@ class ChatMessageEx {
197200 }
198201 }
199202 }
203+ if ( reasoningContent !== undefined ) {
204+ this . ns . reasoning_content += reasoningContent
205+ }
200206 }
201207 }
202208 } else {
@@ -221,6 +227,10 @@ class ChatMessageEx {
221227 this . ns . content = curContent ;
222228 }
223229 }
230+ let curRC = nwo [ "choices" ] [ 0 ] [ "message" ] [ "reasoning_content" ] ;
231+ if ( curRC != undefined ) {
232+ this . ns . reasoning_content = curRC ;
233+ }
224234 let curTCs = nwo [ "choices" ] [ 0 ] [ "message" ] [ "tool_calls" ] ;
225235 if ( curTCs != undefined ) {
226236 this . ns . tool_calls = curTCs ;
@@ -242,10 +252,21 @@ class ChatMessageEx {
242252 }
243253
244254 content_equiv ( ) {
255+ let reasoning = ""
256+ if ( this . ns . reasoning_content . trim ( ) !== "" ) {
257+ reasoning = this . ns . reasoning_content . trim ( )
258+ }
245259 if ( this . ns . content !== "" ) {
260+ if ( reasoning !== "" ) {
261+ return `!!!Reasoning: ${ reasoning } !!! ${ this . ns . content } ` ;
262+ }
246263 return this . ns . content ;
247264 } else if ( this . has_toolcall ( ) ) {
248- return `<tool_call>\n<tool_name>${ this . ns . tool_calls [ 0 ] . function . name } </tool_name>\n<tool_args>${ this . ns . tool_calls [ 0 ] . function . arguments } </tool_args>\n</tool_call>` ;
265+ let ret = ""
266+ if ( reasoning !== "" ) {
267+ ret = `!!!Reasoning: ${ reasoning } !!!`
268+ }
269+ return `${ ret } <tool_call>\n<tool_name>${ this . ns . tool_calls [ 0 ] . function . name } </tool_name>\n<tool_args>${ this . ns . tool_calls [ 0 ] . function . arguments } </tool_args>\n</tool_call>` ;
249270 } else {
250271 return ""
251272 }
@@ -325,7 +346,7 @@ class SimpleChat {
325346 let tcur = /** @type {OldChatMessage } */ ( /** @type {unknown } */ ( cur ) ) ;
326347 this . xchat . push ( new ChatMessageEx ( tcur . role , tcur . content ) )
327348 } else {
328- this . xchat . push ( new ChatMessageEx ( cur . ns . role , cur . ns . content , cur . ns . tool_calls , cur . trimmedContent ) )
349+ this . xchat . push ( new ChatMessageEx ( cur . ns . role , cur . ns . content , cur . ns . reasoning_content , cur . ns . tool_calls , cur . trimmedContent ) )
329350 }
330351 }
331352 }
@@ -394,6 +415,9 @@ class SimpleChat {
394415 if ( ! tmsg . has_toolcall ( ) ) {
395416 tmsg . ns_delete ( "tool_calls" )
396417 }
418+ if ( tmsg . ns . reasoning_content . trim ( ) === "" ) {
419+ tmsg . ns_delete ( "reasoning_content" )
420+ }
397421 if ( tmsg . ns . role == Roles . Tool ) {
398422 let res = ChatMessageEx . extractToolCallResultAllInOne ( tmsg . ns . content )
399423 tmsg . ns . content = res . content
@@ -471,6 +495,10 @@ class SimpleChat {
471495 let last = undefined ;
472496 for ( const x of this . recent_chat ( gMe . chatProps . iRecentUserMsgCnt ) ) {
473497 if ( x . ns . role != Roles . ToolTemp ) {
498+ if ( x . ns . reasoning_content . trim ( ) === "" ) {
499+ let entry = ui . el_create_append_p ( `>>${ x . ns . role } !<<: ${ x . ns . reasoning_content } ` , div ) ;
500+ entry . className = `role-${ x . ns . role } ` ;
501+ }
474502 let entry = ui . el_create_append_p ( `${ x . ns . role } : ${ x . content_equiv ( ) } ` , div ) ;
475503 entry . className = `role-${ x . ns . role } ` ;
476504 last = entry ;
0 commit comments