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

api.provider-tool-dispatch-wire.ts

91 lines3.2 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat } from '@tanstack/ai'
3import type { Tool } from '@tanstack/ai'
4import type { Provider } from '@/lib/types'
5import { createTextAdapter } from '@/lib/providers'
6​
7/**
8 * Drives a provider with an ordinary function whose name collides with one of
9 * that provider's native tools, so the spec can assert what reached the wire.
10 *
11 * Gemini only, deliberately. The OpenAI half of the runtime-discriminator fix
12 * lives in `openai-base`'s `src/tools/tool-converter.ts`, and the only adapter
13 * that runs it is `ai-openai`'s `openaiText()` (it overrides
14 * `mapOptionsToRequest` to route through the full tool converter). Neither
15 * OpenAI-family provider in this app reaches that code:
16 *
17 * - `openai` -> `createOpenaiChat()`, the Chat Completions adapter,
18 * whose converter never dispatched on tool name.
19 * - `openai-compatible` -> inherits `OpenAIBaseResponsesTextAdapter`'s
20 * `mapOptionsToRequest`, which sends every tool
21 * through `convertToolsToResponsesFormat` as a
22 * function tool — no native dispatch at all.
23 *
24 * So an OpenAI case here would pass with or without the fix. That leg is
25 * covered where the code actually lives, in
26 * `packages/openai-base/tests/provider-tool-dispatch.test.ts`. Adding true E2E
27 * coverage would mean adding an `openai-responses` provider to this app.
28 */
29type CollisionProvider = Extract<Provider, 'gemini'>
30​
31const customTools = {
32 gemini: createCustomTool('google_search'),
33} satisfies Record<CollisionProvider, Tool>
34​
35export const Route = createFileRoute('/api/provider-tool-dispatch-wire')({
36 server: {
37 handlers: {
38 POST: async ({ request }) => {
39 await import('@/lib/llmock-server').then((module) =>
40 module.ensureLLMock(),
41 )
42 const url = new URL(request.url)
43 const provider = readProvider(url.searchParams.get('provider'))
44 const testId = url.searchParams.get('testId') ?? undefined
45​
46 if (!provider) {
47 return new Response('Unsupported provider', { status: 400 })
48 }
49​
50 try {
51 for await (const _ of chat({
52 threadId: 'provider-tool-dispatch-wire',
53 ...createTextAdapter(provider, undefined, undefined, testId),
54 messages: [
55 {
56 role: 'user',
57 content: '[provider-tool-dispatch] preserve custom tool',
58 },
59 ],
60 tools: [customTools[provider]],
61 })) {
62 // Drain the stream.
63 }
64 } catch (error) {
65 return Response.json({
66 ok: false,
67 error: error instanceof Error ? error.message : String(error),
68 })
69 }
70​
71 return Response.json({ ok: true })
72 },
73 },
74 },
75})
76​
77function createCustomTool(name: string): Tool {
78 return {
79 name,
80 description: 'Run an application function',
81 inputSchema: {
82 type: 'object',
83 properties: { query: { type: 'string' } },
84 required: ['query'],
85 },
86 }
88​
89function readProvider(value: string | null): CollisionProvider | undefined {
90 return value === 'gemini' ? value : undefined