-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (39 loc) · 1.28 KB
/
index.js
File metadata and controls
42 lines (39 loc) · 1.28 KB
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
const https = require('https');
/**
* Parse natural language into structured JDON intent using JSONFIRST.
* Use the returned JDON as a deterministic context before calling OpenAI.
*
* @param {string} text - Raw natural language input
* @param {string} apiKey - Your JSONFIRST API key (jsonfirst.com/dashboard)
* @param {object} [options] - { mode: 'ANTI_CREDIT_WASTE_V2' | 'MAX_PERFORMANCE' }
* @returns {Promise<object>} JDON structured intent
*/
function parseIntent(text, apiKey, options = {}) {
const payload = JSON.stringify({
text,
mode: options.mode || 'ANTI_CREDIT_WASTE_V2'
});
return new Promise((resolve, reject) => {
const req = https.request({
hostname: 'jsonfirst.com',
path: '/api/convert',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'Content-Length': Buffer.byteLength(payload)
}
}, (res) => {
let data = '';
res.on('data', chunk => { data += chunk; });
res.on('end', () => {
try { resolve(JSON.parse(data)); }
catch (e) { reject(new Error('Invalid JSON response from JSONFIRST')); }
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
module.exports = { parseIntent };