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