Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.gemini-server-tools-wire.ts

47 lines1.7 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat, toolDefinition } from '@tanstack/ai'
3import { createGeminiChat } from '@tanstack/ai-gemini'
4import { googleSearchTool } from '@tanstack/ai-gemini/tools'
5​
6const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
7const DUMMY_KEY = 'sk-e2e-test-dummy-key'
8​
9const lookupCaucus = toolDefinition({
10 name: 'lookup_caucus',
11 description: 'Look up a caucus location by county',
12 inputSchema: {
13 type: 'object',
14 properties: { county: { type: 'string' } },
15 required: ['county'],
16 },
17}).server(async () => ({ location: 'Library' }))
18​
19export const Route = createFileRoute('/api/gemini-server-tools-wire')({
20 server: {
21 handlers: {
22 POST: async () => {
23 const adapter = createGeminiChat('gemini-3.6-flash', DUMMY_KEY, {
24 baseURL: `${LLMOCK_DEFAULT_BASE}/gemini-server-tools`,
25 })
26 let text = ''
27 let error: string | undefined
28 const tools: Array<string> = []
29 try {
30 for await (const chunk of chat({
31 threadId: 'gemini-server-tools-wire',
32 adapter,
33 messages: [{ role: 'user', content: 'Search and look up.' }],
34 tools: [googleSearchTool(), lookupCaucus],
35 })) {
36 if (chunk.type === 'TOOL_CALL_START') tools.push(chunk.toolCallName)
37 if (chunk.type === 'TEXT_MESSAGE_CONTENT') text += chunk.delta
38 if (chunk.type === 'RUN_ERROR') error = chunk.message
39 }
40 } catch (caught) {
41 error = caught instanceof Error ? caught.message : String(caught)
42 }
43 return Response.json({ ok: error === undefined, error, text, tools })
44 },
45 },
46 },
47})