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

Viewing files at commit 9a8fdf2387ffCommit detailsView latest

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