Spaces:
Runtime error
Runtime error
File size: 8,162 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | /**
* db/modelComboMappings.ts β Per-model combo mapping CRUD + resolution.
*
* Maps model name patterns (glob-style wildcards) to specific combos.
* When a request arrives for a model string like "claude-sonnet-4",
* the resolver checks all enabled mappings (highest priority first)
* and returns the first matching combo.
*/
import { v4 as uuidv4 } from "uuid";
import { getDbInstance } from "./core";
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Types
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export interface ModelComboMapping {
id: string;
pattern: string;
comboId: string;
comboName?: string;
priority: number;
enabled: boolean;
description: string;
createdAt: string;
updatedAt: string;
}
interface MappingRow {
id: string;
pattern: string;
combo_id: string;
combo_name?: string;
priority: number;
enabled: number;
description: string;
created_at: string;
updated_at: string;
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Glob β RegExp conversion
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Convert a simple glob pattern to a RegExp.
* Supports `*` (any characters) and `?` (single character).
* Case-insensitive matching.
*/
function globToRegex(pattern: string): RegExp {
const escaped = pattern
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // escape regex specials
.replace(/\*/g, ".*") // * β .*
.replace(/\?/g, "."); // ? β .
return new RegExp(`^${escaped}$`, "i");
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Row mapping
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function rowToMapping(row: MappingRow): ModelComboMapping {
return {
id: row.id,
pattern: row.pattern,
comboId: row.combo_id,
comboName: row.combo_name || undefined,
priority: row.priority,
enabled: row.enabled === 1,
description: row.description || "",
createdAt: row.created_at,
updatedAt: row.updated_at,
};
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// CRUD
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* List all model-combo mappings, joined with combo name.
* Ordered by priority descending (highest first).
*/
export async function getModelComboMappings(): Promise<ModelComboMapping[]> {
const db = getDbInstance();
const rows = db
.prepare(
`SELECT m.id, m.pattern, m.combo_id, c.name AS combo_name,
m.priority, m.enabled, m.description,
m.created_at, m.updated_at
FROM model_combo_mappings m
LEFT JOIN combos c ON c.id = m.combo_id
ORDER BY m.priority DESC, m.created_at ASC`
)
.all() as MappingRow[];
return rows.map(rowToMapping);
}
/**
* Get a single mapping by ID.
*/
export async function getModelComboMappingById(id: string): Promise<ModelComboMapping | null> {
const db = getDbInstance();
const row = db
.prepare(
`SELECT m.id, m.pattern, m.combo_id, c.name AS combo_name,
m.priority, m.enabled, m.description,
m.created_at, m.updated_at
FROM model_combo_mappings m
LEFT JOIN combos c ON c.id = m.combo_id
WHERE m.id = ?`
)
.get(id) as MappingRow | undefined;
return row ? rowToMapping(row) : null;
}
/**
* Create a new model-combo mapping.
*/
export async function createModelComboMapping(data: {
pattern: string;
comboId: string;
priority?: number;
enabled?: boolean;
description?: string;
}): Promise<ModelComboMapping> {
const db = getDbInstance();
const now = new Date().toISOString();
const id = uuidv4();
db.prepare(
`INSERT INTO model_combo_mappings
(id, pattern, combo_id, priority, enabled, description, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
).run(
id,
data.pattern,
data.comboId,
data.priority ?? 0,
data.enabled !== false ? 1 : 0,
data.description || "",
now,
now
);
return {
id,
pattern: data.pattern,
comboId: data.comboId,
priority: data.priority ?? 0,
enabled: data.enabled !== false,
description: data.description || "",
createdAt: now,
updatedAt: now,
};
}
/**
* Update an existing model-combo mapping.
*/
export async function updateModelComboMapping(
id: string,
data: Partial<{
pattern: string;
comboId: string;
priority: number;
enabled: boolean;
description: string;
}>
): Promise<ModelComboMapping | null> {
const existing = await getModelComboMappingById(id);
if (!existing) return null;
const db = getDbInstance();
const now = new Date().toISOString();
const updated = {
pattern: data.pattern ?? existing.pattern,
combo_id: data.comboId ?? existing.comboId,
priority: data.priority ?? existing.priority,
enabled: data.enabled !== undefined ? (data.enabled ? 1 : 0) : existing.enabled ? 1 : 0,
description: data.description ?? existing.description,
};
db.prepare(
`UPDATE model_combo_mappings
SET pattern = ?, combo_id = ?, priority = ?, enabled = ?,
description = ?, updated_at = ?
WHERE id = ?`
).run(
updated.pattern,
updated.combo_id,
updated.priority,
updated.enabled,
updated.description,
now,
id
);
return getModelComboMappingById(id);
}
/**
* Delete a model-combo mapping.
*/
export async function deleteModelComboMapping(id: string): Promise<boolean> {
const db = getDbInstance();
const result = db.prepare("DELETE FROM model_combo_mappings WHERE id = ?").run(id);
return (result.changes ?? 0) > 0;
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Core: Resolve combo for a model string
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Check if a model string matches any enabled model-combo mapping.
* Returns the full combo object if a match is found, null otherwise.
*
* Mappings are checked in priority order (highest first).
* Uses glob-style pattern matching (* = any chars, ? = single char).
*/
export async function resolveComboForModel(
modelStr: string
): Promise<Record<string, unknown> | null> {
const db = getDbInstance();
// Fetch enabled mappings, ordered by priority (highest first)
const rows = db
.prepare(
`SELECT m.pattern, m.combo_id, c.data AS combo_data
FROM model_combo_mappings m
JOIN combos c ON c.id = m.combo_id
WHERE m.enabled = 1
ORDER BY m.priority DESC, m.created_at ASC`
)
.all() as Array<{ pattern: string; combo_id: string; combo_data: string }>;
for (const row of rows) {
const regex = globToRegex(row.pattern);
if (regex.test(modelStr)) {
try {
const combo = JSON.parse(row.combo_data) as Record<string, unknown>;
if (combo.isActive === false) {
continue;
}
return combo;
} catch {
// Corrupted combo data β skip
continue;
}
}
}
return null;
}
|