Skip to content

Commit 2a2dcff

Browse files
committed
fix: fix reasoning content
1 parent 174e616 commit 2a2dcff

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

ai/agent.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,11 @@ func (a *agent) processStepStream(ctx context.Context, stream StreamResponse, op
10681068

10691069
activeToolCalls := make(map[string]*ToolCallContent)
10701070
activeTextContent := make(map[string]string)
1071+
type reasoningContent struct {
1072+
content string
1073+
options ProviderMetadata
1074+
}
1075+
activeReasoningContent := make(map[string]reasoningContent)
10711076

10721077
// Process stream parts
10731078
for part := range stream {
@@ -1125,7 +1130,7 @@ func (a *agent) processStepStream(ctx context.Context, stream StreamResponse, op
11251130
}
11261131

11271132
case StreamPartTypeReasoningStart:
1128-
activeTextContent[part.ID] = ""
1133+
activeReasoningContent[part.ID] = reasoningContent{content: ""}
11291134
if opts.OnReasoningStart != nil {
11301135
err := opts.OnReasoningStart(part.ID)
11311136
if err != nil {
@@ -1134,8 +1139,10 @@ func (a *agent) processStepStream(ctx context.Context, stream StreamResponse, op
11341139
}
11351140

11361141
case StreamPartTypeReasoningDelta:
1137-
if _, exists := activeTextContent[part.ID]; exists {
1138-
activeTextContent[part.ID] += part.Delta
1142+
if active, exists := activeReasoningContent[part.ID]; exists {
1143+
active.content += part.Delta
1144+
active.options = part.ProviderMetadata
1145+
activeReasoningContent[part.ID] = active
11391146
}
11401147
if opts.OnReasoningDelta != nil {
11411148
err := opts.OnReasoningDelta(part.ID, part.Delta)
@@ -1145,16 +1152,14 @@ func (a *agent) processStepStream(ctx context.Context, stream StreamResponse, op
11451152
}
11461153

11471154
case StreamPartTypeReasoningEnd:
1148-
if text, exists := activeTextContent[part.ID]; exists {
1149-
stepContent = append(stepContent, ReasoningContent{
1150-
Text: text,
1151-
ProviderMetadata: part.ProviderMetadata,
1152-
})
1155+
if active, exists := activeReasoningContent[part.ID]; exists {
1156+
content := ReasoningContent{
1157+
Text: active.content,
1158+
ProviderMetadata: active.options,
1159+
}
1160+
stepContent = append(stepContent, content)
11531161
if opts.OnReasoningEnd != nil {
1154-
err := opts.OnReasoningEnd(part.ID, ReasoningContent{
1155-
Text: text,
1156-
ProviderMetadata: part.ProviderMetadata,
1157-
})
1162+
err := opts.OnReasoningEnd(part.ID, content)
11581163
if err != nil {
11591164
return StepResult{}, false, err
11601165
}

0 commit comments

Comments
 (0)