File size: 1,464 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * 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);
}