chatluna-plugin-onebot-sandbox 1.0.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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -0
  3. package/client/app.tsx +306 -0
  4. package/client/chat/chat-pane.tsx +303 -0
  5. package/client/chat/composer.css +635 -0
  6. package/client/chat/composer.tsx +646 -0
  7. package/client/chat/details-panel.tsx +196 -0
  8. package/client/chat/emoji-picker.tsx +70 -0
  9. package/client/chat/extension-mark.tsx +16 -0
  10. package/client/chat/forward.tsx +257 -0
  11. package/client/chat/image-preview.tsx +93 -0
  12. package/client/chat/member-menu.tsx +64 -0
  13. package/client/chat/message-list.tsx +617 -0
  14. package/client/chat/messages.css +1018 -0
  15. package/client/chat/notifications.tsx +83 -0
  16. package/client/chat/reactions.tsx +52 -0
  17. package/client/chat/search-panel.tsx +231 -0
  18. package/client/chat/sidebar.tsx +424 -0
  19. package/client/chat/webqq-view.tsx +71 -0
  20. package/client/environment/avatar-picker.tsx +92 -0
  21. package/client/environment/create-popover.tsx +228 -0
  22. package/client/environment/directory-model.ts +63 -0
  23. package/client/environment/entity-dialog.css +383 -0
  24. package/client/environment/entity-dialog.tsx +442 -0
  25. package/client/environment/page.css +14 -0
  26. package/client/environment/page.tsx +280 -0
  27. package/client/i18n.ts +19 -0
  28. package/client/index.ts +17 -0
  29. package/client/locales/en.yml +617 -0
  30. package/client/locales/zh.yml +617 -0
  31. package/client/mcp/capability-catalog.tsx +277 -0
  32. package/client/mcp/credential-manager.tsx +257 -0
  33. package/client/onebot-debug/page.tsx +329 -0
  34. package/client/records/record-layout.tsx +429 -0
  35. package/client/records/records.css +259 -0
  36. package/client/settings/about.tsx +43 -0
  37. package/client/settings/draft.ts +47 -0
  38. package/client/settings/localize.ts +76 -0
  39. package/client/settings/page.tsx +210 -0
  40. package/client/settings/settings.css +47 -0
  41. package/client/shared/avatar.tsx +35 -0
  42. package/client/shared/confirm-dialog.tsx +44 -0
  43. package/client/shared/controls.css +17 -0
  44. package/client/shared/discard-confirm.tsx +45 -0
  45. package/client/shared/floating-panel.ts +40 -0
  46. package/client/shared/format-duration.ts +22 -0
  47. package/client/shared/format-time.ts +49 -0
  48. package/client/shared/glass.css +150 -0
  49. package/client/shared/implementation.ts +12 -0
  50. package/client/shared/overlay-scrollbar.css +45 -0
  51. package/client/shared/overlay-scrollbar.tsx +123 -0
  52. package/client/shared/use-element-width.ts +19 -0
  53. package/client/shared/use-retained.ts +12 -0
  54. package/client/test-call/page.tsx +260 -0
  55. package/client/test-space/agent-visuals.tsx +121 -0
  56. package/client/test-space/observe-overlay.tsx +42 -0
  57. package/client/test-space/overview.tsx +267 -0
  58. package/client/test-space/spaces.css +201 -0
  59. package/client/test-space/thumbnail-capture.ts +63 -0
  60. package/client/test-space/thumbnail-scale.ts +21 -0
  61. package/client/test-space/use-space-list.ts +35 -0
  62. package/client/test-space/zoom.ts +197 -0
  63. package/client/webqq/composer-draft-host.ts +578 -0
  64. package/client/webqq/composer-draft.ts +81 -0
  65. package/client/webqq/conversation-tree.ts +155 -0
  66. package/client/webqq/emoji-catalog.ts +78 -0
  67. package/client/webqq/fork-boundary.ts +28 -0
  68. package/client/webqq/forward-cluster.ts +20 -0
  69. package/client/webqq/forward-dialog-stack.ts +74 -0
  70. package/client/webqq/forward-preview.ts +40 -0
  71. package/client/webqq/friend-menu.ts +17 -0
  72. package/client/webqq/group-display.ts +18 -0
  73. package/client/webqq/group-menu.ts +25 -0
  74. package/client/webqq/mention.ts +4 -0
  75. package/client/webqq/message-capabilities.ts +37 -0
  76. package/client/webqq/message-cluster.ts +68 -0
  77. package/client/webqq/message-presentation.ts +197 -0
  78. package/client/webqq/message-reveal.ts +31 -0
  79. package/client/webqq/message-search-date.ts +40 -0
  80. package/client/webqq/message-selection.ts +85 -0
  81. package/client/webqq/notification-requests.ts +20 -0
  82. package/client/webqq/participant-presentation.ts +128 -0
  83. package/client/webqq/profile-card.ts +199 -0
  84. package/client/webqq/relationship-directory.ts +62 -0
  85. package/client/webqq/thinking-panel.ts +7 -0
  86. package/client/webqq/user-stack.ts +53 -0
  87. package/client/workspace/agent-overlay-effect.ts +238 -0
  88. package/client/workspace/context.tsx +117 -0
  89. package/client/workspace/overlay-host.tsx +299 -0
  90. package/client/workspace/profile-card.css +164 -0
  91. package/client/workspace/state.ts +28 -0
  92. package/client/workspace/store.ts +388 -0
  93. package/client/workspace/use-test-spaces.ts +21 -0
  94. package/dist/.vite/manifest.json +21 -0
  95. package/dist/app-63rfTh6w.js +85 -0
  96. package/dist/client.css +4 -0
  97. package/dist/client.js +1 -0
  98. package/lib/account-profile.d.ts +17 -0
  99. package/lib/account-profile.d.ts.map +1 -0
  100. package/lib/base-dir.d.ts +4 -0
  101. package/lib/base-dir.d.ts.map +1 -0
  102. package/lib/bot.d.ts +185 -0
  103. package/lib/bot.d.ts.map +1 -0
  104. package/lib/builtin-avatar-options.d.ts +11 -0
  105. package/lib/builtin-avatar-options.d.ts.map +1 -0
  106. package/lib/builtin-avatars.d.ts +6 -0
  107. package/lib/builtin-avatars.d.ts.map +1 -0
  108. package/lib/channel-assignee.d.ts +52 -0
  109. package/lib/channel-assignee.d.ts.map +1 -0
  110. package/lib/chatluna/conversation-lines.d.ts +46 -0
  111. package/lib/chatluna/conversation-lines.d.ts.map +1 -0
  112. package/lib/chatluna/runtime.d.ts +26 -0
  113. package/lib/chatluna/runtime.d.ts.map +1 -0
  114. package/lib/chatluna/state.d.ts +67 -0
  115. package/lib/chatluna/state.d.ts.map +1 -0
  116. package/lib/chatluna/thinking.d.ts +10 -0
  117. package/lib/chatluna/thinking.d.ts.map +1 -0
  118. package/lib/chatluna/tools.d.ts +36 -0
  119. package/lib/chatluna/tools.d.ts.map +1 -0
  120. package/lib/chatluna/wakeup.d.ts +143 -0
  121. package/lib/chatluna/wakeup.d.ts.map +1 -0
  122. package/lib/console-contract.d.ts +131 -0
  123. package/lib/console-contract.d.ts.map +1 -0
  124. package/lib/console.d.ts +31 -0
  125. package/lib/console.d.ts.map +1 -0
  126. package/lib/control-service.d.ts +425 -0
  127. package/lib/control-service.d.ts.map +1 -0
  128. package/lib/conversation-resolution.d.ts +157 -0
  129. package/lib/conversation-resolution.d.ts.map +1 -0
  130. package/lib/error-detail.d.ts +3 -0
  131. package/lib/error-detail.d.ts.map +1 -0
  132. package/lib/inbound-delivery.d.ts +156 -0
  133. package/lib/inbound-delivery.d.ts.map +1 -0
  134. package/lib/index.d.ts +76 -0
  135. package/lib/index.d.ts.map +1 -0
  136. package/lib/index.js +12834 -0
  137. package/lib/index.js.map +1 -0
  138. package/lib/lifecycle.d.ts +8 -0
  139. package/lib/lifecycle.d.ts.map +1 -0
  140. package/lib/mcp/arguments.d.ts +71 -0
  141. package/lib/mcp/arguments.d.ts.map +1 -0
  142. package/lib/mcp/call-records.d.ts +32 -0
  143. package/lib/mcp/call-records.d.ts.map +1 -0
  144. package/lib/mcp/http-api.d.ts +147 -0
  145. package/lib/mcp/http-api.d.ts.map +1 -0
  146. package/lib/mcp/page-cursor.d.ts +37 -0
  147. package/lib/mcp/page-cursor.d.ts.map +1 -0
  148. package/lib/mcp/server.d.ts +60 -0
  149. package/lib/mcp/server.d.ts.map +1 -0
  150. package/lib/mcp/service.d.ts +219 -0
  151. package/lib/mcp/service.d.ts.map +1 -0
  152. package/lib/mcp/tool-registry.d.ts +113 -0
  153. package/lib/mcp/tool-registry.d.ts.map +1 -0
  154. package/lib/mcp/types.d.ts +144 -0
  155. package/lib/mcp/types.d.ts.map +1 -0
  156. package/lib/media-storage.d.ts +33 -0
  157. package/lib/media-storage.d.ts.map +1 -0
  158. package/lib/message-capabilities.d.ts +78 -0
  159. package/lib/message-capabilities.d.ts.map +1 -0
  160. package/lib/onebot-debug.d.ts +115 -0
  161. package/lib/onebot-debug.d.ts.map +1 -0
  162. package/lib/onebot-message.d.ts +23 -0
  163. package/lib/onebot-message.d.ts.map +1 -0
  164. package/lib/onebot-profiles.d.ts +45 -0
  165. package/lib/onebot-profiles.d.ts.map +1 -0
  166. package/lib/persistence.d.ts +148 -0
  167. package/lib/persistence.d.ts.map +1 -0
  168. package/lib/random.d.ts +5 -0
  169. package/lib/random.d.ts.map +1 -0
  170. package/lib/record-store.d.ts +80 -0
  171. package/lib/record-store.d.ts.map +1 -0
  172. package/lib/relationship-actions.d.ts +157 -0
  173. package/lib/relationship-actions.d.ts.map +1 -0
  174. package/lib/scope-directory.d.ts +105 -0
  175. package/lib/scope-directory.d.ts.map +1 -0
  176. package/lib/settings.d.ts +33 -0
  177. package/lib/settings.d.ts.map +1 -0
  178. package/lib/test-spaces.d.ts +69 -0
  179. package/lib/test-spaces.d.ts.map +1 -0
  180. package/lib/types.d.ts +739 -0
  181. package/lib/types.d.ts.map +1 -0
  182. package/lib/webui.d.ts +26 -0
  183. package/lib/webui.d.ts.map +1 -0
  184. package/package.json +112 -0
