@yuandc/aica 0.1.92 → 0.1.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{record as i}from"../../acp/providers/workbuddy/config.js";const h=new Intl.DateTimeFormat("en-GB",{timeZone:"Asia/Shanghai",hour:"2-digit",hourCycle:"h23"});function d(t){const e=Number(h.format(t));return e>=7&&e<19}async function w(t){try{const e=t.now??Date.now;if(!d(e()))return;const r=AbortSignal.timeout(8e3),o=async s=>{const n=await(t.fetch??fetch)(`https://www.codebuddy.cn/v2/billing/meter/${s}`,{method:"POST",headers:t.headers,redirect:"error",signal:r,body:"{}"});if(!n.ok){await n.body?.cancel();return}const c=i(await n.json());if(c.code===0)return i(c.data)},a=await o("checkin-activity-status");if(a?.active!==!0||a.today_checked_in!==!1||r.aborted||!d(e()))return;await o("daily-checkin")}catch{}}export{w as checkinWorkBuddy,d as inWorkBuddyCheckinWindow};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createWorkBuddyAuthTransport as
|
|
1
|
+
import{createWorkBuddyAuthTransport as T}from"../../acp/providers/workbuddy/auth-transport.js";import{enterWorkBuddyTask as _}from"../../acp/providers/workbuddy/auth-gate.js";import{workBuddyRegion as R,workBuddyEnvironmentCredentials as S}from"../../acp/providers/workbuddy/auth-environment.js";import{record as s}from"../../acp/providers/workbuddy/config.js";import{checkinWorkBuddy as P}from"./checkin.js";function m(n,t=null){return{state:n,account:t,remaining:null,total:null,unlimited:!1,resources:[]}}function h(n){if(typeof n!="number"&&(typeof n!="string"||!/^-?\d+(?:\.\d+)?$/.test(n)))return;const t=Number(n);return Number.isFinite(t)?t:void 0}function A(n){const t=[];for(const r of n){const e=s(r);if(e.Status!==void 0&&![0,3].includes(Number(e.Status)))continue;const a=h(e.CycleCapacityRemainPrecise??e.CycleCapacityRemain??e.CapacityRemainPrecise??e.CapacityRemain),i=h(e.CycleCapacitySizePrecise??e.CycleCapacitySize??e.CapacitySizePrecise??e.CapacitySize),c=e.Unlimited===!0||e.Unlimited===1||i===-1;if(a===void 0||i===void 0||!c&&(i<0||a<0))throw new Error("invalid_credits");const g=typeof e.CycleEndTime=="string"&&e.CycleEndTime.length<=100?e.CycleEndTime:null;t.push({name:typeof e.PackageName=="string"?e.PackageName.slice(0,200):"积分套餐",remaining:a,total:i,unlimited:c,cycleEnd:g})}const o=t.some(r=>r.unlimited),l=r=>Math.round(t.reduce((e,a)=>e+a[r],0)*100)/100;return{remaining:o?null:l("remaining"),total:o?null:l("total"),unlimited:o,resources:t}}async function U(n={}){let t=null,o,l;try{l=_();const r=R();o=(n.transport??T)(),await o.initialize();const e=await o.billingIdentity();if(!e.account)return m(S()?"unsupported":"signed_out");if(t={...e.account,region:r,scope:e.enterpriseId.slice(0,200)},r==="ioa"||!e.token)return m("unsupported",t);const a=r==="internal"?"https://www.codebuddy.cn":"https://www.codebuddy.ai",i={Authorization:`Bearer ${e.token}`,"Content-Type":"application/json",Accept:"application/json","X-User-Id":t.id};e.enterpriseId&&(i["X-Enterprise-Id"]=e.enterpriseId,i["X-Tenant-Id"]=e.enterpriseId),n.allowCheckin&&r==="internal"&&await P({headers:i,fetch:n.fetch,now:n.now});const c=[],g=AbortSignal.timeout(25e3),C=new Date,w=new Date(C);w.setUTCFullYear(w.getUTCFullYear()+101);const b=p=>p.toISOString().slice(0,19).replace("T"," ");for(let p=1;p<=10;p++){const d=await(n.fetch??fetch)(`${a}/v2/billing/meter/get-user-resource`,{method:"POST",headers:i,redirect:"error",signal:g,body:JSON.stringify({PageNumber:p,PageSize:100,ProductCode:"p_tcaca",Status:[0,3],PackageEndTimeRangeBegin:b(C),PackageEndTimeRangeEnd:b(w)})});if(d.status===429){const f=d.headers.get("Retry-After"),E=f&&/^\d+$/.test(f)?Number(f)*1e3:Date.parse(f||"")-Date.now();return{...m("error",t),retryAfterMs:Number.isFinite(E)?Math.max(0,E):3e5}}if(d.status===401||d.status===403)return m("signed_out");if(!d.ok)throw new Error("credits_http_error");const u=s(await d.json());if(u.code!==void 0&&![0,"0"].includes(u.code))throw new Error("credits_business_error");const y=s(s(s(u.data).Response??u.Response).Data);if(s(s(u.data).Response??u.Response).Error||!Array.isArray(y.Accounts))throw new Error("invalid_credits_response");c.push(...y.Accounts);const k=h(y.TotalCount);if(y.Accounts.length<100||k!==void 0&&c.length>=k)return{state:"ready",account:t,...A(c)}}throw new Error("credits_page_limit")}catch{return m("error",t)}finally{try{await o?.close()}finally{l?.()}}}export{m as emptyCredits,A as parseCreditsResources,U as queryWorkBuddyCredits};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{emptyCredits as r}from"./query.js";import{ACTIVE_CREDITS_INTERVAL_MS as n,IDLE_CREDITS_INTERVAL_MS as u}from"./types.js";class y{query;changed;now;value={...r("error"),fetchedAtMs:null,checkedAtMs:null,revision:0};lastAttempt=-1/0;retryAt=0;limitedUntil=0;failures=0;dirty=!1;active=0;epoch=0;timer;pending;running=!1;constructor(t,s,e=Date.now){this.query=t,this.changed=s,this.now=e}snapshot(){return this.value}start(){this.running||(this.running=!0,this.tick())}async stop(){this.running=!1,clearTimeout(this.timer),await this.pending}beginUsage(){this.active++,this.dirty=!0,this.running&&this.tick();let t=!1;return()=>{t||(t=!0,this.active--,this.dirty=!0,this.running&&this.tick())}}reset(){this.epoch++,this.lastAttempt=-1/0,this.retryAt=0,this.limitedUntil=0,this.failures=0,this.dirty=!1,this.value={...r("error"),fetchedAtMs:null,checkedAtMs:null,revision:this.value.revision+1},this.changed(this.value),this.running&&(this.pending??Promise.resolve()).then(()=>{if(this.running)return this.tick()})}nextDue(){const t=this.dirty||this.active||this.failures?n:u;return Math.max(this.lastAttempt+t,this.retryAt,this.limitedUntil)}async tick(){
|
|
1
|
+
import{emptyCredits as r}from"./query.js";import{ACTIVE_CREDITS_INTERVAL_MS as n,IDLE_CREDITS_INTERVAL_MS as u}from"./types.js";class y{query;changed;now;value={...r("error"),fetchedAtMs:null,checkedAtMs:null,revision:0};lastAttempt=-1/0;retryAt=0;limitedUntil=0;failures=0;dirty=!1;active=0;epoch=0;timer;pending;running=!1;constructor(t,s,e=Date.now){this.query=t,this.changed=s,this.now=e}snapshot(){return this.value}start(){this.running||(this.running=!0,this.tick())}async stop(){this.running=!1,clearTimeout(this.timer),await this.pending}beginUsage(){this.active++,this.dirty=!0,this.running&&this.tick();let t=!1;return()=>{t||(t=!0,this.active--,this.dirty=!0,this.running&&this.tick())}}reset(){this.epoch++,this.lastAttempt=-1/0,this.retryAt=0,this.limitedUntil=0,this.failures=0,this.dirty=!1,this.value={...r("error"),fetchedAtMs:null,checkedAtMs:null,revision:this.value.revision+1},this.changed(this.value),this.running&&(this.pending??Promise.resolve()).then(()=>{if(this.running)return this.tick()})}nextDue(){const t=this.dirty||this.active||this.failures?n:u;return Math.max(this.lastAttempt+t,this.retryAt,this.limitedUntil)}async tick(){if(this.pending)return this.pending;if(this.now()>=this.nextDue()){const t=Number.isFinite(this.lastAttempt)?this.failures?"retry":this.dirty||this.active?"usage":"hourly":"initial";return this.collect(t)}return this.arm(),this.value}refresh(t=!1){return this.pending?this.pending:t&&this.now()<this.limitedUntil?Promise.resolve(this.value):this.collect("manual")}collect(t){const s=this.epoch,e=this.now();this.lastAttempt=e;const l=this.dirty;return this.dirty=!1,this.pending=Promise.resolve().then(()=>this.query(t)).catch(()=>r("error")).then(i=>{if(s!==this.epoch)return this.value;const o=i.state==="error"&&!i.account||JSON.stringify(i.account)===JSON.stringify(this.value.account),h=i.state==="error";h?(this.failures++,this.dirty||=l,this.retryAt=this.now()+Math.max(n,Math.min(u,n*2**Math.min(4,this.failures-1)))):(this.failures=0,this.retryAt=0,this.limitedUntil=0),i.retryAfterMs&&(this.limitedUntil=this.now()+i.retryAfterMs);const{retryAfterMs:f,...c}=i,a=h&&o&&this.value.fetchedAtMs!==null;return this.value={...c,...a?{account:this.value.account,remaining:this.value.remaining,total:this.value.total,unlimited:this.value.unlimited,resources:this.value.resources}:{},fetchedAtMs:i.state==="ready"?this.now():a?this.value.fetchedAtMs:null,checkedAtMs:e,revision:this.value.revision+1},this.changed(this.value),this.value}).finally(()=>{this.pending=void 0,this.arm()}),this.pending}arm(){clearTimeout(this.timer),!(!this.running||this.pending)&&(this.timer=setTimeout(()=>{this.tick()},Math.min(2147483647,Math.max(1,this.nextDue()-this.now()))),this.timer.unref())}}export{y as WorkBuddyCreditsScheduler};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{acaServerRequest as u}from"../aca-server-client.js";import{loadAcaConfig as a}from"../aca-config.js";import{managementActivity as i}from"../management/activity.js";import{emptyCredits as f,queryWorkBuddyCredits as
|
|
1
|
+
import{acaServerRequest as u}from"../aca-server-client.js";import{loadAcaConfig as a}from"../aca-config.js";import{managementActivity as i}from"../management/activity.js";import{emptyCredits as f,queryWorkBuddyCredits as l}from"./query.js";import{WorkBuddyCreditsScheduler as p}from"./scheduler.js";let r=!1,s=!1,d="",o=Promise.resolve();const n=new p(async e=>{const t=i.enter("workbuddy-credits");if(!t)return f("error");try{return await l({allowCheckin:e==="hourly"})}finally{t()}},()=>{c()});function c(){const e=a();if(!e.token||!r)return Promise.resolve();const t=n.snapshot();return o=o.catch(()=>{}).then(()=>u("POST","/api/client/workbuddy-credits",{machineId:e.machineId,instanceId:process.env.ACA_WORKER_INSTANCE_ID||String(process.pid),snapshot:t},{serverUrl:e.serverUrl,token:e.token})).catch(()=>{}),o}function w(){r=!0,s&&m()}function m(){if(s=!0,!r||i.paused||i.stopping)return;const e=a(),t=`${e.serverUrl}\0${e.machineId}\0${e.token}`;d&&d!==t&&n.reset(),d=t,n.start(),c()}async function g(){r=!1,s=!1,await n.stop(),await o}export{w as activateWorkBuddyCredits,g as stopWorkBuddyCredits,n as workBuddyCredits,m as workBuddyCreditsConnected};
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const o="0.1.
|
|
1
|
+
const o="0.1.93";export{o as AICA_VERSION};
|