luckytri 1.0.0

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.
Files changed (235) hide show
  1. package/.github/SECURITY.md +71 -0
  2. package/LICENSE +21 -0
  3. package/README.en.md +250 -0
  4. package/README.md +250 -0
  5. package/bin/luckytri.js +107 -0
  6. package/docs/brand/banner.png +0 -0
  7. package/docs/brand/palette.png +0 -0
  8. package/docs/brand/portrait.png +0 -0
  9. package/docs/en/CHANGELOG.md +113 -0
  10. package/docs/en/architecture.md +152 -0
  11. package/docs/en/chat-style.md +62 -0
  12. package/docs/en/connect.md +63 -0
  13. package/docs/en/guide.md +216 -0
  14. package/docs/en/her-life.md +189 -0
  15. package/docs/en/npm-release.md +25 -0
  16. package/docs/en/plugins.md +82 -0
  17. package/docs/en/recovery.md +28 -0
  18. package/docs/en/time.md +71 -0
  19. package/docs/en/webui.md +73 -0
  20. package/docs/zh/CHANGELOG.md +113 -0
  21. package/docs/zh/architecture.md +152 -0
  22. package/docs/zh/chat-style.md +62 -0
  23. package/docs/zh/connect.md +63 -0
  24. package/docs/zh/guide.md +216 -0
  25. package/docs/zh/her-life.md +189 -0
  26. package/docs/zh/npm-release.md +33 -0
  27. package/docs/zh/plugins.md +82 -0
  28. package/docs/zh/recovery.md +28 -0
  29. package/docs/zh/time.md +71 -0
  30. package/docs/zh/webui.md +73 -0
  31. package/package.json +108 -0
  32. package/plugins/sdk/index.d.ts +155 -0
  33. package/plugins/sdk/testing.js +58 -0
  34. package/plugins/weather/index.js +82 -0
  35. package/plugins/weather/luckytri-plugin.json +29 -0
  36. package/public/app/assets/ChatsPage-C-FcRapR.js +3 -0
  37. package/public/app/assets/ChatsPage-DfyPCS9N.css +1 -0
  38. package/public/app/assets/Empty-CpP5kQHH.js +2 -0
  39. package/public/app/assets/Empty-DmUB2-Bk.css +1 -0
  40. package/public/app/assets/Field-D4gY-INU.js +1 -0
  41. package/public/app/assets/HeartPage-Bft9wZp5.css +1 -0
  42. package/public/app/assets/HeartPage-CPy_thQd.js +1 -0
  43. package/public/app/assets/LifePage-BUi5euBF.css +1 -0
  44. package/public/app/assets/LifePage-CxWvwmSf.js +1 -0
  45. package/public/app/assets/MemoryPage-DC4rhQ68.js +1 -0
  46. package/public/app/assets/MemoryPage-cZvXfkXb.css +1 -0
  47. package/public/app/assets/NaturePage-CTXHCE2D.css +1 -0
  48. package/public/app/assets/NaturePage-IvYw04Zq.js +3 -0
  49. package/public/app/assets/PeoplePage-BmUxkrOU.css +1 -0
  50. package/public/app/assets/PeoplePage-Cqefm9-q.js +1 -0
  51. package/public/app/assets/PluginsPage-DKSaKvXb.js +2 -0
  52. package/public/app/assets/PluginsPage-HyBe5D8w.css +1 -0
  53. package/public/app/assets/SystemPage-CAMPoYqM.css +1 -0
  54. package/public/app/assets/SystemPage-CJmtJkrK.js +1 -0
  55. package/public/app/assets/TimePage-BOms_n4I.css +1 -0
  56. package/public/app/assets/TimePage-Cw44inM4.js +3 -0
  57. package/public/app/assets/formatters-ZmW_xFHw.js +1 -0
  58. package/public/app/assets/index-BheRW-1t.js +4 -0
  59. package/public/app/assets/index-YDz1ree1.css +1 -0
  60. package/public/app/assets/knowledge-DG0e0uKw.js +1 -0
  61. package/public/app/assets/liquid-garden-IMoHBwzc.png +0 -0
  62. package/public/app/assets/luckytri-home-95QKeMtc.png +0 -0
  63. package/public/app/assets/media-BzPDBm7e.js +1 -0
  64. package/public/app/index.html +27 -0
  65. package/public/guide.en.html +1053 -0
  66. package/public/guide.html +893 -0
  67. package/public/plugin-kit/v1/bridge.js +30 -0
  68. package/public/plugin-kit/v1/kit.css +14 -0
  69. package/public/tutorial.en.md +214 -0
  70. package/public/tutorial.md +214 -0
  71. package/scripts/backup.js +311 -0
  72. package/scripts/browser.js +41 -0
  73. package/scripts/launch.js +94 -0
  74. package/scripts/plugin.js +150 -0
  75. package/scripts/proxy-config.js +167 -0
  76. package/scripts/recover.js +26 -0
  77. package/scripts/runtime.js +25 -0
  78. package/scripts/service.js +47 -0
  79. package/scripts/setup.js +50 -0
  80. package/scripts/start-server.js +41 -0
  81. package/scripts/stop.js +36 -0
  82. package/server/app.js +96 -0
  83. package/server/channels/adapters.js +27 -0
  84. package/server/channels/contract.js +57 -0
  85. package/server/channels/hub.js +165 -0
  86. package/server/channels/index.js +25 -0
  87. package/server/channels/onebot/adapter.js +288 -0
  88. package/server/channels/onebot/index.js +336 -0
  89. package/server/channels/onebot/token.js +3 -0
  90. package/server/channels/qqbot/adapter.js +253 -0
  91. package/server/channels/qqbot/api.js +199 -0
  92. package/server/channels/qqbot/credentials.js +11 -0
  93. package/server/channels/qqbot/gateway.js +274 -0
  94. package/server/channels/qqbot/index.js +389 -0
  95. package/server/channels/segments.js +40 -0
  96. package/server/channels/session-key.js +153 -0
  97. package/server/channels/state.js +42 -0
  98. package/server/core/api.js +564 -0
  99. package/server/core/capabilities.js +208 -0
  100. package/server/core/context-builder.js +288 -0
  101. package/server/core/context-compactor.js +454 -0
  102. package/server/core/conversation-cues.js +325 -0
  103. package/server/core/conversation-manager.js +87 -0
  104. package/server/core/events.js +60 -0
  105. package/server/core/feedback.js +8 -0
  106. package/server/core/group-style.js +72 -0
  107. package/server/core/image-preprocessor.js +208 -0
  108. package/server/core/initiative-context.js +66 -0
  109. package/server/core/input-budget.js +32 -0
  110. package/server/core/local-demo.js +98 -0
  111. package/server/core/message-manager.js +118 -0
  112. package/server/core/message-scheduler.js +78 -0
  113. package/server/core/model-json.js +190 -0
  114. package/server/core/model-manager.js +1213 -0
  115. package/server/core/model-presets.js +900 -0
  116. package/server/core/name-call.js +29 -0
  117. package/server/core/net-guard.js +49 -0
  118. package/server/core/network.js +157 -0
  119. package/server/core/orchestrator.js +569 -0
  120. package/server/core/perceivers.js +91 -0
  121. package/server/core/person-name.js +22 -0
  122. package/server/core/persona-manager.js +243 -0
  123. package/server/core/reply-delivery.js +393 -0
  124. package/server/core/reply-target-resolver.js +50 -0
  125. package/server/core/repository.js +293 -0
  126. package/server/core/response-generator.js +50 -0
  127. package/server/core/response-validator.js +508 -0
  128. package/server/core/retired-prompts.js +51 -0
  129. package/server/core/sessions.js +109 -0
  130. package/server/core/speaker-names.js +92 -0
  131. package/server/core/topic-tracker.js +65 -0
  132. package/server/core/turn-processor.js +630 -0
  133. package/server/core/turn.js +236 -0
  134. package/server/core/vision-manager.js +633 -0
  135. package/server/http.js +18 -0
  136. package/server/i18n/decisions.js +206 -0
  137. package/server/i18n/errors.js +497 -0
  138. package/server/i18n/index.js +144 -0
  139. package/server/i18n/labels.js +653 -0
  140. package/server/i18n/models.js +111 -0
  141. package/server/index.js +102 -0
  142. package/server/knowledge/api.js +342 -0
  143. package/server/knowledge/embedding-profile.js +114 -0
  144. package/server/knowledge/index.js +13 -0
  145. package/server/knowledge/manager.js +386 -0
  146. package/server/knowledge/retrieval.js +93 -0
  147. package/server/knowledge/schema.js +154 -0
  148. package/server/knowledge/vector-index.js +90 -0
  149. package/server/mind/affect.js +265 -0
  150. package/server/mind/anticipations.js +507 -0
  151. package/server/mind/api.js +703 -0
  152. package/server/mind/attention.js +200 -0
  153. package/server/mind/bonds.js +558 -0
  154. package/server/mind/budget.js +152 -0
  155. package/server/mind/clock.js +83 -0
  156. package/server/mind/context-selection.js +61 -0
  157. package/server/mind/continuity.js +562 -0
  158. package/server/mind/conversation-origin.js +175 -0
  159. package/server/mind/day-planner.js +324 -0
  160. package/server/mind/days.js +210 -0
  161. package/server/mind/evidence-read.js +45 -0
  162. package/server/mind/evidence.js +148 -0
  163. package/server/mind/faces.js +138 -0
  164. package/server/mind/guard.js +60 -0
  165. package/server/mind/index.js +445 -0
  166. package/server/mind/initiative.js +333 -0
  167. package/server/mind/life/activities.js +21 -0
  168. package/server/mind/life/context.js +309 -0
  169. package/server/mind/life/diary.js +786 -0
  170. package/server/mind/life/growth.js +252 -0
  171. package/server/mind/life/index.js +1049 -0
  172. package/server/mind/life/presence.js +305 -0
  173. package/server/mind/meetings.js +1069 -0
  174. package/server/mind/memory-policy.js +48 -0
  175. package/server/mind/memory.js +888 -0
  176. package/server/mind/nature.js +262 -0
  177. package/server/mind/novelty.js +19 -0
  178. package/server/mind/observations.js +84 -0
  179. package/server/mind/own-day.js +324 -0
  180. package/server/mind/own-voice.js +318 -0
  181. package/server/mind/periods.js +118 -0
  182. package/server/mind/reading.js +120 -0
  183. package/server/mind/salience.js +201 -0
  184. package/server/mind/schema.js +562 -0
  185. package/server/mind/self.js +484 -0
  186. package/server/mind/senses.js +53 -0
  187. package/server/mind/thoughts.js +274 -0
  188. package/server/mind/time/activity-clock.js +196 -0
  189. package/server/mind/time/agenda.js +242 -0
  190. package/server/mind/time/api.js +200 -0
  191. package/server/mind/time/attention.js +34 -0
  192. package/server/mind/time/availability.js +69 -0
  193. package/server/mind/time/commitment-review.js +198 -0
  194. package/server/mind/time/delivery-links.js +130 -0
  195. package/server/mind/time/executor.js +512 -0
  196. package/server/mind/time/experiences.js +128 -0
  197. package/server/mind/time/games.js +562 -0
  198. package/server/mind/time/index.js +739 -0
  199. package/server/mind/time/intent.js +166 -0
  200. package/server/mind/time/kinds.js +171 -0
  201. package/server/mind/time/model-material.js +77 -0
  202. package/server/mind/time/plugin-activity.js +207 -0
  203. package/server/mind/time/presentation.js +51 -0
  204. package/server/mind/time/schedule.js +83 -0
  205. package/server/mind/time/schema.js +84 -0
  206. package/server/mind/time/search.js +383 -0
  207. package/server/mind/time/sharing.js +472 -0
  208. package/server/mind/time/task-links.js +311 -0
  209. package/server/mind/time/tasks.js +587 -0
  210. package/server/mind/time/works.js +432 -0
  211. package/server/mind/traits.js +191 -0
  212. package/server/mind/util.js +110 -0
  213. package/server/mind/view.js +273 -0
  214. package/server/paths.js +102 -0
  215. package/server/plugins/api.js +204 -0
  216. package/server/plugins/host.js +967 -0
  217. package/server/plugins/install.js +295 -0
  218. package/server/plugins/manifest.js +201 -0
  219. package/server/plugins/permissions.js +42 -0
  220. package/server/plugins/runner/guard.mjs +36 -0
  221. package/server/plugins/runner/worker.mjs +250 -0
  222. package/server/plugins/schema.js +91 -0
  223. package/server/plugins/zip.js +112 -0
  224. package/server/startup-lock.js +118 -0
  225. package/server/storage/archive.js +241 -0
  226. package/server/storage/backup-cleanup-worker.js +8 -0
  227. package/server/storage/backup-cleanup.js +414 -0
  228. package/server/storage/backup-lock.js +76 -0
  229. package/server/storage/backup-scheduler.js +131 -0
  230. package/server/storage/store.js +199 -0
  231. package/server/studio/display-names.js +138 -0
  232. package/server/studio/management.js +102 -0
  233. package/server/studio/readiness.js +190 -0
  234. package/server/studio/settings.js +236 -0
  235. package/server/version.js +10 -0
