@nocobase/portal-template-default 1.0.0 → 1.0.2

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 (194) hide show
  1. package/README.MD +90 -1
  2. package/components.json +3 -1
  3. package/package.json +21 -5
  4. package/registry/nocobase-acl/README.md +19 -0
  5. package/registry/nocobase-acl/components/acl-boundary.tsx +106 -0
  6. package/registry/nocobase-acl/components/index.ts +3 -0
  7. package/registry/nocobase-acl/components/role-options.ts +36 -0
  8. package/registry/nocobase-acl/components/role-switcher.tsx +135 -0
  9. package/registry/nocobase-acl/demo/boundary-api.tsx +186 -0
  10. package/registry/nocobase-acl/demo/components.tsx +231 -0
  11. package/registry/nocobase-acl/demo/index.tsx +505 -0
  12. package/registry/nocobase-acl/demo/lazy-route.tsx +10 -0
  13. package/registry/nocobase-acl/demo/prompt-generator.tsx +134 -0
  14. package/registry/nocobase-acl/demo/role-switcher-prompt-generator.tsx +198 -0
  15. package/registry/nocobase-acl/demo/scenario-section.tsx +37 -0
  16. package/registry/nocobase-acl/extension.tsx +71 -0
  17. package/registry/nocobase-acl/index.ts +1 -0
  18. package/registry/nocobase-acl/tests/acl-regression.mjs +314 -0
  19. package/registry/nocobase-ai/README.md +70 -0
  20. package/registry/nocobase-ai/adapters/react-hook-form.ts +20 -0
  21. package/registry/nocobase-ai/assets/nocobase-ai-chat.svg +50 -0
  22. package/registry/nocobase-ai/components/ai-root-provider.tsx +32 -0
  23. package/registry/nocobase-ai/components/chat/ai-employee-avatar.tsx +31 -0
  24. package/registry/nocobase-ai/components/chat/chat-attachment.tsx +92 -0
  25. package/registry/nocobase-ai/components/chat/chat-compact.tsx +90 -0
  26. package/registry/nocobase-ai/components/chat/chat-composer.tsx +429 -0
  27. package/registry/nocobase-ai/components/chat/chat-empty-state.tsx +51 -0
  28. package/registry/nocobase-ai/components/chat/chat-header.tsx +88 -0
  29. package/registry/nocobase-ai/components/chat/chat-history-dialog.tsx +77 -0
  30. package/registry/nocobase-ai/components/chat/chat-message.tsx +252 -0
  31. package/registry/nocobase-ai/components/chat/chat-messages.tsx +173 -0
  32. package/registry/nocobase-ai/components/chat/chat-window.tsx +118 -0
  33. package/registry/nocobase-ai/components/chat/conversation-list.tsx +343 -0
  34. package/registry/nocobase-ai/components/chat/markdown-message.tsx +165 -0
  35. package/registry/nocobase-ai/components/chat/model-select-options.tsx +30 -0
  36. package/registry/nocobase-ai/components/chat/reasoning-panel.tsx +29 -0
  37. package/registry/nocobase-ai/components/chat/sub-agent-conversation.tsx +142 -0
  38. package/registry/nocobase-ai/components/chat/tool-call-card.tsx +316 -0
  39. package/registry/nocobase-ai/components/chat/user-prompt-editor.tsx +99 -0
  40. package/registry/nocobase-ai/components/chat/work-context-chip.tsx +114 -0
  41. package/registry/nocobase-ai/components/index.ts +59 -0
  42. package/registry/nocobase-ai/components/page-elements/ai-form.tsx +44 -0
  43. package/registry/nocobase-ai/components/page-elements/page-element-provider.tsx +469 -0
  44. package/registry/nocobase-ai/components/surfaces/chat-dialog.tsx +31 -0
  45. package/registry/nocobase-ai/components/surfaces/chat-inline.tsx +18 -0
  46. package/registry/nocobase-ai/components/surfaces/chat-page.tsx +18 -0
  47. package/registry/nocobase-ai/components/surfaces/chat-side-panel-layout.tsx +54 -0
  48. package/registry/nocobase-ai/components/surfaces/chat-side-panel.tsx +74 -0
  49. package/registry/nocobase-ai/components/surfaces/chat-surface-actions.tsx +54 -0
  50. package/registry/nocobase-ai/components/tools/builtin-tool-renderers.tsx +31 -0
  51. package/registry/nocobase-ai/components/tools/business-report-dialog.tsx +364 -0
  52. package/registry/nocobase-ai/components/tools/business-report-renderer.tsx +122 -0
  53. package/registry/nocobase-ai/components/tools/business-report-utils.tsx +282 -0
  54. package/registry/nocobase-ai/components/tools/chart-renderer.tsx +99 -0
  55. package/registry/nocobase-ai/components/tools/echarts-preview.tsx +59 -0
  56. package/registry/nocobase-ai/components/tools/echarts-runtime-advanced.ts +18 -0
  57. package/registry/nocobase-ai/components/tools/echarts-runtime-common.ts +20 -0
  58. package/registry/nocobase-ai/components/tools/echarts-runtime-components.ts +34 -0
  59. package/registry/nocobase-ai/components/tools/echarts-runtime-hierarchy.ts +24 -0
  60. package/registry/nocobase-ai/components/tools/echarts-runtime.ts +138 -0
  61. package/registry/nocobase-ai/components/tools/sub-agent-renderer.tsx +67 -0
  62. package/registry/nocobase-ai/components/tools/suggestions-renderer.tsx +59 -0
  63. package/registry/nocobase-ai/components/tools/tool-renderer-provider.tsx +55 -0
  64. package/registry/nocobase-ai/components/tools/tool-renderer-utils.ts +18 -0
  65. package/registry/nocobase-ai/components/tools/workflow-renderer.tsx +108 -0
  66. package/registry/nocobase-ai/components/triggers/ai-chat-floating-trigger.tsx +73 -0
  67. package/registry/nocobase-ai/components/triggers/ai-employee-shortcut.tsx +149 -0
  68. package/registry/nocobase-ai/demo/configuration-gate.tsx +49 -0
  69. package/registry/nocobase-ai/demo/container-showcase.tsx +210 -0
  70. package/registry/nocobase-ai/demo/floating.tsx +134 -0
  71. package/registry/nocobase-ai/demo/index.tsx +346 -0
  72. package/registry/nocobase-ai/demo/interaction-showcase.tsx +151 -0
  73. package/registry/nocobase-ai/demo/page-context-prompt-generator.tsx +355 -0
  74. package/registry/nocobase-ai/demo/page-context.tsx +884 -0
  75. package/registry/nocobase-ai/demo/page-element-showcase.tsx +197 -0
  76. package/registry/nocobase-ai/demo/prompt-card.tsx +41 -0
  77. package/registry/nocobase-ai/demo/prompt-generator.tsx +452 -0
  78. package/registry/nocobase-ai/demo/shortcut.tsx +1207 -0
  79. package/registry/nocobase-ai/demo/tool-cards.tsx +530 -0
  80. package/registry/nocobase-ai/extension.tsx +158 -0
  81. package/registry/nocobase-ai/global-ai-chat.tsx +213 -0
  82. package/registry/nocobase-ai/index.ts +3 -0
  83. package/registry/nocobase-ai/providers/ai-provider.tsx +323 -0
  84. package/registry/nocobase-ai/providers/avatars.ts +817 -0
  85. package/registry/nocobase-ai/providers/chat-context.tsx +109 -0
  86. package/registry/nocobase-ai/providers/chat-controller.ts +84 -0
  87. package/registry/nocobase-ai/providers/chat-message-utils.ts +198 -0
  88. package/registry/nocobase-ai/providers/chat-provider.tsx +871 -0
  89. package/registry/nocobase-ai/providers/chat-reducer.ts +147 -0
  90. package/registry/nocobase-ai/providers/chat-task-utils.ts +105 -0
  91. package/registry/nocobase-ai/providers/chat-transport.ts +184 -0
  92. package/registry/nocobase-ai/providers/form-registry.tsx +271 -0
  93. package/registry/nocobase-ai/providers/frontend-tool-registry.tsx +231 -0
  94. package/registry/nocobase-ai/providers/index.ts +61 -0
  95. package/registry/nocobase-ai/providers/model.ts +32 -0
  96. package/registry/nocobase-ai/providers/page-context.tsx +136 -0
  97. package/registry/nocobase-ai/providers/stream-coalescer.ts +60 -0
  98. package/registry/nocobase-ai/providers/stream-event-utils.ts +82 -0
  99. package/registry/nocobase-ai/providers/stream-parser.ts +61 -0
  100. package/registry/nocobase-ai/providers/sub-agent-stream.ts +316 -0
  101. package/registry/nocobase-ai/providers/types.ts +183 -0
  102. package/registry/nocobase-ai/providers/ui-message-stream.ts +463 -0
  103. package/registry/nocobase-ai/providers/use-automatic-tool-approval.ts +94 -0
  104. package/registry/nocobase-ai/providers/use-chat-attachments.ts +206 -0
  105. package/registry/nocobase-ai/providers/use-chat-message-actions.ts +554 -0
  106. package/registry/nocobase-ai/providers/use-chat-runtime.ts +210 -0
  107. package/registry/nocobase-ai/providers/use-chat-work-context.ts +93 -0
  108. package/registry/nocobase-ai/providers/use-conversation-catalog.ts +108 -0
  109. package/registry/nocobase-ai/providers/use-conversation-history.ts +178 -0
  110. package/registry/nocobase-ai/services/index.ts +7 -0
  111. package/registry/nocobase-ai/services/nocobase-ai-service.ts +454 -0
  112. package/registry/nocobase-ai/services/types.ts +82 -0
  113. package/registry/nocobase-ai/tests/business-report-regression.mjs +70 -0
  114. package/registry/nocobase-ai/tests/form-filler-regression.mjs +129 -0
  115. package/registry/nocobase-ai/tests/frontend-tool-regression.mjs +128 -0
  116. package/registry/nocobase-ai/tests/page-context-regression.mjs +48 -0
  117. package/registry/nocobase-ai/tests/stream-regression.mjs +207 -0
  118. package/registry/nocobase-i18n/README.md +53 -0
  119. package/registry/nocobase-i18n/components/index.ts +1 -0
  120. package/registry/nocobase-i18n/components/language-switcher.tsx +138 -0
  121. package/registry/nocobase-i18n/demo/index.tsx +209 -0
  122. package/registry/nocobase-i18n/demo/prompt-generator.tsx +155 -0
  123. package/registry/nocobase-i18n/extension.tsx +46 -0
  124. package/registry/nocobase-i18n/i18n-provider.ts +9 -0
  125. package/registry/nocobase-i18n/index.ts +6 -0
  126. package/registry/nocobase-i18n/locale-store.ts +134 -0
  127. package/registry/nocobase-i18n/locales/en-US.ts +89 -0
  128. package/registry/nocobase-i18n/locales/zh-CN.ts +85 -0
  129. package/registry/nocobase-i18n/provider.tsx +32 -0
  130. package/registry/nocobase-i18n/runtime.ts +118 -0
  131. package/registry/nocobase-i18n/server-resources.ts +184 -0
  132. package/registry/nocobase-i18n/tests/i18n-regression.mjs +42 -0
  133. package/registry.config.json +163 -0
  134. package/scripts/registry.mjs +164 -0
  135. package/src/App.css +95 -0
  136. package/src/App.tsx +87 -69
  137. package/src/app/extension.ts +11 -0
  138. package/src/app/extensions.tsx +49 -0
  139. package/src/components/access-control/access-denied.tsx +30 -0
  140. package/src/components/access-control/acl-bootstrap.tsx +9 -0
  141. package/src/components/access-control/acl-gate.tsx +36 -0
  142. package/src/components/access-control/navigate-to-accessible-resource.tsx +17 -0
  143. package/src/components/access-control/resource-access-guard.tsx +21 -0
  144. package/src/components/access-control/use-acl-runtime.ts +28 -0
  145. package/src/components/app-shell/breadcrumb.tsx +39 -3
  146. package/src/components/app-shell/document-title-handler.tsx +11 -20
  147. package/src/components/app-shell/header.tsx +22 -4
  148. package/src/components/app-shell/loading-overlay.tsx +2 -4
  149. package/src/components/app-shell/loading-state.tsx +15 -0
  150. package/src/components/app-shell/sidebar.tsx +103 -45
  151. package/src/components/data-table/data-table-pagination.tsx +27 -7
  152. package/src/components/data-table/data-table-styles.ts +57 -0
  153. package/src/components/data-table/data-table.tsx +14 -59
  154. package/src/components/resources/resource-label.ts +101 -0
  155. package/src/components/resources/views/create-view.tsx +17 -14
  156. package/src/components/resources/views/edit-view.tsx +17 -15
  157. package/src/components/resources/views/list-view.tsx +20 -10
  158. package/src/components/resources/views/show-view.tsx +20 -18
  159. package/src/index.tsx +2 -3
  160. package/src/lib/i18n.ts +161 -0
  161. package/src/lib/nocobase/acl/action.ts +147 -0
  162. package/src/lib/nocobase/acl/data-source.ts +24 -0
  163. package/src/lib/nocobase/acl/index.ts +6 -0
  164. package/src/lib/nocobase/acl/menu.ts +41 -0
  165. package/src/lib/nocobase/acl/record-permissions.ts +111 -0
  166. package/src/lib/nocobase/acl/store.ts +182 -0
  167. package/src/lib/nocobase/acl/types.ts +96 -0
  168. package/src/lib/nocobase/client.ts +279 -0
  169. package/src/lib/nocobase/error.ts +32 -0
  170. package/src/locales/en-US.ts +82 -0
  171. package/src/locales/index.ts +13 -0
  172. package/src/locales/zh-CN.ts +79 -0
  173. package/src/pages/users/create.tsx +98 -0
  174. package/src/pages/users/edit.tsx +92 -0
  175. package/src/pages/users/form-context.ts +57 -0
  176. package/src/pages/users/form-fields.tsx +211 -0
  177. package/src/pages/users/list.tsx +205 -0
  178. package/src/pages/users/show.tsx +187 -0
  179. package/src/pages/users/types.ts +20 -0
  180. package/src/providers/access-control.ts +34 -0
  181. package/src/providers/auth.ts +74 -60
  182. package/src/providers/constants.ts +3 -37
  183. package/src/providers/data.ts +242 -85
  184. package/vite.config.ts +17 -0
  185. package/src/pages/blog-posts/create.tsx +0 -177
  186. package/src/pages/blog-posts/edit.tsx +0 -197
  187. package/src/pages/blog-posts/list.tsx +0 -131
  188. package/src/pages/blog-posts/show.tsx +0 -81
  189. package/src/pages/categories/create.tsx +0 -73
  190. package/src/pages/categories/edit.tsx +0 -73
  191. package/src/pages/categories/index.ts +0 -4
  192. package/src/pages/categories/list.tsx +0 -86
  193. package/src/pages/categories/show.tsx +0 -37
  194. /package/src/pages/{blog-posts → users}/index.ts +0 -0
