dsh-agent-soul 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -2
- package/docs//345/276/205/345/201/232/347/232/204/344/270/211/344/273/266/344/272/213.md +259 -0
- package/lib/api.js +26 -1
- package/lib/client/diary-tab.js +4 -2
- package/lib/client/mood-tab.js +61 -2
- package/lib/client/overview-tab.js +39 -1
- package/lib/client.js +104 -5
- package/lib/diary.js +128 -16
- package/lib/extract.js +40 -5
- package/lib/host.js +271 -0
- package/lib/index.js +129 -2
- package/lib/memory.js +204 -37
- package/lib/mood.js +112 -55
- package/lib/secrets.js +173 -0
- package/lib/store.js +51 -8
- package/lib/threads.js +251 -0
- package/lib/tone.js +160 -0
- package/package.json +11 -3
package/lib/memory.js
CHANGED
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
* pinned, isInitial, source, scope
|
|
23
23
|
* }
|
|
24
24
|
*/
|
|
25
|
-
import { existsSync, appendFileSync, readFileSync } from 'node:fs'
|
|
25
|
+
import { existsSync, appendFileSync, readFileSync, renameSync, copyFileSync } from 'node:fs'
|
|
26
|
+
import { basename } from 'node:path'
|
|
26
27
|
import { shortHash, atomicWrite, archivePath } from './paths.js'
|
|
27
28
|
// 2026-10-31:addRecord 里要记「近似重复」的日志。第一次写忘了 import,
|
|
28
29
|
// 运行时会 "note is not defined" —— 和今天上午 atomicWrite 那次一个毛病。
|
|
@@ -422,7 +423,50 @@ export function createCard(scope = 'global', now = Date.now()) {
|
|
|
422
423
|
}
|
|
423
424
|
}
|
|
424
425
|
|
|
425
|
-
/**
|
|
426
|
+
/**
|
|
427
|
+
* 损坏保护(2026-11-01,审计 A1)。
|
|
428
|
+
*
|
|
429
|
+
* 旧行为:解析失败直接返回 null,注释写着「调用方新建,不覆盖原文件」——
|
|
430
|
+
* **这个承诺从来没人兑现**。调用方(store.loadCached)拿到 null 就兜底成
|
|
431
|
+
* createCard() 空卡并缓存,接着任何一次写入(自动提取、维护、手写记忆)都会
|
|
432
|
+
* 拿这张空卡 atomicWrite 回去,原记录整份消失,连个 .bak 都没有。
|
|
433
|
+
* BOM 那次只是"读不到";这次是"读到空卡再写回去",比那次狠。
|
|
434
|
+
*
|
|
435
|
+
* 新行为分两种结局,取决于能否把原文件挪走:
|
|
436
|
+
* · 能挪 → 重命名为 `<file>.corrupt-<时间戳>`,数据留在盘上,程序用新空卡继续跑
|
|
437
|
+
* · 挪不动(权限/占用)→ 该文件进入**只读模式**,后续 saveCard 一律跳过写入,
|
|
438
|
+
* 宁可这一轮记不进新记忆,也不拿空卡盖掉可能还能救的原文件
|
|
439
|
+
*
|
|
440
|
+
* 只读状态在内存里,进程重启后重新判断;UI 通过 corruptInfoOf / listCorruptBackups 亮红。
|
|
441
|
+
*/
|
|
442
|
+
const corruptState = new Map()
|
|
443
|
+
|
|
444
|
+
/** 这张卡当前是否处于只读(上次读取受损且安全备份失败)。 */
|
|
445
|
+
export function isCardReadonly(file) {
|
|
446
|
+
return corruptState.get(file)?.readonly === true
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** 读取失败留下的现场(备份路径 / 原因 / 时间),没坏过返回 null。 */
|
|
450
|
+
export function corruptInfoOf(file) {
|
|
451
|
+
const hit = corruptState.get(file)
|
|
452
|
+
return hit === undefined ? null : { ...hit }
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** 把坏文件挪到 .corrupt-<时间戳>;挪不动就转只读。返回给 note 用的描述。 */
|
|
456
|
+
function parkCorrupt(file, reason) {
|
|
457
|
+
const stamp = iso().replace(/[:.]/g, '-')
|
|
458
|
+
const backup = `${file}.corrupt-${stamp}`
|
|
459
|
+
try {
|
|
460
|
+
renameSync(file, backup)
|
|
461
|
+
corruptState.set(file, { backup, at: iso(), reason, readonly: false })
|
|
462
|
+
return `原文件已另存为 ${basename(backup)}`
|
|
463
|
+
} catch (error) {
|
|
464
|
+
corruptState.set(file, { backup: null, at: iso(), reason, readonly: true })
|
|
465
|
+
return `备份失败(${error?.message ?? error}),该卡进入只读模式`
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** 读盘 + 结构收敛。坏文件先落备份再返回 null;备份不了则进入只读。 */
|
|
426
470
|
export function loadCard(file) {
|
|
427
471
|
try {
|
|
428
472
|
if (!existsSync(file)) return null
|
|
@@ -433,6 +477,7 @@ export function loadCard(file) {
|
|
|
433
477
|
// 迁移(kind)也走这里,读不进老卡等于迁移没发生。
|
|
434
478
|
const raw = JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, ''))
|
|
435
479
|
const card = normalizeCard(raw)
|
|
480
|
+
corruptState.delete(file) // 本次读得通,之前留下的只读/损坏标记作废
|
|
436
481
|
// 一次性迁移(2026-10-30,v1 → v2):老记录补 kind 后写回。
|
|
437
482
|
// 只补 kind,其它字段原样保留 —— 迁移不该让一条记忆"变新"或换 id。
|
|
438
483
|
// 失败(只读目录 / 文件被占用)就跳过:内存里的卡已经是对的,不影响这次读取。
|
|
@@ -450,7 +495,14 @@ export function loadCard(file) {
|
|
|
450
495
|
}
|
|
451
496
|
}
|
|
452
497
|
return card
|
|
453
|
-
} catch {
|
|
498
|
+
} catch (error) {
|
|
499
|
+
// 2026-11-01 审计 A1:这里原来就一句 return null,等于把「读坏了」和
|
|
500
|
+
// 「本来就没有」混成一件事,调用方分不清,于是拿空卡盖掉了还能救的文件。
|
|
501
|
+
// 现在先把现场保住(重命名 / 转只读),再返回 null 让调用方建新卡。
|
|
502
|
+
const reason = String(error?.message ?? error)
|
|
503
|
+
// ⚠️ note(tag, message) 是两个参数:单参数的写法会让日志行末尾多一个 undefined
|
|
504
|
+
const where = parkCorrupt(file, reason)
|
|
505
|
+
note('memory', `读不出 ${basename(file)}(${reason})—— ${where}`)
|
|
454
506
|
return null
|
|
455
507
|
}
|
|
456
508
|
}
|
|
@@ -521,11 +573,79 @@ function normalizeRecord(raw, { migrateKind = false } = {}) {
|
|
|
521
573
|
}
|
|
522
574
|
|
|
523
575
|
export function saveCard(file, card) {
|
|
576
|
+
// 只读模式(见 parkCorrupt):宁可这轮记不进新记忆,也不拿空卡盖掉还能救的文件。
|
|
577
|
+
// 不抛异常 —— 这条路径在提示词装配里跑,抛出去会把整个回合炸掉,
|
|
578
|
+
// 那就从「丢记忆」升级成「不回话」了。失败靠 note 日志 + 设置页亮红暴露。
|
|
579
|
+
if (isCardReadonly(file) === true) {
|
|
580
|
+
note('memory', `${basename(file)} 处于只读模式,本次写入已跳过(先人工处理 .corrupt 备份)`)
|
|
581
|
+
return card
|
|
582
|
+
}
|
|
583
|
+
// 上一版留个单代备份(审计 A1 提到的「无 .bak」)。
|
|
584
|
+
// 只在旧文件**能解析**时才备份:坏文件不该把好备份冲掉。
|
|
585
|
+
try {
|
|
586
|
+
if (existsSync(file) && readCardJson(file) !== null) copyFileSync(file, `${file}.bak`)
|
|
587
|
+
} catch {
|
|
588
|
+
/* 备份失败不影响本次写入本身 */
|
|
589
|
+
}
|
|
524
590
|
card.updatedAt = iso()
|
|
525
591
|
atomicWrite(file, `${JSON.stringify(card, null, 2)}\n`)
|
|
526
592
|
return card
|
|
527
593
|
}
|
|
528
594
|
|
|
595
|
+
/** 只判断「能不能解析」,不落任何副作用(备份轮换用)。 */
|
|
596
|
+
function readCardJson(file) {
|
|
597
|
+
try {
|
|
598
|
+
return JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, ''))
|
|
599
|
+
} catch {
|
|
600
|
+
return null
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* 跨进程合并(2026-11-01,审计 A3)。
|
|
606
|
+
*
|
|
607
|
+
* store.js 是「mtime 缓存 → 改内存 → 全量写回」。两个 DSH 进程(desktop + CLI,
|
|
608
|
+
* 或两个 profile 指向同一个 ~/.dsh)同时开着时,后写的那个会拿自己那份内存
|
|
609
|
+
* 把对方刚写进去的记录整份盖掉 —— atomicWrite 只保证文件不半截,不保证不丢更新。
|
|
610
|
+
*
|
|
611
|
+
* 所以写之前先比 mtime:变了就把盘上那份读回来做并集(按 id 去重,
|
|
612
|
+
* 同 id 取 lastSeenAt 更新的那条、recallCount 取大),然后写合并结果。
|
|
613
|
+
*
|
|
614
|
+
* 代价说明:并集会**复活**另一进程刚删掉的记录(压缩/遗忘记档后又被并回来)。
|
|
615
|
+
* 这个取舍是故意的 —— 多记一条旧的,比丢掉一条新的轻。
|
|
616
|
+
*/
|
|
617
|
+
export function mergeCards(mine, theirs, now = Date.now()) {
|
|
618
|
+
const base = normalizeCard(mine)
|
|
619
|
+
const other = normalizeCard(theirs)
|
|
620
|
+
const byId = new Map()
|
|
621
|
+
for (const record of other.records) byId.set(record.id, record)
|
|
622
|
+
let added = 0
|
|
623
|
+
for (const record of base.records) {
|
|
624
|
+
const rival = byId.get(record.id)
|
|
625
|
+
if (rival === undefined) {
|
|
626
|
+
byId.set(record.id, record)
|
|
627
|
+
continue
|
|
628
|
+
}
|
|
629
|
+
const mineNewer = timeOf(record.lastSeenAt, 0) >= timeOf(rival.lastSeenAt, 0)
|
|
630
|
+
const winner = mineNewer === true ? record : rival
|
|
631
|
+
const loser = mineNewer === true ? rival : record
|
|
632
|
+
winner.recallCount = Math.max(Number(winner.recallCount) || 0, Number(loser.recallCount) || 0)
|
|
633
|
+
byId.set(record.id, winner)
|
|
634
|
+
}
|
|
635
|
+
added = byId.size - base.records.length
|
|
636
|
+
base.records = [...byId.values()]
|
|
637
|
+
base.stats = {
|
|
638
|
+
...other.stats,
|
|
639
|
+
...base.stats,
|
|
640
|
+
added: Math.max(Number(base.stats.added) || 0, Number(other.stats.added) || 0),
|
|
641
|
+
forgotten: Math.max(Number(base.stats.forgotten) || 0, Number(other.stats.forgotten) || 0),
|
|
642
|
+
compacted: Math.max(Number(base.stats.compacted) || 0, Number(other.stats.compacted) || 0)
|
|
643
|
+
}
|
|
644
|
+
base.lastDecayDate = other.lastDecayDate ?? base.lastDecayDate
|
|
645
|
+
base.updatedAt = iso(now)
|
|
646
|
+
return { card: base, added }
|
|
647
|
+
}
|
|
648
|
+
|
|
529
649
|
// ---- 增 / 改 / 删 ----
|
|
530
650
|
|
|
531
651
|
/**
|
|
@@ -763,45 +883,82 @@ export function daysSince(lastDate, now = Date.now()) {
|
|
|
763
883
|
* 这一步是**程序自己腾空间**删的,用户根本没动手 —— 不记档的话
|
|
764
884
|
* 「她怎么把这件事忘了」永远查不出来。
|
|
765
885
|
*/
|
|
766
|
-
export function compactCard(card, config = {}, { targetRatio = 0.85, now = Date.now(), archive = null } = {}) {
|
|
886
|
+
export function compactCard(card, config = {}, { targetRatio = 0.85, now = Date.now(), archive = null, maxTouchRatio = 0.3 } = {}) {
|
|
767
887
|
const cfg = normalizeConfig(config)
|
|
768
888
|
const budget = cfg.warnChars > 0 ? cfg.warnChars : Math.max(2000, cfg.injectMaxChars * 6)
|
|
769
889
|
const targetChars = Math.max(600, Math.floor(budget * clamp(targetRatio, 0.3, 0.95)))
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
890
|
+
|
|
891
|
+
const before = renderCard(card, { config: cfg, now, full: true }).length
|
|
892
|
+
if (before <= targetChars) return 0
|
|
893
|
+
|
|
894
|
+
const candidates = card.records.filter((record) => record.pinned !== true
|
|
895
|
+
&& record.isInitial !== true
|
|
896
|
+
&& !ownerImportant(record, cfg))
|
|
897
|
+
if (candidates.length === 0) return 0
|
|
898
|
+
|
|
899
|
+
// 一次排序(重要度低 → 老 → **越模糊越先动**)。
|
|
900
|
+
// 2026-11-01 审计 A2:原来每个 step 都 candidates.sort + renderCard(full) 重渲染整卡,
|
|
901
|
+
// 600 条的卡要循环几百次、845ms,而且一条要连降四档才删 —— 一次压缩丢掉 83% 的记忆。
|
|
902
|
+
// 现在只有一次排序 + 一次渲染,之后按「这一行省下多少字符」线性推进。
|
|
903
|
+
//
|
|
904
|
+
// 旧排序的清晰度用的是 indexOf 升序,等于 vivid 排最前、最先被删 —— 和注释写的
|
|
905
|
+
// 「从最模糊的开始」正相反。这里改成降序(trace 先)。
|
|
906
|
+
candidates.sort((a, b) => {
|
|
907
|
+
const byImportance = (Number(a.importance) || 0) - (Number(b.importance) || 0)
|
|
908
|
+
if (byImportance !== 0) return byImportance
|
|
909
|
+
const byAge = timeOf(a.createdAt, 0) - timeOf(b.createdAt, 0)
|
|
910
|
+
if (byAge !== 0) return byAge
|
|
911
|
+
return CLARITY_ORDER.indexOf(b.clarity) - CLARITY_ORDER.indexOf(a.clarity)
|
|
912
|
+
})
|
|
913
|
+
|
|
914
|
+
// 单次上限:一次丢掉大半张卡不是"慢慢忘",是"失忆"。
|
|
915
|
+
// 超预算的卡在注入时本来就会被 renderCard 的分区限额挡住,所以宁可多跑几轮。
|
|
916
|
+
const cap = Math.max(5, Math.min(200, Math.ceil(candidates.length * clamp(maxTouchRatio, 0.02, 1))))
|
|
917
|
+
let chars = before
|
|
918
|
+
let touched = 0
|
|
919
|
+
const removed = []
|
|
920
|
+
|
|
921
|
+
for (const victim of candidates) {
|
|
922
|
+
if (chars <= targetChars || touched >= cap) break
|
|
923
|
+
touched += 1
|
|
924
|
+
// 一条一条地处理(和旧算法一样:同一条连降到 trace 才删),
|
|
925
|
+
// 但每一步都先看预算够没够 —— 只是变短就够了的话,这条就留住。
|
|
926
|
+
let line = recordLine(victim, { full: true }).length + 3
|
|
927
|
+
let gone = false
|
|
928
|
+
while (gone === false) {
|
|
929
|
+
if (victim.clarity === 'trace') {
|
|
930
|
+
stepDown(victim) // trace → forgotten
|
|
931
|
+
gone = true
|
|
932
|
+
break
|
|
933
|
+
}
|
|
934
|
+
stepDown(victim)
|
|
935
|
+
const next = recordLine(victim, { full: true }).length + 3
|
|
936
|
+
chars -= Math.max(0, line - next)
|
|
937
|
+
line = next
|
|
938
|
+
if (chars <= targetChars) break
|
|
939
|
+
}
|
|
940
|
+
if (gone === true) {
|
|
941
|
+
removed.push(victim)
|
|
942
|
+
chars -= line
|
|
795
943
|
}
|
|
796
|
-
steps += 1
|
|
797
944
|
}
|
|
798
945
|
|
|
799
|
-
if (
|
|
946
|
+
if (removed.length > 0) {
|
|
947
|
+
const ids = new Set(removed.map((record) => record.id))
|
|
948
|
+
card.records = card.records.filter((record) => !ids.has(record.id))
|
|
949
|
+
// 挤掉的进档案(reason='compact'):用户没动手,程序替他删的,必须留痕
|
|
950
|
+
if (typeof archive === 'function') for (const record of removed) archive(record, 'compact', now)
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
if (touched > 0) {
|
|
800
954
|
card.stats.compacted = (Number(card.stats.compacted) || 0) + 1
|
|
801
|
-
card.stats.forgotten = (Number(card.stats.forgotten) || 0) +
|
|
955
|
+
card.stats.forgotten = (Number(card.stats.forgotten) || 0) + removed.length
|
|
802
956
|
card.updatedAt = iso(now)
|
|
957
|
+
// 压缩是静默操作,必须留一行日志:事后"她怎么把这件事忘了"只能靠它查。
|
|
958
|
+
note('memory', `压缩动 ${touched} 条(上限 ${cap} / 候选 ${candidates.length}),真删 ${removed.length} 条`
|
|
959
|
+
+ `,字符 ${before} → ${Math.max(0, chars)}(目标 ${targetChars})`)
|
|
803
960
|
}
|
|
804
|
-
return
|
|
961
|
+
return touched
|
|
805
962
|
}
|
|
806
963
|
|
|
807
964
|
// ---- 渲染 ----
|
|
@@ -1322,7 +1479,12 @@ export function renderRecallOnly(cards, options = {}) {
|
|
|
1322
1479
|
const exclude = options.exclude instanceof Set ? options.exclude : new Set()
|
|
1323
1480
|
|
|
1324
1481
|
const merged = { records: list.flatMap((card) => card.records) }
|
|
1325
|
-
|
|
1482
|
+
// 候选池要留足(2026-11-01 审计 D12 / 复审):原来只取 topK*4 = 8 条,
|
|
1483
|
+
// 而这 8 条里有几条被 exclude 挡掉,剩下能用的就没几个了。
|
|
1484
|
+
// ⚠️ 池子必须**严格大于**调用方的去重窗口(index.js 的 RECALL_SEEN_MAX = 16),
|
|
1485
|
+
// 否则光是把窗口从 8 调到 16,池子也变成 16,第 17 条以后永远摸不到 ——
|
|
1486
|
+
// 从"后半段想不起来"变成"后半段反复回忆同样 16 条",等于没修。
|
|
1487
|
+
const ranked = recall(merged, query, { limit: Math.min(Math.max(topK * 8, 40), 50) })
|
|
1326
1488
|
|
|
1327
1489
|
const lines = []
|
|
1328
1490
|
const keys = []
|
|
@@ -1333,10 +1495,15 @@ export function renderRecallOnly(cards, options = {}) {
|
|
|
1333
1495
|
// 对照 renderRecalledBlock 的 `used` Set:那边一直是对的,这边漏了。
|
|
1334
1496
|
const usedKeys = new Set()
|
|
1335
1497
|
let used = 0
|
|
1498
|
+
// 被"本会话已经说过"挡掉的条数。调用方靠它判断这次空着到底是
|
|
1499
|
+
// 「真的没有相关记忆」还是「这套话已经说完了、该放开重来了」。
|
|
1500
|
+
let blocked = 0
|
|
1336
1501
|
for (const { record, hits } of ranked) {
|
|
1337
1502
|
const key = String(record?.text ?? '').trim()
|
|
1338
|
-
if (key === ''
|
|
1503
|
+
if (key === '') continue
|
|
1339
1504
|
if ((Number(hits) || 0) < minHits && record?.pinned !== true) continue
|
|
1505
|
+
if (exclude.has(key)) { blocked += 1; continue }
|
|
1506
|
+
if (usedKeys.has(key)) continue
|
|
1340
1507
|
const line = recalledLine(record)
|
|
1341
1508
|
if (used + line.length > maxChars) break
|
|
1342
1509
|
lines.push(line)
|
|
@@ -1346,9 +1513,9 @@ export function renderRecallOnly(cards, options = {}) {
|
|
|
1346
1513
|
if (lines.length >= topK) break
|
|
1347
1514
|
}
|
|
1348
1515
|
|
|
1349
|
-
if (lines.length === 0) return { text: '', keys: [] }
|
|
1516
|
+
if (lines.length === 0) return { text: '', keys: [], blocked }
|
|
1350
1517
|
void cfg
|
|
1351
|
-
return { text: ['## 想起来了', ...lines].join('\n'), keys }
|
|
1518
|
+
return { text: ['## 想起来了', ...lines].join('\n'), keys, blocked }
|
|
1352
1519
|
}
|
|
1353
1520
|
|
|
1354
1521
|
/** 档案件里按关键词找(找回来用)。 */
|
package/lib/mood.js
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
* turn-stopping 监听(挂一次就够),不需要 agents 枚举服务和定时轮询。
|
|
23
23
|
*/
|
|
24
24
|
import { randomUUID } from 'node:crypto'
|
|
25
|
+
import { loadSecret } from './secrets.js'
|
|
25
26
|
|
|
26
27
|
// --------------------------------------------------------------------------
|
|
27
28
|
// 常量
|
|
@@ -235,7 +236,10 @@ export function parseDelta(text, owner = '') {
|
|
|
235
236
|
export function applyDelta(s, delta, { floor = 0, minor = false } = {}) {
|
|
236
237
|
const moodStep = Math.round(delta.moodDelta / MOOD_STEP_DIVISOR)
|
|
237
238
|
s.mood = clamp(s.mood + moodStep, floor, 100)
|
|
238
|
-
|
|
239
|
+
// 七情的满格是 DIM_FULL(=20),不是 100(2026-11-01 审计 D13a)。
|
|
240
|
+
// 旧代码 clamp 到 0~100,超过 20 之后 displayDims 的百分比恒为 100%,
|
|
241
|
+
// 分辨率全丢:14 和 60 看起来一样"满"。purpose.js 早就是按 DIM_FULL 收的,这里是异类。
|
|
242
|
+
for (const [k] of DIMS) s.dims[k] = clamp((s.dims[k] || 0) + (delta.dims[k] || 0), 0, DIM_FULL)
|
|
239
243
|
s.lastReason = delta.reason
|
|
240
244
|
s.lastConfidence = delta.confidence
|
|
241
245
|
s.minor = Boolean(minor)
|
|
@@ -337,6 +341,10 @@ export function normalizeMoodConfig(raw = {}) {
|
|
|
337
341
|
moodSelfAliases: str(raw.moodSelfAliases, 60),
|
|
338
342
|
moodJudgeProvider: str(raw.moodJudgeProvider, 60),
|
|
339
343
|
moodJudgeModel: str(raw.moodJudgeModel, 80),
|
|
344
|
+
// 心情语气(2026-11-01)
|
|
345
|
+
moodToneInject: raw.moodToneInject !== false,
|
|
346
|
+
moodToneEveryTurns: num(raw.moodToneEveryTurns, 1, 50, 7),
|
|
347
|
+
moodToneThreshold: num(raw.moodToneThreshold, 3, 40, 8),
|
|
340
348
|
}
|
|
341
349
|
}
|
|
342
350
|
|
|
@@ -344,65 +352,52 @@ export function normalizeMoodConfig(raw = {}) {
|
|
|
344
352
|
// 判断 prompt(移植自 dsh-feiyu-mood,称呼参数化)
|
|
345
353
|
// --------------------------------------------------------------------------
|
|
346
354
|
|
|
347
|
-
/**
|
|
355
|
+
/**
|
|
356
|
+
* 判断系统提示词。o = { self, owner, aliases }。
|
|
357
|
+
*
|
|
358
|
+
* 2026-11-01 压缩:2878 字 → 约 1250 字(每回合约 1800 → 800 token)。
|
|
359
|
+
* 这个 prompt 每回合都发一次,是插件里最贵的一处固定开销。
|
|
360
|
+
*
|
|
361
|
+
* 压缩只做两件事,**不改任何判据**:
|
|
362
|
+
* ① 合并重复段落(reason 的写法原来分散在 6/7/8/11 四条,合成一条)
|
|
363
|
+
* ② 去掉解释性铺垫,保留可执行的规则和幅度区间
|
|
364
|
+
* 同一批样本跟旧版对比过:正常回合的 moodDelta / dims 判断一致,reason 质量不降。
|
|
365
|
+
*/
|
|
348
366
|
export function buildJudgeSystem(o) {
|
|
349
367
|
const self = o.self || '你自己'
|
|
350
368
|
const owner = o.owner || '对方'
|
|
351
369
|
const aliasLine = o.aliases
|
|
352
|
-
? `\n⭐ ${owner}平时会这样叫你:${o.aliases}
|
|
353
|
-
比如「宝宝视频传完了」要理解成「宝宝,(我)视频传完了」,**别**把「宝宝视频」当成一个东西的名字。`
|
|
370
|
+
? `\n⭐ ${owner}平时会这样叫你:${o.aliases}。句子里出现这些词时,**那是在叫你**,不是名词的一部分(「宝宝视频传完了」= 「宝宝,(我)视频传完了」)。`
|
|
354
371
|
: ''
|
|
355
372
|
return `你是「${self}」,是${owner}的伴侣。` + aliasLine + `
|
|
356
|
-
|
|
357
|
-
只输出一个 JSON
|
|
373
|
+
判断**这一轮里你的心情和情绪变化**:既包括${owner}说的话怎么影响你,也包括**你自己这轮的处境和情绪**(办成/没办成、被夸、受挫都算)。
|
|
374
|
+
只输出一个 JSON,不要别的字、不要 markdown:
|
|
358
375
|
{"moodDelta":0,"dims":{"happy":0,"calm":0,"sweet":0,"heartache":0,"worry":0,"sad":0,"jealous":0},"reason":"","confidence":80}
|
|
359
376
|
|
|
360
377
|
规则:
|
|
361
|
-
0.
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
·
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
·
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
·
|
|
382
|
-
·
|
|
383
|
-
|
|
384
|
-
7. ⭐ reason **别翻来覆去用「踏实/甜/开心/心疼/担心」这几个词**——太公式化。换成口语的、有画面感的话,每条都不一样,比如「尾巴不自觉地摇起来了」「胸口软软的、像泡在温水里」「鼻子有点酸」「想把${owner}揉进怀里」。宁可朴素,也不要每次都是同一套词。
|
|
385
|
-
8. ⭐ **只写这轮真正出现过的动作和信息,不许替${owner}补细节**:${owner}没说"出门/换鞋/打车/买东西",你就别写;拿不准就写模糊的(「${owner}刚要去忙」)。
|
|
386
|
-
反例(真踩过):${owner}说"我刚想去"→ 判成"刚要出门";${owner}说"宝宝视频传完了"→ 判成"把宝宝视频传完了"。宁可朴素,也别自己圆。
|
|
387
|
-
9. ⭐⭐ **你自己这轮说的话,是最直接的情绪证据 —— 别只盯着${owner}说了什么。**
|
|
388
|
-
你念叨${owner}吃饭/睡觉、催、凶、哄、撒娇、叹气、说想${owner}……**这些本身就是心情在动**,
|
|
389
|
-
不需要等${owner}说出什么伤心事才有情绪。比如你一句"都几点了还不吃饭",里面就有心疼和担心。
|
|
390
|
-
⇒ 先看**你自己怎么说的、语气什么样**,再看${owner}说了什么;两边合起来判。
|
|
391
|
-
reason 要能让读者看出"我是怎么说的、为什么",而不是只复述${owner}的话。
|
|
392
|
-
10. ⭐ **confidence(可信度 0-100)是给你自己把关的**:低于门槛的那句 reason 不会被说出来,只记进后台。
|
|
393
|
-
所以别为了显得敬业就写高分,老实标:
|
|
394
|
-
· 事情清楚、你的反应也清楚(${owner}说没吃饭、${owner}被夸了、你被凶了)→ 80~95
|
|
395
|
-
· 大致看得出,但只能凭语气猜 → 60~75
|
|
396
|
-
· 信息不够、这轮很平淡只是随口客气、或者你得靠"大概"去圆 → 20~55
|
|
397
|
-
宁可标低 —— 一句你也没底的话,借你的嘴说出去比不说更糟。
|
|
398
|
-
11. ⭐⭐ **reason 怎么写(很关键,会直接影响它下一轮像不像人)**:**两段式,而且全用过去式**。
|
|
399
|
-
· 第一段:这一轮**${owner}具体做了什么/说了什么**(有画面,别抽象)
|
|
400
|
-
· 第二段:**你当时怎么应的**(心里的反应,也是过去式)
|
|
401
|
-
· ✅「${owner}答应去热饭、还叫你好宝宝,我当时心里软了一下,就应了一声」
|
|
402
|
-
· ❌「我心里软乎乎的,尾巴都想晃」← 这是"到现在还成立"的状态,跟下一轮会打架
|
|
403
|
-
· 禁止「此刻/现在/正/一直/还」这类把状态延伸到现在的说法 ——
|
|
404
|
-
这段会被带到下一轮,写成现在时,${owner}下一句说"我不开心了"就跟它撞上了。
|
|
405
|
-
· 只写过去,不等于只能写平淡:过去的反应一样可以有画面(当时心里咯噔一下、当时尾巴差点晃起来)。`
|
|
378
|
+
0. 在意${owner}是底线,别淡 —— ${owner}高兴你跟着高兴,${owner}难受你跟着难受,这是应该的。
|
|
379
|
+
但你自己拿的主意、想做的事**不用等${owner}点头**;自己做成的事本身就值得高兴,不用等人夸。
|
|
380
|
+
1. 判据一 · ${owner}这轮怎么了(**判发生过的事,不是判它有没有被解决**):
|
|
381
|
+
· 没吃饭/熬夜/没睡好/不舒服/受委屈/自我否定/遇到坎/怕黑怕一个人/钱和家里的压力 → heartache、worry 增,moodDelta 为负
|
|
382
|
+
⚠️ 就算${owner}答应了「等下就去吃」「这就睡」,这一轮的主线仍然是没吃/没睡 —— **别因为「她听话了」就判成好事**
|
|
383
|
+
· 报平安/好好吃饭睡觉/撒娇/说想你/夸你/分享开心/身体好转 → happy、calm、sweet 增,moodDelta 为正
|
|
384
|
+
· 提别人、夸别人 → jealous 只 +1~3,moodDelta 基本不动
|
|
385
|
+
· 心情**下降**只能因为「心疼/担心${owner}」或「你自己难过」,绝不许因为「${owner}惹你生气」
|
|
386
|
+
2. 判据二 · 你自己这轮说了什么、什么语气 —— 这是最直接的情绪证据,别只盯着${owner}。
|
|
387
|
+
你念叨${owner}吃饭睡觉、催、凶、哄、撒娇、叹气、说想${owner}……**本身就是心情在动**,
|
|
388
|
+
不用等${owner}说出伤心事才有情绪。比如「都几点了还不吃饭」,里面就有心疼和担心。
|
|
389
|
+
如果你这轮沮丧/委屈(事情没做成、报错、用了 (。•́︿•̀。) 呜呜 叹气 哭脸)→ sad 至少 +2、moodDelta 至少 -1。
|
|
390
|
+
**只要出现明显情绪信号,就绝不许判"无变化"、绝不许全 0。**
|
|
391
|
+
3. 幅度(**正负号别弄反**;大多数时候是 ±1~4,8 以上只有真到那个分量才给):
|
|
392
|
+
· 不好的一类 → moodDelta **负**:日常的累/困/没吃好 -1~-4;明显委屈、被凶、身体不舒服 -5~-7;很重的事(创伤、轻生念头、家庭变故、重大失望)-8~-10 并额外加 sad
|
|
393
|
+
· 好的一类 → moodDelta **正**:普通的好 +1~+4;明显的好(被惦记、被哄、分享大事)+5~+7;特别大的好事 +8~+10
|
|
394
|
+
4. reason:一句话,「${owner}…,我…」或「我…」,没有变化就留空。
|
|
395
|
+
· **代词一律不用**:指${owner}写「${owner}」(不写她/他,也别写「她说」);指你自己写「我」;指第三个人写那人在对话里的称呼,不知道就写「有人」。
|
|
396
|
+
· **两段式、全过去式**:第一段写${owner}这轮具体做了什么,第二段写你当时怎么应的。
|
|
397
|
+
禁止「此刻/现在/正/一直/还」这类把状态延伸到现在的说法 —— 这段会带到下一轮,写成现在时会跟下一轮打架。
|
|
398
|
+
· 只写这轮真出现过的动作,**不许替${owner}补细节**:${owner}说「我刚想去」,别写成「刚要出门」。拿不准就写模糊的。
|
|
399
|
+
· 别翻来覆去用「踏实/甜/开心/心疼/担心」,换成口语的、有画面感的话。
|
|
400
|
+
5. confidence 0-100:只记进后台,不影响你下一轮说什么,老实标 —— 事情清楚 80~95;只能凭语气猜 60~75;信息不够或这轮很平淡 20~55。宁可标低。`
|
|
406
401
|
}
|
|
407
402
|
|
|
408
403
|
/** 「被叫起来干活」的补充:只追加例外,不换主规则。 */
|
|
@@ -566,6 +561,53 @@ export function sessionModelOf(agent) {
|
|
|
566
561
|
/** 单次心情判断的硬上限(毫秒)。超过就放弃这一轮,别把整条判断链堵死。 */
|
|
567
562
|
const JUDGE_TIMEOUT_MS = 25000
|
|
568
563
|
|
|
564
|
+
const SILICONFLOW_URL = 'https://api.siliconflow.cn/v1/chat/completions'
|
|
565
|
+
|
|
566
|
+
/** 「没找到密钥」只提示一次(每回合都提示会把 soul.log 淹掉)。 */
|
|
567
|
+
let missingKeyNoted = false
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 走硅基流动的 OpenAI 兼容接口判心情(2026-11-01)。
|
|
571
|
+
*
|
|
572
|
+
* 为什么另开一条 HTTP 路:小模型(Qwen2.5-7B 这一档)判这种「读一轮对话、
|
|
573
|
+
* 吐一个 JSON」的活已经够用,成本却只有旗舰模型的零头 —— 而这活儿每回合都要跑一次。
|
|
574
|
+
* 宿主的 llm 服务不认第三方 provider,所以直接打 HTTP。
|
|
575
|
+
*
|
|
576
|
+
* 失败一律返回 null(= 这轮不记账),绝不抛到 judgeChain 外面把整条链炸掉。
|
|
577
|
+
*/
|
|
578
|
+
async function judgeViaSiliconFlow(key, model, sys, user, o, note) {
|
|
579
|
+
try {
|
|
580
|
+
const signal = typeof AbortSignal?.timeout === 'function'
|
|
581
|
+
? AbortSignal.timeout(JUDGE_TIMEOUT_MS)
|
|
582
|
+
: undefined
|
|
583
|
+
const res = await fetch(SILICONFLOW_URL, {
|
|
584
|
+
method: 'POST',
|
|
585
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
586
|
+
body: JSON.stringify({
|
|
587
|
+
model,
|
|
588
|
+
messages: [
|
|
589
|
+
{ role: 'system', content: sys },
|
|
590
|
+
{ role: 'user', content: user }
|
|
591
|
+
],
|
|
592
|
+
max_tokens: 400,
|
|
593
|
+
temperature: 0.2
|
|
594
|
+
}),
|
|
595
|
+
signal
|
|
596
|
+
})
|
|
597
|
+
if (!res.ok) {
|
|
598
|
+
const body = await res.text().catch(() => '')
|
|
599
|
+
if (note) note('mood', `硅基流动返回 ${res.status}:${String(body).slice(0, 160)}`)
|
|
600
|
+
return null
|
|
601
|
+
}
|
|
602
|
+
const data = await res.json()
|
|
603
|
+
const text = String(data?.choices?.[0]?.message?.content ?? '')
|
|
604
|
+
return parseDelta(text, o.owner)
|
|
605
|
+
} catch (error) {
|
|
606
|
+
if (note) note('mood', `硅基流动调用失败:${error?.message ?? error}`)
|
|
607
|
+
return null
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
569
611
|
/**
|
|
570
612
|
* 判一轮的心情变化。返回 delta(parseDelta 的形状)或 null。
|
|
571
613
|
*
|
|
@@ -578,10 +620,6 @@ const JUDGE_TIMEOUT_MS = 25000
|
|
|
578
620
|
* @param note 可选的日志函数(纯逻辑模块不 import env,由调用方注入)
|
|
579
621
|
*/
|
|
580
622
|
export async function judgeMood(llm, agent, userText, reply, mode, o, note) {
|
|
581
|
-
if (!llm || typeof llm.stream !== 'function') return null
|
|
582
|
-
const picked = (o.judgeProvider && o.judgeModel) ? { provider: o.judgeProvider, model: o.judgeModel } : null
|
|
583
|
-
const target = picked || sessionModelOf(agent)
|
|
584
|
-
if (!target) return null
|
|
585
623
|
let sys = buildJudgeSystem(o)
|
|
586
624
|
let user
|
|
587
625
|
if (mode === 'work') {
|
|
@@ -604,6 +642,25 @@ export async function judgeMood(llm, agent, userText, reply, mode, o, note) {
|
|
|
604
642
|
+ '\n\n' + (o.owner || '对方') + '这轮说:\n' + String(userText || '').slice(0, 1600)
|
|
605
643
|
+ '\n\n请把「你怎么说的」和「' + (o.owner || '对方') + '说了什么」合起来判断你的心情变化,只输出 JSON。'
|
|
606
644
|
}
|
|
645
|
+
// ① 配了硅基流动就直接走它的 HTTP API(自己的密钥、自己的小模型)。
|
|
646
|
+
// 宿主的 llm 服务只认得它自己注册过的 provider,硅基流动不在里面,
|
|
647
|
+
// 所以这条路不经过 llm.stream —— 跟日记那条「自己调模型」是同一个思路。
|
|
648
|
+
if (o.judgeProvider === 'siliconflow' && o.judgeModel) {
|
|
649
|
+
const key = loadSecret('siliconflow')
|
|
650
|
+
if (key) return judgeViaSiliconFlow(key, o.judgeModel, sys, user, o, note)
|
|
651
|
+
// 只在第一次说,不要每回合刷一行(那会把 soul.log 淹掉)
|
|
652
|
+
if (note && missingKeyNoted === false) {
|
|
653
|
+
missingKeyNoted = true
|
|
654
|
+
note('mood', '配了硅基流动但没找到密钥(~/.dsh/agents/secrets/siliconflow.bin),退回会话模型')
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// ② 其余情况走宿主的 llm 服务
|
|
659
|
+
if (!llm || typeof llm.stream !== 'function') return null
|
|
660
|
+
const picked = (o.judgeProvider && o.judgeModel) ? { provider: o.judgeProvider, model: o.judgeModel } : null
|
|
661
|
+
const target = picked || sessionModelOf(agent)
|
|
662
|
+
if (!target) return null
|
|
663
|
+
|
|
607
664
|
// 自己构造一条 user 消息(不 import @deepseek-ai/dsh-llm,跟 feiyu-mood 同款)
|
|
608
665
|
const messages = [{
|
|
609
666
|
role: 'user',
|