UNPKG

@adonis-agora/agent

Version:

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

194 lines • 9.13 kB
import type { PoppyAuthRequest, PoppyPrincipal } from './auth.js'; /** * 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 declare const POPPY_OPERATIONS_EXTENSION = "operations"; /** The version of the Operations extension conversations speak. */ export declare const POPPY_OPERATIONS_VERSION = "3"; /** One action the Company performs once it is approved (Operations §3). */ export interface PoppyOperation { operation_id: string; /** Identifies these terms; changes whenever the terms or `expires_at` change. */ revision: string; /** The terms in plain language, for the User. */ summary?: string; /** The terms as JSON: everything that affects the action. */ terms: Record<string, unknown>; /** The last time an approval of this revision is accepted (RFC 3339). */ expires_at: string; } /** How an operation was approved (Operations §5, §6): `user`, `agent`, or a later value. */ export type PoppyApprover = 'user' | 'agent' | (string & {}); /** An approval of one revision (Operations §6). Unknown members are ignored. */ export interface PoppyApproval { operation_id: string; revision: string; approver: PoppyApprover; /** Unix seconds. */ approved_at?: number; /** Only with `user`: how the User answered (`tap`, `typed`, `voice`, …). */ input_type?: string; /** The Personal Agent's own record of the approval. */ reference?: string; } /** Another way to do an action (spec §9.2). */ export interface PoppyAlternative { instructions?: string; references?: { type: string; url?: string; [field: string]: unknown; }[]; [field: string]: unknown; } /** A declined approval (Operations §6): nothing was performed. */ export interface PoppyDecline { operation_id: string; revision: string; /** Why, in short plain language for the User. */ reason?: string; alternative?: PoppyAlternative; } /** What a Company message or a Personal Agent message carries in `extensions.operations`. */ export type PoppyOperationsContent = { operation: PoppyOperation; } | { declined: PoppyDecline; } | { approval: PoppyApproval; }; /** * What the app (or this package) asks the operations service to require approval for. * `action` names the action — two requests with the same `action` are the same action, so a * changed `terms` gets the same `operation_id` with a new revision (Operations §3, "New terms"). */ export interface PoppyOperationInput { /** Which interface the request came through. Conversations send `'conversation'`. */ interface: 'api' | 'mcp' | 'conversation' | 'website' | (string & {}); /** Stable key of the action, chosen by the caller. */ action: string; /** The terms of the action (Operations §3). */ terms: Record<string, unknown>; /** The terms in plain language (basic Markdown allowed, no links or images). */ summary?: string; /** When an approval of this revision lapses — a future `Date`. Default: the service's. */ expiresAt?: Date; /** Continue this operation (a new revision of the same action). */ operationId?: string; /** The conversation the operation belongs to, for `interface: 'conversation'`. */ conversationId?: string; /** * The approval, when it did not come in a `Poppy-Approval` header — a conversation carries it in * the message (`extensions.operations.approval`). Omitted when the request carries none. */ approval?: PoppyApproval; } /** * What {@link PoppyOperationsService.require} answers. `ok: true` — the request carries a valid * approval of exactly these terms (checks 1–3 of Operations §6): perform the action, then call * `markPerformed`. `ok: false` — perform nothing and answer `status`, `body` and `headers`: `403` * `approval_required` with the operation, `403` `approval_declined`, `503` while it is being * performed, a challenge for an operation that belongs to an account, `400` `invalid_request`. */ export type PoppyOperationRequireResult = { ok: true; kind?: 'approved'; approval: PoppyApproval; operation?: PoppyOperation; } | { ok: false; /** * What happened: `approval_required`, `approval_declined`, `performed` (the first answer is * in `status`/`body`), `performing`, `challenge`, `extension_required`, `invalid_request`. * Optional: without it, `body.error` and `status` tell. */ kind?: string; status: number; body: Record<string, unknown> | null; headers?: Record<string, string>; /** The operation of `approval_required`, when the service gives it apart from `body`. */ operation?: PoppyOperation; /** The decline of `approval_declined`, when the service gives it apart from `body`. */ declined?: PoppyDecline; }; /** An operation as the service keeps it (what {@link PoppyOperationsService.get} answers). */ export interface PoppyStoredOperation { operationId: string; /** `pending`, `performing`, `performed`, … */ status: string; /** The current revision. */ operation: PoppyOperation; [field: string]: unknown; } /** * The operations service an authorization server installs in {@link POPPY_OPERATIONS_SLOT} * (`@adonis-agora/authkit-poppy` does): it stores operations, verifies approvals and keeps each * operation performed at most once. Conversations call it; without it, they never ask for * approval with operations. * * @experimental Implements the Operations extension, version 3. */ export interface PoppyOperationsService { /** The version of the Operations extension the service speaks (`'3'`). */ version?: string; /** * Require approval for an action. `request` is the HTTP request (an Adonis `HttpContext` or a * {@link PoppyAuthRequest}); its `Poppy-Approval` header, or `input.approval`, is the approval. */ require(request: PoppyAuthRequest | { request: unknown; }, principal: PoppyPrincipal, input: PoppyOperationInput): Promise<PoppyOperationRequireResult>; /** An operation the principal may see, or `null`. */ get(principal: PoppyPrincipal, operationId: string): Promise<PoppyStoredOperation | null>; /** The action was performed: later approvals of it get the first answer (Operations §7). */ markPerformed(operationId: string, response?: { status: number; body?: unknown; headers?: Record<string, string>; }): Promise<unknown>; /** * An approved action was not performed, and won't be under that approval (it expired before the * Company Agent made the call, or the conversation closed): the operation stops being * `performing` (authkit puts it back to `pending`, so the next ask is a new revision of it). * Optional — a service without it keeps its own state. */ release?(operationId: string): Promise<unknown>; } /** * The contract with the authorization server is this global symbol, not a module (like * `Symbol.for('@adonis-agora/poppy:authenticate')`). */ export declare const POPPY_OPERATIONS_SLOT: unique symbol; /** The operations service installed in the global slot, if any — read per use, so install order is free. */ export declare function globalPoppyOperations(): PoppyOperationsService | undefined; /** * 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 declare function parsePoppyApproval(raw: unknown): PoppyApproval | null; /** * 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 declare function formatPoppyApproval(approval: PoppyApproval): string; /** The operation of an `approval_required` answer, when it is one. */ export declare function operationOf(result: { body: Record<string, unknown> | null; operation?: unknown; }): PoppyOperation | null; /** The decline of an `approval_declined` answer — its members without `error` (Operations §6). */ export declare function declineOf(result: { body: Record<string, unknown> | null; declined?: unknown; }, approval: PoppyApproval): PoppyDecline; //# sourceMappingURL=operations.d.ts.map