@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
45 lines • 1.96 kB
JavaScript
import { GOAL_STATUS_VALUES } from "./types.js";
export const MAX_OBJECTIVE_LENGTH = 4_000;
const WHITESPACE_LOOKBACK = 200;
export function validateObjective(value, fullTextFileName) {
const objective = value.trim();
if (objective.length === 0)
throw new Error("objective must not be empty");
const codePoints = [...objective];
if (codePoints.length <= MAX_OBJECTIVE_LENGTH)
return { objective, truncated: false };
const marker = truncationMarker(fullTextFileName);
const payloadBudget = MAX_OBJECTIVE_LENGTH - [...marker].length;
const payload = codePoints.slice(0, nearestWhitespaceCut(codePoints, payloadBudget) ?? payloadBudget).join("");
return { objective: `${payload}${marker}`, truncated: true, fullTextFileName };
}
export function truncationMarker(fullTextFileName) {
return `… [truncated; full objective: ${fullTextFileName}]`;
}
export function objectiveTruncationNotice(fullTextFileName) {
return `Objective was truncated; full objective saved to ${fullTextFileName}.`;
}
export function isGoalStatus(value) {
return GOAL_STATUS_VALUES.some((status) => status === value);
}
export function isNonNegativeSafeInteger(value) {
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
}
export function resolveTokenBudget(current, update) {
if (update === undefined)
return current;
return update === null ? undefined : validateTokenBudget(update);
}
export function validateTokenBudget(value) {
if (!isNonNegativeSafeInteger(value))
throw new Error("token budget must be a non-negative integer");
return value;
}
function nearestWhitespaceCut(codePoints, payloadBudget) {
for (let index = payloadBudget - 1; index >= Math.max(0, payloadBudget - WHITESPACE_LOOKBACK); index -= 1) {
if (/\s/u.test(codePoints[index] ?? ""))
return index;
}
return undefined;
}
//# sourceMappingURL=validation.js.map