File size: 2,962 Bytes
6cdce85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { NextRequest, NextResponse } from 'next/server';
import { datasetLoader } from '@/lib/dataset/loader';
import type { TaskType, Category } from '@/types';

// Server-side loader for API route (used for split info and fallback)
export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);

    const split = (searchParams.get('split') as 'train' | 'validation' | 'test') || 'test';
    const limit = Math.min(parseInt(searchParams.get('limit') || '50', 10), 100);
    const offset = parseInt(searchParams.get('offset') || '0', 10);
    const type = searchParams.get('type') as TaskType | null;
    const category = searchParams.get('category') as Category | null;
    const hasImage = searchParams.get('hasImage');
    const search = searchParams.get('search') || undefined;
    const codingOnly = searchParams.get('codingOnly') === 'true';

    // Ensure the split is loaded
    if (!datasetLoader.isLoaded(split)) {
      await datasetLoader.preloadSplit(split);
    }

    // Build filters
    const filters: {
      type?: TaskType;
      category?: Category;
      hasImage?: boolean;
      search?: string;
      codingOnly?: boolean;
    } = { codingOnly };

    if (type) filters.type = type;
    if (category) filters.category = category;
    if (hasImage !== null) filters.hasImage = hasImage === 'true';
    if (search) filters.search = search;

    const result = datasetLoader.filterExamples(split, filters, limit, offset);

    return NextResponse.json({
      examples: result.examples,
      total: result.total,
      split,
      offset,
      limit,
      hasMore: offset + result.examples.length < result.total,
    });
  } catch (error) {
    console.error('Examples API error:', error);

    const errorMessage =
      error instanceof Error ? error.message : 'Failed to load examples';

    return NextResponse.json(
      { error: errorMessage, examples: [], total: 0 },
      { status: 500 }
    );
  }
}

// Endpoint to get split info
export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    
    if (body.action === 'getSplitInfo') {
      const splitInfo = await datasetLoader.getSplitInfo();
      return NextResponse.json({ splitInfo });
    }
    
    if (body.action === 'getCodingCount') {
      const split = (body.split || 'test') as 'train' | 'validation' | 'test';
      
      // Ensure the split is loaded
      if (!datasetLoader.isLoaded(split)) {
        await datasetLoader.preloadSplit(split);
      }
      
      const codingProblems = datasetLoader.getCodingProblems(split);
      return NextResponse.json({ count: codingProblems.length, split });
    }

    return NextResponse.json({ error: 'Unknown action' }, { status: 400 });
  } catch (error) {
    console.error('Examples API POST error:', error);
    return NextResponse.json(
      { error: 'Failed to process request' },
      { status: 500 }
    );
  }
}