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

@@ -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) })),
};
}