-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (51 loc) · 1.84 KB
/
Copy pathindex.js
File metadata and controls
55 lines (51 loc) · 1.84 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
43
44
45
46
47
48
49
50
51
52
53
54
55
const https = require('https');
/**
* JSONFIRST intent parser for Mistral AI pipelines.
* Adds a structured governance layer before Mistral processes user input.
*
* @param {string} text - Raw natural language input
* @param {string} apiKey - Your JSONFIRST API key
* @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();
});
}
/**
* Returns a Mistral-ready system prompt enriched with JSONFIRST structured intent.
* Inject into your Mistral chat completion for deterministic execution.
*
* @param {string} text - User input
* @param {string} apiKey - JSONFIRST API key
* @returns {Promise<string>} System prompt with embedded JDON context
*/
async function buildSystemPrompt(text, apiKey, basePrompt = '') {
const jdon = await parseIntent(text, apiKey);
return `${basePrompt}\n\nStructured intent for this request:\n${JSON.stringify(jdon.jdons?.[0] || jdon, null, 2)}\n\nExecute based on the structured intent above.`;
}
module.exports = { parseIntent, buildSystemPrompt };