* Powerpoint File Creation (#5278) * wip * download card * UI for downloading * move to fs system with endpoint to pull files * refactor UI * final-pass * remove save-file-browser skill and refactor * remove fileDownload event * reset * reset file * reset timeout * persist toggle * Txt creation (#5279) * wip * download card * UI for downloading * move to fs system with endpoint to pull files * refactor UI * final-pass * remove save-file-browser skill and refactor * remove fileDownload event * reset * reset file * reset timeout * wip * persist toggle * add arbitrary text creation file * Add PDF document generation with markdown formatting (#5283) add support for branding in bottom right corner refactor core utils and frontend rendering * Xlsx document creation (#5284) add Excel doc & sheet creation * Basic docx creation (#5285) * Basic docx creation * add test theme support + styling and title pages * simplify skill selection * handle TG attachments * send documents over tg * lazy import * pin deps * fix lock * i18n for file creation (#5286) i18n for file-creation connect #5280 * theme overhaul * Add PPTX subagent for better results * forgot files * Add PPTX subagent for better results (#5287) * Add PPTX subagent for better results * forgot files * make sub-agent use proper tool calling if it can and better UI hints
109 lines
3.3 KiB
JavaScript
109 lines
3.3 KiB
JavaScript
const { log, conclude } = require("./helpers/index.js");
|
|
const { WorkspaceChats } = require("../models/workspaceChats.js");
|
|
const createFilesLib = require("../utils/agents/aibitat/plugins/create-files/lib.js");
|
|
const { safeJsonParse } = require("../utils/http/index.js");
|
|
|
|
(async () => {
|
|
try {
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const storageDirectory = await createFilesLib.getOutputDirectory();
|
|
if (!fs.existsSync(storageDirectory)) return;
|
|
|
|
const files = fs.readdirSync(storageDirectory);
|
|
if (files.length === 0) return;
|
|
|
|
// Get all storage filenames referenced in active (include: true) chats
|
|
const activeFileRefs = await getActiveStorageFilenames();
|
|
const filesToDelete = [];
|
|
for (const filename of files) {
|
|
const fullPath = path.join(storageDirectory, filename);
|
|
const stat = fs.statSync(fullPath);
|
|
|
|
// Skip files/folders that don't match our naming pattern and add to deletion list
|
|
if (!filename.match(/^[a-z]+-[a-f0-9-]{36}(\.\w+)?$/i)) {
|
|
filesToDelete.push({ path: fullPath, isDirectory: stat.isDirectory() });
|
|
continue;
|
|
}
|
|
|
|
// If file/folder is not referenced in any active chat, add to deletion list
|
|
if (!activeFileRefs.has(filename))
|
|
filesToDelete.push({ path: fullPath, isDirectory: stat.isDirectory() });
|
|
}
|
|
|
|
if (filesToDelete.length === 0) return;
|
|
|
|
log(`Found ${filesToDelete.length} orphaned files/folders to delete.`);
|
|
let deletedCount = 0;
|
|
let failedCount = 0;
|
|
for (const { path: itemPath, isDirectory } of filesToDelete) {
|
|
try {
|
|
if (isDirectory) fs.rmSync(itemPath, { recursive: true });
|
|
else fs.unlinkSync(itemPath);
|
|
deletedCount++;
|
|
} catch (error) {
|
|
failedCount++;
|
|
log(`Failed to delete ${itemPath}: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
log(
|
|
`Cleanup complete: deleted ${deletedCount} items, ${failedCount} failures.`
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
log(`Error during cleanup: ${error.message}`);
|
|
} finally {
|
|
conclude();
|
|
}
|
|
})();
|
|
|
|
/**
|
|
* Retrieves all storage filenames referenced in active (include: true) workspace chats.
|
|
* Searches through the outputs array in chat responses.
|
|
* Uses pagination to avoid loading all chats into memory at once.
|
|
* @param {number} batchSize - Number of chats to process per batch (default: 50)
|
|
* @returns {Promise<Set<string>>}
|
|
*/
|
|
async function getActiveStorageFilenames(batchSize = 50) {
|
|
const storageFilenames = new Set();
|
|
|
|
try {
|
|
let offset = 0;
|
|
let hasMore = true;
|
|
|
|
while (hasMore) {
|
|
const chats = await WorkspaceChats.where(
|
|
{ include: true },
|
|
batchSize,
|
|
{ id: "asc" },
|
|
offset
|
|
);
|
|
|
|
if (chats.length === 0) {
|
|
hasMore = false;
|
|
break;
|
|
}
|
|
|
|
for (const chat of chats) {
|
|
try {
|
|
const response = safeJsonParse(chat.response, { outputs: [] });
|
|
for (const output of response.outputs) {
|
|
if (output?.payload?.storageFilename)
|
|
storageFilenames.add(output.payload.storageFilename);
|
|
}
|
|
} catch {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
offset += chats.length;
|
|
hasMore = chats.length === batchSize;
|
|
}
|
|
} catch (error) {
|
|
console.error("[getActiveStorageFilenames] Error:", error.message);
|
|
}
|
|
|
|
return storageFilenames;
|
|
}
|