@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
72 lines • 2.54 kB
JavaScript
export function encodeKeyset(at, id) {
return Buffer.from(JSON.stringify(['k', at, id]), 'utf8').toString('base64url');
}
/** The position a keyset cursor names, or `null` when `cursor` is not one this module wrote. */
export function decodeKeyset(cursor) {
try {
const value = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
if (Array.isArray(value) &&
value.length === 3 &&
value[0] === 'k' &&
typeof value[1] === 'number' &&
Number.isFinite(value[1]) &&
typeof value[2] === 'string') {
return { at: value[1], id: value[2] };
}
}
catch {
// not ours
}
return null;
}
/**
* A `listRuns` `after`: a keyset position, or the offset an older server handed out (`"50"`).
* Anything else reads as the first page, as before.
*/
export function readRunsCursor(after) {
if (after === undefined)
return { offset: 0 };
const position = decodeKeyset(after);
if (position !== null)
return { position };
if (/^\d+$/.test(after))
return { offset: Number.parseInt(after, 10) };
return { offset: 0 };
}
/** `(at, id) < (position)`: is `row` strictly older than `position` in `(at DESC, id DESC)`? */
export function isBefore(row, position) {
return row.at < position.at || (row.at === position.at && row.id < position.id);
}
/** `(at DESC, id DESC)` comparator. */
export function newestFirst(left, right) {
if (left.at !== right.at)
return right.at - left.at;
return left.id < right.id ? 1 : left.id > right.id ? -1 : 0;
}
/**
* The seek predicate for `(column, "id") < (?, ?)`, spelled so every dialect (Postgres, MySQL,
* SQLite) can still range-scan an index on `(column[, id])`.
*/
export function seekSql(column, position) {
return {
sql: `(${column} <= ? AND (${column} < ? OR "id" < ?))`,
bindings: [position.at, position.at, position.id],
};
}
/**
* `limit + 1` rows → the page, with the next cursor at its last row. Forward-only, like every read
* of this SPI: `prevCursor` stays `null`.
*/
export function keysetPage(rows, limit) {
const hasNext = rows.length > limit;
const page = rows.slice(0, limit);
const last = page.at(-1);
return {
items: page.map((row) => row.view),
nextCursor: hasNext && last !== undefined ? encodeKeyset(last.at, last.id) : null,
prevCursor: null,
hasNext,
hasPrev: false,
};
}
//# sourceMappingURL=keyset.js.map