dsh-world 0.7.5 → 0.8.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 (115) hide show
  1. package/CHANGELOG.md +541 -446
  2. package/CONTRIBUTING.md +174 -163
  3. package/INSTALL.md +314 -314
  4. package/NOTES.md +98 -5
  5. package/README.md +332 -258
  6. package/client/index.js +1126 -986
  7. package/lib/compaction.d.ts +7 -1
  8. package/lib/compaction.js +8 -3
  9. package/lib/console/routes.d.ts +11 -3
  10. package/lib/console/routes.js +12 -7
  11. package/lib/console/snapshot.d.ts +2 -0
  12. package/lib/console/snapshot.js +1 -0
  13. package/lib/ecosystem/computer/browser/browser.d.ts +34 -0
  14. package/lib/ecosystem/computer/browser/browser.js +101 -0
  15. package/lib/ecosystem/computer/browser/cdp.d.ts +22 -0
  16. package/lib/ecosystem/computer/browser/cdp.js +91 -0
  17. package/lib/ecosystem/computer/browser/edge.d.ts +15 -0
  18. package/lib/ecosystem/computer/browser/edge.js +80 -0
  19. package/lib/ecosystem/computer/browser/page.d.ts +41 -0
  20. package/lib/ecosystem/computer/browser/page.js +140 -0
  21. package/lib/ecosystem/computer/browser/pageLib.d.ts +7 -0
  22. package/lib/ecosystem/computer/browser/pageLib.js +106 -0
  23. package/lib/ecosystem/computer/desktop/desktop.d.ts +91 -0
  24. package/lib/ecosystem/computer/desktop/desktop.js +322 -0
  25. package/lib/ecosystem/computer/desktop/driver.d.ts +34 -0
  26. package/lib/ecosystem/computer/desktop/driver.js +197 -0
  27. package/lib/ecosystem/computer/desktop/processes.d.ts +5 -0
  28. package/lib/ecosystem/computer/desktop/processes.js +15 -0
  29. package/lib/ecosystem/computer/desktop/recording.d.ts +77 -0
  30. package/lib/ecosystem/computer/desktop/recording.js +187 -0
  31. package/lib/ecosystem/computer/desktop/screen.d.ts +33 -0
  32. package/lib/ecosystem/computer/desktop/screen.js +182 -0
  33. package/lib/ecosystem/computer/desktop/turns.d.ts +58 -0
  34. package/lib/ecosystem/computer/desktop/turns.js +94 -0
  35. package/lib/ecosystem/computer/desktop/watching.d.ts +25 -0
  36. package/lib/ecosystem/computer/desktop/watching.js +81 -0
  37. package/lib/ecosystem/computer/inbox/inbox.d.ts +32 -0
  38. package/lib/ecosystem/computer/inbox/inbox.js +142 -0
  39. package/lib/ecosystem/computer/index.d.ts +82 -0
  40. package/lib/ecosystem/computer/index.js +133 -0
  41. package/lib/ecosystem/computer/procedures/alarm.d.ts +13 -0
  42. package/lib/ecosystem/computer/procedures/alarm.js +46 -0
  43. package/lib/ecosystem/computer/procedures/archive.d.ts +32 -0
  44. package/lib/ecosystem/computer/procedures/archive.js +68 -0
  45. package/lib/ecosystem/computer/procedures/catalog.d.ts +7 -0
  46. package/lib/ecosystem/computer/procedures/catalog.js +75 -0
  47. package/lib/ecosystem/computer/procedures/format.d.ts +167 -0
  48. package/lib/ecosystem/computer/procedures/format.js +332 -0
  49. package/lib/ecosystem/computer/procedures/library.d.ts +53 -0
  50. package/lib/ecosystem/computer/procedures/library.js +178 -0
  51. package/lib/ecosystem/computer/procedures/run.d.ts +80 -0
  52. package/lib/ecosystem/computer/procedures/run.js +245 -0
  53. package/lib/ecosystem/computer/tools.d.ts +21 -0
  54. package/lib/ecosystem/computer/tools.js +197 -0
  55. package/lib/ecosystem/qq-native/images.d.ts +8 -1
  56. package/lib/ecosystem/qq-native/images.js +34 -15
  57. package/lib/ecosystem/qq-native/index.js +5 -5
  58. package/lib/ecosystem/shared/turn.d.ts +3 -0
  59. package/lib/ecosystem/shared/turn.js +13 -3
  60. package/lib/ecosystem/wechat/home.d.ts +1 -0
  61. package/lib/ecosystem/wechat/home.js +9 -3
  62. package/lib/ecosystem/wechat/images.js +3 -5
  63. package/lib/ecosystem/wechat/index.d.ts +6 -0
  64. package/lib/ecosystem/wechat/index.js +3 -1
  65. package/lib/ecosystem/wechat/presence.d.ts +5 -1
  66. package/lib/ecosystem/wechat/presence.js +22 -13
  67. package/lib/ecosystem/wechat/reading.d.ts +1 -1
  68. package/lib/ecosystem/wechat/reading.js +2 -2
  69. package/lib/elsewhere.d.ts +34 -0
  70. package/lib/elsewhere.js +40 -0
  71. package/lib/history.d.ts +5 -0
  72. package/lib/history.js +7 -1
  73. package/lib/hostContract.d.ts +6 -0
  74. package/lib/images.d.ts +30 -0
  75. package/lib/images.js +101 -0
  76. package/lib/index.d.ts +4 -0
  77. package/lib/index.js +98 -6
  78. package/lib/life/photoTools.js +4 -15
  79. package/lib/life/runtime.d.ts +12 -2
  80. package/lib/life/runtime.js +93 -12
  81. package/lib/life/toolRegistry.d.ts +9 -3
  82. package/lib/life/toolRegistry.js +46 -27
  83. package/lib/life/turnCost.d.ts +16 -0
  84. package/lib/life/turnCost.js +44 -0
  85. package/lib/life/types.d.ts +17 -0
  86. package/lib/memoryTools.d.ts +4 -2
  87. package/lib/memoryTools.js +25 -5
  88. package/lib/mood.d.ts +5 -1
  89. package/lib/mood.js +25 -8
  90. package/lib/promptSection.d.ts +3 -0
  91. package/lib/promptSection.js +29 -42
  92. package/lib/runtime.js +44 -17
  93. package/lib/sessionNews.d.ts +17 -0
  94. package/lib/sessionNews.js +55 -0
  95. package/lib/store.d.ts +6 -0
  96. package/lib/store.js +17 -3
  97. package/lib/types.d.ts +4 -0
  98. package/lib/worldAdd.d.ts +9 -0
  99. package/lib/worldAdd.js +39 -0
  100. package/lib/worldPrompt.d.ts +3 -2
  101. package/lib/worldPrompt.js +3 -2
  102. package/lib/worldRoutes.d.ts +5 -2
  103. package/lib/worldRoutes.js +29 -4
  104. package/lib/worldSwitch.d.ts +8 -2
  105. package/lib/worldSwitch.js +11 -5
  106. package/package.json +132 -129
  107. package/src/ecosystem/README.md +2 -1
  108. package/src/ecosystem/computer/NOTICE.md +27 -0
  109. package/src/ecosystem/computer/README.md +244 -0
  110. package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
  111. package/src/ecosystem/wechat/windows/README.md +4 -2
  112. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  113. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  114. package/templates/world-prompt-memory.md +10 -10
  115. package/templates/world-prompt.md +65 -65
