@kood/claude-code 0.1.6 → 0.1.9
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/index.js +109 -216
- package/package.json +8 -2
- package/templates/hono/CLAUDE.md +59 -328
- package/templates/hono/docs/architecture/architecture.md +93 -747
- package/templates/hono/docs/deployment/cloudflare.md +59 -513
- package/templates/hono/docs/deployment/docker.md +41 -356
- package/templates/hono/docs/deployment/index.md +54 -190
- package/templates/hono/docs/deployment/railway.md +36 -306
- package/templates/hono/docs/deployment/vercel.md +49 -434
- package/templates/hono/docs/library/ai-sdk/index.md +53 -290
- package/templates/hono/docs/library/ai-sdk/openrouter.md +19 -387
- package/templates/hono/docs/library/ai-sdk/providers.md +28 -394
- package/templates/hono/docs/library/ai-sdk/streaming.md +52 -353
- package/templates/hono/docs/library/ai-sdk/structured-output.md +63 -395
- package/templates/hono/docs/library/ai-sdk/tools.md +62 -431
- package/templates/hono/docs/library/hono/env-setup.md +24 -313
- package/templates/hono/docs/library/hono/error-handling.md +34 -295
- package/templates/hono/docs/library/hono/index.md +29 -121
- package/templates/hono/docs/library/hono/middleware.md +21 -188
- package/templates/hono/docs/library/hono/rpc.md +40 -341
- package/templates/hono/docs/library/hono/validation.md +35 -195
- package/templates/hono/docs/library/pino/index.md +42 -333
- package/templates/hono/docs/library/prisma/cloudflare-d1.md +64 -367
- package/templates/hono/docs/library/prisma/config.md +19 -260
- package/templates/hono/docs/library/prisma/index.md +67 -320
- package/templates/hono/docs/library/zod/index.md +53 -257
- package/templates/npx/CLAUDE.md +62 -274
- package/templates/npx/docs/references/patterns.md +160 -0
- package/templates/tanstack-start/CLAUDE.md +100 -256
- package/templates/tanstack-start/docs/architecture/architecture.md +44 -589
- package/templates/tanstack-start/docs/deployment/cloudflare.md +37 -424
- package/templates/tanstack-start/docs/deployment/index.md +57 -286
- package/templates/tanstack-start/docs/deployment/nitro.md +36 -318
- package/templates/tanstack-start/docs/deployment/railway.md +40 -409
- package/templates/tanstack-start/docs/deployment/vercel.md +43 -465
- package/templates/tanstack-start/docs/design/components.md +77 -311
- package/templates/tanstack-start/docs/design/index.md +113 -69
- package/templates/tanstack-start/docs/design/safe-area.md +51 -250
- package/templates/tanstack-start/docs/design/tailwind-setup.md +45 -359
- package/templates/tanstack-start/docs/guides/conventions.md +103 -0
- package/templates/tanstack-start/docs/guides/env-setup.md +34 -340
- package/templates/tanstack-start/docs/guides/getting-started.md +22 -209
- package/templates/tanstack-start/docs/guides/hooks.md +166 -0
- package/templates/tanstack-start/docs/guides/routes.md +166 -0
- package/templates/tanstack-start/docs/guides/services.md +143 -0
- package/templates/tanstack-start/docs/library/better-auth/2fa.md +27 -115
- package/templates/tanstack-start/docs/library/better-auth/advanced.md +22 -105
- package/templates/tanstack-start/docs/library/better-auth/index.md +17 -66
- package/templates/tanstack-start/docs/library/better-auth/plugins.md +11 -88
- package/templates/tanstack-start/docs/library/better-auth/session.md +12 -92
- package/templates/tanstack-start/docs/library/better-auth/setup.md +9 -91
- package/templates/tanstack-start/docs/library/prisma/cloudflare-d1.md +30 -358
- package/templates/tanstack-start/docs/library/prisma/config.md +27 -327
- package/templates/tanstack-start/docs/library/prisma/crud.md +46 -174
- package/templates/tanstack-start/docs/library/prisma/index.md +23 -113
- package/templates/tanstack-start/docs/library/prisma/relations.md +31 -153
- package/templates/tanstack-start/docs/library/prisma/schema.md +40 -217
- package/templates/tanstack-start/docs/library/prisma/setup.md +12 -112
- package/templates/tanstack-start/docs/library/prisma/transactions.md +20 -110
- package/templates/tanstack-start/docs/library/tanstack-query/index.md +26 -97
- package/templates/tanstack-start/docs/library/tanstack-query/invalidation.md +28 -107
- package/templates/tanstack-start/docs/library/tanstack-query/optimistic-updates.md +44 -146
- package/templates/tanstack-start/docs/library/tanstack-query/use-mutation.md +33 -127
- package/templates/tanstack-start/docs/library/tanstack-query/use-query.md +49 -149
- package/templates/tanstack-start/docs/library/tanstack-start/auth-patterns.md +19 -112
- package/templates/tanstack-start/docs/library/tanstack-start/index.md +33 -80
- package/templates/tanstack-start/docs/library/tanstack-start/middleware.md +28 -106
- package/templates/tanstack-start/docs/library/tanstack-start/routing.md +21 -118
- package/templates/tanstack-start/docs/library/tanstack-start/server-functions.md +34 -246
- package/templates/tanstack-start/docs/library/tanstack-start/setup.md +6 -39
- package/templates/tanstack-start/docs/library/zod/complex-types.md +32 -156
- package/templates/tanstack-start/docs/library/zod/index.md +31 -144
- package/templates/tanstack-start/docs/library/zod/transforms.md +20 -129
- package/templates/tanstack-start/docs/library/zod/validation.md +39 -155
- package/templates/hono/docs/commands/git.md +0 -145
- package/templates/hono/docs/mcp/context7.md +0 -106
- package/templates/hono/docs/mcp/index.md +0 -176
- package/templates/hono/docs/mcp/sequential-thinking.md +0 -101
- package/templates/hono/docs/mcp/serena.md +0 -269
- package/templates/hono/docs/mcp/sgrep.md +0 -105
- package/templates/hono/docs/skills/gemini-review/SKILL.md +0 -220
- package/templates/hono/docs/skills/gemini-review/references/checklists.md +0 -136
- package/templates/hono/docs/skills/gemini-review/references/prompt-templates.md +0 -303
- package/templates/npx/docs/commands/git.md +0 -145
- package/templates/npx/docs/mcp/index.md +0 -60
- package/templates/npx/docs/skills/gemini-review/SKILL.md +0 -220
- package/templates/npx/docs/skills/gemini-review/references/checklists.md +0 -134
- package/templates/npx/docs/skills/gemini-review/references/prompt-templates.md +0 -301
- package/templates/tanstack-start/docs/commands/git.md +0 -145
- package/templates/tanstack-start/docs/design/accessibility.md +0 -433
- package/templates/tanstack-start/docs/design/color.md +0 -235
- package/templates/tanstack-start/docs/design/spacing.md +0 -341
- package/templates/tanstack-start/docs/design/typography.md +0 -324
- package/templates/tanstack-start/docs/guides/best-practices.md +0 -950
- package/templates/tanstack-start/docs/guides/husky-lint-staged.md +0 -303
- package/templates/tanstack-start/docs/guides/prettier.md +0 -189
- package/templates/tanstack-start/docs/guides/project-templates.md +0 -710
- package/templates/tanstack-start/docs/library/tanstack-query/setup.md +0 -107
- package/templates/tanstack-start/docs/library/zod/basic-types.md +0 -186
- package/templates/tanstack-start/docs/mcp/context7.md +0 -204
- package/templates/tanstack-start/docs/mcp/index.md +0 -177
- package/templates/tanstack-start/docs/mcp/sequential-thinking.md +0 -180
- package/templates/tanstack-start/docs/mcp/serena.md +0 -269
- package/templates/tanstack-start/docs/mcp/sgrep.md +0 -174
- package/templates/tanstack-start/docs/skills/gemini-review/SKILL.md +0 -220
- package/templates/tanstack-start/docs/skills/gemini-review/references/checklists.md +0 -144
- package/templates/tanstack-start/docs/skills/gemini-review/references/prompt-templates.md +0 -292
|
@@ -1,25 +1,16 @@
|
|
|
1
|
-
# AI SDK - Tool Calling
|
|
1
|
+
# AI SDK - Tool Calling
|
|
2
2
|
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 개요
|
|
8
|
-
|
|
9
|
-
AI SDK의 도구(Tool)를 Hono에서 사용하면 AI 모델이 외부 함수를 호출할 수 있습니다. 날씨 조회, 데이터베이스 검색, API 호출 등 다양한 작업을 수행할 수 있습니다.
|
|
3
|
+
> AI 모델이 외부 함수를 호출
|
|
10
4
|
|
|
11
5
|
---
|
|
12
6
|
|
|
13
7
|
## 기본 도구 정의
|
|
14
8
|
|
|
15
9
|
```typescript
|
|
16
|
-
import { Hono } from 'hono'
|
|
17
10
|
import { streamText, tool, convertToModelMessages } from 'ai'
|
|
18
11
|
import { openai } from '@ai-sdk/openai'
|
|
19
12
|
import { z } from 'zod'
|
|
20
13
|
|
|
21
|
-
const app = new Hono()
|
|
22
|
-
|
|
23
14
|
app.post('/api/chat', async (c) => {
|
|
24
15
|
const { messages } = await c.req.json()
|
|
25
16
|
|
|
@@ -33,12 +24,7 @@ app.post('/api/chat', async (c) => {
|
|
|
33
24
|
location: z.string().describe('The city name'),
|
|
34
25
|
}),
|
|
35
26
|
execute: async ({ location }) => {
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
location,
|
|
39
|
-
temperature: 22,
|
|
40
|
-
condition: 'Sunny',
|
|
41
|
-
}
|
|
27
|
+
return { location, temperature: 22, condition: 'Sunny' }
|
|
42
28
|
},
|
|
43
29
|
}),
|
|
44
30
|
},
|
|
@@ -50,26 +36,21 @@ app.post('/api/chat', async (c) => {
|
|
|
50
36
|
|
|
51
37
|
---
|
|
52
38
|
|
|
53
|
-
## tool()
|
|
39
|
+
## tool() 구조
|
|
54
40
|
|
|
55
41
|
```typescript
|
|
56
42
|
import { tool } from 'ai'
|
|
57
43
|
import { z } from 'zod'
|
|
58
44
|
|
|
59
45
|
const myTool = tool({
|
|
60
|
-
|
|
61
|
-
description: 'Tool description for the AI model',
|
|
62
|
-
|
|
63
|
-
// 입력 스키마 (Zod로 정의)
|
|
46
|
+
description: 'Tool description for the AI',
|
|
64
47
|
inputSchema: z.object({
|
|
65
48
|
param1: z.string().describe('Parameter description'),
|
|
66
49
|
param2: z.number().optional(),
|
|
67
50
|
}),
|
|
68
|
-
|
|
69
|
-
// 실행 함수
|
|
70
51
|
execute: async (args, context) => {
|
|
71
|
-
// args: 스키마에 맞는
|
|
72
|
-
// context: { toolCallId, messages }
|
|
52
|
+
// args: 스키마에 맞는 입력값
|
|
53
|
+
// context: { toolCallId, messages }
|
|
73
54
|
return { result: 'success' }
|
|
74
55
|
},
|
|
75
56
|
})
|
|
@@ -77,437 +58,87 @@ const myTool = tool({
|
|
|
77
58
|
|
|
78
59
|
---
|
|
79
60
|
|
|
80
|
-
## 여러 도구
|
|
61
|
+
## 여러 도구
|
|
81
62
|
|
|
82
63
|
```typescript
|
|
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
|
-
getAttractions: tool({
|
|
109
|
-
description: 'Get tourist attractions for a city',
|
|
110
|
-
inputSchema: z.object({
|
|
111
|
-
city: z.string(),
|
|
112
|
-
}),
|
|
113
|
-
execute: async ({ city }) => ({
|
|
114
|
-
attractions: [
|
|
115
|
-
'Gyeongbokgung Palace',
|
|
116
|
-
'N Seoul Tower',
|
|
117
|
-
'Bukchon Hanok Village',
|
|
118
|
-
],
|
|
119
|
-
}),
|
|
120
|
-
}),
|
|
121
|
-
|
|
122
|
-
searchDatabase: tool({
|
|
123
|
-
description: 'Search the database for information',
|
|
124
|
-
inputSchema: z.object({
|
|
125
|
-
query: z.string(),
|
|
126
|
-
limit: z.number().optional().default(10),
|
|
127
|
-
}),
|
|
128
|
-
execute: async ({ query, limit }) => {
|
|
129
|
-
// 데이터베이스 검색 로직
|
|
130
|
-
return { results: [], total: 0 }
|
|
131
|
-
},
|
|
132
|
-
}),
|
|
133
|
-
},
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
return result.toUIMessageStreamResponse()
|
|
137
|
-
})
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
---
|
|
141
|
-
|
|
142
|
-
## 도구와 외부 서비스 연동
|
|
143
|
-
|
|
144
|
-
### 데이터베이스 연동
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
import { Hono } from 'hono'
|
|
148
|
-
import { streamText, tool, convertToModelMessages } from 'ai'
|
|
149
|
-
import { openai } from '@ai-sdk/openai'
|
|
150
|
-
import { z } from 'zod'
|
|
151
|
-
import { prisma } from '@/database/prisma'
|
|
152
|
-
|
|
153
|
-
const app = new Hono()
|
|
154
|
-
|
|
155
|
-
app.post('/api/chat', async (c) => {
|
|
156
|
-
const { messages } = await c.req.json()
|
|
157
|
-
|
|
158
|
-
const result = streamText({
|
|
159
|
-
model: openai('gpt-4o'),
|
|
160
|
-
messages: convertToModelMessages(messages),
|
|
161
|
-
tools: {
|
|
162
|
-
searchProducts: tool({
|
|
163
|
-
description: 'Search for products in the database',
|
|
164
|
-
inputSchema: z.object({
|
|
165
|
-
query: z.string().describe('Search query'),
|
|
166
|
-
category: z.string().optional().describe('Product category'),
|
|
167
|
-
maxPrice: z.number().optional().describe('Maximum price'),
|
|
168
|
-
}),
|
|
169
|
-
execute: async ({ query, category, maxPrice }) => {
|
|
170
|
-
const products = await prisma.product.findMany({
|
|
171
|
-
where: {
|
|
172
|
-
name: { contains: query },
|
|
173
|
-
...(category && { category }),
|
|
174
|
-
...(maxPrice && { price: { lte: maxPrice } }),
|
|
175
|
-
},
|
|
176
|
-
take: 10,
|
|
177
|
-
})
|
|
178
|
-
return { products }
|
|
179
|
-
},
|
|
180
|
-
}),
|
|
181
|
-
|
|
182
|
-
getOrderStatus: tool({
|
|
183
|
-
description: 'Get the status of an order',
|
|
184
|
-
inputSchema: z.object({
|
|
185
|
-
orderId: z.string().describe('The order ID'),
|
|
186
|
-
}),
|
|
187
|
-
execute: async ({ orderId }) => {
|
|
188
|
-
const order = await prisma.order.findUnique({
|
|
189
|
-
where: { id: orderId },
|
|
190
|
-
include: { items: true },
|
|
191
|
-
})
|
|
192
|
-
return order ?? { error: 'Order not found' }
|
|
193
|
-
},
|
|
194
|
-
}),
|
|
195
|
-
},
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
return result.toUIMessageStreamResponse()
|
|
199
|
-
})
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### 외부 API 연동
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
import { Hono } from 'hono'
|
|
206
|
-
import { streamText, tool, convertToModelMessages } from 'ai'
|
|
207
|
-
import { openai } from '@ai-sdk/openai'
|
|
208
|
-
import { z } from 'zod'
|
|
209
|
-
|
|
210
|
-
type Bindings = {
|
|
211
|
-
WEATHER_API_KEY: string
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const app = new Hono<{ Bindings: Bindings }>()
|
|
215
|
-
|
|
216
|
-
app.post('/api/chat', async (c) => {
|
|
217
|
-
const { messages } = await c.req.json()
|
|
218
|
-
|
|
219
|
-
const result = streamText({
|
|
220
|
-
model: openai('gpt-4o'),
|
|
221
|
-
messages: convertToModelMessages(messages),
|
|
222
|
-
tools: {
|
|
223
|
-
getWeather: tool({
|
|
224
|
-
description: 'Get real-time weather data',
|
|
225
|
-
inputSchema: z.object({
|
|
226
|
-
city: z.string(),
|
|
227
|
-
}),
|
|
228
|
-
execute: async ({ city }) => {
|
|
229
|
-
const response = await fetch(
|
|
230
|
-
`https://api.weather.com/v1/current?city=${city}&key=${c.env.WEATHER_API_KEY}`
|
|
231
|
-
)
|
|
232
|
-
return response.json()
|
|
233
|
-
},
|
|
234
|
-
}),
|
|
235
|
-
|
|
236
|
-
translateText: tool({
|
|
237
|
-
description: 'Translate text to another language',
|
|
238
|
-
inputSchema: z.object({
|
|
239
|
-
text: z.string(),
|
|
240
|
-
targetLanguage: z.string(),
|
|
241
|
-
}),
|
|
242
|
-
execute: async ({ text, targetLanguage }) => {
|
|
243
|
-
// 번역 API 호출
|
|
244
|
-
const response = await fetch('https://api.translate.com/v1/translate', {
|
|
245
|
-
method: 'POST',
|
|
246
|
-
headers: { 'Content-Type': 'application/json' },
|
|
247
|
-
body: JSON.stringify({ text, target: targetLanguage }),
|
|
248
|
-
})
|
|
249
|
-
return response.json()
|
|
250
|
-
},
|
|
251
|
-
}),
|
|
252
|
-
},
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
return result.toUIMessageStreamResponse()
|
|
256
|
-
})
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
---
|
|
260
|
-
|
|
261
|
-
## 다중 단계 실행
|
|
262
|
-
|
|
263
|
-
AI가 여러 도구를 순차적으로 호출하도록 허용:
|
|
264
|
-
|
|
265
|
-
```typescript
|
|
266
|
-
import { Hono } from 'hono'
|
|
267
|
-
import { generateText, tool, stepCountIs } from 'ai'
|
|
268
|
-
import { openai } from '@ai-sdk/openai'
|
|
269
|
-
import { z } from 'zod'
|
|
270
|
-
|
|
271
|
-
const app = new Hono()
|
|
272
|
-
|
|
273
|
-
app.post('/api/plan-trip', async (c) => {
|
|
274
|
-
const { destination } = await c.req.json()
|
|
275
|
-
|
|
276
|
-
const result = await generateText({
|
|
277
|
-
model: openai('gpt-4o'),
|
|
278
|
-
prompt: `Plan a trip to ${destination}`,
|
|
279
|
-
tools: {
|
|
280
|
-
getWeather: tool({
|
|
281
|
-
description: 'Get weather forecast',
|
|
282
|
-
inputSchema: z.object({ location: z.string() }),
|
|
283
|
-
execute: async ({ location }) => ({ forecast: 'Sunny, 22°C' }),
|
|
284
|
-
}),
|
|
285
|
-
getFlights: tool({
|
|
286
|
-
description: 'Search for flights',
|
|
287
|
-
inputSchema: z.object({ destination: z.string() }),
|
|
288
|
-
execute: async ({ destination }) => ({ flights: ['Flight A', 'Flight B'] }),
|
|
289
|
-
}),
|
|
290
|
-
getHotels: tool({
|
|
291
|
-
description: 'Search for hotels',
|
|
292
|
-
inputSchema: z.object({ location: z.string() }),
|
|
293
|
-
execute: async ({ location }) => ({ hotels: ['Hotel A', 'Hotel B'] }),
|
|
64
|
+
const result = streamText({
|
|
65
|
+
model: openai('gpt-4o'),
|
|
66
|
+
messages: convertToModelMessages(messages),
|
|
67
|
+
tools: {
|
|
68
|
+
getWeather: tool({
|
|
69
|
+
description: 'Get weather',
|
|
70
|
+
inputSchema: z.object({ location: z.string() }),
|
|
71
|
+
execute: async ({ location }) => {
|
|
72
|
+
return { temperature: 22, condition: 'Sunny' }
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
searchDatabase: tool({
|
|
76
|
+
description: 'Search the database',
|
|
77
|
+
inputSchema: z.object({ query: z.string() }),
|
|
78
|
+
execute: async ({ query }) => {
|
|
79
|
+
return { results: [] }
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
sendEmail: tool({
|
|
83
|
+
description: 'Send an email',
|
|
84
|
+
inputSchema: z.object({
|
|
85
|
+
to: z.email(),
|
|
86
|
+
subject: z.string(),
|
|
87
|
+
body: z.string(),
|
|
294
88
|
}),
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
plan: result.text,
|
|
301
|
-
steps: result.steps.map((step) => ({
|
|
302
|
-
toolCalls: step.toolCalls,
|
|
303
|
-
toolResults: step.toolResults,
|
|
304
|
-
})),
|
|
305
|
-
})
|
|
89
|
+
execute: async (args) => {
|
|
90
|
+
return { sent: true }
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
306
94
|
})
|
|
307
95
|
```
|
|
308
96
|
|
|
309
97
|
---
|
|
310
98
|
|
|
311
|
-
## 도구
|
|
99
|
+
## 다단계 도구 호출
|
|
312
100
|
|
|
313
101
|
```typescript
|
|
314
|
-
|
|
315
|
-
const { messages } = await c.req.json()
|
|
102
|
+
import { generateText } from 'ai'
|
|
316
103
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
description: 'Process a user request',
|
|
323
|
-
inputSchema: z.object({ request: z.string() }),
|
|
324
|
-
execute: async (args, context) => {
|
|
325
|
-
// 도구 호출 ID
|
|
326
|
-
console.log('Tool call ID:', context.toolCallId)
|
|
327
|
-
|
|
328
|
-
// 전체 메시지 히스토리 접근
|
|
329
|
-
console.log('Messages:', context.messages)
|
|
330
|
-
|
|
331
|
-
return { processed: true }
|
|
332
|
-
},
|
|
333
|
-
}),
|
|
334
|
-
},
|
|
335
|
-
})
|
|
336
|
-
|
|
337
|
-
return result.toUIMessageStreamResponse()
|
|
104
|
+
const { text, steps } = await generateText({
|
|
105
|
+
model: openai('gpt-4o'),
|
|
106
|
+
messages: convertToModelMessages(messages),
|
|
107
|
+
tools: { ... },
|
|
108
|
+
maxSteps: 5, // 최대 도구 호출 횟수
|
|
338
109
|
})
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
---
|
|
342
|
-
|
|
343
|
-
## 도구 결과 스트리밍
|
|
344
|
-
|
|
345
|
-
```typescript
|
|
346
|
-
import { Hono } from 'hono'
|
|
347
|
-
import { streamText, tool } from 'ai'
|
|
348
|
-
import { openai } from '@ai-sdk/openai'
|
|
349
|
-
import { z } from 'zod'
|
|
350
|
-
import { stream } from 'hono/streaming'
|
|
351
110
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
app.post('/api/chat', async (c) => {
|
|
355
|
-
const { messages } = await c.req.json()
|
|
356
|
-
|
|
357
|
-
const result = streamText({
|
|
358
|
-
model: openai('gpt-4o'),
|
|
359
|
-
messages,
|
|
360
|
-
tools: {
|
|
361
|
-
getWeather: tool({
|
|
362
|
-
description: 'Get weather',
|
|
363
|
-
inputSchema: z.object({ location: z.string() }),
|
|
364
|
-
execute: async ({ location }) => ({ temp: 22 }),
|
|
365
|
-
}),
|
|
366
|
-
},
|
|
367
|
-
})
|
|
368
|
-
|
|
369
|
-
return stream(c, async (stream) => {
|
|
370
|
-
for await (const event of result.fullStream) {
|
|
371
|
-
switch (event.type) {
|
|
372
|
-
case 'text-delta':
|
|
373
|
-
await stream.write(
|
|
374
|
-
JSON.stringify({ type: 'text', content: event.textDelta }) + '\n'
|
|
375
|
-
)
|
|
376
|
-
break
|
|
377
|
-
case 'tool-call':
|
|
378
|
-
await stream.write(
|
|
379
|
-
JSON.stringify({
|
|
380
|
-
type: 'tool-call',
|
|
381
|
-
name: event.toolName,
|
|
382
|
-
args: event.args,
|
|
383
|
-
}) + '\n'
|
|
384
|
-
)
|
|
385
|
-
break
|
|
386
|
-
case 'tool-result':
|
|
387
|
-
await stream.write(
|
|
388
|
-
JSON.stringify({
|
|
389
|
-
type: 'tool-result',
|
|
390
|
-
name: event.toolName,
|
|
391
|
-
result: event.result,
|
|
392
|
-
}) + '\n'
|
|
393
|
-
)
|
|
394
|
-
break
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
})
|
|
398
|
-
})
|
|
111
|
+
// steps: 각 단계의 도구 호출 정보
|
|
399
112
|
```
|
|
400
113
|
|
|
401
114
|
---
|
|
402
115
|
|
|
403
|
-
##
|
|
116
|
+
## 도구 선택 제어
|
|
404
117
|
|
|
405
118
|
```typescript
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
greet: tool({
|
|
411
|
-
description: 'Greet a user',
|
|
412
|
-
inputSchema: z.object({ name: z.string() }),
|
|
413
|
-
execute: async ({ name }) => `Hello, ${name}!`,
|
|
414
|
-
}),
|
|
415
|
-
calculate: tool({
|
|
416
|
-
description: 'Calculate sum',
|
|
417
|
-
inputSchema: z.object({
|
|
418
|
-
a: z.number(),
|
|
419
|
-
b: z.number(),
|
|
420
|
-
}),
|
|
421
|
-
execute: async ({ a, b }) => a + b,
|
|
422
|
-
}),
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// 타입 추출
|
|
426
|
-
type MyToolCall = TypedToolCall<typeof myToolSet>
|
|
427
|
-
type MyToolResult = TypedToolResult<typeof myToolSet>
|
|
428
|
-
|
|
429
|
-
// Hono 라우트에서 사용
|
|
430
|
-
app.post('/api/typed-chat', async (c) => {
|
|
431
|
-
const { messages } = await c.req.json()
|
|
432
|
-
|
|
433
|
-
const result = await generateText({
|
|
434
|
-
model: openai('gpt-4o'),
|
|
435
|
-
tools: myToolSet,
|
|
436
|
-
messages,
|
|
437
|
-
})
|
|
119
|
+
const result = streamText({
|
|
120
|
+
model: openai('gpt-4o'),
|
|
121
|
+
messages,
|
|
122
|
+
tools: { ... },
|
|
438
123
|
|
|
439
|
-
//
|
|
440
|
-
|
|
441
|
-
const toolResults: MyToolResult[] = result.toolResults
|
|
124
|
+
// 도구 사용 강제
|
|
125
|
+
toolChoice: 'required',
|
|
442
126
|
|
|
443
|
-
|
|
444
|
-
}
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
---
|
|
127
|
+
// 특정 도구만
|
|
128
|
+
toolChoice: { type: 'tool', toolName: 'getWeather' },
|
|
448
129
|
|
|
449
|
-
|
|
130
|
+
// 도구 사용 금지
|
|
131
|
+
toolChoice: 'none',
|
|
450
132
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
const { messages } = await c.req.json()
|
|
454
|
-
|
|
455
|
-
const result = streamText({
|
|
456
|
-
model: openai('gpt-4o'),
|
|
457
|
-
messages: convertToModelMessages(messages),
|
|
458
|
-
tools: {
|
|
459
|
-
riskyOperation: tool({
|
|
460
|
-
description: 'Perform a risky operation',
|
|
461
|
-
inputSchema: z.object({ data: z.string() }),
|
|
462
|
-
execute: async ({ data }) => {
|
|
463
|
-
try {
|
|
464
|
-
// 위험한 작업 수행
|
|
465
|
-
const result = await performRiskyTask(data)
|
|
466
|
-
return { success: true, result }
|
|
467
|
-
} catch (error) {
|
|
468
|
-
// 에러를 AI가 이해할 수 있는 형태로 반환
|
|
469
|
-
return {
|
|
470
|
-
success: false,
|
|
471
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
},
|
|
475
|
-
}),
|
|
476
|
-
},
|
|
477
|
-
})
|
|
478
|
-
|
|
479
|
-
return result.toUIMessageStreamResponse()
|
|
133
|
+
// 자동 (기본)
|
|
134
|
+
toolChoice: 'auto',
|
|
480
135
|
})
|
|
481
136
|
```
|
|
482
137
|
|
|
483
138
|
---
|
|
484
139
|
|
|
485
|
-
##
|
|
486
|
-
|
|
487
|
-
1. **명확한 설명**: 모델이 언제 도구를 사용할지 이해하도록 상세히 작성
|
|
488
|
-
2. **스키마 설명**: 각 파라미터에 `.describe()` 추가
|
|
489
|
-
3. **에러 처리**: execute 함수에서 적절한 에러 처리
|
|
490
|
-
4. **반환값**: 모델이 이해할 수 있는 구조화된 데이터 반환
|
|
140
|
+
## 관련 문서
|
|
491
141
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
Search for products in the database.
|
|
496
|
-
Use this when the user asks about available products,
|
|
497
|
-
product prices, or product availability.
|
|
498
|
-
`,
|
|
499
|
-
inputSchema: z.object({
|
|
500
|
-
query: z.string().describe('Search query for product name or category'),
|
|
501
|
-
maxResults: z.number().default(10).describe('Maximum number of results'),
|
|
502
|
-
category: z.string().optional().describe('Filter by category'),
|
|
503
|
-
}),
|
|
504
|
-
execute: async ({ query, maxResults, category }) => {
|
|
505
|
-
try {
|
|
506
|
-
const results = await db.products.search({ query, maxResults, category })
|
|
507
|
-
return { success: true, products: results }
|
|
508
|
-
} catch (error) {
|
|
509
|
-
return { success: false, error: 'Failed to search products' }
|
|
510
|
-
}
|
|
511
|
-
},
|
|
512
|
-
})
|
|
513
|
-
```
|
|
142
|
+
- [AI SDK 개요](./index.md)
|
|
143
|
+
- [스트리밍](./streaming.md)
|
|
144
|
+
- [구조화된 출력](./structured-output.md)
|