devnonla-ui 0.4.2 → 0.5.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/README.md +12 -4
- package/package.json +1 -1
- package/src/chat/AgentChatbox.tsx +112 -0
- package/src/chat/AgentPanel.tsx +5 -98
- package/src/chat/chatMessageList.tsx +114 -0
- package/src/chat/common/agentStreamHelpers.ts +104 -0
- package/src/chat/common/createAgentSseSession.ts +304 -0
- package/src/chat/common/sse.ts +13 -7
- package/src/chat/common/types.ts +0 -4
- package/src/chat/common/useAgentChatStream.ts +224 -0
- package/src/chat/common/useAgentStream.ts +28 -306
- package/src/chat/index.ts +5 -1
- package/src/chat/message-ui/ChatInput.tsx +1 -1
- package/src/chat/tool-ui/ChatToolCall.tsx +27 -27
- package/src/dropdown/ContextMenu.tsx +14 -2
- package/src/dropdown/Dropdown.tsx +15 -3
- package/src/dropdown/menuClasses.ts +5 -1
- package/src/index.ts +3 -1
package/README.md
CHANGED
|
@@ -119,10 +119,12 @@ import { DesktopStage, DesktopHeader, MeadowDesktop, DesktopWindow, MeadowShell,
|
|
|
119
119
|
|
|
120
120
|
# Chat
|
|
121
121
|
|
|
122
|
-
`AgentPanel` is the full chat surface: welcome, messages, thinking, tool calls, markdown, composer. Attach an SSE endpoint and send.
|
|
122
|
+
`AgentPanel` is the full chat surface: welcome, messages, thinking, tool calls, markdown, composer. Attach an SSE endpoint and send. The panel POSTs `{ messages }` (plus optional `extraBody`).
|
|
123
|
+
|
|
124
|
+
`AgentChatbox` is the same UI when the **app** owns the request: `send` POSTs only the new user text; `resume` reattaches a live stream on mount; `onStop` runs after the client abort.
|
|
123
125
|
|
|
124
126
|
```tsx
|
|
125
|
-
import { AgentPanel } from "devnonla-ui";
|
|
127
|
+
import { AgentChatbox, AgentPanel } from "devnonla-ui";
|
|
126
128
|
|
|
127
129
|
<AgentPanel
|
|
128
130
|
endpoint="/api/agents/abc/assistant/stream"
|
|
@@ -133,11 +135,17 @@ import { AgentPanel } from "devnonla-ui";
|
|
|
133
135
|
{ name: "web_fetch", onCall: (e) => console.log("fetch", e.input), onResult: (e) => console.log("done", e.output) },
|
|
134
136
|
]}
|
|
135
137
|
/>
|
|
138
|
+
|
|
139
|
+
<AgentChatbox
|
|
140
|
+
send={({ text, signal }) => fetch("/api/chat", { method: "POST", body: JSON.stringify({ text }), signal })}
|
|
141
|
+
resume={({ signal }) => fetch("/api/chat/stream", { signal })}
|
|
142
|
+
onStop={() => fetch("/api/chat/stop", { method: "POST" })}
|
|
143
|
+
/>
|
|
136
144
|
```
|
|
137
145
|
|
|
138
|
-
`endpoint` can also be a function that returns a `Response` (tests, mocks). Pass any node to `toolbar` for the composer (model picker, tools, …). Extra tool cards go in `toolUis` and win over builtins on the same `toolName`. `toolHooks` fire `onCall` / `onResult` for matching tools (omit `name` to hear every tool).
|
|
146
|
+
`endpoint` can also be a function that returns a `Response` (tests, mocks). Pass any node to `toolbar` for the composer (model picker, tools, …). Extra tool cards go in `toolUis` and win over builtins on the same `toolName`. `toolHooks` fire `onCall` / `onResult` for matching tools (omit `name` to hear every tool).
|
|
139
147
|
|
|
140
|
-
|
|
148
|
+
Stream events:
|
|
141
149
|
|
|
142
150
|
`text-delta` | `thinking-delta` | `tool-call` | `tool-result` | `done` | `error`
|
|
143
151
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type ReactNode, useEffect, useLayoutEffect } from "react";
|
|
2
|
+
import { cn } from "../lib/cn";
|
|
3
|
+
import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
|
|
4
|
+
import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
|
|
5
|
+
import type { AgentMessage, AgentToolHook } from "./common/types";
|
|
6
|
+
import {
|
|
7
|
+
type AgentChatResumeRequest,
|
|
8
|
+
type AgentChatSendRequest,
|
|
9
|
+
useAgentChatStream,
|
|
10
|
+
} from "./common/useAgentChatStream";
|
|
11
|
+
import { ChatInput } from "./message-ui/ChatInput";
|
|
12
|
+
import { ChatWelcome } from "./message-ui/ChatWelcome";
|
|
13
|
+
import type { AgentToolUI } from "./tool-ui/registry";
|
|
14
|
+
|
|
15
|
+
export type AgentChatboxProps = {
|
|
16
|
+
/** Seed messages for display (mount only). Remount when conversation changes. */
|
|
17
|
+
initialMessages?: AgentMessage[];
|
|
18
|
+
/** App POSTs only the new user text; must return an SSE `Response`. */
|
|
19
|
+
send: (req: AgentChatSendRequest) => Promise<Response>;
|
|
20
|
+
/** On mount: reattach a live stream (e.g. GET /stream). `null` = idle. */
|
|
21
|
+
resume?: (req: AgentChatResumeRequest) => Promise<Response | null>;
|
|
22
|
+
/** User pressed Stop — after client abort, notify app to cancel server generation. */
|
|
23
|
+
onStop?: () => void | Promise<void>;
|
|
24
|
+
|
|
25
|
+
title?: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
avatar?: ReactNode;
|
|
29
|
+
starters?: string[];
|
|
30
|
+
placeholder?: string;
|
|
31
|
+
toolbar?: ReactNode;
|
|
32
|
+
toolUis?: AgentToolUI[];
|
|
33
|
+
toolHooks?: AgentToolHook[];
|
|
34
|
+
accessory?: ReactNode;
|
|
35
|
+
emptyState?: ReactNode;
|
|
36
|
+
scrollbar?: OverlayScrollVisibility;
|
|
37
|
+
className?: string;
|
|
38
|
+
onGeneratingChange?: (generating: boolean) => void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export function AgentChatbox({
|
|
42
|
+
initialMessages,
|
|
43
|
+
send,
|
|
44
|
+
resume,
|
|
45
|
+
onStop,
|
|
46
|
+
title = "Assistant",
|
|
47
|
+
name,
|
|
48
|
+
description,
|
|
49
|
+
avatar,
|
|
50
|
+
starters,
|
|
51
|
+
placeholder = "Message…",
|
|
52
|
+
toolbar,
|
|
53
|
+
toolUis,
|
|
54
|
+
toolHooks,
|
|
55
|
+
accessory,
|
|
56
|
+
emptyState,
|
|
57
|
+
scrollbar = "hover",
|
|
58
|
+
className,
|
|
59
|
+
onGeneratingChange,
|
|
60
|
+
}: AgentChatboxProps) {
|
|
61
|
+
const { messages, generating, send: sendText, cancel } = useAgentChatStream({
|
|
62
|
+
initialMessages,
|
|
63
|
+
send,
|
|
64
|
+
resume,
|
|
65
|
+
onStop,
|
|
66
|
+
toolHooks,
|
|
67
|
+
});
|
|
68
|
+
const { scrollRef, onScroll, scrollToBottom } = useStickToBottom();
|
|
69
|
+
const welcomeName = name ?? title;
|
|
70
|
+
const lastMsg = messages[messages.length - 1];
|
|
71
|
+
const status = activityStatus(messages);
|
|
72
|
+
const lastToolPending = lastMsg?.role === "tool-call" && lastMsg.toolOutput == null && !lastMsg.toolError;
|
|
73
|
+
const hasActiveThinking = lastMsg?.role === "assistant" && Boolean(lastMsg.meta?.thinking) && lastMsg.meta?.thinkingDuration == null;
|
|
74
|
+
const isLiveText = status === "Writing..." || (lastMsg?.role === "assistant" && Boolean(lastMsg.streaming) && Boolean(lastMsg.content));
|
|
75
|
+
const showFooter = generating && !hasActiveThinking && !lastToolPending && !isLiveText;
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
onGeneratingChange?.(generating);
|
|
79
|
+
}, [generating, onGeneratingChange]);
|
|
80
|
+
|
|
81
|
+
useLayoutEffect(() => {
|
|
82
|
+
scrollToBottom();
|
|
83
|
+
}, [messages, generating, showFooter, scrollToBottom]);
|
|
84
|
+
|
|
85
|
+
const sendMessage = (text: string) => {
|
|
86
|
+
scrollToBottom({ force: true });
|
|
87
|
+
void sendText(text);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div className={cn("nonla-agent-chatbox relative flex h-full min-h-0 flex-col overflow-hidden bg-chat", className)}>
|
|
92
|
+
<OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
|
|
93
|
+
<div data-chat-scroll-content className={`max-w-190 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col pb-24" : "pt-4 pb-28"}`}>
|
|
94
|
+
{messages.length === 0 ? (
|
|
95
|
+
(emptyState ?? (
|
|
96
|
+
<ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
|
|
97
|
+
))
|
|
98
|
+
) : (
|
|
99
|
+
<AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
|
|
100
|
+
)}
|
|
101
|
+
</div>
|
|
102
|
+
</OverlayScroll>
|
|
103
|
+
|
|
104
|
+
<div className="relative shrink-0">
|
|
105
|
+
{accessory}
|
|
106
|
+
<div className="mx-auto w-full max-w-190">
|
|
107
|
+
<ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} toolbar={toolbar} enableTypeToFocus />
|
|
108
|
+
</div>
|
|
109
|
+
</div>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
package/src/chat/AgentPanel.tsx
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import { type ReactNode,
|
|
1
|
+
import { type ReactNode, useEffect, useLayoutEffect, useState } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
3
|
import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import type { AgentMessage, AgentPanelEndpoint, AgentToolAction, AgentToolHook } from "./common/types";
|
|
4
|
+
import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
|
|
5
|
+
import type { AgentMessage, AgentPanelEndpoint, AgentToolHook } from "./common/types";
|
|
7
6
|
import { useAgentStream } from "./common/useAgentStream";
|
|
8
|
-
import { formatToolName } from "./common/utils";
|
|
9
|
-
import { ChatAgentMessage } from "./message-ui/ChatAgentMessage";
|
|
10
|
-
import { ChatError } from "./message-ui/ChatError";
|
|
11
7
|
import { ChatInput } from "./message-ui/ChatInput";
|
|
12
|
-
import { ChatMarkdown } from "./message-ui/ChatMarkdown";
|
|
13
|
-
import { ChatThinking } from "./message-ui/ChatThinking";
|
|
14
|
-
import { ChatUserMessage } from "./message-ui/ChatUserMessage";
|
|
15
8
|
import { ChatWelcome } from "./message-ui/ChatWelcome";
|
|
16
|
-
import {
|
|
17
|
-
import { ChatToolCall } from "./tool-ui/ChatToolCall";
|
|
18
|
-
import { type AgentToolUI, resolveToolUI } from "./tool-ui/registry";
|
|
9
|
+
import type { AgentToolUI } from "./tool-ui/registry";
|
|
19
10
|
|
|
20
11
|
export type AgentPanelProps = {
|
|
21
12
|
/** POST URL, or a function that returns an SSE `Response`. */
|
|
@@ -43,82 +34,9 @@ export type AgentPanelProps = {
|
|
|
43
34
|
/** Overlay scrollbar — does not reserve layout space. */
|
|
44
35
|
scrollbar?: OverlayScrollVisibility;
|
|
45
36
|
className?: string;
|
|
46
|
-
onToolAction?: (event: AgentToolAction) => void;
|
|
47
37
|
onGeneratingChange?: (generating: boolean) => void;
|
|
48
38
|
};
|
|
49
39
|
|
|
50
|
-
function activityStatus(messages: AgentMessage[]): string {
|
|
51
|
-
const last = messages[messages.length - 1];
|
|
52
|
-
if (!last) return "Thinking...";
|
|
53
|
-
if (last.role === "tool-call") {
|
|
54
|
-
if (last.toolOutput != null || last.toolError) return "Waiting for model...";
|
|
55
|
-
return `Running ${last.toolLabel ?? formatToolName(last.toolName ?? "tool")}...`;
|
|
56
|
-
}
|
|
57
|
-
if (last.role === "assistant" && last.streaming && last.content) return "Writing...";
|
|
58
|
-
return "Thinking...";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function useStickToBottom() {
|
|
62
|
-
const elRef = useRef<HTMLDivElement | null>(null);
|
|
63
|
-
const pinned = useRef(true);
|
|
64
|
-
|
|
65
|
-
const scrollRef = useCallback((node: HTMLDivElement | null) => {
|
|
66
|
-
elRef.current = node;
|
|
67
|
-
}, []);
|
|
68
|
-
|
|
69
|
-
const onScroll = () => {
|
|
70
|
-
const el = elRef.current;
|
|
71
|
-
if (!el) return;
|
|
72
|
-
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const scrollToBottom = useCallback((opts?: { force?: boolean }) => {
|
|
76
|
-
const el = elRef.current;
|
|
77
|
-
if (!el) return;
|
|
78
|
-
if (!opts?.force && !pinned.current) return;
|
|
79
|
-
pinned.current = true;
|
|
80
|
-
el.scrollTop = el.scrollHeight;
|
|
81
|
-
}, []);
|
|
82
|
-
|
|
83
|
-
return { scrollRef, onScroll, scrollToBottom };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function MessageRow({ msg, generating, toolUis }: { msg: AgentMessage; generating: boolean; toolUis?: AgentToolUI[] }) {
|
|
87
|
-
if (msg.role === "user") return <ChatUserMessage content={msg.content} />;
|
|
88
|
-
if (msg.role === "error") return <ChatError>{msg.content}</ChatError>;
|
|
89
|
-
if (msg.role === "thinking") {
|
|
90
|
-
return <ChatThinking thinking={msg.content} duration={(msg.meta?.thinkingDuration as number) ?? 0} />;
|
|
91
|
-
}
|
|
92
|
-
if (msg.role === "tool-call") {
|
|
93
|
-
const CustomUI = resolveToolUI(msg.toolName, toolUis);
|
|
94
|
-
if (CustomUI) {
|
|
95
|
-
return <CustomUI msg={msg} generating={generating} showAvatar={false} />;
|
|
96
|
-
}
|
|
97
|
-
if (parseBgTaskRef(msg.toolOutput)) {
|
|
98
|
-
return <BackgroundTaskToolUI msg={msg} generating={generating} showAvatar={false} />;
|
|
99
|
-
}
|
|
100
|
-
const pending = msg.toolOutput == null && !msg.toolError;
|
|
101
|
-
return (
|
|
102
|
-
<ChatToolCall
|
|
103
|
-
toolName={msg.toolName}
|
|
104
|
-
label={msg.toolLabel}
|
|
105
|
-
toolInput={msg.toolInput}
|
|
106
|
-
toolOutput={msg.toolOutput}
|
|
107
|
-
toolError={msg.toolError}
|
|
108
|
-
running={pending && generating}
|
|
109
|
-
/>
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const thinking = msg.meta?.thinking;
|
|
114
|
-
const thinkingDuration = msg.meta?.thinkingDuration;
|
|
115
|
-
return (
|
|
116
|
-
<ChatAgentMessage thinking={thinking} thinkingDuration={thinkingDuration ?? 0} thinkingStreaming={thinking != null && thinkingDuration == null}>
|
|
117
|
-
{msg.content ? <ChatMarkdown content={msg.content} streaming={!!msg.streaming} /> : null}
|
|
118
|
-
</ChatAgentMessage>
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
40
|
export function AgentPanel({
|
|
123
41
|
endpoint,
|
|
124
42
|
title = "Assistant",
|
|
@@ -138,7 +56,6 @@ export function AgentPanel({
|
|
|
138
56
|
emptyState,
|
|
139
57
|
scrollbar = "hover",
|
|
140
58
|
className,
|
|
141
|
-
onToolAction,
|
|
142
59
|
onGeneratingChange,
|
|
143
60
|
}: AgentPanelProps) {
|
|
144
61
|
const { messages, generating, send, cancel, clear } = useAgentStream({
|
|
@@ -147,7 +64,6 @@ export function AgentPanel({
|
|
|
147
64
|
headers,
|
|
148
65
|
fetcher,
|
|
149
66
|
initialMessages,
|
|
150
|
-
onToolAction,
|
|
151
67
|
toolHooks,
|
|
152
68
|
});
|
|
153
69
|
const { scrollRef, onScroll, scrollToBottom } = useStickToBottom();
|
|
@@ -187,16 +103,7 @@ export function AgentPanel({
|
|
|
187
103
|
<ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
|
|
188
104
|
))
|
|
189
105
|
) : (
|
|
190
|
-
<
|
|
191
|
-
{messages.map((msg) => (
|
|
192
|
-
<MessageRow key={msg.id} msg={msg} generating={generating} toolUis={toolUis} />
|
|
193
|
-
))}
|
|
194
|
-
{showFooter ? (
|
|
195
|
-
<div className="mt-1 px-4 pb-0.5">
|
|
196
|
-
<Shimmer className="text-(length:--chat-body-size) leading-5.5 font-medium">{status}</Shimmer>
|
|
197
|
-
</div>
|
|
198
|
-
) : null}
|
|
199
|
-
</div>
|
|
106
|
+
<AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
|
|
200
107
|
)}
|
|
201
108
|
</div>
|
|
202
109
|
</OverlayScroll>
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useCallback, useRef } from "react";
|
|
2
|
+
import { Shimmer } from "../shimmer/Shimmer";
|
|
3
|
+
import { parseBgTaskRef } from "./common/bgTasks";
|
|
4
|
+
import type { AgentMessage } from "./common/types";
|
|
5
|
+
import { formatToolName } from "./common/utils";
|
|
6
|
+
import { ChatAgentMessage } from "./message-ui/ChatAgentMessage";
|
|
7
|
+
import { ChatError } from "./message-ui/ChatError";
|
|
8
|
+
import { ChatMarkdown } from "./message-ui/ChatMarkdown";
|
|
9
|
+
import { ChatThinking } from "./message-ui/ChatThinking";
|
|
10
|
+
import { ChatUserMessage } from "./message-ui/ChatUserMessage";
|
|
11
|
+
import { BackgroundTaskToolUI } from "./tool-ui/BackgroundTaskToolUI";
|
|
12
|
+
import { ChatToolCall } from "./tool-ui/ChatToolCall";
|
|
13
|
+
import { type AgentToolUI, resolveToolUI } from "./tool-ui/registry";
|
|
14
|
+
|
|
15
|
+
export function activityStatus(messages: AgentMessage[]): string {
|
|
16
|
+
const last = messages[messages.length - 1];
|
|
17
|
+
if (!last) return "Thinking...";
|
|
18
|
+
if (last.role === "tool-call") {
|
|
19
|
+
if (last.toolOutput != null || last.toolError) return "Waiting for model...";
|
|
20
|
+
return `Running ${last.toolLabel ?? formatToolName(last.toolName ?? "tool")}...`;
|
|
21
|
+
}
|
|
22
|
+
if (last.role === "assistant" && last.streaming && last.content) return "Writing...";
|
|
23
|
+
return "Thinking...";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useStickToBottom() {
|
|
27
|
+
const elRef = useRef<HTMLDivElement | null>(null);
|
|
28
|
+
const pinned = useRef(true);
|
|
29
|
+
|
|
30
|
+
const scrollRef = useCallback((node: HTMLDivElement | null) => {
|
|
31
|
+
elRef.current = node;
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
const onScroll = () => {
|
|
35
|
+
const el = elRef.current;
|
|
36
|
+
if (!el) return;
|
|
37
|
+
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const scrollToBottom = useCallback((opts?: { force?: boolean }) => {
|
|
41
|
+
const el = elRef.current;
|
|
42
|
+
if (!el) return;
|
|
43
|
+
if (!opts?.force && !pinned.current) return;
|
|
44
|
+
pinned.current = true;
|
|
45
|
+
el.scrollTop = el.scrollHeight;
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
return { scrollRef, onScroll, scrollToBottom };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function MessageRow({ msg, generating, toolUis }: { msg: AgentMessage; generating: boolean; toolUis?: AgentToolUI[] }) {
|
|
52
|
+
if (msg.role === "user") return <ChatUserMessage content={msg.content} />;
|
|
53
|
+
if (msg.role === "error") return <ChatError>{msg.content}</ChatError>;
|
|
54
|
+
if (msg.role === "thinking") {
|
|
55
|
+
return <ChatThinking thinking={msg.content} duration={(msg.meta?.thinkingDuration as number) ?? 0} />;
|
|
56
|
+
}
|
|
57
|
+
if (msg.role === "tool-call") {
|
|
58
|
+
const CustomUI = resolveToolUI(msg.toolName, toolUis);
|
|
59
|
+
if (CustomUI) {
|
|
60
|
+
return <CustomUI msg={msg} generating={generating} showAvatar={false} />;
|
|
61
|
+
}
|
|
62
|
+
if (parseBgTaskRef(msg.toolOutput)) {
|
|
63
|
+
return <BackgroundTaskToolUI msg={msg} generating={generating} showAvatar={false} />;
|
|
64
|
+
}
|
|
65
|
+
const pending = msg.toolOutput == null && !msg.toolError;
|
|
66
|
+
return (
|
|
67
|
+
<ChatToolCall
|
|
68
|
+
toolName={msg.toolName}
|
|
69
|
+
label={msg.toolLabel}
|
|
70
|
+
toolInput={msg.toolInput}
|
|
71
|
+
toolOutput={msg.toolOutput}
|
|
72
|
+
toolError={msg.toolError}
|
|
73
|
+
running={pending && generating}
|
|
74
|
+
/>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const thinking = msg.meta?.thinking;
|
|
79
|
+
const thinkingDuration = msg.meta?.thinkingDuration;
|
|
80
|
+
return (
|
|
81
|
+
<ChatAgentMessage thinking={thinking} thinkingDuration={thinkingDuration ?? 0} thinkingStreaming={thinking != null && thinkingDuration == null}>
|
|
82
|
+
{msg.content ? <ChatMarkdown content={msg.content} streaming={!!msg.streaming} /> : null}
|
|
83
|
+
</ChatAgentMessage>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
export function AgentMessageList({
|
|
89
|
+
messages,
|
|
90
|
+
generating,
|
|
91
|
+
toolUis,
|
|
92
|
+
showFooter,
|
|
93
|
+
status,
|
|
94
|
+
}: {
|
|
95
|
+
messages: AgentMessage[];
|
|
96
|
+
generating: boolean;
|
|
97
|
+
toolUis?: AgentToolUI[];
|
|
98
|
+
showFooter: boolean;
|
|
99
|
+
status: string;
|
|
100
|
+
}) {
|
|
101
|
+
return (
|
|
102
|
+
<div className="flex flex-col">
|
|
103
|
+
{messages.map((msg) => (
|
|
104
|
+
<MessageRow key={msg.id} msg={msg} generating={generating} toolUis={toolUis} />
|
|
105
|
+
))}
|
|
106
|
+
{showFooter ? (
|
|
107
|
+
<div className="mt-1 px-4 pb-0.5">
|
|
108
|
+
<Shimmer className="text-sm font-medium text-tertiary-foreground">{status}</Shimmer>
|
|
109
|
+
</div>
|
|
110
|
+
) : null}
|
|
111
|
+
</div>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { AgentHistoryMessage, AgentMessage } from "./types";
|
|
2
|
+
import { hasMeaningfulInput } from "./utils";
|
|
3
|
+
|
|
4
|
+
let _id = 0;
|
|
5
|
+
export function nextId(prefix: string) {
|
|
6
|
+
return `${prefix}-${Date.now()}-${++_id}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function thinkingDurationSec(startedAt: number): number {
|
|
10
|
+
if (!startedAt) return 0;
|
|
11
|
+
return Math.round((Date.now() - startedAt) / 1000);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function buildAgentHistory(messages: AgentMessage[]): AgentHistoryMessage[] {
|
|
15
|
+
return messages
|
|
16
|
+
.filter((m) => {
|
|
17
|
+
if (m.role === "user") return m.content.trim() !== "";
|
|
18
|
+
if (m.role === "assistant") return m.content.trim() !== "";
|
|
19
|
+
if (m.role === "tool-call") return m.toolOutput != null;
|
|
20
|
+
return false;
|
|
21
|
+
})
|
|
22
|
+
.map((m) => {
|
|
23
|
+
if (m.role === "tool-call") {
|
|
24
|
+
return {
|
|
25
|
+
role: "tool-call" as const,
|
|
26
|
+
content: "" as const,
|
|
27
|
+
toolCallId: m.toolCallId,
|
|
28
|
+
toolName: m.toolName,
|
|
29
|
+
toolInput: m.toolInput,
|
|
30
|
+
toolOutput: m.toolOutput,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return { role: m.role as "user" | "assistant", content: m.content };
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function finalizeStreaming(prev: AgentMessage[]): AgentMessage[] {
|
|
38
|
+
return prev
|
|
39
|
+
.map((m) => {
|
|
40
|
+
if (m.role === "assistant" && m.streaming) {
|
|
41
|
+
if (!m.content.trim() && m.meta?.thinking) {
|
|
42
|
+
return {
|
|
43
|
+
...m,
|
|
44
|
+
role: "thinking" as const,
|
|
45
|
+
content: String(m.meta.thinking),
|
|
46
|
+
streaming: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return { ...m, streaming: false };
|
|
50
|
+
}
|
|
51
|
+
return m;
|
|
52
|
+
})
|
|
53
|
+
.filter((m) => !(m.role === "assistant" && !m.content.trim() && !m.meta?.thinking));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function failOpenTools(prev: AgentMessage[], error: string): AgentMessage[] {
|
|
57
|
+
return prev.map((m) => {
|
|
58
|
+
if (m.role !== "tool-call" || m.toolOutput != null || m.toolError) return m;
|
|
59
|
+
return { ...m, toolError: error, toolOutput: JSON.stringify({ success: false, error }) };
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function sameStreamTool(m: AgentMessage, toolName: string, toolLabel: string) {
|
|
64
|
+
if (m.role !== "tool-call") return false;
|
|
65
|
+
if (m.toolName === toolName) return true;
|
|
66
|
+
if (m.toolLabel && (m.toolLabel === toolLabel || m.toolLabel === toolName)) return true;
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function lastToolIndex(prev: AgentMessage[], pred: (m: AgentMessage) => boolean): number {
|
|
71
|
+
for (let i = prev.length - 1; i >= 0; i--) {
|
|
72
|
+
if (pred(prev[i])) return i;
|
|
73
|
+
}
|
|
74
|
+
return -1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Merge a streamed tool-call into an existing card (same id, still running, or result-before-params). */
|
|
78
|
+
export function findToolCallMergeIndex(
|
|
79
|
+
prev: AgentMessage[],
|
|
80
|
+
event: { toolCallId?: string; toolName: string; toolLabel: string; input: unknown },
|
|
81
|
+
): number {
|
|
82
|
+
if (event.toolCallId) {
|
|
83
|
+
const byId = prev.findIndex((m) => m.role === "tool-call" && m.toolCallId === event.toolCallId);
|
|
84
|
+
if (byId !== -1) return byId;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const pending = lastToolIndex(
|
|
88
|
+
prev,
|
|
89
|
+
(m) => sameStreamTool(m, event.toolName, event.toolLabel) && m.toolOutput == null && !m.toolError,
|
|
90
|
+
);
|
|
91
|
+
if (pending !== -1) {
|
|
92
|
+
const m = prev[pending];
|
|
93
|
+
if (!event.toolCallId || !m.toolCallId || m.toolCallId === event.toolCallId) return pending;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!hasMeaningfulInput(event.input)) return -1;
|
|
97
|
+
|
|
98
|
+
const sparse = lastToolIndex(prev, (m) => sameStreamTool(m, event.toolName, event.toolLabel) && !hasMeaningfulInput(m.toolInput));
|
|
99
|
+
if (sparse === -1) return -1;
|
|
100
|
+
const m = prev[sparse];
|
|
101
|
+
if (!event.toolCallId || !m.toolCallId || m.toolCallId === event.toolCallId) return sparse;
|
|
102
|
+
return -1;
|
|
103
|
+
}
|
|
104
|
+
|