15f2a11162
- reasoningmode: 新增正文边界识别(结构行 + 前置空行)与流式 PartialFeed: 分步叙述按行拼接显示,正文开始时只输出正文,卡片更新即刷新清掉推理区 - dingtalk: 事件流与流式回调统一改走 PartialFeed - qa: 推理执行的最终答案在入库/投递前去掉步骤叙述(快速模式行为不变) 语料回归:生产 118 条内建智能推理答案中 106 条识别出正文边界(被隐藏的 均为 59-164 字过渡句),12 条无推理的问候类答案保持原样;go vet 与 go test ./... 全绿。
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
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
|
|
}
|