chatluna-plugin-onebot-sandbox 1.0.2 → 1.1.5

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 (68) hide show
  1. package/README.md +1 -0
  2. package/client/chat/composer.tsx +8 -3
  3. package/client/chat/message-list.tsx +6 -0
  4. package/client/environment/avatar-picker.tsx +2 -5
  5. package/client/environment/entity-dialog.css +9 -0
  6. package/client/environment/entity-dialog.tsx +113 -6
  7. package/client/locales/en.yml +18 -0
  8. package/client/locales/zh.yml +18 -0
  9. package/client/settings/localize.ts +2 -2
  10. package/client/settings/page.tsx +2 -2
  11. package/client/workspace/media-cache.ts +38 -0
  12. package/client/workspace/store.ts +57 -21
  13. package/dist/.vite/manifest.json +1 -1
  14. package/dist/app-CpJLSY0S.js +85 -0
  15. package/dist/client.css +1 -1
  16. package/dist/client.js +1 -1
  17. package/lib/bot.d.ts +4 -1
  18. package/lib/bot.d.ts.map +1 -1
  19. package/lib/builtin-avatar-options.d.ts.map +1 -1
  20. package/lib/builtin-avatar-png.d.ts +15 -0
  21. package/lib/builtin-avatar-png.d.ts.map +1 -0
  22. package/lib/builtin-avatars.d.ts +1 -1
  23. package/lib/builtin-avatars.d.ts.map +1 -1
  24. package/lib/channel-assignee.d.ts +4 -3
  25. package/lib/channel-assignee.d.ts.map +1 -1
  26. package/lib/chatluna/conversation-lines.d.ts +41 -27
  27. package/lib/chatluna/conversation-lines.d.ts.map +1 -1
  28. package/lib/chatluna/model-calls.d.ts +68 -0
  29. package/lib/chatluna/model-calls.d.ts.map +1 -0
  30. package/lib/chatluna/presets.d.ts +142 -0
  31. package/lib/chatluna/presets.d.ts.map +1 -0
  32. package/lib/chatluna/runtime.d.ts +14 -0
  33. package/lib/chatluna/runtime.d.ts.map +1 -1
  34. package/lib/chatluna/state.d.ts +13 -4
  35. package/lib/chatluna/state.d.ts.map +1 -1
  36. package/lib/chatluna/user-permissions.d.ts +15 -0
  37. package/lib/chatluna/user-permissions.d.ts.map +1 -0
  38. package/lib/chatluna/wakeup.d.ts +2 -2
  39. package/lib/chatluna/wakeup.d.ts.map +1 -1
  40. package/lib/console-contract.d.ts +21 -1
  41. package/lib/console-contract.d.ts.map +1 -1
  42. package/lib/console.d.ts +2 -1
  43. package/lib/console.d.ts.map +1 -1
  44. package/lib/control-service.d.ts +16 -6
  45. package/lib/control-service.d.ts.map +1 -1
  46. package/lib/conversation-resolution.d.ts +17 -0
  47. package/lib/conversation-resolution.d.ts.map +1 -1
  48. package/lib/inbound-delivery.d.ts +17 -8
  49. package/lib/inbound-delivery.d.ts.map +1 -1
  50. package/lib/index.d.ts +5 -0
  51. package/lib/index.d.ts.map +1 -1
  52. package/lib/index.js +1628 -294
  53. package/lib/index.js.map +1 -1
  54. package/lib/mcp/http-api.d.ts.map +1 -1
  55. package/lib/mcp/service.d.ts +14 -1
  56. package/lib/mcp/service.d.ts.map +1 -1
  57. package/lib/mcp/tool-registry.d.ts +7 -0
  58. package/lib/mcp/tool-registry.d.ts.map +1 -1
  59. package/lib/onebot-profiles.d.ts +8 -0
  60. package/lib/onebot-profiles.d.ts.map +1 -1
  61. package/lib/test-spaces.d.ts.map +1 -1
  62. package/lib/types.d.ts +7 -1
  63. package/lib/types.d.ts.map +1 -1
  64. package/lib/user-authority.d.ts +6 -0
  65. package/lib/user-authority.d.ts.map +1 -0
  66. package/lib/webui.d.ts.map +1 -1
  67. package/package.json +10 -2
  68. package/dist/app-bHKIhF6g.js +0 -85
