AnyBot/docs/restructure/notes/b04c-memory-memstore.md

46 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# b04c:memory.rs(1,599 行)+ memstore.rs(957 行)— 記憶與技能
## memory.rs 常數(實讀)
MAX_MEMORIES=200、ABOUT_YOU=0(現行不單列「關於你」)、ABOUT_YOU_IN_PROMPT=40、MAX_MEMORY_CHARS=500、LEARN_MIN_STEPS=5、MAX_SKILL_CHARS=4000、ISSUE_CHARS=200、MAX_ISSUES_PER_TURN=5、BLANK_NAME_CHARS=12、SHOW_SCORE=0.15、PICK_SCORE=0.5、AUTO_SCORE=0.55、AUTO_MAX=3、SUGGEST_MAX=8、SAME_SCORE=0.85、SAME_WORDS=0.6。
`TEMPLATES: [Template; 5]`(line 94)。`LEARN_SYSTEM`(350)=學習小經理的系統提示全文(記經驗、整理技能)。
## 結構
`Skill{22}`(含 slug/空白欄位/規則)、`ReplayStep{79}`、`Template{86}`。
## 技能檔(副作用路徑)
`skill_dir()`(107)=<data>/skills/;`skill_to_file`/`parse_skill_file`/`install_skill_text`/`load_skill_files`/`remove_skill_files`/`merge_skill_files`(127–181)=單檔 .md 格式新舊相容。
## 學習流(順序實讀)
1. `taught_turn(prompt, at, starts)`(215)=「教你學」入口,吃 oplog::Record 開頭。
2. `skillify(said, recs)`(243)=把一個回合變成 Skill。
3. `worth_learning(turn)`(345)=≥LEARN_MIN_STEPS 步才學。
4. `learn_brief(turn, skills)`(403)→ 小經理 → `learned_issues`(428,每回合最多 5 條、每條 200 字)/`learned_skill`(439)/`learned_replay`(461,抓「第幾步」數字)/`learned_blanks`(470,空白欄名 ≤12 字)。
5. `blanks_of`(510)/`fill_blanks`(528,填不齊回缺欄清單)/`replay_steps`(562)=重放步驟圈選。
6. `learned_note(name, fresh)`(589)=學到後對使用者的說法。
## 提示與檢索
- `suggest/suggest_json`(616/621,走 memstore::Hit,上限 SUGGEST_MAX=8);`recall`(639);`carry`(648,本回合要帶入的記憶,扣掉已帶的)。
- `context_of(m, timezone)`(683)/`now_line(now, timezone)`(712)=時間寫入脈絡。
- `prompt_section(pinned, about_you, skills)`(725)=系統提示的記憶段。
- `named_skill`/`named_skill_line`/`slash_matches`(772–796)=「/技能名」直呼(draft 逐條比對)。
- `remember_tool`(831:replaces 換掉舊條、separate 另存;SAME_SCORE/SAME_WORDS 判「同一條」)/`forget_tool`(893)/`search_tool`(909)。
- `api_read`(950)/`api_write(state, b)`(982)=設定頁的记忆 API。
- 副作用:`remember/forget/upsert_skill`(289–320)動 list(State 內的 Vec,落盤由 store 負責)。
## memstore.rs(SQLite 側)
常數:TRASH_DAYS=30、EVENT_DAYS=30、WEEK_DAYS=180、STALE_DAYS=180、DAY=86_400_000、FOLD_CHARS=400。
`Mem{38}`/`Hit{60}`/`Page{389}`;`COLS`(65)固定欄序 id,bot,text,kind,created,pinned,uses,last_used,deleted,superseded_by,span。
- **三種 row kind**:fact/event/rollup(rollup=摺疊產物)。
- `meta_get/meta_set`(139/143)=庫內一鍵值表(記 last_tidy 等)。
- 文字檢索:`STOP` 停用字表(164)、`words_of`(182,jieba 切詞+bigram)、`terms_of`(199,IDF 加權)。
- `search/search_as(query, kinds, limit, literal)`(271/277)=兩路:FTS5 字面 + cosine 語意(拉伸:0.45 起中段、0.7 頂格 — 實讀 score 映射)。
- CRUD:`get/find/pinned`(242–252)、`list(filter,q,offset,limit)`(396)、`stats`(429)、`add/ensure`(453/474)、`set_pinned/edit(回 (新,舊))/trash(by)/restore/used(ids)/purge`(486–539)。
- 向量:`pending(limit)`(549,待 embed 列)→ `set_vecs`(562);`likeness(new, old)`(581,回 (語意, 字面))。
- `suggestions(bot, now)`(601)=給 Bot 的候補記憶。
- `Tidy/tidy(bot, timezone, now)`(716/725)=週摺 30 天、月摺 180 天;trash 到期清;stale 標記。副作用:寫 rollup 列、刪被摺事件。
- `seed_demo(bot, now)`(813)=示範資料。
- 庫位置:SQLite 檔在資料目錄、與 state 檔比鄰(b01d store 筆記互印)。
## 迁移
memory.rs → **core::memory**(技能/學習純邏輯);memstore.rs → **core::memstore**(rusqlite bundled 紅線不動);embed 呼叫介面留 core(實現在 b04d)。