import { v as Agent } from "./agent-tool-types-DSteYkkS.js"; import { ClientParameters, Method, RPCMethod, SerializableReturnValue, SerializableValue } from "./serializable.js"; import { PartyFetchOptions, PartySocket, PartySocketOptions } from "partysocket"; //#region src/client.d.ts /** * Options for creating an AgentClient */ type AgentClientOptions = Omit< PartySocketOptions, "party" | "room" > & { /** Name of the agent to connect to (ignored if basePath is set) */ agent: string /** Name of the specific Agent instance (ignored if basePath is set) */; name?: string; /** * Full URL path - bypasses agent/name URL construction. * When set, the client connects to this path directly. * Server must handle routing manually (e.g., with getAgentByName + fetch). * @example * // Client connects to /user, server routes based on session * useAgent({ agent: "UserAgent", basePath: "user" }) */ basePath?: string /** Called when the Agent's state is updated */; onStateUpdate?: ( state: State, source: "server" | "client" ) => void /** Called when a state update fails (e.g., connection is readonly) */; onStateUpdateError?: (error: string) => void; /** * Called when the server sends the agent's identity on connect. * Useful when using basePath, as the actual instance name is determined server-side. * @param name The actual agent instance name * @param agent The agent class name (kebab-case) */ onIdentity?: (name: string, agent: string) => void; /** * Called when identity changes on reconnect (different instance than before). * If not provided and identity changes, a warning will be logged. * @param oldName Previous instance name * @param newName New instance name * @param oldAgent Previous agent class name * @param newAgent New agent class name */ onIdentityChange?: ( oldName: string, newName: string, oldAgent: string, newAgent: string ) => void; /** * Additional path to append to the URL. * Works with both standard routing and basePath. * @example * // With basePath: /user/settings * { basePath: "user", path: "settings" } * // Standard: /agents/my-agent/room/settings * { agent: "MyAgent", name: "room", path: "settings" } */ path?: string; }; /** * Options for streaming RPC calls */ type StreamOptions = { /** Called when a chunk of data is received */ onChunk?: ( chunk: unknown ) => void /** Called when the stream ends */; onDone?: (finalChunk: unknown) => void /** Called when an error occurs */; onError?: (error: string) => void; }; /** * Options for RPC calls */ type CallOptions = { /** Timeout in milliseconds. If the call doesn't complete within this time, it will be rejected. */ timeout?: number /** Streaming options for handling streaming responses */; stream?: StreamOptions; }; /** * Options for the agentFetch function */ type AgentClientFetchOptions = Omit & { /** Name of the agent to connect to (ignored if basePath is set) */ agent: string /** Name of the specific Agent instance (ignored if basePath is set) */; name?: string; /** * Full URL path - bypasses agent/name URL construction. * When set, the request is made to this path directly. */ basePath?: string; }; type AllOptional = T extends [infer A, ...infer R] ? undefined extends A ? AllOptional : false : true; type RPCMethods = { [K in keyof T as T[K] extends RPCMethod ? K : never]: RPCMethod; }; type OptionalParametersMethod = AllOptional> extends true ? T : never; type AgentMethods = Omit, keyof Agent>; type OptionalAgentMethods = { [K in keyof AgentMethods as AgentMethods[K] extends OptionalParametersMethod< AgentMethods[K] > ? K : never]: OptionalParametersMethod[K]>; }; type RequiredAgentMethods = Omit< AgentMethods, keyof OptionalAgentMethods >; type AgentPromiseReturnType> = ReturnType[K]> extends Promise ? ReturnType[K]> : Promise[K]>>; type AgentStub = { [K in keyof AgentMethods]: ( ...args: ClientParameters[K]> ) => AgentPromiseReturnType, K>; }; type UntypedAgentStub = Record; type AgentClientStub = keyof AgentMethods extends never ? UntypedAgentStub : AgentStub; type OptionalArgsAgentClientCall = < K extends keyof OptionalAgentMethods >( method: K, args?: ClientParameters[K]>, options?: CallOptions | StreamOptions ) => AgentPromiseReturnType; type RequiredArgsAgentClientCall = < K extends keyof RequiredAgentMethods >( method: K, args: ClientParameters[K]>, options?: CallOptions | StreamOptions ) => AgentPromiseReturnType; type TypedAgentClientCall = OptionalArgsAgentClientCall & RequiredArgsAgentClientCall; type UntypedAgentClientCall = { ( method: string, args?: SerializableValue[], options?: CallOptions | StreamOptions ): Promise; ( method: string, args?: unknown[], options?: CallOptions | StreamOptions ): Promise; }; type AgentClientCall = keyof AgentMethods extends never ? UntypedAgentClientCall : TypedAgentClientCall; /** * Creates a proxy that wraps RPC method calls. * Internal JS methods (toJSON, then, etc.) return undefined to avoid * triggering RPC calls during serialization (e.g., console.log) */ declare function createStubProxy>( call: (method: string, args: unknown[]) => unknown ): T; /** * WebSocket client for connecting to an Agent */ declare class AgentClient< AgentT = unknown, State = AgentT extends { get state(): infer S; } ? S : AgentT > extends PartySocket { /** * @deprecated Use agentFetch instead */ static fetch(_opts: PartyFetchOptions): Promise; agent: string; name: string; call: AgentClientCall; stub: AgentClientStub; /** * The current agent state, updated on server broadcasts and client setState calls. * Starts as undefined until the first state message is received from the server. */ state: State | undefined; /** * Whether the client has received identity from the server. * Becomes true after the first identity message is received. * Resets to false on connection close. */ identified: boolean; /** * Promise that resolves when identity has been received from the server. * Useful for waiting before making calls that depend on knowing the instance. * Resets on connection close so it can be awaited again after reconnect. */ get ready(): Promise; private options; private _pendingCalls; private _readyPromise; private _resolveReady; private _previousName; private _previousAgent; private _resetReady; constructor(options: AgentClientOptions); /** * Reject all pending RPC calls with the given reason. */ private _rejectPendingCalls; setState(state: State): void; /** * Close the connection and immediately reject all pending RPC calls. * This provides immediate feedback on intentional close rather than * waiting for the WebSocket close handshake to complete. * * Note: Any calls made after `close()` will be rejected when the * underlying WebSocket close event fires. */ close(code?: number, reason?: string): void; /** * Call a method on the Agent. * When AgentT is provided, method names are inferred from the agent's methods. * Falls back to untyped string-based calls when AgentT is not provided. */ private _callImpl; } /** * Make an HTTP request to an Agent * @param opts Connection options * @param init Request initialization options * @returns Promise resolving to a Response */ declare function agentFetch( opts: AgentClientFetchOptions, init?: RequestInit ): Promise; //#endregion export { AgentClient, AgentClientFetchOptions, AgentClientOptions, AgentMethods, AgentPromiseReturnType, AgentStub, CallOptions, OptionalAgentMethods, RPCMethods, RequiredAgentMethods, StreamOptions, UntypedAgentStub, agentFetch, createStubProxy }; //# sourceMappingURL=client.d.ts.map