@copilotkit/react-native 1.66.1-canary.rn-tool-render → 1.66.1

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.
@@ -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
- }