@zhin.js/adapter-sandbox 1.1.33 → 1.1.34

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.
@@ -0,0 +1,41 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { getSandboxApiBase, getSandboxAuthHeaders } from './sandboxTransport.js';
3
+ /**
4
+ * Host marker of a human approval prompt (#1250): `UserInteractionRequest.purpose = 'approval'`
5
+ * is rendered by Core as keyboard `data.purpose`. Any segment carrying it marks the message, so a
6
+ * text-only approval card that sets the same field on its body is recognised too.
7
+ */
8
+ export const APPROVAL_PURPOSE = 'approval';
9
+ /** Only real approval prompts get the "needs your confirmation" label, not game cards or menu pages. */
10
+ export function isApprovalPrompt(message) {
11
+ return message.type === 'received' && message.interactionReadOnly !== true
12
+ && message.content.some((segment) => {
13
+ const data = segment.data;
14
+ return data !== null && typeof data === 'object' && data.purpose === APPROVAL_PURPOSE;
15
+ });
16
+ }
17
+ /** Quick commands of the Host's command index; null when the Host cannot tell (older Host, no binding). */
18
+ export async function readSandboxCommandHints(connectionId, scene, locale, signal, context) {
19
+ const url = new URL('/api/conversations/workspaces/commands', `${(context ? context.apiBase : getSandboxApiBase()) || window.location.origin}/`);
20
+ url.searchParams.set('connectionId', connectionId);
21
+ url.searchParams.set('type', scene.type);
22
+ url.searchParams.set('id', scene.id);
23
+ url.searchParams.set('locale', locale);
24
+ try {
25
+ const response = await fetch(url, { headers: context?.headers ?? getSandboxAuthHeaders(), signal });
26
+ if (!response.ok)
27
+ return null;
28
+ const body = await response.json();
29
+ const data = body?.data;
30
+ if (!body?.success || !data || typeof data.prefix !== 'string' || !Array.isArray(data.quick))
31
+ return null;
32
+ return Object.freeze({
33
+ prefix: data.prefix,
34
+ ...(typeof data.menu === 'string' ? { menu: data.menu } : {}),
35
+ quick: Object.freeze(data.quick.filter((item) => typeof item === 'string' && item.trim().length > 0).slice(0, 6)),
36
+ });
37
+ }
38
+ catch {
39
+ return null;
40
+ }
41
+ }
@@ -0,0 +1,56 @@
1
+ import type { MessageSegment } from '@zhin.js/client';
2
+ import { getSandboxApiBase, getSandboxAuthHeaders, type SandboxRequestContext } from './sandboxTransport.js';
3
+ import type { SandboxScope } from './agentTrace.js';
4
+ import type { PlaygroundMessage } from './playgroundState.js';
5
+
6
+ /**
7
+ * Host marker of a human approval prompt (#1250): `UserInteractionRequest.purpose = 'approval'`
8
+ * is rendered by Core as keyboard `data.purpose`. Any segment carrying it marks the message, so a
9
+ * text-only approval card that sets the same field on its body is recognised too.
10
+ */
11
+ export const APPROVAL_PURPOSE = 'approval';
12
+
13
+ /** Only real approval prompts get the "needs your confirmation" label, not game cards or menu pages. */
14
+ export function isApprovalPrompt(message: Pick<PlaygroundMessage, 'type' | 'interactionReadOnly' | 'content'>): boolean {
15
+ return message.type === 'received' && message.interactionReadOnly !== true
16
+ && message.content.some((segment: MessageSegment) => {
17
+ const data = segment.data as Record<string, unknown> | undefined;
18
+ return data !== null && typeof data === 'object' && data.purpose === APPROVAL_PURPOSE;
19
+ });
20
+ }
21
+
22
+ /** What `GET /api/conversations/workspaces/commands` returns for the live Sandbox binding. */
23
+ export interface SandboxCommandHints {
24
+ readonly prefix: string;
25
+ readonly menu?: string;
26
+ readonly quick: readonly string[];
27
+ }
28
+
29
+ /** Quick commands of the Host's command index; null when the Host cannot tell (older Host, no binding). */
30
+ export async function readSandboxCommandHints(
31
+ connectionId: string,
32
+ scene: Readonly<{ type: SandboxScope; id: string }>,
33
+ locale: string,
34
+ signal?: AbortSignal,
35
+ context?: SandboxRequestContext,
36
+ ): Promise<SandboxCommandHints | null> {
37
+ const url = new URL('/api/conversations/workspaces/commands', `${(context ? context.apiBase : getSandboxApiBase()) || window.location.origin}/`);
38
+ url.searchParams.set('connectionId', connectionId);
39
+ url.searchParams.set('type', scene.type);
40
+ url.searchParams.set('id', scene.id);
41
+ url.searchParams.set('locale', locale);
42
+ try {
43
+ const response = await fetch(url, { headers: context?.headers ?? getSandboxAuthHeaders(), signal });
44
+ if (!response.ok) return null;
45
+ const body = await response.json() as { success?: boolean; data?: Partial<SandboxCommandHints> };
46
+ const data = body?.data;
47
+ if (!body?.success || !data || typeof data.prefix !== 'string' || !Array.isArray(data.quick)) return null;
48
+ return Object.freeze({
49
+ prefix: data.prefix,
50
+ ...(typeof data.menu === 'string' ? { menu: data.menu } : {}),
51
+ quick: Object.freeze(data.quick.filter((item): item is string => typeof item === 'string' && item.trim().length > 0).slice(0, 6)),
52
+ });
53
+ } catch {
54
+ return null;
55
+ }
56
+ }
@@ -123,6 +123,7 @@ export const sandboxEnglish = {
123
123
  "编写": "Write",
124
124
  "预览": "Preview",
125
125
  "清空输入": "Clear input",
126
+ "正在加载会话记录…": "Loading conversation history…",
126
127
  "搜索表情...": "Search emoji…",
127
128
  "未找到匹配的表情": "No matching emoji",
128
129
  "支持 http(s) 图片链接或 data URL": "Supports HTTP(S) image links or data URLs",
@@ -136,7 +137,15 @@ export const sandboxEnglish = {
136
137
  "输入 Markdown 后可在这里检查最终效果": "Enter Markdown to preview the final result here",
137
138
  "未找到匹配的用户": "No matching users",
138
139
  "发送": "Send",
139
- "快捷操作:": "Shortcuts:",
140
+ "快捷操作与语法": "Shortcuts and syntax",
141
+ "媒体": "Media",
142
+ "命令以 {0} 开头": "Commands start with {0}",
143
+ "命令无需前缀": "Commands need no prefix",
144
+ "更多操作": "More actions",
145
+ "@ 机器人": "@ the bot",
146
+ "@ 机器人(插入 [@{0}])": "@ the bot (inserts [@{0}])",
147
+ "@机器人(群聊里唤起机器人,也可点 @ 按钮插入)": "@ the bot (wakes it in group chats; or use the @ button)",
148
+ "模拟发送者": "Simulated sender",
140
149
  "换行": "New line",
141
150
  "代码块": "Code block",
142
151
  "**文本**": "**text**",
@@ -124,6 +124,7 @@ export const sandboxEnglish: Readonly<Record<string, string>> = {
124
124
  "编写": "Write",
125
125
  "预览": "Preview",
126
126
  "清空输入": "Clear input",
127
+ "正在加载会话记录…": "Loading conversation history…",
127
128
  "搜索表情...": "Search emoji…",
128
129
  "未找到匹配的表情": "No matching emoji",
129
130
  "支持 http(s) 图片链接或 data URL": "Supports HTTP(S) image links or data URLs",
@@ -137,7 +138,15 @@ export const sandboxEnglish: Readonly<Record<string, string>> = {
137
138
  "输入 Markdown 后可在这里检查最终效果": "Enter Markdown to preview the final result here",
138
139
  "未找到匹配的用户": "No matching users",
139
140
  "发送": "Send",
140
- "快捷操作:": "Shortcuts:",
141
+ "快捷操作与语法": "Shortcuts and syntax",
142
+ "媒体": "Media",
143
+ "命令以 {0} 开头": "Commands start with {0}",
144
+ "命令无需前缀": "Commands need no prefix",
145
+ "更多操作": "More actions",
146
+ "@ 机器人": "@ the bot",
147
+ "@ 机器人(插入 [@{0}])": "@ the bot (inserts [@{0}])",
148
+ "@机器人(群聊里唤起机器人,也可点 @ 按钮插入)": "@ the bot (wakes it in group chats; or use the @ button)",
149
+ "模拟发送者": "Simulated sender",
141
150
  "换行": "New line",
142
151
  "代码块": "Code block",
143
152
  "**文本**": "**text**",
@@ -0,0 +1,83 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ /**
3
+ * Page-side fallback for segments the Sandbox page has no dedicated renderer for (#1250).
4
+ * Core already flattens outbound history; this is the last guard so a reply never shows a raw
5
+ * `{"$jsx":…}` object or a bare `[markdown]`: Markdown keeps its source, buttons (keyboard rows and
6
+ * JSX `<button>`) become `[label]`, unrendered JSX is read through its children.
7
+ */
8
+ const MAX_DEPTH = 32;
9
+ export function isJsxNode(value) {
10
+ if (!value || typeof value !== 'object' || Array.isArray(value))
11
+ return false;
12
+ const node = value;
13
+ return node.$jsx === 'zhin.jsx/1' && !!node.props && typeof node.props === 'object' && !Array.isArray(node.props);
14
+ }
15
+ function keyboardLabels(rows) {
16
+ if (!Array.isArray(rows))
17
+ return '';
18
+ return rows.flatMap(row => Array.isArray(row) ? row : [])
19
+ .map(button => button && typeof button === 'object' ? button.label : undefined)
20
+ .filter((label) => typeof label === 'string' && label.length > 0)
21
+ .map(label => `[${label}]`).join(' ');
22
+ }
23
+ function flatten(node, depth) {
24
+ if (typeof node === 'string')
25
+ return node;
26
+ if (node == null || typeof node === 'boolean')
27
+ return '';
28
+ if (typeof node === 'number' || typeof node === 'bigint')
29
+ return String(node);
30
+ if (depth > MAX_DEPTH || typeof node !== 'object')
31
+ return '';
32
+ if (Array.isArray(node))
33
+ return node.map(item => flatten(item, depth + 1)).join('');
34
+ if (isJsxNode(node)) {
35
+ const props = node.props;
36
+ if (node.type === 'button') {
37
+ const label = typeof props.label === 'string' ? props.label : flatten(props.children, depth + 1);
38
+ return label ? `[${label}]` : '';
39
+ }
40
+ if (typeof props.text === 'string')
41
+ return props.text;
42
+ if (node.type === 'keyboard' && Array.isArray(props.rows))
43
+ return keyboardLabels(props.rows);
44
+ return flatten(props.children ?? props.content, depth + 1);
45
+ }
46
+ const record = node;
47
+ if (typeof record.type === 'string')
48
+ return segmentFallbackText(record, depth + 1) ?? `[${record.type}]`;
49
+ return typeof record.text === 'string' ? record.text : '';
50
+ }
51
+ /**
52
+ * Display text (Markdown) for a segment, or undefined when nothing better than `[type]` exists.
53
+ * Accepts canonical segments (`{ type, data }`) and unrendered JSX nodes.
54
+ */
55
+ export function segmentFallbackText(segment, depth = 0) {
56
+ if (!segment || typeof segment !== 'object' || depth > MAX_DEPTH)
57
+ return undefined;
58
+ if (isJsxNode(segment)) {
59
+ const text = flatten(segment, depth);
60
+ return text || undefined;
61
+ }
62
+ const record = segment;
63
+ const data = record.data && typeof record.data === 'object' && !Array.isArray(record.data)
64
+ ? record.data : undefined;
65
+ if (!data)
66
+ return undefined;
67
+ if (record.type === 'keyboard')
68
+ return keyboardLabels(data.rows) || undefined;
69
+ for (const field of ['text', 'content', 'markdown', 'children']) {
70
+ const value = data[field];
71
+ if (value === undefined || value === null)
72
+ continue;
73
+ const text = flatten(value, depth + 1);
74
+ if (text)
75
+ return text;
76
+ }
77
+ return undefined;
78
+ }
79
+ /** Plain text of a segment's body for previews; `[type]` only when nothing readable exists. */
80
+ export function segmentPreviewText(segment) {
81
+ const type = segment && typeof segment === 'object' ? segment.type : undefined;
82
+ return segmentFallbackText(segment) ?? (typeof type === 'string' ? `[${type}]` : '');
83
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Page-side fallback for segments the Sandbox page has no dedicated renderer for (#1250).
3
+ * Core already flattens outbound history; this is the last guard so a reply never shows a raw
4
+ * `{"$jsx":…}` object or a bare `[markdown]`: Markdown keeps its source, buttons (keyboard rows and
5
+ * JSX `<button>`) become `[label]`, unrendered JSX is read through its children.
6
+ */
7
+ const MAX_DEPTH = 32;
8
+
9
+ interface JsxLike {
10
+ readonly $jsx: 'zhin.jsx/1';
11
+ readonly type: unknown;
12
+ readonly props: Readonly<Record<string, unknown>>;
13
+ }
14
+
15
+ export function isJsxNode(value: unknown): value is JsxLike {
16
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
17
+ const node = value as Partial<JsxLike>;
18
+ return node.$jsx === 'zhin.jsx/1' && !!node.props && typeof node.props === 'object' && !Array.isArray(node.props);
19
+ }
20
+
21
+ function keyboardLabels(rows: unknown): string {
22
+ if (!Array.isArray(rows)) return '';
23
+ return rows.flatMap(row => Array.isArray(row) ? row : [])
24
+ .map(button => button && typeof button === 'object' ? (button as { label?: unknown }).label : undefined)
25
+ .filter((label): label is string => typeof label === 'string' && label.length > 0)
26
+ .map(label => `[${label}]`).join(' ');
27
+ }
28
+
29
+ function flatten(node: unknown, depth: number): string {
30
+ if (typeof node === 'string') return node;
31
+ if (node == null || typeof node === 'boolean') return '';
32
+ if (typeof node === 'number' || typeof node === 'bigint') return String(node);
33
+ if (depth > MAX_DEPTH || typeof node !== 'object') return '';
34
+ if (Array.isArray(node)) return node.map(item => flatten(item, depth + 1)).join('');
35
+ if (isJsxNode(node)) {
36
+ const props = node.props;
37
+ if (node.type === 'button') {
38
+ const label = typeof props.label === 'string' ? props.label : flatten(props.children, depth + 1);
39
+ return label ? `[${label}]` : '';
40
+ }
41
+ if (typeof props.text === 'string') return props.text;
42
+ if (node.type === 'keyboard' && Array.isArray(props.rows)) return keyboardLabels(props.rows);
43
+ return flatten(props.children ?? props.content, depth + 1);
44
+ }
45
+ const record = node as Record<string, unknown>;
46
+ if (typeof record.type === 'string') return segmentFallbackText(record, depth + 1) ?? `[${record.type}]`;
47
+ return typeof record.text === 'string' ? record.text : '';
48
+ }
49
+
50
+ /**
51
+ * Display text (Markdown) for a segment, or undefined when nothing better than `[type]` exists.
52
+ * Accepts canonical segments (`{ type, data }`) and unrendered JSX nodes.
53
+ */
54
+ export function segmentFallbackText(segment: unknown, depth = 0): string | undefined {
55
+ if (!segment || typeof segment !== 'object' || depth > MAX_DEPTH) return undefined;
56
+ if (isJsxNode(segment)) {
57
+ const text = flatten(segment, depth);
58
+ return text || undefined;
59
+ }
60
+ const record = segment as { type?: unknown; data?: unknown };
61
+ const data = record.data && typeof record.data === 'object' && !Array.isArray(record.data)
62
+ ? record.data as Record<string, unknown> : undefined;
63
+ if (!data) return undefined;
64
+ if (record.type === 'keyboard') return keyboardLabels(data.rows) || undefined;
65
+ for (const field of ['text', 'content', 'markdown', 'children'] as const) {
66
+ const value = data[field];
67
+ if (value === undefined || value === null) continue;
68
+ const text = flatten(value, depth + 1);
69
+ if (text) return text;
70
+ }
71
+ return undefined;
72
+ }
73
+
74
+ /** Plain text of a segment's body for previews; `[type]` only when nothing readable exists. */
75
+ export function segmentPreviewText(segment: unknown): string {
76
+ const type = segment && typeof segment === 'object' ? (segment as { type?: unknown }).type : undefined;
77
+ return segmentFallbackText(segment) ?? (typeof type === 'string' ? `[${type}]` : '');
78
+ }
@@ -346,8 +346,22 @@ export const sandboxStyles = `
346
346
  .zhin-sandbox .sandbox-at-item > svg { flex-shrink: 0; color: var(--sb-muted-fg); }
347
347
  .zhin-sandbox .sandbox-at-name { font-size: 13px; font-weight: 600; }
348
348
  .zhin-sandbox .sandbox-at-id { font-size: 12px; color: var(--sb-muted-fg); }
349
- .zhin-sandbox .sandbox-hints { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; font-size: 12px; line-height: 1.8; color: var(--sb-muted-fg); }
350
- .zhin-sandbox .sandbox-hints > svg { flex-shrink: 0; }
349
+ .zhin-sandbox .sandbox-hints { font-size: 12px; line-height: 1.8; color: var(--sb-muted-fg); }
350
+ .zhin-sandbox .sandbox-hints > summary { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; list-style: none; }
351
+ .zhin-sandbox .sandbox-hints > summary::-webkit-details-marker { display: none; }
352
+ .zhin-sandbox .sandbox-hints > summary:hover { color: var(--sb-fg); }
353
+ .zhin-sandbox .sandbox-hints > ul { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 4px 0 0; padding: 0; list-style: none; }
354
+ .zhin-sandbox .sandbox-hints li { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
355
+ .zhin-sandbox .sandbox-more { position: relative; display: inline-flex; }
356
+ .zhin-sandbox .sandbox-more-trigger { color: var(--sb-muted-fg); }
357
+ .zhin-sandbox .sandbox-more-trigger.active { border-color: var(--sb-primary-line); background: var(--sb-primary-soft); color: var(--sb-primary); }
358
+ .zhin-sandbox .sandbox-more-backdrop { position: fixed; inset: 0; z-index: 40; }
359
+ .zhin-sandbox .sandbox-more-menu { position: absolute; top: calc(100% + 6px); right: 0; z-index: 41; display: grid; min-width: 220px; padding: 4px; border: 1px solid var(--sb-border); border-radius: var(--sb-radius-sm); background: var(--sb-bg); box-shadow: 0 8px 24px rgb(0 0 0 / 0.12); }
360
+ .zhin-sandbox .sandbox-more-menu > button { display: flex; align-items: center; gap: 8px; justify-content: flex-start; width: 100%; padding: 8px 10px; border: 0; background: transparent; color: var(--sb-fg); font-size: 13px; text-align: left; }
361
+ .zhin-sandbox .sandbox-more-menu > button:hover { background: var(--sb-accent); }
362
+ .zhin-sandbox .sandbox-more-menu > button[aria-checked="true"] { color: var(--sb-primary); }
363
+ .zhin-sandbox .sandbox-more-menu > button.is-danger { color: var(--sb-danger); }
364
+ .zhin-sandbox .sandbox-more-menu > button > em { margin-left: auto; font-style: normal; }
351
365
  .zhin-sandbox .sandbox-hint-key { padding: 1px 6px; border: 1px solid var(--sb-border); border-radius: var(--sb-radius-sm); background: var(--sb-muted); color: var(--sb-fg); font-family: var(--sb-mono); font-size: 12px; line-height: 1.4; }
352
366
  .zhin-sandbox .sandbox-hint-text { margin-right: 6px; }
353
367
 
@@ -471,8 +485,8 @@ export const sandboxStyles = `
471
485
  .zhin-sandbox .agent-playground-runbar { flex-direction: column; align-items: flex-start; gap: 10px; min-height: 0; padding: 12px; }
472
486
  /* In a column the 14rem flex-basis would become the identity row height. */
473
487
  .zhin-sandbox .agent-playground-run-identity { flex: 0 0 auto; width: 100%; }
474
- .zhin-sandbox .agent-playground-run-actions { flex-wrap: nowrap; justify-content: flex-start; margin-left: 0; overflow-x: auto; max-width: 100%; }
475
- .zhin-sandbox .agent-playground-run-actions > button[aria-controls="sandbox-run-inspector"] { display: none; }
488
+ .zhin-sandbox .agent-playground-run-actions { flex-wrap: wrap; justify-content: flex-start; margin-left: 0; max-width: 100%; }
489
+ .zhin-sandbox .sandbox-more-menu { right: auto; left: 0; }
476
490
  .zhin-sandbox .agent-playground-run-actions > a, .zhin-sandbox .agent-playground-run-actions > button { flex-shrink: 0; white-space: nowrap; width: auto; font-size: 13px; }
477
491
  .zhin-sandbox .agent-playground-run-title span { display: none; }
478
492
  .zhin-sandbox .agent-playground-message-scroll { padding: 16px 10px; }
@@ -345,8 +345,22 @@ export const sandboxStyles = `
345
345
  .zhin-sandbox .sandbox-at-item > svg { flex-shrink: 0; color: var(--sb-muted-fg); }
346
346
  .zhin-sandbox .sandbox-at-name { font-size: 13px; font-weight: 600; }
347
347
  .zhin-sandbox .sandbox-at-id { font-size: 12px; color: var(--sb-muted-fg); }
348
- .zhin-sandbox .sandbox-hints { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; font-size: 12px; line-height: 1.8; color: var(--sb-muted-fg); }
349
- .zhin-sandbox .sandbox-hints > svg { flex-shrink: 0; }
348
+ .zhin-sandbox .sandbox-hints { font-size: 12px; line-height: 1.8; color: var(--sb-muted-fg); }
349
+ .zhin-sandbox .sandbox-hints > summary { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; list-style: none; }
350
+ .zhin-sandbox .sandbox-hints > summary::-webkit-details-marker { display: none; }
351
+ .zhin-sandbox .sandbox-hints > summary:hover { color: var(--sb-fg); }
352
+ .zhin-sandbox .sandbox-hints > ul { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 4px 0 0; padding: 0; list-style: none; }
353
+ .zhin-sandbox .sandbox-hints li { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
354
+ .zhin-sandbox .sandbox-more { position: relative; display: inline-flex; }
355
+ .zhin-sandbox .sandbox-more-trigger { color: var(--sb-muted-fg); }
356
+ .zhin-sandbox .sandbox-more-trigger.active { border-color: var(--sb-primary-line); background: var(--sb-primary-soft); color: var(--sb-primary); }
357
+ .zhin-sandbox .sandbox-more-backdrop { position: fixed; inset: 0; z-index: 40; }
358
+ .zhin-sandbox .sandbox-more-menu { position: absolute; top: calc(100% + 6px); right: 0; z-index: 41; display: grid; min-width: 220px; padding: 4px; border: 1px solid var(--sb-border); border-radius: var(--sb-radius-sm); background: var(--sb-bg); box-shadow: 0 8px 24px rgb(0 0 0 / 0.12); }
359
+ .zhin-sandbox .sandbox-more-menu > button { display: flex; align-items: center; gap: 8px; justify-content: flex-start; width: 100%; padding: 8px 10px; border: 0; background: transparent; color: var(--sb-fg); font-size: 13px; text-align: left; }
360
+ .zhin-sandbox .sandbox-more-menu > button:hover { background: var(--sb-accent); }
361
+ .zhin-sandbox .sandbox-more-menu > button[aria-checked="true"] { color: var(--sb-primary); }
362
+ .zhin-sandbox .sandbox-more-menu > button.is-danger { color: var(--sb-danger); }
363
+ .zhin-sandbox .sandbox-more-menu > button > em { margin-left: auto; font-style: normal; }
350
364
  .zhin-sandbox .sandbox-hint-key { padding: 1px 6px; border: 1px solid var(--sb-border); border-radius: var(--sb-radius-sm); background: var(--sb-muted); color: var(--sb-fg); font-family: var(--sb-mono); font-size: 12px; line-height: 1.4; }
351
365
  .zhin-sandbox .sandbox-hint-text { margin-right: 6px; }
352
366
 
@@ -470,8 +484,8 @@ export const sandboxStyles = `
470
484
  .zhin-sandbox .agent-playground-runbar { flex-direction: column; align-items: flex-start; gap: 10px; min-height: 0; padding: 12px; }
471
485
  /* In a column the 14rem flex-basis would become the identity row height. */
472
486
  .zhin-sandbox .agent-playground-run-identity { flex: 0 0 auto; width: 100%; }
473
- .zhin-sandbox .agent-playground-run-actions { flex-wrap: nowrap; justify-content: flex-start; margin-left: 0; overflow-x: auto; max-width: 100%; }
474
- .zhin-sandbox .agent-playground-run-actions > button[aria-controls="sandbox-run-inspector"] { display: none; }
487
+ .zhin-sandbox .agent-playground-run-actions { flex-wrap: wrap; justify-content: flex-start; margin-left: 0; max-width: 100%; }
488
+ .zhin-sandbox .sandbox-more-menu { right: auto; left: 0; }
475
489
  .zhin-sandbox .agent-playground-run-actions > a, .zhin-sandbox .agent-playground-run-actions > button { flex-shrink: 0; white-space: nowrap; width: auto; font-size: 13px; }
476
490
  .zhin-sandbox .agent-playground-run-title span { display: none; }
477
491
  .zhin-sandbox .agent-playground-message-scroll { padding: 16px 10px; }
package/plugin.js CHANGED
@@ -3,7 +3,4 @@ import { definePlugin } from 'zhin.js';
3
3
  export default definePlugin({
4
4
  name: 'sandbox',
5
5
  configUpdate: { featurePaths: [['nickname'], ['profile'], ['endpoints']] },
6
- metadata: {
7
- displayName: 'Sandbox Adapter',
8
- },
9
6
  });
package/src/endpoint.ts CHANGED
@@ -345,7 +345,10 @@ export class SandboxWsEndpoint extends SandboxClient<SandboxConnection> implemen
345
345
  }));
346
346
  return;
347
347
  }
348
- const frame = parseSandboxWsFrame(raw);
348
+ const frame = parseSandboxWsFrame(raw, {
349
+ id: this.#options.defaults.id,
350
+ ...(this.#options.defaults.nickname ? { names: [this.#options.defaults.nickname] } : {}),
351
+ });
349
352
  if (frame.kind === 'invalid') {
350
353
  socket.send(JSON.stringify({
351
354
  type: 'error', endpoint: target, connectionId,
package/src/protocol.ts CHANGED
@@ -75,6 +75,8 @@ export type ResolvedSandboxBot = {
75
75
  readonly randomNamePerConnection: boolean;
76
76
  readonly streaming: boolean;
77
77
  readonly streamingIntervalMs: number;
78
+ /** Configured `nickname`; `[@nickname]` also mentions this bot. */
79
+ readonly nickname?: string;
78
80
  };
79
81
 
80
82
  /** One endpoint config after AdapterIndex expands `plugins.<instanceKey>.endpoints`. */
@@ -102,6 +104,7 @@ export function resolveSandboxEndpoint(
102
104
  randomNamePerConnection: false,
103
105
  streaming: config.streaming !== false,
104
106
  streamingIntervalMs,
107
+ ...(optionalEndpointField(config.nickname) ? { nickname: optionalEndpointField(config.nickname)! } : {}),
105
108
  };
106
109
  }
107
110
 
@@ -208,8 +211,72 @@ export type SandboxWsFrame =
208
211
  | { readonly kind: 'message'; readonly message: SandboxParsedMessage }
209
212
  | { readonly kind: 'invalid' };
210
213
 
214
+ /**
215
+ * The bot this socket talks to. Typed `[@<id or name>]` text (the input hint the Sandbox page shows)
216
+ * becomes a mention segment; a match on `id` or one of `names` (case-insensitive) targets `id`,
217
+ * so the IM layer recognises it as a mention of the bot.
218
+ */
219
+ export interface SandboxSelfMention {
220
+ readonly id: string;
221
+ readonly names?: readonly string[];
222
+ }
223
+
224
+ const TYPED_MENTION = /\[@([^\[\]\n]{1,64})\]/gu;
225
+
226
+ function expandTypedMentions(
227
+ content: MessageElement[],
228
+ text: string,
229
+ self: SandboxSelfMention | undefined,
230
+ ): { content: MessageElement[]; text: string } {
231
+ const selfKeys = new Set([self?.id, ...(self?.names ?? [])]
232
+ .flatMap(value => typeof value === 'string' && value.trim() ? [value.trim().toLowerCase()] : []));
233
+ const resolve = (name: string): { target: string; self: boolean } => {
234
+ const trimmed = name.trim();
235
+ return self && selfKeys.has(trimmed.toLowerCase()) ? { target: self.id, self: true } : { target: trimmed, self: false };
236
+ };
237
+ let expanded = false;
238
+ const next: MessageElement[] = [];
239
+ for (const segment of content) {
240
+ // A picked mention (`mention` segment) may carry the bot's name instead of its id.
241
+ if ((segment.type === 'mention' || segment.type === 'at') && self && typeof segment.data?.target === 'string'
242
+ && segment.data.target !== self.id && selfKeys.has(segment.data.target.trim().toLowerCase())) {
243
+ expanded = true;
244
+ next.push({ ...segment, data: { ...segment.data, target: self.id } });
245
+ continue;
246
+ }
247
+ const value = segment.type === 'text' ? segment.data?.text : undefined;
248
+ if (typeof value !== 'string' || !value.includes('[@')) {
249
+ next.push(segment);
250
+ continue;
251
+ }
252
+ let last = 0;
253
+ for (const match of value.matchAll(TYPED_MENTION)) {
254
+ const name = match[1]!;
255
+ if (!name.trim()) continue;
256
+ expanded = true;
257
+ if (match.index > last) next.push({ type: 'text', data: { text: value.slice(last, match.index) } });
258
+ const { target } = resolve(name);
259
+ next.push({ type: 'mention', data: { target, name: name.trim() } });
260
+ last = match.index + match[0].length;
261
+ }
262
+ if (last === 0) next.push(segment);
263
+ else if (last < value.length) next.push({ type: 'text', data: { text: value.slice(last) } });
264
+ }
265
+ if (!expanded) return { content, text };
266
+ // Plain-text view: drop mentions of the bot (like other platforms strip the at-self prefix),
267
+ // keep `@name` for anyone else so commands and the Agent still see who was addressed.
268
+ let removedSelf = false;
269
+ const plain = text.replace(TYPED_MENTION, (whole, name: string) => {
270
+ if (!name.trim()) return whole;
271
+ const resolved = resolve(name);
272
+ if (resolved.self) { removedSelf = true; return ''; }
273
+ return `@${resolved.target}`;
274
+ });
275
+ return { content: next, text: removedSelf ? plain.replace(/[ \t]{2,}/gu, ' ').trim() : plain };
276
+ }
277
+
211
278
  /** Control frames never fall through into chat or platform message hooks. */
212
- export function parseSandboxWsFrame(raw: string): SandboxWsFrame {
279
+ export function parseSandboxWsFrame(raw: string, self?: SandboxSelfMention): SandboxWsFrame {
213
280
  const payload = readSandboxWirePayload(raw);
214
281
  if ('op' in payload) {
215
282
  const conversation = payload.conversation as Partial<SandboxConversation> | null;
@@ -218,28 +285,28 @@ export function parseSandboxWsFrame(raw: string): SandboxWsFrame {
218
285
  || typeof conversation.id !== 'string' || !conversation.id.trim()) return { kind: 'invalid' };
219
286
  return { kind: 'subscribe', conversation: { type: conversation.type, id: conversation.id } };
220
287
  }
221
- const message = normalizeSandboxWsPayload(payload, raw);
288
+ const message = normalizeSandboxWsPayload(payload, raw, self);
222
289
  if (!['private', 'direct', 'group', 'channel', 'guild'].includes(message.type)
223
290
  || typeof message.id !== 'string') return { kind: 'invalid' };
224
291
  return { kind: 'message', message };
225
292
  }
226
293
 
227
- export function parseSandboxWsPayload(raw: string): SandboxParsedMessage {
228
- return normalizeSandboxWsPayload(readSandboxWirePayload(raw), raw);
294
+ export function parseSandboxWsPayload(raw: string, self?: SandboxSelfMention): SandboxParsedMessage {
295
+ return normalizeSandboxWsPayload(readSandboxWirePayload(raw), raw, self);
229
296
  }
230
297
 
231
298
  export const parseSandboxInboundPayload = parseSandboxWsPayload;
232
299
 
233
- function normalizeSandboxWsPayload(payload: SandboxWirePayload, raw: string): SandboxParsedMessage {
300
+ function normalizeSandboxWsPayload(payload: SandboxWirePayload, raw: string, self?: SandboxSelfMention): SandboxParsedMessage {
234
301
  const type = payload.type ?? 'private';
235
302
  const id = payload.id ?? 'sandbox-user';
236
- const content: MessageElement[] = typeof payload.content === 'string'
303
+ const wireContent: MessageElement[] = typeof payload.content === 'string'
237
304
  ? [{ type: 'text', data: { text: payload.content } }]
238
305
  : Array.isArray(payload.content)
239
306
  ? payload.content
240
307
  : [{ type: 'text', data: { text: payload.text ?? raw } }];
241
308
 
242
- const actionSegment = content.find((segment) => segment.type === 'action');
309
+ const actionSegment = wireContent.find((segment) => segment.type === 'action');
243
310
  let action: { id: string; payload: string } | undefined;
244
311
  if (actionSegment?.data) {
245
312
  const actionPayload = typeof actionSegment.data.payload === 'string'
@@ -255,7 +322,7 @@ function normalizeSandboxWsPayload(payload: SandboxWirePayload, raw: string): Sa
255
322
  }
256
323
  }
257
324
 
258
- let text = content
325
+ let text = wireContent
259
326
  .flatMap((segment) => (segment.type === 'text' && typeof segment.data?.text === 'string'
260
327
  ? [segment.data.text]
261
328
  : []))
@@ -265,6 +332,9 @@ function normalizeSandboxWsPayload(payload: SandboxWirePayload, raw: string): Sa
265
332
  ? payload.text
266
333
  : action?.payload ?? raw;
267
334
  }
335
+ const typed = expandTypedMentions(wireContent, text, self);
336
+ const content = typed.content;
337
+ text = typed.text;
268
338
  const agentRun = normalizeSandboxAgentRunConfig(payload.agentRun);
269
339
  const rawMessageId = typeof payload.messageId === 'string' ? payload.messageId.trim() : '';
270
340
  const messageId = /^[A-Za-z0-9._:-]{1,160}$/u.test(rawMessageId) ? rawMessageId : undefined;