Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { summarize } from '@tanstack/ai'
3import { anthropicSummarize } from '@tanstack/ai-anthropic'
4import { geminiSummarize } from '@tanstack/ai-gemini'
5import { grokSummarize } from '@tanstack/ai-grok'
6import { openaiSummarize } from '@tanstack/ai-openai'
7import { ollamaSummarize } from '@tanstack/ai-ollama'
8import { openRouterSummarize } from '@tanstack/ai-openrouter'
9import type { AnySummarizeAdapter } from '@tanstack/ai'
10
11type Provider =
12 | 'openai'
13 | 'anthropic'
14 | 'gemini'
15 | 'ollama'
16 | 'grok'
17 | 'openrouter'
18
19export const Route = createFileRoute('/api/summarize')({
20 server: {
21 handlers: {
22 POST: async ({ request }) => {
23 const body = await request.json()
24 const {
25 text,
26 maxLength = 100,
27 style = 'concise',
28 stream = false,
29 } = body
30 const data = body.data || {}
31 const provider: Provider = data.provider || body.provider || 'openai'
32 // Don't set a global default - let each adapter use its own default model
33 const model: string | undefined = data.model || body.model
34
35 try {
36 // Default models per provider
37 const defaultModels: Record<Provider, string> = {
38 anthropic: 'claude-sonnet-4-5',
39 gemini: 'gemini-2.5-flash',
40 grok: 'grok-build-0.1',
41 ollama: 'mistral:7b',
42 openai: 'gpt-4o-mini',
43 openrouter: 'openai/gpt-4o-mini',
44 }
45
46 // Determine the actual model being used
47 const actualModel = model || defaultModels[provider]
48
49 // Pre-define adapter factories per provider. The map is typed as the
50 // adapter union so each provider's summarize adapter unifies cleanly
51 // when handed to summarize() below.
52 const adapterConfig: Record<Provider, () => AnySummarizeAdapter> = {
53 anthropic: () => anthropicSummarize(actualModel as any),
54 gemini: () => geminiSummarize(actualModel as any),
55 grok: () => grokSummarize(actualModel as any),
56 ollama: () => ollamaSummarize(actualModel as any),
57 openai: () => openaiSummarize(actualModel as any),
58 openrouter: () => openRouterSummarize(actualModel as any),
59 }
60
61 // Select the provider's summarize adapter
62 const adapter = adapterConfig[provider]()
63
64 console.log(
65 `>> summarize with model: ${actualModel} on provider: ${provider} (stream: ${stream})`,
66 )
67
68 if (stream) {
69 // Streaming mode
70 const encoder = new TextEncoder()
71 const readable = new ReadableStream({
72 async start(controller) {
73 try {
74 const streamResult = summarize({
75 adapter,
76 text,
77 maxLength,
78 style,
79 stream: true,
80 })
81
82 for await (const chunk of streamResult) {
83 const data = JSON.stringify({
84 type: chunk.type,
85 delta: 'delta' in chunk ? chunk.delta : undefined,
86 content: 'content' in chunk ? chunk.content : undefined,
87 provider,
88 model: ('model' in chunk && chunk.model) || actualModel,
89 })
90 controller.enqueue(encoder.encode(`data: ${data}\n\n`))
91 }
92
93 controller.enqueue(encoder.encode('data: [DONE]\n\n'))
94 controller.close()
95 } catch (error: any) {
96 const errorData = JSON.stringify({
97 type: 'error',
98 error: error.message || 'An error occurred',
99 })
100 controller.enqueue(encoder.encode(`data: ${errorData}\n\n`))
101 controller.close()
102 }
103 },
104 })
105
106 return new Response(readable, {
107 status: 200,
108 headers: {
109 'Content-Type': 'text/event-stream',
110 'Cache-Control': 'no-cache',
111 Connection: 'keep-alive',
112 },
113 })
114 }
115
116 // Non-streaming mode
117 const result = await summarize({
118 adapter,
119 text,
120 maxLength,
121 style,
122 })
123
124 return new Response(
125 JSON.stringify({
126 summary: result.summary,
127 provider,
128 model: result.model || actualModel,
129 }),
130 {
131 status: 200,
132 headers: { 'Content-Type': 'application/json' },
133 },
134 )
135 } catch (error: any) {
136 console.error('[API Route] Error in summarize request:', error)
137 return new Response(
138 JSON.stringify({
139 error: error.message || 'An error occurred',
140 }),
141 {
142 status: 500,
143 headers: { 'Content-Type': 'application/json' },
144 },
145 )
146 }
147 },
148 },
149 },
150})