File size: 1,434 Bytes
7072a89
e1d8498
 
7e18e50
 
 
 
 
 
 
7e64514
7e18e50
7072a89
7e18e50
 
 
7072a89
7e64514
7072a89
 
e1d8498
 
 
7072a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e64514
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
const BASE = "/api";

async function request(method, path, body) {
  const res = await fetch(BASE + path, {
    method: method,
    headers: { "Content-Type": "application/json" },
    body: body ? JSON.stringify(body) : undefined,
  });

  const text = await res.text();

  let data;
  try {
    data = JSON.parse(text);
  } catch (e) {
    throw new Error("HTTP " + res.status + " -- server returned: " + text.slice(0, 200));
  }

  if (!data.success) throw new Error(data.error || "Request failed");
  return data;
}

export const api = {
  listModels: function() { return request("GET", "/models"); },
  getModel: function(id) { return request("GET", "/models/" + id); },
  createModel: function(body) { return request("POST", "/models", body); },
  updateModel: function(id, body) { return request("PATCH", "/models/" + id, body); },
  deleteModel: function(id) { return request("DELETE", "/models/" + id); },
  testModel: function(id, messages) {
    return request("POST", "/models/" + id + "/test", {
      messages: Array.isArray(messages) ? messages : undefined,
      prompt: typeof messages === "string" ? messages : undefined,
    });
  },
  toggleModel: function(id) { return request("POST", "/models/" + id + "/toggle"); },
  getStats: function() { return request("GET", "/stats"); },
  getHealth: function() { return request("GET", "/health"); },
  getProviders: function() { return request("GET", "/providers"); },
};