dsh-world 0.7.3 → 0.7.5

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 (62) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/NOTES.md +37 -17
  3. package/README.md +14 -7
  4. package/client/index.js +37 -2
  5. package/lib/abortable.d.ts +1 -0
  6. package/lib/abortable.js +17 -0
  7. package/lib/compaction.js +2 -2
  8. package/lib/compactionSwap.d.ts +23 -14
  9. package/lib/compactionSwap.js +90 -36
  10. package/lib/console/homeApply.js +1 -12
  11. package/lib/console/routes.d.ts +1 -0
  12. package/lib/console/routes.js +10 -0
  13. package/lib/console/snapshot.d.ts +4 -0
  14. package/lib/console/snapshot.js +3 -0
  15. package/lib/ecosystem/bridge/bridge.d.ts +2 -2
  16. package/lib/ecosystem/bridge/bridge.js +24 -11
  17. package/lib/ecosystem/qq-native/home.d.ts +2 -1
  18. package/lib/ecosystem/qq-native/home.js +6 -2
  19. package/lib/ecosystem/qq-native/images.d.ts +9 -0
  20. package/lib/ecosystem/qq-native/images.js +69 -0
  21. package/lib/ecosystem/qq-native/index.d.ts +1 -0
  22. package/lib/ecosystem/qq-native/index.js +8 -2
  23. package/lib/ecosystem/qq-native/snowluma.d.ts +1 -0
  24. package/lib/ecosystem/qq-native/snowluma.js +13 -4
  25. package/lib/ecosystem/shared/turn.d.ts +3 -0
  26. package/lib/ecosystem/shared/turn.js +9 -2
  27. package/lib/ecosystem/wechat/home.d.ts +3 -0
  28. package/lib/ecosystem/wechat/home.js +12 -4
  29. package/lib/ecosystem/wechat/index.js +3 -1
  30. package/lib/ecosystem/wechat/supervisor.d.ts +1 -1
  31. package/lib/ecosystem/wechat/supervisor.js +5 -1
  32. package/lib/hostContract.d.ts +6 -0
  33. package/lib/index.d.ts +1 -1
  34. package/lib/index.js +85 -47
  35. package/lib/life/home.js +8 -2
  36. package/lib/life/photoTools.js +60 -26
  37. package/lib/life/runtime.js +2 -0
  38. package/lib/memoryTools.d.ts +1 -1
  39. package/lib/memoryTools.js +1 -1
  40. package/lib/mood.d.ts +57 -0
  41. package/lib/mood.js +95 -0
  42. package/lib/moodTools.d.ts +6 -0
  43. package/lib/moodTools.js +30 -0
  44. package/lib/persona.js +7 -0
  45. package/lib/promptSection.d.ts +9 -13
  46. package/lib/promptSection.js +63 -80
  47. package/lib/runtime.d.ts +21 -2
  48. package/lib/runtime.js +148 -39
  49. package/lib/self.d.ts +1 -1
  50. package/lib/self.js +1 -1
  51. package/lib/sourceArchive.d.ts +1 -5
  52. package/lib/sourceArchive.js +31 -14
  53. package/lib/types.d.ts +2 -0
  54. package/lib/worldScope.d.ts +2 -4
  55. package/lib/worldScope.js +6 -6
  56. package/package.json +1 -1
  57. package/src/ecosystem/wechat/windows/README.md +5 -3
  58. package/src/ecosystem/wechat/windows/adapter.py +23 -2
  59. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  60. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  61. package/src/ecosystem/wechat/windows/sender.py +6 -2
  62. package/templates/world-prompt.md +6 -3
@@ -1,6 +1,7 @@
1
1
  import { randomBytes } from 'node:crypto';
