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

Viewing files at commit 6e4e24f0d92fCommit detailsView latest

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