UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

120 lines • 5.35 kB
/** * The Operations extension (version 3) as conversations use it: the operation a Company message * asks approval for, the approval a Personal Agent message carries, and the decline — all in * `extensions.operations` of a message (spec §8.4). Storage and verification of operations live * in the authorization server, reached through the global slot {@link POPPY_OPERATIONS_SLOT}. * * @experimental Implements the Operations extension, version 3 * (https://personalagentprotocol.org/docs/1.0/extensions/operations). * The surface may still change between minor releases of this package while it settles. */ /** The extension's name in `poppy.json` and `Poppy-Extensions`. */ export const POPPY_OPERATIONS_EXTENSION = 'operations'; /** The version of the Operations extension conversations speak. */ export const POPPY_OPERATIONS_VERSION = '3'; /** * The contract with the authorization server is this global symbol, not a module (like * `Symbol.for('@adonis-agora/poppy:authenticate')`). */ export const POPPY_OPERATIONS_SLOT = Symbol.for('@adonis-agora/poppy:operations'); /** The operations service installed in the global slot, if any — read per use, so install order is free. */ export function globalPoppyOperations() { const value = globalThis[POPPY_OPERATIONS_SLOT]; if (typeof value !== 'object' || value === null) return undefined; const service = value; return typeof service.require === 'function' && typeof service.get === 'function' && typeof service.markPerformed === 'function' ? service : undefined; } const ID_PATTERN = /^[A-Za-z0-9_-]{1,256}$/; function isRecord(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); } /** * Read an approval as a Personal Agent message carries it (`extensions.operations.approval`). * `null` when it is not one — a missing or malformed `operation_id`, `revision` or `approver`. * Optional members of the wrong type are dropped, never refused (Operations §6); `input_type` is * kept only with `user`. */ export function parsePoppyApproval(raw) { if (!isRecord(raw)) return null; const { operation_id, revision, approver } = raw; if (typeof operation_id !== 'string' || !ID_PATTERN.test(operation_id)) return null; if (typeof revision !== 'string' || !ID_PATTERN.test(revision)) return null; if (typeof approver !== 'string' || approver === '') return null; const approvedAt = raw.approved_at; return { operation_id, revision, approver, ...(typeof approvedAt === 'number' && Number.isInteger(approvedAt) ? { approved_at: approvedAt } : {}), ...(approver === 'user' && typeof raw.input_type === 'string' && raw.input_type !== '' ? { input_type: raw.input_type } : {}), ...(typeof raw.reference === 'string' && ID_PATTERN.test(raw.reference) ? { reference: raw.reference } : {}), }; } /** An RFC 9651 String. */ function sfString(value) { return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; } /** * An approval as a `Poppy-Approval` header (a Dictionary Structured Field, Operations §6) — what an * operations service that reads only the header receives for a conversation's approval. */ export function formatPoppyApproval(approval) { const members = [ `operation_id=${sfString(approval.operation_id)}`, `revision=${sfString(approval.revision)}`, `approver=${sfString(approval.approver)}`, ]; if (approval.approved_at !== undefined) members.push(`approved_at=${approval.approved_at}`); if (approval.input_type !== undefined) { members.push(`input_type=${sfString(approval.input_type)}`); } if (approval.reference !== undefined) members.push(`reference=${sfString(approval.reference)}`); return members.join(', '); } /** The operation of an `approval_required` answer, when it is one. */ export function operationOf(result) { const operation = result.operation ?? result.body?.operation; if (!isRecord(operation)) return null; if (typeof operation.operation_id !== 'string' || typeof operation.revision !== 'string' || !isRecord(operation.terms) || typeof operation.expires_at !== 'string') { return null; } return { operation_id: operation.operation_id, revision: operation.revision, ...(typeof operation.summary === 'string' ? { summary: operation.summary } : {}), terms: operation.terms, expires_at: operation.expires_at, }; } /** The decline of an `approval_declined` answer — its members without `error` (Operations §6). */ export function declineOf(result, approval) { const body = (isRecord(result.declined) ? result.declined : result.body) ?? {}; return { operation_id: typeof body.operation_id === 'string' ? body.operation_id : approval.operation_id, revision: typeof body.revision === 'string' ? body.revision : approval.revision, ...(typeof body.reason === 'string' && body.reason !== '' ? { reason: body.reason } : {}), ...(isRecord(body.alternative) ? { alternative: body.alternative } : {}), }; } //# sourceMappingURL=operations.js.map