@downcity/plugins 1.0.152 → 1.0.156

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 (175) hide show
  1. package/bin/chat/ChatPlugin.d.ts +2 -10
  2. package/bin/chat/ChatPlugin.d.ts.map +1 -1
  3. package/bin/chat/ChatPlugin.js +3 -13
  4. package/bin/chat/ChatPlugin.js.map +1 -1
  5. package/bin/chat/PROMPT.direct.d.ts +1 -1
  6. package/bin/chat/PROMPT.direct.d.ts.map +1 -1
  7. package/bin/chat/PROMPT.direct.js +1 -1
  8. package/bin/chat/PROMPT.direct.js.map +1 -1
  9. package/bin/chat/access/ChatAccessActions.d.ts +11 -0
  10. package/bin/chat/access/ChatAccessActions.d.ts.map +1 -0
  11. package/bin/chat/access/ChatAccessActions.js +137 -0
  12. package/bin/chat/access/ChatAccessActions.js.map +1 -0
  13. package/bin/chat/access/ChatAccessRuntime.d.ts +17 -0
  14. package/bin/chat/access/ChatAccessRuntime.d.ts.map +1 -0
  15. package/bin/chat/access/ChatAccessRuntime.js +31 -0
  16. package/bin/chat/access/ChatAccessRuntime.js.map +1 -0
  17. package/bin/chat/access/ChatAccessSchema.d.ts +16 -0
  18. package/bin/chat/access/ChatAccessSchema.d.ts.map +1 -0
  19. package/bin/chat/access/ChatAccessSchema.js +99 -0
  20. package/bin/chat/access/ChatAccessSchema.js.map +1 -0
  21. package/bin/chat/access/ChatAccessService.d.ts +44 -0
  22. package/bin/chat/access/ChatAccessService.d.ts.map +1 -0
  23. package/bin/chat/access/ChatAccessService.js +281 -0
  24. package/bin/chat/access/ChatAccessService.js.map +1 -0
  25. package/bin/chat/access/ChatAccessStore.d.ts +67 -0
  26. package/bin/chat/access/ChatAccessStore.d.ts.map +1 -0
  27. package/bin/chat/access/ChatAccessStore.js +367 -0
  28. package/bin/chat/access/ChatAccessStore.js.map +1 -0
  29. package/bin/chat/accounts/Crypto.d.ts +1 -1
  30. package/bin/chat/accounts/Crypto.js +1 -1
  31. package/bin/chat/accounts/Store.d.ts +1 -1
  32. package/bin/chat/accounts/Store.js +1 -1
  33. package/bin/chat/channels/BaseChatChannel.d.ts +18 -21
  34. package/bin/chat/channels/BaseChatChannel.d.ts.map +1 -1
  35. package/bin/chat/channels/BaseChatChannel.js +48 -58
  36. package/bin/chat/channels/BaseChatChannel.js.map +1 -1
  37. package/bin/chat/channels/BaseChatChannelQueue.d.ts +1 -1
  38. package/bin/chat/channels/BaseChatChannelQueue.d.ts.map +1 -1
  39. package/bin/chat/channels/BaseChatChannelQueue.js +2 -14
  40. package/bin/chat/channels/BaseChatChannelQueue.js.map +1 -1
  41. package/bin/chat/channels/Configuration.d.ts +2 -2
  42. package/bin/chat/channels/Configuration.d.ts.map +1 -1
  43. package/bin/chat/channels/Configuration.js +3 -3
  44. package/bin/chat/channels/Configuration.js.map +1 -1
  45. package/bin/chat/channels/feishu/Configuration.js +4 -4
  46. package/bin/chat/channels/feishu/Configuration.js.map +1 -1
  47. package/bin/chat/channels/feishu/Feishu.d.ts +2 -0
  48. package/bin/chat/channels/feishu/Feishu.d.ts.map +1 -1
  49. package/bin/chat/channels/feishu/Feishu.js +8 -7
  50. package/bin/chat/channels/feishu/Feishu.js.map +1 -1
  51. package/bin/chat/channels/feishu/FeishuMessageHandler.d.ts +9 -17
  52. package/bin/chat/channels/feishu/FeishuMessageHandler.d.ts.map +1 -1
  53. package/bin/chat/channels/feishu/FeishuMessageHandler.js +6 -17
  54. package/bin/chat/channels/feishu/FeishuMessageHandler.js.map +1 -1
  55. package/bin/chat/channels/qq/Configuration.js +4 -4
  56. package/bin/chat/channels/qq/Configuration.js.map +1 -1
  57. package/bin/chat/channels/qq/QQ.d.ts +2 -0
  58. package/bin/chat/channels/qq/QQ.d.ts.map +1 -1
  59. package/bin/chat/channels/qq/QQ.js +8 -7
  60. package/bin/chat/channels/qq/QQ.js.map +1 -1
  61. package/bin/chat/channels/qq/QQMessageHandler.d.ts +9 -17
  62. package/bin/chat/channels/qq/QQMessageHandler.d.ts.map +1 -1
  63. package/bin/chat/channels/qq/QQMessageHandler.js +7 -25
  64. package/bin/chat/channels/qq/QQMessageHandler.js.map +1 -1
  65. package/bin/chat/channels/telegram/Bot.d.ts +2 -0
  66. package/bin/chat/channels/telegram/Bot.d.ts.map +1 -1
  67. package/bin/chat/channels/telegram/Bot.js +8 -7
  68. package/bin/chat/channels/telegram/Bot.js.map +1 -1
  69. package/bin/chat/channels/telegram/Configuration.js +4 -4
  70. package/bin/chat/channels/telegram/Configuration.js.map +1 -1
  71. package/bin/chat/channels/telegram/TelegramMessageHandler.d.ts +9 -17
  72. package/bin/chat/channels/telegram/TelegramMessageHandler.d.ts.map +1 -1
  73. package/bin/chat/channels/telegram/TelegramMessageHandler.js +5 -16
  74. package/bin/chat/channels/telegram/TelegramMessageHandler.js.map +1 -1
  75. package/bin/chat/runtime/ChatChannelActions.d.ts +3 -2
  76. package/bin/chat/runtime/ChatChannelActions.d.ts.map +1 -1
  77. package/bin/chat/runtime/ChatChannelActions.js +57 -0
  78. package/bin/chat/runtime/ChatChannelActions.js.map +1 -1
  79. package/bin/chat/runtime/ChatChannelConfig.d.ts +2 -2
  80. package/bin/chat/runtime/ChatChannelConfig.js +3 -3
  81. package/bin/chat/runtime/ChatChannelConfig.js.map +1 -1
  82. package/bin/chat/runtime/ChatChannelCore.d.ts +2 -2
  83. package/bin/chat/runtime/ChatChannelCore.js +1 -1
  84. package/bin/chat/runtime/ChatChannelLifecycle.d.ts +1 -1
  85. package/bin/chat/runtime/ChatChannelLifecycle.js +1 -1
  86. package/bin/chat/runtime/PluginPoints.d.ts +0 -24
  87. package/bin/chat/runtime/PluginPoints.d.ts.map +1 -1
  88. package/bin/chat/runtime/PluginPoints.js +0 -24
  89. package/bin/chat/runtime/PluginPoints.js.map +1 -1
  90. package/bin/chat/runtime/QueuedUserMessage.d.ts.map +1 -1
  91. package/bin/chat/runtime/QueuedUserMessage.js +0 -2
  92. package/bin/chat/runtime/QueuedUserMessage.js.map +1 -1
  93. package/bin/chat/types/ChannelConfiguration.d.ts +4 -4
  94. package/bin/chat/types/ChannelConfiguration.d.ts.map +1 -1
  95. package/bin/chat/types/ChatAccess.d.ts +300 -0
  96. package/bin/chat/types/ChatAccess.d.ts.map +1 -0
  97. package/bin/chat/types/ChatAccess.js +22 -0
  98. package/bin/chat/types/ChatAccess.js.map +1 -0
  99. package/bin/chat/types/ChatPromptContext.d.ts +0 -15
  100. package/bin/chat/types/ChatPromptContext.d.ts.map +1 -1
  101. package/bin/index.d.ts +4 -4
  102. package/bin/index.d.ts.map +1 -1
  103. package/bin/index.js +3 -3
  104. package/bin/index.js.map +1 -1
  105. package/bin/skill/Config.d.ts +1 -1
  106. package/bin/skill/Config.js +1 -1
  107. package/bin/skill/runtime/Paths.d.ts +1 -1
  108. package/bin/skill/runtime/Paths.js +1 -1
  109. package/package.json +6 -4
  110. package/scripts/chat-access.test.mjs +90 -0
  111. package/scripts/chat-channel-config.test.mjs +82 -0
  112. package/src/chat/ChatPlugin.ts +4 -24
  113. package/src/chat/PROMPT.direct.ts +1 -1
  114. package/src/chat/PROMPT.direct.ts.txt +1 -1
  115. package/src/chat/access/ChatAccessActions.ts +159 -0
  116. package/src/chat/access/ChatAccessRuntime.ts +41 -0
  117. package/src/chat/access/ChatAccessSchema.ts +104 -0
  118. package/src/chat/access/ChatAccessService.ts +311 -0
  119. package/src/chat/access/ChatAccessStore.ts +462 -0
  120. package/src/chat/accounts/Crypto.ts +1 -1
  121. package/src/chat/accounts/Store.ts +1 -1
  122. package/src/chat/channels/BaseChatChannel.ts +59 -80
  123. package/src/chat/channels/BaseChatChannelQueue.ts +2 -14
  124. package/src/chat/channels/Configuration.ts +3 -3
  125. package/src/chat/channels/feishu/Configuration.ts +4 -4
  126. package/src/chat/channels/feishu/Feishu.ts +11 -9
  127. package/src/chat/channels/feishu/FeishuMessageHandler.ts +16 -40
  128. package/src/chat/channels/qq/Configuration.ts +4 -4
  129. package/src/chat/channels/qq/QQ.ts +11 -9
  130. package/src/chat/channels/qq/QQMessageHandler.ts +17 -49
  131. package/src/chat/channels/telegram/Bot.ts +11 -9
  132. package/src/chat/channels/telegram/Configuration.ts +4 -4
  133. package/src/chat/channels/telegram/TelegramMessageHandler.ts +15 -39
  134. package/src/chat/runtime/ChatChannelActions.ts +72 -1
  135. package/src/chat/runtime/ChatChannelConfig.ts +3 -3
  136. package/src/chat/runtime/ChatChannelCore.ts +1 -1
  137. package/src/chat/runtime/ChatChannelLifecycle.ts +1 -1
  138. package/src/chat/runtime/PluginPoints.ts +0 -24
  139. package/src/chat/runtime/QueuedUserMessage.ts +0 -2
  140. package/src/chat/types/ChannelConfiguration.ts +4 -4
  141. package/src/chat/types/ChatAccess.ts +335 -0
  142. package/src/chat/types/ChatPromptContext.ts +0 -17
  143. package/src/index.ts +27 -31
  144. package/src/skill/Config.ts +1 -1
  145. package/src/skill/runtime/Paths.ts +1 -1
  146. package/bin/auth/runtime/AuthorizationConfig.d.ts +0 -41
  147. package/bin/auth/runtime/AuthorizationConfig.d.ts.map +0 -1
  148. package/bin/auth/runtime/AuthorizationConfig.js +0 -185
  149. package/bin/auth/runtime/AuthorizationConfig.js.map +0 -1
  150. package/bin/auth/runtime/AuthorizationPolicy.d.ts +0 -28
  151. package/bin/auth/runtime/AuthorizationPolicy.d.ts.map +0 -1
  152. package/bin/auth/runtime/AuthorizationPolicy.js +0 -124
  153. package/bin/auth/runtime/AuthorizationPolicy.js.map +0 -1
  154. package/bin/auth/runtime/AuthorizationStore.d.ts +0 -29
  155. package/bin/auth/runtime/AuthorizationStore.d.ts.map +0 -1
  156. package/bin/auth/runtime/AuthorizationStore.js +0 -179
  157. package/bin/auth/runtime/AuthorizationStore.js.map +0 -1
  158. package/bin/auth/types/AuthPlugin.d.ts +0 -403
  159. package/bin/auth/types/AuthPlugin.d.ts.map +0 -1
  160. package/bin/auth/types/AuthPlugin.js +0 -111
  161. package/bin/auth/types/AuthPlugin.js.map +0 -1
  162. package/bin/chat/runtime/ChatAuthorizationRuntime.d.ts +0 -22
  163. package/bin/chat/runtime/ChatAuthorizationRuntime.d.ts.map +0 -1
  164. package/bin/chat/runtime/ChatAuthorizationRuntime.js +0 -230
  165. package/bin/chat/runtime/ChatAuthorizationRuntime.js.map +0 -1
  166. package/bin/chat/runtime/PluginDispatch.d.ts +0 -43
  167. package/bin/chat/runtime/PluginDispatch.d.ts.map +0 -1
  168. package/bin/chat/runtime/PluginDispatch.js +0 -57
  169. package/bin/chat/runtime/PluginDispatch.js.map +0 -1
  170. package/src/auth/runtime/AuthorizationConfig.ts +0 -247
  171. package/src/auth/runtime/AuthorizationPolicy.ts +0 -167
  172. package/src/auth/runtime/AuthorizationStore.ts +0 -220
  173. package/src/auth/types/AuthPlugin.ts +0 -540
  174. package/src/chat/runtime/ChatAuthorizationRuntime.ts +0 -270
  175. package/src/chat/runtime/PluginDispatch.ts +0 -89