@@ -0,0 +1,43 @@
1
+ import { FlaskConicalIcon } from 'lucide-react'
2
+ import { Badge } from '@chatluna/ui-kit'
3
+ import { license, name, version } from '../../package.json'
4
+ import { useT } from '../i18n'
5
+
6
+ /**
7
+ * 设置页导航末尾的「关于」面板。版本号、包名与协议直接取自 package.json,发版只改那一处;
8
+ * 读的是客户端打包时的 package.json,不依赖服务端,设置读不到时也照常显示。
9
+ */
10
+ export function AboutPanel() {
11
+ const t = useT()
12
+ const rows: Array<{ label: string, value: string, mono?: boolean }> = [
13
+ { label: t('sandbox.settings.about.version'), value: version, mono: true },
14
+ { label: t('sandbox.settings.about.package'), value: name, mono: true },
15
+ { label: t('sandbox.settings.about.license'), value: license },
16
+ { label: t('sandbox.settings.about.protocol'), value: 'OneBot v11' },
17
+ { label: t('sandbox.settings.about.host'), value: 'ChatLuna v2' },
18
+ ]
19
+ return (
20
+ <div className="flex flex-col gap-5">
21
+ <div className="flex items-center gap-3">
22
+ <div className="flex size-11 shrink-0 items-center justify-center rounded-xl bg-primary/10 text-primary">
23
+ <FlaskConicalIcon className="size-5" />
24
+ </div>
25
+ <div className="flex min-w-0 flex-col gap-0.5">
26
+ <div className="flex flex-wrap items-center gap-2">
27
+ <h3 className="text-[15px] leading-5 font-semibold">{t('onebot-sandbox.title')}</h3>
28
+ <Badge variant="outline" className="font-mono">v{version}</Badge>
29
+ </div>
30
+ <p className="text-xs text-muted-foreground">{t('sandbox.settings.about.summary')}</p>
31
+ </div>
32
+ </div>
33
+ <dl className="grid grid-cols-[max-content_minmax(0,1fr)] gap-x-6 gap-y-2.5 border-t border-border pt-4 text-[13px] leading-5">
34
+ {rows.map((row) => (
35
+ <div key={row.label} className="contents">
36
+ <dt className="text-muted-foreground">{row.label}</dt>
37
+ <dd className={row.mono ? 'font-mono text-xs leading-5 break-all' : undefined}>{row.value}</dd>
38
+ </div>
39
+ ))}
40
+ </dl>
41
+ </div>
42
+ )
43
+ }
@@ -0,0 +1,47 @@
1
+ import * as React from 'react'
2
+
3
+ type Values = Record<string, unknown>
4
+
5
+ interface SettingsDraftState {
6
+ value?: Values
7
+ /** 草稿与已保存值不同;页面卸载后仍保留,供页头标签标出未保存修改。 */
8
+ dirty: boolean
9
+ }
10
+
11
+ /**
12
+ * 设置页的未保存草稿,放在模块里而不是组件状态里:宿主用 BrowserRouter,没有 `useBlocker` 能拦住
13
+ * 侧栏导航,离开设置页时页面直接卸载。草稿留在这里,回到设置页原样还原;刷新或关闭标签页时由
14
+ * `beforeunload` 提示。
15
+ */
16
+ let state: SettingsDraftState = { dirty: false }
17
+ const listeners = new Set<() => void>()
18
+
19
+ const warnBeforeUnload = (event: BeforeUnloadEvent) => {
20
+ event.preventDefault()
21
+ }
22
+
23
+ function update(next: SettingsDraftState) {
24
+ if (next.dirty !== state.dirty) {
25
+ if (next.dirty) window.addEventListener('beforeunload', warnBeforeUnload)
26
+ else window.removeEventListener('beforeunload', warnBeforeUnload)
27
+ }
28
+ state = next
29
+ for (const listener of listeners) listener()
30
+ }
31
+
32
+ const subscribe = (listener: () => void) => {
33
+ listeners.add(listener)
34
+ return () => listeners.delete(listener)
35
+ }
36
+ const getState = () => state
37
+
38
+ export const settingsDraft = {
39
+ setValue: (value: Values | undefined) => update({ value, dirty: value === undefined ? false : state.dirty }),
40
+ setDirty: (dirty: boolean) => {
41
+ if (dirty !== state.dirty) update({ ...state, dirty })
42
+ },
43
+ }
44
+
45
+ export function useSettingsDraft() {
46
+ return React.useSyncExternalStore(subscribe, getState, getState)
47
+ }
@@ -0,0 +1,76 @@
1
+ import type { Translate } from '@chatluna/ui-client/client'
2
+ import type { SchemaDocument, SchemaDocumentNode } from '@chatluna/schema'
3
+
4
+ /** schema 文案的键前缀,后接节点的文案 ID 与 `label`/`description`。 */
5
+ export const SCHEMA_LOCALE_PREFIX = 'sandbox.settings.schema.'
6
+
7
+ const TEXT_FIELDS = ['label', 'description'] as const
8
+
9
+ /** 一个带文案的 schema 节点:文案 ID 与服务端写在 schema 里的中文原文。 */
10
+ export interface SchemaText {
11
+ id: string
12
+ field: typeof TEXT_FIELDS[number]
13
+ text: string
14
+ }
15
+
16
+ /**
17
+ * 按配置路径给 schema 节点分配文案 ID:字段用点分路径(`testEndpoint.shared.host`),单选与多选的选项用
18
+ * `<路径>.option.<值>`。分区与小节没有自己的配置键(intersect 成员与所在字段共用路径),在 `src/index.ts`
19
+ * 里用 `extra('i18n', 'group.<名称>')` 显式起名;没起名的 intersect 成员不翻译,免得与所在字段撞键。
20
+ */
21
+ function visit(document: SchemaDocument, id: number, textId: string | undefined, path: string, found: (id: number, textId: string) => void) {
22
+ const node = document.refs[id]
23
+ if (!node) return
24
+ const named = (node.meta as { i18n?: unknown } | undefined)?.i18n
25
+ const own = typeof named === 'string' ? named : textId
26
+ if (own) found(id, own)
27
+ if (node.type === 'object') {
28
+ for (const [key, child] of Object.entries(node.dict ?? {})) {
29
+ const childPath = path ? `${path}.${key}` : key
30
+ visit(document, child, childPath, childPath, found)
31
+ }
32
+ } else if (node.type === 'intersect') {
33
+ for (const child of node.list ?? []) visit(document, child, undefined, path, found)
34
+ } else if (node.type === 'array' && node.inner !== undefined) {
35
+ // 多选(`role('checkbox')`)的选项是数组元素上的 union,与单选项共用 `<路径>.option.<值>`。
36
+ visit(document, node.inner, undefined, path, found)
37
+ } else if (node.type === 'union') {
38
+ for (const child of node.list ?? []) {
39
+ const option = document.refs[child]
40
+ if (option?.type === 'const') visit(document, child, `${path}.option.${String(option.value)}`, path, found)
41
+ }
42
+ }
43
+ }
44
+
45
+ /** 列出 schema 里全部待翻译的文案,供测试核对两份语言文件。 */
46
+ export function schemaTexts(document: SchemaDocument): SchemaText[] {
47
+ const texts: SchemaText[] = []
48
+ visit(document, document.uid, undefined, '', (id, textId) => {
49
+ const meta = document.refs[id]?.meta ?? {}
50
+ for (const field of TEXT_FIELDS) {
51
+ const text = meta[field]
52
+ if (typeof text === 'string' && text) texts.push({ id: textId, field, text })
53
+ }
54
+ })
55
+ return texts
56
+ }
57
+
58
+ /**
59
+ * v2 SchemaForm 只接受字符串形式的 `label`/`description`,没有多语言;这里在交给表单前按当前语言替换文案。
60
+ * 语言文件里缺的键原样保留服务端的中文,新增字段忘了补译文时不会显示成键名。
61
+ */
62
+ export function localizeSchema(document: SchemaDocument, t: Translate): SchemaDocument {
63
+ const refs: Record<string, SchemaDocumentNode> = { ...document.refs }
64
+ visit(document, document.uid, undefined, '', (id, textId) => {
65
+ const node = refs[id]!
66
+ const meta = { ...node.meta }
67
+ for (const field of TEXT_FIELDS) {
68
+ if (typeof meta[field] !== 'string' || !meta[field]) continue
69
+ const key = `${SCHEMA_LOCALE_PREFIX}${textId}.${field}`
70
+ const translated = t(key)
71
+ if (translated !== key) meta[field] = translated
72
+ }
73
+ refs[id] = { ...node, meta }
74
+ })
75
+ return { uid: document.uid, refs }
76
+ }
@@ -0,0 +1,210 @@
1
+ import * as React from 'react'
2
+ import { DatabaseIcon, InfoIcon, PaletteIcon, SaveIcon, ServerCogIcon, SlidersHorizontalIcon, Undo2Icon, type LucideIcon } from 'lucide-react'
3
+ import { Alert, AlertDescription, Button, cn, Spinner } from '@chatluna/ui-kit'
4
+ import { apiErrorMessage, callAction, toast, useLive } from '@chatluna/ui-client/client'
5
+ import { SchemaForm, useFormValidation } from '@chatluna/ui-schema-form/client'
6
+ import type { SchemaDocument, SchemaDocumentNode } from '@chatluna/schema'
7
+ import { useT } from '../i18n'
8
+ import { OverlayScrollbar } from '../shared/overlay-scrollbar'
9
+ import { AboutPanel } from './about'
10
+ import { settingsDraft, useSettingsDraft } from './draft'
11
+ import { localizeSchema } from './localize'
12
+ import './settings.css'
13
+
14
+ type Values = Record<string, unknown>
15
+
16
+ interface SettingsSection {
17
+ title: string
18
+ description?: string
19
+ /** 只含本分区的 schema 文档:根节点换成分区节点,并去掉分区自身的标题,免得表单里再渲染一遍。 */
20
+ schema: SchemaDocument
21
+ /** 本分区覆盖的顶层配置键,用来标出有未保存修改的分区。 */
22
+ keys: string[]
23
+ }
24
+
25
+ /** 分区图标按分区的第一个配置键选取;新分区没有登记时用通用图标。 */
26
+ const SECTION_ICONS: Record<string, LucideIcon> = {
27
+ sandboxAccentColor: PaletteIcon,
28
+ persistenceMode: DatabaseIcon,
29
+ testEndpoint: ServerCogIcon,
30
+ }
31
+
32
+ const sameValues = (left: unknown, right: unknown) => JSON.stringify(left) === JSON.stringify(right)
33
+
34
+ const textOf = (value: unknown) => typeof value === 'string' && value ? value : undefined
35
+
36
+ function keysOf(refs: SchemaDocument['refs'], id: number): string[] {
37
+ const node = refs[id]
38
+ if (node?.type === 'intersect') return (node.list ?? []).flatMap((child) => keysOf(refs, child))
39
+ return Object.keys(node?.dict ?? {})
40
+ }
41
+
42
+ /**
43
+ * 把 `Settings`(`src/index.ts`)的顶层 intersect 拆成分区。根节点不是 intersect 时整份 schema 作为一个分区,
44
+ * 页面仍能渲染。
45
+ */
46
+ function sectionsOf(document: SchemaDocument, fallbackTitle: string): SettingsSection[] {
47
+ const root = document.refs[document.uid]
48
+ const ids = root?.type === 'intersect' ? root.list ?? [] : [document.uid]
49
+ return ids.map((id) => {
50
+ const node = document.refs[id]!
51
+ const { label, description, ...meta } = node.meta ?? {}
52
+ const stripped: SchemaDocumentNode = { ...node, meta }
53
+ return {
54
+ title: textOf(label) ?? textOf(description) ?? fallbackTitle,
55
+ description: textOf(label) ? textOf(description) : undefined,
56
+ schema: { uid: id, refs: { ...document.refs, [id]: stripped } },
57
+ keys: keysOf(document.refs, id),
58
+ }
59
+ })
60
+ }
61
+
62
+ /**
63
+ * 插件设置页。读写的是 chatluna.yml 里本插件条目的配置(`src/settings.ts`),v2 插件页不再显示这些字段。
64
+ * 版式与环境管理页一致:左侧分区导航,右侧面板只显示当前分区;保存与撤销作用于全部分区。
65
+ * 导航末尾固定有「关于」(`about.tsx`),它不来自 schema,选中时面板换成插件信息。
66
+ *
67
+ * 各分区的表单都挂载着(未选中的隐藏),共用同一个校验,保存时不会漏掉看不见的分区。
68
+ *
69
+ * 草稿存在 `draft.ts`,离开设置页再回来不会丢;页头的「设置」标签据此标出未保存修改。
70
+ *
71
+ * 保存后插件按新配置重启,实时 state 要等重启完成才推来新值;这段时间里表单先显示刚保存的值,
72
+ * 文件戳一变再换成服务端读回的值,避免先闪回旧值。
73
+ */
74
+ export function SettingsPage() {
75
+ const t = useT()
76
+ const live = useLive('onebot-sandbox.settings')
77
+ const validation = useFormValidation()
78
+ const draft = useSettingsDraft().value
79
+ const setDraft = settingsDraft.setValue
80
+ const [pending, setPending] = React.useState<{ revision: string, value: Values }>()
81
+ const [saving, setSaving] = React.useState(false)
82
+ const [active, setActive] = React.useState<number | 'about'>(0)
83
+ const scroller = React.useRef<HTMLDivElement>(null)
84
+ const data = live.data
85
+ const saved = pending && pending.revision === data?.revision ? pending.value : data?.value
86
+ const value = draft ?? saved ?? {}
87
+ const dirty = draft !== undefined && !sameValues(draft, saved)
88
+ // 只在已读到保存值时同步:加载期间 saved 为空,不能把留下的草稿误判成已改动或未改动。
89
+ React.useEffect(() => {
90
+ if (data) settingsDraft.setDirty(dirty)
91
+ }, [data, dirty])
92
+ const fallbackTitle = t('sandbox.nav.settings')
93
+ const sections = React.useMemo(() => data ? sectionsOf(localizeSchema(data.schema, t), fallbackTitle) : [], [data, t, fallbackTitle])
94
+ const about = active === 'about'
95
+ const current = about ? undefined : sections[Math.min(active, sections.length - 1)]
96
+ const panelTitle = about ? t('sandbox.settings.about.title') : current?.title
97
+ const panelDescription = about ? t('sandbox.settings.about.description') : current?.description
98
+
99
+ const discard = () => {
100
+ setDraft(undefined)
101
+ validation.reset()
102
+ }
103
+
104
+ const save = async () => {
105
+ if (!data || !draft) return
106
+ if (!validation.attempt()) {
107
+ toast.error(t('sandbox.settings.invalid'))
108
+ return
109
+ }
110
+ setSaving(true)
111
+ try {
112
+ await callAction('onebot-sandbox.settings.save', { config: draft, revision: data.revision })
113
+ setPending({ revision: data.revision, value: draft })
114
+ setDraft(undefined)
115
+ validation.reset()
116
+ toast.success(t('sandbox.settings.saved'))
117
+ } catch (error) {
118
+ toast.error(t('sandbox.settings.saveFailed'), { description: apiErrorMessage(error) })
119
+ } finally {
120
+ setSaving(false)
121
+ }
122
+ }
123
+
124
+ const select = (index: number | 'about') => {
125
+ setActive(index)
126
+ scroller.current?.scrollTo({ top: 0 })
127
+ }
128
+
129
+ return (
130
+ <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-hidden px-6 pt-3 pb-6 text-sm leading-5">
131
+ <div className="flex shrink-0 flex-wrap items-center justify-between gap-3">
132
+ <p className="min-w-0 text-xs text-muted-foreground">{t('sandbox.settings.description')}</p>
133
+ <div className="flex items-center gap-2">
134
+ {dirty && <span className="text-xs text-muted-foreground">{t('sandbox.settings.unsaved')}</span>}
135
+ <Button variant="outline" size="sm" disabled={!dirty || saving} onClick={discard}>
136
+ <Undo2Icon />{t('sandbox.settings.discard')}
137
+ </Button>
138
+ <Button size="sm" disabled={!dirty || saving} onClick={() => void save()}>
139
+ {saving ? <Spinner /> : <SaveIcon />}{t(saving ? 'sandbox.common.saving' : 'sandbox.common.save')}
140
+ </Button>
141
+ </div>
142
+ </div>
143
+ {data && sections.length ? (
144
+ <div className="grid min-h-0 w-full max-w-5xl flex-1 grid-rows-[auto_minmax(0,1fr)] gap-4 md:grid-cols-[14rem_minmax(0,1fr)] md:grid-rows-1">
145
+ <nav aria-label={t('sandbox.settings.sections')} className="sandbox-scroll-strip flex gap-0.5 self-start overflow-x-auto rounded-xl p-1.5 ring-1 ring-border md:flex-col md:overflow-x-visible">
146
+ {sections.map((section, index) => {
147
+ const Icon = SECTION_ICONS[section.keys[0] ?? ''] ?? SlidersHorizontalIcon
148
+ const changed = draft !== undefined && section.keys.some((key) => !sameValues(draft[key], saved?.[key]))
149
+ return (
150
+ <Button
151
+ key={index}
152
+ variant="ghost"
153
+ className={cn('shrink-0 justify-start md:w-full', section === current && 'bg-accent text-accent-foreground')}
154
+ aria-current={section === current ? 'page' : undefined}
155
+ onClick={() => select(index)}
156
+ >
157
+ <Icon />
158
+ <span>{section.title}</span>
159
+ {changed && <span role="img" aria-label={t('sandbox.settings.unsaved')} className="ml-auto size-1.5 shrink-0 rounded-full bg-primary" />}
160
+ </Button>
161
+ )
162
+ })}
163
+ <div role="separator" className="mx-1 shrink-0 bg-border max-md:w-px md:my-1 md:h-px" />
164
+ <Button
165
+ variant="ghost"
166
+ className={cn('shrink-0 justify-start md:w-full', about && 'bg-accent text-accent-foreground')}
167
+ aria-current={about ? 'page' : undefined}
168
+ onClick={() => select('about')}
169
+ >
170
+ <InfoIcon />
171
+ <span>{t('sandbox.settings.about.title')}</span>
172
+ </Button>
173
+ </nav>
174
+ <section aria-label={panelTitle} className="flex min-h-0 flex-col overflow-hidden rounded-xl ring-1 ring-border">
175
+ <header className="flex shrink-0 flex-col gap-0.5 border-b border-border px-4 py-3">
176
+ <h2 className="text-[13px] font-medium">{panelTitle}</h2>
177
+ {panelDescription && <p className="text-[11px] text-muted-foreground">{panelDescription}</p>}
178
+ </header>
179
+ <div className="relative flex min-h-0 flex-1 flex-col">
180
+ <div ref={scroller} className="sandbox-settings-scroller min-h-0 flex-1 overflow-y-auto">
181
+ <fieldset disabled={saving} className="sandbox-settings-form min-w-0 px-5 py-4">
182
+ {sections.map((section, index) => (
183
+ <div key={index} hidden={section !== current}>
184
+ <SchemaForm
185
+ schema={section.schema}
186
+ value={value}
187
+ initial={saved}
188
+ onChange={setDraft}
189
+ validation={validation}
190
+ formId="onebot-sandbox.settings"
191
+ />
192
+ </div>
193
+ ))}
194
+ {about && <AboutPanel />}
195
+ </fieldset>
196
+ </div>
197
+ <OverlayScrollbar target={scroller} className="sandbox-settings-scrollbar" />
198
+ </div>
199
+ </section>
200
+ </div>
201
+ ) : live.error ? (
202
+ <Alert variant="destructive" className="max-w-5xl">
203
+ <AlertDescription>{t('sandbox.settings.unavailableDetail', { error: live.error })}</AlertDescription>
204
+ </Alert>
205
+ ) : (
206
+ <div className="flex justify-center py-12 text-muted-foreground"><Spinner /></div>
207
+ )}
208
+ </div>
209
+ )
210
+ }
@@ -0,0 +1,47 @@
1
+ /*
2
+ * 设置页(settings/page.tsx)。只按 sandbox-settings- 类名选择,SchemaForm 的结构钩子用它自带的
3
+ * data-schema-group。本文件不分层,压过 SchemaForm 在宿主 utilities 层里的工具类。
4
+ */
5
+
6
+ /* 原生滚动条隐藏,由 .sandbox-settings-scrollbar 自绘(SCROLLBAR.md)。 */
7
+ .sandbox-settings-scroller {
8
+ scrollbar-width: none;
9
+ }
10
+
11
+ /* 轨道上端紧贴面板标题栏下方,下端让出面板的 12px 圆角。 */
12
+ .sandbox-settings-scrollbar {
13
+ top: 4px;
14
+ bottom: 12px;
15
+ }
16
+
17
+ /*
18
+ * 分区内的小节(MCP 端点、调用配额等)。SchemaForm 把小节标题和字段标题都渲染成 16px 粗体,
19
+ * 混在一起分不清层级;小节标题改成小号弱化色,小节之间加分隔线。
20
+ */
21
+ .sandbox-settings-form [data-schema-group] > h3 {
22
+ font-size: 12px;
23
+ line-height: 16px;
24
+ font-weight: 600;
25
+ color: var(--muted-foreground);
26
+ }
27
+
28
+ /* 不是第一个的小节加分隔线;第一个小节紧贴面板标题栏,不再加线。嵌套 intersect 的小节在内层 fieldset 里。 */
29
+ .sandbox-settings-form fieldset > [data-schema-group]:not(:first-child),
30
+ .sandbox-settings-form fieldset:not(:first-child) > [data-schema-group]:first-child {
31
+ border-top: 1px solid var(--border);
32
+ padding-top: 20px;
33
+ }
34
+
35
+ /*
36
+ * SchemaForm 的字段标题是 16px(text-base)、说明是 14px(text-sm),比沙盒其他页面的 13px 正文大一档;
37
+ * 设置页嵌在沙盒里,统一压到 13px 标题、12px 说明。小节标题由上面的规则单独处理(选择器更具体)。
38
+ */
39
+ .sandbox-settings-form .text-base {
40
+ font-size: 13px;
41
+ line-height: 20px;
42
+ }
43
+
44
+ .sandbox-settings-form .text-sm.text-muted-foreground {
45
+ font-size: 12px;
46
+ line-height: 18px;
47
+ }
@@ -0,0 +1,35 @@
1
+ import * as React from 'react'
2
+ import { BotIcon, UsersIcon } from 'lucide-react'
3
+ import { cn } from '@chatluna/ui-kit'
4
+
5
+ export type AvatarKind = 'user' | 'bot' | 'group'
6
+
7
+ /**
8
+ * 沙盒身份头像:图片可用时显示图片,否则显示名字首字。机器人与群组在角上带一个类型标记,
9
+ * 让同名的用户、机器人与群在列表里一眼可分。`sandbox-media://` 引用必须先由调用方解析成 Data URL。
10
+ */
11
+ export function SandboxAvatar({ kind, name = '', src, className, badge = true }: {
12
+ kind: AvatarKind
13
+ name?: string
14
+ src?: string
15
+ className?: string
16
+ badge?: boolean
17
+ }) {
18
+ const [failed, setFailed] = React.useState(false)
19
+ const usable = src && !src.startsWith('sandbox-media:') ? src : ''
20
+ React.useEffect(() => setFailed(false), [usable])
21
+ return (
22
+ <span className={cn('relative inline-flex size-9 shrink-0 select-none', className)}>
23
+ <span className="flex size-full items-center justify-center overflow-hidden rounded-full bg-muted text-[13px] font-medium text-muted-foreground">
24
+ {usable && !failed
25
+ ? <img src={usable} alt={name} className="size-full object-cover" onError={() => setFailed(true)} />
26
+ : name.trim().slice(0, 1).toUpperCase() || '?'}
27
+ </span>
28
+ {badge && kind !== 'user' && (
29
+ <span className="absolute -right-0.5 -bottom-0.5 flex size-3.5 items-center justify-center rounded-full bg-background ring-1 ring-border">
30
+ {kind === 'bot' ? <BotIcon className="size-2.5 text-primary" /> : <UsersIcon className="size-2.5 text-muted-foreground" />}
31
+ </span>
32
+ )}
33
+ </span>
34
+ )
35
+ }
@@ -0,0 +1,44 @@
1
+ import {
2
+ AlertDialog,
3
+ AlertDialogAction,
4
+ AlertDialogCancel,
5
+ AlertDialogContent,
6
+ AlertDialogDescription,
7
+ AlertDialogFooter,
8
+ AlertDialogHeader,
9
+ AlertDialogTitle,
10
+ } from '@chatluna/ui-kit'
11
+ import { useT } from '../i18n'
12
+ import { useRetained } from './use-retained'
13
+
14
+ export interface ConfirmRequest {
15
+ title: string
16
+ description: string
17
+ action: string
18
+ onConfirm(): void
19
+ }
20
+
21
+ /**
22
+ * 不可撤销操作(删除公告、删除测试空间)的确认框。`request` 为空时关闭;关闭后保留最后一份文案播完退场动画。
23
+ */
24
+ export function ConfirmDialog({ request, onClose }: { request?: ConfirmRequest, onClose(): void }) {
25
+ const t = useT()
26
+ const shown = useRetained(request)
27
+ return (
28
+ <AlertDialog open={!!request} onOpenChange={(open) => !open && onClose()}>
29
+ <AlertDialogContent>
30
+ <AlertDialogHeader>
31
+ <AlertDialogTitle>{shown?.title}</AlertDialogTitle>
32
+ <AlertDialogDescription>{shown?.description}</AlertDialogDescription>
33
+ </AlertDialogHeader>
34
+ <AlertDialogFooter>
35
+ <AlertDialogCancel>{t('sandbox.common.cancel')}</AlertDialogCancel>
36
+ <AlertDialogAction variant="destructive" onClick={() => {
37
+ onClose()
38
+ request?.onConfirm()
39
+ }}>{shown?.action}</AlertDialogAction>
40
+ </AlertDialogFooter>
41
+ </AlertDialogContent>
42
+ </AlertDialog>
43
+ )
44
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * 沙盒内的表单控件细节。只按 sandbox- 类名选择,不波及 v2 宿主的其他页面。
3
+ *
4
+ * 搜索框:Chrome 给 type="search" 画一个原生清除按钮,Firefox 没有;消息搜索还自带一个清除按钮,
5
+ * Chrome 下会出现两个。统一去掉原生按钮,两种浏览器表现一致。
6
+ */
7
+ .sandbox-search-input::-webkit-search-cancel-button {
8
+ appearance: none;
9
+ }
10
+
11
+ /*
12
+ * 窄屏时横向滑动的一排标签(页头分段标签、环境管理的分区导航):30px 高的标签条里冒出原生横向
13
+ * 滚动条会压住标签。隐藏滚动条,仍可用触控板横滑、滚轮与方向键切换(选中项会滚入视野)。
14
+ */
15
+ .sandbox-scroll-strip {
16
+ scrollbar-width: none;
17
+ }
@@ -0,0 +1,45 @@
1
+ import * as React from 'react'
2
+ import {
3
+ AlertDialog,
4
+ AlertDialogAction,
5
+ AlertDialogCancel,
6
+ AlertDialogContent,
7
+ AlertDialogDescription,
8
+ AlertDialogFooter,
9
+ AlertDialogHeader,
10
+ AlertDialogTitle,
11
+ } from '@chatluna/ui-kit'
12
+ import { useT } from '../i18n'
13
+
14
+ /**
15
+ * 有未保存修改的弹窗:关闭按钮、取消、Esc 与点遮罩都先确认放弃,没有改动直接关闭。
16
+ *
17
+ * 返回的 `confirm` 要渲染在弹窗的 `DialogContent` 里面:嵌在同一棵 Base UI 弹窗树中,
18
+ * Esc 与点外部只作用于最上层的确认框,不会同时再触发一次底下弹窗的关闭请求。
19
+ */
20
+ export function useDiscardConfirm(dirty: boolean, close: () => void) {
21
+ const t = useT()
22
+ const [asking, setAsking] = React.useState(false)
23
+ const requestClose = () => {
24
+ if (dirty) setAsking(true)
25
+ else close()
26
+ }
27
+ const confirm = (
28
+ <AlertDialog open={asking} onOpenChange={setAsking}>
29
+ <AlertDialogContent>
30
+ <AlertDialogHeader>
31
+ <AlertDialogTitle>{t('sandbox.discard.title')}</AlertDialogTitle>
32
+ <AlertDialogDescription>{t('sandbox.discard.description')}</AlertDialogDescription>
33
+ </AlertDialogHeader>
34
+ <AlertDialogFooter>
35
+ <AlertDialogCancel>{t('sandbox.discard.keep')}</AlertDialogCancel>
36
+ <AlertDialogAction variant="destructive" onClick={() => {
37
+ setAsking(false)
38
+ close()
39
+ }}>{t('sandbox.discard.confirm')}</AlertDialogAction>
40
+ </AlertDialogFooter>
41
+ </AlertDialogContent>
42
+ </AlertDialog>
43
+ )
44
+ return { requestClose, confirm }
45
+ }
@@ -0,0 +1,40 @@
1
+ export interface FloatingPanelAnchor {
2
+ x: number
3
+ y: number
4
+ }
5
+
6
+ export interface FloatingPanelViewport {
7
+ width: number
8
+ height: number
9
+ }
10
+
11
+ const panelGap = 8
12
+ const viewportPadding = 12
13
+
14
+ export function getFloatingPanelPosition(
15
+ anchor: FloatingPanelAnchor,
16
+ viewport: FloatingPanelViewport,
17
+ panel: { width: number, height: number },
18
+ ): FloatingPanelAnchor {
19
+ const width = Math.min(panel.width, Math.max(0, viewport.width - viewportPadding * 2))
20
+ const height = Math.min(panel.height, Math.max(0, viewport.height - viewportPadding * 2))
21
+ return {
22
+ x: Math.max(viewportPadding, Math.min(anchor.x + panelGap, viewport.width - width - viewportPadding)),
23
+ y: Math.max(viewportPadding, Math.min(anchor.y + panelGap, viewport.height - height - viewportPadding)),
24
+ }
25
+ }
26
+
27
+ export function clampFloatingPanelPosition(
28
+ position: FloatingPanelAnchor,
29
+ viewport: FloatingPanelViewport,
30
+ panel: { width: number, height: number },
31
+ ): FloatingPanelAnchor {
32
+ return {
33
+ x: Math.max(viewportPadding, Math.min(position.x, viewport.width - panel.width - viewportPadding)),
34
+ y: Math.max(viewportPadding, Math.min(position.y, viewport.height - panel.height - viewportPadding)),
35
+ }
36
+ }
37
+
38
+ export function isFloatingPanelInteractiveTarget(target: EventTarget | null): boolean {
39
+ return typeof Element !== 'undefined' && target instanceof Element && !!target.closest('button, input, textarea, select, [role="button"], a')
40
+ }
@@ -0,0 +1,22 @@
1
+ export function formatDuration(durationMs: number): string {
2
+ if (!Number.isFinite(durationMs) || durationMs < 0) return '—'
3
+ if (durationMs < 1000) return `${Math.round(durationMs)} ms`
4
+ if (durationMs < 60_000) {
5
+ const seconds = durationMs / 1000
6
+ return `${formatDurationNumber(seconds, seconds < 10 ? 2 : 1)} s`
7
+ }
8
+
9
+ const totalSeconds = Math.round(durationMs / 1000)
10
+ const hours = Math.floor(totalSeconds / 3600)
11
+ const minutes = Math.floor((totalSeconds % 3600) / 60)
12
+ const seconds = totalSeconds % 60
13
+ const parts: string[] = []
14
+ if (hours) parts.push(`${hours} h`)
15
+ if (minutes) parts.push(`${minutes} min`)
16
+ if (seconds || !parts.length) parts.push(`${seconds} s`)
17
+ return parts.join(' ')
18
+ }
19
+
20
+ function formatDurationNumber(value: number, digits: number): string {
21
+ return String(Number(value.toFixed(digits)))
22
+ }