kuzco-cli/gui/js/kuzcoCore.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-03-09 02:15:01 -05:00
const fs = require('fs');
const path = require('path');
const os = require('os');
const fetch = require('node-fetch');
async function fetchData(url, options = {}) {
const { default: fetch } = await import('node-fetch');
const response = await fetch(url, options);
return response;
}
class KuzcoCore {
constructor() {
this.configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json');
2024-03-09 02:15:01 -05:00
this.API_KEY = this.loadApiKey();
2024-03-09 15:22:44 -05:00
this.controller = new AbortController();
this.isAborted = false;
2024-03-09 02:15:01 -05:00
}
loadApiKey() {
try {
if (fs.existsSync(this.configPath)) {
const configFile = fs.readFileSync(this.configPath);
const config = JSON.parse(configFile);
return config.API_KEY;
} else {
console.log('API Key config file does not exist. Please set up your API Key.');
return '';
}
2024-03-09 02:15:01 -05:00
} catch (error) {
console.error(`An error occurred while reading the API key: ${error.message}`);
return '';
}
}
apiKeyExists() {
return fs.existsSync(this.configPath) && this.API_KEY !== '';
}
2024-03-09 15:22:44 -05:00
abortFetch() {
this.isAborted = true;
this.controller.abort();
}
async sendPrompt(prompt, model) {
console.log("Model received in sendPrompt:", model)
2024-03-09 15:22:44 -05:00
this.controller = new AbortController();
const signal = this.controller.signal;
2024-03-09 05:56:54 -05:00
2024-03-09 15:22:44 -05:00
const timeoutId = setTimeout(() => this.controller.abort(), 25000);
2024-03-09 05:56:54 -05:00
2024-03-09 02:15:01 -05:00
try {
const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages: [{ role: 'user', 'content': prompt + '\n' }],
model: model,
2024-03-09 02:15:01 -05:00
stream: false,
}),
2024-03-09 05:56:54 -05:00
signal: signal,
2024-03-09 02:15:01 -05:00
});
2024-03-09 05:56:54 -05:00
clearTimeout(timeoutId);
2024-03-09 02:15:01 -05:00
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
2024-03-09 15:22:44 -05:00
2024-03-09 02:15:01 -05:00
} catch (error) {
2024-03-09 05:56:54 -05:00
clearTimeout(timeoutId);
2024-03-09 15:22:44 -05:00
const errorMessage = this.isAborted
? 'Request aborted by the user. Please try again.'
: 'Request timed out. Please try again.';
this.isAborted = false;
this.controller = new AbortController();
return error.name === 'AbortError'
? { error: errorMessage }
: { error: error.message };
2024-03-09 02:15:01 -05:00
}
}
}
module.exports = KuzcoCore;