@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
package/src/CopilotChat.tsx
CHANGED
|
@@ -3,14 +3,26 @@ import React, {
|
|
|
3
3
|
useCallback,
|
|
4
4
|
useContext,
|
|
5
5
|
useEffect,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
6
8
|
useRef,
|
|
9
|
+
useState,
|
|
7
10
|
} from "react";
|
|
8
11
|
import type { ReactNode } from "react";
|
|
9
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
CopilotChatConfigurationProvider,
|
|
14
|
+
useAgent,
|
|
15
|
+
useCopilotChatConfiguration,
|
|
16
|
+
} from "@copilotkit/react-core/headless";
|
|
10
17
|
import { useCopilotKit } from "@copilotkit/react-core/context";
|
|
11
|
-
import { DEFAULT_AGENT_ID
|
|
12
|
-
import type { InputContent } from "@copilotkit/shared";
|
|
18
|
+
import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
|
|
13
19
|
import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
|
|
20
|
+
import {
|
|
21
|
+
ɵChatConnection,
|
|
22
|
+
ɵFreshThreadId,
|
|
23
|
+
ɵcreateChatSubmitter,
|
|
24
|
+
ɵsubscribeToAgentErrors,
|
|
25
|
+
} from "@copilotkit/core";
|
|
14
26
|
import { useAttachments } from "./hooks/use-attachments";
|
|
15
27
|
import type { NativeAttachmentsConfig } from "./hooks/use-attachments";
|
|
16
28
|
import type { Attachment } from "@copilotkit/shared";
|
|
@@ -35,9 +47,10 @@ export interface CopilotChatContextValue {
|
|
|
35
47
|
/** Remove an attachment by ID. */
|
|
36
48
|
removeAttachment: (id: string) => void;
|
|
37
49
|
/**
|
|
38
|
-
* Submit a message with
|
|
39
|
-
*
|
|
40
|
-
*
|
|
50
|
+
* Submit a message with the ready attachments and run the agent. Waits
|
|
51
|
+
* for the thread's connection and for the run in flight. Blank text, and a
|
|
52
|
+
* send while an attachment uploads, are ignored. Resolves once the run has
|
|
53
|
+
* settled.
|
|
41
54
|
*/
|
|
42
55
|
submitMessage: (text: string) => Promise<void>;
|
|
43
56
|
}
|
|
@@ -67,7 +80,8 @@ export interface CopilotChatBaseProps {
|
|
|
67
80
|
* The agent ID to use for this chat session.
|
|
68
81
|
* Matches the web SDK's CopilotChat `agentId` prop.
|
|
69
82
|
*
|
|
70
|
-
* Resolution order: `agentId` > `agentName` >
|
|
83
|
+
* Resolution order: `agentId` > `agentName` > the agent of the chat (or
|
|
84
|
+
* `CopilotChatConfigurationProvider`) around this one > `"default"`
|
|
71
85
|
*/
|
|
72
86
|
agentId?: string;
|
|
73
87
|
|
|
@@ -78,15 +92,17 @@ export interface CopilotChatBaseProps {
|
|
|
78
92
|
agentName?: string;
|
|
79
93
|
|
|
80
94
|
/**
|
|
81
|
-
* Thread ID for this chat session. When provided, the chat
|
|
82
|
-
* the
|
|
95
|
+
* Thread ID for this chat session. When provided, the chat restores the
|
|
96
|
+
* thread from the backend and reconnects to a run in progress. Without it,
|
|
97
|
+
* the chat uses the thread of the chat around it, or a new thread. Matches
|
|
98
|
+
* the web SDK's CopilotChat `threadId` prop.
|
|
83
99
|
*/
|
|
84
100
|
threadId?: string;
|
|
85
101
|
|
|
86
102
|
/**
|
|
87
103
|
* Error handler scoped to this chat's agent. Fires in addition to the
|
|
88
|
-
* provider-level onError (does not suppress it). Receives
|
|
89
|
-
*
|
|
104
|
+
* provider-level onError (does not suppress it). Receives errors whose
|
|
105
|
+
* context.agentId matches this chat's agent, and errors with no agentId.
|
|
90
106
|
*/
|
|
91
107
|
onError?: (event: {
|
|
92
108
|
error: Error;
|
|
@@ -130,6 +146,16 @@ export interface CopilotChatProps extends CopilotChatBaseProps {
|
|
|
130
146
|
*
|
|
131
147
|
* Children can access chat state via `useCopilotChatContext()`.
|
|
132
148
|
*
|
|
149
|
+
* Hooks inside the chat, such as `useAgent` and `useInterrupt`, use this
|
|
150
|
+
* chat's agent when they do not pass an `agentId`. When the chat has a
|
|
151
|
+
* `threadId`, `useAgent` inside the chat sets that thread on each agent it
|
|
152
|
+
* returns, also on an agent that the hook names with its own `agentId`. Only
|
|
153
|
+
* `useAgent({ agentId, runtimeAgentId, threadId })` keeps a different thread.
|
|
154
|
+
*
|
|
155
|
+
* A chat inside another chat uses the outer chat's agent when it has no
|
|
156
|
+
* `agentId`. When the outer chat has a `threadId` and the inner chat does not,
|
|
157
|
+
* the inner chat uses the outer thread. The web `CopilotChat` does the same.
|
|
158
|
+
*
|
|
133
159
|
* ```tsx
|
|
134
160
|
* import { CopilotChat, useCopilotChatContext } from "@copilotkit/react-native";
|
|
135
161
|
*
|
|
@@ -153,7 +179,11 @@ export function CopilotChat({
|
|
|
153
179
|
children,
|
|
154
180
|
..._rest
|
|
155
181
|
}: CopilotChatProps) {
|
|
156
|
-
|
|
182
|
+
// Same precedence as the web CopilotChat: a chat that names no agent takes
|
|
183
|
+
// the agent of the chat configuration around it before "default".
|
|
184
|
+
const chatConfig = useCopilotChatConfiguration();
|
|
185
|
+
const resolvedAgentId =
|
|
186
|
+
agentId ?? agentName ?? chatConfig?.agentId ?? DEFAULT_AGENT_ID;
|
|
157
187
|
|
|
158
188
|
// Deprecation warning (dev only, fires once per mount)
|
|
159
189
|
const warnedRef = useRef(false);
|
|
@@ -172,17 +202,37 @@ export function CopilotChat({
|
|
|
172
202
|
}
|
|
173
203
|
}, [agentName, agentId]);
|
|
174
204
|
|
|
205
|
+
// Same thread rules as the web CopilotChat: the `threadId` prop, else the
|
|
206
|
+
// thread of the chat around this one, else a UUID minted for this chat.
|
|
207
|
+
// Only a thread a caller picked is "explicit" and restored from the backend.
|
|
208
|
+
const providedThreadId = threadId ?? chatConfig?.threadId;
|
|
209
|
+
const [freshThreadId] = useState(() => new ɵFreshThreadId());
|
|
210
|
+
const resolvedThreadId = useMemo(
|
|
211
|
+
() => freshThreadId.resolve(providedThreadId),
|
|
212
|
+
[freshThreadId, providedThreadId],
|
|
213
|
+
);
|
|
214
|
+
const hasExplicitThreadId = !!threadId || !!chatConfig?.hasExplicitThreadId;
|
|
215
|
+
|
|
175
216
|
const { agent } = useAgent({ agentId: resolvedAgentId, throttleMs });
|
|
217
|
+
const { copilotkit } = useCopilotKit();
|
|
176
218
|
|
|
177
|
-
//
|
|
219
|
+
// Keeps the agent on the thread this chat shows, restores a picked thread
|
|
220
|
+
// with a connect, and empties the agent on a switch to a fresh thread. Core
|
|
221
|
+
// owns the connect's abort and detach, as in the web chats.
|
|
222
|
+
const [connection] = useState(
|
|
223
|
+
() => new ɵChatConnection({ core: copilotkit }),
|
|
224
|
+
);
|
|
225
|
+
const { threads } = connection;
|
|
178
226
|
useEffect(() => {
|
|
179
|
-
|
|
180
|
-
agent
|
|
181
|
-
|
|
182
|
-
|
|
227
|
+
const handle = connection.open({
|
|
228
|
+
agent,
|
|
229
|
+
threadId: resolvedThreadId,
|
|
230
|
+
explicit: hasExplicitThreadId,
|
|
231
|
+
});
|
|
232
|
+
return () => handle.release();
|
|
233
|
+
}, [connection, agent, resolvedThreadId, hasExplicitThreadId]);
|
|
183
234
|
|
|
184
235
|
// onError subscription -- forward core errors scoped to this chat's agent
|
|
185
|
-
const { copilotkit } = useCopilotKit();
|
|
186
236
|
const onErrorRef = useRef(onError);
|
|
187
237
|
useEffect(() => {
|
|
188
238
|
onErrorRef.current = onError;
|
|
@@ -190,26 +240,9 @@ export function CopilotChat({
|
|
|
190
240
|
|
|
191
241
|
useEffect(() => {
|
|
192
242
|
if (!onErrorRef.current) return;
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// Only forward errors that match this chat's agent
|
|
197
|
-
if (
|
|
198
|
-
event.context?.agentId === resolvedAgentId ||
|
|
199
|
-
!event.context?.agentId
|
|
200
|
-
) {
|
|
201
|
-
onErrorRef.current?.({
|
|
202
|
-
error: event.error,
|
|
203
|
-
code: event.code,
|
|
204
|
-
context: event.context,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return () => {
|
|
211
|
-
subscription.unsubscribe();
|
|
212
|
-
};
|
|
243
|
+
return ɵsubscribeToAgentErrors(copilotkit, resolvedAgentId, (event) =>
|
|
244
|
+
onErrorRef.current?.(event),
|
|
245
|
+
);
|
|
213
246
|
}, [copilotkit, resolvedAgentId]);
|
|
214
247
|
|
|
215
248
|
// Attachments
|
|
@@ -220,60 +253,39 @@ export function CopilotChat({
|
|
|
220
253
|
removeAttachment,
|
|
221
254
|
consumeAttachments,
|
|
222
255
|
} = useAttachments({ config: attachmentsConfig });
|
|
256
|
+
// A send waits for the run in flight, so the submitter re-checks the
|
|
257
|
+
// upload guard against the latest attachments, not this render's.
|
|
258
|
+
const attachmentsRef = useRef(selectedAttachments);
|
|
259
|
+
useLayoutEffect(() => {
|
|
260
|
+
attachmentsRef.current = selectedAttachments;
|
|
261
|
+
});
|
|
223
262
|
|
|
224
|
-
//
|
|
263
|
+
// The send side lives in core: the upload guard, the message content, and
|
|
264
|
+
// waiting for the thread's connection and the run in flight.
|
|
265
|
+
const agentRef = useRef(agent);
|
|
266
|
+
useLayoutEffect(() => {
|
|
267
|
+
agentRef.current = agent;
|
|
268
|
+
});
|
|
269
|
+
const [submitter] = useState(() =>
|
|
270
|
+
ɵcreateChatSubmitter({
|
|
271
|
+
core: copilotkit,
|
|
272
|
+
getAgent: () => agentRef.current,
|
|
273
|
+
// This chat renders no input box, so there is no text to clear or put
|
|
274
|
+
// back.
|
|
275
|
+
input: { get: () => "", set: () => {} },
|
|
276
|
+
attachments: {
|
|
277
|
+
isUploading: () =>
|
|
278
|
+
attachmentsRef.current.some((a) => a.status === "uploading"),
|
|
279
|
+
consumeReady: () => consumeAttachments(),
|
|
280
|
+
},
|
|
281
|
+
threads,
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
284
|
+
// Clears the notice timer of a blocked send on unmount.
|
|
285
|
+
useEffect(() => () => submitter.showNotice(null), [submitter]);
|
|
225
286
|
const submitMessage = useCallback(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const hasUploading = selectedAttachments.some(
|
|
229
|
-
(a) => a.status === "uploading",
|
|
230
|
-
);
|
|
231
|
-
if (hasUploading) {
|
|
232
|
-
console.error(
|
|
233
|
-
"[CopilotKit] Cannot send while attachments are uploading",
|
|
234
|
-
);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const readyAttachments = consumeAttachments();
|
|
239
|
-
|
|
240
|
-
if (readyAttachments.length > 0) {
|
|
241
|
-
const contentParts: InputContent[] = [];
|
|
242
|
-
if (value.trim()) {
|
|
243
|
-
contentParts.push({ type: "text", text: value });
|
|
244
|
-
}
|
|
245
|
-
for (const att of readyAttachments) {
|
|
246
|
-
contentParts.push({
|
|
247
|
-
type: att.type,
|
|
248
|
-
source: att.source,
|
|
249
|
-
metadata: {
|
|
250
|
-
...(att.filename ? { filename: att.filename } : {}),
|
|
251
|
-
...att.metadata,
|
|
252
|
-
},
|
|
253
|
-
} as InputContent);
|
|
254
|
-
}
|
|
255
|
-
agent.addMessage({
|
|
256
|
-
id: randomUUID(),
|
|
257
|
-
role: "user",
|
|
258
|
-
content: contentParts,
|
|
259
|
-
});
|
|
260
|
-
} else {
|
|
261
|
-
agent.addMessage({
|
|
262
|
-
id: randomUUID(),
|
|
263
|
-
role: "user",
|
|
264
|
-
content: value,
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
try {
|
|
269
|
-
await copilotkit.runAgent({ agent });
|
|
270
|
-
} catch (error) {
|
|
271
|
-
console.error("CopilotChat: runAgent failed", error);
|
|
272
|
-
}
|
|
273
|
-
},
|
|
274
|
-
// copilotkit is intentionally excluded -- it is a stable ref that never changes.
|
|
275
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
276
|
-
[agent, selectedAttachments, consumeAttachments],
|
|
287
|
+
(value: string) => submitter.submit(value),
|
|
288
|
+
[submitter],
|
|
277
289
|
);
|
|
278
290
|
|
|
279
291
|
const contextValue: CopilotChatContextValue = {
|
|
@@ -287,9 +299,18 @@ export function CopilotChat({
|
|
|
287
299
|
submitMessage,
|
|
288
300
|
};
|
|
289
301
|
|
|
302
|
+
// Publish this chat's agent and thread to the subtree, as the web
|
|
303
|
+
// CopilotChat does. Hooks inside it that name no agent use this chat's
|
|
304
|
+
// agent, which is the outer chat's agent when this chat names none.
|
|
290
305
|
return (
|
|
291
|
-
<
|
|
292
|
-
{
|
|
293
|
-
|
|
306
|
+
<CopilotChatConfigurationProvider
|
|
307
|
+
agentId={resolvedAgentId}
|
|
308
|
+
threadId={resolvedThreadId}
|
|
309
|
+
hasExplicitThreadId={hasExplicitThreadId}
|
|
310
|
+
>
|
|
311
|
+
<CopilotChatCtx.Provider value={contextValue}>
|
|
312
|
+
{children}
|
|
313
|
+
</CopilotChatCtx.Provider>
|
|
314
|
+
</CopilotChatConfigurationProvider>
|
|
294
315
|
);
|
|
295
316
|
}
|
|
@@ -8,77 +8,37 @@ import type {
|
|
|
8
8
|
CopilotKitContextValue,
|
|
9
9
|
CopilotKitCoreReact as CopilotKitCoreReactInstance,
|
|
10
10
|
} from "@copilotkit/react-core/context";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
CopilotKitCoreReact,
|
|
13
|
+
ɵuseOwnedCore,
|
|
14
|
+
} from "@copilotkit/react-core/headless";
|
|
12
15
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
ɵCopilotKitProviderBaseOptions,
|
|
17
|
+
ɵRuntimeInfo,
|
|
18
|
+
} from "@copilotkit/core";
|
|
19
|
+
import {
|
|
20
|
+
ɵreportProviderConfigDiagnostics,
|
|
21
|
+
ɵresolveProviderConfig,
|
|
15
22
|
} from "@copilotkit/core";
|
|
16
|
-
import type { DebugConfig, RuntimeLicenseStatus } from "@copilotkit/shared";
|
|
17
23
|
import { createLicenseContextValue } from "@copilotkit/shared";
|
|
18
24
|
import { MarkdownRendererProvider } from "./components/MarkdownRendererContext";
|
|
19
25
|
import type { MarkdownRendererValue } from "./components/MarkdownRendererContext";
|
|
20
26
|
|
|
21
|
-
export interface CopilotKitNativeProviderProps
|
|
27
|
+
export interface CopilotKitNativeProviderProps extends Pick<
|
|
28
|
+
ɵCopilotKitProviderBaseOptions,
|
|
29
|
+
| "credentials"
|
|
30
|
+
| "messageFilter"
|
|
31
|
+
| "useSingleEndpoint"
|
|
32
|
+
| "properties"
|
|
33
|
+
| "onError"
|
|
34
|
+
| "debug"
|
|
35
|
+
| "defaultThrottleMs"
|
|
36
|
+
> {
|
|
22
37
|
children: ReactNode;
|
|
23
38
|
/** URL of the CopilotKit runtime endpoint */
|
|
24
39
|
runtimeUrl: string;
|
|
25
40
|
/** Custom headers sent with every request */
|
|
26
41
|
headers?: Record<string, string> | (() => Record<string, string>);
|
|
27
|
-
/**
|
|
28
|
-
* Credentials mode for fetch requests (e.g., "include" for HTTP-only cookies in cross-origin requests).
|
|
29
|
-
*/
|
|
30
|
-
credentials?: RequestCredentials;
|
|
31
|
-
/**
|
|
32
|
-
* Rewrites the message list sent to runtime agents on every run.
|
|
33
|
-
*
|
|
34
|
-
* CopilotKit sends the whole thread each time. When your agent already
|
|
35
|
-
* stores the conversation, most of that payload is waste, and an agent that
|
|
36
|
-
* merges the inbound list with its own store can show the model every turn
|
|
37
|
-
* twice. Return the messages to send:
|
|
38
|
-
*
|
|
39
|
-
* ```tsx
|
|
40
|
-
* <CopilotKitProvider
|
|
41
|
-
* runtimeUrl="/api/copilotkit"
|
|
42
|
-
* messageFilter={(messages) => messages.slice(-1)}
|
|
43
|
-
* />
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* The filter changes the request body only. The transcript the UI renders is
|
|
47
|
-
* untouched. Broken tool-call pairs are repaired before the request is sent,
|
|
48
|
-
* so a filter this blunt cannot strand a tool result mid-HITL.
|
|
49
|
-
*
|
|
50
|
-
* Agents reached through your CopilotRuntime honor this. An agent your app
|
|
51
|
-
* passes in directly does not, and neither Intelligence runs nor suggestion
|
|
52
|
-
* runs are ever filtered.
|
|
53
|
-
*
|
|
54
|
-
* Prefer a stable reference (`useCallback`). An inline arrow re-registers the
|
|
55
|
-
* filter on every render, which is harmless but needless.
|
|
56
|
-
*/
|
|
57
|
-
messageFilter?: CopilotKitMessageFilter;
|
|
58
|
-
/** Whether the runtime uses a single-route endpoint */
|
|
59
|
-
useSingleEndpoint?: boolean;
|
|
60
|
-
/** Custom properties forwarded to agents */
|
|
61
|
-
properties?: Record<string, unknown>;
|
|
62
|
-
/**
|
|
63
|
-
* Error handler called when CopilotKit encounters an error.
|
|
64
|
-
* Fires for all error types (runtime connection failures, agent errors, tool errors).
|
|
65
|
-
* If not provided, errors are logged to console.error.
|
|
66
|
-
*/
|
|
67
|
-
onError?: (event: {
|
|
68
|
-
error: Error;
|
|
69
|
-
code: CopilotKitCoreErrorCode;
|
|
70
|
-
context: Record<string, any>;
|
|
71
|
-
}) => void | Promise<void>;
|
|
72
|
-
/**
|
|
73
|
-
* Enable debug logging for the client-side event pipeline.
|
|
74
|
-
* When `true`, enables verbose logging from the core instance.
|
|
75
|
-
*/
|
|
76
|
-
debug?: DebugConfig;
|
|
77
|
-
/**
|
|
78
|
-
* Default throttle interval (ms) for `onMessagesChanged` / `onStateChanged`
|
|
79
|
-
* subscriptions. Individual subscriptions can override with their own `throttleMs`.
|
|
80
|
-
*/
|
|
81
|
-
defaultThrottleMs?: number;
|
|
82
42
|
/**
|
|
83
43
|
* Custom markdown renderer for assistant messages. Either a component (escape
|
|
84
44
|
* hatch — receives `content` and an optional `isStreaming` flag) used instead
|
|
@@ -143,66 +103,75 @@ export const CopilotKitProvider: React.FC<CopilotKitNativeProviderProps> = ({
|
|
|
143
103
|
[JSON.stringify(properties)],
|
|
144
104
|
);
|
|
145
105
|
|
|
106
|
+
// Transport mapping and config checks come from core.
|
|
107
|
+
const resolvedConfig = useMemo(
|
|
108
|
+
() =>
|
|
109
|
+
ɵresolveProviderConfig({
|
|
110
|
+
runtimeUrl,
|
|
111
|
+
useSingleEndpoint,
|
|
112
|
+
properties: stableProperties,
|
|
113
|
+
}),
|
|
114
|
+
[runtimeUrl, useSingleEndpoint, stableProperties],
|
|
115
|
+
);
|
|
116
|
+
const loggedDiagnosticsRef = useRef(new Set<string>());
|
|
117
|
+
ɵreportProviderConfigDiagnostics(resolvedConfig.diagnostics, {
|
|
118
|
+
isProduction: process.env.NODE_ENV === "production",
|
|
119
|
+
logged: loggedDiagnosticsRef.current,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// One core for the provider lifetime, disposed on unmount (StrictMode
|
|
123
|
+
// safe, see `ɵuseOwnedCore`).
|
|
146
124
|
const copilotkitRef = useRef<CopilotKitCoreReactInstance | null>(null);
|
|
125
|
+
const copilotkit = ɵuseOwnedCore(
|
|
126
|
+
copilotkitRef,
|
|
127
|
+
(): CopilotKitCoreReactInstance =>
|
|
128
|
+
new CopilotKitCoreReact({
|
|
129
|
+
runtimeUrl: resolvedConfig.runtimeUrl,
|
|
130
|
+
runtimeTransport: resolvedConfig.runtimeTransport,
|
|
131
|
+
// `/info` starts from the effect below, so a render React discards
|
|
132
|
+
// never fetches it (#5801).
|
|
133
|
+
deferInitialConnection: true,
|
|
134
|
+
headers: stableHeaders,
|
|
135
|
+
credentials,
|
|
136
|
+
messageFilter,
|
|
137
|
+
properties: resolvedConfig.properties,
|
|
138
|
+
debug,
|
|
139
|
+
// Set at construction so child hooks see it on their first render.
|
|
140
|
+
defaultThrottleMs,
|
|
141
|
+
onError,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
147
144
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
145
|
+
// Core calls `onError`, and logs the error itself when there is none.
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
copilotkit.setErrorHandler(onError);
|
|
148
|
+
}, [copilotkit, onError]);
|
|
149
|
+
|
|
150
|
+
// Sync props to the core. Core applies only what changed.
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
copilotkit.ɵupdateConfig({
|
|
153
|
+
messageFilter,
|
|
154
|
+
runtimeUrl: resolvedConfig.runtimeUrl,
|
|
155
|
+
runtimeTransport: resolvedConfig.runtimeTransport,
|
|
157
156
|
headers: stableHeaders,
|
|
158
157
|
credentials,
|
|
159
|
-
|
|
160
|
-
properties: stableProperties,
|
|
158
|
+
properties: resolvedConfig.properties,
|
|
161
159
|
debug,
|
|
160
|
+
defaultThrottleMs,
|
|
162
161
|
});
|
|
163
|
-
|
|
164
|
-
// correct value on their first render (before useEffect fires).
|
|
165
|
-
if (defaultThrottleMs !== undefined) {
|
|
166
|
-
instance.setDefaultThrottleMs(defaultThrottleMs);
|
|
167
|
-
}
|
|
168
|
-
copilotkitRef.current = instance;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const copilotkit = copilotkitRef.current;
|
|
172
|
-
|
|
173
|
-
// Sync props to core instance
|
|
174
|
-
useEffect(() => {
|
|
175
|
-
copilotkit.setRuntimeUrl(runtimeUrl);
|
|
176
|
-
copilotkit.setRuntimeTransport(
|
|
177
|
-
useSingleEndpoint === true
|
|
178
|
-
? "single"
|
|
179
|
-
: useSingleEndpoint === false
|
|
180
|
-
? "rest"
|
|
181
|
-
: "auto",
|
|
182
|
-
);
|
|
183
|
-
copilotkit.setHeaders(stableHeaders);
|
|
184
|
-
copilotkit.setCredentials(credentials);
|
|
185
|
-
copilotkit.setMessageFilter(messageFilter);
|
|
186
|
-
copilotkit.setProperties(stableProperties);
|
|
187
|
-
copilotkit.setDebug(debug);
|
|
162
|
+
copilotkit.connect();
|
|
188
163
|
}, [
|
|
189
|
-
|
|
190
|
-
|
|
164
|
+
copilotkit,
|
|
165
|
+
messageFilter,
|
|
166
|
+
resolvedConfig.runtimeUrl,
|
|
167
|
+
resolvedConfig.runtimeTransport,
|
|
191
168
|
stableHeaders,
|
|
192
169
|
credentials,
|
|
193
|
-
|
|
194
|
-
stableProperties,
|
|
170
|
+
resolvedConfig.properties,
|
|
195
171
|
debug,
|
|
196
|
-
|
|
172
|
+
defaultThrottleMs,
|
|
197
173
|
]);
|
|
198
174
|
|
|
199
|
-
// Sync defaultThrottleMs to the core instance on prop changes.
|
|
200
|
-
// Initial value is set synchronously during instance creation (inside the
|
|
201
|
-
// ref guard above), so this only handles subsequent updates.
|
|
202
|
-
useEffect(() => {
|
|
203
|
-
copilotkit.setDefaultThrottleMs(defaultThrottleMs);
|
|
204
|
-
}, [copilotkit, defaultThrottleMs]);
|
|
205
|
-
|
|
206
175
|
// Track executing tool call IDs at the provider level.
|
|
207
176
|
// Critical for HITL reconnection: onToolExecutionStart fires before child
|
|
208
177
|
// components mount, so we must capture the state here.
|
|
@@ -210,17 +179,19 @@ export const CopilotKitProvider: React.FC<CopilotKitNativeProviderProps> = ({
|
|
|
210
179
|
ReadonlySet<string>
|
|
211
180
|
>(() => new Set());
|
|
212
181
|
|
|
213
|
-
const [
|
|
214
|
-
|
|
215
|
-
>(undefined);
|
|
182
|
+
const [runtimeInfo, setRuntimeInfo] = useState<ɵRuntimeInfo | null>(null);
|
|
183
|
+
const runtimeLicenseStatus = runtimeInfo?.licenseStatus;
|
|
216
184
|
|
|
217
|
-
//
|
|
218
|
-
|
|
185
|
+
// Project core's runtime info snapshot; the catch-up read reflects a status
|
|
186
|
+
// that settled before this effect ran.
|
|
219
187
|
useEffect(() => {
|
|
220
|
-
|
|
221
|
-
|
|
188
|
+
const syncRuntimeInfo = () => setRuntimeInfo(copilotkit.ɵruntimeInfo);
|
|
189
|
+
const unsubscribe = copilotkit.ɵsubscribeToRuntimeInfo(syncRuntimeInfo);
|
|
190
|
+
syncRuntimeInfo();
|
|
191
|
+
return unsubscribe;
|
|
192
|
+
}, [copilotkit]);
|
|
222
193
|
|
|
223
|
-
//
|
|
194
|
+
// Tool execution tracking.
|
|
224
195
|
// Tool call IDs are tracked at the provider level because onToolExecutionStart
|
|
225
196
|
// fires before child components mount — critical for HITL reconnection.
|
|
226
197
|
useEffect(() => {
|
|
@@ -241,20 +212,6 @@ export const CopilotKitProvider: React.FC<CopilotKitNativeProviderProps> = ({
|
|
|
241
212
|
return next;
|
|
242
213
|
});
|
|
243
214
|
},
|
|
244
|
-
onError: (event) => {
|
|
245
|
-
if (onErrorRef.current) {
|
|
246
|
-
onErrorRef.current(event);
|
|
247
|
-
} else {
|
|
248
|
-
console.error(
|
|
249
|
-
`[CopilotKit] Error (${event.code}):`,
|
|
250
|
-
event.error,
|
|
251
|
-
event.context ?? {},
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
},
|
|
255
|
-
onRuntimeConnectionStatusChanged: () => {
|
|
256
|
-
setRuntimeLicenseStatus(copilotkit.licenseStatus);
|
|
257
|
-
},
|
|
258
215
|
});
|
|
259
216
|
return () => subscription.unsubscribe();
|
|
260
217
|
}, [copilotkit]);
|
package/src/CopilotPopup.tsx
CHANGED
|
@@ -78,8 +78,8 @@ export interface CopilotPopupProps {
|
|
|
78
78
|
attachments?: NativeAttachmentsConfig;
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
81
|
+
* Your chat UI, rendered inside the popup card below the header. The inner
|
|
82
|
+
* CopilotChat is headless and draws no messages or input of its own.
|
|
83
83
|
*/
|
|
84
84
|
children?: ReactNode;
|
|
85
85
|
|
package/src/CopilotSidebar.tsx
CHANGED
|
@@ -9,7 +9,6 @@ import React, {
|
|
|
9
9
|
import type { ReactNode } from "react";
|
|
10
10
|
import {
|
|
11
11
|
Animated,
|
|
12
|
-
Dimensions,
|
|
13
12
|
Pressable,
|
|
14
13
|
StyleSheet,
|
|
15
14
|
Text,
|
|
@@ -70,7 +69,10 @@ export interface CopilotSidebarProps extends Omit<
|
|
|
70
69
|
/** Custom style applied to the drawer container. */
|
|
71
70
|
style?: ViewStyle;
|
|
72
71
|
|
|
73
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* Your chat UI, rendered inside the drawer below the header. The inner
|
|
74
|
+
* CopilotChat is headless and draws no messages or input of its own.
|
|
75
|
+
*/
|
|
74
76
|
children?: ReactNode;
|
|
75
77
|
}
|
|
76
78
|
|
|
@@ -133,9 +135,9 @@ export const CopilotSidebar = forwardRef<
|
|
|
133
135
|
|
|
134
136
|
// Animation & open state -------------------------------------------------
|
|
135
137
|
const [isOpen, setIsOpen] = useState(defaultOpen);
|
|
136
|
-
const slideAnim =
|
|
137
|
-
new Animated.Value(defaultOpen ? 0 : drawerWidth),
|
|
138
|
-
)
|
|
138
|
+
const [slideAnim] = useState(
|
|
139
|
+
() => new Animated.Value(defaultOpen ? 0 : drawerWidth),
|
|
140
|
+
);
|
|
139
141
|
|
|
140
142
|
// Keep the animated value in sync when drawerWidth changes while closed
|
|
141
143
|
useEffect(() => {
|
|
@@ -30,6 +30,36 @@ export const ScrollView = createMockComponent("ScrollView");
|
|
|
30
30
|
export const ActivityIndicator = createMockComponent("ActivityIndicator");
|
|
31
31
|
export const Image = createMockComponent("Image");
|
|
32
32
|
|
|
33
|
+
// A no-op Animated API. Components such as TypingIndicator create
|
|
34
|
+
// Animated.Value instances and start timing loops at render time; in tests the
|
|
35
|
+
// values never change and the animations never run.
|
|
36
|
+
class AnimatedValue {
|
|
37
|
+
constructor(private value: number) {}
|
|
38
|
+
setValue(value: number) {
|
|
39
|
+
this.value = value;
|
|
40
|
+
}
|
|
41
|
+
interpolate({ outputRange }: { outputRange: unknown[] }) {
|
|
42
|
+
return outputRange[0];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const noopAnimation = () => ({
|
|
47
|
+
start: () => {},
|
|
48
|
+
stop: () => {},
|
|
49
|
+
reset: () => {},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const Animated = {
|
|
53
|
+
Value: AnimatedValue,
|
|
54
|
+
View: createMockComponent("Animated.View"),
|
|
55
|
+
Text: createMockComponent("Animated.Text"),
|
|
56
|
+
timing: noopAnimation,
|
|
57
|
+
sequence: noopAnimation,
|
|
58
|
+
parallel: noopAnimation,
|
|
59
|
+
loop: noopAnimation,
|
|
60
|
+
delay: noopAnimation,
|
|
61
|
+
};
|
|
62
|
+
|
|
33
63
|
export const Platform = {
|
|
34
64
|
OS: "ios" as const,
|
|
35
65
|
select: <T>(obj: { ios?: T; android?: T; default?: T }): T | undefined =>
|
|
@@ -49,4 +79,5 @@ export default {
|
|
|
49
79
|
ActivityIndicator,
|
|
50
80
|
Image,
|
|
51
81
|
Platform,
|
|
82
|
+
Animated,
|
|
52
83
|
};
|