feat: preserve and echo thinking blocks for extended-thinking models (DeepSeek, Claude thinking mode)
This commit is contained in:
1 parent
d3c74e5681
commit
152d8b972e
6 files changed
+116
-17
No files matched your search
@@ -212,6 +212,15 @@ func assembleMessages(history []session.SessionEntry) []llm.Message {
|
||||
Role: entry.Role,
|
||||
Content: md.Text,
|
||||
}
|
||||
// Restore thinking blocks for assistant messages (must be echoed back).
|
||||
if entry.Role == "assistant" {
|
||||
for _, tb := range md.ThinkingBlocks {
|
||||
msg.ThinkingBlocks = append(msg.ThinkingBlocks, llm.ThinkingBlock{
|
||||
Thinking: tb.Thinking,
|
||||
Signature: tb.Signature,
|
||||
})
|
||||
}
|
||||
}
|
||||
// Convert session images to LLM image content
|
||||
if entry.Role == "user" {
|
||||
for _, img := range md.Images {
|
||||
|
||||
+16
-2
@@ -531,6 +531,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
|
||||
|
||||
var textContent strings.Builder
|
||||
var toolCalls []llm.ToolCall
|
||||
var thinkingBlocks []session.ThinkingBlockData
|
||||
gotFirstToken := false
|
||||
|
||||
streamingOn := r.streamingToolsEnabled()
|
||||
@@ -562,6 +563,14 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
|
||||
}
|
||||
r.emit(AgentEvent{Type: EventToolCallStart, ToolCall: event.ToolCall})
|
||||
|
||||
case llm.EventThinkingBlock:
|
||||
if event.ThinkingBlock != nil {
|
||||
thinkingBlocks = append(thinkingBlocks, session.ThinkingBlockData{
|
||||
Thinking: event.ThinkingBlock.Thinking,
|
||||
Signature: event.ThinkingBlock.Signature,
|
||||
})
|
||||
}
|
||||
|
||||
case llm.EventToolCallDone:
|
||||
if event.ToolCall == nil {
|
||||
continue
|
||||
@@ -696,8 +705,13 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
|
||||
"text_chars", textContent.Len(),
|
||||
"tool_calls", len(toolCalls))
|
||||
|
||||
if textContent.Len() > 0 {
|
||||
r.Session.Append(session.AssistantMessageEntry(textContent.String()))
|
||||
if textContent.Len() > 0 || len(thinkingBlocks) > 0 {
|
||||
if len(thinkingBlocks) > 0 {
|
||||
r.Session.Append(session.AssistantMessageEntryWithThinking(textContent.String(), thinkingBlocks))
|
||||
} else {
|
||||
r.Session.Append(session.AssistantMessageEntry(textContent.String()))
|
||||
}
|
||||
thinkingBlocks = nil
|
||||
if r.KG != nil {
|
||||
r.kgMu.Lock()
|
||||
thread = append(thread, Message{
|
||||
|
||||
+14
-2
@@ -112,6 +112,7 @@ func (r *Runtime) RunTurn(ctx context.Context, userMsg string, images []llm.Imag
|
||||
|
||||
var textContent strings.Builder
|
||||
var toolCalls []llm.ToolCall
|
||||
var thinkingBlocks []session.ThinkingBlockData
|
||||
var lastUsage *llm.Usage
|
||||
retriedRefusal := false
|
||||
streamLoop:
|
||||
@@ -125,6 +126,13 @@ streamLoop:
|
||||
emit(AgentEvent{Type: EventTextDelta, Text: event.Text})
|
||||
case llm.EventToolCallStart:
|
||||
emit(AgentEvent{Type: EventToolCallStart, ToolCall: event.ToolCall})
|
||||
case llm.EventThinkingBlock:
|
||||
if event.ThinkingBlock != nil {
|
||||
thinkingBlocks = append(thinkingBlocks, session.ThinkingBlockData{
|
||||
Thinking: event.ThinkingBlock.Thinking,
|
||||
Signature: event.ThinkingBlock.Signature,
|
||||
})
|
||||
}
|
||||
case llm.EventToolCallDone:
|
||||
if event.ToolCall != nil {
|
||||
toolCalls = append(toolCalls, *event.ToolCall)
|
||||
@@ -170,8 +178,12 @@ streamLoop:
|
||||
break streamLoop
|
||||
}
|
||||
|
||||
if textContent.Len() > 0 {
|
||||
r.Session.Append(session.AssistantMessageEntry(textContent.String()))
|
||||
if textContent.Len() > 0 || len(thinkingBlocks) > 0 {
|
||||
if len(thinkingBlocks) > 0 {
|
||||
r.Session.Append(session.AssistantMessageEntryWithThinking(textContent.String(), thinkingBlocks))
|
||||
} else {
|
||||
r.Session.Append(session.AssistantMessageEntry(textContent.String()))
|
||||
}
|
||||
}
|
||||
|
||||
if len(toolCalls) == 0 {
|
||||
|
||||
Reference in new issue
Block a user