openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.28 kB
JavaScript
import{l as isRecord,r as asNullableRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import{t as DEFAULT_AGENT_ID}from"./session-key-BPgKfU2m.mjs";import{n as normalizeAccountId}from"./account-id-3yqrEsRn.mjs";import{t as AgentSelectionRequiredError}from"./agent-scope-config-CG9CFpGX.mjs";import{n as projectLegacyAgentRosterEntries}from"./legacy.roster-CU2eR_i6.mjs";import{r as resolveChannelAccountEntry}from"./account-lookup-DXbrh7Os.mjs";import{o as resolveAgentRoute}from"./resolve-route-DpjPYoyg.mjs";import{t as resolveChannelAccountBindingRepairInput}from"./legacy-config-binding-repair-input-CtZME5H8.mjs";import{r as resolveReadOnlyChannelPluginsForConfig}from"./read-only-B-Uv2t8Q.mjs";
//!src/commands/doctor/shared/legacy-config-binding-repair.ts
function pruneBindingsForMissingAgents(e,t){const n=asNullableRecord(e);const r=asNullableRecord(n?.agents)?.list;if(!n||!Array.isArray(r)||r.length===0||!Array.isArray(n.bindings))return e;const i=r;const a=n.bindings;const o=i.filter(e=>isRecord(e)&&typeof e.id===`string`);if(o.length!==i.length)return e;const s=new Set(o.map(e=>normalizeAgentId(e.id)));const c=a.filter(e=>{const t=asNullableRecord(e)?.agentId;return typeof t!==`string`||t===`main`||s.has(normalizeAgentId(t))});const l=a.length-c.length;if(l===0)return e;t.push(`Removed ${l} binding${l===1?``:`s`} that referenced missing agents.list ids.`);return{...e,bindings:c.length>0?c:void 0}}function repairUnownedChannelAccountBindings({config:e,sourceConfigBeforeMigrations:t}){const n=resolveChannelAccountBindingRepairInput(e);if(!n)return{config:e,changes:[]};const{agentIds:r,bindings:i}=n;const a=[];const o=[];const s=asNullableRecord(asNullableRecord(t)?.agents);const c=s?.list;const l=s?.ownership===void 0&&s?.entries===void 0&&Array.isArray(c)&&c.length>1&&c.every(e=>isRecord(e)&&typeof e.id===`string`&&e.id.trim().length>0&&(e.default===void 0||e.default===false))?projectLegacyAgentRosterEntries(c).entries[0]?.id:void 0;const u=resolveReadOnlyChannelPluginsForConfig(e,{includePersistedAuthState:false,includeSetupFallbackPlugins:true});const d=new Map(u.plugins.map(e=>[e.id,e]));for(const t of[...u.configuredChannelIds].toSorted()){const n=d.get(t);const i=asNullableRecord(e.channels?.[t]);if(!n||i?.enabled===false)continue;const u=asNullableRecord(i?.accounts)??void 0;const f=[...new Set(n.config.listAccountIds(e).map(normalizeAccountId))].toSorted();for(const n of f){const i=resolveChannelAccountEntry(u,n,t,normalizeAccountId);if(asNullableRecord(i)?.enabled===false)continue;let d;try{const r=resolveAgentRoute({cfg:e,channel:t,accountId:n});if(!l||r.matchedBy!=="default")continue}catch(e){if(!(e instanceof AgentSelectionRequiredError))throw e;d=e}const f=l;if(f&&r.has(f))a.push({agentId:f,match:{channel:t,accountId:n}});else if(d)o.push(!Array.isArray(c)&&!isRecord(s?.entries)?`${t}:${n} unresolved: original roster unavailable. ${d.message}`:d.message)}}return{config:a.length?{...e,bindings:[...i,...a]}:e,changes:a.map(({agentId:e,match:t})=>`Preserved ${t.channel}:${t.accountId} ownership with binding ${JSON.stringify({agentId:e,match:t})}.`),...o.length>0?{warnings:o}:{}}}export{repairUnownedChannelAccountBindings as n,pruneBindingsForMissingAgents as t};