@@ -1,270 +0,0 @@
1
- /**
2
- * ChatAuthorizationRuntime:ChatPlugin 内置授权能力。
3
- *
4
- * 关键点(中文)
5
- * - 授权能力归属于 ChatPlugin,不再作为独立 plugin 注册。
6
- * - chat runtime 仍通过通用 plugin hook / resolve 点调用,保持执行链路统一。
7
- * - action 名称统一带 `authorization-` 前缀,避免和 chat 会话动作混淆。
8
- */
9
-
10
- import type {
11
- PluginActions,
12
- PluginHooks,
13
- PluginResolves,
14
- } from "@downcity/agent";
15
- import { createAction } from "@downcity/agent";
16
- import { z } from "zod";
17
- import type { JsonValue } from "@downcity/agent";
18
- import { CHAT_PLUGIN_POINTS } from "@/chat/runtime/PluginPoints.js";
19
- import type { ChatDispatchChannel } from "@/chat/types/ChatDispatcher.js";
20
- import {
21
- CHAT_AUTHORIZATION_ACTIONS,
22
- CHAT_AUTHORIZATION_CATALOG,
23
- type ChatAuthorizationConfig,
24
- type ChatAuthorizationEvaluateInput,
25
- type ChatAuthorizationObservePrincipalPayload,
26
- type ChatAuthorizationSetUserRolePayload,
27
- type ChatAuthorizationSnapshot,
28
- type ChatAuthorizationWriteConfigPayload,
29
- } from "@/auth/types/AuthPlugin.js";
30
- import {
31
- readChatAuthorizationConfig,
32
- setChatAuthorizationUserRole,
33
- writeChatAuthorizationConfig,
34
- } from "@/auth/runtime/AuthorizationConfig.js";
35
- import {
36
- evaluateIncomingChatAuthorization,
37
- resolveAuthorizedUserRole,
38
- } from "@/auth/runtime/AuthorizationPolicy.js";
39
- import {
40
- readAuthorizationSnapshot,
41
- recordObservedAuthorizationPrincipal,
42
- } from "@/auth/runtime/AuthorizationStore.js";
43
-
44
- function toChannel(value: unknown): ChatDispatchChannel | null {
45
- const text = String(value || "").trim().toLowerCase();
46
- if (text === "telegram" || text === "feishu" || text === "qq") return text;
47
- return null;
48
- }
49
-
50
- function toRecord(value: unknown): Record<string, unknown> {
51
- if (!value || typeof value !== "object" || Array.isArray(value)) return {};
52
- return value as Record<string, unknown>;
53
- }
54
-
55
- function toEvaluateInput(payload: Record<string, unknown>): ChatAuthorizationEvaluateInput {
56
- const channel = toChannel(payload.channel);
57
- if (!channel) {
58
- throw new Error("chat.authorizeIncoming requires a valid channel");
59
- }
60
- return {
61
- channel,
62
- chatId: String(payload.chatId || "").trim(),
63
- ...(typeof payload.chatType === "string" ? { chatType: payload.chatType.trim() } : {}),
64
- ...(typeof payload.userId === "string" ? { userId: payload.userId.trim() } : {}),
65
- ...(typeof payload.username === "string" ? { username: payload.username.trim() } : {}),
66
- ...(typeof payload.chatTitle === "string" ? { chatTitle: payload.chatTitle.trim() } : {}),
67
- };
68
- }
69
-
70
- function toSnapshotData(snapshot: ChatAuthorizationSnapshot): JsonValue {
71
- return {
72
- catalog: CHAT_AUTHORIZATION_CATALOG as unknown as JsonValue,
73
- config: snapshot.config as unknown as JsonValue,
74
- users: snapshot.users as unknown as JsonValue,
75
- chats: snapshot.chats as unknown as JsonValue,
76
- };
77
- }
78
-
79
- /**
80
- * 创建 ChatPlugin 内置授权 hooks。
81
- */
82
- export function createChatAuthorizationHooks(): PluginHooks {
83
- return {
84
- guard: {
85
- [CHAT_PLUGIN_POINTS.authorizeIncoming]: [
86
- async ({ context, value }) => {
87
- const input =
88
- value && typeof value === "object" && !Array.isArray(value)
89
- ? (value as Record<string, unknown>)
90
- : {};
91
- const evaluateInput = toEvaluateInput(input);
92
- const authorizationConfig = readChatAuthorizationConfig(context);
93
- const result = evaluateIncomingChatAuthorization({
94
- config: context.config,
95
- channel: evaluateInput.channel,
96
- input: evaluateInput,
97
- authorizationConfig,
98
- });
99
- if (result.decision !== "allow") {
100
- throw new Error(result.reason || "chat authorization blocked");
101
- }
102
- },
103
- ],
104
- },
105
- effect: {
106
- [CHAT_PLUGIN_POINTS.observePrincipal]: [
107
- async ({ context, value }) => {
108
- const input = toRecord(
109
- value,
110
- ) as unknown as ChatAuthorizationObservePrincipalPayload;
111
- const channel = toChannel(input.channel);
112
- if (!channel) {
113
- throw new Error("chat.observePrincipal requires a valid channel");
114
- }
115
- await recordObservedAuthorizationPrincipal({
116
- context,
117
- channel,
118
- chatId: String(input.chatId || "").trim(),
119
- ...(typeof input.chatType === "string"
120
- ? { chatType: input.chatType.trim() }
121
- : {}),
122
- ...(typeof input.chatTitle === "string"
123
- ? { chatTitle: input.chatTitle.trim() }
124
- : {}),
125
- ...(typeof input.userId === "string"
126
- ? { userId: input.userId.trim() }
127
- : {}),
128
- ...(typeof input.username === "string"
129
- ? { username: input.username.trim() }
130
- : {}),
131
- });
132
- },
133
- ],
134
- },
135
- };
136
- }
137
-
138
- /**
139
- * 创建 ChatPlugin 内置授权 resolve 点。
140
- */
141
- export function createChatAuthorizationResolves(): PluginResolves {
142
- return {
143
- [CHAT_PLUGIN_POINTS.resolveUserRole]: async ({ context, value }) => {
144
- const input =
145
- value && typeof value === "object" && !Array.isArray(value)
146
- ? (value as Record<string, unknown>)
147
- : {};
148
- const channel = toChannel(input.channel);
149
- if (!channel) {
150
- throw new Error("chat.resolveUserRole requires a valid channel");
151
- }
152
- const role = resolveAuthorizedUserRole({
153
- channel,
154
- userId: String(input.userId || "").trim(),
155
- rootPath: context.rootPath,
156
- });
157
- return ((role || null) as unknown) as JsonValue;
158
- },
159
- };
160
- }
161
-
162
- /**
163
- * 创建 ChatPlugin 内置授权 actions。
164
- */
165
- export function createChatAuthorizationActions(): PluginActions {
166
- return {
167
- [CHAT_AUTHORIZATION_ACTIONS.snapshot]: createAction({
168
- description: "Read chat authorization snapshot (catalog/config/users/chats).",
169
- input_schema: {
170
- zod: z.object({}).passthrough(),
171
- json_schema: { type: "object", properties: {} },
172
- },
173
- execute: async ({ context }) => {
174
- const snapshot = await readAuthorizationSnapshot({
175
- context,
176
- });
177
- return {
178
- success: true,
179
- data: toSnapshotData(snapshot),
180
- };
181
- },
182
- }),
183
- [CHAT_AUTHORIZATION_ACTIONS.readConfig]: createAction({
184
- description: "Read current chat authorization config.",
185
- input_schema: {
186
- zod: z.object({}).passthrough(),
187
- json_schema: { type: "object", properties: {} },
188
- },
189
- execute: async ({ context }) => {
190
- return {
191
- success: true,
192
- data: readChatAuthorizationConfig(context) as unknown as JsonValue,
193
- };
194
- },
195
- }),
196
- [CHAT_AUTHORIZATION_ACTIONS.writeConfig]: createAction({
197
- description: "Write chat authorization config as a full replacement.",
198
- input_schema: {
199
- zod: z.object({
200
- config: z.record(z.string(), z.unknown()).optional(),
201
- }),
202
- json_schema: {
203
- type: "object",
204
- properties: {
205
- config: { type: "object", description: "Authorization config." },
206
- },
207
- },
208
- },
209
- execute: async ({ context, input }) => {
210
- const body = toRecord(
211
- input,
212
- ) as unknown as ChatAuthorizationWriteConfigPayload;
213
- const nextConfig =
214
- body.config && typeof body.config === "object" && !Array.isArray(body.config)
215
- ? (body.config as ChatAuthorizationConfig)
216
- : {};
217
- await writeChatAuthorizationConfig({
218
- context,
219
- nextConfig,
220
- });
221
- return {
222
- success: true,
223
- data: readChatAuthorizationConfig(context) as unknown as JsonValue,
224
- };
225
- },
226
- }),
227
- [CHAT_AUTHORIZATION_ACTIONS.setUserRole]: createAction({
228
- description: "Set the role for a channel user.",
229
- input_schema: {
230
- zod: z.object({
231
- channel: z.enum(["telegram", "feishu", "qq"]),
232
- userId: z.string(),
233
- roleId: z.string(),
234
- }),
235
- json_schema: {
236
- type: "object",
237
- required: ["channel", "userId", "roleId"],
238
- properties: {
239
- channel: { type: "string", enum: ["telegram", "feishu", "qq"] },
240
- userId: { type: "string" },
241
- roleId: { type: "string" },
242
- },
243
- },
244
- },
245
- execute: async ({ context, input }) => {
246
- const body = toRecord(
247
- input,
248
- ) as unknown as ChatAuthorizationSetUserRolePayload;
249
- const channel = toChannel(body.channel);
250
- if (!channel) {
251
- return {
252
- success: false,
253
- error: "authorization-set-user-role requires a valid channel",
254
- message: "authorization-set-user-role requires a valid channel",
255
- };
256
- }
257
- await setChatAuthorizationUserRole({
258
- context,
259
- channel,
260
- userId: String(body.userId || "").trim(),
261
- roleId: String(body.roleId || "").trim(),
262
- });
263
- return {
264
- success: true,
265
- data: readChatAuthorizationConfig(context) as unknown as JsonValue,
266
- };
267
- },
268
- }),
269
- };
270
- }
@@ -1,89 +0,0 @@
1
- /**
2
- * PluginDispatch:chat 领域的 plugin 调用辅助。
3
- *
4
- * 关键点(中文)
5
- * - chat plugin runtime 只依赖这里,不直接依赖某个具体 plugin 模块路径。
6
- * - 这里封装 chat 领域的 plugin 点调用语义,保持 service 边界稳定。
7
- */
8
-
9
- import type { AgentContext } from "@downcity/agent";
10
- import type { JsonObject } from "@downcity/agent";
11
- import type {
12
- ChatAuthorizationObservePrincipalPayload,
13
- ChatAuthorizationEvaluateInput,
14
- ChatAuthorizationRole,
15
- } from "@/auth/types/AuthPlugin.js";
16
- import type { ChatDispatchChannel } from "@/chat/types/ChatDispatcher.js";
17
- import { CHAT_PLUGIN_POINTS } from "@/chat/runtime/PluginPoints.js";
18
-
19
- function toRecord(value: unknown): Record<string, unknown> {
20
- if (!value || typeof value !== "object" || Array.isArray(value)) return {};
21
- return value as Record<string, unknown>;
22
- }
23
-
24
- /**
25
- * 观测入站主体。
26
- */
27
- export async function observeIncomingChatPrincipal(params: {
28
- context: AgentContext;
29
- channel: ChatDispatchChannel;
30
- chatId: string;
31
- chatType?: string;
32
- chatTitle?: string;
33
- userId?: string;
34
- username?: string;
35
- }): Promise<void> {
36
- await params.context.plugins.effect(CHAT_PLUGIN_POINTS.observePrincipal, {
37
- channel: params.channel,
38
- chatId: params.chatId,
39
- ...(params.chatType ? { chatType: params.chatType } : {}),
40
- ...(params.chatTitle ? { chatTitle: params.chatTitle } : {}),
41
- ...(params.userId ? { userId: params.userId } : {}),
42
- ...(params.username ? { username: params.username } : {}),
43
- } as ChatAuthorizationObservePrincipalPayload as unknown as JsonObject);
44
- }
45
-
46
- /**
47
- * 校验入站消息是否允许执行。
48
- *
49
- * 关键点(中文)
50
- * - 这是 guard 语义:允许则静默返回,拒绝则直接抛错。
51
- * - 这样 chat plugin runtime 可以像处理中间件一样串联 guard 点。
52
- */
53
- export async function guardIncomingChat(params: {
54
- context: AgentContext;
55
- channel: ChatDispatchChannel;
56
- input: ChatAuthorizationEvaluateInput;
57
- }): Promise<void> {
58
- await params.context.plugins.guard(
59
- CHAT_PLUGIN_POINTS.authorizeIncoming,
60
- {
61
- channel: params.channel,
62
- chatId: params.input.chatId,
63
- ...(params.input.chatType ? { chatType: params.input.chatType } : {}),
64
- ...(params.input.userId ? { userId: params.input.userId } : {}),
65
- ...(params.input.username ? { username: params.input.username } : {}),
66
- ...(params.input.chatTitle ? { chatTitle: params.input.chatTitle } : {}),
67
- } as JsonObject,
68
- );
69
- }
70
-
71
- /**
72
- * 解析用户角色。
73
- */
74
- export async function resolveIncomingChatUserRole(params: {
75
- context: AgentContext;
76
- channel: ChatDispatchChannel;
77
- userId?: string;
78
- }): Promise<ChatAuthorizationRole | undefined> {
79
- const result = await params.context.plugins.resolve<JsonObject, unknown>(
80
- CHAT_PLUGIN_POINTS.resolveUserRole,
81
- {
82
- channel: params.channel,
83
- ...(params.userId ? { userId: params.userId } : {}),
84
- },
85
- );
86
- const role = toRecord(result);
87
- if (!role.roleId) return undefined;
88
- return role as unknown as ChatAuthorizationRole;
89
- }