UNPKG

@adonis-agora/agent

Version:

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

122 lines • 6.26 kB
/** * The Browser Control extension, version 1 (Personal Agent side), for the browser this agent drives * on a Company's website (section 6). The Company marks steps it wants the User to take with * `data-poppy-user-step`; this agent leaves them to the User, offers the User control of the * browser, and tells the page who controls it through `navigator.poppy.controller`. * * It works with any driver that can run a script in every new document and evaluate one in the * current page, such as Playwright (`page.addInitScript`, `page.evaluate`) or the DevTools protocol * (`Page.addScriptToEvaluateOnNewDocument`, `Runtime.evaluate`). Marks are signals, not locks: the * Company's servers still decide (8.1). */ import type { CompanyClient } from './company.js'; import { PoppyError } from './errors.js'; export declare const BROWSER_CONTROL_EXTENSION = "browser-control"; export declare const BROWSER_CONTROL_VERSION = "1"; export declare const USER_STEP_ATTRIBUTE = "data-poppy-user-step"; /** A browser this agent controls (never the User's own browser). */ export interface ControlledBrowser { /** Runs `source` in every new document, before the page's own scripts. */ addInitScript(source: string): Promise<unknown>; /** Evaluates the expression `source` in the current page. */ evaluate(source: string): Promise<unknown>; } /** The kind of browser action the agent's model wants to take. */ export type BrowserAction = /** Change a control's value: type, select, check. */ 'fill' /** Click or otherwise activate an element. */ | 'activate' /** Submit a form, by any means (a submit button, Enter in a field, `form.submit()`). */ | 'submit'; /** What the page says about the element an action targets (computed by `STEP_PROBE_SOURCE`). */ export interface StepProbe { /** `data-poppy-user-step` on the element or an element around it other than a form. */ elementMark: string | null; /** `data-poppy-user-step` on the element's form owner (the form itself for a form). */ formMark: string | null; kind: 'field' | 'submit' | 'button' | 'form' | 'other'; } export type UserStepKind = 'interaction' | 'submission'; /** * In-page probe, as a function expression taking the target element. It finds the element an action * really reaches (a button around a span), the marks on it and around it, and its form owner, * including a control outside the form that names it in its `form` attribute (section 4). */ export declare const STEP_PROBE_SOURCE: string; /** The probe as a function, for a DOM in this process (jsdom, tests). */ export declare const stepProbe: (el: unknown) => StepProbe; /** * Whether the agent may take `action` (section 4): a marked control is the User's to change or * activate; a form marked `interaction` leaves its controls and its submission to the User; a form * marked `submission` lets the agent fill its controls but leaves submitting it to the User. A value * other than `interaction` or `submission`, or `submission` on anything but a form, is * `interaction`. */ export declare function userStepFor(probe: StepProbe, action: BrowserAction): UserStepKind | null; /** A browser action the agent's model must not take (section 5). */ export declare class BrowserControlError extends PoppyError { readonly step?: UserStepKind | undefined; constructor(code: 'user_step' | 'held_by_user', message: string, step?: UserStepKind | undefined); } /** * Browser Control for one browser joined to a Session at one Company. Attach it before browsing; * call `guard` before every browser action the model asks for. * * ```ts * const control = new BrowserControl(company, { * addInitScript: (s) => page.addInitScript(s), * evaluate: (s) => page.evaluate(s), * }, { onUserStep: (step) => tellTheUser(step) }); * await control.attach(); * await control.guard({ evaluate: (src) => page.locator('#check-in').evaluate(new Function(`return ${src}`)()) }, 'activate'); * ``` */ export declare class BrowserControl { #private; readonly company: CompanyClient; readonly browser: ControlledBrowser; readonly options: { /** Called when a marked step is the User's: tell them, and offer them control (section 5). */ onUserStep?: ((step: { kind: UserStepKind; action: BrowserAction; }) => void) | undefined; }; constructor(company: CompanyClient, browser: ControlledBrowser, options?: { /** Called when a marked step is the User's: tell them, and offer them control (section 5). */ onUserStep?: ((step: { kind: UserStepKind; action: BrowserAction; }) => void) | undefined; }); /** Whether this agent declares Browser Control version 1 to the Company (section 3). */ get declared(): boolean; /** `user` while the User holds control, `agent` otherwise (section 6). */ get controller(): 'agent' | 'user'; /** * Provides `navigator.poppy.controller` in the browser, on the Company's hosts only, when the * Session declares this extension (section 6). Without it, `navigator.poppy` stays undefined. */ attach(): Promise<void>; /** * Hands control to the User (section 5). Until they hand it back, `guard` refuses every action of * the model; this agent's own requests and its conversation with the User go on meanwhile. */ handToUser(): Promise<void>; /** * The User hands control back (or cancels). Wire it ONLY to the User's own action: no timeout, page * change or anything the Company sends ends their control. Look at the page again before going on. */ userHandsBack(): Promise<void>; /** * Checks an action the model wants to take on an element. `target.evaluate(source)` runs the probe * function expression on that element (Playwright: `locator.evaluate(new Function(...))`), or pass * a `StepProbe` you computed. Throws `held_by_user` while the User holds control and `user_step` * when a mark leaves the step to the User, after telling `onUserStep`. */ guard(target: StepProbe | { evaluate(source: string): Promise<unknown>; }, action: BrowserAction): Promise<void>; } //# sourceMappingURL=browser_control.d.ts.map