@zhin.js/adapter-sandbox 1.0.70 → 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.
- package/CHANGELOG.md +951 -50
- package/README.md +60 -36
- package/adapters/sandbox.js +25 -0
- package/adapters/sandbox.ts +30 -0
- package/agent/skills/sandbox.md +33 -0
- package/lib/client.d.ts +27 -0
- package/lib/client.js +27 -0
- package/lib/endpoint.d.ts +42 -0
- package/lib/endpoint.js +357 -0
- package/lib/index.d.ts +3 -55
- package/lib/index.js +3 -176
- package/lib/protocol.d.ts +78 -0
- package/lib/protocol.js +301 -0
- package/lib/run-config.d.ts +10 -0
- package/lib/run-config.js +30 -0
- package/package.json +60 -24
- package/pages/RichTextEditor.js +366 -0
- package/{client → pages}/RichTextEditor.tsx +59 -13
- package/pages/SandboxChat.js +615 -0
- package/pages/SandboxChat.tsx +1186 -0
- package/pages/agentTrace.js +559 -0
- package/pages/agentTrace.test.js +235 -0
- package/pages/agentTrace.test.ts +265 -0
- package/pages/agentTrace.ts +646 -0
- package/pages/index.js +18 -0
- package/pages/index.tsx +18 -0
- package/pages/playgroundState.js +126 -0
- package/pages/playgroundState.test.js +92 -0
- package/pages/playgroundState.test.ts +105 -0
- package/pages/playgroundState.ts +172 -0
- package/pages/sandboxTransport.js +45 -0
- package/pages/sandboxTransport.ts +45 -0
- package/plugin.js +8 -0
- package/schema.json +76 -0
- package/src/client.ts +47 -0
- package/src/endpoint.ts +420 -0
- package/src/index.ts +26 -238
- package/src/protocol.ts +398 -0
- package/src/run-config.ts +41 -0
- package/LICENSE +0 -21
- package/client/Sandbox.tsx +0 -493
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/dist/index.js +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -1,238 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
MessageType,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
namespace Plugin {
|
|
28
|
-
interface Contexts {
|
|
29
|
-
router: Router;
|
|
30
|
-
web: any;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface Adapters {
|
|
35
|
-
sandbox: SandboxAdapter;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const plugin = usePlugin();
|
|
40
|
-
const logger = plugin.logger;
|
|
41
|
-
|
|
42
|
-
interface WebSocketMessage {
|
|
43
|
-
type: MessageType;
|
|
44
|
-
id: string;
|
|
45
|
-
content: MessageElement[] | string;
|
|
46
|
-
timestamp: number;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class SandboxBot extends EventEmitter implements Bot<SandboxConfig, { content: MessageElement[]; ts: number }> {
|
|
50
|
-
$connected: boolean = false;
|
|
51
|
-
|
|
52
|
-
get $id() {
|
|
53
|
-
return this.$config.name;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private logger = logger;
|
|
57
|
-
|
|
58
|
-
constructor(public adapter: SandboxAdapter, public $config: SandboxConfig) {
|
|
59
|
-
super();
|
|
60
|
-
this.$config.ws.on("message", (data) => {
|
|
61
|
-
const message = JSON.parse(data.toString()) as WebSocketMessage;
|
|
62
|
-
// 确保 content 是 MessageElement[] 格式
|
|
63
|
-
const content: MessageElement[] = typeof message.content === 'string'
|
|
64
|
-
? [{ type: 'text', data: { text: message.content } }]
|
|
65
|
-
: message.content;
|
|
66
|
-
this.logger.debug(`${this.$config.name} recv ${message.type}(${message.id}):${segment.raw(content)}`);
|
|
67
|
-
const formattedMessage = this.$formatMessage({ content: content, type: message.type, id: message.id, ts: message.timestamp });
|
|
68
|
-
this.adapter.emit("message.receive", formattedMessage);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
this.$config.ws.on("close", () => {
|
|
72
|
-
this.logger.debug(`Sandbox bot ${this.$config.name} disconnected`);
|
|
73
|
-
this.$connected = false;
|
|
74
|
-
// 从 adapter 中移除 bot
|
|
75
|
-
this.adapter.bots.delete(this.$id);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async $connect(): Promise<void> {
|
|
80
|
-
this.$connected = true;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async $disconnect(): Promise<void> {
|
|
84
|
-
this.$config.ws.close();
|
|
85
|
-
this.$connected = false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
$formatMessage({ content, type, id, ts }: { content: MessageElement[]; id: string; type: MessageType; ts: number }) {
|
|
89
|
-
// 沙箱模式:发言者即为 owner
|
|
90
|
-
if (!this.$config.owner) this.$config.owner = id;
|
|
91
|
-
const message = Message.from(
|
|
92
|
-
{ content, ts },
|
|
93
|
-
{
|
|
94
|
-
$id: `${ts}`,
|
|
95
|
-
$adapter: "sandbox" as const,
|
|
96
|
-
$bot: `${this.$config.name}`,
|
|
97
|
-
$sender: {
|
|
98
|
-
id: `${id}`,
|
|
99
|
-
name: `mock`,
|
|
100
|
-
},
|
|
101
|
-
$channel: {
|
|
102
|
-
id: `${id}`,
|
|
103
|
-
type: type,
|
|
104
|
-
},
|
|
105
|
-
$content: content,
|
|
106
|
-
$raw: segment.raw(content),
|
|
107
|
-
$timestamp: ts,
|
|
108
|
-
$recall: async () => {
|
|
109
|
-
await this.$recallMessage(message.$id);
|
|
110
|
-
},
|
|
111
|
-
$reply: async (content: SendContent, quote?: boolean | string): Promise<string> => {
|
|
112
|
-
if (!Array.isArray(content)) content = [content];
|
|
113
|
-
if (quote) content.unshift({ type: "reply", data: { id: typeof quote === "boolean" ? message.$id : quote } });
|
|
114
|
-
return await this.adapter.sendMessage({
|
|
115
|
-
...message.$channel,
|
|
116
|
-
context: "sandbox",
|
|
117
|
-
bot: `${this.$config.name}`,
|
|
118
|
-
content,
|
|
119
|
-
});
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
);
|
|
123
|
-
return message;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async $sendMessage(options: SendOptions): Promise<string> {
|
|
127
|
-
if (!this.$connected) return "";
|
|
128
|
-
this.logger.debug(`${this.$config.name} send ${options.type}(${options.id}):${segment.raw(options.content)}`);
|
|
129
|
-
options.bot = this.$config.name;
|
|
130
|
-
options.context = "sandbox";
|
|
131
|
-
this.$config.ws.send(
|
|
132
|
-
JSON.stringify({
|
|
133
|
-
...options,
|
|
134
|
-
content: options.content, // 发送消息段数组
|
|
135
|
-
timestamp: Date.now(),
|
|
136
|
-
})
|
|
137
|
-
);
|
|
138
|
-
return "";
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async $recallMessage(id: string): Promise<void> {
|
|
142
|
-
// 沙盒不支持撤回消息
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
class SandboxAdapter extends Adapter<SandboxBot> {
|
|
147
|
-
wss?: ReturnType<Router["ws"]>;
|
|
148
|
-
|
|
149
|
-
constructor(plugin: Plugin) {
|
|
150
|
-
super(plugin, "sandbox", []);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
createBot(config: SandboxConfig): SandboxBot {
|
|
154
|
-
const bot = new SandboxBot(this, config);
|
|
155
|
-
// 将 bot 添加到 bots Map 中
|
|
156
|
-
this.bots.set(bot.$id, bot);
|
|
157
|
-
return bot;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async start(): Promise<void> {
|
|
161
|
-
// start 方法会在 mounted 时被调用
|
|
162
|
-
// WebSocket server 的创建在 useContext("router") 中处理
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
async setupWebSocket(router: Router): Promise<void> {
|
|
166
|
-
if (this.wss) return; // 已经设置过了
|
|
167
|
-
// 创建 WebSocket server
|
|
168
|
-
this.wss = router.ws("/sandbox");
|
|
169
|
-
|
|
170
|
-
this.wss.on("connection", (ws: WebSocket, req) => {
|
|
171
|
-
// 为每个连接创建一个唯一的 bot 名称
|
|
172
|
-
const botName = `sandbox-${Math.random().toString(36).slice(2, 9)}`;
|
|
173
|
-
logger.debug(`New sandbox connection: ${botName} from ${req.socket.remoteAddress}`);
|
|
174
|
-
|
|
175
|
-
// 创建 bot 配置
|
|
176
|
-
const config: SandboxConfig = {
|
|
177
|
-
context: "sandbox",
|
|
178
|
-
ws,
|
|
179
|
-
name: botName,
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
// 创建并连接 bot
|
|
183
|
-
const bot = this.createBot(config);
|
|
184
|
-
bot.$connect();
|
|
185
|
-
|
|
186
|
-
// WebSocket 关闭时清理
|
|
187
|
-
ws.on("close", () => {
|
|
188
|
-
logger.debug(`Sandbox connection closed: ${botName}`);
|
|
189
|
-
this.bots.delete(bot.$id);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
ws.on("error", (error) => {
|
|
193
|
-
logger.error(`Sandbox WebSocket error for ${botName}:`, error);
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
logger.debug("Sandbox WebSocket server started at /sandbox");
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const { provide } = usePlugin();
|
|
202
|
-
|
|
203
|
-
provide({
|
|
204
|
-
name: "sandbox",
|
|
205
|
-
description: "Sandbox Adapter",
|
|
206
|
-
mounted: async (p: Plugin) => {
|
|
207
|
-
const adapter = new SandboxAdapter(p);
|
|
208
|
-
await adapter.start();
|
|
209
|
-
return adapter;
|
|
210
|
-
},
|
|
211
|
-
dispose: async (adapter: SandboxAdapter) => {
|
|
212
|
-
// 关闭所有 bot 连接
|
|
213
|
-
for (const bot of adapter.bots.values()) {
|
|
214
|
-
await bot.$disconnect();
|
|
215
|
-
}
|
|
216
|
-
// 关闭 WebSocket server
|
|
217
|
-
adapter.wss?.close();
|
|
218
|
-
await adapter.stop();
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// 使用 router 上下文创建 WebSocket server
|
|
223
|
-
plugin.useContext("router", async (router: Router) => {
|
|
224
|
-
// 等待 sandbox adapter 就绪
|
|
225
|
-
plugin.useContext("sandbox", async (adapter: SandboxAdapter) => {
|
|
226
|
-
await adapter.setupWebSocket(router);
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// 使用 web 上下文注册客户端入口
|
|
231
|
-
plugin.useContext("web", (web: any) => {
|
|
232
|
-
// 注册 Sandbox 适配器的客户端入口文件
|
|
233
|
-
const dispose = web.addEntry({
|
|
234
|
-
production: path.resolve(import.meta.dirname, "../dist/index.js"),
|
|
235
|
-
development: path.resolve(import.meta.dirname, "../client/index.tsx"),
|
|
236
|
-
});
|
|
237
|
-
return dispose;
|
|
238
|
-
});
|
|
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';
|
package/src/protocol.ts
ADDED
|
@@ -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
|
+
}
|