fix(model): 解析 stop_reason + max_tokens 截断重试/非空兜底 — 不再静默空回复
- 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平台交叉编译
This commit is contained in:
1 parent
18b2040c95
commit
c7b2cd8ae0
10 files changed
+792
-200
No files matched your search
@@ -46,6 +46,11 @@ type StepResult struct {
|
||||
ToolCalls []ToolCall
|
||||
// StopReason classifies how the step ended (see the StopReason* consts).
|
||||
StopReason string
|
||||
// ModelStopReason is the provider's raw stop/finish reason for the model
|
||||
// call (e.g. Anthropic max_tokens / end_turn / tool_use), so a trace can
|
||||
// distinguish a token-budget truncation from a normal end. Empty when the
|
||||
// step never reached the model (guardrail denial).
|
||||
ModelStopReason string
|
||||
}
|
||||
|
||||
// RunStep executes one step of the agent loop. It assembles a model.Request from
|
||||
@@ -101,9 +106,9 @@ func (a *Agent) RunStep(ctx context.Context, sessionID string, messages []model.
|
||||
}
|
||||
|
||||
if calls, ok := ExtractToolCalls(resp); ok {
|
||||
return &StepResult{Final: false, ToolCalls: calls, StopReason: StopReasonToolCalls}, nil
|
||||
return &StepResult{Final: false, ToolCalls: calls, StopReason: StopReasonToolCalls, ModelStopReason: resp.StopReason}, nil
|
||||
}
|
||||
return &StepResult{Final: true, Text: resp.Content, StopReason: StopReasonStop}, nil
|
||||
return &StepResult{Final: true, Text: resp.Content, StopReason: StopReasonStop, ModelStopReason: resp.StopReason}, nil
|
||||
}
|
||||
|
||||
// ExtractToolCalls decodes tool calls from a model.Response. It prefers the
|
||||
|
||||
Reference in new issue
Block a user