@@ -0,0 +1,40 @@
1
+ export function ago(milliseconds) {
2
+ const minutes = Math.max(0, Math.floor(milliseconds / 60_000));
3
+ return minutes < 1 ? '刚刚' : minutes < 60 ? `${minutes} 分钟前` : minutes < 1440 ? `${Math.floor(minutes / 60)} 小时前` : `${Math.floor(minutes / 1440)} 天前`;
4
+ }
5
+ export const ELSEWHERE_HEADER = '【别处的动静】我不在场的地方攒下的消息,只是让我知道,不是对我说的话;要不要看、要不要回,我自己决定。';
6
+ export function renderElsewhere(groups) {
7
+ return [ELSEWHERE_HEADER, ...groups.flatMap(group => group.news.map(news => `- ${news.text}`)), `(${groups.map(group => group.look).join(';')})`].join('\n');
8
+ }
9
+ /**
10
+ * The one way news from elsewhere reaches her: sources register here, and each session is told what is new since it
11
+ * was last told. An event is told once; whatever she has not looked at comes back with the next news from that place.
12
+ */
13
+ export function createElsewhere() {
14
+ const sources = new Set();
15
+ const told = new WeakMap();
16
+ return {
17
+ register(source) {
18
+ sources.add(source);
19
+ return () => { sources.delete(source); };
20
+ },
21
+ /** The block of what is new for this session, marking it told; undefined when nothing is. */
22
+ take(session, now) {
23
+ const previous = told.get(session) ?? new Map();
24
+ const marks = new Map(), groups = [];
25
+ for (const source of sources) {
26
+ const pending = source.news(now, session);
27
+ for (const news of pending)
28
+ marks.set(news.key, news.mark);
29
+ const fresh = pending.filter(news => previous.get(news.key) !== news.mark);
30
+ if (fresh.length)
31
+ groups.push({ look: source.look, news: fresh });
32
+ }
33
+ // A place she has since caught up with is forgotten, so its next message is news again.
34
+ told.set(session, marks);
35
+ return groups.length ? renderElsewhere(groups) : undefined;
36
+ },
37
+ /** What she was told is gone from her view (compaction, leaving her world): tell again what is still pending. */
38
+ retell(session) { told.delete(session); },
39
+ };
40
+ }
package/lib/history.d.ts CHANGED
@@ -15,4 +15,9 @@ export declare function shortenLocalImages(content: string): string;
15
15
  * name, so the memory reads as hers rather than a generic assistant's. `homeOnly` (archiveScope home) says the
16
16
  * memory is her time at home.
17
17
  */
18
+ /**
19
+ * Said wherever a group session meets what was said elsewhere (her recent chats, news from elsewhere): those words
20
+ * are between her and one person, and a group is the wrong place to repeat them.
21
+ */
22
+ export declare const GROUP_PRIVACY = "\u6211\u73B0\u5728\u5728\u7FA4\u91CC\u3002\u5BB6\u91CC\u548C\u79C1\u804A\u91CC\u7684\u8BDD\u662F\u6211\u548C\u5BF9\u65B9\u4E4B\u95F4\u7684\uFF1A\u4E0D\u628A\u539F\u8BDD\u548C\u7EC6\u8282\u5E26\u8FDB\u7FA4\u91CC\u8BF4\uFF0C\u4E5F\u4E0D\u66FF\u8C01\u8F6C\u8FF0\uFF1B\u7FA4\u91CC\u6709\u4EBA\u95EE\u8D77\uFF0C\u5C31\u5E26\u8FC7\u53BB\uFF0C\u4E0D\u4E71\u8BF4\u3002";
18
23
  export declare function renderHistory(turns: readonly ConversationTurn[], timeZone: string, names: WorldNames, homeOnly?: boolean): string;
package/lib/history.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { shortenArchivedImages } from "./images.js";
1
2
  import { formatLocalTime } from "./timeContext.js";
2
3
  import { homeTitle } from "./worldPrompt.js";
3
4
  import { replyWords } from "./ecosystem/wechat/archive.js";
