@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
1 lines • 6.1 kB
Source Map (JSON)
{"version":3,"file":"agent-server.cjs","names":["#delegate","ProtocolWebSocketTransportAdapter","ProtocolSseTransportAdapter"],"sources":["../../../../src/client/stream/transport/agent-server.ts"],"sourcesContent":["/**\n * Stock `AgentServerAdapter` implementation for \"point `useStream` at a\n * single HTTP endpoint that speaks the v2 protocol\" deployments.\n *\n * Internally delegates to the appropriate built-in transport:\n * - `new HttpAgentServerAdapter({ apiUrl, threadId })` → SSE\n * - `new HttpAgentServerAdapter({ apiUrl, threadId, webSocketFactory })`\n * → WebSocket\n *\n * Keeps the user-facing import surface small: callers only ever import\n * `HttpAgentServerAdapter` from `@langchain/langgraph-sdk` instead of\n * knowing the two wire-specific class names. The class is deliberately\n * thin — it forwards every method on {@link AgentServerAdapter} to the\n * delegate it picked at construction time.\n *\n * See `plan-custom-transport.md` §4.3 for motivation.\n */\nimport type {\n AgentServerAdapter,\n EventStreamHandle,\n TransportAdapter,\n} from \"../transport.js\";\nimport type {\n Command,\n CommandResponse,\n ErrorResponse,\n Message,\n SubscribeParams,\n} from \"@langchain/protocol\";\nimport type { AsyncCaller } from \"../../../utils/async_caller.js\";\nimport { ProtocolSseTransportAdapter } from \"./http.js\";\nimport { ProtocolWebSocketTransportAdapter } from \"./websocket.js\";\nimport type {\n HeaderValue,\n ProtocolRequestHook,\n ProtocolTransportPaths,\n} from \"./types.js\";\n\nexport interface HttpAgentServerAdapterOptions {\n apiUrl: string;\n /**\n * Thread this adapter targets. Optional: omit to construct an unbound\n * adapter and let the framework bind it via {@link HttpAgentServerAdapter.setThreadId}\n * (`client.threads.stream` does this) — e.g. lazy thread creation, where\n * the id is only known after the first `submit()`.\n */\n threadId?: string;\n /** Auth / tenant / diagnostic headers applied to every request. */\n defaultHeaders?: Record<string, HeaderValue>;\n /** Per-request hook for last-mile header mutation. */\n onRequest?: ProtocolRequestHook;\n /** Override the default `/threads/:threadId/...` protocol paths. */\n paths?: ProtocolTransportPaths;\n /**\n * Optional `fetch` override, forwarded to the SSE transport. Useful\n * for auth proxies, Next.js route handlers, or tests with injected\n * mocks. Ignored when `webSocketFactory` is also supplied.\n */\n fetch?: typeof fetch;\n /**\n * Retries and concurrency for SSE/command HTTP. When omitted, requests\n * use raw `fetch` with no automatic retries (same as constructing\n * {@link ProtocolSseTransportAdapter} without this option).\n */\n asyncCaller?: AsyncCaller;\n /**\n * Optional WebSocket factory. Supplying it flips the adapter into\n * WebSocket mode — SSE is bypassed entirely.\n */\n webSocketFactory?: (url: string) => WebSocket;\n}\n\nexport class HttpAgentServerAdapter implements AgentServerAdapter {\n threadId: string;\n\n readonly apiUrl: string;\n\n readonly #delegate: TransportAdapter;\n\n /**\n * Thread-state reads are SSE-only. WebSocket delegates omit this so\n * {@link StreamController} falls back to `client.threads.getState()`.\n */\n getState?: AgentServerAdapter[\"getState\"];\n\n constructor(options: HttpAgentServerAdapterOptions) {\n this.threadId = options.threadId ?? \"\";\n this.apiUrl = options.apiUrl;\n this.#delegate =\n options.webSocketFactory != null\n ? new ProtocolWebSocketTransportAdapter({\n apiUrl: options.apiUrl,\n threadId: options.threadId,\n defaultHeaders: options.defaultHeaders,\n onRequest: options.onRequest,\n paths: options.paths,\n webSocketFactory: options.webSocketFactory,\n })\n : new ProtocolSseTransportAdapter({\n apiUrl: options.apiUrl,\n threadId: options.threadId,\n defaultHeaders: options.defaultHeaders,\n onRequest: options.onRequest,\n fetch: options.fetch,\n asyncCaller: options.asyncCaller,\n paths: options.paths,\n });\n\n if (options.webSocketFactory == null) {\n const sse = this.#delegate as ProtocolSseTransportAdapter;\n this.getState = sse.getState.bind(sse);\n }\n }\n\n /** {@inheritDoc TransportAdapter.setThreadId} */\n setThreadId(threadId: string): void {\n this.threadId = threadId;\n this.#delegate.setThreadId?.(threadId);\n }\n\n open(): Promise<void> {\n return this.#delegate.open();\n }\n\n send(command: Command): Promise<CommandResponse | ErrorResponse | void> {\n return this.#delegate.send(command);\n }\n\n events(): AsyncIterable<Message> {\n return this.#delegate.events();\n }\n\n openEventStream(params: SubscribeParams): EventStreamHandle {\n if (this.#delegate.openEventStream == null) {\n throw new Error(\n \"HttpAgentServerAdapter delegate does not support openEventStream (WebSocket path).\"\n );\n }\n return this.#delegate.openEventStream(params);\n }\n\n close(): Promise<void> {\n return this.#delegate.close();\n }\n}\n"],"mappings":";;;AAwEA,IAAa,yBAAb,MAAkE;CAChE;CAEA;CAEA;;;;;CAMA;CAEA,YAAY,SAAwC;AAClD,OAAK,WAAW,QAAQ,YAAY;AACpC,OAAK,SAAS,QAAQ;AACtB,QAAA,WACE,QAAQ,oBAAoB,OACxB,IAAIC,kBAAAA,kCAAkC;GACpC,QAAQ,QAAQ;GAChB,UAAU,QAAQ;GAClB,gBAAgB,QAAQ;GACxB,WAAW,QAAQ;GACnB,OAAO,QAAQ;GACf,kBAAkB,QAAQ;GAC3B,CAAC,GACF,IAAIC,aAAAA,4BAA4B;GAC9B,QAAQ,QAAQ;GAChB,UAAU,QAAQ;GAClB,gBAAgB,QAAQ;GACxB,WAAW,QAAQ;GACnB,OAAO,QAAQ;GACf,aAAa,QAAQ;GACrB,OAAO,QAAQ;GAChB,CAAC;AAER,MAAI,QAAQ,oBAAoB,MAAM;GACpC,MAAM,MAAM,MAAA;AACZ,QAAK,WAAW,IAAI,SAAS,KAAK,IAAI;;;;CAK1C,YAAY,UAAwB;AAClC,OAAK,WAAW;AAChB,QAAA,SAAe,cAAc,SAAS;;CAGxC,OAAsB;AACpB,SAAO,MAAA,SAAe,MAAM;;CAG9B,KAAK,SAAmE;AACtE,SAAO,MAAA,SAAe,KAAK,QAAQ;;CAGrC,SAAiC;AAC/B,SAAO,MAAA,SAAe,QAAQ;;CAGhC,gBAAgB,QAA4C;AAC1D,MAAI,MAAA,SAAe,mBAAmB,KACpC,OAAM,IAAI,MACR,qFACD;AAEH,SAAO,MAAA,SAAe,gBAAgB,OAAO;;CAG/C,QAAuB;AACrB,SAAO,MAAA,SAAe,OAAO"}