@zhin.js/adapter-sandbox 1.1.22 → 1.1.24
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 +78 -0
- package/README.md +10 -1
- package/adapters/sandbox/index.js +16 -6
- package/adapters/sandbox/index.ts +16 -6
- package/lib/adapter.js +1 -1
- package/lib/endpoint.js +44 -0
- package/lib/protocol.d.ts +6 -2
- package/lib/protocol.js +5 -0
- package/package.json +18 -18
- package/pages/index/RichTextEditor.js +1 -15
- package/pages/index/RichTextEditor.tsx +4 -15
- package/pages/index/SandboxChat.js +5 -2
- package/pages/index/SandboxChat.tsx +7 -1
- package/pages/index/playgroundState.js +21 -2
- package/pages/index/playgroundState.ts +23 -2
- package/pages/index/styles.js +200 -0
- package/pages/index/styles.ts +199 -0
- package/pages/index/useSandboxHistory.js +41 -8
- package/pages/index/useSandboxHistory.ts +37 -8
- package/plugin.js +1 -0
- package/schema.json +126 -1
- package/src/adapter.ts +1 -1
- package/src/endpoint.ts +41 -1
- package/src/protocol.ts +10 -1
package/schema.json
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
"type": "object",
|
|
4
4
|
"additionalProperties": false,
|
|
5
5
|
"properties": {
|
|
6
|
+
"streaming": {
|
|
7
|
+
"type": "boolean",
|
|
8
|
+
"default": true,
|
|
9
|
+
"description": "Publish supported replies while generating. Set false to send only complete replies."
|
|
10
|
+
},
|
|
11
|
+
"streamingIntervalMs": {
|
|
12
|
+
"type": "number",
|
|
13
|
+
"minimum": 0,
|
|
14
|
+
"default": 500,
|
|
15
|
+
"description": "Minimum delay in milliseconds between reply updates; the first update is immediate."
|
|
16
|
+
},
|
|
6
17
|
"master": {
|
|
7
18
|
"type": [
|
|
8
19
|
"string",
|
|
@@ -70,12 +81,42 @@
|
|
|
70
81
|
"type": "string",
|
|
71
82
|
"description": "Sandbox owner user ID"
|
|
72
83
|
},
|
|
84
|
+
"streaming": {
|
|
85
|
+
"type": "boolean",
|
|
86
|
+
"description": "Publish supported replies while generating. Set false to send only complete replies."
|
|
87
|
+
},
|
|
88
|
+
"streamingIntervalMs": {
|
|
89
|
+
"type": "number",
|
|
90
|
+
"minimum": 0,
|
|
91
|
+
"description": "Minimum delay in milliseconds between reply updates; the first update is immediate."
|
|
92
|
+
},
|
|
73
93
|
"commandPrefix": {
|
|
74
94
|
"type": "string"
|
|
75
95
|
},
|
|
76
96
|
"id": {
|
|
77
97
|
"type": "string",
|
|
78
98
|
"description": "Sandbox bot name"
|
|
99
|
+
},
|
|
100
|
+
"nickname": {
|
|
101
|
+
"type": [
|
|
102
|
+
"string",
|
|
103
|
+
"null"
|
|
104
|
+
],
|
|
105
|
+
"description": "账号昵称兜底",
|
|
106
|
+
"x-i18n": {
|
|
107
|
+
"en": {
|
|
108
|
+
"description": "Fallback account nickname"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"profile": {
|
|
113
|
+
"$ref": "#/$defs/selfProfileFallback",
|
|
114
|
+
"description": "账号资料兜底",
|
|
115
|
+
"x-i18n": {
|
|
116
|
+
"en": {
|
|
117
|
+
"description": "Fallback account profile"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
79
120
|
}
|
|
80
121
|
},
|
|
81
122
|
"required": [
|
|
@@ -97,9 +138,93 @@
|
|
|
97
138
|
"description": "Command prefix. The default empty string allows unprefixed commands; '/' requires a leading slash. Each endpoints[i] can override it."
|
|
98
139
|
}
|
|
99
140
|
}
|
|
141
|
+
},
|
|
142
|
+
"nickname": {
|
|
143
|
+
"type": [
|
|
144
|
+
"string",
|
|
145
|
+
"null"
|
|
146
|
+
],
|
|
147
|
+
"description": "账号昵称兜底",
|
|
148
|
+
"x-i18n": {
|
|
149
|
+
"en": {
|
|
150
|
+
"description": "Fallback account nickname"
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
"profile": {
|
|
155
|
+
"$ref": "#/$defs/selfProfileFallback",
|
|
156
|
+
"description": "账号资料兜底",
|
|
157
|
+
"x-i18n": {
|
|
158
|
+
"en": {
|
|
159
|
+
"description": "Fallback account profile"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
100
162
|
}
|
|
101
163
|
},
|
|
102
164
|
"required": [
|
|
103
165
|
"endpoints"
|
|
104
|
-
]
|
|
166
|
+
],
|
|
167
|
+
"$defs": {
|
|
168
|
+
"selfProfileFallback": {
|
|
169
|
+
"type": "object",
|
|
170
|
+
"additionalProperties": false,
|
|
171
|
+
"properties": {
|
|
172
|
+
"gender": {
|
|
173
|
+
"type": [
|
|
174
|
+
"string",
|
|
175
|
+
"null"
|
|
176
|
+
],
|
|
177
|
+
"enum": [
|
|
178
|
+
"male",
|
|
179
|
+
"female",
|
|
180
|
+
"other",
|
|
181
|
+
"unknown",
|
|
182
|
+
null
|
|
183
|
+
]
|
|
184
|
+
},
|
|
185
|
+
"age": {
|
|
186
|
+
"type": [
|
|
187
|
+
"integer",
|
|
188
|
+
"null"
|
|
189
|
+
],
|
|
190
|
+
"minimum": 0
|
|
191
|
+
},
|
|
192
|
+
"birthday": {
|
|
193
|
+
"type": [
|
|
194
|
+
"string",
|
|
195
|
+
"null"
|
|
196
|
+
],
|
|
197
|
+
"pattern": "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$"
|
|
198
|
+
},
|
|
199
|
+
"signature": {
|
|
200
|
+
"type": [
|
|
201
|
+
"string",
|
|
202
|
+
"null"
|
|
203
|
+
]
|
|
204
|
+
},
|
|
205
|
+
"avatar": {
|
|
206
|
+
"type": [
|
|
207
|
+
"string",
|
|
208
|
+
"null"
|
|
209
|
+
],
|
|
210
|
+
"pattern": "^(https?://[^\\s/?#]+(?:[/?#].*)?|)$"
|
|
211
|
+
},
|
|
212
|
+
"interests": {
|
|
213
|
+
"type": [
|
|
214
|
+
"array",
|
|
215
|
+
"null"
|
|
216
|
+
],
|
|
217
|
+
"items": {
|
|
218
|
+
"type": "string"
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
"bio": {
|
|
222
|
+
"type": [
|
|
223
|
+
"string",
|
|
224
|
+
"null"
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
105
230
|
}
|
package/src/adapter.ts
CHANGED
|
@@ -10,7 +10,7 @@ export class SandboxAdapter extends Adapter<SandboxWsEndpoint, SandboxEndpointCo
|
|
|
10
10
|
constructor(context: AdapterContext<SandboxEndpointConfig>) { super(); this.#context = context; }
|
|
11
11
|
override get configurationIdentityFields(): readonly string[] { return ['id']; }
|
|
12
12
|
// owner binds the synthetic sender's existing permissions and is not a runtime connection setting.
|
|
13
|
-
override get configurationUpdateFields(): readonly string[] { return []; }
|
|
13
|
+
override get configurationUpdateFields(): readonly string[] { return ['streaming', 'streamingIntervalMs']; }
|
|
14
14
|
protected override validateConfiguration(config: SandboxEndpointConfig): void { resolveSandboxEndpoint(config); }
|
|
15
15
|
protected createEndpoint(input: SandboxEndpointConfig, execution: EndpointCreationContext): SandboxWsEndpoint {
|
|
16
16
|
return new SandboxWsEndpoint({ id: execution.canonicalId, defaults: resolveSandboxEndpoint(input),
|
package/src/endpoint.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createMessages, resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
-
import { createEndpointSpecification, endpointSpec, type Endpoint, type EndpointSpecification, type EndpointSendRequest
|
|
2
|
+
import { createEndpointSpecification, endpointSpec, type Endpoint, type EndpointSpecification, type EndpointSendRequest,
|
|
3
|
+
type EndpointStreaming, type EndpointStreamRequest, type EndpointMessageStream, type EndpointStreamAcknowledgement } from 'zhin.js/adapter';
|
|
3
4
|
/**
|
|
4
5
|
* SandboxWsEndpoint — WebSocket lifecycle and OutboundMessageService bridge for /sandbox.
|
|
5
6
|
*/
|
|
@@ -161,6 +162,9 @@ export class SandboxWsEndpoint extends SandboxClient<SandboxConnection> implemen
|
|
|
161
162
|
start: () => this.#start(), open: () => this.#openEndpoint(),
|
|
162
163
|
close: () => this.#close(), stop: () => this.#stop(),
|
|
163
164
|
send: request => this.#send(request),
|
|
165
|
+
...(options.defaults.streaming ? { streaming: Object.freeze<EndpointStreaming>({ mode: 'native',
|
|
166
|
+
minIntervalMs: options.defaults.streamingIntervalMs, supports: request => this.#supportsStream(request),
|
|
167
|
+
open: request => this.#openStream(request) }) } : {}),
|
|
164
168
|
get transportState() { return readTransportState(); },
|
|
165
169
|
});
|
|
166
170
|
}
|
|
@@ -251,6 +255,42 @@ export class SandboxWsEndpoint extends SandboxClient<SandboxConnection> implemen
|
|
|
251
255
|
return messageId;
|
|
252
256
|
}
|
|
253
257
|
|
|
258
|
+
#supportsStream(request: Pick<EndpointStreamRequest, 'conversation' | 'source'>): boolean {
|
|
259
|
+
return !request.conversation.parent && request.conversation.threadId === undefined
|
|
260
|
+
&& ['private', 'group', 'channel'].includes(request.conversation.kind);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#openStream(request: EndpointStreamRequest): EndpointMessageStream | undefined {
|
|
264
|
+
if (!this.#supportsStream(request)) return undefined;
|
|
265
|
+
const messageId = randomUUID();
|
|
266
|
+
let revision = 0, timestamp: number | undefined, acceptedText = '';
|
|
267
|
+
let state: 'active' | 'completed' | 'cancelled' = 'active';
|
|
268
|
+
let last: EndpointStreamAcknowledgement | undefined;
|
|
269
|
+
const submit = async (text: string, finish: boolean): Promise<EndpointStreamAcknowledgement> => {
|
|
270
|
+
if (state === 'completed') {
|
|
271
|
+
if (finish && text === acceptedText) return last!;
|
|
272
|
+
throw new EndpointDeliveryError('stream_closed', 'Sandbox reply stream has completed', 'not_sent');
|
|
273
|
+
}
|
|
274
|
+
if (state === 'cancelled' || request.signal?.aborted || !this.#open) {
|
|
275
|
+
throw new EndpointDeliveryError('endpoint_inactive', 'Sandbox reply stream has ended', 'not_sent');
|
|
276
|
+
}
|
|
277
|
+
if (!text.startsWith(acceptedText)) throw new EndpointDeliveryError('invalid_payload', 'Sandbox stream text must preserve the confirmed prefix', 'not_sent');
|
|
278
|
+
const sentAt = timestamp ?? Date.now();
|
|
279
|
+
this.#send({ conversation: request.conversation, signal: request.signal, payload: {
|
|
280
|
+
messageId, revision: revision + 1, streaming: !finish, timestamp: sentAt,
|
|
281
|
+
content: [...(request.prefix ?? []), request.format === 'markdown'
|
|
282
|
+
? { type: 'markdown', data: { content: text } } : { type: 'text', data: { text } }],
|
|
283
|
+
} });
|
|
284
|
+
timestamp = sentAt; revision += 1; acceptedText = text;
|
|
285
|
+
last = { status: 'sent', messageId, acceptedText };
|
|
286
|
+
if (finish) state = 'completed';
|
|
287
|
+
return last;
|
|
288
|
+
};
|
|
289
|
+
return Object.freeze<EndpointMessageStream>({ write: text => submit(text, false), finish: text => submit(text, true),
|
|
290
|
+
cancel: () => { if (state === 'active') state = 'cancelled'; },
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
254
294
|
#subscribers(scene: { readonly type: string; readonly id: string }, echo = false): SandboxConnection[] {
|
|
255
295
|
return [...this.connectionRecords.values()].filter(connection => !connection.placeholder && connection.canExecute
|
|
256
296
|
&& (!echo || connection.scoped)
|
package/src/protocol.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { EndpointProfileConfiguration } from 'zhin.js/adapter';
|
|
1
2
|
/** Sandbox WebSocket wire protocol helpers (no legacy Adapter/Endpoint). */
|
|
2
3
|
|
|
3
4
|
import { readFileSync } from 'node:fs';
|
|
@@ -41,12 +42,16 @@ export type ResolvedSandboxBot = {
|
|
|
41
42
|
readonly id: string;
|
|
42
43
|
readonly owner: string;
|
|
43
44
|
readonly randomNamePerConnection: boolean;
|
|
45
|
+
readonly streaming: boolean;
|
|
46
|
+
readonly streamingIntervalMs: number;
|
|
44
47
|
};
|
|
45
48
|
|
|
46
49
|
/** One endpoint config after AdapterIndex expands `plugins.<instanceKey>.endpoints`. */
|
|
47
|
-
export interface SandboxEndpointConfig {
|
|
50
|
+
export interface SandboxEndpointConfig extends EndpointProfileConfiguration {
|
|
48
51
|
readonly id?: string;
|
|
49
52
|
readonly owner?: string;
|
|
53
|
+
readonly streaming?: boolean;
|
|
54
|
+
readonly streamingIntervalMs?: number;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
export function resolveSandboxEndpoint(
|
|
@@ -54,6 +59,8 @@ export function resolveSandboxEndpoint(
|
|
|
54
59
|
): ResolvedSandboxBot {
|
|
55
60
|
const id = optionalEndpointField(config.id) ?? 'sandbox-bot';
|
|
56
61
|
const owner = optionalEndpointField(config.owner) ?? 'sandbox-user';
|
|
62
|
+
const streamingIntervalMs = config.streamingIntervalMs ?? 500;
|
|
63
|
+
if (!Number.isFinite(streamingIntervalMs) || streamingIntervalMs < 0) throw new TypeError('streamingIntervalMs must be a non-negative finite number');
|
|
57
64
|
return {
|
|
58
65
|
context: 'sandbox',
|
|
59
66
|
id,
|
|
@@ -62,6 +69,8 @@ export function resolveSandboxEndpoint(
|
|
|
62
69
|
// across browser reconnects and Host restarts so a persisted playground
|
|
63
70
|
// session resumes the same Agent context.
|
|
64
71
|
randomNamePerConnection: false,
|
|
72
|
+
streaming: config.streaming !== false,
|
|
73
|
+
streamingIntervalMs,
|
|
65
74
|
};
|
|
66
75
|
}
|
|
67
76
|
|