@@ -0,0 +1,190 @@
1
+ import { createHash } from "node:crypto";
2
+ import { defaultModel, pickModel } from "../core/model-manager.js";
3
+
4
+ // The health panel and connection test must describe the same saved profile.
5
+ export function savedConnectionSettings(store, modelId = "") {
6
+ const hasProfiles = store.db
7
+ .prepare(
8
+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='core_config'",
9
+ )
10
+ .get();
11
+ const row =
12
+ hasProfiles &&
13
+ store.db.prepare("SELECT value FROM core_config WHERE id='models'").get();
14
+ const models = row ? JSON.parse(row.value) : [];
15
+ const profile = Array.isArray(models)
16
+ ? modelId
17
+ ? models.find((m) => m.id === modelId)
18
+ : pickModel(models, "default")
19
+ : null;
20
+ if (modelId && !profile) return null;
21
+ const settings = store.settings();
22
+ const resolved = profile || defaultModel(settings);
23
+ return {
24
+ isDefault: !profile || !!profile.isDefault,
25
+ profile: resolved,
26
+ settings: profile
27
+ ? {
28
+ ...settings,
29
+ baseUrl: profile.baseUrl,
30
+ model: profile.model,
31
+ apiKey: profile.apiKey || "",
32
+ providerPreset: profile.provider || settings.providerPreset,
33
+ reasoningEffort: profile.reasoningEffort || "none",
34
+ temperature: profile.temperature ?? settings.temperature,
35
+ topP: profile.topP ?? settings.topP,
36
+ maxTokens: profile.maxOutputTokens || settings.maxTokens || 256,
37
+ }
38
+ : settings,
39
+ };
40
+ }
41
+ export function modelSignature(settings) {
42
+ return createHash("sha256")
43
+ .update(
44
+ JSON.stringify([
45
+ settings.baseUrl,
46
+ settings.model,
47
+ process.env.LLM_API_KEY || settings.apiKey,
48
+ settings.providerPreset,
49
+ settings.reasoningEffort,
50
+ settings.temperature,
51
+ settings.topP,
52
+ settings.maxTokens,
53
+ ]),
54
+ )
55
+ .digest("hex");
56
+ }
57
+ export function recordModelCheck(store, settings, ok, latency, error = "") {
58
+ store.db
59
+ .prepare(
60
+ "INSERT INTO model_checks(id,signature,ok,latency,time,error) VALUES (1,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET signature=excluded.signature,ok=excluded.ok,latency=excluded.latency,time=excluded.time,error=excluded.error",
61
+ )
62
+ .run(modelSignature(settings), Number(ok), latency, Date.now(), error);
63
+ }
64
+ // The steps to get a channel talking. Which two come first depends on how she
65
+ // is connected: a OneBot client needs the shared token, the official bot needs
66
+ // its AppID and AppSecret.
67
+ function connectionChecks({ channel, online, tokenConfigured, qqbot }) {
68
+ if (channel === "qqbot") {
69
+ const configured = !!qqbot?.configured;
70
+ const groups = qqbot?.groupMessages;
71
+ return [
72
+ {
73
+ id: "credentials",
74
+ name: "QQ 官方机器人凭据",
75
+ done: configured,
76
+ detail: configured
77
+ ? `AppID ${qqbot.appId}`
78
+ : "在「系统 → 连接 QQ」填写机器人的 AppID 和 AppSecret",
79
+ tab: "setup",
80
+ },
81
+ {
82
+ id: "qq",
83
+ name: "QQ 连接",
84
+ done: online,
85
+ detail: online
86
+ ? groups === "mentions"
87
+ ? "已连接 QQ 开放平台;群里目前只收到 @ 她的消息,在开放平台开启「接收所有消息」后她才能看到整个群"
88
+ : "已连接 QQ 开放平台"
89
+ : qqbot?.error ||
90
+ (configured ? "正在连接 QQ 开放平台" : "填写凭据后自动连接"),
91
+ tab: "settings",
92
+ },
93
+ ];
94
+ }
95
+ return [
96
+ {
97
+ id: "token",
98
+ name: "QQ 接入令牌",
99
+ done: tokenConfigured,
100
+ detail: tokenConfigured
101
+ ? "已配置"
102
+ : "在本机 .env 中填写 ONEBOT_TOKEN,然后重启 LuckyTri",
103
+ tab: "setup",
104
+ },
105
+ {
106
+ id: "qq",
107
+ name: "QQ 连接",
108
+ done: online,
109
+ detail: online
110
+ ? "OneBot 11 反向 WebSocket 已连接"
111
+ : "在接入端配置 OneBot 11 反向 WebSocket 客户端",
112
+ tab: "settings",
113
+ },
114
+ ];
115
+ }
116
+
117
+ export function readiness(
118
+ store,
119
+ {
120
+ online = false,
121
+ tokenConfigured = !!process.env.ONEBOT_TOKEN,
122
+ channel = "onebot",
123
+ qqbot = null,
124
+ } = {},
125
+ ) {
126
+ const s = store.settings(),
127
+ record = store.db.prepare("SELECT * FROM model_checks WHERE id=1").get();
128
+ const effective = savedConnectionSettings(store).settings;
129
+ // Previous connection tests signed their temporary 256-token test budget.
130
+ // Accept that exact historical signature, without marking untested profiles ready.
131
+ const current =
132
+ !!record &&
133
+ [
134
+ effective,
135
+ { ...effective, maxTokens: Math.min(effective.maxTokens || 256, 256) },
136
+ ].some((value) => record.signature === modelSignature(value));
137
+ const sessions = store.db
138
+ .prepare("SELECT COUNT(*) n FROM sessions WHERE enabled=1")
139
+ .get().n;
140
+ const checks = [
141
+ ...connectionChecks({ channel, online, tokenConfigured, qqbot }),
142
+ {
143
+ id: "model",
144
+ name: "当前模型已测试",
145
+ done: current && !!record.ok,
146
+ detail: current
147
+ ? record.ok
148
+ ? `测试成功 · ${record.latency} ms`
149
+ : record.error
150
+ : record
151
+ ? "模型配置已变更,请重新测试"
152
+ : "保存模型配置后,点击测试模型连接",
153
+ tab: "settings",
154
+ },
155
+ {
156
+ id: "mode",
157
+ name: "开启真实回复",
158
+ done: !s.demo && s.enabled,
159
+ detail: s.demo
160
+ ? "当前是模拟模式,不向 QQ 发言"
161
+ : !s.enabled
162
+ ? "总开关已关闭"
163
+ : "真实模式与总开关已开启",
164
+ tab: "settings",
165
+ },
166
+ {
167
+ id: "session",
168
+ name: "选择参与的会话",
169
+ done: sessions > 0,
170
+ detail: sessions
171
+ ? `${sessions} 个会话已开启`
172
+ : "到「对话」里开启一个群聊或私聊",
173
+ tab: "sessions",
174
+ },
175
+ ];
176
+ return {
177
+ checks,
178
+ completed: checks.filter((x) => x.done).length,
179
+ total: checks.length,
180
+ ready: checks.every((x) => x.done),
181
+ modelTest: record
182
+ ? {
183
+ current,
184
+ ok: current && !!record.ok,
185
+ time: record.time,
186
+ latency: record.latency,
187
+ }
188
+ : null,
189
+ };
190
+ }
@@ -0,0 +1,236 @@
1
+ import { runtimePaths } from "../paths.js";
2
+ import { VERSION } from "../version.js";
3
+ import { publicSession } from "../channels/session-key.js";
4
+ import { CHANNEL_TYPES } from "../channels/hub.js";
5
+ import { readiness } from "./readiness.js";
6
+ import { FEEDBACK_LABELS } from "../core/feedback.js";
7
+ import {
8
+ EFFORT_LABELS,
9
+ MODEL_CATALOG,
10
+ MODEL_PRESETS,
11
+ REASONING_EFFORTS,
12
+ } from "../core/model-presets.js";
13
+ import { applySpeakerNames, speakerNames } from "../core/speaker-names.js";
14
+ import { displayNames, displaySession } from "./display-names.js";
15
+
16
+ export function mountStudio(app, store, runtime, chat) {
17
+ app.get("/api/storage/cleanup", (req, res) => {
18
+ if (!runtime.cleanup)
19
+ return res.status(501).json({ error: "备份整理不可用" });
20
+ res.json(runtime.cleanup.info());
21
+ });
22
+ app.patch("/api/storage/cleanup", (req, res) => {
23
+ if (!runtime.cleanup)
24
+ return res.status(501).json({ error: "备份整理不可用" });
25
+ try {
26
+ res.json(runtime.cleanup.save(req.body));
27
+ } catch (error) {
28
+ res.status(400).json({ error: error.message });
29
+ }
30
+ });
31
+ app.post("/api/storage/cleanup/manual", async (req, res) => {
32
+ if (!runtime.cleanup)
33
+ return res.status(501).json({ error: "备份整理不可用" });
34
+ try {
35
+ res.json(await runtime.cleanup.manual(req.body.items));
36
+ } catch (error) {
37
+ res.status(409).json({ error: error.message });
38
+ }
39
+ });
40
+ app.get("/api/service/status", (req, res) =>
41
+ res.json({
42
+ app: "luckybot",
43
+ workspace: runtimePaths().package,
44
+ home: runtimePaths().home,
45
+ version: VERSION,
46
+ pid: process.pid,
47
+ schemaVersion: store.db.prepare("PRAGMA user_version").get().user_version,
48
+ }),
49
+ );
50
+ app.post("/api/service/stop", (req, res) => {
51
+ res.json({ ok: true });
52
+ setTimeout(runtime.shutdown, 150);
53
+ });
54
+ app.get("/api/state", (req, res) => {
55
+ // Secrets are written from the studio and never read back into it.
56
+ const {
57
+ apiKey,
58
+ qqbotSecret,
59
+ onebotToken: _onebotToken,
60
+ ...settings
61
+ } = store.settings();
62
+ const demo = Number(settings.demo);
63
+ const connection = runtime.connection();
64
+ const namesForDisplay = displayNames(store.db, chat?.now() ?? Date.now());
65
+ res.json({
66
+ settings: {
67
+ ...settings,
68
+ hasApiKey: !!(apiKey || process.env.LLM_API_KEY),
69
+ hasQqbotSecret: !!(qqbotSecret || process.env.QQBOT_APP_SECRET),
70
+ },
71
+ readiness: readiness(store, {
72
+ online: connection.online,
73
+ tokenConfigured: connection.onebot?.tokenConfigured,
74
+ channel: connection.channel,
75
+ qqbot: connection.qqbot,
76
+ }),
77
+ feedbackLabels: FEEDBACK_LABELS,
78
+ modelPresets: MODEL_PRESETS,
79
+ modelCatalog: MODEL_CATALOG,
80
+ reasoningEfforts: REASONING_EFFORTS,
81
+ effortLabels: EFFORT_LABELS,
82
+ connection,
83
+ backup: runtime.backup?.() ?? null,
84
+ sessions: store.db
85
+ .prepare(
86
+ "SELECT s.*, (SELECT text FROM messages WHERE session_id=s.id AND is_demo=? ORDER BY id DESC LIMIT 1) preview,(SELECT COUNT(*) FROM messages WHERE session_id=s.id AND is_demo=?) message_count FROM sessions s ORDER BY s.id",
87
+ )
88
+ .all(demo, demo)
89
+ .map((session) => ({
90
+ ...displaySession(publicSession(session), namesForDisplay),
91
+ style: store.groupStyle(session.id, demo),
92
+ })),
93
+ decisions: (() => {
94
+ const rows = store.db
95
+ .prepare(
96
+ "SELECT d.*,f.tag AS feedback FROM decisions d LEFT JOIN reply_feedback f ON f.decision_id=d.id WHERE d.is_demo=? ORDER BY d.id DESC LIMIT 80",
97
+ )
98
+ .all(demo);
99
+ const names = speakerNames(
100
+ store.db,
101
+ rows.map((row) => row.session_id),
102
+ );
103
+ return rows.map((row) => ({
104
+ ...row,
105
+ reason: applySpeakerNames(row.reason, names),
106
+ }));
107
+ })(),
108
+ stats: {
109
+ messages: store.db
110
+ .prepare("SELECT COUNT(*) n FROM messages WHERE is_demo=?")
111
+ .get(demo).n,
112
+ replies: store.db
113
+ .prepare(
114
+ "SELECT COUNT(*) n FROM decisions WHERE reply!='' AND is_demo=?",
115
+ )
116
+ .get(demo).n,
117
+ },
118
+ });
119
+ });
120
+ // Connection and runtime switches only. Who she is lives in her nature;
121
+ // whether she speaks is hers to decide.
122
+ app.patch("/api/settings", (req, res) => {
123
+ const v = req.body;
124
+ const allowed = {};
125
+ for (const k of [
126
+ "aliases",
127
+ "baseUrl",
128
+ "model",
129
+ "apiKey",
130
+ "providerPreset",
131
+ "reasoningEffort",
132
+ "channel",
133
+ "qqbotAppId",
134
+ "qqbotSecret",
135
+ ])
136
+ if (k in v) {
137
+ if (typeof v[k] !== "string" || v[k].length > 4000)
138
+ return res.status(400).json({ error: "文本配置无效" });
139
+ allowed[k] = v[k];
140
+ }
141
+ if ("channel" in allowed) {
142
+ if (!CHANNEL_TYPES.includes(allowed.channel))
143
+ return res.status(400).json({ error: "接入方式无效" });
144
+ const forced = String(process.env.LUCKYTRI_CHANNEL || "").trim();
145
+ if (forced && forced !== allowed.channel)
146
+ return res
147
+ .status(409)
148
+ .json({ error: "接入方式已由环境变量 LUCKYTRI_CHANNEL 固定" });
149
+ }
150
+ if ("qqbotAppId" in allowed) {
151
+ allowed.qqbotAppId = allowed.qqbotAppId.trim();
152
+ if (
153
+ allowed.qqbotAppId &&
154
+ !/^[0-9A-Za-z_-]{1,40}$/.test(allowed.qqbotAppId)
155
+ )
156
+ return res.status(400).json({ error: "AppID 格式无效" });
157
+ }
158
+ if ("qqbotSecret" in allowed) {
159
+ allowed.qqbotSecret = allowed.qqbotSecret.trim();
160
+ if (allowed.qqbotSecret.length > 200)
161
+ return res.status(400).json({ error: "AppSecret 格式无效" });
162
+ }
163
+ for (const k of ["enabled", "demo", "memoryEnabled", "memoryCandidates"])
164
+ if (k in v) {
165
+ if (typeof v[k] !== "boolean")
166
+ return res.status(400).json({ error: "开关配置无效" });
167
+ allowed[k] = v[k];
168
+ }
169
+ for (const [k, min, max] of [
170
+ ["temperature", 0, 2],
171
+ ["topP", 0, 1],
172
+ ["maxTokens", 64, 2000],
173
+ ])
174
+ if (k in v) {
175
+ if (
176
+ !Number.isFinite(v[k]) ||
177
+ v[k] < min ||
178
+ v[k] > max ||
179
+ (k === "maxTokens" && !Number.isInteger(v[k]))
180
+ )
181
+ return res.status(400).json({ error: "数值配置超出范围" });
182
+ allowed[k] = v[k];
183
+ }
184
+ const retired = [
185
+ "probability",
186
+ "cooldown",
187
+ "voicePreset",
188
+ "slangLevel",
189
+ "allowMildProfanity",
190
+ "qualityRewrite",
191
+ "adaptGroupStyle",
192
+ "persona",
193
+ "name",
194
+ ].filter((k) => k in v);
195
+ if (retired.length)
196
+ return res.status(400).json({
197
+ error: `这些设置已经不存在:${retired.join("、")}。名字和天性在「TA → 天性」里修改`,
198
+ });
199
+ if (
200
+ "providerPreset" in allowed &&
201
+ !Object.hasOwn(MODEL_PRESETS, allowed.providerPreset)
202
+ )
203
+ return res.status(400).json({ error: "模型服务商预设无效" });
204
+ if (
205
+ "reasoningEffort" in allowed &&
206
+ !REASONING_EFFORTS.some((item) => item.id === allowed.reasoningEffort)
207
+ )
208
+ return res.status(400).json({ error: "思考强度无效" });
209
+ if ("baseUrl" in allowed) {
210
+ try {
211
+ const u = new URL(allowed.baseUrl);
212
+ if (
213
+ !["http:", "https:"].includes(u.protocol) ||
214
+ u.username ||
215
+ u.password ||
216
+ u.search ||
217
+ u.hash
218
+ )
219
+ throw 0;
220
+ } catch {
221
+ return res.status(400).json({ error: "API 地址无效" });
222
+ }
223
+ }
224
+ if ("model" in allowed && !allowed.model.trim())
225
+ return res.status(400).json({ error: "模型名称不能为空" });
226
+ store.save(allowed);
227
+ // A new channel or new credentials take effect now, without a restart.
228
+ if (
229
+ "channel" in allowed ||
230
+ "qqbotAppId" in allowed ||
231
+ "qqbotSecret" in allowed
232
+ )
233
+ runtime.syncChannel?.();
234
+ res.json({ ok: true });
235
+ });
236
+ }
@@ -0,0 +1,10 @@
1
+ // The one place the running version is read: `package.json`. Everything that
2
+ // needs to say which LuckyTri it is (request headers, database archives)
3
+ // imports it from here, so a release only changes one number.
4
+ import { readFileSync } from "node:fs";
5
+
6
+ export const VERSION = JSON.parse(
7
+ readFileSync(new URL("../package.json", import.meta.url), "utf8"),
8
+ ).version;
9
+
10
+ export const USER_AGENT = `LuckyTri/${VERSION}`;