UNPKG

agentfootprint

Version:

The explainable agent framework — backtrack a wrong answer to the exact context that caused it (evidence, not guesses). Built on footprintjs.

98 lines • 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.projectArtifactDetail = void 0; const payload_js_1 = require("./payload.js"); const detailRequest_js_1 = require("./detailRequest.js"); const detailError_js_1 = require("./detailError.js"); const object = (value) => value !== null && typeof value === 'object' && !Array.isArray(value); const container = (value) => object(value) || Array.isArray(value); // The payload owner counts bare strings as text; a one-element JSON array also counts quotes. const jsonBytes = (value) => (0, payload_js_1.measureArtifactBytes)([value]) - 2; const kind = (value) => value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value; function select(root, path) { let value = root; for (const key of (0, detailRequest_js_1.detailTokens)(path)) { if (Array.isArray(value) && !/^(0|[1-9]\d*)$/.test(key)) return (0, detailError_js_1.invalidDetail)(); if (!container(value) || !Object.hasOwn(value, key)) return (0, detailError_js_1.unavailableDetail)(); value = Array.isArray(value) ? value[Number(key)] : value[key]; } return value; } function childPath(parent, key) { return `${parent}/${key.replace(/~/g, '~0').replace(/\//g, '~1')}`; } function bounded(value) { if (jsonBytes(value) > detailRequest_js_1.DETAIL_LIMITS.outputBytes) return (0, detailError_js_1.unavailableDetail)(); return value; } /** Navigate only detached ordinary JSON; this function reads no store and authorizes no claim. */ function projectArtifactDetail(root, args, tool, context) { const value = select(root, args.path); const base = { ref: args.ref, path: args.path, kind: kind(value), ...(context === undefined ? {} : { context }), }; const keys = container(value) ? Object.keys(value) : []; const inline = { ...base, mode: 'value', value, complete: true }; if (args.offset === 0 && keys.length <= args.limit && jsonBytes(value) <= detailRequest_js_1.DETAIL_LIMITS.valueBytes && jsonBytes(inline) <= detailRequest_js_1.DETAIL_LIMITS.outputBytes) return inline; if (!container(value)) { if (args.offset !== 0) return (0, detailError_js_1.invalidDetail)(); return bounded({ ...base, mode: 'scalar-omitted', omitted: true, scalarBytes: jsonBytes(value), complete: false, reason: 'Selected scalar exceeds the bounded view; no partial value was returned.', }); } if (args.offset > keys.length) return (0, detailError_js_1.invalidDetail)(); const children = []; const response = () => ({ ...base, mode: 'children', children, offset: args.offset, shown: children.length, total: keys.length, complete: args.offset + children.length === keys.length, ...(args.offset + children.length < keys.length ? { nextOffset: args.offset + children.length } : {}), }); for (const key of keys.slice(args.offset, args.offset + args.limit)) { const path = childPath(args.path, key); (0, detailRequest_js_1.detailTokens)(path); const child = Array.isArray(value) ? value[Number(key)] : value[key]; children.push({ path, type: kind(child), ...(container(child) ? { count: Object.keys(child).length } : {}), action: { tool, args: { ref: args.ref, path } }, }); if (jsonBytes(response()) > detailRequest_js_1.DETAIL_LIMITS.outputBytes - detailRequest_js_1.DETAIL_LIMITS.reserveBytes) { children.pop(); break; } } if (!children.length && args.offset < keys.length) return (0, detailError_js_1.unavailableDetail)(); return bounded(response()); } exports.projectArtifactDetail = projectArtifactDetail; //# sourceMappingURL=detailView.js.map