@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
1 lines • 4.56 kB
Source Map (JSON)
{"version":3,"file":"values.cjs","names":["DEFAULT_MESSAGES_KEY","namespaceKey","isRootNamespace","openProjectionSubscription","ensureMessageInstances"],"sources":["../../../src/stream/projections/values.ts"],"sourcesContent":["/**\n * Namespace-scoped `values` projection.\n *\n * Opens `thread.subscribe({ channels: [\"values\"], namespaces: [ns] })`\n * and stores the most-recent `values.data` payload. Mirrors\n * {@link ThreadStream.values} but scoped to an arbitrary namespace so\n * subgraphs and subagents can expose their own state.\n *\n * When the state payload carries a `messages` array of serialized\n * messages, those are coerced to `BaseMessage` class instances so the\n * surface shape matches the root snapshot.\n */\nimport type { ValuesEvent } from \"@langchain/protocol\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport {\n ensureMessageInstances,\n tryCoerceMessageLikeToMessage,\n} from \"../message-coercion.js\";\nimport type { Message } from \"../../types.messages.js\";\nimport type { ProjectionSpec, ProjectionRuntime } from \"../types.js\";\nimport { DEFAULT_MESSAGES_KEY } from \"../constants.js\";\nimport { isRootNamespace, namespaceKey } from \"../namespace.js\";\nimport { openProjectionSubscription } from \"./runtime.js\";\n\nexport function valuesProjection<T = unknown>(\n namespace: readonly string[],\n messagesKey: string = DEFAULT_MESSAGES_KEY\n): ProjectionSpec<T | undefined> {\n const ns = [...namespace];\n const key = `values|${messagesKey}|${namespaceKey(ns)}`;\n\n return {\n key,\n namespace: ns,\n initial: undefined,\n open({ thread, store, rootBus }): ProjectionRuntime {\n const applyValuesEvent = (event: ValuesEvent): void => {\n const coerced = coerceMessagesInState(event.params.data, messagesKey);\n store.setValue(coerced as T);\n };\n\n // See `messagesProjection` — root-scoped projections attach to\n // the controller's root bus instead of opening a duplicate\n // server subscription.\n const rootShortCircuit =\n isRootNamespace(ns) && rootBus.channels.includes(\"values\");\n\n if (rootShortCircuit) {\n const unsubscribe = rootBus.subscribe((event) => {\n if (event.method !== \"values\") return;\n if (!isRootNamespace(event.params.namespace)) return;\n applyValuesEvent(event as ValuesEvent);\n });\n return {\n dispose() {\n unsubscribe();\n },\n };\n }\n\n return openProjectionSubscription({\n thread,\n channels: [\"values\"],\n namespace: ns,\n onEvent(event) {\n if (event.method !== \"values\") return;\n applyValuesEvent(event as ValuesEvent);\n },\n });\n },\n };\n}\n\nfunction coerceMessagesInState(value: unknown, messagesKey: string): unknown {\n if (value == null || typeof value !== \"object\" || Array.isArray(value)) {\n return value;\n }\n const state = value as Record<string, unknown>;\n const maybeMessages = state[messagesKey];\n if (!Array.isArray(maybeMessages) || maybeMessages.length === 0) {\n return value;\n }\n // Fast path: array already contains class instances.\n const hasPlain = maybeMessages.some(\n (m) => m != null && typeof (m as BaseMessage).getType !== \"function\"\n );\n if (!hasPlain) return value;\n return {\n ...state,\n [messagesKey]: ensureMessageInstances(\n maybeMessages as (Message | BaseMessage)[]\n ),\n };\n}\n\nexport { tryCoerceMessageLikeToMessage };\n"],"mappings":";;;;;AAwBA,SAAgB,iBACd,WACA,cAAsBA,kBAAAA,sBACS;CAC/B,MAAM,KAAK,CAAC,GAAG,UAAU;AAGzB,QAAO;EACL,KAHU,UAAU,YAAY,GAAGC,kBAAAA,aAAa,GAAG;EAInD,WAAW;EACX,SAAS,KAAA;EACT,KAAK,EAAE,QAAQ,OAAO,WAA8B;GAClD,MAAM,oBAAoB,UAA6B;IACrD,MAAM,UAAU,sBAAsB,MAAM,OAAO,MAAM,YAAY;AACrE,UAAM,SAAS,QAAa;;AAS9B,OAFEC,kBAAAA,gBAAgB,GAAG,IAAI,QAAQ,SAAS,SAAS,SAAS,EAEtC;IACpB,MAAM,cAAc,QAAQ,WAAW,UAAU;AAC/C,SAAI,MAAM,WAAW,SAAU;AAC/B,SAAI,CAACA,kBAAAA,gBAAgB,MAAM,OAAO,UAAU,CAAE;AAC9C,sBAAiB,MAAqB;MACtC;AACF,WAAO,EACL,UAAU;AACR,kBAAa;OAEhB;;AAGH,UAAOC,gBAAAA,2BAA2B;IAChC;I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