Files
Cyrene_v2/internal/runtime/loop/step.go
T
Aska 33eeadfc38
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
fix(backthink): 后台思考静默广播——不再把内部推理泄到聊天思维链 + XML 动作/旁白解析
【静默广播修复(bug:思维链露出 backthink 推理)】
- 根因:LoopThinker.Think 复用同一 loop.Loop 且 loop 全局带 OnStream,backthink 后台思考时
  把 [background autonomous reflection] 的 thinking/流式事件广播到前端,被当聊天思维链显示。
- loop/mute_ctx.go:WithMutedBroadcast(ctx) + isMutedBroadcast(ctx)——context 上静默标志。
- loop 四处广播抑制(stream.go 流式 / step.go 静默走非流式 agentStep / request.go capacity 事件 /
  tools.go tool_execution),静默时跳过 UI 广播,推理照常返回结论。
- thinker.go LoopThinker.Think 用 loop.WithMutedBroadcast(ctx),backthink 不再泄 thinking。

【XML 动作/旁白结构化解析】
- cyrene.yaml systemPrompt 加「表达方式:动作与旁白」——动作 <action>…</action>、
  旁白 <narration>…</narration>;克制≤1-2个/段、贴合唤醒态、重要信息不加、朗读正文忽略标签。
- 前端 parseContent.ts:ContentPart[](text/action/narration),非贪婪+反向引用、跨行、非法/
  未闭合兜底纯文本不崩;summarizeContent 供思维导图/状态。
- 前端 renderContent.tsx:action/narration → 灰斜体(mu-text-lo italic)句中原位渲染。
- 应用到 chat.tsx UserBubble/AssistantBubble + messages 各 mode body。

go build/vet/test(loop/backthink)绿 + 前端 tsc/vite 绿。
2026-09-06 22:09:51 +08:00

67 lines
2.2 KiB
Go

package loop
import (
"context"
"fmt"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
)
// stepOutcome is the loop's internal view of one model step, normalized from the
// agent's *agent.StepResult (non-streaming) or the equivalent built by the
// streaming path.
type stepOutcome struct {
final bool
text string
toolCalls []agent.ToolCall
stopReason string
}
// step runs a single model call. When an OnStream callback is configured it
// drives the adapter's ChatStream directly, threading deltas to OnStream and
// applying the loop's guardrails; otherwise it delegates to the agent's RunStep,
// which runs the agent's own guardrails and records its own CallRecord. The whole
// call is wrapped in an llm_call span so the M9 panel sees each LLM invocation.
func (l *Loop) step(ctx context.Context, sessionID string, messages []model.Message) (*stepOutcome, error) {
_, llmSpan := l.span(ctx, "llm.call", trace.SpanKindLLM)
var (
out *stepOutcome
err error
)
// Muted (internal autonomous pass) bypasses streaming: it neither streams to
// the UI nor pays the streaming path's extra overhead. Normal chat streams.
if l.onStream != nil && !isMutedBroadcast(ctx) {
out, err = l.streamStep(ctx, messages)
} else {
out, err = l.agentStep(ctx, sessionID, messages)
}
if err != nil {
l.spanProp(llmSpan, "error", err.Error())
l.finishSpan(llmSpan, trace.SpanStatusError)
return nil, fmt.Errorf("loop: model step: %w", err)
}
l.spanProp(llmSpan, "stop_reason", out.stopReason)
l.spanProp(llmSpan, "tool_calls", len(out.toolCalls))
l.finishSpan(llmSpan, trace.SpanStatusOK)
return out, nil
}
// agentStep delegates a non-streaming step to the agent, which runs input and
// output guardrails, calls the model and decodes any tool calls.
func (l *Loop) agentStep(ctx context.Context, sessionID string, messages []model.Message) (*stepOutcome, error) {
sr, err := l.agent.RunStep(ctx, sessionID, messages)
if err != nil {
return nil, fmt.Errorf("loop: agent step: %w", err)
}
return &stepOutcome{
final: sr.Final,
text: sr.Text,
toolCalls: sr.ToolCalls,
stopReason: sr.StopReason,
}, nil
}