Spaces:
Runtime error
Runtime error
File size: 4,931 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /**
* CLI Tool State Persistence
*
* Stores last-configured timestamps and initial config snapshots
* for CLI tools in the key_value table.
*
* Namespaces:
* - cliToolLastConfig: ISO timestamp of last configuration
* - cliToolInitialConfig: JSON snapshot of pre-OmniRoute configuration
*
* @module lib/db/cliToolState
*/
import { getDbInstance, isBuildPhase, isCloud } from "./core";
type JsonRecord = Record<string, unknown>;
interface StatementLike<TRow = unknown> {
all: (...params: unknown[]) => TRow[];
get: (...params: unknown[]) => TRow | undefined;
run: (...params: unknown[]) => { changes?: number };
}
interface DbLike {
prepare: <TRow = unknown>(sql: string) => StatementLike<TRow>;
}
interface KeyValueRow {
key: string;
value: string;
}
function parseJsonValue(raw: string): unknown {
try {
return JSON.parse(raw) as unknown;
} catch {
return null;
}
}
function toRecord(value: unknown): JsonRecord | null {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null;
}
// ββββββββββββββββ Last Configured Timestamp ββββββββββββββββ
/**
* Save last-configured timestamp for a CLI tool.
*/
export function saveCliToolLastConfigured(
toolId: string,
timestamp: string = new Date().toISOString()
): void {
if (isBuildPhase || isCloud) return;
const db = getDbInstance() as unknown as DbLike;
db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
"cliToolLastConfig",
toolId,
JSON.stringify(timestamp)
);
}
/**
* Get last-configured timestamp for a CLI tool.
* @returns ISO timestamp string or null if never configured.
*/
export function getCliToolLastConfigured(toolId: string): string | null {
if (isBuildPhase || isCloud) return null;
const db = getDbInstance() as unknown as DbLike;
const row = db
.prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?")
.get("cliToolLastConfig", toolId);
if (!row) return null;
const parsed = parseJsonValue((row as KeyValueRow).value);
return typeof parsed === "string" ? parsed : null;
}
/**
* Get all CLI tool last-configured timestamps.
* @returns Record<toolId, ISO timestamp>
*/
export function getAllCliToolLastConfigured(): Record<string, string> {
if (isBuildPhase || isCloud) return {};
const db = getDbInstance() as unknown as DbLike;
const rows = db
.prepare("SELECT key, value FROM key_value WHERE namespace = ?")
.all("cliToolLastConfig") as KeyValueRow[];
const result: Record<string, string> = {};
for (const row of rows) {
const parsed = parseJsonValue(row.value);
if (typeof parsed === "string") {
result[row.key] = parsed;
}
}
return result;
}
/**
* Delete last-configured timestamp for a CLI tool.
*/
export function deleteCliToolLastConfigured(toolId: string): void {
if (isBuildPhase || isCloud) return;
const db = getDbInstance() as unknown as DbLike;
db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run(
"cliToolLastConfig",
toolId
);
}
// ββββββββββββββββ Initial Config Snapshot ββββββββββββββββ
/**
* Save the initial (pre-OmniRoute) config snapshot for a CLI tool.
* Only saves if no snapshot exists yet (first-time only).
* @returns true if saved, false if snapshot already exists.
*/
export function saveCliToolInitialConfig(toolId: string, config: JsonRecord): boolean {
if (isBuildPhase || isCloud) return false;
const db = getDbInstance() as unknown as DbLike;
// Only save if not already stored
const existing = db
.prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?")
.get("cliToolInitialConfig", toolId);
if (existing) return false;
db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
"cliToolInitialConfig",
toolId,
JSON.stringify(config)
);
return true;
}
/**
* Get the initial config snapshot for a CLI tool.
* @returns Config object or null if no snapshot exists.
*/
export function getCliToolInitialConfig(toolId: string): JsonRecord | null {
if (isBuildPhase || isCloud) return null;
const db = getDbInstance() as unknown as DbLike;
const row = db
.prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?")
.get("cliToolInitialConfig", toolId);
if (!row) return null;
const parsed = parseJsonValue((row as KeyValueRow).value);
return toRecord(parsed);
}
/**
* Delete the initial config snapshot for a CLI tool.
*/
export function deleteCliToolInitialConfig(toolId: string): void {
if (isBuildPhase || isCloud) return;
const db = getDbInstance() as unknown as DbLike;
db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run(
"cliToolInitialConfig",
toolId
);
}
|