dsh-world 0.7.6 → 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 (102) hide show
  1. package/CHANGELOG.md +541 -479
  2. package/CONTRIBUTING.md +174 -174
  3. package/INSTALL.md +314 -314
  4. package/NOTES.md +57 -3
  5. package/README.md +332 -259
  6. package/client/index.js +1126 -991
  7. package/lib/compaction.d.ts +1 -0
  8. package/lib/compaction.js +1 -1
  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 +3 -0
  56. package/lib/ecosystem/qq-native/images.js +18 -6
  57. package/lib/ecosystem/qq-native/index.js +2 -2
  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 +1 -0
  64. package/lib/ecosystem/wechat/reading.d.ts +1 -1
  65. package/lib/ecosystem/wechat/reading.js +2 -2
  66. package/lib/history.js +2 -1
  67. package/lib/images.d.ts +30 -0
  68. package/lib/images.js +101 -0
  69. package/lib/index.d.ts +4 -0
  70. package/lib/index.js +90 -5
  71. package/lib/life/photoTools.js +4 -15
  72. package/lib/life/runtime.d.ts +12 -2
  73. package/lib/life/runtime.js +93 -12
  74. package/lib/life/toolRegistry.d.ts +9 -3
  75. package/lib/life/toolRegistry.js +46 -27
  76. package/lib/life/turnCost.d.ts +16 -0
  77. package/lib/life/turnCost.js +44 -0
  78. package/lib/life/types.d.ts +17 -0
  79. package/lib/memoryTools.d.ts +2 -1
  80. package/lib/memoryTools.js +16 -3
  81. package/lib/promptSection.d.ts +1 -0
  82. package/lib/promptSection.js +29 -42
  83. package/lib/runtime.js +1 -1
  84. package/lib/types.d.ts +4 -0
  85. package/lib/worldAdd.d.ts +9 -0
  86. package/lib/worldAdd.js +39 -0
  87. package/lib/worldPrompt.d.ts +3 -2
  88. package/lib/worldPrompt.js +3 -2
  89. package/lib/worldRoutes.d.ts +5 -2
  90. package/lib/worldRoutes.js +29 -4
  91. package/lib/worldSwitch.d.ts +8 -2
  92. package/lib/worldSwitch.js +11 -5
  93. package/package.json +132 -129
  94. package/src/ecosystem/README.md +2 -1
  95. package/src/ecosystem/computer/NOTICE.md +27 -0
  96. package/src/ecosystem/computer/README.md +244 -0
  97. package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
  98. package/src/ecosystem/wechat/windows/README.md +4 -2
  99. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  100. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  101. package/templates/world-prompt-memory.md +10 -10
  102. package/templates/world-prompt.md +65 -65
@@ -22,13 +22,19 @@ export function createWechatHome(deps) {
22
22
  handedOver.catch(() => { });
23
23
  // Checked again after every wait: the session may leave her world meanwhile.
24
24
  const agent = admit(deps.inWorld, await Promise.race([deps.home(identity), handedOver]));
25
- await Promise.race([agent.whenIdle(), handedOver]);
25
+ const content = incomingText(text, reference);
26
+ // Her home takes words at her next step, the way the partner's words in DSH reach her while she is busy; any other
27
+ // session, and a picture with no words (context, not someone speaking), waits for the turn under way to end.
28
+ // Taken in, the request is done: the adapter reads the chat one message at a time, so waiting here for her answer
29
+ // (which leaves through wechat_send anyway) held back every later message, and timed out into a blocked chat.
30
+ const midTurn = Boolean(content) && deps.midTurn?.(agent) === true;
31
+ if (!midTurn)
32
+ await Promise.race([agent.whenIdle(), handedOver]);
26
33
  admit(deps.inWorld, agent);
27
34
  if ((reference?.image || [...context, ...media].some(message => message.image || message.reference?.image)) && !deps.images)
28
35
  throw new Error('Image admission is unavailable');
29
36
  const images = await Promise.race([deps.images?.(identity, [...context, ...media], reference) ?? Promise.resolve([]), handedOver]);
30
37
  queue.alive();
31
- const content = incomingText(text, reference);
32
38
  const source = content ? { kind: 'user', identity } : { kind: 'dsh-world-channel-context', form: 'recall' };
33
39
  const words = createUserMessage({ source, content: [{ type: 'text', text: identityPrefix(identity) + (content || '【收到微信图片】') }] });
34
40
  // How to reply lives in the WeChat tools' descriptions, which are sent once; only what this message brings along comes here.
@@ -42,7 +48,7 @@ export function createWechatHome(deps) {
42
48
  : undefined;
43
49
  // What she sends to this chat herself is her answer; the rest of her turn is not this request's to wait for.
44
50
  const answered = (sent) => sent.kind === 'dsh-world-wechat-outgoing' && sent.identity.conversationId === identity.conversationId;
45
- const reply = await askTurn(deps.ctx, agent, words, { name: 'WeChat', context: instructions, cancelled, inWorld: deps.inWorld, answered });
51
+ const reply = await askTurn(deps.ctx, agent, words, { name: 'WeChat', context: instructions, cancelled, inWorld: deps.inWorld, answered, steer: midTurn, untilTaken: midTurn });
46
52
  queue.alive();
47
53
  return { id: words.id, text: reply };
48
54
  });
