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,197 @@
1
+ import { isInheritedMessage as isInheritedPrefixMessage } from '../../src/conversation-resolution'
2
+ import { NO_MESSAGE_CAPABILITIES, type MessageCapabilities } from '../../src/message-capabilities'
3
+ import {
4
+ formatRecalledMessageEventText,
5
+ isRecalledMessage,
6
+ type SandboxForwardPreview,
7
+ type SandboxMedia,
8
+ type SandboxMessage,
9
+ } from '../../src/types'
10
+ import { isForkBoundaryMessage } from './fork-boundary'
11
+ import { formatMentionContent } from './mention'
12
+
13
+ /**
14
+ * 「这条消息该怎么显示」——消息列表的呈现判定。
15
+ *
16
+ * 全部是纯函数:不起组件就能逐条问,也因此每一条都能被断言,例如「撤回的消息不能被引用」。
17
+ */
18
+
19
+ /** 呈现判定要读的那部分列表模型。收窄到这几项,测试不必造一整个列表模型。 */
20
+ export interface MessagePresentationContext {
21
+ readonly replyMessages: Record<string, SandboxMessage>
22
+ readonly forwardPreviews: Record<string, SandboxForwardPreview>
23
+ readonly messageCapabilities: Record<string, MessageCapabilities>
24
+ readonly participantNames: Record<string, string>
25
+ /** 默认 true:保留撤回气泡;false:隐藏原文并显示结构化撤回事件。 */
26
+ readonly markRecalledMessages: boolean
27
+ readonly currentConversationId?: string
28
+ readonly currentOperatorId?: string
29
+ }
30
+
31
+ /**
32
+ * 媒体消息正文里的模拟文本标签(`[图片] 名称`)。它与服务端生成的消息内容逐字比较,属于消息内容,
33
+ * 不翻译;合并转发预览与消息气泡共用这一份。
34
+ */
35
+ export function getMediaLabel(media: Pick<SandboxMedia, 'type'>): string {
36
+ return media.type === 'image' ? '图片'
37
+ : media.type === 'audio' ? '语音'
38
+ : media.type === 'video' ? '视频'
39
+ : '文件'
40
+ }
41
+
42
+ /** 文件消息上的体积文案。1024 进制,KB 与 MB 保留一位小数。 */
43
+ export function formatMediaSize(size: number): string {
44
+ if (size < 1024) return `${size} B`
45
+ if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`
46
+ return `${(size / 1024 / 1024).toFixed(1)} MB`
47
+ }
48
+
49
+ /**
50
+ * 事件化:这一行渲染成一条居中的系统提示,而不是一个气泡。
51
+ *
52
+ * 两种来源——戳一戳这类本来就是事件的消息,以及关闭了撤回标记时的撤回消息。开启撤回标记时
53
+ * 撤回消息仍然渲染原气泡并弱化,因此这里必须同时看消息与设置,只看其中一个都会错。
54
+ */
55
+ export function shouldRenderAsEvent(
56
+ message: SandboxMessage,
57
+ context: Pick<MessagePresentationContext, 'markRecalledMessages'>,
58
+ ): boolean {
59
+ return !!message.event || (isRecalledMessage(message) && !context.markRecalledMessages)
60
+ }
61
+
62
+ /** 事件行上的文案。撤回事件按操作者渲染,其余事件直接用消息正文。 */
63
+ export function getEventMessageText(
64
+ message: SandboxMessage,
65
+ resolveOperatorName: (participantId: string) => string,
66
+ ): string {
67
+ if (isRecalledMessage(message)) {
68
+ const operatorId = message.lifecycle?.operatorId ?? message.authorId
69
+ return formatRecalledMessageEventText(message, resolveOperatorName(operatorId))
70
+ }
71
+ return message.content
72
+ }
73
+
74
+ /** 思考内容归档在消息上,因此多轮对话后每条机器人消息都保留自己的那一份。 */
75
+ export function getMessageThinking(message: SandboxMessage) {
76
+ return message.chatLuna?.thought ? message.chatLuna : undefined
77
+ }
78
+
79
+ /** 没有思考内容但拿到了 Token 用量时单独常显指标。 */
80
+ export function getMessageUsage(message: SandboxMessage) {
81
+ const chatLuna = message.chatLuna
82
+ return chatLuna && !chatLuna.thought && chatLuna.usage ? chatLuna : undefined
83
+ }
84
+
85
+ /**
86
+ * 思考与用量随撤回标记一同隐藏。
87
+ *
88
+ * 关闭撤回标记时原文已经被换成事件行,思考与用量若还留着就等于把撤回掉的内容又露出来一次。
89
+ */
90
+ export function shouldShowThinking(
91
+ message: SandboxMessage,
92
+ context: Pick<MessagePresentationContext, 'markRecalledMessages'>,
93
+ ): boolean {
94
+ return !!getMessageThinking(message) && !(isRecalledMessage(message) && !context.markRecalledMessages)
95
+ }
96
+
97
+ export function shouldShowUsage(
98
+ message: SandboxMessage,
99
+ context: Pick<MessagePresentationContext, 'markRecalledMessages'>,
100
+ ): boolean {
101
+ return !!getMessageUsage(message) && !(isRecalledMessage(message) && !context.markRecalledMessages)
102
+ }
103
+
104
+ /**
105
+ * 气泡里显示的正文。
106
+ *
107
+ * 两处返回空串:外层合并转发卡片自己渲染预览行,正文再显示一次会和摘要重复;单媒体消息的正文
108
+ * 若恰好就是服务端生成的「[标签] 文件名」,气泡里已经有那个文件本身,重复一遍是噪音。
109
+ * 去重按逐字相等判定——用户手写的同样内容也会被去掉,但那是同一句话说两遍。
110
+ */
111
+ export function getMessageText(
112
+ message: SandboxMessage,
113
+ context: Pick<MessagePresentationContext, 'participantNames'>,
114
+ ): string {
115
+ if (message.forwardId) return ''
116
+ const media = message.media
117
+ if (media?.length === 1 && message.content === `[${getMediaLabel(media[0]!)}] ${media[0]!.name}`) return ''
118
+ return formatMentionContent(message.content, context.participantNames)
119
+ }
120
+
121
+ /** 引用回复的被引消息。查表拿,取不到就当没有引用。 */
122
+ export function getReplyMessage(
123
+ message: SandboxMessage,
124
+ context: Pick<MessagePresentationContext, 'replyMessages'>,
125
+ ): SandboxMessage | undefined {
126
+ return message.replyToMessageId ? context.replyMessages[message.replyToMessageId] : undefined
127
+ }
128
+
129
+ /**
130
+ * 合并转发卡片的预览。
131
+ *
132
+ * 先看消息自己有没有转发标识,再查表:只查表会让「预览表里恰好有同名条目」的消息误显示成
133
+ * 转发卡片。预览缺失时卡片仍然渲染但入口禁用——资源还在加载,不是没有。
134
+ */
135
+ export function getForwardPreview(
136
+ message: SandboxMessage,
137
+ context: Pick<MessagePresentationContext, 'forwardPreviews'>,
138
+ ): SandboxForwardPreview | undefined {
139
+ return message.forwardId ? context.forwardPreviews[message.id] : undefined
140
+ }
141
+
142
+ /**
143
+ * 这条消息现在能做什么。
144
+ *
145
+ * 判定住在 `src/message-capabilities`,服务端在写入路径上问的是同一份判据;这里只读答案,
146
+ * 因此右键里出现的动作都真的做得到,也不会长出第二份口径。投影还没给出这条消息时一律读作
147
+ * 「一条都做不到」,不猜。
148
+ */
149
+ export function readMessageCapabilities(
150
+ message: Pick<SandboxMessage, 'id'>,
151
+ context: Pick<MessagePresentationContext, 'messageCapabilities'>,
152
+ ): MessageCapabilities {
153
+ return context.messageCapabilities[message.id] ?? NO_MESSAGE_CAPABILITIES
154
+ }
155
+
156
+ /**
157
+ * 消息是不是这条分支继承来的那一段。
158
+ *
159
+ * 判定住在共享的 `isInheritedMessage`:同一份判定既决定这一行要不要弱化,也是消息能力里
160
+ * 「继承前缀在实例视图里只读」那一条的依据,因此右键里做不到的动作干脆不显示。
161
+ */
162
+ export function isInheritedMessage(
163
+ message: SandboxMessage,
164
+ context: Pick<MessagePresentationContext, 'currentConversationId'>,
165
+ ): boolean {
166
+ return isInheritedPrefixMessage(message, context.currentConversationId)
167
+ }
168
+
169
+ /** 这一行之前要不要画分界:它解释了上面那段为什么右键项更少。 */
170
+ export function isForkBoundary(
171
+ messages: readonly SandboxMessage[],
172
+ index: number,
173
+ context: Pick<MessagePresentationContext, 'currentConversationId'>,
174
+ ): boolean {
175
+ return isForkBoundaryMessage(messages, index, context.currentConversationId)
176
+ }
177
+
178
+ /**
179
+ * 点一下已有回应条要不要生效,以及生效时是贴上还是取消。
180
+ *
181
+ * 两道闸门缺一不可:能力位管「这条消息还允不允许被贴」,当前操作者管「是谁在贴」。少一道
182
+ * 就会让用户点一下已有 emoji 绕过只读,或者在没有操作者时贴出一条无主回应。
183
+ */
184
+ export function resolveReactionToggle(
185
+ message: SandboxMessage,
186
+ emojiId: string,
187
+ context: Pick<MessagePresentationContext, 'messageCapabilities' | 'currentOperatorId'>,
188
+ ): { messageId: string, emojiId: string, enabled: boolean } | undefined {
189
+ const operatorId = context.currentOperatorId
190
+ if (!readMessageCapabilities(message, context).react || !operatorId) return
191
+ const reaction = message.reactions?.find((item) => item.emojiId === emojiId)
192
+ return {
193
+ messageId: message.id,
194
+ emojiId,
195
+ enabled: !reaction?.participantIds.includes(operatorId),
196
+ }
197
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * 若消息尚未进入当前列表,循环加载更早历史直到出现或无法继续。
3
+ * 用最旧消息锚点检测“加载无进展”,避免 hasMore 与服务端不一致时死循环。
4
+ */
5
+ export async function ensureMessageLoaded(input: {
6
+ messageId: string
7
+ isLoaded: () => boolean
8
+ canLoadMore: () => boolean
9
+ loadMore: () => Promise<void>
10
+ getOldestLoadedMessageId?: () => string | undefined
11
+ maxAttempts?: number
12
+ }): Promise<boolean> {
13
+ if (input.isLoaded()) return true
14
+
15
+ let attempts = 0
16
+ const maxAttempts = input.maxAttempts ?? 40
17
+ let previousAnchor = input.getOldestLoadedMessageId?.()
18
+
19
+ while (!input.isLoaded() && input.canLoadMore() && attempts < maxAttempts) {
20
+ attempts += 1
21
+ await input.loadMore()
22
+ if (input.isLoaded()) return true
23
+ const nextAnchor = input.getOldestLoadedMessageId?.()
24
+ if (input.getOldestLoadedMessageId && nextAnchor === previousAnchor) {
25
+ return false
26
+ }
27
+ previousAnchor = nextAnchor
28
+ }
29
+
30
+ return input.isLoaded()
31
+ }
@@ -0,0 +1,40 @@
1
+ export interface MessageSearchDateRange {
2
+ createdAtStart: string
3
+ createdAtEnd: string
4
+ }
5
+
6
+ /** `YYYY-MM-DD` 本地日历日 → 日历控件使用的本地零点 Date;格式或日期非法时返回 undefined。 */
7
+ export function localDateToCalendarValue(value: string): Date | undefined {
8
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
9
+ if (!match) return undefined
10
+ const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]))
11
+ return date.getDate() === Number(match[3]) ? date : undefined
12
+ }
13
+
14
+ export function calendarValueToLocalDate(value: Date | undefined): string {
15
+ if (!value) return ''
16
+ const pad = (part: number) => String(part).padStart(2, '0')
17
+ return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`
18
+ }
19
+
20
+ export function localDateToMessageSearchRange(value: string): MessageSearchDateRange | undefined {
21
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
22
+ if (!match) return undefined
23
+ const year = Number(match[1])
24
+ const month = Number(match[2])
25
+ const day = Number(match[3])
26
+ const start = new Date(year, month - 1, day)
27
+ if (
28
+ start.getFullYear() !== year
29
+ || start.getMonth() !== month - 1
30
+ || start.getDate() !== day
31
+ ) return undefined
32
+
33
+ // 按本地日历推进到次日零点,不能固定加 24 小时,否则夏令时切换日会偏移。
34
+ const end = new Date(start)
35
+ end.setDate(end.getDate() + 1)
36
+ return {
37
+ createdAtStart: start.toISOString(),
38
+ createdAtEnd: end.toISOString(),
39
+ }
40
+ }
@@ -0,0 +1,85 @@
1
+ import type { MessageCapabilities } from '../../src/message-capabilities'
2
+
3
+ /**
4
+ * 多选转发的整个流程:进入、切换、退出、可选性,以及确认转发到目标会话。
5
+ *
6
+ * 「哪些消息可选」只读投影给出的 `forward` 能力位,不在客户端按事件、撤回状态另行推导:
7
+ * 消息能力只许有一份口径(`src/message-capabilities`),服务端写入时问的是同一份判据。
8
+ */
9
+
10
+ export interface MessageSelectionState {
11
+ readonly active: boolean
12
+ readonly messageIds: readonly string[]
13
+ }
14
+
15
+ export const NO_MESSAGE_SELECTION: MessageSelectionState = { active: false, messageIds: [] }
16
+
17
+ /** 可选性只问投影给出的能力位。 */
18
+ export interface MessageSelectionContext {
19
+ readonly messageCapabilities: Record<string, MessageCapabilities>
20
+ }
21
+
22
+ /**
23
+ * 这条消息能不能进多选。
24
+ *
25
+ * 投影还没给出这条消息时读作不能——这与右键菜单的口径一致:宁可少给一个动作,也不要提供一个
26
+ * 服务端会拒绝的动作。
27
+ */
28
+ export function readForwardCapability(messageId: string, context: MessageSelectionContext): boolean {
29
+ return context.messageCapabilities[messageId]?.forward ?? false
30
+ }
31
+
32
+ /**
33
+ * 从某条消息进入多选,并把它作为默认选中。
34
+ *
35
+ * 不可转发的消息不能作为入口:它进不了选中集合,进入后会得到一个「已选 0 条」的空多选态。
36
+ */
37
+ export function enterMessageSelection(
38
+ messageId: string,
39
+ context: MessageSelectionContext,
40
+ ): MessageSelectionState | undefined {
41
+ if (!readForwardCapability(messageId, context)) return
42
+ return { active: true, messageIds: [messageId] }
43
+ }
44
+
45
+ /**
46
+ * 切换某条消息的选中。
47
+ *
48
+ * 不在多选态时什么都不做——列表在非多选态下不会派发这个动作,但外壳与页面都能派发,
49
+ * 少这道闸门会让一次误派发悄悄改掉选中集合。
50
+ */
51
+ export function toggleMessageSelection(
52
+ state: MessageSelectionState,
53
+ messageId: string,
54
+ context: MessageSelectionContext,
55
+ ): MessageSelectionState {
56
+ if (!state.active || !readForwardCapability(messageId, context)) return state
57
+ if (state.messageIds.includes(messageId)) {
58
+ return { active: true, messageIds: state.messageIds.filter((id) => id !== messageId) }
59
+ }
60
+ return { active: true, messageIds: [...state.messageIds, messageId] }
61
+ }
62
+
63
+ export function exitMessageSelection(): MessageSelectionState {
64
+ return NO_MESSAGE_SELECTION
65
+ }
66
+
67
+ export type ForwardConfirmation =
68
+ | { readonly kind: 'send', readonly conversationId: string, readonly messageIds: readonly string[] }
69
+ | { readonly kind: 'reject' }
70
+
71
+ /**
72
+ * 确认转发。
73
+ *
74
+ * 确认那一刻重新按能力位过滤一遍:从进入多选到点确认之间,选中的消息可能已经被撤回,
75
+ * 此时把它一起发出去会被服务端拒掉整批。过滤后为空则本地就拒绝,不发无谓的 RPC。
76
+ */
77
+ export function resolveForwardConfirmation(
78
+ state: MessageSelectionState,
79
+ conversationId: string,
80
+ context: MessageSelectionContext,
81
+ ): ForwardConfirmation {
82
+ const messageIds = state.messageIds.filter((messageId) => readForwardCapability(messageId, context))
83
+ if (!messageIds.length) return { kind: 'reject' }
84
+ return { kind: 'send', conversationId, messageIds }
85
+ }
@@ -0,0 +1,20 @@
1
+ import type { SandboxRelationshipRequest, SandboxSnapshot } from '../../src/types'
2
+
3
+ export interface IncomingNotificationRequests {
4
+ friends: SandboxRelationshipRequest[]
5
+ groups: SandboxRelationshipRequest[]
6
+ }
7
+
8
+ export function getIncomingNotificationRequests(snapshot: SandboxSnapshot, operatorId: string | undefined): IncomingNotificationRequests {
9
+ if (!operatorId) return { friends: [], groups: [] }
10
+
11
+ const manageableGroupIds = new Set(snapshot.groups
12
+ .filter(({ members }) => members.some(({ participantId, role }) => participantId === operatorId && (role === 'owner' || role === 'admin')))
13
+ .map(({ id }) => id))
14
+
15
+ return {
16
+ friends: snapshot.requests.filter(({ type, targetId }) => type === 'friend' && targetId === operatorId),
17
+ groups: snapshot.requests.filter(({ type, subType, groupId, targetId }) => type === 'group' && !!groupId
18
+ && ((subType === 'invite' && targetId === operatorId) || ((subType ?? 'add') === 'add' && manageableGroupIds.has(groupId)))),
19
+ }
20
+ }
@@ -0,0 +1,128 @@
1
+ import type { SandboxGroup, SandboxGroupMember } from '../../src/types'
2
+ import { getFriendMenuActions, type FriendMenuAction, type FriendMenuState } from './friend-menu'
3
+ import { getGroupAuthorityBadge, getGroupMemberDisplayName } from './group-display'
4
+ import { getGroupMemberMenuActions, type GroupMemberMenuAction } from './group-menu'
5
+
6
+ /**
7
+ * 「这个参与者该怎么显示」——消息列表的参与者呈现投影。
8
+ *
9
+ * 名称、徽标与两张菜单的取数收成纯函数,可以不起组件逐条断言「群名片为空白时回退到全局昵称」
10
+ * 这类边界。
11
+ */
12
+
13
+ export interface MessageParticipant {
14
+ name: string
15
+ avatar?: string
16
+ isBot: boolean
17
+ }
18
+
19
+ /** 参与者投影要读的那部分列表模型。 */
20
+ export interface ParticipantPresentationContext {
21
+ readonly participants: Record<string, MessageParticipant>
22
+ readonly friendMenuStates: Record<string, FriendMenuState>
23
+ readonly currentGroup?: SandboxGroup
24
+ readonly currentOperatorId?: string
25
+ }
26
+
27
+ const NO_FRIEND_MENU_STATE: FriendMenuState = {
28
+ isFriend: false,
29
+ pendingOutgoing: false,
30
+ pendingIncoming: false,
31
+ }
32
+
33
+ /** 全局昵称。参与者目录里没有这个人时退回标识本身,界面上宁可显示一串 ID 也不显示空白。 */
34
+ export function getParticipantName(
35
+ participantId: string,
36
+ context: Pick<ParticipantPresentationContext, 'participants'>,
37
+ ): string {
38
+ return context.participants[participantId]?.name ?? participantId
39
+ }
40
+
41
+ /** 当前群里的成员记录。不在群里(私聊、或已退群的历史发言者)时取不到。 */
42
+ export function getCurrentGroupMember(
43
+ participantId: string,
44
+ context: Pick<ParticipantPresentationContext, 'currentGroup'>,
45
+ ): SandboxGroupMember | undefined {
46
+ return context.currentGroup?.members.find((member) => member.participantId === participantId)
47
+ }
48
+
49
+ /**
50
+ * 消息上显示的作者名:群名片优先,回退到全局昵称。
51
+ *
52
+ * 回退按「去空白后为空」判定而不是按「字段存在」——只判存在会让一张全是空格的群名片顶掉昵称,
53
+ * 表现为消息上的名字整个消失。
54
+ */
55
+ export function getMessageAuthorName(
56
+ participantId: string,
57
+ context: Pick<ParticipantPresentationContext, 'participants' | 'currentGroup'>,
58
+ ): string {
59
+ return getGroupMemberDisplayName(
60
+ getCurrentGroupMember(participantId, context),
61
+ getParticipantName(participantId, context),
62
+ )
63
+ }
64
+
65
+ /** 群身份徽标与专属头衔共用同一个槽位,判定住在 `group-display`。 */
66
+ export function getMessageRoleBadge(
67
+ participantId: string,
68
+ context: Pick<ParticipantPresentationContext, 'currentGroup'>,
69
+ ) {
70
+ return getGroupAuthorityBadge(getCurrentGroupMember(participantId, context))
71
+ }
72
+
73
+ /**
74
+ * 头像右键里的群成员动作。
75
+ *
76
+ * 目标不在当前群里就一个都不给:私聊里点头像不该冒出群操作,历史发言者已退群时那些操作
77
+ * 也真的做不到。发起者用当前操作者,因此同一条消息在不同操作者视角下给出的动作不同。
78
+ */
79
+ export function getMessageGroupMemberActions(
80
+ participantId: string,
81
+ context: Pick<ParticipantPresentationContext, 'currentGroup' | 'currentOperatorId'>,
82
+ ): GroupMemberMenuAction[] {
83
+ const target = getCurrentGroupMember(participantId, context)
84
+ if (!target) return []
85
+ return getGroupMemberMenuActions(
86
+ getCurrentGroupMember(context.currentOperatorId ?? '', context),
87
+ target,
88
+ )
89
+ }
90
+
91
+ /**
92
+ * 哪些动作算「群管理操作」。
93
+ *
94
+ * 这张表决定头像右键里那个「群成员操作」子菜单出不出现:`mention` 与 `poke` 是互动而不是
95
+ * 管理,它们留在一级菜单里,因此只有这六项进表。表写错不会报错,只会让子菜单在该出现时不出现。
96
+ */
97
+ export const GROUP_MANAGEMENT_ACTIONS: readonly GroupMemberMenuAction[] = [
98
+ 'set-card',
99
+ 'set-title',
100
+ 'set-admin',
101
+ 'unset-admin',
102
+ 'transfer-owner',
103
+ 'kick',
104
+ ]
105
+
106
+ export function hasGroupMemberManagementActions(
107
+ participantId: string,
108
+ context: Pick<ParticipantPresentationContext, 'currentGroup' | 'currentOperatorId'>,
109
+ ): boolean {
110
+ return getMessageGroupMemberActions(participantId, context)
111
+ .some((action) => GROUP_MANAGEMENT_ACTIONS.includes(action))
112
+ }
113
+
114
+ /** 好友关系状态。投影里没有这个人时按「不是好友、没有待处理申请」处理。 */
115
+ export function getFriendMenuStateOf(
116
+ participantId: string,
117
+ context: Pick<ParticipantPresentationContext, 'friendMenuStates'>,
118
+ ): FriendMenuState {
119
+ return context.friendMenuStates[participantId] ?? NO_FRIEND_MENU_STATE
120
+ }
121
+
122
+ /** 头像右键里的好友动作。聊天区域里的入口带互动项(戳一戳),因此第二参数恒为 true。 */
123
+ export function getChatFriendActions(
124
+ participantId: string,
125
+ context: Pick<ParticipantPresentationContext, 'friendMenuStates'>,
126
+ ): FriendMenuAction[] {
127
+ return getFriendMenuActions(getFriendMenuStateOf(participantId, context), true)
128
+ }