feat: add per-session model selection
This commit is contained in:
73
src/agents/model-catalog.ts
Normal file
73
src/agents/model-catalog.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { resolveClawdisAgentDir } from "./agent-paths.js";
|
||||
import { type ClawdisConfig, loadConfig } from "../config/config.js";
|
||||
import { ensureClawdisModelsJson } from "./models-config.js";
|
||||
|
||||
export type ModelCatalogEntry = {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
contextWindow?: number;
|
||||
};
|
||||
|
||||
let modelCatalogPromise: Promise<ModelCatalogEntry[]> | null = null;
|
||||
|
||||
export function resetModelCatalogCacheForTest() {
|
||||
modelCatalogPromise = null;
|
||||
}
|
||||
|
||||
export async function loadModelCatalog(params?: {
|
||||
config?: ClawdisConfig;
|
||||
useCache?: boolean;
|
||||
}): Promise<ModelCatalogEntry[]> {
|
||||
if (params?.useCache === false) {
|
||||
modelCatalogPromise = null;
|
||||
}
|
||||
if (modelCatalogPromise) return modelCatalogPromise;
|
||||
|
||||
modelCatalogPromise = (async () => {
|
||||
const piSdk = (await import("@mariozechner/pi-coding-agent")) as {
|
||||
discoverModels: (agentDir?: string) => Array<{
|
||||
id: string;
|
||||
name?: string;
|
||||
provider: string;
|
||||
contextWindow?: number;
|
||||
}>;
|
||||
};
|
||||
|
||||
let entries: Array<{
|
||||
id: string;
|
||||
name?: string;
|
||||
provider: string;
|
||||
contextWindow?: number;
|
||||
}> = [];
|
||||
try {
|
||||
const cfg = params?.config ?? loadConfig();
|
||||
await ensureClawdisModelsJson(cfg);
|
||||
entries = piSdk.discoverModels(resolveClawdisAgentDir());
|
||||
} catch {
|
||||
entries = [];
|
||||
}
|
||||
|
||||
const models: ModelCatalogEntry[] = [];
|
||||
for (const entry of entries) {
|
||||
const id = String(entry?.id ?? "").trim();
|
||||
if (!id) continue;
|
||||
const provider = String(entry?.provider ?? "").trim();
|
||||
if (!provider) continue;
|
||||
const name = String(entry?.name ?? id).trim() || id;
|
||||
const contextWindow =
|
||||
typeof entry?.contextWindow === "number" && entry.contextWindow > 0
|
||||
? entry.contextWindow
|
||||
: undefined;
|
||||
models.push({ id, name, provider, contextWindow });
|
||||
}
|
||||
|
||||
return models.sort((a, b) => {
|
||||
const p = a.provider.localeCompare(b.provider);
|
||||
if (p !== 0) return p;
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
})();
|
||||
|
||||
return modelCatalogPromise;
|
||||
}
|
||||
75
src/agents/model-selection.ts
Normal file
75
src/agents/model-selection.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import type { ClawdisConfig } from "../config/config.js";
|
||||
import type { ModelCatalogEntry } from "./model-catalog.js";
|
||||
|
||||
export type ModelRef = {
|
||||
provider: string;
|
||||
model: string;
|
||||
};
|
||||
|
||||
export function modelKey(provider: string, model: string) {
|
||||
return `${provider}/${model}`;
|
||||
}
|
||||
|
||||
export function parseModelRef(
|
||||
raw: string,
|
||||
defaultProvider: string,
|
||||
): ModelRef | null {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
const slash = trimmed.indexOf("/");
|
||||
if (slash === -1) {
|
||||
return { provider: defaultProvider, model: trimmed };
|
||||
}
|
||||
const provider = trimmed.slice(0, slash).trim();
|
||||
const model = trimmed.slice(slash + 1).trim();
|
||||
if (!provider || !model) return null;
|
||||
return { provider, model };
|
||||
}
|
||||
|
||||
export function buildAllowedModelSet(params: {
|
||||
cfg: ClawdisConfig;
|
||||
catalog: ModelCatalogEntry[];
|
||||
defaultProvider: string;
|
||||
}): {
|
||||
allowAny: boolean;
|
||||
allowedCatalog: ModelCatalogEntry[];
|
||||
allowedKeys: Set<string>;
|
||||
} {
|
||||
const rawAllowlist = params.cfg.agent?.allowedModels ?? [];
|
||||
const allowAny = rawAllowlist.length === 0;
|
||||
const catalogKeys = new Set(
|
||||
params.catalog.map((entry) => modelKey(entry.provider, entry.id)),
|
||||
);
|
||||
|
||||
if (allowAny) {
|
||||
return {
|
||||
allowAny: true,
|
||||
allowedCatalog: params.catalog,
|
||||
allowedKeys: catalogKeys,
|
||||
};
|
||||
}
|
||||
|
||||
const allowedKeys = new Set<string>();
|
||||
for (const raw of rawAllowlist) {
|
||||
const parsed = parseModelRef(String(raw), params.defaultProvider);
|
||||
if (!parsed) continue;
|
||||
const key = modelKey(parsed.provider, parsed.model);
|
||||
if (catalogKeys.has(key)) {
|
||||
allowedKeys.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
const allowedCatalog = params.catalog.filter((entry) =>
|
||||
allowedKeys.has(modelKey(entry.provider, entry.id)),
|
||||
);
|
||||
|
||||
if (allowedCatalog.length === 0) {
|
||||
return {
|
||||
allowAny: true,
|
||||
allowedCatalog: params.catalog,
|
||||
allowedKeys: catalogKeys,
|
||||
};
|
||||
}
|
||||
|
||||
return { allowAny: false, allowedCatalog, allowedKeys };
|
||||
}
|
||||
Reference in New Issue
Block a user