-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanobot.js
172 lines (141 loc) · 4.41 KB
/
nanobot.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const vscode = require('vscode');
const http = require('http');
const https = require('https');
const url = require('url');
const NanoBotState = require('./nanobot_state');
class NanoBot {
static stop() {
NanoBotState.instance().stop();
}
static async perform(config, params, callback) {
const cartridge = await this.cartridge(config, params.cartridge);
this.stop();
if (config.NANO_BOTS_STREAM) {
this.stream_request(config, params, cartridge, callback);
} else {
this.non_stream_request(config, params, cartridge, callback);
}
}
static async cartridges(config) {
return await this.send_request(config, null, 'GET', '/cartridges', 1000);
}
static async cartridge(config, cartridge_id) {
return await this.send_request(
config,
{ id: cartridge_id },
'POST',
'/cartridges/source',
1000
);
}
static non_stream_request(config, params, cartridge, callback) {
const thread = NanoBotState.thread();
NanoBotState.instance().update(cartridge, {
status: 'pending',
started_at: Date.now(),
thread: thread
});
this.send_request(config, params, 'POST', '/cartridges').then((response) => {
if (
NanoBotState.instance().state.status !== 'stopped' &&
NanoBotState.instance().state.thread === thread
) {
NanoBotState.instance().update(cartridge, { status: 'finished' });
callback(response);
}
});
}
static stream_request(config, params, cartridge, callback) {
const thread = NanoBotState.thread();
NanoBotState.instance().update(cartridge, {
status: 'pending',
started_at: Date.now(),
thread: thread
});
this.send_request(config, params, 'POST', '/cartridges/stream').then((response) => {
let stream_id = response.id;
if (!stream_id) {
vscode.window.showErrorMessage('Nano Bots: No Stream ID received.');
return;
}
let state = '';
const streamChecker = () => {
if (
NanoBotState.instance().state.status === 'stopped' ||
NanoBotState.instance().state.thread !== thread
) {
return;
}
this.send_request(config, null, 'GET', '/cartridges/stream/' + stream_id).then(
(response) => {
let output = response.output;
if (state !== output) {
response.fragment = output.slice(state.length);
state = output;
callback(response);
}
if (response.state === 'finished') {
NanoBotState.instance().update(cartridge, { status: 'finished' });
} else {
setTimeout(streamChecker, 0);
}
}
);
};
setTimeout(streamChecker, 0);
});
}
static async send_request(config, params, method, path, timeout = undefined, retries = 0) {
return new Promise((resolve, reject) => {
const api_url = new url.URL(config.NANO_BOTS_API_ADDRESS);
const options = {
hostname: api_url.hostname,
port: api_url.port,
path: path,
method: method,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
NANO_BOTS_END_USER: 'vscode-' + config.NANO_BOTS_END_USER
}
};
const protocol = api_url.protocol.startsWith('https') ? https : http;
const request = protocol.request(options, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (error) {
reject(error);
}
});
});
request.on('error', async (error) => {
if (retries < 2) {
console.warn(`[${retries + 1}] RETRY`);
return await this.send_request(config, params, method, path, timeout, retries + 1);
} else {
vscode.window.showErrorMessage('Nano Bots: ' + error.message);
reject(error);
}
});
if (params) {
request.write(JSON.stringify(params));
}
request.end();
if (timeout) {
const timer = setTimeout(() => {
request.abort();
reject(new Error('Request timed out'));
}, timeout);
request.on('response', () => {
clearTimeout(timer);
});
}
});
}
}
module.exports = NanoBot;