chatluna-plugin-onebot-sandbox 1.0.1 → 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/README.md +1 -0
- package/client/chat/composer.tsx +4 -2
- package/client/environment/entity-dialog.css +4 -0
- package/client/environment/entity-dialog.tsx +72 -3
- package/client/locales/en.yml +11 -0
- package/client/locales/zh.yml +11 -0
- package/client/workspace/media-cache.ts +38 -0
- package/client/workspace/store.ts +28 -9
- package/dist/.vite/manifest.json +1 -1
- package/dist/app-SGdVOART.js +85 -0
- package/dist/client.css +1 -1
- package/dist/client.js +1 -1
- package/lib/bot.d.ts +4 -1
- package/lib/bot.d.ts.map +1 -1
- package/lib/channel-assignee.d.ts +4 -3
- package/lib/channel-assignee.d.ts.map +1 -1
- package/lib/chatluna/conversation-lines.d.ts +41 -27
- package/lib/chatluna/conversation-lines.d.ts.map +1 -1
- package/lib/chatluna/model-calls.d.ts +68 -0
- package/lib/chatluna/model-calls.d.ts.map +1 -0
- package/lib/chatluna/presets.d.ts +142 -0
- package/lib/chatluna/presets.d.ts.map +1 -0
- package/lib/chatluna/runtime.d.ts +14 -0
- package/lib/chatluna/runtime.d.ts.map +1 -1
- package/lib/chatluna/state.d.ts +13 -4
- package/lib/chatluna/state.d.ts.map +1 -1
- package/lib/chatluna/wakeup.d.ts +2 -2
- package/lib/chatluna/wakeup.d.ts.map +1 -1
- package/lib/console-contract.d.ts +21 -1
- package/lib/console-contract.d.ts.map +1 -1
- package/lib/console.d.ts +2 -1
- package/lib/console.d.ts.map +1 -1
- package/lib/control-service.d.ts +10 -0
- package/lib/control-service.d.ts.map +1 -1
- package/lib/conversation-resolution.d.ts +17 -0
- package/lib/conversation-resolution.d.ts.map +1 -1
- package/lib/inbound-delivery.d.ts +17 -8
- package/lib/inbound-delivery.d.ts.map +1 -1
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1244 -212
- package/lib/index.js.map +1 -1
- package/lib/mcp/http-api.d.ts.map +1 -1
- package/lib/mcp/service.d.ts +11 -1
- package/lib/mcp/service.d.ts.map +1 -1
- package/lib/mcp/tool-registry.d.ts +6 -0
- package/lib/mcp/tool-registry.d.ts.map +1 -1
- package/lib/onebot-profiles.d.ts +8 -0
- package/lib/onebot-profiles.d.ts.map +1 -1
- package/lib/test-spaces.d.ts.map +1 -1
- package/lib/webui.d.ts.map +1 -1
- package/package.json +5 -1
- package/dist/app-sK3yIFCF.js +0 -85
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ ChatLuna v2 插件:在 WebUI 中模拟 QQ(OneBot v11)环境,不接入真
|
|
|
12
12
|
- **AI 测试空间**:相互隔离的独立场景,可供 AI 通过 MCP 自行操作与观察。
|
|
13
13
|
- **测试端点**:提供 MCP 与 HTTP 两种接口,带凭证与调用配额,供自动化脚本驱动沙盒;默认关闭。
|
|
14
14
|
- **回复出处**:机器人的回复可跳转到 ChatLuna 的 trace 页,查看对应的模型请求。
|
|
15
|
+
- **模型调用与测试预设**:测试端点可读取机器人每一步模型调用的请求与响应,新建与修改测试用预设,并把沙盒机器人切换到指定预设;已有预设只有在设置中列为可编辑的才能修改。
|
|
15
16
|
|
|
16
17
|
## 安装
|
|
17
18
|
|
package/client/chat/composer.tsx
CHANGED
|
@@ -386,8 +386,10 @@ export const Composer = React.forwardRef<ComposerHandle, {
|
|
|
386
386
|
setCompactStack(element.clientWidth < fullWidth + COMPOSER_FIXED_WIDTH + COMPOSER_MIN_INPUT_WIDTH)
|
|
387
387
|
// 回复与附件共用一个浮动包络,消息区底部留白按包络真实高度计算。28 = 胶囊距底 20px + 置底时
|
|
388
388
|
// 末条消息与输入区的可视间隙 8px;包络与胶囊之间的 8px 已计入包络高度。
|
|
389
|
-
|
|
390
|
-
|
|
389
|
+
// 量布局高度(offsetHeight)而不是 getBoundingClientRect:进出测试空间的缩放动画期间工作区带 scale,
|
|
390
|
+
// 量到的是缩小后的高度,动画结束时布局尺寸不变、ResizeObserver 不再触发,留白就一直偏小。
|
|
391
|
+
const formHeight = element.offsetHeight
|
|
392
|
+
const overlay = context.current ? context.current.offsetHeight + 8 : 0
|
|
391
393
|
onSpaceChange?.(formHeight + overlay + 28)
|
|
392
394
|
}
|
|
393
395
|
const observer = new ResizeObserver(update)
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
SelectValue,
|
|
21
21
|
Switch,
|
|
22
22
|
} from '@chatluna/ui-kit'
|
|
23
|
+
import type { SandboxBotPresetCatalog } from '../../src/console-contract'
|
|
23
24
|
import { getOneBotProfileBaseline } from '../../src/onebot-profiles'
|
|
24
25
|
import {
|
|
25
26
|
getSandboxBots,
|
|
@@ -34,7 +35,7 @@ import { SandboxAvatar } from '../shared/avatar'
|
|
|
34
35
|
import { useDiscardConfirm } from '../shared/discard-confirm'
|
|
35
36
|
import { OverlayScrollbar } from '../shared/overlay-scrollbar'
|
|
36
37
|
import { useSandbox, useWorkspace } from '../workspace/context'
|
|
37
|
-
import { errorText } from '../workspace/store'
|
|
38
|
+
import { errorText, invoke } from '../workspace/store'
|
|
38
39
|
import { AvatarChoices } from './avatar-picker'
|
|
39
40
|
import { ImplementationSelect } from './create-popover'
|
|
40
41
|
import './entity-dialog.css'
|
|
@@ -56,6 +57,15 @@ interface Draft {
|
|
|
56
57
|
members: SandboxGroupMember[]
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
/** 「跟随路由规则」的选项值;预设 ID 由 v2 按文件名生成,不含冒号,不会与它撞上。 */
|
|
61
|
+
const FOLLOW_ROUTE = 'onebot-sandbox:follow-route'
|
|
62
|
+
|
|
63
|
+
/** 机器人的预设选项:未读取、服务未加载、读取失败或读到的目录。 */
|
|
64
|
+
type PresetState =
|
|
65
|
+
| { status: 'loading' }
|
|
66
|
+
| { status: 'failed'; error: string }
|
|
67
|
+
| { status: 'ready'; catalog: SandboxBotPresetCatalog }
|
|
68
|
+
|
|
59
69
|
const EMPTY_DRAFT: Draft = {
|
|
60
70
|
id: '', name: '', avatar: '', personalNote: '', sex: 'unset', implementation: 'napcat', enabled: true, disabledCapabilities: [], members: [],
|
|
61
71
|
}
|
|
@@ -78,6 +88,10 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
78
88
|
const [picking, setPicking] = React.useState(false)
|
|
79
89
|
const [capabilitySearch, setCapabilitySearch] = React.useState('')
|
|
80
90
|
const [editingMemberId, setEditingMemberId] = React.useState('')
|
|
91
|
+
// 预设不在场景快照里,打开时另外读取,所以不进 Draft:读到之前两份都是 FOLLOW_ROUTE,选单禁用。
|
|
92
|
+
const [presetState, setPresetState] = React.useState<PresetState>({ status: 'loading' })
|
|
93
|
+
const [preset, setPreset] = React.useState(FOLLOW_ROUTE)
|
|
94
|
+
const [initialPreset, setInitialPreset] = React.useState(FOLLOW_ROUTE)
|
|
81
95
|
const scroller = React.useRef<HTMLDivElement>(null)
|
|
82
96
|
const capabilityScroller = React.useRef<HTMLDivElement>(null)
|
|
83
97
|
const users = getSandboxUsers(snapshot)
|
|
@@ -94,7 +108,7 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
94
108
|
setDraft(next)
|
|
95
109
|
setInitial(next)
|
|
96
110
|
}
|
|
97
|
-
const dirty = mode === 'edit' && JSON.stringify(draft) !== JSON.stringify(initial)
|
|
111
|
+
const dirty = mode === 'edit' && (JSON.stringify(draft) !== JSON.stringify(initial) || preset !== initialPreset)
|
|
98
112
|
const { requestClose, confirm } = useDiscardConfirm(dirty, () => onOpenChange(false))
|
|
99
113
|
|
|
100
114
|
React.useEffect(() => {
|
|
@@ -127,11 +141,32 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
127
141
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
142
|
}, [open, target?.type, target?.id])
|
|
129
143
|
|
|
130
|
-
|
|
144
|
+
React.useEffect(() => {
|
|
145
|
+
setPreset(FOLLOW_ROUTE)
|
|
146
|
+
setInitialPreset(FOLLOW_ROUTE)
|
|
147
|
+
if (!open || mode !== 'edit' || target?.type !== 'bot') return
|
|
148
|
+
const botId = target.id
|
|
149
|
+
let active = true
|
|
150
|
+
setPresetState({ status: 'loading' })
|
|
151
|
+
invoke('onebot-sandbox.bot-presets', undefined).then((catalog) => {
|
|
152
|
+
if (!active) return
|
|
153
|
+
const assigned = catalog.assignments.find((assignment) => assignment.botId === botId)?.presetId ?? FOLLOW_ROUTE
|
|
154
|
+
setPresetState({ status: 'ready', catalog })
|
|
155
|
+
setPreset(assigned)
|
|
156
|
+
setInitialPreset(assigned)
|
|
157
|
+
}, (failure) => {
|
|
158
|
+
if (active) setPresetState({ status: 'failed', error: errorText(failure) })
|
|
159
|
+
})
|
|
160
|
+
return () => { active = false }
|
|
161
|
+
}, [open, mode, target?.type, target?.id])
|
|
162
|
+
|
|
163
|
+
/** `prepare` 先于环境变更执行:预设依赖外部服务,更容易失败,失败时整次保存都不落地。 */
|
|
164
|
+
const runAction = async (input: ManageSandboxEnvironmentInput, prepare?: () => Promise<unknown>) => {
|
|
131
165
|
if (busy) return
|
|
132
166
|
setBusy(true)
|
|
133
167
|
setError('')
|
|
134
168
|
try {
|
|
169
|
+
await prepare?.()
|
|
135
170
|
await store.manageEnvironment(input)
|
|
136
171
|
onOpenChange(false)
|
|
137
172
|
} catch (failure) {
|
|
@@ -165,6 +200,13 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
165
200
|
avatar: draft.avatar,
|
|
166
201
|
profile: accountProfile(),
|
|
167
202
|
},
|
|
203
|
+
}, preset === initialPreset ? undefined : async () => {
|
|
204
|
+
await invoke('onebot-sandbox.set-bot-preset', {
|
|
205
|
+
botId: draft.id,
|
|
206
|
+
presetId: preset === FOLLOW_ROUTE ? null : preset,
|
|
207
|
+
...(store.spaceId ? { spaceId: store.spaceId } : {}),
|
|
208
|
+
})
|
|
209
|
+
setInitialPreset(preset)
|
|
168
210
|
})
|
|
169
211
|
} else if (target?.type === 'group') {
|
|
170
212
|
void runAction({ action: 'update-group', data: { id: draft.id, name: draft.name, avatar: draft.avatar, members: draft.members.map((member) => ({ ...member })) } })
|
|
@@ -207,6 +249,23 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
207
249
|
const entityLabel = t(`sandbox.kind.${type}`)
|
|
208
250
|
const editingParticipant = participants.find(({ id }) => id === editingMemberId)
|
|
209
251
|
const editingMember = memberOf(editingMemberId)
|
|
252
|
+
const presetItems = presetState.status === 'ready' ? [
|
|
253
|
+
{ value: FOLLOW_ROUTE, label: t('sandbox.entity.presetFollowRoute') },
|
|
254
|
+
...presetState.catalog.presets.map(({ id, name, status }) => {
|
|
255
|
+
const label = name === id ? id : `${name}(${id})`
|
|
256
|
+
return { value: id, label: status === 'error' ? t('sandbox.entity.presetError', { name: label }) : label }
|
|
257
|
+
}),
|
|
258
|
+
// 规则指向的预设已被删掉时仍列出并标明,否则选单显示空白,也无从看出该恢复默认。
|
|
259
|
+
...(preset !== FOLLOW_ROUTE && !presetState.catalog.presets.some(({ id }) => id === preset)
|
|
260
|
+
? [{ value: preset, label: t('sandbox.entity.presetMissing', { id: preset }) }]
|
|
261
|
+
: []),
|
|
262
|
+
] : [{ value: FOLLOW_ROUTE, label: t('sandbox.entity.presetFollowRoute') }]
|
|
263
|
+
const presetReady = presetState.status === 'ready' && presetState.catalog.available
|
|
264
|
+
const presetHint = presetState.status === 'loading'
|
|
265
|
+
? t('sandbox.entity.presetLoading')
|
|
266
|
+
: presetState.status === 'failed'
|
|
267
|
+
? t('sandbox.entity.presetFailed', { error: presetState.error })
|
|
268
|
+
: presetState.catalog.available ? t('sandbox.entity.presetHint') : t('sandbox.entity.presetUnavailable')
|
|
210
269
|
const roleItems = [
|
|
211
270
|
{ value: 'owner', label: t('sandbox.role.owner') },
|
|
212
271
|
{ value: 'admin', label: t('sandbox.role.admin') },
|
|
@@ -291,6 +350,16 @@ export function EntityDialog({ open, mode, target, onOpenChange }: {
|
|
|
291
350
|
<Switch checked={draft.enabled} onCheckedChange={(value) => setDraft((current) => ({ ...current, enabled: value === true }))} />
|
|
292
351
|
</label>
|
|
293
352
|
<ImplementationSelect value={draft.implementation} onChange={(implementation) => setDraft((value) => ({ ...value, implementation }))} />
|
|
353
|
+
<div className="sandbox-entity-field">
|
|
354
|
+
<Label>{t('sandbox.entity.preset')}</Label>
|
|
355
|
+
<Select value={preset} disabled={!presetReady} onValueChange={(value) => value && setPreset(value)} items={presetItems}>
|
|
356
|
+
<SelectTrigger className="w-full"><SelectValue /></SelectTrigger>
|
|
357
|
+
<SelectContent className="sandbox-glass">
|
|
358
|
+
{presetItems.map(({ value, label }) => <SelectItem key={value} value={value}>{label}</SelectItem>)}
|
|
359
|
+
</SelectContent>
|
|
360
|
+
</Select>
|
|
361
|
+
<p className={cn('sandbox-entity-hint', presetState.status === 'failed' && 'is-error')}>{presetHint}</p>
|
|
362
|
+
</div>
|
|
294
363
|
<div className="sandbox-entity-field">
|
|
295
364
|
<div className="sandbox-entity-section-title">
|
|
296
365
|
<Label>{t('sandbox.entity.capabilities')}</Label>
|
package/client/locales/en.yml
CHANGED
|
@@ -186,6 +186,14 @@ sandbox.entity.native: "Native"
|
|
|
186
186
|
sandbox.entity.needOwner: "A group needs at least one member as owner."
|
|
187
187
|
sandbox.entity.noCapability: "No matching capability"
|
|
188
188
|
sandbox.entity.noCard: "No card"
|
|
189
|
+
sandbox.entity.preset: "ChatLuna preset"
|
|
190
|
+
sandbox.entity.presetError: "{name} (failed to load)"
|
|
191
|
+
sandbox.entity.presetFailed: "Failed to read presets: {error}"
|
|
192
|
+
sandbox.entity.presetFollowRoute: "Follow routing rules"
|
|
193
|
+
sandbox.entity.presetHint: "Saved as a ChatLuna routing rule that matches only this QQ ID, shared by bots with the same ID in the main environment and every test space; more specific routing rules (per channel, for example) still take precedence. The switch applies from the next message and keeps the conversation history."
|
|
194
|
+
sandbox.entity.presetLoading: "Reading presets…"
|
|
195
|
+
sandbox.entity.presetMissing: "{id} (preset no longer exists)"
|
|
196
|
+
sandbox.entity.presetUnavailable: "The ChatLuna preset service is not loaded; the preset cannot be switched."
|
|
189
197
|
sandbox.entity.removeMember: "Remove {name} ({id})"
|
|
190
198
|
sandbox.entity.role: "Role"
|
|
191
199
|
sandbox.entity.section.basic: "Basic info"
|
|
@@ -483,6 +491,9 @@ sandbox.settings.schema.persistenceMode.description: "Database mode persists acr
|
|
|
483
491
|
sandbox.settings.schema.persistenceMode.label: "Storage mode"
|
|
484
492
|
sandbox.settings.schema.persistenceMode.option.database.description: "Database"
|
|
485
493
|
sandbox.settings.schema.persistenceMode.option.memory.description: "Server memory"
|
|
494
|
+
sandbox.settings.schema.presetEditing.description: "Preset editing"
|
|
495
|
+
sandbox.settings.schema.presetEditing.editablePresets.description: "Besides test presets with the `sandbox-test-` prefix, test tools may modify these presets; they can be modified but not deleted"
|
|
496
|
+
sandbox.settings.schema.presetEditing.editablePresets.label: "Editable presets"
|
|
486
497
|
sandbox.settings.schema.sandboxAccentColor.description: "Color of own bubbles, the composer and selected states. Does not follow the ChatLuna theme color"
|
|
487
498
|
sandbox.settings.schema.sandboxAccentColor.label: "Accent color"
|
|
488
499
|
sandbox.settings.schema.sandboxIncomingBubbleStyle.description: "Shadow uses a translucent white background with a drop shadow; outline uses a white background with a thin border. Applies only to the light theme"
|
package/client/locales/zh.yml
CHANGED
|
@@ -186,6 +186,14 @@ sandbox.entity.native: "原生扩展"
|
|
|
186
186
|
sandbox.entity.needOwner: "群组至少需要一位成员作为群主。"
|
|
187
187
|
sandbox.entity.noCapability: "无匹配能力"
|
|
188
188
|
sandbox.entity.noCard: "未设置群昵称"
|
|
189
|
+
sandbox.entity.preset: "ChatLuna 预设"
|
|
190
|
+
sandbox.entity.presetError: "{name}(加载出错)"
|
|
191
|
+
sandbox.entity.presetFailed: "预设列表读取失败:{error}"
|
|
192
|
+
sandbox.entity.presetFollowRoute: "跟随路由规则"
|
|
193
|
+
sandbox.entity.presetHint: "写成只命中这个 QQ 号的 ChatLuna 路由规则,主环境与各测试空间里同号的机器人共用;按频道等写得更具体的路由规则仍会盖过它。切换后从下一条消息起生效,会话历史保留。"
|
|
194
|
+
sandbox.entity.presetLoading: "正在读取预设列表…"
|
|
195
|
+
sandbox.entity.presetMissing: "{id}(预设已不存在)"
|
|
196
|
+
sandbox.entity.presetUnavailable: "ChatLuna 的预设服务没有加载,预设不可切换。"
|
|
189
197
|
sandbox.entity.removeMember: "移除 {name}({id})"
|
|
190
198
|
sandbox.entity.role: "群身份"
|
|
191
199
|
sandbox.entity.section.basic: "基本资料"
|
|
@@ -483,6 +491,9 @@ sandbox.settings.schema.persistenceMode.description: "数据库模式在重启
|
|
|
483
491
|
sandbox.settings.schema.persistenceMode.label: "存储方式"
|
|
484
492
|
sandbox.settings.schema.persistenceMode.option.database.description: "数据库"
|
|
485
493
|
sandbox.settings.schema.persistenceMode.option.memory.description: "服务端内存"
|
|
494
|
+
sandbox.settings.schema.presetEditing.description: "预设编辑"
|
|
495
|
+
sandbox.settings.schema.presetEditing.editablePresets.description: "测试工具除了`sandbox-test-`前缀的测试预设,还可以修改这些预设;只能修改,不能删除"
|
|
496
|
+
sandbox.settings.schema.presetEditing.editablePresets.label: "可编辑预设"
|
|
486
497
|
sandbox.settings.schema.sandboxAccentColor.description: "己方气泡、发送框与选中态使用的颜色,不跟随 ChatLuna 主题色"
|
|
487
498
|
sandbox.settings.schema.sandboxAccentColor.label: "强调色"
|
|
488
499
|
sandbox.settings.schema.sandboxIncomingBubbleStyle.description: "投影为半透明白底加阴影,描边为白底加细边框;仅在亮色主题下生效"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 媒体 Data URL 的页面级缓存,各空间的 `WorkspaceStore` 与每次挂载共用。
|
|
3
|
+
*
|
|
4
|
+
* store 跟着沙盒页面挂载、卸载,缓存放在 store 里时,从别的页面回来每个头像都要重新请求一遍,
|
|
5
|
+
* 先闪名字首字再出图片。媒体 ID 是内容的 sha256(`src/media-storage.ts`),同一个 ID 的内容不变,
|
|
6
|
+
* 所以缓存不会过期,插件重启、切换空间与操作者都照用。只在内存里,刷新页面后重新拉取。
|
|
7
|
+
*
|
|
8
|
+
* 头像是几 KB 的 SVG,数量有限,长期保留;消息里的图片可能有几 MB,只保留最近用到的
|
|
9
|
+
* {@link MESSAGE_MEDIA_LIMIT} 张,按最近使用淘汰。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const MESSAGE_MEDIA_LIMIT = 64
|
|
13
|
+
|
|
14
|
+
const avatars = new Map<string, string>()
|
|
15
|
+
/** Map 的插入顺序即使用顺序:命中时删掉重插,淘汰时取第一项。 */
|
|
16
|
+
const messageMedia = new Map<string, string>()
|
|
17
|
+
|
|
18
|
+
export function readCachedMedia(id: string): string | undefined {
|
|
19
|
+
const avatar = avatars.get(id)
|
|
20
|
+
if (avatar !== undefined) return avatar
|
|
21
|
+
const url = messageMedia.get(id)
|
|
22
|
+
if (url === undefined) return undefined
|
|
23
|
+
messageMedia.delete(id)
|
|
24
|
+
messageMedia.set(id, url)
|
|
25
|
+
return url
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function rememberMedia(id: string, url: string, avatar: boolean) {
|
|
29
|
+
if (avatar) {
|
|
30
|
+
avatars.set(id, url)
|
|
31
|
+
messageMedia.delete(id)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
if (avatars.has(id)) return
|
|
35
|
+
messageMedia.delete(id)
|
|
36
|
+
messageMedia.set(id, url)
|
|
37
|
+
while (messageMedia.size > MESSAGE_MEDIA_LIMIT) messageMedia.delete(messageMedia.keys().next().value!)
|
|
38
|
+
}
|
|
@@ -15,6 +15,7 @@ import type {
|
|
|
15
15
|
SandboxSnapshot,
|
|
16
16
|
SandboxWorkspaceState,
|
|
17
17
|
} from '../../src/types'
|
|
18
|
+
import { readCachedMedia, rememberMedia } from './media-cache'
|
|
18
19
|
import { resolveWorkspaceSelection, type SandboxWorkspaceSelection } from './state'
|
|
19
20
|
|
|
20
21
|
type Endpoint = keyof SandboxConsoleEvents
|
|
@@ -140,11 +141,18 @@ export class WorkspaceStore {
|
|
|
140
141
|
currentOperatorId: preferred.currentOperatorId ?? this.view.currentOperatorId,
|
|
141
142
|
activeConversationId: preferred.activeConversationId ?? this.view.activeConversationId,
|
|
142
143
|
})
|
|
144
|
+
// 页面级缓存里已有的媒体随快照同一次提交,首帧就画出头像,不先闪名字首字。
|
|
145
|
+
const { avatarIds, messageMediaIds } = visibleMediaIdsOf(workspace.snapshot)
|
|
146
|
+
const cached = [...avatarIds, ...messageMediaIds].flatMap((id) => {
|
|
147
|
+
const url = this.view.media[id] ? undefined : readCachedMedia(id)
|
|
148
|
+
return url ? [[id, url] as const] : []
|
|
149
|
+
})
|
|
143
150
|
this.set({
|
|
144
151
|
workspace,
|
|
145
152
|
currentOperatorId: selection.currentOperatorId,
|
|
146
153
|
activeConversationId: selection.activeConversationId,
|
|
147
154
|
loadError: undefined,
|
|
155
|
+
...(cached.length ? { media: { ...this.view.media, ...Object.fromEntries(cached) } } : {}),
|
|
148
156
|
})
|
|
149
157
|
if (persist) this.saveSelection()
|
|
150
158
|
void this.loadVisibleMedia()
|
|
@@ -347,24 +355,24 @@ export class WorkspaceStore {
|
|
|
347
355
|
return id ? this.view.media[id] ?? '' : reference ?? ''
|
|
348
356
|
}
|
|
349
357
|
|
|
350
|
-
/**
|
|
358
|
+
/**
|
|
359
|
+
* 头像与消息媒体都是受控引用;按需拉取 Base64 并缓存为 Data URL,失败只标记不重试风暴。
|
|
360
|
+
* 拉到的内容同时写进页面级缓存(`media-cache.ts`),离开沙盒页面再回来不必重新请求。
|
|
361
|
+
*/
|
|
351
362
|
async loadVisibleMedia() {
|
|
352
363
|
const snapshot = this.snapshot
|
|
353
364
|
const operatorId = this.view.currentOperatorId
|
|
354
365
|
if (!snapshot || !operatorId) return
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
...(snapshot.forwards ?? []).flatMap(({ nodes }) => nodes.flatMap(({ media }) => media?.map(({ id }) => id) ?? [])),
|
|
358
|
-
...[...snapshot.participants.map(({ avatar }) => avatar), ...snapshot.groups.map(({ avatar }) => avatar)]
|
|
359
|
-
.flatMap((reference) => mediaIdOf(reference) ?? []),
|
|
360
|
-
]
|
|
361
|
-
const missing = [...new Set(ids)].filter((id) => !this.view.media[id] && !this.mediaLoading.has(id))
|
|
366
|
+
const { avatarIds, messageMediaIds } = visibleMediaIdsOf(snapshot)
|
|
367
|
+
const missing = [...new Set([...avatarIds, ...messageMediaIds])].filter((id) => !this.view.media[id] && !this.mediaLoading.has(id))
|
|
362
368
|
await Promise.all(missing.map(async (mediaId) => {
|
|
363
369
|
this.mediaLoading.add(mediaId)
|
|
364
370
|
try {
|
|
365
371
|
const content = await invoke('onebot-sandbox.media-content', this.scoped({ operatorId, mediaId }))
|
|
372
|
+
const url = `data:${content.mimeType};base64,${content.dataBase64}`
|
|
373
|
+
rememberMedia(mediaId, url, avatarIds.has(mediaId))
|
|
366
374
|
const { [mediaId]: _, ...failures } = this.view.mediaFailures
|
|
367
|
-
this.set({ media: { ...this.view.media, [mediaId]:
|
|
375
|
+
this.set({ media: { ...this.view.media, [mediaId]: url }, mediaFailures: failures })
|
|
368
376
|
} catch {
|
|
369
377
|
this.set({ mediaFailures: { ...this.view.mediaFailures, [mediaId]: true } })
|
|
370
378
|
} finally {
|
|
@@ -374,6 +382,17 @@ export class WorkspaceStore {
|
|
|
374
382
|
}
|
|
375
383
|
}
|
|
376
384
|
|
|
385
|
+
/** 快照里引用到的媒体,分成头像(参与者与群)和消息媒体(含合并转发节点),两类的缓存策略不同。 */
|
|
386
|
+
function visibleMediaIdsOf(snapshot: SandboxSnapshot) {
|
|
387
|
+
const avatarIds = new Set([...snapshot.participants.map(({ avatar }) => avatar), ...snapshot.groups.map(({ avatar }) => avatar)]
|
|
388
|
+
.flatMap((reference) => mediaIdOf(reference) ?? []))
|
|
389
|
+
const messageMediaIds = new Set([
|
|
390
|
+
...snapshot.messages.flatMap(({ media }) => media?.map(({ id }) => id) ?? []),
|
|
391
|
+
...(snapshot.forwards ?? []).flatMap(({ nodes }) => nodes.flatMap(({ media }) => media?.map(({ id }) => id) ?? [])),
|
|
392
|
+
])
|
|
393
|
+
return { avatarIds, messageMediaIds }
|
|
394
|
+
}
|
|
395
|
+
|
|
377
396
|
/** 当前操作者可见的全部会话(根会话与实例),与侧栏、选择恢复同一口径。 */
|
|
378
397
|
export function visibleConversationsOf(snapshot: SandboxSnapshot, operatorId: string | undefined): ResolvedConversation[] {
|
|
379
398
|
if (!operatorId) return []
|