@copilotkit/react-core 1.54.1 → 1.55.0-next.8
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 +127 -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,525 @@
|
|
|
1
|
+
import { CopilotKitProvider } from "../../providers/CopilotKitProvider";
|
|
2
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
3
|
+
import { ToolCallStatus } from "@copilotkit/core";
|
|
4
|
+
import { render, screen } from "@testing-library/react";
|
|
5
|
+
import type React from "react";
|
|
6
|
+
import { describe, expect, it, vi } from "vitest";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { defineToolCallRenderer } from "../defineToolCallRenderer";
|
|
9
|
+
import type { ReactToolCallRenderer } from "../react-tool-call-renderer";
|
|
10
|
+
|
|
11
|
+
describe("defineToolCallRenderer", () => {
|
|
12
|
+
describe("Array compatibility", () => {
|
|
13
|
+
it("should work with multiple tool renders in an array", () => {
|
|
14
|
+
// This test verifies that multiple tool renders with different arg types
|
|
15
|
+
// can be used together in the renderToolCalls array
|
|
16
|
+
const WildCardRender = defineToolCallRenderer({
|
|
17
|
+
name: "*",
|
|
18
|
+
render: ({ args, result, name, status }) => {
|
|
19
|
+
return <div>Wildcard: {name}</div>;
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const OtherToolRender = defineToolCallRenderer({
|
|
24
|
+
name: "get_weather",
|
|
25
|
+
args: z.object({
|
|
26
|
+
location: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
render: ({ args, result, name, status }) => {
|
|
29
|
+
return <div>Weather in {args.location}</div>;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// This should compile without errors
|
|
34
|
+
// Testing that mixed types can be used together
|
|
35
|
+
const renderToolCalls = [WildCardRender, OtherToolRender];
|
|
36
|
+
|
|
37
|
+
expect(renderToolCalls).toHaveLength(2);
|
|
38
|
+
expect(renderToolCalls[0]!.name).toBe("*");
|
|
39
|
+
expect(renderToolCalls[1]!.name).toBe("get_weather");
|
|
40
|
+
|
|
41
|
+
// Verify they have the expected structure
|
|
42
|
+
expect(renderToolCalls[0]!.render).toBeDefined();
|
|
43
|
+
expect(renderToolCalls[1]!.render).toBeDefined();
|
|
44
|
+
expect(renderToolCalls[1]!.args).toBeDefined();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should work with CopilotKitProvider accepting mixed tool renders", () => {
|
|
48
|
+
// This is the exact scenario the user reported
|
|
49
|
+
const WildCardRender = defineToolCallRenderer({
|
|
50
|
+
name: "*",
|
|
51
|
+
render: ({ args, result, name, status }) => {
|
|
52
|
+
return <div data-testid="wildcard">TODO: {name}</div>;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const OtherToolRender = defineToolCallRenderer({
|
|
57
|
+
name: "get_weather",
|
|
58
|
+
args: z.object({
|
|
59
|
+
location: z.string(),
|
|
60
|
+
}),
|
|
61
|
+
render: ({ args, result, name, status }) => {
|
|
62
|
+
return <div data-testid="weather">Weather for {args.location}</div>;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// This should compile without type errors
|
|
67
|
+
const TestComponent = () => {
|
|
68
|
+
const renderToolCalls = [WildCardRender, OtherToolRender];
|
|
69
|
+
|
|
70
|
+
// In real usage, this would be passed to CopilotKitProvider
|
|
71
|
+
// We're just checking that the type is compatible
|
|
72
|
+
const providerProps: {
|
|
73
|
+
renderToolCalls?: ReactToolCallRenderer<any>[];
|
|
74
|
+
} = {
|
|
75
|
+
renderToolCalls: renderToolCalls,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return <div data-testid="test">Test</div>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const { getByTestId } = render(<TestComponent />);
|
|
82
|
+
expect(getByTestId("test")).toBeDefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should work with actual CopilotKitProvider - replicating user's exact scenario", () => {
|
|
86
|
+
// Exact replication of the user's code that was causing type errors
|
|
87
|
+
const WildCardRender = defineToolCallRenderer({
|
|
88
|
+
name: "*",
|
|
89
|
+
render: ({ args, result, name, status }) => {
|
|
90
|
+
return <div>TODO</div>;
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const OtherToolRender = defineToolCallRenderer({
|
|
95
|
+
name: "get_weather",
|
|
96
|
+
args: z.object({
|
|
97
|
+
location: z.string(),
|
|
98
|
+
}),
|
|
99
|
+
render: ({ args, result, name, status }) => {
|
|
100
|
+
return <div>TODO</div>;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Create a mock agent for testing
|
|
105
|
+
const mockAgent = {
|
|
106
|
+
clone: vi.fn(),
|
|
107
|
+
run: vi.fn(),
|
|
108
|
+
subscribe: vi.fn(() => ({ unsubscribe: () => {} })),
|
|
109
|
+
} as unknown as AbstractAgent;
|
|
110
|
+
|
|
111
|
+
// This is the exact code pattern the user wanted to use
|
|
112
|
+
// Previously this would cause a type error, now it should compile
|
|
113
|
+
const TestApp = () => (
|
|
114
|
+
<CopilotKitProvider
|
|
115
|
+
agents__unsafe_dev_only={{
|
|
116
|
+
default: mockAgent,
|
|
117
|
+
}}
|
|
118
|
+
renderToolCalls={[WildCardRender, OtherToolRender]}
|
|
119
|
+
>
|
|
120
|
+
<div data-testid="app">App content</div>
|
|
121
|
+
</CopilotKitProvider>
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
// If this renders without TypeScript errors, the fix is working
|
|
125
|
+
const { getByTestId } = render(<TestApp />);
|
|
126
|
+
expect(getByTestId("app")).toBeDefined();
|
|
127
|
+
expect(getByTestId("app").textContent).toBe("App content");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe("Type inference and rendering", () => {
|
|
131
|
+
it("should properly infer types for regular tools", () => {
|
|
132
|
+
const weatherRender = defineToolCallRenderer({
|
|
133
|
+
name: "get_weather",
|
|
134
|
+
args: z.object({
|
|
135
|
+
location: z.string(),
|
|
136
|
+
units: z.enum(["celsius", "fahrenheit"]).optional(),
|
|
137
|
+
}),
|
|
138
|
+
render: ({ args, status, name, result }) => {
|
|
139
|
+
// Test that types are properly inferred
|
|
140
|
+
if (status === ToolCallStatus.InProgress) {
|
|
141
|
+
// args should be Partial
|
|
142
|
+
const loc: string | undefined = args.location;
|
|
143
|
+
return <div data-testid="progress">Loading {loc || "..."}...</div>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (status === ToolCallStatus.Executing) {
|
|
147
|
+
// args should be complete
|
|
148
|
+
const loc: string = args.location;
|
|
149
|
+
return (
|
|
150
|
+
<div data-testid="executing">Fetching weather for {loc}</div>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Complete status
|
|
155
|
+
return (
|
|
156
|
+
<div data-testid="complete">
|
|
157
|
+
Weather in {args.location}: {result}
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Test InProgress state
|
|
164
|
+
const ProgressComponent = weatherRender.render as React.FC<any>;
|
|
165
|
+
const { rerender } = render(
|
|
166
|
+
<ProgressComponent
|
|
167
|
+
name="get_weather"
|
|
168
|
+
args={{ location: "Paris" }}
|
|
169
|
+
status={ToolCallStatus.InProgress}
|
|
170
|
+
result={undefined}
|
|
171
|
+
/>,
|
|
172
|
+
);
|
|
173
|
+
expect(screen.getByTestId("progress").textContent).toBe(
|
|
174
|
+
"Loading Paris...",
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// Test Executing state
|
|
178
|
+
rerender(
|
|
179
|
+
<ProgressComponent
|
|
180
|
+
name="get_weather"
|
|
181
|
+
args={{ location: "London", units: "celsius" }}
|
|
182
|
+
status={ToolCallStatus.Executing}
|
|
183
|
+
result={undefined}
|
|
184
|
+
/>,
|
|
185
|
+
);
|
|
186
|
+
expect(screen.getByTestId("executing").textContent).toBe(
|
|
187
|
+
"Fetching weather for London",
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
// Test Complete state
|
|
191
|
+
rerender(
|
|
192
|
+
<ProgressComponent
|
|
193
|
+
name="get_weather"
|
|
194
|
+
args={{ location: "Tokyo", units: "fahrenheit" }}
|
|
195
|
+
status={ToolCallStatus.Complete}
|
|
196
|
+
result="Sunny, 75°F"
|
|
197
|
+
/>,
|
|
198
|
+
);
|
|
199
|
+
expect(screen.getByTestId("complete").textContent).toBe(
|
|
200
|
+
"Weather in Tokyo: Sunny, 75°F",
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("should work with wildcard tool without args definition", () => {
|
|
205
|
+
// No args field - should default to z.any()
|
|
206
|
+
const wildcardRender = defineToolCallRenderer({
|
|
207
|
+
name: "*",
|
|
208
|
+
render: ({ name, args, status }) => (
|
|
209
|
+
<div data-testid="wildcard">
|
|
210
|
+
<span data-testid="tool-name">{name}</span>
|
|
211
|
+
<span data-testid="status">{status}</span>
|
|
212
|
+
<span data-testid="args">{JSON.stringify(args)}</span>
|
|
213
|
+
</div>
|
|
214
|
+
),
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const WildcardComponent = wildcardRender.render as React.FC<any>;
|
|
218
|
+
|
|
219
|
+
// Test that wildcard receives actual tool name, not "*"
|
|
220
|
+
render(
|
|
221
|
+
<WildcardComponent
|
|
222
|
+
name="customTool"
|
|
223
|
+
args={{ param1: "value1", param2: 42 }}
|
|
224
|
+
status={ToolCallStatus.Executing}
|
|
225
|
+
result={undefined}
|
|
226
|
+
/>,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
expect(screen.getByTestId("tool-name").textContent).toBe("customTool");
|
|
230
|
+
expect(screen.getByTestId("tool-name").textContent).not.toBe("*");
|
|
231
|
+
expect(screen.getByTestId("status").textContent).toBe("executing");
|
|
232
|
+
|
|
233
|
+
const argsText = screen.getByTestId("args").textContent || "";
|
|
234
|
+
expect(argsText).toContain("value1");
|
|
235
|
+
expect(argsText).toContain("42");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should handle complex nested schemas", () => {
|
|
239
|
+
const complexRender = defineToolCallRenderer({
|
|
240
|
+
name: "complex_tool",
|
|
241
|
+
args: z.object({
|
|
242
|
+
user: z.object({
|
|
243
|
+
id: z.number(),
|
|
244
|
+
name: z.string(),
|
|
245
|
+
email: z.string().email(),
|
|
246
|
+
}),
|
|
247
|
+
options: z.array(z.string()),
|
|
248
|
+
metadata: z.record(z.unknown()).optional(),
|
|
249
|
+
}),
|
|
250
|
+
render: ({ args, status }) => {
|
|
251
|
+
if (status === ToolCallStatus.Executing) {
|
|
252
|
+
return (
|
|
253
|
+
<div data-testid="complex">
|
|
254
|
+
<div data-testid="user-info">
|
|
255
|
+
User: {args.user.name} ({args.user.email})
|
|
256
|
+
</div>
|
|
257
|
+
<div data-testid="options">
|
|
258
|
+
Options: {args.options.join(", ")}
|
|
259
|
+
</div>
|
|
260
|
+
{args.metadata && (
|
|
261
|
+
<div data-testid="metadata">
|
|
262
|
+
Metadata keys: {Object.keys(args.metadata).join(", ")}
|
|
263
|
+
</div>
|
|
264
|
+
)}
|
|
265
|
+
</div>
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return <div>Processing...</div>;
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const ComplexComponent = complexRender.render as React.FC<any>;
|
|
273
|
+
render(
|
|
274
|
+
<ComplexComponent
|
|
275
|
+
name="complex_tool"
|
|
276
|
+
args={{
|
|
277
|
+
user: { id: 1, name: "John Doe", email: "john@example.com" },
|
|
278
|
+
options: ["option1", "option2", "option3"],
|
|
279
|
+
metadata: { key1: "value1", key2: "value2" },
|
|
280
|
+
}}
|
|
281
|
+
status={ToolCallStatus.Executing}
|
|
282
|
+
result={undefined}
|
|
283
|
+
/>,
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
expect(screen.getByTestId("user-info").textContent).toBe(
|
|
287
|
+
"User: John Doe (john@example.com)",
|
|
288
|
+
);
|
|
289
|
+
expect(screen.getByTestId("options").textContent).toBe(
|
|
290
|
+
"Options: option1, option2, option3",
|
|
291
|
+
);
|
|
292
|
+
expect(screen.getByTestId("metadata").textContent).toBe(
|
|
293
|
+
"Metadata keys: key1, key2",
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("should properly handle all status states in union", () => {
|
|
298
|
+
const unionTestRender = defineToolCallRenderer({
|
|
299
|
+
name: "union_test",
|
|
300
|
+
args: z.object({
|
|
301
|
+
value: z.string(),
|
|
302
|
+
}),
|
|
303
|
+
render: (props) => {
|
|
304
|
+
switch (props.status) {
|
|
305
|
+
case ToolCallStatus.InProgress:
|
|
306
|
+
return (
|
|
307
|
+
<div data-testid="in-progress">
|
|
308
|
+
In Progress: {props.args.value || "..."}
|
|
309
|
+
</div>
|
|
310
|
+
);
|
|
311
|
+
case ToolCallStatus.Executing:
|
|
312
|
+
return (
|
|
313
|
+
<div data-testid="executing">Executing: {props.args.value}</div>
|
|
314
|
+
);
|
|
315
|
+
case ToolCallStatus.Complete:
|
|
316
|
+
return (
|
|
317
|
+
<div data-testid="complete">
|
|
318
|
+
Complete: {props.args.value} = {props.result}
|
|
319
|
+
</div>
|
|
320
|
+
);
|
|
321
|
+
default:
|
|
322
|
+
return <div data-testid="unknown">Unknown status</div>;
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const UnionComponent = unionTestRender.render as React.FC<any>;
|
|
328
|
+
const { rerender } = render(
|
|
329
|
+
<UnionComponent
|
|
330
|
+
name="union_test"
|
|
331
|
+
args={{ value: "partial" }}
|
|
332
|
+
status={ToolCallStatus.InProgress}
|
|
333
|
+
result={undefined}
|
|
334
|
+
/>,
|
|
335
|
+
);
|
|
336
|
+
expect(screen.getByTestId("in-progress").textContent).toBe(
|
|
337
|
+
"In Progress: partial",
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
rerender(
|
|
341
|
+
<UnionComponent
|
|
342
|
+
name="union_test"
|
|
343
|
+
args={{ value: "test" }}
|
|
344
|
+
status={ToolCallStatus.Executing}
|
|
345
|
+
result={undefined}
|
|
346
|
+
/>,
|
|
347
|
+
);
|
|
348
|
+
expect(screen.getByTestId("executing").textContent).toBe(
|
|
349
|
+
"Executing: test",
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
rerender(
|
|
353
|
+
<UnionComponent
|
|
354
|
+
name="union_test"
|
|
355
|
+
args={{ value: "test" }}
|
|
356
|
+
status={ToolCallStatus.Complete}
|
|
357
|
+
result="success"
|
|
358
|
+
/>,
|
|
359
|
+
);
|
|
360
|
+
expect(screen.getByTestId("complete").textContent).toBe(
|
|
361
|
+
"Complete: test = success",
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("should support agentId parameter", () => {
|
|
366
|
+
const agentSpecificRender = defineToolCallRenderer({
|
|
367
|
+
name: "agent_tool",
|
|
368
|
+
args: z.object({ message: z.string() }),
|
|
369
|
+
agentId: "special-agent",
|
|
370
|
+
render: ({ args }) => <div data-testid="agent">{args.message}</div>,
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
expect(agentSpecificRender.agentId).toBe("special-agent");
|
|
374
|
+
expect(agentSpecificRender.name).toBe("agent_tool");
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("should work with wildcard and agentId", () => {
|
|
378
|
+
const agentWildcard = defineToolCallRenderer({
|
|
379
|
+
name: "*",
|
|
380
|
+
agentId: "fallback-agent",
|
|
381
|
+
render: ({ name }) => (
|
|
382
|
+
<div data-testid="wildcard-agent">Unknown: {name}</div>
|
|
383
|
+
),
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
expect(agentWildcard.agentId).toBe("fallback-agent");
|
|
387
|
+
expect(agentWildcard.name).toBe("*");
|
|
388
|
+
|
|
389
|
+
const Component = agentWildcard.render as React.FC<any>;
|
|
390
|
+
render(
|
|
391
|
+
<Component
|
|
392
|
+
name="unknownTool"
|
|
393
|
+
args={{ anything: "goes" }}
|
|
394
|
+
status={ToolCallStatus.Executing}
|
|
395
|
+
result={undefined}
|
|
396
|
+
/>,
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
expect(screen.getByTestId("wildcard-agent").textContent).toBe(
|
|
400
|
+
"Unknown: unknownTool",
|
|
401
|
+
);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
describe("Real-world use cases", () => {
|
|
406
|
+
it("should handle the user's original weather example without type errors", () => {
|
|
407
|
+
// This is the exact code the user reported was causing errors
|
|
408
|
+
const weatherRender = defineToolCallRenderer({
|
|
409
|
+
name: "get_weather",
|
|
410
|
+
args: z.object({
|
|
411
|
+
location: z.string(),
|
|
412
|
+
}),
|
|
413
|
+
render: ({ args }) => {
|
|
414
|
+
// No type casting needed - TypeScript infers correctly
|
|
415
|
+
return <div data-testid="weather">Weather: {args.location}</div>;
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
const Component = weatherRender.render as React.FC<any>;
|
|
420
|
+
render(
|
|
421
|
+
<Component
|
|
422
|
+
name="get_weather"
|
|
423
|
+
args={{ location: "San Francisco" }}
|
|
424
|
+
status={ToolCallStatus.Executing}
|
|
425
|
+
result={undefined}
|
|
426
|
+
/>,
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
expect(screen.getByTestId("weather").textContent).toBe(
|
|
430
|
+
"Weather: San Francisco",
|
|
431
|
+
);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it("should allow wildcard as fallback for undefined tools", () => {
|
|
435
|
+
const renders = [
|
|
436
|
+
defineToolCallRenderer({
|
|
437
|
+
name: "known_tool",
|
|
438
|
+
args: z.object({ id: z.number() }),
|
|
439
|
+
render: ({ args }) => (
|
|
440
|
+
<div data-testid="known">Known tool: {args.id}</div>
|
|
441
|
+
),
|
|
442
|
+
}),
|
|
443
|
+
defineToolCallRenderer({
|
|
444
|
+
name: "*",
|
|
445
|
+
render: ({ name, args }) => (
|
|
446
|
+
<div data-testid="fallback">
|
|
447
|
+
Fallback for {name}: {JSON.stringify(args)}
|
|
448
|
+
</div>
|
|
449
|
+
),
|
|
450
|
+
}),
|
|
451
|
+
];
|
|
452
|
+
|
|
453
|
+
// Test known tool
|
|
454
|
+
const KnownComponent = renders[0]!.render as React.FC<any>;
|
|
455
|
+
const { rerender } = render(
|
|
456
|
+
<KnownComponent
|
|
457
|
+
name="known_tool"
|
|
458
|
+
args={{ id: 123 }}
|
|
459
|
+
status={ToolCallStatus.Executing}
|
|
460
|
+
result={undefined}
|
|
461
|
+
/>,
|
|
462
|
+
);
|
|
463
|
+
expect(screen.getByTestId("known").textContent).toBe("Known tool: 123");
|
|
464
|
+
|
|
465
|
+
// Test wildcard fallback
|
|
466
|
+
const WildcardComponent = renders[1]!.render as React.FC<any>;
|
|
467
|
+
rerender(
|
|
468
|
+
<WildcardComponent
|
|
469
|
+
name="unknown_tool"
|
|
470
|
+
args={{ data: "test" }}
|
|
471
|
+
status={ToolCallStatus.Executing}
|
|
472
|
+
result={undefined}
|
|
473
|
+
/>,
|
|
474
|
+
);
|
|
475
|
+
expect(screen.getByTestId("fallback").textContent).toBe(
|
|
476
|
+
'Fallback for unknown_tool: {"data":"test"}',
|
|
477
|
+
);
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it("should handle optional fields correctly", () => {
|
|
481
|
+
const optionalRender = defineToolCallRenderer({
|
|
482
|
+
name: "optional_test",
|
|
483
|
+
args: z.object({
|
|
484
|
+
required: z.string(),
|
|
485
|
+
optional: z.string().optional(),
|
|
486
|
+
nullable: z.string().nullable(),
|
|
487
|
+
defaulted: z.string().default("default_value"),
|
|
488
|
+
}),
|
|
489
|
+
render: ({ args, status }) => {
|
|
490
|
+
if (status === ToolCallStatus.Executing) {
|
|
491
|
+
return (
|
|
492
|
+
<div data-testid="optional">
|
|
493
|
+
<div>Required: {args.required}</div>
|
|
494
|
+
<div>Optional: {args.optional || "not provided"}</div>
|
|
495
|
+
<div>Nullable: {args.nullable || "null"}</div>
|
|
496
|
+
<div>Defaulted: {args.defaulted}</div>
|
|
497
|
+
</div>
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
return <div>Loading...</div>;
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const Component = optionalRender.render as React.FC<any>;
|
|
505
|
+
render(
|
|
506
|
+
<Component
|
|
507
|
+
name="optional_test"
|
|
508
|
+
args={{
|
|
509
|
+
required: "test",
|
|
510
|
+
nullable: null,
|
|
511
|
+
defaulted: "custom",
|
|
512
|
+
}}
|
|
513
|
+
status={ToolCallStatus.Executing}
|
|
514
|
+
result={undefined}
|
|
515
|
+
/>,
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
const element = screen.getByTestId("optional");
|
|
519
|
+
expect(element.textContent).toContain("Required: test");
|
|
520
|
+
expect(element.textContent).toContain("Optional: not provided");
|
|
521
|
+
expect(element.textContent).toContain("Nullable: null");
|
|
522
|
+
expect(element.textContent).toContain("Defaulted: custom");
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { StandardSchemaV1, InferSchemaOutput } from "@copilotkit/shared";
|
|
4
|
+
import { ReactToolCallRenderer } from "./react-tool-call-renderer";
|
|
5
|
+
import { ToolCallStatus } from "@copilotkit/core";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Helper to define a type-safe tool call renderer entry.
|
|
9
|
+
* - Accepts a single object whose keys match ReactToolCallRenderer's fields: { name, args, render, agentId? }.
|
|
10
|
+
* - Derives `args` type from the provided schema (any Standard Schema V1 compatible library).
|
|
11
|
+
* - Ensures the render function param type exactly matches ReactToolCallRenderer<T>["render"]'s param.
|
|
12
|
+
* - For wildcard tools (name: "*"), args is optional and defaults to z.any()
|
|
13
|
+
*/
|
|
14
|
+
type RenderProps<T> =
|
|
15
|
+
| {
|
|
16
|
+
name: string;
|
|
17
|
+
args: Partial<T>;
|
|
18
|
+
status: ToolCallStatus.InProgress;
|
|
19
|
+
result: undefined;
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
name: string;
|
|
23
|
+
args: T;
|
|
24
|
+
status: ToolCallStatus.Executing;
|
|
25
|
+
result: undefined;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
name: string;
|
|
29
|
+
args: T;
|
|
30
|
+
status: ToolCallStatus.Complete;
|
|
31
|
+
result: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Overload for wildcard tools without args
|
|
35
|
+
export function defineToolCallRenderer(def: {
|
|
36
|
+
name: "*";
|
|
37
|
+
render: (props: RenderProps<any>) => React.ReactElement;
|
|
38
|
+
agentId?: string;
|
|
39
|
+
}): ReactToolCallRenderer<any>;
|
|
40
|
+
|
|
41
|
+
// Overload for regular tools with args
|
|
42
|
+
export function defineToolCallRenderer<S extends StandardSchemaV1>(def: {
|
|
43
|
+
name: string;
|
|
44
|
+
args: S;
|
|
45
|
+
render: (props: RenderProps<InferSchemaOutput<S>>) => React.ReactElement;
|
|
46
|
+
agentId?: string;
|
|
47
|
+
}): ReactToolCallRenderer<InferSchemaOutput<S>>;
|
|
48
|
+
|
|
49
|
+
// Implementation
|
|
50
|
+
export function defineToolCallRenderer<S extends StandardSchemaV1>(def: {
|
|
51
|
+
name: string;
|
|
52
|
+
args?: S;
|
|
53
|
+
render: (props: any) => React.ReactElement;
|
|
54
|
+
agentId?: string;
|
|
55
|
+
}): ReactToolCallRenderer<any> {
|
|
56
|
+
// For wildcard tools, default to z.any() if no args provided
|
|
57
|
+
const argsSchema = def.name === "*" && !def.args ? z.any() : def.args;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
name: def.name,
|
|
61
|
+
args: argsSchema,
|
|
62
|
+
render: def.render as React.ComponentType<any>,
|
|
63
|
+
...(def.agentId ? { agentId: def.agentId } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FrontendTool } from "@copilotkit/core";
|
|
2
|
+
import { ReactToolCallRenderer } from "./react-tool-call-renderer";
|
|
3
|
+
|
|
4
|
+
export type ReactFrontendTool<
|
|
5
|
+
T extends Record<string, unknown> = Record<string, unknown>,
|
|
6
|
+
> = FrontendTool<T> & {
|
|
7
|
+
render?: ReactToolCallRenderer<T>["render"];
|
|
8
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { FrontendTool, ToolCallStatus } from "@copilotkit/core";
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
export type ReactHumanInTheLoop<
|
|
5
|
+
T extends Record<string, unknown> = Record<string, unknown>,
|
|
6
|
+
> = Omit<FrontendTool<T>, "handler"> & {
|
|
7
|
+
render: React.ComponentType<
|
|
8
|
+
| {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
args: Partial<T>;
|
|
12
|
+
status: ToolCallStatus.InProgress;
|
|
13
|
+
result: undefined;
|
|
14
|
+
respond: undefined;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
args: T;
|
|
20
|
+
status: ToolCallStatus.Executing;
|
|
21
|
+
result: undefined;
|
|
22
|
+
respond: (result: unknown) => Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
name: string;
|
|
26
|
+
description: string;
|
|
27
|
+
args: T;
|
|
28
|
+
status: ToolCallStatus.Complete;
|
|
29
|
+
result: string;
|
|
30
|
+
respond: undefined;
|
|
31
|
+
}
|
|
32
|
+
>;
|
|
33
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./react-tool-call-renderer";
|
|
2
|
+
export * from "./react-activity-message-renderer";
|
|
3
|
+
export * from "./react-custom-message-renderer";
|
|
4
|
+
export * from "./frontend-tool";
|
|
5
|
+
export * from "./human-in-the-loop";
|
|
6
|
+
export * from "./defineToolCallRenderer";
|
|
7
|
+
export * from "./interrupt";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface InterruptEvent<TValue = unknown> {
|
|
2
|
+
name: string;
|
|
3
|
+
value: TValue;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface InterruptHandlerProps<TValue = unknown> {
|
|
7
|
+
event: InterruptEvent<TValue>;
|
|
8
|
+
resolve: (response: unknown) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface InterruptRenderProps<TValue = unknown, TResult = unknown> {
|
|
12
|
+
event: InterruptEvent<TValue>;
|
|
13
|
+
result: TResult;
|
|
14
|
+
resolve: (response: unknown) => void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ActivityMessage } from "@ag-ui/core";
|
|
2
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
3
|
+
import type { StandardSchemaV1 } from "@copilotkit/shared";
|
|
4
|
+
|
|
5
|
+
export interface ReactActivityMessageRenderer<TActivityContent> {
|
|
6
|
+
/**
|
|
7
|
+
* Activity type to match when rendering. Use "*" as a wildcard renderer.
|
|
8
|
+
*/
|
|
9
|
+
activityType: string;
|
|
10
|
+
/**
|
|
11
|
+
* Optional agent ID to scope the renderer to a particular agent.
|
|
12
|
+
*/
|
|
13
|
+
agentId?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Schema describing the activity content payload.
|
|
16
|
+
*/
|
|
17
|
+
content: StandardSchemaV1<any, TActivityContent>;
|
|
18
|
+
/**
|
|
19
|
+
* React component invoked to render the activity message.
|
|
20
|
+
*/
|
|
21
|
+
render: React.ComponentType<{
|
|
22
|
+
activityType: string;
|
|
23
|
+
content: TActivityContent;
|
|
24
|
+
message: ActivityMessage;
|
|
25
|
+
agent: AbstractAgent | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
}
|