@@ -98,6 +99,11 @@ function sentState({ status, confirmed, total }) {
98
99
  * name, so the memory reads as hers rather than a generic assistant's. `homeOnly` (archiveScope home) says the
99
100
  * memory is her time at home.
100
101
  */
102
+ /**
103
+ * Said wherever a group session meets what was said elsewhere (her recent chats, news from elsewhere): those words
104
+ * are between her and one person, and a group is the wrong place to repeat them.
105
+ */
106
+ export const GROUP_PRIVACY = '我现在在群里。家里和私聊里的话是我和对方之间的:不把原话和细节带进群里说,也不替谁转述;群里有人问起,就带过去,不乱说。';
101
107
  export function renderHistory(turns, timeZone, names, homeOnly = false) {
102
108
  const selfName = names.companion;
103
109
  if (!turns.length)
@@ -114,7 +120,7 @@ export function renderHistory(turns, timeZone, names, homeOnly = false) {
114
120
  role: message.role === 'assistant' ? selfName : message.role, time: minute(message.occurredAt),
115
121
  ...(chat ? { via: chat } : {}), ...(chat && message.role === 'user' && message.conversationType === 'group' ? { who: who(message) } : {}),
116
122
  ...(message.wechatDelivery ? { sent: sentState(message.wechatDelivery) } : {}),
117
- ...(words ? { content: shortenLocalImages(words) } : {}), ...(message.status === 'interrupted' ? { interrupted: true } : {}),
123
+ ...(words ? { content: shortenArchivedImages(shortenLocalImages(words)) } : {}), ...(message.status === 'interrupted' ? { interrupted: true } : {}),
118
124
  };
119
125
  }),
120
126
  }));
@@ -29,6 +29,12 @@ declare module '@deepseek-ai/dsh-llm' {
29
29
  form: 'snapshot';
30
30
  worldId: string;
31
31
  };
