@copilotkit/react-native 2.0.0-rc.5 → 2.0.0-rc.51
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-CBVQLdzn.d.cts +77 -0
- package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-CCBBonxr.mjs} +63 -43
- package/dist/CopilotKitProvider-CCBBonxr.mjs.map +1 -0
- package/dist/CopilotKitProvider-CtQg_0S-.d.mts +77 -0
- package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-QdcQrrSA.cjs} +69 -42
- package/dist/CopilotKitProvider-QdcQrrSA.cjs.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 +3 -2
- package/dist/headless.d.cts +3 -3
- package/dist/headless.d.mts +3 -3
- package/dist/headless.mjs +2 -2
- package/dist/index.cjs +132 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -19
- package/dist/index.d.mts +37 -19
- package/dist/index.mjs +131 -162
- 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 +101 -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-alias-exports.test.ts +16 -0
- 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 -61
- 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/headless.ts +4 -0
- package/src/hooks/use-attachments.ts +95 -139
- package/src/index.ts +10 -4
- 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
|
@@ -88,6 +88,12 @@ function createMockCore() {
|
|
|
88
88
|
setMessageFilter: vi.fn(),
|
|
89
89
|
setDebug: vi.fn(),
|
|
90
90
|
setDefaultThrottleMs: vi.fn(),
|
|
91
|
+
ɵupdateConfig: vi.fn(),
|
|
92
|
+
ɵruntimeInfo: { status: "disconnected" },
|
|
93
|
+
ɵsubscribeToRuntimeInfo: vi.fn(() => () => {}),
|
|
94
|
+
setErrorHandler: vi.fn(),
|
|
95
|
+
connect: vi.fn(),
|
|
96
|
+
dispose: vi.fn(),
|
|
91
97
|
getAgent: vi.fn(() => undefined),
|
|
92
98
|
runtimeUrl: "https://api.test",
|
|
93
99
|
runtimeTransport: "auto",
|
|
@@ -107,7 +113,10 @@ function createMockCore() {
|
|
|
107
113
|
|
|
108
114
|
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
109
115
|
|
|
110
|
-
vi.mock("@copilotkit/react-core/headless", () => {
|
|
116
|
+
vi.mock("@copilotkit/react-core/headless", async (importOriginal) => {
|
|
117
|
+
// The real entry, so the provider gets the real `ɵuseOwnedCore`.
|
|
118
|
+
const actual =
|
|
119
|
+
await importOriginal<typeof import("@copilotkit/react-core/headless")>();
|
|
111
120
|
// Regular function (not arrow) so it's new-able
|
|
112
121
|
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
113
122
|
hoisted.MockCoreConstructor(...args);
|
|
@@ -115,6 +124,7 @@ vi.mock("@copilotkit/react-core/headless", () => {
|
|
|
115
124
|
if (instance) Object.assign(this, instance);
|
|
116
125
|
}
|
|
117
126
|
return {
|
|
127
|
+
...actual,
|
|
118
128
|
CopilotKitCoreReact,
|
|
119
129
|
useAgent: () => {
|
|
120
130
|
const ctx = require("react").useContext(hoisted.RealContext);
|
|
@@ -17,8 +17,10 @@ vi.mock("expo-file-system", () => ({
|
|
|
17
17
|
EncodingType: { Base64: "base64" },
|
|
18
18
|
}));
|
|
19
19
|
|
|
20
|
-
// Mock @copilotkit/shared
|
|
21
|
-
|
|
20
|
+
// Mock @copilotkit/shared over the real module: core's attachment queue
|
|
21
|
+
// needs its other exports.
|
|
22
|
+
vi.mock("@copilotkit/shared", async (importOriginal) => ({
|
|
23
|
+
...(await importOriginal<typeof import("@copilotkit/shared")>()),
|
|
22
24
|
getModalityFromMimeType: (mimeType: string) => {
|
|
23
25
|
if (mimeType.startsWith("image/")) return "image";
|
|
24
26
|
if (mimeType.startsWith("audio/")) return "audio";
|
|
@@ -157,6 +159,7 @@ describe("useAttachments (React Native)", () => {
|
|
|
157
159
|
expect(onUploadFailed).toHaveBeenCalledWith(
|
|
158
160
|
expect.objectContaining({
|
|
159
161
|
reason: "file-too-large",
|
|
162
|
+
file,
|
|
160
163
|
}),
|
|
161
164
|
);
|
|
162
165
|
});
|
|
@@ -189,6 +192,7 @@ describe("useAttachments (React Native)", () => {
|
|
|
189
192
|
expect(onUploadFailed).toHaveBeenCalledWith(
|
|
190
193
|
expect.objectContaining({
|
|
191
194
|
reason: "invalid-type",
|
|
195
|
+
file,
|
|
192
196
|
}),
|
|
193
197
|
);
|
|
194
198
|
});
|
|
@@ -219,10 +223,31 @@ describe("useAttachments (React Native)", () => {
|
|
|
219
223
|
expect(onUploadFailed).toHaveBeenCalledWith(
|
|
220
224
|
expect.objectContaining({
|
|
221
225
|
reason: "upload-failed",
|
|
226
|
+
file,
|
|
222
227
|
}),
|
|
223
228
|
);
|
|
224
229
|
});
|
|
225
230
|
|
|
231
|
+
it("ignores files while attachments are disabled", async () => {
|
|
232
|
+
const { result } = renderHook(() =>
|
|
233
|
+
useAttachments({ config: { enabled: false } }),
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
await act(async () => {
|
|
237
|
+
await result.current.processFiles([
|
|
238
|
+
{
|
|
239
|
+
uri: "file:///tmp/photo.jpg",
|
|
240
|
+
name: "photo.jpg",
|
|
241
|
+
size: 1024,
|
|
242
|
+
mimeType: "image/jpeg",
|
|
243
|
+
},
|
|
244
|
+
]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
expect(result.current.attachments).toEqual([]);
|
|
248
|
+
expect(mockReadAsStringAsync).not.toHaveBeenCalled();
|
|
249
|
+
});
|
|
250
|
+
|
|
226
251
|
it("calls custom onUpload instead of reading file directly", async () => {
|
|
227
252
|
const customUpload = vi.fn().mockResolvedValue({
|
|
228
253
|
type: "url",
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
2
9
|
import {
|
|
3
10
|
FlatList,
|
|
4
11
|
KeyboardAvoidingView,
|
|
@@ -13,11 +20,15 @@ import {
|
|
|
13
20
|
import type { ListRenderItemInfo, ViewStyle } from "react-native";
|
|
14
21
|
import { useAgent, useRenderToolCall } from "@copilotkit/react-core/headless";
|
|
15
22
|
import { useCopilotKit } from "@copilotkit/react-core/context";
|
|
23
|
+
import {
|
|
24
|
+
ɵcreateChatSubmitter,
|
|
25
|
+
ɵgetToolResults,
|
|
26
|
+
ɵmessagesFingerprint,
|
|
27
|
+
} from "@copilotkit/core";
|
|
28
|
+
import type { CopilotKitCore } from "@copilotkit/core";
|
|
16
29
|
import { AssistantMessage } from "./messages/AssistantMessage";
|
|
17
30
|
import { UserMessage } from "./messages/UserMessage";
|
|
18
|
-
import type { Message } from "@copilotkit/shared";
|
|
19
31
|
import type { ToolMessage } from "@ag-ui/client";
|
|
20
|
-
import { contentToText } from "@ag-ui/client";
|
|
21
32
|
|
|
22
33
|
/** Shape of an assistant message with optional tool calls. */
|
|
23
34
|
interface AssistantMessageShape {
|
|
@@ -72,179 +83,37 @@ interface ChatListItem {
|
|
|
72
83
|
}
|
|
73
84
|
|
|
74
85
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* The identity of `agent.messages` is NOT a reliable change signal, and it fails
|
|
78
|
-
* in BOTH directions:
|
|
79
|
-
*
|
|
80
|
-
* - It changes on paths that have nothing to do with what is read below.
|
|
81
|
-
* `@ag-ui/client`'s apply pipeline REASSIGNS the array
|
|
82
|
-
* (`AbstractAgent.processApplyEvents` does `this.messages = applied.messages`),
|
|
83
|
-
* so a streaming run hands down a fresh array — and fresh message, `toolCall`
|
|
84
|
-
* and `function` objects — on every applied delta.
|
|
85
|
-
* - It does NOT change on the paths these memos exist to serve. Core inserts
|
|
86
|
-
* tool results by mutating in place —
|
|
87
|
-
* `agent.messages.splice(insertAt, 0, toolMessage)`
|
|
88
|
-
* (packages/core/src/core/run-handler.ts:931, :1080) —
|
|
89
|
-
* `AbstractAgent.addMessage` is a `this.messages.push(...)`, and `useAgent`
|
|
90
|
-
* re-renders with a bare `forceUpdate()` rather than a new array.
|
|
91
|
-
*
|
|
92
|
-
* (Grepping `packages/core/src` for a `.messages` assignment finds test files
|
|
93
|
-
* only, but that proves nothing about identity: the reassignment lives in
|
|
94
|
-
* `@ag-ui/client`, outside that tree.)
|
|
95
|
-
*
|
|
96
|
-
* So anything derived from messages must depend on their CONTENT, not on the
|
|
97
|
-
* array reference.
|
|
98
|
-
*
|
|
99
|
-
* Captures exactly what the derivations below read — id, role, content size,
|
|
100
|
-
* `toolCallId` (so an inserted tool result is visible), and each tool call's id
|
|
101
|
-
* plus argument length (so streaming args advance).
|
|
102
|
-
*
|
|
103
|
-
* String and array content contribute their LENGTH rather than their value, so
|
|
104
|
-
* large text and base64 attachment payloads are never re-serialized on every
|
|
105
|
-
* render. Object content is the deliberate exception and IS serialized: a
|
|
106
|
-
* length-based key is a constant 0 for every object, so an in-place content
|
|
107
|
-
* replacement that keeps the same message id would otherwise be invisible to
|
|
108
|
-
* every memo below.
|
|
109
|
-
*
|
|
110
|
-
* That object branch is convergence, not a fix for a reachable stale render: the
|
|
111
|
-
* producer of same-id object content is an ACTIVITY_SNAPSHOT replace, and
|
|
112
|
-
* `role: "activity"` never reaches `listItems`, which builds rows for `user` and
|
|
113
|
-
* `assistant` only. It follows the SHAPE of react-core's `messagesMemoKey`
|
|
114
|
-
* (react-core #6325). The two are independent implementations of the same idea —
|
|
115
|
-
* a change to that key does not propagate here on its own, so treat this as a
|
|
116
|
-
* documented parallel rather than a mirror.
|
|
117
|
-
*/
|
|
118
|
-
function messagesFingerprint(messages: readonly unknown[]): string {
|
|
119
|
-
return messages
|
|
120
|
-
.map((msg) => {
|
|
121
|
-
const m = msg as {
|
|
122
|
-
id?: string;
|
|
123
|
-
role?: string;
|
|
124
|
-
content?: unknown;
|
|
125
|
-
toolCallId?: string;
|
|
126
|
-
toolCalls?: Array<{ id: string; function?: { arguments?: string } }>;
|
|
127
|
-
};
|
|
128
|
-
const content = m.content;
|
|
129
|
-
const contentKey =
|
|
130
|
-
typeof content === "string" || Array.isArray(content)
|
|
131
|
-
? content.length
|
|
132
|
-
: content && typeof content === "object"
|
|
133
|
-
? objectContentKey(content)
|
|
134
|
-
: 0;
|
|
135
|
-
const toolCallsKey = Array.isArray(m.toolCalls)
|
|
136
|
-
? m.toolCalls
|
|
137
|
-
.map((tc) => `${tc.id}:${tc.function?.arguments?.length ?? 0}`)
|
|
138
|
-
.join(";")
|
|
139
|
-
: "";
|
|
140
|
-
return `${m.id}:${m.role}:${contentKey}:${m.toolCallId ?? ""}:${toolCallsKey}`;
|
|
141
|
-
})
|
|
142
|
-
.join(",");
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Serializes object content for the fingerprint above.
|
|
147
|
-
*
|
|
148
|
-
* Guarded because `JSON.stringify` THROWS on a circular structure and
|
|
149
|
-
* `messagesFingerprint` runs on EVERY render, so a bare call would take the whole
|
|
150
|
-
* chat down on content this component is explicitly required to tolerate —
|
|
151
|
-
* `toolResultContent` below documents why non-string content reaches RN at all,
|
|
152
|
-
* and "does not throw on tool content that cannot be JSON-serialised" pins that a
|
|
153
|
-
* circular tool result must still render.
|
|
154
|
-
*
|
|
155
|
-
* The fallback is a constant, which means a circular object is invisible to the
|
|
156
|
-
* memo exactly as every object used to be. That is the correct trade: it confines
|
|
157
|
-
* the old always-equal behaviour to the pathological case instead of letting it
|
|
158
|
-
* decide the common one. NOTE: this guard is a deliberate divergence from
|
|
159
|
-
* react-core's `messagesMemoKey`, which stringifies unguarded.
|
|
160
|
-
*/
|
|
161
|
-
function objectContentKey(content: object): string {
|
|
162
|
-
try {
|
|
163
|
-
return JSON.stringify(content) ?? "";
|
|
164
|
-
} catch {
|
|
165
|
-
return "[unserializable]";
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Coerces a tool message's `content` to the `string` the renderer contract
|
|
171
|
-
* requires (`ReactToolCallRenderer`'s Complete branch declares `result: string`)
|
|
172
|
-
* WITHOUT inventing an empty result.
|
|
173
|
-
*
|
|
174
|
-
* AG-UI 1.0 models tool content as `string | ContentPart[]`: a plain string, or
|
|
175
|
-
* an ordered list of typed parts (text, image, audio, video, document). A
|
|
176
|
-
* string passes through; a list of parts is rendered as its text parts
|
|
177
|
-
* concatenated — the flattening the spec permits for a string-only consumer —
|
|
178
|
-
* and the media parts stay on the message for anything that can show them.
|
|
179
|
-
* Neither is malformed, so neither warns.
|
|
180
|
-
*
|
|
181
|
-
* Anything else is only reachable from a producer that skipped validation —
|
|
182
|
-
* restored thread history, a non-SSE transport, or app code casting on
|
|
183
|
-
* `addMessage`. Core hedges against that case too (it accepts `unknown` content
|
|
184
|
-
* and handles arrays of text parts), so this must not answer it with `""`: an
|
|
185
|
-
* empty string is a LEGITIMATE tool result, which makes a dropped result
|
|
186
|
-
* indistinguishable from an empty one. Serialise faithfully — the same
|
|
187
|
-
* representation core uses for non-string results — and warn in dev.
|
|
86
|
+
* The core as the chat's submitter takes it, with a failed run also passed to
|
|
87
|
+
* `onRunError` so the chat can show it. The submitter logs the failure itself.
|
|
188
88
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
(
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
function toolResultContent(content: unknown, toolCallId: string): string {
|
|
218
|
-
if (typeof content === "string") return content;
|
|
219
|
-
|
|
220
|
-
if (Array.isArray(content) && content.every(isContentPart)) {
|
|
221
|
-
return contentToText(content as ToolMessage["content"]);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// null/undefined carry no payload, so "" loses nothing — but the message is
|
|
225
|
-
// still malformed, so it warns below rather than passing silently.
|
|
226
|
-
let serialized = "";
|
|
227
|
-
if (content !== null && content !== undefined) {
|
|
228
|
-
try {
|
|
229
|
-
serialized =
|
|
230
|
-
JSON.stringify(content) ?? Object.prototype.toString.call(content);
|
|
231
|
-
} catch {
|
|
232
|
-
// Circular or otherwise non-serialisable: keep SOMETHING over dropping
|
|
233
|
-
// the result, and never throw from a render path.
|
|
234
|
-
serialized = Object.prototype.toString.call(content);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (typeof __DEV__ === "undefined" || __DEV__) {
|
|
239
|
-
console.warn(
|
|
240
|
-
`[CopilotChat] Tool message for tool call "${toolCallId}" had non-string ` +
|
|
241
|
-
`content (${content === null ? "null" : typeof content}), but renderers ` +
|
|
242
|
-
`receive \`result: string\`. Rendering a serialized form instead of ` +
|
|
243
|
-
`dropping it: ${serialized === "" ? "<empty>" : serialized}`,
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return serialized;
|
|
89
|
+
function withRunErrorReporting(
|
|
90
|
+
core: CopilotKitCore,
|
|
91
|
+
onRunError: (message: string) => void,
|
|
92
|
+
): Parameters<typeof ɵcreateChatSubmitter>[0]["core"] {
|
|
93
|
+
return {
|
|
94
|
+
runAgent: async (params) => {
|
|
95
|
+
try {
|
|
96
|
+
return await core.runAgent(params);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
onRunError(
|
|
99
|
+
err instanceof Error ? err.message : "An unexpected error occurred",
|
|
100
|
+
);
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
stopAgent: (params) => core.stopAgent(params),
|
|
105
|
+
// This chat does not transcribe, but the submitter's core type covers it.
|
|
106
|
+
get runtimeUrl() {
|
|
107
|
+
return core.runtimeUrl;
|
|
108
|
+
},
|
|
109
|
+
get runtimeTransport() {
|
|
110
|
+
return core.runtimeTransport;
|
|
111
|
+
},
|
|
112
|
+
get credentials() {
|
|
113
|
+
return core.credentials;
|
|
114
|
+
},
|
|
115
|
+
resolveHeaders: () => core.resolveHeaders(),
|
|
116
|
+
};
|
|
248
117
|
}
|
|
249
118
|
|
|
250
119
|
/**
|
|
@@ -276,7 +145,6 @@ export function CopilotChat({
|
|
|
276
145
|
const [inputText, setInputText] = useState("");
|
|
277
146
|
const [error, setError] = useState<string | null>(null);
|
|
278
147
|
const flatListRef = useRef<FlatList>(null);
|
|
279
|
-
const messageIdCounter = useRef(0);
|
|
280
148
|
|
|
281
149
|
const { copilotkit } = useCopilotKit();
|
|
282
150
|
const { agent } = useAgent({ agentId: agentName });
|
|
@@ -286,34 +154,39 @@ export function CopilotChat({
|
|
|
286
154
|
|
|
287
155
|
const renderToolCall = useRenderToolCall();
|
|
288
156
|
|
|
289
|
-
//
|
|
290
|
-
//
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
157
|
+
// useInterrupt publishes its element to core unless renderInChat is false.
|
|
158
|
+
// Draw it after the last message, as the web message view does.
|
|
159
|
+
const [interruptElement, setInterruptElement] =
|
|
160
|
+
useState<React.ReactElement | null>(null);
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
setInterruptElement(copilotkit.interruptElement);
|
|
163
|
+
const subscription = copilotkit.subscribe({
|
|
164
|
+
onInterruptElementChanged: ({ interruptElement: element }) => {
|
|
165
|
+
setInterruptElement(element);
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
return () => subscription.unsubscribe();
|
|
169
|
+
}, [copilotkit]);
|
|
170
|
+
|
|
171
|
+
// Recomputed every render: cheap, and the only honest dependency for the
|
|
172
|
+
// message-derived memos below. The array reference does not track content
|
|
173
|
+
// in either direction: core inserts tool results with an in-place `splice`,
|
|
174
|
+
// `addMessage` pushes, and the AG-UI apply pipeline replaces the array on
|
|
175
|
+
// every streamed delta.
|
|
176
|
+
const messagesKey = ɵmessagesFingerprint(messages);
|
|
177
|
+
|
|
178
|
+
// toolCallId -> tool result message, so a renderer reports "complete" with
|
|
179
|
+
// its `result`.
|
|
297
180
|
const toolMessages = useMemo(() => {
|
|
298
181
|
const byId = new Map<string, ToolMessage>();
|
|
299
|
-
for (const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
content?: unknown;
|
|
305
|
-
};
|
|
306
|
-
if (m.role === "tool" && m.toolCallId) {
|
|
307
|
-
byId.set(m.toolCallId, {
|
|
308
|
-
id: m.id ?? m.toolCallId,
|
|
309
|
-
role: "tool",
|
|
310
|
-
toolCallId: m.toolCallId,
|
|
311
|
-
content: toolResultContent(m.content, m.toolCallId),
|
|
312
|
-
});
|
|
313
|
-
}
|
|
182
|
+
for (const [toolCallId, message] of ɵgetToolResults(messages)) {
|
|
183
|
+
// A copy, so the tool-call renderer's memo sees content that was
|
|
184
|
+
// replaced in place on the same message. Core turns the content into
|
|
185
|
+
// the renderer's `result` string.
|
|
186
|
+
byId.set(toolCallId, { ...message });
|
|
314
187
|
}
|
|
315
188
|
return byId;
|
|
316
|
-
//
|
|
189
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- keyed on message content, see messagesKey
|
|
317
190
|
}, [messagesKey]);
|
|
318
191
|
|
|
319
192
|
// Build flat list items from messages
|
|
@@ -365,7 +238,7 @@ export function CopilotChat({
|
|
|
365
238
|
// in-place `splice` and `addMessage`'s push leave it untouched, while the
|
|
366
239
|
// AG-UI apply pipeline replaces it on every delta. Without this, an
|
|
367
240
|
// assistant message or tool call appended mid-run never reaches the list.
|
|
368
|
-
//
|
|
241
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- keyed on message content, see messagesKey
|
|
369
242
|
}, [messagesKey, isRunning]);
|
|
370
243
|
|
|
371
244
|
// Id of the trailing row. renderItem needs "is this the last row?" to place
|
|
@@ -388,47 +261,46 @@ export function CopilotChat({
|
|
|
388
261
|
[isRunning, lastItemId, renderToolCall, toolMessages],
|
|
389
262
|
);
|
|
390
263
|
|
|
391
|
-
//
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
console.error("[CopilotChat] runAgent failed:", err);
|
|
412
|
-
setError(message);
|
|
413
|
-
}
|
|
414
|
-
},
|
|
415
|
-
[isRunning, agent, copilotkit, onSendMessage],
|
|
264
|
+
// The send side lives in core: it builds the message, waits for the run in
|
|
265
|
+
// flight, and runs the agent. A failed run also shows under the list.
|
|
266
|
+
const agentRef = useRef(agent);
|
|
267
|
+
const inputTextRef = useRef(inputText);
|
|
268
|
+
useLayoutEffect(() => {
|
|
269
|
+
agentRef.current = agent;
|
|
270
|
+
inputTextRef.current = inputText;
|
|
271
|
+
});
|
|
272
|
+
const [submitter] = useState(() =>
|
|
273
|
+
ɵcreateChatSubmitter({
|
|
274
|
+
core: withRunErrorReporting(copilotkit, setError),
|
|
275
|
+
getAgent: () => agentRef.current,
|
|
276
|
+
input: {
|
|
277
|
+
get: () => inputTextRef.current,
|
|
278
|
+
set: (value) => {
|
|
279
|
+
inputTextRef.current = value;
|
|
280
|
+
setInputText(value);
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
}),
|
|
416
284
|
);
|
|
285
|
+
useEffect(() => () => submitter.showNotice(null), [submitter]);
|
|
417
286
|
|
|
418
287
|
// Send from the input field
|
|
419
|
-
const handleSend = useCallback(
|
|
288
|
+
const handleSend = useCallback(() => {
|
|
420
289
|
const text = inputText.trim();
|
|
421
290
|
if (!text) return;
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
291
|
+
setError(null);
|
|
292
|
+
onSendMessage?.(text);
|
|
293
|
+
void submitter.submit(text);
|
|
294
|
+
}, [inputText, onSendMessage, submitter]);
|
|
425
295
|
|
|
426
|
-
// Handle suggestion pill press
|
|
296
|
+
// Handle suggestion pill press. Leaves the typed text alone.
|
|
427
297
|
const handleSuggestion = useCallback(
|
|
428
298
|
(text: string) => {
|
|
429
|
-
|
|
299
|
+
setError(null);
|
|
300
|
+
onSendMessage?.(text);
|
|
301
|
+
void submitter.selectSuggestion({ message: text, isLoading: false });
|
|
430
302
|
},
|
|
431
|
-
[
|
|
303
|
+
[onSendMessage, submitter],
|
|
432
304
|
);
|
|
433
305
|
|
|
434
306
|
// Auto-scroll when content changes
|
|
@@ -522,6 +394,7 @@ export function CopilotChat({
|
|
|
522
394
|
contentContainerStyle={[styles.messageList, messageContainerStyle]}
|
|
523
395
|
onContentSizeChange={handleContentSizeChange}
|
|
524
396
|
ListEmptyComponent={emptyComponent}
|
|
397
|
+
ListFooterComponent={interruptElement}
|
|
525
398
|
/>
|
|
526
399
|
|
|
527
400
|
{error && (
|
|
@@ -2,9 +2,9 @@ import React from "react";
|
|
|
2
2
|
import {
|
|
3
3
|
StreamingMarkdownRenderer,
|
|
4
4
|
defaultMarkdownStyles as _streamingDefaultStyles,
|
|
5
|
-
} from "@copilotkit/markdown
|
|
6
|
-
import type { MarkdownStyle as StreamingMarkdownStyle } from "@copilotkit/markdown
|
|
7
|
-
import { warnUnsupportedRichSyntaxOnce } from "@copilotkit/markdown
|
|
5
|
+
} from "@copilotkit/markdown/react-native";
|
|
6
|
+
import type { MarkdownStyle as StreamingMarkdownStyle } from "@copilotkit/markdown/react-native";
|
|
7
|
+
import { warnUnsupportedRichSyntaxOnce } from "@copilotkit/markdown";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Style map for CopilotMarkdown. Each key maps a node type to a React Native
|
|
@@ -45,7 +45,7 @@ export interface CopilotMarkdownProps {
|
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* GFM markdown renderer for React Native. Delegates to the shared
|
|
48
|
-
* `StreamingMarkdownRenderer` from `@copilotkit/markdown
|
|
48
|
+
* `StreamingMarkdownRenderer` from `@copilotkit/markdown/react-native`.
|
|
49
49
|
*
|
|
50
50
|
* Public API is backward-compatible with the previous basic renderer:
|
|
51
51
|
* - `CopilotMarkdownProps` — same shape.
|
|
@@ -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
|
}));
|
|
@@ -320,7 +324,7 @@ describe("CopilotChat", () => {
|
|
|
320
324
|
expect(getByText("Network timeout")).toBeTruthy();
|
|
321
325
|
});
|
|
322
326
|
|
|
323
|
-
it("
|
|
327
|
+
it("gives every sent message its own id", async () => {
|
|
324
328
|
const { getByTestId } = render(<CopilotChat />);
|
|
325
329
|
const input = getByTestId("text-input");
|
|
326
330
|
const sendBtn = getByTestId("send-button");
|
|
@@ -340,7 +344,11 @@ describe("CopilotChat", () => {
|
|
|
340
344
|
});
|
|
341
345
|
|
|
342
346
|
const calls = hoisted.mockAgent.addMessage.mock.calls;
|
|
343
|
-
expect(calls[
|
|
344
|
-
|
|
347
|
+
expect(calls.map(([message]) => message.content)).toEqual([
|
|
348
|
+
"first",
|
|
349
|
+
"second",
|
|
350
|
+
]);
|
|
351
|
+
expect(calls[0][0].id).toEqual(expect.any(String));
|
|
352
|
+
expect(calls[0][0].id).not.toBe(calls[1][0].id);
|
|
345
353
|
});
|
|
346
354
|
});
|