diff --git a/src/backend/src/modules/puterai/lib/OpenAIUtil.js b/src/backend/src/modules/puterai/lib/OpenAIUtil.js index 045d04c840..6f21510896 100644 --- a/src/backend/src/modules/puterai/lib/OpenAIUtil.js +++ b/src/backend/src/modules/puterai/lib/OpenAIUtil.js @@ -120,6 +120,12 @@ const create_chat_stream_handler = ({ const choice = chunk.choices[0]; + if ( choice.delta.reasoning_content ){ + textblock.addReasoning(choice.delta.reasoning_content); + // Q: Why don't "continue" to next chunk here? + // A: For now, reasoning_content and content never appear together, but I’m not sure if they’ll always be mutually exclusive. + } + if ( choice.delta.content ){ if ( mode === 'tool' ) { toolblock.end(); diff --git a/src/backend/src/modules/puterai/lib/Streaming.js b/src/backend/src/modules/puterai/lib/Streaming.js index 5bbfb8bff5..933c078b5d 100644 --- a/src/backend/src/modules/puterai/lib/Streaming.js +++ b/src/backend/src/modules/puterai/lib/Streaming.js @@ -35,6 +35,13 @@ class AIChatTextStream extends AIChatConstructStream { }); this.chatStream.stream.write(json + '\n'); } + + addReasoning (reasoning) { + const json = JSON.stringify({ + type: 'reasoning', reasoning, + }); + this.chatStream.stream.write(json + '\n'); + } } class AIChatToolUseStream extends AIChatConstructStream {