UNPKG

@adonis-agora/agent

Version:

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

157 lines • 6.56 kB
import type { StoredKey } from './keys.js'; import type { Approver, PoppyOperation } from './types.js'; export interface TokenRecord { accessToken: string; tokenType: string; /** Epoch ms. */ expiresAt: number; scope: string; /** RFC 8707 resource this token was issued for, if any. */ resource?: string | undefined; } /** The refresh token of a sign-in (4.4). Stored as `accountToken`, its name before 0.3. */ export interface AccountTokenRecord { /** A secret: never logged or shown (10.2). */ refreshToken: string; scope: string; obtainedAt: number; /** Which sign-in type produced it. */ via: 'direct' | 'device' | 'mediated'; /** * Whether the sign-in sent a DPoP proof. Then every refresh of it, and of the refresh tokens that * replace it, MUST carry a proof from the same key (4.4). */ dpop?: boolean | undefined; } /** The delegated key that signs this User ID's Session JWTs, with its delegation (5.2). */ export interface DelegationRecord { key: StoredKey; /** The `poppy-delegation+jwt`, compact. */ jwt: string; /** Epoch seconds: the delegation's `exp`. */ exp: number; } export interface ConversationRecord { endpoint: string; /** Last event handled (a cursor / Last-Event-ID). */ cursor?: string | undefined; parentConversationId?: string | undefined; createdAt: number; closed?: boolean | undefined; } /** Everything the agent remembers about one User at one Company, by its identifier (3.2). */ export interface CompanyState { /** The Company's identifier: its issuer, or `https://{domain}` without one (3.2). */ issuer: string; /** Whether `issuer` is a real OAuth issuer, not the domain fallback. */ hasIssuer?: boolean | undefined; /** The protocol version this state was written for; an older one drops tokens and Session. */ protocol?: string | undefined; domains: string[]; /** Opaque, random, per-Company User ID (5.1). Never derived from personal information. */ userId: string; /** DPoP key for this User at this Company. Never published. */ dpopKey: StoredKey; /** One User ID's delegated key: never shared with another User ID (5.2). */ delegation?: DelegationRecord | undefined; accountToken?: AccountTokenRecord | undefined; /** Access tokens by key: `default`, or `{resource}|{scope}|{bearer}`. */ tokens: Record<string, TokenRecord>; /** The current Session's `sid`: random, used for no other Session (5.2). Not a credential. */ sessionId?: string | undefined; /** * `Poppy-Version` and `Poppy-Extensions` of the current Session. They stay the same throughout a * Session, so a change starts a new one (3.4). */ sessionVersions?: string | undefined; /** * Agent IDs (5.1), by the local name of the agent a Session is for: opaque, random, per Company. * Never derived from personal information. */ agentIds?: Record<string, string> | undefined; /** The current Session of each named agent: a Session keeps one `agent_id` (5.2). */ agentSessions?: Record<string, { sessionId?: string | undefined; versions?: string | undefined; }>; /** * The `sid`s this agent joined its browser to (section 6). A `Poppy-Operation` header counts only * when its `sid` is one of them (Operations section 4). */ browserSessions?: string[] | undefined; /** The operations this User met at this Company, by `operation_id` (Operations extension). */ operations?: Record<string, OperationRecord> | undefined; conversations: Record<string, ConversationRecord>; } /** * What this agent remembers about one operation: enough to never send a declined approval again, to * ask the User when an agent approval met new terms, and to approve a parked one later. */ export interface OperationRecord { via: 'api' | 'mcp' | 'conversation' | 'website'; /** The latest revision the Company returned. */ operation: PoppyOperation; /** What the agent asked for when the Company returned it (to resend it with the approval). */ asked?: Record<string, unknown> | undefined; /** Approvals sent, oldest first. */ approvals: { revision: string; approver: Approver; reference: string; approvedAt: number; }[]; /** * `pending` (waiting for an approval), `sent` (approval sent, outcome not in), `performed`, * `declined`, `unknown` (a retry answered with new terms: whether it took effect is unknown). */ status: 'pending' | 'sent' | 'performed' | 'declined' | 'unknown'; /** A decline for this action: a new revision needs the User (Operations section 6). */ declined?: boolean | undefined; /** On a website: the reference from `Poppy-Operation`, and the `sid` it named. */ webRef?: string | undefined; updatedAt: number; } export interface IdentityState { baseUrl: string; clientName: string; logoUri?: string | undefined; keys: StoredKey[]; } export interface StateFile { version: 1; identity?: IdentityState | undefined; /** Local users ("profiles") of this agent, each with their Companies. */ profiles: Record<string, { companies: Record<string, CompanyState>; }>; } export declare function defaultStatePath(): string; /** * The local state file. It holds private keys and refresh tokens, so it is written with mode * 0600 (directory 0700) and atomically (temp file + rename). */ export declare class StateStore { #private; readonly path: string; data: StateFile; private readonly persist?; private constructor(); /** * A state kept by the caller instead of a file — a server keeping each user's Companies in its * own database. `persist` receives the whole state on every save (serialized, like the file). */ static memory(data?: StateFile, persist?: (data: StateFile) => Promise<void>): StateStore; static open(path?: string): Promise<StateStore>; /** Directory next to the state file, used for the Direct Sign-In callback relay. */ get dir(): string; profile(name: string): { companies: Record<string, CompanyState>; }; /** * Writes the state atomically (tmp file + rename). Saves are serialized: a conversation stream * and the prompt can save at the same time, and two writes sharing one tmp file made the second * `rename` fail with ENOENT. */ save(): Promise<void>; } //# sourceMappingURL=store.d.ts.map