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,已归因非本次引入)
This commit is contained in:
1 parent
e4e18ce2a2
commit
38a403afca
22 files changed
+1714
-56
No files matched your search
@@ -104,6 +104,9 @@ type Loop struct {
|
||||
toolParallelism int
|
||||
hooks ToolLoopHooks
|
||||
onStream func(model.StreamEvent)
|
||||
thinking *model.Thinking
|
||||
reasoningEffort string
|
||||
thinkingShow *bool
|
||||
onAudit func(AuditEvent)
|
||||
onTurnEnd TurnEndHook
|
||||
maxContextTokens int
|
||||
@@ -157,6 +160,9 @@ func New(opts Options) *Loop {
|
||||
toolParallelism: toolParallelism,
|
||||
hooks: opts.Hooks,
|
||||
onStream: opts.OnStream,
|
||||
thinking: opts.Thinking,
|
||||
reasoningEffort: opts.ReasoningEffort,
|
||||
thinkingShow: opts.ThinkingShowToUser,
|
||||
onAudit: opts.OnAudit,
|
||||
onTurnEnd: opts.OnTurnEnd,
|
||||
maxContextTokens: opts.MaxContextTokens,
|
||||
|
||||
@@ -89,6 +89,22 @@ type Options struct {
|
||||
// forwarded to this callback (model.StreamEvent), so the frontend can render
|
||||
// body text, tool calls and (eventually) reasoning segments independently.
|
||||
OnStream func(model.StreamEvent)
|
||||
// Thinking carries the reasoning knobs (budget / explicit switch) injected
|
||||
// into every model request the loop builds. nil means "do not intervene",
|
||||
// which is the pre-existing behavior. It is a loop option rather than a
|
||||
// persona read inside the loop so the per-turn, per-role resolution a later
|
||||
// multi-character batch adds only changes the wiring-time resolver.
|
||||
Thinking *model.Thinking
|
||||
// ReasoningEffort is the OpenAI-compatible strength knob injected into every
|
||||
// model request. Empty means "do not intervene".
|
||||
ReasoningEffort string
|
||||
// ThinkingShowToUser gates the thinking frames the loop broadcasts: nil (the
|
||||
// default) keeps the pre-existing behavior and pushes them; an explicit false
|
||||
// keeps thinking_start/delta/end out of the client stream while text and tool
|
||||
// frames are unaffected. The frames are still produced and still reach the
|
||||
// model/transcript paths — only the UI broadcast is gated, so display and
|
||||
// persistence stay decoupled (design/04 §3.2).
|
||||
ThinkingShowToUser *bool
|
||||
// ToolExecution selects how a batch of tool calls is run: "parallel" (the
|
||||
// default) or "sequential". Empty means parallel.
|
||||
ToolExecution string
|
||||
|
||||
@@ -13,12 +13,14 @@ import (
|
||||
// RunStep (leading instruction message, tool schemas, model routing).
|
||||
func (l *Loop) buildRequest(ctx context.Context, messages []model.Message) model.Request {
|
||||
req := model.Request{
|
||||
Model: l.agent.Model(),
|
||||
Messages: l.assembleMessages(messages),
|
||||
Temperature: l.temperature(),
|
||||
MaxTokens: l.agent.MaxTokens(),
|
||||
Purpose: l.agent.Purpose(),
|
||||
Tools: l.toolSchemas(ctx, messages),
|
||||
Model: l.agent.Model(),
|
||||
Messages: l.assembleMessages(messages),
|
||||
Temperature: l.temperature(),
|
||||
MaxTokens: l.agent.MaxTokens(),
|
||||
Purpose: l.agent.Purpose(),
|
||||
Tools: l.toolSchemas(ctx, messages),
|
||||
Thinking: l.thinking,
|
||||
ReasoningEffort: l.reasoningEffort,
|
||||
}
|
||||
if l.agent.Router() != nil {
|
||||
if m, _ := l.agent.Router().RouteRequest(req); m != "" {
|
||||
|
||||
@@ -39,8 +39,11 @@ func (l *Loop) streamStep(ctx context.Context, messages []model.Message) (*stepO
|
||||
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.
|
||||
if l.onStream != nil && !isMutedBroadcast(ctx) {
|
||||
// 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)
|
||||
}
|
||||
})
|
||||
@@ -65,3 +68,25 @@ func (l *Loop) streamStep(ctx context.Context, messages []model.Message) (*stepO
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package loop
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
)
|
||||
|
||||
// thinkingAdapter emits one full thinking segment followed by one text segment,
|
||||
// and records every request it is handed so a test can assert the loop's
|
||||
// reasoning knobs actually reached the model call.
|
||||
type thinkingAdapter struct {
|
||||
reqs []model.Request
|
||||
}
|
||||
|
||||
func (a *thinkingAdapter) Name() string { return "thinking-mock" }
|
||||
|
||||
func (a *thinkingAdapter) Chat(_ context.Context, req model.Request, _ model.Options) (model.Response, error) {
|
||||
a.reqs = append(a.reqs, req)
|
||||
return model.Response{Content: "Hello"}, nil
|
||||
}
|
||||
|
||||
func (a *thinkingAdapter) ChatStream(_ context.Context, req model.Request, on func(model.StreamEvent)) (model.Response, error) {
|
||||
a.reqs = append(a.reqs, req)
|
||||
events := []model.StreamEvent{
|
||||
{Kind: model.StreamKindThinkingStart},
|
||||
{Kind: model.StreamKindThinkingDelta, Delta: "let me think"},
|
||||
{Kind: model.StreamKindThinkingEnd},
|
||||
{Kind: model.StreamKindTextStart},
|
||||
{Kind: model.StreamKindTextDelta, Delta: "Hello"},
|
||||
{Kind: model.StreamKindTextEnd},
|
||||
}
|
||||
for _, ev := range events {
|
||||
if on != nil {
|
||||
on(ev)
|
||||
}
|
||||
}
|
||||
return model.Response{Content: "Hello"}, nil
|
||||
}
|
||||
|
||||
// kindsOf returns the event kinds as a set for compact assertions.
|
||||
func kindsOf(events []model.StreamEvent) map[model.StreamKind]int {
|
||||
out := make(map[model.StreamKind]int, len(events))
|
||||
for _, ev := range events {
|
||||
out[ev.Kind]++
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// runThinkingTurn drives one streaming turn through a loop configured with the
|
||||
// given broadcast switch and returns the events OnStream saw.
|
||||
func runThinkingTurn(t *testing.T, show *bool) ([]model.StreamEvent, *thinkingAdapter) {
|
||||
t.Helper()
|
||||
ad := &thinkingAdapter{}
|
||||
onStream, streams := collectStream()
|
||||
l := New(Options{
|
||||
Agent: newTestAgent(t, ad, nil),
|
||||
OnStream: onStream,
|
||||
MaxRounds: 8,
|
||||
ThinkingShowToUser: show,
|
||||
})
|
||||
if _, err := l.Run(context.Background(), "web", []model.Message{model.TextMessage(model.RoleUser, "hi")}); err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
return streams(), ad
|
||||
}
|
||||
|
||||
// TestRun_ThinkingShowToUserFalseWithholdsThinkingFrames is the showToUser=false
|
||||
// contract: thinking frames never reach the client callback while the body text
|
||||
// segment is delivered untouched (display is gated, the answer is not).
|
||||
func TestRun_ThinkingShowToUserFalseWithholdsThinkingFrames(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
show := false
|
||||
events, _ := runThinkingTurn(t, &show)
|
||||
kinds := kindsOf(events)
|
||||
for _, k := range []model.StreamKind{model.StreamKindThinkingStart, model.StreamKindThinkingDelta, model.StreamKindThinkingEnd} {
|
||||
if n := kinds[k]; n != 0 {
|
||||
t.Fatalf("event kind %q emitted %d time(s) with showToUser=false", k, n)
|
||||
}
|
||||
}
|
||||
for _, k := range []model.StreamKind{model.StreamKindTextStart, model.StreamKindTextDelta, model.StreamKindTextEnd} {
|
||||
if kinds[k] != 1 {
|
||||
t.Fatalf("event kind %q emitted %d time(s), want 1 (body must be unaffected)", k, kinds[k])
|
||||
}
|
||||
}
|
||||
if len(events) != 3 {
|
||||
t.Fatalf("events = %d, want only the 3 text frames", len(events))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRun_ThinkingShowToUserDefaultBroadcastsThinking keeps the pre-existing
|
||||
// behavior when the option is unset (nil): thinking frames are pushed.
|
||||
func TestRun_ThinkingShowToUserDefaultBroadcastsThinking(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
events, _ := runThinkingTurn(t, nil)
|
||||
kinds := kindsOf(events)
|
||||
if kinds[model.StreamKindThinkingDelta] != 1 || kinds[model.StreamKindThinkingStart] != 1 || kinds[model.StreamKindThinkingEnd] != 1 {
|
||||
t.Fatalf("thinking frames = %v, want start/delta/end each once with the option unset", kinds)
|
||||
}
|
||||
if len(events) != 6 {
|
||||
t.Fatalf("events = %d, want all 6 frames", len(events))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRun_ThinkingShowToUserTrueBroadcastsThinking asserts an explicit true
|
||||
// behaves like the default.
|
||||
func TestRun_ThinkingShowToUserTrueBroadcastsThinking(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
show := true
|
||||
events, _ := runThinkingTurn(t, &show)
|
||||
if kinds := kindsOf(events); kinds[model.StreamKindThinkingDelta] != 1 {
|
||||
t.Fatalf("thinking delta frames = %d, want 1 with showToUser=true", kinds[model.StreamKindThinkingDelta])
|
||||
}
|
||||
}
|
||||
|
||||
// TestRun_RequestCarriesThinkingKnobs asserts the loop injects its reasoning
|
||||
// options into every model request it builds, so a persona's thinking config is
|
||||
// actually applied rather than validated-and-ignored.
|
||||
func TestRun_RequestCarriesThinkingKnobs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ad := &thinkingAdapter{}
|
||||
onStream, _ := collectStream()
|
||||
want := &model.Thinking{BudgetTokens: 4096}
|
||||
l := New(Options{
|
||||
Agent: newTestAgent(t, ad, nil),
|
||||
OnStream: onStream,
|
||||
MaxRounds: 8,
|
||||
Thinking: want,
|
||||
ReasoningEffort: "low",
|
||||
})
|
||||
if _, err := l.Run(context.Background(), "web", []model.Message{model.TextMessage(model.RoleUser, "hi")}); err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
if len(ad.reqs) == 0 {
|
||||
t.Fatal("no model request was recorded")
|
||||
}
|
||||
got := ad.reqs[0]
|
||||
if got.Thinking != want {
|
||||
t.Fatalf("request.Thinking = %+v, want %+v", got.Thinking, want)
|
||||
}
|
||||
if got.Thinking.BudgetTokens != 4096 {
|
||||
t.Fatalf("request.Thinking.BudgetTokens = %d, want 4096", got.Thinking.BudgetTokens)
|
||||
}
|
||||
if got.ReasoningEffort != "low" {
|
||||
t.Fatalf("request.ReasoningEffort = %q, want low", got.ReasoningEffort)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user