@copilotkit/react-native 2.0.0-rc.14 → 2.0.0-rc.16
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/dist/CopilotKitProvider-DbI3aQam.cjs.map +1 -1
- package/dist/CopilotKitProvider-DlP73sLt.mjs.map +1 -1
- package/dist/{CopilotKitProvider-BOaKT5J9.d.cts → CopilotKitProvider-lfVMYSby.d.cts} +3 -2
- package/dist/{CopilotKitProvider-DcRVO6WN.d.mts → CopilotKitProvider-mVDI6DLx.d.mts} +3 -2
- package/dist/components/index.cjs +10 -1
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.cts +1 -1
- package/dist/components/index.d.mts +1 -1
- package/dist/components/index.mjs +10 -1
- package/dist/components/index.mjs.map +1 -1
- package/dist/headless.d.cts +1 -1
- package/dist/headless.d.mts +1 -1
- package/dist/index.cjs +19 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -7
- package/dist/index.d.mts +21 -7
- package/dist/index.mjs +20 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
- package/src/CopilotChat.tsx +35 -8
- package/src/CopilotKitProvider.tsx +2 -1
- package/src/CopilotPopup.tsx +2 -2
- package/src/CopilotSidebar.tsx +4 -1
- package/src/__mocks__/test-copilotkit.tsx +9 -3
- package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
- package/src/components/CopilotChat.tsx +22 -1
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
- package/src/components/__tests__/CopilotChat.test.tsx +5 -1
- package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
- package/src/components/messages/AssistantMessage.tsx +3 -2
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks inside the headless `<CopilotChat agentId>` must use that chat's agent,
|
|
3
|
+
* as they do inside the web CopilotChat. Runs on a REAL `CopilotKitCoreReact`
|
|
4
|
+
* (via `TestCopilotKit`) and the real react-core hooks: mocking
|
|
5
|
+
* `@copilotkit/react-core/headless` (as the sibling chat tests do) would
|
|
6
|
+
* stub the chat configuration under test.
|
|
7
|
+
*/
|
|
8
|
+
import React from "react";
|
|
9
|
+
import { render } from "@testing-library/react";
|
|
10
|
+
import { describe, it, expect } from "vitest";
|
|
11
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
12
|
+
import {
|
|
13
|
+
useAgent,
|
|
14
|
+
useCopilotChatConfiguration,
|
|
15
|
+
} from "@copilotkit/react-core/headless";
|
|
16
|
+
import { CopilotChat, useCopilotChatContext } from "../CopilotChat";
|
|
17
|
+
import { SeededAgent, TestCopilotKit } from "../__mocks__/test-copilotkit";
|
|
18
|
+
|
|
19
|
+
function renderInChat(chat: React.ReactElement) {
|
|
20
|
+
const agents = { x: new SeededAgent() };
|
|
21
|
+
render(
|
|
22
|
+
<TestCopilotKit messages={[]} agents={agents}>
|
|
23
|
+
{chat}
|
|
24
|
+
</TestCopilotKit>,
|
|
25
|
+
);
|
|
26
|
+
return agents;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("headless CopilotChat chat configuration", () => {
|
|
30
|
+
it("gives hooks inside <CopilotChat agentId> that agent", () => {
|
|
31
|
+
let seen: AbstractAgent | undefined;
|
|
32
|
+
function Probe() {
|
|
33
|
+
seen = useAgent().agent;
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const agents = renderInChat(
|
|
38
|
+
<CopilotChat agentId="x">
|
|
39
|
+
<Probe />
|
|
40
|
+
</CopilotChat>,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect(seen).toBe(agents.x);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("gives hooks inside <CopilotChat threadId> that thread", () => {
|
|
47
|
+
let seenThreadId: string | undefined;
|
|
48
|
+
function Probe() {
|
|
49
|
+
seenThreadId = useCopilotChatConfiguration()?.threadId;
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
renderInChat(
|
|
54
|
+
<CopilotChat agentId="x" threadId="thread-1">
|
|
55
|
+
<Probe />
|
|
56
|
+
</CopilotChat>,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(seenThreadId).toBe("thread-1");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("gives a <CopilotChat> without agentId the agent of the chat around it", () => {
|
|
63
|
+
let seenByChat: AbstractAgent | undefined;
|
|
64
|
+
let seenByHook: AbstractAgent | undefined;
|
|
65
|
+
function Probe() {
|
|
66
|
+
seenByChat = useCopilotChatContext().agent;
|
|
67
|
+
seenByHook = useAgent().agent;
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const agents = renderInChat(
|
|
72
|
+
<CopilotChat agentId="x">
|
|
73
|
+
<CopilotChat>
|
|
74
|
+
<Probe />
|
|
75
|
+
</CopilotChat>
|
|
76
|
+
</CopilotChat>,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(seenByChat).toBe(agents.x);
|
|
80
|
+
expect(seenByHook).toBe(agents.x);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
} from "react";
|
|
2
8
|
import {
|
|
3
9
|
FlatList,
|
|
4
10
|
KeyboardAvoidingView,
|
|
@@ -286,6 +292,20 @@ export function CopilotChat({
|
|
|
286
292
|
|
|
287
293
|
const renderToolCall = useRenderToolCall();
|
|
288
294
|
|
|
295
|
+
// useInterrupt publishes its element to core unless renderInChat is false.
|
|
296
|
+
// Draw it after the last message, as the web message view does.
|
|
297
|
+
const [interruptElement, setInterruptElement] =
|
|
298
|
+
useState<React.ReactElement | null>(null);
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
setInterruptElement(copilotkit.interruptElement);
|
|
301
|
+
const subscription = copilotkit.subscribe({
|
|
302
|
+
onInterruptElementChanged: ({ interruptElement: element }) => {
|
|
303
|
+
setInterruptElement(element);
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
return () => subscription.unsubscribe();
|
|
307
|
+
}, [copilotkit]);
|
|
308
|
+
|
|
289
309
|
// Recomputed every render — cheap, and the only honest dependency for the
|
|
290
310
|
// message-derived memos below. See `messagesFingerprint` for why the array
|
|
291
311
|
// reference cannot be trusted as one.
|
|
@@ -522,6 +542,7 @@ export function CopilotChat({
|
|
|
522
542
|
contentContainerStyle={[styles.messageList, messageContainerStyle]}
|
|
523
543
|
onContentSizeChange={handleContentSizeChange}
|
|
524
544
|
ListEmptyComponent={emptyComponent}
|
|
545
|
+
ListFooterComponent={interruptElement}
|
|
525
546
|
/>
|
|
526
547
|
|
|
527
548
|
{error && (
|
|
@@ -29,7 +29,11 @@ vi.mock("@copilotkit/react-core/headless", () => ({
|
|
|
29
29
|
|
|
30
30
|
vi.mock("@copilotkit/react-core/context", () => ({
|
|
31
31
|
useCopilotKit: vi.fn(() => ({
|
|
32
|
-
copilotkit: {
|
|
32
|
+
copilotkit: {
|
|
33
|
+
runAgent: hoisted.mockRunAgent,
|
|
34
|
+
interruptElement: null,
|
|
35
|
+
subscribe: () => ({ unsubscribe: () => {} }),
|
|
36
|
+
},
|
|
33
37
|
executingToolCallIds: hoisted.mockExecutingToolCallIds,
|
|
34
38
|
})),
|
|
35
39
|
}));
|
|
@@ -28,7 +28,11 @@ vi.mock("@copilotkit/react-core/headless", () => ({
|
|
|
28
28
|
|
|
29
29
|
vi.mock("@copilotkit/react-core/context", () => ({
|
|
30
30
|
useCopilotKit: vi.fn(() => ({
|
|
31
|
-
copilotkit: {
|
|
31
|
+
copilotkit: {
|
|
32
|
+
runAgent: hoisted.mockRunAgent,
|
|
33
|
+
interruptElement: null,
|
|
34
|
+
subscribe: () => ({ unsubscribe: () => {} }),
|
|
35
|
+
},
|
|
32
36
|
executingToolCallIds: new Set<string>(),
|
|
33
37
|
})),
|
|
34
38
|
}));
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {
|
|
3
|
+
act,
|
|
4
|
+
fireEvent,
|
|
5
|
+
render,
|
|
6
|
+
screen,
|
|
7
|
+
waitFor,
|
|
8
|
+
} from "@testing-library/react";
|
|
9
|
+
import { describe, expect, it, vi } from "vitest";
|
|
10
|
+
import { EventType, HttpAgent } from "@ag-ui/client";
|
|
11
|
+
import type { RunAgentInput } from "@ag-ui/client";
|
|
12
|
+
import { useCopilotKit } from "@copilotkit/react-core/context";
|
|
13
|
+
import type { CopilotKitCoreReact } from "@copilotkit/react-core/context";
|
|
14
|
+
|
|
15
|
+
// The shared react-native stub passes FlatList props through unused. This one
|
|
16
|
+
// renders the rows and then the footer, which is where CopilotChat draws the
|
|
17
|
+
// interrupt element.
|
|
18
|
+
vi.mock("react-native", async () => {
|
|
19
|
+
const actual = await vi.importActual<any>("../../__mocks__/react-native");
|
|
20
|
+
return {
|
|
21
|
+
...actual,
|
|
22
|
+
FlatList: ({
|
|
23
|
+
data,
|
|
24
|
+
renderItem,
|
|
25
|
+
keyExtractor,
|
|
26
|
+
ListEmptyComponent,
|
|
27
|
+
ListFooterComponent,
|
|
28
|
+
}: any) =>
|
|
29
|
+
React.createElement(
|
|
30
|
+
"div",
|
|
31
|
+
{ "data-testid": "flatlist" },
|
|
32
|
+
data?.length
|
|
33
|
+
? data.map((item: any, index: number) =>
|
|
34
|
+
React.createElement(
|
|
35
|
+
"div",
|
|
36
|
+
{ key: keyExtractor?.(item) ?? index },
|
|
37
|
+
renderItem({ item, index, separators: {} }),
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
: ListEmptyComponent,
|
|
41
|
+
ListFooterComponent,
|
|
42
|
+
),
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
import { CopilotChat } from "../CopilotChat";
|
|
47
|
+
import { useInterrupt } from "../../headless";
|
|
48
|
+
import { TestCopilotKit } from "../../__mocks__/test-copilotkit";
|
|
49
|
+
|
|
50
|
+
function CoreProbe({
|
|
51
|
+
coreRef,
|
|
52
|
+
}: {
|
|
53
|
+
coreRef: { current: CopilotKitCoreReact | null };
|
|
54
|
+
}) {
|
|
55
|
+
coreRef.current = useCopilotKit().copilotkit;
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* An agent whose first run stops on an approval interrupt and whose later runs
|
|
61
|
+
* finish normally. It streams real AG-UI events over SSE, so core and
|
|
62
|
+
* useInterrupt see the same run lifecycle as in an app.
|
|
63
|
+
*/
|
|
64
|
+
function createInterruptingAgent() {
|
|
65
|
+
const inputs: RunAgentInput[] = [];
|
|
66
|
+
const agent = new HttpAgent({
|
|
67
|
+
url: "http://localhost/agent",
|
|
68
|
+
fetch: async (_url, init) => {
|
|
69
|
+
const input = JSON.parse(String(init.body)) as RunAgentInput;
|
|
70
|
+
inputs.push(input);
|
|
71
|
+
const { threadId, runId } = input;
|
|
72
|
+
const events = [
|
|
73
|
+
{ type: EventType.RUN_STARTED, threadId, runId },
|
|
74
|
+
{
|
|
75
|
+
type: EventType.RUN_FINISHED,
|
|
76
|
+
threadId,
|
|
77
|
+
runId,
|
|
78
|
+
...(inputs.length === 1
|
|
79
|
+
? {
|
|
80
|
+
outcome: {
|
|
81
|
+
type: "interrupt",
|
|
82
|
+
interrupts: [
|
|
83
|
+
{ id: "approve", reason: "approval", message: "Deploy?" },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
: {}),
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
return new Response(
|
|
91
|
+
events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""),
|
|
92
|
+
{ headers: { "Content-Type": "text/event-stream" } },
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return { agent, inputs };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Uses useInterrupt with the default renderInChat: true. */
|
|
100
|
+
function Approval() {
|
|
101
|
+
useInterrupt({
|
|
102
|
+
render: ({ interrupt, resolve }) => (
|
|
103
|
+
<button
|
|
104
|
+
data-testid="interrupt-ui"
|
|
105
|
+
onClick={() => void resolve({ approved: true })}
|
|
106
|
+
>
|
|
107
|
+
{interrupt?.message}
|
|
108
|
+
</button>
|
|
109
|
+
),
|
|
110
|
+
});
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const messages = [
|
|
115
|
+
{ id: "u1", role: "user", content: "Deploy the app" },
|
|
116
|
+
{ id: "a1", role: "assistant", content: "I need your approval first." },
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
function Chat({
|
|
120
|
+
agent,
|
|
121
|
+
coreRef,
|
|
122
|
+
withApproval = true,
|
|
123
|
+
}: {
|
|
124
|
+
agent: HttpAgent;
|
|
125
|
+
coreRef: { current: CopilotKitCoreReact | null };
|
|
126
|
+
withApproval?: boolean;
|
|
127
|
+
}) {
|
|
128
|
+
return (
|
|
129
|
+
<TestCopilotKit messages={messages} agent={agent}>
|
|
130
|
+
<CoreProbe coreRef={coreRef} />
|
|
131
|
+
{withApproval && <Approval />}
|
|
132
|
+
<CopilotChat />
|
|
133
|
+
</TestCopilotKit>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Mounts the chat and runs the agent until it stops on the interrupt. */
|
|
138
|
+
async function renderInterruptedChat() {
|
|
139
|
+
const { agent, inputs } = createInterruptingAgent();
|
|
140
|
+
const coreRef: { current: CopilotKitCoreReact | null } = { current: null };
|
|
141
|
+
const view = render(<Chat agent={agent} coreRef={coreRef} />);
|
|
142
|
+
expect(screen.queryByTestId("interrupt-ui")).toBeNull();
|
|
143
|
+
|
|
144
|
+
await act(async () => {
|
|
145
|
+
await coreRef.current!.runAgent({ agent });
|
|
146
|
+
});
|
|
147
|
+
await waitFor(() => expect(screen.getByTestId("interrupt-ui")).toBeTruthy());
|
|
148
|
+
return { ...view, agent, inputs, coreRef };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
describe("CopilotChat interrupt rendering", () => {
|
|
152
|
+
it("draws the interrupt element that useInterrupt publishes to core", () => {
|
|
153
|
+
const coreRef: { current: CopilotKitCoreReact | null } = { current: null };
|
|
154
|
+
render(
|
|
155
|
+
<TestCopilotKit messages={[]}>
|
|
156
|
+
<CoreProbe coreRef={coreRef} />
|
|
157
|
+
<CopilotChat />
|
|
158
|
+
</TestCopilotKit>,
|
|
159
|
+
);
|
|
160
|
+
expect(screen.queryByTestId("interrupt-ui")).toBeNull();
|
|
161
|
+
|
|
162
|
+
act(() => {
|
|
163
|
+
coreRef.current!.setInterruptElement(
|
|
164
|
+
<div data-testid="interrupt-ui">Approve?</div>,
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
expect(screen.getByTestId("interrupt-ui").textContent).toBe("Approve?");
|
|
168
|
+
|
|
169
|
+
act(() => {
|
|
170
|
+
coreRef.current!.setInterruptElement(null);
|
|
171
|
+
});
|
|
172
|
+
expect(screen.queryByTestId("interrupt-ui")).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("draws a useInterrupt element after the last message and removes it once resolved", async () => {
|
|
176
|
+
const { inputs } = await renderInterruptedChat();
|
|
177
|
+
|
|
178
|
+
const interrupt = screen.getByTestId("interrupt-ui");
|
|
179
|
+
expect(interrupt.textContent).toBe("Deploy?");
|
|
180
|
+
const lastMessage = screen.getByText("I need your approval first.");
|
|
181
|
+
expect(
|
|
182
|
+
lastMessage.compareDocumentPosition(interrupt) &
|
|
183
|
+
Node.DOCUMENT_POSITION_FOLLOWING,
|
|
184
|
+
).toBeTruthy();
|
|
185
|
+
|
|
186
|
+
await act(async () => {
|
|
187
|
+
fireEvent.click(interrupt);
|
|
188
|
+
});
|
|
189
|
+
await waitFor(() => expect(inputs).toHaveLength(2));
|
|
190
|
+
expect(inputs[1]!.resume).toEqual([
|
|
191
|
+
{
|
|
192
|
+
interruptId: "approve",
|
|
193
|
+
status: "resolved",
|
|
194
|
+
payload: { approved: true },
|
|
195
|
+
},
|
|
196
|
+
]);
|
|
197
|
+
await waitFor(() =>
|
|
198
|
+
expect(screen.queryByTestId("interrupt-ui")).toBeNull(),
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("removes the useInterrupt element when its component unmounts", async () => {
|
|
203
|
+
const { agent, coreRef, rerender } = await renderInterruptedChat();
|
|
204
|
+
|
|
205
|
+
rerender(<Chat agent={agent} coreRef={coreRef} withApproval={false} />);
|
|
206
|
+
expect(screen.queryByTestId("interrupt-ui")).toBeNull();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { View, Text, StyleSheet
|
|
2
|
+
import { View, Text, StyleSheet } from "react-native";
|
|
3
|
+
import type { ViewStyle } from "react-native";
|
|
3
4
|
import { CopilotMarkdown } from "../Markdown";
|
|
4
5
|
import {
|
|
5
6
|
useMarkdownRenderer,
|
|
@@ -19,7 +20,7 @@ const TIMESTAMP_COLOR = "#999999";
|
|
|
19
20
|
export interface AssistantMessageProps {
|
|
20
21
|
/** Markdown content to render inside the bubble */
|
|
21
22
|
content: string;
|
|
22
|
-
/** When true, shows a typing indicator
|
|
23
|
+
/** When true, shows a typing indicator below any content */
|
|
23
24
|
isLoading?: boolean;
|
|
24
25
|
/** Optional timestamp displayed below the bubble */
|
|
25
26
|
timestamp?: Date;
|