Spaces:
Running
Running
File size: 3,261 Bytes
fb4d8fe | 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 | import type { RuntimeEnv } from "../../runtime.js";
import { loadConfig } from "../../config/config.js";
import { logConfigUpdated } from "../../config/logging.js";
import {
ensureFlagCompatibility,
normalizeAlias,
resolveModelTarget,
updateConfig,
} from "./shared.js";
export async function modelsAliasesListCommand(
opts: { json?: boolean; plain?: boolean },
runtime: RuntimeEnv,
) {
ensureFlagCompatibility(opts);
const cfg = loadConfig();
const models = cfg.agents?.defaults?.models ?? {};
const aliases = Object.entries(models).reduce<Record<string, string>>(
(acc, [modelKey, entry]) => {
const alias = entry?.alias?.trim();
if (alias) {
acc[alias] = modelKey;
}
return acc;
},
{},
);
if (opts.json) {
runtime.log(JSON.stringify({ aliases }, null, 2));
return;
}
if (opts.plain) {
for (const [alias, target] of Object.entries(aliases)) {
runtime.log(`${alias} ${target}`);
}
return;
}
runtime.log(`Aliases (${Object.keys(aliases).length}):`);
if (Object.keys(aliases).length === 0) {
runtime.log("- none");
return;
}
for (const [alias, target] of Object.entries(aliases)) {
runtime.log(`- ${alias} -> ${target}`);
}
}
export async function modelsAliasesAddCommand(
aliasRaw: string,
modelRaw: string,
runtime: RuntimeEnv,
) {
const alias = normalizeAlias(aliasRaw);
const resolved = resolveModelTarget({ raw: modelRaw, cfg: loadConfig() });
const _updated = await updateConfig((cfg) => {
const modelKey = `${resolved.provider}/${resolved.model}`;
const nextModels = { ...cfg.agents?.defaults?.models };
for (const [key, entry] of Object.entries(nextModels)) {
const existing = entry?.alias?.trim();
if (existing && existing === alias && key !== modelKey) {
throw new Error(`Alias ${alias} already points to ${key}.`);
}
}
const existing = nextModels[modelKey] ?? {};
nextModels[modelKey] = { ...existing, alias };
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models: nextModels,
},
},
};
});
logConfigUpdated(runtime);
runtime.log(`Alias ${alias} -> ${resolved.provider}/${resolved.model}`);
}
export async function modelsAliasesRemoveCommand(aliasRaw: string, runtime: RuntimeEnv) {
const alias = normalizeAlias(aliasRaw);
const updated = await updateConfig((cfg) => {
const nextModels = { ...cfg.agents?.defaults?.models };
let found = false;
for (const [key, entry] of Object.entries(nextModels)) {
if (entry?.alias?.trim() === alias) {
nextModels[key] = { ...entry, alias: undefined };
found = true;
break;
}
}
if (!found) {
throw new Error(`Alias not found: ${alias}`);
}
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models: nextModels,
},
},
};
});
logConfigUpdated(runtime);
if (
!updated.agents?.defaults?.models ||
Object.values(updated.agents.defaults.models).every((entry) => !entry?.alias?.trim())
) {
runtime.log("No aliases configured.");
}
}
|