Files
Cyrene_v2/internal/runtime/loop/stream.go
T
AskaEth 38a403afca
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(persona,model,loop): 人格 thinking 配置 + 5 个死字段接线 + 推理旋钮(按 provider 映射)
按 DevDocs/design/01 §2.1/§2.2 与 design/04 §3.2 实现(规格含联网核过的官方依据:思考内容刻意不角色化、不写分步思考指令、思考控制只走参数级)。
- 人格 schema 新增 thinking:budgetTokens / effort / showToUser(*bool,省略=nil=保持现行为,显式 false=只落库) / mode(default|on|off→Enable 三态);非法 mode/负预算 → ErrInvalidThinking(不静默)
- 5 个死字段接上注入:personality→values→speechStyle→catchphrase→background 拼为独立段落(带【】小标题,空值不拼);**全空时输出与旧实现逐字节一致**(有专门测试)
- model.Request 新增 Thinking{ BudgetTokens, Enable } 与 ReasoningEffort;新增 model/reasoning.go:厂商识别(模型名→baseURL 兜底)、取值白名单(OpenAI 系 none…max / DeepSeek low|high|max)、非法值降级+WARN、**不支持一律 WARN 而非静默**;Anthropic 映射 thinking{budget_tokens} 并**保证 budget<max_tokens**(不足时抬高 max_tokens+WARN,正是历史上思考吃光预算→空回复那个坑)
- loop:新增 Options.ThinkingShowToUser(*bool) 仅门控 thinking_* 帧(正文/工具帧不受影响);场景级 mute 语义不变
- 补一处任务清单外但必需的接线:persona→model.Request 的 thinking 映射(否则 budget/effort/mode 会变成新的死字段);showToUser 刻意不产生 Thinking(纯呈现开关)
- 管理端:personaUpsertRequest 补 thinking DTO + 校验(400);PUT 省略时**保留已存块**(防旧客户端抹配置);写侧 yaml round-trip 已验证
- Docs/api-client v0.40→v0.41(可选请求字段)
- 测试:26 个新用例(loader 解析/拼接逐字节/映射、anthropic 非流式+流式预算约束、openai 透传/降级/丢弃、Qwen 诚实 WARN、loop 门控、DTO round-trip);3 次变异验证(省略与 false 混同 / 去掉预算约束 / 短路门控 各自 FAIL)
- 验收:go build/vet/test 全绿(含 57 包)+ linux/darwin 交叉编译 + 前端 build;唯一 FAIL 是既有 flake TestInterruptCancelsTurnAndClearsPending(3/20 新代码 vs 3/20 纯 HEAD,已归因非本次引入)
2026-10-03 19:54:22 +08:00

93 lines
3.5 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"
)
// streamStep drives a streaming model call. It is the streaming counterpart of
// agent.RunStep: it builds the request from the agent's public surface, runs the
// loop's input guardrail, streams deltas through OnStream, runs the output
// guardrail and decodes any tool calls. Because RunStep does not expose a stream
// path, this method is owned by the loop; it mirrors the agent's semantics so the
// two behave identically apart from the delta callback.
func (l *Loop) streamStep(ctx context.Context, messages []model.Message) (*stepOutcome, error) {
adapter := l.agent.Adapter()
if adapter == nil {
return nil, fmt.Errorf("loop: stream step: %w", ErrNoAdapter)
}
req := l.buildRequest(ctx, messages)
if l.guards != nil {
verdict, err := l.guards.CheckInput(ctx, &req)
if err != nil {
return nil, fmt.Errorf("loop: check input: %w", err)
}
if verdict != nil && !verdict.Allow {
return &stepOutcome{final: true, text: verdict.Reason, stopReason: agent.StopReasonGuardrailInput}, nil
}
}
resp, err := adapter.ChatStream(ctx, req, func(ev model.StreamEvent) {
// Stamp the session/turn identity at the loop boundary: the adapter runs
// below the loop and cannot know which conversation the stream belongs to,
// so the loop fills it from the context it was driven with (design/16 §4.2)
// and the frontend can route the delta to the active session.
ev.SessionID = loopSession(ctx)
ev.TurnID = loopTurn(ctx)
// Suppress UI broadcast for internal autonomous passes (backthink),
// whose intermediate reasoning must not leak into the chat stream, and for
// thinking frames when the bound persona turned them off (showToUser:
// false). Both gates are broadcast-only: the response the model produced is
// unchanged, so display and persistence stay decoupled.
if l.onStream != nil && !isMutedBroadcast(ctx) && l.broadcasts(ev) {
l.onStream(ev)
}
})
if err != nil {
return nil, fmt.Errorf("loop: chat stream: %w", err)
}
if l.guards != nil {
verdict, err := l.guards.CheckOutput(ctx, &resp)
if err != nil {
return nil, fmt.Errorf("loop: check output: %w", err)
}
if verdict != nil && !verdict.Allow {
return &stepOutcome{final: true, text: verdict.Reason, stopReason: agent.StopReasonGuardrailOutput}, nil
}
}
if calls, ok := decodeToolCalls(resp); ok {
return &stepOutcome{final: false, toolCalls: calls, stopReason: agent.StopReasonToolCalls,
modelStopReason: resp.StopReason, model: resp.Model}, nil
}
return &stepOutcome{final: true, text: resp.Content, stopReason: agent.StopReasonStop,
modelStopReason: resp.StopReason, model: resp.Model}, nil
}
// broadcasts reports whether a streamed event may be pushed to the UI. Only
// thinking frames are ever withheld, and only when the wiring resolved the
// persona's showToUser to false; text, tool-call and tool-progress frames are
// never affected, so turning thinking off cannot change what the client sees of
// the answer itself.
func (l *Loop) broadcasts(ev model.StreamEvent) bool {
if !isThinkingKind(ev.Kind) {
return true
}
return l.thinkingShow == nil || *l.thinkingShow
}
// isThinkingKind reports whether kind is one of the reasoning segment events.
func isThinkingKind(kind model.StreamKind) bool {
switch kind {
case model.StreamKindThinkingStart, model.StreamKindThinkingDelta, model.StreamKindThinkingEnd:
return true
default:
return false
}
}