Skip to content

Commit d5135d6

Browse files
committed
Preserve DeepSeek reasoning for tool calls
1 parent 415161c commit d5135d6

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

packages/agent-runtime/src/tools/stream-parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ export async function processStream(
276276
}
277277

278278
if (chunk.type === 'reasoning') {
279+
if (chunk.text) {
280+
assistantMessages.push(
281+
assistantMessage({ type: 'reasoning', text: chunk.text }),
282+
)
283+
}
279284
onResponseChunk({
280285
type: 'reasoning_delta',
281286
text: chunk.text,

packages/internal/src/openai-compatible/chat/convert-to-openai-compatible-chat-messages.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,43 @@ describe('provider-specific metadata merging', () => {
509509
])
510510
})
511511

512+
it('should preserve assistant reasoning content with tool calls', () => {
513+
const result = convertToOpenAICompatibleChatMessages([
514+
{
515+
role: 'assistant',
516+
content: [
517+
{ type: 'reasoning', text: 'Need the date first. ' },
518+
{ type: 'reasoning', text: 'Then call weather.' },
519+
{ type: 'text', text: 'Checking that now...' },
520+
{
521+
type: 'tool-call',
522+
toolCallId: 'call1',
523+
toolName: 'get_weather',
524+
input: { location: 'Hangzhou' },
525+
},
526+
],
527+
},
528+
])
529+
530+
expect(result).toEqual([
531+
{
532+
role: 'assistant',
533+
content: 'Checking that now...',
534+
reasoning_content: 'Need the date first. Then call weather.',
535+
tool_calls: [
536+
{
537+
id: 'call1',
538+
type: 'function',
539+
function: {
540+
name: 'get_weather',
541+
arguments: JSON.stringify({ location: 'Hangzhou' }),
542+
},
543+
},
544+
],
545+
},
546+
])
547+
})
548+
512549
it('should handle a single tool role message with multiple tool-result parts', () => {
513550
const result = convertToOpenAICompatibleChatMessages([
514551
{

packages/internal/src/openai-compatible/chat/convert-to-openai-compatible-chat-messages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function convertToOpenAICompatibleChatMessages(
6565

6666
case 'assistant': {
6767
let text = ''
68+
let reasoningContent = ''
6869
const toolCalls: Array<{
6970
id: string
7071
type: 'function'
@@ -78,6 +79,10 @@ export function convertToOpenAICompatibleChatMessages(
7879
text += part.text
7980
break
8081
}
82+
case 'reasoning': {
83+
reasoningContent += part.text
84+
break
85+
}
8186
case 'tool-call': {
8287
toolCalls.push({
8388
id: part.toolCallId,
@@ -96,6 +101,8 @@ export function convertToOpenAICompatibleChatMessages(
96101
messages.push({
97102
role: 'assistant',
98103
content: text,
104+
reasoning_content:
105+
reasoningContent.length > 0 ? reasoningContent : undefined,
99106
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
100107
...metadata,
101108
})

0 commit comments

Comments
 (0)