File size: 3,711 Bytes
7fcdb70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { getApiBaseUrl } from '@/config/api';

/**

 * Fetch available models from the backend

 */
export async function fetchAvailableModels(): Promise<string[]> {
  try {
    const response = await fetch(`${getApiBaseUrl()}/models`);
    if (!response.ok) {
      throw new Error('Failed to fetch models');
    }
    const data = await response.json();
    // Backend returns array directly
    return Array.isArray(data) ? data : (data.models || []);
  } catch (error) {
    console.error('Error fetching models:', error);
    // Return default model on error
    return ['microsoft/Fara-7B'];
  }
}

/**

 * Generate a random instruction from the backend

 */
export async function generateRandomQuestion(): Promise<string> {
  try {
    const response = await fetch(`${getApiBaseUrl()}/random-question`);
    if (!response.ok) {
      throw new Error('Failed to generate instruction');
    }
    const data = await response.json();
    return data.question || data.instruction || 'Search for the latest AI news';
  } catch (error) {
    console.error('Error generating question:', error);
    // Return a default question on error
    return 'Search for the latest news about AI agents';
  }
}

/**

 * Update step evaluation (vote)

 */
export async function updateStepEvaluation(

  traceId: string,

  stepId: string,

  evaluation: 'like' | 'dislike' | 'neutral'

): Promise<void> {
  try {
    const response = await fetch(`${getApiBaseUrl()}/traces/${traceId}/steps/${stepId}`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        step_evaluation: evaluation,
      }),
    });

    if (!response.ok) {
      console.warn('Failed to update step evaluation (endpoint may not exist)');
    }
  } catch (error) {
    console.warn('Error updating step evaluation:', error);
  }
}

/**

 * Update trace evaluation (overall task feedback)

 */
export async function updateTraceEvaluation(

  traceId: string,

  evaluation: 'success' | 'failed' | 'not_evaluated'

): Promise<void> {
  try {
    const response = await fetch(`${getApiBaseUrl()}/traces/${traceId}/evaluation`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        user_evaluation: evaluation,
      }),
    });

    if (!response.ok) {
      console.warn('Failed to update trace evaluation (endpoint may not exist)');
    }
  } catch (error) {
    console.warn('Error updating trace evaluation:', error);
  }
}

/**

 * Upload a trace to Modal's trace storage endpoint

 * Returns success status and any error message

 * 

 * Note: This calls the backend server which proxies to Modal with proper auth,

 * keeping Modal credentials server-side only.

 */
export async function uploadTraceToModal(traceData: object): Promise<{
  success: boolean;
  message?: string;
  error?: string;
}> {
  try {
    const response = await fetch(`${getApiBaseUrl()}/traces`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(traceData),
    });

    if (!response.ok) {
      const errorText = await response.text();
      console.error('Failed to upload trace:', errorText);
      return { success: false, error: `HTTP ${response.status}: ${errorText}` };
    }

    const result = await response.json();
    return result;
  } catch (error) {
    console.error('Error uploading trace:', error);
    return { 
      success: false, 
      error: error instanceof Error ? error.message : 'Unknown error' 
    };
  }
}