openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 4.06 kB
JavaScript
import{c as normalizeOptionalLowercaseString}from"./string-coerce-CpTFcSwY.mjs";import{l as isRecord,r as asNullableRecord}from"./record-coerce-BTbqdJ3-.mjs";import{i as parseModelCatalogRef}from"./model-catalog-refs-eJnrGUHo.mjs";import{c as parseModelPolicyWildcardRef,i as materializeModelPolicyAllowlist,r as hasModelPolicyAllowlistMigrationMarker,t as computeModelPolicyAllowlist}from"./model-policy-allowlist-migration-BI8Uf3RI.mjs";import"./legacy-config-record-shared-B0hPoAz4.mjs";
//!src/commands/doctor/shared/legacy-runtime-model-policy.ts
function collectLegacyDefaultModelAllowRefs(e){const t=asNullableRecord(asNullableRecord(e.agents)?.defaults);return computeModelPolicyAllowlist({root:e,defaults:t})}function migrateExplicitDefaultModelAllowPolicy(e,t){if(hasModelPolicyAllowlistMigrationMarker(e))return;const n=asNullableRecord(asNullableRecord(e.agents)?.defaults);const r=asNullableRecord(n?.modelPolicy);if(!(Boolean(asNullableRecord(n?.models))&&!(r&&Object.hasOwn(r,`allow`))))return;const i=materializeModelPolicyAllowlist(e);if(i.kind===`deferred`)return;Object.assign(e,i.config);t.push(i.config.agents?.defaults?.modelPolicy?.allow?`Copied the legacy default model map to agents.defaults.modelPolicy.allow.`:`Recorded the legacy default model map as unrestricted without creating modelPolicy.allow.`)}function familyWildcard(e){if(parseModelPolicyWildcardRef(e))return null;const t=parseModelCatalogRef(e.trim());if(!t)return null;const n=t.modelId.split(`/`).slice(0,-1);return parseModelPolicyWildcardRef([t.provider,...n,`*`].join(`/`))?.key??null}function wildcardOffer(e,t){if(!Array.isArray(e)||e.length===0)return null;const n=e.filter(e=>typeof e===`string`);const r=new Set(n.map(e=>e.trim()));if(n.length!==e.length||r.size!==t.size||[...r].some(e=>!t.has(e)))return null;const i=/* @__PURE__ */ new Set;for(const e of n){const t=familyWildcard(e);if(!t)return null;i.add(t)}return{before:n,after:[...i]}}function collectModelAllowlistWildcardOffers(e){const t=new Set(Object.keys(e.agents?.defaults?.models??{}).map(e=>e.trim()).filter(Boolean));if(t.size===0)return[];const n=[];const r=wildcardOffer(e.agents?.defaults?.modelPolicy?.allow,t);if(r)n.push(r);for(const[r,i]of Object.entries(e.agents?.entries??{})){const e=wildcardOffer(i?.modelPolicy?.allow,t);if(e)n.push({agentId:r,...e})}return n}function applyModelAllowlistWildcardOffers(e,t){const n={...e.agents};for(const{agentId:e,after:r}of t){if(e===void 0){n.defaults={...n.defaults,modelPolicy:{...n.defaults?.modelPolicy,allow:r}};continue}const t=n.entries?.[e];n.entries={...n.entries,[e]:{...t,modelPolicy:{...t?.modelPolicy,allow:r}}}}return{...e,agents:n}}async function maybeOfferModelAllowlistWildcards(e){const t={config:e.cfg,changes:[]};const{repairMode:n}=e.prompter;if(!n.canPrompt||n.updateInProgress)return t;const r=collectModelAllowlistWildcardOffers(e.cfg);if(r.length===0)return t;const{note:i}=await import(`./terminal-core/note.js`);i([`Your allowed-models list matches your per-model settings, so new models from these providers stay hidden.`,...r.map(({agentId:e,before:t,after:n})=>`${e===void 0?`agents.defaults`:`agents.entries.${e}`}.modelPolicy.allow\n now: ${t.join(`, `)}\n new: ${n.join(`, `)}`)].join(`
`),`Model policy`);if(!await e.prompter.confirmRuntimeRepair({message:`Allow all models from these providers instead?`,initialValue:false,requiresInteractiveConfirmation:true}))return t;return{config:applyModelAllowlistWildcardOffers(e.cfg,r),changes:r.map(({agentId:e,after:t})=>`- ${e===void 0?`agents.defaults`:`agents.entries.${e}`}.modelPolicy.allow: ${t.join(`, `)}`)}}function modelEntryWithRuntimePolicy(e,t){const n=isRecord(e)?{...e}:{};const r=isRecord(n.agentRuntime)?n.agentRuntime:void 0;const i=normalizeOptionalLowercaseString(r?.id);if(i&&i!==`auto`)return{changed:false,entry:n};n.agentRuntime={...r,id:t};return{changed:true,entry:n}}export{modelEntryWithRuntimePolicy as i,maybeOfferModelAllowlistWildcards as n,migrateExplicitDefaultModelAllowPolicy as r,collectLegacyDefaultModelAllowRefs as t};