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

Viewing files at commit 1da1b7ef4016Commit detailsView latest

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