@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
169 lines • 6.87 kB
JavaScript
/** Every dev-tool result is capped at this many bytes of text. */
export const DEV_TOOLS_MAX_BYTES = 48 * 1024;
/** Rows a list tool returns at most. */
export const DEV_TOOLS_MAX_ROWS = 100;
/** A single blob field (a run's input, a message) is cut to this many bytes. */
export const DEV_TOOLS_FIELD_BYTES = 4 * 1024;
const SENSITIVE_KEY = /(secret|token|password|authorization|cookie|api[_-]?key)/i;
/** Token COUNTS (`inputTokens`, `total_tokens`, `tokenCount`), not token values. */
const TOKEN_COUNT_KEY = /(tokens|token_?count)$/i;
const NUMERIC = /^-?\d+(\.\d+)?$/;
const REDACTED = '[redacted]';
/**
* The final redaction pass over everything a dev tool returns (and over the arguments the audit line
* logs): a key that names a secret (`secret`, `token`, `password`, `authorization`, `cookie`,
* `api_key`) loses its value, whatever its type. Kept: `null`, booleans (`hasPassword: true`) and
* the numbers of token-count keys (`inputTokens`, `tokenCount`).
*
* Only a real cycle (an object inside itself) becomes `'[circular]'`: the same object reached twice
* from different places (a flag's `stored` and `definition`) is written out both times.
*/
export function redactDevToolOutput(value, ancestors = new WeakSet()) {
if (value instanceof Date)
return value.toISOString();
if (typeof value === 'bigint')
return value.toString();
if (typeof value !== 'object' || value === null)
return value;
if (ancestors.has(value))
return '[circular]';
ancestors.add(value);
try {
if (Array.isArray(value))
return value.map((item) => redactDevToolOutput(item, ancestors));
const source = typeof value.toJSON === 'function'
? value.toJSON()
: value;
if (source !== value)
return redactDevToolOutput(source, ancestors);
const out = {};
for (const [key, child] of Object.entries(value)) {
out[key] =
SENSITIVE_KEY.test(key) && !harmless(key, child)
? REDACTED
: redactDevToolOutput(child, ancestors);
}
return out;
}
finally {
ancestors.delete(value);
}
}
function harmless(key, value) {
if (value === null || value === undefined || typeof value === 'boolean')
return true;
const numeric = typeof value === 'number' || (typeof value === 'string' && NUMERIC.test(value));
return numeric && TOKEN_COUNT_KEY.test(key);
}
const byteLength = (text) => Buffer.byteLength(text, 'utf8');
/**
* Cut a string to `maxBytes`, ending it with `…(truncated, N bytes)`, N being the original size.
* Under the limit it is returned as is.
*/
export function truncateText(text, maxBytes = DEV_TOOLS_FIELD_BYTES) {
const size = byteLength(text);
if (size <= maxBytes)
return text;
const suffix = `…(truncated, ${size} bytes)`;
const keep = Math.max(0, maxBytes - byteLength(suffix));
let cut = Buffer.from(text, 'utf8').subarray(0, keep).toString('utf8');
// A cut through a multi-byte character leaves a replacement char: drop it.
if (cut.endsWith('�'))
cut = cut.slice(0, -1);
return `${cut}${suffix}`;
}
/**
* Serialize a tool result as the text the MCP client receives, capped at `maxBytes`, and always valid
* JSON. Over the cap, a list (`{ items }` or a bare array) loses rows from the end and gains
* `truncated: true` and `next`; any other object has its largest arrays and strings cut (strings end
* with `…(truncated, N bytes)`) and gains `truncated: true`.
*/
export function capDevToolResult(value, maxBytes = DEV_TOOLS_MAX_BYTES) {
if (typeof value === 'string')
return truncateText(value, maxBytes);
const text = JSON.stringify(value ?? null);
if (byteLength(text) <= maxBytes)
return text;
const list = Array.isArray(value)
? { rest: {}, items: value }
: typeof value === 'object' &&
value !== null &&
Array.isArray(value.items)
? {
rest: value,
items: value.items,
}
: null;
if (list === null)
return shrinkToFit(JSON.parse(text), maxBytes);
const shaped = (count) => JSON.stringify({
...list.rest,
items: list.items.slice(0, count),
truncated: true,
next: list.rest.next ?? null,
});
// Largest prefix that fits (binary search on the row count).
let low = 0;
let high = list.items.length;
while (low < high) {
const middle = Math.ceil((low + high) / 2);
if (byteLength(shaped(middle)) <= maxBytes)
low = middle;
else
high = middle - 1;
}
const fitted = shaped(low);
return byteLength(fitted) <= maxBytes ? fitted : shrinkToFit(JSON.parse(fitted), maxBytes);
}
/**
* Cut the biggest array or string of a JSON value, again and again, until it serializes under
* `maxBytes`. An array keeps its first half, a string its first half (marked as truncated).
*/
function shrinkToFit(value, maxBytes) {
let root = typeof value === 'object' && value !== null && !Array.isArray(value)
? { ...value, truncated: true }
: value;
for (let round = 0; round < 200; round++) {
const text = JSON.stringify(root);
if (byteLength(text) <= maxBytes)
return text;
const target = biggestNode(root);
if (target === null)
break;
const { parent, key, node } = target;
const cut = Array.isArray(node)
? node.slice(0, Math.floor(node.length / 2))
: truncateText(node, Math.max(64, Math.floor(byteLength(node) / 2)));
if (parent === null)
root = cut;
else
parent[key] = cut;
}
return JSON.stringify({ truncated: true, error: `the result is over ${maxBytes} bytes` });
}
/** The largest array (with rows) or long string in `root`, by serialized size. */
function biggestNode(root) {
let best = null;
let bestSize = 0;
const visit = (parent, key, node) => {
const shrinkable = (Array.isArray(node) && node.length > 0) || (typeof node === 'string' && node.length > 128);
if (shrinkable) {
const size = byteLength(JSON.stringify(node));
if (size > bestSize) {
best = { parent, key, node };
bestSize = size;
}
}
if (Array.isArray(node)) {
for (const [index, child] of node.entries())
visit(node, index, child);
}
else if (typeof node === 'object' && node !== null) {
for (const [childKey, child] of Object.entries(node))
visit(node, childKey, child);
}
};
visit(null, '', root);
return best;
}
//# sourceMappingURL=limits.js.map