Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Agent Drafts Service
*
* Persists generated content as drafts per agent.
* Drafts are stored as JSON files: data/agents/drafts/{agentId}.json
*/
import { join } from 'path';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { atomicWrite, ensureDir, PATHS, readJSONFileStrict, unreadableStoreError } from '../lib/fileUtils.js';
import { isTopLevelEntryName } from '../lib/pathSafety.js';
import { ServerError } from '../lib/errorHandler.js';
const DRAFTS_DIR = join(PATHS.agentPersonalities, 'drafts');
async function getDraftsPath(agentId) {
// agentId comes straight from a query string / body; it names the file, so it
// must be a single path segment (no `../` walk out of the drafts directory).
if (!isTopLevelEntryName(agentId)) {
throw new ServerError('Invalid agentId', { status: 400, code: 'VALIDATION_ERROR' });
}
await ensureDir(DRAFTS_DIR);
return join(DRAFTS_DIR, `${agentId}.json`);
}
async function loadDrafts(agentId) {
const filePath = await getDraftsPath(agentId);
// Absent initializes to []; an unreadable file throws so no caller can save over it.
const { ok, value } = await readJSONFileStrict(filePath, []);
if (!ok) throw unreadableStoreError(filePath);
return value;
}
async function saveDrafts(agentId, drafts) {
const filePath = await getDraftsPath(agentId);
await atomicWrite(filePath, drafts);
}
/**
* List all drafts for an agent
*/
export async function listDrafts(agentId) {
const drafts = await loadDrafts(agentId);
return drafts.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
}
/**
* Get a single draft by ID
*/
export async function getDraft(agentId, draftId) {
const drafts = await loadDrafts(agentId);
return drafts.find(d => d.id === draftId) || null;
}
/**
* Save a new draft
*/
export async function createDraft(agentId, draft) {
const drafts = await loadDrafts(agentId);
const entry = {
id: uuidv4(),
type: draft.type, // 'post' or 'comment'
status: 'draft',
title: draft.title || null,
content: draft.content,
submolt: draft.submolt || null,
postId: draft.postId || null,
parentId: draft.parentId || null,
postTitle: draft.postTitle || null,
accountId: draft.accountId || null,
createdAt: new Date().toISOString()
};
drafts.push(entry);
await saveDrafts(agentId, drafts);
console.log(`📋 Draft saved for agent ${agentId}: ${entry.type} "${entry.title || entry.content?.substring(0, 40)}..."`);
return entry;
}
/**
* Update an existing draft
*/
export async function updateDraft(agentId, draftId, updates) {
const drafts = await loadDrafts(agentId);
const idx = drafts.findIndex(d => d.id === draftId);
if (idx === -1) return null;
drafts[idx] = {
...drafts[idx],
...updates,
updatedAt: new Date().toISOString()
};
await saveDrafts(agentId, drafts);
return drafts[idx];
}
/**
* Delete a draft
*/
export async function deleteDraft(agentId, draftId) {
const drafts = await loadDrafts(agentId);
const filtered = drafts.filter(d => d.id !== draftId);
if (filtered.length === drafts.length) return false;
await saveDrafts(agentId, filtered);
console.log(`🗑️ Draft deleted for agent ${agentId}: ${draftId}`);
return true;
}