Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 createChatOptions,
5 maxIterations,
6 toServerSentEventsResponse,
7} from '@tanstack/ai'
8import { memoryMiddleware } from '@tanstack/ai-memory'
9import { anthropicText } from '@tanstack/ai-anthropic'
10import { geminiText } from '@tanstack/ai-gemini'
11import { grokText } from '@tanstack/ai-grok'
12import { openaiText } from '@tanstack/ai-openai'
13import { ollamaText } from '@tanstack/ai-ollama'
14import { openRouterText } from '@tanstack/ai-openrouter'
15import {
16 lastRecallByThread,
17 memoryAdapter,
18 panelMemoryScope,
19 panelScopeKey,
20} from '@/lib/memory-store'
21import type { Provider } from '@/lib/model-selection'
22
23const SYSTEM_PROMPT = `You are a helpful, friendly assistant with long-term memory.
24
25Earlier facts the user shared may be injected into your system prompt under a
26"memory" heading. When they are, use them to answer — for example, if the user
27tells you their name in one turn and asks for it in a later turn, recall it from
28memory rather than saying you don't know.`
29
30/**
31 * Chat endpoint for the `/memory` demo. Identical in spirit to `/api/chat`,
32 * minus the guitar tools and trace recording, plus a `memoryMiddleware` wired
33 * to the shared {@link memoryAdapter} singleton so recall/save persist across
34 * requests. The middleware is built per request with a static scope derived
35 * from a client-supplied `threadId` plus server-trusted user/tenant constants
36 * (demo-only — production must derive every Scope field from validated session
37 * state, never from the request body alone).
38 */
39export const Route = createFileRoute('/api/memory-chat')({
40 server: {
41 handlers: {
42 POST: async ({ request }) => {
43 const requestSignal = request.signal
44 if (requestSignal.aborted) {
45 return new Response(null, { status: 499 })
46 }
47
48 const abortController = new AbortController()
49 const body = await request.json()
50 const messages = body.messages
51 const data = body.data || {}
52
53 const provider: Provider = data.provider || 'openai'
54 const model: string | undefined = data.model
55 // threadId may come from the client for multi-thread demo UX; user and
56 // tenant are always server-side constants (see panelMemoryScope).
57 const threadId: string =
58 typeof data.threadId === 'string' && data.threadId.length > 0
59 ? data.threadId
60 : 'panel-default-thread'
61 const scope = panelMemoryScope(threadId)
62
63 try {
64 const adapterConfig = {
65 anthropic: () =>
66 createChatOptions({
67 adapter: anthropicText((model || 'claude-sonnet-4-5') as any),
68 }),
69 gemini: () =>
70 createChatOptions({
71 adapter: geminiText((model || 'gemini-2.5-flash') as any),
72 }),
73 grok: () =>
74 createChatOptions({
75 adapter: grokText((model || 'grok-build-0.1') as any),
76 }),
77 ollama: () =>
78 createChatOptions({
79 adapter: ollamaText((model || 'mistral:7b') as any),
80 }),
81 openai: () =>
82 createChatOptions({
83 adapter: openaiText((model || 'gpt-4o') as any),
84 }),
85 openrouter: () =>
86 createChatOptions({
87 adapter: openRouterText((model || 'openai/gpt-4o') as any),
88 }),
89 }
90
91 const options = adapterConfig[provider]()
92 const { adapter } = options
93
94 console.log(
95 `>> memory chat: model ${model} on ${provider} (thread ${threadId})`,
96 )
97
98 const memory = memoryMiddleware({
99 adapter: memoryAdapter,
100 scope,
101 onRecall: (info) => {
102 lastRecallByThread.set(panelScopeKey(scope), info.result)
103 },
104 })
105
106 const stream = chat({
107 ...options,
108 adapter,
109 tools: [],
110 systemPrompts: [SYSTEM_PROMPT],
111 middleware: [memory],
112 agentLoopStrategy: maxIterations(5),
113 messages,
114 abortController,
115 })
116
117 return toServerSentEventsResponse(stream, { abortController })
118 } catch (error: any) {
119 console.error('[api.memory-chat] Error:', error?.message)
120 if (error.name === 'AbortError' || abortController.signal.aborted) {
121 return new Response(null, { status: 499 })
122 }
123 return new Response(
124 JSON.stringify({ error: error.message || 'An error occurred' }),
125 {
126 status: 500,
127 headers: { 'Content-Type': 'application/json' },
128 },
129 )
130 }
131 },
132 },
133 },
134})