File size: 2,308 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * Database module: AgentBridgeBypass
 * CRUD + seed for agent_bridge_bypass table.
 */

import { getDbInstance } from "./core.ts";
import type { AgentBridgeBypassRow } from "./_rowTypes.ts";

// SQLite rows have source as plain string
interface AgentBridgeBypassDbRow {
  pattern: string;
  source: string;
  created_at: string;
}

function mapRow(row: AgentBridgeBypassDbRow): AgentBridgeBypassRow {
  return {
    pattern: row.pattern,
    source: row.source as "default" | "user",
    created_at: row.created_at,
  };
}

export function getAllBypassPatterns(): AgentBridgeBypassRow[] {
  const db = getDbInstance();
  const rows = db
    .prepare(
      "SELECT pattern, source, created_at FROM agent_bridge_bypass ORDER BY source ASC, pattern ASC"
    )
    .all() as AgentBridgeBypassDbRow[];
  return rows.map(mapRow);
}

export function getUserBypassPatterns(): string[] {
  const db = getDbInstance();
  const rows = db
    .prepare("SELECT pattern FROM agent_bridge_bypass WHERE source = 'user' ORDER BY pattern ASC")
    .all() as Array<{ pattern: string }>;
  return rows.map((r) => r.pattern);
}

export function replaceUserBypassPatterns(patterns: string[]): void {
  const db = getDbInstance();
  const now = new Date().toISOString();

  const deleteUserStmt = db.prepare("DELETE FROM agent_bridge_bypass WHERE source = 'user'");
  const insertStmt = db.prepare(
    `INSERT INTO agent_bridge_bypass (pattern, source, created_at) VALUES (?, 'user', ?)`
  );

  const runTransaction = db.transaction(() => {
    deleteUserStmt.run();
    for (const pattern of patterns) {
      insertStmt.run(pattern, now);
    }
  });

  runTransaction();
}

/**
 * Seeds default bypass patterns — idempotent.
 * Only inserts a pattern if it does not already exist in the table.
 * Called at app boot by the AgentBridge manager (F3 will wire this).
 */
export function seedDefaultBypassPatterns(defaults: string[]): void {
  const db = getDbInstance();
  const now = new Date().toISOString();

  const insertIfMissing = db.prepare(
    `INSERT OR IGNORE INTO agent_bridge_bypass (pattern, source, created_at) VALUES (?, 'default', ?)`
  );

  const runTransaction = db.transaction(() => {
    for (const pattern of defaults) {
      insertIfMissing.run(pattern, now);
    }
  });

  runTransaction();
}