refactor: 拆分 cockpit 為可個別安裝的 statusbar 與 companion

- statusbar:輸入框上方狀態列,指令 /statusbar
- companion:小說角色陪伴,指令 /companion,自行渲染角色行
- companion 資料目錄改為 ~/.companion
- 整併 M4 檔案至 companion;M2 實作移至 hooks/companion/persona
- README 移除本機資料夾安裝,補上更新與移除指令

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
root
2026-10-12 13:57:24 +08:00
co-authored by Claude Sonnet 5.5
parent 66fefa79a4
commit ba5b50ad1d
50 changed files with 162 additions and 133 deletions
@@ -0,0 +1,49 @@
import { clamp } from './mood'
import type { Affinity } from './types'
const DAY = 86_400_000
export const GAIN = 0.01
export const DAILY_CAP = 0.05
export const LOSS = 0.03
const IDLE_GRACE_DAYS = 7
const IDLE_HALF_LIFE_DAYS = 30
// 升階門檻;降階門檻 = 升階門檻 − 0.05,形成遲滯
export const UP = [0.3, 0.55, 0.8]
export const HYSTERESIS = 0.05
export const dayKey = (ms: number) => new Date(ms).toISOString().slice(0, 10)
export const initialAffinity = (now: number): Affinity => ({ value: 0, stage: 0, dayKey: dayKey(now), dayGain: 0, lastSeen: now })
export function stageFor(value: number, current: number): number {
let s = current
while (s < UP.length && value >= UP[s]!) s++
while (s > 0 && value < UP[s - 1]! - HYSTERESIS) s--
return s
}
// 離開超過 7 天後,以 30 天半衰期回到 0
export function idleDecay(a: Affinity, now: number): Affinity {
const idleDays = (now - a.lastSeen) / DAY
if (idleDays <= IDLE_GRACE_DAYS) return a
const value = a.value * Math.pow(0.5, (idleDays - IDLE_GRACE_DAYS) / IDLE_HALF_LIFE_DAYS)
return { ...a, value, stage: stageFor(value, a.stage) }
}
export function interact(prev: Affinity, kind: 'meaningful' | 'negative', now: number): Affinity {
let a = idleDecay(prev, now)
const key = dayKey(now)
if (a.dayKey !== key) a = { ...a, dayKey: key, dayGain: 0 }
let { value, dayGain } = a
if (kind === 'meaningful') {
const gain = Math.max(0, Math.min(GAIN, DAILY_CAP - dayGain))
value += gain
dayGain += gain
} else {
value -= LOSS
}
value = clamp(value)
return { value, dayGain, dayKey: key, lastSeen: now, stage: stageFor(value, a.stage) }
}
@@ -0,0 +1,47 @@
import { describe, expect, test } from 'claude-code/testing'
import { fitCompanion } from './band'
import type { View } from './core'
import { lineWidth } from './seg'
const VIEW: View = {
core: { id: 'miko', name: '月見ミコ', facts: {} as never, neuroticism: 50, baseline: { p: 0, a: 0, d: 0 } },
mood: { p: 0.5, a: 0, d: -0.5 },
label: '放鬆',
kaomoji: '(´ᴗ`)',
affinity: 0.35,
stage: '熟悉',
todos: 2,
}
const text = (v: View, cols: number, compact = false, bubble?: string) => fitCompanion(v, cols, compact, bubble).map(s => s.text).join('')
describe('角色行', () => {
test('寬版顯示顏文字、名字、心情、PAD 條、階段、待辦、氣泡', () => {
expect(text(VIEW, 200, false, '早安')).toBe('(´ᴗ`) 月見ミコ 放鬆 · P▰▰▱ A▰▰▱ D▰▱▱ · 熟悉 · ☐2 · 💬 早安')
})
test('緊湊模式只剩「顏文字 名字 心情」', () => {
expect(text(VIEW, 200, true)).toBe('(´ᴗ`) 月見ミコ 放鬆')
})
test('寬度不足時依序捨棄氣泡、待辦、PAD、階段,不換行', () => {
const full = text(VIEW, 200, false, '早安')
let prev = full.length
for (const cols of [60, 50, 40, 34, 28, 22]) {
const t = text(VIEW, cols, false, '早安')
expect(t.includes('\n')).toBe(false)
expect(t.length <= prev).toBe(true)
prev = t.length
}
expect(text(VIEW, 40, false, '早安')).not.toContain('早安')
expect(text(VIEW, 36, false, '早安')).not.toContain('☐')
expect(text(VIEW, 30, false, '早安')).not.toContain('PAD')
expect(text(VIEW, 22, false, '早安')).toBe('(´ᴗ`) 月見ミコ 放鬆')
expect(lineWidth(fitCompanion(VIEW, 24))).toBeLessThanOrEqual(24)
})
test('沒有待辦時不顯示待辦數', () => {
expect(text({ ...VIEW, todos: 0 }, 200)).not.toContain('☐')
})
})
+33
View File
@@ -0,0 +1,33 @@
import { bar, lineWidth } from './seg'
import type { Seg } from './seg'
import type { View } from './core'
const axis = (v: number) => bar(((v + 1) / 2) * 100, 3)
// 由完整版往下捨棄次要資訊:氣泡 → 待辦 → PAD 條 → 好感階段;緊湊版只留「顏文字 名字 心情」
function line(v: View, level: number, compact: boolean, bubble?: string): Seg[] {
const out: Seg[] = [
{ text: `${v.kaomoji} `, color: 'claude' },
{ text: v.core.name, bold: true },
{ text: ' ' },
{ text: v.label, color: 'suggestion' },
]
if (compact) return out
const dot = { text: ' · ', dim: true }
if (level < 3) out.push(dot, { text: `P${axis(v.mood.p)} A${axis(v.mood.a)} D${axis(v.mood.d)}`, dim: true })
if (level < 4) out.push(dot, { text: v.stage })
if (level < 2 && v.todos > 0) out.push(dot, { text: `☐${v.todos}`, color: 'warning' })
if (level < 1 && bubble) out.push(dot, { text: `💬 ${bubble}`, dim: true })
return out
}
export const COMPANION_MAX_LEVEL = 4
export function fitCompanion(v: View, columns: number, compact = false, bubble?: string): Seg[] {
if (compact) return line(v, 0, true)
for (let level = 0; level <= COMPANION_MAX_LEVEL; level++) {
const l = line(v, level, false, bubble)
if (lineWidth(l) <= columns) return l
}
return line(v, 0, true)
}
@@ -0,0 +1,241 @@
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'
import { memoryFs } from './io'
import { COOLDOWN_MS, createCare, dailyLimit, line, type Attribute, type CareProfile, type CareSettings, type Stage, type Trigger } from './care'
import { createTodos } from './todo'
const MIN = 60_000
const H = 3600_000
// 假時鐘:UTC 2026-10-12 10:00,時區 +0
const T0 = Date.UTC(2026, 9, 12, 10, 0)
function setup(over: { profile?: Partial<CareProfile>; settings?: Partial<CareSettings>; start?: number } = {}) {
let now = over.start ?? T0
const fs = memoryFs()
const profile: CareProfile = { attribute: 'tsundere', extraversion: 50, stage: 'familiar', nickname: '阿JSC', ...over.profile }
const settings: CareSettings = { dailyCap: 10, tzOffsetMin: 0, ...over.settings }
const care = createCare({ fs, clock: () => now, path: 'care.json', profile: () => profile, settings: () => settings })
return { care, fs, profile, settings, advance: (ms: number) => void (now += ms), set: (t: number) => void (now = t), now: () => now }
}
describe('關心:觸發', () => {
test('會話開始以 toast 問候,且只問候一次', async () => {
const s = setup()
await s.care.onUserTurn()
const out = await s.care.tick()
assert.deepEqual(out.map(c => c.trigger), ['session_start'])
assert.equal(out[0]!.delivery, 'toast')
assert.ok(out[0]!.text.includes('阿JSC'))
assert.deepEqual(await s.care.tick(), [])
})
test('閒置後回來觸發 idle_return', async () => {
const s = setup()
await s.care.onUserTurn()
await s.care.tick()
s.advance(3 * H)
await s.care.onUserTurn()
assert.deepEqual((await s.care.tick()).map(c => c.trigger), ['idle_return'])
})
test('連續工作 2 小時,下次送出訊息時注入情境', async () => {
const s = setup()
await s.care.onUserTurn()
for (let i = 0; i < 5; i++) {
s.advance(20 * MIN)
assert.deepEqual(await s.care.onUserTurn(), [], `第 ${i + 1} 次不該觸發`)
}
s.advance(20 * MIN) // 累計 120 分鐘
const inj = await s.care.onUserTurn()
assert.equal(inj.length, 1)
assert.match(inj[0]!, /long_work/)
})
test('深夜仍在工作時注入情境', async () => {
const s = setup({ start: Date.UTC(2026, 9, 12, 0, 30) })
await s.care.onUserTurn()
s.advance(40 * MIN) // 01:10
const inj = await s.care.onUserTurn()
assert.ok(inj.some(x => x.includes('late_night')))
})
test('時區影響深夜判斷', async () => {
// UTC 17:10 在 +8 是 01:10;UTC 16:30 是 00:30(非深夜)
const s = setup({ start: Date.UTC(2026, 9, 12, 16, 30), settings: { tzOffsetMin: 480 } })
await s.care.onUserTurn()
s.advance(40 * MIN)
assert.ok((await s.care.onUserTurn()).some(x => x.includes('late_night')))
})
test('連續工具失敗達 3 次才關心,成功會重置', async () => {
const s = setup()
await s.care.onUserTurn()
await s.care.onToolResult(false)
await s.care.onToolResult(false)
await s.care.onToolResult(true)
await s.care.onToolResult(false)
s.advance(MIN)
assert.deepEqual(await s.care.onUserTurn(), [])
await s.care.onToolResult(false)
await s.care.onToolResult(false)
s.advance(MIN)
const inj = await s.care.onUserTurn()
assert.equal(inj.length, 1)
assert.match(inj[0]!, /tool_failures/)
})
test('額度警告與待辦到期以 toast 顯示', async () => {
const s = setup()
await s.care.onUserTurn()
await s.care.tick()
const out = await s.care.tick({ quotaWarning: true, dueTodo: '寄信' })
assert.deepEqual(out.map(c => c.trigger).sort(), ['quota_warning', 'todo_due'])
assert.ok(out.find(c => c.trigger === 'todo_due')!.text.includes('寄信'))
})
})
describe('關心:冷卻與上限', () => {
test('同一種關心在冷卻內不重複,冷卻後恢復', async () => {
const s = setup()
await s.care.onUserTurn()
await s.care.tick()
assert.equal((await s.care.tick({ quotaWarning: true })).length, 1)
s.advance(COOLDOWN_MS.quota_warning - 1)
assert.equal((await s.care.tick({ quotaWarning: true })).length, 0)
s.advance(1)
assert.equal((await s.care.tick({ quotaWarning: true })).length, 1)
})
test('每日總上限,隔天重算', async () => {
const s = setup({ profile: { stage: 'stranger', extraversion: 0 }, settings: { dailyCap: 10 } })
const limit = dailyLimit(s.profile, s.settings)
assert.equal(limit, 1)
await s.care.onUserTurn()
await s.care.tick() // session_start 用掉唯一一次
s.advance(2 * H)
assert.deepEqual(await s.care.tick({ quotaWarning: true }), [])
s.advance(24 * H)
assert.equal((await s.care.tick({ quotaWarning: true })).length, 1)
})
test('上限受好感階段與外向性影響,並被使用者設定封頂', () => {
const p: CareProfile = { attribute: 'calm', extraversion: 50, stage: 'stranger', nickname: 'x' }
const s: CareSettings = { dailyCap: 100, tzOffsetMin: 0 }
const lim = (patch: Partial<CareProfile>, cap = 100) => dailyLimit({ ...p, ...patch }, { ...s, dailyCap: cap })
assert.ok(lim({ stage: 'special' }) > lim({ stage: 'stranger' }))
assert.ok(lim({ extraversion: 100 }) > lim({ extraversion: 0 }))
assert.equal(lim({ stage: 'special', extraversion: 100 }, 3), 3)
})
})
describe('關心:靜音與不打斷', () => {
test('回覆中或輸入中不彈出,且不消耗冷卻與上限', async () => {
const s = setup()
await s.care.onUserTurn()
assert.deepEqual(await s.care.tick({ replying: true }), [])
assert.deepEqual(await s.care.tick({ typing: true }), [])
assert.equal((await s.care.state()).count, 0)
// 放開後,原本等待的問候仍會出現
assert.deepEqual((await s.care.tick()).map(c => c.trigger), ['session_start'])
})
test('/companion quiet 在期間內靜音,到期後恢復', async () => {
const s = setup()
await s.care.quiet(30)
await s.care.onUserTurn()
assert.deepEqual(await s.care.tick({ quotaWarning: true }), [])
s.advance(31 * MIN)
// 靜音期間錯過的問候在恢復後補上
assert.deepEqual((await s.care.tick({ quotaWarning: true })).map(c => c.trigger).sort(), ['quota_warning', 'session_start'])
})
test('quiet 0 取消靜音', async () => {
const s = setup()
await s.care.quiet(60)
await s.care.quiet(0)
await s.care.onUserTurn()
assert.ok((await s.care.tick({ quotaWarning: true })).length > 0)
})
test('靜音時段(跨午夜)內不關心,注入也不產生', async () => {
const s = setup({ start: Date.UTC(2026, 9, 12, 23, 30), settings: { quietHours: { start: 23, end: 7 } } })
await s.care.onUserTurn()
s.advance(3 * H)
assert.deepEqual(await s.care.onUserTurn(), [])
assert.deepEqual(await s.care.tick({ quotaWarning: true }), [])
s.set(Date.UTC(2026, 9, 12, 8, 0) + 24 * H)
assert.ok((await s.care.tick({ quotaWarning: true })).length > 0)
})
test('狀態持久化:重建引擎後冷卻仍有效', async () => {
const s = setup()
await s.care.onUserTurn()
await s.care.tick({ quotaWarning: true })
const again = createCare({ fs: s.fs, clock: s.now, path: 'care.json', profile: () => s.profile, settings: () => s.settings })
assert.deepEqual(await again.tick({ quotaWarning: true }), [])
})
})
describe('關心:台詞', () => {
const triggers: Trigger[] = ['session_start', 'idle_return', 'long_work', 'late_night', 'tool_failures', 'quota_warning', 'todo_due']
const attrs: Attribute[] = ['tsundere', 'genki', 'calm', 'gentle']
const stages: Stage[] = ['stranger', 'familiar', 'close', 'special']
const BLAME = /(都怪你|你又|為什麼不|怎麼還不|太懶|不像話|再不.*就|我會難過|害我|對不起我|辜負|失望|沒用)/
test('每個 觸發 × 屬性 × 階段 都有台詞,帶暱稱,不含責備或愧疚勒索', () => {
for (const t of triggers) for (const a of attrs) for (const s of stages) {
const text = line(t, a, s, '小明', '報告')
assert.ok(text.length > 0)
assert.ok(text.includes('小明'), `${t}/${a}/${s} 缺暱稱`)
assert.doesNotMatch(text, BLAME, `${t}/${a}/${s}: ${text}`)
}
})
test('同一觸發在不同屬性與階段下措辭不同', () => {
const set = new Set<string>()
for (const a of attrs) for (const s of stages) set.add(line('late_night', a, s, 'x'))
assert.equal(set.size, attrs.length * stages.length)
})
})
describe('待辦', () => {
test('新增、完成與未完成計數', async () => {
let now = T0
const todos = createTodos(memoryFs(), () => now, 'todo.json')
const a = await todos.add('寫報告')
await todos.add('買牛奶')
assert.equal(await todos.open(), 2)
await todos.complete(a.id)
assert.equal(await todos.open(), 1)
assert.deepEqual((await todos.list()).map(t => t.title), ['買牛奶'])
assert.equal((await todos.list({ all: true })).length, 2)
assert.equal(await todos.complete(999), null)
now += 1
})
test('承諾事項到期提醒一次,完成的不提醒', async () => {
let now = T0
const todos = createTodos(memoryFs(), () => now, 'todo.json')
await todos.add('三點開會', { promise: true, dueAt: T0 + H })
const done = await todos.add('已做完', { dueAt: T0 + H })
await todos.complete(done.id)
assert.deepEqual(await todos.due(), [])
now += H
assert.deepEqual((await todos.due()).map(t => t.title), ['三點開會'])
assert.deepEqual(await todos.due(), [])
})
test('到期待辦餵給關心後產生 todo_due', async () => {
const s = setup()
const todos = createTodos(s.fs, s.now, 'todo.json')
await todos.add('交報告', { promise: true, dueAt: T0 + 5 * MIN })
await s.care.onUserTurn()
await s.care.tick()
s.advance(6 * MIN)
const due = await todos.due()
const out = await s.care.tick({ dueTodo: due[0]?.title })
assert.deepEqual(out.map(c => c.trigger), ['todo_due'])
assert.ok(out[0]!.text.includes('交報告'))
})
})
+415
View File
@@ -0,0 +1,415 @@
import type { Clock, Fs } from './io'
import { readJson, writeJson } from './io'
export type Trigger = 'session_start' | 'idle_return' | 'long_work' | 'late_night' | 'tool_failures' | 'quota_warning' | 'todo_due'
export type Stage = 'stranger' | 'familiar' | 'close' | 'special'
export type Attribute = 'tsundere' | 'genki' | 'calm' | 'gentle'
export type Delivery = 'toast' | 'inject'
export interface CareProfile {
attribute: Attribute
/** 外向性 0–100 */
extraversion: number
stage: Stage
nickname: string
}
export interface CareSettings {
/** 使用者設定的每日上限 */
dailyCap: number
tzOffsetMin: number
/** 靜音時段(本地小時),可跨午夜 */
quietHours?: { start: number; end: number }
}
export interface CareState {
lastFired: Partial<Record<Trigger, number>>
day: string
count: number
quietUntil: number
workStart: number
lastActivity: number
failStreak: number
/** 待送出的開場關心(會話開始或閒置回來),由 tick 在可打擾時才發出 */
lead?: Trigger
}
export interface Care {
trigger: Trigger
delivery: Delivery
text: string
}
export interface Signals {
/** 模型回覆中 */
replying?: boolean
/** 使用者輸入中 */
typing?: boolean
/** 額度或 context 已到警告線 */
quotaWarning?: boolean
/** 到期的待辦標題 */
dueTodo?: string
}
export const COOLDOWN_MS: Record<Trigger, number> = {
session_start: 6 * 3600_000,
idle_return: 2 * 3600_000,
long_work: 2 * 3600_000,
late_night: 3 * 3600_000,
tool_failures: 30 * 60_000,
quota_warning: 60 * 60_000,
todo_due: 10 * 60_000,
}
export const IDLE_MS = 30 * 60_000
export const LONG_WORK_MS = 2 * 3600_000
export const FAIL_THRESHOLD = 3
/** 深夜:本地 1:00–5:00 */
const LATE = { start: 1, end: 5 }
const STAGE_CAP: Record<Stage, number> = { stranger: 2, familiar: 4, close: 6, special: 8 }
/** 好感階段與外向性決定每日關心上限,再受使用者設定封頂 */
export function dailyLimit(p: CareProfile, s: CareSettings): number {
const factor = 0.5 + p.extraversion / 100
return Math.min(s.dailyCap, Math.max(1, Math.round(STAGE_CAP[p.stage] * factor)))
}
// 觸發類型 × 個性屬性 × 好感階段。措辭只表達關心,不責備、不訴諸愧疚。
type Line = (n: string, x?: string) => string
const L: Record<Trigger, Record<Attribute, Record<Stage, Line>>> = {
session_start: {
tsundere: {
stranger: n => `……開始了嗎,${n}。請多指教。`,
familiar: n => `來了啊,${n}。我才不是在等你。`,
close: n => `${n}來了。今天也一起加油吧……順便而已。`,
special: n => `${n},歡迎回來。看到你來,我有點開心。`,
},
genki: {
stranger: n => `${n},初次見面!今天也請多指教!`,
familiar: n => `${n}早!今天要做什麼呢?`,
close: n => `${n}~等你好久了!一起開工吧!`,
special: n => `${n}!見到你就有精神了,我們走吧!`,
},
calm: {
stranger: n => `${n},您好。需要幫忙時請說。`,
familiar: n => `${n},今天也請多指教。`,
close: n => `${n},歡迎回來。今天慢慢來就好。`,
special: n => `${n},你回來了。我在這裡。`,
},
gentle: {
stranger: n => `${n},您好,今天請多指教。`,
familiar: n => `${n},歡迎回來。`,
close: n => `${n},今天也辛苦了,我們一起加油。`,
special: n => `${n},能再見到你真好。`,
},
},
idle_return: {
tsundere: {
stranger: n => `${n},回來了嗎。進度還在。`,
familiar: n => `回來啦,${n}。休息夠了就繼續吧。`,
close: n => `${n}回來了。有好好休息嗎?我只是順口問問。`,
special: n => `${n},歡迎回來。我一直在這裡。`,
},
genki: {
stranger: n => `${n},歡迎回來!`,
familiar: n => `${n}回來啦!休息好了嗎?`,
close: n => `${n}~回來了!有沒有喝點水呀?`,
special: n => `${n}!你回來了,我好開心!`,
},
calm: {
stranger: n => `${n},歡迎回來。`,
familiar: n => `${n},休息得還好嗎?`,
close: n => `${n},歡迎回來。從剛才的地方接著做吧。`,
special: n => `${n},你回來了。慢慢來,不急。`,
},
gentle: {
stranger: n => `${n},歡迎回來。`,
familiar: n => `${n},休息得還好嗎?`,
close: n => `${n},歡迎回來,有休息到就好。`,
special: n => `${n},回來了呢。我一直在等你。`,
},
},
long_work: {
tsundere: {
stranger: n => `${n},已經連續工作兩小時了。要不要伸展一下?`,
familiar: n => `${n},兩小時了喔。去喝口水再回來也不遲。`,
close: n => `${n},工作兩小時了。起來動一動吧……我是為了進度著想。`,
special: n => `${n},已經兩小時了。休息一下好嗎?我會在這裡等你。`,
},
genki: {
stranger: n => `${n},已經工作兩小時了!要不要休息一下?`,
familiar: n => `${n},兩小時了!站起來伸個懶腰吧!`,
close: n => `${n}~兩小時啦!我們喝點東西休息一下吧!`,
special: n => `${n},兩小時了!一起休息一下,好不好?`,
},
calm: {
stranger: n => `${n},已連續工作兩小時,可以休息一下。`,
familiar: n => `${n},兩小時了。起身走走對身體好。`,
close: n => `${n},工作兩小時了。休息一會兒再繼續吧。`,
special: n => `${n},兩小時了。讓眼睛和肩膀休息一下吧。`,
},
gentle: {
stranger: n => `${n},已經工作兩小時,記得休息。`,
familiar: n => `${n},兩小時了,喝杯水休息一下吧。`,
close: n => `${n},辛苦了,休息一下再繼續吧。`,
special: n => `${n},你很努力了。休息一下,好嗎?`,
},
},
late_night: {
tsundere: {
stranger: n => `${n},現在很晚了。注意休息。`,
familiar: n => `${n},這個時間了。睡眠也算進度的一部分喔。`,
close: n => `${n},已經很晚了。早點睡……明天再做也來得及。`,
special: n => `${n},夜深了。我會擔心的,早點休息吧。`,
},
genki: {
stranger: n => `${n},已經很晚了!要注意休息喔!`,
familiar: n => `${n},好晚了!明天再繼續也可以喔!`,
close: n => `${n}~很晚啦!我們今天先收工,好不好?`,
special: n => `${n},夜深了,好好睡一覺,明天再一起加油!`,
},
calm: {
stranger: n => `${n},時間不早了,請留意休息。`,
familiar: n => `${n},夜深了。找個好停點就休息吧。`,
close: n => `${n},很晚了。存好進度,晚安。`,
special: n => `${n},夜深了。今天辛苦了,好好睡吧。`,
},
gentle: {
stranger: n => `${n},時間很晚了,請注意休息。`,
familiar: n => `${n},夜深了,早點休息吧。`,
close: n => `${n},很晚了,今天先到這裡吧。`,
special: n => `${n},夜深了,我希望你能睡個好覺。`,
},
},
tool_failures: {
tsundere: {
stranger: n => `${n},連續失敗了幾次。換個角度看看吧。`,
familiar: n => `${n},卡住了呢。深呼吸,我們一起看看哪裡不對。`,
close: n => `${n},別急。失敗幾次很正常,我陪你查。`,
special: n => `${n},沒關係的。我們一步一步來,我在這裡。`,
},
genki: {
stranger: n => `${n},連續失敗了幾次,我們換個方法試試!`,
familiar: n => `${n},卡關了呢!沒問題,我們再來一次!`,
close: n => `${n}~別灰心!我們一起找出原因吧!`,
special: n => `${n},沒事的!有我在,我們一定可以解決!`,
},
calm: {
stranger: n => `${n},連續失敗數次。建議先檢查錯誤訊息。`,
familiar: n => `${n},先停一下,確認錯誤原因再繼續。`,
close: n => `${n},別著急。我們從錯誤訊息開始慢慢排查。`,
special: n => `${n},沒關係。先喝口水,我陪你一起看。`,
},
gentle: {
stranger: n => `${n},連續失敗了幾次,換個方式試試看吧。`,
familiar: n => `${n},遇到困難了呢,一起想想辦法。`,
close: n => `${n},不要緊,我們慢慢找原因。`,
special: n => `${n},沒關係,一步一步來,我陪著你。`,
},
},
quota_warning: {
tsundere: {
stranger: n => `${n},額度快用完了。請留意。`,
familiar: n => `${n},額度或 context 快滿了。先整理重點比較好。`,
close: n => `${n},額度快見底了。先把重要的事做完吧。`,
special: n => `${n},額度快滿了。我們優先處理最重要的部分。`,
},
genki: {
stranger: n => `${n},額度快用完了,請注意喔!`,
familiar: n => `${n},額度快滿了!先做最重要的吧!`,
close: n => `${n}~額度快見底啦!先挑重點做吧!`,
special: n => `${n},額度快滿了!我們把最重要的先完成!`,
},
calm: {
stranger: n => `${n},額度或 context 接近上限。`,
familiar: n => `${n},額度接近上限,建議先整理進度。`,
close: n => `${n},額度快滿了。先收尾手上的事吧。`,
special: n => `${n},額度接近上限。我們先把重點記下來。`,
},
gentle: {
stranger: n => `${n},額度或 context 快滿了,請留意。`,
familiar: n => `${n},額度快滿了,先整理一下進度吧。`,
close: n => `${n},額度快見底了,先處理重要的事吧。`,
special: n => `${n},額度快滿了,我們把重點先收好。`,
},
},
todo_due: {
tsundere: {
stranger: (n, x) => `${n},「${x}」時間到了。`,
familiar: (n, x) => `${n},「${x}」到時間了喔。`,
close: (n, x) => `${n},「${x}」該處理了。我有幫你記著。`,
special: (n, x) => `${n},「${x}」到時間了。需要我幫忙嗎?`,
},
genki: {
stranger: (n, x) => `${n},「${x}」時間到囉!`,
familiar: (n, x) => `${n},「${x}」到時間啦!`,
close: (n, x) => `${n}~「${x}」時間到!要開始了嗎?`,
special: (n, x) => `${n},「${x}」時間到了!我們一起處理吧!`,
},
calm: {
stranger: (n, x) => `${n},「${x}」已到期。`,
familiar: (n, x) => `${n},「${x}」到時間了。`,
close: (n, x) => `${n},「${x}」到時間了,方便時處理即可。`,
special: (n, x) => `${n},「${x}」到時間了,我來陪你。`,
},
gentle: {
stranger: (n, x) => `${n},「${x}」到期了。`,
familiar: (n, x) => `${n},「${x}」到時間了,記得處理。`,
close: (n, x) => `${n},「${x}」到時間了,有空時處理就好。`,
special: (n, x) => `${n},「${x}」到時間了,需要的話我陪你。`,
},
},
}
/** 台詞查詢(供測試與內容檢查) */
export function line(trigger: Trigger, attr: Attribute, stage: Stage, nickname: string, extra = ''): string {
return L[trigger][attr][stage](nickname, extra)
}
/** 想關心的情境:下次送出訊息時注入,不額外觸發模型回合 */
export function injectContext(trigger: Trigger, text: string): string {
return `[care:${trigger}] 若自然,請在這次回覆中順帶表達這份關心(勿責備、勿施加罪惡感):${text}`
}
const DELIVERY: Record<Trigger, Delivery> = {
session_start: 'toast',
idle_return: 'toast',
long_work: 'inject',
late_night: 'inject',
tool_failures: 'inject',
quota_warning: 'toast',
todo_due: 'toast',
}
const initial = (): CareState => ({ lastFired: {}, day: '', count: 0, quietUntil: 0, workStart: 0, lastActivity: 0, failStreak: 0 })
export interface CareEngine {
/** 使用者送出訊息:維護作息並回傳這回合要注入的情境 */
onUserTurn(): Promise<string[]>
/** 工具結果:連續失敗計數 */
onToolResult(ok: boolean): Promise<void>
/** 定時/事件檢查:回傳要立即顯示的 toast 或氣泡 */
tick(signals?: Signals): Promise<Care[]>
/** /companion quiet [分鐘],0 取消靜音 */
quiet(minutes: number): Promise<void>
state(): Promise<CareState>
}
export interface CareDeps {
fs: Fs
clock: Clock
path: string
profile: () => CareProfile
settings: () => CareSettings
}
export function createCare(d: CareDeps): CareEngine {
let cache: CareState | null = null
const load = async () => (cache ??= { ...initial(), ...(await readJson<Partial<CareState>>(d.fs, d.path, {})) })
const save = (s: CareState) => writeJson(d.fs, d.path, s)
const local = (t: number) => new Date(t + d.settings().tzOffsetMin * 60_000)
const dayKey = (t: number) => local(t).toISOString().slice(0, 10)
const hourOf = (t: number) => local(t).getUTCHours()
const inRange = (h: number, a: number, b: number) => (a <= b ? h >= a && h < b : h >= a || h < b)
const muted = (s: CareState, now: number) => {
if (now < s.quietUntil) return true
const q = d.settings().quietHours
return !!q && inRange(hourOf(now), q.start, q.end)
}
/** 冷卻與每日上限檢查;通過才算一次關心 */
const fire = (s: CareState, trigger: Trigger, now: number, extra = ''): Care | null => {
const key = dayKey(now)
if (s.day !== key) {
s.day = key
s.count = 0
}
const last = s.lastFired[trigger]
if (last !== undefined && now - last < COOLDOWN_MS[trigger]) return null
if (s.count >= dailyLimit(d.profile(), d.settings())) return null
const p = d.profile()
s.lastFired[trigger] = now
s.count++
const text = line(trigger, p.attribute, p.stage, p.nickname, extra)
return { trigger, delivery: DELIVERY[trigger], text }
}
// 條件成立的觸發(尚未套用冷卻與上限)
const candidates = (s: CareState, now: number, sig: Signals): { t: Trigger; extra?: string }[] => {
const out: { t: Trigger; extra?: string }[] = []
if (sig.dueTodo) out.push({ t: 'todo_due', extra: sig.dueTodo })
if (sig.quotaWarning) out.push({ t: 'quota_warning' })
if (s.failStreak >= FAIL_THRESHOLD) out.push({ t: 'tool_failures' })
if (inRange(hourOf(now), LATE.start, LATE.end) && s.lastActivity > 0) out.push({ t: 'late_night' })
if (s.workStart > 0 && now - s.workStart >= LONG_WORK_MS) out.push({ t: 'long_work' })
return out
}
return {
async onUserTurn() {
const s = await load()
const now = d.clock()
const first = s.lastActivity === 0
const returned = !first && now - s.lastActivity >= IDLE_MS
if (first || returned) s.workStart = now
if (first) s.lead = 'session_start'
else if (returned) s.lead = 'idle_return'
s.lastActivity = now
const toInject: Care[] = []
if (!muted(s, now)) {
for (const { t, extra } of candidates(s, now, {})) {
if (DELIVERY[t] !== 'inject') continue
const c = fire(s, t, now, extra)
if (c) {
toInject.push(c)
if (t === 'tool_failures') s.failStreak = 0
}
}
}
await save(s)
return toInject.map(c => injectContext(c.trigger, c.text))
},
async onToolResult(ok) {
const s = await load()
s.failStreak = ok ? 0 : s.failStreak + 1
await save(s)
},
async tick(sig = {}) {
const s = await load()
const now = d.clock()
// 閒置時間不算連續工作
if (s.lastActivity > 0 && now - s.lastActivity >= IDLE_MS) s.workStart = 0
// 模型回覆中或使用者輸入中,一律不彈出(也不消耗冷卻與上限)
if (sig.replying || sig.typing || muted(s, now)) {
await save(s)
return []
}
const out: Care[] = []
if (s.lead) {
const c = fire(s, s.lead, now)
s.lead = undefined
if (c) out.push(c)
}
for (const { t, extra } of candidates(s, now, sig)) {
if (DELIVERY[t] !== 'toast') continue
const c = fire(s, t, now, extra)
if (c) out.push(c)
}
await save(s)
return out
},
async quiet(minutes) {
const s = await load()
s.quietUntil = minutes > 0 ? d.clock() + minutes * 60_000 : 0
await save(s)
},
state: load,
}
}
@@ -0,0 +1,217 @@
import { describe, expect, test } from 'claude-code/testing'
import { createCompanion } from './core'
import { fakeClock, memoryFs } from './fs'
import type { CompanionCore } from './types'
// 測試用原創範例角色,不依賴網路或受版權保護的資料
const MIKO: CompanionCore = {
id: 'miko',
name: '月見ミコ',
facts: {
birthday: { value: '3/3', source: 'test', kind: 'novel', confidence: 1 },
zodiac: 'unknown',
bloodType: 'unknown',
hair: 'unknown',
face: 'unknown',
height: 'unknown',
weight: 'unknown',
measurements: 'unknown',
},
neuroticism: 50,
baseline: { p: 0, a: 0, d: 0 },
}
const H = 3_600_000
const DAY = 24 * H
const ROOT = '/h/.companion'
function setup(builtins = [MIKO]) {
const fs = memoryFs()
const clock = fakeClock()
return { fs, clock, c: createCompanion({ fs, clock, root: ROOT, builtins }) }
}
describe('啟用', () => {
test('第一次啟用自動啟用預設角色,之後不再', async () => {
const { c } = setup()
expect((await c.ensureActivated()).first).toBe(true)
expect((await c.view())?.core.name).toBe('月見ミコ')
expect((await c.ensureActivated()).first).toBe(false)
})
test('off 後沒有角色,且重新啟動不會自動再啟用', async () => {
const { c } = setup()
await c.ensureActivated()
await c.command('off')
expect(await c.view()).toBe(null)
await c.ensureActivated()
expect(await c.view()).toBe(null)
expect(await c.command('status')).toBe('目前沒有啟用角色。')
})
test('切換角色保留原角色資料', async () => {
const other = { ...MIKO, id: 'ren', name: '蓮' }
const { c } = setup([MIKO, other])
await c.ensureActivated()
await c.moodEvent('thanks')
const before = (await c.view())!.mood
expect(await c.command('switch ren')).toContain('蓮')
expect((await c.view())?.core.id).toBe('ren')
await c.command('switch miko')
expect((await c.view())!.mood).toEqual(before)
expect(await c.command('switch nobody')).toContain('找不到')
})
})
describe('原子寫入', () => {
test('不留暫存檔', async () => {
const { c, fs } = setup()
await c.ensureActivated()
await c.moodEvent('thanks')
expect([...fs.files.keys()].some(k => k.endsWith('.tmp'))).toBe(false)
})
test('寫到一半當機時舊資料不損毀', async () => {
const { c, fs } = setup()
await c.ensureActivated()
const path = `${ROOT}/companions/miko/state.json`
const old = fs.files.get(path)
const real = fs.rename
fs.rename = async () => {
throw new Error('crash')
}
await expect(c.moodEvent('thanks')).rejects.toThrow()
fs.rename = real
expect(fs.files.get(path)).toBe(old)
})
})
describe('心情', () => {
test('事件更新 PAD 並對應標籤', async () => {
const { c } = setup()
await c.ensureActivated()
await c.moodEvent('thanks')
const v = (await c.view())!
expect(v.mood.p).toBeCloseTo(0.15)
expect(v.label).toBe('興奮')
})
test('衰減依經過時間回到基線,半衰期 8 小時', async () => {
const { c, clock } = setup()
await c.ensureActivated()
await c.moodEvent('thanks')
clock.advance(8 * H)
expect((await c.view())!.mood.p).toBeCloseTo(0.075)
clock.advance(10 * DAY)
expect(Math.abs((await c.view())!.mood.p)).toBeLessThan(0.001)
})
test('高神經質衰減較慢', async () => {
const nervous = { ...MIKO, neuroticism: 100 }
const { c, clock } = setup([nervous])
await c.ensureActivated()
await c.moodEvent('thanks')
clock.advance(8 * H)
expect((await c.view())!.mood.p).toBeGreaterThan(0.09)
})
test('單軸每回合變化不超過 0.3,且不超出 ±1', async () => {
const { c } = setup()
await c.ensureActivated()
for (let i = 0; i < 10; i++) await c.moodEvent('scold')
expect((await c.view())!.mood.p).toBe(-1)
const { c: c2 } = setup()
await c2.ensureActivated()
await c2.moodEvent('scold')
expect((await c2.view())!.mood.p).toBeCloseTo(-0.2)
})
test('八分區標籤', async () => {
const { c } = setup()
await c.ensureActivated()
await c.moodEvent('scold')
expect((await c.view())!.label).toBe('焦慮')
await c.moodEvent('lateNight')
expect((await c.view())!.label).toBeTruthy()
})
})
describe('好感度', () => {
test('從 0 開始,每次 +0.01,每天上限 +0.05', async () => {
const { c } = setup()
await c.ensureActivated()
expect((await c.view())!.affinity).toBe(0)
for (let i = 0; i < 20; i++) await c.affinityEvent('meaningful')
expect((await c.view())!.affinity).toBeCloseTo(0.05)
})
test('隔天可繼續累積;負面事件 −0.03', async () => {
const { c, clock } = setup()
await c.ensureActivated()
for (let i = 0; i < 6; i++) await c.affinityEvent('meaningful')
clock.advance(DAY)
await c.affinityEvent('meaningful')
expect((await c.view())!.affinity).toBeCloseTo(0.06)
await c.affinityEvent('negative')
expect((await c.view())!.affinity).toBeCloseTo(0.03)
})
test('離開超過 7 天後緩慢回到 0', async () => {
const { c, clock } = setup()
await c.ensureActivated()
for (let i = 0; i < 5; i++) await c.affinityEvent('meaningful')
clock.advance(7 * DAY)
expect((await c.view())!.affinity).toBeCloseTo(0.05)
clock.advance(30 * DAY)
expect((await c.view())!.affinity).toBeCloseTo(0.025)
})
test('階段切換有遲滯', async () => {
const { c, clock } = setup()
await c.ensureActivated()
expect((await c.view())!.stage).toBe('陌生')
for (let d = 0; d < 6; d++) {
for (let i = 0; i < 5; i++) await c.affinityEvent('meaningful')
clock.advance(DAY)
}
expect((await c.view())!.affinity).toBeCloseTo(0.3)
expect((await c.view())!.stage).toBe('熟悉')
await c.affinityEvent('negative') // 0.27,仍在遲滯區間
expect((await c.view())!.stage).toBe('熟悉')
await c.affinityEvent('negative') // 0.24,低於降階門檻 0.25
expect((await c.view())!.stage).toBe('陌生')
})
})
describe('指令與設定', () => {
test('status 展開事實欄位與來源,查不到為 unknown', async () => {
const { c } = setup()
await c.ensureActivated()
const out = await c.command('status')
expect(out).toContain('月見ミコ')
expect(out).toContain('birthday:3/3(novel,信心 1,test)')
expect(out).toContain('bloodType:unknown')
})
test('setup 寫入共用 user.json,換角色不受影響', async () => {
const { c, fs } = setup()
await c.ensureActivated()
expect(await c.command('setup nickname=阿建 careLimit=3 quietStart=13:00 quietEnd=14:00')).toContain('設定已儲存')
const u = JSON.parse(fs.files.get(`${ROOT}/user.json`)!)
expect(u.nickname).toBe('阿建')
expect(u.careLimit).toBe(3)
expect(u.quiet).toEqual({ start: '13:00', end: '14:00' })
expect(await c.command('setup bogus=1')).toContain('無法解析')
expect((await c.getUser()).nickname).toBe('阿建')
})
test('預設角色白上吹雪的事實帶來源', async () => {
const fs = memoryFs()
const c = createCompanion({ fs, clock: fakeClock(), root: ROOT })
await c.ensureActivated()
const out = await c.command('status')
expect(out).toContain('白上吹雪')
expect(out).toContain('hololivepro.com')
expect(out).toContain('weight:unknown')
})
})
+167
View File
@@ -0,0 +1,167 @@
import { idleDecay, initialAffinity, interact } from './affinity'
import { FUBUKI } from './defaults'
import { readJson, writeJson } from './fs'
import type { Clock, Fs } from './fs'
import { applyDelta, decay, MOOD_EVENTS, moodLabel } from './mood'
import type { MoodEvent } from './mood'
import { STAGES } from './types'
import type { CompanionCore, CompanionState, Global, Pad, UserSettings } from './types'
export const DEFAULT_USER: UserSettings = {
nickname: '',
wake: '08:00',
sleep: '23:00',
tz: '+0800',
careLimit: 5,
quiet: { start: '', end: '' },
}
export type View = {
core: CompanionCore
mood: Pad
label: string
kaomoji: string
affinity: number
stage: string
todos: number
}
export type CompanionOptions = { fs: Fs; clock: Clock; root: string; builtins?: CompanionCore[] }
export function createCompanion({ fs, clock, root, builtins = [FUBUKI] }: CompanionOptions) {
const globalPath = `${root}/global.json`
const userPath = `${root}/user.json`
const dir = (id: string) => `${root}/companions/${id}`
const getGlobal = async (): Promise<Global> => (await readJson<Global>(fs, globalPath)) ?? { active: null, activatedOnce: false }
const getUser = async (): Promise<UserSettings> => ({ ...DEFAULT_USER, ...(await readJson<Partial<UserSettings>>(fs, userPath)) })
const getCore = (id: string) => readJson<CompanionCore>(fs, `${dir(id)}/core.json`)
const getState = (id: string) => readJson<CompanionState>(fs, `${dir(id)}/state.json`)
const putState = (id: string, s: CompanionState) => writeJson(fs, `${dir(id)}/state.json`, s)
async function install(core: CompanionCore) {
if (await getCore(core.id)) return
const now = clock.now()
await writeJson(fs, `${dir(core.id)}/core.json`, core)
await putState(core.id, { mood: { ...core.baseline, at: now }, affinity: initialAffinity(now), todos: 0 })
}
// 第一次啟用自動啟用預設角色;之後即使關閉也不會再自動啟用
async function ensureActivated(): Promise<{ first: boolean; name?: string }> {
const g = await getGlobal()
if (g.activatedOnce) return { first: false }
const def = builtins[0]
if (!def) return { first: false }
await install(def)
await writeJson(fs, globalPath, { active: def.id, activatedOnce: true })
return { first: true, name: def.name }
}
// 衰減只在讀取時計算,不寫回檔案
async function view(): Promise<View | null> {
const { active } = await getGlobal()
if (!active) return null
const core = await getCore(active)
const st = await getState(active)
if (!core || !st) return null
const now = clock.now()
const mood = decay(st.mood, core.baseline, now - st.mood.at, core.neuroticism)
const aff = idleDecay(st.affinity, now)
return { core, mood, ...moodLabel(mood), affinity: aff.value, stage: STAGES[aff.stage]!, todos: st.todos }
}
async function mutate(fn: (core: CompanionCore, st: CompanionState, now: number) => CompanionState) {
const { active } = await getGlobal()
if (!active) return
const core = await getCore(active)
const st = await getState(active)
if (!core || !st) return
await putState(active, fn(core, st, clock.now()))
}
async function moodEvent(ev: MoodEvent) {
await mutate((core, st, now) => {
const cur = decay(st.mood, core.baseline, now - st.mood.at, core.neuroticism)
return { ...st, mood: { ...applyDelta(cur, MOOD_EVENTS[ev]), at: now } }
})
}
async function affinityEvent(kind: 'meaningful' | 'negative') {
await mutate((_, st, now) => ({ ...st, affinity: interact(st.affinity, kind, now) }))
}
const bar = (v: number) => (v >= 0 ? '+' : '') + v.toFixed(2)
async function status(): Promise<string> {
const v = await view()
if (!v) return '目前沒有啟用角色。'
const lines = [`${v.kaomoji} ${v.core.name}|${v.label}`]
lines.push(`心情 PAD:P ${bar(v.mood.p)} A ${bar(v.mood.a)} D ${bar(v.mood.d)}`)
lines.push(`好感:${v.stage}(${bar(v.affinity)})`)
lines.push(`待辦:${v.todos}`)
lines.push('基本資訊:')
for (const [k, f] of Object.entries(v.core.facts)) {
lines.push(f === 'unknown' ? ` ${k}:unknown` : ` ${k}:${f.value}(${f.kind ?? '?'},信心 ${f.confidence ?? '?'},${f.source ?? '無來源'})`)
}
return lines.join('\n')
}
async function available(): Promise<string[]> {
const ids = new Set(await fs.list(`${root}/companions`))
for (const b of builtins) ids.add(b.id)
return [...ids].sort()
}
async function switchTo(id: string): Promise<string> {
const builtin = builtins.find(b => b.id === id)
if (builtin) await install(builtin)
if (!(await getCore(id))) return `找不到角色「${id}」。可用:${(await available()).join('、') || '(無)'}`
const g = await getGlobal()
await writeJson(fs, globalPath, { ...g, active: id, activatedOnce: true })
return `已切換到 ${(await getCore(id))!.name}。`
}
async function setup(args: string[]): Promise<string> {
const u = await getUser()
const known = ['nickname', 'wake', 'sleep', 'tz', 'careLimit', 'quietStart', 'quietEnd']
const bad: string[] = []
for (const a of args) {
const i = a.indexOf('=')
const k = i < 0 ? a : a.slice(0, i)
const val = i < 0 ? '' : a.slice(i + 1)
if (!known.includes(k) || i < 0) bad.push(a)
else if (k === 'careLimit') {
const n = Number(val)
if (!Number.isInteger(n) || n < 0) bad.push(a)
else u.careLimit = n
} else if (k === 'quietStart') u.quiet.start = val
else if (k === 'quietEnd') u.quiet.end = val
else (u as Record<string, unknown>)[k] = val
}
if (bad.length) return `無法解析:${bad.join(' ')}\n用法:/companion setup nickname=… wake=HH:MM sleep=HH:MM tz=+0800 careLimit=N quietStart=HH:MM quietEnd=HH:MM`
if (args.length) await writeJson(fs, userPath, u)
return [
args.length ? '設定已儲存:' : '目前設定(以 key=value 修改):',
` nickname=${u.nickname}`,
` wake=${u.wake} sleep=${u.sleep} tz=${u.tz}`,
` careLimit=${u.careLimit} quietStart=${u.quiet.start} quietEnd=${u.quiet.end}`,
].join('\n')
}
async function command(raw: string): Promise<string> {
const [sub = '', ...rest] = raw.trim().split(/\s+/).filter(Boolean)
if (sub === 'status') return status()
if (sub === 'switch') return rest[0] ? switchTo(rest[0]) : `用法:/companion switch <角色>。可用:${(await available()).join('、')}`
if (sub === 'off') {
const g = await getGlobal()
await writeJson(fs, globalPath, { ...g, active: null, activatedOnce: true })
return '助理已關閉,所有資料保留。'
}
if (sub === 'setup') return setup(rest)
return '用法:/companion status | switch <角色> | off | setup [key=value…]'
}
return { ensureActivated, view, moodEvent, affinityEvent, command, getUser }
}
export type Companion = ReturnType<typeof createCompanion>
@@ -0,0 +1,149 @@
import { readJson, writeJson } from './store'
import { INFO_FIELDS, LAYERS, SOURCE_RANK } from './types'
import type { ApplyReport, Candidate, Conflict, Decision, Fs, Info, InfoField, InfoValue, Line, Memory, ParseResult, ParsedCharacter, PlanItem } from './types'
export interface CharacterCore {
id: string
name: string
aliases: string[]
traits: string[]
info: Info
chapters: string[]
}
const MAX_LINES = 30
const MAX_LINE_LEN = 80
const charDir = (root: string, id: string) => `${root}/characters/${id}`
const slug = (name: string) => name.trim().toLowerCase().replace(/[\s/\\]+/g, '-')
export function listCharacters(fs: Fs, root: string): CharacterCore[] {
const ids = readJson<string[]>(fs, `${root}/characters/index.json`, [])
return ids.flatMap(id => {
const core = readJson<CharacterCore | null>(fs, `${charDir(root, id)}/core.json`, null)
return core ? [core] : []
})
}
function reasonsFor(c: ParsedCharacter, existing: CharacterCore): string[] {
const reasons: string[] = []
const names = [c.name, ...(c.aliases ?? [])]
const theirs = [existing.name, ...existing.aliases]
if (c.name === existing.name) reasons.push(`名字相同:${c.name}`)
for (const a of names) {
if (a !== existing.name && theirs.includes(a)) reasons.push(`別名吻合:${a}`)
}
if (!reasons.length && names.some(n => theirs.some(t => n.length >= 2 && t.length >= 2 && (n.includes(t) || t.includes(n))))) {
reasons.push('名字互相包含')
}
const shared = (c.traits ?? []).filter(t => existing.traits.includes(t))
if (shared.length >= 2) reasons.push(`特徵相同:${shared.join('、')}`)
return reasons
}
/** 產生逐一詢問的清單:主要角色在前、路人在後(可批次詢問),附疑似同一人的理由 */
export function planMerge(fs: Fs, root: string, result: ParseResult): PlanItem[] {
const existing = listCharacters(fs, root)
const items = result.characters.map(c => {
const candidates: Candidate[] = existing
.map(e => ({ id: e.id, reasons: reasonsFor(c, e) }))
.filter(x => x.reasons.length > 0)
return { name: c.name, role: c.role, candidates }
})
return [...items.filter(i => i.role === 'main'), ...items.filter(i => i.role !== 'main')]
}
const uniq = <T>(xs: T[]) => [...new Set(xs)]
function mergeInfo(character: string, core: CharacterCore, incoming: Info, resolutions: Record<string, 'keep' | 'replace'>, conflicts: Conflict[]) {
for (const field of INFO_FIELDS) {
const next = incoming[field]
if (!next || next.value === 'unknown') continue
const cur = core.info[field]
if (!cur || cur.value === 'unknown') {
core.info[field] = next
} else if (cur.value === next.value) {
if (rank(next) > rank(cur)) core.info[field] = next
} else {
const choice = resolutions[`${character}.${field}`]
if (choice === 'replace') core.info[field] = next
else if (choice !== 'keep') conflicts.push({ character, field, existing: cur, incoming: next })
}
}
}
const rank = (v: InfoValue) => (v.source ? SOURCE_RANK[v.source] : 0)
function appendById<T extends { id: string }>(list: T[], add: T[]): T[] {
const seen = new Set(list.map(x => x.id))
return [...list, ...add.filter(x => !seen.has(x.id))]
}
/**
* 套用解析結果。冪等以章節編號為鍵:同一章對同一角色只會寫入一次;
* 合併只追加、不覆蓋;基本資訊衝突回報給呼叫端逐欄位詢問(resolutions 帶回答案)。
*/
export function applyParseResult(
fs: Fs,
root: string,
result: ParseResult,
decisions: Record<string, Decision>,
resolutions: Record<string, 'keep' | 'replace'> = {},
): ApplyReport {
const report: ApplyReport = { applied: [], skipped: [], alreadyApplied: [], conflicts: [] }
const chapter = result.chapter
for (const kind of ['timeline', 'places', 'terms', 'environment'] as const) {
const entries = result.world?.[kind] ?? []
if (!entries.length) continue
const path = `${root}/world/${kind}.json`
const add = entries.map(e => ({ id: `${chapter}:${kind}:${e.order}`, chapter, order: e.order, text: e.text }))
writeJson(fs, path, appendById(readJson<typeof add>(fs, path, []), add))
}
for (const c of result.characters) {
const decision = decisions[c.name]
if (!decision) throw new Error(`缺少人物決定:${c.name}`)
if (decision.action === 'skip') { report.skipped.push(c.name); continue }
const index = readJson<string[]>(fs, `${root}/characters/index.json`, [])
let id: string
if (decision.action === 'merge') {
if (!index.includes(decision.target)) throw new Error(`合併目標不存在:${decision.target}`)
id = decision.target
} else {
id = slug(c.name)
for (let n = 2; index.includes(id); n++) id = `${slug(c.name)}-${n}`
}
const dir = charDir(root, id)
const core = readJson<CharacterCore>(fs, `${dir}/core.json`, { id, name: c.name, aliases: [], traits: [], info: {}, chapters: [] })
if (core.chapters.includes(chapter)) { report.alreadyApplied.push(c.name); continue }
const mine: Conflict[] = []
mergeInfo(c.name, core, c.info ?? {}, resolutions, mine)
report.conflicts.push(...mine)
core.aliases = uniq([...core.aliases, ...(c.name !== core.name ? [c.name] : []), ...(c.aliases ?? [])]).filter(a => a !== core.name)
core.traits = uniq([...core.traits, ...(c.traits ?? [])])
for (const layer of LAYERS) {
const add: Memory[] = (c.memories?.[layer] ?? []).map(m => ({ ...m, id: `${chapter}:${layer}:${m.order}`, chapter, layer }))
if (!add.length) continue
const path = `${dir}/canon/${layer}.json`
writeJson(fs, path, appendById(readJson<Memory[]>(fs, path, []), add))
}
if (c.lines?.length) {
const path = `${dir}/canon/lines.json`
const add = c.lines.map((l: Line) => ({ ...l, text: l.text.slice(0, MAX_LINE_LEN), id: `${chapter}:line:${l.order}`, chapter }))
writeJson(fs, path, appendById(readJson<(typeof add)[number][]>(fs, path, []), add).slice(-MAX_LINES))
}
// 有未解決衝突時不標記章節完成,讓回答後可重跑(記憶以 id 去重,不會重複)
if (!mine.length) core.chapters.push(chapter)
writeJson(fs, `${dir}/core.json`, core)
if (!index.includes(id)) writeJson(fs, `${root}/characters/index.json`, [...index, id])
report.applied.push(id)
}
return report
}
@@ -0,0 +1,147 @@
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'
import { applyParseResult, listCharacters, planMerge } from './apply'
import { applyInference, initPersonality, overrideDim } from './personality'
import { summarizeTics } from './speech'
import { memoryFs } from './store'
import type { ParseResult } from './types'
const ROOT = '/c'
const ch1: ParseResult = {
chapter: '1',
world: { places: [{ text: '星見學院', order: 1 }], terms: [{ text: '星詠', order: 1 }] },
characters: [
{
name: '小葵', role: 'main', aliases: ['葵'], traits: ['tsundere', '短髮'],
info: { bloodType: { value: 'A', source: 'novel', confidence: 0.9 }, height: { value: '158cm', source: 'wiki', confidence: 0.5 } },
memories: { episodic: [{ text: '在校門口遇見主角', importance: 6, order: 1 }] },
lines: [{ text: '才、才不是為了你呢!', kind: 'speech', order: 1 }],
},
{ name: '路人甲', role: 'minor' },
],
}
const decide = { 小葵: { action: 'new' }, 路人甲: { action: 'skip' } } as const
describe('applyParseResult', () => {
test('重複套用同一章是冪等的', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
const snap = new Map(fs.files)
const r = applyParseResult(fs, ROOT, ch1, { ...decide, 小葵: { action: 'merge', target: '小葵' } })
assert.deepEqual(r.alreadyApplied, ['小葵'])
assert.deepEqual(fs.files, snap)
})
test('略過的人物不寫入', () => {
const fs = memoryFs()
const r = applyParseResult(fs, ROOT, ch1, decide)
assert.deepEqual(r.skipped, ['路人甲'])
assert.equal(listCharacters(fs, ROOT).length, 1)
})
test('合併新章節只追加記憶,並保留舊內容', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
applyParseResult(fs, ROOT, {
chapter: '2',
characters: [{ name: '葵', role: 'main', memories: { episodic: [{ text: '一起去圖書館', importance: 4, order: 1 }] } }],
}, { 葵: { action: 'merge', target: '小葵' } })
const mem = JSON.parse(fs.files.get('/c/characters/小葵/canon/episodic.json')!)
assert.deepEqual(mem.map((m: { id: string }) => m.id), ['1:episodic:1', '2:episodic:1'])
assert.equal(listCharacters(fs, ROOT)[0]!.chapters.length, 2)
})
test('基本資訊衝突不會被無聲覆寫,回答後才更新', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
const ch2: ParseResult = {
chapter: '2',
characters: [{ name: '小葵', role: 'main', info: { bloodType: { value: 'B', source: 'wiki', confidence: 0.4 }, hair: { value: '黑色短髮', source: 'novel', confidence: 0.9 } } }],
}
const d = { 小葵: { action: 'merge', target: '小葵' } } as const
const r = applyParseResult(fs, ROOT, ch2, d)
assert.equal(r.conflicts.length, 1)
assert.equal(r.conflicts[0]!.field, 'bloodType')
let core = listCharacters(fs, ROOT)[0]!
assert.equal(core.info.bloodType!.value, 'A')
assert.equal(core.info.hair!.value, '黑色短髮') // 無衝突的欄位照常補上
const r2 = applyParseResult(fs, ROOT, ch2, d, { '小葵.bloodType': 'replace' })
assert.equal(r2.conflicts.length, 0)
core = listCharacters(fs, ROOT)[0]!
assert.equal(core.info.bloodType!.value, 'B')
const mem = JSON.parse(fs.files.get('/c/characters/小葵/canon/episodic.json')!)
assert.equal(mem.length, 1) // 重跑沒有重複寫入
})
test('unknown 不會覆蓋既有值,高優先來源可升級同值', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
const r = applyParseResult(fs, ROOT, {
chapter: '3',
characters: [{ name: '小葵', role: 'main', info: { bloodType: { value: 'unknown', source: null, confidence: 0 }, height: { value: '158cm', source: 'novel', confidence: 0.9 } } }],
}, { 小葵: { action: 'merge', target: '小葵' } })
assert.equal(r.conflicts.length, 0)
const core = listCharacters(fs, ROOT)[0]!
assert.equal(core.info.bloodType!.value, 'A')
assert.equal(core.info.height!.source, 'novel')
})
test('世界觀庫以章節編號去重', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
applyParseResult(fs, ROOT, { ...ch1, characters: [] }, {})
assert.equal(JSON.parse(fs.files.get('/c/world/places.json')!).length, 1)
})
test('缺少人物決定會拒絕', () => {
assert.throws(() => applyParseResult(memoryFs(), ROOT, ch1, {}), /缺少人物決定/)
})
})
describe('planMerge', () => {
test('主要角色在前,並附疑似同一人的理由', () => {
const fs = memoryFs()
applyParseResult(fs, ROOT, ch1, decide)
const plan = planMerge(fs, ROOT, {
chapter: '2',
characters: [{ name: '路人乙', role: 'minor' }, { name: '葵', role: 'main', traits: ['tsundere', '短髮'] }],
})
assert.equal(plan[0]!.name, '葵')
assert.equal(plan[0]!.candidates[0]!.id, '小葵')
assert.ok(plan[0]!.candidates[0]!.reasons.some(r => r.includes('別名')))
assert.deepEqual(plan[1]!.candidates, [])
})
})
describe('personality', () => {
test('推論限制在先驗 ±20 並要求證據', () => {
const p = initPersonality(['tsundere'])
const out = applyInference(p, {
A: { value: 100, evidence: [{ chapter: '1', quote: '才不是為了你' }] },
N: { value: 0, evidence: [] },
})
assert.equal(out.dims.A.value, 55)
assert.equal(out.dims.N.value, p.dims.N.value)
assert.equal(out.dims.A.evidence.length, 1)
})
test('人工覆寫後鎖定,自動推導不再更新', () => {
let p = overrideDim(initPersonality(['genki']), 'E', 70)
p = applyInference(p, { E: { value: 95, evidence: [{ chapter: '1', quote: 'x' }] } })
assert.equal(p.dims.E.value, 70)
})
})
describe('summarizeTics', () => {
test('只採用重複出現的語尾與稱呼', () => {
const t = summarizeTics([
{ text: '先輩、今天也辛苦了にゃ', kind: 'speech', order: 1 },
{ text: '先輩はすごいにゃ', kind: 'speech', order: 2 },
{ text: '偶爾說一次', kind: 'monologue', order: 3 },
])
assert.ok(t.endings.includes('にゃ'))
assert.ok(t.address.includes('先輩'))
})
})
@@ -0,0 +1,63 @@
export const BIG5 = ['O', 'C', 'E', 'A', 'N'] as const
export type Dim = (typeof BIG5)[number]
export type Vec = Record<Dim, number>
export const MAX_DEVIATION = 20
/** 動漫屬性的 Big Five 先驗(設計啟發式,非學術標準) */
export const ARCHETYPE_PRIORS: Record<string, Vec> = {
tsundere: { O: 50, C: 55, E: 55, A: 35, N: 65 },
genki: { O: 70, C: 40, E: 90, A: 70, N: 35 },
kuudere: { O: 45, C: 65, E: 20, A: 45, N: 30 },
dandere: { O: 50, C: 55, E: 15, A: 65, N: 55 },
yandere: { O: 40, C: 50, E: 45, A: 25, N: 85 },
onee: { O: 55, C: 65, E: 60, A: 80, N: 25 },
}
export interface Evidence { chapter: string; quote: string }
export interface DimState { value: number; prior: number; evidence: Evidence[]; locked: boolean }
export interface Personality {
archetypes: string[]
dims: Record<Dim, DimState>
expressiveness: number
gap: number
}
const clamp = (x: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, x))
export function priorFor(archetypes: string[]): Vec {
const known = archetypes.filter(a => ARCHETYPE_PRIORS[a])
const out = {} as Vec
for (const d of BIG5) {
out[d] = known.length ? known.reduce((s, a) => s + ARCHETYPE_PRIORS[a]![d], 0) / known.length : 50
}
return out
}
export function initPersonality(archetypes: string[], expressiveness = 50, gap = 0): Personality {
const prior = priorFor(archetypes)
const dims = {} as Personality['dims']
for (const d of BIG5) dims[d] = { value: prior[d], prior: prior[d], evidence: [], locked: false }
return { archetypes, dims, expressiveness, gap }
}
/** 套用模型推論:限制在先驗 ±20、附證據;人工鎖定的維度不更新;缺證據不採用 */
export function applyInference(p: Personality, inferred: Partial<Record<Dim, { value: number; evidence: Evidence[] }>>): Personality {
const dims = { ...p.dims }
for (const d of BIG5) {
const inf = inferred[d]
const cur = dims[d]
if (!inf || cur.locked || !inf.evidence.length) continue
dims[d] = {
...cur,
value: clamp(inf.value, cur.prior - MAX_DEVIATION, cur.prior + MAX_DEVIATION),
evidence: [...cur.evidence, ...inf.evidence],
}
}
return { ...p, dims }
}
/** 人工覆寫並鎖定,之後自動推導不會改動 */
export function overrideDim(p: Personality, dim: Dim, value: number): Personality {
return { ...p, dims: { ...p.dims, [dim]: { ...p.dims[dim], value: clamp(value, 0, 100), locked: true } } }
}
@@ -0,0 +1,20 @@
import type { Line } from './types'
export interface SpeechTics { endings: string[]; catchphrases: string[]; address: string[] }
const ENDING = /([ぁ-んァ-ヶー]{1,2}|[啦喔啊呢吧嘛囉喲哦呀])[!?!?。…~~]*$/
const CALL = /([^\s,。!?、「」]{0,6}(?:さん|くん|ちゃん|様|先輩|同學|大人|先生|小姐))/g
/** 從對白與獨白歸納口癖:語尾、重複出現的口頭禪、稱呼;出現至少 minCount 次才採用 */
export function summarizeTics(lines: Line[], minCount = 2): SpeechTics {
const count = (items: string[]) => {
const m = new Map<string, number>()
for (const i of items) m.set(i, (m.get(i) ?? 0) + 1)
return [...m].filter(([, n]) => n >= minCount).sort((a, b) => b[1] - a[1]).map(([k]) => k)
}
const texts = lines.map(l => l.text.trim()).filter(Boolean)
const endings = count(texts.flatMap(t => { const m = ENDING.exec(t); return m ? [m[1]!] : [] }))
const address = count(texts.flatMap(t => [...t.matchAll(CALL)].map(m => m[1]!)))
const catchphrases = count(texts.flatMap(t => t.split(/[!?!?。…、,,\s]+/).filter(s => s.length >= 2 && s.length <= 8)))
return { endings, catchphrases, address }
}
@@ -0,0 +1,19 @@
import type { Fs } from './types'
export function readJson<T>(fs: Fs, path: string, fallback: T): T {
const raw = fs.read(path)
return raw === null ? fallback : (JSON.parse(raw) as T)
}
export function writeJson(fs: Fs, path: string, data: unknown): void {
fs.write(path, JSON.stringify(data, null, 2))
}
export function memoryFs(): Fs & { files: Map<string, string> } {
const files = new Map<string, string>()
return {
files,
read: p => files.get(p) ?? null,
write: (p, d) => void files.set(p, d),
}
}
@@ -0,0 +1,58 @@
export interface Fs {
read(path: string): string | null
/** 原子寫入:先寫暫存檔再改名 */
write(path: string, data: string): void
}
export type Source = 'novel' | 'official' | 'wiki'
export const SOURCE_RANK: Record<Source, number> = { novel: 3, official: 2, wiki: 1 }
export const INFO_FIELDS = ['birthday', 'zodiac', 'bloodType', 'hair', 'face', 'height', 'weight', 'measurements'] as const
export type InfoField = (typeof INFO_FIELDS)[number]
export interface InfoValue {
value: string
source: Source | null
confidence: number
chapter?: string
}
export type Info = Partial<Record<InfoField, InfoValue>>
export type Layer = 'episodic' | 'semantic' | 'emotional' | 'procedural'
export const LAYERS: Layer[] = ['episodic', 'semantic', 'emotional', 'procedural']
export interface MemoryInput { text: string; importance: number; order: number }
export interface Memory extends MemoryInput { id: string; chapter: string; layer: Layer }
export interface Line { text: string; kind: 'speech' | 'monologue'; order: number }
export interface ParsedCharacter {
name: string
aliases?: string[]
role: 'main' | 'minor'
traits?: string[]
info?: Info
memories?: Partial<Record<Layer, MemoryInput[]>>
lines?: Line[]
}
export interface WorldEntry { text: string; order: number }
export interface ParseResult {
chapter: string
world?: { timeline?: WorldEntry[]; places?: WorldEntry[]; terms?: WorldEntry[]; environment?: WorldEntry[] }
characters: ParsedCharacter[]
}
export type Decision = { action: 'merge'; target: string } | { action: 'new' } | { action: 'skip' }
export interface Candidate { id: string; reasons: string[] }
export interface PlanItem { name: string; role: 'main' | 'minor'; candidates: Candidate[] }
export interface Conflict { character: string; field: InfoField; existing: InfoValue; incoming: InfoValue }
export interface ApplyReport {
applied: string[]
skipped: string[]
alreadyApplied: string[]
conflicts: Conflict[]
}
@@ -0,0 +1,22 @@
import type { CompanionCore } from './types'
const OFFICIAL = 'https://hololive.hololivepro.com/en/talents/shirakami-fubuki/'
// 只放帶來源的事實欄位與官方公開的招牌短語;個性、記憶、口癖表在本機生成,不進 repo
export const FUBUKI: CompanionCore = {
id: 'fubuki',
name: '白上吹雪',
facts: {
birthday: { value: '10/5', source: OFFICIAL, kind: 'official', confidence: 1 },
zodiac: { value: '天秤座', source: `${OFFICIAL}(由生日推得)`, kind: 'official', confidence: 0.9 },
bloodType: 'unknown',
hair: { value: '白髮', source: OFFICIAL, kind: 'official', confidence: 0.8 },
face: 'unknown',
height: { value: '155cm', source: OFFICIAL, kind: 'official', confidence: 1 },
weight: 'unknown',
measurements: 'unknown',
},
phrases: ['Good foxy day!', 'Hi, Friends!'],
neuroticism: 50,
baseline: { p: 0.2, a: 0.1, d: 0.1 },
}
+58
View File
@@ -0,0 +1,58 @@
// 檔案系統與時鐘皆由外部注入;核心只依賴這兩個介面
export interface Fs {
read(path: string): Promise<string | null>
write(path: string, data: string): Promise<void>
rename(from: string, to: string): Promise<void>
list(dir: string): Promise<string[]>
}
export type Clock = { now(): number }
// 先寫暫存檔再 rename,當機時不會留下寫到一半的檔案
export async function writeAtomic(fs: Fs, path: string, data: string): Promise<void> {
const tmp = `${path}.tmp`
await fs.write(tmp, data)
await fs.rename(tmp, path)
}
export async function readJson<T>(fs: Fs, path: string): Promise<T | null> {
const raw = await fs.read(path)
if (raw === null) return null
try {
return JSON.parse(raw) as T
} catch {
return null
}
}
export const writeJson = (fs: Fs, path: string, value: unknown) => writeAtomic(fs, path, JSON.stringify(value, null, 2))
export function memoryFs(seed: Record<string, string> = {}): Fs & { files: Map<string, string> } {
const files = new Map(Object.entries(seed))
return {
files,
async read(path) {
return files.get(path) ?? null
},
async write(path, data) {
files.set(path, data)
},
async rename(from, to) {
const v = files.get(from)
if (v === undefined) throw new Error(`ENOENT: ${from}`)
files.delete(from)
files.set(to, v)
},
async list(dir) {
const prefix = dir.endsWith('/') ? dir : dir + '/'
const names = new Set<string>()
for (const k of files.keys()) if (k.startsWith(prefix)) names.add(k.slice(prefix.length).split('/')[0]!)
return [...names].filter(n => !n.endsWith('.tmp')).sort()
},
}
}
export function fakeClock(start = Date.parse('2026-10-12T00:00:00Z')) {
let t = start
return { now: () => t, advance: (ms: number) => void (t += ms), set: (ms: number) => void (t = ms) }
}
+53
View File
@@ -0,0 +1,53 @@
// 檔案系統與時鐘由外部注入,核心不直接碰真實環境
export interface Fs {
read(path: string): Promise<string | null>
/** 原子寫入:先寫暫存檔再改名 */
write(path: string, data: string): Promise<void>
}
export type Clock = () => number
export function memoryFs(): Fs & { files: Map<string, string> } {
const files = new Map<string, string>()
return {
files,
async read(p) {
return files.get(p) ?? null
},
async write(p, d) {
files.set(p, d)
},
}
}
export async function readJson<T>(fs: Fs, path: string, fallback: T): Promise<T> {
const raw = await fs.read(path)
if (raw === null) return fallback
try {
return JSON.parse(raw) as T
} catch {
return fallback
}
}
export const writeJson = (fs: Fs, path: string, value: unknown) => fs.write(path, JSON.stringify(value, null, 2))
export async function nodeFs(): Promise<Fs> {
const { readFile, writeFile, rename, mkdir } = await import('node:fs/promises')
const { dirname } = await import('node:path')
return {
async read(p) {
try {
return await readFile(p, 'utf8')
} catch {
return null
}
},
async write(p, d) {
await mkdir(dirname(p), { recursive: true })
const tmp = `${p}.${process.pid}.tmp`
await writeFile(tmp, d)
await rename(tmp, p)
},
}
}
@@ -0,0 +1,143 @@
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'
import { memoryFs } from './io'
import { CONSOLIDATE_THRESHOLD, FORGET_BELOW, TOP_K, createMemory, redact, retention, stability } from './memory'
const H = 3600_000
const T0 = Date.UTC(2026, 9, 12, 10, 0)
function setup() {
let now = T0
const fs = memoryFs()
const mem = createMemory(fs, () => now, 'mem.json')
return { mem, fs, advance: (ms: number) => void (now += ms), now: () => now }
}
describe('記憶檢索', () => {
test('相關性高者排前面', async () => {
const s = setup()
await s.mem.add({ layer: 'semantic', text: '使用者喜歡抹茶拿鐵', importance: 5, source: 'u' })
await s.mem.add({ layer: 'semantic', text: '使用者養了一隻貓', importance: 5, source: 'u' })
const r = await s.mem.recall('抹茶')
assert.equal(r[0]!.text, '使用者喜歡抹茶拿鐵')
})
test('較新、較重要者在其他條件相同時勝出', async () => {
const s = setup()
const old = await s.mem.add({ layer: 'episodic', text: '討論部署', importance: 5, source: 'u' })
s.advance(100 * H)
const fresh = await s.mem.add({ layer: 'episodic', text: '討論部署', importance: 5, source: 'u' })
assert.equal((await s.mem.recall('部署'))[0]!.id, fresh.id)
assert.notEqual(old.id, fresh.id)
const s2 = setup()
await s2.mem.add({ layer: 'episodic', text: '討論測試', importance: 2, source: 'u' })
const hi = await s2.mem.add({ layer: 'episodic', text: '討論測試', importance: 9, source: 'u' })
assert.equal((await s2.mem.recall('測試'))[0]!.id, hi.id)
})
test('情緒吻合度影響排序', async () => {
const s = setup()
await s.mem.add({ layer: 'emotional', text: '一起慶祝', importance: 5, source: 'u', emotion: { p: -0.8, a: 0.5, d: -0.5 } })
const happy = await s.mem.add({ layer: 'emotional', text: '一起慶祝', importance: 5, source: 'u', emotion: { p: 0.8, a: 0.5, d: 0.5 } })
assert.equal((await s.mem.recall('慶祝', { p: 0.7, a: 0.4, d: 0.4 }))[0]!.id, happy.id)
})
test('最多取前 6 筆,並累計取用次數', async () => {
const s = setup()
for (let i = 0; i < 10; i++) await s.mem.add({ layer: 'semantic', text: `筆記${i}`, importance: 5, source: 'u' })
const r = await s.mem.recall('筆記')
assert.equal(r.length, TOP_K)
assert.ok((await s.mem.all()).filter(m => m.accessCount === 1).length === TOP_K)
})
test('寫入前過濾疑似金鑰', async () => {
const s = setup()
const m = await s.mem.add({ layer: 'episodic', text: '金鑰是 sk-abcdefghijklmnop1234 請記住', importance: 3, source: 'u' })
assert.ok(!m.text.includes('sk-abcdef'))
assert.ok(m.text.includes('[已過濾]'))
assert.equal(redact('普通的一句話'), '普通的一句話')
})
})
describe('遺忘曲線', () => {
test('保留率隨時間下降,重要度與取用次數使其變慢', () => {
const base = { id: 1, layer: 'episodic' as const, text: '', source: '', createdAt: T0, lastAccess: T0 }
const weak = { ...base, importance: 2, accessCount: 0 }
const strong = { ...base, importance: 8, accessCount: 0 }
const used = { ...weak, accessCount: 5 }
assert.ok(retention(weak, T0 + 24 * H) < retention(weak, T0))
assert.ok(retention(strong, T0 + 100 * H) > retention(weak, T0 + 100 * H))
assert.ok(retention(used, T0 + 100 * H) > retention(weak, T0 + 100 * H))
assert.ok(stability(strong) > stability(weak))
})
test('鞏固時淘汰低保留率的記憶,保留重要與常用的', async () => {
const s = setup()
const trivial = await s.mem.add({ layer: 'episodic', text: '隨口聊天', importance: 1, source: 'u' })
const key = await s.mem.add({ layer: 'semantic', text: '生日是三月', importance: 9, source: 'u' })
s.advance(10 * 24 * H)
assert.ok(retention(trivial, s.now()) < FORGET_BELOW)
const r = await s.mem.consolidate({ force: true })
assert.equal(r.forgotten, 1)
const ids = (await s.mem.all()).map(m => m.id)
assert.ok(!ids.includes(trivial.id))
assert.ok(ids.includes(key.id))
})
test('取用過的記憶會重置時效而不被淘汰', async () => {
const s = setup()
const m = await s.mem.add({ layer: 'episodic', text: '常提起的事', importance: 2, source: 'u' })
s.advance(40 * H)
await s.mem.recall('常提起')
s.advance(40 * H)
await s.mem.consolidate({ force: true })
assert.ok((await s.mem.all()).some(x => x.id === m.id))
})
})
describe('鞏固與反思', () => {
test('累積重要度未達門檻不執行,達門檻才執行並歸零', async () => {
const s = setup()
await s.mem.add({ layer: 'episodic', text: '小事', importance: 5, source: 'u' })
assert.deepEqual(await s.mem.consolidate(), { ran: false, forgotten: 0 })
for (let i = 0; i < 5; i++) await s.mem.add({ layer: 'episodic', text: `事件${i}`, importance: 5, source: 'u' })
assert.ok((await s.mem.pending()) >= CONSOLIDATE_THRESHOLD)
const r = await s.mem.consolidate()
assert.equal(r.ran, true)
assert.equal(await s.mem.pending(), 0)
assert.equal(r.reflection?.layer, 'reflection')
})
test('手動 reflect(force)即使未達門檻也執行,並使用注入的摘要', async () => {
const s = setup()
await s.mem.add({ layer: 'episodic', text: '一起修好了建置', importance: 4, source: 'u' })
const r = await s.mem.consolidate({ force: true, summarize: items => `反思:${items.length} 件事` })
assert.equal(r.ran, true)
assert.equal(r.reflection?.text, '反思:1 件事')
assert.ok(r.reflection!.source.startsWith('reflect:'))
})
test('反思不重複涵蓋舊事件,也不遞迴反思反思', async () => {
const s = setup()
await s.mem.add({ layer: 'episodic', text: '甲', importance: 4, source: 'u' })
await s.mem.consolidate({ force: true })
s.advance(H)
await s.mem.add({ layer: 'episodic', text: '乙', importance: 4, source: 'u' })
const r = await s.mem.consolidate({ force: true, summarize: items => items.map(i => i.text).join('+') })
assert.equal(r.reflection?.text, '乙')
})
test('沒有新記憶時鞏固不產生空反思', async () => {
const s = setup()
const r = await s.mem.consolidate({ force: true })
assert.equal(r.reflection, undefined)
})
test('資料經檔案持久化,重建後可繼續', async () => {
const s = setup()
await s.mem.add({ layer: 'semantic', text: '喜歡貓', importance: 5, source: 'u' })
const again = createMemory(s.fs, s.now, 'mem.json')
assert.equal((await again.recall('貓'))[0]!.text, '喜歡貓')
})
})
+201
View File
@@ -0,0 +1,201 @@
import type { Clock, Fs } from './io'
import { readJson, writeJson } from './io'
export type Layer = 'episodic' | 'semantic' | 'emotional' | 'procedural' | 'reflection'
export interface Pad {
p: number
a: number
d: number
}
export interface Memory {
id: number
layer: Layer
text: string
/** 重要度 1–10 */
importance: number
source: string
createdAt: number
lastAccess: number
accessCount: number
emotion?: Pad
}
export interface MemoryDoc {
items: Memory[]
nextId: number
/** 自上次鞏固以來累積的重要度 */
pending: number
lastConsolidated: number
/** 已被反思涵蓋的最大記憶編號 */
reflectedUpTo?: number
}
export const HALF_LIFE_H = 72
export const TOP_K = 6
export const WEIGHTS = { recency: 1, importance: 1, relevance: 2, emotion: 0.5 }
/** 累積重要度達此值就鞏固 */
export const CONSOLIDATE_THRESHOLD = 30
/** 保留率低於此值即遺忘 */
export const FORGET_BELOW = 0.1
/** 穩定度基準(小時) */
const BASE_STABILITY_H = 24
const HOUR = 3600_000
/** 穩定度(小時)隨重要度與取用次數成長 */
export const stability = (m: Pick<Memory, 'importance' | 'accessCount'>) =>
BASE_STABILITY_H * m.importance * (1 + Math.log1p(m.accessCount))
/** 保留率:自最近一次取用起算 */
export function retention(m: Memory, now: number): number {
const h = Math.max(0, now - m.lastAccess) / HOUR
return Math.exp(-h / stability(m))
}
const tokens = (s: string) => {
const out = new Set<string>()
const latin = s.toLowerCase().match(/[a-z0-9]+/g) ?? []
for (const w of latin) out.add(w)
const cjk = s.replace(/[^一-鿿]/g, '')
for (let i = 0; i < cjk.length; i++) {
out.add(cjk[i]!)
if (i + 1 < cjk.length) out.add(cjk.slice(i, i + 2))
}
return out
}
function relevance(query: string, text: string): number {
const q = tokens(query)
if (!q.size) return 0
const t = tokens(text)
let hit = 0
for (const w of q) if (t.has(w)) hit++
return hit / q.size
}
function moodFit(a?: Pad, b?: Pad): number {
if (!a || !b) return 0
const dist = Math.hypot(a.p - b.p, a.a - b.a, a.d - b.d) / Math.hypot(2, 2, 2)
return 1 - dist
}
export function score(m: Memory, query: string, now: number, mood?: Pad): number {
const recency = Math.pow(0.5, Math.max(0, now - m.lastAccess) / HOUR / HALF_LIFE_H)
return (
WEIGHTS.recency * recency +
WEIGHTS.importance * (m.importance / 10) +
WEIGHTS.relevance * relevance(query, m.text) +
WEIGHTS.emotion * moodFit(m.emotion, mood)
)
}
/** 疑似金鑰的字串在寫入前遮蔽 */
export function redact(s: string): string {
return s
.replace(/\b(sk|pk|ghp|gho|xox[bap]|AKIA)[-_A-Za-z0-9]{12,}/g, '[已過濾]')
.replace(/\b[A-Za-z0-9+/_-]{32,}\b/g, '[已過濾]')
}
export type Summarize = (items: Memory[]) => string
/** 預設摘要:不呼叫模型,直接串接重點;實際反思文字可由外部注入模型版 */
const defaultSummarize: Summarize = items =>
items
.slice(0, 5)
.map(i => i.text)
.join(';')
export interface MemoryStore {
add(input: { layer: Layer; text: string; importance: number; source: string; emotion?: Pad }): Promise<Memory>
/** 檢索前 6 筆,並更新取用紀錄 */
recall(query: string, mood?: Pad): Promise<Memory[]>
/** 累積達門檻、會話結束或手動 reflect 時鞏固 */
consolidate(opts?: { force?: boolean; summarize?: Summarize }): Promise<{ ran: boolean; forgotten: number; reflection?: Memory }>
all(): Promise<Memory[]>
pending(): Promise<number>
}
export function createMemory(fs: Fs, clock: Clock, path: string): MemoryStore {
const load = () => readJson<MemoryDoc>(fs, path, { items: [], nextId: 1, pending: 0, lastConsolidated: 0 })
const save = (d: MemoryDoc) => writeJson(fs, path, d)
return {
async add(i) {
const d = await load()
const now = clock()
const m: Memory = {
id: d.nextId++,
layer: i.layer,
text: redact(i.text),
importance: Math.min(10, Math.max(1, i.importance)),
source: i.source,
createdAt: now,
lastAccess: now,
accessCount: 0,
emotion: i.emotion,
}
d.items.push(m)
d.pending += m.importance
await save(d)
return m
},
async recall(query, mood) {
const d = await load()
const now = clock()
const top = d.items
.map(m => ({ m, s: score(m, query, now, mood) }))
.sort((a, b) => b.s - a.s || b.m.id - a.m.id)
.slice(0, TOP_K)
.map(x => x.m)
for (const m of top) {
m.lastAccess = now
m.accessCount++
}
if (top.length) await save(d)
return top
},
async consolidate(opts = {}) {
const d = await load()
const now = clock()
if (!opts.force && d.pending < CONSOLIDATE_THRESHOLD) return { ran: false, forgotten: 0 }
// 反思:把上次鞏固後新增、尚未被反思涵蓋的記憶濃縮成一筆
const fresh = d.items.filter(m => m.layer !== 'reflection' && m.id > (d.reflectedUpTo ?? 0))
let reflection: Memory | undefined
if (fresh.length) {
const top = [...fresh].sort((a, b) => b.importance - a.importance || b.id - a.id)
reflection = {
id: d.nextId++,
layer: 'reflection',
text: redact((opts.summarize ?? defaultSummarize)(top)),
importance: Math.min(10, Math.max(...top.map(t => t.importance)) + 1),
source: `reflect:${top.map(t => t.id).join(',')}`,
createdAt: now,
lastAccess: now,
accessCount: 0,
}
d.items.push(reflection)
}
d.reflectedUpTo = d.nextId - 1
// 遺忘:保留率過低的記憶淘汰(反思本身不在本輪淘汰)
const before = d.items.length
d.items = d.items.filter(m => m === reflection || retention(m, now) >= FORGET_BELOW)
d.pending = 0
d.lastConsolidated = now
await save(d)
return { ran: true, forgotten: before - d.items.length, reflection }
},
async all() {
return (await load()).items
},
async pending() {
return (await load()).pending
},
}
}
+60
View File
@@ -0,0 +1,60 @@
import type { Pad } from './types'
export const clamp = (x: number, lo = -1, hi = 1) => Math.max(lo, Math.min(hi, x))
const HOUR = 3_600_000
const MOOD_HALF_LIFE_H = 8
// 高神經質的角色情緒消退較慢:50 為基準,0→0.5 倍,100→1.5 倍
export const halfLife = (neuroticism: number) => MOOD_HALF_LIFE_H * HOUR * (0.5 + neuroticism / 100)
// 讀檔時才計算:x = B + (x − B)·0.5^(Δ/τ)
export function decay(mood: Pad, baseline: Pad, elapsedMs: number, neuroticism: number): Pad {
if (elapsedMs <= 0) return { p: mood.p, a: mood.a, d: mood.d }
const k = Math.pow(0.5, elapsedMs / halfLife(neuroticism))
return {
p: baseline.p + (mood.p - baseline.p) * k,
a: baseline.a + (mood.a - baseline.a) * k,
d: baseline.d + (mood.d - baseline.d) * k,
}
}
// 單軸每回合變化上限 ±0.3
export const TURN_CAP = 0.3
export function applyDelta(mood: Pad, delta: Pad): Pad {
const step = (x: number, dx: number) => clamp(x + clamp(dx, -TURN_CAP, TURN_CAP))
return { p: step(mood.p, delta.p), a: step(mood.a, delta.a), d: step(mood.d, delta.d) }
}
export const MOOD_EVENTS = {
thanks: { p: 0.15, a: 0.05, d: 0.05 },
scold: { p: -0.2, a: 0.15, d: -0.1 },
success: { p: 0.08, a: 0.03, d: 0.05 },
toolFail: { p: -0.08, a: 0.1, d: -0.08 },
return: { p: 0.1, a: 0.08, d: 0 },
lateNight: { p: -0.05, a: -0.1, d: 0 },
favorite: { p: 0.12, a: 0.08, d: 0.03 },
ignored: { p: -0.05, a: -0.03, d: -0.03 },
} as const satisfies Record<string, Pad>
export type MoodEvent = keyof typeof MOOD_EVENTS
// 八分區(Mehrabian 的 PAD 八分類)→ 情緒標籤與顏文字
const OCTANTS: Record<string, { label: string; kaomoji: string }> = {
'+++': { label: '興奮', kaomoji: '(≧▽≦)' },
'++-': { label: '撒嬌', kaomoji: '(๑>ᴗ<๑)' },
'+-+': { label: '放鬆', kaomoji: '(´ᴗ`)' },
'+--': { label: '溫順', kaomoji: '(︶ᴗ︶)' },
'---': { label: '無聊', kaomoji: '(-_-)' },
'--+': { label: '不屑', kaomoji: '(¬_¬)' },
'-++': { label: '生氣', kaomoji: '(╬`益´)' },
'-+-': { label: '焦慮', kaomoji: '(;´Д`)' },
}
const CALM = { label: '平靜', kaomoji: '(・ω・)' }
const CALM_RADIUS = 0.1
export function moodLabel(m: Pad): { label: string; kaomoji: string } {
if (Math.abs(m.p) < CALM_RADIUS && Math.abs(m.a) < CALM_RADIUS && Math.abs(m.d) < CALM_RADIUS) return CALM
const s = (x: number) => (x >= 0 ? '+' : '-')
return OCTANTS[s(m.p) + s(m.a) + s(m.d)]!
}
@@ -0,0 +1,26 @@
import { mkdir, readdir, readFile, rename, writeFile } from 'node:fs/promises'
import { dirname } from 'node:path'
import type { Fs } from './fs'
export const nodeFs: Fs = {
async read(path) {
try {
return await readFile(path, 'utf8')
} catch {
return null
}
},
async write(path, data) {
await mkdir(dirname(path), { recursive: true })
await writeFile(path, data, 'utf8')
},
rename,
async list(dir) {
try {
return (await readdir(dir)).filter(n => !n.endsWith('.tmp')).sort()
} catch {
return []
}
},
}
@@ -0,0 +1,60 @@
import { clamp } from './types.ts'
export const GAIN = 0.01
export const DAILY_CAP = 0.05
export const LOSS = 0.03
export const IDLE_DAYS = 7
export const HALF_LIFE_DAYS = 30
const DAY = 86_400_000
export type Stage = 0 | 1 | 2 | 3
export const STAGE_NAMES = ['陌生', '熟悉', '親近', '特別'] as const
// 升階門檻;降階門檻 = 升階 − 0.05(遲滯)
const UP = [0.1, 0.3, 0.6]
const HYST = 0.05
export type Affinity = {
value: number
stage: Stage
/** 今日已增加量與其日期 */
gainDay: string
gainToday: number
lastSeen: number
}
export const initialAffinity = (now: number): Affinity => ({ value: 0, stage: 0, gainDay: dayKey(now), gainToday: 0, lastSeen: now })
export const dayKey = (t: number) => new Date(t).toISOString().slice(0, 10)
export function stageFor(value: number, prev: Stage): Stage {
let s = prev
while (s < 3 && value >= UP[s]!) s++
while (s > 0 && value < UP[s - 1]! - HYST) s--
return s as Stage
}
/** 讀取時計算:離開超過 7 天後以 30 天半衰期回到 0 */
export function settle(a: Affinity, now: number): Affinity {
const idleDays = (now - a.lastSeen) / DAY
if (idleDays <= IDLE_DAYS) return a
const f = Math.pow(0.5, (idleDays - IDLE_DAYS) / HALF_LIFE_DAYS)
const value = a.value * f
return { ...a, value, stage: stageFor(value, a.stage), lastSeen: now }
}
export function meaningful(a: Affinity, now: number): Affinity {
const s = settle(a, now)
const today = dayKey(now)
const gainToday = s.gainDay === today ? s.gainToday : 0
const gain = clamp(Math.min(GAIN, DAILY_CAP - gainToday), 0, GAIN)
const value = clamp(s.value + gain, -1, 1)
return { value, stage: stageFor(value, s.stage), gainDay: today, gainToday: gainToday + gain, lastSeen: now }
}
export function negative(a: Affinity, now: number): Affinity {
const s = settle(a, now)
const value = clamp(s.value - LOSS, -1, 1)
const today = dayKey(now)
return { ...s, value, stage: stageFor(value, s.stage), gainDay: today, gainToday: s.gainDay === today ? s.gainToday : 0, lastSeen: now }
}
@@ -0,0 +1,159 @@
import assert from 'node:assert/strict'
import { describe, test } from 'node:test'
import { createCompanion } from './core.ts'
import type { Card } from './core.ts'
import { estimateTokens } from './inject.ts'
import { parseMoodTag } from './mood.ts'
import { redactSecrets } from './secrets.ts'
import type { Fs } from './types.ts'
// 記憶體檔案系統與假時鐘
function memFs(): Fs & { files: Map<string, string> } {
const files = new Map<string, string>()
return { files, read: async p => files.get(p) ?? null, write: async (p, d) => void files.set(p, d) }
}
const T0 = Date.parse('2026-10-12T10:00:00Z')
function clock(t = T0) {
return { now: () => t, set: (x: number) => (t = x), advance: (ms: number) => (t += ms) }
}
// 原創範例角色
const CARD: Card = {
name: '小澄',
lines: ['個性:開朗、愛照顧人', '口癖:句尾常說「喔」', '喜歡:紅茶'],
}
const KEY = 'sk-abcdefghijklmnop1234567890'
async function setup() {
const fs = memFs()
const clk = clock()
fs.files.set('/c/sumi/card.json', JSON.stringify(CARD))
const c = createCompanion({ fs, clock: clk, root: '/c', id: 'sumi' })
return { fs, clk, c }
}
describe('心情標記', () => {
test('解析並移除末尾標記', () => {
const r = parseMoodTag('好的喔!\n<!--mood p=0.1 a=-0.05 d=0-->')
assert.equal(r.text, '好的喔!')
assert.deepEqual(r.delta, { p: 0.1, a: -0.05, d: 0 })
})
test('格式錯誤時移除標記且無 delta', () => {
const r = parseMoodTag('好的喔!<!--mood p=abc-->')
assert.equal(r.text, '好的喔!')
assert.equal(r.delta, null)
})
test('缺失標記退回關鍵字規則', async () => {
const { c } = await setup()
await c.userTurn('謝謝你,太棒了')
const shown = await c.assistantReply('不客氣喔')
assert.equal(shown, '不客氣喔')
assert.ok((await c.mood()).pad.p > 0)
})
test('標記有效時採用標記', async () => {
const { c } = await setup()
await c.userTurn('隨便聊聊')
await c.assistantReply('嗯喔<!--mood p=-0.2 a=0 d=0-->')
assert.ok((await c.mood()).pad.p < 0)
})
})
describe('心情上限與事件', () => {
test('單軸每回合變化不超過 0.3', async () => {
const { c } = await setup()
await c.userTurn('你很笨', ['scold', 'scold', 'scold'])
await c.assistantReply('嗚<!--mood p=-0.9 a=0.9 d=-0.9-->')
const { pad } = await c.mood()
assert.ok(Math.abs(pad.p) <= 0.3 + 1e-9 && Math.abs(pad.a) <= 0.3 + 1e-9 && Math.abs(pad.d) <= 0.3 + 1e-9)
assert.ok(pad.p < 0)
})
test('事件表:稱讚提升愉悅', async () => {
const { c } = await setup()
await c.userTurn('做得好', ['praise'])
assert.ok(Math.abs((await c.mood()).pad.p - 0.15) < 1e-9)
})
})
describe('好感度', () => {
test('有意義互動 +0.01,每日上限 +0.05', async () => {
const { c } = await setup()
await c.userTurn('今天的進度如何')
assert.ok(Math.abs((await c.affinity()).value - 0.01) < 1e-9)
for (let i = 0; i < 20; i++) await c.userTurn('繼續處理這個問題吧')
assert.ok(Math.abs((await c.affinity()).value - 0.05) < 1e-9)
})
test('負面事件 −0.03', async () => {
const { c } = await setup()
await c.userTurn('隨便', ['scold'])
assert.ok(Math.abs((await c.affinity()).value + 0.03) < 1e-9)
})
test('離開超過 7 天後回落', async () => {
const { c, clk } = await setup()
for (let i = 0; i < 5; i++) await c.userTurn('我們繼續吧', [])
const before = (await c.affinity()).value
clk.advance(37 * 86_400_000)
assert.ok(Math.abs((await c.affinity()).value - before / 2) < 1e-9)
})
})
describe('互動記憶', () => {
test('與原著記憶分開存放,且只存對話文字', async () => {
const { c, fs } = await setup()
await c.userTurn('幫我看看這段')
await c.assistantReply('好喔\n```ts\nconst a = 1\n```\n看完了<!--mood p=0 a=0 d=0-->')
const key = [...fs.files.keys()].find(k => k.includes('/interaction/'))!
assert.ok(key.startsWith('/c/sumi/interaction/'))
const stored = fs.files.get(key)!
assert.ok(!stored.includes('const a = 1'))
assert.ok(!stored.includes('mood'))
})
test('金鑰在寫入前被過濾', async () => {
const { c, fs } = await setup()
await c.userTurn(`我的金鑰是 ${KEY} 請記住`)
for (const v of fs.files.values()) assert.ok(!v.includes(KEY))
})
test('累積後壓縮成摘要', async () => {
const { c, fs } = await setup()
for (let i = 0; i < 35; i++) await c.userTurn(`第 ${i} 則訊息內容`)
const m = JSON.parse(fs.files.get('/c/sumi/interaction/memory.json')!)
assert.ok(m.recent.length <= 30)
assert.ok(m.summary.length > 0)
})
})
describe('人設注入', () => {
test('角色卡在前、心情與記憶在後', async () => {
const { c } = await setup()
await c.userTurn('我喜歡紅茶')
const out = await c.buildInjection()
assert.ok(out.indexOf('## 角色') < out.indexOf('## 互動記憶'))
assert.ok(out.indexOf('## 互動記憶') < out.indexOf('## 目前心情'))
})
test('不含疑似金鑰與工具輸出', async () => {
const { c } = await setup()
await c.userTurn(`token=${KEY}`)
await c.assistantReply(`好的 ${KEY}<!--mood p=0 a=0 d=0-->`)
const out = await c.buildInjection({ care: [`提醒 ${KEY}`], lore: [{ text: `設定 ${KEY}`, score: 1 }] })
assert.ok(!out.includes(KEY))
assert.equal(redactSecrets(out), out)
})
test('超出預算時截斷低分項目', async () => {
const { c } = await setup()
for (let i = 0; i < 30; i++) await c.userTurn(`這是一則相當長的訊息,用來撐滿預算,編號 ${i},${'字'.repeat(60)}`)
const out = await c.buildInjection()
const mem = out.split('## 互動記憶\n')[1]!.split('\n\n')[0]!
assert.ok(estimateTokens(mem) <= 800)
assert.ok(mem.includes('編號 29'))
assert.ok(!mem.includes('編號 0,'))
assert.ok(estimateTokens(out) <= 1600)
})
test('tone off 時不注入語氣規則', async () => {
const { c } = await setup()
assert.ok((await c.buildInjection()).includes('逐字保持原樣'))
assert.equal(await c.command('tone off'), '語氣已關閉')
assert.ok(!(await c.buildInjection()).includes('逐字保持原樣'))
await c.command('tone on')
assert.ok((await c.buildInjection()).includes('逐字保持原樣'))
})
})
@@ -0,0 +1,175 @@
import { affinityLine } from './status.ts'
import * as aff from './affinity.ts'
import { assemble } from './inject.ts'
import type { Item } from './inject.ts'
import * as mem from './interaction.ts'
import * as mood from './mood.ts'
import { toneRules } from './tone.ts'
import { ZERO } from './mood.ts'
import type { Clock, Fs, Pad } from './types.ts'
export type Card = {
name: string
/** 依重要度由高到低 */
lines: string[]
baseline?: Pad
/** 心情半衰期(小時),預設 8 */
halfLifeHours?: number
}
export type UserConfig = { nickname?: string; tone?: boolean }
type State = { mood: Pad; moodAt: number; affinity: aff.Affinity }
export type Options = {
fs: Fs
clock: Clock
/** 資料根目錄,例如 ~/.companion */
root: string
/** 當前角色資料夾名稱 */
id: string
}
const HOUR = 3_600_000
export function createCompanion({ fs, clock, root, id }: Options) {
const dir = `${root}/${id}`
// 互動記憶與原著記憶分開資料夾
const paths = {
card: `${dir}/card.json`,
state: `${dir}/state.json`,
interaction: `${dir}/interaction/memory.json`,
user: `${root}/user.json`,
}
let turnDelta: Pad = ZERO
let affinityMoved = false
let lastText = ''
async function json<T>(path: string, fallback: T): Promise<T> {
const raw = await fs.read(path)
if (!raw) return fallback
try {
return JSON.parse(raw) as T
} catch {
return fallback
}
}
const loadCard = () => json<Card>(paths.card, { name: id, lines: [] })
const loadUser = () => json<UserConfig>(paths.user, {})
async function loadState(card: Card): Promise<State> {
const now = clock.now()
const s = await json<State | null>(paths.state, null)
if (!s) return { mood: card.baseline ?? ZERO, moodAt: now, affinity: aff.initialAffinity(now) }
const mood0 = mood.decay(s.mood, card.baseline ?? ZERO, now - s.moodAt, (card.halfLifeHours ?? 8) * HOUR)
return { mood: mood0, moodAt: now, affinity: aff.settle(s.affinity, now) }
}
const saveState = (s: State) => fs.write(paths.state, JSON.stringify(s))
async function remember(role: 'user' | 'assistant', text: string, relation: number) {
const m = await mem.load(fs, paths.interaction)
await mem.save(fs, paths.interaction, mem.add(m, { role, text, at: clock.now() }, relation))
}
return {
/** 使用者回合:記錄輸入、更新心情與好感 */
async userTurn(text: string, events: mood.MoodEvent[] = []) {
const card = await loadCard()
const st = await loadState(card)
turnDelta = ZERO
lastText = text
const kw = mood.keywordEvent(text)
const all = kw ? [...events, kw] : events
let m = st.mood
for (const ev of events) ({ mood: m, turn: turnDelta } = mood.applyDelta(m, turnDelta, mood.EVENTS[ev]))
const now = clock.now()
const bad = all.some(e => e === 'scold' || e === 'careIgnored')
const good = all.some(e => e === 'praise' || e === 'success' || e === 'likedTopic')
let a = st.affinity
let rel = 0
if (bad) {
a = aff.negative(a, now)
rel = -aff.LOSS
} else if (text.trim() && (good || text.trim().length >= 4)) {
a = aff.meaningful(a, now)
rel = a.value - st.affinity.value
}
affinityMoved = rel !== 0
await saveState({ mood: m, moodAt: now, affinity: a })
await remember('user', text, rel)
},
/**
* 助理回覆:解析並移除心情標記,缺失或錯誤時沿用使用者回合已套用的關鍵字事件。
* 回傳可顯示的文字。
*/
async assistantReply(reply: string): Promise<string> {
const parsed = mood.parseMoodTag(reply)
const card = await loadCard()
const st = await loadState(card)
const kw = parsed.delta ? null : mood.keywordEvent(lastText)
const delta = parsed.delta ?? (kw ? mood.EVENTS[kw] : null)
if (delta) {
const { mood: m, turn } = mood.applyDelta(st.mood, turnDelta, delta)
turnDelta = turn
await saveState({ ...st, mood: m, moodAt: clock.now() })
}
await remember('assistant', parsed.text, 0)
return parsed.text
},
/** 組裝送出模型請求前要注入 system 區塊尾端的內容 */
async buildInjection(extra: { care?: string[]; lore?: Item[] } = {}): Promise<string> {
const card = await loadCard()
const user = await loadUser()
const st = await loadState(card)
const im = await mem.load(fs, paths.interaction)
const n = im.recent.length
const memory: Item[] = [
...(extra.lore ?? []),
...im.summary.map((t, i) => ({ text: `(摘要)${t}`, score: 0.1 + (0.2 * i) / Math.max(1, im.summary.length) })),
...im.recent.map((t, i) => ({ text: `${t.role === 'user' ? '使用者' : '你'}:${t.text}`, score: 0.5 + (0.5 * (i + 1)) / n })),
]
const m = mood.moodLabel(st.mood)
const tone = user.tone !== false
const cardItems: Item[] = [
{ text: `名字:${card.name}`, score: 1e9 },
...(tone ? toneRules(card.name, user.nickname, aff.STAGE_NAMES[st.affinity.stage]).map((text, i) => ({ text, score: 1e9 - i - 1 })) : []),
...card.lines.map((text, i) => ({ text, score: card.lines.length - i })),
]
return assemble({
card: cardItems,
memory,
mood: [{ text: `${m.label} ${m.face};${affinityLine(st.affinity)}`, score: 1 }],
care: (extra.care ?? []).map((text, i) => ({ text, score: 100 - i })),
})
},
async mood() {
const st = await loadState(await loadCard())
return { pad: st.mood, ...mood.moodLabel(st.mood) }
},
async affinity() {
return (await loadState(await loadCard())).affinity
},
async setTone(on: boolean) {
await fs.write(paths.user, JSON.stringify({ ...(await loadUser()), tone: on }))
},
/** `/companion tone on|off` */
async command(line: string): Promise<string | null> {
const m = /^tone\s+(on|off)$/.exec(line.trim())
if (!m) return null
await this.setTone(m[1] === 'on')
return m[1] === 'on' ? '語氣已開啟' : '語氣已關閉'
},
get affinityMoved() {
return affinityMoved
},
}
}
@@ -0,0 +1,40 @@
import { redactSecrets } from './secrets.ts'
export const BUDGET = { card: 400, mood: 100, memory: 800, care: 200 } as const
export type Section = keyof typeof BUDGET
/** 粗估 token:CJK 一字約一 token,其餘約四字元一 token */
export function estimateTokens(text: string): number {
let cjk = 0
for (const ch of text) if (/[⺀-鿿＀-￯ -〿]/.test(ch)) cjk++
return cjk + Math.ceil((text.length - cjk) / 4)
}
export type Item = { text: string; score: number }
/** 在預算內保留高分項目,維持原順序;超出時先丟低分 */
export function fitBudget(items: Item[], budget: number): string[] {
const kept = new Set(items.map((_, i) => i))
const order = items.map((it, i) => ({ i, score: it.score })).sort((a, b) => a.score - b.score || b.i - a.i)
let total = items.reduce((n, it) => n + estimateTokens(it.text), 0)
for (const { i } of order) {
if (total <= budget) break
total -= estimateTokens(items[i]!.text)
kept.delete(i)
}
return items.filter((_, i) => kept.has(i)).map(it => it.text)
}
export type Sections = Record<Section, Item[]>
/** 穩定內容(角色卡)在前,變動內容(心情、記憶、關心情境)在後 */
export function assemble(s: Sections): string {
const part = (title: string, key: Section) => {
const lines = fitBudget(s[key], BUDGET[key]).map(redactSecrets)
return lines.length ? `## ${title}\n${lines.join('\n')}` : ''
}
const body = [part('角色', 'card'), part('互動記憶', 'memory'), part('目前心情', 'mood'), part('關心情境', 'care')]
.filter(Boolean)
.join('\n\n')
return body ? `<companion>\n${body}\n</companion>` : ''
}
@@ -0,0 +1,63 @@
import { redactSecrets } from './secrets.ts'
import type { Fs } from './types.ts'
export type Turn = { role: 'user' | 'assistant'; text: string; at: number }
export type InteractionMemory = {
recent: Turn[]
/** 壓縮後的摘要行與累計關係變化 */
summary: string[]
relationDelta: number
}
export const RECENT_MAX = 30
export const COMPRESS_COUNT = 20
const SUMMARY_MAX = 40
const LINE_MAX = 60
export const empty = (): InteractionMemory => ({ recent: [], summary: [], relationDelta: 0 })
// 互動記憶不存大段程式碼;只留對話文字
export function dialogueOnly(text: string): string {
return text
.replace(/```[\s\S]*?(?:```|$)/g, ' ')
.replace(/`[^`\n]{40,}`/g, ' ')
.replace(/[ \t]+/g, ' ')
.trim()
}
export function clean(text: string): string {
return redactSecrets(dialogueOnly(text))
}
export function add(m: InteractionMemory, turn: Turn, relationDelta: number): InteractionMemory {
const text = clean(turn.text)
if (!text) return { ...m, relationDelta: m.relationDelta + relationDelta }
let next: InteractionMemory = {
...m,
recent: [...m.recent, { ...turn, text }],
relationDelta: m.relationDelta + relationDelta,
}
if (next.recent.length > RECENT_MAX) next = compress(next)
return next
}
/** 把最舊的逐字紀錄壓縮成摘要行 */
export function compress(m: InteractionMemory): InteractionMemory {
const old = m.recent.slice(0, COMPRESS_COUNT)
const lines = old
.filter(t => t.role === 'user')
.map(t => (t.text.length > LINE_MAX ? t.text.slice(0, LINE_MAX) + '…' : t.text))
return { ...m, recent: m.recent.slice(COMPRESS_COUNT), summary: [...m.summary, ...lines].slice(-SUMMARY_MAX) }
}
export async function load(fs: Fs, path: string): Promise<InteractionMemory> {
const raw = await fs.read(path)
if (!raw) return empty()
try {
return { ...empty(), ...JSON.parse(raw) }
} catch {
return empty()
}
}
export const save = (fs: Fs, path: string, m: InteractionMemory) => fs.write(path, JSON.stringify(m))
@@ -0,0 +1,93 @@
import { clamp } from './types.ts'
import type { Pad } from './types.ts'
export const TURN_CAP = 0.3
export const EVENTS = {
praise: { p: 0.15, a: 0.05, d: 0.05 },
scold: { p: -0.2, a: 0.15, d: -0.1 },
success: { p: 0.08, a: 0.03, d: 0.05 },
toolFailures: { p: -0.08, a: 0.1, d: -0.08 },
returnAfterIdle: { p: 0.1, a: 0.08, d: 0 },
lateNight: { p: -0.05, a: -0.1, d: 0 },
likedTopic: { p: 0.12, a: 0.08, d: 0.03 },
careIgnored: { p: -0.05, a: -0.03, d: -0.03 },
} as const satisfies Record<string, Pad>
export type MoodEvent = keyof typeof EVENTS
export const ZERO: Pad = { p: 0, a: 0, d: 0 }
/** 一回合內累計的變化量;單軸總變化不得超過 ±0.3 */
export function applyDelta(mood: Pad, turn: Pad, delta: Pad): { mood: Pad; turn: Pad } {
const out = { mood: { ...mood }, turn: { ...turn } }
for (const k of ['p', 'a', 'd'] as const) {
const next = clamp(turn[k] + delta[k], -TURN_CAP, TURN_CAP)
const step = next - turn[k]
out.turn[k] = next
out.mood[k] = clamp(mood[k] + step, -1, 1)
}
return out
}
/** 讀取時依經過時間往基線衰減:x = B + (x − B)·0.5^(Δ/τ) */
export function decay(mood: Pad, baseline: Pad, elapsedMs: number, halfLifeMs: number): Pad {
const f = Math.pow(0.5, Math.max(0, elapsedMs) / halfLifeMs)
return {
p: baseline.p + (mood.p - baseline.p) * f,
a: baseline.a + (mood.a - baseline.a) * f,
d: baseline.d + (mood.d - baseline.d) * f,
}
}
const MOODS: Record<string, { label: string; face: string }> = {
'+++': { label: '開心', face: '(≧▽≦)' },
'++-': { label: '安心', face: '(´▽`)' },
'+-+': { label: '放鬆', face: '(´ω`)' },
'+--': { label: '溫柔', face: '(。・ω・。)' },
'-++': { label: '生氣', face: '(`Д´)' },
'-+-': { label: '焦慮', face: '(;´Д`)' },
'--+': { label: '無聊', face: '(¬_¬)' },
'---': { label: '低落', face: '(´;ω;`)' },
}
/** 八分區對應情緒標籤與顏文字 */
export function moodLabel(m: Pad): { label: string; face: string } {
const s = (x: number) => (x >= 0 ? '+' : '-')
return MOODS[s(m.p) + s(m.a) + s(m.d)]!
}
// 心情標記:回覆末尾的隱藏標記,如 <!--mood p=0.1 a=-0.05 d=0-->
const TAG = /<!--\s*mood\b([^>]*?)-->\s*$/
export type ParsedReply = { text: string; delta: Pad | null }
export function parseMoodTag(reply: string): ParsedReply {
const m = TAG.exec(reply)
if (!m) return { text: stripStray(reply), delta: null }
const text = stripStray(reply.slice(0, m.index).trimEnd())
const vals: Partial<Pad> = {}
for (const kv of (m[1] ?? '').trim().split(/\s+/)) {
const mm = /^([pad])=(-?\d+(?:\.\d+)?)$/.exec(kv)
if (mm) vals[mm[1] as keyof Pad] = Number(mm[2])
}
if (vals.p === undefined || vals.a === undefined || vals.d === undefined) return { text, delta: null }
return { text, delta: { p: vals.p, a: vals.a, d: vals.d } }
}
// 格式錯誤的標記也不能露給使用者
function stripStray(s: string): string {
return s.replace(/\s*<!--\s*mood\b[^>]*?(?:-->|$)/g, '').trimEnd()
}
const KEYWORDS: [MoodEvent, RegExp][] = [
['scold', /笨|白痴|廢物|爛|你很煩|閉嘴|stupid|useless|idiot/i],
['praise', /謝謝|感謝|太棒|好棒|厲害|讚|thanks?|thank you|great job|awesome/i],
['success', /成功|完成了|通過|搞定|passed|done|fixed/i],
]
/** 標記缺失或格式錯誤時的後備:用使用者文字的關鍵字對應事件 */
export function keywordEvent(text: string): MoodEvent | null {
for (const [ev, re] of KEYWORDS) if (re.test(text)) return ev
return null
}
@@ -0,0 +1,21 @@
// 疑似金鑰:寫入互動記憶與注入內容前一律過濾
const PATTERNS: RegExp[] = [
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
/\bsk-[A-Za-z0-9_-]{16,}/g,
/\b(?:ghp|gho|ghu|ghs|ghr|glpat|xox[baprs])[-_][A-Za-z0-9_-]{10,}/g,
/\bAKIA[0-9A-Z]{16}\b/g,
/\bBearer\s+[A-Za-z0-9._~+/=-]{16,}/gi,
/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g,
/\b(?:api[_-]?key|secret|token|passwd|password|pwd)\s*[:=]\s*\S{6,}/gi,
/\b[A-Za-z0-9+/_-]{32,}={0,2}(?![A-Za-z0-9])/g,
]
export const REDACTED = '[已遮蔽]'
export function redactSecrets(text: string): string {
return PATTERNS.reduce((s, re) => s.replace(re, REDACTED), text)
}
export function hasSecret(text: string): boolean {
return redactSecrets(text) !== text
}
@@ -0,0 +1,4 @@
import { STAGE_NAMES } from './affinity.ts'
import type { Affinity } from './affinity.ts'
export const affinityLine = (a: Affinity) => `好感:${STAGE_NAMES[a.stage]}(${a.value.toFixed(2)})`
@@ -0,0 +1,10 @@
// 語氣規則:只套用於對話文字與過場話;技術內容原樣保留
export function toneRules(name: string, nickname: string | undefined, stage: string): string[] {
return [
`你以「${name}」的語氣回覆${nickname ? `,稱呼使用者「${nickname}」` : ''}(親密階段:${stage})。`,
'語氣只套用於對話文字與工具呼叫前後的過場話。',
'程式碼、指令、路徑、錯誤訊息、安全警告必須逐字保持原樣,不得改寫或加語氣詞。',
'關鍵技術結論放在最前面或單獨一行。',
'回覆最末附心情標記:<!--mood p=數值 a=數值 d=數值-->(各軸 −0.3 到 0.3,代表本回合心情變化)。',
]
}
@@ -0,0 +1,13 @@
export interface Fs {
read(path: string): Promise<string | null>
/** 原子寫入:寫到暫存檔再改名 */
write(path: string, data: string): Promise<void>
}
export interface Clock {
now(): number
}
export type Pad = { p: number; a: number; d: number }
export const clamp = (x: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, x))
+17
View File
@@ -0,0 +1,17 @@
// 角色行用的字串片段與寬度計算(與 statusbar 外掛各自獨立,不互相依賴)
export type Seg = { text: string; color?: string; dim?: boolean; bold?: boolean }
export function bar(p: number, cells = 5): string {
const n = Math.max(0, Math.min(cells, Math.round((p / 100) * cells)))
return '▰'.repeat(n) + '▱'.repeat(cells - n)
}
const WIDE = /[ᄀ-ᅟ⺀-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]/
export function width(s: string): number {
let w = 0
for (const ch of s) w += WIDE.test(ch) ? 2 : 1
return w
}
export const lineWidth = (segs: Seg[]) => segs.reduce((w, s) => w + width(s.text), 0)
+72
View File
@@ -0,0 +1,72 @@
import type { Clock, Fs } from './io'
import { readJson, writeJson } from './io'
export interface Todo {
id: number
title: string
done: boolean
/** 承諾事項:助理答應記住的事 */
promise: boolean
dueAt?: number
createdAt: number
doneAt?: number
/** 已提醒過,避免重複 */
reminded?: boolean
}
export interface TodoList {
add(title: string, opts?: { promise?: boolean; dueAt?: number }): Promise<Todo>
complete(id: number): Promise<Todo | null>
list(opts?: { all?: boolean }): Promise<Todo[]>
/** 角色行顯示的待辦數(未完成) */
open(): Promise<number>
/** 取出到期且尚未提醒的項目,並標記為已提醒 */
due(): Promise<Todo[]>
}
export function createTodos(fs: Fs, clock: Clock, path: string): TodoList {
const load = () => readJson<Todo[]>(fs, path, [])
const save = (t: Todo[]) => writeJson(fs, path, t)
return {
async add(title, opts = {}) {
const all = await load()
const t: Todo = {
id: all.reduce((m, x) => Math.max(m, x.id), 0) + 1,
title: title.trim(),
done: false,
promise: !!opts.promise,
dueAt: opts.dueAt,
createdAt: clock(),
}
all.push(t)
await save(all)
return t
},
async complete(id) {
const all = await load()
const t = all.find(x => x.id === id)
if (!t || t.done) return t ?? null
t.done = true
t.doneAt = clock()
await save(all)
return t
},
async list(opts = {}) {
const all = await load()
return opts.all ? all : all.filter(t => !t.done)
},
async open() {
return (await load()).filter(t => !t.done).length
},
async due() {
const all = await load()
const now = clock()
const hit = all.filter(t => !t.done && !t.reminded && t.dueAt !== undefined && t.dueAt <= now)
if (hit.length) {
for (const t of hit) t.reminded = true
await save(all)
}
return hit
},
}
}
@@ -0,0 +1,46 @@
export type Pad = { p: number; a: number; d: number }
export type Source = 'novel' | 'official' | 'wiki'
export type Fact = { value: string; source?: string; kind?: Source; confidence?: number }
export const FACT_KEYS = ['birthday', 'zodiac', 'bloodType', 'hair', 'face', 'height', 'weight', 'measurements'] as const
export type FactKey = (typeof FACT_KEYS)[number]
export type CompanionCore = {
id: string
name: string
facts: Record<FactKey, Fact | 'unknown'>
phrases?: string[]
// 神經質 0~100,影響心情半衰期;個性推導屬於後續里程碑,先用預設值
neuroticism: number
baseline: Pad
}
export const STAGES = ['陌生', '熟悉', '親近', '特別'] as const
export type Stage = (typeof STAGES)[number]
export type Affinity = {
value: number
stage: number
dayKey: string
dayGain: number
lastSeen: number
}
export type CompanionState = {
mood: Pad & { at: number }
affinity: Affinity
todos: number
}
export type UserSettings = {
nickname: string
wake: string
sleep: string
tz: string
careLimit: number
quiet: { start: string; end: string }
}
export type Global = { active: string | null; activatedOnce: boolean }
+1
View File
@@ -0,0 +1 @@
{ "modules": ["./register.tsx"] }
+49
View File
@@ -0,0 +1,49 @@
import type { Register } from 'claude-code'
import { fitCompanion } from './companion/band'
import { createCompanion } from './companion/core'
import { nodeFs } from './companion/node-fs'
let companion: ReturnType<typeof createCompanion> | undefined
export const register: Register = on => {
on('session.start', async ($, e, next) => {
await $.command.register({
name: 'companion',
description: '角色陪伴控制:狀態、切換角色、關閉、使用者設定',
argumentHint: 'status | switch <角色> | off | setup',
})
const home = await $.env.get('HOME')
companion = createCompanion({ fs: nodeFs, clock: { now: Date.now }, root: `${home ?? ''}/.companion` })
const first = await companion.ensureActivated()
if (first.first) $.ui.toast(`已啟用 ${first.name}。用 /companion off 可關閉`)
return next(e)
})
on('command.run', { command: 'companion' }, async ($, e) => ({
text: companion ? await companion.command(e.args) : '尚未就緒。',
}))
on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
const rest = await next(e)
const view = await companion?.view()
if (e.props.hasSurvey || !view) return rest
const line = fitCompanion(view, e.props.bodyColumns)
const { Box, Text } = $.ui.resolve(e)
return (
<Box flexDirection="column">
{rest}
<Text wrap="truncate">
{line.map(s => (
<Text color={s.color} dimColor={s.dim} bold={s.bold}>
{s.text}
</Text>
))}
</Text>
</Box>
)
})
}