Create code/ss-to-kode.js
Browse files- code/ss-to-kode.js +49 -0
code/ss-to-kode.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const WebSocket = require('ws');
|
| 2 |
+
|
| 3 |
+
async function ss2code(imageBuffer) {
|
| 4 |
+
return new Promise((resolve, reject) => {
|
| 5 |
+
const ws = new WebSocket('wss://imagetoappv2.ngrok.app/generate-code');
|
| 6 |
+
let finalCode = '';
|
| 7 |
+
|
| 8 |
+
ws.on('open', () => {
|
| 9 |
+
console.log('Connect to WebSocket');
|
| 10 |
+
ws.send(JSON.stringify({
|
| 11 |
+
generationType: 'create',
|
| 12 |
+
image: `data:image/jpeg;base64,${imageBuffer.toString('base64')}`,
|
| 13 |
+
inputMode: 'image',
|
| 14 |
+
openAiApiKey: null,
|
| 15 |
+
openAiBaseURL: null,
|
| 16 |
+
anthropicApiKey: null,
|
| 17 |
+
screenshotOneApiKey: null,
|
| 18 |
+
isImageGenerationEnabled: true,
|
| 19 |
+
editorTheme: 'cobalt',
|
| 20 |
+
generatedCodeConfig: 'html_tailwind',
|
| 21 |
+
codeGenerationModel: 'gpt-4o-2024-05-13',
|
| 22 |
+
isTermOfServiceAccepted: false
|
| 23 |
+
}));
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
ws.on('message', (message) => {
|
| 27 |
+
const response = JSON.parse(message.toString());
|
| 28 |
+
if (response.type === 'setCode') {
|
| 29 |
+
finalCode = response.value;
|
| 30 |
+
} else if (response.type === 'status') {
|
| 31 |
+
console.log(response.value);
|
| 32 |
+
}
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
ws.on('close', () => {
|
| 36 |
+
console.log('WebSocket connection closed');
|
| 37 |
+
resolve(finalCode.trim());
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
ws.on('error', (error) => {
|
| 41 |
+
reject(new Error(error.message));
|
| 42 |
+
});
|
| 43 |
+
});
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// Usage:
|
| 47 |
+
const fs = require('fs');
|
| 48 |
+
const resp = await ss2code(fs.readFileSync('./ss.jpg'));
|
| 49 |
+
console.log(resp);
|