package reasoningmode import "strings" // PartialFeed assembles the streaming card content of one turn. // // A reasoning run streams every agent step's narrative through the answer // channel and appends the final answer body to the same accumulation, so the // raw text reaches the card as one run-on paragraph. The feed // // - joins step segments with a blank line, so the card shows the reasoning // line by line instead of glued sentences, and // - switches to the final body only as soon as the body starts. The DingTalk // card update replaces the card content, so that switch is what the user // sees as the "refresh": the reasoning block disappears and the body is // streamed on its own. // // Quick executions are a pass-through and keep the previous behaviour. type PartialFeed struct { reasoning bool seen string display string stepPending bool bodyOnly string bodyStarted bool started bool } // NewPartialFeed builds the feed for one turn's execution mode. func NewPartialFeed(execution string) *PartialFeed { return &PartialFeed{reasoning: IsReasoningExecution(execution)} } // Started reports whether any answer text has been streamed yet. func (f *PartialFeed) Started() bool { return f.started } // StepBoundary marks the end of an agent step (tool call or tool result) so // the next narrative segment starts on a new line. func (f *PartialFeed) StepBoundary() { if !f.reasoning { return } f.stepPending = true } // OnAnswer records the accumulated answer text reported by the stream and // returns the content the card must show right now. func (f *PartialFeed) OnAnswer(fullAnswer string) string { if !f.reasoning { if strings.TrimSpace(fullAnswer) != "" { f.started = true } return fullAnswer } delta := fullAnswer switch { case strings.HasPrefix(fullAnswer, f.seen): delta = fullAnswer[len(f.seen):] case fullAnswer != f.seen: // The stream restarted (new WeKnora session or a retry): treat the // text as a fresh segment instead of gluing it to the previous one. f.stepPending = true } f.seen = fullAnswer if delta != "" { f.started = true if f.stepPending && strings.TrimSpace(f.display) != "" { f.display += "\n\n" } f.stepPending = false f.display += delta } if body, ok := SplitReasoningAnswer(f.display); ok { f.bodyStarted = true f.bodyOnly = body } if f.bodyStarted { return f.bodyOnly } return f.display }