@sema-agent/core
Version:
Stateless, task-oriented AI agent core
35 lines (34 loc) • 1.46 kB
JavaScript
import { turnContextOptions } from "../../internal/harness.js";
import { lastNoticeCarried } from "../request-copy-projection.js";
import { frameCarried } from "./compaction-restate.js";
import { CHANGED_FILES_MAX } from "./turn-attachments.js";
const SCAN_CEILING_MS = 2_000;
export function scanChangedFiles(input) {
const { cfg, detect, ledger, settlesFirst } = input;
if (!Boolean(cfg?.changedFiles) || frameCarried(ledger, "changed_files") || detect === undefined)
return undefined;
const maxFiles = typeof cfg?.changedFiles === "object" ? (cfg.changedFiles.maxFiles ?? CHANGED_FILES_MAX) : CHANGED_FILES_MAX;
return settlesFirst === undefined ? runScan(detect, maxFiles) : settlesFirst().then((later) => (later ? [] : runScan(detect, maxFiles)));
}
export function announceWhenCarried(prepared, announce) {
return async () => {
let carried = true;
try {
carried = lastNoticeCarried((await prepared.session.buildContext(turnContextOptions(prepared.harness.getModel()))).messages);
}
catch {
}
if (carried)
announce();
};
}
async function runScan(detect, maxFiles) {
let ceiling;
const scan = await Promise.race([
detect(maxFiles),
new Promise((resolve) => {
ceiling = setTimeout(() => resolve({ changed: [] }), SCAN_CEILING_MS);
}),
]).finally(() => clearTimeout(ceiling));
return scan.changed;
}