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

api.load-trace.ts

51 lines1.5 KB

1import { createFileRoute } from '@tanstack/react-router'
2import * as fs from 'node:fs'
3import * as path from 'node:path'
4​
5export const Route = createFileRoute('/api/load-trace')({
6 server: {
7 handlers: {
8 GET: async ({ request }) => {
9 try {
10 const url = new URL(request.url)
11 const traceId = url.searchParams.get('id')
12​
13 if (!traceId) {
14 return new Response(JSON.stringify({ error: 'Missing trace ID' }), {
15 status: 400,
16 headers: { 'Content-Type': 'application/json' },
17 })
18 }
19​
20 const traceDir = path.join(process.cwd(), 'test-traces')
21 const traceFile = path.join(traceDir, `${traceId}.json`)
22​
23 if (!fs.existsSync(traceFile)) {
24 return new Response(
25 JSON.stringify({ error: 'Trace file not found' }),
26 {
27 status: 404,
28 headers: { 'Content-Type': 'application/json' },
29 },
30 )
31 }
32​
33 const traceData = fs.readFileSync(traceFile, 'utf-8')
34 return new Response(traceData, {
35 status: 200,
36 headers: { 'Content-Type': 'application/json' },
37 })
38 } catch (error: any) {
39 console.error('[API] Error loading trace:', error)
40 return new Response(
41 JSON.stringify({ error: error.message || 'Failed to load trace' }),
42 {
43 status: 500,
44 headers: { 'Content-Type': 'application/json' },
45 },
46 )
47 }
48 },
49 },
50 },
51})