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.
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/client/app.tsx +306 -0
- package/client/chat/chat-pane.tsx +303 -0
- package/client/chat/composer.css +635 -0
- package/client/chat/composer.tsx +646 -0
- package/client/chat/details-panel.tsx +196 -0
- package/client/chat/emoji-picker.tsx +70 -0
- package/client/chat/extension-mark.tsx +16 -0
- package/client/chat/forward.tsx +257 -0
- package/client/chat/image-preview.tsx +93 -0
- package/client/chat/member-menu.tsx +64 -0
- package/client/chat/message-list.tsx +617 -0
- package/client/chat/messages.css +1018 -0
- package/client/chat/notifications.tsx +83 -0
- package/client/chat/reactions.tsx +52 -0
- package/client/chat/search-panel.tsx +231 -0
- package/client/chat/sidebar.tsx +424 -0
- package/client/chat/webqq-view.tsx +71 -0
- package/client/environment/avatar-picker.tsx +92 -0
- package/client/environment/create-popover.tsx +228 -0
- package/client/environment/directory-model.ts +63 -0
- package/client/environment/entity-dialog.css +383 -0
- package/client/environment/entity-dialog.tsx +442 -0
- package/client/environment/page.css +14 -0
- package/client/environment/page.tsx +280 -0
- package/client/i18n.ts +19 -0
- package/client/index.ts +17 -0
- package/client/locales/en.yml +617 -0
- package/client/locales/zh.yml +617 -0
- package/client/mcp/capability-catalog.tsx +277 -0
- package/client/mcp/credential-manager.tsx +257 -0
- package/client/onebot-debug/page.tsx +329 -0
- package/client/records/record-layout.tsx +429 -0
- package/client/records/records.css +259 -0
- package/client/settings/about.tsx +43 -0
- package/client/settings/draft.ts +47 -0
- package/client/settings/localize.ts +76 -0
- package/client/settings/page.tsx +210 -0
- package/client/settings/settings.css +47 -0
- package/client/shared/avatar.tsx +35 -0
- package/client/shared/confirm-dialog.tsx +44 -0
- package/client/shared/controls.css +17 -0
- package/client/shared/discard-confirm.tsx +45 -0
- package/client/shared/floating-panel.ts +40 -0
- package/client/shared/format-duration.ts +22 -0
- package/client/shared/format-time.ts +49 -0
- package/client/shared/glass.css +150 -0
- package/client/shared/implementation.ts +12 -0
- package/client/shared/overlay-scrollbar.css +45 -0
- package/client/shared/overlay-scrollbar.tsx +123 -0
- package/client/shared/use-element-width.ts +19 -0
- package/client/shared/use-retained.ts +12 -0
- package/client/test-call/page.tsx +260 -0
- package/client/test-space/agent-visuals.tsx +121 -0
- package/client/test-space/observe-overlay.tsx +42 -0
- package/client/test-space/overview.tsx +267 -0
- package/client/test-space/spaces.css +201 -0
- package/client/test-space/thumbnail-capture.ts +63 -0
- package/client/test-space/thumbnail-scale.ts +21 -0
- package/client/test-space/use-space-list.ts +35 -0
- package/client/test-space/zoom.ts +197 -0
- package/client/webqq/composer-draft-host.ts +578 -0
- package/client/webqq/composer-draft.ts +81 -0
- package/client/webqq/conversation-tree.ts +155 -0
- package/client/webqq/emoji-catalog.ts +78 -0
- package/client/webqq/fork-boundary.ts +28 -0
- package/client/webqq/forward-cluster.ts +20 -0
- package/client/webqq/forward-dialog-stack.ts +74 -0
- package/client/webqq/forward-preview.ts +40 -0
- package/client/webqq/friend-menu.ts +17 -0
- package/client/webqq/group-display.ts +18 -0
- package/client/webqq/group-menu.ts +25 -0
- package/client/webqq/mention.ts +4 -0
- package/client/webqq/message-capabilities.ts +37 -0
- package/client/webqq/message-cluster.ts +68 -0
- package/client/webqq/message-presentation.ts +197 -0
- package/client/webqq/message-reveal.ts +31 -0
- package/client/webqq/message-search-date.ts +40 -0
- package/client/webqq/message-selection.ts +85 -0
- package/client/webqq/notification-requests.ts +20 -0
- package/client/webqq/participant-presentation.ts +128 -0
- package/client/webqq/profile-card.ts +199 -0
- package/client/webqq/relationship-directory.ts +62 -0
- package/client/webqq/thinking-panel.ts +7 -0
- package/client/webqq/user-stack.ts +53 -0
- package/client/workspace/agent-overlay-effect.ts +238 -0
- package/client/workspace/context.tsx +117 -0
- package/client/workspace/overlay-host.tsx +299 -0
- package/client/workspace/profile-card.css +164 -0
- package/client/workspace/state.ts +28 -0
- package/client/workspace/store.ts +388 -0
- package/client/workspace/use-test-spaces.ts +21 -0
- package/dist/.vite/manifest.json +21 -0
- package/dist/app-63rfTh6w.js +85 -0
- package/dist/client.css +4 -0
- package/dist/client.js +1 -0
- package/lib/account-profile.d.ts +17 -0
- package/lib/account-profile.d.ts.map +1 -0
- package/lib/base-dir.d.ts +4 -0
- package/lib/base-dir.d.ts.map +1 -0
- package/lib/bot.d.ts +185 -0
- package/lib/bot.d.ts.map +1 -0
- package/lib/builtin-avatar-options.d.ts +11 -0
- package/lib/builtin-avatar-options.d.ts.map +1 -0
- package/lib/builtin-avatars.d.ts +6 -0
- package/lib/builtin-avatars.d.ts.map +1 -0
- package/lib/channel-assignee.d.ts +52 -0
- package/lib/channel-assignee.d.ts.map +1 -0
- package/lib/chatluna/conversation-lines.d.ts +46 -0
- package/lib/chatluna/conversation-lines.d.ts.map +1 -0
- package/lib/chatluna/runtime.d.ts +26 -0
- package/lib/chatluna/runtime.d.ts.map +1 -0
- package/lib/chatluna/state.d.ts +67 -0
- package/lib/chatluna/state.d.ts.map +1 -0
- package/lib/chatluna/thinking.d.ts +10 -0
- package/lib/chatluna/thinking.d.ts.map +1 -0
- package/lib/chatluna/tools.d.ts +36 -0
- package/lib/chatluna/tools.d.ts.map +1 -0
- package/lib/chatluna/wakeup.d.ts +143 -0
- package/lib/chatluna/wakeup.d.ts.map +1 -0
- package/lib/console-contract.d.ts +131 -0
- package/lib/console-contract.d.ts.map +1 -0
- package/lib/console.d.ts +31 -0
- package/lib/console.d.ts.map +1 -0
- package/lib/control-service.d.ts +425 -0
- package/lib/control-service.d.ts.map +1 -0
- package/lib/conversation-resolution.d.ts +157 -0
- package/lib/conversation-resolution.d.ts.map +1 -0
- package/lib/error-detail.d.ts +3 -0
- package/lib/error-detail.d.ts.map +1 -0
- package/lib/inbound-delivery.d.ts +156 -0
- package/lib/inbound-delivery.d.ts.map +1 -0
- package/lib/index.d.ts +76 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +12834 -0
- package/lib/index.js.map +1 -0
- package/lib/lifecycle.d.ts +8 -0
- package/lib/lifecycle.d.ts.map +1 -0
- package/lib/mcp/arguments.d.ts +71 -0
- package/lib/mcp/arguments.d.ts.map +1 -0
- package/lib/mcp/call-records.d.ts +32 -0
- package/lib/mcp/call-records.d.ts.map +1 -0
- package/lib/mcp/http-api.d.ts +147 -0
- package/lib/mcp/http-api.d.ts.map +1 -0
- package/lib/mcp/page-cursor.d.ts +37 -0
- package/lib/mcp/page-cursor.d.ts.map +1 -0
- package/lib/mcp/server.d.ts +60 -0
- package/lib/mcp/server.d.ts.map +1 -0
- package/lib/mcp/service.d.ts +219 -0
- package/lib/mcp/service.d.ts.map +1 -0
- package/lib/mcp/tool-registry.d.ts +113 -0
- package/lib/mcp/tool-registry.d.ts.map +1 -0
- package/lib/mcp/types.d.ts +144 -0
- package/lib/mcp/types.d.ts.map +1 -0
- package/lib/media-storage.d.ts +33 -0
- package/lib/media-storage.d.ts.map +1 -0
- package/lib/message-capabilities.d.ts +78 -0
- package/lib/message-capabilities.d.ts.map +1 -0
- package/lib/onebot-debug.d.ts +115 -0
- package/lib/onebot-debug.d.ts.map +1 -0
- package/lib/onebot-message.d.ts +23 -0
- package/lib/onebot-message.d.ts.map +1 -0
- package/lib/onebot-profiles.d.ts +45 -0
- package/lib/onebot-profiles.d.ts.map +1 -0
- package/lib/persistence.d.ts +148 -0
- package/lib/persistence.d.ts.map +1 -0
- package/lib/random.d.ts +5 -0
- package/lib/random.d.ts.map +1 -0
- package/lib/record-store.d.ts +80 -0
- package/lib/record-store.d.ts.map +1 -0
- package/lib/relationship-actions.d.ts +157 -0
- package/lib/relationship-actions.d.ts.map +1 -0
- package/lib/scope-directory.d.ts +105 -0
- package/lib/scope-directory.d.ts.map +1 -0
- package/lib/settings.d.ts +33 -0
- package/lib/settings.d.ts.map +1 -0
- package/lib/test-spaces.d.ts +69 -0
- package/lib/test-spaces.d.ts.map +1 -0
- package/lib/types.d.ts +739 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/webui.d.ts +26 -0
- package/lib/webui.d.ts.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SandboxAccountProfile,
|
|
3
|
+
SandboxBotProfile,
|
|
4
|
+
SandboxGroup,
|
|
5
|
+
SandboxGroupMember,
|
|
6
|
+
SandboxParticipant,
|
|
7
|
+
SandboxSnapshot,
|
|
8
|
+
} from '../../src/types'
|
|
9
|
+
import { isSandboxBot } from '../../src/types'
|
|
10
|
+
import { implementationLabel } from '../shared/implementation'
|
|
11
|
+
|
|
12
|
+
/** 字段名与取值都经插件文案翻译;纯逻辑模块不依赖 React,由调用方传入 `t`。 */
|
|
13
|
+
export type ProfileCardTranslate = (key: string, params?: Record<string, unknown>) => string
|
|
14
|
+
|
|
15
|
+
export type ProfileCardFieldGroup = 'account' | 'friendship' | 'group-member' | 'bot-runtime' | 'group'
|
|
16
|
+
|
|
17
|
+
export interface ProfileCardField {
|
|
18
|
+
group: ProfileCardFieldGroup
|
|
19
|
+
label: string
|
|
20
|
+
value: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ProfileCardModel {
|
|
24
|
+
participantId: string
|
|
25
|
+
name: string
|
|
26
|
+
avatar?: string
|
|
27
|
+
avatarKind: 'user' | 'bot' | 'group'
|
|
28
|
+
/** 「QQ」或「群号」,已按当前语言翻译。 */
|
|
29
|
+
identityLabel: string
|
|
30
|
+
/** 个性签名单独展示在英雄区,不混入字段分组。 */
|
|
31
|
+
personalNote?: string
|
|
32
|
+
fields: ProfileCardField[]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getProfileCardGroupLabel(t: ProfileCardTranslate, group: ProfileCardFieldGroup): string {
|
|
36
|
+
return t(`sandbox.profileCard.group.${group}`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function formatSex(t: ProfileCardTranslate, value: SandboxAccountProfile['sex']): string | undefined {
|
|
40
|
+
if (value) return t(`sandbox.sex.${value}`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function yesNo(t: ProfileCardTranslate, value: boolean) {
|
|
44
|
+
return t(value ? 'sandbox.profileCard.yes' : 'sandbox.profileCard.no')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function pushField(fields: ProfileCardField[], group: ProfileCardFieldGroup, label: string, value: string | number | boolean | undefined) {
|
|
48
|
+
if (value === undefined || value === '') return
|
|
49
|
+
fields.push({ group, label, value: String(value) })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getAccountFields(t: ProfileCardTranslate, profile: SandboxAccountProfile | undefined): ProfileCardField[] {
|
|
53
|
+
if (!profile) return []
|
|
54
|
+
const fields: ProfileCardField[] = []
|
|
55
|
+
const label = (name: string) => t(`sandbox.profileCard.field.${name}`)
|
|
56
|
+
pushField(fields, 'account', label('sex'), formatSex(t, profile.sex))
|
|
57
|
+
pushField(fields, 'account', label('age'), profile.age)
|
|
58
|
+
pushField(fields, 'account', 'QID', profile.qid)
|
|
59
|
+
pushField(fields, 'account', label('level'), profile.level)
|
|
60
|
+
pushField(fields, 'account', label('loginDays'), profile.loginDays)
|
|
61
|
+
pushField(fields, 'account', label('regTime'), profile.regTime)
|
|
62
|
+
pushField(fields, 'account', label('city'), profile.city)
|
|
63
|
+
pushField(fields, 'account', label('country'), profile.country)
|
|
64
|
+
if (profile.birthdayYear !== undefined || profile.birthdayMonth !== undefined || profile.birthdayDay !== undefined) {
|
|
65
|
+
const parts = [
|
|
66
|
+
profile.birthdayYear,
|
|
67
|
+
profile.birthdayMonth,
|
|
68
|
+
profile.birthdayDay,
|
|
69
|
+
].filter((part): part is number => part !== undefined)
|
|
70
|
+
if (parts.length) pushField(fields, 'account', label('birthday'), parts.join('-'))
|
|
71
|
+
}
|
|
72
|
+
if (profile.labels?.length) pushField(fields, 'account', label('labels'), profile.labels.join(t('sandbox.profileCard.separator')))
|
|
73
|
+
if (profile.isVip !== undefined) pushField(fields, 'account', 'VIP', yesNo(t, profile.isVip))
|
|
74
|
+
if (profile.isYearsVip !== undefined) pushField(fields, 'account', label('yearsVip'), yesNo(t, profile.isYearsVip))
|
|
75
|
+
pushField(fields, 'account', label('vipLevel'), profile.vipLevel)
|
|
76
|
+
return fields
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getFriendshipFields(
|
|
80
|
+
t: ProfileCardTranslate,
|
|
81
|
+
snapshot: Pick<SandboxSnapshot, 'participants' | 'friendships'>,
|
|
82
|
+
participantId: string,
|
|
83
|
+
): ProfileCardField[] {
|
|
84
|
+
const fields: ProfileCardField[] = []
|
|
85
|
+
for (const friendship of snapshot.friendships) {
|
|
86
|
+
if (!friendship.participantIds.includes(participantId)) continue
|
|
87
|
+
for (const [ownerId, remark] of Object.entries(friendship.remarks)) {
|
|
88
|
+
if (!remark) continue
|
|
89
|
+
const peerId = friendship.participantIds.find((id) => id !== ownerId) ?? participantId
|
|
90
|
+
// 备注属于 owner 对 peer 的好友局部资料,标签标明双方以免与全局昵称混淆。
|
|
91
|
+
if (peerId === participantId) {
|
|
92
|
+
const owner = snapshot.participants.find(({ id }) => id === ownerId)
|
|
93
|
+
pushField(fields, 'friendship', t('sandbox.profileCard.remarkBy', { name: owner?.name ?? ownerId }), remark)
|
|
94
|
+
} else if (ownerId === participantId) {
|
|
95
|
+
const peer = snapshot.participants.find(({ id }) => id === peerId)
|
|
96
|
+
pushField(fields, 'friendship', t('sandbox.profileCard.remarkFor', { name: peer?.name ?? peerId }), remark)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return fields
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getGroupMemberFields(t: ProfileCardTranslate, group: SandboxGroup, member: SandboxGroupMember): ProfileCardField[] {
|
|
104
|
+
const fields: ProfileCardField[] = []
|
|
105
|
+
const label = (name: string) => t(`sandbox.profileCard.field.${name}`)
|
|
106
|
+
pushField(fields, 'group-member', label('group'), group.name)
|
|
107
|
+
pushField(fields, 'group-member', label('groupId'), group.id)
|
|
108
|
+
pushField(fields, 'group-member', label('role'), t(`sandbox.role.${member.role}`))
|
|
109
|
+
// 仅在有值时展示,不使用「未设置」等伪造默认值掩盖缺失。
|
|
110
|
+
pushField(fields, 'group-member', label('card'), member.card)
|
|
111
|
+
pushField(fields, 'group-member', label('title'), member.title)
|
|
112
|
+
pushField(fields, 'group-member', label('area'), member.area)
|
|
113
|
+
pushField(fields, 'group-member', label('joinTime'), member.joinTime)
|
|
114
|
+
pushField(fields, 'group-member', label('lastSentTime'), member.lastSentTime)
|
|
115
|
+
pushField(fields, 'group-member', label('groupLevel'), member.level)
|
|
116
|
+
if (member.unfriendly !== undefined) pushField(fields, 'group-member', label('unfriendly'), yesNo(t, member.unfriendly))
|
|
117
|
+
pushField(fields, 'group-member', label('titleExpire'), member.titleExpireTime)
|
|
118
|
+
if (member.cardChangeable !== undefined) pushField(fields, 'group-member', label('cardChangeable'), yesNo(t, member.cardChangeable))
|
|
119
|
+
return fields
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function getBotRuntimeFields(t: ProfileCardTranslate, bot: SandboxBotProfile): ProfileCardField[] {
|
|
123
|
+
const label = (name: string) => t(`sandbox.profileCard.field.${name}`)
|
|
124
|
+
const fields: ProfileCardField[] = [
|
|
125
|
+
{ group: 'bot-runtime', label: label('implementation'), value: implementationLabel(bot.implementation) },
|
|
126
|
+
{ group: 'bot-runtime', label: label('enabled'), value: t(bot.enabled ? 'sandbox.env.enabled' : 'sandbox.env.disabled') },
|
|
127
|
+
]
|
|
128
|
+
if (bot.disabledCapabilities?.length) {
|
|
129
|
+
fields.push({ group: 'bot-runtime', label: label('disabledCapabilities'), value: bot.disabledCapabilities.join(t('sandbox.profileCard.separator')) })
|
|
130
|
+
}
|
|
131
|
+
return fields
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function buildProfileCardModel(t: ProfileCardTranslate, input: {
|
|
135
|
+
snapshot: Pick<SandboxSnapshot, 'participants' | 'friendships' | 'groups'>
|
|
136
|
+
participantId: string
|
|
137
|
+
viewerId?: string
|
|
138
|
+
groupId?: string
|
|
139
|
+
}): ProfileCardModel | undefined {
|
|
140
|
+
const participant = input.snapshot.participants.find(({ id }) => id === input.participantId) as SandboxParticipant | undefined
|
|
141
|
+
if (!participant) return undefined
|
|
142
|
+
|
|
143
|
+
const fields: ProfileCardField[] = []
|
|
144
|
+
fields.push(...getAccountFields(t, participant.profile))
|
|
145
|
+
fields.push(...getFriendshipFields(t, input.snapshot, participant.id))
|
|
146
|
+
|
|
147
|
+
// 优先当前会话群;否则列出参与者加入的全部群成员资料,满足「场景中全部可用资料」。
|
|
148
|
+
const memberships = input.groupId
|
|
149
|
+
? input.snapshot.groups
|
|
150
|
+
.filter(({ id }) => id === input.groupId)
|
|
151
|
+
.flatMap((group) => {
|
|
152
|
+
const member = group.members.find(({ participantId }) => participantId === participant.id)
|
|
153
|
+
return member ? [{ group, member }] : []
|
|
154
|
+
})
|
|
155
|
+
: input.snapshot.groups.flatMap((group) => {
|
|
156
|
+
const member = group.members.find(({ participantId }) => participantId === participant.id)
|
|
157
|
+
return member ? [{ group, member }] : []
|
|
158
|
+
})
|
|
159
|
+
for (const { group, member } of memberships) {
|
|
160
|
+
fields.push(...getGroupMemberFields(t, group, member))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (isSandboxBot(participant)) fields.push(...getBotRuntimeFields(t, participant))
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
participantId: participant.id,
|
|
167
|
+
name: participant.name,
|
|
168
|
+
avatar: participant.avatar,
|
|
169
|
+
avatarKind: isSandboxBot(participant) ? 'bot' : 'user',
|
|
170
|
+
identityLabel: 'QQ',
|
|
171
|
+
...(participant.profile?.personalNote ? { personalNote: participant.profile.personalNote } : {}),
|
|
172
|
+
fields,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function buildGroupProfileCardModel(t: ProfileCardTranslate, group: SandboxGroup): ProfileCardModel {
|
|
177
|
+
return {
|
|
178
|
+
participantId: group.id,
|
|
179
|
+
name: group.name,
|
|
180
|
+
...(group.avatar ? { avatar: group.avatar } : {}),
|
|
181
|
+
avatarKind: 'group',
|
|
182
|
+
identityLabel: t('sandbox.profileCard.field.groupId'),
|
|
183
|
+
fields: [
|
|
184
|
+
{ group: 'group', label: t('sandbox.profileCard.field.members'), value: t('sandbox.profileCard.memberCount', { count: group.members.length }) },
|
|
185
|
+
{ group: 'group', label: t('sandbox.profileCard.field.announcements'), value: t('sandbox.profileCard.announcementCount', { count: group.announcements.length }) },
|
|
186
|
+
],
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function groupProfileCardFields(t: ProfileCardTranslate, fields: readonly ProfileCardField[]): Array<{ group: ProfileCardFieldGroup, label: string, fields: ProfileCardField[] }> {
|
|
191
|
+
const order: ProfileCardFieldGroup[] = ['account', 'friendship', 'group-member', 'bot-runtime', 'group']
|
|
192
|
+
return order
|
|
193
|
+
.map((group) => ({
|
|
194
|
+
group,
|
|
195
|
+
label: getProfileCardGroupLabel(t, group),
|
|
196
|
+
fields: fields.filter((field) => field.group === group),
|
|
197
|
+
}))
|
|
198
|
+
.filter((section) => section.fields.length > 0)
|
|
199
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { findDirectRootConversation, findGroupRootConversation, type ResolvedConversation } from '../../src/conversation-resolution'
|
|
2
|
+
import type { SandboxSnapshot } from '../../src/types'
|
|
3
|
+
|
|
4
|
+
export function getConversationPeerId(conversation: ResolvedConversation, operatorId: string | undefined) {
|
|
5
|
+
if (conversation.type !== 'direct') return undefined
|
|
6
|
+
const participantIds = conversation.participantIds
|
|
7
|
+
return participantIds?.find((id) => id !== operatorId) ?? participantIds?.[0]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function getFriendDirectory(snapshot: SandboxSnapshot, operatorId?: string) {
|
|
11
|
+
if (!operatorId) return []
|
|
12
|
+
|
|
13
|
+
return snapshot.participants
|
|
14
|
+
.filter(({ id }) => id !== operatorId)
|
|
15
|
+
.map((participant) => {
|
|
16
|
+
const friendship = snapshot.friendships.find(({ participantIds }) => participantIds.includes(operatorId) && participantIds.includes(participant.id))
|
|
17
|
+
const pendingOutgoing = snapshot.requests.some(({ type, requesterId, targetId }) => type === 'friend' && requesterId === operatorId && targetId === participant.id)
|
|
18
|
+
const pendingIncoming = snapshot.requests.some(({ type, requesterId, targetId }) => type === 'friend' && requesterId === participant.id && targetId === operatorId)
|
|
19
|
+
const isBot = participant.kind === 'bot'
|
|
20
|
+
const conversationId = findDirectRootConversation(snapshot, operatorId, participant.id)?.id
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
id: participant.id,
|
|
24
|
+
isBot,
|
|
25
|
+
isFriend: !!friendship,
|
|
26
|
+
pendingOutgoing,
|
|
27
|
+
pendingIncoming,
|
|
28
|
+
conversationId,
|
|
29
|
+
avatar: participant.avatar,
|
|
30
|
+
displayName: friendship?.remarks[operatorId] || participant.name,
|
|
31
|
+
// 文案键,组件按 `{ name: participant.name }` 翻译;纯逻辑模块不持有界面语言。
|
|
32
|
+
statusKey: friendship
|
|
33
|
+
? isBot ? 'sandbox.relation.botFriend' : 'sandbox.relation.friend'
|
|
34
|
+
: pendingOutgoing
|
|
35
|
+
? 'sandbox.relation.outgoing'
|
|
36
|
+
: pendingIncoming
|
|
37
|
+
? 'sandbox.relation.incoming'
|
|
38
|
+
: isBot ? 'sandbox.relation.botMissing' : 'sandbox.relation.missing',
|
|
39
|
+
participantName: participant.name,
|
|
40
|
+
relation: friendship ? 'added' as const : pendingOutgoing || pendingIncoming ? 'pending' as const : 'missing' as const,
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getGroupDirectory(snapshot: SandboxSnapshot, operatorId?: string) {
|
|
46
|
+
if (!operatorId) return []
|
|
47
|
+
|
|
48
|
+
return snapshot.groups.map((group) => {
|
|
49
|
+
const member = group.members.find(({ participantId }) => participantId === operatorId)
|
|
50
|
+
const pending = snapshot.requests.some(({ type, subType, requesterId, groupId }) => type === 'group'
|
|
51
|
+
&& (subType ?? 'add') === 'add' && requesterId === operatorId && groupId === group.id)
|
|
52
|
+
const conversationId = member ? findGroupRootConversation(snapshot, group.id)?.id : undefined
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
...group,
|
|
56
|
+
member,
|
|
57
|
+
pending,
|
|
58
|
+
conversationId,
|
|
59
|
+
relation: member ? 'joined' as const : pending ? 'pending' as const : 'missing' as const,
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type ThinkingDurationTranslate = (key: string, params?: Record<string, unknown>) => string
|
|
2
|
+
|
|
3
|
+
/** 折叠按钮上的时长文案。没有时长时退回「思考过程」,负值兜底到 0。 */
|
|
4
|
+
export function formatThinkingDuration(t: ThinkingDurationTranslate, durationMs?: number): string {
|
|
5
|
+
if (durationMs === undefined) return t('sandbox.chat.thinkingProcess')
|
|
6
|
+
return t('sandbox.chat.thoughtFor', { seconds: Math.max(0, Math.round(durationMs / 1000)) })
|
|
7
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const USER_AVATAR_SIZE = 36
|
|
2
|
+
export const USER_STACK_COLLAPSED_STEP = 21
|
|
3
|
+
export const USER_STACK_EXPANDED_STEP = 27
|
|
4
|
+
|
|
5
|
+
export interface UserStackMetrics {
|
|
6
|
+
collapsedVisibleCount: number
|
|
7
|
+
overflowCount: number
|
|
8
|
+
collapsedWidth: number
|
|
9
|
+
expandedWidth: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface UserStackLayoutMetrics {
|
|
13
|
+
collapsedWidth: number
|
|
14
|
+
expandedWidth: number
|
|
15
|
+
addCollapsedRight: number
|
|
16
|
+
addExpandedRight: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function orderUsersByActive<User extends { id: string }>(users: User[], activeUserId?: string): User[] {
|
|
20
|
+
const activeIndex = users.findIndex(({ id }) => id === activeUserId)
|
|
21
|
+
return activeIndex > 0
|
|
22
|
+
? [users[activeIndex], ...users.slice(0, activeIndex), ...users.slice(activeIndex + 1)]
|
|
23
|
+
: users
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// compact:极限窄屏下折叠态只保留当前操作者头像,其余全部并入 +N 省略,
|
|
27
|
+
// 保证胶囊剩余宽度足够包住输入框与附件、发送图标。
|
|
28
|
+
export function getUserStackMetrics(userCount: number, compact = false): UserStackMetrics {
|
|
29
|
+
const collapsedVisibleCount = Math.min(userCount, compact ? 1 : 3)
|
|
30
|
+
const overflowCount = Math.max(0, userCount - collapsedVisibleCount)
|
|
31
|
+
return {
|
|
32
|
+
collapsedVisibleCount,
|
|
33
|
+
overflowCount,
|
|
34
|
+
collapsedWidth: USER_AVATAR_SIZE
|
|
35
|
+
+ Math.max(0, collapsedVisibleCount - 1) * USER_STACK_COLLAPSED_STEP
|
|
36
|
+
+ (overflowCount ? USER_STACK_COLLAPSED_STEP : 0),
|
|
37
|
+
expandedWidth: USER_AVATAR_SIZE + Math.max(0, userCount - 1) * USER_STACK_EXPANDED_STEP,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getUserStackLayoutMetrics(userCount: number, compact = false): UserStackLayoutMetrics {
|
|
42
|
+
const metrics = getUserStackMetrics(userCount, compact)
|
|
43
|
+
const addCollapsedRight = metrics.overflowCount
|
|
44
|
+
? metrics.collapsedWidth - USER_AVATAR_SIZE
|
|
45
|
+
: userCount * USER_STACK_COLLAPSED_STEP
|
|
46
|
+
const addExpandedRight = userCount * USER_STACK_EXPANDED_STEP
|
|
47
|
+
return {
|
|
48
|
+
collapsedWidth: metrics.overflowCount ? metrics.collapsedWidth : USER_AVATAR_SIZE + addCollapsedRight,
|
|
49
|
+
expandedWidth: USER_AVATAR_SIZE + addExpandedRight,
|
|
50
|
+
addCollapsedRight,
|
|
51
|
+
addExpandedRight,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// ego lite 被控空间边缘跑马灯的 Web 移植。
|
|
2
|
+
// 片元着色器逐字取自 ego lite resources.pak(条目 58803,原生 OverlayOuter/InnerGlowRenderer 共用),
|
|
3
|
+
// 效果为:贴窗缘描边环经 14 次随机旋转采样模糊成雾带,accent 色纵向 alpha ramp 随 uScrollOffset
|
|
4
|
+
// 循环滚动——光带因此沿边缘持续移动。参数预设在 C++ overlay_params.h 内无法提取,
|
|
5
|
+
// 按 ego 实机截屏标定:边缘近满饱和、向内扩散 ≈120-160 CSS px、四角因双边叠加自然更亮。
|
|
6
|
+
const GLOW_FRAGMENT = `#version 100
|
|
7
|
+
precision mediump float;
|
|
8
|
+
|
|
9
|
+
uniform vec2 uResolution;
|
|
10
|
+
|
|
11
|
+
uniform float uStroke;
|
|
12
|
+
uniform float uBlur;
|
|
13
|
+
uniform float uRadius;
|
|
14
|
+
uniform float uFade;
|
|
15
|
+
uniform vec4 uAccent;
|
|
16
|
+
uniform float uScrollOffset;
|
|
17
|
+
|
|
18
|
+
float hash21(vec2 p) {
|
|
19
|
+
vec2 q = fract(p * vec2(0.3183099, 0.3678794)) + 0.1;
|
|
20
|
+
q = q + dot(q, q + 19.19);
|
|
21
|
+
return fract(q.x * q.y);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
float ringDist(vec2 p_in) {
|
|
25
|
+
float r = max(uRadius, 0.0);
|
|
26
|
+
vec2 half_res = uResolution * 0.5;
|
|
27
|
+
vec2 p = abs(p_in - half_res) - (half_res - vec2(r));
|
|
28
|
+
float outside = length(max(p, vec2(0.0)));
|
|
29
|
+
float inside = min(max(p.x, p.y), 0.0);
|
|
30
|
+
return -(outside + inside - r);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
vec4 rampColor(float t_in) {
|
|
34
|
+
float t = fract(t_in);
|
|
35
|
+
vec4 a = vec4(uAccent.rgb, 0.8);
|
|
36
|
+
vec4 m = vec4(uAccent.rgb, 0.0);
|
|
37
|
+
vec4 lo = mix(a, m, smoothstep(0.0, 0.5, t));
|
|
38
|
+
vec4 hi = mix(m, a, smoothstep(0.5, 1.0, t));
|
|
39
|
+
return t < 0.5 ? lo : hi;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
float hardStroke(vec2 p) {
|
|
43
|
+
float d = ringDist(p);
|
|
44
|
+
float halfS = max(uStroke * 0.5, 1.0);
|
|
45
|
+
return smoothstep(halfS, 0.0, abs(d));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
float sampleStroke(vec2 fragcoord, float blur, float cs, float sn, vec2 off) {
|
|
49
|
+
vec2 rot = vec2(off.x * cs - off.y * sn, off.x * sn + off.y * cs);
|
|
50
|
+
return hardStroke(fragcoord + rot * blur);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void main() {
|
|
54
|
+
vec2 fragcoord = gl_FragCoord.xy;
|
|
55
|
+
float blur = max(uBlur, 0.5);
|
|
56
|
+
float theta = hash21(fragcoord) * 6.2831853;
|
|
57
|
+
float cs = cos(theta);
|
|
58
|
+
float sn = sin(theta);
|
|
59
|
+
|
|
60
|
+
float sum = hardStroke(fragcoord) * 1.00000000;
|
|
61
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.5253, -0.1547)) * 0.54895195;
|
|
62
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.4254, -0.3611)) * 0.53648497;
|
|
63
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.0840, 0.6276)) * 0.44848703;
|
|
64
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.6783, 0.2515)) * 0.35109927;
|
|
65
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.2049, -0.8231)) * 0.23717542;
|
|
66
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.7975, 0.4521)) * 0.18622469;
|
|
67
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.8654, -0.1832)) * 0.20909663;
|
|
68
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.1521, -0.5736)) * 0.49445322;
|
|
69
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.5612, 0.2138)) * 0.48611417;
|
|
70
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.1834, 0.3015)) * 0.77952102;
|
|
71
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.4421, 0.7531)) * 0.21757231;
|
|
72
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.8123, -0.4986)) * 0.16253425;
|
|
73
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(-0.7211, -0.6173)) * 0.16495394;
|
|
74
|
+
sum += sampleStroke(fragcoord, blur, cs, sn, vec2(0.2412, -0.9012)) * 0.17540148;
|
|
75
|
+
|
|
76
|
+
float tile = max(uResolution.y, 1.0);
|
|
77
|
+
float scroll = mod(uScrollOffset, tile);
|
|
78
|
+
if (scroll < 0.0) {
|
|
79
|
+
scroll += tile;
|
|
80
|
+
}
|
|
81
|
+
float t = (fragcoord.y + scroll) / tile;
|
|
82
|
+
vec4 ramp = rampColor(t);
|
|
83
|
+
float alpha = (sum / 6.22071317) * clamp(uFade, 0.0, 1.0) * ramp.a;
|
|
84
|
+
gl_FragColor = vec4(ramp.rgb * alpha, alpha);
|
|
85
|
+
}
|
|
86
|
+
`
|
|
87
|
+
|
|
88
|
+
const VERTEX = `attribute vec2 a_Position;
|
|
89
|
+
void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }
|
|
90
|
+
`
|
|
91
|
+
|
|
92
|
+
// 内外两层光晕(CSS px 单位,运行时乘 dpr)。向内扩散 ≈ stroke/2 + blur;相位错半圈保证任意时刻
|
|
93
|
+
// 整圈都有光带,反向滚动制造流动纵深 [推断]。光晕颜色不在预设里:取画布的 CSS color(spaces.css 设为
|
|
94
|
+
// v2 主题的 --primary),ego 原版的蓝色 [0.27, 0.42, 0.85] 只在读不到时兜底。
|
|
95
|
+
const FALLBACK_ACCENT: readonly [number, number, number] = [0.27, 0.42, 0.85]
|
|
96
|
+
const GLOW_PRESETS = [
|
|
97
|
+
{ stroke: 160, blur: 115, radius: 14, fade: 1, scrollSpeed: 1 / 7, scrollPhase: 0 },
|
|
98
|
+
{ stroke: 76, blur: 185, radius: 14, fade: 1, scrollSpeed: -1 / 9, scrollPhase: 0.5 },
|
|
99
|
+
// 这是原本的数据(按 ego 实机截屏标定的初版参数,后应需求加宽扩散后弃用,保留备查):
|
|
100
|
+
// { stroke: 110, blur: 80, radius: 14, fade: 1, accent: [0.27, 0.42, 0.85, 1], scrollSpeed: 1 / 7, scrollPhase: 0 },
|
|
101
|
+
// { stroke: 50, blur: 130, radius: 14, fade: 0.85, accent: [0.27, 0.42, 0.85, 1], scrollSpeed: -1 / 9, scrollPhase: 0.5 },
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
interface GlProgram {
|
|
105
|
+
program: WebGLProgram
|
|
106
|
+
uniforms: Record<string, WebGLUniformLocation | null>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function compile(gl: WebGLRenderingContext, type: number, source: string): WebGLShader | null {
|
|
110
|
+
const shader = gl.createShader(type)
|
|
111
|
+
if (!shader) return null
|
|
112
|
+
gl.shaderSource(shader, source)
|
|
113
|
+
gl.compileShader(shader)
|
|
114
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
115
|
+
console.warn('[chatluna-sandbox] overlay shader 编译失败:', gl.getShaderInfoLog(shader))
|
|
116
|
+
gl.deleteShader(shader)
|
|
117
|
+
return null
|
|
118
|
+
}
|
|
119
|
+
return shader
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function createProgram(gl: WebGLRenderingContext, fragment: string): GlProgram | null {
|
|
123
|
+
const vs = compile(gl, gl.VERTEX_SHADER, VERTEX)
|
|
124
|
+
const fs = compile(gl, gl.FRAGMENT_SHADER, fragment)
|
|
125
|
+
if (!vs || !fs) return null
|
|
126
|
+
const program = gl.createProgram()
|
|
127
|
+
if (!program) return null
|
|
128
|
+
gl.attachShader(program, vs)
|
|
129
|
+
gl.attachShader(program, fs)
|
|
130
|
+
gl.bindAttribLocation(program, 0, 'a_Position')
|
|
131
|
+
gl.linkProgram(program)
|
|
132
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
133
|
+
console.warn('[chatluna-sandbox] overlay shader 链接失败:', gl.getProgramInfoLog(program))
|
|
134
|
+
return null
|
|
135
|
+
}
|
|
136
|
+
const uniforms: Record<string, WebGLUniformLocation | null> = {}
|
|
137
|
+
const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS) as number
|
|
138
|
+
for (let i = 0; i < count; i++) {
|
|
139
|
+
const info = gl.getActiveUniform(program, i)
|
|
140
|
+
if (info) uniforms[info.name] = gl.getUniformLocation(program, info.name)
|
|
141
|
+
}
|
|
142
|
+
return { program, uniforms }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 计算样式里的 color 一律是 rgb()/rgba()(--primary 是十六进制色),取前三个分量归一到 0-1。
|
|
146
|
+
function readAccent(canvas: HTMLCanvasElement): readonly [number, number, number] {
|
|
147
|
+
const match = getComputedStyle(canvas).color.match(/rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/)
|
|
148
|
+
return match ? [Number(match[1]) / 255, Number(match[2]) / 255, Number(match[3]) / 255] : FALLBACK_ACCENT
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 挂载 ego 式跑马灯特效。返回卸载函数;WebGL 不可用时返回 null(此时仅保留 CSS scrim 与点阵背景)。
|
|
152
|
+
export function mountAgentOverlayEffect(canvas: HTMLCanvasElement): (() => void) | null {
|
|
153
|
+
const gl = canvas.getContext('webgl', { alpha: true, premultipliedAlpha: true, antialias: false })
|
|
154
|
+
if (!gl) return null
|
|
155
|
+
const glow = createProgram(gl, GLOW_FRAGMENT)
|
|
156
|
+
if (!glow) return null
|
|
157
|
+
|
|
158
|
+
const buffer = gl.createBuffer()
|
|
159
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
|
|
160
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW)
|
|
161
|
+
gl.enableVertexAttribArray(0)
|
|
162
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0)
|
|
163
|
+
gl.enable(gl.BLEND)
|
|
164
|
+
// 着色器输出 premultiplied alpha,按 source-over 叠加(与 ego OverlayCompositor 一致)。
|
|
165
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
|
166
|
+
|
|
167
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2)
|
|
168
|
+
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
169
|
+
let frame = 0
|
|
170
|
+
let disposed = false
|
|
171
|
+
const start = performance.now()
|
|
172
|
+
let accent = readAccent(canvas)
|
|
173
|
+
|
|
174
|
+
function resize() {
|
|
175
|
+
const width = Math.max(1, Math.round(canvas.clientWidth * dpr))
|
|
176
|
+
const height = Math.max(1, Math.round(canvas.clientHeight * dpr))
|
|
177
|
+
if (canvas.width !== width || canvas.height !== height) {
|
|
178
|
+
canvas.width = width
|
|
179
|
+
canvas.height = height
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function setFloat(name: string, value: number) {
|
|
184
|
+
const loc = glow!.uniforms[name]
|
|
185
|
+
if (loc) gl!.uniform1f(loc, value)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function render(now: number) {
|
|
189
|
+
if (disposed) return
|
|
190
|
+
resize()
|
|
191
|
+
const width = canvas.width
|
|
192
|
+
const height = canvas.height
|
|
193
|
+
const time = (now - start) / 1000
|
|
194
|
+
gl!.viewport(0, 0, width, height)
|
|
195
|
+
gl!.clearColor(0, 0, 0, 0)
|
|
196
|
+
gl!.clear(gl!.COLOR_BUFFER_BIT)
|
|
197
|
+
|
|
198
|
+
gl!.useProgram(glow!.program)
|
|
199
|
+
for (const preset of GLOW_PRESETS) {
|
|
200
|
+
gl!.uniform2f(glow!.uniforms.uResolution!, width, height)
|
|
201
|
+
setFloat('uStroke', preset.stroke * dpr)
|
|
202
|
+
setFloat('uBlur', preset.blur * dpr)
|
|
203
|
+
setFloat('uRadius', preset.radius * dpr)
|
|
204
|
+
setFloat('uFade', preset.fade)
|
|
205
|
+
const accentLoc = glow!.uniforms.uAccent
|
|
206
|
+
if (accentLoc) gl!.uniform4f(accentLoc, accent[0], accent[1], accent[2], 1)
|
|
207
|
+
setFloat('uScrollOffset', (time * preset.scrollSpeed + preset.scrollPhase) * height)
|
|
208
|
+
gl!.drawArrays(gl!.TRIANGLES, 0, 3)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (!reducedMotion) frame = requestAnimationFrame(render)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => {
|
|
215
|
+
if (reducedMotion) render(performance.now())
|
|
216
|
+
})
|
|
217
|
+
observer?.observe(canvas)
|
|
218
|
+
// v2 切换亮暗主题改的是 <html> 的 class,--primary 随之变化,重新取色。
|
|
219
|
+
const themeObserver = typeof MutationObserver === 'undefined' ? null : new MutationObserver(() => {
|
|
220
|
+
accent = readAccent(canvas)
|
|
221
|
+
if (reducedMotion) render(performance.now())
|
|
222
|
+
})
|
|
223
|
+
themeObserver?.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'style', 'data-theme'] })
|
|
224
|
+
// 首帧同步绘制:后台标签页的 rAF 可能被冻结,保证至少渲染出静态画面。
|
|
225
|
+
render(performance.now())
|
|
226
|
+
|
|
227
|
+
return () => {
|
|
228
|
+
disposed = true
|
|
229
|
+
cancelAnimationFrame(frame)
|
|
230
|
+
observer?.disconnect()
|
|
231
|
+
themeObserver?.disconnect()
|
|
232
|
+
// 只释放本次创建的资源,不调用 loseContext:开发模式的 StrictMode 会在同一个 canvas 上卸载再挂载一次,
|
|
233
|
+
// 上下文一旦被主动丢弃,第二次 getContext 拿到的是已丢失的上下文,边缘光整个不显示。
|
|
234
|
+
// canvas 卸载后上下文随元素一起回收。
|
|
235
|
+
gl.deleteBuffer(buffer)
|
|
236
|
+
gl.deleteProgram(glow.program)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { toast } from '@chatluna/ui-client/client'
|
|
3
|
+
import type { ResolvedConversation } from '../../src/conversation-resolution'
|
|
4
|
+
import { getSandboxBots, getSandboxUsers, type SandboxBotProfile, type SandboxGroup, type SandboxParticipant, type SandboxSnapshot, type SandboxWorkspaceState } from '../../src/types'
|
|
5
|
+
import { getConversationPeerId } from '../webqq/relationship-directory'
|
|
6
|
+
import { errorText, visibleConversationsOf, WorkspaceStore, type WorkspaceView } from './store'
|
|
7
|
+
|
|
8
|
+
export interface ParticipantSummary {
|
|
9
|
+
name: string
|
|
10
|
+
avatar?: string
|
|
11
|
+
isBot: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 一次渲染内复用的派生视图:所有组件按同一口径读当前操作者、会话与参与者。 */
|
|
15
|
+
export interface WorkspaceDerived {
|
|
16
|
+
workspace: SandboxWorkspaceState
|
|
17
|
+
snapshot: SandboxSnapshot
|
|
18
|
+
operator?: SandboxParticipant
|
|
19
|
+
operatorId?: string
|
|
20
|
+
conversations: ResolvedConversation[]
|
|
21
|
+
conversation?: ResolvedConversation
|
|
22
|
+
group?: SandboxGroup
|
|
23
|
+
peer?: SandboxParticipant
|
|
24
|
+
bot?: SandboxBotProfile
|
|
25
|
+
participants: Record<string, ParticipantSummary>
|
|
26
|
+
participantNames: Record<string, string>
|
|
27
|
+
resolveAvatar(reference: string | undefined): string
|
|
28
|
+
media: Readonly<Record<string, string>>
|
|
29
|
+
mediaFailures: Readonly<Record<string, true>>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface SandboxContextValue {
|
|
33
|
+
store: WorkspaceStore
|
|
34
|
+
view: WorkspaceView
|
|
35
|
+
derived?: WorkspaceDerived
|
|
36
|
+
/** 运行一次操作;失败以 Toast 呈现并返回 false,调用方据此决定是否关闭浮层。 */
|
|
37
|
+
run(label: string, task: () => Promise<unknown>): Promise<boolean>
|
|
38
|
+
dialogs: SandboxDialogs
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 跨区域打开的二级面统一由工作区托管,任一区域只发出「打开」意图。 */
|
|
42
|
+
export interface SandboxDialogs {
|
|
43
|
+
openEntity(mode: 'edit' | 'delete', target: { type: 'user' | 'bot' | 'group', id: string }): void
|
|
44
|
+
openRemark(targetId: string): void
|
|
45
|
+
openGroupAction(mode: 'card' | 'name' | 'title', targetId: string, groupId?: string): void
|
|
46
|
+
openConversationRename(conversationId: string): void
|
|
47
|
+
openProfile(participantId: string, anchor?: { x: number, y: number }): void
|
|
48
|
+
openGroupProfile(groupId: string, anchor?: { x: number, y: number }): void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const SandboxContext = React.createContext<SandboxContextValue | undefined>(undefined)
|
|
52
|
+
|
|
53
|
+
export function useSandbox() {
|
|
54
|
+
const value = React.useContext(SandboxContext)
|
|
55
|
+
if (!value) throw new Error('useSandbox must be used inside SandboxProvider')
|
|
56
|
+
return value
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function useWorkspace(): WorkspaceDerived {
|
|
60
|
+
const { derived } = useSandbox()
|
|
61
|
+
if (!derived) throw new Error('workspace is not loaded')
|
|
62
|
+
return derived
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function deriveWorkspace(view: WorkspaceView, store: WorkspaceStore): WorkspaceDerived | undefined {
|
|
66
|
+
const workspace = view.workspace
|
|
67
|
+
if (!workspace) return undefined
|
|
68
|
+
const snapshot = workspace.snapshot
|
|
69
|
+
const operatorId = view.currentOperatorId
|
|
70
|
+
const operator = snapshot.participants.find(({ id }) => id === operatorId)
|
|
71
|
+
const conversations = visibleConversationsOf(snapshot, operatorId)
|
|
72
|
+
const conversation = conversations.find(({ id }) => id === view.activeConversationId)
|
|
73
|
+
const peerId = conversation ? getConversationPeerId(conversation, operatorId) : undefined
|
|
74
|
+
const bot = getSandboxBots(snapshot).find(({ id }) => id === peerId)
|
|
75
|
+
const peer = bot ?? getSandboxUsers(snapshot).find(({ id }) => id === peerId)
|
|
76
|
+
const resolveAvatar = (reference: string | undefined) => store.resolveAvatar(reference)
|
|
77
|
+
const participants = Object.fromEntries(snapshot.participants.map(({ id, name, avatar, kind }) => [
|
|
78
|
+
id,
|
|
79
|
+
{ name, avatar: resolveAvatar(avatar), isBot: kind === 'bot' },
|
|
80
|
+
]))
|
|
81
|
+
return {
|
|
82
|
+
workspace,
|
|
83
|
+
snapshot,
|
|
84
|
+
operator,
|
|
85
|
+
operatorId,
|
|
86
|
+
conversations,
|
|
87
|
+
conversation,
|
|
88
|
+
group: snapshot.groups.find(({ id }) => id === conversation?.groupId),
|
|
89
|
+
peer,
|
|
90
|
+
bot,
|
|
91
|
+
participants,
|
|
92
|
+
participantNames: Object.fromEntries(snapshot.participants.map(({ id, name }) => [id, name])),
|
|
93
|
+
resolveAvatar,
|
|
94
|
+
media: view.media,
|
|
95
|
+
mediaFailures: view.mediaFailures,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function SandboxProvider({ store, dialogs, children }: {
|
|
100
|
+
store: WorkspaceStore
|
|
101
|
+
dialogs: SandboxDialogs
|
|
102
|
+
children: React.ReactNode
|
|
103
|
+
}) {
|
|
104
|
+
const view = React.useSyncExternalStore(store.subscribe, store.getView, store.getView)
|
|
105
|
+
const derived = React.useMemo(() => deriveWorkspace(view, store), [view, store])
|
|
106
|
+
const run = React.useCallback(async (label: string, task: () => Promise<unknown>) => {
|
|
107
|
+
try {
|
|
108
|
+
await task()
|
|
109
|
+
return true
|
|
110
|
+
} catch (error) {
|
|
111
|
+
toast.error(label, { description: errorText(error) })
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
}, [])
|
|
115
|
+
const value = React.useMemo(() => ({ store, view, derived, run, dialogs }), [store, view, derived, run, dialogs])
|
|
116
|
+
return <SandboxContext.Provider value={value}>{children}</SandboxContext.Provider>
|
|
117
|
+
}
|