32
+ /** 别处的动静:她不在场的地方攒下多少条、最后是谁,只是事实,不是谁说的话,不进归档。 */
33
+ 'dsh-world-elsewhere': {
34
+ kind: 'dsh-world-elsewhere';
35
+ form: 'notice';
36
+ worldId: string;
37
+ };
32
38
  'dsh-world-channel-context': {
33
39
  kind: 'dsh-world-channel-context';
34
40
  form: 'recall';
@@ -0,0 +1,30 @@
1
+ import type { AttachmentStore, ImageAttachmentRef, ImageMediaType } from '@deepseek-ai/dsh-attachment';
2
+ import { type ContentBlock, type UserMessage } from '@deepseek-ai/dsh-llm';
3
+ /** The one line a picture is in her memory: everything the store needs to hand the same picture back. */
4
+ export declare function imagePlaceholder(ref: ImageAttachmentRef): string;
5
+ /** The stored picture a placeholder names, or undefined when the text is not one copied whole. */
6
+ export declare function placeholderImage(text: string): ImageAttachmentRef | undefined;
7
+ /** Archived text for her to read: each image block becomes its placeholder; anything that is not a whole one stays as written. */
8
+ export declare function shortenArchivedImages(content: string): string;
9
+ /** A picture handed to her in the next message, as tool results carry text only. */
10
+ /** The stored picture a placeholder names, read back and verified; a picture the store no longer has is said, not shown wrong. */
11
+ export declare function storedImage(store: AttachmentStore | undefined, placeholder: string, signal?: AbortSignal): Promise<ImageAttachmentRef>;
12
+ /** Pictures kept but not put in front of her: one line each, to open with memory_image when she wants to. */
13
+ export declare function keptLines(lead: string, refs: readonly ImageAttachmentRef[]): ContentBlock[];
14
+ /** The four types the store keeps, told from the bytes themselves: never from a file name or what a server claims. */
15
+ export declare function sniffMediaType(bytes: Uint8Array): ImageMediaType | undefined;
16
+ /** A picture as it comes in, from any channel, her screen or a file; its type is read from the bytes when not given. */
17
+ export interface IncomingImage {
18
+ data: Uint8Array;
19
+ mediaType?: ImageMediaType;
20
+ name?: string;
21
+ }
22
+ /**
23
+ * The one way a picture enters her world: into the host's store, all of the batch or none of it, in order. Without a
24
+ * store, or with bytes that are no picture, it throws; a caller that goes on without the picture says so itself.
25
+ */
26
+ export declare function keepImages(store: AttachmentStore | undefined, images: readonly IncomingImage[]): Promise<readonly ImageAttachmentRef[]>;
27
+ /** A kept picture put in front of her, with the words that say where it is from. */
28
+ export declare const shownImage: (caption: string, attachment: ImageAttachmentRef) => ContentBlock[];
29
+ /** Pictures and their words handed to her in the next message, as tool results carry text only. */
30
+ export declare const imagesMessage: (content: ContentBlock[]) => UserMessage;
package/lib/images.js ADDED
@@ -0,0 +1,101 @@
1
+ import { createUserMessage } from '@deepseek-ai/dsh-llm';
2
+ /**
3
+ * Pictures in her world. The bytes live once, in the host's attachment store: content-addressed, immutable, never
4
+ * deleted. What her memory carries of a picture is one line that names that stored object completely, so the line is
5
+ * both what she reads and what brings the picture back; nothing else is indexed or kept.
6
+ */
7
+ const MEDIA_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'image/gif'];
8
+ /** The store's ids are opaque; today they are `sha256:<hex>`. Anything that could break out of the line is refused. */
9
+ const ID = /^[A-Za-z0-9:_.-]{8,200}$/;
10
+ /** How the archive has always written an image block (`visibleText`); read back, never rewritten. */
11
+ const ARCHIVED = /\{"type":"image","attachment":(\{[^{}]*\})\}/g;
12
+ const PLACEHOLDER = /\[图片 ([A-Za-z0-9:_.-]{8,200}) (image\/[a-z]+) (\d{1,6})×(\d{1,6}) (\d{1,10})字节(?: ([^[\]\n]{1,120}))?\]/;
13
+ const count = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
14
+ function checkedRef(value) {
15
+ const ref = value;
16
+ if (!ref || typeof ref.attachmentId !== 'string' || !ID.test(ref.attachmentId) || !MEDIA_TYPES.includes(ref.mediaType)
17
+ || !count(ref.width) || !count(ref.height) || !count(ref.bytes))
18
+ return undefined;
19
+ const name = typeof ref.name === 'string' ? ref.name.replace(/[[\]\r\n]/g, ' ').trim().slice(0, 120) : '';
20
+ return { attachmentId: ref.attachmentId, mediaType: ref.mediaType, width: ref.width, height: ref.height, bytes: ref.bytes, ...(name ? { name } : {}) };
21
+ }
22
+ /** The one line a picture is in her memory: everything the store needs to hand the same picture back. */
23
+ export function imagePlaceholder(ref) {
24
+ const checked = checkedRef(ref);
25
+ if (!checked)
26
+ throw new Error('图片引用不完整');
27
+ return `[图片 ${checked.attachmentId} ${checked.mediaType} ${checked.width}×${checked.height} ${checked.bytes}字节${checked.name ? ` ${checked.name}` : ''}]`;
28
+ }
29
+ /** The stored picture a placeholder names, or undefined when the text is not one copied whole. */
30
+ export function placeholderImage(text) {
31
+ const found = PLACEHOLDER.exec(text);
32
+ return found ? checkedRef({ attachmentId: found[1], mediaType: found[2], width: Number(found[3]), height: Number(found[4]), bytes: Number(found[5]), ...(found[6] ? { name: found[6] } : {}) }) : undefined;
33
+ }
34
+ /** Archived text for her to read: each image block becomes its placeholder; anything that is not a whole one stays as written. */
35
+ export function shortenArchivedImages(content) {
36
+ return content.replace(ARCHIVED, (whole, attachment) => {
37
+ let ref;
38
+ try {
39
+ ref = checkedRef(JSON.parse(attachment));
40
+ }
41
+ catch {
42
+ return whole;
43
+ }
44
+ return ref ? imagePlaceholder(ref) : whole;
45
+ });
46
+ }
47
+ /** A picture handed to her in the next message, as tool results carry text only. */
48
+ /** The stored picture a placeholder names, read back and verified; a picture the store no longer has is said, not shown wrong. */
49
+ export async function storedImage(store, placeholder, signal) {
50
+ const ref = placeholderImage(placeholder);
51
+ if (!ref)
52
+ throw new Error('image 要原样填记忆里那一整段 [图片 sha256:… ],从 [ 到 ]');
53
+ if (!store)
54
+ throw new Error('宿主图片存储不可用');
55
+ try {
56
+ return (await store.readImage(ref, signal)).ref;
57
+ }
58
+ catch (error) {
59
+ signal?.throwIfAborted();
60
+ throw new Error(`这张图片在本地找不到了,或者和记下的不一样:${error instanceof Error ? error.message : String(error)}`);
61
+ }
62
+ }
63
+ /** Pictures kept but not put in front of her: one line each, to open with memory_image when she wants to. */
64
+ export function keptLines(lead, refs) {
65
+ return refs.length ? [{ type: 'text', text: `${lead}\n${refs.map(imagePlaceholder).join('\n')}\n想看哪张,把那一整段 [图片 …] 交给 memory_image。` }] : [];
66
+ }
67
+ /** The four types the store keeps, told from the bytes themselves: never from a file name or what a server claims. */
68
+ export function sniffMediaType(bytes) {
69
+ const starts = (...head) => head.every((byte, at) => bytes[at] === byte);
70
+ if (starts(0x89, 0x50, 0x4e, 0x47))
71
+ return 'image/png';
72
+ if (starts(0xff, 0xd8, 0xff))
73
+ return 'image/jpeg';
74
+ if (starts(0x47, 0x49, 0x46, 0x38))
75
+ return 'image/gif';
76
+ if (starts(0x52, 0x49, 0x46, 0x46) && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50)
77
+ return 'image/webp';
78
+ return undefined;
79
+ }
80
+ /**
81
+ * The one way a picture enters her world: into the host's store, all of the batch or none of it, in order. Without a
82
+ * store, or with bytes that are no picture, it throws; a caller that goes on without the picture says so itself.
83
+ */
84
+ export async function keepImages(store, images) {
85
+ if (!store)
86
+ throw new Error('宿主图片存储不可用');
87
+ const inputs = images.map(({ data, mediaType, name }) => {
88
+ const type = mediaType ?? sniffMediaType(data);
89
+ if (!type)
90
+ throw new Error('只认 png、jpg、webp、gif 图片');
91
+ return { data, mediaType: type, ...(name ? { name } : {}) };
92
+ });
93
+ const refs = await store.saveImages(inputs);
94
+ if (refs.length !== inputs.length)
95
+ throw new Error('图片没能存下来');
96
+ return refs;
97
+ }
98
+ /** A kept picture put in front of her, with the words that say where it is from. */
99
+ export const shownImage = (caption, attachment) => [{ type: 'text', text: caption }, { type: 'image', attachment }];
100
+ /** Pictures and their words handed to her in the next message, as tool results carry text only. */
101
+ export const imagesMessage = (content) => createUserMessage({ source: { kind: 'dsh-world-channel-context', form: 'recall' }, content });
package/lib/index.d.ts CHANGED
@@ -93,6 +93,8 @@ export declare const Config: z<Schemastery.ObjectS<NoInfer<{
93
93
  }>>, "defined">;
94
94
  }>>, "plain">;
95
95
  wechat: z<boolean, boolean, "defined">;