@@ -1,6 +1,7 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { open, realpath } from 'node:fs/promises';
3
3
  import { join, relative } from 'node:path';
4
+ import { keepImages, shownImage } from "../../images.js";
4
5
  import { channelContext, channelMedia, channelReference } from "../shared/context.js";
5
6
  import { conversationKey } from "../shared/identity.js";
6
7
  export async function admitContextImages(ctx, incomingDir, identity, context, suppliedReference) {
@@ -41,9 +42,6 @@ export async function admitContextImages(ctx, incomingDir, identity, context, su
41
42
  await handle.close();
42
43
  }
43
44
  }
44
- const refs = await attachments.saveImages(inputs);
45
- return refs.flatMap((attachment, index) => [
46
- { type: 'text', text: `下面这张微信图片的来源:${JSON.stringify({ ...images[index].source, quality: images[index].image.quality })}。thumbnail 为缩略图,cached 为微信本机缓存版本,original 为原图档。${images[index].image.firstFrame ? '动图仅查看首帧,尚未查看完整动画。' : ''}` },
47
- { type: 'image', attachment },
48
- ]);
45
+ const refs = await keepImages(attachments, inputs);
46
+ return refs.flatMap((attachment, index) => shownImage(`下面这张微信图片的来源:${JSON.stringify({ ...images[index].source, quality: images[index].image.quality })}。thumbnail 为缩略图,cached 为微信本机缓存版本,original 为原图档。${images[index].image.firstFrame ? '动图仅查看首帧,尚未查看完整动画。' : ''}`, attachment));
49
47
  }
@@ -7,6 +7,7 @@ import { type InWorld } from '../../worldScope.ts';
7
7
  export declare function createWechatModule(deps: {
8
8
  ctx: Context;
9
9
  home(identity: SpeakerIdentity): Promise<Agent>;
10
+ midTurn?(agent: Agent): boolean;
10
11
  file: string;
11
12
  worldId?: string;
12
13
  inWorld?: InWorld;
@@ -1,4 +1,4 @@
1
- import { type ContentBlock } from '@deepseek-ai/dsh-llm';
1
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm';
2
2
  import { type ToolRunContext } from '@deepseek-ai/dsh-tools';
3
3
  import { type ChannelContextMessage } from '../shared/context.ts';
4
4
  import type { SpeakerIdentity } from '../shared/identity.ts';
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomBytes } from 'node:crypto';
2
- import { createUserMessage } from '@deepseek-ai/dsh-llm';
2
+ import { imagesMessage } from "../../images.js";
3
3
  import { defineTool } from '@deepseek-ai/dsh-tools';
4
4
  import { channelContext } from "../shared/context.js";
5
5
  import { wechatTextOutput } from "./messages.js";
@@ -71,7 +71,7 @@ export function createWechatReading(deps) {
71
71
  context.signal.throwIfAborted();
72
72
  if (!blocks.some(block => block.type === 'image'))
73
73
  throw new Error('图片没能交给模型');
74
- context.deferContext(createUserMessage({ source: { kind: 'dsh-world-channel-context', form: 'recall' }, content: blocks }));
74
+ context.deferContext(imagesMessage(blocks));
75
75
  return `图片 ${entry.id}(${entry.identity.speakerName ?? entry.identity.speakerId},${entry.occurredAt})已附在下一条消息里。`;
76
76
  },
77
77
  }),
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";
@@ -119,7 +120,7 @@ export function renderHistory(turns, timeZone, names, homeOnly = false) {
119
120
  role: message.role === 'assistant' ? selfName : message.role, time: minute(message.occurredAt),
120
121
  ...(chat ? { via: chat } : {}), ...(chat && message.role === 'user' && message.conversationType === 'group' ? { who: who(message) } : {}),
121
122
  ...(message.wechatDelivery ? { sent: sentState(message.wechatDelivery) } : {}),
122
- ...(words ? { content: shortenLocalImages(words) } : {}), ...(message.status === 'interrupted' ? { interrupted: true } : {}),
123
+ ...(words ? { content: shortenArchivedImages(shortenLocalImages(words)) } : {}), ...(message.status === 'interrupted' ? { interrupted: true } : {}),
123
124
  };
124
125
  }),
125
126
  }));
@@ -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,6 +20,7 @@ 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";
24
25
  import { createElsewhere } from "./elsewhere.js";
25
26
  import { qqRoute } from "./ecosystem/qq/route.js";
@@ -36,6 +37,7 @@ import { createAdapterSupervisor } from "./ecosystem/wechat/supervisor.js";
36
37
  import { createChannelSessions } from "./ecosystem/shared/sessions.js";
37
38
  import { canonicalPath, createArchiveSelection, createWorkspaceMembership } from "./archiveSelection.js";
38
39
  import { inWorld } from "./sourceArchive.js";
40
+ import { addWorldEntry } from "./worldAdd.js";
39
41
  import { validWorldId, worldListRoute, worldPath } from "./worldRoutes.js";
