- model.go: Response.StopReason; anthropic wire加stop_reason; openai finish_reason映射(两协议统一,原值透传)
- truncation.go(新,159行): effectiveMaxTokens/retryMaxTokens(×2封顶32768)/isTruncatedEmpty(空正文+无tool_use+stop_reason∈{max_tokens,length})/withEmptyBodyHint/callWithTruncationRetry/callStreamWithTruncationRetry/dropThinking/emitTextSegment
- anthropic.go Chat/ChatStream 走重试+兜底; anthropic_encode.go(新)拆编码(anthropic.go 529→357合规); anthropic_stream message_delta落StopReason
- agent/loop: 把模型层StopReason带到llm.call span的model_stop_reason属性(修掉trace硬编码stop的误导); loop.Result.StopReason语义未动
- 触发仅当截断空正文; tool_use空正文永不触发(native_toolcall_test仍过); 至多1次重试; 兜底回'(思考过长,未能生成回复)'绝不用thinking
- 新增7测试(非流式/流式重试+兜底+tool_use不触发+封顶+openai映射), build/vet/test绿+3平台交叉编译
60 lines
2.1 KiB
Go
60 lines
2.1 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) {
|
|
// Suppress UI broadcast for internal autonomous passes (backthink),
|
|
// whose intermediate reasoning must not leak into the chat stream.
|
|
if l.onStream != nil && !isMutedBroadcast(ctx) {
|
|
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}, nil
|
|
}
|
|
return &stepOutcome{final: true, text: resp.Content, stopReason: agent.StopReasonStop, modelStopReason: resp.StopReason}, nil
|
|
}
|