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
refactor: remove redundant ensureDataDir wrappers and duplicate formatFileSize
Adam Eivy7130235e61e0
diff --git a/client/src/components/cos/TaskAddForm.jsx b/client/src/components/cos/TaskAddForm.jsxindex 4429c80d24..c8ac211f2b 100644--- a/client/src/components/cos/TaskAddForm.jsx+++ b/client/src/components/cos/TaskAddForm.jsx@@ -2,7 +2,8 @@ import { useState, useEffect, useRef, useMemo, useCallback } from 'react';import { Plus, Image, X, ChevronDown, ChevronRight, Sparkles, Loader2, Paperclip, FileText, Zap, Bookmark, Ticket, GitBranch } from 'lucide-react';import toast from 'react-hot-toast';import * as api from '../../services/api';-import { processScreenshotUploads, processAttachmentUploads, formatFileSize } from '../../utils/fileUpload';+import { processScreenshotUploads, processAttachmentUploads } from '../../utils/fileUpload';+import { formatBytes } from '../../utils/formatters';export default function TaskAddForm({ providers, apps, onTaskAdded, compact = false, defaultApp = '' }) {const [newTask, setNewTask] = useState({ description: '', context: '', model: '', provider: '', app: defaultApp });@@ -525,7 +526,7 @@ export default function TaskAddForm({ providers, apps, onTaskAdded, compact = fa<span className="text-xs text-white truncate max-w-[120px]" title={a.originalName}>{a.originalName}</span>- <span className="text-xs text-gray-500">{formatFileSize(a.size)}</span>+ <span className="text-xs text-gray-500">{formatBytes(a.size)}</span></div><buttontype="button"diff --git a/client/src/utils/fileUpload.js b/client/src/utils/fileUpload.jsindex 6b06418878..b973518409 100644--- a/client/src/utils/fileUpload.js+++ b/client/src/utils/fileUpload.js@@ -240,12 +240,3 @@ export async function uploadAttachmentFile(file, options = {}) {reader.readAsDataURL(file);});}--/**- * Format file size for display- */-export function formatFileSize(bytes) {- if (bytes < 1024) return `${bytes} B`;- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;- return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;-}diff --git a/server/services/appActivity.js b/server/services/appActivity.jsindex 248dcc7450..b19dd163f0 100644--- a/server/services/appActivity.js+++ b/server/services/appActivity.js@@ -20,18 +20,11 @@ const DEFAULT_ACTIVITY = {}};-/**- * Ensure the data directory exists- */-async function ensureDataDir() {- await ensureDir(DATA_DIR);-}-/*** Load app activity data*/export async function loadAppActivity() {- await ensureDataDir();+ await ensureDir(DATA_DIR);const loaded = await readJSONFile(ACTIVITY_FILE, null);return loaded ? { ...DEFAULT_ACTIVITY, ...loaded } : { ...DEFAULT_ACTIVITY };@@ -41,7 +34,7 @@ export async function loadAppActivity() {* Save app activity data*/export async function saveAppActivity(activity) {- await ensureDataDir();+ await ensureDir(DATA_DIR);await writeFile(ACTIVITY_FILE, JSON.stringify(activity, null, 2));}diff --git a/server/services/apps.js b/server/services/apps.jsindex c19a9f493f..6ad95cd74e 100644--- a/server/services/apps.js+++ b/server/services/apps.js@@ -59,13 +59,6 @@ let appsCache = null;let cacheTimestamp = 0;const CACHE_TTL_MS = 2000; // Cache for 2 seconds to reduce file reads during rapid polling-/**- * Ensure data directory exists- */-async function ensureDataDir() {- await ensureDir(DATA_DIR);-}-/*** Load apps registry from disk (with caching).* Ensures the PortOS baseline app always exists.@@ -78,7 +71,7 @@ async function loadApps() {return appsCache;}- await ensureDataDir();+ await ensureDir(DATA_DIR);const data = await readJSONFile(APPS_FILE, { apps: {} });@@ -125,7 +118,7 @@ async function loadApps() {* Save apps registry to disk (and invalidate cache)*/async function saveApps(data) {- await ensureDataDir();+ await ensureDir(DATA_DIR);await writeFile(APPS_FILE, JSON.stringify(data, null, 2));// Update cache with saved dataappsCache = data;diff --git a/server/services/autobiography.js b/server/services/autobiography.jsindex e73d418528..5c984a4493 100644--- a/server/services/autobiography.js+++ b/server/services/autobiography.js@@ -166,27 +166,23 @@ const DEFAULT_DATA = {usedPrompts: []};-async function ensureDataDir() {- await ensureDir(DATA_DIR);-}-async function loadStories() {- await ensureDataDir();+ await ensureDir(DATA_DIR);return readJSONFile(STORIES_FILE, DEFAULT_DATA);}async function saveStories(data) {- await ensureDataDir();+ await ensureDir(DATA_DIR);await writeFile(STORIES_FILE, JSON.stringify(data, null, 2));}async function loadConfig() {- await ensureDataDir();+ await ensureDir(DATA_DIR);return readJSONFile(CONFIG_FILE, DEFAULT_CONFIG);}async function saveConfig(config) {- await ensureDataDir();+ await ensureDir(DATA_DIR);await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));}diff --git a/server/services/autonomousJobs.js b/server/services/autonomousJobs.jsindex 1fa27ea8e5..9eb3320cb1 100644--- a/server/services/autonomousJobs.js+++ b/server/services/autonomousJobs.js@@ -283,19 +283,12 @@ Phase 3 — Report:},]-/**- * Ensure data directory exists- */-async function ensureDataDir() {- await ensureDir(DATA_DIR)-}-/*** Load jobs from disk* @returns {Promise<Object>} Jobs data*/async function loadJobs() {- await ensureDataDir()+ await ensureDir(DATA_DIR)const loaded = await readJSONFile(JOBS_FILE, null)if (!loaded) {@@ -356,7 +349,7 @@ function mergeWithDefaults(loaded) {* Save jobs to disk*/async function saveJobs(data) {- await ensureDataDir()+ await ensureDir(DATA_DIR)data.lastUpdated = new Date().toISOString()const tmp = JOBS_FILE + '.tmp'await writeFile(tmp, JSON.stringify(data, null, 2))diff --git a/server/services/cosEvolution.js b/server/services/cosEvolution.jsindex 2e7d8f5616..128333646f 100644--- a/server/services/cosEvolution.js+++ b/server/services/cosEvolution.js@@ -15,7 +15,7 @@ import path from 'path'import { v4 as uuidv4 } from 'uuid'import { cosEvents } from './cosEvents.js'import * as lmStudioManager from './lmStudioManager.js'-import { safeJSONParse } from '../lib/fileUtils.js'+import { safeJSONParse, ensureDir } from '../lib/fileUtils.js'const DATA_DIR = path.join(process.cwd(), 'data', 'cos')const EVOLUTION_FILE = path.join(DATA_DIR, 'evolution.json')@@ -45,13 +45,6 @@ const DEFAULT_STATE = {// In-memory statelet evolutionState = null-/**- * Ensure data directory exists- */-async function ensureDataDir() {- await fs.mkdir(DATA_DIR, { recursive: true })-}-/*** Load evolution state* @returns {Promise<Object>} - Evolution state@@ -59,7 +52,7 @@ async function ensureDataDir() {async function loadState() {if (evolutionState) return evolutionState- await ensureDataDir()+ await ensureDir(DATA_DIR)const exists = await fs.access(EVOLUTION_FILE).then(() => true).catch(() => false)if (exists) {@@ -76,7 +69,7 @@ async function loadState() {* Save evolution state*/async function saveState() {- await ensureDataDir()+ await ensureDir(DATA_DIR)await fs.writeFile(EVOLUTION_FILE, JSON.stringify(evolutionState, null, 2))}diff --git a/server/services/history.js b/server/services/history.jsindex 323dd11988..291a002e76 100644--- a/server/services/history.js+++ b/server/services/history.js@@ -12,10 +12,6 @@ let historyCache = null;let cacheTimestamp = 0;const CACHE_TTL_MS = 2000; // 2 second cache TTL-async function ensureDataDir() {- await ensureDir(DATA_DIR);-}-async function loadHistory() {// Return cached data if still validconst now = Date.now();@@ -23,7 +19,7 @@ async function loadHistory() {return historyCache;}- await ensureDataDir();+ await ensureDir(DATA_DIR);historyCache = await readJSONFile(HISTORY_FILE, { entries: [] });cacheTimestamp = now;@@ -31,7 +27,7 @@ async function loadHistory() {}async function saveHistory(data) {- await ensureDataDir();+ await ensureDir(DATA_DIR);// Trim to max entriesif (data.entries.length > MAX_ENTRIES) {data.entries = data.entries.slice(-MAX_ENTRIES);diff --git a/server/services/memoryBM25.js b/server/services/memoryBM25.jsindex 028c49b1bb..f27a76485c 100644--- a/server/services/memoryBM25.js+++ b/server/services/memoryBM25.js@@ -17,6 +17,7 @@ import {deserializeIndex,getIndexStats} from '../lib/bm25.js'+import { ensureDir } from '../lib/fileUtils.js'const DATA_DIR = path.join(process.cwd(), 'data', 'cos', 'memory')const INDEX_FILE = path.join(DATA_DIR, 'bm25-index.json')@@ -25,13 +26,6 @@ const INDEX_FILE = path.join(DATA_DIR, 'bm25-index.json')let indexCache = nulllet isDirty = false-/**- * Ensure the data directory exists- */-async function ensureDataDir() {- await fs.mkdir(DATA_DIR, { recursive: true })-}-/*** Load the BM25 index from disk* @returns {Promise<Object>} - The BM25 index@@ -39,7 +33,7 @@ async function ensureDataDir() {async function loadIndex() {if (indexCache) return indexCache- await ensureDataDir()+ await ensureDir(DATA_DIR)const exists = await fs.access(INDEX_FILE).then(() => true).catch(() => false)@@ -81,7 +75,7 @@ async function loadIndex() {async function saveIndex() {if (!indexCache || !isDirty) return- await ensureDataDir()+ await ensureDir(DATA_DIR)const serialized = serializeIndex(indexCache)await fs.writeFile(INDEX_FILE, JSON.stringify(serialized, null, 2))isDirty = false@@ -95,7 +89,7 @@ async function saveIndex() {* @returns {Promise<Object>} - Index statistics*/async function rebuildIndex(memories) {- await ensureDataDir()+ await ensureDir(DATA_DIR)// Convert memories to documents for indexingconst documents = memories.map(m => ({diff --git a/server/services/missions.js b/server/services/missions.jsindex 102c165553..7038146b73 100644--- a/server/services/missions.js+++ b/server/services/missions.js@@ -9,20 +9,13 @@ import { promises as fs } from 'fs'import path from 'path'import { v4 as uuidv4 } from 'uuid'import { cosEvents } from './cosEvents.js'-import { safeJSONParse } from '../lib/fileUtils.js'+import { safeJSONParse, ensureDir } from '../lib/fileUtils.js'const DATA_DIR = path.join(process.cwd(), 'data', 'cos', 'missions')// In-memory cachelet missionsCache = null-/**- * Ensure data directory exists- */-async function ensureDataDir() {- await fs.mkdir(DATA_DIR, { recursive: true })-}-/*** Load all missions* @returns {Promise<Array>} - All missions@@ -30,7 +23,7 @@ async function ensureDataDir() {async function loadMissions() {if (missionsCache) return missionsCache- await ensureDataDir()+ await ensureDir(DATA_DIR)const files = await fs.readdir(DATA_DIR).catch(() => [])const missions = []@@ -55,7 +48,7 @@ async function loadMissions() {* @param {Object} mission - Mission object*/async function saveMission(mission) {- await ensureDataDir()+ await ensureDir(DATA_DIR)const filePath = path.join(DATA_DIR, `${mission.id}.json`)await fs.writeFile(filePath, JSON.stringify(mission, null, 2))diff --git a/server/services/productivity.js b/server/services/productivity.jsindex d83663d3f3..f1739268aa 100644--- a/server/services/productivity.js+++ b/server/services/productivity.js@@ -40,18 +40,11 @@ const DEFAULT_PRODUCTIVITY = {lastUpdated: null};-/**- * Ensure data directory exists- */-async function ensureDataDir() {- await ensureDir(DATA_DIR);-}-/*** Load productivity data*/export async function loadProductivity() {- await ensureDataDir();+ await ensureDir(DATA_DIR);const data = await readJSONFile(PRODUCTIVITY_FILE, DEFAULT_PRODUCTIVITY);// Merge with defaults to ensure all fields existreturn {@@ -65,7 +58,7 @@ export async function loadProductivity() {* Save productivity data*/async function saveProductivity(data) {- await ensureDataDir();+ await ensureDir(DATA_DIR);data.lastUpdated = new Date().toISOString();await writeFile(PRODUCTIVITY_FILE, JSON.stringify(data, null, 2));return data;