@cmach_socket/opencode-qq-bot 0.9.2 → 0.10.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.
Files changed (49) hide show
  1. package/NOTICE +0 -1
  2. package/README.md +43 -20
  3. package/chat-overrides.example.json +3 -3
  4. package/package.json +6 -2
  5. package/src/chat.ts +5 -0
  6. package/src/commands.ts +88 -1
  7. package/src/config/fields.ts +68 -5
  8. package/src/config/live.ts +33 -3
  9. package/src/config/registry.ts +32 -0
  10. package/src/config/store.ts +6 -11
  11. package/src/config/types.ts +29 -1
  12. package/src/config.ts +8 -1
  13. package/src/diary/command.ts +21 -20
  14. package/src/diary/format.ts +18 -1
  15. package/src/diary/scheduler.ts +7 -7
  16. package/src/diary/scope.ts +1 -1
  17. package/src/diary/summary.ts +39 -38
  18. package/src/emoji/collection.ts +33 -1
  19. package/src/fs/atomic.ts +82 -0
  20. package/src/home.ts +6 -2
  21. package/src/index.ts +113 -45
  22. package/src/keepalive.ts +60 -2
  23. package/src/known-secrets.ts +20 -0
  24. package/src/logger.ts +19 -3
  25. package/src/model-view.ts +4 -15
  26. package/src/naplink/history.ts +12 -7
  27. package/src/overrides.ts +268 -51
  28. package/src/panel/api.ts +79 -7
  29. package/src/panel/documents.ts +622 -0
  30. package/src/panel/server.ts +18 -1
  31. package/src/panel/status.ts +10 -3
  32. package/src/restartable.ts +74 -0
  33. package/src/safe-object.ts +48 -0
  34. package/src/sensitive.ts +31 -2
  35. package/src/session/find.ts +3 -2
  36. package/src/session/manager.ts +646 -43
  37. package/src/session/proactive.ts +3 -1
  38. package/src/session/prompt.ts +5 -13
  39. package/src/session/soul-guard.ts +162 -0
  40. package/src/session/soul.ts +78 -13
  41. package/src/state-lock.ts +74 -0
  42. package/src/time-format.ts +45 -0
  43. package/src/tools/policy.ts +65 -24
  44. package/src/tools/qq.ts +20 -12
  45. package/web/dist/assets/index-B-3ZGYMq.js +8 -0
  46. package/web/dist/assets/index-C5lRsdbz.css +1 -0
  47. package/web/dist/index.html +2 -2
  48. package/web/dist/assets/index-7eMQEM7-.css +0 -1
  49. package/web/dist/assets/index-DCkgUY7y.js +0 -15
package/src/panel/api.ts CHANGED
@@ -16,6 +16,12 @@ import {
16
16
  } from "../config/registry"
17
17
  import { readLogTail, subscribeLogs, type LogEntry } from "../logger"
18
18
  import { PanelAuth, SESSION_COOKIE } from "./auth"
19
+ import {
20
+ readChatOverrides,
21
+ readToolProfiles,
22
+ saveChatOverrides,
23
+ saveToolProfiles,
24
+ } from "./documents"
19
25
  import type { ModelProvider } from "./models"
20
26
  import { collectStatus } from "./status"
21
27
  import type { PanelRuntime } from "./runtime"
@@ -42,6 +48,10 @@ export interface PanelAppOptions {
42
48
  models: ModelProvider
43
49
  /** 前端构建产物目录;没有就不托管静态资源。 */
44
50
  staticDir?: string
51
+ /** 面板触发重启时调用;不传表示当前环境不支持重启。 */
52
+ onRestart?: () => void | Promise<void>
53
+ /** 重启请求先返回响应,再延迟这么久执行(毫秒)。 */
54
+ restartDelayMs?: number
45
55
  }
46
56
 
47
57
  /** 状态推送的间隔(毫秒)。 */
@@ -90,20 +100,24 @@ export function createPanelApp(options: PanelAppOptions): Hono {
90
100
  }),
91
101
  )
92
102
 
