openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 1.45 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as pruneMapToMaxSize}from"./map-size-DLQqorvK.mjs";import{t as KeyedAsyncQueue}from"./keyed-async-queue-qBcorzrt.mjs";import path from"node:path";import fs from"node:fs/promises";
//!src/agents/models-config-state.ts
const MODELS_JSON_STATE=resolveGlobalSingleton(Symbol.for(`openclaw.modelsJsonState`),()=>({writeQueue:new KeyedAsyncQueue,readyCache:/* @__PURE__ */ new Map,costCache:/* @__PURE__ */ new Map}));function publishModelsJsonCostSource(e,t){let n;try{n=t?JSON.parse(t)?.providers:void 0}catch{n=void 0}const r={providers:n};MODELS_JSON_STATE.costCache.set(e,r);pruneMapToMaxSize(MODELS_JSON_STATE.costCache,128);return r}async function readModelsJsonSource(e,t){let n;try{n=await fs.readFile(t,`utf8`)}catch(e){if(e.code!==`ENOENT`)throw e;n=null}return{contents:n,pricing:publishModelsJsonCostSource(e,n)}}async function captureModelsJsonSource(e){const t=path.join(e,`models.json`);return MODELS_JSON_STATE.writeQueue.enqueue(t,()=>readModelsJsonSource(e,t))}async function captureModelsJsonCostSource(e){const t=MODELS_JSON_STATE.costCache.get(e);if(t)return t;const n=path.join(e,`models.json`);return MODELS_JSON_STATE.writeQueue.enqueue(n,async()=>MODELS_JSON_STATE.costCache.get(e)??(await readModelsJsonSource(e,n)).pricing)}export{publishModelsJsonCostSource as i,captureModelsJsonCostSource as n,captureModelsJsonSource as r,MODELS_JSON_STATE as t};