Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 chatParamsFromRequestBody,
5 maxIterations,
6 toServerSentEventsResponse,
7} from '@tanstack/ai'
8import { memoryMiddleware } from '@tanstack/ai-memory'
9import { createTextAdapter } from '@/lib/providers'
10import { devtoolsMemoryAdapter } from '@/lib/devtools-memory-store'
11import type { StreamChunk } from '@tanstack/ai'
12
13/**
14 * Chat endpoint for the `/devtools-memory` E2E route. Mirrors `/api/chat` but
15 * wires `memoryMiddleware({ adapter: inMemory() })` so recall/save run and the
16 * middleware injects the `memory:state` CUSTOM chunk the client devtools bridge
17 * re-emits as `memory:*`. Scope is the per-test `testId`.
18 */
19export const Route = createFileRoute('/api/devtools-memory')({
20 server: {
21 handlers: {
22 POST: async ({ request }) => {
23 await import('@/lib/llmock-server').then((m) => m.ensureLLMock())
24 if (request.signal.aborted) {
25 return new Response(null, { status: 499 })
26 }
27
28 const abortController = new AbortController()
29
30 let params
31 try {
32 params = await chatParamsFromRequestBody(await request.json())
33 } catch (error) {
34 return new Response(
35 error instanceof Error ? error.message : 'Bad request',
36 { status: 400 },
37 )
38 }
39
40 const fp = params.forwardedProps as Record<string, unknown>
41 const testId = typeof fp.testId === 'string' ? fp.testId : undefined
42 const aimockPort =
43 fp.aimockPort != null ? Number(fp.aimockPort) : undefined
44 const threadId = testId ?? 'devtools-memory'
45
46 const adapterOptions = createTextAdapter(
47 'openai',
48 undefined,
49 aimockPort,
50 testId,
51 'chat',
52 )
53
54 try {
55 const memory = memoryMiddleware({
56 adapter: devtoolsMemoryAdapter,
57 scope: { threadId },
58 })
59
60 const stream = chat({
61 ...adapterOptions,
62 tools: [],
63 systemPrompts: [
64 'You are a helpful assistant with long-term memory.',
65 ],
66 middleware: [memory],
67 agentLoopStrategy: maxIterations(5),
68 messages: params.messages,
69 threadId: params.threadId,
70 runId: params.runId,
71 abortController,
72 })
73
74 return toServerSentEventsResponse(
75 stream as AsyncIterable<StreamChunk>,
76 { abortController },
77 )
78 } catch (error) {
79 console.error('[api.devtools-memory] Error:', error)
80 if (
81 (error instanceof Error && error.name === 'AbortError') ||
82 abortController.signal.aborted
83 ) {
84 return new Response(null, { status: 499 })
85 }
86 return new Response(
87 JSON.stringify({
88 error: error instanceof Error ? error.message : 'error',
89 }),
90 { status: 500, headers: { 'Content-Type': 'application/json' } },
91 )
92 }
93 },
94 },
95 },
96})