@sema-agent/core
Version:
Stateless, task-oriented AI agent core
57 lines (56 loc) • 2.71 kB
JavaScript
import { EntryCutter, PARENS_NOT_ONE_ZONE, SEQUENCE_NOT_A_NAME, notARule, ruleSpellingOf } from "@sema-agent/primitives";
import { readToolNameList } from "./tool-name-list.js";
const LIST_UNREADABLE = "its value is a list whose entries cannot be read (an accessor or a proxy trap threw), not a list of names";
const notAList = (type) => `its value is ${type}, not a list of names`;
const DENY_ALL = "`*` in a denied list denies every tool (a reader that collapses the list to `*` would deny nothing)";
export function parseToolListField(value, side) {
if (value === undefined)
return { entries: undefined, problems: [] };
const entries = [];
const problems = [];
const read = () => {
const star = side === "denied" ? entries.find((e) => e.name === "*") : undefined;
if (star !== undefined)
problems.push({ level: "info", code: "deny_all", position: star.position, spelling: star.spelling, why: DENY_ALL });
return { entries, problems };
};
if (value === null)
return read();
let position = 0;
const cutString = (text) => {
const cut = new EntryCutter();
for (const line of text.split("\n")) {
for (const ch of line)
cut.push(ch);
cut.endScalar();
}
cut.entries.forEach((spelling, index) => {
position++;
const rule = cut.brokenParens[index] === true ? undefined : ruleSpellingOf(spelling);
if (rule !== undefined) {
entries.push({ position, spelling, name: rule.toolName, ...(rule.ruleContent !== undefined ? { arg: rule.ruleContent } : {}) });
}
else if (cut.brokenParens[index] === true)
problems.push({ level: "error", code: "parens", position, spelling, why: PARENS_NOT_ONE_ZONE });
else
problems.push({ level: "error", code: "not_a_rule", position, spelling, why: notARule(position) });
});
return cut.entries.length;
};
if (typeof value === "string") {
cutString(value);
return read();
}
const list = readToolNameList(value);
if (list.kind === "not_a_list")
return { entries, problems: [{ level: "error", code: "not_a_list", why: notAList(list.type) }] };
if (list.kind === "unreadable")
return { entries, problems: [{ level: "error", code: "unreadable", why: LIST_UNREADABLE }] };
for (const item of list.entries) {
if (typeof item === "string" && cutString(item) > 0)
continue;
position++;
problems.push({ level: "error", code: "not_a_name", position, why: SEQUENCE_NOT_A_NAME });
}
return read();
}