Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.summarize.ts

88 lines3.2 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { summarize, toServerSentEventsResponse } from '@tanstack/ai'
3import { createOpenaiSummarize } from '@tanstack/ai-openai'
4import { createAnthropicSummarize } from '@tanstack/ai-anthropic'
5import { createGeminiSummarize } from '@tanstack/ai-gemini'
6import { createOllamaSummarize } from '@tanstack/ai-ollama'
7import { createGrokSummarize } from '@tanstack/ai-grok'
8import { createOpenRouterSummarize } from '@tanstack/ai-openrouter'
9import type { Provider } from '@/lib/types'
10​
11const LLMOCK_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
12const LLMOCK_OPENAI = `${LLMOCK_BASE}/v1`
13const DUMMY_KEY = 'sk-e2e-test-dummy-key'
14​
15function createSummarizeAdapter(provider: Provider) {
16 const factories: Record<string, () => any> = {
17 openai: () =>
18 createOpenaiSummarize('gpt-4o', DUMMY_KEY, { baseURL: LLMOCK_OPENAI }),
19 anthropic: () =>
20 createAnthropicSummarize('claude-sonnet-4-5', DUMMY_KEY, {
21 baseURL: LLMOCK_BASE,
22 }),
23 gemini: () =>
24 createGeminiSummarize(DUMMY_KEY, 'gemini-2.0-flash', {
25 httpOptions: { baseUrl: LLMOCK_BASE },
26 }),
27 ollama: () => createOllamaSummarize('mistral', LLMOCK_BASE),
28 grok: () =>
29 createGrokSummarize('grok-3', DUMMY_KEY, { baseURL: LLMOCK_OPENAI }),
30 // Both OpenRouter provider rows use the OpenRouter summarize adapter:
31 // `createOpenRouterSummarize` wraps the OpenRouter chat-completions
32 // text adapter regardless of whether the caller selected the Chat
33 // Completions or Responses surface, so a single factory backs both
34 // matrix entries.
35 openrouter: () =>
36 createOpenRouterSummarize('openai/gpt-4o', DUMMY_KEY, {
37 serverURL: LLMOCK_OPENAI,
38 }),
39 'openrouter-responses': () =>
40 createOpenRouterSummarize('openai/gpt-4o', DUMMY_KEY, {
41 serverURL: LLMOCK_OPENAI,
42 }),
43 }
44 return factories[provider]?.()
46​
47export const Route = createFileRoute('/api/summarize')({
48 server: {
49 handlers: {
50 POST: async ({ request }) => {
51 await import('@/lib/llmock-server').then((m) => m.ensureLLMock())
52 const body = await request.json()
53 const { text, provider, stream: shouldStream } = body
54​
55 try {
56 const adapter = createSummarizeAdapter(provider)
57 if (!adapter) {
58 return new Response(
59 JSON.stringify({
60 error: `Provider ${provider} does not support summarize`,
61 }),
62 { status: 400, headers: { 'Content-Type': 'application/json' } },
63 )
64 }
65 const result = summarize({
66 adapter,
67 text,
68 stream: shouldStream ?? true,
69 })
70​
71 if (shouldStream === false) {
72 const summary = await result
73 return new Response(JSON.stringify({ summary }), {
74 headers: { 'Content-Type': 'application/json' },
75 })
76 }
77​
78 return toServerSentEventsResponse(result)
79 } catch (error: any) {
80 return new Response(JSON.stringify({ error: error.message }), {
81 status: 500,
82 headers: { 'Content-Type': 'application/json' },
83 })
84 }
85 },
86 },
87 },
88})