@@ -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
@@ -84,6 +85,7 @@ export interface WorkspaceView {
84
85
  export class WorkspaceStore {
85
86
  private view: WorkspaceView = { media: {}, mediaFailures: {} }
86
87
  private listeners = new Set<() => void>()
88
+ private loadTask: Promise<void> | undefined
87
89
  private refreshTask: Promise<void> | undefined
88
90
  private pendingRefresh = false
89
91
  private mediaLoading = new Set<string>()
@@ -136,15 +138,23 @@ export class WorkspaceStore {
136
138
  }
137
139
 
138
140
  private replaceWorkspace(workspace: SandboxWorkspaceState, preferred: SandboxWorkspaceSelection = {}, persist = true) {
141
+ if (this.disposed) return
139
142
  const selection = resolveWorkspaceSelection(workspace.snapshot, {
140
143
  currentOperatorId: preferred.currentOperatorId ?? this.view.currentOperatorId,
141
144
  activeConversationId: preferred.activeConversationId ?? this.view.activeConversationId,
142
145
  })
146
+ // 页面级缓存里已有的媒体随快照同一次提交,首帧就画出头像,不先闪名字首字。
147
+ const { avatarIds, messageMediaIds } = visibleMediaIdsOf(workspace.snapshot)
148
+ const cached = [...avatarIds, ...messageMediaIds].flatMap((id) => {
149
+ const url = this.view.media[id] ? undefined : readCachedMedia(id)
150
+ return url ? [[id, url] as const] : []
151
+ })
143
152
  this.set({
144
153
  workspace,
145
154
  currentOperatorId: selection.currentOperatorId,
146
155
  activeConversationId: selection.activeConversationId,
147
156
  loadError: undefined,
157
+ ...(cached.length ? { media: { ...this.view.media, ...Object.fromEntries(cached) } } : {}),
148
158
  })
149
159
  if (persist) this.saveSelection()
150
160
  void this.loadVisibleMedia()
@@ -158,20 +168,28 @@ export class WorkspaceStore {
158
168
  this.replaceWorkspace(workspace, readSelection(this.spaceId), false)
159
169
  }
160
170
 
161
- async load() {
171
+ load() {
172
+ if (this.loadTask) return this.loadTask
162
173
  const stored = readSelection(this.spaceId)
163
- try {
164
- let workspace: SandboxWorkspaceState
174
+ const operatorId = this.view.currentOperatorId
175
+ this.loadTask = (async () => {
165
176
  try {
166
- workspace = await invoke('onebot-sandbox.workspace', this.scoped(stored.currentOperatorId ? { operatorId: stored.currentOperatorId } : {}))
167
- } catch {
168
- // 本地记住的操作者可能已被删除;退回默认视角再解析一次选择。
169
- workspace = await invoke('onebot-sandbox.workspace', this.scoped({}))
177
+ let workspace: SandboxWorkspaceState
178
+ try {
179
+ workspace = await invoke('onebot-sandbox.workspace', this.scoped(stored.currentOperatorId ? { operatorId: stored.currentOperatorId } : {}))
180
+ } catch {
181
+ // 本地记住的操作者可能已被删除;退回默认视角再解析一次选择。
182
+ workspace = await invoke('onebot-sandbox.workspace', this.scoped({}))
183
+ }
184
+ if (operatorId !== this.view.currentOperatorId) return
185
+ this.replaceWorkspace(workspace, stored)
186
+ } catch (error) {
187
+ this.set({ loadError: errorText(error, translate('sandbox.error.loadWorkspace')) })
188
+ } finally {
189
+ this.loadTask = undefined
170
190
  }
171
- this.replaceWorkspace(workspace, stored)
172
- } catch (error) {
173
- this.set({ loadError: errorText(error, translate('sandbox.error.loadWorkspace')) })
174
- }
191
+ })()
192
+ return this.loadTask
175
193
  }
176
194
 
177
195
  /** 场景变更广播只是失效信号;多次信号合并成串行的若干次读取,不会并发打满。 */
@@ -181,10 +199,18 @@ export class WorkspaceStore {
181
199
  this.refreshTask = (async () => {
182
200
  try {
183
201
  while (this.pendingRefresh && !this.disposed) {
202
+ // 初次加载会恢复本地操作者;提前请求会按默认用户裁剪,随后覆盖掉当前用户的私聊。
203
+ await this.loadTask
204
+ if (this.disposed) break
184
205
  this.pendingRefresh = false
206
+ if (!this.view.workspace) continue
185
207
  const operatorId = this.view.currentOperatorId
186
208
  try {
187
209
  const workspace = await invoke('onebot-sandbox.workspace', this.scoped(operatorId ? { operatorId } : {}))
210
+ if (operatorId !== this.view.currentOperatorId) {
211
+ this.pendingRefresh = true
212
+ continue
213
+ }
188
214
  if (workspace.snapshot.revision >= (this.snapshot?.revision ?? 0)) this.replaceWorkspace(workspace)
189
215
  } catch {
190
216
  // 刷新失败保留当前画面,下一次信号会再试。
@@ -203,7 +229,6 @@ export class WorkspaceStore {
203
229
 
204
230
  async selectOperator(participantId: string) {
205
231
  const workspace = await invoke('onebot-sandbox.workspace', this.scoped({ operatorId: participantId }))
206
- this.set({ currentOperatorId: participantId })
207
232
  this.replaceWorkspace(workspace, { currentOperatorId: participantId })
208
233
  }
209
234
 
@@ -347,24 +372,24 @@ export class WorkspaceStore {
347
372
  return id ? this.view.media[id] ?? '' : reference ?? ''
348
373
  }
349
374
 
350
- /** 头像与消息媒体都是受控引用;按需拉取 Base64 并缓存为 Data URL,失败只标记不重试风暴。 */
375
+ /**
376
+ * 头像与消息媒体都是受控引用;按需拉取 Base64 并缓存为 Data URL,失败只标记不重试风暴。
377
+ * 拉到的内容同时写进页面级缓存(`media-cache.ts`),离开沙盒页面再回来不必重新请求。
378
+ */
351
379
  async loadVisibleMedia() {
352
380
  const snapshot = this.snapshot
353
381
  const operatorId = this.view.currentOperatorId
354
382
  if (!snapshot || !operatorId) return
355
- const ids = [
356
- ...snapshot.messages.flatMap(({ media }) => media?.map(({ id }) => id) ?? []),
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))
383
+ const { avatarIds, messageMediaIds } = visibleMediaIdsOf(snapshot)
384
+ const missing = [...new Set([...avatarIds, ...messageMediaIds])].filter((id) => !this.view.media[id] && !this.mediaLoading.has(id))
362
385
  await Promise.all(missing.map(async (mediaId) => {
363
386
  this.mediaLoading.add(mediaId)
364
387
  try {
365
388
  const content = await invoke('onebot-sandbox.media-content', this.scoped({ operatorId, mediaId }))
389
+ const url = `data:${content.mimeType};base64,${content.dataBase64}`
390
+ rememberMedia(mediaId, url, avatarIds.has(mediaId))
366
391
  const { [mediaId]: _, ...failures } = this.view.mediaFailures
367
- this.set({ media: { ...this.view.media, [mediaId]: `data:${content.mimeType};base64,${content.dataBase64}` }, mediaFailures: failures })
392
+ this.set({ media: { ...this.view.media, [mediaId]: url }, mediaFailures: failures })
368
393
  } catch {
369
394
  this.set({ mediaFailures: { ...this.view.mediaFailures, [mediaId]: true } })
370
395
  } finally {
@@ -374,6 +399,17 @@ export class WorkspaceStore {
374
399
  }
375
400
  }
376
401
 
402
+ /** 快照里引用到的媒体,分成头像(参与者与群)和消息媒体(含合并转发节点),两类的缓存策略不同。 */
403
+ function visibleMediaIdsOf(snapshot: SandboxSnapshot) {
404
+ const avatarIds = new Set([...snapshot.participants.map(({ avatar }) => avatar), ...snapshot.groups.map(({ avatar }) => avatar)]
405
+ .flatMap((reference) => mediaIdOf(reference) ?? []))
406
+ const messageMediaIds = new Set([
407
+ ...snapshot.messages.flatMap(({ media }) => media?.map(({ id }) => id) ?? []),
408
+ ...(snapshot.forwards ?? []).flatMap(({ nodes }) => nodes.flatMap(({ media }) => media?.map(({ id }) => id) ?? [])),
409
+ ])
410
+ return { avatarIds, messageMediaIds }
411
+ }
412
+
377
413
  /** 当前操作者可见的全部会话(根会话与实例),与侧栏、选择恢复同一口径。 */
378
414
  export function visibleConversationsOf(snapshot: SandboxSnapshot, operatorId: string | undefined): ResolvedConversation[] {
379
415
  if (!operatorId) return []
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "client/app.tsx": {
3
- "file": "app-bHKIhF6g.js",
3
+ "file": "app-CpJLSY0S.js",
4
4
  "name": "app",
5
5
  "src": "client/app.tsx",
6
6
  "isDynamicEntry": true