File size: 2,175 Bytes
60faa09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const MUAPI_BASE = "https://api.muapi.ai/api/v1";
const delay = (ms) => new Promise((r) => setTimeout(r, ms));

export async function callMuAPI({
  model, prompt, systemPrompt, temperature = 1.0, maxTokens = 2048,
  reasoning = false, imageUrl = null, apiKey,
}) {
  const modelName = model.replace(/^muapi:/, "");
  const isVision = !!imageUrl;
  const apiUrl = isVision ? `${MUAPI_BASE}/openrouter-vision` : `${MUAPI_BASE}/any-llm-models`;

  const payload = {
    prompt, system_prompt: systemPrompt, model: modelName,
    temperature: parseFloat(temperature), max_tokens: parseInt(maxTokens),
    reasoning: !!reasoning,
  };
  if (isVision) payload.images_list = [imageUrl];

  const response = await fetch(apiUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json", "x-api-key": apiKey },
    body: JSON.stringify(payload),
  });

  if (!response.ok) {
    const errText = await response.text();
    console.error("[MUAPI_LLM_ERROR]", errText);
    throw new Error(`MuAPI error: ${response.statusText}`);
  }

  const data = await response.json();
  const requestId = data.request_id;
  if (!requestId) throw new Error("Did not receive a request_id from MuAPI.");

  let completedText = "";
  let status = "processing";
  while (status === "processing") {
    await delay(1500);
    const checkRes = await fetch(`${MUAPI_BASE}/predictions/${requestId}/result`, {
      method: "GET",
      headers: { "Content-Type": "application/json", "x-api-key": apiKey },
    });
    if (checkRes.ok) {
      const checkData = await checkRes.json();
      status = checkData.status || checkData.state || "processing";
      if (status === "completed" || status === "succeeded") {
        completedText =
          checkData.outputs?.[0] ||
          (typeof checkData.output === "string" ? checkData.output : "") ||
          checkData.output?.text ||
          checkData.output?.choices?.[0]?.message?.content ||
          checkData.response || "";
        break;
      } else if (status === "failed") {
        throw new Error("Generation failed on MuAPI.");
      }
    }
  }
  return { text: completedText || "Hello! How can I help you?", usage: null };
}