@copilotkit/react-native 1.67.0 → 1.68.0

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.
Files changed (56) hide show
  1. package/dist/chunk-C0xms8kb.cjs +34 -0
  2. package/dist/components/index.cjs +146 -33
  3. package/dist/components/index.cjs.map +1 -1
  4. package/dist/components/index.mjs +144 -32
  5. package/dist/components/index.mjs.map +1 -1
  6. package/dist/headless.cjs +40 -4
  7. package/dist/headless.d.cts +3 -3
  8. package/dist/headless.d.mts +3 -3
  9. package/dist/headless.mjs +5 -4
  10. package/dist/index.cjs +43 -7
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +3 -3
  13. package/dist/index.d.mts +4 -4
  14. package/dist/index.mjs +3 -4
  15. package/dist/index.mjs.map +1 -1
  16. package/dist/polyfills/encoding.cjs +1 -1
  17. package/dist/polyfills/streams.cjs +1 -1
  18. package/dist/useRenderTool-BBxnOOeJ.d.mts +161 -0
  19. package/dist/useRenderTool-BWo11PCL.d.cts +161 -0
  20. package/dist/{useRenderTool-DLavcsVB.cjs → useRenderTool-CKEPq7MQ.cjs} +20 -34
  21. package/dist/useRenderTool-CKEPq7MQ.cjs.map +1 -0
  22. package/dist/{useRenderTool-DUqpS97B.mjs → useRenderTool-l7M-I8rT.mjs} +18 -33
  23. package/dist/useRenderTool-l7M-I8rT.mjs.map +1 -0
  24. package/package.json +9 -6
  25. package/scripts/__tests__/fixtures/stray-react-dom-headless.js +16 -0
  26. package/scripts/__tests__/fixtures/tiny-headless.js +10 -0
  27. package/scripts/__tests__/fixtures/warning-headless.js +13 -0
  28. package/scripts/__tests__/measure-headless.test.mjs +275 -0
  29. package/scripts/measure-headless.mjs +310 -0
  30. package/src/CopilotKitProvider.tsx +1 -2
  31. package/src/__mocks__/test-copilotkit.tsx +116 -0
  32. package/src/__mocks__/tool-fixtures.ts +50 -0
  33. package/src/__tests__/headless-entry-surface.test.ts +464 -46
  34. package/src/__tests__/headless-integration.test.tsx +12 -1
  35. package/src/__tests__/headless-value-exports.test.ts +369 -0
  36. package/src/components/CopilotChat.tsx +218 -32
  37. package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -126
  38. package/src/components/__tests__/CopilotChat.test.tsx +5 -49
  39. package/src/components/__tests__/CopilotChatToolCalls.test.tsx +468 -0
  40. package/src/headless.ts +36 -14
  41. package/src/hooks/__tests__/render-tool-call.integration.test.tsx +181 -0
  42. package/src/hooks/__tests__/useRenderTool.test.tsx +232 -267
  43. package/src/hooks/index.ts +1 -7
  44. package/src/hooks/render-tool-types.ts +55 -0
  45. package/src/hooks/useRenderTool.ts +29 -57
  46. package/src/index.ts +13 -5
  47. package/dist/RenderToolContext-CBNKcJqz.cjs +0 -126
  48. package/dist/RenderToolContext-CBNKcJqz.cjs.map +0 -1
  49. package/dist/RenderToolContext-D8M-JhD1.mjs +0 -75
  50. package/dist/RenderToolContext-D8M-JhD1.mjs.map +0 -1
  51. package/dist/useRenderTool-9YDIfZ0D.d.mts +0 -164
  52. package/dist/useRenderTool-BugVTDoM.d.cts +0 -164
  53. package/dist/useRenderTool-DLavcsVB.cjs.map +0 -1
  54. package/dist/useRenderTool-DUqpS97B.mjs.map +0 -1
  55. package/src/hooks/RenderToolContext.tsx +0 -150
  56. package/src/hooks/__tests__/RenderToolContext.test.tsx +0 -264
