devnonla-ui 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/app/App.tsx +2 -2
- package/src/app/context.ts +1 -12
- package/src/avatar/avatar.css +107 -0
- package/src/blob-shape/BlobShape.tsx +337 -0
- package/src/button/Button.tsx +4 -4
- package/src/button/ButtonCopy.tsx +3 -3
- package/src/calendar/Calendar.tsx +1 -1
- package/src/card/Card.tsx +1 -1
- package/src/chat/AgentChatbox.tsx +1 -1
- package/src/chat/AgentPanel.tsx +1 -1
- package/src/chat/chatMessageList.tsx +5 -3
- package/src/chat/chatRhythm.ts +8 -0
- package/src/chat/common/createAgentSseSession.ts +9 -3
- package/src/chat/common/sse.ts +290 -122
- package/src/chat/common/useAgentStream.ts +1 -1
- package/src/chat/index.ts +2 -2
- package/src/chat/message-ui/ChatAgentMessage.tsx +4 -3
- package/src/chat/message-ui/ChatError.tsx +5 -4
- package/src/chat/message-ui/ChatInput.tsx +9 -8
- package/src/chat/message-ui/ChatMarkdown.tsx +36 -2
- package/src/chat/message-ui/ChatThinking.tsx +8 -7
- package/src/chat/message-ui/ChatUserMessage.tsx +5 -5
- package/src/chat/tool-ui/BackgroundTaskToolUI.tsx +7 -6
- package/src/chat/tool-ui/BackgroundTasksBar.tsx +9 -9
- package/src/chat/tool-ui/CallAgentToolUI.tsx +16 -15
- package/src/chat/tool-ui/ChatToolCall.tsx +13 -12
- package/src/chat/tool-ui/GetCurrentTimeToolUI.tsx +7 -6
- package/src/chat/tool-ui/ReadSkillToolUI.tsx +10 -9
- package/src/chat/tool-ui/RunJsToolUI.tsx +9 -8
- package/src/chat/tool-ui/ToolUiBadge.tsx +1 -1
- package/src/chat/tool-ui/ToolUiTrailing.tsx +3 -3
- package/src/chat/tool-ui/WebFetchToolUI.tsx +10 -9
- package/src/checkbox/Checkbox.tsx +2 -2
- package/src/codeblock/CodeBlock.tsx +1 -1
- package/src/colorpicker/ColorPicker.tsx +10 -8
- package/src/datepicker/DatePicker.tsx +3 -3
- package/src/desktop/DesktopHeader.tsx +2 -2
- package/src/desktop/DesktopIcon.tsx +3 -4
- package/src/desktop/DesktopStage.tsx +1 -1
- package/src/desktop/DesktopWindow.tsx +3 -3
- package/src/desktop/MeadowDesktop.tsx +2 -2
- package/src/drawer/Drawer.tsx +6 -2
- package/src/dropdown/menuClasses.ts +1 -1
- package/src/empty/Empty.tsx +1 -1
- package/src/form/variants/FieldRadio.tsx +1 -1
- package/src/form/variants/FieldRepeaterItem.tsx +1 -1
- package/src/icon/Icon.tsx +21 -0
- package/src/icon/glyphs.ts +35 -0
- package/src/index.ts +9 -23
- package/src/input/EditableInput.tsx +5 -4
- package/src/input/Input.tsx +15 -7
- package/src/lib/sizes.ts +6 -6
- package/src/lib/surface.ts +14 -4
- package/src/markdown-editor/MarkdownDiff.tsx +4 -4
- package/src/markdown-editor/MarkdownEditor.tsx +4 -4
- package/src/markdown-editor/MarkdownPreview.tsx +6 -6
- package/src/markdown-editor/MarkdownSource.tsx +1 -1
- package/src/markdown-editor/MarkdownTable.tsx +48 -11
- package/src/markdown-editor/MarkdownViewer.tsx +28 -31
- package/src/markdown-editor/MermaidBlock.tsx +5 -5
- package/src/markdown-editor/ReactCodeFrame.tsx +10 -6
- package/src/markdown-editor/ReactCodeSandbox.tsx +2 -2
- package/src/markdown-editor/markdownScale.ts +66 -127
- package/src/menu/Menu.tsx +3 -3
- package/src/message/message.tsx +18 -8
- package/src/modal/Modal.tsx +13 -7
- package/src/pagination/Pagination.tsx +7 -7
- package/src/popover/Popover.tsx +1 -1
- package/src/scroll/OverlayScroll.tsx +13 -5
- package/src/segmented/Segmented.tsx +1 -1
- package/src/select/Select.tsx +4 -4
- package/src/sidebar/Sidebar.tsx +39 -36
- package/src/skeleton/Skeleton.tsx +4 -4
- package/src/spin/Spin.tsx +14 -2
- package/src/splitter/Splitter.tsx +1 -1
- package/src/styles.css +188 -844
- package/src/table/Table.tsx +12 -12
- package/src/tabs/Tabs.tsx +76 -14
- package/src/tag/Tag.tsx +3 -3
- package/src/theme-switcher/ThemeSwitcher.tsx +6 -6
- package/src/theme.ts +53 -147
- package/src/timepicker/TimePicker.tsx +2 -2
- package/src/typography/Typography.tsx +8 -8
- package/src/icon/SolarIcon.tsx +0 -39
- package/src/icon/solar.ts +0 -172
|
@@ -56,17 +56,18 @@ export function createAgentSseSession(args: AgentSseSessionArgs): AgentSseSessio
|
|
|
56
56
|
|
|
57
57
|
const freezeOpen = () => {
|
|
58
58
|
const freezeId = currentId;
|
|
59
|
+
const text = assistantText;
|
|
59
60
|
if (freezeId) {
|
|
60
61
|
const thinkingSnapshot = thinkingRef.current;
|
|
61
62
|
const liveDuration = thinkingStartRef.current > 0 ? thinkingDurationSec(thinkingStartRef.current) : undefined;
|
|
62
|
-
if (
|
|
63
|
+
if (text.trim()) {
|
|
63
64
|
setMessages((prev) =>
|
|
64
65
|
prev.map((m) => {
|
|
65
66
|
if (m.id !== freezeId) return m;
|
|
66
67
|
const existing = typeof m.meta?.thinkingDuration === "number" ? m.meta.thinkingDuration : undefined;
|
|
67
68
|
return {
|
|
68
69
|
...m,
|
|
69
|
-
content:
|
|
70
|
+
content: text,
|
|
70
71
|
streaming: false,
|
|
71
72
|
meta: thinkingSnapshot ? { ...m.meta, thinking: thinkingSnapshot, thinkingDuration: liveDuration ?? existing ?? 0 } : m.meta,
|
|
72
73
|
};
|
|
@@ -142,8 +143,9 @@ export function createAgentSseSession(args: AgentSseSessionArgs): AgentSseSessio
|
|
|
142
143
|
ensureBubble({ content: delta });
|
|
143
144
|
} else {
|
|
144
145
|
assistantText += delta;
|
|
146
|
+
const text = assistantText;
|
|
145
147
|
const targetId = currentId;
|
|
146
|
-
setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, content:
|
|
148
|
+
setMessages((prev) => prev.map((m) => (m.id === targetId ? { ...m, content: text, streaming: true } : m)));
|
|
147
149
|
}
|
|
148
150
|
},
|
|
149
151
|
onThinkingDelta: (delta) => {
|
|
@@ -259,6 +261,10 @@ export function createAgentSseSession(args: AgentSseSessionArgs): AgentSseSessio
|
|
|
259
261
|
});
|
|
260
262
|
emitResult({ toolCallId: event.toolCallId, toolName: event.toolName, output: raw });
|
|
261
263
|
},
|
|
264
|
+
onTextEnd: () => {
|
|
265
|
+
if (!assistantText.trim()) return;
|
|
266
|
+
freezeOpen();
|
|
267
|
+
},
|
|
262
268
|
onDone: () => {
|
|
263
269
|
finishTerminalTools("Tool did not return a result");
|
|
264
270
|
},
|
package/src/chat/common/sse.ts
CHANGED
|
@@ -1,162 +1,330 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* AG-UI SSE subscriber for AgentChatbox.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Wire format comes from `@ag-ui/client` `parseSSEStream`.
|
|
5
|
+
* Each frame is checked with `EventSchemas` from `@ag-ui/core`.
|
|
6
|
+
* This file only maps those events onto chat bubbles.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
+
* Renders TEXT_MESSAGE_*, REASONING_MESSAGE_*, and TOOL_CALL_*.
|
|
9
|
+
* RUN_FINISHED ends the turn. RUN_ERROR shows the error row.
|
|
10
|
+
* RUN_STARTED, STEP_*, STATE_*, MESSAGES_SNAPSHOT, ACTIVITY_*, and RAW are ignored.
|
|
11
|
+
* CUSTOM `tool-meta` and `tool-call-input` update the open tool card.
|
|
8
12
|
*/
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
| { type: "thinking-delta"; text: string }
|
|
13
|
-
| { type: "tool-call"; toolCallId?: string; toolName: string; toolLabel?: string; input: unknown }
|
|
14
|
-
| { type: "tool-result"; toolCallId?: string; toolName: string; result: unknown }
|
|
15
|
-
| { type: "done"; text?: string; reason?: string }
|
|
16
|
-
| { type: "error"; error: string };
|
|
14
|
+
import { parseSSEStream, runHttpRequest } from "@ag-ui/client";
|
|
15
|
+
import { EventSchemas } from "@ag-ui/core/schemas";
|
|
17
16
|
|
|
18
17
|
export interface AgentSseCallbacks {
|
|
19
18
|
onTextDelta: (text: string) => void;
|
|
20
19
|
onThinkingDelta: (text: string) => void;
|
|
21
|
-
onToolCall: (call: { toolCallId?: string; toolName: string; toolLabel?: string; input: unknown }) => void;
|
|
20
|
+
onToolCall: (call: { toolCallId?: string; toolName: string; toolLabel?: string; toolIcon?: string | null; input: unknown }) => void;
|
|
22
21
|
onToolResult: (call: { toolCallId?: string; toolName: string; result: unknown }) => void;
|
|
23
22
|
onDone: (text: string) => void | Promise<void>;
|
|
24
23
|
onError: (error: string) => void | Promise<void>;
|
|
24
|
+
/** Close the current assistant bubble so the next text delta starts another. */
|
|
25
|
+
onTextEnd?: () => void;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
export type ParseSseResult = "done" | "error" | "aborted" | "connection-lost";
|
|
28
29
|
|
|
29
30
|
function readToolCallId(raw: Record<string, unknown>): string | undefined {
|
|
30
|
-
const v = raw.toolCallId
|
|
31
|
+
const v = raw.toolCallId;
|
|
31
32
|
if (typeof v === "string" && v.trim()) return v;
|
|
32
33
|
return undefined;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
result: raw.result ?? raw.output,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
if (type === "context-usage" || type === "token-usage") return null;
|
|
62
|
-
if (type === "done") {
|
|
63
|
-
return { type: "done", text: raw.text as string | undefined, reason: raw.reason as string | undefined };
|
|
64
|
-
}
|
|
65
|
-
if (type === "error") {
|
|
66
|
-
return { type: "error", error: String(raw.error ?? "Unknown error") };
|
|
36
|
+
const AG_UI_TYPES = new Set([
|
|
37
|
+
"TEXT_MESSAGE_START",
|
|
38
|
+
"TEXT_MESSAGE_CONTENT",
|
|
39
|
+
"TEXT_MESSAGE_END",
|
|
40
|
+
"TEXT_MESSAGE_CHUNK",
|
|
41
|
+
"TOOL_CALL_START",
|
|
42
|
+
"TOOL_CALL_ARGS",
|
|
43
|
+
"TOOL_CALL_END",
|
|
44
|
+
"TOOL_CALL_CHUNK",
|
|
45
|
+
"TOOL_CALL_RESULT",
|
|
46
|
+
"REASONING_MESSAGE_CONTENT",
|
|
47
|
+
"REASONING_MESSAGE_CHUNK",
|
|
48
|
+
"RUN_FINISHED",
|
|
49
|
+
"RUN_ERROR",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
type OpenTool = { name: string; args: string; announced: boolean; label?: string; icon?: string | null };
|
|
53
|
+
|
|
54
|
+
function parseJson(value: string): unknown {
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(value);
|
|
57
|
+
} catch {
|
|
58
|
+
return value;
|
|
67
59
|
}
|
|
68
|
-
return null;
|
|
69
60
|
}
|
|
70
61
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
62
|
+
function toolContent(content: unknown): unknown {
|
|
63
|
+
return typeof content === "string" ? parseJson(content) : content;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function createSseDispatcher(callbacks: AgentSseCallbacks) {
|
|
67
|
+
const tools = new Map<string, OpenTool>();
|
|
68
|
+
const toolNames = new Map<string, string>();
|
|
69
|
+
const ignoredText = new Set<string>();
|
|
70
|
+
let textId: string | undefined;
|
|
71
|
+
let currentToolId: string | undefined;
|
|
72
|
+
let anon = 0;
|
|
73
|
+
|
|
74
|
+
const paint = (id: string, tool: OpenTool, input: unknown) => {
|
|
75
|
+
callbacks.onToolCall({
|
|
76
|
+
toolCallId: id,
|
|
77
|
+
toolName: tool.name,
|
|
78
|
+
toolLabel: tool.label,
|
|
79
|
+
toolIcon: tool.icon,
|
|
80
|
+
input,
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const announce = (id: string) => {
|
|
85
|
+
const tool = tools.get(id);
|
|
86
|
+
if (!tool || tool.announced) return;
|
|
87
|
+
tool.announced = true;
|
|
88
|
+
paint(id, tool, undefined);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const flushTool = (id: string) => {
|
|
92
|
+
const tool = tools.get(id);
|
|
93
|
+
if (!tool) return;
|
|
94
|
+
announce(id);
|
|
95
|
+
const trimmed = tool.args.trim();
|
|
96
|
+
tools.delete(id);
|
|
97
|
+
if (!trimmed) return;
|
|
98
|
+
paint(id, tool, parseJson(trimmed));
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const applyCustom = (raw: Record<string, unknown>) => {
|
|
102
|
+
const value = (raw.value ?? {}) as Record<string, unknown>;
|
|
103
|
+
const toolCallId = typeof value.toolCallId === "string" ? value.toolCallId : "";
|
|
104
|
+
if (!toolCallId) return;
|
|
105
|
+
const tool = tools.get(toolCallId);
|
|
106
|
+
|
|
107
|
+
if (raw.name === "tool-meta") {
|
|
108
|
+
if (!tool) return;
|
|
109
|
+
if (typeof value.toolLabel === "string") tool.label = value.toolLabel;
|
|
110
|
+
if ("toolIcon" in value) tool.icon = (value.toolIcon as string | null | undefined) ?? null;
|
|
111
|
+
paint(toolCallId, tool, tool.args.trim() ? parseJson(tool.args) : undefined);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (raw.name === "tool-call-input") {
|
|
116
|
+
const toolName = typeof value.toolName === "string" && value.toolName ? value.toolName : (tool?.name ?? "unknown");
|
|
117
|
+
if (tool) tool.name = toolName;
|
|
118
|
+
toolNames.set(toolCallId, toolName);
|
|
119
|
+
callbacks.onToolCall({
|
|
120
|
+
toolCallId,
|
|
121
|
+
toolName,
|
|
122
|
+
toolLabel: tool?.label,
|
|
123
|
+
toolIcon: tool?.icon,
|
|
124
|
+
input: value.input,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const flushAll = () => {
|
|
130
|
+
for (const id of [...tools.keys()]) flushTool(id);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const ensure = (id: string, name?: string) => {
|
|
134
|
+
let tool = tools.get(id);
|
|
135
|
+
if (!tool) {
|
|
136
|
+
tool = { name: name?.trim() ? name : "unknown", args: "", announced: false };
|
|
137
|
+
tools.set(id, tool);
|
|
138
|
+
} else if (name?.trim()) {
|
|
139
|
+
tool.name = name;
|
|
140
|
+
}
|
|
141
|
+
announce(id);
|
|
142
|
+
toolNames.set(id, tool.name);
|
|
143
|
+
return tool;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const resolveToolId = (raw: Record<string, unknown>) => {
|
|
147
|
+
const id = readToolCallId(raw);
|
|
148
|
+
if (id) {
|
|
149
|
+
currentToolId = id;
|
|
150
|
+
return id;
|
|
151
|
+
}
|
|
152
|
+
if (currentToolId && tools.has(currentToolId)) return currentToolId;
|
|
153
|
+
anon += 1;
|
|
154
|
+
currentToolId = `tool-${anon}`;
|
|
155
|
+
return currentToolId;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const noteText = (raw: Record<string, unknown>) => {
|
|
159
|
+
const messageId = typeof raw.messageId === "string" ? raw.messageId : undefined;
|
|
160
|
+
if (raw.role != null && raw.role !== "assistant") {
|
|
161
|
+
if (messageId) ignoredText.add(messageId);
|
|
88
162
|
return false;
|
|
89
163
|
}
|
|
164
|
+
if (messageId && ignoredText.has(messageId)) return false;
|
|
165
|
+
if (messageId && textId && messageId !== textId) callbacks.onTextEnd?.();
|
|
166
|
+
if (messageId) textId = messageId;
|
|
167
|
+
return true;
|
|
168
|
+
};
|
|
90
169
|
|
|
91
|
-
|
|
92
|
-
|
|
170
|
+
return async (raw: Record<string, unknown>): Promise<"done" | "error" | false> => {
|
|
171
|
+
const type = raw.type;
|
|
172
|
+
if (type === "CUSTOM") {
|
|
173
|
+
applyCustom(raw);
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (typeof type !== "string" || !AG_UI_TYPES.has(type)) return false;
|
|
93
177
|
|
|
94
|
-
switch (
|
|
95
|
-
case "
|
|
96
|
-
|
|
178
|
+
switch (type) {
|
|
179
|
+
case "TEXT_MESSAGE_START":
|
|
180
|
+
case "TEXT_MESSAGE_CONTENT":
|
|
181
|
+
case "TEXT_MESSAGE_CHUNK": {
|
|
182
|
+
if (!noteText(raw)) return false;
|
|
183
|
+
if (typeof raw.delta === "string" && raw.delta) callbacks.onTextDelta(raw.delta);
|
|
97
184
|
return false;
|
|
98
|
-
|
|
99
|
-
|
|
185
|
+
}
|
|
186
|
+
case "TEXT_MESSAGE_END": {
|
|
187
|
+
const messageId = typeof raw.messageId === "string" ? raw.messageId : undefined;
|
|
188
|
+
if (messageId && ignoredText.has(messageId)) return false;
|
|
189
|
+
callbacks.onTextEnd?.();
|
|
190
|
+
textId = undefined;
|
|
100
191
|
return false;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
toolLabel: event.toolLabel,
|
|
106
|
-
input: event.input,
|
|
107
|
-
});
|
|
192
|
+
}
|
|
193
|
+
case "REASONING_MESSAGE_CONTENT":
|
|
194
|
+
case "REASONING_MESSAGE_CHUNK": {
|
|
195
|
+
if (typeof raw.delta === "string" && raw.delta) callbacks.onThinkingDelta(raw.delta);
|
|
108
196
|
return false;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
197
|
+
}
|
|
198
|
+
case "TOOL_CALL_START":
|
|
199
|
+
ensure(resolveToolId(raw), typeof raw.toolCallName === "string" ? raw.toolCallName : undefined);
|
|
200
|
+
return false;
|
|
201
|
+
case "TOOL_CALL_ARGS": {
|
|
202
|
+
const tool = ensure(resolveToolId(raw));
|
|
203
|
+
if (typeof raw.delta === "string") tool.args += raw.delta;
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
case "TOOL_CALL_CHUNK": {
|
|
207
|
+
const tool = ensure(resolveToolId(raw), typeof raw.toolCallName === "string" ? raw.toolCallName : undefined);
|
|
208
|
+
if (typeof raw.delta === "string") tool.args += raw.delta;
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
case "TOOL_CALL_END": {
|
|
212
|
+
const id = readToolCallId(raw) ?? currentToolId;
|
|
213
|
+
if (id) flushTool(id);
|
|
214
|
+
if (id && id === currentToolId) currentToolId = undefined;
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
case "TOOL_CALL_RESULT": {
|
|
218
|
+
const id = readToolCallId(raw);
|
|
219
|
+
const name = (id && (tools.get(id)?.name || toolNames.get(id))) || (typeof raw.toolCallName === "string" ? raw.toolCallName : "unknown");
|
|
220
|
+
if (id) {
|
|
221
|
+
flushTool(id);
|
|
222
|
+
toolNames.delete(id);
|
|
223
|
+
}
|
|
224
|
+
callbacks.onToolResult({ toolCallId: id, toolName: name, result: toolContent(raw.content) });
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
case "RUN_FINISHED":
|
|
228
|
+
flushAll();
|
|
229
|
+
await Promise.resolve(callbacks.onDone(""));
|
|
230
|
+
return "done";
|
|
231
|
+
case "RUN_ERROR":
|
|
232
|
+
flushAll();
|
|
233
|
+
await Promise.resolve(callbacks.onError(String(raw.message ?? "Unknown error")));
|
|
234
|
+
return "error";
|
|
235
|
+
default:
|
|
115
236
|
return false;
|
|
116
|
-
case "done":
|
|
117
|
-
gotTerminal = true;
|
|
118
|
-
terminal = "done";
|
|
119
|
-
await Promise.resolve(callbacks.onDone(event.text ?? ""));
|
|
120
|
-
return true;
|
|
121
|
-
case "error":
|
|
122
|
-
gotTerminal = true;
|
|
123
|
-
terminal = "error";
|
|
124
|
-
await Promise.resolve(callbacks.onError(event.error));
|
|
125
|
-
return true;
|
|
126
237
|
}
|
|
127
238
|
};
|
|
239
|
+
}
|
|
128
240
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const { done, value } = await reader.read();
|
|
134
|
-
if (done) {
|
|
135
|
-
buffer += decoder.decode();
|
|
136
|
-
for (const line of buffer.split("\n")) {
|
|
137
|
-
if (await handleLine(line)) return terminal;
|
|
138
|
-
}
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
241
|
+
function invalidEventMessage(error: { issues?: { message?: string }[] }): string {
|
|
242
|
+
return error.issues?.[0]?.message || "Invalid AG-UI event";
|
|
243
|
+
}
|
|
141
244
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
buffer = lines.pop() ?? "";
|
|
245
|
+
export async function parseSseStream(body: ReadableStream<Uint8Array>, callbacks: AgentSseCallbacks, options?: { signal?: AbortSignal }): Promise<ParseSseResult> {
|
|
246
|
+
if (options?.signal?.aborted) return "aborted";
|
|
145
247
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
248
|
+
const dispatch = createSseDispatcher(callbacks);
|
|
249
|
+
const events$ = parseSSEStream(
|
|
250
|
+
runHttpRequest(
|
|
251
|
+
async () =>
|
|
252
|
+
new Response(body, {
|
|
253
|
+
status: 200,
|
|
254
|
+
headers: { "content-type": "text/event-stream" },
|
|
255
|
+
}),
|
|
256
|
+
),
|
|
257
|
+
);
|
|
150
258
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
259
|
+
return await new Promise<ParseSseResult>((resolve) => {
|
|
260
|
+
let settled = false;
|
|
261
|
+
let gotTerminal = false;
|
|
262
|
+
let chain = Promise.resolve();
|
|
263
|
+
let sub: { unsubscribe: () => void } | undefined;
|
|
155
264
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
265
|
+
const finish = (result: ParseSseResult) => {
|
|
266
|
+
if (settled) return;
|
|
267
|
+
settled = true;
|
|
268
|
+
options?.signal?.removeEventListener("abort", onAbort);
|
|
269
|
+
sub?.unsubscribe();
|
|
270
|
+
resolve(result);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const onAbort = () => finish("aborted");
|
|
274
|
+
options?.signal?.addEventListener("abort", onAbort);
|
|
275
|
+
|
|
276
|
+
const fail = async (message: string) => {
|
|
277
|
+
if (settled) return;
|
|
278
|
+
await Promise.resolve(callbacks.onError(message));
|
|
279
|
+
finish("error");
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
sub = events$.subscribe({
|
|
283
|
+
next: (json) => {
|
|
284
|
+
chain = chain
|
|
285
|
+
.then(async () => {
|
|
286
|
+
if (settled || options?.signal?.aborted) return;
|
|
287
|
+
const parsed = EventSchemas.safeParse(json);
|
|
288
|
+
if (!parsed.success) {
|
|
289
|
+
await fail(invalidEventMessage(parsed.error));
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const result = await dispatch(parsed.data as Record<string, unknown>);
|
|
293
|
+
if (!result || settled) return;
|
|
294
|
+
gotTerminal = true;
|
|
295
|
+
finish(result);
|
|
296
|
+
})
|
|
297
|
+
.catch(async (err: unknown) => {
|
|
298
|
+
if ((err as Error).name === "AbortError" || options?.signal?.aborted) {
|
|
299
|
+
finish("aborted");
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
await fail((err as Error).message ?? "Stream read error");
|
|
303
|
+
});
|
|
304
|
+
},
|
|
305
|
+
error: (err: unknown) => {
|
|
306
|
+
void chain.then(async () => {
|
|
307
|
+
if (settled) return;
|
|
308
|
+
if ((err as Error)?.name === "AbortError" || options?.signal?.aborted) {
|
|
309
|
+
finish("aborted");
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
await fail((err as Error)?.message ?? "Stream read error");
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
complete: () => {
|
|
316
|
+
void chain.then(async () => {
|
|
317
|
+
if (settled) return;
|
|
318
|
+
if (options?.signal?.aborted) {
|
|
319
|
+
finish("aborted");
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (!gotTerminal) {
|
|
323
|
+
await Promise.resolve(callbacks.onError("Connection lost"));
|
|
324
|
+
finish("connection-lost");
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
});
|
|
162
330
|
}
|
|
@@ -138,7 +138,7 @@ export function useAgentStream({
|
|
|
138
138
|
: await fetcher(ep, {
|
|
139
139
|
method: "POST",
|
|
140
140
|
credentials: "include",
|
|
141
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
141
|
+
headers: { "Content-Type": "application/json", Accept: "text/event-stream", ...headers },
|
|
142
142
|
body: JSON.stringify({ messages: payload, ...extra }),
|
|
143
143
|
signal: controller.signal,
|
|
144
144
|
});
|
package/src/chat/index.ts
CHANGED
|
@@ -4,8 +4,8 @@ export type { AgentPanelProps } from "./AgentPanel";
|
|
|
4
4
|
export { AgentPanel } from "./AgentPanel";
|
|
5
5
|
export type { ChatBgTask, ChatBgTaskStatus } from "./common/bgTasks";
|
|
6
6
|
export { formatBgElapsed, parseBgTaskRef } from "./common/bgTasks";
|
|
7
|
-
export type { AgentSseCallbacks,
|
|
8
|
-
export {
|
|
7
|
+
export type { AgentSseCallbacks, ParseSseResult } from "./common/sse";
|
|
8
|
+
export { parseSseStream } from "./common/sse";
|
|
9
9
|
export type { AgentHistoryMessage, AgentMessage, AgentMessageRole, AgentPanelEndpoint, AgentStreamRequest, AgentToolCallEvent, AgentToolHook, AgentToolNameMatch, AgentToolResultEvent } from "./common/types";
|
|
10
10
|
export type { AgentChatResumeRequest, AgentChatSendRequest, UseAgentChatStreamOptions } from "./common/useAgentChatStream";
|
|
11
11
|
export { useAgentChatStream } from "./common/useAgentChatStream";
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
2
|
import { cn } from "../../lib/cn";
|
|
3
|
+
import { chatRowBoxClass, chatRowClass } from "../chatRhythm";
|
|
3
4
|
import { ChatMarkdown } from "./ChatMarkdown";
|
|
4
5
|
import { ChatThinking } from "./ChatThinking";
|
|
5
6
|
|
|
6
7
|
export type ChatAgentMessageProps = {
|
|
7
|
-
/** Markdown body. Rendered with `MarkdownViewer
|
|
8
|
+
/** Markdown body. Rendered with `MarkdownViewer`. */
|
|
8
9
|
content?: string;
|
|
9
10
|
streaming?: boolean;
|
|
10
11
|
/** Extra row content. Replaces `content` when set. */
|
|
@@ -19,9 +20,9 @@ export function ChatAgentMessage({ content, streaming = false, children, thinkin
|
|
|
19
20
|
const body = children ?? (content ? <ChatMarkdown content={content} streaming={streaming} /> : null);
|
|
20
21
|
|
|
21
22
|
return (
|
|
22
|
-
<div className={cn("nonla-chat-agent
|
|
23
|
+
<div className={cn("nonla-chat-agent", chatRowClass, className)}>
|
|
23
24
|
{thinking ? <ChatThinking thinking={thinking} duration={thinkingDuration ?? 0} streaming={thinkingStreaming} /> : null}
|
|
24
|
-
{body ? <div className="min-w-0
|
|
25
|
+
{body ? <div className={cn("min-w-0", chatRowBoxClass, thinking && chatRowClass)}>{body}</div> : null}
|
|
25
26
|
</div>
|
|
26
27
|
);
|
|
27
28
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Icon } from "../../icon/Icon";
|
|
3
3
|
import { cn } from "../../lib/cn";
|
|
4
|
+
import { chatRowClass } from "../chatRhythm";
|
|
4
5
|
|
|
5
6
|
export type ChatErrorProps = {
|
|
6
7
|
children: ReactNode;
|
|
@@ -9,12 +10,12 @@ export type ChatErrorProps = {
|
|
|
9
10
|
|
|
10
11
|
export function ChatError({ children, className }: ChatErrorProps) {
|
|
11
12
|
return (
|
|
12
|
-
<div className={cn("nonla-chat-error px-4 py-1", className)}>
|
|
13
|
+
<div className={cn("nonla-chat-error px-4 py-1", chatRowClass, className)}>
|
|
13
14
|
<div
|
|
14
15
|
role="alert"
|
|
15
|
-
className="flex items-center gap-2 rounded-lg border border-destructive/40 bg-[color-mix(in_oklab,var(--destructive)_14%,var(--nonla-surface))] px-3 py-2 text-
|
|
16
|
+
className="flex items-center gap-2 rounded-lg border border-destructive/40 bg-[color-mix(in_oklab,var(--destructive)_14%,var(--nonla-surface))] px-3 py-2 text-sm text-destructive"
|
|
16
17
|
>
|
|
17
|
-
<
|
|
18
|
+
<Icon name="error" size={16} className="shrink-0" />
|
|
18
19
|
<div className="min-w-0">{children}</div>
|
|
19
20
|
</div>
|
|
20
21
|
</div>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type KeyboardEvent as ReactKeyboardEvent, type ReactNode, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Icon } from "../../icon/Icon";
|
|
3
3
|
import { cn } from "../../lib/cn";
|
|
4
|
+
import { glassSurfaceClass } from "../../lib/surface";
|
|
4
5
|
|
|
5
6
|
export type ChatInputProps = {
|
|
6
7
|
generating?: boolean;
|
|
@@ -124,7 +125,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
124
125
|
};
|
|
125
126
|
|
|
126
127
|
return (
|
|
127
|
-
<div className={cn("nonla-
|
|
128
|
+
<div className={cn(glassSurfaceClass, "nonla-chat-input relative isolate mx-4 mb-2.5 flex shrink-0 flex-col overflow-hidden rounded-xl border-(--nonla-input) bg-muted-strong pt-1 [.light:not(.dark)_&]:bg-background [.light:not(.dark)_&]:shadow-none [.light_.nonla-ui:not(.dark)_&]:bg-background [.light_.nonla-ui:not(.dark)_&]:shadow-none", disabled && "opacity-70", className)}>
|
|
128
129
|
<textarea
|
|
129
130
|
ref={textareaRef}
|
|
130
131
|
data-chat-input
|
|
@@ -136,23 +137,23 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
136
137
|
rows={1}
|
|
137
138
|
className={cn(
|
|
138
139
|
"w-full resize-none appearance-none border-0 bg-transparent shadow-none outline-none focus:outline-none px-2.5 pt-1 pb-2.5",
|
|
139
|
-
"text-
|
|
140
|
-
disabled ? "text-muted-foreground cursor-not-allowed placeholder:text-
|
|
140
|
+
"text-sm font-normal",
|
|
141
|
+
disabled ? "text-muted-foreground cursor-not-allowed placeholder:text-quaternary-foreground" : "text-foreground placeholder:text-quaternary-foreground",
|
|
141
142
|
)}
|
|
142
143
|
/>
|
|
143
144
|
|
|
144
145
|
<div className="flex items-center gap-1.5 pb-2 px-2">
|
|
145
146
|
{onClear ? (
|
|
146
147
|
<button type="button" onClick={onClear} title="Clear chat" aria-label="Clear chat" className="inline-flex size-6 shrink-0 items-center justify-center rounded-lg border-0 bg-transparent cursor-pointer hover:bg-ink-hover">
|
|
147
|
-
<
|
|
148
|
+
<Icon name="eraser" size={14} />
|
|
148
149
|
</button>
|
|
149
150
|
) : null}
|
|
150
151
|
{toolbar}
|
|
151
152
|
<div className="flex-1" />
|
|
152
153
|
|
|
153
154
|
{generating ? (
|
|
154
|
-
<button type="button" onClick={onCancel} title="Stop" className="w-6 h-6 rounded-full bg-brand text-(--nonla-solid
|
|
155
|
-
<
|
|
155
|
+
<button type="button" onClick={onCancel} title="Stop" className="w-6 h-6 rounded-full bg-brand text-(--nonla-text-on-solid) flex items-center justify-center shrink-0 cursor-pointer hover:bg-brand/90 active:scale-95 transition-all duration-100 border-0">
|
|
156
|
+
<Icon name="stop" size={12} />
|
|
156
157
|
<span className="sr-only">Stop</span>
|
|
157
158
|
</button>
|
|
158
159
|
) : (
|
|
@@ -163,7 +164,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
163
164
|
title="Send (Enter)"
|
|
164
165
|
className={cn("w-6 h-6 rounded-full flex items-center justify-center shrink-0 transition-all duration-100 border-0", canSend ? "bg-foreground text-background cursor-pointer" : "bg-border text-muted-foreground cursor-not-allowed opacity-50")}
|
|
165
166
|
>
|
|
166
|
-
<
|
|
167
|
+
<Icon name="arrow-up" size={14} />
|
|
167
168
|
<span className="sr-only">Send</span>
|
|
168
169
|
</button>
|
|
169
170
|
)}
|