UNPKG

@adonis-agora/agent

Version:

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

184 lines • 9.51 kB
import { type Discovered, HttpJsonCache } from './discovery.js'; import { DpopKey } from './dpop.js'; import { PoppyHttp, type PoppyRequestInit } from './http.js'; import type { AgentIdentity } from './identity.js'; import type { OperationApprover } from './operations.js'; import type { AccountTokenRecord, CompanyState, StateStore, TokenRecord } from './store.js'; import { type NarrowingParams, TokenClient } from './token_client.js'; import type { AuthServerMetadata, PoppyDocument, TokenResponse } from './types.js'; import { type FetchLike, type SecurityOptions } from './util.js'; export interface PoppyAgentOptions extends SecurityOptions { store: StateStore; identity: AgentIdentity; /** Local user ("profile") the agent acts for. Defaults to `default`. */ profile?: string | undefined; fetch?: FetchLike | undefined; log?: ((line: string) => void) | undefined; maxRetryAfterSeconds?: number | undefined; /** * Extensions this agent uses, by name, with the major version of each (3.3). With a Company that * lists the same version, each request declares it in `Poppy-Extensions` (3.4). None by default. */ extensions?: Record<string, string> | undefined; /** Use Companies on ports other than 443, for testing (3.1). `insecureDev` implies it. */ allowPorts?: boolean | undefined; /** * Which of the operator's agents acts (5.1), such as `travel` or `shopping`: each name gets its own * Agent ID at each Company, opaque and random, sent as `agent_id` in its Sessions. Leave it out for * an agent that is the only one: then no `agent_id` is sent. */ agent?: string | undefined; /** * The Operations extension (declare `operations: '3'` in `extensions`): `approve` decides each * operation a Company returns, asking the User or approving on an instruction they gave * (Operations section 5). Without it, nothing is approved and the action isn't performed. */ operations?: { approve?: OperationApprover | undefined; /** Times a request carrying an approval is sent again after a failure. Default 3. */ maxRetries?: number | undefined; /** Longest wait for a `503` with `Retry-After` while an operation is performed. Default 30 s. */ maxRetryAfterSeconds?: number | undefined; } | undefined; } /** Entry point: one agent identity, one local state file, one local user. */ export declare class PoppyAgent { readonly opts: PoppyAgentOptions; readonly http: PoppyHttp; readonly cache: HttpJsonCache; readonly profile: string; constructor(opts: PoppyAgentOptions); get identity(): AgentIdentity; get store(): StateStore; log(line: string): void; /** Discovery (section 3) with this agent's fetch, cache and options. */ discover(domain: string, opts?: { fresh?: boolean | undefined; }): Promise<Discovered>; /** Discovers a Company and loads (or creates) this user's state there, by its identifier. */ company(domain: string): Promise<CompanyClient>; } interface TokenOptions extends NarrowingParams { forceRenew?: boolean | undefined; } /** `Poppy-Extensions`: an RFC 9651 List of Strings, each with its version in `v` (3.4). */ export declare function formatExtensionsHeader(declared: Record<string, string>): string; export interface SignInResult { grantedScope: string; /** Requested scopes the User didn't grant (4.7: "checks scope before continuing"). */ missingScopes: string[]; } export type CompanyFetchInit = Omit<PoppyRequestInit, 'dpop' | 'accessToken' | 'scheme' | 'session'> & NarrowingParams & { /** The service's URL, whose protected resource metadata says if it needs its own tokens (4.5). */ service?: string | undefined; /** Only the Session header, even with a refresh token. */ signedOut?: boolean | undefined; }; /** One User at one Company: the Session, the refresh token, and requests to its endpoints. */ export declare class CompanyClient { #private; readonly agent: PoppyAgent; readonly state: CompanyState; readonly dpop: DpopKey; /** An anonymous browsing session (5.1): signed out only, and nothing saved. */ readonly anonymous: boolean; constructor(agent: PoppyAgent, discovered: Discovered, state: CompanyState, options?: { anonymous?: boolean | undefined; }); /** The discovery this client works with: the chosen version's document (3.4). */ get discovered(): Discovered; /** `Poppy-Version`: the version this agent implements, compatible with the chosen document (3.4). */ get protocolVersion(): string; /** * The extensions each request declares (3.4): those this agent uses, in a version the chosen * document lists. Never one only a later version defines (3.4, minor versions). */ get declaredExtensions(): Record<string, string>; /** The version headers of every request with the Session header (3.4). */ versionHeaders(): Record<string, string>; /** The Company's OAuth calls. A Company without an issuer offers no sign-in (3.2). */ get tokens(): TokenClient; get document(): PoppyDocument; get metadata(): AuthServerMetadata; /** The Company's identifier (3.2): its issuer, or its HTTPS domain without one. */ get issuer(): string; get userId(): string; get http(): PoppyHttp; get security(): SecurityOptions; get accountToken(): AccountTokenRecord | undefined; /** * The current Session's `sid`, chosen here at random (5.2). A Session keeps its `Poppy-Version` and * `Poppy-Extensions`, so new values start a new Session (3.4). */ get sessionId(): string; /** * The local name of the agent this client's Sessions are for (5.1). Never in an anonymous * browsing Session: an Agent ID there would tie it to the User's regular one. */ get agentName(): string | undefined; /** * This User's Agent ID for the named agent at this Company (5.1): stable, opaque, random, one per * Company. `undefined` without a named agent. */ get agentId(): string | undefined; save(): Promise<void>; /** Stops using the current `sid`; the next request starts a new Session (5.5). */ newSession(): Promise<void>; /** * An anonymous browsing session at this Company (5.1), like a private browser window: a new User * ID, its own delegated key and DPoP key, no Agent ID, never an access token. Nothing of it is * saved; `discard()` it when the browsing session ends. */ anonymousSession(): Promise<CompanyClient>; /** Ends an anonymous browsing session: its User ID, keys and Session are forgotten. */ discard(): void; /** Sends a proof with token requests when the issuer's metadata lists ES256 (4.5). */ tokenDpop(): DpopKey | undefined; /** * The proof key of a refresh (4.4): the key the refresh token was got with, for every refresh of it * and of the tokens that replace it, whatever the metadata says now. The key changes only with a * new sign-in. */ refreshDpop(): DpopKey | undefined; /** A `Poppy-Session` JWT good for this one request (5.2). */ sessionJwt(method: string, url: string, accessToken?: string | undefined): Promise<string>; /** * An access token, refreshed when close to expiry, or `null` when signed out. A refresh answered * with `invalid_grant` means the sign-in is gone: the Session continues signed out (5.5). */ token(opts?: TokenOptions): Promise<TokenRecord | null>; /** Stores the result of any sign-in type (4.7): the refresh token and the access token. */ applySignIn(resp: TokenResponse, via: AccountTokenRecord['via'], requestedScope: string): Promise<SignInResult>; /** Sign out (5.5): revoke the refresh token. Sessions carry on signed out. */ signOut(): Promise<boolean>; /** * A request to one of the Company's APIs or endpoints: the Session header with `Poppy-Version` (and * `Poppy-Extensions`), and, when signed in, an access token (5.2, 3.4, 4.5). Retries once on: * `session_ended` in a new Session (5.5); `version_unsupported` after fetching `poppy.json` again, * when that chooses another version (3.4); `invalid_token` after a refresh, falling back to signed * out when the sign-in is gone (5.5). `invalid_session` isn't retried: a new Session doesn't fix it * (section 7). */ fetch(url: string, init?: CompanyFetchInit): Promise<Response>; /** * A page of the Company's website fetched with this agent's own HTTP client, not a browser (5.2): * the Session header, always signed out, only on HTTPS pages whose host is `organization.domain`. * A redirect gets a new Session JWT only while it stays on that host. After `invalid_session` the * page is fetched once more without the header. */ fetchPage(url: string, init?: { headers?: Record<string, string>; }): Promise<Response>; /** `fetch` + JSON, mapping failures to typed errors (section 7, 8.13). */ json<T>(url: string, init?: CompanyFetchInit): Promise<{ status: number; body: T; }>; } /** * Maps a failed resource response to a typed error. A challenge without an error code means the * request needs the account: it's handled as `insufficient_scope` (section 7). */ export declare function throwForResponse(res: Response): Promise<never>; export {}; //# sourceMappingURL=company.d.ts.map