support patterns in model key

This commit is contained in:
miloschwartz
2026-08-06 14:27:08 -04:00
parent 36b8ef5fba
commit c5d68675c9
4 changed files with 162 additions and 36 deletions
+23 -13
View File
@@ -10,6 +10,7 @@ import {
type Transaction
} from "@server/db";
import { z } from "zod";
import { modelKeysConflict } from "@server/lib/aiModelKeyMatch";
type DbOrTrx = Transaction | typeof db;
@@ -55,10 +56,13 @@ function normalizeAttachments(
}
/**
* Ensure enabled catalog modelKeys are unique across attached providers.
* Ensure enabled catalog modelKeys do not conflict across attached providers.
* Catalog attachments contribute all enabled models on the provider.
* Allowlist attachments contribute nothing until models are allowlisted
* (those are checked when the allowlist is set).
*
* Conflicts: identical keys, or an exact key that matches another provider's
* pattern. Full glob intersections are left to runtime ambiguity errors.
*/
export async function assertNoOverlappingModelKeys(
attachments: ResourceAiProviderAttachment[],
@@ -85,25 +89,31 @@ export async function assertNoOverlappingModelKeys(
)
);
const keyToProviders = new Map<string, number[]>();
for (const model of models) {
const existing = keyToProviders.get(model.modelKey) ?? [];
if (!existing.includes(model.providerId)) {
existing.push(model.providerId);
const conflictPairs: string[] = [];
for (let i = 0; i < models.length; i++) {
for (let j = i + 1; j < models.length; j++) {
const left = models[i];
const right = models[j];
if (left.providerId === right.providerId) {
continue;
}
if (!modelKeysConflict(left.modelKey, right.modelKey)) {
continue;
}
const pair = [left.modelKey, right.modelKey].sort().join(" vs ");
if (!conflictPairs.includes(pair)) {
conflictPairs.push(pair);
}
}
keyToProviders.set(model.modelKey, existing);
}
const overlaps = [...keyToProviders.entries()].filter(
([, providerIds]) => providerIds.length > 1
);
if (overlaps.length === 0) {
if (conflictPairs.length === 0) {
return null;
}
const keys = overlaps.map(([key]) => key).sort();
conflictPairs.sort();
return {
error: `Model keys must be unique across providers on a resource. Overlapping keys: ${keys.join(", ")}`
error: `Model keys must be unique across providers on a resource. Overlapping keys: ${conflictPairs.join(", ")}`
};
}
+85
View File
@@ -0,0 +1,85 @@
const modelKeyRegexCache = new Map<string, RegExp>();
export function isModelKeyPattern(key: string): boolean {
return key.includes("*") || key.includes("?");
}
function getModelKeyRegex(pattern: string): RegExp {
let regex = modelKeyRegexCache.get(pattern);
if (!regex) {
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
regex = new RegExp(
`^${escaped.replace(/\*/g, ".*").replace(/\?/g, ".")}$`
);
modelKeyRegexCache.set(pattern, regex);
}
return regex;
}
export function modelKeyMatches(
pattern: string,
requestedModel: string
): boolean {
return getModelKeyRegex(pattern).test(requestedModel);
}
function wildcardCharCount(key: string): number {
let count = 0;
for (const char of key) {
if (char === "*" || char === "?") {
count += 1;
}
}
return count;
}
function literalLength(key: string): number {
return key.replace(/[*?]/g, "").length;
}
/**
* Sort comparator: more specific patterns sort before less specific ones
* (negative when `a` is more specific than `b`).
*
* 1. Exact keys beat patterns
* 2. Fewer wildcard characters win
* 3. Longer literal length wins
*/
export function compareModelKeySpecificity(a: string, b: string): number {
const aIsPattern = isModelKeyPattern(a);
const bIsPattern = isModelKeyPattern(b);
if (aIsPattern !== bIsPattern) {
return aIsPattern ? 1 : -1;
}
const wildcardDiff = wildcardCharCount(a) - wildcardCharCount(b);
if (wildcardDiff !== 0) {
return wildcardDiff;
}
return literalLength(b) - literalLength(a);
}
/**
* Attach-time conflict check. Detects identical keys and exact-vs-pattern
* matches. Does not attempt full glob intersection.
*/
export function modelKeysConflict(a: string, b: string): boolean {
if (a === b) {
return true;
}
const aIsPattern = isModelKeyPattern(a);
const bIsPattern = isModelKeyPattern(b);
if (aIsPattern === bIsPattern) {
return false;
}
if (aIsPattern) {
return modelKeyMatches(a, b);
}
return modelKeyMatches(b, a);
}