UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 3.75 kB
import{r as e,t}from"./openclaw-state-read-pid-alive-C5QrpWWk.mjs";import{t as n}from"./openclaw-state-read-errors-D4PLrhBz.mjs";import{a as r,r as i}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import"./openclaw-state-read-plugin-instance-error-kZ_c_Ig5.mjs";import"./openclaw-state-read-backoff-B79CSFak.mjs";import{randomUUID as a}from"node:crypto";const o=18e5;function readProcessStartTime(n){return!Number.isSafeInteger(n)||n<=0?null:process.platform===`darwin`?t(n,process.env,2e3):e(n)}const s=readProcessStartTime(process.pid);createIngressDrainOwnerId();function createIngressDrainOwnerId(){return[process.pid,s??`x`,a()].join(`:`)}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`).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-o,s=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(s).select(e=>[`lanes.channel_id as channelId`,`lanes.account_id as accountId`,e.fn.countAll().filterWhere(`lanes.pressuredCount`,`>`,0).as(`laneCount`),e.fn.sum(`lanes.pendingCount`).filterWhere(`lanes.pressuredCount`,`>`,0).as(`pendingCount`),e.fn.sum(`lanes.claimedCount`).filterWhere(`lanes.pressuredCount`,`>`,0).as(`claimedCount`),e(e.fn.sum(`lanes.activeCount`).filterWhere(`lanes.pressuredCount`,`>`,0),`-`,e.fn.countAll().filterWhere(`lanes.pressuredCount`,`>`,0)).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+o)),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 r={errors:{}};if(t.input.failed)try{r.failed=countFailedChannelIngressQueueEntriesInDatabase(e)}catch(e){r.errors.failed=n(e)}if(t.input.pressure)try{r.pressure=countChannelIngressQueuePressureInDatabase(e,t.input.now)}catch(e){r.errors.pressure=n(e)}return{ok:!0,sourceAdmitted:!0,type:t.type,result:r}}export{readChannelIngressInDatabase};