File size: 5,890 Bytes
227c43a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { io } from 'socket.io-client';

const API_BASE_URL = '/api';
const socket = io();

// Helper function to check for HTML response
const handleResponse = async (response) => {
  const contentType = response.headers.get('content-type');
  
  // Check if response is HTML instead of JSON
  if (contentType && contentType.includes('text/html')) {
    const text = await response.text();
    const firstChars = text.substring(0, 30); // Get first 30 chars for error message
    throw new Error(`Received HTML instead of JSON: ${firstChars}...`);
  }
  
  if (!response.ok) {
    try {
      const error = await response.json();
      throw new Error(error.message || `API error: ${response.status}`);
    } catch (e) {
      // If we can't parse the JSON, throw the original response text
      const text = await response.text();
      throw new Error(`API error (${response.status}): ${text.substring(0, 100)}...`);
    }
  }
  
  return response.json();
};

export const fetchPlugins = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/plugins`);
    return await handleResponse(response);
  } catch (error) {
    console.error('Error fetching plugins:', error);
    throw error;
  }
};

export const fetchPlugin = async (pluginKey) => {
  try {
    const response = await fetch(`${API_BASE_URL}/plugin/${pluginKey}`);
    return await handleResponse(response);
  } catch (error) {
    console.error(`Error fetching plugin ${pluginKey}:`, error);
    throw error;
  }
};

export const runPlugin = async (pluginKey, params, onProgress) => {
  return new Promise((resolve, reject) => {
    // Set up socket event listeners
    socket.on('plugin_progress', (data) => {
      if (data.plugin_key === pluginKey && onProgress) {
        onProgress(data.progress, data.message);
      }
    });

    socket.on('plugin_result', (data) => {
      if (data.plugin_key === pluginKey) {
        resolve(data.result);
      }
    });

    socket.on('plugin_error', (data) => {
      if (data.plugin_key === pluginKey) {
        reject(new Error(data.error));
      }
    });

    // Emit the run_plugin event
    socket.emit('run_plugin', {
      plugin_key: pluginKey,
      params: params
    });
  });
};

export const fetchGlossary = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/glossary`);
    return await handleResponse(response);
  } catch (error) {
    console.error('Error fetching glossary:', error);
    throw error;
  }
};

export const fetchCategory = async (category) => {
  try {
    const response = await fetch(`${API_BASE_URL}/category/${category}`);
    return await handleResponse(response);
  } catch (error) {
    console.error(`Error fetching category ${category}:`, error);
    throw error;
  }
};

export const fetchEducationalContent = async (pluginKey) => {
  try {
    const response = await fetch(`${API_BASE_URL}/educational/${pluginKey}`);
    return await handleResponse(response);
  } catch (error) {
    console.error(`Error fetching educational content for ${pluginKey}:`, error);
    throw error;
  }
};

export const validateParameters = async (pluginKey, params) => {
  try {
    const response = await fetch(`${API_BASE_URL}/validate/${pluginKey}`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(params),
    });
    return await handleResponse(response);
  } catch (error) {
    console.error(`Error validating parameters for ${pluginKey}:`, error);
    throw error;
  }
};

// Circuit Designer API Services

export const getAvailableGates = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/gates`);
    return await handleResponse(response);
  } catch (error) {
    console.error('Error fetching available gates:', error);
    throw error;
  }
};

export const runCircuitSimulation = async (circuit, shots) => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/simulate`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ circuit, shots }),
    });
    return await handleResponse(response);
  } catch (error) {
    console.error('Error running circuit simulation:', error);
    throw error;
  }
};

export const saveCircuit = async (circuitData) => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/save`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(circuitData),
    });
    return await handleResponse(response);
  } catch (error) {
    console.error('Error saving circuit:', error);
    throw error;
  }
};

export const loadCircuit = async (circuitId) => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/load/${circuitId}`);
    return await handleResponse(response);
  } catch (error) {
    console.error(`Error loading circuit ${circuitId}:`, error);
    throw error;
  }
};

export const getSavedCircuits = async () => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/saved`);
    return await handleResponse(response);
  } catch (error) {
    console.error('Error fetching saved circuits:', error);
    throw error;
  }
};

export const exportCircuitCode = async (circuit, format = 'cirq') => {
  try {
    const response = await fetch(`${API_BASE_URL}/circuit/export`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ circuit, format }),
    });
    return await handleResponse(response);
  } catch (error) {
    console.error('Error exporting circuit code:', error);
    throw error;
  }
};