@copilotkit/react-native 2.0.0-rc.4 → 2.0.0-rc.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +334 -2
- package/USAGE.md +382 -21
- package/dist/CopilotKitProvider-Cbfrm_7u.d.mts +65 -0
- package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-CvJYytjn.cjs} +56 -42
- package/dist/CopilotKitProvider-CvJYytjn.cjs.map +1 -0
- package/dist/CopilotKitProvider-Dv3ISMKf.d.cts +65 -0
- package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-oTdmylSY.mjs} +55 -42
- package/dist/CopilotKitProvider-oTdmylSY.mjs.map +1 -0
- package/dist/{Markdown-C_9Re36O.cjs → Markdown-DLopAh5W.cjs} +10 -10
- package/dist/Markdown-DLopAh5W.cjs.map +1 -0
- package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-Du-b_Qpr.d.mts} +3 -3
- package/dist/{Markdown-1ips7myr.d.cts → Markdown-UXZdDjzs.d.cts} +3 -3
- package/dist/{Markdown-C6AsLHmd.mjs → Markdown-VxtPJ_yh.mjs} +4 -4
- package/dist/Markdown-VxtPJ_yh.mjs.map +1 -0
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-EobUsERg.mjs} +1 -1
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-EobUsERg.mjs.map} +1 -1
- package/dist/MarkdownRendererContext-cDEHvbw9.cjs +40 -0
- package/dist/{MarkdownRendererContext-BRNe808d.cjs.map → MarkdownRendererContext-cDEHvbw9.cjs.map} +1 -1
- package/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/components/index.cjs +78 -175
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +76 -174
- package/dist/components/index.mjs.map +1 -1
- package/dist/generative-ui/index.cjs +612 -0
- package/dist/generative-ui/index.cjs.map +1 -0
- package/dist/generative-ui/index.d.cts +341 -0
- package/dist/generative-ui/index.d.mts +341 -0
- package/dist/generative-ui/index.mjs +549 -0
- package/dist/generative-ui/index.mjs.map +1 -0
- package/dist/headless.cjs +2 -2
- package/dist/headless.d.cts +2 -2
- package/dist/headless.d.mts +2 -2
- package/dist/headless.mjs +1 -1
- package/dist/index.cjs +131 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -18
- package/dist/index.d.mts +36 -18
- package/dist/index.mjs +130 -161
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +115 -5
- package/dist/polyfills/encoding.cjs.map +1 -1
- package/dist/polyfills/encoding.d.cts +31 -1
- package/dist/polyfills/encoding.d.mts +31 -1
- package/dist/polyfills/encoding.mjs +113 -6
- package/dist/polyfills/encoding.mjs.map +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/dist/polyfills.cjs +218 -2
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +217 -1
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +51 -21
- package/project.json +6 -1
- package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
- package/scripts/verify-polyfill-barrel.mjs +158 -11
- package/src/CopilotChat.tsx +114 -93
- package/src/CopilotKitProvider.tsx +87 -130
- package/src/CopilotPopup.tsx +2 -2
- package/src/CopilotSidebar.tsx +7 -5
- package/src/__mocks__/react-native.ts +31 -0
- package/src/__mocks__/test-copilotkit.tsx +9 -3
- package/src/__tests__/attachments-integration.test.tsx +15 -2
- package/src/__tests__/copilot-chat-agentid.test.tsx +15 -2
- package/src/__tests__/copilot-chat-attachments.test.tsx +15 -2
- package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
- package/src/__tests__/copilot-chat-threads.test.tsx +232 -0
- package/src/__tests__/copilot-popup.test.tsx +15 -2
- package/src/__tests__/copilot-sidebar.test.tsx +15 -2
- package/src/__tests__/copilotkit-provider.test.tsx +68 -73
- package/src/__tests__/generative-ui-harness.tsx +243 -0
- package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
- package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
- package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
- package/src/__tests__/generative-ui.test.tsx +1000 -0
- package/src/__tests__/headless-entry-surface.test.ts +979 -60
- package/src/__tests__/headless-integration.test.tsx +11 -1
- package/src/__tests__/use-attachments.test.ts +27 -2
- package/src/components/CopilotChat.tsx +106 -233
- package/src/components/CopilotModal.tsx +1 -1
- package/src/components/Markdown.tsx +4 -4
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
- package/src/components/__tests__/CopilotChat.test.tsx +12 -4
- package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
- package/src/components/__tests__/Markdown.code-alias.test.tsx +1 -1
- package/src/components/index.ts +6 -0
- package/src/components/messages/AssistantMessage.tsx +3 -3
- package/src/components/messages/TypingIndicator.tsx +4 -4
- package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
- package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
- package/src/generative-ui/GenerativeUISurface.tsx +303 -0
- package/src/generative-ui/host.tsx +138 -0
- package/src/generative-ui/index.ts +93 -0
- package/src/generative-ui/internal.ts +55 -0
- package/src/hooks/use-attachments.ts +95 -139
- package/src/index.ts +8 -3
- package/src/polyfills/__tests__/encoding-utf8.test.ts +112 -0
- package/src/polyfills/encoding.ts +160 -4
- package/src/types.d.ts +0 -13
- package/tsdown.config.ts +1 -0
- package/dist/CopilotKitProvider-BWjzCdHN.d.mts +0 -121
- package/dist/CopilotKitProvider-Btm10msB.d.cts +0 -121
- package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +0 -1
- package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +0 -1
- package/dist/Markdown-C6AsLHmd.mjs.map +0 -1
- package/dist/Markdown-C_9Re36O.cjs.map +0 -1
- package/dist/MarkdownRendererContext-BRNe808d.cjs +0 -72
- package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
- package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
- package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
- package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* would stub the very thing under test. Driving a real core exercises the
|
|
10
10
|
* production path end-to-end.
|
|
11
11
|
*
|
|
12
|
-
* The core and its
|
|
12
|
+
* The core and its agents are created ONCE (via refs) and kept
|
|
13
13
|
* stable across re-renders. That stability is load-bearing: `useRenderTool`
|
|
14
14
|
* registers its renderer into core and deliberately does NOT remove it on
|
|
15
15
|
* unmount, so a renderer registered on one render must survive a later
|
|
@@ -38,7 +38,7 @@ import type { CopilotKitContextValue } from "@copilotkit/react-core/context";
|
|
|
38
38
|
* bundler cannot resolve a bare `rxjs` import. A throw-only body is assignable
|
|
39
39
|
* to any declared return type, so no Observable value is constructed.
|
|
40
40
|
*/
|
|
41
|
-
class SeededAgent extends AbstractAgent {
|
|
41
|
+
export class SeededAgent extends AbstractAgent {
|
|
42
42
|
run(): ReturnType<AbstractAgent["run"]> {
|
|
43
43
|
throw new Error("SeededAgent.run() is not used in tests");
|
|
44
44
|
}
|
|
@@ -83,6 +83,11 @@ export interface TestCopilotKitProps {
|
|
|
83
83
|
* instance, and therefore the registered renderers, must stay stable).
|
|
84
84
|
*/
|
|
85
85
|
agent?: AbstractAgent;
|
|
86
|
+
/**
|
|
87
|
+
* More agents to register next to "default", keyed by agent id, for tests
|
|
88
|
+
* that pick a non-default agent. Captured on first render like `agent`.
|
|
89
|
+
*/
|
|
90
|
+
agents?: Record<string, AbstractAgent>;
|
|
86
91
|
children: React.ReactNode;
|
|
87
92
|
}
|
|
88
93
|
|
|
@@ -91,6 +96,7 @@ export function TestCopilotKit({
|
|
|
91
96
|
executingToolCallIds = EMPTY_SET,
|
|
92
97
|
agentRef: publishAgentRef,
|
|
93
98
|
agent,
|
|
99
|
+
agents,
|
|
94
100
|
children,
|
|
95
101
|
}: TestCopilotKitProps) {
|
|
96
102
|
const agentRef = useRef<AbstractAgent | null>(null);
|
|
@@ -102,7 +108,7 @@ export function TestCopilotKit({
|
|
|
102
108
|
const copilotkitRef = useRef<CopilotKitCoreReact | null>(null);
|
|
103
109
|
if (copilotkitRef.current === null) {
|
|
104
110
|
copilotkitRef.current = new CopilotKitCoreReact({
|
|
105
|
-
agents__unsafe_dev_only: { default: agentRef.current },
|
|
111
|
+
agents__unsafe_dev_only: { ...agents, default: agentRef.current },
|
|
106
112
|
});
|
|
107
113
|
}
|
|
108
114
|
|
|
@@ -62,6 +62,12 @@ function createMockCore() {
|
|
|
62
62
|
setMessageFilter: vi.fn(),
|
|
63
63
|
setDebug: vi.fn(),
|
|
64
64
|
setDefaultThrottleMs: vi.fn(),
|
|
65
|
+
ɵupdateConfig: vi.fn(),
|
|
66
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
67
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
68
|
+
setErrorHandler: vi.fn(),
|
|
69
|
+
connect: vi.fn(),
|
|
70
|
+
dispose: vi.fn(),
|
|
65
71
|
getAgent: vi.fn(() => undefined),
|
|
66
72
|
runAgent: (...args: any[]) => mockRunAgent(...args),
|
|
67
73
|
runtimeUrl: "https://api.test",
|
|
@@ -82,13 +88,17 @@ function createMockCore() {
|
|
|
82
88
|
|
|
83
89
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
84
90
|
|
|
85
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
91
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
92
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
93
|
+
const actual =
|
|
94
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
86
95
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
87
96
|
hoisted.MockCoreConstructor(...args);
|
|
88
97
|
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
89
98
|
if (instance) Object.assign(this, instance);
|
|
90
99
|
}
|
|
91
100
|
return {
|
|
101
|
+
...actual,
|
|
92
102
|
CopilotKitCoreReact,
|
|
93
103
|
useAgent: () => {
|
|
94
104
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -147,7 +157,10 @@ vi.mock("@copilotkit/react-core/context", () => {
|
|
|
147
157
|
};
|
|
148
158
|
});
|
|
149
159
|
|
|
150
|
-
vi.mock("@copilotkit/shared", () => ({
|
|
160
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
161
|
+
// The real module first: the provider evaluates @copilotkit/core, which
|
|
162
|
+
// needs shared's value exports.
|
|
163
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
151
164
|
DEFAULT_AGENT_ID: "default",
|
|
152
165
|
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
153
166
|
getModalityFromMimeType: (mimeType: string) => {
|
|
@@ -33,6 +33,12 @@ function createMockCore() {
|
|
|
33
33
|
setMessageFilter: vi.fn(),
|
|
34
34
|
setDebug: vi.fn(),
|
|
35
35
|
setDefaultThrottleMs: vi.fn(),
|
|
36
|
+
ɵupdateConfig: vi.fn(),
|
|
37
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
38
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
39
|
+
setErrorHandler: vi.fn(),
|
|
40
|
+
connect: vi.fn(),
|
|
41
|
+
dispose: vi.fn(),
|
|
36
42
|
getAgent: vi.fn(() => undefined),
|
|
37
43
|
runtimeUrl: "https://api.test",
|
|
38
44
|
runtimeTransport: "auto",
|
|
@@ -52,13 +58,17 @@ function createMockCore() {
|
|
|
52
58
|
|
|
53
59
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
54
60
|
|
|
55
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
61
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
62
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
63
|
+
const actual =
|
|
64
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
56
65
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
57
66
|
hoisted.MockCoreConstructor(...args);
|
|
58
67
|
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
59
68
|
if (instance) Object.assign(this, instance);
|
|
60
69
|
}
|
|
61
70
|
return {
|
|
71
|
+
...actual,
|
|
62
72
|
CopilotKitCoreReact,
|
|
63
73
|
useAgent: (props: any) => {
|
|
64
74
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -118,7 +128,10 @@ vi.mock("@copilotkit/react-core/context", () => {
|
|
|
118
128
|
};
|
|
119
129
|
});
|
|
120
130
|
|
|
121
|
-
vi.mock("@copilotkit/shared", () => ({
|
|
131
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
132
|
+
// The real module first: the provider evaluates @copilotkit/core, which
|
|
133
|
+
// needs shared's value exports.
|
|
134
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
122
135
|
DEFAULT_AGENT_ID: "default",
|
|
123
136
|
createLicenseContextValue: () => ({
|
|
124
137
|
status: null,
|
|
@@ -60,6 +60,12 @@ function createMockCore() {
|
|
|
60
60
|
setMessageFilter: vi.fn(),
|
|
61
61
|
setDebug: vi.fn(),
|
|
62
62
|
setDefaultThrottleMs: vi.fn(),
|
|
63
|
+
ɵupdateConfig: vi.fn(),
|
|
64
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
65
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
66
|
+
setErrorHandler: vi.fn(),
|
|
67
|
+
connect: vi.fn(),
|
|
68
|
+
dispose: vi.fn(),
|
|
63
69
|
getAgent: vi.fn(() => undefined),
|
|
64
70
|
runAgent: mockRunAgent,
|
|
65
71
|
runtimeUrl: "https://api.test",
|
|
@@ -80,13 +86,17 @@ function createMockCore() {
|
|
|
80
86
|
|
|
81
87
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
82
88
|
|
|
83
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
89
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
90
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
91
|
+
const actual =
|
|
92
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
84
93
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
85
94
|
hoisted.MockCoreConstructor(...args);
|
|
86
95
|
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
87
96
|
if (instance) Object.assign(this, instance);
|
|
88
97
|
}
|
|
89
98
|
return {
|
|
99
|
+
...actual,
|
|
90
100
|
CopilotKitCoreReact,
|
|
91
101
|
useAgent: () => {
|
|
92
102
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -145,7 +155,10 @@ vi.mock("@copilotkit/react-core/context", () => {
|
|
|
145
155
|
};
|
|
146
156
|
});
|
|
147
157
|
|
|
148
|
-
vi.mock("@copilotkit/shared", () => ({
|
|
158
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
159
|
+
// The real module first: the provider evaluates @copilotkit/core, which
|
|
160
|
+
// needs shared's value exports.
|
|
161
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
149
162
|
DEFAULT_AGENT_ID: "default",
|
|
150
163
|
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
151
164
|
getModalityFromMimeType: (mimeType: string) => {
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thread restore and thread switching in the headless `<CopilotChat>`. Runs on
|
|
3
|
+
* a REAL `CopilotKitCoreReact` (via `TestCopilotKit`), so the connect, the
|
|
4
|
+
* reset on a switch to a fresh thread, and the send queue are core's
|
|
5
|
+
* `ɵThreadSwitchCoordinator` and `ɵChatSubmitter`, as in the web chats.
|
|
6
|
+
*/
|
|
7
|
+
import React from "react";
|
|
8
|
+
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
9
|
+
import { describe, expect, it, vi } from "vitest";
|
|
10
|
+
import type { Message, RunAgentResult } from "@ag-ui/client";
|
|
11
|
+
import { CopilotChat, useCopilotChatContext } from "../CopilotChat";
|
|
12
|
+
import { SeededAgent, TestCopilotKit } from "../__mocks__/test-copilotkit";
|
|
13
|
+
import type { CopilotChatContextValue } from "../CopilotChat";
|
|
14
|
+
|
|
15
|
+
const EMPTY_RESULT: RunAgentResult = { result: undefined, newMessages: [] };
|
|
16
|
+
|
|
17
|
+
const text = (id: string, content: string): Message => ({
|
|
18
|
+
id,
|
|
19
|
+
role: "assistant",
|
|
20
|
+
content,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An agent with stored threads. `connectAgent` restores the history of the
|
|
25
|
+
* thread the agent is on, as the runtime's `/connect` does. A test can hold
|
|
26
|
+
* the connect open with `holdConnect`.
|
|
27
|
+
*/
|
|
28
|
+
class ThreadedAgent extends SeededAgent {
|
|
29
|
+
readonly connectedThreadIds: string[] = [];
|
|
30
|
+
readonly runs: { threadId: string; messages: string[] }[] = [];
|
|
31
|
+
private heldConnect: Promise<void> | undefined;
|
|
32
|
+
private heldRun: Promise<void> | undefined;
|
|
33
|
+
|
|
34
|
+
constructor(private readonly histories: Record<string, Message[]>) {
|
|
35
|
+
super();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
holdConnect() {
|
|
39
|
+
let release!: () => void;
|
|
40
|
+
this.heldConnect = new Promise<void>((resolve) => {
|
|
41
|
+
release = resolve;
|
|
42
|
+
});
|
|
43
|
+
return release;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override async connectAgent(): Promise<RunAgentResult> {
|
|
47
|
+
const threadId = this.threadId;
|
|
48
|
+
this.connectedThreadIds.push(threadId);
|
|
49
|
+
await this.heldConnect;
|
|
50
|
+
this.setMessages(this.histories[threadId] ?? []);
|
|
51
|
+
return EMPTY_RESULT;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Holds the next run open; the returned function finishes it with a reply. */
|
|
55
|
+
holdRun() {
|
|
56
|
+
let release!: () => void;
|
|
57
|
+
this.heldRun = new Promise<void>((resolve) => {
|
|
58
|
+
release = resolve;
|
|
59
|
+
});
|
|
60
|
+
return release;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override async runAgent(): Promise<RunAgentResult> {
|
|
64
|
+
this.runs.push({
|
|
65
|
+
threadId: this.threadId,
|
|
66
|
+
messages: this.messages.map((m) => String(m.content)),
|
|
67
|
+
});
|
|
68
|
+
const held = this.heldRun;
|
|
69
|
+
if (held) {
|
|
70
|
+
this.heldRun = undefined;
|
|
71
|
+
await held;
|
|
72
|
+
this.addMessage(text("reply", "reply"));
|
|
73
|
+
}
|
|
74
|
+
return EMPTY_RESULT;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let chat: CopilotChatContextValue | null = null;
|
|
79
|
+
|
|
80
|
+
function Transcript() {
|
|
81
|
+
chat = useCopilotChatContext();
|
|
82
|
+
return (
|
|
83
|
+
<ul data-testid="transcript">
|
|
84
|
+
{chat.messages.map((m: Message) => (
|
|
85
|
+
<li key={m.id}>{String(m.content)}</li>
|
|
86
|
+
))}
|
|
87
|
+
</ul>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function renderChat(agent: ThreadedAgent, threadId?: string) {
|
|
92
|
+
const agents = { x: agent };
|
|
93
|
+
const view = (id?: string) => (
|
|
94
|
+
<TestCopilotKit messages={[]} agents={agents}>
|
|
95
|
+
<CopilotChat agentId="x" threadId={id}>
|
|
96
|
+
<Transcript />
|
|
97
|
+
</CopilotChat>
|
|
98
|
+
</TestCopilotKit>
|
|
99
|
+
);
|
|
100
|
+
const result = render(view(threadId));
|
|
101
|
+
return {
|
|
102
|
+
switchTo: (id?: string) => result.rerender(view(id)),
|
|
103
|
+
unmount: result.unmount,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const transcript = () =>
|
|
108
|
+
Array.from(screen.getByTestId("transcript").children).map(
|
|
109
|
+
(li) => li.textContent,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
describe("headless CopilotChat threads", () => {
|
|
113
|
+
it("restores the thread named by threadId", async () => {
|
|
114
|
+
const agent = new ThreadedAgent({ t1: [text("a", "from t1")] });
|
|
115
|
+
|
|
116
|
+
renderChat(agent, "t1");
|
|
117
|
+
|
|
118
|
+
await waitFor(() => expect(transcript()).toEqual(["from t1"]));
|
|
119
|
+
expect(agent.connectedThreadIds).toEqual(["t1"]);
|
|
120
|
+
expect(agent.threadId).toBe("t1");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("restores the new thread when threadId changes", async () => {
|
|
124
|
+
const agent = new ThreadedAgent({
|
|
125
|
+
t1: [text("a", "from t1")],
|
|
126
|
+
t2: [text("b", "from t2")],
|
|
127
|
+
});
|
|
128
|
+
const { switchTo } = renderChat(agent, "t1");
|
|
129
|
+
await waitFor(() => expect(transcript()).toEqual(["from t1"]));
|
|
130
|
+
|
|
131
|
+
switchTo("t2");
|
|
132
|
+
|
|
133
|
+
await waitFor(() => expect(transcript()).toEqual(["from t2"]));
|
|
134
|
+
expect(agent.connectedThreadIds).toEqual(["t1", "t2"]);
|
|
135
|
+
expect(agent.threadId).toBe("t2");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("does not connect a chat without threadId, and gives the agent a new thread", () => {
|
|
139
|
+
const agent = new ThreadedAgent({});
|
|
140
|
+
const before = agent.threadId;
|
|
141
|
+
|
|
142
|
+
renderChat(agent);
|
|
143
|
+
|
|
144
|
+
expect(agent.connectedThreadIds).toEqual([]);
|
|
145
|
+
expect(agent.threadId).toEqual(expect.any(String));
|
|
146
|
+
expect(agent.threadId).not.toBe(before);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("empties the chat when threadId is removed", async () => {
|
|
150
|
+
const agent = new ThreadedAgent({ t1: [text("a", "from t1")] });
|
|
151
|
+
const { switchTo } = renderChat(agent, "t1");
|
|
152
|
+
await waitFor(() => expect(transcript()).toEqual(["from t1"]));
|
|
153
|
+
|
|
154
|
+
switchTo(undefined);
|
|
155
|
+
|
|
156
|
+
await waitFor(() => expect(transcript()).toEqual([]));
|
|
157
|
+
expect(agent.threadId).not.toBe("t1");
|
|
158
|
+
expect(agent.connectedThreadIds).toEqual(["t1"]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("sends a message only after the thread has been restored", async () => {
|
|
162
|
+
const agent = new ThreadedAgent({ t1: [text("a", "from t1")] });
|
|
163
|
+
const release = agent.holdConnect();
|
|
164
|
+
renderChat(agent, "t1");
|
|
165
|
+
await waitFor(() => expect(agent.connectedThreadIds).toEqual(["t1"]));
|
|
166
|
+
|
|
167
|
+
let sent: Promise<void> | undefined;
|
|
168
|
+
act(() => {
|
|
169
|
+
sent = chat!.submitMessage("hello");
|
|
170
|
+
});
|
|
171
|
+
// The connect is still loading, so the send waits for it.
|
|
172
|
+
await Promise.resolve();
|
|
173
|
+
expect(agent.runs).toEqual([]);
|
|
174
|
+
|
|
175
|
+
await act(async () => {
|
|
176
|
+
release();
|
|
177
|
+
await sent;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
expect(agent.runs).toEqual([
|
|
181
|
+
{ threadId: "t1", messages: ["from t1", "hello"] },
|
|
182
|
+
]);
|
|
183
|
+
});
|
|
184
|
+
it("leaves the agent's run running when the chat unmounts after the thread loaded", async () => {
|
|
185
|
+
const agent = new ThreadedAgent({ t1: [text("a", "from t1")] });
|
|
186
|
+
const { unmount } = renderChat(agent, "t1");
|
|
187
|
+
await waitFor(() => expect(transcript()).toEqual(["from t1"]));
|
|
188
|
+
const finishRun = agent.holdRun();
|
|
189
|
+
let sent: Promise<void> | undefined;
|
|
190
|
+
act(() => {
|
|
191
|
+
sent = chat!.submitMessage("hello");
|
|
192
|
+
});
|
|
193
|
+
await waitFor(() => expect(agent.runs).toHaveLength(1));
|
|
194
|
+
// Core detaches the previous run itself before a connect or a run; count
|
|
195
|
+
// only what the chat's cleanup does.
|
|
196
|
+
const detach = vi.spyOn(agent, "detachActiveRun");
|
|
197
|
+
|
|
198
|
+
unmount();
|
|
199
|
+
await act(async () => {});
|
|
200
|
+
|
|
201
|
+
// The connect had settled, so the run in flight is not the connect's
|
|
202
|
+
// own: the reply the user asked for keeps streaming.
|
|
203
|
+
expect(detach).not.toHaveBeenCalled();
|
|
204
|
+
finishRun();
|
|
205
|
+
await sent;
|
|
206
|
+
expect(agent.messages.map((m) => String(m.content))).toEqual([
|
|
207
|
+
"from t1",
|
|
208
|
+
"hello",
|
|
209
|
+
"reply",
|
|
210
|
+
]);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("keeps the messages of an agent that cannot connect, and sends on its thread", async () => {
|
|
214
|
+
// An agent without connect: a connect would only empty it.
|
|
215
|
+
const agent = new (class extends SeededAgent {
|
|
216
|
+
readonly runs: string[] = [];
|
|
217
|
+
override async runAgent(): Promise<RunAgentResult> {
|
|
218
|
+
this.runs.push(this.threadId);
|
|
219
|
+
return EMPTY_RESULT;
|
|
220
|
+
}
|
|
221
|
+
})();
|
|
222
|
+
agent.setMessages([text("a", "kept")]);
|
|
223
|
+
renderChat(agent as unknown as ThreadedAgent, "t1");
|
|
224
|
+
|
|
225
|
+
await act(async () => {
|
|
226
|
+
await chat!.submitMessage("hello");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
expect(transcript()).toEqual(["kept", "hello"]);
|
|
230
|
+
expect(agent.runs).toEqual(["t1"]);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -33,6 +33,12 @@ function createMockCore() {
|
|
|
33
33
|
setMessageFilter: vi.fn(),
|
|
34
34
|
setDebug: vi.fn(),
|
|
35
35
|
setDefaultThrottleMs: vi.fn(),
|
|
36
|
+
ɵupdateConfig: vi.fn(),
|
|
37
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
38
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
39
|
+
setErrorHandler: vi.fn(),
|
|
40
|
+
connect: vi.fn(),
|
|
41
|
+
dispose: vi.fn(),
|
|
36
42
|
getAgent: vi.fn(() => undefined),
|
|
37
43
|
runtimeUrl: "https://api.test",
|
|
38
44
|
runtimeTransport: "auto",
|
|
@@ -52,13 +58,17 @@ function createMockCore() {
|
|
|
52
58
|
|
|
53
59
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
54
60
|
|
|
55
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
61
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
62
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
63
|
+
const actual =
|
|
64
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
56
65
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
57
66
|
hoisted.MockCoreConstructor(...args);
|
|
58
67
|
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
59
68
|
if (instance) Object.assign(this, instance);
|
|
60
69
|
}
|
|
61
70
|
return {
|
|
71
|
+
...actual,
|
|
62
72
|
CopilotKitCoreReact,
|
|
63
73
|
useAgent: (props: any) => {
|
|
64
74
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -118,7 +128,10 @@ vi.mock("@copilotkit/react-core/context", () => {
|
|
|
118
128
|
};
|
|
119
129
|
});
|
|
120
130
|
|
|
121
|
-
vi.mock("@copilotkit/shared", () => ({
|
|
131
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
132
|
+
// The real module first: the provider evaluates @copilotkit/core, which
|
|
133
|
+
// needs shared's value exports.
|
|
134
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
122
135
|
DEFAULT_AGENT_ID: "default",
|
|
123
136
|
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
124
137
|
getModalityFromMimeType: (mimeType: string) => {
|
|
@@ -33,6 +33,12 @@ function createMockCore() {
|
|
|
33
33
|
setMessageFilter: vi.fn(),
|
|
34
34
|
setDebug: vi.fn(),
|
|
35
35
|
setDefaultThrottleMs: vi.fn(),
|
|
36
|
+
ɵupdateConfig: vi.fn(),
|
|
37
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
38
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
39
|
+
setErrorHandler: vi.fn(),
|
|
40
|
+
connect: vi.fn(),
|
|
41
|
+
dispose: vi.fn(),
|
|
36
42
|
getAgent: vi.fn(() => undefined),
|
|
37
43
|
runtimeUrl: "https://api.test",
|
|
38
44
|
runtimeTransport: "auto",
|
|
@@ -52,13 +58,17 @@ function createMockCore() {
|
|
|
52
58
|
|
|
53
59
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
54
60
|
|
|
55
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
61
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
62
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
63
|
+
const actual =
|
|
64
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
56
65
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
57
66
|
hoisted.MockCoreConstructor(...args);
|
|
58
67
|
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
59
68
|
if (instance) Object.assign(this, instance);
|
|
60
69
|
}
|
|
61
70
|
return {
|
|
71
|
+
...actual,
|
|
62
72
|
CopilotKitCoreReact,
|
|
63
73
|
useAgent: (props: any) => {
|
|
64
74
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -118,7 +128,10 @@ vi.mock("@copilotkit/react-core/context", () => {
|
|
|
118
128
|
};
|
|
119
129
|
});
|
|
120
130
|
|
|
121
|
-
vi.mock("@copilotkit/shared", () => ({
|
|
131
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
132
|
+
// The real module first: the provider evaluates @copilotkit/core, which
|
|
133
|
+
// needs shared's value exports.
|
|
134
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
122
135
|
DEFAULT_AGENT_ID: "default",
|
|
123
136
|
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
124
137
|
getModalityFromMimeType: (mimeType: string) => {
|