@copilotkit/react-core 1.54.1 → 1.55.0-next.7
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/CHANGELOG.md +117 -116
- package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
- package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
- package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
- package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
- package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
- package/dist/index.cjs +27 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1941 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +77 -7
- package/dist/v2/index.css +1 -2
- package/dist/v2/index.d.cts +6 -4
- package/dist/v2/index.d.mts +6 -4
- package/dist/v2/index.mjs +7 -4
- package/dist/v2/index.umd.js +5725 -24
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +37 -9
- package/scripts/scope-preflight.mjs +101 -0
- package/src/components/CopilotListeners.tsx +2 -6
- package/src/components/copilot-provider/copilot-messages.tsx +1 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
- package/src/components/copilot-provider/copilotkit.tsx +4 -4
- package/src/context/copilot-messages-context.tsx +1 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
- package/src/hooks/use-agent-nodename.ts +1 -1
- package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat_internal.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +1 -1
- package/src/hooks/use-frontend-tool.ts +2 -2
- package/src/hooks/use-human-in-the-loop.ts +2 -2
- package/src/hooks/use-langgraph-interrupt.ts +2 -5
- package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
- package/src/hooks/use-render-tool-call.ts +1 -1
- package/src/lib/copilot-task.ts +1 -1
- package/src/setupTests.ts +18 -14
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
- package/src/v2/__tests__/globalSetup.ts +14 -0
- package/src/v2/__tests__/setup.ts +93 -0
- package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
- package/src/v2/components/CopilotKitInspector.tsx +50 -0
- package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
- package/src/v2/components/WildcardToolCallRender.tsx +86 -0
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
- package/src/v2/components/chat/CopilotChat.tsx +431 -0
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
- package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
- package/src/v2/components/chat/CopilotChatView.tsx +598 -0
- package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
- package/src/v2/components/chat/CopilotPopup.tsx +81 -0
- package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
- package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
- package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
- package/src/v2/components/chat/__tests__/setup.ts +1 -0
- package/src/v2/components/chat/index.ts +79 -0
- package/src/v2/components/index.ts +7 -0
- package/src/v2/components/license-warning-banner.tsx +198 -0
- package/src/v2/components/ui/button.tsx +123 -0
- package/src/v2/components/ui/dropdown-menu.tsx +258 -0
- package/src/v2/components/ui/tooltip.tsx +60 -0
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
- package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
- package/src/v2/hooks/index.ts +18 -0
- package/src/v2/hooks/use-agent-context.tsx +45 -0
- package/src/v2/hooks/use-agent.tsx +155 -0
- package/src/v2/hooks/use-component.tsx +89 -0
- package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
- package/src/v2/hooks/use-default-render-tool.tsx +254 -0
- package/src/v2/hooks/use-frontend-tool.tsx +43 -0
- package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
- package/src/v2/hooks/use-interrupt.tsx +305 -0
- package/src/v2/hooks/use-keyboard-height.tsx +67 -0
- package/src/v2/hooks/use-render-activity-message.tsx +73 -0
- package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
- package/src/v2/hooks/use-render-tool-call.tsx +175 -0
- package/src/v2/hooks/use-render-tool.tsx +181 -0
- package/src/v2/hooks/use-suggestions.tsx +91 -0
- package/src/v2/hooks/use-threads.tsx +256 -0
- package/src/v2/hooks/useKatexStyles.ts +27 -0
- package/src/v2/index.css +1 -1
- package/src/v2/index.ts +18 -2
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
- package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
- package/src/v2/lib/react-core.ts +156 -0
- package/src/v2/lib/slots.tsx +143 -0
- package/src/v2/lib/transcription-client.ts +184 -0
- package/src/v2/lib/utils.ts +8 -0
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
- package/src/v2/providers/CopilotKitProvider.tsx +600 -0
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
- package/src/v2/providers/index.ts +14 -0
- package/src/v2/styles/globals.css +230 -0
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
- package/src/v2/types/defineToolCallRenderer.ts +65 -0
- package/src/v2/types/frontend-tool.ts +8 -0
- package/src/v2/types/human-in-the-loop.ts +33 -0
- package/src/v2/types/index.ts +7 -0
- package/src/v2/types/interrupt.ts +15 -0
- package/src/v2/types/react-activity-message-renderer.ts +27 -0
- package/src/v2/types/react-custom-message-renderer.ts +17 -0
- package/src/v2/types/react-tool-call-renderer.ts +32 -0
- package/tsdown.config.ts +34 -10
- package/vitest.config.mjs +4 -3
- package/LICENSE +0 -21
- package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
- package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
- package/dist/copilotkit-C94ayZbs.cjs +0 -2161
- package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
- package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
- package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
- package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
- package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
- package/dist/v2/index.css.map +0 -1
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import {
|
|
5
|
+
CopilotKitProvider,
|
|
6
|
+
useCopilotKit,
|
|
7
|
+
} from "../../../providers/CopilotKitProvider";
|
|
8
|
+
import { CopilotChat } from "../CopilotChat";
|
|
9
|
+
import {
|
|
10
|
+
AbstractAgent,
|
|
11
|
+
EventType,
|
|
12
|
+
type BaseEvent,
|
|
13
|
+
type RunAgentInput,
|
|
14
|
+
} from "@ag-ui/client";
|
|
15
|
+
import { Observable, Subject } from "rxjs";
|
|
16
|
+
import {
|
|
17
|
+
defineToolCallRenderer,
|
|
18
|
+
ReactToolCallRenderer,
|
|
19
|
+
ReactFrontendTool,
|
|
20
|
+
} from "../../../types";
|
|
21
|
+
import CopilotChatToolCallsView from "../CopilotChatToolCallsView";
|
|
22
|
+
import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
|
|
23
|
+
import { AssistantMessage, Message, ToolMessage } from "@ag-ui/core";
|
|
24
|
+
import { ToolCallStatus } from "@copilotkit/core";
|
|
25
|
+
import { useFrontendTool } from "../../../hooks/use-frontend-tool";
|
|
26
|
+
|
|
27
|
+
// A minimal mock agent that streams a tool call and a result
|
|
28
|
+
class MockStreamingAgent extends AbstractAgent {
|
|
29
|
+
clone(): MockStreamingAgent {
|
|
30
|
+
return new MockStreamingAgent();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async detachActiveRun(): Promise<void> {}
|
|
34
|
+
|
|
35
|
+
run(_input: RunAgentInput): Observable<BaseEvent> {
|
|
36
|
+
return new Observable<BaseEvent>((observer) => {
|
|
37
|
+
const messageId = `m_${Date.now()}`;
|
|
38
|
+
const toolCallId = `tc_${Date.now()}`;
|
|
39
|
+
|
|
40
|
+
// Start run
|
|
41
|
+
observer.next({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
42
|
+
|
|
43
|
+
// Stream assistant text chunks
|
|
44
|
+
observer.next({
|
|
45
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
46
|
+
messageId,
|
|
47
|
+
delta: "I will check the weather.",
|
|
48
|
+
} as BaseEvent);
|
|
49
|
+
|
|
50
|
+
// Start tool call (first chunk contains name + first args)
|
|
51
|
+
observer.next({
|
|
52
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
53
|
+
toolCallId,
|
|
54
|
+
toolCallName: "getWeather",
|
|
55
|
+
parentMessageId: messageId,
|
|
56
|
+
delta: '{"location":"Paris","unit":"c',
|
|
57
|
+
} as BaseEvent);
|
|
58
|
+
|
|
59
|
+
// Continue tool call args
|
|
60
|
+
observer.next({
|
|
61
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
62
|
+
toolCallId,
|
|
63
|
+
parentMessageId: messageId,
|
|
64
|
+
delta: 'elsius"}',
|
|
65
|
+
} as BaseEvent);
|
|
66
|
+
|
|
67
|
+
// Tool result
|
|
68
|
+
observer.next({
|
|
69
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
70
|
+
toolCallId,
|
|
71
|
+
messageId: `${messageId}_result`,
|
|
72
|
+
content: JSON.stringify({ temperature: 21, unit: "celsius" }),
|
|
73
|
+
} as BaseEvent);
|
|
74
|
+
|
|
75
|
+
// Finish run
|
|
76
|
+
observer.next({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
77
|
+
observer.complete();
|
|
78
|
+
|
|
79
|
+
return () => {};
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
describe("CopilotChat tool rendering with mock agent", () => {
|
|
85
|
+
function renderWithProvider() {
|
|
86
|
+
const agents = { default: new MockStreamingAgent() };
|
|
87
|
+
const renderToolCalls = [
|
|
88
|
+
defineToolCallRenderer({
|
|
89
|
+
name: "getWeather",
|
|
90
|
+
args: z.object({
|
|
91
|
+
location: z.string(),
|
|
92
|
+
unit: z.string(),
|
|
93
|
+
}),
|
|
94
|
+
render: ({ name, args, result }) => (
|
|
95
|
+
<div data-testid="weather-result">
|
|
96
|
+
Tool: {name} | args: {args.location}-{args.unit} | result:{" "}
|
|
97
|
+
{String(result ?? "")}
|
|
98
|
+
</div>
|
|
99
|
+
),
|
|
100
|
+
}),
|
|
101
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
102
|
+
|
|
103
|
+
return render(
|
|
104
|
+
<CopilotKitProvider
|
|
105
|
+
agents__unsafe_dev_only={agents}
|
|
106
|
+
renderToolCalls={renderToolCalls}
|
|
107
|
+
>
|
|
108
|
+
<div style={{ height: 400 }}>
|
|
109
|
+
<CopilotChat />
|
|
110
|
+
</div>
|
|
111
|
+
</CopilotKitProvider>,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
it("renders the tool component when the agent emits a tool call and result", async () => {
|
|
116
|
+
renderWithProvider();
|
|
117
|
+
|
|
118
|
+
// Type a message and submit
|
|
119
|
+
const input = await screen.findByRole("textbox");
|
|
120
|
+
fireEvent.change(input, { target: { value: "What is the weather?" } });
|
|
121
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
122
|
+
|
|
123
|
+
// Assert that our tool render appears with the expected test id
|
|
124
|
+
const tool = await screen.findByTestId("weather-result");
|
|
125
|
+
expect(tool).toBeDefined();
|
|
126
|
+
|
|
127
|
+
// Optionally, ensure result content shows up (from our mock agent)
|
|
128
|
+
await waitFor(() => {
|
|
129
|
+
expect(tool.textContent).toMatch(/temperature/);
|
|
130
|
+
expect(tool.textContent).toMatch(/celsius/);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("Tool render status narrowing", () => {
|
|
136
|
+
function renderStatusWithProvider({
|
|
137
|
+
isRunning,
|
|
138
|
+
withResult,
|
|
139
|
+
}: {
|
|
140
|
+
isRunning: boolean;
|
|
141
|
+
withResult: boolean;
|
|
142
|
+
}) {
|
|
143
|
+
const renderToolCalls = [
|
|
144
|
+
defineToolCallRenderer({
|
|
145
|
+
name: "getWeather",
|
|
146
|
+
args: z.object({ city: z.string().optional() }),
|
|
147
|
+
render: ({ status, args, result }) => {
|
|
148
|
+
if (status === ToolCallStatus.InProgress) {
|
|
149
|
+
return (
|
|
150
|
+
<div data-testid="status">
|
|
151
|
+
INPROGRESS {String(args.city ?? "")}
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (status === ToolCallStatus.Executing) {
|
|
156
|
+
return <div data-testid="status">EXECUTING {args.city}</div>;
|
|
157
|
+
}
|
|
158
|
+
// ToolCallStatus.Complete
|
|
159
|
+
return (
|
|
160
|
+
<div data-testid="status">
|
|
161
|
+
COMPLETE {args.city} {String(result ?? "")}
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
},
|
|
165
|
+
}),
|
|
166
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
167
|
+
|
|
168
|
+
const toolCallId = "tc_status_1";
|
|
169
|
+
|
|
170
|
+
const assistantMessage: AssistantMessage = {
|
|
171
|
+
id: "a1",
|
|
172
|
+
role: "assistant",
|
|
173
|
+
content: "",
|
|
174
|
+
toolCalls: [
|
|
175
|
+
{
|
|
176
|
+
id: toolCallId,
|
|
177
|
+
type: "function",
|
|
178
|
+
function: { name: "getWeather", arguments: '{"city":"Berlin"}' },
|
|
179
|
+
} as any,
|
|
180
|
+
],
|
|
181
|
+
} as AssistantMessage;
|
|
182
|
+
|
|
183
|
+
const messages: Message[] = [];
|
|
184
|
+
if (withResult) {
|
|
185
|
+
messages.push({
|
|
186
|
+
id: "t1",
|
|
187
|
+
role: "tool",
|
|
188
|
+
toolCallId,
|
|
189
|
+
content: "Sunny",
|
|
190
|
+
} as ToolMessage as any);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return render(
|
|
194
|
+
<CopilotKitProvider renderToolCalls={renderToolCalls}>
|
|
195
|
+
<CopilotChatConfigurationProvider
|
|
196
|
+
agentId="default"
|
|
197
|
+
threadId="test-thread"
|
|
198
|
+
>
|
|
199
|
+
<CopilotChatToolCallsView
|
|
200
|
+
message={assistantMessage}
|
|
201
|
+
messages={messages}
|
|
202
|
+
/>
|
|
203
|
+
</CopilotChatConfigurationProvider>
|
|
204
|
+
</CopilotKitProvider>,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
it("renders InProgress when running and no result", async () => {
|
|
209
|
+
renderStatusWithProvider({ isRunning: true, withResult: false });
|
|
210
|
+
const el = await screen.findByTestId("status");
|
|
211
|
+
expect(el.textContent).toMatch(/INPROGRESS/);
|
|
212
|
+
expect(el.textContent).toMatch(/Berlin/);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("renders Complete with result when tool message exists", async () => {
|
|
216
|
+
renderStatusWithProvider({ isRunning: false, withResult: true });
|
|
217
|
+
const el = await screen.findByTestId("status");
|
|
218
|
+
expect(el.textContent).toMatch(/COMPLETE/);
|
|
219
|
+
expect(el.textContent).toMatch(/Berlin/);
|
|
220
|
+
expect(el.textContent).toMatch(/Sunny/);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("renders InProgress when not running and no tool result", async () => {
|
|
224
|
+
renderStatusWithProvider({ isRunning: false, withResult: false });
|
|
225
|
+
const el = await screen.findByTestId("status");
|
|
226
|
+
expect(el.textContent).toMatch(/INPROGRESS/);
|
|
227
|
+
expect(el.textContent).toMatch(/Berlin/);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// A controllable streaming agent to step through events deterministically
|
|
232
|
+
class MockStepwiseAgent extends AbstractAgent {
|
|
233
|
+
private subject = new Subject<BaseEvent>();
|
|
234
|
+
|
|
235
|
+
emit(event: BaseEvent) {
|
|
236
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
237
|
+
this.isRunning = true;
|
|
238
|
+
} else if (
|
|
239
|
+
event.type === EventType.RUN_FINISHED ||
|
|
240
|
+
event.type === EventType.RUN_ERROR
|
|
241
|
+
) {
|
|
242
|
+
this.isRunning = false;
|
|
243
|
+
}
|
|
244
|
+
this.subject.next(event);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
complete() {
|
|
248
|
+
this.isRunning = false;
|
|
249
|
+
this.subject.complete();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
clone(): MockStepwiseAgent {
|
|
253
|
+
// For tests, return same instance so we can keep controlling it.
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async detachActiveRun(): Promise<void> {}
|
|
258
|
+
|
|
259
|
+
run(_input: RunAgentInput): Observable<BaseEvent> {
|
|
260
|
+
return this.subject.asObservable();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
describe("Streaming in-progress without timers", () => {
|
|
265
|
+
it("shows InProgress for partial args and Complete after result", async () => {
|
|
266
|
+
const agent = new MockStepwiseAgent();
|
|
267
|
+
|
|
268
|
+
const renderToolCalls = [
|
|
269
|
+
defineToolCallRenderer({
|
|
270
|
+
name: "getWeather",
|
|
271
|
+
args: z.object({
|
|
272
|
+
location: z.string(),
|
|
273
|
+
unit: z.string(),
|
|
274
|
+
}),
|
|
275
|
+
render: ({ name, status, args, result }) => (
|
|
276
|
+
<div data-testid="tool-status">
|
|
277
|
+
{name}{" "}
|
|
278
|
+
{status === ToolCallStatus.InProgress ? "INPROGRESS" : "COMPLETE"}{" "}
|
|
279
|
+
{String(args.location ?? "")} - {String(args.unit ?? "")}{" "}
|
|
280
|
+
{String(result ?? "")}
|
|
281
|
+
</div>
|
|
282
|
+
),
|
|
283
|
+
}),
|
|
284
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
285
|
+
|
|
286
|
+
render(
|
|
287
|
+
<CopilotKitProvider
|
|
288
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
289
|
+
renderToolCalls={renderToolCalls}
|
|
290
|
+
>
|
|
291
|
+
<div style={{ height: 400 }}>
|
|
292
|
+
<CopilotChat />
|
|
293
|
+
</div>
|
|
294
|
+
</CopilotKitProvider>,
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Submit a user message to trigger runAgent
|
|
298
|
+
const input = await screen.findByRole("textbox");
|
|
299
|
+
fireEvent.change(input, { target: { value: "Weather please" } });
|
|
300
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
301
|
+
|
|
302
|
+
// Allow React to process the state update
|
|
303
|
+
await waitFor(() => {
|
|
304
|
+
expect(screen.getByText("Weather please")).toBeDefined();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const messageId = "m_step";
|
|
308
|
+
const toolCallId = "tc_step";
|
|
309
|
+
|
|
310
|
+
// Begin run and stream partial tool-call args
|
|
311
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
312
|
+
agent.emit({
|
|
313
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
314
|
+
messageId,
|
|
315
|
+
delta: "Checking weather",
|
|
316
|
+
} as BaseEvent);
|
|
317
|
+
|
|
318
|
+
// First emit just the tool call start with partial args
|
|
319
|
+
agent.emit({
|
|
320
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
321
|
+
toolCallId,
|
|
322
|
+
toolCallName: "getWeather",
|
|
323
|
+
parentMessageId: messageId,
|
|
324
|
+
delta: '{"location":"Paris"',
|
|
325
|
+
} as BaseEvent);
|
|
326
|
+
|
|
327
|
+
// Wait for the tool status element to show partial args
|
|
328
|
+
await waitFor(async () => {
|
|
329
|
+
const el = await screen.findByTestId("tool-status");
|
|
330
|
+
expect(el.textContent).toContain("getWeather INPROGRESS");
|
|
331
|
+
expect(el.textContent).toContain("Paris");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// Continue streaming more partial data
|
|
335
|
+
agent.emit({
|
|
336
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
337
|
+
toolCallId,
|
|
338
|
+
parentMessageId: messageId,
|
|
339
|
+
delta: ',"unit":"celsius"}',
|
|
340
|
+
} as BaseEvent);
|
|
341
|
+
|
|
342
|
+
// Wait for the tool status element and check it shows complete args but no result yet
|
|
343
|
+
await waitFor(
|
|
344
|
+
async () => {
|
|
345
|
+
const el = await screen.findByTestId("tool-status");
|
|
346
|
+
expect(el.textContent).toContain("getWeather");
|
|
347
|
+
expect(el.textContent).toContain("Paris");
|
|
348
|
+
expect(el.textContent).toContain("celsius");
|
|
349
|
+
// Since we haven't sent a result yet, it should be INPROGRESS
|
|
350
|
+
expect(el.textContent).toMatch(/INPROGRESS/);
|
|
351
|
+
},
|
|
352
|
+
{ timeout: 3000 },
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
// Now send the tool result
|
|
356
|
+
agent.emit({
|
|
357
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
358
|
+
toolCallId,
|
|
359
|
+
messageId: `${messageId}_result`,
|
|
360
|
+
content: JSON.stringify({ temperature: 21, unit: "celsius" }),
|
|
361
|
+
} as BaseEvent);
|
|
362
|
+
|
|
363
|
+
// Check result appears and status changes to COMPLETE
|
|
364
|
+
await waitFor(async () => {
|
|
365
|
+
const el = await screen.findByTestId("tool-status");
|
|
366
|
+
expect(el.textContent).toMatch(/COMPLETE/);
|
|
367
|
+
expect(el.textContent).toContain("temperature");
|
|
368
|
+
expect(el.textContent).toContain("21");
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
372
|
+
agent.complete();
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
describe("Executing State Transitions", () => {
|
|
377
|
+
it("should show Executing status while tool handler is running", async () => {
|
|
378
|
+
const agent = new MockStepwiseAgent();
|
|
379
|
+
let resolveHandler: (() => void) | undefined;
|
|
380
|
+
|
|
381
|
+
const ToolWithDeferredHandler: React.FC = () => {
|
|
382
|
+
const tool: ReactFrontendTool<{ value: string }> = {
|
|
383
|
+
name: "slowTool",
|
|
384
|
+
parameters: z.object({ value: z.string() }),
|
|
385
|
+
handler: async () =>
|
|
386
|
+
new Promise((resolve) => {
|
|
387
|
+
resolveHandler = () => resolve({ result: "done" });
|
|
388
|
+
}),
|
|
389
|
+
render: ({ name, status, args, result }) => (
|
|
390
|
+
<div data-testid="slow-tool-status">
|
|
391
|
+
Tool: {name} | Status: {status} | Value: {args.value} | Result:{" "}
|
|
392
|
+
{result ? "Complete" : "Pending"}
|
|
393
|
+
</div>
|
|
394
|
+
),
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
useFrontendTool(tool);
|
|
398
|
+
return null;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
render(
|
|
402
|
+
<CopilotKitProvider agents__unsafe_dev_only={{ default: agent }}>
|
|
403
|
+
<ToolWithDeferredHandler />
|
|
404
|
+
<div style={{ height: 400 }}>
|
|
405
|
+
<CopilotChat />
|
|
406
|
+
</div>
|
|
407
|
+
</CopilotKitProvider>,
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
const input = await screen.findByRole("textbox");
|
|
411
|
+
fireEvent.change(input, { target: { value: "Run slow tool" } });
|
|
412
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
413
|
+
|
|
414
|
+
await waitFor(() => {
|
|
415
|
+
expect(screen.getByText("Run slow tool")).toBeDefined();
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const messageId = "m_exec";
|
|
419
|
+
const toolCallId = "tc_exec";
|
|
420
|
+
|
|
421
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
422
|
+
agent.emit({
|
|
423
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
424
|
+
toolCallId,
|
|
425
|
+
toolCallName: "slowTool",
|
|
426
|
+
parentMessageId: messageId,
|
|
427
|
+
delta: '{"value":"test"}',
|
|
428
|
+
} as BaseEvent);
|
|
429
|
+
|
|
430
|
+
await waitFor(() => {
|
|
431
|
+
const status = screen.getByTestId("slow-tool-status");
|
|
432
|
+
expect(status.textContent).toMatch(/Status: inProgress/i);
|
|
433
|
+
expect(status.textContent).toMatch(/Value: test/);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
437
|
+
agent.complete();
|
|
438
|
+
|
|
439
|
+
await waitFor(() => {
|
|
440
|
+
const status = screen.getByTestId("slow-tool-status");
|
|
441
|
+
expect(status.textContent).toMatch(/Status: executing/i);
|
|
442
|
+
expect(resolveHandler).toBeTruthy();
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
if (resolveHandler) {
|
|
446
|
+
resolveHandler();
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
await waitFor(() => {
|
|
450
|
+
const status = screen.getByTestId("slow-tool-status");
|
|
451
|
+
expect(status.textContent).toMatch(/Status: complete/i);
|
|
452
|
+
expect(status.textContent).toMatch(/Result: Complete/);
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
describe("Multiple Tool Calls in Same Message", () => {
|
|
458
|
+
it("should render multiple tools independently with their own status", async () => {
|
|
459
|
+
const agent = new MockStepwiseAgent();
|
|
460
|
+
|
|
461
|
+
const renderToolCalls = [
|
|
462
|
+
defineToolCallRenderer({
|
|
463
|
+
name: "tool1",
|
|
464
|
+
args: z.object({ id: z.string() }),
|
|
465
|
+
render: ({ status, args, result }) => (
|
|
466
|
+
<div data-testid={`tool1-${args.id}`}>
|
|
467
|
+
Tool1[{args.id}]: {status} -{" "}
|
|
468
|
+
{result ? JSON.stringify(result) : "waiting"}
|
|
469
|
+
</div>
|
|
470
|
+
),
|
|
471
|
+
}),
|
|
472
|
+
defineToolCallRenderer({
|
|
473
|
+
name: "tool2",
|
|
474
|
+
args: z.object({ id: z.string() }),
|
|
475
|
+
render: ({ status, args, result }) => (
|
|
476
|
+
<div data-testid={`tool2-${args.id}`}>
|
|
477
|
+
Tool2[{args.id}]: {status} -{" "}
|
|
478
|
+
{result ? JSON.stringify(result) : "waiting"}
|
|
479
|
+
</div>
|
|
480
|
+
),
|
|
481
|
+
}),
|
|
482
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
483
|
+
|
|
484
|
+
render(
|
|
485
|
+
<CopilotKitProvider
|
|
486
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
487
|
+
renderToolCalls={renderToolCalls}
|
|
488
|
+
>
|
|
489
|
+
<div style={{ height: 400 }}>
|
|
490
|
+
<CopilotChat />
|
|
491
|
+
</div>
|
|
492
|
+
</CopilotKitProvider>,
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
// Submit message
|
|
496
|
+
const input = await screen.findByRole("textbox");
|
|
497
|
+
fireEvent.change(input, { target: { value: "Multiple tools" } });
|
|
498
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
499
|
+
|
|
500
|
+
// Allow React to process the state update
|
|
501
|
+
await waitFor(() => {
|
|
502
|
+
expect(screen.getByText("Multiple tools")).toBeDefined();
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
const messageId = "m_multi";
|
|
506
|
+
const toolCallId1 = "tc_1";
|
|
507
|
+
const toolCallId2 = "tc_2";
|
|
508
|
+
const toolCallId3 = "tc_3";
|
|
509
|
+
|
|
510
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
511
|
+
|
|
512
|
+
// Stream three tool calls (2 of tool1, 1 of tool2)
|
|
513
|
+
agent.emit({
|
|
514
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
515
|
+
toolCallId: toolCallId1,
|
|
516
|
+
toolCallName: "tool1",
|
|
517
|
+
parentMessageId: messageId,
|
|
518
|
+
delta: '{"id":"first"}',
|
|
519
|
+
} as BaseEvent);
|
|
520
|
+
|
|
521
|
+
agent.emit({
|
|
522
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
523
|
+
toolCallId: toolCallId2,
|
|
524
|
+
toolCallName: "tool2",
|
|
525
|
+
parentMessageId: messageId,
|
|
526
|
+
delta: '{"id":"second"}',
|
|
527
|
+
} as BaseEvent);
|
|
528
|
+
|
|
529
|
+
agent.emit({
|
|
530
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
531
|
+
toolCallId: toolCallId3,
|
|
532
|
+
toolCallName: "tool1",
|
|
533
|
+
parentMessageId: messageId,
|
|
534
|
+
delta: '{"id":"third"}',
|
|
535
|
+
} as BaseEvent);
|
|
536
|
+
|
|
537
|
+
// All three should render
|
|
538
|
+
await waitFor(() => {
|
|
539
|
+
expect(screen.getByTestId("tool1-first")).toBeDefined();
|
|
540
|
+
expect(screen.getByTestId("tool2-second")).toBeDefined();
|
|
541
|
+
expect(screen.getByTestId("tool1-third")).toBeDefined();
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
// Send results in different order
|
|
545
|
+
agent.emit({
|
|
546
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
547
|
+
toolCallId: toolCallId2,
|
|
548
|
+
messageId: `${messageId}_r2`,
|
|
549
|
+
content: JSON.stringify({ result: "B" }),
|
|
550
|
+
} as BaseEvent);
|
|
551
|
+
|
|
552
|
+
await waitFor(() => {
|
|
553
|
+
const tool2 = screen.getByTestId("tool2-second");
|
|
554
|
+
expect(tool2.textContent).toContain("B");
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
agent.emit({
|
|
558
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
559
|
+
toolCallId: toolCallId1,
|
|
560
|
+
messageId: `${messageId}_r1`,
|
|
561
|
+
content: JSON.stringify({ result: "A" }),
|
|
562
|
+
} as BaseEvent);
|
|
563
|
+
|
|
564
|
+
agent.emit({
|
|
565
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
566
|
+
toolCallId: toolCallId3,
|
|
567
|
+
messageId: `${messageId}_r3`,
|
|
568
|
+
content: JSON.stringify({ result: "C" }),
|
|
569
|
+
} as BaseEvent);
|
|
570
|
+
|
|
571
|
+
// All results should be visible
|
|
572
|
+
await waitFor(() => {
|
|
573
|
+
expect(screen.getByTestId("tool1-first").textContent).toContain("A");
|
|
574
|
+
expect(screen.getByTestId("tool2-second").textContent).toContain("B");
|
|
575
|
+
expect(screen.getByTestId("tool1-third").textContent).toContain("C");
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
579
|
+
agent.complete();
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
describe("Partial Args Accumulation", () => {
|
|
584
|
+
it("should properly show InProgress status with accumulating partial args", async () => {
|
|
585
|
+
const agent = new MockStepwiseAgent();
|
|
586
|
+
|
|
587
|
+
const renderToolCalls = [
|
|
588
|
+
defineToolCallRenderer({
|
|
589
|
+
name: "complexTool",
|
|
590
|
+
args: z.object({
|
|
591
|
+
name: z.string().optional(),
|
|
592
|
+
age: z.number().optional(),
|
|
593
|
+
city: z.string().optional(),
|
|
594
|
+
}),
|
|
595
|
+
render: ({ status, args }) => (
|
|
596
|
+
<div data-testid="complex-tool">
|
|
597
|
+
<div>Status: {status}</div>
|
|
598
|
+
<div>Name: {args.name || "pending"}</div>
|
|
599
|
+
<div>Age: {args.age !== undefined ? args.age : "pending"}</div>
|
|
600
|
+
<div>City: {args.city || "pending"}</div>
|
|
601
|
+
</div>
|
|
602
|
+
),
|
|
603
|
+
}),
|
|
604
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
605
|
+
|
|
606
|
+
render(
|
|
607
|
+
<CopilotKitProvider
|
|
608
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
609
|
+
renderToolCalls={renderToolCalls}
|
|
610
|
+
>
|
|
611
|
+
<div style={{ height: 400 }}>
|
|
612
|
+
<CopilotChat />
|
|
613
|
+
</div>
|
|
614
|
+
</CopilotKitProvider>,
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
// Submit message
|
|
618
|
+
const input = await screen.findByRole("textbox");
|
|
619
|
+
fireEvent.change(input, { target: { value: "Complex tool test" } });
|
|
620
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
621
|
+
|
|
622
|
+
// Allow React to process the state update
|
|
623
|
+
await waitFor(() => {
|
|
624
|
+
expect(screen.getByText("Complex tool test")).toBeDefined();
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
const messageId = "m_partial";
|
|
628
|
+
const toolCallId = "tc_partial";
|
|
629
|
+
|
|
630
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
631
|
+
|
|
632
|
+
// Stream args piece by piece
|
|
633
|
+
agent.emit({
|
|
634
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
635
|
+
toolCallId,
|
|
636
|
+
toolCallName: "complexTool",
|
|
637
|
+
parentMessageId: messageId,
|
|
638
|
+
delta: '{"name":"',
|
|
639
|
+
} as BaseEvent);
|
|
640
|
+
|
|
641
|
+
// Let React update with the partial data
|
|
642
|
+
await waitFor(() => {
|
|
643
|
+
const tool = screen.queryByTestId("complex-tool");
|
|
644
|
+
expect(tool).toBeDefined();
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
agent.emit({
|
|
648
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
649
|
+
toolCallId,
|
|
650
|
+
parentMessageId: messageId,
|
|
651
|
+
delta: 'Alice"',
|
|
652
|
+
} as BaseEvent);
|
|
653
|
+
|
|
654
|
+
await waitFor(() => {
|
|
655
|
+
const tool = screen.getByTestId("complex-tool");
|
|
656
|
+
expect(tool.textContent).toContain("Name: Alice");
|
|
657
|
+
expect(tool.textContent).toContain("Age: pending");
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
agent.emit({
|
|
661
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
662
|
+
toolCallId,
|
|
663
|
+
parentMessageId: messageId,
|
|
664
|
+
delta: ',"age":30',
|
|
665
|
+
} as BaseEvent);
|
|
666
|
+
|
|
667
|
+
await waitFor(() => {
|
|
668
|
+
const tool = screen.getByTestId("complex-tool");
|
|
669
|
+
expect(tool.textContent).toContain("Age: 30");
|
|
670
|
+
expect(tool.textContent).toContain("City: pending");
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
agent.emit({
|
|
674
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
675
|
+
toolCallId,
|
|
676
|
+
parentMessageId: messageId,
|
|
677
|
+
delta: ',"city":"Paris"}',
|
|
678
|
+
} as BaseEvent);
|
|
679
|
+
|
|
680
|
+
await waitFor(() => {
|
|
681
|
+
const tool = screen.getByTestId("complex-tool");
|
|
682
|
+
expect(tool.textContent).toContain("City: Paris");
|
|
683
|
+
// All args complete but no result yet - status shows inProgress until result is received
|
|
684
|
+
expect(tool.textContent).toMatch(/Status: (complete|inProgress)/i);
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
688
|
+
agent.complete();
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
describe("Status Persistence After Agent Stops", () => {
|
|
693
|
+
it("should remain in InProgress status after agent stops if no result", async () => {
|
|
694
|
+
const agent = new MockStepwiseAgent();
|
|
695
|
+
|
|
696
|
+
const renderToolCalls = [
|
|
697
|
+
defineToolCallRenderer({
|
|
698
|
+
name: "testTool",
|
|
699
|
+
args: z.object({ value: z.string() }),
|
|
700
|
+
render: ({ args, status }) => (
|
|
701
|
+
<div data-testid="tool-render">
|
|
702
|
+
<span data-testid="status">{status}</span>
|
|
703
|
+
<span data-testid="value">{args.value}</span>
|
|
704
|
+
</div>
|
|
705
|
+
),
|
|
706
|
+
}),
|
|
707
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
708
|
+
|
|
709
|
+
render(
|
|
710
|
+
<CopilotKitProvider
|
|
711
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
712
|
+
renderToolCalls={renderToolCalls}
|
|
713
|
+
>
|
|
714
|
+
<CopilotChat />
|
|
715
|
+
</CopilotKitProvider>,
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
// Submit message to trigger tool call
|
|
719
|
+
const input = await screen.findByRole("textbox");
|
|
720
|
+
fireEvent.change(input, { target: { value: "Test message" } });
|
|
721
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
722
|
+
|
|
723
|
+
// Wait for user message to appear
|
|
724
|
+
await waitFor(() => {
|
|
725
|
+
expect(screen.getByText("Test message")).toBeDefined();
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
const messageId = "msg_status";
|
|
729
|
+
const toolCallId = "tc_status";
|
|
730
|
+
|
|
731
|
+
// Start run and emit tool call
|
|
732
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
733
|
+
|
|
734
|
+
agent.emit({
|
|
735
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
736
|
+
toolCallId,
|
|
737
|
+
toolCallName: "testTool",
|
|
738
|
+
parentMessageId: messageId,
|
|
739
|
+
delta: '{"value":"test"}',
|
|
740
|
+
} as BaseEvent);
|
|
741
|
+
|
|
742
|
+
// Tool should be in InProgress while agent is running
|
|
743
|
+
await waitFor(() => {
|
|
744
|
+
const statusElement = screen.getByTestId("status");
|
|
745
|
+
expect(statusElement.textContent).toBe("inProgress");
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
// Finish the run without providing a tool result
|
|
749
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
750
|
+
|
|
751
|
+
// Important: tool should REMAIN in InProgress status, not Complete
|
|
752
|
+
// Verify status remains inProgress (not changing to complete)
|
|
753
|
+
await waitFor(() => {
|
|
754
|
+
const statusElement = screen.getByTestId("status");
|
|
755
|
+
expect(statusElement.textContent).toBe("inProgress");
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
const statusElement = screen.getByTestId("status");
|
|
759
|
+
expect(statusElement.textContent).toBe("inProgress");
|
|
760
|
+
expect(statusElement.textContent).not.toBe("complete");
|
|
761
|
+
|
|
762
|
+
// To provide result after run finished, we need to start a new run
|
|
763
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
764
|
+
|
|
765
|
+
// Now provide the tool result
|
|
766
|
+
agent.emit({
|
|
767
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
768
|
+
toolCallId,
|
|
769
|
+
messageId: `${messageId}_result`,
|
|
770
|
+
content: JSON.stringify({ result: "Tool execution completed" }),
|
|
771
|
+
} as BaseEvent);
|
|
772
|
+
|
|
773
|
+
// NOW it should be complete
|
|
774
|
+
await waitFor(() => {
|
|
775
|
+
const statusEl = screen.getByTestId("status");
|
|
776
|
+
expect(statusEl.textContent).toBe("complete");
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
780
|
+
agent.complete();
|
|
781
|
+
});
|
|
782
|
+
});
|