primocode 1.1.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/api.js CHANGED
@@ -1,39 +1,39 @@
1
- /**
2
- * API client — comunicação com primocode-server
3
- */
4
-
5
- 'use strict';
6
-
7
- const https = require('https');
8
- const http = require('http');
9
- const { URL } = require('url');
10
-
11
- function request(serverUrl, endpoint, payload) {
12
- return new Promise((resolve, reject) => {
13
- const url = new URL(endpoint, serverUrl);
14
- const data = JSON.stringify(payload);
15
- const options = { hostname: url.hostname, port: url.port || (url.protocol==='https:'?443:80), path: url.pathname+url.search, method: 'POST', headers: { 'Content-Type':'application/json', 'Content-Length':Buffer.byteLength(data) } };
16
- const client = url.protocol==='https:' ? https : http;
17
- const req = client.request(options, (res) => { let body=''; res.on('data', c=>body+=c); res.on('end', () => { try { const json=JSON.parse(body); if (res.statusCode>=400) reject(new Error(json.erro||json.error||`HTTP ${res.statusCode}`)); else resolve(json); } catch { reject(new Error(`Resposta inválida: ${body.slice(0,200)}`)); } }); });
18
- req.on('error', reject); req.write(data); req.end();
19
- });
20
- }
21
-
22
- function requestStream(serverUrl, endpoint, payload, onEvent) {
23
- return new Promise((resolve, reject) => {
24
- const url = new URL(endpoint, serverUrl);
25
- const data = JSON.stringify(payload);
26
- const options = { hostname: url.hostname, port: url.port || (url.protocol==='https:'?443:80), path: url.pathname+url.search, method: 'POST', headers: { 'Content-Type':'application/json', 'Content-Length':Buffer.byteLength(data), 'Accept':'text/event-stream' } };
27
- const client = url.protocol==='https:' ? https : http;
28
- const req = client.request(options, (res) => { let buffer=''; res.on('data', chunk => { buffer+=chunk; const lines=buffer.split('\n'); buffer=lines.pop(); for (const line of lines) if (line.startsWith('data: ')) { try { const evt=JSON.parse(line.slice(6)); onEvent(evt); if (evt.done) resolve(); } catch {} } }); res.on('end', () => resolve()); });
29
- req.on('error', reject); req.write(data); req.end();
30
- });
31
- }
32
-
33
- function requestAct(serverUrl, messages, model) { return request(serverUrl, '/api/act', { messages, model }); }
34
-
35
- function getHealth(serverUrl) {
36
- return new Promise((resolve, reject) => { const url=new URL('/health',serverUrl); const client=url.protocol==='https:'?https:http; const req=client.get(url, (res)=>{ let body=''; res.on('data',c=>body+=c); res.on('end',()=>{ try { resolve(JSON.parse(body)); } catch { reject(new Error('Resposta inválida do health check')); } }); }); req.on('error',reject); });
37
- }
38
-
39
- module.exports = { request, requestStream, requestAct, getHealth };
1
+ /**
2
+ * API client — comunicação com primocode-server
3
+ */
4
+
5
+ 'use strict';
6
+
7
+ const https = require('https');
8
+ const http = require('http');
9
+ const { URL } = require('url');
10
+
11
+ function request(serverUrl, endpoint, payload) {
12
+ return new Promise((resolve, reject) => {
13
+ const url = new URL(endpoint, serverUrl);
14
+ const data = JSON.stringify(payload);
15
+ const options = { hostname: url.hostname, port: url.port || (url.protocol==='https:'?443:80), path: url.pathname+url.search, method: 'POST', headers: { 'Content-Type':'application/json', 'Content-Length':Buffer.byteLength(data) } };
16
+ const client = url.protocol==='https:' ? https : http;
17
+ const req = client.request(options, (res) => { let body=''; res.on('data', c=>body+=c); res.on('end', () => { try { const json=JSON.parse(body); if (res.statusCode>=400) reject(new Error(json.erro||json.error||`HTTP ${res.statusCode}`)); else resolve(json); } catch { reject(new Error(`Resposta inválida: ${body.slice(0,200)}`)); } }); });
18
+ req.on('error', reject); req.write(data); req.end();
19
+ });
20
+ }
21
+
22
+ function requestStream(serverUrl, endpoint, payload, onEvent) {
23
+ return new Promise((resolve, reject) => {
24
+ const url = new URL(endpoint, serverUrl);
25
+ const data = JSON.stringify(payload);
26
+ const options = { hostname: url.hostname, port: url.port || (url.protocol==='https:'?443:80), path: url.pathname+url.search, method: 'POST', headers: { 'Content-Type':'application/json', 'Content-Length':Buffer.byteLength(data), 'Accept':'text/event-stream' } };
27
+ const client = url.protocol==='https:' ? https : http;
28
+ const req = client.request(options, (res) => { let buffer=''; res.on('data', chunk => { buffer+=chunk; const lines=buffer.split('\n'); buffer=lines.pop(); for (const line of lines) if (line.startsWith('data: ')) { try { const evt=JSON.parse(line.slice(6)); onEvent(evt); if (evt.done) resolve(); } catch {} } }); res.on('end', () => resolve()); });
29
+ req.on('error', reject); req.write(data); req.end();
30
+ });
31
+ }
32
+
33
+ function requestAct(serverUrl, messages, model) { return request(serverUrl, '/api/act', { messages, model }); }
34
+
35
+ function getHealth(serverUrl) {
36
+ return new Promise((resolve, reject) => { const url=new URL('/health',serverUrl); const client=url.protocol==='https:'?https:http; const req=client.get(url, (res)=>{ let body=''; res.on('data',c=>body+=c); res.on('end',()=>{ try { resolve(JSON.parse(body)); } catch { reject(new Error('Resposta inválida do health check')); } }); }); req.on('error',reject); });
37
+ }
38
+
39
+ module.exports = { request, requestStream, requestAct, getHealth };