@nocobase/portal-template-default 1.0.1 → 1.0.3
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/README.MD +22 -3
- package/package.json +4 -2
- package/registry/nocobase-acl/components/role-options.ts +7 -0
- package/registry/nocobase-acl/components/role-switcher.tsx +9 -4
- package/registry/nocobase-acl/demo/boundary-api.tsx +87 -16
- package/registry/nocobase-acl/demo/components.tsx +32 -11
- package/registry/nocobase-acl/demo/index.tsx +45 -14
- package/registry/nocobase-acl/demo/lazy-route.tsx +2 -7
- package/registry/nocobase-acl/demo/prompt-generator.tsx +52 -33
- package/registry/nocobase-acl/demo/role-switcher-prompt-generator.tsx +28 -9
- package/registry/nocobase-acl/extension.tsx +1 -1
- package/registry/nocobase-acl/tests/acl-regression.mjs +7 -6
- package/registry/nocobase-ai/components/chat/chat-messages.tsx +3 -4
- package/registry/nocobase-ai/components/chat/conversation-list.tsx +2 -4
- package/registry/nocobase-ai/components/tools/business-report-dialog.tsx +16 -19
- package/registry/nocobase-ai/components/tools/chart-renderer.tsx +3 -14
- package/registry/nocobase-ai/components/tools/echarts-preview.tsx +2 -5
- package/registry/nocobase-ai/demo/configuration-gate.tsx +9 -12
- package/registry/nocobase-ai/extension.tsx +2 -7
- package/registry/nocobase-i18n/README.md +53 -0
- package/registry/nocobase-i18n/components/index.ts +1 -0
- package/registry/nocobase-i18n/components/language-switcher.tsx +138 -0
- package/registry/nocobase-i18n/demo/index.tsx +209 -0
- package/registry/nocobase-i18n/demo/prompt-generator.tsx +155 -0
- package/registry/nocobase-i18n/extension.tsx +46 -0
- package/registry/nocobase-i18n/i18n-provider.ts +9 -0
- package/registry/nocobase-i18n/index.ts +6 -0
- package/registry/nocobase-i18n/locale-store.ts +134 -0
- package/registry/nocobase-i18n/locales/en-US.ts +89 -0
- package/registry/nocobase-i18n/locales/zh-CN.ts +85 -0
- package/registry/nocobase-i18n/provider.tsx +32 -0
- package/registry/nocobase-i18n/runtime.ts +118 -0
- package/registry/nocobase-i18n/server-resources.ts +184 -0
- package/registry/nocobase-i18n/tests/i18n-regression.mjs +42 -0
- package/registry.config.json +34 -14
- package/scripts/registry.mjs +5 -0
- package/src/App.tsx +27 -86
- package/src/app/extension.ts +3 -1
- package/src/app/extensions.tsx +21 -0
- package/src/components/access-control/acl-gate.tsx +4 -10
- package/src/components/app-shell/breadcrumb.tsx +39 -3
- package/src/components/app-shell/document-title-handler.tsx +11 -20
- package/src/components/app-shell/header.tsx +18 -3
- package/src/components/app-shell/loading-overlay.tsx +2 -4
- package/src/components/app-shell/loading-state.tsx +15 -0
- package/src/components/app-shell/sidebar.tsx +75 -47
- package/src/components/data-table/data-table-pagination.tsx +27 -7
- package/src/components/data-table/data-table-styles.ts +57 -0
- package/src/components/data-table/data-table.tsx +14 -59
- package/src/components/resources/resource-label.ts +101 -0
- package/src/components/resources/views/create-view.tsx +17 -14
- package/src/components/resources/views/edit-view.tsx +17 -15
- package/src/components/resources/views/list-view.tsx +20 -10
- package/src/components/resources/views/show-view.tsx +20 -18
- package/src/extensions/nocobase-acl/README.md +19 -0
- package/src/extensions/nocobase-acl/components/acl-boundary.tsx +106 -0
- package/src/extensions/nocobase-acl/components/index.ts +3 -0
- package/src/extensions/nocobase-acl/components/role-options.ts +36 -0
- package/src/extensions/nocobase-acl/components/role-switcher.tsx +135 -0
- package/src/extensions/nocobase-acl/demo/boundary-api.tsx +186 -0
- package/src/extensions/nocobase-acl/demo/components.tsx +231 -0
- package/src/extensions/nocobase-acl/demo/index.tsx +505 -0
- package/src/extensions/nocobase-acl/demo/lazy-route.tsx +10 -0
- package/src/extensions/nocobase-acl/demo/prompt-generator.tsx +134 -0
- package/src/extensions/nocobase-acl/demo/role-switcher-prompt-generator.tsx +198 -0
- package/src/extensions/nocobase-acl/demo/scenario-section.tsx +37 -0
- package/src/extensions/nocobase-acl/extension.tsx +71 -0
- package/src/extensions/nocobase-acl/index.ts +1 -0
- package/src/extensions/nocobase-ai/README.md +70 -0
- package/src/extensions/nocobase-ai/adapters/react-hook-form.ts +20 -0
- package/src/extensions/nocobase-ai/assets/nocobase-ai-chat.svg +50 -0
- package/src/extensions/nocobase-ai/components/ai-root-provider.tsx +32 -0
- package/src/extensions/nocobase-ai/components/chat/ai-employee-avatar.tsx +31 -0
- package/src/extensions/nocobase-ai/components/chat/chat-attachment.tsx +92 -0
- package/src/extensions/nocobase-ai/components/chat/chat-compact.tsx +90 -0
- package/src/extensions/nocobase-ai/components/chat/chat-composer.tsx +429 -0
- package/src/extensions/nocobase-ai/components/chat/chat-empty-state.tsx +51 -0
- package/src/extensions/nocobase-ai/components/chat/chat-header.tsx +88 -0
- package/src/extensions/nocobase-ai/components/chat/chat-history-dialog.tsx +77 -0
- package/src/extensions/nocobase-ai/components/chat/chat-message.tsx +252 -0
- package/src/extensions/nocobase-ai/components/chat/chat-messages.tsx +173 -0
- package/src/extensions/nocobase-ai/components/chat/chat-window.tsx +118 -0
- package/src/extensions/nocobase-ai/components/chat/conversation-list.tsx +343 -0
- package/src/extensions/nocobase-ai/components/chat/markdown-message.tsx +165 -0
- package/src/extensions/nocobase-ai/components/chat/model-select-options.tsx +30 -0
- package/src/extensions/nocobase-ai/components/chat/reasoning-panel.tsx +29 -0
- package/src/extensions/nocobase-ai/components/chat/sub-agent-conversation.tsx +142 -0
- package/src/extensions/nocobase-ai/components/chat/tool-call-card.tsx +316 -0
- package/src/extensions/nocobase-ai/components/chat/user-prompt-editor.tsx +99 -0
- package/src/extensions/nocobase-ai/components/chat/work-context-chip.tsx +114 -0
- package/src/extensions/nocobase-ai/components/index.ts +59 -0
- package/src/extensions/nocobase-ai/components/page-elements/ai-form.tsx +44 -0
- package/src/extensions/nocobase-ai/components/page-elements/page-element-provider.tsx +469 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-dialog.tsx +31 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-inline.tsx +18 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-page.tsx +18 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-side-panel-layout.tsx +54 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-side-panel.tsx +74 -0
- package/src/extensions/nocobase-ai/components/surfaces/chat-surface-actions.tsx +54 -0
- package/src/extensions/nocobase-ai/components/tools/builtin-tool-renderers.tsx +31 -0
- package/src/extensions/nocobase-ai/components/tools/business-report-dialog.tsx +364 -0
- package/src/extensions/nocobase-ai/components/tools/business-report-renderer.tsx +122 -0
- package/src/extensions/nocobase-ai/components/tools/business-report-utils.tsx +282 -0
- package/src/extensions/nocobase-ai/components/tools/chart-renderer.tsx +99 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-preview.tsx +59 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-runtime-advanced.ts +18 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-runtime-common.ts +20 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-runtime-components.ts +34 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-runtime-hierarchy.ts +24 -0
- package/src/extensions/nocobase-ai/components/tools/echarts-runtime.ts +138 -0
- package/src/extensions/nocobase-ai/components/tools/sub-agent-renderer.tsx +67 -0
- package/src/extensions/nocobase-ai/components/tools/suggestions-renderer.tsx +59 -0
- package/src/extensions/nocobase-ai/components/tools/tool-renderer-provider.tsx +55 -0
- package/src/extensions/nocobase-ai/components/tools/tool-renderer-utils.ts +18 -0
- package/src/extensions/nocobase-ai/components/tools/workflow-renderer.tsx +108 -0
- package/src/extensions/nocobase-ai/components/triggers/ai-chat-floating-trigger.tsx +73 -0
- package/src/extensions/nocobase-ai/components/triggers/ai-employee-shortcut.tsx +149 -0
- package/src/extensions/nocobase-ai/demo/configuration-gate.tsx +49 -0
- package/src/extensions/nocobase-ai/demo/container-showcase.tsx +210 -0
- package/src/extensions/nocobase-ai/demo/floating.tsx +134 -0
- package/src/extensions/nocobase-ai/demo/index.tsx +346 -0
- package/src/extensions/nocobase-ai/demo/interaction-showcase.tsx +151 -0
- package/src/extensions/nocobase-ai/demo/page-context-prompt-generator.tsx +355 -0
- package/src/extensions/nocobase-ai/demo/page-context.tsx +884 -0
- package/src/extensions/nocobase-ai/demo/page-element-showcase.tsx +197 -0
- package/src/extensions/nocobase-ai/demo/prompt-card.tsx +41 -0
- package/src/extensions/nocobase-ai/demo/prompt-generator.tsx +452 -0
- package/src/extensions/nocobase-ai/demo/shortcut.tsx +1207 -0
- package/src/extensions/nocobase-ai/demo/tool-cards.tsx +530 -0
- package/src/extensions/nocobase-ai/extension.tsx +158 -0
- package/src/extensions/nocobase-ai/global-ai-chat.tsx +213 -0
- package/src/extensions/nocobase-ai/index.ts +3 -0
- package/src/extensions/nocobase-ai/providers/ai-provider.tsx +323 -0
- package/src/extensions/nocobase-ai/providers/avatars.ts +817 -0
- package/src/extensions/nocobase-ai/providers/chat-context.tsx +109 -0
- package/src/extensions/nocobase-ai/providers/chat-controller.ts +84 -0
- package/src/extensions/nocobase-ai/providers/chat-message-utils.ts +198 -0
- package/src/extensions/nocobase-ai/providers/chat-provider.tsx +871 -0
- package/src/extensions/nocobase-ai/providers/chat-reducer.ts +147 -0
- package/src/extensions/nocobase-ai/providers/chat-task-utils.ts +105 -0
- package/src/extensions/nocobase-ai/providers/chat-transport.ts +184 -0
- package/src/extensions/nocobase-ai/providers/form-registry.tsx +271 -0
- package/src/extensions/nocobase-ai/providers/frontend-tool-registry.tsx +231 -0
- package/src/extensions/nocobase-ai/providers/index.ts +61 -0
- package/src/extensions/nocobase-ai/providers/model.ts +32 -0
- package/src/extensions/nocobase-ai/providers/page-context.tsx +136 -0
- package/src/extensions/nocobase-ai/providers/stream-coalescer.ts +60 -0
- package/src/extensions/nocobase-ai/providers/stream-event-utils.ts +82 -0
- package/src/extensions/nocobase-ai/providers/stream-parser.ts +61 -0
- package/src/extensions/nocobase-ai/providers/sub-agent-stream.ts +316 -0
- package/src/extensions/nocobase-ai/providers/types.ts +183 -0
- package/src/extensions/nocobase-ai/providers/ui-message-stream.ts +463 -0
- package/src/extensions/nocobase-ai/providers/use-automatic-tool-approval.ts +94 -0
- package/src/extensions/nocobase-ai/providers/use-chat-attachments.ts +206 -0
- package/src/extensions/nocobase-ai/providers/use-chat-message-actions.ts +554 -0
- package/src/extensions/nocobase-ai/providers/use-chat-runtime.ts +210 -0
- package/src/extensions/nocobase-ai/providers/use-chat-work-context.ts +93 -0
- package/src/extensions/nocobase-ai/providers/use-conversation-catalog.ts +108 -0
- package/src/extensions/nocobase-ai/providers/use-conversation-history.ts +178 -0
- package/src/extensions/nocobase-ai/services/index.ts +7 -0
- package/src/extensions/nocobase-ai/services/nocobase-ai-service.ts +454 -0
- package/src/extensions/nocobase-ai/services/types.ts +82 -0
- package/src/index.tsx +2 -3
- package/src/lib/i18n.ts +161 -0
- package/src/lib/nocobase/client.ts +19 -2
- package/src/locales/en-US.ts +82 -0
- package/src/locales/index.ts +13 -0
- package/src/locales/zh-CN.ts +79 -0
- package/src/pages/users/create.tsx +98 -0
- package/src/pages/users/edit.tsx +92 -0
- package/src/pages/users/form-context.ts +57 -0
- package/src/pages/users/form-fields.tsx +211 -0
- package/src/pages/users/list.tsx +205 -0
- package/src/pages/users/show.tsx +187 -0
- package/src/pages/users/types.ts +20 -0
- package/src/providers/constants.ts +1 -0
- package/src/pages/blog-posts/create.tsx +0 -177
- package/src/pages/blog-posts/edit.tsx +0 -197
- package/src/pages/blog-posts/list.tsx +0 -131
- package/src/pages/blog-posts/show.tsx +0 -81
- package/src/pages/categories/create.tsx +0 -73
- package/src/pages/categories/edit.tsx +0 -73
- package/src/pages/categories/index.ts +0 -4
- package/src/pages/categories/list.tsx +0 -86
- package/src/pages/categories/show.tsx +0 -37
- /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>;
|