docs(gateway): gateway 剩余文件注释改中文(逐行 1:1,代码零字节改动)
主人 2026-10-03 拍板「注释一律中文」;gateway 至此整包注释中文化完成(本会话共 3 批: 已推库 16 个文件 + 本批剩余文件)。 校验:机械校验(非注释改动行)= 空;剥注释后代码文本逐行比对全等(不一致文件数 0); 全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。
This commit is contained in:
1 parent
91e8d7a029
commit
074ffc4ebf
73 files changed
+2529
-2529
No files matched your search
@@ -16,14 +16,14 @@ import (
|
||||
personareg "git.yeij.top/AskaEth/Cyrene_v2/internal/persona/registry"
|
||||
)
|
||||
|
||||
// persona-to-identity and API-key admin writes (owner-only, audited). Persona
|
||||
// writes register into the persona registry (real runtime effect) and persist a
|
||||
// YAML file so they survive a restart; API keys are minted/revoked in the
|
||||
// auth.APIKey store.
|
||||
// 人格到身份与 API key 的管理写操作(仅 owner,带审计)。人格
|
||||
// 写操作注册进人格注册表(真实的运行时效果),并持久化
|
||||
// 一个 YAML 文件使其在重启后存活;API key 在
|
||||
// auth.APIKey 存储中铸造/吊销。
|
||||
|
||||
const personaDir = "configs/personas"
|
||||
|
||||
// personaUpsertRequest is the POST/PUT /api/v1/admin/personas body.
|
||||
// personaUpsertRequest 是 POST/PUT /api/v1/admin/personas 的请求体。
|
||||
type personaUpsertRequest struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -38,14 +38,14 @@ type personaUpsertRequest struct {
|
||||
Modes []string `json:"modes"`
|
||||
Mode string `json:"mode"`
|
||||
MemoryNS string `json:"memoryNamespace"`
|
||||
// Thinking is the optional reasoning/presentation block (design/01 §2.1). It
|
||||
// is persisted into the persona YAML as-is; omitting it on an update keeps the
|
||||
// stored block (see adminUpdatePersona).
|
||||
// Thinking 是可选的推理/呈现块(design/01 §2.1)。它
|
||||
// 原样持久化进人格 YAML;更新时省略它会保留
|
||||
// 已存块(见 adminUpdatePersona)。
|
||||
Thinking *personaThinkingRequest `json:"thinking,omitempty"`
|
||||
}
|
||||
|
||||
// personaThinkingRequest is the wire form of the persona `thinking` section. The
|
||||
// pointers/naming mirror the YAML so a panel can round-trip it verbatim.
|
||||
// personaThinkingRequest 是人格 `thinking` 段的线上形式。
|
||||
// 其指针/命名与 YAML 一致,使面板能原样往返。
|
||||
type personaThinkingRequest struct {
|
||||
BudgetTokens int `json:"budgetTokens,omitempty"`
|
||||
Effort string `json:"effort,omitempty"`
|
||||
@@ -53,8 +53,8 @@ type personaThinkingRequest struct {
|
||||
Mode string `json:"mode,omitempty"`
|
||||
}
|
||||
|
||||
// adminCreatePersona registers a new persona in the registry (real runtime
|
||||
// effect) and persists it to the persona YAML library so it survives a restart.
|
||||
// adminCreatePersona 在注册表中注册一个新人格(真实的运行时
|
||||
// 效果),并持久化进人格 YAML 库使其在重启后存活。
|
||||
func (s *Server) adminCreatePersona(w http.ResponseWriter, r *http.Request) {
|
||||
var req personaUpsertRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -103,8 +103,8 @@ func (s *Server) adminCreatePersona(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusCreated, s.personaSummaryOf(p, characterFromRequest(req)))
|
||||
}
|
||||
|
||||
// adminUpdatePersona edits an existing persona, persisting the YAML and
|
||||
// replacing the in-memory persona/character.
|
||||
// adminUpdatePersona 编辑既有人格,持久化 YAML 并
|
||||
// 替换内存中的人格/角色。
|
||||
func (s *Server) adminUpdatePersona(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
var req personaUpsertRequest
|
||||
@@ -135,10 +135,10 @@ func (s *Server) adminUpdatePersona(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "invalid thinking config: %v", err))
|
||||
return
|
||||
}
|
||||
// A client that does not know the optional `thinking` block (an older panel, a
|
||||
// script) omits it; keep the stored one instead of silently wiping a
|
||||
// hand-edited config. Sending the object replaces it, and sending it with all
|
||||
// fields default clears it.
|
||||
// 不认识可选 `thinking` 块的客户端(旧面板、脚本)会省略它;
|
||||
// 应保留已存的那个,而不是静默清掉手工编辑的
|
||||
// 配置。发送该对象则替换它,全字段为默认值
|
||||
// 时清除它。
|
||||
p.Thinking = applyThinkingEdit(req.Thinking, existingPersona(reg, id))
|
||||
if err := savePersonaYAML(p); err != nil {
|
||||
s.auditClaims(r, "persona.update", "error")
|
||||
@@ -160,11 +160,11 @@ func (s *Server) adminUpdatePersona(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, s.personaSummaryOf(p, characterFromRequest(req)))
|
||||
}
|
||||
|
||||
// adminCreateUser answers honestly: there is no persistent user table and no
|
||||
// user-authenticated login to back one (the deployment is a single admin
|
||||
// account from .env plus runtime API keys; design/11 is single-user first), so
|
||||
// user creation would be a row nobody could log in with. It reports the missing
|
||||
// store instead of fabricating a user.
|
||||
// adminCreateUser 诚实作答:既没有持久化用户表,也没有
|
||||
// 可供支撑的用户认证登录(部署形态是来自 .env 的单个管理
|
||||
// 账号加运行时 API key;design/11 是单用户优先),因此
|
||||
// 创建用户会是一行没人能登录的记录。它报告缺失的
|
||||
// 存储,而不是编造一个用户。
|
||||
func (s *Server) adminCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
s.auditClaims(r, "user.create", "error")
|
||||
writeError(w, &apperr.AppError{
|
||||
@@ -174,14 +174,14 @@ func (s *Server) adminCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// apiKeyIssueRequest is the POST /api/v1/admin/users/apikey body.
|
||||
// apiKeyIssueRequest 是 POST /api/v1/admin/users/apikey 的请求体。
|
||||
type apiKeyIssueRequest struct {
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
// adminIssueApiKey mints a real API key bound to a role and returns the plaintext
|
||||
// once (the only time it is visible). The id in the response is the masked form
|
||||
// the admin list shows, so it can be used to revoke the key later.
|
||||
// adminIssueApiKey 铸造一个绑定到某角色的真实 API key,并返回一次
|
||||
// 明文(唯一可见的一次)。响应中的 id 是管理列表所展示的
|
||||
// 脱敏形式,因此之后可用它吊销该 key。
|
||||
func (s *Server) adminIssueApiKey(w http.ResponseWriter, r *http.Request) {
|
||||
var req apiKeyIssueRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -210,8 +210,8 @@ func (s *Server) adminIssueApiKey(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"id": auth.MaskKey(key), "key": key, "role": string(role)})
|
||||
}
|
||||
|
||||
// adminRevokeApiKey revokes a key by its masked id (the identifier the admin list
|
||||
// returns). It reports not-found when no key carries that mask.
|
||||
// adminRevokeApiKey 按脱敏 id(管理列表返回的标识)吊销一个
|
||||
// key。没有 key 携带该掩码时报告 not-found。
|
||||
func (s *Server) adminRevokeApiKey(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
keys, ok := s.users.(*auth.APIKey)
|
||||
@@ -229,8 +229,8 @@ func (s *Server) adminRevokeApiKey(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"id": id, "revoked": true})
|
||||
}
|
||||
|
||||
// personaFromRequest builds a loader.Persona from the upsert request, filling a
|
||||
// derived system prompt/characterRef when omitted so a minimal create validates.
|
||||
// personaFromRequest 从 upsert 请求构建 loader.Persona,在省略时
|
||||
// 填入派生的 system prompt/characterRef,使最小创建也能通过校验。
|
||||
func personaFromRequest(req personaUpsertRequest) *loader.Persona {
|
||||
systemPrompt := strings.TrimSpace(req.SystemPrompt)
|
||||
if systemPrompt == "" {
|
||||
@@ -255,8 +255,8 @@ func personaFromRequest(req personaUpsertRequest) *loader.Persona {
|
||||
}
|
||||
}
|
||||
|
||||
// thinkingFromRequest maps the wire `thinking` block onto the persona field; a
|
||||
// missing block yields the zero value, which yaml.Marshal omits entirely.
|
||||
// thinkingFromRequest 把线上 `thinking` 块映射到人格字段;
|
||||
// 块缺失得到零值,yaml.Marshal 会完全省略它。
|
||||
func thinkingFromRequest(req *personaThinkingRequest) loader.ThinkingParams {
|
||||
if req == nil {
|
||||
return loader.ThinkingParams{}
|
||||
@@ -269,7 +269,7 @@ func thinkingFromRequest(req *personaThinkingRequest) loader.ThinkingParams {
|
||||
}
|
||||
}
|
||||
|
||||
// existingPersona returns the stored persona for id, or nil when it is absent.
|
||||
// existingPersona 返回 id 对应的已存人格,不存在时为 nil。
|
||||
func existingPersona(reg *personareg.Registry, id string) *loader.Persona {
|
||||
if p, ok := reg.Persona(id); ok {
|
||||
return p
|
||||
@@ -277,10 +277,10 @@ func existingPersona(reg *personareg.Registry, id string) *loader.Persona {
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyThinkingEdit returns the `thinking` block an update should persist: the
|
||||
// request's own block when it carries one, otherwise the stored persona's block —
|
||||
// so a client that does not know the optional field can never wipe it. An update
|
||||
// that sends the object replaces the block outright (all-default fields clear it).
|
||||
// applyThinkingEdit 返回更新应持久化的 `thinking` 块:请求自带
|
||||
// 块时用请求的,否则用已存人格的块 ——
|
||||
// 因此不认识该可选字段的客户端绝不可能清掉它。发送
|
||||
// 该对象的更新会直接替换整块(全默认字段则清除它)。
|
||||
func applyThinkingEdit(req *personaThinkingRequest, existing *loader.Persona) loader.ThinkingParams {
|
||||
if req != nil {
|
||||
return thinkingFromRequest(req)
|
||||
@@ -291,7 +291,7 @@ func applyThinkingEdit(req *personaThinkingRequest, existing *loader.Persona) lo
|
||||
return loader.ThinkingParams{}
|
||||
}
|
||||
|
||||
// characterFromRequest builds the registry.Character bound to the persona.
|
||||
// characterFromRequest 构建绑定到该人格的 registry.Character。
|
||||
func characterFromRequest(req personaUpsertRequest) *personareg.Character {
|
||||
modes := append([]string(nil), req.Modes...)
|
||||
if len(modes) == 0 && req.Mode != "" {
|
||||
@@ -300,10 +300,10 @@ func characterFromRequest(req personaUpsertRequest) *personareg.Character {
|
||||
return &personareg.Character{ID: req.ID, Name: req.Name, PersonaID: req.ID, Modes: modes}
|
||||
}
|
||||
|
||||
// personaSummaryOf maps a persona+character to its wire summary. It is the same
|
||||
// shape the admin list returns — the role-level mood plus the `thinking` block —
|
||||
// so a write response and a subsequent read describe the persona identically
|
||||
// (a client can trust either for backfill).
|
||||
// personaSummaryOf 把人格+角色映射为其线上摘要。它与
|
||||
// 管理列表返回的形状相同 —— 角色级 mood 加上 `thinking` 块 ——
|
||||
// 因此写响应与随后的读对人格的描述完全一致
|
||||
// (客户端可信任任一方用于回填)。
|
||||
func (s *Server) personaSummaryOf(p *loader.Persona, c *personareg.Character) PersonaSummary {
|
||||
return PersonaSummary{
|
||||
ID: p.ID,
|
||||
@@ -317,9 +317,9 @@ func (s *Server) personaSummaryOf(p *loader.Persona, c *personareg.Character) Pe
|
||||
}
|
||||
}
|
||||
|
||||
// savePersonaYAML persists p to the persona library as <id>.yaml so it survives
|
||||
// a restart (wire.go loads this directory). The id is sanitized so user input
|
||||
// cannot traverse out of the library directory.
|
||||
// savePersonaYAML 把 p 以 <id>.yaml 持久化进人格库,使其在
|
||||
// 重启后存活(wire.go 加载该目录)。id 会被净化,使用户
|
||||
// 输入无法穿越出库目录。
|
||||
func savePersonaYAML(p *loader.Persona) error {
|
||||
data, err := yaml.Marshal(p)
|
||||
if err != nil {
|
||||
@@ -335,8 +335,8 @@ func savePersonaYAML(p *loader.Persona) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// sanitizeID keeps only URL/path-safe characters so a persona id cannot escape
|
||||
// the persona directory.
|
||||
// sanitizeID 只保留 URL/路径安全字符,使人格 id 不能
|
||||
// 逃出人格目录。
|
||||
func sanitizeID(id string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range id {
|
||||
|
||||
Reference in new issue
Block a user