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
@@ -5,11 +5,11 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// buildAnthropicMessages splits a normalized message list into a top-level system
|
||||
// string and the remaining messages in Anthropic's content-block form. All
|
||||
// RoleSystem messages are concatenated into a single system prompt (Anthropic
|
||||
// carries it at the request root, not in messages); messages with no content
|
||||
// parts are dropped because Anthropic rejects empty content.
|
||||
// buildAnthropicMessages 把归一化消息列表拆成顶层的 system
|
||||
// 字符串与其余按 Anthropic 内容块形式表示的消息。所有
|
||||
// RoleSystem 消息拼接成单个 system 提示词(Anthropic
|
||||
// 把它放在请求根而非 messages 里);没有内容片段的消息
|
||||
// 会被丢弃,因为 Anthropic 拒绝空内容。
|
||||
func buildAnthropicMessages(msgs []Message) (string, []anthropicMessage, error) {
|
||||
var sys strings.Builder
|
||||
out := make([]anthropicMessage, 0, len(msgs))
|
||||
@@ -35,9 +35,9 @@ func buildAnthropicMessages(msgs []Message) (string, []anthropicMessage, error)
|
||||
return sys.String(), out, nil
|
||||
}
|
||||
|
||||
// messageText concatenates the text-bearing parts of a message (system prompts
|
||||
// and tool-result bodies). Tool-result parts carry their body in Text, so a
|
||||
// RoleTool turn's content serializes correctly for OpenAI's tool message.
|
||||
// messageText 拼接消息中带文本的片段(system 提示词
|
||||
// 与工具结果正文)。工具结果片段把正文放在 Text 里,
|
||||
// 因此 RoleTool 回合的内容能正确序列化为 OpenAI 的 tool message。
|
||||
func messageText(m Message) string {
|
||||
var b strings.Builder
|
||||
for _, part := range m.Content {
|
||||
@@ -48,23 +48,23 @@ func messageText(m Message) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// anthropicRole maps a normalized Role to the Anthropic role vocabulary. system
|
||||
// messages are extracted to the top-level system field and never reach here.
|
||||
// anthropicRole 把归一化的 Role 映射到 Anthropic 的角色词汇。
|
||||
// system 消息已被抽取到顶层 system 字段,不会走到这里。
|
||||
func anthropicRole(r Role) string {
|
||||
switch r {
|
||||
case RoleAssistant:
|
||||
return "assistant"
|
||||
default:
|
||||
// user, tool and any future roles all land on the user turn; tool_result
|
||||
// blocks (M2) belong to the user role in Anthropic.
|
||||
// user、tool 以及未来任何角色都落在 user 回合上;
|
||||
// tool_result 块(M2)在 Anthropic 里属于 user 角色。
|
||||
return "user"
|
||||
}
|
||||
}
|
||||
|
||||
// encodeAnthropicMessage turns a normalized message into Anthropic content
|
||||
// blocks. A RoleTool turn is ALWAYS rendered as native tool_result blocks (never
|
||||
// a user plain-text turn), because Anthropic requires a tool result to arrive as
|
||||
// a tool_result block carrying the tool_use_id and the is_error flag.
|
||||
// encodeAnthropicMessage 把归一化消息转成 Anthropic 内容块。
|
||||
// RoleTool 回合永远渲染为原生 tool_result 块
|
||||
// (绝不是 user 纯文本回合),因为 Anthropic 要求工具结果以
|
||||
// 携带 tool_use_id 与 is_error 标志的 tool_result 块形式到达。
|
||||
func encodeAnthropicMessage(m Message) ([]any, error) {
|
||||
if m.Role == RoleTool {
|
||||
return encodeAnthropicToolResult(m)
|
||||
@@ -72,11 +72,11 @@ func encodeAnthropicMessage(m Message) ([]any, error) {
|
||||
return encodeAnthropicContent(m.Content)
|
||||
}
|
||||
|
||||
// encodeAnthropicToolResult renders a RoleTool turn as tool_result blocks. Each
|
||||
// ContentTypeToolResult part becomes one block; legacy text parts are wrapped as
|
||||
// tool_result blocks too so the turn never degrades into a plain user message.
|
||||
// The tool_use_id falls back to the message-level ToolCallID when a part does not
|
||||
// carry its own.
|
||||
// encodeAnthropicToolResult 把 RoleTool 回合渲染为 tool_result 块。
|
||||
// 每个 ContentTypeToolResult 片段变成一个块;遗留的文本片段也会
|
||||
// 被包成 tool_result 块,使该回合永远不会退化成普通 user 消息。
|
||||
// 当片段自身没有携带 tool_use_id 时,回退到消息级的
|
||||
// ToolCallID。
|
||||
func encodeAnthropicToolResult(m Message) ([]any, error) {
|
||||
blocks := make([]any, 0, len(m.Content))
|
||||
for _, p := range m.Content {
|
||||
@@ -95,9 +95,9 @@ func encodeAnthropicToolResult(m Message) ([]any, error) {
|
||||
return blocks, nil
|
||||
}
|
||||
|
||||
// encodeAnthropicContent turns normalized content parts into Anthropic content
|
||||
// blocks. Text / image / tool_use (assistant) parts are handled here; tool_result
|
||||
// parts are rendered at the message level by encodeAnthropicToolResult.
|
||||
// encodeAnthropicContent 把归一化内容片段转成 Anthropic 内容块。
|
||||
// 文本 / 图片 / tool_use(助手)片段在此处理;tool_result
|
||||
// 片段由 encodeAnthropicToolResult 在消息级渲染。
|
||||
func encodeAnthropicContent(parts []Content) ([]any, error) {
|
||||
blocks := make([]any, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
@@ -137,9 +137,9 @@ func encodeAnthropicContent(parts []Content) ([]any, error) {
|
||||
return blocks, nil
|
||||
}
|
||||
|
||||
// parseAnthropicImageData extracts the media type and base64 payload from a data:
|
||||
// URI. Anthropic requires a base64 image source and cannot reference a remote
|
||||
// URL, so any other URI form is rejected as a bad request.
|
||||
// parseAnthropicImageData 从 data: URI 中提取媒体类型与 base64
|
||||
// 负载。Anthropic 要求 base64 图片源、无法引用远程
|
||||
// URL,因此其它任何 URI 形式都按错误请求拒绝。
|
||||
func parseAnthropicImageData(uri string) (string, string, error) {
|
||||
if !strings.HasPrefix(uri, anthropicDataPrefix) {
|
||||
return "", "", fmt.Errorf("model: %w: anthropic image requires a data: URI, got %q", ErrBadRequest, uri)
|
||||
@@ -161,7 +161,7 @@ func parseAnthropicImageData(uri string) (string, string, error) {
|
||||
return mediaType, data, nil
|
||||
}
|
||||
|
||||
// buildAnthropicTools converts ToolSpec schemas into Anthropic tools.
|
||||
// buildAnthropicTools 把 ToolSpec schema 转换为 Anthropic 工具。
|
||||
func buildAnthropicTools(tools []ToolSpec) []anthropicTool {
|
||||
if len(tools) == 0 {
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user