96
+ computer: z<boolean, boolean, "defined">;
97
+ desktop: z<boolean, boolean, "defined">;
96
98
  wechatAdapter: z<Schemastery.ObjectS<NoInfer<{
97
99
  python: z<string, string, "plain">;
98
100
  config: z<string, string, "plain">;
@@ -188,6 +190,8 @@ export declare const Config: z<Schemastery.ObjectS<NoInfer<{
188
190
  }>>, "defined">;
189
191
  }>>, "plain">;
190
192
  wechat: z<boolean, boolean, "defined">;
193
+ computer: z<boolean, boolean, "defined">;
194
+ desktop: z<boolean, boolean, "defined">;
191
195
  wechatAdapter: z<Schemastery.ObjectS<NoInfer<{
192
196
  python: z<string, string, "plain">;
193
197
  config: z<string, string, "plain">;
package/lib/index.js CHANGED
@@ -20,7 +20,9 @@ import { toastSupported, windowsToast } from "./life/notify.js";
20
20
  import { createLifeRuntime } from "./life/runtime.js";
21
21
  import { createOutlet } from "./ecosystem/shared/outlets.js";
22
22
  import { readPersona, writePersona } from "./persona.js";
23
+ import { createComputerModule } from "./ecosystem/computer/index.js";
23
24
  import { MOOD_FILE } from "./mood.js";
25
+ import { createElsewhere } from "./elsewhere.js";
24
26
  import { qqRoute } from "./ecosystem/qq/route.js";
25
27
  import { createConsoleDoors, discoverSnowlumaHome } from "./ecosystem/qq/panel.js";
26
28
  import { createMemory } from "./memory.js";
@@ -35,6 +37,7 @@ import { createAdapterSupervisor } from "./ecosystem/wechat/supervisor.js";
35
37
  import { createChannelSessions } from "./ecosystem/shared/sessions.js";
36
38
  import { canonicalPath, createArchiveSelection, createWorkspaceMembership } from "./archiveSelection.js";
37
39
  import { inWorld } from "./sourceArchive.js";
40
+ import { addWorldEntry } from "./worldAdd.js";
38
41
  import { validWorldId, worldListRoute, worldPath } from "./worldRoutes.js";
39
42
  import { createWorkspacePermissions } from "./workspacePermissions.js";
40
43
  import { createPhotoStore } from "./life/photos.js";
@@ -92,6 +95,8 @@ export const Config = z.object({
92
95
  }).default({ port: 0, token: '', title: '阳台', contextMessages: 6 }),
93
96
  }).description('QQ 直接连 OneBot:不经过 qq-bridge 进程,会话与状态都由这个插件自己管'),
94
97
  wechat: z.boolean().default(false),
98
+ computer: z.boolean().default(false).description('她自己的浏览器与自己学会的工具(只在家会话)'),
99
+ desktop: z.boolean().default(false).description('她也用伙伴的电脑,在后台操作(Windows,只在家会话,要先开 computer)'),
95
100
  wechatAdapter: z.object({ python: z.string(), config: z.string() }).description('随微信模块启动和停止 Windows 适配器;写上即视为允许它发送消息'),
96
101
  toast: z.boolean().default(true),
97
102
  toastAppId: z.string().description('系统通知用的 AppUserModelID;不填就在开始菜单里找 DSH 桌面版'),
