-
Notifications
You must be signed in to change notification settings - Fork 4
/
connectorInstaller.js
115 lines (96 loc) · 3.42 KB
/
connectorInstaller.js
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
import { promises as fs, existsSync, mkdirSync } from 'fs';
import { join } from 'path';
import { app, net } from 'electron';
const configPath = join(app.getPath('userData'), 'appConfig.json');
const connectorsPath = join(app.getPath('userData'), 'connectors');
if (!existsSync(connectorsPath)) {
mkdirSync(connectorsPath, { recursive: true });
}
async function fetchUrl(url) {
return new Promise((resolve, reject) => {
const request = net.request(url);
request.on('response', (response) => {
const chunks = [];
response.on('data', (chunk) => {
chunks.push(chunk);
});
response.on('end', () => {
const buffer = Buffer.concat(chunks);
resolve(buffer);
});
});
request.on('error', (error) => {
reject(error);
});
request.end();
});
}
export async function installConnector(connectorName, githubReleaseApiUrl) {
const connectorPath = join(connectorsPath, connectorName);
if (!existsSync(connectorPath)) {
mkdirSync(connectorPath, { recursive: true });
}
console.log(`Installing connector: ${connectorName}`);
try {
// Fetch the latest release data from GitHub API
const releaseBuffer = await fetchUrl(githubReleaseApiUrl);
const releaseData = JSON.parse(releaseBuffer.toString());
// Filter out source code archives
const assetFiles = releaseData.assets.filter(asset => !asset.name.endsWith('.zip') && !asset.name.endsWith('.tar.gz'));
// Download each asset file
for (const asset of assetFiles) {
const assetPath = join(connectorPath, asset.name);
console.log(`Downloading asset: ${asset.name}`);
const assetBuffer = await fetchUrl(asset.browser_download_url);
await fs.writeFile(assetPath, assetBuffer);
console.log(`${asset.name} has been saved successfully.`);
}
console.log(`${connectorName} has been installed successfully.`);
} catch (error) {
console.error(`Failed to install ${connectorName}:`, error);
throw error;
}
}
async function readConfig() {
if (existsSync(configPath)) {
const data = await fs.readFile(configPath, 'utf-8');
return JSON.parse(data);
}
return {};
}
async function writeConfig(config) {
await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8');
}
async function firstTimeSetup() {
try {
let config = await readConfig();
if (!config.firstRunComplete) {
console.log('Running first-time setup...');
const connectorsToInstall = [
{
name: 'prompt-mixer-open-ai-connector',
api_url: 'https://api.github.com/repos/PromptMixerDev/prompt-mixer-open-ai-connector/releases/latest',
},
{
name: 'prompt-mixer-anthropic-ai-connector',
api_url: 'https://api.github.com/repos/PromptMixerDev/prompt-mixer-anthropic-ai-connector/releases/latest',
},
];
for (const connector of connectorsToInstall) {
try {
await installConnector(connector.name, connector.api_url);
} catch (error) {
console.error(`Failed to install connector ${connector.name} error: ${error}`);
}
}
config.firstRunComplete = true;
await writeConfig(config);
console.log('First-time setup completed.');
} else {
console.log('First-time setup already completed. Skipping...');
}
} catch (error) {
console.error(`First-time setup error: ${error}`);
}
}
export default firstTimeSetup;