93
- // 写接口校验 Origin:带 Origin 的请求必须同源,不带的(curl)放行。
103
+ // 写接口校验 Origin:带 Origin 的请求必须同源。
104
+ // 带会话 Cookie 的请求必须带 Origin:Cookie 会被浏览器自动带上,
105
+ // 没有 Origin 的请求才可能是脚本伪造的。Bearer 令牌走命令行,不带 Origin 是正常的。
94
106
  app.use("/api/*", async (c, next) => {
95
107
  if (SAFE_METHODS.has(c.req.method)) return await next()
96
108
  const origin = c.req.header("Origin")
109
+ let expected = ""
110
+ try {
111
+ expected = new URL(c.req.url).origin
112
+ } catch {
113
+ expected = ""
114
+ }
97
115
  if (origin) {
98
- let expected = ""
99
- try {
100
- expected = new URL(c.req.url).origin
101
- } catch {
102
- expected = ""
103
- }
104
116
  if (origin !== expected) {
105
117
  return c.json({ error: "forbidden", message: "跨站请求已被拒绝" }, 403)
106
118
  }
119
+ } else if (getCookie(c, SESSION_COOKIE)) {
120
+ return c.json({ error: "forbidden", message: "缺少 Origin 头,请求已被拒绝" }, 403)
107
121
  }
108
122
  return await next()
109
123
  })
@@ -192,6 +206,50 @@ export function createPanelApp(options: PanelAppOptions): Hono {
192
206
  }
193
207
  })
194
208
 
209
+ // 会话覆盖文件:生效值、文件值与告警。
210
+ app.get("/api/chat-overrides", async (c) => {
211
+ return c.json(await readChatOverrides(runtime))
212
+ })
213
+
214
+ // 保存会话覆盖:稀疏改动,校验在服务端做。
215
+ app.put("/api/chat-overrides", async (c) => {
216
+ const contentType = c.req.header("Content-Type") ?? ""
217
+ if (!contentType.toLowerCase().includes("application/json")) {
218
+ return c.json({ error: "unsupported_media_type", message: "只接受 JSON 请求体" }, 415)
219
+ }
220
+ const body = await readJson(c.req.raw)
221
+ const { problems, result } = await saveChatOverrides(runtime, body)
222
+ if (!result) {
223
+ return c.json(
224
+ { error: "validation_failed", message: "会话覆盖校验不通过", fields: problems },
225
+ 400,
226
+ )
227
+ }
228
+ return c.json({ ok: true, ...result })
229
+ })
230
+
231
+ // 工具配置文件:生效值、文件值与告警。
232
+ app.get("/api/tool-profiles", async (c) => {
233
+ return c.json(await readToolProfiles(runtime))
234
+ })
235
+
236
+ // 保存工具配置:稀疏改动,校验在服务端做。
237
+ app.put("/api/tool-profiles", async (c) => {
238
+ const contentType = c.req.header("Content-Type") ?? ""
239
+ if (!contentType.toLowerCase().includes("application/json")) {
240
+ return c.json({ error: "unsupported_media_type", message: "只接受 JSON 请求体" }, 415)
241
+ }
242
+ const body = await readJson(c.req.raw)
243
+ const { problems, result } = await saveToolProfiles(runtime, body)
244
+ if (!result) {
245
+ return c.json(
246
+ { error: "validation_failed", message: "工具配置校验不通过", fields: problems },
247
+ 400,
248
+ )
249
+ }
250
+ return c.json({ ok: true, ...result })
251
+ })
252
+
195
253
  // 可用模型列表。
196
254
  app.get("/api/models", async (c) => {
197
255
  try {
@@ -209,6 +267,20 @@ export function createPanelApp(options: PanelAppOptions): Hono {
209
267
  return c.json(await collectStatus(runtime))
210
268
  })
211
269
 
270
+ // 就地重启插件:先返回响应,再卸载当前运行时并用最新配置重新初始化。
271
+ app.post("/api/restart", (c) => {
272
+ if (!options.onRestart) {
273
+ return c.json({ error: "unsupported", message: "当前运行时不支持重启" }, 501)
274
+ }
275
+ const timer = setTimeout(() => {
276
+ void Promise.resolve(options.onRestart?.()).catch((error: unknown) => {
277
+ runtime.logger.error("面板触发重启失败", error)
278
+ })
279
+ }, options.restartDelayMs ?? 200)
280
+ timer.unref?.()
281
+ return c.json({ ok: true, restarting: true }, 202)
282
+ })
283
+
212
284
  // 日志尾部。
213
285
  app.get("/api/logs", (c) => {
214
286
  const limit = Number(c.req.query("limit") ?? "200")