openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 1.8 kB
JavaScript
import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{s as readFileDescriptorBounded}from"./boundary-file-read-bB9An35K.mjs";import{t as readByteStreamWithLimit}from"./read-byte-stream-with-limit-Ockoa332.mjs";import fs from"node:fs/promises";import{TextDecoder}from"node:util";
//!src/commands/agent-message-input.ts
const MESSAGE_FILE_DECODER=new TextDecoder(`utf-8`,{fatal:true});function formatTextFileReadFailure(e,t,n){const r=asOptionalRecord(n)?.code;if(r===`ENOENT`)return`${e} file not found: ${t}`;if(r===`EISDIR`)return`${e} file is a directory: ${t}`;const i=n instanceof Error?n.message:String(n);return`Unable to read ${e.toLowerCase()} file ${t}: ${i}`}const AGENT_MESSAGE_FILE_MAX_BYTES=4194304;async function readBoundedCliTextInput(e,t){const n=t?.maxBytes??AGENT_MESSAGE_FILE_MAX_BYTES;const r=t?.label??`Message`;let i;if(e===`-`)i=await readByteStreamWithLimit(process.stdin,{maxBytes:n,onOverflow:()=>/* @__PURE__ */ new Error(`${r} stdin exceeds ${n} bytes`)});else{let t;try{t=await fs.open(e,`r`);const a=await t.stat();if(a.isDirectory())throw Object.assign(/* @__PURE__ */ new Error(`${r} file is a directory`),{code:`EISDIR`});if(a.isFile()&&a.size>n)throw new Error(`File exceeds ${n} bytes: ${e}`);i=await readFileDescriptorBounded(t.fd,n)}catch(t){throw new Error(formatTextFileReadFailure(r,e,t),{cause:t})}finally{await t?.close().catch(()=>void 0)}}let a;try{a=MESSAGE_FILE_DECODER.decode(i).replace(/^\uFEFF/,``)}catch{throw new Error(e===`-`?`${r} stdin must be valid UTF-8`:`${r} file must be valid UTF-8: ${e}`)}if(!a.trim())throw new Error(e===`-`?`${r} stdin is empty`:`${r} file is empty: ${e}`);return a}async function readAgentMessageFile(e){return readBoundedCliTextInput(e)}export{readBoundedCliTextInput as n,readAgentMessageFile as t};