File size: 3,113 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { getDbInstance } from "./core";

export interface ApiKeyContextSource {
  apiKeyId: string;
  sourceType: string;
  token: string | null;
  baseUrl: string | null;
  vaultPath: string | null;
  enabled: boolean;
}

interface ContextSourceRow {
  api_key_id: string;
  source_type: string;
  token: string | null;
  base_url: string | null;
  vault_path: string | null;
  enabled: number;
}

function rowToSource(row: ContextSourceRow): ApiKeyContextSource {
  return {
    apiKeyId: row.api_key_id,
    sourceType: row.source_type,
    token: row.token,
    baseUrl: row.base_url,
    vaultPath: row.vault_path,
    enabled: row.enabled === 1,
  };
}

export function getApiKeyContextSource(
  apiKeyId: string | null | undefined,
  sourceType: string
): (ApiKeyContextSource & { enabled: true }) | null {
  if (!apiKeyId) return null;
  const db = getDbInstance();
  const row = db
    .prepare(
      "SELECT * FROM api_key_context_sources WHERE api_key_id = ? AND source_type = ? AND enabled = 1"
    )
    .get(apiKeyId, sourceType) as ContextSourceRow | undefined;
  if (!row) return null;
  return rowToSource(row) as ApiKeyContextSource & { enabled: true };
}

export function setApiKeyContextSource(
  apiKeyId: string,
  sourceType: string,
  config: { token?: string; baseUrl?: string; vaultPath?: string; enabled?: boolean }
): void {
  const db = getDbInstance();
  const existing = db
    .prepare("SELECT * FROM api_key_context_sources WHERE api_key_id = ? AND source_type = ?")
    .get(apiKeyId, sourceType) as ContextSourceRow | undefined;

  const now = new Date().toISOString();
  if (existing) {
    db.prepare(
      `UPDATE api_key_context_sources SET
        token = COALESCE(?, token),
        base_url = COALESCE(?, base_url),
        vault_path = COALESCE(?, vault_path),
        enabled = COALESCE(?, enabled),
        updated_at = ?
      WHERE api_key_id = ? AND source_type = ?`
    ).run(
      config.token ?? null,
      config.baseUrl ?? null,
      config.vaultPath ?? null,
      config.enabled !== undefined ? (config.enabled ? 1 : 0) : null,
      now,
      apiKeyId,
      sourceType
    );
  } else {
    db.prepare(
      `INSERT INTO api_key_context_sources
        (api_key_id, source_type, token, base_url, vault_path, enabled, created_at, updated_at)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
    ).run(
      apiKeyId,
      sourceType,
      config.token ?? null,
      config.baseUrl ?? null,
      config.vaultPath ?? null,
      config.enabled !== undefined ? (config.enabled ? 1 : 0) : 1,
      now,
      now
    );
  }
}

export function deleteApiKeyContextSource(apiKeyId: string, sourceType: string): void {
  const db = getDbInstance();
  db.prepare(
    "DELETE FROM api_key_context_sources WHERE api_key_id = ? AND source_type = ?"
  ).run(apiKeyId, sourceType);
}

export function listApiKeyContextSources(apiKeyId: string): ApiKeyContextSource[] {
  const db = getDbInstance();
  const rows = db
    .prepare("SELECT * FROM api_key_context_sources WHERE api_key_id = ?")
    .all(apiKeyId) as ContextSourceRow[];
  return rows.map(rowToSource);
}