@copilotkit/react-native 1.57.4 → 1.58.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.
- package/USAGE.md +116 -0
- package/dist/RenderToolContext-CBNKcJqz.cjs +126 -0
- package/dist/RenderToolContext-CBNKcJqz.cjs.map +1 -0
- package/dist/RenderToolContext-CszHyes6.mjs +75 -0
- package/dist/RenderToolContext-CszHyes6.mjs.map +1 -0
- package/dist/components/index.cjs +722 -0
- package/dist/components/index.cjs.map +1 -0
- package/dist/components/index.d.cts +203 -0
- package/dist/components/index.d.mts +203 -0
- package/dist/components/index.mjs +714 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/index.cjs +53 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +102 -6
- package/dist/index.d.mts +100 -4
- package/dist/index.mjs +49 -3
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/package.json +24 -4
- package/src/CopilotKitProvider.tsx +2 -1
- package/src/__mocks__/react-native-streamdown.ts +16 -0
- package/src/__mocks__/react-native.ts +52 -0
- package/src/__tests__/headless-integration.test.tsx +16 -0
- package/src/components/CopilotChat.tsx +465 -0
- package/src/components/CopilotModal.tsx +210 -0
- package/src/components/Markdown.tsx +124 -0
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +418 -0
- package/src/components/__tests__/CopilotChat.test.tsx +390 -0
- package/src/components/__tests__/CopilotModal.test.tsx +257 -0
- package/src/components/__tests__/Markdown.test.tsx +106 -0
- package/src/components/index.ts +18 -0
- package/src/components/messages/AssistantMessage.tsx +73 -0
- package/src/components/messages/TypingIndicator.tsx +103 -0
- package/src/components/messages/UserMessage.tsx +68 -0
- package/src/components/messages/__tests__/messages-edge-cases.test.tsx +179 -0
- package/src/components/messages/__tests__/messages.test.tsx +189 -0
- package/src/components/messages/__tests__/utils.test.ts +39 -0
- package/src/components/messages/index.ts +8 -0
- package/src/components/messages/utils.ts +11 -0
- package/src/hooks/RenderToolContext.tsx +150 -0
- package/src/hooks/__tests__/RenderToolContext.test.tsx +264 -0
- package/src/hooks/__tests__/useRenderTool.test.tsx +334 -0
- package/src/hooks/index.ts +9 -0
- package/src/hooks/useRenderTool.ts +96 -0
- package/src/index.ts +10 -3
- package/tsdown.config.ts +9 -0
- package/vitest.config.mjs +10 -0
package/USAGE.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# @copilotkit/react-native — Usage
|
|
2
|
+
|
|
3
|
+
## Prerequisites
|
|
4
|
+
|
|
5
|
+
Install all required peer dependencies:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install react react-native @gorhom/bottom-sheet react-native-gesture-handler react-native-reanimated react-native-streamdown
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`@gorhom/bottom-sheet`, `react-native-gesture-handler`, `react-native-reanimated`, and `react-native-streamdown` are required peer dependencies for the UI components.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import "@copilotkit/react-native/polyfills";
|
|
17
|
+
import {
|
|
18
|
+
CopilotKitProvider,
|
|
19
|
+
CopilotChat,
|
|
20
|
+
useRenderTool,
|
|
21
|
+
} from "@copilotkit/react-native";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
function App() {
|
|
25
|
+
return (
|
|
26
|
+
<CopilotKitProvider runtimeUrl="https://your-server/api/copilotkit">
|
|
27
|
+
<ChatScreen />
|
|
28
|
+
</CopilotKitProvider>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function ChatScreen() {
|
|
33
|
+
// parameters accepts any StandardSchemaV1-compatible schema (Zod, Valibot, ArkType, etc.)
|
|
34
|
+
useRenderTool({
|
|
35
|
+
name: "showWeather",
|
|
36
|
+
description: "Show weather info",
|
|
37
|
+
parameters: z.object({ city: z.string() }),
|
|
38
|
+
render: ({ args }) => <WeatherCard city={args.city} />,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return <CopilotChat placeholder="Ask anything..." />;
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Available Components
|
|
46
|
+
|
|
47
|
+
### CopilotChat
|
|
48
|
+
|
|
49
|
+
Inline chat panel. Renders a message list with an input bar.
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { CopilotChat } from "@copilotkit/react-native";
|
|
53
|
+
|
|
54
|
+
<CopilotChat placeholder="Type a message..." />;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### CopilotModal
|
|
58
|
+
|
|
59
|
+
Modal chat overlay. Open/close programmatically via a ref.
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import { CopilotModal, type CopilotModalRef } from "@copilotkit/react-native";
|
|
63
|
+
import { useRef } from "react";
|
|
64
|
+
|
|
65
|
+
const modalRef = useRef<CopilotModalRef>(null);
|
|
66
|
+
|
|
67
|
+
<CopilotModal ref={modalRef} headerTitle="Assistant" />;
|
|
68
|
+
|
|
69
|
+
// Open it:
|
|
70
|
+
modalRef.current?.open();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### CopilotMarkdown
|
|
74
|
+
|
|
75
|
+
Renders Markdown text with sensible React Native styling.
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { CopilotMarkdown } from "@copilotkit/react-native";
|
|
79
|
+
|
|
80
|
+
<CopilotMarkdown content="**Hello** from CopilotKit!" />;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### AssistantMessage / UserMessage
|
|
84
|
+
|
|
85
|
+
Individual message bubbles. Useful when building a custom chat UI.
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { AssistantMessage, UserMessage } from "@copilotkit/react-native";
|
|
89
|
+
|
|
90
|
+
<UserMessage content="What's the weather?" />
|
|
91
|
+
<AssistantMessage content="It's sunny!" isLoading={false} />
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Hooks
|
|
95
|
+
|
|
96
|
+
### useRenderTool
|
|
97
|
+
|
|
98
|
+
Register a React Native component to render inline when the agent calls a tool.
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
// parameters accepts any StandardSchemaV1-compatible schema (Zod, Valibot, ArkType, etc.)
|
|
102
|
+
useRenderTool({
|
|
103
|
+
name: "showChart",
|
|
104
|
+
description: "Display a chart",
|
|
105
|
+
parameters: z.object({ data: z.record(z.unknown()) }),
|
|
106
|
+
render: ({ args }) => <ChartView data={args.data} />,
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Alternative Import Path
|
|
111
|
+
|
|
112
|
+
Components can also be imported from the `/components` subpath:
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { CopilotChat, CopilotModal } from "@copilotkit/react-native/components";
|
|
116
|
+
```
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
let react = require("react");
|
|
29
|
+
react = __toESM(react);
|
|
30
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
+
|
|
32
|
+
//#region src/hooks/RenderToolContext.tsx
|
|
33
|
+
function createRegistryStore() {
|
|
34
|
+
return {
|
|
35
|
+
registry: /* @__PURE__ */ new Map(),
|
|
36
|
+
version: 0,
|
|
37
|
+
listeners: /* @__PURE__ */ new Set()
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function emitChange(store) {
|
|
41
|
+
store.version++;
|
|
42
|
+
store.registry = new Map(store.registry);
|
|
43
|
+
for (const listener of store.listeners) listener();
|
|
44
|
+
}
|
|
45
|
+
const RenderToolCtx = (0, react.createContext)(null);
|
|
46
|
+
/**
|
|
47
|
+
* Provider that maintains the render tool registry.
|
|
48
|
+
* Should be nested inside CopilotKitProvider.
|
|
49
|
+
*/
|
|
50
|
+
function RenderToolProvider({ children }) {
|
|
51
|
+
const storeRef = (0, react.useRef)(createRegistryStore());
|
|
52
|
+
const register = (0, react.useCallback)((name, render) => {
|
|
53
|
+
const store = storeRef.current;
|
|
54
|
+
store.registry.set(name, render);
|
|
55
|
+
emitChange(store);
|
|
56
|
+
return () => {
|
|
57
|
+
if (store.registry.get(name) === render) {
|
|
58
|
+
store.registry.delete(name);
|
|
59
|
+
emitChange(store);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}, []);
|
|
63
|
+
const value = (0, react.useMemo)(() => ({
|
|
64
|
+
store: storeRef.current,
|
|
65
|
+
register
|
|
66
|
+
}), [register]);
|
|
67
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderToolCtx.Provider, {
|
|
68
|
+
value,
|
|
69
|
+
children
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns the current render tool registry (a Map of tool name to render function).
|
|
74
|
+
* Re-renders when the registry changes.
|
|
75
|
+
*
|
|
76
|
+
* @throws if called outside of RenderToolProvider
|
|
77
|
+
*/
|
|
78
|
+
function useRenderToolRegistry() {
|
|
79
|
+
const ctx = (0, react.useContext)(RenderToolCtx);
|
|
80
|
+
if (!ctx) throw new Error("useRenderToolRegistry must be used within a RenderToolProvider");
|
|
81
|
+
const { store } = ctx;
|
|
82
|
+
return (0, react.useSyncExternalStore)((onStoreChange) => {
|
|
83
|
+
store.listeners.add(onStoreChange);
|
|
84
|
+
return () => {
|
|
85
|
+
store.listeners.delete(onStoreChange);
|
|
86
|
+
};
|
|
87
|
+
}, () => store.registry, () => store.registry);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Internal hook used by useRenderTool to register a render function.
|
|
91
|
+
* Not exported from the package — consumers use useRenderTool instead.
|
|
92
|
+
*
|
|
93
|
+
* @throws if called outside of RenderToolProvider
|
|
94
|
+
*/
|
|
95
|
+
function useRenderToolContext() {
|
|
96
|
+
const ctx = (0, react.useContext)(RenderToolCtx);
|
|
97
|
+
if (!ctx) throw new Error("useRenderTool must be used within a RenderToolProvider (inside CopilotKitProvider)");
|
|
98
|
+
return ctx;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
Object.defineProperty(exports, 'RenderToolProvider', {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
get: function () {
|
|
105
|
+
return RenderToolProvider;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(exports, '__toESM', {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
get: function () {
|
|
111
|
+
return __toESM;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(exports, 'useRenderToolContext', {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
get: function () {
|
|
117
|
+
return useRenderToolContext;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(exports, 'useRenderToolRegistry', {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function () {
|
|
123
|
+
return useRenderToolRegistry;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=RenderToolContext-CBNKcJqz.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RenderToolContext-CBNKcJqz.cjs","names":[],"sources":["../src/hooks/RenderToolContext.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * Props passed to a render tool function.\n */\nexport interface RenderToolProps<T = Record<string, unknown>> {\n args: T;\n status: \"executing\" | \"complete\";\n result?: string;\n}\n\n/**\n * A render function that returns a React Native element for a tool call.\n * Returns `ReactElement | null` (not ReactNode) because React Native's\n * FlatList cannot render strings or portals.\n */\nexport type RenderToolFunction<T = Record<string, unknown>> = (\n props: RenderToolProps<T>,\n) => React.ReactElement | null;\n\n/**\n * The registry maps tool names to their render functions.\n */\nexport type RenderToolRegistry = Map<string, RenderToolFunction>;\n\n/**\n * Internal store that notifies subscribers when the registry changes.\n * This avoids unnecessary re-renders of the entire tree when a single\n * tool's render function is registered or removed.\n */\ninterface RegistryStore {\n registry: RenderToolRegistry;\n version: number;\n listeners: Set<() => void>;\n}\n\nfunction createRegistryStore(): RegistryStore {\n return {\n registry: new Map(),\n version: 0,\n listeners: new Set(),\n };\n}\n\nfunction emitChange(store: RegistryStore) {\n store.version++;\n // Create a new Map so useSyncExternalStore detects the reference change\n store.registry = new Map(store.registry);\n for (const listener of store.listeners) {\n listener();\n }\n}\n\ninterface RenderToolContextValue {\n store: RegistryStore;\n register: (name: string, render: RenderToolFunction) => () => void;\n}\n\nconst RenderToolCtx = createContext<RenderToolContextValue | null>(null);\n\n/**\n * Provider that maintains the render tool registry.\n * Should be nested inside CopilotKitProvider.\n */\nexport function RenderToolProvider({\n children,\n}: {\n children: React.ReactNode;\n}) {\n const storeRef = useRef<RegistryStore>(createRegistryStore());\n\n const register = useCallback(\n (name: string, render: RenderToolFunction): (() => void) => {\n const store = storeRef.current;\n store.registry.set(name, render);\n emitChange(store);\n\n // Return unregister function\n return () => {\n // Only delete if the current render function is the one we registered\n if (store.registry.get(name) === render) {\n store.registry.delete(name);\n emitChange(store);\n }\n };\n },\n [],\n );\n\n const value = useMemo<RenderToolContextValue>(\n () => ({ store: storeRef.current, register }),\n [register],\n );\n\n return (\n <RenderToolCtx.Provider value={value}>{children}</RenderToolCtx.Provider>\n );\n}\n\n/**\n * Returns the current render tool registry (a Map of tool name to render function).\n * Re-renders when the registry changes.\n *\n * @throws if called outside of RenderToolProvider\n */\nexport function useRenderToolRegistry(): RenderToolRegistry {\n const ctx = useContext(RenderToolCtx);\n if (!ctx) {\n throw new Error(\n \"useRenderToolRegistry must be used within a RenderToolProvider\",\n );\n }\n\n const { store } = ctx;\n\n // Subscribe to registry changes via useSyncExternalStore for tear-safe reads\n return useSyncExternalStore(\n (onStoreChange) => {\n store.listeners.add(onStoreChange);\n return () => {\n store.listeners.delete(onStoreChange);\n };\n },\n () => store.registry,\n () => store.registry,\n );\n}\n\n/**\n * Internal hook used by useRenderTool to register a render function.\n * Not exported from the package — consumers use useRenderTool instead.\n *\n * @throws if called outside of RenderToolProvider\n */\nexport function useRenderToolContext() {\n const ctx = useContext(RenderToolCtx);\n if (!ctx) {\n throw new Error(\n \"useRenderTool must be used within a RenderToolProvider (inside CopilotKitProvider)\",\n );\n }\n return ctx;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAS,sBAAqC;AAC5C,QAAO;EACL,0BAAU,IAAI,KAAK;EACnB,SAAS;EACT,2BAAW,IAAI,KAAK;EACrB;;AAGH,SAAS,WAAW,OAAsB;AACxC,OAAM;AAEN,OAAM,WAAW,IAAI,IAAI,MAAM,SAAS;AACxC,MAAK,MAAM,YAAY,MAAM,UAC3B,WAAU;;AASd,MAAM,yCAA6D,KAAK;;;;;AAMxE,SAAgB,mBAAmB,EACjC,YAGC;CACD,MAAM,6BAAiC,qBAAqB,CAAC;CAE7D,MAAM,mCACH,MAAc,WAA6C;EAC1D,MAAM,QAAQ,SAAS;AACvB,QAAM,SAAS,IAAI,MAAM,OAAO;AAChC,aAAW,MAAM;AAGjB,eAAa;AAEX,OAAI,MAAM,SAAS,IAAI,KAAK,KAAK,QAAQ;AACvC,UAAM,SAAS,OAAO,KAAK;AAC3B,eAAW,MAAM;;;IAIvB,EAAE,CACH;CAED,MAAM,kCACG;EAAE,OAAO,SAAS;EAAS;EAAU,GAC5C,CAAC,SAAS,CACX;AAED,QACE,2CAAC,cAAc;EAAgB;EAAQ;GAAkC;;;;;;;;AAU7E,SAAgB,wBAA4C;CAC1D,MAAM,4BAAiB,cAAc;AACrC,KAAI,CAAC,IACH,OAAM,IAAI,MACR,iEACD;CAGH,MAAM,EAAE,UAAU;AAGlB,yCACG,kBAAkB;AACjB,QAAM,UAAU,IAAI,cAAc;AAClC,eAAa;AACX,SAAM,UAAU,OAAO,cAAc;;UAGnC,MAAM,gBACN,MAAM,SACb;;;;;;;;AASH,SAAgB,uBAAuB;CACrC,MAAM,4BAAiB,cAAc;AACrC,KAAI,CAAC,IACH,OAAM,IAAI,MACR,qFACD;AAEH,QAAO"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import React, { createContext, useCallback, useContext, useMemo, useRef, useSyncExternalStore } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks/RenderToolContext.tsx
|
|
5
|
+
function createRegistryStore() {
|
|
6
|
+
return {
|
|
7
|
+
registry: /* @__PURE__ */ new Map(),
|
|
8
|
+
version: 0,
|
|
9
|
+
listeners: /* @__PURE__ */ new Set()
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function emitChange(store) {
|
|
13
|
+
store.version++;
|
|
14
|
+
store.registry = new Map(store.registry);
|
|
15
|
+
for (const listener of store.listeners) listener();
|
|
16
|
+
}
|
|
17
|
+
const RenderToolCtx = createContext(null);
|
|
18
|
+
/**
|
|
19
|
+
* Provider that maintains the render tool registry.
|
|
20
|
+
* Should be nested inside CopilotKitProvider.
|
|
21
|
+
*/
|
|
22
|
+
function RenderToolProvider({ children }) {
|
|
23
|
+
const storeRef = useRef(createRegistryStore());
|
|
24
|
+
const register = useCallback((name, render) => {
|
|
25
|
+
const store = storeRef.current;
|
|
26
|
+
store.registry.set(name, render);
|
|
27
|
+
emitChange(store);
|
|
28
|
+
return () => {
|
|
29
|
+
if (store.registry.get(name) === render) {
|
|
30
|
+
store.registry.delete(name);
|
|
31
|
+
emitChange(store);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}, []);
|
|
35
|
+
const value = useMemo(() => ({
|
|
36
|
+
store: storeRef.current,
|
|
37
|
+
register
|
|
38
|
+
}), [register]);
|
|
39
|
+
return /* @__PURE__ */ jsx(RenderToolCtx.Provider, {
|
|
40
|
+
value,
|
|
41
|
+
children
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns the current render tool registry (a Map of tool name to render function).
|
|
46
|
+
* Re-renders when the registry changes.
|
|
47
|
+
*
|
|
48
|
+
* @throws if called outside of RenderToolProvider
|
|
49
|
+
*/
|
|
50
|
+
function useRenderToolRegistry() {
|
|
51
|
+
const ctx = useContext(RenderToolCtx);
|
|
52
|
+
if (!ctx) throw new Error("useRenderToolRegistry must be used within a RenderToolProvider");
|
|
53
|
+
const { store } = ctx;
|
|
54
|
+
return useSyncExternalStore((onStoreChange) => {
|
|
55
|
+
store.listeners.add(onStoreChange);
|
|
56
|
+
return () => {
|
|
57
|
+
store.listeners.delete(onStoreChange);
|
|
58
|
+
};
|
|
59
|
+
}, () => store.registry, () => store.registry);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Internal hook used by useRenderTool to register a render function.
|
|
63
|
+
* Not exported from the package — consumers use useRenderTool instead.
|
|
64
|
+
*
|
|
65
|
+
* @throws if called outside of RenderToolProvider
|
|
66
|
+
*/
|
|
67
|
+
function useRenderToolContext() {
|
|
68
|
+
const ctx = useContext(RenderToolCtx);
|
|
69
|
+
if (!ctx) throw new Error("useRenderTool must be used within a RenderToolProvider (inside CopilotKitProvider)");
|
|
70
|
+
return ctx;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { useRenderToolContext as n, useRenderToolRegistry as r, RenderToolProvider as t };
|
|
75
|
+
//# sourceMappingURL=RenderToolContext-CszHyes6.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RenderToolContext-CszHyes6.mjs","names":[],"sources":["../src/hooks/RenderToolContext.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * Props passed to a render tool function.\n */\nexport interface RenderToolProps<T = Record<string, unknown>> {\n args: T;\n status: \"executing\" | \"complete\";\n result?: string;\n}\n\n/**\n * A render function that returns a React Native element for a tool call.\n * Returns `ReactElement | null` (not ReactNode) because React Native's\n * FlatList cannot render strings or portals.\n */\nexport type RenderToolFunction<T = Record<string, unknown>> = (\n props: RenderToolProps<T>,\n) => React.ReactElement | null;\n\n/**\n * The registry maps tool names to their render functions.\n */\nexport type RenderToolRegistry = Map<string, RenderToolFunction>;\n\n/**\n * Internal store that notifies subscribers when the registry changes.\n * This avoids unnecessary re-renders of the entire tree when a single\n * tool's render function is registered or removed.\n */\ninterface RegistryStore {\n registry: RenderToolRegistry;\n version: number;\n listeners: Set<() => void>;\n}\n\nfunction createRegistryStore(): RegistryStore {\n return {\n registry: new Map(),\n version: 0,\n listeners: new Set(),\n };\n}\n\nfunction emitChange(store: RegistryStore) {\n store.version++;\n // Create a new Map so useSyncExternalStore detects the reference change\n store.registry = new Map(store.registry);\n for (const listener of store.listeners) {\n listener();\n }\n}\n\ninterface RenderToolContextValue {\n store: RegistryStore;\n register: (name: string, render: RenderToolFunction) => () => void;\n}\n\nconst RenderToolCtx = createContext<RenderToolContextValue | null>(null);\n\n/**\n * Provider that maintains the render tool registry.\n * Should be nested inside CopilotKitProvider.\n */\nexport function RenderToolProvider({\n children,\n}: {\n children: React.ReactNode;\n}) {\n const storeRef = useRef<RegistryStore>(createRegistryStore());\n\n const register = useCallback(\n (name: string, render: RenderToolFunction): (() => void) => {\n const store = storeRef.current;\n store.registry.set(name, render);\n emitChange(store);\n\n // Return unregister function\n return () => {\n // Only delete if the current render function is the one we registered\n if (store.registry.get(name) === render) {\n store.registry.delete(name);\n emitChange(store);\n }\n };\n },\n [],\n );\n\n const value = useMemo<RenderToolContextValue>(\n () => ({ store: storeRef.current, register }),\n [register],\n );\n\n return (\n <RenderToolCtx.Provider value={value}>{children}</RenderToolCtx.Provider>\n );\n}\n\n/**\n * Returns the current render tool registry (a Map of tool name to render function).\n * Re-renders when the registry changes.\n *\n * @throws if called outside of RenderToolProvider\n */\nexport function useRenderToolRegistry(): RenderToolRegistry {\n const ctx = useContext(RenderToolCtx);\n if (!ctx) {\n throw new Error(\n \"useRenderToolRegistry must be used within a RenderToolProvider\",\n );\n }\n\n const { store } = ctx;\n\n // Subscribe to registry changes via useSyncExternalStore for tear-safe reads\n return useSyncExternalStore(\n (onStoreChange) => {\n store.listeners.add(onStoreChange);\n return () => {\n store.listeners.delete(onStoreChange);\n };\n },\n () => store.registry,\n () => store.registry,\n );\n}\n\n/**\n * Internal hook used by useRenderTool to register a render function.\n * Not exported from the package — consumers use useRenderTool instead.\n *\n * @throws if called outside of RenderToolProvider\n */\nexport function useRenderToolContext() {\n const ctx = useContext(RenderToolCtx);\n if (!ctx) {\n throw new Error(\n \"useRenderTool must be used within a RenderToolProvider (inside CopilotKitProvider)\",\n );\n }\n return ctx;\n}\n"],"mappings":";;;;AA2CA,SAAS,sBAAqC;AAC5C,QAAO;EACL,0BAAU,IAAI,KAAK;EACnB,SAAS;EACT,2BAAW,IAAI,KAAK;EACrB;;AAGH,SAAS,WAAW,OAAsB;AACxC,OAAM;AAEN,OAAM,WAAW,IAAI,IAAI,MAAM,SAAS;AACxC,MAAK,MAAM,YAAY,MAAM,UAC3B,WAAU;;AASd,MAAM,gBAAgB,cAA6C,KAAK;;;;;AAMxE,SAAgB,mBAAmB,EACjC,YAGC;CACD,MAAM,WAAW,OAAsB,qBAAqB,CAAC;CAE7D,MAAM,WAAW,aACd,MAAc,WAA6C;EAC1D,MAAM,QAAQ,SAAS;AACvB,QAAM,SAAS,IAAI,MAAM,OAAO;AAChC,aAAW,MAAM;AAGjB,eAAa;AAEX,OAAI,MAAM,SAAS,IAAI,KAAK,KAAK,QAAQ;AACvC,UAAM,SAAS,OAAO,KAAK;AAC3B,eAAW,MAAM;;;IAIvB,EAAE,CACH;CAED,MAAM,QAAQ,eACL;EAAE,OAAO,SAAS;EAAS;EAAU,GAC5C,CAAC,SAAS,CACX;AAED,QACE,oBAAC,cAAc;EAAgB;EAAQ;GAAkC;;;;;;;;AAU7E,SAAgB,wBAA4C;CAC1D,MAAM,MAAM,WAAW,cAAc;AACrC,KAAI,CAAC,IACH,OAAM,IAAI,MACR,iEACD;CAGH,MAAM,EAAE,UAAU;AAGlB,QAAO,sBACJ,kBAAkB;AACjB,QAAM,UAAU,IAAI,cAAc;AAClC,eAAa;AACX,SAAM,UAAU,OAAO,cAAc;;UAGnC,MAAM,gBACN,MAAM,SACb;;;;;;;;AASH,SAAgB,uBAAuB;CACrC,MAAM,MAAM,WAAW,cAAc;AACrC,KAAI,CAAC,IACH,OAAM,IAAI,MACR,qFACD;AAEH,QAAO"}
|