UNPKG

@langchain/langgraph-sdk

Version:

Client library for interacting with the LangGraph API

1 lines • 5.38 kB
{"version":3,"file":"utils.cjs","names":[],"sources":["../../../../src/client/stream/transport/utils.ts"],"sourcesContent":["import type { HeaderValue, ProtocolPath } from \"./types.js\";\nimport type { CommandResponse, ErrorResponse } from \"@langchain/protocol\";\n\nexport const isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null;\n\n/**\n * True for a 4xx HTTP error other than 408 (Request Timeout) and 429\n * (Too Many Requests): retrying the same request cannot succeed. Errors\n * with no `status` (network errors, idle timeouts, server closes) stay\n * retryable.\n */\nexport function isNonRetryableHttpError(error: unknown): boolean {\n if (!isRecord(error) || typeof error.status !== \"number\") return false;\n const { status } = error;\n return status >= 400 && status < 500 && status !== 408 && status !== 429;\n}\n\n/**\n * Resolve a {@link ProtocolPath} against the transport's currently-bound\n * thread.\n *\n * - a fixed `string` is trusted and used verbatim, with no encoding\n * (back-compat: a baked path is independent of the bound thread);\n * - a function path and the default fallback are evaluated against\n * `threadId`, so late-bound / re-bound adapters target the right thread.\n *\n * Throws when neither a fixed path nor a bound thread is available — i.e.\n * a request was attempted before `client.threads.stream(threadId, …)` /\n * {@link TransportAdapter.setThreadId} bound a thread.\n */\nexport function resolveProtocolPath(\n path: ProtocolPath | undefined,\n threadId: string,\n fallback: (threadId: string) => string\n): string {\n if (typeof path === \"string\") return path;\n if (!threadId) {\n throw new Error(\n \"Protocol transport has no bound threadId. Bind one — the framework \" +\n \"calls client.threads.stream(threadId, { transport }) / \" +\n \"transport.setThreadId(threadId) — before issuing requests.\"\n );\n }\n return path ? path(threadId) : fallback(threadId);\n}\n\n/** Match {@link BaseClient.prepareFetchOptions}: preserve any apiUrl path prefix. */\nexport const toAbsoluteUrl = (apiUrl: string, path: string) =>\n new URL(`${apiUrl.replace(/\\/$/, \"\")}${path}`);\n\nexport const toError = (error: unknown) =>\n // oxlint-disable-next-line no-instanceof/no-instanceof\n error instanceof Error ? error : new Error(String(error));\n\nexport const toWebSocketUrl = (apiUrl: string): string => {\n // Extract path from the input (e.g. \"http://host/threads/X/stream\") and\n // swap the scheme to ws/wss. The caller passes a fully-formed URL\n // with the desired path.\n const url = new URL(apiUrl);\n url.protocol = url.protocol === \"https:\" ? \"wss:\" : \"ws:\";\n url.search = \"\";\n url.hash = \"\";\n return url.toString();\n};\n\nexport const hasHeaders = (headers?: Record<string, HeaderValue>) =>\n Object.values(headers ?? {}).some((value) => value != null);\n\nexport function mergeHeaders(\n ...headerGroups: Array<\n HeadersInit | Record<string, HeaderValue> | undefined | null\n >\n): Headers {\n const merged = new Headers();\n\n for (const group of headerGroups) {\n if (!group) {\n continue;\n }\n\n // oxlint-disable-next-line no-instanceof/no-instanceof\n if (group instanceof Headers) {\n group.forEach((value, key) => {\n merged.set(key, value);\n });\n continue;\n }\n\n if (Array.isArray(group)) {\n for (const [key, value] of group) {\n if (value == null) {\n merged.delete(key);\n } else {\n merged.set(key, value);\n }\n }\n continue;\n }\n\n for (const [key, value] of Object.entries(group)) {\n if (value == null) {\n merged.delete(key);\n } else {\n merged.set(key, value);\n }\n }\n }\n\n return merged;\n}\n\nexport function isProtocolResponse(\n value: unknown\n): value is CommandResponse | ErrorResponse {\n return (\n isRecord(value) &&\n typeof value.type === \"string\" &&\n (value.type === \"success\" || value.type === \"error\")\n );\n}\n"],"mappings":";AAGA,MAAa,YAAY,UACvB,OAAO,UAAU,YAAY,UAAU;;;;;;;AAQzC,SAAgB,wBAAwB,OAAyB;AAC/D,KAAI,CAAC,SAAS,MAAM,IAAI,OAAO,MAAM,WAAW,SAAU,QAAO;CACjE,MAAM,EAAE,WAAW;AACnB,QAAO,UAAU,OAAO,SAAS,OAAO,WAAW,OAAO,WAAW;;;;;;;;;;;;;;;AAgBvE,SAAgB,oBACd,MACA,UACA,UACQ;AACR,KAAI,OAAO,SAAS,SAAU,QAAO;AACrC,KAAI,CAAC,SACH,OAAM,IAAI,MACR,uLAGD;AAEH,QAAO,OAAO,KAA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