Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(db): add agentBridge state/mappings/bypass CRUD modules (F2)
diegosouzapw45f602606bf9
diff --git a/src/lib/db/_rowTypes.ts b/src/lib/db/_rowTypes.tsnew file mode 100644index 0000000000..f16ae44aea--- /dev/null+++ b/src/lib/db/_rowTypes.ts@@ -0,0 +1,45 @@+/**+ * Row types for F2 DB modules (AgentBridge + Inspector).+ * These are local type definitions used by the CRUD modules in this directory.+ * F1 will create canonical Zod schemas in src/shared/schemas/; F10 reconciles them.+ */++export interface AgentBridgeStateRow {+ agent_id: string;+ dns_enabled: boolean;+ cert_trusted: boolean;+ setup_completed: boolean;+ last_started_at: string | null;+ last_error: string | null;+}++export interface AgentBridgeMappingRow {+ agent_id: string;+ source_model: string;+ target_model: string;+ updated_at: string;+}++export interface AgentBridgeBypassRow {+ pattern: string;+ source: "default" | "user";+ created_at: string;+}++export interface InspectorCustomHostRow {+ host: string;+ enabled: boolean;+ label: string | null;+ kind: "llm" | "app" | "custom";+ added_at: string;+ last_seen_at: string | null;+}++export interface InspectorSessionRow {+ id: string;+ name: string | null;+ started_at: string;+ ended_at: string | null;+ request_count: number;+ profile: "llm" | "custom" | "all" | null;+}diff --git a/src/lib/db/agentBridgeBypass.ts b/src/lib/db/agentBridgeBypass.tsnew file mode 100644index 0000000000..457a6413b4--- /dev/null+++ b/src/lib/db/agentBridgeBypass.ts@@ -0,0 +1,79 @@+/**+ * Database module: AgentBridgeBypass+ * CRUD + seed for agent_bridge_bypass table.+ */++import { getDbInstance } from "./core";+import type { AgentBridgeBypassRow } from "./_rowTypes";++// 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();+}diff --git a/src/lib/db/agentBridgeMappings.ts b/src/lib/db/agentBridgeMappings.tsnew file mode 100644index 0000000000..200cb2412e--- /dev/null+++ b/src/lib/db/agentBridgeMappings.ts@@ -0,0 +1,47 @@+/**+ * Database module: AgentBridgeMappings+ * CRUD operations for agent_bridge_mappings table.+ */++import { getDbInstance } from "./core";+import type { AgentBridgeMappingRow } from "./_rowTypes";++export function getMappingsForAgent(agentId: string): AgentBridgeMappingRow[] {+ const db = getDbInstance();+ const rows = db+ .prepare(+ "SELECT agent_id, source_model, target_model, updated_at FROM agent_bridge_mappings WHERE agent_id = ? ORDER BY source_model ASC"+ )+ .all(agentId) as AgentBridgeMappingRow[];+ return rows;+}++export function setMappings(+ agentId: string,+ mappings: Array<{ source: string; target: string }>+): void {+ const db = getDbInstance();+ const now = new Date().toISOString();++ const deleteStmt = db.prepare("DELETE FROM agent_bridge_mappings WHERE agent_id = ?");+ const insertStmt = db.prepare(+ `INSERT INTO agent_bridge_mappings (agent_id, source_model, target_model, updated_at)+ VALUES (?, ?, ?, ?)`+ );++ const runTransaction = db.transaction(() => {+ deleteStmt.run(agentId);+ for (const mapping of mappings) {+ insertStmt.run(agentId, mapping.source, mapping.target, now);+ }+ });++ runTransaction();+}++export function deleteMapping(agentId: string, source: string): void {+ const db = getDbInstance();+ db.prepare(+ "DELETE FROM agent_bridge_mappings WHERE agent_id = ? AND source_model = ?"+ ).run(agentId, source);+}diff --git a/src/lib/db/agentBridgeState.ts b/src/lib/db/agentBridgeState.tsnew file mode 100644index 0000000000..2cda21cd8d--- /dev/null+++ b/src/lib/db/agentBridgeState.ts@@ -0,0 +1,115 @@+/**+ * Database module: AgentBridgeState+ * CRUD operations for agent_bridge_state table.+ */++import { getDbInstance } from "./core";+import type { AgentBridgeStateRow } from "./_rowTypes";++// SQLite stores booleans as 0/1 integers+interface AgentBridgeStateDbRow {+ agent_id: string;+ dns_enabled: number;+ cert_trusted: number;+ setup_completed: number;+ last_started_at: string | null;+ last_error: string | null;+}++function mapRow(row: AgentBridgeStateDbRow): AgentBridgeStateRow {+ return {+ agent_id: row.agent_id,+ dns_enabled: row.dns_enabled === 1,+ cert_trusted: row.cert_trusted === 1,+ setup_completed: row.setup_completed === 1,+ last_started_at: row.last_started_at,+ last_error: row.last_error,+ };+}++export function getAllAgentBridgeStates(): AgentBridgeStateRow[] {+ const db = getDbInstance();+ const rows = db+ .prepare("SELECT * FROM agent_bridge_state ORDER BY agent_id ASC")+ .all() as AgentBridgeStateDbRow[];+ return rows.map(mapRow);+}++export function getAgentBridgeState(agentId: string): AgentBridgeStateRow | null {+ const db = getDbInstance();+ const row = db+ .prepare("SELECT * FROM agent_bridge_state WHERE agent_id = ?")+ .get(agentId) as AgentBridgeStateDbRow | undefined;+ return row ? mapRow(row) : null;+}++export function upsertAgentBridgeState(+ row: Partial<AgentBridgeStateRow> & { agent_id: string }+): void {+ const db = getDbInstance();+ const existing = getAgentBridgeState(row.agent_id);++ if (!existing) {+ db.prepare(+ `INSERT INTO agent_bridge_state+ (agent_id, dns_enabled, cert_trusted, setup_completed, last_started_at, last_error)+ VALUES (?, ?, ?, ?, ?, ?)`+ ).run(+ row.agent_id,+ row.dns_enabled !== undefined ? (row.dns_enabled ? 1 : 0) : 0,+ row.cert_trusted !== undefined ? (row.cert_trusted ? 1 : 0) : 0,+ row.setup_completed !== undefined ? (row.setup_completed ? 1 : 0) : 0,+ row.last_started_at ?? null,+ row.last_error ?? null+ );+ } else {+ const fields: string[] = [];+ const values: (string | number | null)[] = [];++ if (row.dns_enabled !== undefined) {+ fields.push("dns_enabled = ?");+ values.push(row.dns_enabled ? 1 : 0);+ }+ if (row.cert_trusted !== undefined) {+ fields.push("cert_trusted = ?");+ values.push(row.cert_trusted ? 1 : 0);+ }+ if (row.setup_completed !== undefined) {+ fields.push("setup_completed = ?");+ values.push(row.setup_completed ? 1 : 0);+ }+ if (row.last_started_at !== undefined) {+ fields.push("last_started_at = ?");+ values.push(row.last_started_at);+ }+ if (row.last_error !== undefined) {+ fields.push("last_error = ?");+ values.push(row.last_error);+ }++ if (fields.length === 0) return;++ values.push(row.agent_id);+ db.prepare(`UPDATE agent_bridge_state SET ${fields.join(", ")} WHERE agent_id = ?`).run(+ ...values+ );+ }+}++export function setLastStarted(agentId: string, ts: string): void {+ const db = getDbInstance();+ db.prepare(+ `INSERT INTO agent_bridge_state (agent_id, last_started_at)+ VALUES (?, ?)+ ON CONFLICT(agent_id) DO UPDATE SET last_started_at = excluded.last_started_at`+ ).run(agentId, ts);+}++export function setLastError(agentId: string, err: string | null): void {+ const db = getDbInstance();+ db.prepare(+ `INSERT INTO agent_bridge_state (agent_id, last_error)+ VALUES (?, ?)+ ON CONFLICT(agent_id) DO UPDATE SET last_error = excluded.last_error`+ ).run(agentId, err);+}