@@ -160,6 +165,8 @@ export function apply(ctx, config) {
160
165
  // Her self-description lives in her room whether or not the life line runs; she writes it with her file tools.
161
166
  const selfFile = config.selfFile || join(worldDir, 'room', '我是谁.md');
162
167
  const moodFile = join(roomDir, MOOD_FILE);
168
+ /** Every source of news from where she is not registers here; the world runtime tells her sessions. */
169
+ const elsewhere = createElsewhere();
163
170
  const personaFile = config.personaFile || join(worldDir, 'persona.md');
164
171
  const worldPromptFile = config.worldPromptFile || join(worldDir, 'world-prompt.md');
165
172
  const options = { worldDir, roomDir, diaryDir: config.diaryDir || join(worldDir, 'diary'), selfFile, schedule: () => ({ review: power.reviewTime(), wake: power.wakeTime() }), timeZone: config.timeZone, alarmFile: config.alarmTemplateFile || join(worldDir, 'alarm.md') };
@@ -203,7 +210,7 @@ export function apply(ctx, config) {
203
210
  return;
204
211
  try {
205
212
  const prompt = { file: worldPromptFile, persona: personaFile, conversationsDir: join(worldDir, 'conversations'), names: names(), memoryTools: true, ...(config.life ? { life: options } : {}) };
206
- memory = createWorldRuntime(ctx, { ...memoryConfig, selfFile, moodFile }, store, { prompt, names, isHome: id => isHome(id), archiveSession, inWorkspace, memory: memoryService }, power.memoryTurns, power.coldHours);
213
+ memory = createWorldRuntime(ctx, { ...memoryConfig, selfFile, moodFile }, store, { prompt, names, isHome: id => isHome(id), archiveSession, inWorkspace, memory: memoryService, elsewhere }, power.memoryTurns, power.coldHours);
207
214
  await memory.start({ fresh });
208
215
  faults.clear('memory');
209
216
  }
@@ -298,10 +305,47 @@ export function apply(ctx, config) {
298
305
  let bridge;
299
306
  let wechat;
300
307
  let qqNative;
308
+ let computer;
309
+ /** Her own browser follows the life line and its console switch; a failure to start is a fault, never a broken plugin. */
310
+ function startComputer() {
311
+ if (computer || !config.computer || !power.computerEnabled())
312
+ return;
313
+ try {
314
+ computer = createComputerModule({ worldDir, worldId: config.worldId, faults, store, archived: archiveSession, attachments: () => lifeCtx.get('attachments'),
315
+ // 信箱随「她用电脑」开关:替她看任务栏(按钮在闪、系统通知),有动静时汇总成一句话告诉她,看不看由她定。
316
+ inbox: {
317
+ timeZone: config.timeZone, tell: async (text) => await life?.tell(text) ?? false,
318
+ // A program with a channel of its own reaches her there; the taskbar would tell her the same thing twice.
319
+ ignore: () => [...(wechat ? ['Weixin', 'WeChat', '微信'] : []), ...(qqNative ? ['QQ'] : [])],
320
+ },
321
+ companion: () => names().companion, notice: text => life?.notice(text) });
322
+ computer.setDesktop(Boolean(config.desktop) && power.desktopEnabled());
323
+ faults.clear('computer-module');
324
+ }
325
+ catch (error) {
326
+ faults.set('computer-module', `她的浏览器模块没能启动:${errorText(error)}`);
327
+ lifeCtx.logger.error(error);
328
+ }
329
+ }
330
+ /** The WeChat line is worked out first and never waits on her tools; if they fail, that is a fault and the line still goes out. */
331
+ async function alarmContext(now) {
332
+ const presence = wechat?.presence(now);
333
+ try {
334
+ const facts = await computer?.alarmFacts();
335
+ faults.clear('computer-alarm');
336
+ return [presence, facts].filter(Boolean).join('\n') || undefined;
337
+ }
338
+ catch (error) {
339
+ faults.set('computer-alarm', `她闹钟前的工具没能跑:${errorText(error)}`);
340
+ return presence;
341
+ }
342
+ }
343
+ function stopComputer() { computer?.stop(); computer = undefined; faults.clear('computer-module'); faults.clear('computer-alarm'); }
301
344
  let qqChannels;
302
345
  let adapter;
303
346
  let channels;
304
347
  let unregisterWechat;
348
+ let unregisterWechatNews;
305
349
  let lifeStop;
306
350
  let wechatClosing = Promise.resolve();
307
351
  /** Starts the WeChat module (its tools, route and, when configured, the adapter) against the running life line. */
@@ -313,10 +357,11 @@ export function apply(ctx, config) {
313
357
  return;
314
358
  const groupSessions = createChannelSessions({ ctx: lifeCtx, host, store, roomDir: options.roomDir, roomTitle: roomTitle(names()), agentPreset: config.agentPreset });
315
359
  channels = groupSessions;
316
- wechat = createWechatModule({ ctx: lifeCtx, worldId: config.worldId, inWorld: inWorldSession, home: identity => identity.conversationType === 'group' ? groupSessions.open(identity) : current.home(), origin: () => host.origin(), file: join(worldDir, 'wechat.json'), incomingDir: join(worldDir, 'wechat-incoming'), allowedHosts: config.allowedHosts, settings: () => ({ contextMessages: power.wechatContext(), bufferSeconds: power.wechatBufferSeconds() }),
360
+ wechat = createWechatModule({ ctx: lifeCtx, worldId: config.worldId, inWorld: inWorldSession, home: identity => identity.conversationType === 'group' ? groupSessions.open(identity) : current.home(), midTurn: agent => current.isHome(agent.id), origin: () => host.origin(), file: join(worldDir, 'wechat.json'), incomingDir: join(worldDir, 'wechat-incoming'), allowedHosts: config.allowedHosts, settings: () => ({ contextMessages: power.wechatContext(), bufferSeconds: power.wechatBufferSeconds() }),
317
361
  ...createWechatRecorder(store, config.worldId, archiveSession) });
318
362
  wechat.start();
319
363
  unregisterWechat = webServer.register(wechat.route);
364
+ unregisterWechatNews = elsewhere.register(wechat.elsewhere);
320
365
  if (config.wechatAdapter?.python && config.wechatAdapter.config) {
321
366
  adapter = createAdapterSupervisor({ python: config.wechatAdapter.python, config: config.wechatAdapter.config, logFile: join(worldDir, 'wechat-adapter.log'), log: message => lifeCtx.logger.warn(message) });
322
367
  adapter.start();
@@ -326,6 +371,8 @@ export function apply(ctx, config) {
326
371
  function stopWechat() {
327
372
  unregisterWechat?.();
328
373
  unregisterWechat = undefined;
374
+ unregisterWechatNews?.();
375
+ unregisterWechatNews = undefined;
329
376
  const closingAdapter = adapter?.stop();
330
377
  adapter = undefined;
331
378
  const closing = Promise.all([closingAdapter, wechat?.stop()]).then(() => { });
@@ -413,8 +460,9 @@ export function apply(ctx, config) {
413
460
  return;
414
461
  const outlets = config.outlets.map(outlet => createOutlet(outlet, () => host.origin()));
415
462
  const notifier = { ...(config.toast && toastSupported() ? { toast: windowsToast(config.toastAppId || undefined) } : {}), outlets };
416
- const runtime = createLifeRuntime({ ctx: lifeCtx, config: { ...config, companionName: names().companion }, options, store: lifeStore, host, notifier, dailyBudget: power.dailyBudget, inWorkspace, photos, alarmContext: now => wechat?.presence(now), afterReview: () => { if (power.compactAfterReview())
417
- homeApply.request(); } });
463
+ const runtime = createLifeRuntime({ ctx: lifeCtx, config: { ...config, companionName: names().companion }, options, store: lifeStore, host, notifier, dailyBudget: power.dailyBudget, inWorkspace, photos, alarmContext: now => alarmContext(now),
464
+ homeTools: { tools: agent => computer?.tools(agent) ?? [], revision: () => computer?.revision() ?? 'off' }, afterReview: () => { if (power.compactAfterReview())
465
+ homeApply.request(); }, partnerSpoke: () => computer?.partnerSpoke() });
418
466
  life = current = runtime;
419
467
  isHome = runtime.isHome;
420
468
  if (config.bridge) {
@@ -427,6 +475,7 @@ export function apply(ctx, config) {
427
475
  await startQqNative(runtime);
428
476
  if (disposed || hostAbort.signal.aborted)
429
477
  return;
478
+ startComputer();
430
479
  faults.clear('life');
431
480
  const started = runtime.start({ resumed });
432
481
  if (resumed)
@@ -444,6 +493,7 @@ export function apply(ctx, config) {
444
493
  life?.stop();
445
494
  const closingWechat = stopWechat();
446
495
  stopQqNative();
496
+ stopComputer();
447
497
  bridge = undefined;
448
498
  life = undefined;
449
499
  return lifeStop = Promise.all([lifeStop, closingWechat, homeApply.cancel()]).then(() => { });
@@ -461,7 +511,7 @@ export function apply(ctx, config) {
461
511
  // Her album lives in her room, so the console shows it whether or not the life line is running.
462
512
  const sources = consoleSources({ ctx: lifeCtx, config: memoryConfig, archive: store, moodFile, longTerm: memoryService, names, ...(lifeStore ? { lifeStore } : {}), life: () => life, homeApply: homeApply.snapshot, compactAfterReview: power.compactAfterReview, ...(config.life ? { schedule: () => ({ review: power.reviewTime(), wake: power.wakeTime(), defaults: { review: DEFAULT_REVIEW_TIME, wake: config.fallbackWakeTime } }) } : {}), photos, diaryDir: options.diaryDir, running: power.running, dailyBudget: power.dailyBudget, memoryTurns: power.memoryTurns, coldHours: power.coldHours, selectedArchiveScope: power.selectedArchiveScope, workspacePermissions: permissions.snapshot, wechat: () => wechat ? { ...wechat.status(), adapter: adapter?.status() ?? null } : undefined, archiveSession, native: () => qqNative?.status() ?? null,
463
513
  compactionPreset: () => configEditor ? compactionPresetOverride(configEditor, config.agentPreset, profileDir()) : null,
464
- modules: () => ({ wechat: { available: config.wechat, enabled: power.wechatEnabled(), running: wechat !== undefined }, qq: { available: Boolean(config.qqNative?.enabled), enabled: power.qqEnabled(), running: qqNative !== undefined, ...(qqSdk.needsRestart() ? { restartRequired: true } : {}) } }), faults: () => [...faults.list(), ...(adapter?.status().error ? [`微信适配器:${adapter.status().error}`] : []), ...(memory?.faults() ?? []), ...(bridge?.faults() ?? []), ...memoryHealth()] });
514
+ modules: () => ({ wechat: { available: config.wechat, enabled: power.wechatEnabled(), running: wechat !== undefined }, qq: { available: Boolean(config.qqNative?.enabled), enabled: power.qqEnabled(), running: qqNative !== undefined, ...(qqSdk.needsRestart() ? { restartRequired: true } : {}) }, computer: { available: Boolean(config.computer), enabled: power.computerEnabled(), running: computer !== undefined }, desktop: { available: Boolean(config.computer && config.desktop), enabled: power.desktopEnabled(), running: computer?.desktopOn() === true }, foreground: { available: computer?.desktopOn() === true, enabled: computer?.foregroundOn() === true, running: computer?.foregroundOn() === true } }), computer: () => computer?.status(), faults: () => [...faults.list(), ...(adapter?.status().error ? [`微信适配器:${adapter.status().error}`] : []), ...(memory?.faults() ?? []), ...(bridge?.faults() ?? []), ...memoryHealth()] });
465
515
  const actions = {
466
516
  start: () => changeWorld(async () => {
467
517
  if (disposed || power.running())
@@ -509,6 +559,10 @@ export function apply(ctx, config) {
509
559
  return '没有配置微信模块(配置里 wechat: true)';
510
560
  if (patch.qq !== undefined && !config.qqNative?.enabled)
511
561
  return '没有配置原生 QQ 通道(配置里 qqNative.enabled)';
562
+ if (patch.computer !== undefined && !config.computer)
563
+ return '没有配置计算机模块(配置里 computer: true)';
564
+ if (patch.desktop !== undefined && !(config.computer && config.desktop))
565
+ return '没有配置她用电脑(配置里 computer: true 和 desktop: true)';
512
566
  const change = patch.qq === undefined ? qqChange : ++qqChange;
513
567
  let restartRequired = false;
514
568
  if (patch.qq === true) {
@@ -531,6 +585,24 @@ export function apply(ctx, config) {
531
585
  await stopWechat();
532
586
  if (patch.wechat === true && life)
533
587
  await startWechat(life);
588
+ if (patch.computer === false)
589
+ stopComputer();
590
+ if (patch.computer === true && life)
591
+ startComputer();
592
+ if (patch.computer === true && life && !computer)
593
+ return '她的浏览器模块没能启动,请查看健康状态';
594
+ if (patch.desktop !== undefined)
595
+ computer?.setDesktop(patch.desktop);
596
+ if (patch.desktop === true && computer && !computer.desktopOn())
597
+ return '她用电脑没能打开,请查看健康状态';
598
+ // Not kept in world.json: every time it is on, the partner has just approved it.
599
+ if (patch.foreground !== undefined) {
600
+ if (!computer)
601
+ return '她的浏览器模块没有运行';
602
+ const refused = await computer.setForeground(patch.foreground);
603
+ if (refused)
604
+ return refused;
605
+ }
534
606
  if (patch.qq === false)
535
607
  stopQqNative();
536
608
  if (patch.qq === true && life && !restartRequired)
@@ -551,6 +623,17 @@ export function apply(ctx, config) {
551
623
  return { restartRequired: true, message: 'SnowLuma SDK 已安装。请重启 DSH,QQ 会按已保存的开关启用。' };
552
624
  return undefined;
553
625
  },
626
+ showComputer: async () => {
627
+ if (!computer)
628
+ return '她的浏览器模块没有运行';
629
+ try {
630
+ await computer.show();
631
+ return undefined;
632
+ }
633
+ catch (error) {
634
+ return `打不开她的浏览器:${errorText(error)}`;
635
+ }
636
+ },
554
637
  restartWechatAdapter: async () => { if (!adapter)
555
638
  return false; try {
556
639
  await adapter.restart();
@@ -567,7 +650,16 @@ export function apply(ctx, config) {
567
650
  };
568
651
  lifeCtx.effect(() => webServer.register(consoleRoute(prefix, sources, actions, config.allowedHosts)), 'dsh-world console routes');
569
652
  lifeCtx.effect(() => {
570
- worlds.unregisterList ??= webServer.register(worldListRoute(worlds.consoles));
653
+ // Profile-wide like the list itself, so it hangs on the root: the world that registered it may be the first to go.
654
+ const addWorld = (id, companionName) => worlds.profileEdits = worlds.profileEdits.then(() => { }, () => { }).then(() => {
655
+ if ([...worlds.rooms.keys()].some(loaded => loaded.toLowerCase() === id.toLowerCase()))
656
+ throw new Error(`${id} 这个世界已经加载了`);
657
+ const patchFile = root.get('profileContext')?.patchPath;
658
+ if (!patchFile)
659
+ throw new Error('宿主没有给出配置文件的位置,请手动添加(见 README「添加第二个世界」)');
660
+ addWorldEntry(patchFile, id, companionName);
661
+ });
662
+ worlds.unregisterList ??= webServer.register(worldListRoute(worlds.consoles, addWorld));
571
663
  worlds.consoles.set(worldId, config.allowedHosts);
572
664
  return () => {
573
665
  worlds.consoles.delete(worldId);
@@ -1,6 +1,7 @@
1
1
  import { readFile, stat } from 'node:fs/promises';
2
2
  import { basename, resolve } from 'node:path';
3
3
  import { defineTool } from '@deepseek-ai/dsh-tools';
4
+ import { keepImages, sniffMediaType } from "../images.js";
4
5
  const MAX_FILE_BYTES = 20 * 1024 * 1024;
5
6
  /**
6
7
  * Every image still in front of her: the images on her session's surface, newest message first and in their own order
@@ -25,19 +26,6 @@ function visibleImages(agent) {
25
26
  }
26
27
  return found;
27
28
  }
28
- /** Only the four image types the attachment service keeps; the bytes decide, never the file name. */
29
- function mediaTypeOf(bytes) {
30
- const starts = (...head) => head.every((byte, at) => bytes[at] === byte);
31
- if (starts(0x89, 0x50, 0x4e, 0x47))
32
- return 'image/png';
33
- if (starts(0xff, 0xd8, 0xff))
34
- return 'image/jpeg';
35
- if (starts(0x47, 0x49, 0x46, 0x38))
36
- return 'image/gif';
37
- if (starts(0x52, 0x49, 0x46, 0x46) && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50)
38
- return 'image/webp';
39
- return undefined;
40
- }
41
29
  /** A picture on her computer: committed to the host's attachment store like any channel image, then read back verified. */
42
30
  async function fileImage(attachments, path, signal) {
43
31
  const info = await stat(path).catch(() => undefined);
@@ -46,10 +34,11 @@ async function fileImage(attachments, path, signal) {
46
34
  if (info.size > MAX_FILE_BYTES)
47
35
  throw new Error('图片文件最多 20MB');
48
36
  const data = new Uint8Array(await readFile(path, { signal }));
49
- const mediaType = mediaTypeOf(data);
37
+ const mediaType = sniffMediaType(data);
50
38
  if (!mediaType)
51
39
  throw new Error('只能收藏 png、jpg、webp、gif 图片');
52
- return attachments.readImage(await attachments.saveImage({ data, mediaType, name: basename(path) }), signal);
40
+ const [kept] = await keepImages(attachments, [{ data, mediaType, name: basename(path) }]);
41
+ return attachments.readImage(kept, signal);
53
42
  }
54
43
  export function photoTools(deps) {
55
44
  const output = { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] };
@@ -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 { ToolDefinition } from '@deepseek-ai/dsh-tools';
3
4
  import '../hostContract.ts';
4
5
  import type { WorldConfig } from '../types.ts';
5
6
  import { type LifeTimers } from './alarmTimer.ts';
@@ -19,10 +20,17 @@ export interface LifeDeps {
19
20
  timers?: LifeTimers;
20
21
  /** CNY her home may spend a day; undefined or absent is no limit. */
21
22
  dailyBudget?: () => number | undefined;
23
+ /** Called whenever her partner's words reach her home: a stop they pressed on her use of the computer is lifted. */
24
+ partnerSpoke?: () => void;
22
25
  /** Called once each evening look back has been handed to her home (not for alarms or greetings, and not when delivery failed). */
23
26
  afterReview?: () => void;
24
- /** Read-only facts appended after the user's alarm template is rendered. */
25
- alarmContext?: (now: Date) => string | undefined;
27
+ /** Read-only facts appended after the user's alarm template is rendered; may take a moment (her own tools run first). */
28
+ alarmContext?: (now: Date) => string | undefined | Promise<string | undefined>;
29
+ /** More tools for her home whose set changes at run time; registered again when the revision moves. */
30
+ homeTools?: {
31
+ tools(agent: Agent): readonly ToolDefinition[];
32
+ revision(): string;
33
+ };
26
34
  photos?: () => PhotoStore;
27
35
  /** Her room's workspace; the life line reaches only its sessions. Absent, every session of hers. */
28
36
  inWorkspace?: (sessionId: string) => boolean;
@@ -45,6 +53,8 @@ export declare function createLifeRuntime(deps: LifeDeps): {
45
53
  cancelAlarm: () => string;
46
54
  deliver: () => Promise<void>;
47
55
  reschedule: () => void;
56
+ tell: (text: string) => Promise<boolean>;
57
+ notice: (text: string) => void;
48
58
  snapshot: () => {
49
59
  homeSessionId: string | undefined;
50
60
  alarm: Alarm | undefined;