@copilotkit/react-native 1.66.1-canary.rn-tool-render → 1.66.2-canary.1785866877
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/dist/RenderToolContext-CBNKcJqz.cjs.map +1 -1
- package/dist/RenderToolContext-D8M-JhD1.mjs.map +1 -1
- package/dist/components/index.cjs +6 -8
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.mjs +6 -8
- package/dist/components/index.mjs.map +1 -1
- package/dist/headless.cjs +9 -5
- package/dist/headless.d.cts +3 -3
- package/dist/headless.d.mts +3 -3
- package/dist/headless.mjs +3 -3
- package/dist/index.cjs +9 -5
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +3 -3
- package/dist/{useRenderToolCall-BToS3UWC.d.cts → useRenderTool-9YDIfZ0D.d.mts} +6 -92
- package/dist/{useRenderToolCall-DAvPBgpB.d.mts → useRenderTool-BugVTDoM.d.cts} +6 -92
- package/dist/{useRenderToolCall-DawGeFfu.cjs → useRenderTool-DLavcsVB.cjs} +1 -98
- package/dist/useRenderTool-DLavcsVB.cjs.map +1 -0
- package/dist/{useRenderToolCall-DjhQb8Ki.mjs → useRenderTool-DUqpS97B.mjs} +6 -91
- package/dist/useRenderTool-DUqpS97B.mjs.map +1 -0
- package/package.json +4 -4
- package/src/components/CopilotChat.tsx +12 -19
- package/src/headless.ts +1 -11
- package/src/hooks/RenderToolContext.tsx +2 -15
- package/src/hooks/index.ts +0 -6
- package/src/index.ts +1 -4
- package/dist/useRenderToolCall-DawGeFfu.cjs.map +0 -1
- package/dist/useRenderToolCall-DjhQb8Ki.mjs.map +0 -1
- package/src/hooks/__tests__/useRenderToolCall.test.tsx +0 -160
- package/src/hooks/useComponent.tsx +0 -69
- package/src/hooks/useRenderToolCall.ts +0 -75
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { useCallback } from "react";
|
|
2
|
-
import type { ReactElement } from "react";
|
|
3
|
-
import { partialJSONParse } from "@copilotkit/shared";
|
|
4
|
-
import { useCopilotKit } from "@copilotkit/react-core/v2/headless";
|
|
5
|
-
import { useRenderToolRegistry } from "./RenderToolContext";
|
|
6
|
-
|
|
7
|
-
/** A tool call as it appears on an assistant message. */
|
|
8
|
-
export interface RenderableToolCall {
|
|
9
|
-
id: string;
|
|
10
|
-
function: { name: string; arguments?: string };
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface RenderToolCallInput {
|
|
14
|
-
toolCall: RenderableToolCall;
|
|
15
|
-
/** The matching tool result message, when the call has produced one. */
|
|
16
|
-
toolMessage?: { content?: string } | undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Render a tool call using the render function registered for it, ANYWHERE in your app.
|
|
21
|
-
*
|
|
22
|
-
* `CopilotChat` renders tool calls inline in its own message list, which is the right default for a
|
|
23
|
-
* chat. But plenty of React Native surfaces are not chats — an in-car stage, a kiosk, a dashboard —
|
|
24
|
-
* and they still want the agent to drive the UI. Without this hook there was no public way to render
|
|
25
|
-
* a registered component outside the chat, which also made `useComponent` unusable on those surfaces.
|
|
26
|
-
*
|
|
27
|
-
* Arguments are partial-JSON-parsed, so a component paints as the agent writes the call
|
|
28
|
-
* (`status: "inProgress"`) and fills in as more arrives — the same progressive behaviour the web
|
|
29
|
-
* renderer has. Renderers therefore need to tolerate incomplete `args`.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```tsx
|
|
33
|
-
* const renderToolCall = useRenderToolCall();
|
|
34
|
-
*
|
|
35
|
-
* // panels the agent has called this turn, laid out however you like
|
|
36
|
-
* return <View>{toolCalls.map((tc) => renderToolCall({ toolCall: tc }))}</View>;
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export function useRenderToolCall(): (
|
|
40
|
-
input: RenderToolCallInput,
|
|
41
|
-
) => ReactElement | null {
|
|
42
|
-
// The registry Map, same source CopilotChat renders from — so a component registered once
|
|
43
|
-
// works in the chat and on a custom surface without registering twice.
|
|
44
|
-
const toolRenderers = useRenderToolRegistry();
|
|
45
|
-
const { executingToolCallIds } = useCopilotKit();
|
|
46
|
-
|
|
47
|
-
return useCallback(
|
|
48
|
-
({ toolCall, toolMessage }: RenderToolCallInput) => {
|
|
49
|
-
const renderer = toolRenderers.get(toolCall.function.name);
|
|
50
|
-
if (!renderer) return null;
|
|
51
|
-
|
|
52
|
-
const raw = toolCall.function.arguments || "{}";
|
|
53
|
-
const args = partialJSONParse(raw);
|
|
54
|
-
|
|
55
|
-
// Incomplete JSON means the model is still writing this call.
|
|
56
|
-
let argsComplete = true;
|
|
57
|
-
try {
|
|
58
|
-
JSON.parse(raw);
|
|
59
|
-
} catch {
|
|
60
|
-
argsComplete = false;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const status = !argsComplete
|
|
64
|
-
? "inProgress"
|
|
65
|
-
: toolMessage !== undefined
|
|
66
|
-
? "complete"
|
|
67
|
-
: executingToolCallIds?.has(toolCall.id)
|
|
68
|
-
? "executing"
|
|
69
|
-
: "complete";
|
|
70
|
-
|
|
71
|
-
return renderer({ args, status, result: toolMessage?.content });
|
|
72
|
-
},
|
|
73
|
-
[toolRenderers, executingToolCallIds],
|
|
74
|
-
);
|
|
75
|
-
}
|