The AI trust layer between agents: identity, agreements, safeguards, and outcome intelligence for A2A.

Viewing files at commit 68c3fd2f8af0Commit detailsView latest

packages/persistence/src/index.ts 37 lines · 1.3 KB · typescript
1
import { DatabaseSync } from 'node:sqlite';
2
import type { AuditStore } from '../../core/src/index.js';
3
4
export class SqliteAuditStore implements AuditStore {
5
  private db: DatabaseSync;
6
  constructor(path = 'openclasp.db') {
7
    this.db = new DatabaseSync(path);
8
    this.db.exec(`
9
      PRAGMA journal_mode=WAL;
10
      CREATE TABLE IF NOT EXISTS audit_records (
11
        kind TEXT NOT NULL, id TEXT NOT NULL, value TEXT NOT NULL, created_at TEXT NOT NULL,
12
        PRIMARY KEY (kind, id)
13
      );
14
    `);
15
  }
16
  append(kind: string, id: string, value: unknown): void {
17
    const encoded = JSON.stringify(value);
18
    const row = this.db
19
      .prepare('SELECT value FROM audit_records WHERE kind=? AND id=?')
20
      .get(kind, id) as { value: string } | undefined;
21
    if (row && row.value !== encoded) throw new Error(`Conflicting duplicate ${kind}:${id}`);
22
    if (!row)
23
      this.db
24
        .prepare('INSERT INTO audit_records(kind,id,value,created_at) VALUES(?,?,?,?)')
25
        .run(kind, id, encoded, new Date().toISOString());
26
  }
27
  list(kind: string): unknown[] {
28
    return (
29
      this.db
30
        .prepare('SELECT value FROM audit_records WHERE kind=? ORDER BY created_at')
31
        .all(kind) as { value: string }[]
32
    ).map((row) => JSON.parse(row.value));
33
  }
34
  close(): void {
35
    this.db.close();
36
  }
37
}