dist / main.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onLoad = onLoad;
const docx_1 = require("docx");
function normalizeText(text) {
return text.replace(/\r/g, "").trim();
}
async function exportToWord(context) {
try {
const messages = context.getChatMessages();
if (!messages || messages.length === 0) {
context.showToast("No hay mensajes para exportar", "info");
return;
}
const paragraphs = [];
paragraphs.push(new docx_1.Paragraph({
text: "EXPORTACION DE CHAT - LM STUDIO",
heading: docx_1.HeadingLevel.TITLE
}));
paragraphs.push(new docx_1.Paragraph({
children: [
new docx_1.TextRun({
text: "Fecha: " +
new Date().toLocaleString("es-ES"),
italics: true
})
]
}));
paragraphs.push(new docx_1.Paragraph(" "));
messages.forEach((msg, index) => {
const role = msg.role === "user"
? "USUARIO"
: msg.role === "assistant"
? "ASISTENTE"
: "SISTEMA";
const time = msg.timestamp
? new Date(msg.timestamp).toLocaleTimeString("es-ES")
: "--:--";
paragraphs.push(new docx_1.Paragraph({
children: [
new docx_1.TextRun({
text: `#${index + 1} [${time}] ${role}`,
bold: true
})
]
}));
paragraphs.push(new docx_1.Paragraph({
children: [
new docx_1.TextRun({
text: normalizeText(msg.content)
})
]
}));
paragraphs.push(new docx_1.Paragraph(" "));
});
const doc = new docx_1.Document({
sections: [
{
properties: {},
children: paragraphs
}
]
});
const blob = await docx_1.Packer.toBlob(doc);
const now = new Date();
const fileName = "chat-export-" +
now.toISOString().replace(/[:.]/g, "-") +
".docx";
await context.showSaveFileDialog({
filters: [
{
name: "Documento Word",
extensions: ["docx"]
}
],
defaultName: fileName
});
const handle = await window.showSaveFilePicker({
suggestedName: fileName,
types: [
{
description: "Documento Word",
accept: {
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": [".docx"]
}
}
]
});
const writable = await handle.createWritable();
await writable.write(blob);
await writable.close();
context.showToast("Documento Word exportado correctamente", "success");
}
catch (error) {
console.error(error);
context.showToast("Error exportando Word: " +
(error?.message || "Error desconocido"), "error");
}
}
async function onLoad(context) {
console.log("Plugin Export to Word cargado");
context.registerCommand({
id: "export-chat-to-word",
title: "Exportar Chat a Word",
description: "Exporta toda la conversación actual a DOCX",
handler: () => exportToWord(context)
});
context.registerChatBarButton({
id: "export-word-button",
label: "Export Word",
icon: "📘",
handler: () => exportToWord(context)
});
}