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

Viewing files at commit 8974f6e2faf1Commit detailsView latest

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