2
2
  import { copyFileSync, existsSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
+ import { isDeepStrictEqual } from 'node:util';
4
5
  const STOCK = '@deepseek-ai/dsh-compaction-basic';
5
6
  const OURS = 'dsh-world/compaction';
6
7
  const stock = /^(\s*name:\s*)(['"]?)@deepseek-ai\/dsh-compaction-basic\2([ \t]*(?:#.*)?\r?)$/gm;
@@ -38,62 +39,116 @@ export function swapStockCompaction(profileDir) {
38
39
  }
39
40
  return { status: 'swapped', backup };
40
41
  }
41
- /** The compaction rows in a preset's plugin list, including those nested in groups (where DSH's Web host isolates it). */
42
- function compactionRows(rows, found = []) {
42
+ /** The rows of a preset's plugin list that match, including those nested in groups (where DSH's Web host isolates compaction). */
43
+ function rowsWhere(rows, match, found = []) {
43
44
  if (!Array.isArray(rows))
44
45
  return found;
45
46
  for (const row of rows) {
46
- if (row?.name === STOCK || row?.name === OURS)
47
+ if (row && match(row))
47
48
  found.push(row);
48
49
  if (row?.group)
49
- compactionRows(row.config, found);
50
+ rowsWhere(row.config, match, found);
50
51
  }
51
52
  return found;
52
53
  }
53
- /** The preset's config with its one stock compaction row renamed to ours; the input is not changed. */
54
- export function swapPresetConfig(config) {
54
+ /** Removes the matching rows in place, groups included, and says how many went. */
55
+ function removeRows(rows, match) {
56
+ if (!Array.isArray(rows))
57
+ return 0;
58
+ let removed = 0;
59
+ for (let index = rows.length - 1; index >= 0; index--) {
60
+ const row = rows[index];
61
+ if (row && match(row)) {
62
+ rows.splice(index, 1);
63
+ removed++;
64
+ }
65
+ else if (row?.group)
66
+ removed += removeRows(row.config, match);
67
+ }
68
+ return removed;
69
+ }
70
+ const PRESET = '@deepseek-ai/dsh-agent-preset';
71
+ const isPreset = (entry) => entry.options.name === PRESET;
72
+ const plugins = (config) => config?.plugins;
73
+ const usesWorldCompaction = (config) => rowsWhere(plugins(config), row => row.name === OURS).length > 0;
74
+ export const hasAgentPresets = (editor) => editor?.entries().some(isPreset) ?? false;
75
+ function overriddenCompactionPreset(editor, id) {
76
+ if (!editor.entries().some(entry => isPreset(entry) && presetId(entry) === id && usesWorldCompaction(entry.options.config)))
77
+ return;
78
+ return editor.configuration?.().find(({ entry, override }) => isPreset(entry) && presetId(entry) === id && usesWorldCompaction(override));
79
+ }
80
+ export function compactionPresetOverride(editor, id, profileDir) {
81
+ const row = overriddenCompactionPreset(editor, id);
82
+ return row ? { preset: id, canRestore: Boolean(profileDir && Array.isArray(plugins(row.inherited))) } : null;
83
+ }
84
+ /** Return the inherited value through DSH's editor so it removes the profile override. */
85
+ export async function restoreCompactionPreset(editor, id, profileDir) {
86
+ const row = overriddenCompactionPreset(editor, id);
87
+ if (!row || !Array.isArray(plugins(row.inherited)))
88
+ throw new Error(`预设 ${id} 没有可还原的压缩覆盖`);
89
+ const file = join(profileDir, 'cordis.patch.yml');
90
+ await editor.edit(row.entry, (raw, inherited) => {
91
+ if (!isDeepStrictEqual(raw, row.override) || !Array.isArray(plugins(inherited)))
92
+ throw new Error(`预设 ${id} 已变化,请刷新后再试`);
93
+ backupBeside(file);
94
+ return inherited;
95
+ });
96
+ }
97
+ // DSH 0.2.2 folded `list_agents` into the subagent-control package. A preset copied on 0.2.1 still names this row, and
98
+ // DSH then refuses to mount that whole preset, so no session using it can be resumed.
99
+ const RETIRED = '@deepseek-ai/dsh-tool-subagent-control/list-agents';
100
+ const isRetired = (row) => row.name === RETIRED;
101
+ /** The copied preset without the rows DSH itself no longer ships, or undefined when there is nothing to drop; the input is not changed. */
102
+ export function dropRetiredRows(config, inherited) {
103
+ if (rowsWhere(inherited?.plugins, isRetired).length)
104
+ return undefined;
55
105
  const next = structuredClone(config);
56
- const rows = compactionRows(next?.plugins);
57
- if (rows.some(row => row.name === OURS))
58
- return { status: 'already' };
59
- if (rows.length !== 1)
60
- return { status: 'unchanged', reason: rows.length ? `预设里有 ${rows.length} 条原版压缩配置,无法确定改哪一条` : '预设里没有原版压缩那一行' };
61
- rows[0].name = OURS;
62
- return { status: 'swapped', config: next };
106
+ return removeRows(next?.plugins, isRetired) ? next : undefined;
63
107
  }
108
+ const presetId = (entry) => String(entry.options.config?.id ?? entry.options.id);
64
109
  /**
65
- * DSH's Web host composes the stock compaction inside each preset, so the profile has no row to rename. The host's
66
- * preset editor writes the whole preset into the profile when a partner edits it; this does the same for her preset
67
- * with only the compaction row changed. That copy no longer follows later DSH updates to the preset.
110
+ * On every start, drops the retired rows from each preset copied into the profile, with a backup of the profile patch
111
+ * first. Only a row DSH no longer ships beneath the copy goes, so the same copy on DSH 0.2.1 is left alone. The editor
112
+ * reloads the row; sessions that already failed to resume come back after a DSH restart.
68
113
  */
69
- export async function swapPresetCompaction(editor, presetId, profileDir) {
70
- const entry = editor.entries().find(row => row.options.name === '@deepseek-ai/dsh-agent-preset' && row.options.config?.id === presetId);
71
- if (!entry)
72
- return { status: 'unchanged', reason: `没有找到预设 ${presetId}` };
73
- const first = swapPresetConfig(entry.options.config);
74
- if (first.status !== 'swapped')
75
- return first;
76
- const file = join(profileDir, 'cordis.patch.yml');
77
- const backup = existsSync(file) ? backupBeside(file) : undefined;
78
- await editor.edit(entry, raw => {
79
- const result = swapPresetConfig(raw);
80
- if (result.status !== 'swapped')
81
- throw new Error(`预设 ${presetId} 在写入前变了:${result.status === 'unchanged' ? result.reason : '已经换过'}`);
82
- return result.config;
83
- });
84
- return { status: 'swapped', backup: backup ?? '(原来没有 cordis.patch.yml)', preset: presetId };
114
+ export async function repairCopiedPresets(deps) {
115
+ const repaired = [];
116
+ let backup = '(原来没有 cordis.patch.yml)';
117
+ try {
118
+ const broken = (deps.editor.configuration?.() ?? []).filter(({ entry, inherited }) => entry.options.name === PRESET && dropRetiredRows(entry.options.config, inherited) !== undefined);
119
+ if (!broken.length)
120
+ return;
121
+ const file = join(deps.profileDir, 'cordis.patch.yml');
122
+ if (existsSync(file))
123
+ backup = backupBeside(file);
124
+ for (const { entry } of broken) {
125
+ await deps.editor.edit(entry, (raw, inherited) => {
126
+ const next = dropRetiredRows(raw, inherited);
127
+ if (next === undefined)
128
+ throw new Error(`预设 ${presetId(entry)} 在写入前变了`);
129
+ return next;
130
+ });
131
+ repaired.push(presetId(entry));
132
+ }
133
+ }
134
+ catch (error) {
135
+ const done = repaired.length ? `已修好预设 ${repaired.join('、')};` : '';
136
+ deps.report(`${done}没能删掉 profile 预设里 DSH 已不再提供的 list-agents 那一行:${error instanceof Error ? error.message : String(error)}。用这个预设的会话会打不开,可以退出 DSH 后在 cordis.patch.yml 里手动删掉 ${RETIRED} 那一行(原文件备份在 ${backup})。`);
137
+ return;
138
+ }
139
+ deps.report(`已从 profile 里的预设 ${repaired.join('、')} 删掉 DSH 已不再提供的 list-agents 那一行(原文件备份在 ${backup})。重启 DSH 后,用这个预设的会话就能正常打开。`);
85
140
  }
86
141
  /**
87
142
  * Once per world, when the plugin first runs in a profile: make the profile use the tool-free compaction. The attempt is
88
143
  * remembered only when it succeeded or was already done, so a partner who later puts the stock one back is left alone,
89
144
  * while a profile this could not read is looked at again next start.
90
145
  */
91
- export async function swapCompactionOnce(deps) {
146
+ export function swapCompactionOnce(deps) {
92
147
  if (!deps.enabled || deps.done() || !deps.profileDir)
93
148
  return;
94
149
  let result;
95
150
  try {
96
- result = await (deps.swap ?? swapStockCompaction)(deps.profileDir);
151
+ result = swapStockCompaction(deps.profileDir);
97
152
  }
98
153
  catch (error) {
99
154
  deps.report(`没能把压缩换成 dsh-world 的“只留对话”压缩:${error instanceof Error ? error.message : String(error)}`);
@@ -106,6 +161,5 @@ export async function swapCompactionOnce(deps) {
106
161
  deps.markDone();
107
162
  if (result.status !== 'swapped')
108
163
  return;
109
- const copied = result.preset ? `预设 ${result.preset} 已整份写进 profile,之后 DSH 升级对这个预设的改动不会自动进来;` : '';
110
- deps.report(`已把 profile 的压缩换成 dsh-world 的“只留对话”压缩,重启 DSH 后生效(原文件备份在 ${result.backup})。${copied}不想换:恢复备份,并在 dsh-world 配置里写 replaceCompaction: false。`);
164
+ deps.report(`已把 profile 的压缩换成 dsh-world 的“只留对话”压缩,重启 DSH 后生效(原文件备份在 ${result.backup})。不想换:恢复备份,并在 dsh-world 配置里写 replaceCompaction: false。`);
111
165
  }
@@ -1,4 +1,4 @@
1
- import { addAbortListener } from 'node:events';
1
+ import { abortable } from "../abortable.js";
2
2
  import { ManualCompactionError } from '@deepseek-ai/dsh-compaction';
3
3
  import { errorText } from "../faults.js";
4
4
  /**
@@ -17,17 +17,6 @@ function failureText(error) {
17
17
  parts.push(errorText(current));
18
18
  return parts.join(':');
19
19
  }
20
- async function abortable(promise, signal) {
21
- signal.throwIfAborted();
22
- let listener;
23
- const aborted = new Promise((_, reject) => { listener = addAbortListener(signal, () => reject(signal.reason)); });
24
- try {
25
- return await Promise.race([promise, aborted]);
26
- }
27
- finally {
28
- listener[Symbol.dispose]();
29
- }
30
- }
31
20
  /** One explicit request, held behind existing turns and cancelled with the life line. */
32
21
  export function createHomeApply(deps) {
33
22
  let state = { status: 'idle' };
@@ -22,6 +22,7 @@ export interface ConsoleActions {
22
22
  stop(): Promise<void>;
23
23
  /** Enqueue one application of saved prompts and idle-home compaction; returns a refusal, if any. */
24
24
  applyHome(): string | undefined;
25
+ restoreCompactionPreset(): Promise<void>;
25
26
  prompts(): {
26
27
  persona: Persona;
27
28
  worldPrompt: {
@@ -4,6 +4,7 @@ import { validColdHours, validTurns, validWechatBuffer, MAX_COLD_HOURS, validWec
4
4
  import { validArchiveScope } from "../archiveSelection.js";
5
5
  import { validClockTime } from "../life/alarm.js";
6
6
  import { MemoryError } from "../memory.js";
7
+ import { errorText } from "../faults.js";
7
8
  /** A write must carry this header; a page on another site cannot add it without a CORS preflight, which is never answered. */
8
9
  export const WRITE_HEADER = 'x-dsh-world';
9
10
  const MAX_BODY_BYTES = 64 * 1024;
@@ -52,6 +53,15 @@ async function write(actions, path, body) {
52
53
  const refused = actions.applyHome();
53
54
  return refused ? bad(refused) : ok();
54
55
  }
56
+ case '/console/restore-compaction-preset': {
57
+ try {
58
+ await actions.restoreCompactionPreset();
59
+ return ok();
60
+ }
61
+ catch (error) {
62
+ return bad(errorText(error));
63
+ }
64
+ }
55
65
  case '/console/persona': {
56
66
  const layers = persona(body);
57
67
  if (!layers)
@@ -10,6 +10,7 @@ import type { HomeApplyState } from './homeApply.ts';
10
10
  import type { PhotoStore } from '../life/photos.ts';
11
11
  import type { DailySchedule } from '../life/types.ts';
12
12
  import type { MemoryService } from '../memory.ts';
13
+ import type { compactionPresetOverride } from '../compactionSwap.ts';
13
14
  export declare function estimateTokens(text: string): number;
14
15
  export interface SnapshotDeps {
15
16
  ctx: Context;
@@ -21,10 +22,13 @@ export interface SnapshotDeps {
21
22
  /** The running life line; undefined while her world is stopped. */
22
23
  life(): LifeRuntime | undefined;
23
24
  homeApply?(): HomeApplyState;
25
+ compactionPreset?(): ReturnType<typeof compactionPresetOverride>;
24
26
  photos?(): PhotoStore;
25
27
  /** Long-term memory: the console's lookups and the health of its index. */
26
28
  longTerm?: MemoryService;
27
29
  diaryDir: string;
30
+ /** Her mood file, shown read-only; absent in a console without a room. */
31
+ moodFile?: string;
28
32
  running(): boolean;
29
33
  /** CNY a day for her home; undefined is no limit. */
30
34
  dailyBudget(): number | undefined;
@@ -1,6 +1,7 @@
1
1
  import { SessionId } from '@deepseek-ai/dsh-session';
2
2
  import { createHistory, renderHistory } from "../history.js";
3
3
  import { createDiaryReader } from "../life/diary.js";
4
+ import { moodView } from "../mood.js";
4
5
  const SNAPSHOT_TTL_MS = 5000;
5
6
  export function estimateTokens(text) {
6
7
  const cjk = (text.match(/[㐀-鿿豈-﫿 -〿＀-￯]/g) ?? []).length;
@@ -28,11 +29,13 @@ export function consoleSources(deps) {
28
29
  schedule: deps.schedule?.() ?? null,
29
30
  life: life ? { homeSessionId: life.homeSessionId, status: home ? home.status : 'cold', alarm: life.alarm, review: life.review, recentWakes: lifeStore?.wakes().slice(-5).reverse() ?? [] } : null,
30
31
  homeApply: deps.homeApply?.() ?? null,
32
+ compactionPreset: deps.compactionPreset?.() ?? null,
31
33
  health: [...(life?.faults ?? []), ...deps.faults()],
32
34
  memory: { index: deps.longTerm?.status() ?? null, turns: turns.length, windowTurns: window, coldHours: deps.coldHours?.() ?? null, archiveScope, selectedArchiveScope, restartRequired: archiveScope !== selectedArchiveScope, approxTokens: estimateTokens(renderHistory(turns, config.timeZone, deps.names(), archiveScope === 'home')), oldest: turns.at(-1)?.messages[0]?.occurredAt, ...archive.stats() },
33
35
  wechat: deps.wechat?.() ?? null,
34
36
  compactions: lifeStore?.compactions().slice(-5).reverse() ?? [],
35
37
  diaryDates: diary.dates(), activityDays: lifeStore?.activityDays() ?? [],
38
+ mood: deps.moodFile ? moodView(deps.moodFile) : null,
36
39
  native,
37
40
  modules: deps.modules?.() ?? null,
38
41
  };
@@ -28,8 +28,8 @@ export interface BridgeDeps {
28
28
  name?: string;
29
29
  }
30
30
  /**
31
- * A local app's way into her home: its words arrive as her partner's own (source `user`), and the first
32
- * visible text she says after they are admitted goes to the outlets. Only the latest words are tracked.
31
+ * A local app's way into her home: its words arrive as her partner's own (source `user`). The final
32
+ * reply after admission goes to the outlets when that turn completes. Only the latest words are tracked.
33
33
  */
34
34
  export declare function createBridge(deps: BridgeDeps): {
35
35
  start: () => void;
@@ -13,8 +13,8 @@ export class HomeUnavailable extends Error {
13
13
  }
14
14
  const labels = (name) => ({ reply: `${name}的回复`, busy: `「${name}在忙」`, reach: `${name}找你的话` });
15
15
  /**
16
- * A local app's way into her home: its words arrive as her partner's own (source `user`), and the first
17
- * visible text she says after they are admitted goes to the outlets. Only the latest words are tracked.
16
+ * A local app's way into her home: its words arrive as her partner's own (source `user`). The final
17
+ * reply after admission goes to the outlets when that turn completes. Only the latest words are tracked.
18
18
  */
19
19
  export function createBridge(deps) {
20
20
  const { ctx } = deps;
@@ -56,18 +56,31 @@ export function createBridge(deps) {
56
56
  disposers.push(ctx.on('session/event', (session, event) => {
57
57
  if (!tracked || session.id !== tracked.sessionId)
58
58
  return;
59
- if (event.type === 'user/message' && event.data.id === tracked.id) {
60
- tracked.admitted = true;
59
+ if (event.type === 'user/message') {
60
+ if (event.data.id === tracked.id)
61
+ tracked.admitted = true;
62
+ else if (tracked.admitted && event.data.source.kind === 'user')
63
+ untrack();
61
64
  return;
62
65
  }
63
- if (event.type !== 'assistant/message' || !tracked.admitted)
66
+ if (!tracked.admitted)
64
67
  return;
65
- const visible = event.data.message.content.flatMap(block => block.type === 'text' ? [block.text] : []).join('\n');
66
- const reply = pushOf(visible, 'reply', name, safeOrigin());
67
- if (!reply.text && !reply.images)
68
- return;
69
- untrack();
70
- send(reply);
68
+ if (event.type === 'assistant/message') {
69
+ const { content } = event.data.message;
70
+ // Text accompanying a tool call can be narration, including when the tool itself ends the turn.
71
+ tracked.finalText = content.some(block => block.type === 'tool-call') ? undefined
72
+ : content.flatMap(block => block.type === 'text' ? [block.text] : []).join('\n');
73
+ }
74
+ // The next turn end in this session closes the turn that admitted the words, even when steered in.
75
+ if (event.type === 'turn/end') {
76
+ const { finalText } = tracked;
77
+ untrack();
78
+ if (event.data.reason.kind !== 'completed' || !finalText)
79
+ return;
80
+ const reply = pushOf(finalText, 'reply', name, safeOrigin());
81
+ if (reply.text || reply.images)
82
+ send(reply);
83
+ }
71
84
  }));
72
85
  try {
73
86
  durableWrite(deps.file, JSON.stringify({ origin: deps.origin(), ...bridgePaths(deps.prefix), token, pid: process.pid, startedAt: clock().toISOString(), name }, null, 2));
@@ -1,5 +1,6 @@
1
1
  import type { Context } from '@deepseek-ai/cordis';
2
2
  import type { Agent } from '@deepseek-ai/dsh-agent';
3
+ import { type ContentBlock } from '@deepseek-ai/dsh-llm';
3
4
  import { type SpeakerIdentity } from '../shared/identity.ts';
4
5
  import type { LifeTimers } from '../../life/alarmTimer.ts';
5
6
  import { type ChannelContextMessage } from '../shared/context.ts';
@@ -16,7 +17,7 @@ export declare function createQqHome(deps: {
16
17
  inWorld?: InWorld;
17
18
  timers?: LifeTimers;
18
19
  }): {
19
- ask: (text: string, suppliedIdentity: SpeakerIdentity, suppliedContext?: ChannelContextMessage[]) => Promise<{
20
+ ask: (text: string, suppliedIdentity: SpeakerIdentity, suppliedContext?: ChannelContextMessage[], loadImages?: () => Promise<ContentBlock[]>) => Promise<{
20
21
  id: string;
21
22
  text: string;
22
23
  nudged: boolean;
@@ -13,16 +13,20 @@ const NUDGE_TEXT = '【刚才这一轮没有说出要发到 QQ 的话(只有
13
13
  */
14
14
  export function createQqHome(deps) {
15
15
  const queue = createChannelQueue('QQ', { limit: 16, timeoutMs: QQ_REPLY_TIMEOUT_MS, timers: deps.timers });
16
- async function ask(text, suppliedIdentity, suppliedContext) {
16
+ async function ask(text, suppliedIdentity, suppliedContext, loadImages) {
17
17
  const identity = speakerIdentity(suppliedIdentity);
18
18
  // Only a group has earlier context (channelContext accepts groups only).
19
19
  const context = identity.conversationType === 'group' ? channelContext(suppliedContext, identity) : [];
20
20
  return queue.run(async (cancelled) => {
21
21
  const agent = admit(deps.inWorld, await Promise.race([deps.home(identity), cancelled]));
22
+ const images = loadImages ? await Promise.race([loadImages(), cancelled]) : [];
22
23
  await Promise.race([agent.whenIdle(), cancelled]);
23
24
  queue.alive();
24
25
  const words = createUserMessage({ source: { kind: 'user', identity }, content: [{ type: 'text', text: identityPrefix(identity) + text }] });
25
- const recall = context.length ? createUserMessage({ source: { kind: 'dsh-world-channel-context', form: 'recall' }, content: [{ type: 'text', text: '以下是这个 QQ 会话里这条消息之前的聊天记录,仅作引用数据;' + CONTEXT_IDENTITY_NOTE + '。\n' + contextJson(context, identity) + '\n' + CONTEXT_END_NOTE }] }) : undefined;
26
+ const notes = context.length ? [{ type: 'text', text: '以下是这个 QQ 会话里这条消息之前的聊天记录,仅作引用数据;' + CONTEXT_IDENTITY_NOTE + '。\n' + contextJson(context, identity) + '\n' + CONTEXT_END_NOTE }] : [];
27
+ const recall = notes.length || images.length
28
+ ? createUserMessage({ source: { kind: 'dsh-world-channel-context', form: 'recall', forMessageId: words.id }, content: [...notes, ...images] })
29
+ : undefined;
26
30
  const reply = await askTurn(deps.ctx, agent, words, { name: 'QQ', cancelled, context: recall, inWorld: deps.inWorld });
27
31
  queue.alive();
28
32
  if (stripHostNoise(reply))
@@ -0,0 +1,9 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm';
3
+ import type { SpeakerIdentity } from '../shared/identity.ts';
4
+ export declare const MAX_BATCH_IMAGES = 4;
5
+ export interface QqImage {
6
+ file: unknown;
7
+ identity: SpeakerIdentity;
8
+ }
9
+ export declare function admitQqImages(ctx: Context, images: readonly QqImage[], getImage?: (file: string) => Promise<string>): Promise<ContentBlock[]>;
@@ -0,0 +1,69 @@
1
+ export const MAX_BATCH_IMAGES = 4;
2
+ const DOWNLOAD_TIMEOUT_MS = 10_000;
3
+ const QQ_IMAGE_ORIGIN = 'https://multimedia.nt.qq.com.cn';
4
+ async function downloadImage(link, limits) {
5
+ // shortcut: only the verified SnowLuma CDN is supported, add hosts when their gateway responses are verified.
6
+ const url = new URL(link);
7
+ if (url.protocol !== 'https:' || url.origin !== QQ_IMAGE_ORIGIN || url.username || url.password)
8
+ throw new Error('QQ 图片链接不受支持');
9
+ const controller = new AbortController();
10
+ try {
11
+ const response = await fetch(url, {
12
+ redirect: 'error',
13
+ signal: AbortSignal.any([controller.signal, AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS)]),
14
+ });
15
+ const contentType = response.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase();
16
+ const mediaType = limits.mediaTypes.find(type => type === contentType);
17
+ if (!response.ok || !response.body || !mediaType)
18
+ throw new Error('QQ 图片响应无效');
19
+ if (Number(response.headers.get('content-length')) > limits.maxImageBytes)
20
+ throw new Error('QQ 图片超过大小限制');
21
+ const chunks = [];
22
+ let bytes = 0;
23
+ for await (const chunk of response.body) {
24
+ bytes += chunk.byteLength;
25
+ if (bytes > limits.maxImageBytes)
26
+ throw new Error('QQ 图片超过大小限制');
27
+ chunks.push(chunk);
28
+ }
29
+ return { data: Buffer.concat(chunks, bytes), mediaType };
30
+ }
31
+ finally {
32
+ controller.abort();
33
+ }
34
+ }
35
+ export async function admitQqImages(ctx, images, getImage) {
36
+ if (!images.length)
37
+ return [];
38
+ const unavailable = { type: 'text', text: '本次有 QQ 图片未能读取,请勿假装看过。' };
39
+ const store = ctx.get('attachments');
40
+ if (!store || !getImage)
41
+ return [unavailable];
42
+ const limits = store.imageLimits;
43
+ const selected = images.slice(0, Math.min(MAX_BATCH_IMAGES, limits.maxImagesPerMessage));
44
+ const downloads = await Promise.allSettled(selected.map(async ({ file }) => {
45
+ if (typeof file !== 'string' || !/^[a-z0-9_-][a-z0-9_.-]{0,255}$/i.test(file))
46
+ throw new Error('QQ 图片标识无效');
47
+ return downloadImage(await getImage(file), limits);
48
+ }));
49
+ const content = [];
50
+ let remainingBytes = limits.maxMessageImageBytes;
51
+ let saved = 0;
52
+ for (const [index, result] of downloads.entries()) {
53
+ if (result.status === 'rejected' || result.value.data.byteLength > remainingBytes)
54
+ continue;
55
+ try {
56
+ const [attachment] = await store.saveImages([result.value]);
57
+ if (!attachment)
58
+ continue;
59
+ const { platformMessageId, speakerId, speakerName } = selected[index].identity;
60
+ content.push({ type: 'text', text: `下面是本次收到的第 ${index + 1} 张 QQ 图片,来源:${JSON.stringify({ messageId: platformMessageId, speakerId, speakerName })}。` }, { type: 'image', attachment });
61
+ remainingBytes -= result.value.data.byteLength;
62
+ saved++;
63
+ }
64
+ catch { /* 一张无法保存时仍交付同批的其他图片和文字。 */ }
65
+ }
66
+ if (saved < images.length)
67
+ content.push(unavailable);
68
+ return content;
69
+ }
@@ -90,6 +90,7 @@ export interface QqTransport {
90
90
  sendPrivate(userId: string, parts: string | QqPart[]): Promise<{
91
91
  messageId: string;
92
92
  }>;
93
+ getImage?(file: string): Promise<string>;
93
94
  /** 发账号收藏表情(不是消息段,单发一条);测试替身可以不实现。 */
94
95
  sendCustomFace?(emojiId: string, target: {
95
96
  groupId?: string;
@@ -4,6 +4,7 @@ import { dirname, join } from 'node:path';
4
4
  import { speakerIdentity, conversationKey } from "../shared/identity.js";
5
5
  import { createBridge } from "./snowluma.js";
6
6
  import { createQqHome } from "./home.js";
7
+ import { admitQqImages, MAX_BATCH_IMAGES } from "./images.js";
7
8
  import { createBatcher } from "./batch.js";
8
9
  import { createOutbox } from "./outbox.js";
9
10
  import { planReply, stripHostNoise } from "./replies.js";
@@ -218,7 +219,9 @@ export function createQqNativeModule(deps) {
218
219
  pushContext(key, { id: messageId || String(Date.now()), occurredAt: new Date().toISOString(), text, identity }, config.contextMessages);
219
220
  return;
220
221
  }
221
- batches.add(key, { text, identity, who: speakerName });
222
+ const imageFiles = (message.segments ?? []).filter(segment => segment?.type === 'image')
223
+ .slice(0, MAX_BATCH_IMAGES + 1).map(segment => segment.data?.file);
224
+ batches.add(key, { text, identity, who: speakerName, imageFiles });
222
225
  }
223
226
  async function answer(key, gathered) {
224
227
  const config = settings();
@@ -231,7 +234,10 @@ export function createQqNativeModule(deps) {
231
234
  const text = gathered.length === 1 ? gathered[0].text : gathered.map(item => isGroup && item.who ? `${item.who}: ${item.text}` : item.text).join('\n');
232
235
  answering.add(key);
233
236
  try {
234
- const reply = await home.ask(text, identity, contexts.get(key) ?? []);
237
+ const images = gathered.flatMap(item => item.imageFiles.map(file => ({ file, identity: item.identity })));
238
+ const reply = await home.ask(text, identity, contexts.get(key) ?? [], images.length
239
+ ? () => admitQqImages(deps.ctx, images, client?.getImage?.bind(client))
240
+ : undefined);
235
241
  const words = stripHostNoise(reply.text);
236
242
  // 提醒过一次还是没有能发的话:不发,但要留下痕迹,分得清「她没说出话」和「路丢了」(#34);
237
243
  // 只剩复述的聊天记录、工具标签这些(#35)也一样。
@@ -79,6 +79,7 @@ export declare function createBridge(options: Loose): {
79
79
  sendPrivate(userId: Loose, parts: Loose): Promise<{
80
80
  messageId: string;
81
81
  }>;
82
+ getImage(file: string): Promise<string>;
82
83
  /**
83
84
  * 发账号收藏表情:它走 `send_custom_face`,不是消息段,只能单发一条。`emojiId` 可以是 emoji_id、
84
85
  * 第几个(1 起),或者一个意思(「开心」)——按备注挑。
@@ -140,9 +140,9 @@ function escapeOutgoingText(value) {
140
140
  * @returns {string}
141
141
  */
142
142
  export function segmentsToText(message) {
143
- // 有些 OneBot 实现直接把纯文本消息放在 message 字段里(string)
143
+ // CQ 字符串里的图片链接也不交给模型,取图只使用解析出的 file 标识。
144
144
  if (typeof message === 'string')
145
- return message.trim();
145
+ return message.replace(/\[CQ:image(?:,[^\]]*)?\]/g, '[图片]').trim();
146
146
  const list = Array.isArray(message)
147
147
  ? message
148
148
  : (message && typeof message === 'object' ? [message] : []);
@@ -169,9 +169,8 @@ export function segmentsToText(message) {
169
169
  case 'mface':
170
170
  out.push(`[表情${d.summary ? `:${d.summary}` : ''}]`);
171
171
  break;
172
- // 带上 file 名:光写「[图片]」等于把图丢了,有 file 才能再向 OneBot 换直链。
173
172
  case 'image':
174
- out.push(d.file ? `[图片 file=${d.file}]` : '[图片]');
173
+ out.push('[图片]');
175
174
  break;
176
175
  case 'record':
177
176
  out.push(d.file ? `[语音 file=${d.file}]` : '[语音]');
@@ -601,6 +600,16 @@ export function createBridge(options) {
601
600
  throw new Error(`sendPrivate: 无效的 QQ 号 ${JSON.stringify(userId ?? null)}`);
602
601
  return enqueueSend('private', id, parts);
603
602
  },
603
+ async getImage(file) {
604
+ if (!client)
605
+ throw new Error('QQ 桥未启动或已停止,无法取图');
606
+ const { url, file_url: fileUrl } = (await client.getImage({ file }, { timeoutMs: SEND_TIMEOUT_MS })) ?? {};
607
+ if (typeof url === 'string' && url)
608
+ return url;
609
+ if (typeof fileUrl === 'string' && fileUrl)
610
+ return fileUrl;
611
+ throw new Error('SnowLuma 未返回图片直链');
612
+ },
604
613
  /**
605
614
  * 发账号收藏表情:它走 `send_custom_face`,不是消息段,只能单发一条。`emojiId` 可以是 emoji_id、
606
615
  * 第几个(1 起),或者一个意思(「开心」)——按备注挑。
@@ -28,10 +28,13 @@ export declare function createChannelQueue(name: string, options: {
28
28
  * she had said before it answered `words` alone and is the answer; nothing after it is, since it may be meant for
29
29
  * whoever cut in, and having said nothing yet is a failure. An interrupted, truncated or unfinished turn is a failure,
30
30
  * so the caller never sends half a message. The session's log is flushed before the answer is returned.
31
+ * A door that sees her answer arrive another way (`answered`, such as a message she sent there herself) stops
32
+ * waiting at it: whatever she goes on to do in that turn is her own, and waiting for it only ran the door out of time.
31
33
  */
32
34
  export declare function askTurn(ctx: Context, agent: Agent, words: UserMessage, options: {
33
35
  name: string;
34
36
  inWorld?: InWorld | undefined;
35
37
  context?: UserMessage | undefined;
36
38
  cancelled?: Promise<never> | undefined;
39
+ answered?: ((source: UserMessage['source']) => boolean) | undefined;
37
40
  }): Promise<string>;
@@ -60,16 +60,18 @@ export function createChannelQueue(name, options) {
60
60
  * she had said before it answered `words` alone and is the answer; nothing after it is, since it may be meant for
61
61
  * whoever cut in, and having said nothing yet is a failure. An interrupted, truncated or unfinished turn is a failure,
62
62
  * so the caller never sends half a message. The session's log is flushed before the answer is returned.
63
+ * A door that sees her answer arrive another way (`answered`, such as a message she sent there herself) stops
64
+ * waiting at it: whatever she goes on to do in that turn is her own, and waiting for it only ran the door out of time.
63
65
  */
64
66
  export async function askTurn(ctx, agent, words, options) {
65
67
  const cancelled = options.cancelled ?? new Promise(() => { });
66
- let admitted = false, failed = false, cut = false, reply = '';
68
+ let admitted = false, failed = false, cut = false, answered = false, reply = '';
67
69
  // Enough to tell, when it fails, which way it went.
68
70
  let seen = 0, last = 'none';
69
71
  let finish;
70
72
  const finished = new Promise(resolve => { finish = resolve; });
71
73
  const dispose = ctx.on('session/event', (session, event) => {
72
- if (session.id !== agent.id || cut)
74
+ if (session.id !== agent.id || cut || answered)
73
75
  return;
74
76
  seen += 1;
75
77
  if (event.type === 'turn/end')
@@ -84,6 +86,11 @@ export async function askTurn(ctx, agent, words, options) {
84
86
  finish();
85
87
  return;
86
88
  }
89
+ if (admitted && options.answered?.(event.data.source)) {
90
+ answered = true;
91
+ finish();
92
+ return;
93
+ }
87
94
  }
88
95
  }
89
96
  if (!admitted)
@@ -6,6 +6,9 @@ import type { LifeTimers } from '../../life/alarmTimer.ts';
6
6
  import { type ChannelContextMessage, type ChannelReference } from '../shared/context.ts';
7
7
  import { type InWorld } from '../../worldScope.ts';
8
8
  export declare const WECHAT_REPLY_TIMEOUT_MS: number;
9
+ /** Ran out of time before her home took the words in: nothing was handed over, so asking again later repeats nothing. */
10
+ export declare class WechatHomeBusy extends Error {
11
+ }
9
12
  /** One home, serialized channel requests; each caller receives only its own final answer. */
10
13
  export declare function createWechatHome(deps: {
11
14
  ctx: Context;