UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

75 lines (74 loc) • 3.64 kB
import { findActionsOf, findWordOf } from "./find-actions.js"; import { feedsArgvFromStdin, launcherOptionPathRole, launcherOptionStep, programRow, readOptionRole } from "./program-read-model.js"; import { programBasename, readProgramPosition } from "./bash-program-position.js"; import { removalActionOf } from "./removal-actions.js"; function operandsFromFile(argv, from, to, row) { for (let j = from; j < to; j++) { const w = argv[j]?.folded ?? ""; if (!w.startsWith("-") || w === "-") return false; const step = launcherOptionStep(row, w); if (step === undefined) return false; if (step === 2) { if (launcherOptionPathRole(row, w) !== undefined) return true; j++; continue; } const own = readOptionRole(row, w); if (own.form === "attached" && own.role?.kind === "path") return true; } return false; } function pastStatement(segments, at) { const { text, connector } = segments[at]; let k = at + 1; while (k < segments.length && segments[k].text === text && segments[k].connector === connector) k++; return k; } const removesOperands = (program) => removalActionOf(programRow(programBasename(program))?.key ?? "") === "operands"; function feedsRemover(consumer) { if (consumer.connector !== "|" || consumer.redirects.some((r) => r.direction === "in" && (r.fd === undefined || r.fd === 0))) return false; const argv = consumer.argv; const into = readProgramPosition(argv); const fed = argv.findIndex((w) => feedsArgvFromStdin(programBasename(w.folded))); const runs = into.refusal === undefined ? (into.argvFromStdin ? into.program?.index : undefined) : fed < 0 ? undefined : argv.findIndex((w, k) => k > fed && removesOperands(w.folded)); if (runs === undefined || runs < 0 || !removesOperands(argv[runs].folded)) return false; let nearest = runs - 1; while (nearest >= 0 && !feedsArgvFromStdin(programBasename(argv[nearest].folded))) nearest--; const launchers = into.refusal === undefined ? into.launchers.map((l, n) => ({ at: l.index, to: into.launchers[n + 1]?.index ?? runs })) : [{ at: nearest, to: runs }]; for (const { at, to } of launchers) { const name = programBasename(argv[at].folded); const row = feedsArgvFromStdin(name) ? programRow(name) : undefined; if (row !== undefined && operandsFromFile(argv, at + 1, to, row)) return false; } return true; } export function pipedRemovalStarts(segments, at) { const producer = segments[at]; const next = pastStatement(segments, at); if (next >= segments.length || !segments.slice(next, pastStatement(segments, next)).some(feedsRemover)) return undefined; if (producer.redirects.some((r) => (r.fd ?? (r.op.startsWith("<") ? 0 : 1)) === 1)) return undefined; const from = readProgramPosition(producer.argv); const row = programRow("find"); if (row === undefined || from.refusal !== undefined || from.program?.basename !== "find") return undefined; const args = producer.argv.slice(from.program.index + 1).map(findWordOf); const actions = findActionsOf(row, args); if (actions.startsFromFile) return undefined; const named = actions.starts.flatMap((k) => { const start = args[k]; return start === undefined ? [] : [start.raw]; }); return actions.starts.length === 0 ? { raws: new Set([producer.programStart?.raw ?? "."]), implicit: true } : { raws: new Set(named), implicit: false }; }