dsh-qqbot-plus 0.0.0-stage → 0.1.2
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/CHANGELOG.md +188 -0
- package/CHANGELOG_EN.md +188 -0
- package/LICENSE +21 -0
- package/README.md +281 -2
- package/README_EN.md +248 -0
- package/cordis.patch.yml +163 -0
- package/cordis.yml +112 -0
- package/install.sh +108 -0
- package/lib/alarms.js +371 -0
- package/lib/client.js +2411 -0
- package/lib/commands/auth.d.ts +18 -0
- package/lib/commands/auth.d.ts.map +1 -0
- package/lib/commands/auth.js +15 -0
- package/lib/commands/auth.js.map +1 -0
- package/lib/commands/help.d.ts +10 -0
- package/lib/commands/help.d.ts.map +1 -0
- package/lib/commands/help.js +39 -0
- package/lib/commands/help.js.map +1 -0
- package/lib/commands/index.d.ts +15 -0
- package/lib/commands/index.d.ts.map +1 -0
- package/lib/commands/index.js +30 -0
- package/lib/commands/index.js.map +1 -0
- package/lib/commands/misc.d.ts +21 -0
- package/lib/commands/misc.d.ts.map +1 -0
- package/lib/commands/misc.js +93 -0
- package/lib/commands/misc.js.map +1 -0
- package/lib/commands/model.d.ts +6 -0
- package/lib/commands/model.d.ts.map +1 -0
- package/lib/commands/model.js +63 -0
- package/lib/commands/model.js.map +1 -0
- package/lib/commands/new.js +17 -0
- package/lib/commands/preset.d.ts +8 -0
- package/lib/commands/preset.d.ts.map +1 -0
- package/lib/commands/preset.js +56 -0
- package/lib/commands/preset.js.map +1 -0
- package/lib/commands/session.d.ts +14 -0
- package/lib/commands/session.d.ts.map +1 -0
- package/lib/commands/session.js +34 -0
- package/lib/commands/session.js.map +1 -0
- package/lib/commands/status.d.ts +7 -0
- package/lib/commands/status.d.ts.map +1 -0
- package/lib/commands/status.js +26 -0
- package/lib/commands/status.js.map +1 -0
- package/lib/commands/types.d.ts +21 -0
- package/lib/commands/types.d.ts.map +1 -0
- package/lib/commands/types.js +2 -0
- package/lib/commands/types.js.map +1 -0
- package/lib/config.d.ts +95 -0
- package/lib/config.js +88 -0
- package/lib/features/answer-parser.d.ts +9 -0
- package/lib/features/answer-parser.d.ts.map +1 -0
- package/lib/features/answer-parser.js +23 -0
- package/lib/features/answer-parser.js.map +1 -0
- package/lib/features/approval-channel.d.ts +94 -0
- package/lib/features/approval-channel.d.ts.map +1 -0
- package/lib/features/approval-channel.js +158 -0
- package/lib/features/approval-channel.js.map +1 -0
- package/lib/features/approval-renderer.d.ts +18 -0
- package/lib/features/approval-renderer.d.ts.map +1 -0
- package/lib/features/approval-renderer.js +52 -0
- package/lib/features/approval-renderer.js.map +1 -0
- package/lib/features/button-utils.d.ts +34 -0
- package/lib/features/button-utils.d.ts.map +1 -0
- package/lib/features/button-utils.js +27 -0
- package/lib/features/button-utils.js.map +1 -0
- package/lib/features/history-store.d.ts +8 -0
- package/lib/features/history-store.d.ts.map +1 -0
- package/lib/features/history-store.js +23 -0
- package/lib/features/history-store.js.map +1 -0
- package/lib/features/question-channel.d.ts +96 -0
- package/lib/features/question-channel.d.ts.map +1 -0
- package/lib/features/question-channel.js +234 -0
- package/lib/features/question-channel.js.map +1 -0
- package/lib/features/question-renderer.d.ts +20 -0
- package/lib/features/question-renderer.d.ts.map +1 -0
- package/lib/features/question-renderer.js +70 -0
- package/lib/features/question-renderer.js.map +1 -0
- package/lib/fishcheck/measure.js +230 -0
- package/lib/fishcheck/rules.js +99 -0
- package/lib/fishmeme/plugin.js +605 -0
- package/lib/fishmeme/routes.js +141 -0
- package/lib/fishmemory/plugin.js +1052 -0
- package/lib/gateway/bootstrap.d.ts +12 -0
- package/lib/gateway/bootstrap.d.ts.map +1 -0
- package/lib/gateway/bootstrap.js +200 -0
- package/lib/gateway/bootstrap.js.map +1 -0
- package/lib/gateway/index.d.ts +3 -0
- package/lib/gateway/index.d.ts.map +1 -0
- package/lib/gateway/index.js +3 -0
- package/lib/gateway/index.js.map +1 -0
- package/lib/gateway/middleware-setup.d.ts +12 -0
- package/lib/gateway/middleware-setup.d.ts.map +1 -0
- package/lib/gateway/middleware-setup.js +82 -0
- package/lib/gateway/middleware-setup.js.map +1 -0
- package/lib/host/adapter.d.ts +48 -0
- package/lib/host/adapter.d.ts.map +1 -0
- package/lib/host/adapter.js +127 -0
- package/lib/host/adapter.js.map +1 -0
- package/lib/humanlike/ENV_PROMPT.md +28 -0
- package/lib/humanlike/affinity.js +316 -0
- package/lib/humanlike/learn.js +343 -0
- package/lib/humanlike/life.js +899 -0
- package/lib/humanlike/mind.js +323 -0
- package/lib/humanlike/plugin.js +587 -0
- package/lib/humanlike/reach.js +119 -0
- package/lib/humanlike/real.js +276 -0
- package/lib/humanlike/rules.js +67 -0
- package/lib/humanlike/settings-api.js +159 -0
- package/lib/humanlike/settings.js +370 -0
- package/lib/humanlike/together.js +251 -0
- package/lib/identity/paths.js +27 -0
- package/lib/identity/persona.js +75 -0
- package/lib/identity/portrait.js +25 -0
- package/lib/identity/runtime.js +78 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +117 -0
- package/lib/media/draft-tool.js +442 -0
- package/lib/media/draft-tool.js.bak-to-182027 +389 -0
- package/lib/media/media-cleaner.d.ts +19 -0
- package/lib/media/media-cleaner.d.ts.map +1 -0
- package/lib/media/media-cleaner.js +81 -0
- package/lib/media/media-cleaner.js.map +1 -0
- package/lib/media/say-tool.js +72 -0
- package/lib/media/send-file-tool.d.ts +12 -0
- package/lib/media/send-file-tool.d.ts.map +1 -0
- package/lib/media/send-file-tool.js +122 -0
- package/lib/media/send-file-tool.js.map +1 -0
- package/lib/media/send-media.d.ts +61 -0
- package/lib/media/send-media.d.ts.map +1 -0
- package/lib/media/send-media.js +104 -0
- package/lib/media/send-media.js.map +1 -0
- package/lib/media/vision-tool.d.ts +22 -0
- package/lib/media/vision-tool.d.ts.map +1 -0
- package/lib/media/vision-tool.js +288 -0
- package/lib/media/vision-tool.js.map +1 -0
- package/lib/middleware/attachment.d.ts +11 -0
- package/lib/middleware/attachment.d.ts.map +1 -0
- package/lib/middleware/attachment.js +30 -0
- package/lib/middleware/attachment.js.map +1 -0
- package/lib/middleware/question-answer.d.ts +10 -0
- package/lib/middleware/question-answer.d.ts.map +1 -0
- package/lib/middleware/question-answer.js +19 -0
- package/lib/middleware/question-answer.js.map +1 -0
- package/lib/model/index.d.ts +10 -0
- package/lib/model/index.d.ts.map +1 -0
- package/lib/model/index.js +9 -0
- package/lib/model/index.js.map +1 -0
- package/lib/model/model-resolver.d.ts +102 -0
- package/lib/model/model-resolver.d.ts.map +1 -0
- package/lib/model/model-resolver.js +206 -0
- package/lib/model/model-resolver.js.map +1 -0
- package/lib/model/prefs-store.d.ts +50 -0
- package/lib/model/prefs-store.d.ts.map +1 -0
- package/lib/model/prefs-store.js +215 -0
- package/lib/model/prefs-store.js.map +1 -0
- package/lib/model/settings-reader.d.ts +19 -0
- package/lib/model/settings-reader.d.ts.map +1 -0
- package/lib/model/settings-reader.js +76 -0
- package/lib/model/settings-reader.js.map +1 -0
- package/lib/model/types.d.ts +15 -0
- package/lib/model/types.d.ts.map +1 -0
- package/lib/model/types.js +5 -0
- package/lib/model/types.js.map +1 -0
- package/lib/resume-check.js +122 -0
- package/lib/resume-tool.js +29 -0
- package/lib/rules.js +57 -0
- package/lib/session/idle-evictor.d.ts +18 -0
- package/lib/session/idle-evictor.d.ts.map +1 -0
- package/lib/session/idle-evictor.js +31 -0
- package/lib/session/idle-evictor.js.map +1 -0
- package/lib/session/index.d.ts +9 -0
- package/lib/session/index.d.ts.map +1 -0
- package/lib/session/index.js +8 -0
- package/lib/session/index.js.map +1 -0
- package/lib/session/pick-agent.js +116 -0
- package/lib/session/session-id.d.ts +5 -0
- package/lib/session/session-id.d.ts.map +1 -0
- package/lib/session/session-id.js +15 -0
- package/lib/session/session-id.js.map +1 -0
- package/lib/session/session-manager.d.ts +138 -0
- package/lib/session/session-manager.d.ts.map +1 -0
- package/lib/session/session-manager.js +1157 -0
- package/lib/session/session-manager.js.bak-target +1081 -0
- package/lib/session/session-manager.js.bak-title-20261118 +1119 -0
- package/lib/session/session-manager.js.map +1 -0
- package/lib/session/types.d.ts +162 -0
- package/lib/session/types.d.ts.map +1 -0
- package/lib/session/types.js +2 -0
- package/lib/session/types.js.map +1 -0
- package/lib/setup/bind-routes.d.ts +78 -0
- package/lib/setup/bind-routes.d.ts.map +1 -0
- package/lib/setup/bind-routes.js +588 -0
- package/lib/setup/bind-routes.js.bak-join-163944 +761 -0
- package/lib/setup/bind-routes.js.map +1 -0
- package/lib/setup/binding-service.d.ts +86 -0
- package/lib/setup/binding-service.d.ts.map +1 -0
- package/lib/setup/binding-service.js +175 -0
- package/lib/setup/binding-service.js.map +1 -0
- package/lib/setup/index.d.ts +50 -0
- package/lib/setup/index.d.ts.map +1 -0
- package/lib/setup/index.js +163 -0
- package/lib/setup/index.js.map +1 -0
- package/lib/setup/persist.d.ts +42 -0
- package/lib/setup/persist.d.ts.map +1 -0
- package/lib/setup/persist.js +344 -0
- package/lib/setup/persist.js.map +1 -0
- package/lib/setup/presenters.d.ts +40 -0
- package/lib/setup/presenters.d.ts.map +1 -0
- package/lib/setup/presenters.js +84 -0
- package/lib/setup/presenters.js.map +1 -0
- package/lib/setup/qr-svg.d.ts +9 -0
- package/lib/setup/qr-svg.d.ts.map +1 -0
- package/lib/setup/qr-svg.js +33 -0
- package/lib/setup/qr-svg.js.map +1 -0
- package/lib/shared/bind-protocol.d.ts +42 -0
- package/lib/shared/bind-protocol.d.ts.map +1 -0
- package/lib/shared/bind-protocol.js +33 -0
- package/lib/shared/bind-protocol.js.map +1 -0
- package/lib/shared/dirs.d.ts +10 -0
- package/lib/shared/dirs.d.ts.map +1 -0
- package/lib/shared/dirs.js +20 -0
- package/lib/shared/dirs.js.map +1 -0
- package/lib/shared/http-body.js +43 -0
- package/lib/shared/index.d.ts +8 -0
- package/lib/shared/index.d.ts.map +1 -0
- package/lib/shared/index.js +8 -0
- package/lib/shared/index.js.map +1 -0
- package/lib/shared/json-file.js +34 -0
- package/lib/shared/orders.js +26 -0
- package/lib/shared/scope.d.ts +14 -0
- package/lib/shared/scope.d.ts.map +1 -0
- package/lib/shared/scope.js +12 -0
- package/lib/shared/scope.js.map +1 -0
- package/lib/shared/send-helper.d.ts +11 -0
- package/lib/shared/send-helper.d.ts.map +1 -0
- package/lib/shared/send-helper.js +13 -0
- package/lib/shared/send-helper.js.map +1 -0
- package/lib/shared/utils.d.ts +31 -0
- package/lib/shared/utils.d.ts.map +1 -0
- package/lib/shared/utils.js +228 -0
- package/lib/shared/utils.js.map +1 -0
- package/lib/tool-guard.js +74 -0
- package/lib/transport/attachment.d.ts +41 -0
- package/lib/transport/attachment.d.ts.map +1 -0
- package/lib/transport/attachment.js +191 -0
- package/lib/transport/attachment.js.map +1 -0
- package/lib/transport/chunker.d.ts +14 -0
- package/lib/transport/chunker.d.ts.map +1 -0
- package/lib/transport/chunker.js +78 -0
- package/lib/transport/chunker.js.map +1 -0
- package/lib/transport/deliverables.d.ts +20 -0
- package/lib/transport/deliverables.d.ts.map +1 -0
- package/lib/transport/deliverables.js +72 -0
- package/lib/transport/deliverables.js.map +1 -0
- package/lib/transport/events.d.ts +117 -0
- package/lib/transport/events.d.ts.map +1 -0
- package/lib/transport/events.js +112 -0
- package/lib/transport/events.js.map +1 -0
- package/lib/transport/inbound.d.ts +11 -0
- package/lib/transport/inbound.d.ts.map +1 -0
- package/lib/transport/inbound.js +511 -0
- package/lib/transport/inbound.js.map +1 -0
- package/lib/transport/index.d.ts +10 -0
- package/lib/transport/index.d.ts.map +1 -0
- package/lib/transport/index.js +10 -0
- package/lib/transport/index.js.map +1 -0
- package/lib/transport/msgid-cache.d.ts +23 -0
- package/lib/transport/msgid-cache.d.ts.map +1 -0
- package/lib/transport/msgid-cache.js +57 -0
- package/lib/transport/msgid-cache.js.map +1 -0
- package/lib/transport/notify.js +147 -0
- package/lib/transport/outbound-buffer.d.ts +42 -0
- package/lib/transport/outbound-buffer.d.ts.map +1 -0
- package/lib/transport/outbound-buffer.js +68 -0
- package/lib/transport/outbound-buffer.js.map +1 -0
- package/lib/transport/outbound.d.ts +42 -0
- package/lib/transport/outbound.d.ts.map +1 -0
- package/lib/transport/outbound.js +183 -0
- package/lib/transport/outbound.js.map +1 -0
- package/lib/transport/reply-limiter.d.ts +31 -0
- package/lib/transport/reply-limiter.d.ts.map +1 -0
- package/lib/transport/reply-limiter.js +50 -0
- package/lib/transport/reply-limiter.js.map +1 -0
- package/lib/transport/reply-target.d.ts +32 -0
- package/lib/transport/reply-target.d.ts.map +1 -0
- package/lib/transport/reply-target.js +64 -0
- package/lib/transport/reply-target.js.map +1 -0
- package/lib/transport/streaming-writer.d.ts +55 -0
- package/lib/transport/streaming-writer.d.ts.map +1 -0
- package/lib/transport/streaming-writer.js +122 -0
- package/lib/transport/streaming-writer.js.map +1 -0
- package/lib/transport/tool-presenter.d.ts +37 -0
- package/lib/transport/tool-presenter.d.ts.map +1 -0
- package/lib/transport/tool-presenter.js +116 -0
- package/lib/transport/tool-presenter.js.map +1 -0
- package/lib/turn-check.js +85 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +5 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +127 -4
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import { persistCredentialsToProfile, removeCredentialsFromProfile, updateProfileConfig, } from "./persist.js";
|
|
2
|
+
import { getProfileDir } from "../shared/index.js";
|
|
3
|
+
import { AccessControlSchema } from "../config.js";
|
|
4
|
+
import { BIND_CATALOG_PATH, BIND_CONFIG_PATH, BIND_MANUAL_PATH, BIND_MEMORY_PATH, BIND_PERSONA_PATH, BIND_PICK_DIR_PATH, BIND_PORTRAIT_PATH, BIND_REFRESH_PATH, BIND_RULES_PATH, BIND_STATE_PATH, BIND_UNBIND_PATH, } from "../shared/bind-protocol.js";
|
|
5
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, unlinkSync, writeFileSync, appendFileSync } from 'node:fs';
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { homedir } from 'node:os';
|
|
9
|
+
import { dirname } from 'node:path';
|
|
10
|
+
import { DEFAULT_PERSONA, readPersona } from '../memory/persona.js';
|
|
11
|
+
import { readPortrait } from '../memory/portrait.js';
|
|
12
|
+
import { expandHome } from '../memory/runtime.js';
|
|
13
|
+
import { defaultPersonaFile, defaultPortraitFile } from '../memory/paths.js';
|
|
14
|
+
import { QQ_RULES_DEFAULT, RULES_FILE, qqRulesText } from '../rules.js';
|
|
15
|
+
// 协议单一来源:shared/bind-protocol.ts(类型 + 路径常量,客户端共用)
|
|
16
|
+
export { BIND_CATALOG_PATH, BIND_CONFIG_PATH, BIND_MANUAL_PATH, BIND_MEMORY_PATH, BIND_PERSONA_PATH, BIND_PICK_DIR_PATH, BIND_PORTRAIT_PATH, BIND_REFRESH_PATH, BIND_RULES_PATH, BIND_STATE_PATH, BIND_UNBIND_PATH };
|
|
17
|
+
/** 可经 UI 编辑的配置键(白名单:只收用户必要的字段,其余走 patch/env) */
|
|
18
|
+
const EDITABLE_CONFIG_KEYS = new Set([
|
|
19
|
+
'requireMention', 'allowSilence', 'streaming', 'midTurnSteer', 'resumeOnBoot', 'debug',
|
|
20
|
+
'burstWindowMs', 'maxBurstMs', 'maxBurstCount',
|
|
21
|
+
'provider', 'model', 'preset',
|
|
22
|
+
'groupPrompt', 'directPrompt',
|
|
23
|
+
'access',
|
|
24
|
+
'outreach', 'outreachPerDay', 'outreachWindows', 'outreachQuiet',
|
|
25
|
+
'outreachGapMin', 'outreachIdleMin', 'outreachWarmMin',
|
|
26
|
+
'sharedSession',
|
|
27
|
+
]);
|
|
28
|
+
/** 白名单键的子 Schema(服务端校验:客户端校验仅为体验,安全边界在宿主) */
|
|
29
|
+
const EDITABLE_CONFIG_SCHEMAS = {
|
|
30
|
+
requireMention: (v) => booleanSchema(v, 'requireMention'),
|
|
31
|
+
allowSilence: (v) => booleanSchema(v, 'allowSilence'),
|
|
32
|
+
streaming: (v) => booleanSchema(v, 'streaming'),
|
|
33
|
+
midTurnSteer: (v) => booleanSchema(v, 'midTurnSteer'),
|
|
34
|
+
resumeOnBoot: (v) => booleanSchema(v, 'resumeOnBoot'),
|
|
35
|
+
sharedSession: (v) => booleanSchema(v, 'sharedSession'),
|
|
36
|
+
debug: (v) => booleanSchema(v, 'debug'),
|
|
37
|
+
burstWindowMs: (v) => numberSchema(v, 'burstWindowMs', 0, 30000),
|
|
38
|
+
maxBurstMs: (v) => numberSchema(v, 'maxBurstMs', 1000, 180000),
|
|
39
|
+
maxBurstCount: (v) => numberSchema(v, 'maxBurstCount', 1, 50),
|
|
40
|
+
provider: stringSchema('provider'),
|
|
41
|
+
model: stringSchema('model'),
|
|
42
|
+
preset: stringSchema('preset'),
|
|
43
|
+
groupPrompt: stringSchema('groupPrompt'),
|
|
44
|
+
directPrompt: stringSchema('directPrompt'),
|
|
45
|
+
access: (v) => AccessControlSchema(v),
|
|
46
|
+
outreach: (v) => booleanSchema(v, 'outreach'),
|
|
47
|
+
outreachPerDay: (v) => numberSchema(v, 'outreachPerDay', 0, 6),
|
|
48
|
+
outreachWindows: (v) => stringSchema('outreachWindows'),
|
|
49
|
+
outreachQuiet: (v) => stringSchema('outreachQuiet'),
|
|
50
|
+
outreachGapMin: (v) => numberSchema(v, 'outreachGapMin', 30, 720),
|
|
51
|
+
outreachIdleMin: (v) => numberSchema(v, 'outreachIdleMin', 30, 720),
|
|
52
|
+
outreachWarmMin: (v) => numberSchema(v, 'outreachWarmMin', 15, 240),
|
|
53
|
+
};
|
|
54
|
+
function booleanSchema(value, name) {
|
|
55
|
+
if (typeof value !== 'boolean')
|
|
56
|
+
throw new TypeError(`${name} 必须是布尔值`);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
function stringSchema(name) {
|
|
60
|
+
return (value) => {
|
|
61
|
+
if (typeof value !== 'string')
|
|
62
|
+
throw new TypeError(`${name} 必须是字符串`);
|
|
63
|
+
if (value.length > 20000)
|
|
64
|
+
throw new TypeError(`${name} 过长`);
|
|
65
|
+
return value;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function numberSchema(value, name, min, max) {
|
|
69
|
+
const num = Number(value);
|
|
70
|
+
if (typeof value === 'boolean' || !Number.isFinite(num))
|
|
71
|
+
throw new TypeError(`${name} 必须是数字`);
|
|
72
|
+
if (num < min || num > max)
|
|
73
|
+
throw new TypeError(`${name} 必须在 ${min}~${max} 之间`);
|
|
74
|
+
return num;
|
|
75
|
+
}
|
|
76
|
+
/** manual 请求体大小上限(appId + appSecret 远小于此值) */
|
|
77
|
+
const MAX_MANUAL_BODY_BYTES = 4096;
|
|
78
|
+
/**
|
|
79
|
+
* config 请求体大小上限:对齐 Schema 的业务边界——白名单字符串字段单个
|
|
80
|
+
* 允许 20000 字符(全中文 UTF-8 约 60KB),allowlist 不设条目上限(openid
|
|
81
|
+
* 短标识,512KB 约容纳 1.4 万条)。上限只是反滥用闸门,语义校验在 Schema。
|
|
82
|
+
*/
|
|
83
|
+
const MAX_CONFIG_BODY_BYTES = 512 * 1024;
|
|
84
|
+
/** rules 请求体大小上限:正文上限 4000 字(全中文 UTF-8 约 12KB),64KB 足够且留余量 */
|
|
85
|
+
const MAX_RULES_BODY_BYTES = 64 * 1024;
|
|
86
|
+
/** 规则正文长度上限(按可见字符计,不是 UTF-16 码元) */
|
|
87
|
+
const MAX_RULES_CHARS = 4000;
|
|
88
|
+
/**
|
|
89
|
+
* 注册绑定路由(状态查询 / 刷新二维码 / 手动凭据)。
|
|
90
|
+
*
|
|
91
|
+
* @param webServer - ctx.webServer
|
|
92
|
+
* @param service - 绑定状态机;已绑定 fiber 传 null(无扫码流程)
|
|
93
|
+
* @param logger - 日志
|
|
94
|
+
* @param options - 行为注入(bound 态回显、manual 处理)
|
|
95
|
+
* @returns 注销函数(与插件 fiber 生命周期绑定)
|
|
96
|
+
*/
|
|
97
|
+
export function registerBindRoutes(webServer, service, logger, options = {}) {
|
|
98
|
+
const defaultManual = (credentials) => {
|
|
99
|
+
const persisted = persistCredentialsToProfile(credentials, getProfileDir() ?? undefined, logger);
|
|
100
|
+
return persisted ? { persisted: true } : { persisted: false, error: '无法定位 profile 目录或写入失败,请改用环境变量配置' };
|
|
101
|
+
};
|
|
102
|
+
const handleManualSubmit = options.onManualCredentials ?? defaultManual;
|
|
103
|
+
const disposers = [
|
|
104
|
+
webServer.register({
|
|
105
|
+
kind: 'exact',
|
|
106
|
+
path: BIND_STATE_PATH,
|
|
107
|
+
handler: (req, res) => { handleState(req, res, service, options); },
|
|
108
|
+
}),
|
|
109
|
+
webServer.register({
|
|
110
|
+
kind: 'exact',
|
|
111
|
+
path: BIND_REFRESH_PATH,
|
|
112
|
+
handler: (req, res) => { handleRefresh(req, res, service); },
|
|
113
|
+
}),
|
|
114
|
+
webServer.register({
|
|
115
|
+
kind: 'exact',
|
|
116
|
+
path: BIND_MANUAL_PATH,
|
|
117
|
+
handler: (req, res) => handleManual(req, res, handleManualSubmit),
|
|
118
|
+
}),
|
|
119
|
+
webServer.register({
|
|
120
|
+
kind: 'exact',
|
|
121
|
+
path: BIND_UNBIND_PATH,
|
|
122
|
+
handler: (req, res) => handleUnbind(req, res, options),
|
|
123
|
+
}),
|
|
124
|
+
webServer.register({
|
|
125
|
+
kind: 'exact',
|
|
126
|
+
path: BIND_CONFIG_PATH,
|
|
127
|
+
handler: (req, res) => handleConfig(req, res, options),
|
|
128
|
+
}),
|
|
129
|
+
webServer.register({
|
|
130
|
+
kind: 'exact',
|
|
131
|
+
path: BIND_CATALOG_PATH,
|
|
132
|
+
handler: (req, res) => handleCatalog(req, res, options),
|
|
133
|
+
}),
|
|
134
|
+
webServer.register({
|
|
135
|
+
kind: 'exact',
|
|
136
|
+
path: BIND_PERSONA_PATH,
|
|
137
|
+
handler: (req, res) => handlePersona(req, res, options),
|
|
138
|
+
}),
|
|
139
|
+
webServer.register({
|
|
140
|
+
kind: 'exact',
|
|
141
|
+
path: BIND_PORTRAIT_PATH,
|
|
142
|
+
handler: (req, res) => handlePortrait(req, res, options),
|
|
143
|
+
}),
|
|
144
|
+
webServer.register({
|
|
145
|
+
kind: 'exact',
|
|
146
|
+
path: BIND_MEMORY_PATH,
|
|
147
|
+
handler: (req, res) => handleMemory(req, res, options),
|
|
148
|
+
}),
|
|
149
|
+
webServer.register({
|
|
150
|
+
kind: 'exact',
|
|
151
|
+
path: BIND_RULES_PATH,
|
|
152
|
+
handler: (req, res) => handleRules(req, res),
|
|
153
|
+
}),
|
|
154
|
+
webServer.register({
|
|
155
|
+
kind: 'exact',
|
|
156
|
+
path: BIND_PICK_DIR_PATH,
|
|
157
|
+
handler: (req, res) => handlePickDir(req, res),
|
|
158
|
+
}),
|
|
159
|
+
];
|
|
160
|
+
logger.info(`im-qqbot: 端内绑定页已就绪: ${BIND_STATE_PATH}`);
|
|
161
|
+
return () => { for (const dispose of disposers)
|
|
162
|
+
dispose(); };
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 请求来源校验(最小 CSRF 防护)。
|
|
166
|
+
*
|
|
167
|
+
* 带 Origin 的请求必须与 Host 同源;同源 fetch 与 Desktop IPC 桥
|
|
168
|
+
* (后者会删除 origin 头)均放行。本路由数据不敏感且动作无害,
|
|
169
|
+
* 该级别的防护足够。
|
|
170
|
+
*/
|
|
171
|
+
function trusted(req) {
|
|
172
|
+
const origin = req.headers.origin;
|
|
173
|
+
if (origin === undefined)
|
|
174
|
+
return true;
|
|
175
|
+
try {
|
|
176
|
+
return new URL(origin).host === req.headers.host;
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function sendJson(res, status, body) {
|
|
183
|
+
const payload = JSON.stringify(body);
|
|
184
|
+
res.writeHead(status, {
|
|
185
|
+
'content-type': 'application/json; charset=utf-8',
|
|
186
|
+
'cache-control': 'no-store',
|
|
187
|
+
});
|
|
188
|
+
res.end(payload);
|
|
189
|
+
}
|
|
190
|
+
function handleState(req, res, service, options) {
|
|
191
|
+
if (!trusted(req)) {
|
|
192
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
196
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
// 已绑定优先(appSecret 不回传,appId 非机密可回显)
|
|
200
|
+
const boundAppId = options.boundAppId?.() ?? null;
|
|
201
|
+
if (boundAppId !== null) {
|
|
202
|
+
sendJson(res, 200, { phase: 'bound', appId: boundAppId });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
sendJson(res, 200, service === null ? { phase: 'starting' } : service.wireState());
|
|
206
|
+
}
|
|
207
|
+
function handleRefresh(req, res, service) {
|
|
208
|
+
if (!trusted(req)) {
|
|
209
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (req.method !== 'POST') {
|
|
213
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
// 丢弃可能的请求体(refresh 无参数)
|
|
217
|
+
req.resume();
|
|
218
|
+
service?.refresh();
|
|
219
|
+
// 新二维码异步生成,统一由状态轮询收敛
|
|
220
|
+
sendJson(res, 200, { phase: 'starting' });
|
|
221
|
+
}
|
|
222
|
+
/** 读取并解析 JSON 请求体(带大小上限) */
|
|
223
|
+
async function readJsonBody(req, maxBytes = MAX_MANUAL_BODY_BYTES) {
|
|
224
|
+
const chunks = [];
|
|
225
|
+
let size = 0;
|
|
226
|
+
for await (const chunk of req) {
|
|
227
|
+
const buffer = chunk;
|
|
228
|
+
size += buffer.length;
|
|
229
|
+
if (size > maxBytes)
|
|
230
|
+
throw new Error('request body too large');
|
|
231
|
+
chunks.push(buffer);
|
|
232
|
+
}
|
|
233
|
+
if (size === 0)
|
|
234
|
+
throw new Error('empty request body');
|
|
235
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
236
|
+
}
|
|
237
|
+
/** 校验手动提交的凭据形态(不打印内容) */
|
|
238
|
+
function parseCredentials(body) {
|
|
239
|
+
if (typeof body !== 'object' || body === null)
|
|
240
|
+
return null;
|
|
241
|
+
const record = body;
|
|
242
|
+
const appId = typeof record.appId === 'string' ? record.appId.trim() : '';
|
|
243
|
+
const appSecret = typeof record.appSecret === 'string' ? record.appSecret.trim() : '';
|
|
244
|
+
if (appId.length === 0 || appSecret.length === 0)
|
|
245
|
+
return null;
|
|
246
|
+
if (appId.length > 128 || appSecret.length > 256)
|
|
247
|
+
return null;
|
|
248
|
+
return { appId, appSecret };
|
|
249
|
+
}
|
|
250
|
+
async function handleManual(req, res, submit) {
|
|
251
|
+
if (!trusted(req)) {
|
|
252
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (req.method !== 'POST') {
|
|
256
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
let body;
|
|
260
|
+
try {
|
|
261
|
+
body = await readJsonBody(req);
|
|
262
|
+
}
|
|
263
|
+
catch (err) {
|
|
264
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const credentials = parseCredentials(body);
|
|
268
|
+
if (credentials === null) {
|
|
269
|
+
sendJson(res, 400, { ok: false, error: 'appId 与 appSecret 不能为空' });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const result = submit(credentials);
|
|
273
|
+
if (!result.persisted) {
|
|
274
|
+
sendJson(res, 500, { ok: false, error: result.error ?? '保存失败' });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// 写入 profile patch 触发 HMR 重载,新 fiber 以新凭据启动网关
|
|
278
|
+
sendJson(res, 200, { ok: true, message: '凭据已保存,插件将自动重载' });
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* 模型 / 预设目录:设置面板的 Provider、Model、Preset 下拉都从这里取。
|
|
282
|
+
* provider 与模型来自宿主 llm 服务;预设来自 agentPresets 服务。
|
|
283
|
+
* 目录读不到时返回空数组,客户端会退回到纯文本输入。
|
|
284
|
+
*/
|
|
285
|
+
async function handleCatalog(req, res, options) {
|
|
286
|
+
if (!trusted(req)) {
|
|
287
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
291
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
let value;
|
|
295
|
+
try {
|
|
296
|
+
value = (await options.catalog?.()) ?? { providers: [], presets: [] };
|
|
297
|
+
}
|
|
298
|
+
catch (err) {
|
|
299
|
+
value = { providers: [], presets: [], error: err instanceof Error ? err.message : String(err) };
|
|
300
|
+
}
|
|
301
|
+
sendJson(res, 200, value);
|
|
302
|
+
}
|
|
303
|
+
/** 配置里的记忆 / 人设路径;与 SessionManager 的默认规则保持一致。 */
|
|
304
|
+
function memorySettings(config) {
|
|
305
|
+
return config?.memory ?? {};
|
|
306
|
+
}
|
|
307
|
+
function resolveConfigPath(raw, fallback) {
|
|
308
|
+
const text = expandHome(String(raw ?? '').trim(), homedir());
|
|
309
|
+
return text === '' ? fallback : text;
|
|
310
|
+
}
|
|
311
|
+
function personaFileOf(config) {
|
|
312
|
+
return resolveConfigPath(memorySettings(config).personaFile, defaultPersonaFile());
|
|
313
|
+
}
|
|
314
|
+
function portraitFileOf(config) {
|
|
315
|
+
return resolveConfigPath(memorySettings(config).portraitFile, defaultPortraitFile());
|
|
316
|
+
}
|
|
317
|
+
function writeTextFile(file, text) {
|
|
318
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
319
|
+
writeFileSync(file, text, 'utf8');
|
|
320
|
+
}
|
|
321
|
+
function personaView(config) {
|
|
322
|
+
const file = personaFileOf(config);
|
|
323
|
+
const current = readPersona(file);
|
|
324
|
+
return { text: current.text, defaultText: DEFAULT_PERSONA, custom: current.custom === true, file };
|
|
325
|
+
}
|
|
326
|
+
function portraitView(config) {
|
|
327
|
+
const file = portraitFileOf(config);
|
|
328
|
+
const current = readPortrait(file);
|
|
329
|
+
return { text: current.text, custom: current.custom === true, file };
|
|
330
|
+
}
|
|
331
|
+
/** 记忆视图:现在读的是 dsh-fishmemory 的笔记与转录(记忆已经搬出 QQ 插件)。 */
|
|
332
|
+
function fishmemoryHome() {
|
|
333
|
+
return join(homedir(), '.dsh', 'dsh-fishmemory');
|
|
334
|
+
}
|
|
335
|
+
function listMemoryNotes() {
|
|
336
|
+
const dir = join(fishmemoryHome(), 'notes');
|
|
337
|
+
let files = [];
|
|
338
|
+
try {
|
|
339
|
+
files = readdirSync(dir).filter((name) => name.endsWith('.md'));
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return [];
|
|
343
|
+
}
|
|
344
|
+
const notes = [];
|
|
345
|
+
for (const name of files) {
|
|
346
|
+
try {
|
|
347
|
+
const raw = readFileSync(join(dir, name), 'utf8');
|
|
348
|
+
const meta = {};
|
|
349
|
+
let body = raw;
|
|
350
|
+
if (raw.startsWith('---')) {
|
|
351
|
+
const stop = raw.indexOf('\n---', 3);
|
|
352
|
+
if (stop >= 0) {
|
|
353
|
+
body = raw.slice(stop + 4).replace(/^\n/, '');
|
|
354
|
+
for (const line of raw.slice(3, stop).trim().split('\n')) {
|
|
355
|
+
const at = line.indexOf(':');
|
|
356
|
+
if (at < 0)
|
|
357
|
+
continue;
|
|
358
|
+
meta[line.slice(0, at).trim()] = line.slice(at + 1).trim().replace(/^\[(.*)\]$/, '$1');
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
notes.push({
|
|
363
|
+
id: meta.id ?? name.replace(/\.md$/, ''),
|
|
364
|
+
text: String(meta.title ?? '').trim() + '\n' + body.trim(),
|
|
365
|
+
at: meta.updated ?? meta.created ?? null,
|
|
366
|
+
role: meta.kind ?? 'note',
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
catch { /* 坏文件跳过 */ }
|
|
370
|
+
}
|
|
371
|
+
notes.sort((left, right) => String(right.at ?? '').localeCompare(String(left.at ?? '')));
|
|
372
|
+
return notes;
|
|
373
|
+
}
|
|
374
|
+
function listMemoryJournal(limit = 40) {
|
|
375
|
+
const dir = join(fishmemoryHome(), 'journal');
|
|
376
|
+
let files = [];
|
|
377
|
+
try {
|
|
378
|
+
files = readdirSync(dir).filter((name) => name.endsWith('.jsonl')).sort().slice(-2);
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
return [];
|
|
382
|
+
}
|
|
383
|
+
const rows = [];
|
|
384
|
+
for (const name of files) {
|
|
385
|
+
try {
|
|
386
|
+
for (const line of readFileSync(join(dir, name), 'utf8').split('\n')) {
|
|
387
|
+
if (line.trim() === '')
|
|
388
|
+
continue;
|
|
389
|
+
try {
|
|
390
|
+
const row = JSON.parse(line);
|
|
391
|
+
rows.push({ id: '', text: row.text ?? '', at: row.at ?? null, role: row.role ?? '' });
|
|
392
|
+
}
|
|
393
|
+
catch { /* 坏行跳过 */ }
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
catch { /* ignore */ }
|
|
397
|
+
}
|
|
398
|
+
return rows.slice(-limit);
|
|
399
|
+
}
|
|
400
|
+
function memoryView(_config, _key) {
|
|
401
|
+
const facts = listMemoryNotes();
|
|
402
|
+
const episodes = listMemoryJournal();
|
|
403
|
+
return {
|
|
404
|
+
file: join(fishmemoryHome(), 'index.md'),
|
|
405
|
+
key: 'fishmemory',
|
|
406
|
+
buckets: [{ key: 'fishmemory', facts: facts.length, episodes: episodes.length, linkedFrom: [] }],
|
|
407
|
+
facts,
|
|
408
|
+
episodes,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
/** 人设:GET 读当前文本,POST 写回共享的 persona.md(默认文本也可恢复)。 */
|
|
412
|
+
async function handlePersona(req, res, options) {
|
|
413
|
+
if (!trusted(req)) {
|
|
414
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (req.method !== 'GET' && req.method !== 'POST' && req.method !== 'HEAD') {
|
|
418
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const config = options.currentConfig?.() ?? {};
|
|
422
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
423
|
+
sendJson(res, 200, personaView(config));
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
let body;
|
|
427
|
+
try {
|
|
428
|
+
body = await readJsonBody(req, MAX_CONFIG_BODY_BYTES);
|
|
429
|
+
}
|
|
430
|
+
catch (err) {
|
|
431
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const text = typeof body?.text === 'string' ? body.text : '';
|
|
435
|
+
try {
|
|
436
|
+
writeTextFile(personaFileOf(config), text);
|
|
437
|
+
}
|
|
438
|
+
catch (err) {
|
|
439
|
+
sendJson(res, 500, { ok: false, error: `写入人设失败:${err instanceof Error ? err.message : String(err)}` });
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
sendJson(res, 200, { ok: true, ...personaView(config) });
|
|
443
|
+
}
|
|
444
|
+
/** 画像:与 persona 相同的读写方式,文件与 dsh-qq 共用。 */
|
|
445
|
+
async function handlePortrait(req, res, options) {
|
|
446
|
+
if (!trusted(req)) {
|
|
447
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (req.method !== 'GET' && req.method !== 'POST' && req.method !== 'HEAD') {
|
|
451
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const config = options.currentConfig?.() ?? {};
|
|
455
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
456
|
+
sendJson(res, 200, portraitView(config));
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
let body;
|
|
460
|
+
try {
|
|
461
|
+
body = await readJsonBody(req, MAX_CONFIG_BODY_BYTES);
|
|
462
|
+
}
|
|
463
|
+
catch (err) {
|
|
464
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const text = typeof body?.text === 'string' ? body.text : '';
|
|
468
|
+
try {
|
|
469
|
+
writeTextFile(portraitFileOf(config), text);
|
|
470
|
+
}
|
|
471
|
+
catch (err) {
|
|
472
|
+
sendJson(res, 500, { ok: false, error: `写入画像失败:${err instanceof Error ? err.message : String(err)}` });
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
sendJson(res, 200, { ok: true, ...portraitView(config) });
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* 规则视图:与 lib/rules.js 的读取口径完全一致——有 rules.md 且非空就是
|
|
479
|
+
* 自定义,否则是内置默认。custom 用于设置页标题旁的「默认」标记。
|
|
480
|
+
*/
|
|
481
|
+
function rulesView() {
|
|
482
|
+
let custom = false;
|
|
483
|
+
try {
|
|
484
|
+
custom = readFileSync(RULES_FILE, 'utf8').trim() !== '';
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
custom = false;
|
|
488
|
+
}
|
|
489
|
+
return { ok: true, text: qqRulesText(), defaultText: QQ_RULES_DEFAULT, custom, file: RULES_FILE };
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* 规则(在 QQ 里怎么回话):GET 读当前文本,POST 写入 rules.md,
|
|
493
|
+
* DELETE 删掉 rules.md 回到内置默认。
|
|
494
|
+
*
|
|
495
|
+
* 写入前先按可见字符数卡 4000 字上限(超了 400,不落盘),空文本等同
|
|
496
|
+
* DELETE——用户把框清空再点保存就是恢复默认,不用再找「恢复默认」按钮。
|
|
497
|
+
*/
|
|
498
|
+
async function handleRules(req, res) {
|
|
499
|
+
if (!trusted(req)) {
|
|
500
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (req.method !== 'GET' && req.method !== 'POST' && req.method !== 'DELETE' && req.method !== 'HEAD') {
|
|
504
|
+
sendJson(res, 405, { ok: false, error: 'method not allowed' });
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
508
|
+
sendJson(res, 200, rulesView());
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (req.method === 'DELETE') {
|
|
512
|
+
req.resume();
|
|
513
|
+
try {
|
|
514
|
+
rmSync(RULES_FILE, { force: true });
|
|
515
|
+
}
|
|
516
|
+
catch (err) {
|
|
517
|
+
sendJson(res, 500, { ok: false, error: `恢复默认失败:${err instanceof Error ? err.message : String(err)}` });
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
sendJson(res, 200, { message: '已恢复默认规则,下一轮生效', ...rulesView() });
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
let body;
|
|
524
|
+
try {
|
|
525
|
+
body = await readJsonBody(req, MAX_RULES_BODY_BYTES);
|
|
526
|
+
}
|
|
527
|
+
catch (err) {
|
|
528
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
const text = typeof body?.text === 'string' ? body.text : '';
|
|
532
|
+
// 空文本(含只有空白)视为恢复默认:删文件,读取侧自动落回内置默认。
|
|
533
|
+
if (text.trim() === '') {
|
|
534
|
+
try {
|
|
535
|
+
rmSync(RULES_FILE, { force: true });
|
|
536
|
+
}
|
|
537
|
+
catch (err) {
|
|
538
|
+
sendJson(res, 500, { ok: false, error: `恢复默认失败:${err instanceof Error ? err.message : String(err)}` });
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
sendJson(res, 200, { message: '已恢复默认规则,下一轮生效', ...rulesView() });
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const length = [...text].length;
|
|
545
|
+
if (length > MAX_RULES_CHARS) {
|
|
546
|
+
sendJson(res, 400, { ok: false, error: `规则太长(${length} 字,上限 ${MAX_RULES_CHARS})` });
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
writeTextFile(RULES_FILE, text);
|
|
551
|
+
}
|
|
552
|
+
catch (err) {
|
|
553
|
+
sendJson(res, 500, { ok: false, error: `写入规则失败:${err instanceof Error ? err.message : String(err)}` });
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
sendJson(res, 200, { message: '规则已保存,下一轮生效', ...rulesView() });
|
|
557
|
+
}
|
|
558
|
+
/** 记忆:GET 列会话桶/细节,POST 删除单条。 */
|
|
559
|
+
async function handleMemory(req, res, options) {
|
|
560
|
+
if (!trusted(req)) {
|
|
561
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (req.method !== 'GET' && req.method !== 'POST' && req.method !== 'HEAD') {
|
|
565
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const config = options.currentConfig?.() ?? {};
|
|
569
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
570
|
+
let key = '';
|
|
571
|
+
try {
|
|
572
|
+
key = new URL(req.url ?? '/', 'http://localhost').searchParams.get('key') ?? '';
|
|
573
|
+
}
|
|
574
|
+
catch {
|
|
575
|
+
key = '';
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
sendJson(res, 200, memoryView(config, key));
|
|
579
|
+
}
|
|
580
|
+
catch (err) {
|
|
581
|
+
// 诊断用:GET 分支里任何一步抛错都要留下真话,别让宿主回一个空 400。
|
|
582
|
+
try {
|
|
583
|
+
appendFileSync('/tmp/im-memory-error.log', `${new Date().toISOString()} ${err instanceof Error ? err.stack ?? err.message : String(err)}\n`);
|
|
584
|
+
}
|
|
585
|
+
catch { /* 诊断落盘失败就别管了 */ }
|
|
586
|
+
try {
|
|
587
|
+
sendJson(res, 500, { ok: false, error: `读取记忆失败:${err instanceof Error ? err.message : String(err)}` });
|
|
588
|
+
}
|
|
589
|
+
catch { /* 头已经发出去就只能算了 */ }
|
|
590
|
+
}
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
let body;
|
|
594
|
+
try {
|
|
595
|
+
body = await readJsonBody(req, MAX_CONFIG_BODY_BYTES);
|
|
596
|
+
}
|
|
597
|
+
catch (err) {
|
|
598
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
const action = String(body?.action ?? '');
|
|
602
|
+
const key = String(body?.key ?? '').trim();
|
|
603
|
+
if (action !== 'forget') {
|
|
604
|
+
sendJson(res, 400, { ok: false, error: `不支持的记忆操作: ${action}` });
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
if (key === '') {
|
|
608
|
+
sendJson(res, 400, { ok: false, error: '缺少会话 key' });
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const id = String(body?.id ?? '').trim();
|
|
612
|
+
let removed = 0;
|
|
613
|
+
if (id !== '') {
|
|
614
|
+
try {
|
|
615
|
+
unlinkSync(join(fishmemoryHome(), 'notes', `${id}.md`));
|
|
616
|
+
removed = 1;
|
|
617
|
+
}
|
|
618
|
+
catch {
|
|
619
|
+
removed = 0;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
sendJson(res, 200, { ok: true, removed, ...memoryView(config, key) });
|
|
623
|
+
}
|
|
624
|
+
/** 用系统选择器选一个文件夹(macOS 的 choose folder),取消时返回 path: null。 */
|
|
625
|
+
const execFileAsync = promisify(execFile);
|
|
626
|
+
async function handlePickDir(req, res) {
|
|
627
|
+
if (!trusted(req)) {
|
|
628
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
if (req.method !== 'POST') {
|
|
632
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
req.resume();
|
|
636
|
+
if (process.platform !== 'darwin') {
|
|
637
|
+
sendJson(res, 501, { ok: false, error: '当前只支持 macOS 系统选择器,请手动填写路径' });
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
try {
|
|
641
|
+
const { stdout } = await execFileAsync('osascript', ['-e', 'POSIX path of (choose folder with prompt "选择表情包文件夹")'], { timeout: 120000 });
|
|
642
|
+
const picked = String(stdout ?? '').trim().replace(/\/$/, '');
|
|
643
|
+
sendJson(res, 200, { ok: true, path: picked === '' ? null : picked });
|
|
644
|
+
}
|
|
645
|
+
catch (error) {
|
|
646
|
+
const message = String(error?.stderr ?? error?.message ?? error);
|
|
647
|
+
// 用户点取消 = osascript 非 0;按「没选」处理,不报错。
|
|
648
|
+
if (/User cancelled|User canceled|-128/.test(message)) {
|
|
649
|
+
sendJson(res, 200, { ok: true, path: null });
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
sendJson(res, 500, { ok: false, error: message.slice(0, 300) });
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
/** 解绑:仅已绑定态可执行;移除 profile patch 中的凭据并触发 HMR 重载 */
|
|
656
|
+
function handleUnbind(req, res, options) {
|
|
657
|
+
if (!trusted(req)) {
|
|
658
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (req.method !== 'POST') {
|
|
662
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
req.resume();
|
|
666
|
+
if ((options.boundAppId?.() ?? null) === null) {
|
|
667
|
+
sendJson(res, 409, { ok: false, error: '当前未绑定' });
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
const result = options.onUnbind === undefined
|
|
671
|
+
? (() => {
|
|
672
|
+
const removed = removeCredentialsFromProfile(getProfileDir() ?? undefined, undefined);
|
|
673
|
+
if (removed) {
|
|
674
|
+
// 清除进程内凭据:扫码绑定成功时会写入 env(供热更新后的下次
|
|
675
|
+
// apply 兜底读取);不清则 HMR 重载后新 fiber 仍从 env 读到
|
|
676
|
+
// 凭据,解绑不生效(状态一直停留在 bound)
|
|
677
|
+
delete process.env.QQBOT_APPID;
|
|
678
|
+
delete process.env.QQBOT_SECRET;
|
|
679
|
+
}
|
|
680
|
+
return removed
|
|
681
|
+
? { persisted: true }
|
|
682
|
+
: { persisted: false, error: '无法定位 profile 目录或写入失败,请手动编辑 cordis.patch.yml' };
|
|
683
|
+
})()
|
|
684
|
+
: options.onUnbind();
|
|
685
|
+
if (!result.persisted) {
|
|
686
|
+
sendJson(res, 500, { ok: false, error: result.error ?? '解绑失败' });
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
// 移除后写入触发 HMR 重载,插件回到未绑定态(绑定流程重新可用)
|
|
690
|
+
sendJson(res, 200, { ok: true, message: '已解绑,插件将重载并重新进入绑定流程' });
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* 配置读写:
|
|
694
|
+
* - GET:回显当前生效配置(appSecret 剔除,cwd 等只读字段正常返回)
|
|
695
|
+
* - POST:白名单 + 子 Schema 校验 → 最小写入 profile patch → HMR 重载
|
|
696
|
+
*/
|
|
697
|
+
async function handleConfig(req, res, options) {
|
|
698
|
+
if (!trusted(req)) {
|
|
699
|
+
sendJson(res, 403, { error: 'forbidden' });
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
if (req.method !== 'GET' && req.method !== 'POST') {
|
|
703
|
+
sendJson(res, 405, { error: 'method not allowed' });
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
if (req.method === 'GET') {
|
|
707
|
+
const current = options.currentConfig?.() ?? {};
|
|
708
|
+
// appSecret 永不回传;其余(含只读 cwd)原样回显
|
|
709
|
+
const { appSecret: _s, ...safe } = current;
|
|
710
|
+
sendJson(res, 200, { config: safe });
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
let body;
|
|
714
|
+
try {
|
|
715
|
+
body = await readJsonBody(req, MAX_CONFIG_BODY_BYTES);
|
|
716
|
+
}
|
|
717
|
+
catch (err) {
|
|
718
|
+
sendJson(res, 400, { ok: false, error: `invalid request body: ${err instanceof Error ? err.message : String(err)}` });
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
if (typeof body !== 'object' || body === null) {
|
|
722
|
+
sendJson(res, 400, { ok: false, error: '请求体必须是 JSON 对象' });
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const submitted = body;
|
|
726
|
+
// 白名单与凭据/只读字段拦截
|
|
727
|
+
for (const key of Object.keys(submitted)) {
|
|
728
|
+
if (!EDITABLE_CONFIG_KEYS.has(key)) {
|
|
729
|
+
const reason = key === 'appId' || key === 'appSecret'
|
|
730
|
+
? '凭据请使用绑定/更换/解绑功能'
|
|
731
|
+
: key === 'cwd'
|
|
732
|
+
? 'cwd 为只读字段'
|
|
733
|
+
: '不支持的字段';
|
|
734
|
+
sendJson(res, 400, { ok: false, error: `${reason}: ${key}` });
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
// 逐键 Schema 校验(非法值拒绝,返回首个错误)
|
|
739
|
+
let validated = {};
|
|
740
|
+
try {
|
|
741
|
+
validated = Object.fromEntries(Object.entries(submitted).map(([key, value]) => {
|
|
742
|
+
const check = EDITABLE_CONFIG_SCHEMAS[key];
|
|
743
|
+
if (check === undefined)
|
|
744
|
+
throw new TypeError(`不支持的字段: ${key}`);
|
|
745
|
+
return [key, check(value)];
|
|
746
|
+
}));
|
|
747
|
+
}
|
|
748
|
+
catch (err) {
|
|
749
|
+
sendJson(res, 400, { ok: false, error: err instanceof TypeError ? err.message : String(err) });
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
const persisted = options.onUpdateConfig === undefined
|
|
753
|
+
? updateProfileConfig(validated, getProfileDir() ?? undefined, undefined)
|
|
754
|
+
: options.onUpdateConfig(validated);
|
|
755
|
+
if (!persisted) {
|
|
756
|
+
sendJson(res, 500, { ok: false, error: '无法定位 profile 目录或写入失败' });
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
sendJson(res, 200, { ok: true, message: '配置已保存,插件将自动重载(进行中的机器人会话将重置)' });
|
|
760
|
+
}
|
|
761
|
+
//# sourceMappingURL=bind-routes.js.map
|