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

Viewing files at commit 5b7999fcbb0cCommit detailsView latest

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