Add document, knowledge, and automation subsystems

Migration 040 creates documents, knowledge_entries, and automation_rules
tables. Full-stack services (DocumentService, KnowledgeService,
AutomationService), IPC handlers, preload bridge, desktop-api types,
and domain records wired into the snapshot. UI pages follow next.
This commit is contained in:
ZacharyZhang-NY committed 2026-06-07 17:46:55 -04:00
1 parent 3b115e8bec
commit b5b8f3ba3c
13 files changed
+344 -1

No files matched your search

@@ -22,6 +22,9 @@ import { SdlcService } from "../services/sdlc-service";
import { AuditService } from "../services/audit-service";
import { RollbackService } from "../services/rollback-service";
import { TaskExecutionService } from "../services/task-execution-service";
import { DocumentService } from "../services/document-service";
import { KnowledgeService } from "../services/knowledge-service";
import { AutomationService } from "../services/automation-service";
export interface IpcDeps {
connection: DatabaseConnection;
@@ -48,6 +51,9 @@ export function registerIpcHandlers(deps: IpcDeps): void {
const sdlc = new SdlcService(deps.connection.db);
const audit = new AuditService(deps.connection.db);
const rollback = new RollbackService(deps.connection.db);
const documents = new DocumentService(deps.connection.db);
const knowledge = new KnowledgeService(deps.connection.db);
const automation = new AutomationService(deps.connection.db);
const onboarding = new OnboardingService({
db: deps.connection.db,
saveProviderCredential: (input) => providers.saveCredential(input),
@@ -104,6 +110,14 @@ export function registerIpcHandlers(deps: IpcDeps): void {
ipcMain.handle(IPC_CHANNELS.createGoal, (_event, input) => mutate(() => organization.createGoal(input)));
ipcMain.handle(IPC_CHANNELS.createProject, (_event, input) => mutate(() => organization.createProject(input)));
ipcMain.handle(IPC_CHANNELS.createWorkspace, (_event, input) => mutate(() => organization.createWorkspace(input)));
ipcMain.handle(IPC_CHANNELS.createDocument, (_event, input) => mutate(() => documents.create(input)));
ipcMain.handle(IPC_CHANNELS.updateDocument, (_event, input) => mutate(() => documents.update(input)));
ipcMain.handle(IPC_CHANNELS.deleteDocument, (_event, input) => mutate(() => documents.delete(input)));
ipcMain.handle(IPC_CHANNELS.createKnowledgeEntry, (_event, input) => mutate(() => knowledge.create(input)));
ipcMain.handle(IPC_CHANNELS.deleteKnowledgeEntry, (_event, input) => mutate(() => knowledge.delete(input)));
ipcMain.handle(IPC_CHANNELS.createAutomationRule, (_event, input) => mutate(() => automation.create(input)));
ipcMain.handle(IPC_CHANNELS.updateAutomationRule, (_event, input) => mutate(() => automation.update(input)));
ipcMain.handle(IPC_CHANNELS.deleteAutomationRule, (_event, input) => mutate(() => automation.delete(input)));
ipcMain.handle(IPC_CHANNELS.createApproval, (_event, input) => mutate(() => approvals.create(input)));
ipcMain.handle(IPC_CHANNELS.decideApproval, (_event, input) => mutate(() => approvals.decide(input)));
ipcMain.handle(IPC_CHANNELS.syncEmployeeIdentity, (_event, input) => mutate(() => identityAccess.syncEmployeeIdentity(input)));
@@ -0,0 +1,52 @@
export const documentsKnowledgeAutomationSchema = `
create table if not exists documents (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
title text not null,
doc_type text not null,
content text not null,
status text not null,
author_agent_id text references agents(id) on delete set null,
reviewer_agent_id text references agents(id) on delete set null,
project_id text references projects(id) on delete set null,
goal_id text references goals(id) on delete set null,
task_id text references tasks(id) on delete set null,
created_at text not null,
updated_at text not null
);
create index if not exists idx_documents_org on documents(org_id, doc_type);
create table if not exists knowledge_entries (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
topic text not null,
category text not null,
content text not null,
importance text not null,
source_type text not null,
source_id text,
created_at text not null,
updated_at text not null
);
create index if not exists idx_knowledge_org on knowledge_entries(org_id, category);
create table if not exists automation_rules (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
name text not null,
trigger_event text not null,
condition_json text not null,
action_type text not null,
action_config_json text not null,
priority integer not null,
enabled integer not null,
last_fired_at text,
fire_count integer not null default 0,
created_at text not null,
updated_at text not null
);
create index if not exists idx_automation_org on automation_rules(org_id, enabled);
`;
@@ -38,6 +38,7 @@ import { additionalConnectorsSchema } from "./migrations/036-additional-connecto
import { customProvidersSchema } from "./migrations/037-custom-providers";
import { providerAuthProfileCascadeSchema } from "./migrations/038-provider-auth-profile-cascade";
import { auditImmutabilityOrgTeardownSchema } from "./migrations/039-audit-immutability-org-teardown";
import { documentsKnowledgeAutomationSchema } from "./migrations/040-documents-knowledge-automation";
interface Migration {
version: number;
@@ -85,6 +86,7 @@ const MIGRATIONS: Migration[] = [
{ version: 37, name: "custom_providers", sql: customProvidersSchema },
{ version: 38, name: "provider_auth_profile_cascade", sql: providerAuthProfileCascadeSchema },
{ version: 39, name: "audit_immutability_org_teardown", sql: auditImmutabilityOrgTeardownSchema },
{ version: 40, name: "documents_knowledge_automation", sql: documentsKnowledgeAutomationSchema },
];
const SCHEMA_VERSION = MIGRATIONS[MIGRATIONS.length - 1].version;
@@ -0,0 +1,44 @@
import type { DatabaseSync } from "node:sqlite";
import type { CreateAutomationRuleInput, UpdateAutomationRuleInput, DeleteAutomationRuleInput } from "../../shared/commands";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
export class AutomationService {
private readonly audit: AuditService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
}
create(input: CreateAutomationRuleInput): string {
const id = createId("aut");
const now = new Date().toISOString();
this.db.prepare(`
insert into automation_rules(id, org_id, name, trigger_event, condition_json, action_type, action_config_json, priority, enabled, fire_count, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)
`).run(id, input.orgId, input.name.trim(), input.triggerEvent, JSON.stringify(input.conditions), input.actionType, JSON.stringify(input.actionConfig), input.priority, input.enabled ? 1 : 0, now, now);
this.audit.record({
orgId: input.orgId,
entityType: "automation_rule",
entityId: id,
actorType: "system",
actorId: null,
action: "created",
payload: { name: input.name.trim(), triggerEvent: input.triggerEvent },
});
return id;
}
update(input: UpdateAutomationRuleInput): void {
const result = this.db.prepare(`
update automation_rules set name = ?, trigger_event = ?, condition_json = ?, action_type = ?, action_config_json = ?, priority = ?, enabled = ?, updated_at = ?
where id = ? and org_id = ?
`).run(input.name.trim(), input.triggerEvent, JSON.stringify(input.conditions), input.actionType, JSON.stringify(input.actionConfig), input.priority, input.enabled ? 1 : 0, new Date().toISOString(), input.ruleId, input.orgId);
if (result.changes === 0) throw new Error("Automation rule was not found.");
}
delete(input: DeleteAutomationRuleInput): void {
const result = this.db.prepare("delete from automation_rules where id = ? and org_id = ?").run(input.ruleId, input.orgId);
if (result.changes === 0) throw new Error("Automation rule was not found.");
}
}
@@ -0,0 +1,43 @@
import type { DatabaseSync } from "node:sqlite";
import type { CreateDocumentInput, UpdateDocumentInput, DeleteDocumentInput } from "../../shared/commands";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
export class DocumentService {
private readonly audit: AuditService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
}
create(input: CreateDocumentInput): string {
const id = createId("doc");
const now = new Date().toISOString();
this.db.prepare(`
insert into documents(id, org_id, title, doc_type, content, status, author_agent_id, reviewer_agent_id, project_id, goal_id, task_id, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(id, input.orgId, input.title.trim(), input.docType, input.content, "draft", input.authorAgentId, input.reviewerAgentId, input.projectId, input.goalId, input.taskId, now, now);
this.audit.record({
orgId: input.orgId,
entityType: "document",
entityId: id,
actorType: "system",
actorId: null,
action: "created",
payload: { title: input.title.trim(), docType: input.docType },
});
return id;
}
update(input: UpdateDocumentInput): void {
const result = this.db.prepare(`
update documents set title = ?, content = ?, status = ?, updated_at = ? where id = ? and org_id = ?
`).run(input.title.trim(), input.content, input.status, new Date().toISOString(), input.documentId, input.orgId);
if (result.changes === 0) throw new Error("Document was not found.");
}
delete(input: DeleteDocumentInput): void {
const result = this.db.prepare("delete from documents where id = ? and org_id = ?").run(input.documentId, input.orgId);
if (result.changes === 0) throw new Error("Document was not found.");
}
}
@@ -0,0 +1,36 @@
import type { DatabaseSync } from "node:sqlite";
import type { CreateKnowledgeEntryInput, DeleteKnowledgeEntryInput } from "../../shared/commands";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
export class KnowledgeService {
private readonly audit: AuditService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
}
create(input: CreateKnowledgeEntryInput): string {
const id = createId("kne");
const now = new Date().toISOString();
this.db.prepare(`
insert into knowledge_entries(id, org_id, topic, category, content, importance, source_type, source_id, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(id, input.orgId, input.topic.trim(), input.category, input.content, input.importance, input.sourceType, input.sourceId, now, now);
this.audit.record({
orgId: input.orgId,
entityType: "knowledge_entry",
entityId: id,
actorType: "system",
actorId: null,
action: "created",
payload: { topic: input.topic.trim(), category: input.category },
});
return id;
}
delete(input: DeleteKnowledgeEntryInput): void {
const result = this.db.prepare("delete from knowledge_entries where id = ? and org_id = ?").run(input.entryId, input.orgId);
if (result.changes === 0) throw new Error("Knowledge entry was not found.");
}
}
@@ -156,6 +156,10 @@ export class SnapshotService {
prBotGates,
auditReplayRuns: list(this.db, "audit_replay_runs", "created_at desc"),
auditEvents: list<AppSnapshot["auditEvents"][number]>(this.db, "audit_events", "created_at desc").slice(0, 200),
documents: list(this.db, "documents", "updated_at desc"),
knowledgeEntries: list(this.db, "knowledge_entries", "updated_at desc"),
automationRules: list<AppSnapshot["automationRules"][number]>(this.db, "automation_rules", "priority asc")
.map((rule) => ({ ...rule, enabled: toBool(rule.enabled) })),
};
}