@copilotkit/runtime 1.71.0 → 1.71.2
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/README.md +22 -0
- package/dist/agent/index.cjs +15 -3
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +1 -0
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts +1 -0
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +15 -3
- package/dist/agent/index.mjs.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +9 -1
- package/dist/index.d.mts +9 -1
- package/dist/index.mjs +2 -2
- package/dist/lib/telemetry-client.cjs.map +1 -1
- package/dist/lib/telemetry-client.mjs.map +1 -1
- package/dist/package.cjs +8 -6
- package/dist/package.mjs +8 -6
- package/dist/service-adapters/anthropic/anthropic-adapter.cjs +59 -25
- package/dist/service-adapters/anthropic/anthropic-adapter.cjs.map +1 -1
- package/dist/service-adapters/anthropic/anthropic-adapter.d.cts +30 -1
- package/dist/service-adapters/anthropic/anthropic-adapter.d.cts.map +1 -1
- package/dist/service-adapters/anthropic/anthropic-adapter.d.mts +30 -1
- package/dist/service-adapters/anthropic/anthropic-adapter.d.mts.map +1 -1
- package/dist/service-adapters/anthropic/anthropic-adapter.mjs +59 -26
- package/dist/service-adapters/anthropic/anthropic-adapter.mjs.map +1 -1
- package/dist/service-adapters/index.d.cts +1 -1
- package/dist/service-adapters/index.d.mts +1 -1
- package/dist/v2/index.cjs +2 -0
- package/dist/v2/index.d.cts +2 -1
- package/dist/v2/index.d.mts +2 -1
- package/dist/v2/index.mjs +2 -1
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +2 -1
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +2 -1
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +2 -1
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +2 -1
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs +41 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.mjs +42 -2
- package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/threads.cjs +2 -3
- package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/threads.mjs +2 -3
- package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +8 -2
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +8 -2
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
- package/dist/v2/runtime/index.d.cts +1 -0
- package/dist/v2/runtime/index.d.cts.map +1 -1
- package/dist/v2/runtime/index.d.mts +1 -0
- package/dist/v2/runtime/index.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs +106 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +11 -0
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +11 -0
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +106 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/index.d.cts +1 -0
- package/dist/v2/runtime/intelligence-platform/index.d.mts +1 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.cjs +43 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.cjs.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.d.cts +34 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.d.cts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.d.mts +34 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.d.mts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.mjs +41 -0
- package/dist/v2/runtime/intelligence-platform/learned-skills.mjs.map +1 -0
- package/dist/v2/runtime/runner/intelligence.cjs +82 -25
- package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.cts +3 -0
- package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.mts +3 -0
- package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.mjs +83 -26
- package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
- package/package.json +10 -7
- package/skills/runtime/SKILL.md +0 -98
- package/skills/runtime/references/agent-runners-custom.md +0 -161
- package/skills/runtime/references/agent-runners-in-memory.md +0 -79
- package/skills/runtime/references/agent-runners-sqlite.md +0 -90
- package/skills/runtime/references/agent-runners.md +0 -336
- package/skills/runtime/references/built-in-agent-factory-modes.md +0 -232
- package/skills/runtime/references/built-in-agent-helper-utilities.md +0 -123
- package/skills/runtime/references/built-in-agent-model-identifiers.md +0 -58
- package/skills/runtime/references/built-in-agent.md +0 -523
- package/skills/runtime/references/intelligence-mode.md +0 -364
- package/skills/runtime/references/middleware.md +0 -376
- package/skills/runtime/references/server-side-tools.md +0 -414
- package/skills/runtime/references/setup-endpoint.md +0 -503
- package/skills/runtime/references/transcription.md +0 -287
- package/skills/runtime/references/wiring-a2a.md +0 -40
- package/skills/runtime/references/wiring-adk.md +0 -45
- package/skills/runtime/references/wiring-ag2.md +0 -41
- package/skills/runtime/references/wiring-agno.md +0 -40
- package/skills/runtime/references/wiring-aws-strands.md +0 -59
- package/skills/runtime/references/wiring-crewai-crews.md +0 -51
- package/skills/runtime/references/wiring-crewai-flows.md +0 -45
- package/skills/runtime/references/wiring-external-agents.md +0 -348
- package/skills/runtime/references/wiring-langgraph.md +0 -49
- package/skills/runtime/references/wiring-llamaindex.md +0 -39
- package/skills/runtime/references/wiring-mastra.md +0 -70
- package/skills/runtime/references/wiring-mcp-apps-middleware.md +0 -73
- package/skills/runtime/references/wiring-ms-agent-framework.md +0 -41
- package/skills/runtime/references/wiring-pydantic-ai.md +0 -45
|
@@ -1,414 +0,0 @@
|
|
|
1
|
-
# CopilotKit Server-Side Tools
|
|
2
|
-
|
|
3
|
-
Server-side tools run in the runtime process. They are the right choice when the tool needs
|
|
4
|
-
to touch server-only state: DB connections, API keys, filesystem, signed URLs.
|
|
5
|
-
|
|
6
|
-
`defineTool` returns a `ToolDefinition`. Pass an array of them to the Simple-Mode
|
|
7
|
-
`BuiltInAgent.config.tools`, or into the `tools:` option of `chat()` / `streamText()` inside
|
|
8
|
-
a Factory Mode factory.
|
|
9
|
-
|
|
10
|
-
## Setup
|
|
11
|
-
|
|
12
|
-
```typescript
|
|
13
|
-
import {
|
|
14
|
-
CopilotRuntime,
|
|
15
|
-
createCopilotRuntimeHandler,
|
|
16
|
-
BuiltInAgent,
|
|
17
|
-
defineTool,
|
|
18
|
-
} from "@copilotkit/runtime/v2";
|
|
19
|
-
import { z } from "zod";
|
|
20
|
-
|
|
21
|
-
const getInventory = defineTool({
|
|
22
|
-
name: "getInventory",
|
|
23
|
-
description: "Look up stock for a product SKU.",
|
|
24
|
-
parameters: z.object({ sku: z.string() }),
|
|
25
|
-
execute: async ({ sku }) => {
|
|
26
|
-
const row = await db.product.findUnique({ where: { sku } });
|
|
27
|
-
return { sku, inStock: row?.inStock ?? 0 };
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const runtime = new CopilotRuntime({
|
|
32
|
-
agents: {
|
|
33
|
-
default: new BuiltInAgent({
|
|
34
|
-
model: "openai/gpt-4o",
|
|
35
|
-
maxSteps: 5,
|
|
36
|
-
tools: [getInventory],
|
|
37
|
-
}),
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const handler = createCopilotRuntimeHandler({
|
|
42
|
-
runtime,
|
|
43
|
-
basePath: "/api/copilotkit",
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
export default { fetch: handler };
|
|
47
|
-
|
|
48
|
-
declare const db: {
|
|
49
|
-
product: { findUnique: (q: any) => Promise<{ inStock: number } | null> };
|
|
50
|
-
};
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## Core Patterns
|
|
54
|
-
|
|
55
|
-
### Zod parameters (most common)
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
import { defineTool } from "@copilotkit/runtime/v2";
|
|
59
|
-
import { z } from "zod";
|
|
60
|
-
|
|
61
|
-
const searchDocs = defineTool({
|
|
62
|
-
name: "searchDocs",
|
|
63
|
-
description: "Search the internal docs index.",
|
|
64
|
-
parameters: z.object({
|
|
65
|
-
query: z.string().min(1),
|
|
66
|
-
limit: z.number().int().min(1).max(20).default(5),
|
|
67
|
-
}),
|
|
68
|
-
execute: async ({ query, limit }) => {
|
|
69
|
-
const results = await searchIndex(query, limit);
|
|
70
|
-
return { results };
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
declare const searchIndex: (q: string, n: number) => Promise<unknown[]>;
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Valibot parameters (Standard Schema V1)
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
import { defineTool } from "@copilotkit/runtime/v2";
|
|
81
|
-
import * as v from "valibot";
|
|
82
|
-
|
|
83
|
-
const translate = defineTool({
|
|
84
|
-
name: "translate",
|
|
85
|
-
description: "Translate text between languages.",
|
|
86
|
-
parameters: v.object({
|
|
87
|
-
text: v.pipe(v.string(), v.minLength(1)),
|
|
88
|
-
target: v.picklist(["en", "es", "fr", "de"]),
|
|
89
|
-
}),
|
|
90
|
-
execute: async ({ text, target }) => ({ translated: `[${target}] ${text}` }),
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Graceful error handling inside execute
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
import { defineTool } from "@copilotkit/runtime/v2";
|
|
98
|
-
import { z } from "zod";
|
|
99
|
-
|
|
100
|
-
const runQuery = defineTool({
|
|
101
|
-
name: "runQuery",
|
|
102
|
-
description: "Run an analytics query.",
|
|
103
|
-
parameters: z.object({ sql: z.string() }),
|
|
104
|
-
execute: async ({ sql }) => {
|
|
105
|
-
try {
|
|
106
|
-
return { rows: await warehouse.query(sql) };
|
|
107
|
-
} catch (e) {
|
|
108
|
-
return { error: String(e), retryable: true };
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
declare const warehouse: { query: (sql: string) => Promise<unknown[]> };
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
### Server tool + client tool side by side
|
|
117
|
-
|
|
118
|
-
Server tools for I/O, client tools for UI. Both can coexist.
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
// server
|
|
122
|
-
import { defineTool } from "@copilotkit/runtime/v2";
|
|
123
|
-
import { z } from "zod";
|
|
124
|
-
|
|
125
|
-
export const fetchOrder = defineTool({
|
|
126
|
-
name: "fetchOrder",
|
|
127
|
-
description: "Fetch order details from the orders service.",
|
|
128
|
-
parameters: z.object({ orderId: z.string() }),
|
|
129
|
-
execute: async ({ orderId }) => fetchOrderFromService(orderId),
|
|
130
|
-
});
|
|
131
|
-
declare const fetchOrderFromService: (id: string) => Promise<unknown>;
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
```tsx
|
|
135
|
-
// client — a render-only tool lets the LLM display a modal
|
|
136
|
-
import { useComponent } from "@copilotkit/react-core/v2";
|
|
137
|
-
import { z } from "zod";
|
|
138
|
-
|
|
139
|
-
useComponent({
|
|
140
|
-
name: "showOrderDetails",
|
|
141
|
-
parameters: z.object({ orderId: z.string(), status: z.string() }),
|
|
142
|
-
// Schema fields arrive DIRECTLY as props (InferRenderProps<TSchema>) —
|
|
143
|
-
// no { args } wrapper. See packages/react-core/src/v2/hooks/use-component.tsx.
|
|
144
|
-
render: ({ orderId, status }) => (
|
|
145
|
-
<div className="modal">
|
|
146
|
-
Order {orderId} — {status}
|
|
147
|
-
</div>
|
|
148
|
-
),
|
|
149
|
-
});
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### Factory Mode — pass tools into the factory
|
|
153
|
-
|
|
154
|
-
Simple-Mode `config.tools` is ignored in Factory Mode.
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
import {
|
|
158
|
-
BuiltInAgent,
|
|
159
|
-
convertToolDefinitionsToVercelAITools,
|
|
160
|
-
convertMessagesToVercelAISDKMessages,
|
|
161
|
-
defineTool,
|
|
162
|
-
} from "@copilotkit/runtime/v2";
|
|
163
|
-
import { streamText } from "ai";
|
|
164
|
-
import { openai } from "@ai-sdk/openai";
|
|
165
|
-
import { z } from "zod";
|
|
166
|
-
|
|
167
|
-
const searchDocs = defineTool({
|
|
168
|
-
name: "searchDocs",
|
|
169
|
-
description: "Search the internal docs index.",
|
|
170
|
-
parameters: z.object({ query: z.string() }),
|
|
171
|
-
execute: async ({ query }) => ({ results: [] }),
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
new BuiltInAgent({
|
|
175
|
-
type: "aisdk",
|
|
176
|
-
factory: ({ input, abortSignal }) => {
|
|
177
|
-
const serverTools = convertToolDefinitionsToVercelAITools([searchDocs]);
|
|
178
|
-
return streamText({
|
|
179
|
-
model: openai("gpt-4o"),
|
|
180
|
-
messages: convertMessagesToVercelAISDKMessages(input.messages),
|
|
181
|
-
tools: serverTools,
|
|
182
|
-
abortSignal,
|
|
183
|
-
});
|
|
184
|
-
},
|
|
185
|
-
});
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
## Common Mistakes
|
|
189
|
-
|
|
190
|
-
### HIGH Using defineTool for tools that should render UI
|
|
191
|
-
|
|
192
|
-
Wrong:
|
|
193
|
-
|
|
194
|
-
```typescript
|
|
195
|
-
defineTool({
|
|
196
|
-
name: "showModal",
|
|
197
|
-
description: "Show a confirmation modal to the user.",
|
|
198
|
-
parameters: z.object({ title: z.string() }),
|
|
199
|
-
execute: async () => "rendered",
|
|
200
|
-
});
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
Correct:
|
|
204
|
-
|
|
205
|
-
```tsx
|
|
206
|
-
// Keep UI on the client — frontend tool with a renderer
|
|
207
|
-
import { useFrontendTool } from "@copilotkit/react-core/v2";
|
|
208
|
-
import { z } from "zod";
|
|
209
|
-
|
|
210
|
-
useFrontendTool({
|
|
211
|
-
name: "showModal",
|
|
212
|
-
parameters: z.object({ title: z.string() }),
|
|
213
|
-
handler: async (args) => ({ confirmed: true }),
|
|
214
|
-
});
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
Server tools execute on the server and stream only results back. The browser never sees a
|
|
218
|
-
`TOOL_CALL_START` for a server tool, so there is nothing to mount a renderer against.
|
|
219
|
-
|
|
220
|
-
Source: `dev-docs/architecture/plugin-points.md:36-77`;
|
|
221
|
-
`docs/content/docs/integrations/built-in-agent/server-tools.mdx:9-14`.
|
|
222
|
-
|
|
223
|
-
### MEDIUM Redefining AG-UI reserved names
|
|
224
|
-
|
|
225
|
-
Wrong:
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
defineTool({
|
|
229
|
-
name: "AGUISendStateSnapshot",
|
|
230
|
-
description: "My own snapshot tool.",
|
|
231
|
-
parameters: z.object({ snapshot: z.any() }),
|
|
232
|
-
execute: async () => ({ success: true }),
|
|
233
|
-
});
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
Correct:
|
|
237
|
-
|
|
238
|
-
```typescript
|
|
239
|
-
defineTool({
|
|
240
|
-
name: "mySnapshotExport",
|
|
241
|
-
description: "Export a user-facing state snapshot.",
|
|
242
|
-
parameters: z.object({ snapshot: z.any() }),
|
|
243
|
-
execute: async () => ({ success: true }),
|
|
244
|
-
});
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
`AGUISendStateSnapshot` and `AGUISendStateDelta` are auto-injected by BuiltInAgent in
|
|
248
|
-
Simple Mode — redefining them silently overwrites the built-ins.
|
|
249
|
-
|
|
250
|
-
Source: `packages/runtime/src/agent/index.ts:1139-1177`.
|
|
251
|
-
|
|
252
|
-
### MEDIUM Throwing from execute without a result
|
|
253
|
-
|
|
254
|
-
Wrong:
|
|
255
|
-
|
|
256
|
-
```typescript
|
|
257
|
-
defineTool({
|
|
258
|
-
name: "runQuery",
|
|
259
|
-
description: "Run a database query.",
|
|
260
|
-
parameters: z.object({ sql: z.string() }),
|
|
261
|
-
execute: async () => {
|
|
262
|
-
throw new Error("db down");
|
|
263
|
-
},
|
|
264
|
-
});
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
Correct:
|
|
268
|
-
|
|
269
|
-
```typescript
|
|
270
|
-
defineTool({
|
|
271
|
-
name: "runQuery",
|
|
272
|
-
description: "Run a database query.",
|
|
273
|
-
parameters: z.object({ sql: z.string() }),
|
|
274
|
-
execute: async ({ sql }) => {
|
|
275
|
-
try {
|
|
276
|
-
return await db.query(sql);
|
|
277
|
-
} catch (e) {
|
|
278
|
-
return { error: String(e), retryable: true };
|
|
279
|
-
}
|
|
280
|
-
},
|
|
281
|
-
});
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
Thrown errors kill the run; unserializable results (class instances, circular refs) become
|
|
285
|
-
the string `"[Unserializable tool result from X]"`. Return a plain-object error shape
|
|
286
|
-
instead and let the LLM retry.
|
|
287
|
-
|
|
288
|
-
Source: `packages/runtime/src/agent/index.ts:1469-1474`.
|
|
289
|
-
|
|
290
|
-
### MEDIUM Passing a JSON-schema object as parameters
|
|
291
|
-
|
|
292
|
-
Wrong:
|
|
293
|
-
|
|
294
|
-
```typescript
|
|
295
|
-
defineTool({
|
|
296
|
-
name: "x",
|
|
297
|
-
description: "...",
|
|
298
|
-
parameters: {
|
|
299
|
-
type: "object",
|
|
300
|
-
properties: { q: { type: "string" } },
|
|
301
|
-
required: ["q"],
|
|
302
|
-
} as any,
|
|
303
|
-
execute: async ({ q }) => q,
|
|
304
|
-
});
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
Correct:
|
|
308
|
-
|
|
309
|
-
```typescript
|
|
310
|
-
import { z } from "zod";
|
|
311
|
-
|
|
312
|
-
defineTool({
|
|
313
|
-
name: "x",
|
|
314
|
-
description: "...",
|
|
315
|
-
parameters: z.object({ q: z.string() }),
|
|
316
|
-
execute: async ({ q }) => q,
|
|
317
|
-
});
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
`parameters` must be a Standard Schema V1 validator (Zod, Valibot, ArkType, ...). Plain
|
|
321
|
-
JSON Schema throws in `schemaToJsonSchema()`. Also, Standard Schema V1 preserves static
|
|
322
|
-
types — `execute`'s arg type is inferred.
|
|
323
|
-
|
|
324
|
-
Source: `packages/runtime/src/agent/index.ts:633-659`.
|
|
325
|
-
|
|
326
|
-
### HIGH Unavailable in Factory Mode via config.tools
|
|
327
|
-
|
|
328
|
-
Wrong:
|
|
329
|
-
|
|
330
|
-
```typescript
|
|
331
|
-
new BuiltInAgent({
|
|
332
|
-
type: "tanstack",
|
|
333
|
-
factory: myFactory,
|
|
334
|
-
tools: [searchDocs], // ignored in Factory Mode
|
|
335
|
-
} as any);
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
Correct:
|
|
339
|
-
|
|
340
|
-
```typescript
|
|
341
|
-
// Factory Mode — AI SDK factory: convert defineTool → Vercel AI SDK tools
|
|
342
|
-
import {
|
|
343
|
-
BuiltInAgent,
|
|
344
|
-
convertToolDefinitionsToVercelAITools,
|
|
345
|
-
convertMessagesToVercelAISDKMessages,
|
|
346
|
-
} from "@copilotkit/runtime/v2";
|
|
347
|
-
import { streamText } from "ai";
|
|
348
|
-
import { openai } from "@ai-sdk/openai";
|
|
349
|
-
|
|
350
|
-
new BuiltInAgent({
|
|
351
|
-
type: "aisdk",
|
|
352
|
-
factory: ({ input, abortSignal }) => {
|
|
353
|
-
const tools = convertToolDefinitionsToVercelAITools([searchDocs]);
|
|
354
|
-
return streamText({
|
|
355
|
-
model: openai("gpt-4o"),
|
|
356
|
-
messages: convertMessagesToVercelAISDKMessages(input.messages),
|
|
357
|
-
tools,
|
|
358
|
-
abortSignal,
|
|
359
|
-
});
|
|
360
|
-
},
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
// Factory Mode — TanStack AI factory: defineTool output is NOT a TanStack tool.
|
|
364
|
-
// There is no built-in converter in @copilotkit/runtime for TanStack. Either
|
|
365
|
-
// redefine the tool with TanStack's `toolDefinition()` API from `@tanstack/ai`,
|
|
366
|
-
// or write a small adapter that translates your `defineTool` output into
|
|
367
|
-
// TanStack's tool shape before passing it into `chat({ tools })`.
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
Factory Mode ignores `config.tools`. Wire server tools through the factory's LLM call —
|
|
371
|
-
AI SDK has `convertToolDefinitionsToVercelAITools([...])` out of the box; TanStack AI has
|
|
372
|
-
its own `toolDefinition()` API you need to build the tools with directly.
|
|
373
|
-
|
|
374
|
-
Source: `packages/runtime/src/agent/index.ts:1581-1671`.
|
|
375
|
-
|
|
376
|
-
### MEDIUM Shared name between client and server tool
|
|
377
|
-
|
|
378
|
-
Wrong:
|
|
379
|
-
|
|
380
|
-
```tsx
|
|
381
|
-
// frontend
|
|
382
|
-
useFrontendTool({
|
|
383
|
-
name: "getWeather",
|
|
384
|
-
parameters: z.object({ city: z.string() }),
|
|
385
|
-
handler,
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
// server
|
|
389
|
-
defineTool({
|
|
390
|
-
name: "getWeather",
|
|
391
|
-
parameters: z.object({ city: z.string() }),
|
|
392
|
-
execute,
|
|
393
|
-
});
|
|
394
|
-
// Server silently wins on the merge — handler never fires
|
|
395
|
-
```
|
|
396
|
-
|
|
397
|
-
Correct:
|
|
398
|
-
|
|
399
|
-
```tsx
|
|
400
|
-
// Pick one side and give tools distinct names if both sides need their own
|
|
401
|
-
useFrontendTool({ name: "getWeatherClientSide" /* ... */ });
|
|
402
|
-
defineTool({ name: "getWeatherServer" /* ... */ });
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
On collisions, `config.tools` (server) overwrites frontend-registered tools. The LLM sees
|
|
406
|
-
only one `getWeather` — the server version.
|
|
407
|
-
|
|
408
|
-
Source: `packages/runtime/src/agent/index.ts` (tool merge).
|
|
409
|
-
|
|
410
|
-
## See also
|
|
411
|
-
|
|
412
|
-
- `copilotkit/built-in-agent` — `config.tools` only applies in Simple Mode
|
|
413
|
-
- `copilotkit/client-side-tools` (react-core) — browser-side tools, paired decision
|
|
414
|
-
- `copilotkit/rendering-tool-calls` (react-core) — rendering tool invocations in chat
|