Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { chat, createChatOptions, toolDefinition } from '@tanstack/ai'
3import { createOpenaiChat } from '@tanstack/ai-openai'
4import { z } from 'zod'
5import type { LazyToolsConfig } from '@tanstack/ai'
6
7const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
8const DUMMY_KEY = 'sk-e2e-test-dummy-key'
9
10/**
11 * Wire-format coverage for the lazy-tools `lazyToolsConfig.includeDescription`
12 * knob.
13 *
14 * The synthetic discovery tool (`__lazy__tool__discovery__`) is what carries
15 * the lazy-tool catalog. Its `description` is rendered per `includeDescription`
16 * (`'none'` default / `'first-sentence'` / `'full'`) and sent to the provider
17 * as part of the `tools` array — but with aimock the model never reflects that
18 * text back to the browser, so the only place to observe it end-to-end is the
19 * provider request itself.
20 *
21 * This route drives the OpenAI chat adapter with two `lazy: true` tools and a
22 * caller-chosen `includeDescription`, against aimock, so the companion spec can
23 * inspect aimock's journal (`GET /v1/_requests`) and assert the discovery
24 * tool's catalog description actually crossed the wire with the configured
25 * detail level. The model response is irrelevant to the assertion.
26 */
27const searchInventory = toolDefinition({
28 name: 'search_inventory',
29 description: 'Search the guitar inventory by keyword. Returns matches.',
30 inputSchema: z.object({ query: z.string() }),
31 lazy: true,
32}).server(async () => JSON.stringify({ ok: true }))
33
34const checkStock = toolDefinition({
35 name: 'check_stock',
36 description: 'Check stock level for a guitar. Returns quantity on hand.',
37 inputSchema: z.object({ guitarId: z.number() }),
38 lazy: true,
39}).server(async () => JSON.stringify({ ok: true }))
40
41const VALID_INCLUDE = new Set<
42 NonNullable<LazyToolsConfig['includeDescription']>
43>(['none', 'first-sentence', 'full'])
44
45export const Route = createFileRoute('/api/lazy-tools-wire')({
46 server: {
47 handlers: {
48 POST: async ({ request }) => {
49 const url = new URL(request.url)
50 const testId = url.searchParams.get('testId') ?? undefined
51 const rawInclude = url.searchParams.get('includeDescription')
52 const includeDescription =
53 rawInclude != null &&
54 VALID_INCLUDE.has(
55 rawInclude as NonNullable<LazyToolsConfig['includeDescription']>,
56 )
57 ? (rawInclude as NonNullable<LazyToolsConfig['includeDescription']>)
58 : 'none'
59
60 const adapter = createOpenaiChat('gpt-5.5', DUMMY_KEY, {
61 baseURL: `${LLMOCK_DEFAULT_BASE}/v1`,
62 defaultHeaders: testId ? { 'X-Test-Id': testId } : undefined,
63 })
64
65 try {
66 for await (const _ of chat({
67 threadId: 'lazy-tools-wire',
68 ...createChatOptions({ adapter }),
69 messages: [
70 {
71 role: 'user',
72 content: '[lazy-wire] discover and describe inventory tools',
73 },
74 ],
75 tools: [searchInventory, checkStock],
76 lazyToolsConfig: { includeDescription },
77 })) {
78 // Drain the stream.
79 }
80 } catch (error) {
81 return new Response(
82 JSON.stringify({
83 ok: false,
84 error: error instanceof Error ? error.message : String(error),
85 }),
86 { status: 200, headers: { 'Content-Type': 'application/json' } },
87 )
88 }
89
90 return new Response(JSON.stringify({ ok: true }), {
91 status: 200,
92 headers: { 'Content-Type': 'application/json' },
93 })
94 },
95 },
96 },
97})