@sema-agent/core
Version:
Stateless, task-oriented AI agent core
227 lines (226 loc) • 6.98 kB
JavaScript
export function fsGlobClass(source) {
let re;
try {
re = new RegExp(source);
}
catch {
return undefined;
}
return { matches: (unit) => re.test(unit) };
}
const OP_TEXT = 0;
const OP_ONE = 1;
const OP_CLASS = 2;
const OP_ANY = 3;
const OP_SPLIT = 4;
const OP_JUMP = 5;
const OP_END = 6;
const SLASH = 47;
class LiveSet {
dense;
sparse;
count = 0;
constructor(capacity) {
this.dense = new Int32Array(capacity);
this.sparse = new Int32Array(capacity);
}
has(pc) {
const at = this.sparse[pc];
return at < this.count && this.dense[at] === pc;
}
add(pc) {
this.dense[this.count] = pc;
this.sparse[pc] = this.count++;
}
}
const MAX_RETAINED_SIZE = 1 << 16;
let retained;
let retainedCapacity = 0;
const pending = [];
let visitsTotal = 0;
let scratchBytesTotal = 0;
function scratchFor(size) {
const made = (capacity) => {
scratchBytesTotal += 16 * capacity;
return [new LiveSet(capacity), new LiveSet(capacity)];
};
if (size > MAX_RETAINED_SIZE)
return made(size);
if (retained === undefined || size > retainedCapacity) {
retainedCapacity = Math.max(size, retainedCapacity * 2, 1 << 8);
retained = made(retainedCapacity);
}
retained[0].count = 0;
retained[1].count = 0;
return retained;
}
export function fsGlobWorkTotals() {
return { visits: visitsTotal, scratchBytes: scratchBytesTotal };
}
const isStarRun = (token) => token.kind === "star" || token.kind === "globstar";
export function compileFsGlob(tokens, anchor) {
const op = [];
const arg = [];
const arg2 = [];
const classes = [];
const emit = (o, a = 0, b = 0) => {
op.push(o);
arg.push(a);
arg2.push(b);
return op.length - 1;
};
const emitLoop = (unit) => {
const loop = emit(OP_SPLIT);
emit(unit);
emit(OP_JUMP, loop);
arg[loop] = loop + 1;
arg2[loop] = op.length;
};
const emitUnit = (unit) => {
if (unit.kind === "text")
for (let i = 0; i < unit.text.length; i++)
emit(OP_TEXT, unit.text.charCodeAt(i));
else if (unit.kind === "one")
emit(OP_ONE);
else if (unit.kind === "class")
emit(OP_CLASS, classes.push(unit.class) - 1);
else
emitLoop(OP_ONE);
};
const merged = [];
for (const token of tokens) {
const previous = merged[merged.length - 1];
if (previous !== undefined && isStarRun(previous) && isStarRun(token)) {
if (token.kind === "globstar")
merged[merged.length - 1] = token;
}
else
merged.push(token);
}
for (const token of merged) {
if (token.kind === "globstar")
emitLoop(OP_ANY);
else if (token.kind === "alt") {
const exits = [];
token.alternatives.forEach((alternative, k) => {
const last = k === token.alternatives.length - 1;
const choice = last ? -1 : emit(OP_SPLIT);
if (!last)
arg[choice] = choice + 1;
for (const unit of alternative)
emitUnit(unit);
if (!last) {
exits.push(emit(OP_JUMP));
arg2[choice] = op.length;
}
});
for (const exit of exits)
arg[exit] = op.length;
}
else
emitUnit(token);
}
emit(OP_END);
let required = "";
let run = "";
for (const token of merged) {
run = token.kind === "text" ? run + token.text : "";
if (run.length > required.length)
required = run;
}
return new Program(Int32Array.from(op), Int32Array.from(arg), Int32Array.from(arg2), classes, anchor === "segment", required, run);
}
class Program {
op;
arg;
arg2;
classes;
segmentAnchored;
required;
tail;
constructor(op, arg, arg2, classes, segmentAnchored, required, tail) {
this.op = op;
this.arg = arg;
this.arg2 = arg2;
this.classes = classes;
this.segmentAnchored = segmentAnchored;
this.required = required;
this.tail = tail;
this.first = op[0] === OP_TEXT ? arg[0] : -1;
this.beginsAfter = this.first >= 0 ? `/${String.fromCharCode(this.first)}` : "/";
}
first;
beginsAfter;
get size() {
return this.op.length;
}
follow(set, from) {
const { op, arg, arg2 } = this;
let added = 0;
let pc = from;
for (;;) {
if (!set.has(pc)) {
set.add(pc);
added++;
const o = op[pc];
if (o === OP_SPLIT) {
pending.push(arg2[pc]);
pc = arg[pc];
continue;
}
if (o === OP_JUMP) {
pc = arg[pc];
continue;
}
}
if (pending.length === 0)
return added;
pc = pending.pop();
}
}
test(value) {
if (this.tail !== "" && !value.endsWith(this.tail))
return false;
if (this.required !== "" && !value.includes(this.required))
return false;
const { op, arg, classes } = this;
const length = value.length;
const [first, second] = scratchFor(op.length);
let live = first;
let ahead = second;
let entered = 0;
let found = false;
for (let pos = 0;;) {
if ((pos === 0 || (this.segmentAnchored && value.charCodeAt(pos - 1) === SLASH)) && (this.first < 0 || value.charCodeAt(pos) === this.first))
entered += this.follow(live, 0);
if (pos === length) {
found = live.has(op.length - 1);
break;
}
if (live.count === 0) {
if (!this.segmentAnchored)
break;
const slash = value.indexOf(this.beginsAfter, pos);
if (slash < 0)
break;
pos = slash + 1;
continue;
}
const unit = value.charCodeAt(pos);
ahead.count = 0;
for (let i = 0; i < live.count; i++) {
const pc = live.dense[i];
const o = op[pc];
const takes = o === OP_TEXT ? arg[pc] === unit : o === OP_ONE ? unit !== SLASH : o === OP_ANY ? true : o === OP_CLASS ? classes[arg[pc]].matches(value[pos]) : false;
if (takes)
entered += this.follow(ahead, pc + 1);
}
const spent = live;
live = ahead;
ahead = spent;
pos++;
}
visitsTotal += entered;
return found;
}
}