@@ -0,0 +1,181 @@
1
+ import React from "react";
2
+ import { render } from "@testing-library/react";
3
+ import { describe, it, expect } from "vitest";
4
+ import { z } from "zod";
5
+ import type { ToolMessage } from "@ag-ui/client";
6
+ import { useRenderTool } from "../useRenderTool";
7
+ import type { RenderToolProps } from "../render-tool-types";
8
+ import { useComponent, useRenderToolCall } from "../../headless";
9
+ import { TestCopilotKit } from "../../__mocks__/test-copilotkit";
10
+ import { toolMessage } from "../../__mocks__/tool-fixtures";
11
+
12
+ /**
13
+ * Regression assertions ported from PR #6346 (David McKay). That PR verified
14
+ * these against RN's own registry with `vi.mock`; here they run through the real
15
+ * CopilotKitCoreReact registry, so they also prove the convergence itself.
16
+ */
17
+
18
+ /** Args the probe's tool declares, so its render props are typed rather than `any`. */
19
+ type AboutArgs = { dataId: string };
20
+
21
+ /**
22
+ * Renders one tool call and reports the props the registered renderer saw.
23
+ *
24
+ * `resultMessage` is a full `ToolMessage`, NOT a `{ content }` stub: the
25
+ * `toolCallId` it carries is what production correlates a result to a call by.
26
+ * `useRenderToolCall` renders whatever message it is handed and never checks
27
+ * that id itself — the map that does is `CopilotChat`'s, exercised in
28
+ * `components/__tests__/CopilotChatToolCalls.test.tsx` — so a stub here would
29
+ * assert a `result` arriving for a tool call nothing ever tied it to.
30
+ */
31
+ function renderCall(
32
+ args: string | undefined,
33
+ resultMessage?: ToolMessage,
34
+ ): RenderToolProps<AboutArgs> {
35
+ // A mutable box rather than a `let`, so the captured value keeps its declared
36
+ // type after `render()` instead of being narrowed to the `null` initializer.
37
+ const seen: { props?: RenderToolProps<AboutArgs> } = {};
38
+
39
+ function Probe() {
40
+ useRenderTool<AboutArgs>({
41
+ name: "showAbout",
42
+ description: "Show about",
43
+ parameters: z.object({ dataId: z.string() }),
44
+ render: (props) => {
45
+ seen.props = props;
46
+ return null;
47
+ },
48
+ });
49
+ const renderToolCall = useRenderToolCall();
50
+ return (
51
+ <>
52
+ {renderToolCall({
53
+ toolCall: {
54
+ id: "tc-1",
55
+ type: "function",
56
+ // The `arguments` field is a `string` in the AG-UI type; `undefined`
57
+ // is the degenerate runtime input one test below deliberately feeds
58
+ // it, which the type cannot express.
59
+ function: { name: "showAbout", arguments: args as string },
60
+ },
61
+ toolMessage: resultMessage,
62
+ })}
63
+ </>
64
+ );
65
+ }
66
+
67
+ render(
68
+ <TestCopilotKit messages={[]}>
69
+ <Probe />
70
+ </TestCopilotKit>,
71
+ );
72
+
73
+ if (!seen.props) {
74
+ throw new Error("the registered renderer was never invoked");
75
+ }
76
+ return seen.props;
77
+ }
78
+
79
+ describe("useRenderToolCall on React Native", () => {
80
+ it("renders a registered component for a tool call", () => {
81
+ // `renderCall` throws if the renderer never ran, so reaching this line is
82
+ // itself the "it rendered" assertion.
83
+ const props = renderCall('{"dataId":"zosch"}');
84
+ expect(props.args).toEqual({ dataId: "zosch" });
85
+ });
86
+
87
+ it("exposes PARTIAL args with status inProgress while arguments are incomplete", () => {
88
+ const props = renderCall('{"dataId":"neues-mus');
89
+ expect(props.status).toBe("inProgress");
90
+ expect(props.args).toEqual({ dataId: "neues-mus" });
91
+ });
92
+
93
+ it("does not throw on a fragment with no complete key yet", () => {
94
+ const props = renderCall('{"places":[{"title":"Roof');
95
+ expect(props.status).toBe("inProgress");
96
+ expect(props.args).toBeTypeOf("object");
97
+ });
98
+
99
+ it("reports complete and passes the result through when a tool message exists", () => {
100
+ const props = renderCall(
101
+ '{"dataId":"zosch"}',
102
+ // Correlated to the tool call this probe renders ("tc-1"), so the fixture
103
+ // states the relationship the assertion below depends on instead of
104
+ // leaving it to a cast.
105
+ toolMessage("tc-1", "ok"),
106
+ );
107
+ expect(props.status).toBe("complete");
108
+ expect(props.result).toBe("ok");
109
+ // Ties the two together: the id the renderer reports is the same id the
110
+ // result above was filed under, so this really is that call's result.
111
+ expect(props.toolCallId).toBe("tc-1");
112
+ });
113
+
114
+ it("treats absent arguments as an empty object, not a crash", () => {
115
+ const props = renderCall(undefined);
116
+ expect(props.args).toEqual({});
117
+ });
118
+
119
+ it("returns null for a tool with no registered renderer", () => {
120
+ let out: unknown = "unset";
121
+ function Probe() {
122
+ const renderToolCall = useRenderToolCall();
123
+ out = renderToolCall({
124
+ toolCall: {
125
+ id: "x",
126
+ type: "function",
127
+ function: { name: "notRegistered", arguments: "{}" },
128
+ },
129
+ });
130
+ return null;
131
+ }
132
+ render(
133
+ <TestCopilotKit messages={[]}>
134
+ <Probe />
135
+ </TestCopilotKit>,
136
+ );
137
+ expect(out).toBeNull();
138
+ });
139
+ });
140
+
141
+ describe("useComponent on React Native", () => {
142
+ it("renders on a custom surface — the bug this convergence fixes", () => {
143
+ // Before: react-core's useComponent wrote to core's registry while RN's chat
144
+ // read RN's Map, so a component registered this way rendered NOWHERE on RN.
145
+ // Same mutable-box reason as `renderCall` above: a `let` assigned only inside
146
+ // the render callback stays narrowed to its initializer for the type-checker.
147
+ const received: { props?: { city: string } } = {};
148
+
149
+ function Probe() {
150
+ useComponent({
151
+ name: "showCity",
152
+ parameters: z.object({ city: z.string() }),
153
+ render: (props) => {
154
+ received.props = props;
155
+ return null;
156
+ },
157
+ });
158
+ const renderToolCall = useRenderToolCall();
159
+ return (
160
+ <>
161
+ {renderToolCall({
162
+ toolCall: {
163
+ id: "tc-2",
164
+ type: "function",
165
+ function: { name: "showCity", arguments: '{"city":"Berlin"}' },
166
+ },
167
+ })}
168
+ </>
169
+ );
170
+ }
171
+
172
+ render(
173
+ <TestCopilotKit messages={[]}>
174
+ <Probe />
175
+ </TestCopilotKit>,
176
+ );
177
+
178
+ // Props are the tool arguments, spread — the component needs no CopilotKit knowledge.
179
+ expect(received.props).toMatchObject({ city: "Berlin" });
180
+ });
181
+ });