UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 10 kB
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{r as asNullableRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as isBlockedObjectKey}from"./prototype-keys-Dc7Yq32J.mjs";import{r as normalizeConfiguredProviderCatalogModelId}from"./provider-model-id-normalization-CBsl47ji.mjs";import{r as normalizeAgentModelRefForConfig}from"./model-input-CJnF-FEy.mjs";import{t as splitTrailingAuthProfile}from"./model-ref-profile-DFGpvDZX.mjs";import{isDeepStrictEqual}from"node:util"; //!src/commands/doctor/shared/legacy-model-ref-normalization.ts function canonicalizeKnownModelRef(e){const t=splitTrailingAuthProfile(e.trim());const n=t.model.indexOf(`/`);if(n<=0)return null;const r=t.model.slice(0,n).trim();const i=t.model.slice(n+1).trim();return normalizeLowercaseStringOrEmpty(r)===`openai`&&normalizeLowercaseStringOrEmpty(i)===`gpt-5.6`?`${r}/gpt-5.6-sol${t.profile?`@${t.profile}`:``}`:null}function normalizeKnownModelRef(e){const t=e.trim();if(!t)return e===t?null:t;const n=splitTrailingAuthProfile(t);const r=n.model.indexOf(`/`);const i=r>0?normalizeLowercaseStringOrEmpty(n.model.slice(0,r)):``;const a=r>0?n.model.slice(r+1):n.model;const o=`${i===`google`||i===`google-gemini-cli`||i===`google-vertex`||i===`together`||normalizeLowercaseStringOrEmpty(a).startsWith(`google/`)?normalizeAgentModelRefForConfig(n.model):n.model}${n.profile?`@${n.profile}`:``}`;return canonicalizeKnownModelRef(o)??(o===e?null:o)}function normalizeProviderCatalogModelId(e,t){const n=t.trim();const r=normalizeLowercaseStringOrEmpty(e);const i=r===`google`||r===`google-gemini-cli`||r===`google-vertex`||r===`together`||normalizeLowercaseStringOrEmpty(n).startsWith(`google/`)?normalizeConfiguredProviderCatalogModelId(e,n):n;const a=canonicalizeKnownModelRef(`${e}/${i}`);if(!a)return i;const o=a.indexOf(`/`);return o>0&&normalizeLowercaseStringOrEmpty(a.slice(0,o))===normalizeLowercaseStringOrEmpty(e)?a.slice(o+1):i} //!src/commands/doctor/shared/legacy-config-migrations.runtime.models.refs.ts function hasOwnDefinedProperty(e,t){return Object.hasOwn(e,t)&&e[t]!==void 0}const MODEL_REF_STRING_KEYS=/* @__PURE__ */ new Set([`model`,`primary`,`summaryModel`,`imageModel`,`utilityModel`,`voiceModel`,`imageGenerationModel`,`musicGenerationModel`,`pdfModel`,`videoGenerationModel`,`preferredModel`]);const MODEL_REF_ARRAY_KEYS=/* @__PURE__ */ new Set([`fallback`,`fallbacks`,`allowedModels`,`modelFallbacks`,`imageModelFallbacks`]);const MODEL_REF_MAP_KEYS=/* @__PURE__ */ new Set([`models`]);function pathKey(e){return e.slice(e.lastIndexOf(`.`)+1)}function isChannelModelOverridePath(e){return e.includes(`.modelByChannel.`)}function isMediaModelPath(e){return[`image`,`video`,`music`].includes(pathKey(e))&&e.includes(`.mediaModels.`)}function isProviderCatalogsPath(e){return e===`.providers`||e.endsWith(`.models.providers`)}function scanProviderCatalogModelIds(e){return Object.entries(e).some(([e,t])=>{const n=asNullableRecord(t)?.models;return Array.isArray(n)&&n.some(t=>{const n=asNullableRecord(t)?.id;return typeof n===`string`&&normalizeProviderCatalogModelId(e,n)!==n})})}function scanKnownModelRefs(e,t,n=``){if(typeof e===`string`)return Boolean(t&&(MODEL_REF_STRING_KEYS.has(t)||isChannelModelOverridePath(n)||isMediaModelPath(n))&&normalizeKnownModelRef(e));if(Array.isArray(e))return e.some((e,r)=>typeof e===`string`&&t&&(MODEL_REF_ARRAY_KEYS.has(t)||n.endsWith(`.modelPolicy.allow`)||n.endsWith(`.modelPolicy.deny`))?Boolean(normalizeKnownModelRef(e)):scanKnownModelRefs(e,void 0,`${n}.${r}`));const r=asNullableRecord(e);if(!r)return false;const i=typeof r.provider===`string`?r.provider:void 0;const a=typeof r.model===`string`?r.model:void 0;const o=i!==void 0&&a!==void 0;if(o&&normalizeKnownModelRef(`${i}/${a}`))return true;if(isProviderCatalogsPath(n)&&scanProviderCatalogModelIds(r))return true;if(t&&MODEL_REF_MAP_KEYS.has(t))return Object.keys(r).some(e=>Boolean(normalizeKnownModelRef(e)));return Object.entries(r).some(([e,t])=>!(o&&e===`model`)&&scanKnownModelRefs(t,e,`${n}.${e}`))}function rewriteModelRefString(e,t,n,r){const i=r(e);if(i===null||i===e)return e;n.push(`Upgraded ${t} from ${JSON.stringify(e)} to ${JSON.stringify(i)}.`);return i}function setRecordEntry(e,t,n){Object.defineProperty(e,t,{configurable:true,enumerable:true,value:n,writable:true})}function sanitizeModelRefMapEntry(e){if(Array.isArray(e))return e.map(sanitizeModelRefMapEntry);const t=asNullableRecord(e);if(!t)return e;const n={};for(const[e,r]of Object.entries(t))if(!isBlockedObjectKey(e))setRecordEntry(n,e,sanitizeModelRefMapEntry(r));return n}function modelRefValuesAreEqual(e,t,n,r){if(isDeepStrictEqual(e,t))return true;const i=rewriteModelRefs(e,n,[],r).value;const a=rewriteModelRefs(t,n,[],r).value;return isDeepStrictEqual(i,a)}function mergeModelRefMapEntries(e,t,n,r=normalizeKnownModelRef){const i=asNullableRecord(e);const a=asNullableRecord(t);if(!i||!a)return{value:sanitizeModelRefMapEntry(e),conflicts:modelRefValuesAreEqual(e,t,n,r)?[]:[`value`]};const o=sanitizeModelRefMapEntry(i);const s=[];for(const[e,t]of Object.entries(a)){if(t===void 0||isBlockedObjectKey(e))continue;if(!hasOwnDefinedProperty(i,e)){setRecordEntry(o,e,sanitizeModelRefMapEntry(t));continue}const a=i[e];const c=`${n}.${e}`;if(modelRefValuesAreEqual(a,t,c,r))continue;const l=asNullableRecord(a);const u=asNullableRecord(t);if(l&&u){const t=mergeModelRefMapEntries(l,u,c,r);setRecordEntry(o,e,t.value);s.push(...t.conflicts.map(t=>`${e}.${t}`));continue}s.push(e)}return{value:o,conflicts:s}}function rewriteModelRefMapKeys(e,t,n,r){let i=false;const a={};const o=/* @__PURE__ */ new Set;for(const[s,c]of Object.entries(e)){const l=r(s);const u=l??s;if(!l&&o.has(s))continue;if(l){n.push(`Upgraded ${t} key from ${JSON.stringify(s)} to ${JSON.stringify(l)}.`);i=true}if(l&&!Object.hasOwn(a,u)&&Object.hasOwn(e,u)){setRecordEntry(a,u,e[u]);o.add(u)}if(Object.hasOwn(a,u)){const e=a[u];const{value:i,conflicts:o}=mergeModelRefMapEntries(e,c,`${t}.${u}`,r);setRecordEntry(a,u,i);const l=o.toSorted();if(l.length>0)n.push(`Merged ${t} key ${JSON.stringify(s)} into ${JSON.stringify(u)}; kept existing values for conflicting fields: ${l.join(`, `)}.`);else n.push(`Merged ${t} key ${JSON.stringify(s)} into ${JSON.stringify(u)}.`);continue}setRecordEntry(a,u,c)}return{value:i?a:e,changed:i}}function rewriteProviderCatalogModelIds(e,t,n){let r=false;const i={...e};for(const[a,o]of Object.entries(e)){const e=asNullableRecord(o);if(!e||!Array.isArray(e.models))continue;const s=e.models.map((e,t)=>{const n=asNullableRecord(e);if(!n||typeof n.id!==`string`)return{index:t,model:e};const r=normalizeProviderCatalogModelId(a,n.id);return{index:t,model:e,modelRecord:n,originalId:n.id,normalizedId:r,changed:r!==n.id}});if(!s.some(e=>e.changed))continue;const c=/* @__PURE__ */ new Map;for(const e of s){if(e.normalizedId===void 0)continue;const t=c.get(e.normalizedId)??[];t.push(e);c.set(e.normalizedId,t)}const l=/* @__PURE__ */ new Set;const u=[];for(const e of s){if(e.normalizedId===void 0||e.modelRecord===void 0){u.push(e.model);continue}const r=c.get(e.normalizedId)??[e];if(!r.some(e=>e.changed)){u.push(e.model);continue}if(l.has(e.normalizedId))continue;l.add(e.normalizedId);const i=r.find(e=>e.originalId===e.normalizedId)??r[0];const o=i?.modelRecord;if(!i||!o){u.push(e.model);continue}let s={...o,id:e.normalizedId};for(const o of r){if(o===i||!o.modelRecord)continue;const r=mergeModelRefMapEntries(s,{...o.modelRecord,id:e.normalizedId},`${t}.${a}.models.${i.index}`);s=asNullableRecord(r.value)??s;n.push(r.conflicts.length>0?`Merged ${t}.${a}.models.${o.index} into model id ${JSON.stringify(e.normalizedId)}; kept existing values for conflicting fields: ${r.conflicts.toSorted().join(`, `)}.`:`Merged ${t}.${a}.models.${o.index} into model id ${JSON.stringify(e.normalizedId)}.`)}for(const e of r){if(!e.changed)continue;n.push(`Upgraded ${t}.${a}.models.${e.index}.id from ${JSON.stringify(e.originalId)} to ${JSON.stringify(e.normalizedId)}.`)}u.push(s)}i[a]={...e,models:u};r=true}return{value:r?i:e,changed:r}}function rewriteModelRefs(e,t,n,r){const i=pathKey(t);if(typeof e===`string`){if(!MODEL_REF_STRING_KEYS.has(i)&&!isChannelModelOverridePath(t)&&!isMediaModelPath(t))return{value:e,changed:false};const a=rewriteModelRefString(e,t,n,r);return{value:a,changed:a!==e}}if(Array.isArray(e)){let a=false;const o=e.map((e,o)=>{if(typeof e===`string`&&(MODEL_REF_ARRAY_KEYS.has(i)||t.endsWith(`.modelPolicy.allow`)||t.endsWith(`.modelPolicy.deny`))){const i=rewriteModelRefString(e,`${t}.${o}`,n,r);a||=i!==e;return i}const s=rewriteModelRefs(e,`${t}.${o}`,n,r);a||=s.changed;return s.value});return{value:a?o:e,changed:a}}const a=asNullableRecord(e);if(!a)return{value:e,changed:false};let o=a;let s=false;const c=typeof a.provider===`string`?a.provider:void 0;const l=typeof a.model===`string`?a.model:void 0;const u=c!==void 0&&l!==void 0;if(c?.trim()&&l!==void 0){const e=`${c}/${l}`;const i=r(e,a);if(i&&i!==e){const r=i.indexOf(`/`);o={...a,provider:i.slice(0,r),model:i.slice(r+1)};n.push(`Upgraded ${t} provider/model from ${JSON.stringify(e)} to ${JSON.stringify(i)}.`);s=true}}if(r===normalizeKnownModelRef&&isProviderCatalogsPath(t)){const e=rewriteProviderCatalogModelIds(o,t,n);o=e.value;s||=e.changed}if(MODEL_REF_MAP_KEYS.has(i)){const e=rewriteModelRefMapKeys(o,t,n,r);o=e.value;s||=e.changed}const d={};for(const[e,i]of Object.entries(o)){const a=u&&e===`model`?{value:i,changed:false}:rewriteModelRefs(i,`${t}.${e}`,n,r);s||=a.changed;setRecordEntry(d,e,a.value)}return{value:s?d:e,changed:s}}const MODEL_REF_CANONICALIZATION_MESSAGE=`Configured model refs use noncanonical spellings; run "openclaw doctor --fix" to normalize them.`;function rewriteKnownModelRefs(e,t,n){return rewriteModelRefs(e,t,n,normalizeKnownModelRef)}export{rewriteModelRefs as a,normalizeKnownModelRef as c,rewriteKnownModelRefs as i,hasOwnDefinedProperty as n,scanKnownModelRefs as o,mergeModelRefMapEntries as r,setRecordEntry as s,MODEL_REF_CANONICALIZATION_MESSAGE as t};