@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
277 lines • 13.5 kB
TypeScript
/**
* The Operations extension, version 3 (Personal Agent side): a Company refuses an action that needs
* approval and returns the operation, the terms are approved by the User (or by this agent on an
* instruction the User gave), and the same request goes again with the approval. Each interface
* carries it its own way: APIs and the website's operations `endpoint` in the `Poppy-Approval`
* header, MCP in the tool call's `_meta`, conversations in `extensions.operations`.
*
* Guarantees kept here: an approval covers one revision; a declined approval is never sent again; a
* request carrying an approval that failed is sent again with the SAME approval, and no new operation
* for the action is approved until the outcome is known; an agent approval that met new terms or a
* decline never approves the next revision on the same instruction (the User is asked).
*/
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { type CompanyClient } from './company.js';
import type { ConversationClient } from './conversations.js';
import type { OperationRecord } from './store.js';
import type { ConversationEvent, EventMessage, OperationApproval, OperationDecline, PoppyOperation } from './types.js';
export declare const OPERATIONS_EXTENSION = "operations";
export declare const OPERATIONS_VERSION = "3";
/** The tool call `_meta` key an MCP approval goes under (Operations section 6). */
export declare const APPROVAL_META_KEY = "personalagentprotocol.org/approval";
export type OperationInterface = 'api' | 'mcp' | 'conversation' | 'website';
/** What the agent asked for when an operation came back, to check its terms against (section 5). */
export interface AskedFor {
/** APIs and websites: the request the action came from. */
method?: string | undefined;
url?: string | undefined;
body?: unknown;
/** MCP. */
tool?: string | undefined;
arguments?: Record<string, unknown> | undefined;
/** Conversations. */
conversationId?: string | undefined;
message?: string | undefined;
[k: string]: unknown;
}
/** What an approver is asked to decide. */
export interface ApprovalRequest {
/** Show the User the Company's name with the terms (section 5). */
company: {
name: string;
domain: string;
};
operation: PoppyOperation;
/** `summary` with link, image and HTML markup shown as text (section 3). */
summaryText?: string | undefined;
via: OperationInterface;
asked: AskedFor;
/**
* Only the User can approve this one: an earlier agent approval met new terms, or the Company
* declined an approval for this action (sections 5 and 6). An agent decision is then refused.
*/
userOnly: boolean;
why?: 'new_terms_after_agent_approval' | 'after_decline' | 'new_operation_after_agent_approval';
}
/**
* An approval decision. `user`: the User approved through this agent's own interface (never text
* from the Company). `agent`: an instruction the User gave covers these terms.
*/
export type ApprovalDecision = {
approver: 'user';
inputType?: string | undefined;
approvedAt?: number | undefined;
} | {
approver: 'agent';
approvedAt?: number | undefined;
};
/** Decides an operation. `null` or `undefined`: not approved, and the action isn't performed. */
export type OperationApprover = (request: ApprovalRequest) => Promise<ApprovalDecision | null | undefined> | ApprovalDecision | null | undefined;
/** How an operation ended for this agent. The action is done only with `performed`. */
export type OperationOutcome =
/** The Company performed the action and answered as it would without operations. */
{
status: 'performed';
operation: PoppyOperation;
approval: OperationApproval;
}
/** Nobody approved it: tell the User the action wasn't performed. */
| {
status: 'not_approved';
operation: PoppyOperation;
}
/** Its terms expired before an approval was sent. */
| {
status: 'expired';
operation: PoppyOperation;
}
/** The Company declined the approval. Nothing was performed; never send it again. */
| {
status: 'declined';
operation: PoppyOperation;
approval: OperationApproval;
decline: OperationDecline;
}
/**
* A request carrying the approval failed, and the answer to its retry doesn't say whether it took
* effect. Find out another way (the order in the Company's API) before approving anything else.
*/
| {
status: 'unknown';
operation: PoppyOperation;
approval: OperationApproval;
}
/** The request with the approval was refused for another reason (sign-in, Session): not performed. */
| {
status: 'refused';
operation: PoppyOperation;
approval: OperationApproval;
}
/** Conversations: the approval was sent; the Company Agent says how it went in a later message. */
| {
status: 'sent';
operation: PoppyOperation;
approval: OperationApproval;
}
/** Websites: the approval is recorded; have the browser make the action's request again. */
| {
status: 'recorded';
operation: PoppyOperation;
approval: OperationApproval;
ref: string;
};
export interface OperationOptions {
/** Decides this call's operations instead of the agent's `operations.approve`. */
approve?: OperationApprover | undefined;
/** An earlier approval for this action was an agent approval: the User must be asked. */
userOnly?: boolean | undefined;
/**
* The operation this action already returned, waiting for approval (an approve card): the request
* is made again WITH its approval (section 6), never first without one — the Company may answer
* a request without an approval with a new operation.
*/
operation?: PoppyOperation | undefined;
}
/** A refusal that carries an operation (section 4) or declines an approval (section 6). */
export type OperationRefusal = {
kind: 'required';
operation: PoppyOperation;
} | {
kind: 'declined';
decline: OperationDecline;
};
/** An operation as section 3 defines it, or `undefined` when it isn't one. */
export declare function parseOperation(raw: unknown): PoppyOperation | undefined;
/**
* A `summary` shown as text: it may use emphasis, paragraphs and lists, but links, images and HTML
* MUST NOT be rendered (section 3). Their markup is escaped so a Markdown renderer shows it as text.
*/
export declare function operationSummaryText(summary: string | undefined): string | undefined;
/** Whether this agent uses the Operations extension, version 3, with this Company (section 2). */
export declare function usesOperations(company: CompanyClient): boolean;
/** The approval as JSON, for MCP `_meta` and conversations (section 6). */
export declare function approvalJson(approval: OperationApproval): Record<string, unknown>;
/** `Poppy-Approval`: a Dictionary with Strings, and `approved_at` as an Integer (section 6). */
export declare function formatApprovalHeader(approval: OperationApproval): string;
/** `Poppy-Operation`: `sid` and `ref`, both Strings (section 4). `undefined` when malformed. */
export declare function parseOperationHeader(value: string | null | undefined): {
sid: string;
ref: string;
} | undefined;
/** This agent's record of an operation, if it met it. */
export declare function operationRecord(company: CompanyClient, operationId: string): OperationRecord | undefined;
/**
* Asks the approver about one revision (section 5) and builds the approval: `approved_at`, an
* `input_type` only with `user`, and a fresh random `reference`, so no two Companies ever get the
* same one. `undefined` with an outcome when nothing is approved.
*/
export declare function decide(company: CompanyClient, via: OperationInterface, operation: PoppyOperation, asked: AskedFor, opts?: OperationOptions): Promise<{
approval: OperationApproval;
} | {
outcome: OperationOutcome;
}>;
/** The JSON refusal of an API or the operations `endpoint` (sections 4 and 6), if it is one. */
export declare function operationRefusal(res: Response, company?: CompanyClient): Promise<OperationRefusal | undefined>;
/** The refusal in an MCP tool result (sections 4 and 6), if it is one. */
export declare function toolRefusal(result: unknown, company?: CompanyClient): OperationRefusal | undefined;
/**
* Sends an API request (or a request to the website's operations `endpoint`) and handles any
* operation it returns: approval, the same request again with `Poppy-Approval`, declines, new
* revisions and retries (sections 4–7). `send` makes the request, with the approval header when
* given one, and MUST send the same method, URL and content each time.
*/
export declare function sendWithOperations(company: CompanyClient, via: 'api' | 'website', asked: AskedFor, send: (approvalHeader: string | undefined) => Promise<Response>, opts?: OperationOptions): Promise<{
response: Response;
outcome?: OperationOutcome | undefined;
}>;
type ToolParams = {
name: string;
arguments?: Record<string, unknown> | undefined;
};
/**
* Calls an MCP tool and handles any operation it returns (sections 4–7): the same tool with the same
* arguments again, the approval in `_meta` under `personalagentprotocol.org/approval`.
*/
export declare function callToolWithOperations(company: CompanyClient, client: Client, params: ToolParams, opts?: OperationOptions): Promise<{
result: Awaited<ReturnType<Client['callTool']>>;
outcome?: OperationOutcome;
}>;
/** The operation a Company message asks approval for (`extensions.operations.operation`). */
export declare function operationOf(message: EventMessage | undefined): PoppyOperation | undefined;
/** A decline in a Company message (`extensions.operations.declined`). */
export declare function declineOf(message: EventMessage | undefined, company?: CompanyClient): OperationDecline | undefined;
/**
* Notes what a conversation event says about operations: a Company message's operation is waiting
* for approval, a decline means its approval must never be sent again. Returns what it found.
*/
export declare function noteOperationEvent(company: CompanyClient, event: ConversationEvent): {
operation?: PoppyOperation;
decline?: OperationDecline;
};
/**
* Approves an operation a Company message returned in a conversation (section 6): decides it, then
* sends a message with the approval in `extensions.operations.approval`. Its text approves nothing;
* the Company Agent says how it went in a later message (watch for `declineOf`).
*/
export declare function approveInConversation(conversation: ConversationClient, conversationId: string, operation: PoppyOperation, opts?: OperationOptions & {
text?: string | undefined;
asked?: AskedFor | undefined;
}): Promise<OperationOutcome>;
/**
* The website's operations `endpoint` (section 2), when the Company lists one for version 3. It
* comes with `web.browser_session_endpoint`: only a joined browser gets operations.
*/
export declare function operationsEndpoint(company: CompanyClient): string | undefined;
/**
* The reference in a `Poppy-Operation` header of a response THIS agent's own browser received,
* only when its `sid` names a Session the agent joined that browser to (section 4). Anything else,
* including references read from a page or a message, is ignored.
*/
export declare function webOperationReference(company: CompanyClient, headers: Headers | Record<string, string | string[] | undefined>): {
sid: string;
ref: string;
} | undefined;
/** What the operations `endpoint` says about a reference (sections 4 and 6). */
export type WebOperationState = {
kind: 'operation';
operation: PoppyOperation;
} | {
kind: 'status';
operationId: string;
status: 'approved' | 'performing' | 'performed' | string;
}
/** The reference can't be used: have the browser make the request again for a new one. */
| {
kind: 'invalid';
};
/** Gets the operation behind a website reference, or its state once approved (sections 4 and 6). */
export declare function lookupWebOperation(company: CompanyClient, ref: string): Promise<WebOperationState>;
/**
* Approves the operation behind a website reference at the operations `endpoint` (section 6). On
* `recorded`, have the browser make the action's request again (its Continue button, the same
* form); `afterBrowserRepeat` then checks the answer.
*/
export declare function approveWebOperation(company: CompanyClient, ref: string, asked: AskedFor, opts?: OperationOptions): Promise<OperationOutcome | {
status: 'invalid';
} | {
status: 'approved_already';
state: string;
}>;
/**
* After the browser made the action's request again (section 7): with no new `Poppy-Operation`, the
* action went through. A new reference means the approval was used up or no longer matches: the
* earlier one is looked up first — still `performing` is waited for, `performed` is never approved
* again — and only then is the new one decided, by the User when an agent approved the earlier one.
*/
export declare function afterBrowserRepeat(company: CompanyClient, previousRef: string, headers: Headers | Record<string, string | string[] | undefined>, asked: AskedFor, opts?: OperationOptions & {
pollMs?: number | undefined;
maxPolls?: number | undefined;
}): Promise<{
status: 'done';
} | {
status: 'performed';
} | {
status: 'performing';
} | Awaited<ReturnType<typeof approveWebOperation>>>;
export {};
//# sourceMappingURL=operations.d.ts.map