@oracle-agent/oracle 0.21.0 → 0.22.1

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{createHash as $,randomBytes as C}from"node:crypto";import{chmodSync as h,mkdirSync as R,readFileSync as x,renameSync as H,rmSync as k,writeFileSync as L}from"node:fs";import{homedir as D}from"node:os";import{join as O}from"node:path";var m="oa1",j=12,F=1440*60*1e3,T=new Set(["evm-swap","sol","btc"]);function c(e){throw new Error(`oracle-telegram-callback: ${e}`)}function p(e,a){return(!e||typeof e!="object"||Array.isArray(e)||Object.getPrototypeOf(e)!==Object.prototype)&&c(`${a} must be an object`),e}function v(e,a){try{return JSON.parse(JSON.stringify(e))}catch{c(`${a} must be JSON-serializable`)}}function B(e,a){let i=`${m}:${e}:${a}`;return Buffer.byteLength(i,"utf8")>=64&&c("callback ID exceeds Telegram's 64-byte limit"),i}function P(e){if(e.ownerId)return String(e.ownerId);let a=e.ownerConfig?.owner;if(typeof a=="string"&&a.startsWith("telegram:"))return a.slice(9);if(a?.platform==="telegram"&&a.userId)return String(a.userId);let i=String(e.env?.ORACLE_OWNER_TELEGRAM??process.env.ORACLE_OWNER_TELEGRAM??"").trim();if(i)return i;c("an exact Telegram owner identity is required")}function z(e={}){let a=e.control,i=a?.oracle_control_arm?t=>a.oracle_control_arm(t):a?.call?t=>a.call("oracle_control_arm",t):null,g=a?.oracle_control_confirm?t=>a.oracle_control_confirm(t):a?.call?t=>a.call("oracle_control_confirm",t):null;(!i||!g)&&c("bounded arm/confirm control is required");let b=P(e),E=e.now??Date.now,y=e.randomBytes??C,u=e.callbackDir??O(D(),".config","oracle","telegram-callbacks");R(u,{recursive:!0,mode:448}),h(u,448);function d(t){return O(u,`${$("sha256").update(t).digest("hex")}.json`)}function N(t){try{return x(d(t)),!0}catch(o){if(o?.code==="ENOENT")return!1;throw o}}function S(t,o){let r=d(t),n=`${r}.${process.pid}.${y(6).toString("hex")}.tmp`;L(n,JSON.stringify(o),{mode:384,flag:"wx"}),h(n,384),H(n,r),h(r,384)}function M(t){try{return JSON.parse(x(d(t),"utf8"))}catch(o){if(o?.code==="ENOENT")return null;c("callback state is unreadable")}}function _(t,o,r){let n=M(t);return(!n||n.kind!==o)&&c("unknown or consumed callback"),String(r??"")!==n.chatId&&c("chat mismatch"),E()>=n.expiresAtMs&&(k(d(t),{force:!0}),c("callback expired")),k(d(t),{force:!0}),n}function I(t){for(let o=0;o<8;o++){let r=B(t,y(j).toString("base64url"));if(!N(r))return r}c("could not allocate a unique callback ID")}return Object.freeze({issue(t,o={}){let r=v(p(t,"action"),"action"),n=p(r.intent,"action.intent"),s=String(n.surface??"");T.has(s)||c(`unsupported surface ${s||"missing"}`),(s==="sol"||s==="btc")&&!r.prepared&&c(`${s} action requires a prepared envelope`);let l=o.ttlMs??12e4;(!Number.isSafeInteger(l)||l<1||l>F)&&c("ttlMs is outside the bounded range");let f=E()+l,A=String(o.chatId??b);A||c("an exact Telegram chat identity is required");let w=I("a");return S(w,{kind:"arm",expiresAtMs:f,chatId:A,action:{...r,expiresAt:new Date(f).toISOString(),maxExecutions:1}}),w},async handle(t){p(t,"callback request"),(t.platform!=="telegram"||String(t.senderId??"")!==b)&&c("owner mismatch");let o=String(t.callbackData??"");if(o.startsWith(`${m}:a:`)){let r=_(o,"arm",t.chatId),n=await i(r.action);(n?.state??n?.status)!=="pending_confirmation"&&c("arm did not create a pending action"),n?.maxExecutions!=null&&n.maxExecutions!==1&&c("arm did not create a one-shot action"),(!n?.id||!/^[a-f0-9]{64}$/i.test(String(n.intentHash??"")))&&c("arm did not return an exact intent hash");let l=I("c"),f=Date.parse(n.expiresAt??"");return S(l,{kind:"confirm",expiresAtMs:Number.isFinite(f)?Math.min(r.expiresAtMs,f):r.expiresAtMs,chatId:r.chatId,actionId:n.id,intentHash:n.intentHash}),{phase:"pending_confirmation",callbackData:l,actionId:n.id,intentHash:n.intentHash}}if(o.startsWith(`${m}:c:`)){let r=_(o,"confirm",t.chatId),n=await g({id:r.actionId,intentHash:r.intentHash}),s=n?.state??n?.status;return(n?.id!==r.actionId||n?.intentHash!==r.intentHash||s!=="active")&&c("control confirmed a different action"),{phase:"active",actionId:n.id,intentHash:n.intentHash}}c("unknown or consumed callback")}})}var K=m,U=Object.freeze([...T]);export{K as TELEGRAM_EXACT_ACTION_CALLBACK_PREFIX,U as TELEGRAM_EXACT_ACTION_SURFACES,z as createTelegramExactActionCallbacks};