AI, fully local on your computer.
TypeScript aiai-toolskiwixllama-cpplocal-ailocal-llmofflineoffline-aioffline-firstprivacy-firstragwikipediazim
import { guardedHandleFor } from './guarded-handle'
import { IPC } from '../../shared/ipc'
import type { AppContext } from '../services/context'
import type { Collection } from '../../shared/types'
import {
createCollection,
deleteCollection,
getCollection,
listCollections,
projectOnlyDocumentIds,
renameCollection,
setCollectionArchived
} from '../services/collections'
import { deleteDocument, documentsDir } from '../services/ingestion'
import { tMain } from '../services/i18n'
import { workspaceAdmitsWork } from '../services/workspace-vault'
// IPC for document-organization collections (projects + the seeded built-ins;
// document-organization plan §16). Pure local SQLite writes — no network, no model calls.
//
// Audit privacy (plan §17): every collection event records id + type + affected COUNT
// ONLY — NEVER the collection/project NAME (a project name like "Divorce" is content-ish;
// the filename-logging allowance does NOT extend to it). Enforced by the sentinel-grep
// test in tests/integration/audit-ipc.test.ts.
export function registerCollectionsIpc(ctx: AppContext): void {
const ipcHandle = guardedHandleFor(ctx)
// DB-backed handlers require an unlocked workspace; surface a clean message instead of
// the raw "Workspace is locked" the `ctx.db` getter would throw mid-operation.
const requireUnlocked = (): void => {
// AUD-02: `workspaceAdmitsWork`, never a bare `isUnlocked()` — the workspace DB stays
// OPEN for the whole multi-second lock teardown, so a bare check admits work that then
// lazily respawns the sidecars that teardown just killed. This module's copy is unchanged.
if (!workspaceAdmitsWork(ctx.workspace)) {
throw new Error(tMain('main.docs.locked'))
}
}
ipcHandle(IPC.listCollections, (): Collection[] => {
requireUnlocked()
return listCollections(ctx.db)
})
ipcHandle(
IPC.createCollection,
(_e, name: string, opts?: { description?: string | null; color?: string | null }): Collection => {
requireUnlocked()
const coll = createCollection(ctx.db, name, 'project', {
description: opts?.description ?? null,
color: opts?.color ?? null
})
// Privacy: id + type only — never the name.
ctx.audit?.('collection_created', 'Project created', { collectionId: coll.id, type: coll.type })
return coll
}
)
ipcHandle(IPC.renameCollection, (_e, id: string, name: string): Collection => {
requireUnlocked()
const coll = renameCollection(ctx.db, id, name)
ctx.audit?.('collection_renamed', 'Project renamed', { collectionId: coll.id, type: coll.type })
return coll
})
ipcHandle(IPC.setCollectionArchived, (_e, id: string, archived: boolean): Collection => {
requireUnlocked()
const coll = setCollectionArchived(ctx.db, id, archived)
ctx.audit?.('collection_archived', 'Project archive state changed', {
collectionId: coll.id,
type: coll.type,
archived
})
return coll
})
// Delete a project, two modes (plan §12.3):
// - 'membershipOnly': drop the collection + its memberships (CASCADE); keep every doc.
// - 'withDocuments': additionally delete ONLY genuinely project-only docs (C2 predicate
// — never a Library member). The actual shred reuses ingestion `deleteDocument`.
ipcHandle(
IPC.deleteCollection,
(_e, id: string, mode: 'membershipOnly' | 'withDocuments'): void => {
requireUnlocked()
const coll = getCollection(ctx.db, id)
if (!coll) return
if (coll.builtin) throw new Error(tMain('main.collections.builtinUndeletable'))
let deletedCount = 0
if (mode === 'withDocuments') {
// Compute project-only docs BEFORE dropping the collection (CASCADE would remove
// the memberships the predicate reads). Then delete the collection, then shred the
// docs — order keeps the predicate accurate and the doc delete idempotent.
const onlyHere = projectOnlyDocumentIds(ctx.db, id)
deleteCollection(ctx.db, id)
for (const docId of onlyHere) {
deleteDocument(ctx.db, documentsDir(ctx.paths.workspacePath), docId)
deletedCount += 1
}
} else {
deleteCollection(ctx.db, id)
}
ctx.audit?.('collection_deleted', 'Project deleted', {
collectionId: id,
type: coll.type,
mode,
deletedDocumentCount: deletedCount
})
}
)
}