40
42
  import { createWorkspacePermissions } from "./workspacePermissions.js";
41
43
  import { createPhotoStore } from "./life/photos.js";
@@ -93,6 +95,8 @@ export const Config = z.object({
93
95
  }).default({ port: 0, token: '', title: '阳台', contextMessages: 6 }),
94
96
  }).description('QQ 直接连 OneBot:不经过 qq-bridge 进程,会话与状态都由这个插件自己管'),
95
97
  wechat: z.boolean().default(false),
98
+ computer: z.boolean().default(false).description('她自己的浏览器与自己学会的工具(只在家会话)'),
99
+ desktop: z.boolean().default(false).description('她也用伙伴的电脑,在后台操作(Windows,只在家会话,要先开 computer)'),
96
100
  wechatAdapter: z.object({ python: z.string(), config: z.string() }).description('随微信模块启动和停止 Windows 适配器;写上即视为允许它发送消息'),
97
101
  toast: z.boolean().default(true),
98
102
  toastAppId: z.string().description('系统通知用的 AppUserModelID;不填就在开始菜单里找 DSH 桌面版'),
@@ -301,6 +305,42 @@ export function apply(ctx, config) {
301
305
  let bridge;
302
306
  let wechat;
303
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'); }
304
344
  let qqChannels;
305
345
  let adapter;
306
346
  let channels;
@@ -317,7 +357,7 @@ export function apply(ctx, config) {
317
357
  return;
318
358
  const groupSessions = createChannelSessions({ ctx: lifeCtx, host, store, roomDir: options.roomDir, roomTitle: roomTitle(names()), agentPreset: config.agentPreset });
319
359
  channels = groupSessions;
320
- 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() }),
321
361
  ...createWechatRecorder(store, config.worldId, archiveSession) });
322
362
  wechat.start();
323
363
  unregisterWechat = webServer.register(wechat.route);
@@ -420,8 +460,9 @@ export function apply(ctx, config) {
420
460
  return;
421
461
  const outlets = config.outlets.map(outlet => createOutlet(outlet, () => host.origin()));
422
462
  const notifier = { ...(config.toast && toastSupported() ? { toast: windowsToast(config.toastAppId || undefined) } : {}), outlets };
423
- 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())
424
- 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() });
425
466
  life = current = runtime;
426
467
  isHome = runtime.isHome;
427
468
  if (config.bridge) {
@@ -434,6 +475,7 @@ export function apply(ctx, config) {
434
475
  await startQqNative(runtime);
435
476
  if (disposed || hostAbort.signal.aborted)
436
477
  return;
478
+ startComputer();
437
479
  faults.clear('life');
438
480
  const started = runtime.start({ resumed });
439
481
  if (resumed)
@@ -451,6 +493,7 @@ export function apply(ctx, config) {
451
493
  life?.stop();
452
494
  const closingWechat = stopWechat();
453
495
  stopQqNative();
496
+ stopComputer();
454
497
  bridge = undefined;
455
498
  life = undefined;
456
499
  return lifeStop = Promise.all([lifeStop, closingWechat, homeApply.cancel()]).then(() => { });
@@ -468,7 +511,7 @@ export function apply(ctx, config) {
468
511
  // Her album lives in her room, so the console shows it whether or not the life line is running.
469
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,
470
513
  compactionPreset: () => configEditor ? compactionPresetOverride(configEditor, config.agentPreset, profileDir()) : null,
471
- 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()] });
472
515
  const actions = {
473
516
  start: () => changeWorld(async () => {
474
517
  if (disposed || power.running())
@@ -516,6 +559,10 @@ export function apply(ctx, config) {
516
559
  return '没有配置微信模块(配置里 wechat: true)';
517
560
  if (patch.qq !== undefined && !config.qqNative?.enabled)
518
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)';
519
566
  const change = patch.qq === undefined ? qqChange : ++qqChange;
520
567
  let restartRequired = false;
521
568
  if (patch.qq === true) {
@@ -538,6 +585,24 @@ export function apply(ctx, config) {
538
585
  await stopWechat();
539
586
  if (patch.wechat === true && life)
540
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
+ }
541
606
  if (patch.qq === false)
542
607
  stopQqNative();
543
608
  if (patch.qq === true && life && !restartRequired)
@@ -558,6 +623,17 @@ export function apply(ctx, config) {
558
623
  return { restartRequired: true, message: 'SnowLuma SDK 已安装。请重启 DSH,QQ 会按已保存的开关启用。' };
559
624
  return undefined;
560
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
+ },
561
637
  restartWechatAdapter: async () => { if (!adapter)
562
638
  return false; try {
563
639
  await adapter.restart();
@@ -574,7 +650,16 @@ export function apply(ctx, config) {
574
650
  };
575
651
  lifeCtx.effect(() => webServer.register(consoleRoute(prefix, sources, actions, config.allowedHosts)), 'dsh-world console routes');
576
652
  lifeCtx.effect(() => {
577
- 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));
578
663
  worlds.consoles.set(worldId, config.allowedHosts);
579
664
  return () => {
580
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;