Skip to content

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

6.5 KB
Raw Blame
import { noteReadinessChanged } from './readinessSignal.js';
import { join } from 'path';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { ensureDir, PATHS, readJSONFile, atomicWrite } from '../lib/fileUtils.js';
import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
import { isPlainObject } from '../lib/objects.js';
import { accountTypeCanSend, accountTypeHasReadState, sendViaForAccountType, syncModesForAccountType } from '../lib/messageTransport.js';

const ACCOUNTS_FILE = join(PATHS.messages, 'accounts.json');
// All account IDs share this file, including sync metadata writes.
const queueAccountWrite = createFileWriteQueue();

async function loadAccounts() {
  await ensureDir(PATHS.messages);
  const parsed = await readJSONFile(ACCOUNTS_FILE, {}, { strict: true, allowArray: false });
  if (!isPlainObject(parsed)) throw new Error('Invalid message accounts file: expected an object');
  return parsed;
}

async function saveAccounts(accounts) {
  await ensureDir(PATHS.messages);
  await atomicWrite(ACCOUNTS_FILE, accounts);
  noteReadinessChanged();
}

// `canSend`, `syncModes` and `hasReadState` are derived from the transport table, never
// stored: they flip with the code that implements a provider, not with data an install
// carries. `syncModes` tells the UI which sync actions to offer (#9968).
const withDerivedCapabilities = account => ({
  ...account,
  canSend: accountTypeCanSend(account.type),
  syncModes: [...syncModesForAccountType(account.type)],
  hasReadState: accountTypeHasReadState(account.type)
});

export async function listAccounts() {
  const accounts = await loadAccounts();
  return Object.values(accounts).map(withDerivedCapabilities).sort((a, b) => a.name.localeCompare(b.name));
}

export async function getAccount(id) {
  const accounts = await loadAccounts();
  return accounts[id] ? withDerivedCapabilities(accounts[id]) : null;
}

export async function createAccount(data) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    const id = uuidv4();
    accounts[id] = {
      id,
      name: data.name,
      type: data.type, // gmail, outlook, teams
      provider: sendViaForAccountType(data.type) ?? 'playwright',
      email: data.email || '',
      enabled: true,
      syncConfig: {
        maxAge: data.syncConfig?.maxAge || '30d',
        maxMessages: data.syncConfig?.maxMessages || 500,
        syncInterval: data.syncConfig?.syncInterval || 300000,
        // Ingest sent mail into the human-activity timeline as `message.sent` events
        // so Tribe-outreach reply detection can see a thread as answered (#2796).
        // Only Gmail has a sent-fetch path today; default it on there, off elsewhere.
        // Absent (existing accounts) is treated as on for Gmail by the readers, so a
        // migration isn't needed — this only makes new accounts' stored state explicit.
        ingestSent: data.syncConfig?.ingestSent ?? (data.type === 'gmail')
      },
      // Gmail send-as alias addresses (#2831), refreshed on each sync. Owner addresses
      // beyond the primary `email`; used to exclude the owner from received-message
      // participants so a 1:1 email delivered to an alias isn't misread as a group thread.
      // Additive/back-compat: existing accounts lack the key and readers treat absent as [].
      sendAsAliases: [],
      lastSyncAt: null,
      lastSyncStatus: null,
      createdAt: new Date().toISOString()
    };
    await saveAccounts(accounts);
    console.log(`📧 Message account created: ${id} (${data.type})`);
    return accounts[id];
  });
}

export async function updateAccount(id, updates) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    if (!accounts[id]) return null;
    const { name, email, enabled, syncConfig } = updates;
    if (name !== undefined) accounts[id].name = name;
    if (email !== undefined) accounts[id].email = email;
    if (enabled !== undefined) accounts[id].enabled = enabled;
    if (syncConfig) accounts[id].syncConfig = { ...accounts[id].syncConfig, ...syncConfig };
    accounts[id].updatedAt = new Date().toISOString();
    await saveAccounts(accounts);
    return accounts[id];
  });
}

export async function deleteAccount(id) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    if (!accounts[id]) return false;
    delete accounts[id];
    await saveAccounts(accounts);
    console.log(`🗑️ Message account deleted: ${id}`);
    return true;
  });
}

// Persist the account's Gmail send-as aliases (#2831), refreshed opportunistically on
// each successful sync. Owner addresses beyond the primary `email`; consumed by
// `messageActivityCandidates` to exclude ALL owner addresses (not just the primary)
// from received-message participants. Lowercased + deduped; absent → [] for readers.
export async function updateSendAsAliases(id, aliases = []) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    if (!accounts[id]) return null;
    accounts[id].sendAsAliases = Array.isArray(aliases)
      ? [...new Set(aliases.map((a) => String(a || '').trim().toLowerCase()).filter(Boolean))]
      : [];
    await saveAccounts(accounts);
    return accounts[id];
  });
}

export async function updateSyncStatus(id, status) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    if (!accounts[id]) return null;
    accounts[id].lastSyncAt = new Date().toISOString();
    accounts[id].lastSyncStatus = status;
    await saveAccounts(accounts);
    return accounts[id];
  });
}

// Stamp the reply-detection watermark (#2796): the last time this account
// successfully ingested sent mail into the activity timeline. The Tribe-outreach
// detector trusts an account as two-way only when this is set and recent, so an
// account is never trusted before its sent history actually exists.
//
// `partial` (#2820) records whether the sent window was FULLY covered this sync.
// When the sent pass truncates at its ceiling (>SENT_INGEST_MAX sent in the
// window) coverage is incomplete, so the detector must NOT trust this account's
// reply evidence — `sentCoveragePartial:true` drops it from the two-way set for
// the scan (fail closed). A later full sync clears it back to false.
export async function markSentIngested(id, { at = new Date().toISOString(), partial = false } = {}) {
  return queueAccountWrite(async () => {
    const accounts = await loadAccounts();
    if (!accounts[id]) return null;
    accounts[id].sentIngestedAt = at;
    accounts[id].sentCoveragePartial = partial;
    await saveAccounts(accounts);
    return accounts[id];
  });
}