UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

41 lines (40 loc) • 1.94 kB
import { bytesAreBinary } from "./file-history-store.js"; export const CHANGES_CONTENT_MAX_BYTES = 1_000_000; export const CHANGES_CONTENT_BUDGET_BYTES = 64_000_000; export const CHANGES_CONTENT_OF_MAX = 500; export const CHANGES_CHECKPOINTS_PAGE_MAX = 100; export const CHANGES_ROWS_MAX = 500; export const CHANGES_REASONS = Object.freeze(["binary", "too_large", "unreadable", "base_unavailable", "frozen_history", "recorded_outside_root", "rewind_refused", "mode_only", "untracked", "submodule", "repository_filter", "budget_spent"]); export const CHANGES_UNAVAILABLE_REASONS = Object.freeze(["store_unconfigured", "store_cannot_read", "history_unreadable", "files_env_unsupported", "no_checkpoint", "not_a_repository", "repository_busy", "git_failed"]); export function changesSideOf(bytes) { if (bytes === null) return { text: null, size: 0 }; if (bytes.byteLength > CHANGES_CONTENT_MAX_BYTES) return { omitted: "too_large" }; if (bytesAreBinary(bytes)) return { omitted: "binary" }; try { return { text: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes), size: bytes.byteLength }; } catch { return { omitted: "binary" }; } } export function changesContentBudget() { return { left: CHANGES_CONTENT_BUDGET_BYTES }; } export function changesContentOf(before, after, budget) { for (const reason of ["unreadable", "too_large", "binary"]) { if (("omitted" in before && before.omitted === reason) || ("omitted" in after && after.omitted === reason)) return { omitted: reason }; } if ("omitted" in before) return { omitted: before.omitted }; if ("omitted" in after) return { omitted: after.omitted }; const cost = before.size + after.size; if (cost > budget.left) return { omitted: "budget_spent" }; budget.left -= cost; return { before: before.text, after: after.text }; }