docs(comments): 注释中文化 · internal/runtime/memory(31 文件,纯替换)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- 只翻注释/文档字符串:820 增 / 820 删 严格 1:1;剥掉整行注释与行尾注释尾巴后,
  逐文件与 HEAD 比对「代码部分差异 = 0」;裁决/design 编号计数逐项与 HEAD 一致
- 保留可执行 SQL 样例、SQLite 报错原文、pgvector/HNSW/ANN/BLOB 等技术词原文
- 复核:go build 绿 + memory/projectindex/vector 三包测试绿;gofmt 仅剩 2 个既有未格式化文件(改动前即如此,未顺手修)
This commit is contained in:
AskaEth committed 2026-10-04 13:32:54 +08:00
1 parent eb1828ee7a
commit 61cf80d5ee
31 files changed
+820 -820

No files matched your search

+32 -32
View File
@@ -9,28 +9,28 @@ import (
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// Working memory sizing and summarization constants.
// 工作记忆的窗口大小与摘要常量。
const (
// defaultWorkingWindow is the default number of messages retained in the
// sliding window when NewWorkingMemory is called with a non-positive size.
// defaultWorkingWindow 是 NewWorkingMemory 以非正 size
// 调用时滑动窗口默认保留的消息数。
defaultWorkingWindow = 20
// summaryLastTextMax is the maximum rune length of the trailing message text
// included in a naive summary, keeping self-summaries compact.
// summaryLastTextMax 是朴素摘要中所含末条消息文本
// 的最大 rune 数,用于保持自摘要紧凑。
summaryLastTextMax = 80
)
// WorkingMemory is the per-session short-term memory: a sliding window of the
// current conversation's messages plus a summary (design §0 layer ①). It is the
// immediate context an agent reasons over.
// WorkingMemory 是每会话的短期记忆:当前对话消息的
// 滑动窗口加一份摘要(design §0 layer ①)。它是
// 代理推理所依据的即时上下文。
//
// The window is bounded: adding beyond the maximum evicts the oldest message.
// Summarize returns the summary set via SetSummary, or a naive structural
// summary computed on demand. LLM-generated summaries are deferred to M4; the
// plumbing is in place so a summarizer can call SetSummary.
// 窗口有界:超出上限的添加会淘汰最旧的消息。
// Summarize 返回经 SetSummary 设置的摘要,或按需计算的
// 朴素结构化摘要。LLM 生成的摘要推迟到 M4;管线
// 已就位,摘要器可调用 SetSummary。
//
// WorkingMemory is safe for concurrent use; Add/SetSummary serialize with a
// write lock and Window/Summarize read under a read lock.
// WorkingMemory 并发安全;Add/SetSummary 以写锁串行,
// Window/Summarize 在读锁下读取。
type WorkingMemory struct {
mu sync.RWMutex
window []model.Message
@@ -38,8 +38,8 @@ type WorkingMemory struct {
summary string
}
// NewWorkingMemory builds a working memory with a sliding window of up to
// maxMessages messages. A non-positive maxMessages selects defaultWorkingWindow.
// NewWorkingMemory 构建一个工作记忆,滑动窗口最多
// maxMessages 条消息。非正 maxMessages 选取 defaultWorkingWindow。
func NewWorkingMemory(maxMessages int) *WorkingMemory {
if maxMessages <= 0 {
maxMessages = defaultWorkingWindow
@@ -47,8 +47,8 @@ func NewWorkingMemory(maxMessages int) *WorkingMemory {
return &WorkingMemory{max: maxMessages}
}
// Add appends msg to the window and evicts the oldest message when the window
// exceeds its limit.
// Add 把 msg 追加到窗口,并在窗口超出上限时
// 淘汰最旧的消息。
func (w *WorkingMemory) Add(msg model.Message) {
w.mu.Lock()
defer w.mu.Unlock()
@@ -61,9 +61,9 @@ func (w *WorkingMemory) Add(msg model.Message) {
}
}
// Window returns a deep copy of the current message window, oldest first. The
// returned slice and each message's content parts are detached from internal
// state, so callers may mutate them freely.
// Window 返回当前消息窗口的深拷贝,最旧的在前。
// 返回的切片与每条消息的 content 部分都与内部
// 状态脱离,因此调用方可以随意修改。
func (w *WorkingMemory) Window() []model.Message {
w.mu.RLock()
defer w.mu.RUnlock()
@@ -75,9 +75,9 @@ func (w *WorkingMemory) Window() []model.Message {
return out
}
// cloneMessage returns a copy of m whose content parts are detached from the
// source. Every field of model.Content is a value type, so copying the parts
// slice is sufficient.
// cloneMessage 返回 m 的副本,其 content 部分与源脱离。
// model.Content 的每个字段都是值类型,因此复制 parts
// 切片就足够了。
func cloneMessage(m model.Message) model.Message {
parts := make([]model.Content, len(m.Content))
copy(parts, m.Content)
@@ -85,16 +85,16 @@ func cloneMessage(m model.Message) model.Message {
return m
}
// SetSummary records an explicit summary (e.g. produced by an LLM in M4).
// SetSummary 记录一条显式摘要(例如 M4 中由 LLM 产出)。
func (w *WorkingMemory) SetSummary(s string) {
w.mu.Lock()
defer w.mu.Unlock()
w.summary = s
}
// Summarize returns the stored summary when one was set, otherwise a naive
// structural summary derived from the current window. It never mutates state, so
// repeated calls return a value consistent with the window at call time.
// Summarize 在已设置摘要时返回存储的摘要,否则返回由当前
// 窗口推导的朴素结构化摘要。它从不修改状态,因此
// 重复调用返回与调用时窗口一致的值。
func (w *WorkingMemory) Summarize() string {
w.mu.RLock()
defer w.mu.RUnlock()
@@ -105,8 +105,8 @@ func (w *WorkingMemory) Summarize() string {
return w.naiveSummaryLocked()
}
// naiveSummaryLocked builds "{n} 条消息, {role} {count}, ...; 最近: {text}". It
// requires w.mu (read or write) to be held.
// naiveSummaryLocked 构造 "{n} 条消息, {role} {count}, ...; 最近: {text}"。
// 它要求持有 w.mu(读锁或写锁)。
func (w *WorkingMemory) naiveSummaryLocked() string {
if len(w.window) == 0 {
return ""
@@ -136,8 +136,8 @@ func (w *WorkingMemory) naiveSummaryLocked() string {
return b.String()
}
// truncateRunes shortens s to at most n runes. Unicode-safe: it never slices a
// multi-byte rune in half.
// truncateRunes 把 s 截到最多 n 个 rune。Unicode 安全:它绝不把
// 多字节 rune 切一半。
func truncateRunes(s string, n int) string {
if utf8.RuneCountInString(s) <= n {
return s