devnonla-ui 0.14.0 → 0.16.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 +5 -2
- package/src/blob-shape/BlobShape.tsx +337 -0
- package/src/button/ButtonCopy.tsx +2 -2
- 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 +5 -5
- 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 +4 -4
- package/src/chat/tool-ui/BackgroundTaskToolUI.tsx +7 -6
- package/src/chat/tool-ui/BackgroundTasksBar.tsx +7 -7
- package/src/chat/tool-ui/CallAgentToolUI.tsx +14 -13
- 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 +8 -7
- package/src/chat/tool-ui/ToolUiTrailing.tsx +3 -3
- package/src/chat/tool-ui/WebFetchToolUI.tsx +10 -9
- package/src/codeblock/CodeBlock.tsx +1 -1
- package/src/desktop/DesktopIcon.tsx +3 -4
- package/src/icon/Icon.tsx +33 -0
- package/src/icon/glyphs.ts +42 -0
- package/src/index.ts +8 -20
- package/src/input/EditableInput.tsx +2 -2
- package/src/markdown-editor/MarkdownEditor.tsx +4 -4
- package/src/markdown-editor/MarkdownPreview.tsx +4 -4
- package/src/markdown-editor/MarkdownTable.tsx +67 -13
- package/src/markdown-editor/MarkdownViewer.tsx +25 -28
- package/src/markdown-editor/MermaidBlock.tsx +4 -4
- package/src/markdown-editor/ReactCodeFrame.tsx +2 -2
- package/src/markdown-editor/ReactCodeSandbox.tsx +2 -2
- package/src/markdown-editor/markdownScale.ts +65 -131
- package/src/menu/Menu.tsx +2 -2
- package/src/pagination/Pagination.tsx +4 -4
- package/src/scroll/OverlayScroll.tsx +13 -5
- package/src/sidebar/Sidebar.tsx +5 -5
- package/src/sortable-tree/SortableTree.tsx +599 -0
- package/src/styles.css +36 -16
- package/src/tabs/Tabs.tsx +75 -13
- package/src/theme-switcher/ThemeSwitcher.tsx +6 -6
- package/src/typography/Typography.tsx +4 -4
- package/src/icon/SolarIcon.tsx +0 -39
- package/src/icon/solar.ts +0 -172
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,5 +1,5 @@
|
|
|
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
4
|
import { glassSurfaceClass } from "../../lib/surface";
|
|
5
5
|
|
|
@@ -137,7 +137,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
137
137
|
rows={1}
|
|
138
138
|
className={cn(
|
|
139
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",
|
|
140
|
-
"text-
|
|
140
|
+
"text-sm font-normal",
|
|
141
141
|
disabled ? "text-muted-foreground cursor-not-allowed placeholder:text-quaternary-foreground" : "text-foreground placeholder:text-quaternary-foreground",
|
|
142
142
|
)}
|
|
143
143
|
/>
|
|
@@ -145,7 +145,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
145
145
|
<div className="flex items-center gap-1.5 pb-2 px-2">
|
|
146
146
|
{onClear ? (
|
|
147
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">
|
|
148
|
-
<
|
|
148
|
+
<Icon name="eraser" size={14} />
|
|
149
149
|
</button>
|
|
150
150
|
) : null}
|
|
151
151
|
{toolbar}
|
|
@@ -153,7 +153,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
153
153
|
|
|
154
154
|
{generating ? (
|
|
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
|
-
<
|
|
156
|
+
<Icon name="stop" size={12} />
|
|
157
157
|
<span className="sr-only">Stop</span>
|
|
158
158
|
</button>
|
|
159
159
|
) : (
|
|
@@ -164,7 +164,7 @@ export function ChatInput({ generating = false, placeholder = "Message…", disa
|
|
|
164
164
|
title="Send (Enter)"
|
|
165
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")}
|
|
166
166
|
>
|
|
167
|
-
<
|
|
167
|
+
<Icon name="arrow-up" size={14} />
|
|
168
168
|
<span className="sr-only">Send</span>
|
|
169
169
|
</button>
|
|
170
170
|
)}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
1
2
|
import { MarkdownViewer } from "../../markdown-editor/MarkdownViewer";
|
|
2
3
|
|
|
3
4
|
export type ChatMarkdownProps = {
|
|
@@ -5,7 +6,40 @@ export type ChatMarkdownProps = {
|
|
|
5
6
|
streaming?: boolean;
|
|
6
7
|
};
|
|
7
8
|
|
|
8
|
-
/**
|
|
9
|
+
/** Reply headings are `text-lg` at the strong weight. Body stays `text-md`. Strong is 500. Tables stay `text-base`. */
|
|
10
|
+
const chatHeadingScale = {
|
|
11
|
+
"--md-h1-size": "var(--text-lg)",
|
|
12
|
+
"--md-h2-size": "var(--text-lg)",
|
|
13
|
+
"--md-h3-size": "var(--text-lg)",
|
|
14
|
+
"--md-h4-size": "var(--text-lg)",
|
|
15
|
+
"--md-h5-size": "var(--text-lg)",
|
|
16
|
+
"--md-h6-size": "var(--text-lg)",
|
|
17
|
+
"--md-h1-leading": "var(--text-lg--line-height)",
|
|
18
|
+
"--md-h2-leading": "var(--text-lg--line-height)",
|
|
19
|
+
"--md-h3-leading": "var(--text-lg--line-height)",
|
|
20
|
+
"--md-h4-leading": "var(--text-lg--line-height)",
|
|
21
|
+
"--md-h5-leading": "var(--text-lg--line-height)",
|
|
22
|
+
"--md-h6-leading": "var(--text-lg--line-height)",
|
|
23
|
+
"--md-h1-weight": "500",
|
|
24
|
+
"--md-h2-weight": "500",
|
|
25
|
+
"--md-h3-weight": "500",
|
|
26
|
+
"--md-h4-weight": "500",
|
|
27
|
+
"--md-h5-weight": "500",
|
|
28
|
+
"--md-h6-weight": "500",
|
|
29
|
+
"--md-strong-weight": "500",
|
|
30
|
+
} as CSSProperties;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Headings use the paragraph box, so `#` does not keep the article margin.
|
|
34
|
+
* The first and last line drop that padding so the row padding is the only gap.
|
|
35
|
+
*/
|
|
36
|
+
const chatEdgeClass = [
|
|
37
|
+
"[&_:is(h1,h2,h3,h4,h5,h6)]:my-(--md-p-my,1px)! [&_:is(h1,h2,h3,h4,h5,h6)]:py-(--md-p-py,4px)!",
|
|
38
|
+
"[&_[data-md-block]:not(:first-child)_p:first-child]:mt-(--md-p-my,1px)! [&_[data-md-block]:not(:first-child)_p:first-child]:pt-(--md-p-py,4px)!",
|
|
39
|
+
"[&_[data-md-block]:first-child_:is(p,h1,h2,h3,h4,h5,h6,ul,ol)]:mt-0! [&_[data-md-block]:first-child_:is(p,h1,h2,h3,h4,h5,h6)]:pt-0! [&_[data-md-block]:last-child_:is(p,h1,h2,h3,h4,h5,h6,ul,ol)]:mb-0! [&_[data-md-block]:last-child_:is(p,h1,h2,h3,h4,h5,h6)]:pb-0!",
|
|
40
|
+
].join(" ");
|
|
41
|
+
|
|
42
|
+
/** Agent markdown. Headings are `text-lg` at the strong weight. */
|
|
9
43
|
export function ChatMarkdown({ content, streaming = false }: ChatMarkdownProps) {
|
|
10
|
-
return <MarkdownViewer value={content} streaming={streaming}
|
|
44
|
+
return <MarkdownViewer value={content} streaming={streaming} className={chatEdgeClass} style={chatHeadingScale} />;
|
|
11
45
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Icon } from "../../icon/Icon";
|
|
3
3
|
import { cn } from "../../lib/cn";
|
|
4
4
|
import { Shimmer } from "../../shimmer/Shimmer";
|
|
5
|
+
import { chatToolLineClass } from "../chatRhythm";
|
|
5
6
|
|
|
6
7
|
export type ChatThinkingProps = {
|
|
7
8
|
thinking: string;
|
|
@@ -40,7 +41,7 @@ export function ChatThinking({ thinking, duration = 0, streaming = false, classN
|
|
|
40
41
|
|
|
41
42
|
return (
|
|
42
43
|
<details
|
|
43
|
-
className={cn("nonla-chat-thinking px-4
|
|
44
|
+
className={cn("nonla-chat-thinking px-4 group/thinking", className)}
|
|
44
45
|
style={{ overflowAnchor: "none" }}
|
|
45
46
|
onToggle={(e) => {
|
|
46
47
|
if (!streaming || !e.currentTarget.open) return;
|
|
@@ -48,13 +49,13 @@ export function ChatThinking({ thinking, duration = 0, streaming = false, classN
|
|
|
48
49
|
if (el) el.scrollTop = el.scrollHeight;
|
|
49
50
|
}}
|
|
50
51
|
>
|
|
51
|
-
<summary className="cursor-pointer
|
|
52
|
-
<Shimmer active={streaming} className="text-
|
|
53
|
-
<
|
|
52
|
+
<summary className={cn(chatToolLineClass, "cursor-pointer list-none font-medium select-none [&::-webkit-details-marker]:hidden")}>
|
|
53
|
+
<Shimmer active={streaming} className="text-muted-foreground">{label}</Shimmer>
|
|
54
|
+
<Icon name="arrow-right" size={12} className="shrink-0 opacity-0 text-muted-foreground transition-[opacity,transform] duration-150 group-hover/thinking:opacity-100 group-open/thinking:opacity-100 group-open/thinking:rotate-90" />
|
|
54
55
|
</summary>
|
|
55
56
|
|
|
56
|
-
<div ref={bodyRef} className="pt-2 max-h-40 min-w-0 overflow-y-auto overflow-x-hidden mb-2 text-
|
|
57
|
-
<p className="
|
|
57
|
+
<div ref={bodyRef} className="pt-2 max-h-40 min-w-0 overflow-y-auto overflow-x-hidden mb-2 text-sm">
|
|
58
|
+
<p className="m-0 text-sm text-tertiary-foreground whitespace-pre-wrap wrap-break-word">{thinking}</p>
|
|
58
59
|
</div>
|
|
59
60
|
</details>
|
|
60
61
|
);
|