UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

124 lines • 4.66 kB
const READ_RANGE_MAX_BYTES = 1024 * 1024; export class ArgsError extends Error { } export const bad = (detail) => { throw new ArgsError(detail); }; const keys = (a, required, optional = []) => { for (const k of required) if (a[k] === undefined) bad(`${k} is required`); const allowed = new Set([...required, ...optional]); for (const k of Object.keys(a)) if (!allowed.has(k)) bad(`unknown argument ${k}`); }; const text = (v, name) => (typeof v === "string" ? v : bad(`${name} must be a string`)); const nonEmpty = (v, name) => text(v, name).length > 0 ? text(v, name) : bad(`${name} must not be empty`); const uint = (v, name) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0 ? v : bad(`${name} must be a non-negative integer`); const opt = (v, f, name) => v === undefined ? undefined : f(v, name); const flag = (v, name) => (typeof v === "boolean" ? v : bad(`${name} must be a boolean`)); const bytes = (v, name) => { if (v instanceof Uint8Array) return v; if (typeof v === "string" && /^[A-Za-z0-9+/]*={0,2}$/.test(v) && v.length % 4 === 0) return new Uint8Array(Buffer.from(v, "base64")); return bad(`${name} must be bytes or base64`); }; const map = (v, name) => typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Uint8Array) ? v : bad(`${name} must be a map`); const env = (v, name) => { const m = map(v, name); return Object.fromEntries(Object.entries(m).map(([k, x]) => [k, text(x, `${name}.${k}`)])); }; const some = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)); export const readArgs = (a) => { keys(a, ["path"], ["offset", "limit_bytes"]); return some({ path: nonEmpty(a.path, "path"), offset: opt(a.offset, uint, "offset"), limitBytes: opt(a.limit_bytes, uint, "limit_bytes"), }); }; export const readRangeArgs = (a) => { keys(a, ["path", "offset", "length"]); const length = uint(a.length, "length"); if (length > READ_RANGE_MAX_BYTES) bad(`length is above ${READ_RANGE_MAX_BYTES}`); return { path: nonEmpty(a.path, "path"), offset: uint(a.offset, "offset"), limitBytes: length }; }; export const writeArgs = (a) => { keys(a, ["path", "bytes"], ["create_parent_dirs"]); return some({ path: nonEmpty(a.path, "path"), bytes: bytes(a.bytes, "bytes"), createParentDirs: opt(a.create_parent_dirs, flag, "create_parent_dirs"), }); }; export const writeRangeArgs = (a) => { keys(a, ["path", "offset", "bytes", "total"]); return { path: nonEmpty(a.path, "path"), offset: uint(a.offset, "offset"), bytes: bytes(a.bytes, "bytes"), total: uint(a.total, "total"), }; }; export const editArgs = (a) => { keys(a, ["path", "exact", "replacement"]); return { path: nonEmpty(a.path, "path"), exact: text(a.exact, "exact"), replacement: text(a.replacement, "replacement"), }; }; export const globArgs = (a) => { keys(a, ["pattern"], ["cwd", "limit"]); return some({ pattern: nonEmpty(a.pattern, "pattern"), cwd: opt(a.cwd, nonEmpty, "cwd"), limit: opt(a.limit, uint, "limit"), }); }; export const grepArgs = (a) => { keys(a, ["path", "pattern"], ["limit"]); return some({ path: nonEmpty(a.path, "path"), pattern: nonEmpty(a.pattern, "pattern"), limit: opt(a.limit, uint, "limit"), }); }; export const statArgs = (a) => { keys(a, ["path"]); return { path: nonEmpty(a.path, "path") }; }; export const listArgs = (a) => { keys(a, ["path"]); return { path: nonEmpty(a.path, "path") }; }; export const shellArgs = (a) => { keys(a, ["command"], ["kind", "cwd", "env", "timeoutMs"]); if (a.kind !== undefined && a.kind !== "shell") bad("kind must be shell"); const timeoutMs = opt(a.timeoutMs, uint, "timeoutMs"); if (timeoutMs === 0) bad("timeoutMs must be positive"); return some({ command: nonEmpty(a.command, "command"), cwd: opt(a.cwd, nonEmpty, "cwd"), env: opt(a.env, env, "env"), timeoutMs, }); }; export const processInfoArgs = (a) => { keys(a, [], ["pid", "handleId"]); if (a.pid !== undefined && a.handleId !== undefined) bad("pid and handleId are exclusive"); return some({ pid: opt(a.pid, uint, "pid"), handleId: opt(a.handleId, nonEmpty, "handleId") }); }; export const meshMessageArgs = (a) => { keys(a, ["topic", "body"]); return { topic: nonEmpty(a.topic, "topic"), body: a.body }; }; //# sourceMappingURL=op-args.js.map