forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-render-tool.tsx
More file actions
184 lines (172 loc) · 5.17 KB
/
Copy pathuse-render-tool.tsx
File metadata and controls
184 lines (172 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { useEffect } from "react";
import type { StandardSchemaV1, InferSchemaOutput } from "@copilotkit/shared";
import { useCopilotKit } from "../context";
import { defineToolCallRenderer } from "../types/defineToolCallRenderer";
const EMPTY_DEPS: ReadonlyArray<unknown> = [];
export interface RenderToolInProgressProps<S extends StandardSchemaV1> {
name: string;
toolCallId: string;
parameters: Partial<InferSchemaOutput<S>>;
status: "inProgress";
result: undefined;
}
export interface RenderToolExecutingProps<S extends StandardSchemaV1> {
name: string;
toolCallId: string;
parameters: InferSchemaOutput<S>;
status: "executing";
result: undefined;
}
export interface RenderToolCompleteProps<S extends StandardSchemaV1> {
name: string;
toolCallId: string;
parameters: InferSchemaOutput<S>;
status: "complete";
result: string;
}
export type RenderToolProps<S extends StandardSchemaV1> =
| RenderToolInProgressProps<S>
| RenderToolExecutingProps<S>
| RenderToolCompleteProps<S>;
type RenderToolConfig<S extends StandardSchemaV1> = {
name: string;
parameters?: S;
render: (props: RenderToolProps<S>) => React.ReactElement;
agentId?: string;
};
/**
* Registers a wildcard (`"*"`) renderer for tool calls.
*
* The wildcard renderer is used as a fallback when no exact name-matched
* renderer is registered for a tool call.
*
* @param config - Wildcard renderer configuration.
* @param deps - Optional dependencies to refresh registration.
*
* @example
* ```tsx
* useRenderTool(
* {
* name: "*",
* render: ({ name, status }) => (
* <div>
* {status === "complete" ? "✓" : "⏳"} {name}
* </div>
* ),
* },
* [],
* );
* ```
*/
export function useRenderTool(
config: {
name: "*";
render: (props: any) => React.ReactElement;
agentId?: string;
},
deps?: ReadonlyArray<unknown>,
): void;
/**
* Registers a name-scoped renderer for tool calls.
*
* The provided `parameters` schema defines the typed shape of `props.parameters`
* in `render` for `executing` and `complete` states. Accepts any Standard Schema V1
* compatible library (Zod, Valibot, ArkType, etc.).
*
* @typeParam S - Schema type describing tool call parameters.
* @param config - Named renderer configuration.
* @param deps - Optional dependencies to refresh registration.
*
* @example
* ```tsx
* useRenderTool(
* {
* name: "searchDocs",
* parameters: z.object({ query: z.string() }),
* render: ({ status, parameters, result }) => {
* if (status === "inProgress") return <div>Preparing...</div>;
* if (status === "executing") return <div>Searching {parameters.query}</div>;
* return <div>{result}</div>;
* },
* },
* [],
* );
* ```
*/
export function useRenderTool<S extends StandardSchemaV1>(
config: {
name: string;
parameters: S;
render: (props: RenderToolProps<S>) => React.ReactElement;
agentId?: string;
},
deps?: ReadonlyArray<unknown>,
): void;
/**
* Registers a renderer entry in CopilotKit's `renderToolCalls` registry.
*
* Key behavior:
* - deduplicates by `agentId:name` (latest registration wins),
* - keeps renderer entries on cleanup so historical chat tool calls can still render,
* - refreshes registration when `deps` change.
*
* @typeParam S - Schema type describing tool call parameters.
* @param config - Renderer config for wildcard or named tools.
* @param deps - Optional dependencies to refresh registration.
*
* @example
* ```tsx
* useRenderTool(
* {
* name: "searchDocs",
* parameters: z.object({ query: z.string() }),
* render: ({ status, parameters, result }) => {
* if (status === "executing") return <div>Searching {parameters.query}</div>;
* if (status === "complete") return <div>{result}</div>;
* return <div>Preparing...</div>;
* },
* },
* [],
* );
* ```
*
* @example
* ```tsx
* useRenderTool(
* {
* name: "summarize",
* parameters: z.object({ text: z.string() }),
* agentId: "research-agent",
* render: ({ name, status }) => <div>{name}: {status}</div>,
* },
* [selectedAgentId],
* );
* ```
*/
export function useRenderTool<S extends StandardSchemaV1>(
config: RenderToolConfig<S>,
deps?: ReadonlyArray<unknown>,
): void {
const { copilotkit } = useCopilotKit();
const extraDeps = deps ?? EMPTY_DEPS;
useEffect(() => {
// Build the ReactToolCallRenderer via defineToolCallRenderer
const renderer =
config.name === "*" && !config.parameters
? defineToolCallRenderer({
name: "*",
render: (props) =>
config.render({ ...props, parameters: props.args }),
...(config.agentId ? { agentId: config.agentId } : {}),
})
: defineToolCallRenderer({
name: config.name,
args: config.parameters!,
render: (props) =>
config.render({ ...props, parameters: props.args }),
...(config.agentId ? { agentId: config.agentId } : {}),
});
copilotkit.addHookRenderToolCall(renderer);
// No cleanup removal — keeps renderer for chat history, same as useFrontendTool
}, [config.name, copilotkit, JSON.stringify(extraDeps)]);
}