Project Files
dist / generator.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = generate;
const promises_1 = require("fs/promises");
const fs_1 = require("fs");
const path_1 = require("path");
const config_1 = require("./config");
const common_1 = require("./tools/common");
const workingDirectoryInfo_1 = require("./workingDirectoryInfo");
const embedLocalImages_1 = require("./embedLocalImages");
async function generate(ctl, _chat) {
const cfg = ctl.getPluginConfig(config_1.configSchematics);
const shouldOverwrite = cfg.get("overwrite");
const includeThinking = cfg.get("includeThinking");
const includeToolCalls = cfg.get("includeToolCalls");
const emitMessagesJson = cfg.get("emitMessagesJson");
const embedImages = cfg.get("embedImages");
const workingDirectory = ctl.getWorkingDirectory();
const wdInfo = (0, workingDirectoryInfo_1.getWorkingDirectoryInfo)(workingDirectory);
const chatId = (0, path_1.basename)(workingDirectory);
(0, common_1.validateChatId)(chatId);
if (!wdInfo.looksLikeLMStudioWorkingDir) {
ctl.fragmentGenerated(`Error: unexpected working directory layout.\n` +
`Working directory (raw): ${wdInfo.workingDirectoryRaw}\n` +
`Working directory (trimmed): ${wdInfo.workingDirectoryTrimmed}\n` +
`parentDirName: ${wdInfo.parentDirName}\n` +
`chatId: ${wdInfo.chatId} (valid13Digits=${wdInfo.chatIdValid13Digits})`);
return;
}
const lmHome = wdInfo.lmHomeFromWorkingDir;
if (!lmHome) {
ctl.fragmentGenerated(`Error: unable to derive LM Studio home from working directory.\n` +
`Working directory (raw): ${wdInfo.workingDirectoryRaw}\n` +
`Working directory (trimmed): ${wdInfo.workingDirectoryTrimmed}\n` +
`parentDirName: ${wdInfo.parentDirName}\n` +
`chatId: ${wdInfo.chatId} (valid13Digits=${wdInfo.chatIdValid13Digits})`);
return;
}
const globalSource = (0, path_1.join)(lmHome, "conversations", `${chatId}.conversation.json`);
const localJson = (0, path_1.join)(workingDirectory, `${chatId}.conversation.json`);
const destMd = (0, path_1.join)(workingDirectory, `${chatId}.conversation.md`);
const destMessagesJson = (0, path_1.join)(workingDirectory, `${chatId}.conversation.messages.json`);
ctl.fragmentGenerated(`Exporting chat ${chatId}… [embedImages=${embedImages}, emitMessagesJson=${emitMessagesJson}]`);
if (!(0, fs_1.existsSync)(globalSource)) {
ctl.fragmentGenerated(`Error: conversation file not found: ${globalSource}\n` +
`Working directory (raw): ${wdInfo.workingDirectoryRaw}\n` +
`Working directory (trimmed): ${wdInfo.workingDirectoryTrimmed}\n` +
`isAbsolute: ${wdInfo.isAbsolute}\n` +
`resolvedPath: ${wdInfo.resolvedPath}`);
return;
}
await (0, common_1.ensureDir)(workingDirectory);
await (0, promises_1.copyFile)(globalSource, localJson);
const jsonBytes = await (0, common_1.safeFileSize)(localJson);
if (!shouldOverwrite && ((0, fs_1.existsSync)(destMd) || (emitMessagesJson && (0, fs_1.existsSync)(destMessagesJson)))) {
ctl.fragmentGenerated(`Skipped conversion (overwrite=false). JSON refreshed (${jsonBytes} bytes).\n` +
`Markdown: ${destMd}\n` +
(emitMessagesJson ? `Messages JSON: ${destMessagesJson}\n` : ""));
return;
}
const data = await (0, common_1.readJson)(localJson);
const messages = Array.isArray(data?.messages) ? data.messages : [];
if (emitMessagesJson) {
const payload = JSON.stringify({ messages }, null, 2);
await (0, promises_1.writeFile)(destMessagesJson, payload, "utf-8");
}
let markdown = (0, common_1.convertMessagesToMarkdown)(messages, {
includeThinking,
includeToolCalls,
embedImages,
});
if (embedImages) {
markdown = await (0, embedLocalImages_1.embedLocalImagesInMarkdown)(markdown, workingDirectory);
}
await (0, promises_1.writeFile)(destMd, markdown.endsWith("\n") ? markdown : markdown + "\n", "utf-8");
const mdBytes = await (0, common_1.safeFileSize)(destMd);
ctl.fragmentGenerated(`Export complete. Files:\n` +
`- ${destMd} (${mdBytes} bytes)\n` +
(emitMessagesJson ? `- ${destMessagesJson}\n` : ""));
}