@zhin.js/adapter-sandbox 1.0.69 → 1.1.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 (46) hide show
  1. package/CHANGELOG.md +961 -49
  2. package/README.md +60 -36
  3. package/adapters/sandbox.js +25 -0
  4. package/adapters/sandbox.ts +30 -0
  5. package/agent/skills/sandbox.md +33 -0
  6. package/lib/client.d.ts +27 -0
  7. package/lib/client.js +27 -0
  8. package/lib/endpoint.d.ts +42 -0
  9. package/lib/endpoint.js +357 -0
  10. package/lib/index.d.ts +3 -53
  11. package/lib/index.js +3 -173
  12. package/lib/protocol.d.ts +78 -0
  13. package/lib/protocol.js +301 -0
  14. package/lib/run-config.d.ts +10 -0
  15. package/lib/run-config.js +30 -0
  16. package/package.json +60 -24
  17. package/pages/RichTextEditor.js +366 -0
  18. package/{client → pages}/RichTextEditor.tsx +59 -13
  19. package/pages/SandboxChat.js +615 -0
  20. package/pages/SandboxChat.tsx +1186 -0
  21. package/pages/agentTrace.js +559 -0
  22. package/pages/agentTrace.test.js +235 -0
  23. package/pages/agentTrace.test.ts +265 -0
  24. package/pages/agentTrace.ts +646 -0
  25. package/pages/index.js +18 -0
  26. package/pages/index.tsx +18 -0
  27. package/pages/playgroundState.js +126 -0
  28. package/pages/playgroundState.test.js +92 -0
  29. package/pages/playgroundState.test.ts +105 -0
  30. package/pages/playgroundState.ts +172 -0
  31. package/pages/sandboxTransport.js +45 -0
  32. package/pages/sandboxTransport.ts +45 -0
  33. package/plugin.js +8 -0
  34. package/schema.json +76 -0
  35. package/src/client.ts +47 -0
  36. package/src/endpoint.ts +420 -0
  37. package/src/index.ts +26 -234
  38. package/src/protocol.ts +398 -0
  39. package/src/run-config.ts +41 -0
  40. package/LICENSE +0 -21
  41. package/client/Sandbox.tsx +0 -493
  42. package/client/index.tsx +0 -11
  43. package/client/tsconfig.json +0 -7
  44. package/dist/index.js +0 -1
  45. package/lib/index.d.ts.map +0 -1
  46. package/lib/index.js.map +0 -1
