@furongjun1999/dsh-memory 0.5.0 → 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 +552 -465
- package/docs/README.md +1 -0
- package/docs/eval/bench_lingshu_self/bench_self.py +140 -0
- package/docs/eval/bench_lingshu_self/self_bench_result.json +404 -0
- package/docs/eval/bench_lingshu_self//347/201/265/346/236/242/350/207/252/345/272/223/347/253/257/345/210/260/347/253/257/346/243/200/347/264/242/345/256/236/346/265/213_v1.0.md +38 -0
- package/docs/eval//344/270/215/345/217/257/351/235/240/346/200/247/350/220/275/345/234/260_P0_v1.0.md +185 -0
- package/docs/eval//346/225/205/351/232/234/346/263/250/345/205/245/345/256/236/346/265/213_v1.0.md +422 -0
- package/docs/eval//347/253/257/345/210/260/347/253/257LoCoMoQA/345/220/214/345/217/243/345/276/204/345/257/271/347/205/247_v1.0.md +100 -0
- package/docs/eval//347/253/257/345/210/260/347/253/257/345/271/262/346/211/260/346/261/240/350/257/204/346/265/213_/347/241/256/345/256/232/346/200/247/350/243/201/345/206/263vsLLM_judge_v1.1.md +197 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v1.md +156 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v10.md +210 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v11.md +227 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v12.md +203 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v13.md +233 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v14.md +191 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v15.md +213 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v16.md +214 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v17.md +199 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v2.md +156 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v3.md +152 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v4.md +128 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v5.md +114 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v6.md +192 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v7.md +187 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v8.md +207 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v9.md +203 -0
- package/docs/hive//345/256/211/345/205/250/345/256/241/350/256/241/345/256/236/351/224/232_v0.1.md +202 -0
- package/docs/hive//350/234/202/345/267/242/345/217/214/345/256/236/344/276/213/344/272/222/351/252/214_/350/256/276/350/256/241/345/256/232/347/250/277.md +19 -3
- package/docs/images/lingshu-moonlight-covenant-poster-preview.jpg +0 -0
- package/docs/images/lingshu-moonlight-covenant-poster.png +0 -0
- package/docs/mdcg//345/212/237/350/203/275/350/260/203/347/224/250/346/230/240/345/260/204/350/241/250_v0.1.md +40 -40
- package/docs/theory//344/270/215/345/217/257/351/235/240/345/256/232/347/220/206/344/270/216/345/244/261/346/225/210/344/274/230/345/205/210/346/241/206/346/236/266_v0.3.md +365 -0
- package/docs//344/270/215/345/217/257/351/235/240/346/200/247/347/220/206/350/256/272_v0.1.md +343 -0
- package/lib/bridge.d.ts +9 -0
- package/lib/bridge.js +35 -0
- package/lib/index.js +7 -1
- package/lib/lib/roleplay_web.js +530 -443
- package/lib/lib/token_store.d.ts +7 -1
- package/lib/lib/token_store.js +12 -3
- package/md_cg/audit.py +12 -1
- package/md_cg/backfill.py +16 -15
- package/md_cg/backfill_bucket_zh.py +35 -0
- package/md_cg/bench_e2e_judge.py +532 -0
- package/md_cg/bench_e2e_locomo_qa.py +368 -0
- package/md_cg/bench_e2e_qa.py +256 -0
- package/md_cg/branches.py +18 -2
- package/md_cg/ccgc.py +3 -2
- package/md_cg/chain.py +19 -4
- package/md_cg/consolidate.py +7 -6
- package/md_cg/crosscheck.py +4 -3
- package/md_cg/crypto.py +439 -437
- package/md_cg/datapath.py +395 -335
- package/md_cg/evidence.py +582 -580
- package/md_cg/export.py +3 -1
- package/md_cg/forgetting.py +2 -2
- package/md_cg/fsutil.py +48 -0
- package/md_cg/hotcache.py +255 -238
- package/md_cg/interop.py +161 -22
- package/md_cg/judgment_manifest.py +177 -0
- package/md_cg/links.py +655 -622
- package/md_cg/mcp_server.py +280 -29
- package/md_cg/mdcg.py +616 -125
- package/md_cg/mdcos.py +430 -66
- package/md_cg/mreview/govern.py +5 -4
- package/md_cg/postings.py +4 -2
- package/md_cg/readcache.py +76 -18
- package/md_cg/reconcile.py +228 -0
- package/md_cg/review_cli.py +170 -0
- package/md_cg/routing.py +28 -0
- package/md_cg/run_tests.py +211 -0
- package/md_cg/scrub.py +862 -852
- package/md_cg/security.py +385 -275
- package/md_cg/selfreport.py +3 -2
- package/md_cg/signer.py +565 -562
- package/md_cg/sources.py +3 -2
- package/md_cg/stg.py +6 -0
- package/md_cg/sustain.py +1168 -1138
- package/md_cg/test_access_hints.py +147 -0
- package/md_cg/test_branch_discard_tombstone.py +136 -0
- package/md_cg/test_branches.py +259 -249
- package/md_cg/test_chain_read_isolate.py +168 -0
- package/md_cg/test_datapath_device_name.py +203 -0
- package/md_cg/test_emit_negtail_cache.py +156 -0
- package/md_cg/test_en_pipeline.py +186 -166
- package/md_cg/test_govern_directread.py +421 -0
- package/md_cg/test_i32_hotcache_env_key.py +218 -0
- package/md_cg/test_identity_attribution.py +228 -147
- package/md_cg/test_index_durability.py +238 -224
- package/md_cg/test_interop.py +4 -2
- package/md_cg/test_interop_judgment.py +228 -0
- package/md_cg/test_issue39_utf8_stdio.py +273 -0
- package/md_cg/test_links_concurrent_write.py +188 -0
- package/md_cg/test_merge_upsert.py +168 -0
- package/md_cg/test_n123_derive_expiry_chain.py +205 -0
- package/md_cg/test_n130_verify_falsified_protect.py +185 -0
- package/md_cg/test_n131_merge_gate.py +205 -0
- package/md_cg/test_p1x_ref_root.py +160 -0
- package/md_cg/test_p27_docindex.py +774 -765
- package/md_cg/test_p2_mcp.py +3 -0
- package/md_cg/test_p32_backfill.py +304 -298
- package/md_cg/test_p39_verify_flow.py +90 -50
- package/md_cg/test_p47_session_view.py +60 -25
- package/md_cg/test_propose_tail_index.py +157 -0
- package/md_cg/test_read_scope_b27.py +277 -0
- package/md_cg/test_readcache_default_on.py +168 -0
- package/md_cg/test_readcache_precise_inval.py +270 -0
- package/md_cg/test_readcache_prodpath.py +55 -7
- package/md_cg/test_reconcile_v0.py +294 -0
- package/md_cg/test_retr_s1.py +6 -2
- package/md_cg/test_retr_s1b.py +67 -0
- package/md_cg/test_retr_s7.py +8 -0
- package/md_cg/test_retr_s9_entity_ctx.py +181 -175
- package/md_cg/test_retr_score_once.py +208 -0
- package/md_cg/test_review_onepass.py +170 -0
- package/md_cg/test_rrf_graph_seed_cache.py +154 -0
- package/md_cg/test_security_audit.py +155 -0
- package/md_cg/test_security_audit_b26.py +161 -0
- package/md_cg/test_security_audit_v21.py +250 -0
- package/md_cg/test_semantic_canonical.py +255 -241
- package/md_cg/test_session_isolation.py +168 -0
- package/md_cg/test_snapshot_autoclose.py +187 -0
- package/md_cg/test_tail_watermark_race.py +208 -0
- package/md_cg/test_tenant_env_override_warn.py +139 -0
- package/md_cg/test_tenant_registry_corrupt_warn.py +151 -0
- package/md_cg/test_v14_fixes.py +415 -397
- package/md_cg/test_verify_dirty_reconcile.py +157 -0
- package/md_cg/theory.py +276 -273
- package/md_cg/tokens.py +734 -677
- package/md_cg/units.py +3 -2
- package/md_cg/vision_evidence.py +4 -3
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/md_cg/whitebox_kb/wisdom/multilang_ir.py +128 -124
- package/md_cg/writepipe.py +554 -550
- package/package.json +6 -2
- package/src/bridge.ts +434 -401
- package/src/index.ts +526 -518
- package/src/lib/roleplay_web.ts +1019 -932
- package/src/lib/token_store.ts +202 -192
package/lib/lib/roleplay_web.js
CHANGED
|
@@ -13,433 +13,463 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { join, dirname } from 'node:path';
|
|
15
15
|
import { readFileSync, mkdirSync, appendFileSync, existsSync, writeFileSync, renameSync } from 'node:fs';
|
|
16
|
-
const HTML = `<!doctype html>
|
|
17
|
-
<html lang="zh-CN">
|
|
18
|
-
<head>
|
|
19
|
-
<meta charset="utf-8">
|
|
20
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
21
|
-
<title>灵枢 · 角色扮演</title>
|
|
22
|
-
<style>
|
|
23
|
-
:root { --bg:#12141a; --card:#1c2029; --line:#2a3040; --txt:#e8eaf0; --sub:#9aa3b5; --accent:#7c8cff; --accent2:#5ad1a8; }
|
|
24
|
-
* { box-sizing:border-box; margin:0; padding:0; }
|
|
25
|
-
body { background:var(--bg); color:var(--txt); font-family:"PingFang SC","Noto Sans SC",system-ui,sans-serif; height:100vh; height:100dvh; display:flex; flex-direction:column; overflow:hidden; }
|
|
26
|
-
header { flex:none; padding:14px 18px; border-bottom:1px solid var(--line); display:flex; align-items:center; gap:12px; }
|
|
27
|
-
header h1 { font-size:17px; font-weight:600; }
|
|
28
|
-
header .badge { font-size:11px; color:var(--accent2); border:1px solid var(--accent2); padding:2px 8px; border-radius:999px; }
|
|
29
|
-
#roles { margin-left:auto; display:flex; gap:8px; align-items:center; }
|
|
30
|
-
#roles select { background:var(--card); color:var(--txt); border:1px solid var(--line); border-radius:8px; padding:6px 10px; font-size:13px; }
|
|
31
|
-
#roles button { background:var(--card); color:var(--accent); border:1px solid var(--line); border-radius:8px; padding:6px 10px; font-size:13px; cursor:pointer; }
|
|
32
|
-
#roles button:hover { border-color:var(--accent); }
|
|
33
|
-
main { flex:1; min-height:0; overflow-y:auto; padding:16px 18px; display:flex; flex-direction:column; gap:12px; }
|
|
34
|
-
.msg { max-width:78%; padding:10px 14px; border-radius:14px; line-height:1.65; font-size:14px; white-space:pre-wrap; word-break:break-word; }
|
|
35
|
-
.user { align-self:flex-end; background:var(--accent); color:#0b0d14; border-bottom-right-radius:4px; }
|
|
36
|
-
.bot { align-self:flex-start; background:var(--card); border:1px solid var(--line); border-bottom-left-radius:4px; }
|
|
37
|
-
.bot .route { display:block; font-size:10px; color:var(--sub); margin-top:6px; }
|
|
38
|
-
.typing { color:var(--sub); font-size:13px; padding:6px 14px 4px; display:none; flex:none; }
|
|
39
|
-
footer { flex:none; margin:4px 12px calc(env(safe-area-inset-bottom, 0px) + 24px); background:var(--card); border:1px solid var(--line); border-radius:16px; padding:10px; display:flex; gap:8px; align-items:flex-end; box-shadow:0 8px 28px rgba(0,0,0,.4); }
|
|
40
|
-
footer.typing-center { position:fixed; top:45%; left:12px; right:12px; transform:translateY(-50%); z-index:100; margin:0; }
|
|
41
|
-
footer textarea { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:12px; padding:12px 14px; font-size:15px; line-height:1.55; outline:none; resize:none; min-height:46px; max-height:140px; font-family:inherit; }
|
|
42
|
-
footer textarea:focus { border-color:var(--accent); }
|
|
43
|
-
footer button { background:var(--accent); border:none; color:#0b0d14; border-radius:12px; padding:13px 20px; font-size:15px; font-weight:600; cursor:pointer; flex:none; }
|
|
44
|
-
footer button:disabled { opacity:.5; cursor:wait; }
|
|
45
|
-
#newrole { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:10; }
|
|
46
|
-
#newrole .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(420px,90vw); display:flex; flex-direction:column; gap:10px; }
|
|
47
|
-
#newrole h3 { font-size:15px; }
|
|
48
|
-
#newrole input, #newrole textarea { background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:8px 10px; font-size:13px; font-family:inherit; }
|
|
49
|
-
#newrole textarea { height:80px; resize:vertical; }
|
|
50
|
-
#newrole .row { display:flex; gap:8px; justify-content:flex-end; }
|
|
51
|
-
#newrole .row button { padding:6px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
52
|
-
#newrole .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
53
|
-
#transpanel { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:20; }
|
|
54
|
-
#transpanel .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(480px,92vw); max-height:80vh; overflow-y:auto; display:flex; flex-direction:column; gap:10px; }
|
|
55
|
-
#transpanel h3 { font-size:15px; }
|
|
56
|
-
#transpanel .mode-row { display:flex; flex-direction:column; gap:6px; font-size:13px; color:var(--txt); }
|
|
57
|
-
#transpanel .mode-row label { display:flex; gap:6px; align-items:flex-start; line-height:1.5; }
|
|
58
|
-
#transpanel .tp-row { display:flex; gap:6px; align-items:center; }
|
|
59
|
-
#transpanel .tp-row input { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:7px 9px; font-size:13px; font-family:inherit; min-width:0; }
|
|
60
|
-
#transpanel .tp-row .tp-note { flex:1.2; }
|
|
61
|
-
#transpanel .tp-row .tp-del { flex:none; background:none; border:none; color:var(--sub); font-size:16px; cursor:pointer; padding:4px 6px; }
|
|
62
|
-
#transpanel .row button { padding:7px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
63
|
-
#transpanel .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
64
|
-
/* —— 年龄确认(NSFW 门控)—— */
|
|
65
|
-
#agegate { position:fixed; inset:0; background:rgba(0,0,0,.72); display:none; align-items:center; justify-content:center; z-index:100; }
|
|
66
|
-
#agegate .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:22px; width:min(460px,90vw); display:flex; flex-direction:column; gap:12px; }
|
|
67
|
-
#agegate h3 { font-size:15px; color:var(--accent2); }
|
|
68
|
-
#agegate .warn { font-size:13px; line-height:1.7; color:var(--txt); }
|
|
69
|
-
#agegate .warn b { color:#ff9a9a; }
|
|
70
|
-
#agegate .row { display:flex; gap:8px; justify-content:flex-end; }
|
|
71
|
-
#agegate .row button { padding:8px 16px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
72
|
-
#agegate .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
73
|
-
/* —— 角色详情 / 三导入 —— */
|
|
74
|
-
#roledetail { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:30; }
|
|
75
|
-
#roledetail .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(520px,94vw); max-height:82vh; overflow-y:auto; display:flex; flex-direction:column; gap:10px; }
|
|
76
|
-
#roledetail h3 { font-size:15px; }
|
|
77
|
-
#roledetail .meta input, #roledetail .meta textarea { width:100%; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:8px 10px; font-size:13px; font-family:inherit; }
|
|
78
|
-
#roledetail .meta textarea { height:56px; resize:vertical; }
|
|
79
|
-
#roledetail .tabs { display:flex; gap:6px; }
|
|
80
|
-
#roledetail .tabs button { flex:1; padding:7px 0; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--sub); }
|
|
81
|
-
#roledetail .tabs button.on { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
82
|
-
#rd_items { display:flex; flex-direction:column; gap:8px; max-height:36vh; overflow-y:auto; }
|
|
83
|
-
#rd_items .rd-row { display:flex; flex-direction:column; gap:4px; background:var(--bg); border:1px solid var(--line); border-radius:8px; padding:8px 10px; }
|
|
84
|
-
#rd_items .rd-row textarea { background:transparent; border:none; color:var(--txt); font-size:13px; font-family:inherit; resize:vertical; min-height:40px; outline:none; }
|
|
85
|
-
#rd_items .rd-row .rd-sub { display:flex; gap:6px; }
|
|
86
|
-
#rd_items .rd-row .rd-sub input { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:6px; padding:4px 8px; font-size:12px; font-family:inherit; }
|
|
87
|
-
#rd_items .rd-row .rd-sub .rd-del { flex:none; background:none; border:none; color:var(--sub); font-size:14px; cursor:pointer; }
|
|
88
|
-
#roledetail .row { display:flex; gap:8px; justify-content:flex-end; margin-top:6px; }
|
|
89
|
-
#roledetail .row button { padding:7px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
90
|
-
#roledetail .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
91
|
-
#rd_nsfw { display:flex; align-items:center; gap:8px; font-size:12px; color:var(--sub); }
|
|
92
|
-
#rd_nsfw input { accent-color:var(--accent); }
|
|
93
|
-
.hint { color:var(--sub); font-size:12px; text-align:center; padding:6px; }
|
|
94
|
-
</style>
|
|
95
|
-
</head>
|
|
96
|
-
<body>
|
|
97
|
-
<header>
|
|
98
|
-
<h1>灵枢 · 角色扮演</h1><span class="badge">白箱 · 扮演论 v3.3</span>
|
|
99
|
-
<div id="roles">
|
|
100
|
-
<select id="roleSel"></select>
|
|
101
|
-
<button onclick="showNewRole()">+ 新角色</button>
|
|
102
|
-
<button onclick="openRoleDetail()">⚙ 详情</button>
|
|
103
|
-
<button onclick="openTrans()">🌐 翻译</button>
|
|
104
|
-
|
|
105
|
-
</
|
|
106
|
-
|
|
107
|
-
<
|
|
108
|
-
<
|
|
109
|
-
|
|
110
|
-
<
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
<
|
|
116
|
-
<input id="
|
|
117
|
-
<input id="
|
|
118
|
-
<input id="
|
|
119
|
-
<
|
|
120
|
-
|
|
121
|
-
</div>
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
<
|
|
126
|
-
|
|
127
|
-
<label><input type="radio" name="tpmode" value="
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
<div
|
|
131
|
-
|
|
132
|
-
<button
|
|
133
|
-
<button onclick="
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
</div>
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
<
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
</div>
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
<
|
|
154
|
-
|
|
155
|
-
<
|
|
156
|
-
<textarea id="
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
<button id="tab-
|
|
161
|
-
<button id="tab-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
<div
|
|
165
|
-
|
|
166
|
-
<
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
<button
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
</div>
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
//
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
} catch (e) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
if (
|
|
294
|
-
|
|
295
|
-
const
|
|
296
|
-
$('
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
//
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
input.
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
16
|
+
const HTML = `<!doctype html>
|
|
17
|
+
<html lang="zh-CN">
|
|
18
|
+
<head>
|
|
19
|
+
<meta charset="utf-8">
|
|
20
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
21
|
+
<title>灵枢 · 角色扮演</title>
|
|
22
|
+
<style>
|
|
23
|
+
:root { --bg:#12141a; --card:#1c2029; --line:#2a3040; --txt:#e8eaf0; --sub:#9aa3b5; --accent:#7c8cff; --accent2:#5ad1a8; }
|
|
24
|
+
* { box-sizing:border-box; margin:0; padding:0; }
|
|
25
|
+
body { background:var(--bg); color:var(--txt); font-family:"PingFang SC","Noto Sans SC",system-ui,sans-serif; height:100vh; height:100dvh; display:flex; flex-direction:column; overflow:hidden; }
|
|
26
|
+
header { flex:none; padding:14px 18px; border-bottom:1px solid var(--line); display:flex; align-items:center; gap:12px; }
|
|
27
|
+
header h1 { font-size:17px; font-weight:600; }
|
|
28
|
+
header .badge { font-size:11px; color:var(--accent2); border:1px solid var(--accent2); padding:2px 8px; border-radius:999px; }
|
|
29
|
+
#roles { margin-left:auto; display:flex; gap:8px; align-items:center; }
|
|
30
|
+
#roles select { background:var(--card); color:var(--txt); border:1px solid var(--line); border-radius:8px; padding:6px 10px; font-size:13px; }
|
|
31
|
+
#roles button { background:var(--card); color:var(--accent); border:1px solid var(--line); border-radius:8px; padding:6px 10px; font-size:13px; cursor:pointer; }
|
|
32
|
+
#roles button:hover { border-color:var(--accent); }
|
|
33
|
+
main { flex:1; min-height:0; overflow-y:auto; padding:16px 18px; display:flex; flex-direction:column; gap:12px; }
|
|
34
|
+
.msg { max-width:78%; padding:10px 14px; border-radius:14px; line-height:1.65; font-size:14px; white-space:pre-wrap; word-break:break-word; }
|
|
35
|
+
.user { align-self:flex-end; background:var(--accent); color:#0b0d14; border-bottom-right-radius:4px; }
|
|
36
|
+
.bot { align-self:flex-start; background:var(--card); border:1px solid var(--line); border-bottom-left-radius:4px; }
|
|
37
|
+
.bot .route { display:block; font-size:10px; color:var(--sub); margin-top:6px; }
|
|
38
|
+
.typing { color:var(--sub); font-size:13px; padding:6px 14px 4px; display:none; flex:none; }
|
|
39
|
+
footer { flex:none; margin:4px 12px calc(env(safe-area-inset-bottom, 0px) + 24px); background:var(--card); border:1px solid var(--line); border-radius:16px; padding:10px; display:flex; gap:8px; align-items:flex-end; box-shadow:0 8px 28px rgba(0,0,0,.4); }
|
|
40
|
+
footer.typing-center { position:fixed; top:45%; left:12px; right:12px; transform:translateY(-50%); z-index:100; margin:0; }
|
|
41
|
+
footer textarea { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:12px; padding:12px 14px; font-size:15px; line-height:1.55; outline:none; resize:none; min-height:46px; max-height:140px; font-family:inherit; }
|
|
42
|
+
footer textarea:focus { border-color:var(--accent); }
|
|
43
|
+
footer button { background:var(--accent); border:none; color:#0b0d14; border-radius:12px; padding:13px 20px; font-size:15px; font-weight:600; cursor:pointer; flex:none; }
|
|
44
|
+
footer button:disabled { opacity:.5; cursor:wait; }
|
|
45
|
+
#newrole { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:10; }
|
|
46
|
+
#newrole .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(420px,90vw); display:flex; flex-direction:column; gap:10px; }
|
|
47
|
+
#newrole h3 { font-size:15px; }
|
|
48
|
+
#newrole input, #newrole textarea { background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:8px 10px; font-size:13px; font-family:inherit; }
|
|
49
|
+
#newrole textarea { height:80px; resize:vertical; }
|
|
50
|
+
#newrole .row { display:flex; gap:8px; justify-content:flex-end; }
|
|
51
|
+
#newrole .row button { padding:6px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
52
|
+
#newrole .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
53
|
+
#transpanel { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:20; }
|
|
54
|
+
#transpanel .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(480px,92vw); max-height:80vh; overflow-y:auto; display:flex; flex-direction:column; gap:10px; }
|
|
55
|
+
#transpanel h3 { font-size:15px; }
|
|
56
|
+
#transpanel .mode-row { display:flex; flex-direction:column; gap:6px; font-size:13px; color:var(--txt); }
|
|
57
|
+
#transpanel .mode-row label { display:flex; gap:6px; align-items:flex-start; line-height:1.5; }
|
|
58
|
+
#transpanel .tp-row { display:flex; gap:6px; align-items:center; }
|
|
59
|
+
#transpanel .tp-row input { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:7px 9px; font-size:13px; font-family:inherit; min-width:0; }
|
|
60
|
+
#transpanel .tp-row .tp-note { flex:1.2; }
|
|
61
|
+
#transpanel .tp-row .tp-del { flex:none; background:none; border:none; color:var(--sub); font-size:16px; cursor:pointer; padding:4px 6px; }
|
|
62
|
+
#transpanel .row button { padding:7px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
63
|
+
#transpanel .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
64
|
+
/* —— 年龄确认(NSFW 门控)—— */
|
|
65
|
+
#agegate { position:fixed; inset:0; background:rgba(0,0,0,.72); display:none; align-items:center; justify-content:center; z-index:100; }
|
|
66
|
+
#agegate .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:22px; width:min(460px,90vw); display:flex; flex-direction:column; gap:12px; }
|
|
67
|
+
#agegate h3 { font-size:15px; color:var(--accent2); }
|
|
68
|
+
#agegate .warn { font-size:13px; line-height:1.7; color:var(--txt); }
|
|
69
|
+
#agegate .warn b { color:#ff9a9a; }
|
|
70
|
+
#agegate .row { display:flex; gap:8px; justify-content:flex-end; }
|
|
71
|
+
#agegate .row button { padding:8px 16px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
72
|
+
#agegate .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
73
|
+
/* —— 角色详情 / 三导入 —— */
|
|
74
|
+
#roledetail { position:fixed; inset:0; background:rgba(0,0,0,.6); display:none; align-items:center; justify-content:center; z-index:30; }
|
|
75
|
+
#roledetail .box { background:var(--card); border:1px solid var(--line); border-radius:14px; padding:20px; width:min(520px,94vw); max-height:82vh; overflow-y:auto; display:flex; flex-direction:column; gap:10px; }
|
|
76
|
+
#roledetail h3 { font-size:15px; }
|
|
77
|
+
#roledetail .meta input, #roledetail .meta textarea { width:100%; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:8px; padding:8px 10px; font-size:13px; font-family:inherit; }
|
|
78
|
+
#roledetail .meta textarea { height:56px; resize:vertical; }
|
|
79
|
+
#roledetail .tabs { display:flex; gap:6px; }
|
|
80
|
+
#roledetail .tabs button { flex:1; padding:7px 0; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--sub); }
|
|
81
|
+
#roledetail .tabs button.on { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
82
|
+
#rd_items { display:flex; flex-direction:column; gap:8px; max-height:36vh; overflow-y:auto; }
|
|
83
|
+
#rd_items .rd-row { display:flex; flex-direction:column; gap:4px; background:var(--bg); border:1px solid var(--line); border-radius:8px; padding:8px 10px; }
|
|
84
|
+
#rd_items .rd-row textarea { background:transparent; border:none; color:var(--txt); font-size:13px; font-family:inherit; resize:vertical; min-height:40px; outline:none; }
|
|
85
|
+
#rd_items .rd-row .rd-sub { display:flex; gap:6px; }
|
|
86
|
+
#rd_items .rd-row .rd-sub input { flex:1; background:var(--bg); border:1px solid var(--line); color:var(--txt); border-radius:6px; padding:4px 8px; font-size:12px; font-family:inherit; }
|
|
87
|
+
#rd_items .rd-row .rd-sub .rd-del { flex:none; background:none; border:none; color:var(--sub); font-size:14px; cursor:pointer; }
|
|
88
|
+
#roledetail .row { display:flex; gap:8px; justify-content:flex-end; margin-top:6px; }
|
|
89
|
+
#roledetail .row button { padding:7px 14px; border-radius:8px; font-size:13px; cursor:pointer; border:1px solid var(--line); background:var(--card); color:var(--txt); }
|
|
90
|
+
#roledetail .row .ok { background:var(--accent); color:#0b0d14; border-color:var(--accent); font-weight:600; }
|
|
91
|
+
#rd_nsfw { display:flex; align-items:center; gap:8px; font-size:12px; color:var(--sub); }
|
|
92
|
+
#rd_nsfw input { accent-color:var(--accent); }
|
|
93
|
+
.hint { color:var(--sub); font-size:12px; text-align:center; padding:6px; }
|
|
94
|
+
</style>
|
|
95
|
+
</head>
|
|
96
|
+
<body>
|
|
97
|
+
<header>
|
|
98
|
+
<h1>灵枢 · 角色扮演</h1><span class="badge">白箱 · 扮演论 v3.3</span>
|
|
99
|
+
<div id="roles">
|
|
100
|
+
<select id="roleSel"></select>
|
|
101
|
+
<button onclick="showNewRole()">+ 新角色</button>
|
|
102
|
+
<button onclick="openRoleDetail()">⚙ 详情</button>
|
|
103
|
+
<button onclick="openTrans()">🌐 翻译</button>
|
|
104
|
+
<button onclick="setEditKey()" title="编辑密钥:服务端 ROLEPLAY_EDIT_KEY 的值(创建/保存角色与翻译需要;留空清除)">🔑</button>
|
|
105
|
+
</div>
|
|
106
|
+
</header>
|
|
107
|
+
<main id="chat"><div class="hint">选择角色,开始对话。角色回应由灵枢角色扮演引擎生成(白箱优先,LLM 续答)。</div></main>
|
|
108
|
+
<div class="typing" id="typing">正在思考…</div>
|
|
109
|
+
<footer>
|
|
110
|
+
<textarea id="input" rows="1" placeholder="说点什么…(Enter 发送,Shift+Enter 换行)"></textarea>
|
|
111
|
+
<button id="send" onclick="send()">发送</button>
|
|
112
|
+
</footer>
|
|
113
|
+
<div id="newrole">
|
|
114
|
+
<div class="box">
|
|
115
|
+
<h3>创建新角色</h3>
|
|
116
|
+
<input id="nr_id" placeholder="角色 ID(英文/拼音,如 naxiaoda)">
|
|
117
|
+
<input id="nr_name" placeholder="角色名(如 纳西妲)">
|
|
118
|
+
<input id="nr_scenario" placeholder="人设场景(一句话描述性格与背景)">
|
|
119
|
+
<input id="nr_first" placeholder="开场白(可选)">
|
|
120
|
+
<div class="row"><button onclick="closeNewRole()">取消</button><button class="ok" onclick="createRole()">创建</button></div>
|
|
121
|
+
</div>
|
|
122
|
+
</div>
|
|
123
|
+
<div id="transpanel">
|
|
124
|
+
<div class="box">
|
|
125
|
+
<h3>🌐 自定义翻译(<span id="tp_role">-</span>)</h3>
|
|
126
|
+
<div class="mode-row">
|
|
127
|
+
<label><input type="radio" name="tpmode" value="input_only"> 仅输入翻译(现实词 → 扮演词给角色)</label>
|
|
128
|
+
<label><input type="radio" name="tpmode" value="bidirectional"> 双向翻译(输入现实→扮演;角色回复扮演→现实)</label>
|
|
129
|
+
</div>
|
|
130
|
+
<div id="tp_rows"></div>
|
|
131
|
+
<div class="row" style="margin-top:8px">
|
|
132
|
+
<button onclick="addTransRow()">+ 添加词对</button>
|
|
133
|
+
<button class="ok" onclick="saveTrans()">保存</button>
|
|
134
|
+
<button onclick="closeTrans()">关闭</button>
|
|
135
|
+
</div>
|
|
136
|
+
<div class="hint">现实词会按表替换成扮演词交给角色(引擎自动);双向模式再把角色回复中的扮演词翻译回现实词。长词优先匹配。</div>
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
<div id="agegate">
|
|
140
|
+
<div class="box">
|
|
141
|
+
<h3>⛔ 成人内容确认</h3>
|
|
142
|
+
<div class="warn">本页面提供角色扮演内容,部分角色/对话可能包含<b>成人向(NSFW)</b>内容。<br><br>
|
|
143
|
+
依据国家法律法规与灵枢协议(对人类的保护):<br>
|
|
144
|
+
① 仅限<b>已满 18 周岁</b>的用户使用成人向内容;<br>
|
|
145
|
+
② 仅限<b>个人对话场景</b>(本页面为本地一对一,不公开);<br>
|
|
146
|
+
③ 灵枢<b>拒绝一切涉及未成年人的性内容</b>——此类请求会被直接拒绝并记录。<br><br>
|
|
147
|
+
点击「我已满 18 周岁」表示你确认符合上述条件。</div>
|
|
148
|
+
<div class="row"><button onclick="closeAgeGate()">退出</button><button class="ok" onclick="confirmAge()">我已满 18 周岁</button></div>
|
|
149
|
+
</div>
|
|
150
|
+
</div>
|
|
151
|
+
<div id="roledetail">
|
|
152
|
+
<div class="box">
|
|
153
|
+
<h3>角色详情 · <span id="rd_role">-</span></h3>
|
|
154
|
+
<div class="meta">
|
|
155
|
+
<input id="rd_name" placeholder="角色名">
|
|
156
|
+
<textarea id="rd_scenario" placeholder="人设场景(一句话描述性格与背景)"></textarea>
|
|
157
|
+
<textarea id="rd_first" placeholder="开场白(可选)"></textarea>
|
|
158
|
+
</div>
|
|
159
|
+
<div class="tabs">
|
|
160
|
+
<button id="tab-memory" class="on" onclick="rdTab('memory')">记忆</button>
|
|
161
|
+
<button id="tab-anchor" onclick="rdTab('anchor')">锚点</button>
|
|
162
|
+
<button id="tab-values" onclick="rdTab('values')">价值观</button>
|
|
163
|
+
</div>
|
|
164
|
+
<div id="rd_items"></div>
|
|
165
|
+
<div class="row" style="margin-top:4px">
|
|
166
|
+
<button onclick="rdAddItem()">+ 添加条目</button>
|
|
167
|
+
<label id="rd_nsfw"><input type="checkbox" id="rd_nsfw_chk"> 成人向(NSFW)角色</label>
|
|
168
|
+
</div>
|
|
169
|
+
<div class="row">
|
|
170
|
+
<button onclick="closeRoleDetail()">关闭</button>
|
|
171
|
+
<button class="ok" onclick="saveRoleDetail()">保存</button>
|
|
172
|
+
</div>
|
|
173
|
+
<div class="hint">记忆=背景设定;锚点=不可遗忘的自我核心;价值观=扮演的立场准则(带条件)。保存后立即生效(引擎每次重读角色 meta)。</div>
|
|
174
|
+
</div>
|
|
175
|
+
</div>
|
|
176
|
+
<script>
|
|
177
|
+
const $ = (id) => document.getElementById(id);
|
|
178
|
+
// P1 修复(GPT 审查·XSS):innerHTML 拼接角色/翻译内容必须转义——
|
|
179
|
+
// 否则保存 </textarea><script>… 之类内容会在角色详情页执行脚本
|
|
180
|
+
function esc(s) {
|
|
181
|
+
return String(s == null ? '' : s).replace(/[&<>"']/g, (c) => ({
|
|
182
|
+
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
|
|
183
|
+
})[c]);
|
|
184
|
+
}
|
|
185
|
+
// P1 完善(会话隔离):浏览器客户端实例 ID(localStorage 持久化)——
|
|
186
|
+
// 服务端按 x-client-id 隔离 session 与转录;多浏览器/多设备互不串线
|
|
187
|
+
let CLIENT_ID = '';
|
|
188
|
+
try {
|
|
189
|
+
CLIENT_ID = localStorage.getItem('lingshu_client_id');
|
|
190
|
+
if (!CLIENT_ID) {
|
|
191
|
+
CLIENT_ID = 'c' + Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
192
|
+
localStorage.setItem('lingshu_client_id', CLIENT_ID);
|
|
193
|
+
}
|
|
194
|
+
} catch (e) { /* localStorage 不可用(隐私模式)→ 空,服务端落 shared */ }
|
|
195
|
+
// V22 修复(缺陷 src/lib/roleplay_web.ts:338):编辑密钥携带途径——
|
|
196
|
+
// 服务端写操作 fail-closed 要求 x-edit-key 头(P1-2),但此前页面既无输入
|
|
197
|
+
// 密钥的途径、写 POST 又全用裸 headers → 全部编辑恒 403 不可用。现在
|
|
198
|
+
// 🔑 按钮(setEditKey → prompt)把密钥存 localStorage,apiHeaders 统一注入。
|
|
199
|
+
const EDIT_KEY_STORE = 'lingshu_edit_key';
|
|
200
|
+
function editKey() {
|
|
201
|
+
try { return localStorage.getItem(EDIT_KEY_STORE) || ''; } catch (e) { return ''; }
|
|
202
|
+
}
|
|
203
|
+
function setEditKey() {
|
|
204
|
+
const k = prompt('编辑密钥(服务端环境变量 ROLEPLAY_EDIT_KEY 的值;留空清除):', editKey());
|
|
205
|
+
if (k === null) return; // 取消不动
|
|
206
|
+
try {
|
|
207
|
+
if (k.trim()) localStorage.setItem(EDIT_KEY_STORE, k.trim());
|
|
208
|
+
else localStorage.removeItem(EDIT_KEY_STORE);
|
|
209
|
+
} catch (e) { /* localStorage 不可用 → 忽略 */ }
|
|
210
|
+
}
|
|
211
|
+
function apiHeaders(extra) {
|
|
212
|
+
const h = { 'Content-Type': 'application/json' };
|
|
213
|
+
if (CLIENT_ID) h['x-client-id'] = CLIENT_ID;
|
|
214
|
+
const ek = editKey();
|
|
215
|
+
if (ek) h['x-edit-key'] = ek; // V22:写操作鉴权头(无则服务端 403)
|
|
216
|
+
return Object.assign(h, extra || {});
|
|
217
|
+
}
|
|
218
|
+
// —— 内容分级:年龄门控 + NSFW 检测(法律与协议保护)——
|
|
219
|
+
// 国家法律:涉未成年人性内容一律拒绝;成人内容仅限满 18 岁 + 个人对话场景。
|
|
220
|
+
const AGE_KEY = 'lingshu_roleplay_age_confirmed';
|
|
221
|
+
const NSFW_WORDS = ['做爱','性交','性行为','口交','肛交','高潮','自慰','射精','色情','裸体','脱光','爱抚','挑逗','性器官','插入','内射','强暴'];
|
|
222
|
+
const MINOR_WORDS = ['未成年','儿童','小孩','小女孩','小男孩','幼女','萝莉','幼童','初中生','小学生','14岁','13岁','12岁','学生妹','女童','男童'];
|
|
223
|
+
function hasAgeConfirm() { try { return localStorage.getItem(AGE_KEY) === '1'; } catch (e) { return false; } }
|
|
224
|
+
function showAgeGate() { $('agegate').style.display = 'flex'; }
|
|
225
|
+
function closeAgeGate() { $('agegate').style.display = 'none'; }
|
|
226
|
+
function confirmAge() {
|
|
227
|
+
try { localStorage.setItem(AGE_KEY, '1'); } catch (e) { /* 忽略 */ }
|
|
228
|
+
closeAgeGate();
|
|
229
|
+
}
|
|
230
|
+
// 首次进入:要求年龄确认(NSFW 门控前置)
|
|
231
|
+
if (!hasAgeConfirm()) showAgeGate();
|
|
232
|
+
// 服务端也会硬拦截;前端先做温和提示(未成年+性 = 直接拒绝)
|
|
233
|
+
function nsfwCheck(text) {
|
|
234
|
+
const nsfw = NSFW_WORDS.some(w => text.includes(w));
|
|
235
|
+
const minor = MINOR_WORDS.some(w => text.includes(w));
|
|
236
|
+
if (minor && nsfw) {
|
|
237
|
+
return { refuse: true, msg: '⛔ 已拒绝:涉及未成年人的性内容违反国家法律与灵枢协议(未成年人保护)。灵枢不提供任何涉及未成年人的性扮演内容。' };
|
|
238
|
+
}
|
|
239
|
+
if (nsfw && !hasAgeConfirm()) {
|
|
240
|
+
showAgeGate();
|
|
241
|
+
return { refuse: true, msg: '⛔ 成人向内容需确认年龄:请点击「我已满 18 周岁」后再继续(个人对话场景)。' };
|
|
242
|
+
}
|
|
243
|
+
return { refuse: false };
|
|
244
|
+
}
|
|
245
|
+
let roles = [];
|
|
246
|
+
async function refreshRoles() {
|
|
247
|
+
const r = await fetch('/roleplay/api/roles').then(x => x.json());
|
|
248
|
+
roles = r.roles || [];
|
|
249
|
+
const sel = $('roleSel');
|
|
250
|
+
sel.innerHTML = '';
|
|
251
|
+
roles.forEach(x => { const o = document.createElement('option'); o.value = x.id; o.textContent = x.name || x.id; sel.appendChild(o); });
|
|
252
|
+
if (roles.length && !sel.value) sel.value = roles[0].id;
|
|
253
|
+
if (roles.length) await loadHistory(sel.value);
|
|
254
|
+
}
|
|
255
|
+
function addMsg(text, who, route, memories) {
|
|
256
|
+
const d = document.createElement('div');
|
|
257
|
+
d.className = 'msg ' + who;
|
|
258
|
+
d.textContent = text;
|
|
259
|
+
const meta = [];
|
|
260
|
+
if (route) meta.push('route: ' + route);
|
|
261
|
+
if (typeof memories === 'number') meta.push('记忆召回: ' + memories);
|
|
262
|
+
if (meta.length) { const r = document.createElement('span'); r.className = 'route'; r.textContent = meta.join(' · '); d.appendChild(r); }
|
|
263
|
+
$('chat').appendChild(d);
|
|
264
|
+
$('chat').scrollTop = $('chat').scrollHeight;
|
|
265
|
+
}
|
|
266
|
+
// 加载角色历史对话(无限上下文:本地完整转录 JSONL,按客户端实例隔离)
|
|
267
|
+
async function loadHistory(roleId) {
|
|
268
|
+
const chatEl = $('chat');
|
|
269
|
+
chatEl.innerHTML = '';
|
|
270
|
+
try {
|
|
271
|
+
const r = await fetch('/roleplay/api/history?role_id=' + encodeURIComponent(roleId), { headers: apiHeaders() }).then(x => x.json());
|
|
272
|
+
const h = r.history || [];
|
|
273
|
+
if (!h.length) {
|
|
274
|
+
const hint = document.createElement('div');
|
|
275
|
+
hint.className = 'hint';
|
|
276
|
+
hint.textContent = '与「' + ($('roleSel').selectedOptions[0] ? $('roleSel').selectedOptions[0].textContent : roleId) + '」的新对话从这里开始。';
|
|
277
|
+
chatEl.appendChild(hint);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
h.forEach(e => addMsg(e.text, e.role === 'user' ? 'user' : 'bot', e.route, e.memories));
|
|
281
|
+
} catch (e) {
|
|
282
|
+
const hint = document.createElement('div');
|
|
283
|
+
hint.className = 'hint';
|
|
284
|
+
hint.textContent = '历史加载失败:' + e;
|
|
285
|
+
chatEl.appendChild(hint);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async function send() {
|
|
289
|
+
const input = $('input'); const text = input.value.trim();
|
|
290
|
+
if (!text) return;
|
|
291
|
+
// 内容分级检查(前端提示;服务端有硬拦截)
|
|
292
|
+
const gate = nsfwCheck(text);
|
|
293
|
+
if (gate.refuse) { addMsg(gate.msg, 'bot'); return; }
|
|
294
|
+
input.value = ''; addMsg(text, 'user');
|
|
295
|
+
const role = $('roleSel').value;
|
|
296
|
+
const btn = $('send'); btn.disabled = true; $('typing').style.display = 'block';
|
|
297
|
+
try {
|
|
298
|
+
const r = await fetch('/roleplay/api/chat', { method:'POST', headers: apiHeaders(), body: JSON.stringify({ role_id: role, message: text }) }).then(x => x.json());
|
|
299
|
+
addMsg(r.reply || '(无回应:' + (r.error || '未知错误') + ')', 'bot', r.route, r.memories);
|
|
300
|
+
} catch (e) { addMsg('网络错误:' + e, 'bot'); }
|
|
301
|
+
btn.disabled = false; $('typing').style.display = 'none';
|
|
302
|
+
// 保持输入状态:重新聚焦并居中,键盘不收起(连续对话)
|
|
303
|
+
try { input.focus(); } catch (e) { /* 忽略 */ }
|
|
304
|
+
}
|
|
305
|
+
function showNewRole() { $('newrole').style.display = 'flex'; }
|
|
306
|
+
function closeNewRole() { $('newrole').style.display = 'none'; }
|
|
307
|
+
// —— 角色详情 / 三导入(记忆·锚点·价值观)——
|
|
308
|
+
let rdKind = 'memory';
|
|
309
|
+
let rdItems = [];
|
|
310
|
+
function openRoleDetail() {
|
|
311
|
+
const role = $('roleSel').value;
|
|
312
|
+
if (!role) { alert('请先选择角色'); return; }
|
|
313
|
+
$('rd_role').textContent = role;
|
|
314
|
+
const meta = (roles.find(x => x.id === role)) || {};
|
|
315
|
+
$('rd_name').value = meta.name || '';
|
|
316
|
+
$('rd_scenario').value = meta.scenario || '';
|
|
317
|
+
$('rd_first').value = meta.first_mes || '';
|
|
318
|
+
rdTab('memory');
|
|
319
|
+
$('roledetail').style.display = 'flex';
|
|
320
|
+
}
|
|
321
|
+
function closeRoleDetail() { $('roledetail').style.display = 'none'; }
|
|
322
|
+
function rdTab(kind) {
|
|
323
|
+
rdKind = kind;
|
|
324
|
+
['memory','anchor','values'].forEach(k => $('tab-' + k).classList.toggle('on', k === kind));
|
|
325
|
+
loadRoleDetailItems();
|
|
326
|
+
}
|
|
327
|
+
async function loadRoleDetailItems() {
|
|
328
|
+
const role = $('rd_role').textContent;
|
|
329
|
+
const r = await fetch('/roleplay/api/roles/' + encodeURIComponent(role) + '/items?kind=' + rdKind).then(x => x.json());
|
|
330
|
+
rdItems = r.items || [];
|
|
331
|
+
const box = $('rd_items');
|
|
332
|
+
box.innerHTML = '';
|
|
333
|
+
rdItems.forEach((it, i) => rdRenderRow(box, it, i));
|
|
334
|
+
if (!rdItems.length) rdAddItem();
|
|
335
|
+
}
|
|
336
|
+
function rdRenderRow(box, it, i) {
|
|
337
|
+
const row = document.createElement('div');
|
|
338
|
+
row.className = 'rd-row';
|
|
339
|
+
// P1 修复(GPT 审查·XSS):it.content/it.importance/it.tags 来自用户/文件,须转义
|
|
340
|
+
row.innerHTML =
|
|
341
|
+
'<textarea placeholder="内容(该' + (rdKind === 'memory' ? '记忆' : rdKind === 'anchor' ? '锚点' : '价值观') + '的文本)">' + esc(it.content || '') + '</textarea>' +
|
|
342
|
+
'<div class="rd-sub">' +
|
|
343
|
+
'<input class="rd-imp" type="number" step="0.1" min="0" max="1" placeholder="重要性" value="' + esc(it.importance != null ? it.importance : '0.6') + '">' +
|
|
344
|
+
'<input class="rd-tags" placeholder="标签(逗号分隔)" value="' + esc((it.tags || []).join(',')) + '">' +
|
|
345
|
+
'<button class="rd-del" onclick="this.parentNode.parentNode.remove()">✕</button>' +
|
|
346
|
+
'</div>';
|
|
347
|
+
box.appendChild(row);
|
|
348
|
+
}
|
|
349
|
+
function rdAddItem() { rdRenderRow($('rd_items'), {}, rdItems.length); }
|
|
350
|
+
async function saveRoleDetail() {
|
|
351
|
+
const role = $('rd_role').textContent;
|
|
352
|
+
const name = $('rd_name').value.trim();
|
|
353
|
+
const scenario = $('rd_scenario').value.trim();
|
|
354
|
+
const first_mes = $('rd_first').value.trim();
|
|
355
|
+
const nsfw = $('rd_nsfw_chk').checked;
|
|
356
|
+
// V22 修复(缺陷 :338):此前 meta POST ①裸 headers(无 x-edit-key → 恒 403)
|
|
357
|
+
// ②响应完全不检查(fetch 对 403 不 reject)→ 无条件「已保存」= 虚假成功
|
|
358
|
+
// (服务端 403 在写盘之前 return,数据从未落盘)。现在统一走 apiHeaders()
|
|
359
|
+
// 并逐段检查 ok:任一失败 alert 服务端原因并中止,绝不报「已保存」。
|
|
360
|
+
let m;
|
|
361
|
+
try {
|
|
362
|
+
m = await fetch('/roleplay/api/roles/' + encodeURIComponent(role) + '/meta', {
|
|
363
|
+
method: 'POST', headers: apiHeaders(),
|
|
364
|
+
body: JSON.stringify({ name, scenario, first_mes, nsfw }),
|
|
365
|
+
}).then(x => x.json());
|
|
366
|
+
} catch (e) { m = { ok: false, error: '网络错误:' + e }; }
|
|
367
|
+
if (!m || !m.ok) { alert((m && m.error) || '保存失败(角色信息)'); return; }
|
|
368
|
+
// 当前页签的条目
|
|
369
|
+
const items = [];
|
|
370
|
+
document.querySelectorAll('#rd_items .rd-row').forEach(row => {
|
|
371
|
+
const content = row.querySelector('textarea').value.trim();
|
|
372
|
+
const imp = parseFloat(row.querySelector('.rd-imp').value) || 0.6;
|
|
373
|
+
const tags = row.querySelector('.rd-tags').value.split(/[,,]/).map(s => s.trim()).filter(Boolean);
|
|
374
|
+
if (content) items.push({ content, importance: imp, tags });
|
|
375
|
+
});
|
|
376
|
+
let r;
|
|
377
|
+
try {
|
|
378
|
+
r = await fetch('/roleplay/api/roles/' + encodeURIComponent(role) + '/import', {
|
|
379
|
+
method: 'POST', headers: apiHeaders(),
|
|
380
|
+
body: JSON.stringify({ kind: rdKind, items }),
|
|
381
|
+
}).then(x => x.json());
|
|
382
|
+
} catch (e) { r = { ok: false, error: '网络错误:' + e }; }
|
|
383
|
+
if (!r || !r.ok) { alert((r && r.error) || '保存失败(' + rdKind + ')'); return; }
|
|
384
|
+
addMsg('角色「' + role + '」已保存:' + name + ' · ' + rdKind + ' ' + items.length + ' 条' + (nsfw ? ' · NSFW' : ''), 'bot');
|
|
385
|
+
closeRoleDetail();
|
|
386
|
+
await refreshRoles();
|
|
387
|
+
}
|
|
388
|
+
// —— 自定义翻译面板 ——
|
|
389
|
+
let transMode = 'input_only';
|
|
390
|
+
async function openTrans() {
|
|
391
|
+
const role = $('roleSel').value || 'protocol-guide';
|
|
392
|
+
$('tp_role').textContent = role;
|
|
393
|
+
try {
|
|
394
|
+
const r = await fetch('/roleplay/api/translate?role_id=' + encodeURIComponent(role)).then(x => x.json());
|
|
395
|
+
transMode = r.mode || 'input_only';
|
|
396
|
+
$('tp_rows').innerHTML = '';
|
|
397
|
+
(r.pairs || []).forEach(p => addTransRow(p.real, p.virtual, p.note));
|
|
398
|
+
if (!(r.pairs || []).length) addTransRow();
|
|
399
|
+
} catch (e) { alert('翻译配置加载失败:' + e); return; }
|
|
400
|
+
const radios = document.querySelectorAll('input[name="tpmode"]');
|
|
401
|
+
radios.forEach(x => { x.checked = (x.value === transMode); });
|
|
402
|
+
$('transpanel').style.display = 'flex';
|
|
403
|
+
}
|
|
404
|
+
function closeTrans() { $('transpanel').style.display = 'none'; }
|
|
405
|
+
function addTransRow(real, virtual, note) {
|
|
406
|
+
const row = document.createElement('div');
|
|
407
|
+
row.className = 'tp-row';
|
|
408
|
+
// P1 修复(GPT 审查·XSS):real/virtual/note 来自文件/导入,须转义
|
|
409
|
+
row.innerHTML =
|
|
410
|
+
'<input class="tp-real" placeholder="现实词(如:手机)" value="' + esc(real || '') + '">' +
|
|
411
|
+
'<span>→</span>' +
|
|
412
|
+
'<input class="tp-virtual" placeholder="扮演词(如:神之眼终端)" value="' + esc(virtual || '') + '">' +
|
|
413
|
+
'<input class="tp-note" placeholder="备注(可选)" value="' + esc(note || '') + '">' +
|
|
414
|
+
'<button class="tp-del" onclick="this.parentNode.remove()">✕</button>';
|
|
415
|
+
$('tp_rows').appendChild(row);
|
|
416
|
+
}
|
|
417
|
+
async function saveTrans() {
|
|
418
|
+
const role = $('roleSel').value || 'protocol-guide';
|
|
419
|
+
const pairs = [];
|
|
420
|
+
document.querySelectorAll('#tp_rows .tp-row').forEach(row => {
|
|
421
|
+
const real = row.querySelector('.tp-real').value.trim();
|
|
422
|
+
const virtual = row.querySelector('.tp-virtual').value.trim();
|
|
423
|
+
const note = row.querySelector('.tp-note').value.trim();
|
|
424
|
+
if (real && virtual) pairs.push({ real, virtual, note });
|
|
425
|
+
});
|
|
426
|
+
const mode = (document.querySelector('input[name="tpmode"]:checked') || {}).value || 'input_only';
|
|
427
|
+
const r = await fetch('/roleplay/api/translate', {
|
|
428
|
+
method: 'POST', headers: apiHeaders(),
|
|
429
|
+
body: JSON.stringify({ role_id: role, pairs, mode }),
|
|
430
|
+
}).then(x => x.json());
|
|
431
|
+
if (r.ok) {
|
|
432
|
+
transMode = r.mode;
|
|
433
|
+
closeTrans();
|
|
434
|
+
addMsg('翻译配置已保存:' + r.pairs.length + ' 组词对 · ' + (r.mode === 'bidirectional' ? '双向翻译' : '仅输入翻译'), 'bot');
|
|
435
|
+
} else alert((r.error || '保存失败') + (editKey() ? '' : '(可点右上角 🔑 输入编辑密钥)'));
|
|
436
|
+
}
|
|
437
|
+
async function createRole() {
|
|
438
|
+
const body = { role_id: $('nr_id').value.trim(), name: $('nr_name').value.trim(), scenario: $('nr_scenario').value.trim(), first_mes: $('nr_first').value.trim() };
|
|
439
|
+
if (!body.role_id) { alert('需要角色 ID'); return; }
|
|
440
|
+
const r = await fetch('/roleplay/api/roles', { method:'POST', headers: apiHeaders(), body: JSON.stringify(body) }).then(x => x.json());
|
|
441
|
+
if (r.ok) { closeNewRole(); await refreshRoles(); addMsg('角色「' + (body.name || body.role_id) + '」已创建,可以开始对话了', 'bot'); }
|
|
442
|
+
else alert((r.error || '创建失败') + (editKey() ? '' : '(可点右上角 🔑 输入编辑密钥)'));
|
|
443
|
+
}
|
|
444
|
+
var input = document.getElementById('input');
|
|
445
|
+
var chat = document.getElementById('chat');
|
|
446
|
+
var footer = document.querySelector('footer');
|
|
447
|
+
input.addEventListener('keydown', function (e) {
|
|
448
|
+
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send(); }
|
|
449
|
+
});
|
|
450
|
+
refreshRoles();
|
|
451
|
+
// 切换角色 → 加载该角色的历史对话
|
|
452
|
+
$('roleSel').addEventListener('change', function () { loadHistory(this.value); });
|
|
453
|
+
|
|
454
|
+
// —— 输入框随内容自动增高(上限 140px)——
|
|
455
|
+
input.addEventListener('input', function () {
|
|
456
|
+
input.style.height = 'auto';
|
|
457
|
+
input.style.height = Math.min(input.scrollHeight, 140) + 'px';
|
|
458
|
+
});
|
|
459
|
+
// —— 聚焦时输入框自动居中(不依赖 WebView 键盘行为):悬浮到屏幕 45% 高度,
|
|
460
|
+
// 键盘在上抬多少都不会遮住;失焦/发送后恢复原位 ——
|
|
461
|
+
function setTypingCenter(on) {
|
|
462
|
+
if (!footer) return;
|
|
463
|
+
footer.classList.toggle('typing-center', on);
|
|
464
|
+
setTimeout(function () {
|
|
465
|
+
if (chat) chat.scrollTop = chat.scrollHeight;
|
|
466
|
+
}, 60);
|
|
467
|
+
}
|
|
468
|
+
input.addEventListener('focus', function () { setTypingCenter(true); });
|
|
469
|
+
input.addEventListener('blur', function () { setTypingCenter(false); });
|
|
470
|
+
window.addEventListener('resize', function () { if (chat) chat.scrollTop = chat.scrollHeight; });
|
|
471
|
+
</script>
|
|
472
|
+
</body>
|
|
443
473
|
</html>`;
|
|
444
474
|
/** 在 webServer 上挂载 /roleplay 前缀路由(页面 + API)。失败不影响插件主体。
|
|
445
475
|
*
|
|
@@ -476,7 +506,9 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
476
506
|
// P1 修复(GPT 审查):
|
|
477
507
|
// ① 请求体大小限制(此前 for-await 无限累加,恶意请求可制造内存压力)
|
|
478
508
|
const readBody = async (req, maxBytes = 1_000_000) => {
|
|
479
|
-
|
|
509
|
+
// P2-12(批次 30):Buffer 收集后统一 utf-8 解码——旧写法
|
|
510
|
+
// body += chunk 在多字节字符跨 chunk 时产生替换符截断。
|
|
511
|
+
const chunks = [];
|
|
480
512
|
let size = 0;
|
|
481
513
|
for await (const chunk of req) {
|
|
482
514
|
size += chunk.length;
|
|
@@ -485,20 +517,28 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
485
517
|
err.status = 413;
|
|
486
518
|
throw err;
|
|
487
519
|
}
|
|
488
|
-
|
|
520
|
+
chunks.push(chunk);
|
|
489
521
|
}
|
|
490
|
-
return
|
|
522
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
491
523
|
};
|
|
492
|
-
// ②
|
|
493
|
-
//
|
|
494
|
-
//
|
|
524
|
+
// ② 编辑鉴权(P1-2,批次 26 外部审查报告):fail-closed——未配置
|
|
525
|
+
// ROLEPLAY_EDIT_KEY 时**拒绝全部写操作**(旧语义「未配置=开放」是
|
|
526
|
+
// 本地开发便利,但网页面板是唯一对浏览器开放的面,默认无鉴权 =
|
|
527
|
+
// 任意来源可改写角色数据)。403 文案引导配置:设 ROLEPLAY_EDIT_KEY
|
|
528
|
+
// 并在请求带 x-edit-key 头即可恢复编辑。
|
|
495
529
|
const EDIT_KEY = process.env.ROLEPLAY_EDIT_KEY || '';
|
|
496
530
|
const requireEditKey = (req) => {
|
|
497
531
|
if (!EDIT_KEY)
|
|
498
|
-
return
|
|
532
|
+
return false; // P1-2:未配置 = 拒绝(fail-closed)
|
|
499
533
|
const h = (req.headers && req.headers['x-edit-key']) || '';
|
|
500
534
|
return h === EDIT_KEY;
|
|
501
535
|
};
|
|
536
|
+
// V21-1(批次 34,外部报告):真分支此前误写自调用(无限递归 → 配了
|
|
537
|
+
// key 反而 500 且泄露内部异常文本)。两分支都是终止文案:已配置 =
|
|
538
|
+
// 「缺 x-edit-key 头」;未配置 = 「服务端需配置」引导。
|
|
539
|
+
const editKeyHint = () => EDIT_KEY
|
|
540
|
+
? '缺少编辑密钥(x-edit-key 头)'
|
|
541
|
+
: '编辑未开放:服务端需配置环境变量 ROLEPLAY_EDIT_KEY,请求需带 x-edit-key 头(P1-2 fail-closed)';
|
|
502
542
|
const roleDataDir = join(dirname(config.dbPath), 'roleplay_data');
|
|
503
543
|
try {
|
|
504
544
|
mkdirSync(join(roleDataDir, 'roleplay'), { recursive: true });
|
|
@@ -512,6 +552,7 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
512
552
|
}
|
|
513
553
|
catch { /* 忽略 */ }
|
|
514
554
|
const transcriptFile = (roleId, clientId) => join(transcriptsDir, `${roleId.replace(/[^\w.-]/g, '_')}__${clientId}.jsonl`);
|
|
555
|
+
const TRANSCRIPT_TAIL_MAX = 500; // P2-9(批次 31):返回最近 500 条
|
|
515
556
|
const readTranscript = (roleId, clientId = 'shared') => {
|
|
516
557
|
const f = transcriptFile(roleId, clientId);
|
|
517
558
|
if (!existsSync(f))
|
|
@@ -525,7 +566,9 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
525
566
|
}
|
|
526
567
|
catch { /* 跳过坏行 */ }
|
|
527
568
|
}
|
|
528
|
-
|
|
569
|
+
// P2-9:无上限返回会让响应体随会话长度线性增长——只回最近尾部。
|
|
570
|
+
return out.length > TRANSCRIPT_TAIL_MAX
|
|
571
|
+
? out.slice(out.length - TRANSCRIPT_TAIL_MAX) : out;
|
|
529
572
|
};
|
|
530
573
|
const appendTranscript = (roleId, entry, clientId = 'shared') => {
|
|
531
574
|
try {
|
|
@@ -551,16 +594,28 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
551
594
|
// 「角色字典」视角;写入保持原顶层结构(嵌套则写回 meta 内),读写一致——
|
|
552
595
|
// 此前读兼容嵌套但写永远写根,保存成功但列表读旧值。
|
|
553
596
|
const roleMetaFile = join(roleDataDir, 'roleplay', '_roles.json');
|
|
597
|
+
// P1-3(批次 26 外部审查报告):角色 id 白名单——role 从 URL 提取后
|
|
598
|
+
// 直接作 meta 对象键,`__proto__` 键会劫持 Object.prototype(污染插件
|
|
599
|
+
// 与 DSH 宿主共享的同进程对象)。白名单字符集 + 显式拒绝危险键。
|
|
600
|
+
const _BAD_ROLE_IDS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
601
|
+
const validRoleId = (role) => typeof role === 'string'
|
|
602
|
+
&& /^[A-Za-z0-9_.-]{1,64}$/.test(role)
|
|
603
|
+
&& !_BAD_ROLE_IDS.has(role);
|
|
604
|
+
// meta 字典统一 null 原型——即使存量文件已被写入危险键,
|
|
605
|
+
// meta['__proto__'] 也只是普通 own 属性,不再劫持原型链。
|
|
606
|
+
const toNullProto = (o) => (o && typeof o === 'object')
|
|
607
|
+
? Object.assign(Object.create(null), o)
|
|
608
|
+
: o;
|
|
554
609
|
const readRoleMeta = () => {
|
|
555
610
|
try {
|
|
556
611
|
const raw = JSON.parse(readFileSync(roleMetaFile, 'utf8'));
|
|
557
612
|
if (raw && typeof raw === 'object'
|
|
558
613
|
&& raw.meta && typeof raw.meta === 'object')
|
|
559
|
-
return raw.meta; // 嵌套格式 → meta 内角色字典
|
|
560
|
-
return raw;
|
|
614
|
+
return toNullProto(raw.meta); // 嵌套格式 → meta 内角色字典
|
|
615
|
+
return toNullProto(raw);
|
|
561
616
|
}
|
|
562
617
|
catch {
|
|
563
|
-
return
|
|
618
|
+
return Object.create(null);
|
|
564
619
|
}
|
|
565
620
|
};
|
|
566
621
|
const writeRoleMeta = (flat) => {
|
|
@@ -595,8 +650,12 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
595
650
|
}
|
|
596
651
|
};
|
|
597
652
|
const writeTransSettings = (s) => {
|
|
653
|
+
// P2-10(批次 31):tmp+rename 原子写(与 writeRoleMeta 同款)——
|
|
654
|
+
// 直接 writeFileSync 并发/中断会写坏 JSON,后续读静默丢配置。
|
|
598
655
|
try {
|
|
599
|
-
|
|
656
|
+
const tmp = transSettingsFile + '.tmp';
|
|
657
|
+
writeFileSync(tmp, JSON.stringify(s, null, 2), 'utf8');
|
|
658
|
+
renameSync(tmp, transSettingsFile);
|
|
600
659
|
}
|
|
601
660
|
catch { /* 忽略 */ }
|
|
602
661
|
};
|
|
@@ -606,6 +665,8 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
606
665
|
return Array.isArray(t) ? t : [];
|
|
607
666
|
};
|
|
608
667
|
const setTranslations = (roleId, pairs) => {
|
|
668
|
+
if (!validRoleId(roleId))
|
|
669
|
+
return; // P1-3:非法角色 id 拒写(防原型污染)
|
|
609
670
|
const meta = readRoleMeta();
|
|
610
671
|
if (!meta[roleId])
|
|
611
672
|
meta[roleId] = { created_at: Date.now() / 1000, name: roleId };
|
|
@@ -668,7 +729,7 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
668
729
|
// P1 修复(GPT 审查):写操作鉴权(ROLEPLAY_EDIT_KEY)+ 请求体大小限制
|
|
669
730
|
if (!requireEditKey(req)) {
|
|
670
731
|
res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
671
|
-
res.end(JSON.stringify({ ok: false, error:
|
|
732
|
+
res.end(JSON.stringify({ ok: false, error: editKeyHint() }));
|
|
672
733
|
return;
|
|
673
734
|
}
|
|
674
735
|
let body;
|
|
@@ -723,7 +784,7 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
723
784
|
// P1 修复(GPT 审查):写操作鉴权(ROLEPLAY_EDIT_KEY)+ 请求体大小限制
|
|
724
785
|
if (!requireEditKey(req)) {
|
|
725
786
|
res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
726
|
-
res.end(JSON.stringify({ ok: false, error:
|
|
787
|
+
res.end(JSON.stringify({ ok: false, error: editKeyHint() }));
|
|
727
788
|
return;
|
|
728
789
|
}
|
|
729
790
|
let body;
|
|
@@ -737,6 +798,15 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
737
798
|
}
|
|
738
799
|
const p = JSON.parse(body);
|
|
739
800
|
const role = decodeURIComponent(url.pathname.split('/')[4]);
|
|
801
|
+
if (!validRoleId(role)) {
|
|
802
|
+
// P1-3:__proto__/constructor/prototype 及越界字符拒绝
|
|
803
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
804
|
+
res.end(JSON.stringify({ ok: false, error: '非法角色 id(白名单 ^[A-Za-z0-9_.-]{1,64}$)' }));
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
// P2-11(批次 30 注记):本读改写段为同步执行(中间无
|
|
808
|
+
// await)——node 单 tick 内不会交错;若未来在此段引入
|
|
809
|
+
// await,须先加写队列/文件锁(并发丢更新防线)。
|
|
740
810
|
const meta = readRoleMeta();
|
|
741
811
|
if (!meta[role])
|
|
742
812
|
meta[role] = { created_at: Date.now() / 1000 };
|
|
@@ -766,6 +836,14 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
766
836
|
}
|
|
767
837
|
const p = JSON.parse(body);
|
|
768
838
|
const role = p.role_id || 'protocol-guide';
|
|
839
|
+
// P2-23(批次 30):chat 的 role_id 进 node_id 模板——
|
|
840
|
+
// 纵深防御:与 /meta 同款白名单(当前模板前缀使穿越
|
|
841
|
+
// 不可达,但模板改动即破——入口统一卡死)
|
|
842
|
+
if (!validRoleId(role)) {
|
|
843
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
844
|
+
res.end(JSON.stringify({ ok: false, error: '非法角色 id(白名单 ^[A-Za-z0-9_.-]{1,64}$)' }));
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
769
847
|
// P1 完善(会话隔离):按客户端实例隔离 session 与转录
|
|
770
848
|
const cid = clientIdOf(req);
|
|
771
849
|
// —— 服务端内容分级硬拦截(不依赖前端,法律与协议保护)——
|
|
@@ -820,7 +898,7 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
820
898
|
// P1 修复(GPT 审查):写操作鉴权(ROLEPLAY_EDIT_KEY)+ 请求体大小限制
|
|
821
899
|
if (!requireEditKey(req)) {
|
|
822
900
|
res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
823
|
-
res.end(JSON.stringify({ ok: false, error:
|
|
901
|
+
res.end(JSON.stringify({ ok: false, error: editKeyHint() }));
|
|
824
902
|
return;
|
|
825
903
|
}
|
|
826
904
|
let body;
|
|
@@ -834,6 +912,15 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
834
912
|
}
|
|
835
913
|
const p = JSON.parse(body);
|
|
836
914
|
const role = p.role_id || 'protocol-guide';
|
|
915
|
+
// P1-3 补卡(translate POST):与 /meta、/chat 同款白名单——
|
|
916
|
+
// 此前不校验即 setTranslations(内部白名单不匹配**静默 return**,
|
|
917
|
+
// 中文 id 词对永不落盘)+ settings[role] 直接键赋值('__proto__'
|
|
918
|
+
// 走原型 setter 连 mode 都丢),却仍回 ok:true = 虚假成功。
|
|
919
|
+
if (!validRoleId(role)) {
|
|
920
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
921
|
+
res.end(JSON.stringify({ ok: false, error: '非法角色 id(白名单 ^[A-Za-z0-9_.-]{1,64}$)' }));
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
837
924
|
const pairs = (Array.isArray(p.pairs) ? p.pairs : [])
|
|
838
925
|
.map((x) => ({ real: String(x.real || '').trim(), virtual: String(x.virtual || '').trim(), note: String(x.note || '').trim() }))
|
|
839
926
|
.filter((x) => x.real && x.virtual);
|
|
@@ -856,7 +943,7 @@ export async function installRoleplayWeb(ctx, capability, config, disposers, mdc
|
|
|
856
943
|
// P1 修复(GPT 审查):写操作鉴权(ROLEPLAY_EDIT_KEY)+ 请求体大小限制
|
|
857
944
|
if (!requireEditKey(req)) {
|
|
858
945
|
res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
859
|
-
res.end(JSON.stringify({ ok: false, error:
|
|
946
|
+
res.end(JSON.stringify({ ok: false, error: editKeyHint() }));
|
|
860
947
|
return;
|
|
861
948
|
}
|
|
862
949
|
let body;
|