bybot / src /ai /aiClient.js
Franklin212's picture
A production-ready **PWA** (installable, offline-capable) that runs on Android and desktop.
2ac1520 verified
```javascript
export async function sendToAI(messages, {backend="provider", token=null} = {}) {
if (backend === "local") {
return fetch("http://127.0.0.1:11434/v1/chat", {
method:"POST",
headers:{"Content-Type":"application/json"},
body: JSON.stringify({messages})
})
.then(r=>r.json())
.then(d => d.choices?.[0]?.message?.content || JSON.stringify(d));
} else if (backend === "bytebot-lambda") {
return fetch("/api/v1/ai/chat", {
method:"POST",
headers:{"Content-Type":"application/json"},
body: JSON.stringify({messages})
}).then(r=>r.json()).then(d=>d.response);
} else if (backend === "provider") {
return fetch("https://api.openai.com/v1/chat/completions", {
method:"POST",
headers:{"Content-Type":"application/json","Authorization":"Bearer "+token},
body: JSON.stringify({ model:"gpt-3.5-turbo", messages })
}).then(r=>r.json()).then(d=>d.choices?.[0]?.message?.content);
}
return "No backend configured";
}
export function getAIConfig() {
const defaultConfig = {
backend: "provider",
localEndpoint: "http://localhost:11434",
providerToken: "",
model: "gpt-3.5-turbo"
};
try {
return JSON.parse(localStorage.getItem("aiConfig")) || defaultConfig;
} catch {
return defaultConfig;
}
}
export function saveAIConfig(config) {
localStorage.setItem("aiConfig", JSON.stringify(config));
}
```