@@ -0,0 +1,316 @@
1
+ import type { InferUIMessageChunk } from "ai";
2
+ import type { AIChatMessage, AISubAgentConversation } from "./types";
3
+ import type { NocoBaseStreamEvent } from "./stream-parser";
4
+ import {
5
+ getToolCallState,
6
+ getToolProviderMetadata,
7
+ isRecord,
8
+ parseToolInput,
9
+ toolCallsFromEvent,
10
+ type NocoBaseToolCall,
11
+ } from "./stream-event-utils";
12
+
13
+ type AIChatChunk = InferUIMessageChunk<AIChatMessage>;
14
+
15
+ const createMessage = (username: string): AIChatMessage => ({
16
+ id: `sub-agent-${crypto.randomUUID()}`,
17
+ role: "assistant",
18
+ metadata: {
19
+ createdAt: new Date().toISOString(),
20
+ employeeUsername: username,
21
+ },
22
+ parts: [],
23
+ });
24
+
25
+ const updateLastMessage = (
26
+ conversation: AISubAgentConversation,
27
+ updater: (message: AIChatMessage) => AIChatMessage
28
+ ) => {
29
+ const messages = conversation.messages.length
30
+ ? [...conversation.messages]
31
+ : [createMessage(conversation.username)];
32
+ messages[messages.length - 1] = updater(messages[messages.length - 1]);
33
+ return { ...conversation, messages };
34
+ };
35
+
36
+ const appendNarrative = (
37
+ message: AIChatMessage,
38
+ type: "text" | "reasoning",
39
+ delta: string
40
+ ): AIChatMessage => {
41
+ const parts = [...message.parts];
42
+ const last = parts.at(-1);
43
+ if (last?.type === type) {
44
+ parts[parts.length - 1] = {
45
+ ...last,
46
+ text: `${last.text}${delta}`,
47
+ state: "streaming",
48
+ };
49
+ } else {
50
+ parts.push({ type, text: delta, state: "streaming" });
51
+ }
52
+ return { ...message, parts };
53
+ };
54
+
55
+ const toToolPart = (
56
+ toolCall: NocoBaseToolCall
57
+ ): AIChatMessage["parts"][number] => {
58
+ const toolCallId = toolCall.id ?? `tool-${crypto.randomUUID()}`;
59
+ const toolName = toolCall.name ?? "tool";
60
+ const input = parseToolInput(toolCall.args ?? toolCall.input ?? {});
61
+ const state = getToolCallState(toolCall);
62
+ const callProviderMetadata = getToolProviderMetadata(toolCall);
63
+ if (state.failed) {
64
+ return {
65
+ type: "dynamic-tool",
66
+ toolCallId,
67
+ toolName,
68
+ state: "output-error",
69
+ input,
70
+ errorText: String(
71
+ toolCall.content ?? toolCall.output ?? "Tool call failed"
72
+ ),
73
+ callProviderMetadata,
74
+ };
75
+ }
76
+ if (state.completed) {
77
+ return {
78
+ type: "dynamic-tool",
79
+ toolCallId,
80
+ toolName,
81
+ state: "output-available",
82
+ input,
83
+ output: toolCall.output ?? toolCall.content ?? { status: "completed" },
84
+ callProviderMetadata,
85
+ };
86
+ }
87
+ return {
88
+ type: "dynamic-tool",
89
+ toolCallId,
90
+ toolName,
91
+ state: "input-available",
92
+ input,
93
+ callProviderMetadata,
94
+ };
95
+ };
96
+
97
+ const updateToolCalls = (
98
+ message: AIChatMessage,
99
+ toolCalls: NocoBaseToolCall[]
100
+ ) => {
101
+ const parts = [...message.parts];
102
+ for (const toolCall of toolCalls) {
103
+ const nextPart = toToolPart(toolCall);
104
+ if (nextPart.type !== "dynamic-tool") continue;
105
+ const index = parts.findIndex(
106
+ (part) =>
107
+ part.type === "dynamic-tool" && part.toolCallId === nextPart.toolCallId
108
+ );
109
+ if (index >= 0) parts[index] = nextPart;
110
+ else parts.push(nextPart);
111
+ }
112
+ return { ...message, parts };
113
+ };
114
+
115
+ const finishMessages = (messages: AIChatMessage[]) =>
116
+ messages.map((message) => ({
117
+ ...message,
118
+ parts: message.parts.map((part) =>
119
+ part.type === "text" || part.type === "reasoning"
120
+ ? { ...part, state: "done" as const }
121
+ : part
122
+ ),
123
+ }));
124
+
125
+ export class SubAgentStreamAccumulator {
126
+ private readonly conversations = new Map<string, AISubAgentConversation>();
127
+ private readonly toolStreams = new Map<
128
+ string,
129
+ {
130
+ currentId?: string;
131
+ idsByIndex: Map<number, string>;
132
+ names: Map<string, string>;
133
+ inputs: Map<string, unknown>;
134
+ }
135
+ >();
136
+
137
+ constructor(seedMessage?: AIChatMessage) {
138
+ if (!seedMessage) return;
139
+ const seed = (messages: AIChatMessage[]) => {
140
+ for (const message of messages) {
141
+ for (const part of message.parts) {
142
+ if (part.type !== "data-subAgent") continue;
143
+ this.conversations.set(part.data.sessionId, structuredClone(part.data));
144
+ seed(part.data.messages);
145
+ }
146
+ }
147
+ };
148
+ seed([seedMessage]);
149
+ }
150
+
151
+ private getToolStream(sessionId: string) {
152
+ const existing = this.toolStreams.get(sessionId);
153
+ if (existing) return existing;
154
+ const created = {
155
+ currentId: undefined,
156
+ idsByIndex: new Map<number, string>(),
157
+ names: new Map<string, string>(),
158
+ inputs: new Map<string, unknown>(),
159
+ };
160
+ this.toolStreams.set(sessionId, created);
161
+ return created;
162
+ }
163
+
164
+ private normalizeToolChunk(
165
+ sessionId: string,
166
+ chunk: NocoBaseToolCall
167
+ ): NocoBaseToolCall | undefined {
168
+ const stream = this.getToolStream(sessionId);
169
+ const indexedId =
170
+ typeof chunk.index === "number"
171
+ ? stream.idsByIndex.get(chunk.index)
172
+ : undefined;
173
+ const toolCallId = chunk.id ?? indexedId ?? stream.currentId;
174
+ if (!toolCallId) return undefined;
175
+
176
+ stream.currentId = toolCallId;
177
+ if (typeof chunk.index === "number") {
178
+ stream.idsByIndex.set(chunk.index, toolCallId);
179
+ }
180
+ if (chunk.name) stream.names.set(toolCallId, chunk.name);
181
+
182
+ if (typeof chunk.args === "string") {
183
+ const previous = stream.inputs.get(toolCallId);
184
+ stream.inputs.set(
185
+ toolCallId,
186
+ `${typeof previous === "string" ? previous : ""}${chunk.args}`
187
+ );
188
+ } else if (chunk.args !== undefined) {
189
+ stream.inputs.set(toolCallId, chunk.args);
190
+ } else if (chunk.input !== undefined) {
191
+ stream.inputs.set(toolCallId, chunk.input);
192
+ }
193
+
194
+ return {
195
+ ...chunk,
196
+ id: toolCallId,
197
+ name: chunk.name ?? stream.names.get(toolCallId) ?? "tool",
198
+ args: stream.inputs.get(toolCallId),
199
+ };
200
+ }
201
+
202
+ private normalizeToolCall(
203
+ sessionId: string,
204
+ toolCall: NocoBaseToolCall
205
+ ): NocoBaseToolCall {
206
+ const stream = this.getToolStream(sessionId);
207
+ const toolCallId =
208
+ toolCall.id ?? stream.currentId ?? `tool-${crypto.randomUUID()}`;
209
+ stream.currentId = toolCallId;
210
+ if (typeof toolCall.index === "number") {
211
+ stream.idsByIndex.set(toolCall.index, toolCallId);
212
+ }
213
+ const toolName = toolCall.name ?? stream.names.get(toolCallId) ?? "tool";
214
+ stream.names.set(toolCallId, toolName);
215
+ const input = toolCall.args ?? toolCall.input;
216
+ if (input !== undefined) stream.inputs.set(toolCallId, input);
217
+ return {
218
+ ...toolCall,
219
+ id: toolCallId,
220
+ name: toolName,
221
+ args: input ?? stream.inputs.get(toolCallId),
222
+ };
223
+ }
224
+
225
+ process(event: NocoBaseStreamEvent): AIChatChunk[] {
226
+ if (event.from !== "sub-agent" || !event.sessionId) return [];
227
+ const username = event.username || "sub-agent";
228
+ let conversation = this.conversations.get(event.sessionId) ?? {
229
+ sessionId: event.sessionId,
230
+ username,
231
+ status: "pending" as const,
232
+ messages: [],
233
+ };
234
+
235
+ if (event.type === "new_message") {
236
+ this.getToolStream(event.sessionId).currentId = undefined;
237
+ conversation = {
238
+ ...conversation,
239
+ username,
240
+ messages: [...conversation.messages, createMessage(username)],
241
+ };
242
+ } else if (event.type === "content" && typeof event.body === "string") {
243
+ conversation = updateLastMessage(conversation, (message) =>
244
+ appendNarrative(message, "text", event.body as string)
245
+ );
246
+ } else if (event.type === "reasoning" && isRecord(event.body)) {
247
+ const content = event.body.content;
248
+ if (typeof content === "string" && content) {
249
+ conversation = updateLastMessage(conversation, (message) =>
250
+ appendNarrative(message, "reasoning", content)
251
+ );
252
+ }
253
+ } else if (event.type === "tool_call_chunks") {
254
+ const toolCalls = toolCallsFromEvent(event).flatMap((toolCall) => {
255
+ const normalized = this.normalizeToolChunk(event.sessionId!, toolCall);
256
+ return normalized ? [normalized] : [];
257
+ });
258
+ if (toolCalls.length) {
259
+ conversation = updateLastMessage(conversation, (message) =>
260
+ updateToolCalls(message, toolCalls)
261
+ );
262
+ }
263
+ } else if (event.type === "tool_calls") {
264
+ const toolCalls = toolCallsFromEvent(event).map((toolCall) =>
265
+ this.normalizeToolCall(event.sessionId!, toolCall)
266
+ );
267
+ if (toolCalls.length) {
268
+ conversation = updateLastMessage(conversation, (message) =>
269
+ updateToolCalls(message, toolCalls)
270
+ );
271
+ }
272
+ } else if (event.type === "tool_call_status" && isRecord(event.body)) {
273
+ const rawToolCall = isRecord(event.body.toolCall)
274
+ ? ({
275
+ ...event.body.toolCall,
276
+ invokeStatus:
277
+ event.body.invokeStatus ?? event.body.toolCall.invokeStatus,
278
+ status: event.body.status ?? event.body.toolCall.status,
279
+ content: event.body.content ?? event.body.toolCall.content,
280
+ } as NocoBaseToolCall)
281
+ : undefined;
282
+ const toolCall = rawToolCall
283
+ ? this.normalizeToolCall(event.sessionId, rawToolCall)
284
+ : undefined;
285
+ if (toolCall) {
286
+ conversation = updateLastMessage(conversation, (message) => {
287
+ const updated = updateToolCalls(message, [toolCall]);
288
+ return toolCall.messageId
289
+ ? {
290
+ ...updated,
291
+ metadata: {
292
+ ...updated.metadata,
293
+ serverMessageId: String(toolCall.messageId),
294
+ },
295
+ }
296
+ : updated;
297
+ });
298
+ }
299
+ } else if (event.type === "sub_agent_completed") {
300
+ conversation = {
301
+ ...conversation,
302
+ status: "completed",
303
+ messages: finishMessages(conversation.messages),
304
+ };
305
+ }
306
+
307
+ this.conversations.set(event.sessionId, conversation);
308
+ return [
309
+ {
310
+ type: "data-subAgent",
311
+ id: event.sessionId,
312
+ data: conversation,
313
+ },
314
+ ];
315
+ }
316
+ }
@@ -0,0 +1,183 @@
1
+ import type { ChatTransport, UIMessage } from "ai";
2
+
3
+ export const AI_DRAFT_CONVERSATION_ID = "__draft__";
4
+
5
+ export type AIConfigurationStatus = "loading" | "ready" | "error";
6
+
7
+ export type AIEmployee = {
8
+ username: string;
9
+ nickname: string;
10
+ position?: string;
11
+ bio?: string;
12
+ greeting?: string;
13
+ description?: string;
14
+ avatar?: string;
15
+ category?: string;
16
+ deprecated?: boolean;
17
+ builtIn?: boolean;
18
+ userConfig?: {
19
+ prompt?: string;
20
+ };
21
+ modelSettings?: {
22
+ enabled?: boolean;
23
+ llmService?: string;
24
+ model?: string;
25
+ models?: Array<{
26
+ llmService?: string;
27
+ model?: string;
28
+ }>;
29
+ };
30
+ };
31
+
32
+ export type AIChatAttachmentStatus = "uploading" | "done" | "error";
33
+
34
+ export type AIChatAttachment = {
35
+ uid: string;
36
+ filename: string;
37
+ status: AIChatAttachmentStatus;
38
+ size?: number;
39
+ mimetype?: string;
40
+ url?: string;
41
+ preview?: string;
42
+ progress?: number;
43
+ error?: string;
44
+ source?: unknown;
45
+ [key: string]: unknown;
46
+ };
47
+
48
+ export type AIModel = {
49
+ value: string;
50
+ label: string;
51
+ llmService?: string;
52
+ llmServiceTitle?: string;
53
+ supportWebSearch?: boolean;
54
+ isToolConflict?: boolean;
55
+ configured?: boolean;
56
+ };
57
+
58
+ export type AIConversation = {
59
+ id: string;
60
+ title: string;
61
+ employeeUsername: string;
62
+ updatedAt: string;
63
+ unread?: boolean;
64
+ model?: {
65
+ llmService?: string;
66
+ model: string;
67
+ };
68
+ };
69
+
70
+ export type AIWorkContextItem = {
71
+ type: string;
72
+ id?: string;
73
+ title?: string;
74
+ content?: unknown;
75
+ [key: string]: unknown;
76
+ };
77
+
78
+ export type AIEmployeeTask = {
79
+ title?: string;
80
+ message?: {
81
+ system?: string;
82
+ user?: string;
83
+ workContext?: AIWorkContextItem[];
84
+ };
85
+ autoSend?: boolean;
86
+ skillSettings?: {
87
+ skills?: string[];
88
+ tools?: string[];
89
+ };
90
+ webSearch?: boolean;
91
+ model?: {
92
+ llmService?: string;
93
+ model: string;
94
+ };
95
+ };
96
+
97
+ export type AIEmployeeTasks = Record<string, AIEmployeeTask[]>;
98
+
99
+ export type AIEmployeeTaskTrigger = {
100
+ aiEmployee: string | AIEmployee;
101
+ task?: AIEmployeeTask;
102
+ tasks?: AIEmployeeTask[];
103
+ context?: AIWorkContextItem[];
104
+ auto?: boolean;
105
+ open?: boolean;
106
+ };
107
+
108
+ export type AIChatTaskRuntime = {
109
+ systemMessage?: string;
110
+ workContext: AIWorkContextItem[];
111
+ skillSettings?: AIEmployeeTask["skillSettings"];
112
+ webSearch?: boolean;
113
+ };
114
+
115
+ export type AIChatMessageMetadata = {
116
+ createdAt?: string;
117
+ employeeUsername?: string;
118
+ toolApprovals?: Record<string, AIToolCallApproval>;
119
+ serverMessageId?: string;
120
+ editingMessageId?: string;
121
+ attachments?: AIChatAttachment[];
122
+ workContext?: AIWorkContextItem[];
123
+ };
124
+
125
+ export type AIToolCallApproval = {
126
+ required: boolean;
127
+ status?: "pending" | "approved" | "rejected";
128
+ };
129
+
130
+ export type AIToolCallDecision = {
131
+ messageId: string;
132
+ toolCallId: string;
133
+ toolName: string;
134
+ decision: "approve" | "reject" | "edit";
135
+ input?: unknown;
136
+ };
137
+
138
+ export type AIToolCallInvocationContext = {
139
+ sessionId: string;
140
+ messageId: string;
141
+ toolCallId: string;
142
+ toolName: string;
143
+ allowedFrontendToolIds?: string[];
144
+ allowedFormIds?: string[];
145
+ automatic?: boolean;
146
+ };
147
+
148
+ export type AIToolInvoker = (
149
+ input: unknown,
150
+ context: AIToolCallInvocationContext
151
+ ) => unknown | Promise<unknown>;
152
+
153
+ export type AIToolInvokerMap = Record<string, AIToolInvoker>;
154
+
155
+ export type AISubAgentConversation = {
156
+ sessionId: string;
157
+ username: string;
158
+ status: "pending" | "completed";
159
+ messages: AIChatMessage[];
160
+ };
161
+
162
+ export type AIChatDataParts = {
163
+ subAgent: AISubAgentConversation;
164
+ };
165
+
166
+ export type AIChatMessage = UIMessage<AIChatMessageMetadata, AIChatDataParts>;
167
+
168
+ export type AIChatRequestContext = {
169
+ sessionId?: string;
170
+ employee: AIEmployee;
171
+ model: AIModel;
172
+ task?: AIChatTaskRuntime;
173
+ };
174
+
175
+ export type AITransportFactoryOptions = {
176
+ chatId: string;
177
+ getContext: () => AIChatRequestContext;
178
+ onSessionCreated?: (sessionId: string) => void;
179
+ };
180
+
181
+ export type AITransportFactory = (
182
+ options: AITransportFactoryOptions
183
+ ) => ChatTransport<AIChatMessage>;