Forked from vadimfedenko/analyze-images
scripts / native-attachment-live.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const {spawnSync} = require('node:child_process');
const {LMStudioClient,ChatMessage} = require('@lmstudio/sdk');
const {inlineImage} = require('../dist/inlineImage');
const {persistChatImage} = require('../dist/persistentImage');
const bytes = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aWQAAAABJRU5ErkJggg==','base64');
async function main() {
if (process.argv.includes('--child')) {
const client = new LMStudioClient();
const file = await persistChatImage(client,inlineImage(client,'synthetic-lifetime-check.png',bytes));
const target = await file.getFilePath();
console.log(JSON.stringify({identifier:file.identifier,target,size:file.sizeBytes}));
return;
}
const child = spawnSync(process.execPath,[__filename,'--child'],{encoding:'utf8',timeout:20000,windowsHide:true});
assert.equal(child.status,0,child.stderr);
const saved = JSON.parse(child.stdout.trim());
assert.match(saved.identifier,/^\d+ - aia-[a-f0-9-]+\.png$/);
assert.equal(path.basename(saved.target),saved.identifier);
const copy = path.join(path.dirname(saved.target),saved.identifier.replace('.png','-branch-test.png'));
const client = new LMStudioClient();
try {
// The creator's entire process/client has exited. Native attachment must remain.
assert.deepEqual(await fs.readFile(saved.target),bytes);
await fs.copyFile(path.join(path.dirname(saved.target),saved.identifier),copy);
assert.deepEqual(await fs.readFile(copy),bytes);
const file = ChatMessage.from({role:'user',content:[{type:'file',name:'test.png',identifier:saved.identifier,sizeBytes:saved.size,fileType:'image'}]}).getFiles(client)[0];
assert.deepEqual(await fs.readFile(await file.getFilePath()),bytes);
console.log(JSON.stringify({passed:true,creatorDisconnected:true,nativeBranchCopy:true,freshSdkClientRead:true}));
} finally {
// Delete only these two synthetic test files; no recursive cleanup.
await fs.unlink(copy).catch(()=>{});
await fs.unlink(saved.target);
}
}
main().then(()=>process.exit(0)).catch(error=>{console.error(error.message);process.exit(1);});