package/src/index.ts CHANGED
@@ -1,234 +1,26 @@
1
- import { EventEmitter } from "events";
2
- import {
3
- Bot,
4
- Adapter,
5
- usePlugin,
6
- Message,
7
- SendOptions,
8
- segment,
9
- SendContent,
10
- MessageType,
11
- MessageElement,
12
- Plugin,
13
- } from "zhin.js";
14
- import type { WebSocket } from "ws";
15
- import { Router } from "@zhin.js/http";
16
- import path from "path";
17
-
18
- export interface SandboxConfig {
19
- context: "sandbox";
20
- ws: WebSocket;
21
- name: string;
22
- }
23
-
24
- declare module "zhin.js" {
25
- namespace Plugin {
26
- interface Contexts {
27
- router: Router;
28
- web: any;
29
- }
30
- }
31
-
32
- interface Adapters {
33
- sandbox: SandboxAdapter;
34
- }
35
- }
36
-
37
- const plugin = usePlugin();
38
- const logger = plugin.logger;
39
-
40
- interface WebSocketMessage {
41
- type: MessageType;
42
- id: string;
43
- content: MessageElement[] | string;
44
- timestamp: number;
45
- }
46
-
47
- export class SandboxBot extends EventEmitter implements Bot<SandboxConfig, { content: MessageElement[]; ts: number }> {
48
- $connected: boolean = false;
49
-
50
- get $id() {
51
- return this.$config.name;
52
- }
53
-
54
- private logger = logger;
55
-
56
- constructor(public adapter: SandboxAdapter, public $config: SandboxConfig) {
57
- super();
58
- this.$config.ws.on("message", (data) => {
59
- const message = JSON.parse(data.toString()) as WebSocketMessage;
60
- // 确保 content 是 MessageElement[] 格式
61
- const content: MessageElement[] = typeof message.content === 'string'
62
- ? [{ type: 'text', data: { text: message.content } }]
63
- : message.content;
64
- this.logger.debug(`${this.$config.name} recv ${message.type}(${message.id}):${segment.raw(content)}`);
65
- const formattedMessage = this.$formatMessage({ content: content, type: message.type, id: message.id, ts: message.timestamp });
66
- this.adapter.emit("message.receive", formattedMessage);
67
- });
68
-
69
- this.$config.ws.on("close", () => {
70
- this.logger.debug(`Sandbox bot ${this.$config.name} disconnected`);
71
- this.$connected = false;
72
- // 从 adapter 中移除 bot
73
- this.adapter.bots.delete(this.$id);
74
- });
75
- }
76
-
77
- async $connect(): Promise<void> {
78
- this.$connected = true;
79
- }
80
-
81
- async $disconnect(): Promise<void> {
82
- this.$config.ws.close();
83
- this.$connected = false;
84
- }
85
-
86
- $formatMessage({ content, type, id, ts }: { content: MessageElement[]; id: string; type: MessageType; ts: number }) {
87
- const message = Message.from(
88
- { content, ts },
89
- {
90
- $id: `${ts}`,
91
- $adapter: "sandbox" as const,
92
- $bot: `${this.$config.name}`,
93
- $sender: {
94
- id: `${id}`,
95
- name: `mock`,
96
- },
97
- $channel: {
98
- id: `${id}`,
99
- type: type,
100
- },
101
- $content: content,
102
- $raw: segment.raw(content),
103
- $timestamp: ts,
104
- $recall: async () => {
105
- await this.$recallMessage(message.$id);
106
- },
107
- $reply: async (content: SendContent, quote?: boolean | string): Promise<string> => {
108
- if (!Array.isArray(content)) content = [content];
109
- if (quote) content.unshift({ type: "reply", data: { id: typeof quote === "boolean" ? message.$id : quote } });
110
- return await this.adapter.sendMessage({
111
- ...message.$channel,
112
- context: "sandbox",
113
- bot: `${this.$config.name}`,
114
- content,
115
- });
116
- },
117
- }
118
- );
119
- return message;
120
- }
121
-
122
- async $sendMessage(options: SendOptions): Promise<string> {
123
- if (!this.$connected) return "";
124
- this.logger.debug(`${this.$config.name} send ${options.type}(${options.id}):${segment.raw(options.content)}`);
125
- options.bot = this.$config.name;
126
- options.context = "sandbox";
127
- this.$config.ws.send(
128
- JSON.stringify({
129
- ...options,
130
- content: options.content, // 发送消息段数组
131
- timestamp: Date.now(),
132
- })
133
- );
134
- return "";
135
- }
136
-
137
- async $recallMessage(id: string): Promise<void> {
138
- // 沙盒不支持撤回消息
139
- }
140
- }
141
-
142
- class SandboxAdapter extends Adapter<SandboxBot> {
143
- wss?: ReturnType<Router["ws"]>;
144
-
145
- constructor(plugin: Plugin) {
146
- super(plugin, "sandbox", []);
147
- }
148
-
149
- createBot(config: SandboxConfig): SandboxBot {
150
- const bot = new SandboxBot(this, config);
151
- // 将 bot 添加到 bots Map 中
152
- this.bots.set(bot.$id, bot);
153
- return bot;
154
- }
155
-
156
- async start(): Promise<void> {
157
- // start 方法会在 mounted 时被调用
158
- // WebSocket server 的创建在 useContext("router") 中处理
159
- }
160
-
161
- async setupWebSocket(router: Router): Promise<void> {
162
- if (this.wss) return; // 已经设置过了
163
- // 创建 WebSocket server
164
- this.wss = router.ws("/sandbox");
165
-
166
- this.wss.on("connection", (ws: WebSocket, req) => {
167
- // 为每个连接创建一个唯一的 bot 名称
168
- const botName = `sandbox-${Math.random().toString(36).slice(2, 9)}`;
169
- logger.debug(`New sandbox connection: ${botName} from ${req.socket.remoteAddress}`);
170
-
171
- // 创建 bot 配置
172
- const config: SandboxConfig = {
173
- context: "sandbox",
174
- ws,
175
- name: botName,
176
- };
177
-
178
- // 创建并连接 bot
179
- const bot = this.createBot(config);
180
- bot.$connect();
181
-
182
- // WebSocket 关闭时清理
183
- ws.on("close", () => {
184
- logger.debug(`Sandbox connection closed: ${botName}`);
185
- this.bots.delete(bot.$id);
186
- });
187
-
188
- ws.on("error", (error) => {
189
- logger.error(`Sandbox WebSocket error for ${botName}:`, error);
190
- });
191
- });
192
-
193
- logger.debug("Sandbox WebSocket server started at /sandbox");
194
- }
195
- }
196
-
197
- const { provide } = usePlugin();
198
-
199
- provide({
200
- name: "sandbox",
201
- description: "Sandbox Adapter",
202
- mounted: async (p: Plugin) => {
203
- const adapter = new SandboxAdapter(p);
204
- await adapter.start();
205
- return adapter;
206
- },
207
- dispose: async (adapter: SandboxAdapter) => {
208
- // 关闭所有 bot 连接
209
- for (const bot of adapter.bots.values()) {
210
- await bot.$disconnect();
211
- }
212
- // 关闭 WebSocket server
213
- adapter.wss?.close();
214
- await adapter.stop();
215
- },
216
- });
217
-
218
- // 使用 router 上下文创建 WebSocket server
219
- plugin.useContext("router", async (router: Router) => {
220
- // 等待 sandbox adapter 就绪
221
- plugin.useContext("sandbox", async (adapter: SandboxAdapter) => {
222
- await adapter.setupWebSocket(router);
223
- });
224
- });
225
-
226
- // 使用 web 上下文注册客户端入口
227
- plugin.useContext("web", (web: any) => {
228
- // 注册 Sandbox 适配器的客户端入口文件
229
- const dispose = web.addEntry({
230
- production: path.resolve(import.meta.dirname, "../dist/index.js"),
231
- development: path.resolve(import.meta.dirname, "../client/index.tsx"),
232
- });
233
- return dispose;
234
- });
1
+ export {
2
+ bindSandboxWsSocket,
3
+ formatSandboxOutbound,
4
+ normalizeSandboxOutboundSegments,
5
+ parseSandboxWsPayload,
6
+ resolveSandboxEndpoint,
7
+ sandboxInboundConversation,
8
+ whenWsOpen,
9
+ type MessageElement,
10
+ type MessageType,
11
+ type ResolvedSandboxBot,
12
+ type SandboxAdapterConfig,
13
+ type SandboxWsSocket,
14
+ } from './protocol.js';
15
+
16
+ export {
17
+ SandboxClient,
18
+ sandboxClient,
19
+ type SandboxClientEventMap,
20
+ type SandboxClientConnection,
21
+ } from './client.js';
22
+
23
+ export {
24
+ SandboxWsEndpoint,
25
+ type SandboxEndpointOptions,
26
+ } from './endpoint.js';
@@ -0,0 +1,398 @@
1
+ /** Sandbox WebSocket wire protocol helpers (no legacy Adapter/Endpoint). */
2
+
3
+ import { readFileSync } from 'node:fs';
4
+ import { isMediaRef } from '@zhin.js/core';
5
+ import type { ConversationKind, ConversationRef } from '@zhin.js/im-contract';
6
+ import { formatCompact, getLogger } from '@zhin.js/logger';
7
+ import {
8
+ normalizeSandboxAgentRunConfig,
9
+ type SandboxAgentRunConfig,
10
+ } from './run-config.js';
11
+
12
+ const logger = getLogger('sandbox');
13
+
14
+ export type MessageType = 'private' | 'group' | 'guild' | 'direct' | 'channel';
15
+
16
+ export interface MessageElement {
17
+ readonly type: string;
18
+ readonly data?: Record<string, unknown>;
19
+ }
20
+
21
+ export interface SandboxWsSocket {
22
+ send(data: string): void;
23
+ close(code?: number, reason?: string): void;
24
+ on?(event: 'message' | 'close' | 'error', listener: (...args: unknown[]) => void): void;
25
+ off?(
26
+ event: 'message' | 'close' | 'error',
27
+ listener: (...args: unknown[]) => void,
28
+ ): void;
29
+ addEventListener?(
30
+ type: 'message' | 'close' | 'error',
31
+ listener: (ev: Event | MessageEvent | CloseEvent) => void,
32
+ ): void;
33
+ removeEventListener?(
34
+ type: 'message' | 'close' | 'error',
35
+ listener: (ev: Event | MessageEvent | CloseEvent) => void,
36
+ ): void;
37
+ }
38
+
39
+ export type ResolvedSandboxBot = {
40
+ readonly context: 'sandbox';
41
+ readonly id: string;
42
+ readonly owner: string;
43
+ readonly randomNamePerConnection: boolean;
44
+ };
45
+
46
+ export interface SandboxAdapterConfig {
47
+ /** Runtime expands `endpoints[i]` onto the top level — prefer these. */
48
+ readonly context?: string;
49
+ readonly id?: string;
50
+ readonly owner?: string;
51
+ /** Legacy shape: endpoint entries nested under `endpoints[]`. */
52
+ readonly endpoints?: ReadonlyArray<{
53
+ readonly context?: string;
54
+ readonly id?: string;
55
+ readonly owner?: string;
56
+ }>;
57
+ }
58
+
59
+ export function resolveSandboxEndpoint(
60
+ appConfig: SandboxAdapterConfig,
61
+ ): ResolvedSandboxBot {
62
+ const entry = appConfig.endpoints?.find((item) => item.context === 'sandbox');
63
+ const fixedName = typeof appConfig.id === 'string' && appConfig.id
64
+ ? appConfig.id
65
+ : typeof entry?.id === 'string' && entry.id
66
+ ? entry.id
67
+ : undefined;
68
+ const id = fixedName || process.env.SANDBOX_BOT_NAME || 'sandbox-bot';
69
+ const owner = (typeof appConfig.owner === 'string' && appConfig.owner)
70
+ || (typeof entry?.owner === 'string' && entry.owner)
71
+ || process.env.SANDBOX_BOT_OWNER
72
+ || 'sandbox-user';
73
+ return {
74
+ context: 'sandbox',
75
+ id,
76
+ owner,
77
+ // The endpoint id participates in the Agent session key. Keep it stable
78
+ // across browser reconnects and Host restarts so a persisted playground
79
+ // session resumes the same Agent context.
80
+ randomNamePerConnection: false,
81
+ };
82
+ }
83
+
84
+ export function bindSandboxWsSocket(
85
+ ws: SandboxWsSocket,
86
+ handlers: {
87
+ onMessage: (raw: string) => void;
88
+ onClose: () => void;
89
+ onError?: (err: unknown) => void;
90
+ },
91
+ ): () => void {
92
+ if (typeof ws.on === 'function') {
93
+ const onMessage = (...args: unknown[]) => {
94
+ const data = args[0];
95
+ const raw = typeof data === 'string'
96
+ ? data
97
+ : data instanceof ArrayBuffer
98
+ ? new TextDecoder().decode(data)
99
+ : Buffer.isBuffer(data)
100
+ ? data.toString()
101
+ : String(data ?? '');
102
+ handlers.onMessage(raw);
103
+ };
104
+ ws.on('message', onMessage);
105
+ ws.on('close', handlers.onClose);
106
+ if (handlers.onError) ws.on('error', handlers.onError);
107
+ return () => {
108
+ ws.off?.('message', onMessage);
109
+ ws.off?.('close', handlers.onClose);
110
+ if (handlers.onError) ws.off?.('error', handlers.onError);
111
+ };
112
+ }
113
+ const onMessage = (ev: Event) => {
114
+ const data = (ev as MessageEvent).data;
115
+ handlers.onMessage(typeof data === 'string' ? data : '');
116
+ };
117
+ const onClose = () => handlers.onClose();
118
+ const onError = handlers.onError
119
+ ? () => handlers.onError?.(new Error('WebSocket error'))
120
+ : undefined;
121
+ ws.addEventListener!('message', onMessage);
122
+ ws.addEventListener!('close', onClose);
123
+ if (onError) ws.addEventListener!('error', onError);
124
+ return () => {
125
+ ws.removeEventListener!('message', onMessage);
126
+ ws.removeEventListener!('close', onClose);
127
+ if (onError) ws.removeEventListener!('error', onError);
128
+ };
129
+ }
130
+
131
+ export function parseSandboxWsPayload(raw: string): {
132
+ type: MessageType;
133
+ id: string;
134
+ messageId?: string;
135
+ content: MessageElement[];
136
+ timestamp: number;
137
+ text: string;
138
+ action?: { id: string; payload: string };
139
+ agentRun?: SandboxAgentRunConfig;
140
+ } {
141
+ let payload: {
142
+ type?: MessageType;
143
+ id?: string;
144
+ content?: MessageElement[] | string;
145
+ text?: string;
146
+ timestamp?: number;
147
+ messageId?: unknown;
148
+ agentRun?: unknown;
149
+ };
150
+ try {
151
+ payload = JSON.parse(raw) as typeof payload;
152
+ } catch {
153
+ payload = { text: raw };
154
+ }
155
+ const type = payload.type ?? 'private';
156
+ const id = payload.id ?? 'sandbox-user';
157
+ const content: MessageElement[] = typeof payload.content === 'string'
158
+ ? [{ type: 'text', data: { text: payload.content } }]
159
+ : Array.isArray(payload.content)
160
+ ? payload.content
161
+ : [{ type: 'text', data: { text: payload.text ?? raw } }];
162
+
163
+ const actionSegment = content.find((segment) => segment.type === 'action');
164
+ let action: { id: string; payload: string } | undefined;
165
+ if (actionSegment?.data) {
166
+ const actionPayload = typeof actionSegment.data.payload === 'string'
167
+ ? actionSegment.data.payload
168
+ : typeof actionSegment.data.id === 'string'
169
+ ? actionSegment.data.id
170
+ : '';
171
+ const actionId = typeof actionSegment.data.id === 'string'
172
+ ? actionSegment.data.id
173
+ : actionPayload;
174
+ if (actionId || actionPayload) {
175
+ action = { id: actionId || actionPayload, payload: actionPayload || actionId };
176
+ }
177
+ }
178
+
179
+ let text = content
180
+ .flatMap((segment) => (segment.type === 'text' && typeof segment.data?.text === 'string'
181
+ ? [segment.data.text]
182
+ : []))
183
+ .join('\n');
184
+ if (!text.trim()) {
185
+ text = (typeof payload.text === 'string' && payload.text.trim())
186
+ ? payload.text
187
+ : action?.payload ?? raw;
188
+ }
189
+ const agentRun = normalizeSandboxAgentRunConfig(payload.agentRun);
190
+ const rawMessageId = typeof payload.messageId === 'string' ? payload.messageId.trim() : '';
191
+ const messageId = /^[A-Za-z0-9._:-]{1,160}$/u.test(rawMessageId) ? rawMessageId : undefined;
192
+ return {
193
+ type,
194
+ id,
195
+ ...(messageId ? { messageId } : {}),
196
+ content,
197
+ timestamp: payload.timestamp ?? Date.now(),
198
+ text,
199
+ action,
200
+ ...(agentRun ? { agentRun } : {}),
201
+ };
202
+ }
203
+
204
+ /**
205
+ * 入站归一化 → ConversationRef。sandbox 无平台社交图谱:
206
+ * `private`/`group`/`channel` 直映射;`direct`(私聊)归 'private';
207
+ * `guild`(频道容器语义)归 'channel'。无 guild/temp 容器信息,不产生 parent。
208
+ */
209
+ export function sandboxInboundConversation(
210
+ endpointKey: string,
211
+ msg: { readonly type: MessageType; readonly id: string },
212
+ ): ConversationRef {
213
+ const kind: ConversationKind = msg.type === 'direct'
214
+ ? 'private'
215
+ : msg.type === 'guild'
216
+ ? 'channel'
217
+ : msg.type;
218
+ return {
219
+ endpoint: { id: endpointKey, adapter: endpointKey.split('\0')[0] ?? endpointKey },
220
+ kind,
221
+ id: msg.id,
222
+ };
223
+ }
224
+
225
+ export type SandboxOutboundChannel = {
226
+ readonly type?: string;
227
+ readonly id?: string;
228
+ readonly bot?: string;
229
+ readonly endpoint?: string;
230
+ readonly messageId?: string;
231
+ };
232
+
233
+ const MEDIA_SEGMENT_TYPES = new Set(['image', 'audio', 'video', 'file']);
234
+
235
+ /**
236
+ * base64 内联值:Console UI 的 resolveMediaSrc 只识别 data: / base64://
237
+ * 前缀;裸 base64 补前缀(有 mime_type 时拼成可直转 data: URL 的形状)。
238
+ */
239
+ function toInlineBase64Value(value: string, mimeType?: string): string {
240
+ const trimmed = value.trim();
241
+ if (trimmed.startsWith('base64://') || trimmed.startsWith('data:')) return trimmed;
242
+ return mimeType
243
+ ? `base64://${mimeType};base64,${trimmed}`
244
+ : `base64://${trimmed}`;
245
+ }
246
+
247
+ /**
248
+ * 出站媒体段归一(canonical MediaRef 唯一来源,不读 legacy url/file/base64/src):
249
+ * - kind=url → 浏览器直连 URL,原样透传;
250
+ * - kind=base64 → 内联直发(补 base64:// 前缀供 Console UI 解析);
251
+ * - kind=path → 读盘物化为 base64 内联(sandbox 无平台上传通道);
252
+ * - kind=file → sandbox 无不透明引用通道,丢弃。
253
+ * 无 canonical `data.media` 的媒体段一律 warn + 丢弃。
254
+ */
255
+ function normalizeMediaSegment(segment: MessageElement): MessageElement | null {
256
+ const data = segment.data ?? {};
257
+ const media = data.media;
258
+ if (!isMediaRef(media)) {
259
+ logger.warn(formatCompact({
260
+ op: 'sandbox_outbound_media_dropped',
261
+ type: segment.type,
262
+ reason: 'missing_media_ref',
263
+ }));
264
+ return null;
265
+ }
266
+ if (media.kind === 'url') return { type: segment.type, data };
267
+ if (media.kind === 'base64') {
268
+ return {
269
+ type: segment.type,
270
+ data: {
271
+ ...data,
272
+ media: { ...media, value: toInlineBase64Value(media.value, media.mime_type) },
273
+ },
274
+ };
275
+ }
276
+ if (media.kind === 'path') {
277
+ try {
278
+ const base64 = readFileSync(media.value).toString('base64');
279
+ return {
280
+ type: segment.type,
281
+ data: {
282
+ ...data,
283
+ media: {
284
+ ...media,
285
+ kind: 'base64',
286
+ value: toInlineBase64Value(base64, media.mime_type),
287
+ },
288
+ },
289
+ };
290
+ } catch (err) {
291
+ logger.warn(formatCompact({
292
+ op: 'sandbox_outbound_media_dropped',
293
+ type: segment.type,
294
+ reason: 'path_read_failed',
295
+ error: err instanceof Error ? err.message : String(err),
296
+ }));
297
+ return null;
298
+ }
299
+ }
300
+ logger.warn(formatCompact({
301
+ op: 'sandbox_outbound_media_dropped',
302
+ type: segment.type,
303
+ reason: 'unsupported_media_kind',
304
+ }));
305
+ return null;
306
+ }
307
+
308
+ /** 出站段数组归一:媒体段走 MediaRef-only 归一,其余段原样透传。 */
309
+ export function normalizeSandboxOutboundSegments(segments: readonly unknown[]): unknown[] {
310
+ const out: unknown[] = [];
311
+ for (const item of segments) {
312
+ if (
313
+ item
314
+ && typeof item === 'object'
315
+ && !Array.isArray(item)
316
+ && MEDIA_SEGMENT_TYPES.has(String((item as MessageElement).type))
317
+ ) {
318
+ const normalized = normalizeMediaSegment(item as MessageElement);
319
+ if (normalized) out.push(normalized);
320
+ continue;
321
+ }
322
+ out.push(item);
323
+ }
324
+ return out;
325
+ }
326
+
327
+ /**
328
+ * Wire-encode an already-rendered outbound payload.
329
+ * Stamps `channel` so Console SandboxChat can filter by type+id (otherwise
330
+ * replies look like they disappeared).
331
+ */
332
+ export function formatSandboxOutbound(
333
+ payload: unknown,
334
+ channel: SandboxOutboundChannel = {},
335
+ ): string {
336
+ const stamp: Record<string, unknown> = {};
337
+ if (channel.type) stamp.type = channel.type;
338
+ if (channel.id) stamp.id = channel.id;
339
+ if (channel.bot) stamp.bot = channel.bot;
340
+ if (channel.endpoint) stamp.endpoint = channel.endpoint;
341
+ if (channel.messageId) stamp.messageId = channel.messageId;
342
+
343
+ if (typeof payload === 'string') {
344
+ return JSON.stringify({
345
+ ...stamp,
346
+ content: [{ type: 'text', data: { text: payload } }],
347
+ timestamp: Date.now(),
348
+ });
349
+ }
350
+ if (Array.isArray(payload)) {
351
+ return JSON.stringify({
352
+ ...stamp,
353
+ content: normalizeSandboxOutboundSegments(payload),
354
+ timestamp: Date.now(),
355
+ });
356
+ }
357
+ // Already a wire envelope ({ content, type, … }) — pass through so the
358
+ // Console UI can read `content` / `type` without an extra nesting layer.
359
+ // Bare segment objects ({ type: 'text', data: … }) still need wrapping.
360
+ if (
361
+ payload
362
+ && typeof payload === 'object'
363
+ && !Array.isArray(payload)
364
+ && (
365
+ 'content' in (payload as object)
366
+ || 'type' in (payload as object) && 'timestamp' in (payload as object)
367
+ )
368
+ ) {
369
+ const envelope = payload as Record<string, unknown>;
370
+ return JSON.stringify({
371
+ ...stamp,
372
+ ...envelope,
373
+ ...(Array.isArray(envelope.content)
374
+ ? { content: normalizeSandboxOutboundSegments(envelope.content) }
375
+ : {}),
376
+ type: envelope.type ?? stamp.type,
377
+ id: envelope.id ?? stamp.id,
378
+ timestamp: typeof envelope.timestamp === 'number' ? envelope.timestamp : Date.now(),
379
+ });
380
+ }
381
+ return JSON.stringify({ ...stamp, content: payload, timestamp: Date.now() });
382
+ }
383
+
384
+ /** WebSocket.OPEN 常量值;Node <22 无全局 WebSocket,不能用 WebSocket.OPEN。 */
385
+ const WS_OPEN = 1;
386
+
387
+ export function whenWsOpen(ws: SandboxWsSocket, fn: () => void): void {
388
+ const std = ws as WebSocket;
389
+ if (typeof std.readyState === 'number') {
390
+ if (std.readyState === WS_OPEN) {
391
+ fn();
392
+ return;
393
+ }
394
+ std.addEventListener('open', fn, { once: true });
395
+ return;
396
+ }
397
+ fn();
398
+ }
@@ -0,0 +1,41 @@
1
+ export type SandboxSafetyMode = 'read-only' | 'workspace-write' | 'danger-full-access';
2
+ export type SandboxApprovalMode = 'ask' | 'deny' | 'allow';
3
+
4
+ export interface SandboxAgentRunConfig {
5
+ readonly workingDirectory: string;
6
+ readonly safetyMode: SandboxSafetyMode;
7
+ readonly approvalMode: SandboxApprovalMode;
8
+ readonly networkAccess: boolean;
9
+ }
10
+
11
+ export const DEFAULT_SANDBOX_AGENT_RUN_CONFIG: SandboxAgentRunConfig = Object.freeze({
12
+ workingDirectory: '',
13
+ safetyMode: 'workspace-write',
14
+ approvalMode: 'ask',
15
+ networkAccess: false,
16
+ });
17
+
18
+ const SAFETY_MODES = new Set<SandboxSafetyMode>(['read-only', 'workspace-write', 'danger-full-access']);
19
+ const APPROVAL_MODES = new Set<SandboxApprovalMode>(['ask', 'deny', 'allow']);
20
+
21
+ export function normalizeSandboxAgentRunConfig(value: unknown): SandboxAgentRunConfig | undefined {
22
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return undefined;
23
+ const input = value as Record<string, unknown>;
24
+ const workingDirectory = typeof input.workingDirectory === 'string'
25
+ ? input.workingDirectory.trim().slice(0, 4096)
26
+ : '';
27
+ const safetyMode = SAFETY_MODES.has(input.safetyMode as SandboxSafetyMode)
28
+ ? input.safetyMode as SandboxSafetyMode
29
+ : DEFAULT_SANDBOX_AGENT_RUN_CONFIG.safetyMode;
30
+ const approvalMode = APPROVAL_MODES.has(input.approvalMode as SandboxApprovalMode)
31
+ ? input.approvalMode as SandboxApprovalMode
32
+ : DEFAULT_SANDBOX_AGENT_RUN_CONFIG.approvalMode;
33
+ return Object.freeze({
34
+ workingDirectory,
35
+ safetyMode,
36
+ approvalMode,
37
+ // Full host access cannot be combined with a portable network namespace.
38
+ // Keep the contract honest: danger mode includes network authority.
39
+ networkAccess: safetyMode === 'danger-full-access' || input.networkAccess === true,
40
+ });
41
+ }