docs(comments): 注释中文化 · internal/runtime/model + internal/services/backthink(54 文件,纯替换)
- 只翻注释/文档字符串:`go/scanner` 剥注释后按 token 序列+literal 逐 token 比对,**54/54 文件 SAME**;
改动文件总行数与 HEAD 漂移 = 0(严格 1:1,无合并/拆行)
- 裁决编号与 design 节号逐字保留(03.4.1/03.4.2/03.4.3/14.4.1/14.4.5/07.4.2-4、design/04 §3.1/§3.2 等);
`chat_template_kwargs` / `enable_thinking` / `reasoning_effort` / `thinking:{type}` 字段名与形状说明未动
- 存疑译法(已登记):`persona` 在叙事语境用「角色」、标识符语境保留 `persona`(仓库既有模块本身混用);
`rung`→「级」;`provider` 存在「提供商/提供者」两种落笔(可按需一键统一)
- 复核:build/vet 绿 + 两包测试绿;gofmt 干净;残留英文注释 0 条
- 既有存量(未修,只报告):`model/anthropic_test.go` 612 行 >500;`applyAnthropicEvent` 115 行 >80
This commit is contained in:
1 parent
f5cf117482
commit
3dc1232bc2
54 files changed
+1923
-1923
No files matched your search
@@ -8,9 +8,9 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// anthropicStreamEvent is one SSE payload from /v1/messages with stream: true.
|
||||
// Anthropic carries the event kind both in the SSE `event:` field and inside the
|
||||
// JSON `type` field, so the parser keys off the JSON payload type.
|
||||
// anthropicStreamEvent 是 /v1/messages(stream: true)的一个 SSE 负载。
|
||||
// Anthropic 既在 SSE 的 `event:` 字段里、也在 JSON 的 `type` 字段里
|
||||
// 携带事件种类,所以解析器按 JSON 负载的 type 取值。
|
||||
type anthropicStreamEvent struct {
|
||||
Type string `json:"type"`
|
||||
Message *anthropicStartBlock `json:"message,omitempty"`
|
||||
@@ -21,30 +21,30 @@ type anthropicStreamEvent struct {
|
||||
Error *anthropicStreamError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// anthropicStartBlock is the payload of a message_start event.
|
||||
// anthropicStartBlock 是 message_start 事件的负载。
|
||||
type anthropicStartBlock struct {
|
||||
Model string `json:"model"`
|
||||
Usage *anthropicUsage `json:"usage,omitempty"`
|
||||
}
|
||||
|
||||
// anthropicDelta is the incremental payload inside a content_block_delta event.
|
||||
// The message_delta event reuses the same field name for its terminal
|
||||
// stop_reason (end_turn / max_tokens / tool_use), so it is decoded here too.
|
||||
// anthropicDelta 是 content_block_delta 事件里的增量负载。
|
||||
// Anthropic 把 message_delta 事件的终止 stop_reason 复用了同一字段名
|
||||
// (end_turn / max_tokens / tool_use),所以这里也一并解码。
|
||||
type anthropicDelta struct {
|
||||
Type string `json:"type"` // text_delta | input_json_delta | thinking_delta
|
||||
Type string `json:"type"` // 取值:text_delta | input_json_delta | thinking_delta
|
||||
Text string `json:"text,omitempty"`
|
||||
PartialJSON string `json:"partial_json,omitempty"`
|
||||
Thinking string `json:"thinking,omitempty"`
|
||||
StopReason string `json:"stop_reason,omitempty"`
|
||||
}
|
||||
|
||||
// anthropicStreamError is the payload of an error event.
|
||||
// anthropicStreamError 是 error 事件的负载。
|
||||
type anthropicStreamError struct {
|
||||
Type string `json:"type"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// streamBlock tracks the partial state of one content block during streaming.
|
||||
// streamBlock 跟踪流式过程中单个内容块的部分状态。
|
||||
type streamBlock struct {
|
||||
index int
|
||||
typ string
|
||||
@@ -55,13 +55,13 @@ type streamBlock struct {
|
||||
initialArgs map[string]any
|
||||
}
|
||||
|
||||
// scanAnthropicSSE incrementally decodes Anthropic's content-block stream. Text
|
||||
// deltas are forwarded as text_delta events (segment bracketed by
|
||||
// text_start/text_end); tool_use input_json_delta fragments are forwarded as
|
||||
// tool_call_delta events (segment bracketed by tool_call_start/tool_call_end)
|
||||
// and accumulated per block so they re-assemble into a structured Args map when
|
||||
// the block stops. The stream must terminate with a message_stop event,
|
||||
// otherwise ErrStream is returned.
|
||||
// scanAnthropicSSE 增量解码 Anthropic 的内容块流。文本
|
||||
// 增量以 text_delta 事件转发(片段由
|
||||
// text_start/text_end 包起来);tool_use 的 input_json_delta 片段以
|
||||
// tool_call_delta 事件转发(片段由 tool_call_start/tool_call_end 包起来),
|
||||
// 并按块累积,以便块结束时重新拼装成结构化的 Args map。
|
||||
// 流必须以 message_stop 事件终止,
|
||||
// 否则返回 ErrStream。
|
||||
func scanAnthropicSSE(r io.Reader, onEvent func(StreamEvent)) (Response, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
scanner.Buffer(make([]byte, 0, sseBufInit), maxSSELineBytes)
|
||||
@@ -101,7 +101,7 @@ func scanAnthropicSSE(r io.Reader, onEvent func(StreamEvent)) (Response, error)
|
||||
return agg, nil
|
||||
}
|
||||
|
||||
// applyAnthropicEvent folds a single stream event into the running aggregation.
|
||||
// applyAnthropicEvent 把单个流事件折叠进正在运行的聚合状态。
|
||||
func applyAnthropicEvent(
|
||||
agg *Response,
|
||||
blocks map[int]*streamBlock,
|
||||
@@ -218,8 +218,8 @@ func applyAnthropicEvent(
|
||||
return nil
|
||||
}
|
||||
|
||||
// finalizeAnthropicStream assembles the aggregated Response from the accumulated
|
||||
// blocks: concatenated text plus any tool_use ToolCalls derived per block.
|
||||
// finalizeAnthropicStream 由累积的内容块拼装出聚合后的 Response:
|
||||
// 拼接的文本,加上按块导出的 tool_use ToolCall。
|
||||
func finalizeAnthropicStream(agg *Response, blocks map[int]*streamBlock, order []int, content string) {
|
||||
agg.Content = content
|
||||
agg.Message = Message{Role: RoleAssistant, Content: []Content{{Type: ContentTypeText, Text: content}}}
|
||||
|
||||
Reference in new issue
Block a user