dsh-lifecard 0.2.1 → 0.3.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/cordis.patch.yml +15 -7
- package/lib/client.js +1654 -175
- package/lib/codec.js +287 -1
- package/lib/core.js +90 -1
- package/lib/index.js +880 -13
- package/lib/novel.js +1514 -4
- package/lib/prose.js +253 -0
- package/lib/store.js +581 -15
- package/lifecard-tools.js +266 -10
- package/package.json +2 -1
- package/skills/human-writing/SKILL.md +141 -5
- package/skills/human-writing/VERSION +1 -1
- package/skills/human-writing/references/revision.md +24 -0
- package/skills/human-writing/scripts/check_prose.py +83 -0
- package/skills/novel-to-script/SKILL.md +103 -0
package/lib/index.js
CHANGED
|
@@ -11,13 +11,14 @@
|
|
|
11
11
|
|
|
12
12
|
import z from '@deepseek-ai/schemastery'
|
|
13
13
|
import path from 'node:path'
|
|
14
|
-
import { promises as fs, readFileSync } from 'node:fs'
|
|
14
|
+
import { promises as fs, readFileSync, statSync, readdirSync, appendFileSync, writeFileSync } from 'node:fs'
|
|
15
15
|
import { fileURLToPath } from 'node:url'
|
|
16
16
|
import { homedir } from 'node:os'
|
|
17
17
|
import os from 'node:os'
|
|
18
18
|
import * as core from './core.js'
|
|
19
19
|
import * as codec from './codec.js'
|
|
20
20
|
import * as store from './store.js'
|
|
21
|
+
import * as prose from './prose.js'
|
|
21
22
|
import * as prompts from './prompts.js'
|
|
22
23
|
|
|
23
24
|
import * as score from './score.js'
|
|
@@ -759,6 +760,16 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
759
760
|
home: homedir(),
|
|
760
761
|
hasLlm: ctx.get?.('llm') !== undefined && ctx.get?.('llm') !== null,
|
|
761
762
|
defaults: { workspace: ws },
|
|
763
|
+
// 界面上被收起来的门。读不出来就一个都不收(宁可多摆一扇,也不要空一块)。
|
|
764
|
+
hidden_doors: await (async () => {
|
|
765
|
+
try {
|
|
766
|
+
const conf = await store.readConfigState(ws)
|
|
767
|
+
const list = conf.ok === true ? conf.config?.hidden_doors : []
|
|
768
|
+
return Array.isArray(list) ? list.filter((x) => typeof x === 'string') : []
|
|
769
|
+
} catch {
|
|
770
|
+
return []
|
|
771
|
+
}
|
|
772
|
+
})(),
|
|
762
773
|
summary,
|
|
763
774
|
relations: prompts.RELATION_TYPES,
|
|
764
775
|
}
|
|
@@ -819,6 +830,9 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
819
830
|
relations: all.relations,
|
|
820
831
|
materials: all.materials,
|
|
821
832
|
errors: all.errors,
|
|
833
|
+
// 撤回箱:删掉的卡文件还在磁盘上,得有个地方列出来,用户才能恢复
|
|
834
|
+
// 或者彻底删掉。以前什么都没有,删掉的卡等于掉进黑洞。
|
|
835
|
+
withdrawn: all.withdrawn ?? [],
|
|
822
836
|
proposals,
|
|
823
837
|
}
|
|
824
838
|
},
|
|
@@ -894,12 +908,42 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
894
908
|
const safe = await insideWorkspace(ws, body.path)
|
|
895
909
|
if (safe.ok !== true) return { ok: false, error: safe.error }
|
|
896
910
|
rel = safe.rel
|
|
911
|
+
// 显式给了位置也一样:这个位置上有一张撤掉的卡,就没法「另存一张」盖过去。
|
|
912
|
+
// 不然换个入口就能把删掉的卡复活成一份新内容。
|
|
913
|
+
const there = await fs.stat(path.join(ws, rel)).then((st) => st.isFile()).catch(() => false)
|
|
914
|
+
if (!there) {
|
|
915
|
+
// 挡住这张新卡的是账上那张撤掉的卡,所以也带 withdrawn。
|
|
916
|
+
// 光比路径不够:id 是拼音、名字是中文时(people/rat.card.md 那种),
|
|
917
|
+
// 按名字再建会算出另一个路径,于是同一个人并排出现两张。
|
|
918
|
+
const gone = store.hitsRedactionByPath(await store.readRedactions(ws), rel) !== undefined
|
|
919
|
+
|| (await store.withdrawnNameHit(ws, rel, [data.id, data.name])) !== null
|
|
920
|
+
if (gone) {
|
|
921
|
+
return { ok: false, withdrawn: true, error: '这个名字被一张已经删掉的卡占着。先去首页「已经撤掉的」里把它清掉,或者换个名字。' }
|
|
922
|
+
}
|
|
923
|
+
}
|
|
897
924
|
} else {
|
|
898
925
|
rel = store.cardRelPath(id, data.world)
|
|
899
926
|
// 没给路径=这是一张新卡。同名文件已经在了就不再写:
|
|
900
927
|
// 以前会直接覆盖,用户点「新建」写了个已有的名字,那人原来那张卡
|
|
901
928
|
// 连同快照之外的一切(关系史、口癖)就没了声息地被换成了空白。
|
|
902
|
-
const
|
|
929
|
+
const abs = path.join(ws, rel)
|
|
930
|
+
const exists = await fs.stat(abs).then((st) => st.isFile()).catch(() => false)
|
|
931
|
+
if (!exists) {
|
|
932
|
+
// 文件搬去回收目录以后,原位确实空了,可名字还被一张撤掉的卡占着。
|
|
933
|
+
// 只判文件在不在的话,删掉的卡会被当成「没人用这个名字」默默重建回来。
|
|
934
|
+
const redactions = await store.readRedactions(ws)
|
|
935
|
+
// 同目录里同名的也算占着:删掉一张叫「妈妈」的卡(文件是 people/mama.card.md),
|
|
936
|
+
// 再按名字建会算出 people/妈妈.card.md,两张「妈妈」就并排出现了。
|
|
937
|
+
const gone = store.hitsRedactionByPath(redactions, rel) !== undefined
|
|
938
|
+
|| (await store.withdrawnNameHit(ws, rel, [id, data.name])) !== null
|
|
939
|
+
if (gone) {
|
|
940
|
+
return {
|
|
941
|
+
ok: false,
|
|
942
|
+
withdrawn: true,
|
|
943
|
+
error: '这个名字被一张已经删掉的卡占着。先去首页「已经撤掉的」里把它清掉,或者换个名字。',
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
903
947
|
if (exists && body?.overwrite !== true) {
|
|
904
948
|
return {
|
|
905
949
|
ok: false,
|
|
@@ -996,6 +1040,32 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
996
1040
|
},
|
|
997
1041
|
}
|
|
998
1042
|
},
|
|
1043
|
+
// 开一个新对话,并把第一句替用户发出去。
|
|
1044
|
+
//
|
|
1045
|
+
// 为什么放在后台:宿主把「建会话 + 发消息」这套能力挂在 sessionController 上,
|
|
1046
|
+
// 后台一定拿得到;客户端那侧不一定(真机上 uiWorkspace 有时根本不在插件作用域里,
|
|
1047
|
+
// 用户点完只会看到「这个版本不支持」)。所以正路从后台走,客户端只负责把会话切到眼前。
|
|
1048
|
+
'session.open': async (body) => {
|
|
1049
|
+
const controller = ctx.get?.('sessionController', false)
|
|
1050
|
+
if (controller === null || controller === undefined || typeof controller.create !== 'function') {
|
|
1051
|
+
return { ok: false, error: '这台机器没有开新对话的能力,把要说的那句话复制到新对话里也一样能用。' }
|
|
1052
|
+
}
|
|
1053
|
+
const preset = String(body?.preset ?? '').trim()
|
|
1054
|
+
const text = String(body?.text ?? '')
|
|
1055
|
+
try {
|
|
1056
|
+
const created = await controller.create(preset === '' ? {} : { agentPreset: preset })
|
|
1057
|
+
const id = String(created?.sessionId ?? '')
|
|
1058
|
+
if (id === '') return { ok: false, error: '新对话没建起来,再点一次试试。' }
|
|
1059
|
+
let sent = false
|
|
1060
|
+
if (text !== '' && typeof controller.prompt === 'function') {
|
|
1061
|
+
await controller.prompt({ sessionId: id, mode: 'queue', content: [{ type: 'text', text }] })
|
|
1062
|
+
sent = true
|
|
1063
|
+
}
|
|
1064
|
+
return { ok: true, sessionId: id, agentPreset: String(created?.agentPreset ?? ''), sent }
|
|
1065
|
+
} catch (error) {
|
|
1066
|
+
return { ok: false, error: humanizeHostError(error) }
|
|
1067
|
+
}
|
|
1068
|
+
},
|
|
999
1069
|
'card.defaults.set': async (body) => {
|
|
1000
1070
|
const ws = await root(body)
|
|
1001
1071
|
const mode = String(body?.mode ?? '')
|
|
@@ -1010,6 +1080,30 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
1010
1080
|
await store.writeConfig(ws, { default_cards: pins })
|
|
1011
1081
|
return { ok: true, cards: pins }
|
|
1012
1082
|
},
|
|
1083
|
+
/**
|
|
1084
|
+
* 把一扇门从界面上收起来 / 装回来。
|
|
1085
|
+
*
|
|
1086
|
+
* 只是界面上的开关:卡、世界书、书、会话一个字节都不动,
|
|
1087
|
+
* 所以「删掉」这件事随时可以反悔。
|
|
1088
|
+
*/
|
|
1089
|
+
'door.set': async (body) => {
|
|
1090
|
+
const ws = await root(body)
|
|
1091
|
+
const id = String(body?.id ?? '').trim()
|
|
1092
|
+
if (id !== 'real' && id !== 'novel' && id !== 'tavern') return { ok: false, error: '不知道你说的是哪扇门。' }
|
|
1093
|
+
const confState = await store.readConfigState(ws)
|
|
1094
|
+
if (confState.ok !== true) return { ok: false, error: confState.error ?? '这个文件夹里的设置读不出来。' }
|
|
1095
|
+
const conf = confState.config ?? {}
|
|
1096
|
+
const hidden = Array.isArray(conf?.hidden_doors) ? conf.hidden_doors.filter((x) => typeof x === 'string') : []
|
|
1097
|
+
const want = body?.hidden === true
|
|
1098
|
+
const next = want
|
|
1099
|
+
? (hidden.includes(id) ? hidden : [...hidden, id])
|
|
1100
|
+
: hidden.filter((x) => x !== id)
|
|
1101
|
+
if (next.length === 3) return { ok: false, error: '三扇门都收起来就没地方进去了,至少留一扇。' }
|
|
1102
|
+
const saved = await store.writeConfig(ws, { ...conf, hidden_doors: next })
|
|
1103
|
+
if (saved === false || saved?.ok === false) return { ok: false, error: '这个开关没能存下来,等一会儿再试一次。' }
|
|
1104
|
+
return { ok: true, hidden_doors: next }
|
|
1105
|
+
},
|
|
1106
|
+
|
|
1013
1107
|
'card.history': async (body) => {
|
|
1014
1108
|
const ws = await root(body)
|
|
1015
1109
|
const safe = await insideWorkspace(ws, body?.path)
|
|
@@ -1029,21 +1123,44 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
1029
1123
|
return store.readSnapshot(ws, rel, body?.stamp)
|
|
1030
1124
|
},
|
|
1031
1125
|
|
|
1032
|
-
/**
|
|
1126
|
+
/**
|
|
1127
|
+
* 恢复一张卡。两种意思,看有没有给版本号:
|
|
1128
|
+
* - 给了 `stamp`:退回那一版历史(老行为,不动)。
|
|
1129
|
+
* - 没给:把撤回过的卡救回列表(追加一条恢复记录)。
|
|
1130
|
+
*
|
|
1131
|
+
* 合并成一条路由,是因为界面上就一个「恢复」动作,用户不区分这两种。
|
|
1132
|
+
*/
|
|
1033
1133
|
'card.restore': async (body) => {
|
|
1034
1134
|
const ws = await root(body)
|
|
1035
1135
|
const safe = await insideWorkspace(ws, body?.path)
|
|
1036
1136
|
if (safe.ok !== true) return { ok: false, error: safe.error }
|
|
1037
1137
|
const rel = safe.rel
|
|
1038
1138
|
if (!rel.endsWith('.card.md')) return { ok: false, error: '这个位置不是一张卡。' }
|
|
1039
|
-
const
|
|
1139
|
+
const stamp = body?.stamp
|
|
1140
|
+
if (stamp === undefined || stamp === null || stamp === '') {
|
|
1141
|
+
return store.restoreCard(ws, rel)
|
|
1142
|
+
}
|
|
1143
|
+
// 卡还撤着的时候退回旧版本,会把文件写回原位、而它仍然不在列表里——
|
|
1144
|
+
// 那就又造出一个「看得见文件、看不见卡」的死结。先放回来,再谈版本。
|
|
1145
|
+
if (store.hitsRedactionByPath(await store.readRedactions(ws), rel) !== undefined) {
|
|
1146
|
+
// 跟 readCard 的两条「已撤掉」分支一个形:带 withdrawn,上层只看这个字段
|
|
1147
|
+
// 就知道该给「恢复」的门,不用去猜是哪种撤法。
|
|
1148
|
+
return { ok: false, withdrawn: true, error: '这张卡现在是撤掉的状态。先把它放回列表,再退回旧版本。' }
|
|
1149
|
+
}
|
|
1150
|
+
const old = await store.readSnapshot(ws, rel, stamp)
|
|
1040
1151
|
if (old.ok !== true) return old
|
|
1041
1152
|
const written = await store.writeCard(ws, rel, old.data, old.sections, { force: true })
|
|
1042
1153
|
if (written.ok !== true) return written
|
|
1043
1154
|
return { ok: true, path: rel, validation: written.validation, note: '已经退回这一版。刚才那一版也还在历史里。' }
|
|
1044
1155
|
},
|
|
1045
1156
|
|
|
1046
|
-
/**
|
|
1157
|
+
/**
|
|
1158
|
+
* 撤掉一张卡:文件搬进回收目录,再写一条墓碑。
|
|
1159
|
+
*
|
|
1160
|
+
* 以前只写墓碑、文件留在原地。看起来更「可审计」,实际是个死结:
|
|
1161
|
+
* 名字还占着(`card.save` 只判文件在不在),可列表里又看不见,
|
|
1162
|
+
* 用户照着提示去找也找不到,删掉的卡于是就那么赖着。
|
|
1163
|
+
*/
|
|
1047
1164
|
'card.delete': async (body) => {
|
|
1048
1165
|
const ws = await root(body)
|
|
1049
1166
|
const safe = await insideWorkspace(ws, body?.path)
|
|
@@ -1052,8 +1169,84 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
1052
1169
|
if (!rel.endsWith('.card.md')) return { ok: false, error: '这个位置不是一张卡。' }
|
|
1053
1170
|
const card = await store.readCard(ws, rel)
|
|
1054
1171
|
if (!card.ok) return card
|
|
1055
|
-
await store.
|
|
1056
|
-
|
|
1172
|
+
const moved = await store.moveCardToRedacted(ws, rel)
|
|
1173
|
+
if (moved.ok !== true) return moved
|
|
1174
|
+
try {
|
|
1175
|
+
await store.appendRedaction(ws, { path: rel, sha256: store.sha256(card.text), reason: String(body?.reason ?? '用户删除') })
|
|
1176
|
+
} catch (err) {
|
|
1177
|
+
// 账没记上就把文件放回去:不然这张卡既不在列表里,也没人知道它被删过。
|
|
1178
|
+
const putBack = await fs.rename(path.join(ws, moved.redacted), path.join(ws, rel)).then(() => true).catch(() => false)
|
|
1179
|
+
if (!putBack) {
|
|
1180
|
+
// 搬回去也失败了:残份留在回收目录里、账上又没有它。别再让这种残份
|
|
1181
|
+
// 变成「摘不掉的一行」——restoreCard / purgeCard 对「有残份、没账」
|
|
1182
|
+
// 的情况放行,用户可以照常放回来或彻底删掉。
|
|
1183
|
+
return { ok: false, error: '这次删除没记下来,卡也没能放回原处。它现在在「已经撤掉的」里,可以放回来,也可以彻底删掉。' }
|
|
1184
|
+
}
|
|
1185
|
+
return { ok: false, error: '这次删除没记下来,卡已经放回原处了,再试一次。' }
|
|
1186
|
+
}
|
|
1187
|
+
await clearPinsFor(ws, rel, card.data)
|
|
1188
|
+
// 返回值要把「还能找回来」说出来。只写「已经删掉了」,用户手滑之后
|
|
1189
|
+
// 就不知道该去哪儿反悔,删掉的卡于是永远躺在回收目录里没人认领。
|
|
1190
|
+
return {
|
|
1191
|
+
ok: true,
|
|
1192
|
+
path: rel,
|
|
1193
|
+
note: '已经删掉了:它不会再出现在列表、导出和 AI 的眼里。已经放到「已经撤掉的」里了,想反悔可以在那儿恢复;要从磁盘上彻底删掉,也是在那儿删。',
|
|
1194
|
+
withdrawn: true,
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* 收走一张刚建的空卡(访谈开局建错了用)。
|
|
1200
|
+
*
|
|
1201
|
+
* 和「删除」分开是因为它不该留墓碑:这张卡从来没进过用户的列表,
|
|
1202
|
+
* 只是建错了一下。写墓碑会占住名字,紧接着按同一个名字再建就撞墙。
|
|
1203
|
+
* 只对「正文全空、刚建不久」的卡开放,免得变成删活卡的后门。
|
|
1204
|
+
*/
|
|
1205
|
+
'card.discard': async (body) => {
|
|
1206
|
+
const ws = await root(body)
|
|
1207
|
+
const safe = await insideWorkspace(ws, body?.path)
|
|
1208
|
+
if (safe.ok !== true) return { ok: false, error: safe.error }
|
|
1209
|
+
const rel = safe.rel
|
|
1210
|
+
if (!rel.endsWith('.card.md')) return { ok: false, error: '这个位置不是一张卡。' }
|
|
1211
|
+
const card = await store.readCard(ws, rel)
|
|
1212
|
+
if (!card.ok) return card
|
|
1213
|
+
const written = (card.sections ?? []).map((one) => String(one?.body ?? '').trim()).join('')
|
|
1214
|
+
if (written !== '') {
|
|
1215
|
+
return { ok: false, error: '这张卡里已经写了内容,不能当成空卡收走。要删就在列表里删。' }
|
|
1216
|
+
}
|
|
1217
|
+
const stat = await fs.stat(path.join(ws, rel)).catch(() => null)
|
|
1218
|
+
const age = stat === null ? Number.POSITIVE_INFINITY : Date.now() - stat.mtimeMs
|
|
1219
|
+
if (!(age <= 10 * 60 * 1000)) {
|
|
1220
|
+
return { ok: false, error: '这张卡已经放了一会儿了,不能当成刚建的空卡收走。要删就在列表里删。' }
|
|
1221
|
+
}
|
|
1222
|
+
// 搬走之后必须记一笔「收走」:不记账的残份会被列进「已经撤掉的」,
|
|
1223
|
+
// 而「放回来」「彻底删掉」都以账为准,于是那一行两个动作都失败,
|
|
1224
|
+
// 用户看着一条摘不掉的幽灵。
|
|
1225
|
+
// 记「彻底删掉」而不是「撤掉」,是因为 discard 的本意是别让这张建错的
|
|
1226
|
+
// 空卡占住名字——撤掉态会占名字,清掉态不会,同名立刻能再建。
|
|
1227
|
+
const moved = await store.moveCardToRedacted(ws, rel)
|
|
1228
|
+
if (moved.ok !== true) return moved
|
|
1229
|
+
try {
|
|
1230
|
+
await store.appendRedaction(ws, { kind: 'purge', path: rel, reason: '建错了的空卡' })
|
|
1231
|
+
} catch {
|
|
1232
|
+
// 记不上就搬回原处,别留一条谁也处理不了的残份。
|
|
1233
|
+
await fs.rename(path.join(ws, moved.redacted), path.join(ws, rel)).catch(() => {})
|
|
1234
|
+
return { ok: false, error: '这张空卡没能收走,卡还在原处,再试一次。' }
|
|
1235
|
+
}
|
|
1236
|
+
return moved
|
|
1237
|
+
},
|
|
1238
|
+
|
|
1239
|
+
/**
|
|
1240
|
+
* 彻底删掉一张撤回过的卡:真删磁盘文件。
|
|
1241
|
+
*
|
|
1242
|
+
* 只允许对**已撤回**的卡做。这一步不可撤销,所以设一道前提:先在列表里
|
|
1243
|
+
* 删过一次(那一步可恢复)、用户确实看过「已撤回」里那一行,才谈得上彻底删。
|
|
1244
|
+
*/
|
|
1245
|
+
'card.purge': async (body) => {
|
|
1246
|
+
const ws = await root(body)
|
|
1247
|
+
const safe = await insideWorkspace(ws, body?.path)
|
|
1248
|
+
if (safe.ok !== true) return { ok: false, error: safe.error }
|
|
1249
|
+
return store.purgeCard(ws, safe.rel)
|
|
1057
1250
|
},
|
|
1058
1251
|
|
|
1059
1252
|
/** 导入酒馆卡(PNG / JSON / CHARX)。body.files 是 [{name, base64}]。 */
|
|
@@ -2039,9 +2232,42 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
2039
2232
|
const materials = await store.readMaterials(ws)
|
|
2040
2233
|
const names = Object.keys(materials)
|
|
2041
2234
|
if (names.length === 0) return { ok: false, error: '还没有材料。先加一段文字,或者做一次访谈,AI 才有东西可读。' }
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2235
|
+
// 三种意思必须分得开:不传 / null = 全读;给数组 = 只读这些;空数组 = 一份都不读。
|
|
2236
|
+
// 以前空数组也被当成「全读」,用户明明一份都没勾,AI 却把整个材料库读了一遍。
|
|
2237
|
+
const want = body?.materials
|
|
2238
|
+
let chosen = materials
|
|
2239
|
+
if (Array.isArray(want)) {
|
|
2240
|
+
if (want.length === 0) {
|
|
2241
|
+
return { ok: false, error: '这次没选任何材料。至少要选一份,或者把要读的材料挑出来。' }
|
|
2242
|
+
}
|
|
2243
|
+
const picked = {}
|
|
2244
|
+
const missing = []
|
|
2245
|
+
for (const one of want) {
|
|
2246
|
+
const key = String(one ?? '').trim()
|
|
2247
|
+
if (key === '') {
|
|
2248
|
+
missing.push('(空的一条)')
|
|
2249
|
+
continue
|
|
2250
|
+
}
|
|
2251
|
+
if (Object.hasOwn(materials, key)) {
|
|
2252
|
+
picked[key] = materials[key]
|
|
2253
|
+
continue
|
|
2254
|
+
}
|
|
2255
|
+
// 界面上有时只传文件名,或者多带了一层目录;能唯一对上一份就认。
|
|
2256
|
+
const base = path.posix.basename(key)
|
|
2257
|
+
const same = Object.keys(materials).filter((k) => k.endsWith('/' + key) || path.posix.basename(k) === base)
|
|
2258
|
+
if (same.length === 1) {
|
|
2259
|
+
picked[same[0]] = materials[same[0]]
|
|
2260
|
+
continue
|
|
2261
|
+
}
|
|
2262
|
+
missing.push(key)
|
|
2263
|
+
}
|
|
2264
|
+
if (missing.length > 0) {
|
|
2265
|
+
return { ok: false, error: `这几份材料找不到了:${missing.join('、')}。可能刚被删掉或者改了名,刷新一下再挑一次。` }
|
|
2266
|
+
}
|
|
2267
|
+
chosen = picked
|
|
2268
|
+
} else if (want !== undefined && want !== null) {
|
|
2269
|
+
return { ok: false, error: '没看懂要读哪几份材料。要么不传(全都读),要么给一份材料清单。' }
|
|
2270
|
+
}
|
|
2045
2271
|
if (Object.keys(chosen).length === 0) return { ok: false, error: '选中的材料都不存在' }
|
|
2046
2272
|
const aboutName = String(body?.name ?? aboutId)
|
|
2047
2273
|
// 「想要什么样的人」可以临时覆盖工作区里存的那份(比如试一下别的口味)。
|
|
@@ -2291,7 +2517,25 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
2291
2517
|
const materialName = `${date}-${core.safeId(aboutId)}访谈`
|
|
2292
2518
|
let saved = null
|
|
2293
2519
|
if (status === 'answered' && answer.trim() !== '') {
|
|
2294
|
-
|
|
2520
|
+
// 标题用**问题本身**,不只用题号。以前标题是 `## real.q03`,回头
|
|
2521
|
+
// 「整理进卡里」时模型看到的是一串题号加裸答案,不知道这句话在回答什么,
|
|
2522
|
+
// 只能瞎猜字段——于是把答案硬塞进不相干的格子里。
|
|
2523
|
+
const askText = String(body?.ask ?? '').trim()
|
|
2524
|
+
const options = Array.isArray(body?.options) ? body.options.filter((x) => String(x ?? '').trim() !== '') : []
|
|
2525
|
+
const block = [
|
|
2526
|
+
`## ${askText === '' ? questionId : askText}`,
|
|
2527
|
+
'',
|
|
2528
|
+
`问过的题号:${questionId}`,
|
|
2529
|
+
...(options.length === 0 ? [] : ['', `当时给的几个方向:${options.map((x) => String(x).trim()).join(' / ')}`]),
|
|
2530
|
+
'',
|
|
2531
|
+
answer.trim(),
|
|
2532
|
+
'',
|
|
2533
|
+
'```',
|
|
2534
|
+
`q: ${questionId}`,
|
|
2535
|
+
`asked_at: ${new Date().toISOString()}`,
|
|
2536
|
+
'```',
|
|
2537
|
+
'',
|
|
2538
|
+
].join('\n')
|
|
2295
2539
|
saved = await store.appendMaterial(ws, materialName, block)
|
|
2296
2540
|
}
|
|
2297
2541
|
// 账本
|
|
@@ -2673,7 +2917,12 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
2673
2917
|
const ws = await root(body)
|
|
2674
2918
|
const id = String(body?.id ?? '').trim()
|
|
2675
2919
|
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
2676
|
-
|
|
2920
|
+
const gone = await novel.deleteChapter(ws, id, body?.index)
|
|
2921
|
+
if (gone.ok !== true) return gone
|
|
2922
|
+
// 这一章没了,它上面那份剧本留着就是一份谁也点不到的孤儿——
|
|
2923
|
+
// 界面上只能按章翻,第 N 章都不存在了,那份剧本永远打不开也删不掉。
|
|
2924
|
+
const script = await novel.deleteScript(ws, id, body?.index)
|
|
2925
|
+
return script.ok === true ? gone : { ...gone, scriptKept: true }
|
|
2677
2926
|
},
|
|
2678
2927
|
|
|
2679
2928
|
/** 整本书不要了。 */
|
|
@@ -2843,6 +3092,7 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
2843
3092
|
'2. 你读到哪儿不信(这个人不会这么做、这件事不会这么发生)',
|
|
2844
3093
|
'3. 你读到哪儿想跳过(哪几段你在心里划过去了)',
|
|
2845
3094
|
'4. 哪句话是空话,删掉不影响(直接引原句)',
|
|
3095
|
+
'5. 哪一句读着最像机器写的——太顺、太整齐、什么都说了却没具体的东西(直接引原句)',
|
|
2846
3096
|
'',
|
|
2847
3097
|
'【怎么答】',
|
|
2848
3098
|
'- 每条一行,先写「哪里:」再写「为什么:」。要引原句。',
|
|
@@ -2867,7 +3117,27 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
2867
3117
|
if (called.ok !== true) return { ok: false, error: `这次没审成:${called.error ?? '过一会儿再试一次'}` }
|
|
2868
3118
|
const review = String(called.text ?? '').trim()
|
|
2869
3119
|
if (review === '') return { ok: false, error: 'AI 这次没给出意见,再点一次试试。' }
|
|
2870
|
-
|
|
3120
|
+
|
|
3121
|
+
// 读者的意见靠模型读,套话靠数。
|
|
3122
|
+
//
|
|
3123
|
+
// 「不是…而是…」用了四次、「瞳孔一缩」出现三次——这种密度人眼看不出来,
|
|
3124
|
+
// 数一遍就知道。所以这一段是**附带**给的:不改模型的意见,
|
|
3125
|
+
// 也不替作者改句子,只说哪个说法出现了几次、举一句原文。
|
|
3126
|
+
const scan = prose.scanProse(bodyText)
|
|
3127
|
+
const scanned = prose.proseReportText(scan)
|
|
3128
|
+
const combined = scanned === ''
|
|
3129
|
+
? review
|
|
3130
|
+
: `${review}\n\n【顺手数了一下】(不是意见,是数出来的)\n${scanned}`
|
|
3131
|
+
|
|
3132
|
+
return {
|
|
3133
|
+
ok: true,
|
|
3134
|
+
index,
|
|
3135
|
+
review: combined,
|
|
3136
|
+
scan,
|
|
3137
|
+
chars: bodyText.length,
|
|
3138
|
+
blind: true,
|
|
3139
|
+
model: String(body?.model ?? ''),
|
|
3140
|
+
}
|
|
2871
3141
|
},
|
|
2872
3142
|
|
|
2873
3143
|
/** 读一章。 */
|
|
@@ -3242,6 +3512,167 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
3242
3512
|
usedCast: cast.length,
|
|
3243
3513
|
}
|
|
3244
3514
|
},
|
|
3515
|
+
|
|
3516
|
+
// ── 改成剧本 ────────────────────────────────────────────────
|
|
3517
|
+
// 一章小说 → 一份能配音、能拍的剧本。落在 `books/<书>/scripts/<章号>.md`,
|
|
3518
|
+
// 和章节文件同号,回头一眼能对上「第三章改出来的那份」。
|
|
3519
|
+
|
|
3520
|
+
/** 读这一章改出来的剧本(没改过也正常,返回空串)。 */
|
|
3521
|
+
'novel.script.get': async (body) => {
|
|
3522
|
+
if ((await ensureModules()).novel !== true) return missingFeature('novel')
|
|
3523
|
+
const ws = await root(body)
|
|
3524
|
+
const id = String(body?.id ?? '').trim()
|
|
3525
|
+
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
3526
|
+
const target = await scriptTarget(ws, id, body?.index)
|
|
3527
|
+
if (target.error !== undefined) return { ok: false, error: target.error }
|
|
3528
|
+
const got = await novel.readScript(ws, id, target.index)
|
|
3529
|
+
if (got.ok !== true) return got
|
|
3530
|
+
const scan = novel.scanScript(got.text)
|
|
3531
|
+
return { ...got, scan, report: novel.scriptReportText(scan) }
|
|
3532
|
+
},
|
|
3533
|
+
|
|
3534
|
+
/** 存这一章的剧本(手改过的也走这里)。 */
|
|
3535
|
+
'novel.script.save': async (body) => {
|
|
3536
|
+
if ((await ensureModules()).novel !== true) return missingFeature('novel')
|
|
3537
|
+
const ws = await root(body)
|
|
3538
|
+
const id = String(body?.id ?? '').trim()
|
|
3539
|
+
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
3540
|
+
const target = await scriptTarget(ws, id, body?.index)
|
|
3541
|
+
if (target.error !== undefined) return { ok: false, error: target.error }
|
|
3542
|
+
const raw = String(body?.text ?? '')
|
|
3543
|
+
// 只剩空格(含全角空格)和换行:这人不小心清空了。
|
|
3544
|
+
// 以前这里直接删文件,回话还说「存好了」——磁盘上那份就再也找不回来。
|
|
3545
|
+
// 现在只回一句「空的,没往里存」,原来的那份一动不动。
|
|
3546
|
+
if (raw.replace(/[\s\u3000]/g, '') === '') {
|
|
3547
|
+
const scan = novel.scanScript('')
|
|
3548
|
+
return { ok: false, blank: true, index: target.index, error: '这一份是空的,没往里存。原来那一份还在。' }
|
|
3549
|
+
}
|
|
3550
|
+
const saved = await novel.writeScript(ws, id, target.index, raw)
|
|
3551
|
+
if (saved.ok !== true) return saved
|
|
3552
|
+
// 扫的是真正落盘的那一份(writeScript 会先剥掉 ``` 围栏)。
|
|
3553
|
+
// 扫原文的话,回执里的场数、字数会和磁盘上那份对不上。
|
|
3554
|
+
const stored = await novel.readScript(ws, id, target.index)
|
|
3555
|
+
const storedText = stored.ok === true ? stored.text : raw
|
|
3556
|
+
const scan = novel.scanScript(storedText)
|
|
3557
|
+
return { ...saved, scan, report: novel.scriptReportText(scan) }
|
|
3558
|
+
},
|
|
3559
|
+
|
|
3560
|
+
/** 这本书改过哪几章。 */
|
|
3561
|
+
'novel.script.list': async (body) => {
|
|
3562
|
+
if ((await ensureModules()).novel !== true) return missingFeature('novel')
|
|
3563
|
+
const ws = await root(body)
|
|
3564
|
+
const id = String(body?.id ?? '').trim()
|
|
3565
|
+
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
3566
|
+
const book = await novel.readBook(ws, id)
|
|
3567
|
+
if (book.ok !== true) return { ok: false, error: '这本书不在了。回到书架看一眼,可能被移走了。' }
|
|
3568
|
+
return await novel.listScripts(ws, id)
|
|
3569
|
+
},
|
|
3570
|
+
|
|
3571
|
+
/** 删掉这一章改出来的剧本。章节本身不动。 */
|
|
3572
|
+
'novel.script.delete': async (body) => {
|
|
3573
|
+
if ((await ensureModules()).novel !== true) return missingFeature('novel')
|
|
3574
|
+
const ws = await root(body)
|
|
3575
|
+
const id = String(body?.id ?? '').trim()
|
|
3576
|
+
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
3577
|
+
const target = await scriptTarget(ws, id, body?.index)
|
|
3578
|
+
if (target.error !== undefined) return { ok: false, error: target.error }
|
|
3579
|
+
return await novel.deleteScript(ws, id, target.index)
|
|
3580
|
+
},
|
|
3581
|
+
|
|
3582
|
+
/**
|
|
3583
|
+
* 让 AI 把这一章改成剧本。
|
|
3584
|
+
*
|
|
3585
|
+
* 只给这一章的正文 + 人 + 设定 + 上一章剧本的最后一场。
|
|
3586
|
+
* 不给大纲:改剧本不是重写故事,章后面的事它不需要知道——知道了会忍不住
|
|
3587
|
+
* 提前把底抖出来,那是改编里最容易炸的一处。
|
|
3588
|
+
*/
|
|
3589
|
+
'novel.script.draft': async (body) => {
|
|
3590
|
+
if ((await ensureModules()).novel !== true) return missingFeature('novel')
|
|
3591
|
+
const ws = await root(body)
|
|
3592
|
+
const id = String(body?.id ?? '').trim()
|
|
3593
|
+
if (id === '') return { ok: false, error: '没有说是哪本书' }
|
|
3594
|
+
const book = await novel.readBook(ws, id)
|
|
3595
|
+
if (book.ok !== true) return book
|
|
3596
|
+
// 没给章号就用最后一章;给了但不合法要说出来,不能悄悄改成第 1 章去改。
|
|
3597
|
+
const rawIndex = body?.index
|
|
3598
|
+
const gaveIndex = rawIndex !== undefined && rawIndex !== null && String(rawIndex).trim() !== ''
|
|
3599
|
+
if (gaveIndex === true && novel.isValidChapterIndex(rawIndex) !== true) {
|
|
3600
|
+
return { ok: false, error: '章号不对,没法知道你说的是哪一章。' }
|
|
3601
|
+
}
|
|
3602
|
+
const index = gaveIndex === true
|
|
3603
|
+
? Number(String(rawIndex).trim())
|
|
3604
|
+
: (book.chapters.length === 0 ? 1 : book.chapters[book.chapters.length - 1].index)
|
|
3605
|
+
const chapter = await novel.readChapter(ws, id, index)
|
|
3606
|
+
if (chapter.ok !== true) return chapter
|
|
3607
|
+
const chapterBody = String(chapter.body ?? '').trim()
|
|
3608
|
+
if (chapterBody === '') return { ok: false, error: `第 ${index} 章还是空的,没有东西可以改。`, index }
|
|
3609
|
+
if (chapterBody.length > 20000) {
|
|
3610
|
+
return { ok: false, error: '这一章太长(超过两万字),一次改不完。先分成两章再改。', index }
|
|
3611
|
+
}
|
|
3612
|
+
|
|
3613
|
+
// 上一章改出来的剧本,最后一场整段带上:谁站在哪、手上有什么、门开没开。
|
|
3614
|
+
let previousTail = ''
|
|
3615
|
+
if (index > 1) {
|
|
3616
|
+
const prev = await novel.readScript(ws, id, index - 1)
|
|
3617
|
+
if (prev.ok === true && String(prev.text ?? '').trim() !== '') {
|
|
3618
|
+
const parsed = novel.parseScriptScenes(prev.text)
|
|
3619
|
+
const last = parsed.scenes[parsed.scenes.length - 1]
|
|
3620
|
+
previousTail = last === undefined ? novel.tailOf(prev.text, 600) : last.raw.join('\n').trim()
|
|
3621
|
+
}
|
|
3622
|
+
}
|
|
3623
|
+
|
|
3624
|
+
// 世界设定(改剧本时全量给:场的布景、道具、天气都得对得上)
|
|
3625
|
+
const worldNotes = []
|
|
3626
|
+
const worldRel = typeof body?.world === 'string' && body.world.trim() !== '' ? body.world.trim() : ''
|
|
3627
|
+
if (worldRel !== '') {
|
|
3628
|
+
const world = await store.readCard(ws, worldRel)
|
|
3629
|
+
if (world.ok === true) {
|
|
3630
|
+
for (const entry of core.parseWorldEntries(world.sections)) {
|
|
3631
|
+
const content = String(entry?.content ?? '').trim()
|
|
3632
|
+
if (content !== '') worldNotes.push(content)
|
|
3633
|
+
}
|
|
3634
|
+
}
|
|
3635
|
+
}
|
|
3636
|
+
|
|
3637
|
+
const cast = await gatherCast(ws, body?.cast)
|
|
3638
|
+
const firstLine = chapterBody.split(/\r?\n/).find((line) => line.trim() !== '') ?? ''
|
|
3639
|
+
const user = novel.buildScriptPrompt({
|
|
3640
|
+
title: String(book.data?.title ?? id),
|
|
3641
|
+
genre: String(book.data?.genre ?? ''),
|
|
3642
|
+
idea: String(book.idea ?? ''),
|
|
3643
|
+
chapterNo: index,
|
|
3644
|
+
chapterTitle: firstLine.replace(/^#+\s*/, '').trim().slice(0, 60),
|
|
3645
|
+
chapterBody,
|
|
3646
|
+
cast,
|
|
3647
|
+
worldEntries: worldNotes,
|
|
3648
|
+
previousTail,
|
|
3649
|
+
ask: body?.ask,
|
|
3650
|
+
sceneHint: body?.scenes,
|
|
3651
|
+
})
|
|
3652
|
+
const called = await callModel(ctx, {
|
|
3653
|
+
system: novel.buildScriptSystemNote(),
|
|
3654
|
+
user,
|
|
3655
|
+
model: body?.model,
|
|
3656
|
+
})
|
|
3657
|
+
if (called.ok !== true) {
|
|
3658
|
+
return { ok: false, error: `这次没改成:${called.error ?? '过一会儿再试一次'}`, index }
|
|
3659
|
+
}
|
|
3660
|
+
const text = novel.stripFence(called.text)
|
|
3661
|
+
if (text === '') return { ok: false, error: 'AI 这次什么都没给,再点一次试试。', index }
|
|
3662
|
+
|
|
3663
|
+
const scan = novel.scanScript(text)
|
|
3664
|
+
return {
|
|
3665
|
+
ok: true,
|
|
3666
|
+
index,
|
|
3667
|
+
text,
|
|
3668
|
+
scan,
|
|
3669
|
+
report: novel.scriptReportText(scan),
|
|
3670
|
+
// 不落盘:改出来的东西人要先看、能删掉半场、能整段重写。存盘由 script.save 做。
|
|
3671
|
+
usedCast: cast.length,
|
|
3672
|
+
usedWorld: worldNotes.length,
|
|
3673
|
+
continued: previousTail !== '',
|
|
3674
|
+
}
|
|
3675
|
+
},
|
|
3245
3676
|
}
|
|
3246
3677
|
}
|
|
3247
3678
|
|
|
@@ -3252,6 +3683,31 @@ function buildApi(ctx, workspace, config, options) {
|
|
|
3252
3683
|
* 它才不会把「说话很冲的那个人」写成温和的——那是长篇最容易崩的地方。
|
|
3253
3684
|
* 每张卡只取前 500 字:卡可能写得很长,全贴会把大纲和上一章挤掉。
|
|
3254
3685
|
*/
|
|
3686
|
+
/**
|
|
3687
|
+
* 章号闸。
|
|
3688
|
+
*
|
|
3689
|
+
* `chapterFileName` 里有 `Math.max(1, Math.min(999, Math.floor(Number(index) || 1)))`
|
|
3690
|
+
* —— 它把 0、-1、'abc'、null 一律**悄悄**夹成 1。对「读」来说没问题,
|
|
3691
|
+
* 对「写」就是灾难:`novel.script.save({index:0})` 会盖掉第 1 章原来那份剧本,
|
|
3692
|
+
* 而且回话说的是「存好了」。
|
|
3693
|
+
*
|
|
3694
|
+
* 所以写之前先在这里拦一道:章号得是正整数,而且这一章得真的存在。
|
|
3695
|
+
* 返回 `{ index }` 或者 `{ error }`。
|
|
3696
|
+
*/
|
|
3697
|
+
async function scriptTarget(ws, id, rawIndex) {
|
|
3698
|
+
if (novel.isValidChapterIndex(rawIndex) !== true) {
|
|
3699
|
+
return { error: '章号不对,没法知道你说的是哪一章。' }
|
|
3700
|
+
}
|
|
3701
|
+
const index = Number(String(rawIndex).trim())
|
|
3702
|
+
const book = await novel.readBook(ws, id)
|
|
3703
|
+
if (book.ok !== true) return { error: '这本书不在了。回到书架看一眼,可能被移走了。' }
|
|
3704
|
+
const exists = Array.isArray(book.chapters) && book.chapters.some((c) => Number(c.index) === index)
|
|
3705
|
+
if (!exists) {
|
|
3706
|
+
return { error: `这本书没有第 ${index} 章。先翻到真正的那一章再存。` }
|
|
3707
|
+
}
|
|
3708
|
+
return { index }
|
|
3709
|
+
}
|
|
3710
|
+
|
|
3255
3711
|
async function gatherCast(ws, wants) {
|
|
3256
3712
|
const out = []
|
|
3257
3713
|
const list = Array.isArray(wants) ? wants.slice(0, 8) : []
|
|
@@ -3515,6 +3971,351 @@ function blankPng() {
|
|
|
3515
3971
|
* 注册失败不抛:精简 profile 里没有 skills 服务时,应当只是少一个访谈
|
|
3516
3972
|
* skill,而不是整个插件起不来。
|
|
3517
3973
|
*/
|
|
3974
|
+
// ── 人物卡直接进系统提示 ─────────────────────────────────────────────
|
|
3975
|
+
//
|
|
3976
|
+
// 为什么不能靠工具结果:工具结果落在对话里,是「模型查到的资料」;预设和它
|
|
3977
|
+
// 一旦意见不合,模型会站在预设那边跟资料较劲——用户看到的就是「人设完全不
|
|
3978
|
+
// 像、道德感极强」。角色卡在酒馆类软件里从来不是资料,它就是这一场的
|
|
3979
|
+
// 系统提示本身(SillyTavern 的 char card 也是拼进 system 里的)。
|
|
3980
|
+
//
|
|
3981
|
+
// 所以这里把它做成一个 system prompt 段落:`text` 是函数,每次组装提示词都
|
|
3982
|
+
// 重新求值,所以「换个卡」「刚改完卡」下一句就生效,不用重开会话。
|
|
3983
|
+
//
|
|
3984
|
+
// `text` 必须同步返回,所以用 readFileSync + 按 mtime/size 缓存:同一份卡
|
|
3985
|
+
// 一万次组装也只读一次盘。
|
|
3986
|
+
|
|
3987
|
+
/** 小文件读盘缓存:同一份文件没改过就不再读。 */
|
|
3988
|
+
const personaCache = new Map()
|
|
3989
|
+
|
|
3990
|
+
function readCachedSync(abs) {
|
|
3991
|
+
try {
|
|
3992
|
+
const stat = statSync(abs)
|
|
3993
|
+
const hit = personaCache.get(abs)
|
|
3994
|
+
if (hit !== undefined && hit.mtimeMs === stat.mtimeMs && hit.size === stat.size) return hit.text
|
|
3995
|
+
const text = readFileSync(abs, 'utf8')
|
|
3996
|
+
personaCache.set(abs, { mtimeMs: stat.mtimeMs, size: stat.size, text })
|
|
3997
|
+
return text
|
|
3998
|
+
} catch {
|
|
3999
|
+
return null
|
|
4000
|
+
}
|
|
4001
|
+
}
|
|
4002
|
+
|
|
4003
|
+
/** 卡文件名:`people/<id>.card.md`。id 里可能有中文,不能直接拿来当路径猜。 */
|
|
4004
|
+
function cardPathSync(root, id) {
|
|
4005
|
+
const want = String(id ?? '').trim()
|
|
4006
|
+
if (want === '') return ''
|
|
4007
|
+
// 这一段是同步路径(system prompt 每次组装都要跑),所以用同步读的墓碑。
|
|
4008
|
+
// 不过墓碑的话,删掉的卡只要文件还在,就照样被注入进 system prompt——
|
|
4009
|
+
// 用户明明删了,模型却还在演那个人。
|
|
4010
|
+
const redactions = store.readRedactionsSync(root)
|
|
4011
|
+
const relOf = (abs) => path.relative(root, abs).split(path.sep).join('/')
|
|
4012
|
+
const gone = (abs, text) => store.hitsRedaction(redactions, relOf(abs), text) !== undefined
|
|
4013
|
+
const direct = path.join(root, 'people', `${want}.card.md`)
|
|
4014
|
+
try {
|
|
4015
|
+
if (statSync(direct).isFile()) {
|
|
4016
|
+
const text = readCachedSync(direct)
|
|
4017
|
+
if (text !== null && !gone(direct, text)) return direct
|
|
4018
|
+
}
|
|
4019
|
+
} catch {
|
|
4020
|
+
/* 名字不是文件名,退回去扫目录 */
|
|
4021
|
+
}
|
|
4022
|
+
try {
|
|
4023
|
+
const names = readdirSync(path.join(root, 'people')).filter((n) => n.endsWith('.card.md'))
|
|
4024
|
+
for (const name of names) {
|
|
4025
|
+
const abs = path.join(root, 'people', name)
|
|
4026
|
+
const text = readCachedSync(abs)
|
|
4027
|
+
if (text === null) continue
|
|
4028
|
+
if (gone(abs, text)) continue
|
|
4029
|
+
const parsed = core.parseCard(text)
|
|
4030
|
+
if (parsed.ok === true && String(parsed.data?.id ?? '') === want) return abs
|
|
4031
|
+
}
|
|
4032
|
+
} catch {
|
|
4033
|
+
/* 没有 people 目录就当没挑卡 */
|
|
4034
|
+
}
|
|
4035
|
+
return ''
|
|
4036
|
+
}
|
|
4037
|
+
|
|
4038
|
+
/**
|
|
4039
|
+
* 一张卡被撤掉以后,把三扇门(现实 / 酒馆 / 写小说)里指向它的那项清空。
|
|
4040
|
+
*
|
|
4041
|
+
* 不清的后果不是「少了一个人的设定」这么轻:那把门还记着一个不存在的名字,
|
|
4042
|
+
* 每次都白读一遍、拼出一段空的,用户看到的是「明明挑着人,模型却不知道自己演谁」。
|
|
4043
|
+
*/
|
|
4044
|
+
async function clearPinsFor(ws, rel, data) {
|
|
4045
|
+
const state = await store.readConfigState(ws)
|
|
4046
|
+
if (state.ok !== true) return
|
|
4047
|
+
const pins = { real: '', tavern: '', novel: '', ...(state.config?.default_cards ?? {}) }
|
|
4048
|
+
const id = String(data?.id ?? '')
|
|
4049
|
+
const stem = path.posix.basename(rel).replace(/\.card\.md$/i, '')
|
|
4050
|
+
let changed = false
|
|
4051
|
+
for (const mode of ['real', 'tavern', 'novel']) {
|
|
4052
|
+
const picked = String(pins[mode] ?? '')
|
|
4053
|
+
if (picked !== '' && (picked === id || picked === rel || picked === stem)) {
|
|
4054
|
+
pins[mode] = ''
|
|
4055
|
+
changed = true
|
|
4056
|
+
}
|
|
4057
|
+
}
|
|
4058
|
+
if (changed) await store.writeConfig(ws, { default_cards: pins })
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
/** 当前是哪张卡:配置里 default_cards[mode]。 */
|
|
4062
|
+
function pinnedCardIdSync(root, mode) {
|
|
4063
|
+
const raw = readCachedSync(path.join(root, 'lifecard.config.json'))
|
|
4064
|
+
if (raw === null) return ''
|
|
4065
|
+
try {
|
|
4066
|
+
const conf = JSON.parse(raw)
|
|
4067
|
+
const picked = conf?.default_cards?.[mode]
|
|
4068
|
+
return typeof picked === 'string' ? picked : ''
|
|
4069
|
+
} catch {
|
|
4070
|
+
return ''
|
|
4071
|
+
}
|
|
4072
|
+
}
|
|
4073
|
+
|
|
4074
|
+
/** 把世界书条目排成人能读、模型也好读的样子。 */
|
|
4075
|
+
function worldEntriesText(entries) {
|
|
4076
|
+
const list = Array.isArray(entries) ? entries : []
|
|
4077
|
+
if (list.length === 0) return ''
|
|
4078
|
+
return list
|
|
4079
|
+
.map((entry) => {
|
|
4080
|
+
const keys = Array.isArray(entry?.keys) ? entry.keys.filter((k) => typeof k === 'string' && k !== '') : []
|
|
4081
|
+
const content = String(entry?.content ?? '').trim()
|
|
4082
|
+
const head = keys.length === 0 ? '(一直生效)' : `关键词:${keys.join('、')}`
|
|
4083
|
+
return `- ${head}\n ${content.replace(/\n/g, '\n ')}`
|
|
4084
|
+
})
|
|
4085
|
+
.join('\n')
|
|
4086
|
+
}
|
|
4087
|
+
|
|
4088
|
+
/**
|
|
4089
|
+
* 这张卡是不是「提示词型」:自述很长,而且没有性格/开场白/对话示例。
|
|
4090
|
+
*
|
|
4091
|
+
* 这种卡(网上流传的破限卡、规范卡)整份就是给模型的硬性指令,没有人物细节;
|
|
4092
|
+
* 它的正确用法是**原样当规则读**,而不是当成人物描述塞在演法说明后面。
|
|
4093
|
+
*/
|
|
4094
|
+
export function cardIsPromptStyle(parsed) {
|
|
4095
|
+
const sections = Array.isArray(parsed?.sections) ? parsed.sections : []
|
|
4096
|
+
const self = core.sectionBody(sections, '自述')
|
|
4097
|
+
const selfLen = typeof self === 'string' ? self.trim().length : 0
|
|
4098
|
+
const others = ['性格', '开场白', '对话示例', '外貌']
|
|
4099
|
+
.map((title) => core.sectionBody(sections, title))
|
|
4100
|
+
.some((part) => typeof part === 'string' && part.trim() !== '')
|
|
4101
|
+
return selfLen >= 4000 && !others
|
|
4102
|
+
}
|
|
4103
|
+
|
|
4104
|
+
/**
|
|
4105
|
+
* 拼出这一场的人物卡段落。
|
|
4106
|
+
*
|
|
4107
|
+
* 不做任何删减:卡上写什么就是什么(用户说得对——删了就不是那张卡了)。
|
|
4108
|
+
* 只做两件必要的事:标题说清「你要演的是谁」,以及把世界书接在后面。
|
|
4109
|
+
*
|
|
4110
|
+
* @param {string} root 工作区绝对路径
|
|
4111
|
+
* @param {'tavern'|'novel'|'real'} mode 哪扇门
|
|
4112
|
+
* @returns {string} 空串表示这一场不挂卡
|
|
4113
|
+
*/
|
|
4114
|
+
export function buildPersonaSection(root, mode) {
|
|
4115
|
+
const id = pinnedCardIdSync(root, mode)
|
|
4116
|
+
if (id === '') return ''
|
|
4117
|
+
const abs = cardPathSync(root, id)
|
|
4118
|
+
if (abs === '') return ''
|
|
4119
|
+
const text = readCachedSync(abs)
|
|
4120
|
+
if (text === null) return ''
|
|
4121
|
+
const parsed = core.parseCard(text)
|
|
4122
|
+
if (parsed.ok !== true) return ''
|
|
4123
|
+
const data = parsed.data ?? {}
|
|
4124
|
+
const sections = Array.isArray(parsed.sections) ? parsed.sections : []
|
|
4125
|
+
const body = ['自述', '性格', '外貌', '口癖', '不会说的话', '不会做的事', '场景', '开场白', '对话示例']
|
|
4126
|
+
.map((title) => core.sectionBody(sections, title))
|
|
4127
|
+
.filter((part) => typeof part === 'string' && part.trim() !== '')
|
|
4128
|
+
.join('\n\n')
|
|
4129
|
+
if (body.trim() === '') return ''
|
|
4130
|
+
const name = String(data.name ?? '').trim() === '' ? id : String(data.name).trim()
|
|
4131
|
+
// 「提示词型」的卡:整份内容就是给模型下的硬性规则(网上下载的破限卡、规范卡多是这样),
|
|
4132
|
+
// 它没有性格/开场白,自述就是一大段 MUST/ALWAYS。这种卡和预设里的演法说明容易撞,
|
|
4133
|
+
// 撞的时候要以卡为准——否则用户下载了什么就等于白下。
|
|
4134
|
+
const promptStyle = cardIsPromptStyle(parsed)
|
|
4135
|
+
const out = [
|
|
4136
|
+
`# 你要演的人:${name}${String(data.relation ?? '').trim() === '' ? '' : `(${String(data.relation).trim()})`}`,
|
|
4137
|
+
'',
|
|
4138
|
+
promptStyle
|
|
4139
|
+
? '这张卡写的是硬性规矩(格式、口吻、怎么回),它本身就是这个人。以下是全部原文,一句不改;其中与别的说明冲突的地方,以这张卡为准。不要把它当成资料来引用,也不要在回答里提「卡上写着」。'
|
|
4140
|
+
: '下面这张卡就是这个人的全部设定,直接照它演——不要把它当成资料来引用,也不要在回答里提「卡上写着」。',
|
|
4141
|
+
'',
|
|
4142
|
+
body,
|
|
4143
|
+
]
|
|
4144
|
+
const world = String(data.world ?? '').trim()
|
|
4145
|
+
if (world !== '' && world !== 'reality') {
|
|
4146
|
+
const worldAbs = path.join(root, 'worlds', core.safeId(world), 'world.md')
|
|
4147
|
+
const worldText = readCachedSync(worldAbs)
|
|
4148
|
+
if (worldText !== null) {
|
|
4149
|
+
const worldParsed = core.parseCard(worldText)
|
|
4150
|
+
const entries = worldParsed.ok === true ? core.parseWorldEntries(worldParsed.sections ?? []) : []
|
|
4151
|
+
const rendered = worldEntriesText(entries)
|
|
4152
|
+
if (rendered !== '') out.push('', `# 这一场发生在:${String(worldParsed.data?.name ?? world).trim()}`, '', rendered)
|
|
4153
|
+
}
|
|
4154
|
+
}
|
|
4155
|
+
return out.join('\n')
|
|
4156
|
+
}
|
|
4157
|
+
|
|
4158
|
+
/** 把一个 agent 上跟「预设」有关的字段都抄出来,用来定位到底哪个字段才是真的。 */
|
|
4159
|
+
function shapeOfAgent(agent) {
|
|
4160
|
+
const pick = (obj, keys) => {
|
|
4161
|
+
const out = {}
|
|
4162
|
+
for (const key of keys) {
|
|
4163
|
+
const value = obj?.[key]
|
|
4164
|
+
if (value !== undefined && value !== null && typeof value !== 'object') out[key] = String(value).slice(0, 60)
|
|
4165
|
+
}
|
|
4166
|
+
return out
|
|
4167
|
+
}
|
|
4168
|
+
return {
|
|
4169
|
+
id: typeof agent?.id === 'string' ? agent.id : '',
|
|
4170
|
+
options: pick(agent?.options, ['agentPreset', 'preset', 'presetId', 'model', 'provider']),
|
|
4171
|
+
meta: pick(agent?.meta, ['agentPreset', 'preset', 'origin', 'delegationDepth', 'cwd']),
|
|
4172
|
+
header: pick(agent?.header, ['agentPreset', 'cwd']),
|
|
4173
|
+
sessionHeader: pick(agent?.session?.header, ['agentPreset', 'cwd', 'origin']),
|
|
4174
|
+
optionKeys: Object.keys(agent?.options ?? {}).slice(0, 20),
|
|
4175
|
+
metaKeys: Object.keys(agent?.meta ?? {}).slice(0, 20),
|
|
4176
|
+
}
|
|
4177
|
+
}
|
|
4178
|
+
|
|
4179
|
+
/** 人物卡注入的诊断日志。写文件而不是 console:宿主把插件的 stdout 丢进 /dev/null。 */
|
|
4180
|
+
function notePersona(step, detail) {
|
|
4181
|
+
try {
|
|
4182
|
+
const file = path.join(os.tmpdir(), 'lifecard-persona.log')
|
|
4183
|
+
const line = `${new Date().toISOString()} ${step} ${JSON.stringify(detail)}\n`
|
|
4184
|
+
appendFileSync(file, line)
|
|
4185
|
+
const size = statSync(file).size
|
|
4186
|
+
if (size > 200 * 1024) writeFileSync(file, line)
|
|
4187
|
+
} catch {
|
|
4188
|
+
/* 日志写不了不影响正事 */
|
|
4189
|
+
}
|
|
4190
|
+
}
|
|
4191
|
+
|
|
4192
|
+
/** 从会话标签里认出用的是哪个预设(不同版本字段位置不完全一样,挨个试)。 */
|
|
4193
|
+
function presetIdOfAgent(agent) {
|
|
4194
|
+
const spots = [
|
|
4195
|
+
agent?.options?.agentPreset,
|
|
4196
|
+
agent?.meta?.agentPreset,
|
|
4197
|
+
agent?.header?.agentPreset,
|
|
4198
|
+
agent?.session?.header?.agentPreset,
|
|
4199
|
+
agent?.info?.agentPreset,
|
|
4200
|
+
]
|
|
4201
|
+
for (const spot of spots) {
|
|
4202
|
+
if (typeof spot === 'string' && spot !== '') return spot
|
|
4203
|
+
}
|
|
4204
|
+
return ''
|
|
4205
|
+
}
|
|
4206
|
+
|
|
4207
|
+
/**
|
|
4208
|
+
* 从预设注册表里问「这个会话当初是按哪一份预设装的」。
|
|
4209
|
+
*
|
|
4210
|
+
* 为什么需要这条:DSH 顶部的预设是在会话建好之后才定下来的,
|
|
4211
|
+
* agent 上那些 agentPreset 字段实测会停在默认值(standard),
|
|
4212
|
+
* 直接读它会认错门,卡就永远进不去。
|
|
4213
|
+
* inspectCompositions(ctx) 的返回形状没有公开文档,所以这里只做一件事:
|
|
4214
|
+
* 把它序列化之后找 lc-tavern / lc-novel 这两个名字。
|
|
4215
|
+
*/
|
|
4216
|
+
function modeFromPresetRegistry(ctx, agent) {
|
|
4217
|
+
try {
|
|
4218
|
+
const registry = ctx?.get?.('agentPresets', false)
|
|
4219
|
+
if (registry === null || registry === undefined || typeof registry.inspectCompositions !== 'function') return ''
|
|
4220
|
+
const rows = registry.inspectCompositions(agent?.ctx) ?? []
|
|
4221
|
+
const blob = JSON.stringify(rows)
|
|
4222
|
+
if (blob.includes('lc-tavern')) return 'tavern'
|
|
4223
|
+
if (blob.includes('lc-novel')) return 'novel'
|
|
4224
|
+
} catch {
|
|
4225
|
+
/* 问不到就当不知道 */
|
|
4226
|
+
}
|
|
4227
|
+
return ''
|
|
4228
|
+
}
|
|
4229
|
+
|
|
4230
|
+
/** 这个 agent 用哪扇门的卡:只有 LC 那两个创作预设才挂,别的会话一个字都不注入。 */
|
|
4231
|
+
function modeForAgent(agent) {
|
|
4232
|
+
const preset = presetIdOfAgent(agent)
|
|
4233
|
+
if (preset === '') return ''
|
|
4234
|
+
if (preset.includes('lc-tavern')) return 'tavern'
|
|
4235
|
+
if (preset.includes('lc-novel')) return 'novel'
|
|
4236
|
+
return ''
|
|
4237
|
+
}
|
|
4238
|
+
|
|
4239
|
+
/** 子代理不挂人设:它不该以为自己是那个人。 */
|
|
4240
|
+
function isMainAgent(agent) {
|
|
4241
|
+
const origin = String(agent?.meta?.origin ?? agent?.session?.header?.origin ?? '')
|
|
4242
|
+
if (origin === 'subagent') return false
|
|
4243
|
+
const depth = Number(agent?.meta?.delegationDepth ?? agent?.session?.header?.delegationDepth ?? 0)
|
|
4244
|
+
return !(Number.isFinite(depth) && depth > 0)
|
|
4245
|
+
}
|
|
4246
|
+
|
|
4247
|
+
/**
|
|
4248
|
+
* 给一个会话挂上人物卡段落。返回 true 表示挂上了。
|
|
4249
|
+
*
|
|
4250
|
+
* 必须挂在 agent 自己的 ctx 上:段落按 scope 分发,注册在插件 ctx 上的段落
|
|
4251
|
+
* 落不到任何会话的组装里(上一版就这么写的,模型压根看不见卡)。
|
|
4252
|
+
*/
|
|
4253
|
+
export function attachCardToAgent(agent, options = {}) {
|
|
4254
|
+
const rootNow = typeof options.rootNow === 'function' ? options.rootNow : () => defaultWorkspace()
|
|
4255
|
+
const attached = options.attached !== null && typeof options.attached?.has === 'function' && typeof options.attached?.add === 'function' ? options.attached : null
|
|
4256
|
+
const note = typeof options.note === 'function' ? options.note : () => {}
|
|
4257
|
+
const pluginCtx = options.ctx ?? null
|
|
4258
|
+
try {
|
|
4259
|
+
if (agent === null || agent === undefined) return false
|
|
4260
|
+
const agentId = typeof agent.id === 'string' ? agent.id : ''
|
|
4261
|
+
if (attached !== null && attached.has(agent)) return false
|
|
4262
|
+
if (!isMainAgent(agent)) return false
|
|
4263
|
+
const agentCtx = agent.ctx
|
|
4264
|
+
if (agentCtx === null || agentCtx === undefined || typeof agentCtx.effect !== 'function') return false
|
|
4265
|
+
const prompt = agentCtx.get('systemPrompt', false)
|
|
4266
|
+
if (prompt === null || prompt === undefined || typeof prompt.section !== 'function') return false
|
|
4267
|
+
// 注意:这里**不**按预设提前返回。DSH 顶部的预设是在会话建好之后才通过
|
|
4268
|
+
// agentPresets.select 定下来的,创建那一刻 agent 上的预设字段还可能是默认值
|
|
4269
|
+
// (实测拿到的是 standard)。所以段落一律挂上,每次组装提示词时再按当下的
|
|
4270
|
+
// 预设判断要不要给内容——换预设立刻生效,也不用等重新建会话。
|
|
4271
|
+
let evaluated = 0
|
|
4272
|
+
let reported = false
|
|
4273
|
+
agentCtx.effect(
|
|
4274
|
+
() =>
|
|
4275
|
+
prompt.section({
|
|
4276
|
+
name: 'lifecard:card',
|
|
4277
|
+
order: (() => {
|
|
4278
|
+
try {
|
|
4279
|
+
const anchor = prompt.getSectionOrder('deployment:persona-prefix')
|
|
4280
|
+
if (typeof anchor === 'number' && Number.isFinite(anchor)) return anchor + 0.5
|
|
4281
|
+
} catch {
|
|
4282
|
+
/* 拿不到锚点就用一个靠前但又不抢位置的值 */
|
|
4283
|
+
}
|
|
4284
|
+
return 500
|
|
4285
|
+
})(),
|
|
4286
|
+
text: () => {
|
|
4287
|
+
try {
|
|
4288
|
+
const mode = modeForAgent(agent) || modeFromPresetRegistry(pluginCtx, agent)
|
|
4289
|
+
if (mode === '') {
|
|
4290
|
+
if (!reported) {
|
|
4291
|
+
reported = true
|
|
4292
|
+
notePersona('no-door', { agent: agentId, ...shapeOfAgent(agent) })
|
|
4293
|
+
}
|
|
4294
|
+
return ''
|
|
4295
|
+
}
|
|
4296
|
+
const text = buildPersonaSection(rootNow(), mode)
|
|
4297
|
+
if (evaluated < 3) {
|
|
4298
|
+
evaluated += 1
|
|
4299
|
+
notePersona('text', { agent: agentId, mode, chars: text.length, root: rootNow() })
|
|
4300
|
+
}
|
|
4301
|
+
return text
|
|
4302
|
+
} catch {
|
|
4303
|
+
// 提示词拼不出来也不能挡住这一句话。
|
|
4304
|
+
return ''
|
|
4305
|
+
}
|
|
4306
|
+
},
|
|
4307
|
+
interpolate: false,
|
|
4308
|
+
}),
|
|
4309
|
+
'lifecard: 人物卡段落',
|
|
4310
|
+
)
|
|
4311
|
+
if (attached !== null) attached.add(agent)
|
|
4312
|
+
return true
|
|
4313
|
+
} catch (error) {
|
|
4314
|
+
console.error('[lifecard] 人物卡段落没挂上:', error)
|
|
4315
|
+
return false
|
|
4316
|
+
}
|
|
4317
|
+
}
|
|
4318
|
+
|
|
3518
4319
|
function registerInterviewSkill(ctx) {
|
|
3519
4320
|
registerBundledSkill(ctx, {
|
|
3520
4321
|
dir: 'lifecard-interview',
|
|
@@ -3528,6 +4329,14 @@ function registerInterviewSkill(ctx) {
|
|
|
3528
4329
|
fallback: '中文小说与长文的写法:先分清用不用人物卡,按传统文学的标准写正文,要连载节奏就切。',
|
|
3529
4330
|
label: '写作',
|
|
3530
4331
|
})
|
|
4332
|
+
// 小说改剧本。「改剧本」按钮背后的提示词在 lib/novel.js 里,这份 skill 是给
|
|
4333
|
+
// 用户在对话里直接说「把这一章改成剧本」用的——两条路走的是同一套规矩。
|
|
4334
|
+
registerBundledSkill(ctx, {
|
|
4335
|
+
dir: 'novel-to-script',
|
|
4336
|
+
name: 'novel-to-script',
|
|
4337
|
+
fallback: '小说改剧本:分场、把叙述换成 △ 动作、把心理换成演得出来的东西、台词按标点分工。',
|
|
4338
|
+
label: '改剧本',
|
|
4339
|
+
})
|
|
3531
4340
|
// 这三份是写作用得上的通用手艺,随包一起发:装插件即等于有,
|
|
3532
4341
|
// 换台机器不必再单独装一遍。名字与内容都保持原样,方便用户照旧用
|
|
3533
4342
|
// `/human-writing`、`/blind-review-loop`、`/visual-novel` 唤起。
|
|
@@ -3678,12 +4487,70 @@ export function apply(ctx, config) {
|
|
|
3678
4487
|
remember: (ws) => rememberWorkspace(pointerFile, ws),
|
|
3679
4488
|
})
|
|
3680
4489
|
|
|
4490
|
+
// 系统提示段落里的 text() 必须同步返回,拿不到异步的 workspace()。
|
|
4491
|
+
// 所以这里留一份「当前库」的快照:先用默认值兜底,再异步校准一次;
|
|
4492
|
+
// 用户在界面上换文件夹时 settings.workspace 会跟着变,优先用那个。
|
|
4493
|
+
let liveRoot = defaultWorkspace()
|
|
4494
|
+
Promise.resolve()
|
|
4495
|
+
.then(() => workspace())
|
|
4496
|
+
.then((ws) => {
|
|
4497
|
+
if (typeof ws === 'string' && ws !== '') liveRoot = ws
|
|
4498
|
+
})
|
|
4499
|
+
.catch(() => {})
|
|
4500
|
+
const rootNow = () => {
|
|
4501
|
+
const configured = typeof settings.workspace === 'string' ? settings.workspace.trim() : ''
|
|
4502
|
+
return configured === '' ? liveRoot : configured
|
|
4503
|
+
}
|
|
4504
|
+
|
|
3681
4505
|
registerInterviewSkill(ctx)
|
|
3682
4506
|
|
|
3683
4507
|
// 把人物卡、世界书交给模型的那几个只读工具。挂在宿主这一层,所以不只是
|
|
3684
4508
|
// 「LC 酒馆 / LC 小说 / LC 现实」三个预设,任何预览里都能调。
|
|
3685
4509
|
// 用 ctx.inject 等 tools 服务就绪:apply 跑的时候它可能还没起来,
|
|
3686
4510
|
// 直接 ctx.get('tools') 会拿到 undefined,工具就静默少了。
|
|
4511
|
+
// 把挑好的那张卡拼进系统提示(不是对话里的资料)。
|
|
4512
|
+
//
|
|
4513
|
+
// 必须挂在 **agent 自己的 ctx** 上。段落是按 scope 分发的:注册在插件自己
|
|
4514
|
+
// 那个 ctx 上的段落,落不到任何一个会话的组装里——上一版就是这么写的,
|
|
4515
|
+
// 结果模型压根看不见卡,只能反问「演 rat 是什么意思」,然后去翻工具。
|
|
4516
|
+
// `text` 传函数,每轮组装都会重新求值,所以换卡/改卡下一句就生效。
|
|
4517
|
+
// 去重按 **agent 对象**,不是按会话 id:在顶部换预设时 DSH 会给同一个会话
|
|
4518
|
+
// 重新建一个 agent(新对象、新的 meta),按 id 去重会把换过预设的会话锁死在
|
|
4519
|
+
// 「第一次那个 agent 认成 standard」的结果上。换对象就重新挂一次。
|
|
4520
|
+
const attachedAgents = new WeakSet()
|
|
4521
|
+
const attachCard = (agent) => {
|
|
4522
|
+
const ok = attachCardToAgent(agent, { rootNow, attached: attachedAgents, note: notePersona, ctx })
|
|
4523
|
+
if (ok !== true) notePersona('skip', shapeOfAgent(agent))
|
|
4524
|
+
return ok
|
|
4525
|
+
}
|
|
4526
|
+
|
|
4527
|
+
// agent/created 是 serial 事件:这里同步抛错会否决整个会话的创建,
|
|
4528
|
+
// 所以宁可没人设,也绝不能让用户开不了会话。
|
|
4529
|
+
try {
|
|
4530
|
+
ctx.on('agent/created', (event) => {
|
|
4531
|
+
try {
|
|
4532
|
+
attachCard(event?.agent)
|
|
4533
|
+
} catch (error) {
|
|
4534
|
+
console.error('[lifecard] agent/created 处理失败:', error)
|
|
4535
|
+
}
|
|
4536
|
+
})
|
|
4537
|
+
} catch (error) {
|
|
4538
|
+
console.error('[lifecard] 没能监听 agent/created:', error)
|
|
4539
|
+
}
|
|
4540
|
+
// 已经在跑的会话不会再发 agent/created(插件重载、宿主启动时已存在的会话),
|
|
4541
|
+
// 所以要再扫一遍。
|
|
4542
|
+
if (typeof ctx.inject === 'function') {
|
|
4543
|
+
ctx.inject(['agents'], (agentsCtx) => {
|
|
4544
|
+
try {
|
|
4545
|
+
const registry = agentsCtx.get('agents', false)
|
|
4546
|
+
const list = typeof registry?.list === 'function' ? registry.list() : []
|
|
4547
|
+
for (const agent of list) attachCard(agent)
|
|
4548
|
+
} catch (error) {
|
|
4549
|
+
console.error('[lifecard] 补挂已有会话失败:', error)
|
|
4550
|
+
}
|
|
4551
|
+
})
|
|
4552
|
+
}
|
|
4553
|
+
|
|
3687
4554
|
if (typeof ctx.inject === 'function') {
|
|
3688
4555
|
ctx.inject(['tools'], (toolCtx) => {
|
|
3689
4556
|
try {
|