UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.19 kB
import"./openclaw-state-read-map-size-DJbMcZYM.mjs";import{v as e}from"./openclaw-state-read-home-dir-BQZX1pD2.mjs";import{r as t,t as n}from"./openclaw-state-read-pid-alive-_-Y1IvGj.mjs";import{a as r,r as i}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{t as a}from"./openclaw-state-read-errors-D_saNBXy.mjs";import"./openclaw-state-read-backoff-DDprjZZH.mjs";import{randomUUID as o}from"node:crypto";const s=18e5;function readProcessStartTime(e){return!Number.isSafeInteger(e)||e<=0?null:process.platform===`darwin`?n(e,process.env,2e3):t(e)}const c=readProcessStartTime(process.pid);createIngressDrainOwnerId();const{liveIngressDrainInstanceIds:l,retiredClaims:u}=e(Symbol.for(`openclaw.ingressClaimCustody`),()=>({liveIngressDrainInstanceIds:/* @__PURE__ */ new Set,retiredClaims:/* @__PURE__ */ new Set}));function createIngressDrainOwnerId(){return[process.pid,c??`x`,o()].join(`:`)}const d=[`queue-cap`,`queue-cap-old`,`queue-cap-new`];function countFailedChannelIngressQueueEntriesInDatabase(e){let t=r(e);return i(e,t.selectFrom(`channel_ingress_events`).select(e=>[`channel_id as channelId`,`account_id as accountId`,e.fn.countAll().as(`count`),e.fn.min(`failed_at`).as(`oldestFailedAt`)]).where(`status`,`=`,`failed`).where(e=>e.or([e(`failed_reason`,`is`,null),e(`failed_reason`,`not in`,d)])).groupBy([`channel_id`,`account_id`]).orderBy(`channel_id`,`asc`).orderBy(`account_id`,`asc`)).rows.map(({oldestFailedAt:e,...t})=>e==null?t:Object.assign(t,{oldestFailedAt:e}))}function countChannelIngressQueuePressureInDatabase(e,t){let n=r(e),a=t-s,o=n.selectFrom(`channel_ingress_events`).select(e=>[`channel_id`,`account_id`,e.fn.countAll().as(`activeCount`),e.fn.countAll().filterWhere(`status`,`=`,`pending`).as(`pendingCount`),e.fn.countAll().filterWhere(`status`,`=`,`claimed`).as(`claimedCount`),e.fn.min(`received_at`).as(`oldestReceivedAt`),e.fn.min(`claimed_at`).filterWhere(`status`,`=`,`claimed`).filterWhere(`claimed_at`,`>`,a).as(`nextClaimedAt`),e.fn.countAll().filterWhere(e=>e.or([e.and([e(`attempts`,`>=`,8),e(`last_error`,`is not`,null)]),e.and([e(`status`,`=`,`claimed`),e(`claimed_at`,`<=`,a)])])).as(`pressuredCount`)]).where(`status`,`in`,[`pending`,`claimed`]).where(`lane_key`,`is not`,null).groupBy([`queue_name`,`lane_key`,`channel_id`,`account_id`]).as(`lanes`),c=i(e,n.selectFrom(o).select(e=>[`lanes.channel_id as channelId`,`lanes.account_id as accountId`,e.fn.countAll().filterWhere(`lanes.pressuredCount`,`>`,0).as(`laneCount`),e.cast(e.fn.sum(`lanes.pendingCount`).filterWhere(`lanes.pressuredCount`,`>`,0),`bigint`).as(`pendingCount`),e.cast(e.fn.sum(`lanes.claimedCount`).filterWhere(`lanes.pressuredCount`,`>`,0),`bigint`).as(`claimedCount`),e.cast(e(e.fn.sum(`lanes.activeCount`).filterWhere(`lanes.pressuredCount`,`>`,0),`-`,e.fn.countAll().filterWhere(`lanes.pressuredCount`,`>`,0)),`bigint`).as(`blockedCount`),e.fn.min(`lanes.oldestReceivedAt`).filterWhere(`lanes.pressuredCount`,`>`,0).as(`oldestReceivedAt`),e.fn.min(`lanes.nextClaimedAt`).as(`nextClaimedAt`)]).groupBy([`lanes.channel_id`,`lanes.account_id`]).orderBy(`lanes.channel_id`,`asc`).orderBy(`lanes.account_id`,`asc`)).rows,l=null;return{pressure:c.flatMap(({nextClaimedAt:e,...t})=>(e!=null&&(l=Math.min(l??1/0,e+s)),t.laneCount>0?[t]:[])),nextClaimExpiryAtMs:l}}function listChannelIngressAccountsInDatabase(e,t){return i(e,r(e).selectFrom(`channel_ingress_events`).select(`account_id`).distinct().where(`channel_id`,`=`,t.channelId).orderBy(`account_id`,`asc`)).rows.map(e=>e.account_id)}function readChannelIngressInDatabase(e,t){if(t.type===`channelIngress.accounts`)return{ok:!0,sourceAdmitted:!0,type:t.type,result:listChannelIngressAccountsInDatabase(e,t.input)};if(t.type===`channelIngress.failedHealth`)return{ok:!0,sourceAdmitted:!0,type:t.type,result:countFailedChannelIngressQueueEntriesInDatabase(e)};let n={errors:{}};if(t.input.failed)try{n.failed=countFailedChannelIngressQueueEntriesInDatabase(e)}catch(e){n.errors.failed=a(e)}if(t.input.pressure)try{n.pressure=countChannelIngressQueuePressureInDatabase(e,t.input.now)}catch(e){n.errors.pressure=a(e)}return{ok:!0,sourceAdmitted:!0,type:t.type,result:n}}export{readChannelIngressInDatabase};