@langchain/angular 1.0.16 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/selectors.cjs +34 -0
- package/dist/selectors.cjs.map +1 -1
- package/dist/selectors.d.cts.map +1 -1
- package/dist/selectors.d.ts.map +1 -1
- package/dist/selectors.js +36 -2
- package/dist/selectors.js.map +1 -1
- package/dist/use-stream.cjs +2 -1
- package/dist/use-stream.cjs.map +1 -1
- package/dist/use-stream.js +2 -1
- package/dist/use-stream.js.map +1 -1
- package/package.json +5 -5
package/dist/selectors.cjs
CHANGED
|
@@ -12,6 +12,38 @@ function resolveNamespace(target) {
|
|
|
12
12
|
function isRoot(namespace) {
|
|
13
13
|
return namespace.length === 0;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* If `target` is a subagent snapshot still on its default
|
|
17
|
+
* `tools:<toolCallId>` namespace, return that tool-call id. See the
|
|
18
|
+
* React selectors for the rationale (deep-agent subagents execute under
|
|
19
|
+
* a distinct `tools:<uuid>` namespace resolved lazily from history).
|
|
20
|
+
*/
|
|
21
|
+
function subagentNeedingNamespace(target) {
|
|
22
|
+
if (target == null || Array.isArray(target)) return null;
|
|
23
|
+
const obj = target;
|
|
24
|
+
if (typeof obj.id !== "string" || !Array.isArray(obj.namespace)) return null;
|
|
25
|
+
if (obj.namespace.length === 1 && obj.namespace[0] === `tools:${obj.id}`) return obj.id;
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Lazily resolve a subagent's execution namespace on first scoped use.
|
|
30
|
+
* Must be called from an injection context (as the selector primitives
|
|
31
|
+
* are). A `Signal` target re-evaluates via an `effect`; a static target
|
|
32
|
+
* resolves once. The controller de-dupes and skips already-promoted
|
|
33
|
+
* ids.
|
|
34
|
+
*/
|
|
35
|
+
function resolveSubagentNamespaceFor(stream, target) {
|
|
36
|
+
const controller = stream[require_use_stream.STREAM_CONTROLLER];
|
|
37
|
+
if ((0, _angular_core.isSignal)(target)) {
|
|
38
|
+
(0, _angular_core.effect)(() => {
|
|
39
|
+
const id = subagentNeedingNamespace(target());
|
|
40
|
+
if (id != null) controller.resolveSubagentNamespace(id);
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const id = subagentNeedingNamespace(target);
|
|
45
|
+
if (id != null) controller.resolveSubagentNamespace(id);
|
|
46
|
+
}
|
|
15
47
|
function namespaceKey(namespace) {
|
|
16
48
|
return namespace.join(_langchain_langgraph_sdk_stream.NAMESPACE_SEPARATOR);
|
|
17
49
|
}
|
|
@@ -53,12 +85,14 @@ function normalizeTarget(target, prefix) {
|
|
|
53
85
|
* `@langchain/core/messages`.
|
|
54
86
|
*/
|
|
55
87
|
function injectMessages(stream, target) {
|
|
88
|
+
resolveSubagentNamespaceFor(stream, target);
|
|
56
89
|
const { namespace, key, isRootSignal } = normalizeTarget(target, "messages");
|
|
57
90
|
const scoped = require_inject_projection.injectProjection((0, _angular_core.computed)(() => isRootSignal() ? null : require_use_stream.getRegistry(stream)), () => (0, _langchain_langgraph_sdk_stream.messagesProjection)(namespace()), key, EMPTY_MESSAGES);
|
|
58
91
|
return (0, _angular_core.computed)(() => isRootSignal() ? stream.messages() : scoped());
|
|
59
92
|
}
|
|
60
93
|
const EMPTY_MESSAGES = [];
|
|
61
94
|
function injectToolCalls(stream, target) {
|
|
95
|
+
resolveSubagentNamespaceFor(stream, target);
|
|
62
96
|
const { namespace, key, isRootSignal } = normalizeTarget(target, "toolCalls");
|
|
63
97
|
const scoped = require_inject_projection.injectProjection((0, _angular_core.computed)(() => isRootSignal() ? null : require_use_stream.getRegistry(stream)), () => (0, _langchain_langgraph_sdk_stream.toolCallsProjection)(namespace()), key, EMPTY_TOOLCALLS);
|
|
64
98
|
return (0, _angular_core.computed)(() => isRootSignal() ? stream.toolCalls() : scoped());
|
package/dist/selectors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectors.cjs","names":["NAMESPACE_SEPARATOR","injectProjection","getRegistry"],"sources":["../src/selectors.ts"],"sourcesContent":["import { computed, isSignal, type Signal } from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport {\n NAMESPACE_SEPARATOR,\n audioProjection,\n channelProjection,\n extensionProjection,\n filesProjection,\n imagesProjection,\n messagesProjection,\n toolCallsProjection,\n valuesProjection,\n videoProjection,\n type AssembledToolCall,\n type AudioMedia,\n type Channel,\n type ChannelProjectionOptions,\n type Event,\n type FileMedia,\n type ImageMedia,\n type InferToolCalls,\n type InferStateType,\n type SubagentDiscoverySnapshot,\n type SubgraphDiscoverySnapshot,\n type VideoMedia,\n} from \"@langchain/langgraph-sdk/stream\";\nimport {\n getRegistry,\n type AnyStream,\n type UseStreamReturn,\n} from \"./use-stream.js\";\nimport { injectProjection } from \"./inject-projection.js\";\n\n/**\n * Selector primitives don't need to carry `InterruptType` /\n * `ConfigurableType`. Parameterising on `StateType` alone lets\n * callers with a full `injectStream<S, I, C>()` handle pass it in\n * without redeclaring those generics at every call site.\n */\ntype StreamHandle<StateType extends object> = UseStreamReturn<\n StateType,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\n/**\n * What a selector primitive can be targeted at. Callers can pass:\n * - `undefined` / `null` — root namespace (served by the always-on\n * root store — no extra subscription);\n * - a {@link SubagentDiscoverySnapshot} (`stream.subagents().get(...)`);\n * - a {@link SubgraphDiscoverySnapshot} (`stream.subgraphs().get(...)`);\n * - an explicit `{ namespace: string[] }`;\n * - a raw `string[]` escape hatch.\n *\n * Selectors also accept a `Signal<SelectorTarget>` so callers can\n * feed a `computed(() => ...)` and have the projection rebind\n * automatically.\n */\nexport type SelectorTarget =\n | undefined\n | null\n | readonly string[]\n | { namespace: readonly string[] }\n | SubagentDiscoverySnapshot\n | SubgraphDiscoverySnapshot;\n\nconst EMPTY_NAMESPACE: readonly string[] = [];\n\nfunction resolveNamespace(target: SelectorTarget): readonly string[] {\n if (target == null) return EMPTY_NAMESPACE;\n if (Array.isArray(target)) return target as readonly string[];\n const obj = target as { namespace?: readonly string[] };\n return obj.namespace ?? EMPTY_NAMESPACE;\n}\n\nfunction isRoot(namespace: readonly string[]): boolean {\n return namespace.length === 0;\n}\n\nfunction namespaceKey(namespace: readonly string[]): string {\n return namespace.join(NAMESPACE_SEPARATOR);\n}\n\n/**\n * Resolve a target or target-signal into a reactive\n * `[namespace, key]` pair. The helper returns plain signals so\n * `injectProjection` can track them via its internal effect.\n */\nfunction normalizeTarget(\n target: SelectorTarget | Signal<SelectorTarget>,\n prefix: string\n): {\n namespace: Signal<readonly string[]>;\n key: Signal<string>;\n isRootSignal: Signal<boolean>;\n} {\n if (isSignal(target)) {\n const namespace = computed(() =>\n resolveNamespace((target as Signal<SelectorTarget>)())\n );\n const key = computed(() => `${prefix}|${namespaceKey(namespace())}`);\n const isRootSignal = computed(() => isRoot(namespace()));\n return { namespace, key, isRootSignal };\n }\n const ns = resolveNamespace(target as SelectorTarget);\n const staticKey = `${prefix}|${namespaceKey(ns)}`;\n return {\n namespace: computed(() => ns),\n key: computed(() => staticKey),\n isRootSignal: computed(() => isRoot(ns)),\n };\n}\n\n/**\n * Subscribe to a scoped `messages` stream.\n *\n * Contract:\n * - At the root (no `target`) this returns `stream.messages` — the\n * always-on root projection; no extra subscription is opened.\n * - For any non-root namespace, call-time triggers a ref-counted\n * `messages` subscription scoped to that namespace. The\n * subscription is released automatically when the calling\n * component / service is destroyed (and the registry closes the\n * underlying server subscription when the last consumer leaves).\n *\n * Messages are always `BaseMessage` class instances from\n * `@langchain/core/messages`.\n */\nexport function injectMessages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<BaseMessage[]> {\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"messages\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<BaseMessage[]>(\n registry,\n () => messagesProjection(namespace()),\n key,\n EMPTY_MESSAGES\n );\n return computed(() => (isRootSignal() ? stream.messages() : scoped()));\n}\n\nconst EMPTY_MESSAGES: BaseMessage[] = [];\n\n/**\n * Subscribe to a scoped `tools` (tool-call) stream. Same target and\n * lifecycle rules as {@link injectMessages}; at the root this returns\n * `stream.toolCalls` directly.\n */\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]>;\nexport function injectToolCalls<T>(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<InferToolCalls<T>[]>;\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]> {\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"toolCalls\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<AssembledToolCall[]>(\n registry,\n () => toolCallsProjection(namespace()),\n key,\n EMPTY_TOOLCALLS\n );\n return computed(() => (isRootSignal() ? stream.toolCalls() : scoped()));\n}\n\nconst EMPTY_TOOLCALLS: AssembledToolCall[] = [];\n\n/**\n * Subscribe to a scoped `values` stream — the most recent state\n * payload for a namespace. At the root returns `stream.values`.\n *\n * Typing:\n * - **Root** (`injectValues(stream)`): returns the `StateType`\n * declared on `injectStream<State>()` — non-nullable (the root\n * snapshot always has values, falling back to `initialValues ??\n * {}`).\n * - **Scoped** (`injectValues(stream, target)`): scoped payloads can\n * differ from the root state; callers should annotate the expected\n * shape explicitly (`injectValues<SubagentState>(stream, sub)`).\n * Defaults to `unknown` when not annotated.\n */\nexport function injectValues<StateType extends object>(\n stream: StreamHandle<StateType>\n): Signal<StateType>;\nexport function injectValues<T>(stream: AnyStream): Signal<InferStateType<T>>;\nexport function injectValues<T = unknown>(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<T | undefined>;\nexport function injectValues(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<unknown> {\n const messagesKey = options?.messagesKey ?? \"messages\";\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `values|${messagesKey}`\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<unknown>(\n registry,\n () => valuesProjection<unknown>(namespace(), messagesKey),\n key,\n undefined\n );\n return computed(() => (isRootSignal() ? stream.values() : scoped()));\n}\n\n/**\n * Subscribe to a `custom:<name>` stream extension — the most recent\n * payload emitted by the transformer, scoped to the target namespace.\n */\nexport function injectExtension<T = unknown>(\n stream: AnyStream,\n name: string,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<T | undefined> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `extension|${name}`\n );\n return injectProjection<T | undefined>(\n getRegistry(stream),\n () => extensionProjection<T>(name, namespace()),\n key,\n undefined\n );\n}\n\n/**\n * Raw-events escape hatch. Subscribes to one or more channels at a\n * namespace and returns a bounded buffer of raw protocol events.\n * Prefer {@link injectMessages} / {@link injectToolCalls} /\n * {@link injectValues} for the common cases.\n */\nexport type InjectChannelOptions = ChannelProjectionOptions;\n\nexport function injectChannel(\n stream: AnyStream,\n channels: readonly Channel[],\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: InjectChannelOptions\n): Signal<Event[]> {\n const sortedChannels = [...channels].sort().join(\",\");\n const prefix = `channel|${options?.bufferSize ?? \"default\"}|${(options?.replay ?? true) ? \"replay\" : \"live\"}|${sortedChannels}`;\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n prefix\n );\n return injectProjection<Event[]>(\n getRegistry(stream),\n () => channelProjection(channels, namespace(), options),\n key,\n EMPTY_EVENTS\n );\n}\n\nconst EMPTY_EVENTS: Event[] = [];\n\n/**\n * Subscribe to a scoped audio-media stream. Each handle is yielded\n * on its first matching `content-block-start`, exposes\n * `.partialBytes` for live access, settles `.blob` / `.objectURL` /\n * `.transcript` on `message-finish`, and surfaces errors via\n * `.error`.\n *\n * Pair with `injectMediaUrl` to turn a handle into an `<audio src>`.\n */\nexport function injectAudio(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AudioMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"audio\"\n );\n return injectProjection<AudioMedia[]>(\n getRegistry(stream),\n () => audioProjection(namespace()),\n key,\n EMPTY_AUDIO\n );\n}\n\nconst EMPTY_AUDIO: AudioMedia[] = [];\n\n/**\n * Subscribe to a scoped image-media stream. Pair with\n * `injectMediaUrl` for `<img src>`.\n */\nexport function injectImages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<ImageMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"images\"\n );\n return injectProjection<ImageMedia[]>(\n getRegistry(stream),\n () => imagesProjection(namespace()),\n key,\n EMPTY_IMAGES\n );\n}\n\nconst EMPTY_IMAGES: ImageMedia[] = [];\n\n/**\n * Subscribe to a scoped video-media stream. Pair with\n * `injectMediaUrl` for `<video src>`.\n */\nexport function injectVideo(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<VideoMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"video\"\n );\n return injectProjection<VideoMedia[]>(\n getRegistry(stream),\n () => videoProjection(namespace()),\n key,\n EMPTY_VIDEO\n );\n}\n\nconst EMPTY_VIDEO: VideoMedia[] = [];\n\n/**\n * Subscribe to a scoped file-media stream. Pair with\n * `injectMediaUrl` for an `<a download href>` target.\n */\nexport function injectFiles(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<FileMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"files\"\n );\n return injectProjection<FileMedia[]>(\n getRegistry(stream),\n () => filesProjection(namespace()),\n key,\n EMPTY_FILES\n );\n}\n\nconst EMPTY_FILES: FileMedia[] = [];\n"],"mappings":";;;;;AAoEA,MAAM,kBAAqC,EAAE;AAE7C,SAAS,iBAAiB,QAA2C;AACnE,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,MAAM,QAAQ,OAAO,CAAE,QAAO;AAElC,QADY,OACD,aAAa;;AAG1B,SAAS,OAAO,WAAuC;AACrD,QAAO,UAAU,WAAW;;AAG9B,SAAS,aAAa,WAAsC;AAC1D,QAAO,UAAU,KAAKA,gCAAAA,oBAAoB;;;;;;;AAQ5C,SAAS,gBACP,QACA,QAKA;AACA,MAAA,GAAA,cAAA,UAAa,OAAO,EAAE;EACpB,MAAM,aAAA,GAAA,cAAA,gBACJ,iBAAkB,QAAmC,CAAC,CACvD;AAGD,SAAO;GAAE;GAAW,MAAA,GAAA,cAAA,gBAFO,GAAG,OAAO,GAAG,aAAa,WAAW,CAAC,GAAG;GAE3C,eAAA,GAAA,cAAA,gBADW,OAAO,WAAW,CAAC,CAAC;GACjB;;CAEzC,MAAM,KAAK,iBAAiB,OAAyB;CACrD,MAAM,YAAY,GAAG,OAAO,GAAG,aAAa,GAAG;AAC/C,QAAO;EACL,YAAA,GAAA,cAAA,gBAA0B,GAAG;EAC7B,MAAA,GAAA,cAAA,gBAAoB,UAAU;EAC9B,eAAA,GAAA,cAAA,gBAA6B,OAAO,GAAG,CAAC;EACzC;;;;;;;;;;;;;;;;;AAkBH,SAAgB,eACd,QACA,QACuB;CACvB,MAAM,EAAE,WAAW,KAAK,iBAAiB,gBACvC,QACA,WACD;CAID,MAAM,SAASC,0BAAAA,kBAAAA,GAAAA,cAAAA,gBAFb,cAAc,GAAG,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AAI;CAErD,MAAM,EAAE,WAAW,QAAQ,gBACzB,QAFa,WAAW,SAAS,cAAc,UAAU,GAAI,SAAS,UAAU,OAAQ,WAAW,OAAO,GAAG,iBAI9G;AACD,QAAOD,0BAAAA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,mBACK,UAAU,WAAW,EAAE,QAAQ,EACvD,KACA,aACD;;AAGH,MAAM,eAAwB,EAAE;;;;;;;;;;AAWhC,SAAgB,YACd,QACA,QACsB;CACtB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAOD,0BAAAA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,iBACG,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA4B,EAAE;;;;;AAMpC,SAAgB,aACd,QACA,QACsB;CACtB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,SACD;AACD,QAAOD,0BAAAA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,kBACI,WAAW,CAAC,EACnC,KACA,aACD;;AAGH,MAAM,eAA6B,EAAE;;;;;AAMrC,SAAgB,YACd,QACA,QACsB;CACtB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAOD,0BAAAA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,iBACG,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA4B,EAAE;;;;;AAMpC,SAAgB,YACd,QACA,QACqB;CACrB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAOD,0BAAAA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,iBACG,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA2B,EAAE"}
|
|
1
|
+
{"version":3,"file":"selectors.cjs","names":["STREAM_CONTROLLER","NAMESPACE_SEPARATOR","injectProjection","getRegistry"],"sources":["../src/selectors.ts"],"sourcesContent":["import { computed, effect, isSignal, type Signal } from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport {\n NAMESPACE_SEPARATOR,\n audioProjection,\n channelProjection,\n extensionProjection,\n filesProjection,\n imagesProjection,\n messagesProjection,\n toolCallsProjection,\n valuesProjection,\n videoProjection,\n type AssembledToolCall,\n type AudioMedia,\n type Channel,\n type ChannelProjectionOptions,\n type Event,\n type FileMedia,\n type ImageMedia,\n type InferToolCalls,\n type InferStateType,\n type SubagentDiscoverySnapshot,\n type SubgraphDiscoverySnapshot,\n type VideoMedia,\n} from \"@langchain/langgraph-sdk/stream\";\nimport {\n getRegistry,\n STREAM_CONTROLLER,\n type AnyStream,\n type UseStreamReturn,\n} from \"./use-stream.js\";\nimport { injectProjection } from \"./inject-projection.js\";\n\n/**\n * Selector primitives don't need to carry `InterruptType` /\n * `ConfigurableType`. Parameterising on `StateType` alone lets\n * callers with a full `injectStream<S, I, C>()` handle pass it in\n * without redeclaring those generics at every call site.\n */\ntype StreamHandle<StateType extends object> = UseStreamReturn<\n StateType,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\n/**\n * What a selector primitive can be targeted at. Callers can pass:\n * - `undefined` / `null` — root namespace (served by the always-on\n * root store — no extra subscription);\n * - a {@link SubagentDiscoverySnapshot} (`stream.subagents().get(...)`);\n * - a {@link SubgraphDiscoverySnapshot} (`stream.subgraphs().get(...)`);\n * - an explicit `{ namespace: string[] }`;\n * - a raw `string[]` escape hatch.\n *\n * Selectors also accept a `Signal<SelectorTarget>` so callers can\n * feed a `computed(() => ...)` and have the projection rebind\n * automatically.\n */\nexport type SelectorTarget =\n | undefined\n | null\n | readonly string[]\n | { namespace: readonly string[] }\n | SubagentDiscoverySnapshot\n | SubgraphDiscoverySnapshot;\n\nconst EMPTY_NAMESPACE: readonly string[] = [];\n\nfunction resolveNamespace(target: SelectorTarget): readonly string[] {\n if (target == null) return EMPTY_NAMESPACE;\n if (Array.isArray(target)) return target as readonly string[];\n const obj = target as { namespace?: readonly string[] };\n return obj.namespace ?? EMPTY_NAMESPACE;\n}\n\nfunction isRoot(namespace: readonly string[]): boolean {\n return namespace.length === 0;\n}\n\n/**\n * If `target` is a subagent snapshot still on its default\n * `tools:<toolCallId>` namespace, return that tool-call id. See the\n * React selectors for the rationale (deep-agent subagents execute under\n * a distinct `tools:<uuid>` namespace resolved lazily from history).\n */\nfunction subagentNeedingNamespace(target: SelectorTarget): string | null {\n if (target == null || Array.isArray(target)) return null;\n const obj = target as { id?: unknown; namespace?: readonly string[] };\n if (typeof obj.id !== \"string\" || !Array.isArray(obj.namespace)) return null;\n if (obj.namespace.length === 1 && obj.namespace[0] === `tools:${obj.id}`) {\n return obj.id;\n }\n return null;\n}\n\n/**\n * Lazily resolve a subagent's execution namespace on first scoped use.\n * Must be called from an injection context (as the selector primitives\n * are). A `Signal` target re-evaluates via an `effect`; a static target\n * resolves once. The controller de-dupes and skips already-promoted\n * ids.\n */\nfunction resolveSubagentNamespaceFor(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>\n): void {\n const controller = stream[STREAM_CONTROLLER];\n if (isSignal(target)) {\n effect(() => {\n const id = subagentNeedingNamespace((target as Signal<SelectorTarget>)());\n if (id != null) void controller.resolveSubagentNamespace(id);\n });\n return;\n }\n const id = subagentNeedingNamespace(target as SelectorTarget);\n if (id != null) void controller.resolveSubagentNamespace(id);\n}\n\nfunction namespaceKey(namespace: readonly string[]): string {\n return namespace.join(NAMESPACE_SEPARATOR);\n}\n\n/**\n * Resolve a target or target-signal into a reactive\n * `[namespace, key]` pair. The helper returns plain signals so\n * `injectProjection` can track them via its internal effect.\n */\nfunction normalizeTarget(\n target: SelectorTarget | Signal<SelectorTarget>,\n prefix: string\n): {\n namespace: Signal<readonly string[]>;\n key: Signal<string>;\n isRootSignal: Signal<boolean>;\n} {\n if (isSignal(target)) {\n const namespace = computed(() =>\n resolveNamespace((target as Signal<SelectorTarget>)())\n );\n const key = computed(() => `${prefix}|${namespaceKey(namespace())}`);\n const isRootSignal = computed(() => isRoot(namespace()));\n return { namespace, key, isRootSignal };\n }\n const ns = resolveNamespace(target as SelectorTarget);\n const staticKey = `${prefix}|${namespaceKey(ns)}`;\n return {\n namespace: computed(() => ns),\n key: computed(() => staticKey),\n isRootSignal: computed(() => isRoot(ns)),\n };\n}\n\n/**\n * Subscribe to a scoped `messages` stream.\n *\n * Contract:\n * - At the root (no `target`) this returns `stream.messages` — the\n * always-on root projection; no extra subscription is opened.\n * - For any non-root namespace, call-time triggers a ref-counted\n * `messages` subscription scoped to that namespace. The\n * subscription is released automatically when the calling\n * component / service is destroyed (and the registry closes the\n * underlying server subscription when the last consumer leaves).\n *\n * Messages are always `BaseMessage` class instances from\n * `@langchain/core/messages`.\n */\nexport function injectMessages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<BaseMessage[]> {\n resolveSubagentNamespaceFor(\n stream,\n target as SelectorTarget | Signal<SelectorTarget>\n );\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"messages\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<BaseMessage[]>(\n registry,\n () => messagesProjection(namespace()),\n key,\n EMPTY_MESSAGES\n );\n return computed(() => (isRootSignal() ? stream.messages() : scoped()));\n}\n\nconst EMPTY_MESSAGES: BaseMessage[] = [];\n\n/**\n * Subscribe to a scoped `tools` (tool-call) stream. Same target and\n * lifecycle rules as {@link injectMessages}; at the root this returns\n * `stream.toolCalls` directly.\n */\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]>;\nexport function injectToolCalls<T>(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<InferToolCalls<T>[]>;\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]> {\n resolveSubagentNamespaceFor(\n stream,\n target as SelectorTarget | Signal<SelectorTarget>\n );\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"toolCalls\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<AssembledToolCall[]>(\n registry,\n () => toolCallsProjection(namespace()),\n key,\n EMPTY_TOOLCALLS\n );\n return computed(() => (isRootSignal() ? stream.toolCalls() : scoped()));\n}\n\nconst EMPTY_TOOLCALLS: AssembledToolCall[] = [];\n\n/**\n * Subscribe to a scoped `values` stream — the most recent state\n * payload for a namespace. At the root returns `stream.values`.\n *\n * Typing:\n * - **Root** (`injectValues(stream)`): returns the `StateType`\n * declared on `injectStream<State>()` — non-nullable (the root\n * snapshot always has values, falling back to `initialValues ??\n * {}`).\n * - **Scoped** (`injectValues(stream, target)`): scoped payloads can\n * differ from the root state; callers should annotate the expected\n * shape explicitly (`injectValues<SubagentState>(stream, sub)`).\n * Defaults to `unknown` when not annotated.\n */\nexport function injectValues<StateType extends object>(\n stream: StreamHandle<StateType>\n): Signal<StateType>;\nexport function injectValues<T>(stream: AnyStream): Signal<InferStateType<T>>;\nexport function injectValues<T = unknown>(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<T | undefined>;\nexport function injectValues(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<unknown> {\n const messagesKey = options?.messagesKey ?? \"messages\";\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `values|${messagesKey}`\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<unknown>(\n registry,\n () => valuesProjection<unknown>(namespace(), messagesKey),\n key,\n undefined\n );\n return computed(() => (isRootSignal() ? stream.values() : scoped()));\n}\n\n/**\n * Subscribe to a `custom:<name>` stream extension — the most recent\n * payload emitted by the transformer, scoped to the target namespace.\n */\nexport function injectExtension<T = unknown>(\n stream: AnyStream,\n name: string,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<T | undefined> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `extension|${name}`\n );\n return injectProjection<T | undefined>(\n getRegistry(stream),\n () => extensionProjection<T>(name, namespace()),\n key,\n undefined\n );\n}\n\n/**\n * Raw-events escape hatch. Subscribes to one or more channels at a\n * namespace and returns a bounded buffer of raw protocol events.\n * Prefer {@link injectMessages} / {@link injectToolCalls} /\n * {@link injectValues} for the common cases.\n */\nexport type InjectChannelOptions = ChannelProjectionOptions;\n\nexport function injectChannel(\n stream: AnyStream,\n channels: readonly Channel[],\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: InjectChannelOptions\n): Signal<Event[]> {\n const sortedChannels = [...channels].sort().join(\",\");\n const prefix = `channel|${options?.bufferSize ?? \"default\"}|${(options?.replay ?? true) ? \"replay\" : \"live\"}|${sortedChannels}`;\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n prefix\n );\n return injectProjection<Event[]>(\n getRegistry(stream),\n () => channelProjection(channels, namespace(), options),\n key,\n EMPTY_EVENTS\n );\n}\n\nconst EMPTY_EVENTS: Event[] = [];\n\n/**\n * Subscribe to a scoped audio-media stream. Each handle is yielded\n * on its first matching `content-block-start`, exposes\n * `.partialBytes` for live access, settles `.blob` / `.objectURL` /\n * `.transcript` on `message-finish`, and surfaces errors via\n * `.error`.\n *\n * Pair with `injectMediaUrl` to turn a handle into an `<audio src>`.\n */\nexport function injectAudio(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AudioMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"audio\"\n );\n return injectProjection<AudioMedia[]>(\n getRegistry(stream),\n () => audioProjection(namespace()),\n key,\n EMPTY_AUDIO\n );\n}\n\nconst EMPTY_AUDIO: AudioMedia[] = [];\n\n/**\n * Subscribe to a scoped image-media stream. Pair with\n * `injectMediaUrl` for `<img src>`.\n */\nexport function injectImages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<ImageMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"images\"\n );\n return injectProjection<ImageMedia[]>(\n getRegistry(stream),\n () => imagesProjection(namespace()),\n key,\n EMPTY_IMAGES\n );\n}\n\nconst EMPTY_IMAGES: ImageMedia[] = [];\n\n/**\n * Subscribe to a scoped video-media stream. Pair with\n * `injectMediaUrl` for `<video src>`.\n */\nexport function injectVideo(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<VideoMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"video\"\n );\n return injectProjection<VideoMedia[]>(\n getRegistry(stream),\n () => videoProjection(namespace()),\n key,\n EMPTY_VIDEO\n );\n}\n\nconst EMPTY_VIDEO: VideoMedia[] = [];\n\n/**\n * Subscribe to a scoped file-media stream. Pair with\n * `injectMediaUrl` for an `<a download href>` target.\n */\nexport function injectFiles(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<FileMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"files\"\n );\n return injectProjection<FileMedia[]>(\n getRegistry(stream),\n () => filesProjection(namespace()),\n key,\n EMPTY_FILES\n );\n}\n\nconst EMPTY_FILES: FileMedia[] = [];\n"],"mappings":";;;;;AAqEA,MAAM,kBAAqC,EAAE;AAE7C,SAAS,iBAAiB,QAA2C;AACnE,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,MAAM,QAAQ,OAAO,CAAE,QAAO;AAElC,QADY,OACD,aAAa;;AAG1B,SA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AA,iBACLC,mBAAAA,YAAY,OAAO,SAAA,GAAA,gCAAA,iBACG,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA2B,EAAE"}
|
package/dist/selectors.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectors.d.cts","names":[],"sources":["../src/selectors.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"selectors.d.cts","names":[],"sources":["../src/selectors.ts"],"mappings":";;;;;;;;AA+ByB;;;;KASpB,YAAA,6BAAyC,eAAA,CAC5C,SAAA;;;;;AAoBF;;;;;;;;;KAAY,cAAA;EAIN,SAAA;AAAA,IACF,yBAAA,GACA,yBAAA;;;;;;;;;;;;;;;;iBAuGY,cAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,WAAA;;;AA4BV;;;iBAAgB,eAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,iBAAA;AAAA,iBACM,eAAA,GAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,cAAA,CAAe,CAAA;;;;;;;;;;;;;;;iBAyCT,YAAA,0BAAA,CACd,MAAA,EAAQ,YAAA,CAAa,SAAA,IACpB,MAAA,CAAO,SAAA;AAAA,iBACM,YAAA,GAAA,CAAgB,MAAA,EAAQ,SAAA,GAAY,MAAA,CAAO,cAAA,CAAe,CAAA;AAAA,iBAC1D,YAAA,aAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,EAAQ,cAAA,GAAiB,MAAA,CAAO,cAAA,GAChC,OAAA;EAAY,WAAA;AAAA,IACX,MAAA,CAAO,CAAA;;;;;iBA2BM,eAAA,aAAA,CACd,MAAA,EAAQ,SAAA,EACR,IAAA,UACA,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,CAAA;;;;;;;KAmBE,oBAAA,GAAuB,wBAAA;AAAA,iBAEnB,aAAA,CACd,MAAA,EAAQ,SAAA,EACR,QAAA,WAAmB,OAAA,IACnB,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,GACjC,OAAA,GAAU,oBAAA,GACT,MAAA,CAAO,KAAA;;;;;;;;;AAjEV;iBA2FgB,WAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,UAAA;;;;;iBAmBM,YAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,UAAA;;;;;iBAmBM,WAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,UAAA;;;;;iBAmBM,WAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,SAAA"}
|
package/dist/selectors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectors.d.ts","names":[],"sources":["../src/selectors.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"selectors.d.ts","names":[],"sources":["../src/selectors.ts"],"mappings":";;;;;;;;AA+ByB;;;;KASpB,YAAA,6BAAyC,eAAA,CAC5C,SAAA;;;;;AAoBF;;;;;;;;;KAAY,cAAA;EAIN,SAAA;AAAA,IACF,yBAAA,GACA,yBAAA;;;;;;;;;;;;;;;;iBAuGY,cAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,WAAA;;;AA4BV;;;iBAAgB,eAAA,CACd,MAA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ChC,MAAA,CAAO,UAAA;;;;;iBAmBM,WAAA,CACd,MAAA,EAAQ,SAAA,EACR,MAAA,GAAS,cAAA,GAAiB,MAAA,CAAO,cAAA,IAChC,MAAA,CAAO,SAAA"}
|
package/dist/selectors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getRegistry } from "./use-stream.js";
|
|
1
|
+
import { STREAM_CONTROLLER, getRegistry } from "./use-stream.js";
|
|
2
2
|
import { injectProjection } from "./inject-projection.js";
|
|
3
|
-
import { computed, isSignal } from "@angular/core";
|
|
3
|
+
import { computed, effect, isSignal } from "@angular/core";
|
|
4
4
|
import { NAMESPACE_SEPARATOR, audioProjection, channelProjection, extensionProjection, filesProjection, imagesProjection, messagesProjection, toolCallsProjection, valuesProjection, videoProjection } from "@langchain/langgraph-sdk/stream";
|
|
5
5
|
//#region src/selectors.ts
|
|
6
6
|
const EMPTY_NAMESPACE = [];
|
|
@@ -12,6 +12,38 @@ function resolveNamespace(target) {
|
|
|
12
12
|
function isRoot(namespace) {
|
|
13
13
|
return namespace.length === 0;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* If `target` is a subagent snapshot still on its default
|
|
17
|
+
* `tools:<toolCallId>` namespace, return that tool-call id. See the
|
|
18
|
+
* React selectors for the rationale (deep-agent subagents execute under
|
|
19
|
+
* a distinct `tools:<uuid>` namespace resolved lazily from history).
|
|
20
|
+
*/
|
|
21
|
+
function subagentNeedingNamespace(target) {
|
|
22
|
+
if (target == null || Array.isArray(target)) return null;
|
|
23
|
+
const obj = target;
|
|
24
|
+
if (typeof obj.id !== "string" || !Array.isArray(obj.namespace)) return null;
|
|
25
|
+
if (obj.namespace.length === 1 && obj.namespace[0] === `tools:${obj.id}`) return obj.id;
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Lazily resolve a subagent's execution namespace on first scoped use.
|
|
30
|
+
* Must be called from an injection context (as the selector primitives
|
|
31
|
+
* are). A `Signal` target re-evaluates via an `effect`; a static target
|
|
32
|
+
* resolves once. The controller de-dupes and skips already-promoted
|
|
33
|
+
* ids.
|
|
34
|
+
*/
|
|
35
|
+
function resolveSubagentNamespaceFor(stream, target) {
|
|
36
|
+
const controller = stream[STREAM_CONTROLLER];
|
|
37
|
+
if (isSignal(target)) {
|
|
38
|
+
effect(() => {
|
|
39
|
+
const id = subagentNeedingNamespace(target());
|
|
40
|
+
if (id != null) controller.resolveSubagentNamespace(id);
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const id = subagentNeedingNamespace(target);
|
|
45
|
+
if (id != null) controller.resolveSubagentNamespace(id);
|
|
46
|
+
}
|
|
15
47
|
function namespaceKey(namespace) {
|
|
16
48
|
return namespace.join(NAMESPACE_SEPARATOR);
|
|
17
49
|
}
|
|
@@ -53,12 +85,14 @@ function normalizeTarget(target, prefix) {
|
|
|
53
85
|
* `@langchain/core/messages`.
|
|
54
86
|
*/
|
|
55
87
|
function injectMessages(stream, target) {
|
|
88
|
+
resolveSubagentNamespaceFor(stream, target);
|
|
56
89
|
const { namespace, key, isRootSignal } = normalizeTarget(target, "messages");
|
|
57
90
|
const scoped = injectProjection(computed(() => isRootSignal() ? null : getRegistry(stream)), () => messagesProjection(namespace()), key, EMPTY_MESSAGES);
|
|
58
91
|
return computed(() => isRootSignal() ? stream.messages() : scoped());
|
|
59
92
|
}
|
|
60
93
|
const EMPTY_MESSAGES = [];
|
|
61
94
|
function injectToolCalls(stream, target) {
|
|
95
|
+
resolveSubagentNamespaceFor(stream, target);
|
|
62
96
|
const { namespace, key, isRootSignal } = normalizeTarget(target, "toolCalls");
|
|
63
97
|
const scoped = injectProjection(computed(() => isRootSignal() ? null : getRegistry(stream)), () => toolCallsProjection(namespace()), key, EMPTY_TOOLCALLS);
|
|
64
98
|
return computed(() => isRootSignal() ? stream.toolCalls() : scoped());
|
package/dist/selectors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectors.js","names":[],"sources":["../src/selectors.ts"],"sourcesContent":["import { computed, isSignal, type Signal } from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport {\n NAMESPACE_SEPARATOR,\n audioProjection,\n channelProjection,\n extensionProjection,\n filesProjection,\n imagesProjection,\n messagesProjection,\n toolCallsProjection,\n valuesProjection,\n videoProjection,\n type AssembledToolCall,\n type AudioMedia,\n type Channel,\n type ChannelProjectionOptions,\n type Event,\n type FileMedia,\n type ImageMedia,\n type InferToolCalls,\n type InferStateType,\n type SubagentDiscoverySnapshot,\n type SubgraphDiscoverySnapshot,\n type VideoMedia,\n} from \"@langchain/langgraph-sdk/stream\";\nimport {\n getRegistry,\n type AnyStream,\n type UseStreamReturn,\n} from \"./use-stream.js\";\nimport { injectProjection } from \"./inject-projection.js\";\n\n/**\n * Selector primitives don't need to carry `InterruptType` /\n * `ConfigurableType`. Parameterising on `StateType` alone lets\n * callers with a full `injectStream<S, I, C>()` handle pass it in\n * without redeclaring those generics at every call site.\n */\ntype StreamHandle<StateType extends object> = UseStreamReturn<\n StateType,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\n/**\n * What a selector primitive can be targeted at. Callers can pass:\n * - `undefined` / `null` — root namespace (served by the always-on\n * root store — no extra subscription);\n * - a {@link SubagentDiscoverySnapshot} (`stream.subagents().get(...)`);\n * - a {@link SubgraphDiscoverySnapshot} (`stream.subgraphs().get(...)`);\n * - an explicit `{ namespace: string[] }`;\n * - a raw `string[]` escape hatch.\n *\n * Selectors also accept a `Signal<SelectorTarget>` so callers can\n * feed a `computed(() => ...)` and have the projection rebind\n * automatically.\n */\nexport type SelectorTarget =\n | undefined\n | null\n | readonly string[]\n | { namespace: readonly string[] }\n | SubagentDiscoverySnapshot\n | SubgraphDiscoverySnapshot;\n\nconst EMPTY_NAMESPACE: readonly string[] = [];\n\nfunction resolveNamespace(target: SelectorTarget): readonly string[] {\n if (target == null) return EMPTY_NAMESPACE;\n if (Array.isArray(target)) return target as readonly string[];\n const obj = target as { namespace?: readonly string[] };\n return obj.namespace ?? EMPTY_NAMESPACE;\n}\n\nfunction isRoot(namespace: readonly string[]): boolean {\n return namespace.length === 0;\n}\n\nfunction namespaceKey(namespace: readonly string[]): string {\n return namespace.join(NAMESPACE_SEPARATOR);\n}\n\n/**\n * Resolve a target or target-signal into a reactive\n * `[namespace, key]` pair. The helper returns plain signals so\n * `injectProjection` can track them via its internal effect.\n */\nfunction normalizeTarget(\n target: SelectorTarget | Signal<SelectorTarget>,\n prefix: string\n): {\n namespace: Signal<readonly string[]>;\n key: Signal<string>;\n isRootSignal: Signal<boolean>;\n} {\n if (isSignal(target)) {\n const namespace = computed(() =>\n resolveNamespace((target as Signal<SelectorTarget>)())\n );\n const key = computed(() => `${prefix}|${namespaceKey(namespace())}`);\n const isRootSignal = computed(() => isRoot(namespace()));\n return { namespace, key, isRootSignal };\n }\n const ns = resolveNamespace(target as SelectorTarget);\n const staticKey = `${prefix}|${namespaceKey(ns)}`;\n return {\n namespace: computed(() => ns),\n key: computed(() => staticKey),\n isRootSignal: computed(() => isRoot(ns)),\n };\n}\n\n/**\n * Subscribe to a scoped `messages` stream.\n *\n * Contract:\n * - At the root (no `target`) this returns `stream.messages` — the\n * always-on root projection; no extra subscription is opened.\n * - For any non-root namespace, call-time triggers a ref-counted\n * `messages` subscription scoped to that namespace. The\n * subscription is released automatically when the calling\n * component / service is destroyed (and the registry closes the\n * underlying server subscription when the last consumer leaves).\n *\n * Messages are always `BaseMessage` class instances from\n * `@langchain/core/messages`.\n */\nexport function injectMessages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<BaseMessage[]> {\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"messages\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<BaseMessage[]>(\n registry,\n () => messagesProjection(namespace()),\n key,\n EMPTY_MESSAGES\n );\n return computed(() => (isRootSignal() ? stream.messages() : scoped()));\n}\n\nconst EMPTY_MESSAGES: BaseMessage[] = [];\n\n/**\n * Subscribe to a scoped `tools` (tool-call) stream. Same target and\n * lifecycle rules as {@link injectMessages}; at the root this returns\n * `stream.toolCalls` directly.\n */\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]>;\nexport function injectToolCalls<T>(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<InferToolCalls<T>[]>;\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]> {\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"toolCalls\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<AssembledToolCall[]>(\n registry,\n () => toolCallsProjection(namespace()),\n key,\n EMPTY_TOOLCALLS\n );\n return computed(() => (isRootSignal() ? stream.toolCalls() : scoped()));\n}\n\nconst EMPTY_TOOLCALLS: AssembledToolCall[] = [];\n\n/**\n * Subscribe to a scoped `values` stream — the most recent state\n * payload for a namespace. At the root returns `stream.values`.\n *\n * Typing:\n * - **Root** (`injectValues(stream)`): returns the `StateType`\n * declared on `injectStream<State>()` — non-nullable (the root\n * snapshot always has values, falling back to `initialValues ??\n * {}`).\n * - **Scoped** (`injectValues(stream, target)`): scoped payloads can\n * differ from the root state; callers should annotate the expected\n * shape explicitly (`injectValues<SubagentState>(stream, sub)`).\n * Defaults to `unknown` when not annotated.\n */\nexport function injectValues<StateType extends object>(\n stream: StreamHandle<StateType>\n): Signal<StateType>;\nexport function injectValues<T>(stream: AnyStream): Signal<InferStateType<T>>;\nexport function injectValues<T = unknown>(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<T | undefined>;\nexport function injectValues(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<unknown> {\n const messagesKey = options?.messagesKey ?? \"messages\";\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `values|${messagesKey}`\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<unknown>(\n registry,\n () => valuesProjection<unknown>(namespace(), messagesKey),\n key,\n undefined\n );\n return computed(() => (isRootSignal() ? stream.values() : scoped()));\n}\n\n/**\n * Subscribe to a `custom:<name>` stream extension — the most recent\n * payload emitted by the transformer, scoped to the target namespace.\n */\nexport function injectExtension<T = unknown>(\n stream: AnyStream,\n name: string,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<T | undefined> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `extension|${name}`\n );\n return injectProjection<T | undefined>(\n getRegistry(stream),\n () => extensionProjection<T>(name, namespace()),\n key,\n undefined\n );\n}\n\n/**\n * Raw-events escape hatch. Subscribes to one or more channels at a\n * namespace and returns a bounded buffer of raw protocol events.\n * Prefer {@link injectMessages} / {@link injectToolCalls} /\n * {@link injectValues} for the common cases.\n */\nexport type InjectChannelOptions = ChannelProjectionOptions;\n\nexport function injectChannel(\n stream: AnyStream,\n channels: readonly Channel[],\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: InjectChannelOptions\n): Signal<Event[]> {\n const sortedChannels = [...channels].sort().join(\",\");\n const prefix = `channel|${options?.bufferSize ?? \"default\"}|${(options?.replay ?? true) ? \"replay\" : \"live\"}|${sortedChannels}`;\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n prefix\n );\n return injectProjection<Event[]>(\n getRegistry(stream),\n () => channelProjection(channels, namespace(), options),\n key,\n EMPTY_EVENTS\n );\n}\n\nconst EMPTY_EVENTS: Event[] = [];\n\n/**\n * Subscribe to a scoped audio-media stream. Each handle is yielded\n * on its first matching `content-block-start`, exposes\n * `.partialBytes` for live access, settles `.blob` / `.objectURL` /\n * `.transcript` on `message-finish`, and surfaces errors via\n * `.error`.\n *\n * Pair with `injectMediaUrl` to turn a handle into an `<audio src>`.\n */\nexport function injectAudio(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AudioMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"audio\"\n );\n return injectProjection<AudioMedia[]>(\n getRegistry(stream),\n () => audioProjection(namespace()),\n key,\n EMPTY_AUDIO\n );\n}\n\nconst EMPTY_AUDIO: AudioMedia[] = [];\n\n/**\n * Subscribe to a scoped image-media stream. Pair with\n * `injectMediaUrl` for `<img src>`.\n */\nexport function injectImages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<ImageMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"images\"\n );\n return injectProjection<ImageMedia[]>(\n getRegistry(stream),\n () => imagesProjection(namespace()),\n key,\n EMPTY_IMAGES\n );\n}\n\nconst EMPTY_IMAGES: ImageMedia[] = [];\n\n/**\n * Subscribe to a scoped video-media stream. Pair with\n * `injectMediaUrl` for `<video src>`.\n */\nexport function injectVideo(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<VideoMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"video\"\n );\n return injectProjection<VideoMedia[]>(\n getRegistry(stream),\n () => videoProjection(namespace()),\n key,\n EMPTY_VIDEO\n );\n}\n\nconst EMPTY_VIDEO: VideoMedia[] = [];\n\n/**\n * Subscribe to a scoped file-media stream. Pair with\n * `injectMediaUrl` for an `<a download href>` target.\n */\nexport function injectFiles(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<FileMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"files\"\n );\n return injectProjection<FileMedia[]>(\n getRegistry(stream),\n () => filesProjection(namespace()),\n key,\n EMPTY_FILES\n );\n}\n\nconst EMPTY_FILES: FileMedia[] = [];\n"],"mappings":";;;;;AAoEA,MAAM,kBAAqC,EAAE;AAE7C,SAAS,iBAAiB,QAA2C;AACnE,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,MAAM,QAAQ,OAAO,CAAE,QAAO;AAElC,QADY,OACD,aAAa;;AAG1B,SAAS,OAAO,WAAuC;AACrD,QAAO,UAAU,WAAW;;AAG9B,SAAS,aAAa,WAAsC;AAC1D,QAAO,UAAU,KAAK,oBAAoB;;;;;;;AAQ5C,SAAS,gBACP,QACA,QAKA;AACA,KAAI,SAAS,OAAO,EAAE;EACpB,MAAM,YAAY,eAChB,iBAAkB,QAAmC,CAAC,CACvD;AAGD,SAAO;GAAE;GAAW,KAFR,eAAe,GAAG,OAAO,GAAG,aAAa,WAAW,CAAC,GAAG;GAE3C,cADJ,eAAe,OAAO,WAAW,CAAC,CAAC;GACjB;;CAEzC,MAAM,KAAK,iBAAiB,OAAyB;CACrD,MAAM,YAAY,GAAG,OAAO,GAAG,aAAa,GAAG;AAC/C,QAAO;EACL,WAAW,eAAe,GAAG;EAC7B,KAAK,eAAe,UAAU;EAC9B,cAAc,eAAe,OAAO,GAAG,CAAC;EACzC;;;;;;;;;;;;;;;;;AAkBH,SAAgB,eACd,QACA,QACuB;CACvB,MAAM,EAAE,WAAW,KAAK,iBAAiB,gBACvC,QACA,WACD;CAID,MAAM,SAAS,iBAHE,eACf,cAAc,GAAG,OAAO,YAAY,OAAO,CAC5C,QAGO,mBAAmB,WAAW,CAAC,EACrC,KACA,eACD;AACD,QAAO,eAAgB,cAAc,GAAG,OAAO,UAAU,GAAG,QAAQ,CAAE;;AAGxE,MAAM,iBAAgC,EAAE;AAexC,SA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BAAgB,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA4B,EAAE;;;;;AAMpC,SAAgB,aACd,QACA,QACsB;CACtB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,SACD;AACD,QAAO,iBACL,YAAY,OAAO,QACb,iBAAiB,WAAW,CAAC,EACnC,KACA,aACD;;AAGH,MAAM,eAA6B,EAAE;;;;;AAMrC,SAAgB,YACd,QACA,QACsB;CACtB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAO,iBACL,YAAY,OAAO,QACb,gBAAgB,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA4B,EAAE;;;;;AAMpC,SAAgB,YACd,QACA,QACqB;CACrB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAO,iBACL,YAAY,OAAO,QACb,gBAAgB,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA2B,EAAE"}
|
|
1
|
+
{"version":3,"file":"selectors.js","names":[],"sources":["../src/selectors.ts"],"sourcesContent":["import { computed, effect, isSignal, type Signal } from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport {\n NAMESPACE_SEPARATOR,\n audioProjection,\n channelProjection,\n extensionProjection,\n filesProjection,\n imagesProjection,\n messagesProjection,\n toolCallsProjection,\n valuesProjection,\n videoProjection,\n type AssembledToolCall,\n type AudioMedia,\n type Channel,\n type ChannelProjectionOptions,\n type Event,\n type FileMedia,\n type ImageMedia,\n type InferToolCalls,\n type InferStateType,\n type SubagentDiscoverySnapshot,\n type SubgraphDiscoverySnapshot,\n type VideoMedia,\n} from \"@langchain/langgraph-sdk/stream\";\nimport {\n getRegistry,\n STREAM_CONTROLLER,\n type AnyStream,\n type UseStreamReturn,\n} from \"./use-stream.js\";\nimport { injectProjection } from \"./inject-projection.js\";\n\n/**\n * Selector primitives don't need to carry `InterruptType` /\n * `ConfigurableType`. Parameterising on `StateType` alone lets\n * callers with a full `injectStream<S, I, C>()` handle pass it in\n * without redeclaring those generics at every call site.\n */\ntype StreamHandle<StateType extends object> = UseStreamReturn<\n StateType,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n any\n>;\n\n/**\n * What a selector primitive can be targeted at. Callers can pass:\n * - `undefined` / `null` — root namespace (served by the always-on\n * root store — no extra subscription);\n * - a {@link SubagentDiscoverySnapshot} (`stream.subagents().get(...)`);\n * - a {@link SubgraphDiscoverySnapshot} (`stream.subgraphs().get(...)`);\n * - an explicit `{ namespace: string[] }`;\n * - a raw `string[]` escape hatch.\n *\n * Selectors also accept a `Signal<SelectorTarget>` so callers can\n * feed a `computed(() => ...)` and have the projection rebind\n * automatically.\n */\nexport type SelectorTarget =\n | undefined\n | null\n | readonly string[]\n | { namespace: readonly string[] }\n | SubagentDiscoverySnapshot\n | SubgraphDiscoverySnapshot;\n\nconst EMPTY_NAMESPACE: readonly string[] = [];\n\nfunction resolveNamespace(target: SelectorTarget): readonly string[] {\n if (target == null) return EMPTY_NAMESPACE;\n if (Array.isArray(target)) return target as readonly string[];\n const obj = target as { namespace?: readonly string[] };\n return obj.namespace ?? EMPTY_NAMESPACE;\n}\n\nfunction isRoot(namespace: readonly string[]): boolean {\n return namespace.length === 0;\n}\n\n/**\n * If `target` is a subagent snapshot still on its default\n * `tools:<toolCallId>` namespace, return that tool-call id. See the\n * React selectors for the rationale (deep-agent subagents execute under\n * a distinct `tools:<uuid>` namespace resolved lazily from history).\n */\nfunction subagentNeedingNamespace(target: SelectorTarget): string | null {\n if (target == null || Array.isArray(target)) return null;\n const obj = target as { id?: unknown; namespace?: readonly string[] };\n if (typeof obj.id !== \"string\" || !Array.isArray(obj.namespace)) return null;\n if (obj.namespace.length === 1 && obj.namespace[0] === `tools:${obj.id}`) {\n return obj.id;\n }\n return null;\n}\n\n/**\n * Lazily resolve a subagent's execution namespace on first scoped use.\n * Must be called from an injection context (as the selector primitives\n * are). A `Signal` target re-evaluates via an `effect`; a static target\n * resolves once. The controller de-dupes and skips already-promoted\n * ids.\n */\nfunction resolveSubagentNamespaceFor(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>\n): void {\n const controller = stream[STREAM_CONTROLLER];\n if (isSignal(target)) {\n effect(() => {\n const id = subagentNeedingNamespace((target as Signal<SelectorTarget>)());\n if (id != null) void controller.resolveSubagentNamespace(id);\n });\n return;\n }\n const id = subagentNeedingNamespace(target as SelectorTarget);\n if (id != null) void controller.resolveSubagentNamespace(id);\n}\n\nfunction namespaceKey(namespace: readonly string[]): string {\n return namespace.join(NAMESPACE_SEPARATOR);\n}\n\n/**\n * Resolve a target or target-signal into a reactive\n * `[namespace, key]` pair. The helper returns plain signals so\n * `injectProjection` can track them via its internal effect.\n */\nfunction normalizeTarget(\n target: SelectorTarget | Signal<SelectorTarget>,\n prefix: string\n): {\n namespace: Signal<readonly string[]>;\n key: Signal<string>;\n isRootSignal: Signal<boolean>;\n} {\n if (isSignal(target)) {\n const namespace = computed(() =>\n resolveNamespace((target as Signal<SelectorTarget>)())\n );\n const key = computed(() => `${prefix}|${namespaceKey(namespace())}`);\n const isRootSignal = computed(() => isRoot(namespace()));\n return { namespace, key, isRootSignal };\n }\n const ns = resolveNamespace(target as SelectorTarget);\n const staticKey = `${prefix}|${namespaceKey(ns)}`;\n return {\n namespace: computed(() => ns),\n key: computed(() => staticKey),\n isRootSignal: computed(() => isRoot(ns)),\n };\n}\n\n/**\n * Subscribe to a scoped `messages` stream.\n *\n * Contract:\n * - At the root (no `target`) this returns `stream.messages` — the\n * always-on root projection; no extra subscription is opened.\n * - For any non-root namespace, call-time triggers a ref-counted\n * `messages` subscription scoped to that namespace. The\n * subscription is released automatically when the calling\n * component / service is destroyed (and the registry closes the\n * underlying server subscription when the last consumer leaves).\n *\n * Messages are always `BaseMessage` class instances from\n * `@langchain/core/messages`.\n */\nexport function injectMessages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<BaseMessage[]> {\n resolveSubagentNamespaceFor(\n stream,\n target as SelectorTarget | Signal<SelectorTarget>\n );\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"messages\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<BaseMessage[]>(\n registry,\n () => messagesProjection(namespace()),\n key,\n EMPTY_MESSAGES\n );\n return computed(() => (isRootSignal() ? stream.messages() : scoped()));\n}\n\nconst EMPTY_MESSAGES: BaseMessage[] = [];\n\n/**\n * Subscribe to a scoped `tools` (tool-call) stream. Same target and\n * lifecycle rules as {@link injectMessages}; at the root this returns\n * `stream.toolCalls` directly.\n */\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]>;\nexport function injectToolCalls<T>(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<InferToolCalls<T>[]>;\nexport function injectToolCalls(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AssembledToolCall[]> {\n resolveSubagentNamespaceFor(\n stream,\n target as SelectorTarget | Signal<SelectorTarget>\n );\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"toolCalls\"\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<AssembledToolCall[]>(\n registry,\n () => toolCallsProjection(namespace()),\n key,\n EMPTY_TOOLCALLS\n );\n return computed(() => (isRootSignal() ? stream.toolCalls() : scoped()));\n}\n\nconst EMPTY_TOOLCALLS: AssembledToolCall[] = [];\n\n/**\n * Subscribe to a scoped `values` stream — the most recent state\n * payload for a namespace. At the root returns `stream.values`.\n *\n * Typing:\n * - **Root** (`injectValues(stream)`): returns the `StateType`\n * declared on `injectStream<State>()` — non-nullable (the root\n * snapshot always has values, falling back to `initialValues ??\n * {}`).\n * - **Scoped** (`injectValues(stream, target)`): scoped payloads can\n * differ from the root state; callers should annotate the expected\n * shape explicitly (`injectValues<SubagentState>(stream, sub)`).\n * Defaults to `unknown` when not annotated.\n */\nexport function injectValues<StateType extends object>(\n stream: StreamHandle<StateType>\n): Signal<StateType>;\nexport function injectValues<T>(stream: AnyStream): Signal<InferStateType<T>>;\nexport function injectValues<T = unknown>(\n stream: AnyStream,\n target: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<T | undefined>;\nexport function injectValues(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: { messagesKey?: string }\n): Signal<unknown> {\n const messagesKey = options?.messagesKey ?? \"messages\";\n const { namespace, key, isRootSignal } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `values|${messagesKey}`\n );\n const registry = computed(() =>\n isRootSignal() ? null : getRegistry(stream)\n );\n const scoped = injectProjection<unknown>(\n registry,\n () => valuesProjection<unknown>(namespace(), messagesKey),\n key,\n undefined\n );\n return computed(() => (isRootSignal() ? stream.values() : scoped()));\n}\n\n/**\n * Subscribe to a `custom:<name>` stream extension — the most recent\n * payload emitted by the transformer, scoped to the target namespace.\n */\nexport function injectExtension<T = unknown>(\n stream: AnyStream,\n name: string,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<T | undefined> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n `extension|${name}`\n );\n return injectProjection<T | undefined>(\n getRegistry(stream),\n () => extensionProjection<T>(name, namespace()),\n key,\n undefined\n );\n}\n\n/**\n * Raw-events escape hatch. Subscribes to one or more channels at a\n * namespace and returns a bounded buffer of raw protocol events.\n * Prefer {@link injectMessages} / {@link injectToolCalls} /\n * {@link injectValues} for the common cases.\n */\nexport type InjectChannelOptions = ChannelProjectionOptions;\n\nexport function injectChannel(\n stream: AnyStream,\n channels: readonly Channel[],\n target?: SelectorTarget | Signal<SelectorTarget>,\n options?: InjectChannelOptions\n): Signal<Event[]> {\n const sortedChannels = [...channels].sort().join(\",\");\n const prefix = `channel|${options?.bufferSize ?? \"default\"}|${(options?.replay ?? true) ? \"replay\" : \"live\"}|${sortedChannels}`;\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n prefix\n );\n return injectProjection<Event[]>(\n getRegistry(stream),\n () => channelProjection(channels, namespace(), options),\n key,\n EMPTY_EVENTS\n );\n}\n\nconst EMPTY_EVENTS: Event[] = [];\n\n/**\n * Subscribe to a scoped audio-media stream. Each handle is yielded\n * on its first matching `content-block-start`, exposes\n * `.partialBytes` for live access, settles `.blob` / `.objectURL` /\n * `.transcript` on `message-finish`, and surfaces errors via\n * `.error`.\n *\n * Pair with `injectMediaUrl` to turn a handle into an `<audio src>`.\n */\nexport function injectAudio(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<AudioMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"audio\"\n );\n return injectProjection<AudioMedia[]>(\n getRegistry(stream),\n () => audioProjection(namespace()),\n key,\n EMPTY_AUDIO\n );\n}\n\nconst EMPTY_AUDIO: AudioMedia[] = [];\n\n/**\n * Subscribe to a scoped image-media stream. Pair with\n * `injectMediaUrl` for `<img src>`.\n */\nexport function injectImages(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<ImageMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"images\"\n );\n return injectProjection<ImageMedia[]>(\n getRegistry(stream),\n () => imagesProjection(namespace()),\n key,\n EMPTY_IMAGES\n );\n}\n\nconst EMPTY_IMAGES: ImageMedia[] = [];\n\n/**\n * Subscribe to a scoped video-media stream. Pair with\n * `injectMediaUrl` for `<video src>`.\n */\nexport function injectVideo(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<VideoMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"video\"\n );\n return injectProjection<VideoMedia[]>(\n getRegistry(stream),\n () => videoProjection(namespace()),\n key,\n EMPTY_VIDEO\n );\n}\n\nconst EMPTY_VIDEO: VideoMedia[] = [];\n\n/**\n * Subscribe to a scoped file-media stream. Pair with\n * `injectMediaUrl` for an `<a download href>` target.\n */\nexport function injectFiles(\n stream: AnyStream,\n target?: SelectorTarget | Signal<SelectorTarget>\n): Signal<FileMedia[]> {\n const { namespace, key } = normalizeTarget(\n target as SelectorTarget | Signal<SelectorTarget>,\n \"files\"\n );\n return injectProjection<FileMedia[]>(\n getRegistry(stream),\n () => filesProjection(namespace()),\n key,\n EMPTY_FILES\n );\n}\n\nconst EMPTY_FILES: FileMedia[] = [];\n"],"mappings":";;;;;AAqEA,MAAM,kBAAqC,EAAE;AAE7C,SAAS,iBAAiB,QAA2C;AACnE,KAAI,UAAU,KAAM,QAAO;AAC3B,KAAI,MAAM,QAAQ,OAAO,CAAE,QAAO;AAElC,QADY,OACD,aAAa;;AAG1B,SAAS,OAAO,WAAuC;AACrD,QAAO,UAAU,WAAW;;;;;;;;AAS9B,SAAS,yBAAyB,QAAuC;AACvE,KAAI,UAAU,QAAQ,MAAM,QAAQ,OAAO,CAAE,QAAO;CACpD,MAAM,MAAM;AACZ,KAAI,OAAO,IAAI,OAAO,YAAY,CAAC,MAAM,QAAQ,IAAI,UAAU,CAAE,QAAO;AACxE,KAAI,IAAI,UAAU,WAAW,KAAK,IAAI,UAAU,OAAO,SAAS,IAAI,KAClE,QAAO,IAAI;AAEb,QAAO;;;;;;;;;AAUT,SAAS,4BACP,QACA,QACM;CACN,MAAM,aAAa,OAAO;AAC1B,KAAI,SAAS,OAAO,EAAE;AACpB,eAAa;GACX,MAAM,KAAK,yBAA0B,QAAmC,CAAC;AACzE,OAAI,MAAM,KAAW,YAAW,yBAAyB,GAAG;IAC5D;AACF;;CAEF,MAAM,KAAK,yBAAyB,OAAyB;AAC7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AAY,OAAO,QACb,gBAAgB,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA4B,EAAE;;;;;AAMpC,SAAgB,YACd,QACA,QACqB;CACrB,MAAM,EAAE,WAAW,QAAQ,gBACzB,QACA,QACD;AACD,QAAO,iBACL,YAAY,OAAO,QACb,gBAAgB,WAAW,CAAC,EAClC,KACA,YACD;;AAGH,MAAM,cAA2B,EAAE"}
|
package/dist/use-stream.cjs
CHANGED
|
@@ -48,7 +48,8 @@ function useStream(options, destroyRef) {
|
|
|
48
48
|
onCreated: options.onCreated,
|
|
49
49
|
onCompleted: options.onCompleted,
|
|
50
50
|
initialValues: options.initialValues,
|
|
51
|
-
messagesKey: options.messagesKey
|
|
51
|
+
messagesKey: options.messagesKey,
|
|
52
|
+
optimistic: asBag.optimistic
|
|
52
53
|
});
|
|
53
54
|
const deactivate = controller.activate();
|
|
54
55
|
const ref = destroyRef ?? (0, _angular_core.inject)(_angular_core.DestroyRef);
|
package/dist/use-stream.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-stream.cjs","names":["ClientCtor","StreamController","DestroyRef"],"sources":["../src/use-stream.ts"],"sourcesContent":["import {\n DestroyRef,\n computed,\n effect,\n inject,\n isSignal,\n signal,\n untracked,\n type Signal,\n} from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Client, Interrupt } from \"@langchain/langgraph-sdk\";\nimport {\n applyHeadlessToolResumeCommand,\n filterOutHeadlessToolInterrupts,\n flushPendingHeadlessToolInterrupts,\n scheduleCoalescedHeadlessToolFlush,\n type AnyHeadlessToolImplementation,\n type OnToolCallback,\n} from \"@langchain/langgraph-sdk\";\nimport {\n Client as ClientCtor,\n type ClientConfig,\n type ThreadStream,\n} from \"@langchain/langgraph-sdk/client\";\nimport {\n StreamController,\n type AgentServerAdapter,\n type AgentServerOptions as StreamAgentServerOptions,\n type ChannelRegistry,\n type CustomAdapterOptions as StreamCustomAdapterOptions,\n type InferStateType,\n type InferToolCalls,\n type InferSubagentStates,\n type RootSnapshot,\n type RunCompletedInfo,\n type RunExecutionInfo,\n type StreamRespondAllOptions,\n type StreamRespondOptions,\n type StreamStopOptions,\n type StreamSubmitOptions,\n type SubagentDiscoverySnapshot,\n type SubagentMap,\n type SubgraphByNodeMap,\n type SubgraphDiscoverySnapshot,\n type SubgraphMap,\n type UseStreamOptions as StreamUseStreamOptions,\n type WidenUpdateMessages,\n} from \"@langchain/langgraph-sdk/stream\";\n\ntype AngularThreadId = string | null | Signal<string | null | undefined>;\n\nexport type AgentServerOptions<StateType extends object> =\n StreamAgentServerOptions<StateType, AngularThreadId>;\n\nexport type CustomAdapterOptions<StateType extends object> =\n StreamCustomAdapterOptions<StateType, AngularThreadId, string>;\n\nexport type UseStreamOptions<\n StateType extends object = Record<string, unknown>,\n> = StreamUseStreamOptions<\n StateType,\n AngularThreadId,\n string | undefined,\n string | undefined,\n string\n>;\n\n/**\n * Private field on the handle that carries the\n * {@link StreamController} reference. Selector primitives read this\n * to reach the shared {@link ChannelRegistry}. Use the companion\n * `inject*` selectors (`injectMessages`, `injectToolCalls`,\n * `injectValues`, …) instead of reading this directly.\n */\nexport const STREAM_CONTROLLER: unique symbol = Symbol.for(\n \"@langchain/angular/controller\"\n);\n\n/**\n * Return shape of {@link useStream} — the Angular `StreamApi`.\n *\n * Reactivity primitives follow Angular conventions:\n *\n * - Data projections are `Signal<T>`; call them as functions in\n * templates (`stream.messages()`). They are snapshots — never\n * mutate the returned arrays / maps.\n * - Imperative methods (`submit` / `stop` / `respond`) are plain\n * functions. No `WritableSignal`s are exposed on the root handle.\n * - Identity values captured at construction time (`client`,\n * `assistantId`) are exposed as plain values; remount the\n * component to swap them.\n */\nexport interface UseStreamReturn<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n StateType extends object = InferStateType<T>,\n SubagentStates = InferSubagentStates<T>,\n> {\n // ----- always-on root projections -----\n /**\n * The most recent `values`-channel snapshot emitted at the root\n * namespace — i.e. the thread-level state as the server sees it\n * after each superstep. Updated on every root `values` event, not\n * on token-level deltas: if you render `stream.values().messages`\n * directly you'll see full turns appear at once instead of\n * streaming token-by-token. Use {@link messages} (or\n * `injectMessages`) for the token-streamed view.\n *\n * Equivalent to calling `injectValues(stream)`.\n */\n readonly values: Signal<StateType>;\n /**\n * The root message projection. Assembled from two sources and\n * merged in real time:\n *\n * 1. `messages`-channel deltas — token-level streaming events\n * (`message-start`, `content-block-delta`, `message-finish`)\n * emitted by the runtime. These drive live, token-by-token\n * updates.\n * 2. `values.messages` snapshots — the authoritative ordering\n * and any messages the agent produces without token streaming\n * (human turns, tool results, echoes from subagents).\n *\n * If the backend only emits `values` events (no `messages`\n * channel), every message will appear fully-formed on each\n * values update rather than streaming. This is a backend/runtime\n * concern — the Angular layer faithfully renders whatever the\n * server sends.\n *\n * Equivalent to calling `injectMessages(stream)` with no target.\n */\n readonly messages: Signal<BaseMessage[]>;\n /**\n * Root-namespace tool calls assembled from the `tools` channel.\n * Each entry is a fully parsed {@link AssembledToolCall} with\n * name, args, and id — suitable for rendering approval UIs or\n * forwarding to headless tool handlers.\n *\n * When the stream is typed with an agent brand or tool list,\n * entries are narrowed via {@link InferToolCalls}. Equivalent to\n * calling `injectToolCalls(stream)` with no target.\n */\n readonly toolCalls: Signal<InferToolCalls<T>[]>;\n /**\n * All unresolved protocol interrupts observed on the root\n * namespace during the active thread. Populated from lifecycle /\n * input events and seeded on hydration from `thread.getState()`.\n * Cleared optimistically when a new run starts or an interrupt is\n * resolved via {@link respond}.\n */\n readonly interrupts: Signal<Interrupt<InterruptType>[]>;\n /**\n * Convenience alias for {@link interrupts}[0] — the primary\n * interrupt most UIs should act on when only one is pending.\n * `undefined` when no interrupt is active.\n */\n readonly interrupt: Signal<Interrupt<InterruptType> | undefined>;\n /**\n * `true` while a run is active or being started on the current\n * thread. Driven by root-namespace lifecycle events (`running` →\n * `true`, terminal phases → `false`). Use this to disable submit\n * buttons and show in-flight spinners.\n */\n readonly isLoading: Signal<boolean>;\n /**\n * `true` while the initial `thread.getState()` hydration for the\n * active thread is in flight. Distinct from {@link isLoading} —\n * thread loading covers the one-time fetch that seeds\n * {@link values} / {@link messages} before any user submit.\n */\n readonly isThreadLoading: Signal<boolean>;\n /**\n * The last error observed on the active run or hydration attempt.\n * `undefined` when no error has occurred. Cleared optimistically\n * when a new {@link submit} starts.\n */\n readonly error: Signal<unknown>;\n /**\n * Id of the thread the controller is bound to. `null` until the\n * first {@link submit} creates or selects a thread (or until an\n * explicit `threadId` option is provided and hydrated).\n */\n readonly threadId: Signal<string | null>;\n /**\n * Promise that settles when the active thread's initial hydration\n * completes. Exposed so SSR/render-before-flush pipelines can\n * `await stream.hydrationPromise()` before serialising. A fresh\n * promise is installed on every `threadId` change.\n */\n readonly hydrationPromise: Signal<Promise<void>>;\n\n // ----- always-on discovery -----\n /**\n * Subagents discovered on the root run. For DeepAgent-typed\n * streams the key set is narrowed to the subagent names declared\n * on the agent brand (`keyof InferSubagentStates<T>`).\n */\n readonly subagents: Signal<\n ReadonlyMap<\n keyof SubagentStates & string extends never\n ? string\n : keyof SubagentStates & string,\n SubagentDiscoverySnapshot\n >\n >;\n /**\n * Subgraphs discovered on the root run.\n *\n * A namespace is classified as a subgraph iff at least one\n * strictly-deeper namespace has been observed with it as a prefix.\n * This is inferred from the lifecycle event stream — plain function\n * nodes (`orchestrator`, `writer` in the nested-stategraph example)\n * never appear here even though the server emits namespaced\n * lifecycle events for them. Promotion is monotonic and retroactive;\n * an entry appears as soon as the first descendant event lands.\n */\n readonly subgraphs: Signal<ReadonlyMap<string, SubgraphDiscoverySnapshot>>;\n /**\n * Subgraphs indexed by the graph node that produced them\n * (`addNode(\"visualizer_0\", …)`). Each value is an array because\n * parallel fan-outs and loops can spawn multiple invocations of\n * the same node; arrays preserve insertion order. Updates in\n * lock-step with {@link subgraphs}.\n */\n readonly subgraphsByNode: Signal<\n ReadonlyMap<string, readonly SubgraphDiscoverySnapshot[]>\n >;\n\n // ----- imperatives -----\n /**\n * Dispatch a new run on the bound thread.\n *\n * `input` is typed as `Partial<StateType>` so IDE autocompletion\n * surfaces the state keys declared on the root primitive.\n */\n submit(\n input: WidenUpdateMessages<Partial<StateType>> | null | undefined,\n options?: StreamSubmitOptions<StateType, ConfigurableType>\n ): Promise<void>;\n /**\n * Stop the active run on the current thread. By default cancels the\n * run server-side and disconnects the client; pass `{ cancel: false }`\n * or use {@link disconnect} for join/rejoin. Sets {@link isLoading} to\n * `false` immediately; {@link values} and {@link messages} are preserved.\n */\n stop(options?: StreamStopOptions): Promise<void>;\n /**\n * Disconnect the client without cancelling the run server-side.\n * Alias for `stop({ cancel: false })`.\n */\n disconnect(): Promise<void>;\n /**\n * Resume a pending protocol interrupt by sending a response payload\n * back to the interrupted namespace.\n *\n * When `options.interruptId` is omitted, walks `getThread()?.interrupts`\n * from newest to oldest and resumes the first not yet resolved by a prior\n * `respond()` call. That may be a root or subgraph interrupt and is\n * **not** necessarily {@link interrupt} (`interrupts()[0]`, root-only).\n * Safe when exactly one interrupt is pending; otherwise pass an explicit\n * `options.interruptId` (and `options.namespace` for subgraph\n * interrupts).\n *\n * The server validates `namespace` against the pending interrupt. Root\n * interrupts use `namespace: []` (default when omitted). For subgraph\n * interrupts, copy `namespace` from `getThread()?.interrupts`.\n *\n * @example\n * ```ts\n * // Single pending interrupt\n * await stream.respond({ approved: true });\n * ```\n *\n * @example\n * ```ts\n * // Multiple root interrupts\n * for (const intr of stream.interrupts()) {\n * await stream.respond(decide(intr.value), { interruptId: intr.id! });\n * }\n * ```\n *\n * @example\n * ```ts\n * // Subgraph interrupt — namespace from `getThread()`\n * const thread = stream.getThread();\n * for (const entry of thread?.interrupts ?? []) {\n * await stream.respond(buildResponse(entry.payload), {\n * interruptId: entry.interruptId,\n * namespace: entry.namespace,\n * });\n * }\n * ```\n *\n * To resume several interrupts pending at the same checkpoint in one\n * command, use {@link respondAll}.\n */\n respond(\n response: unknown,\n options?: StreamRespondOptions<ConfigurableType>\n ): Promise<void>;\n\n /**\n * Resume several pending interrupts at the same checkpoint in a single\n * command — required when a run pauses on multiple interrupts at once\n * (e.g. parallel tool-authorization prompts), which sequential\n * {@link respond} calls cannot handle. `responsesById` maps each pending\n * `interruptId` to its response, so different interrupts can receive\n * different payloads. Pass `options.config` / `options.metadata` to fold\n * run-level config and metadata into the resumed run, mirroring\n * `submit()`.\n *\n * @example\n * ```ts\n * await stream.respondAll({\n * [interruptA.id]: { approved: true },\n * [interruptB.id]: { approved: false },\n * });\n * ```\n */\n respondAll(\n responsesById: Record<string, unknown>,\n options?: StreamRespondAllOptions<ConfigurableType>\n ): Promise<void>;\n\n // ----- identity -----\n /** LangGraph SDK client used to construct thread streams. */\n readonly client: Client;\n /** Assistant id the thread is bound to for its lifetime. */\n readonly assistantId: string;\n\n /**\n * Returns the bound {@link ThreadStream}, if one exists (`undefined`\n * until the thread is hydrated or the first submit completes). Prefer\n * the projections and selector primitives for UI work; use this for\n * low-level protocol access (raw subscriptions, state commands, etc.).\n */\n getThread(): ThreadStream | undefined;\n\n /** @internal Used by selector primitives. */\n readonly [STREAM_CONTROLLER]: StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >;\n}\n\n/**\n * Erased handle useful as a parameter type for helper components that\n * pass a `stream` through to selector primitives without reading\n * `values` directly. Mirrors the React/Vue `AnyStream` alias.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyStream = UseStreamReturn<any, any, any>;\n\n/**\n * Convenience alias — the fully-resolved return type of\n * {@link useStream} for a given source type `T`.\n */\nexport type StreamApi<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * React-compatible alias for the fully-resolved stream handle type.\n * Angular docs prefer {@link StreamApi}, but shared libraries can use\n * this name across framework bindings.\n */\nexport type UseStreamResult<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * Framework-free factory that constructs a {@link StreamController}\n * and wraps its stores in Angular Signals. Callers must supply the\n * {@link DestroyRef} that owns the controller's lifetime — it's\n * already captured by the public `injectStream` helper.\n *\n * Exported for advanced callers (e.g. testing utilities, custom\n * factories) that prefer to manage injection scope themselves.\n */\nexport function useStream<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n>(\n options: UseStreamOptions<InferStateType<T>>,\n destroyRef?: DestroyRef\n): UseStreamReturn<T, InterruptType, ConfigurableType> {\n type StateType = InferStateType<T>;\n\n interface OptionsBag {\n assistantId?: string;\n threadId?: string | null | Signal<string | null | undefined>;\n client?: Client;\n apiUrl?: string;\n apiKey?: string;\n callerOptions?: ClientConfig[\"callerOptions\"];\n defaultHeaders?: ClientConfig[\"defaultHeaders\"];\n transport?: \"sse\" | \"websocket\" | AgentServerAdapter;\n fetch?: typeof fetch;\n webSocketFactory?: (url: string) => WebSocket;\n onThreadId?: (threadId: string) => void;\n onCreated?: (info: RunExecutionInfo) => void;\n onCompleted?: (info: RunCompletedInfo) => void;\n initialValues?: StateType;\n messagesKey?: string;\n tools?: AnyHeadlessToolImplementation[];\n onTool?: OnToolCallback;\n }\n const asBag = options as OptionsBag;\n\n const hasCustomAdapter =\n asBag.transport != null && typeof asBag.transport !== \"string\";\n const transport = asBag.transport;\n\n const client: Client =\n asBag.client ??\n (new ClientCtor({\n apiUrl: asBag.apiUrl,\n apiKey: asBag.apiKey,\n callerOptions: asBag.callerOptions,\n defaultHeaders: asBag.defaultHeaders,\n }) as unknown as Client);\n\n // Custom adapters may omit `assistantId`; the controller still\n // requires one so it has something to forward to `threads.stream`.\n const sentinel = \"_\";\n const assistantId =\n \"assistantId\" in options ? (options.assistantId ?? sentinel) : sentinel;\n\n // Normalize threadId input to a signal — callers may pass plain\n // values, nulls, or their own signals.\n const threadIdInput: Signal<string | null> = (() => {\n const raw = asBag.threadId;\n if (isSignal(raw)) {\n return computed(\n () => (raw as Signal<string | null | undefined>)() ?? null\n );\n }\n const initial: string | null = (raw as string | null | undefined) ?? null;\n return signal(initial) as unknown as Signal<string | null>;\n })();\n\n const controller = new StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >({\n assistantId,\n // Cast: the runtime `Client` is state-shape agnostic, but the\n // controller declares `client: Client<StateType>` for its own\n // typings. Same cast is applied in the React/Vue bindings.\n client: client as unknown as Client<StateType>,\n threadId: untracked(() => threadIdInput()),\n transport,\n fetch: hasCustomAdapter ? undefined : asBag.fetch,\n webSocketFactory: hasCustomAdapter ? undefined : asBag.webSocketFactory,\n onThreadId: options.onThreadId,\n onCreated: options.onCreated,\n onCompleted: options.onCompleted,\n initialValues: options.initialValues,\n messagesKey: options.messagesKey,\n });\n\n // Deferred dispose — matches the React `useEffect(() =>\n // controller.activate())` and Vue `onScopeDispose(deactivate)`\n // patterns. HMR / scope-reuse scenarios stay clean because\n // `activate()` cancels the pending dispose if the scope survives.\n const deactivate = controller.activate();\n const ref = destroyRef ?? inject(DestroyRef);\n ref.onDestroy(deactivate);\n\n // ─── Reactivity bridge: StreamStore → Signal ────────────────────────\n function bindStore<S>(\n subscribe: (listener: () => void) => () => void,\n getSnapshot: () => S\n ): Signal<S> {\n const s = signal<S>(getSnapshot());\n const unsubscribe = subscribe(() => {\n s.set(getSnapshot());\n });\n ref.onDestroy(unsubscribe);\n return computed(() => s());\n }\n\n const rootSignal = bindStore<RootSnapshot<StateType, InterruptType>>(\n controller.rootStore.subscribe,\n controller.rootStore.getSnapshot\n );\n const subagentSignal = bindStore<SubagentMap>(\n controller.subagentStore.subscribe,\n controller.subagentStore.getSnapshot\n );\n const subgraphSignal = bindStore<SubgraphMap>(\n controller.subgraphStore.subscribe,\n controller.subgraphStore.getSnapshot\n );\n const subgraphByNodeSignal = bindStore<SubgraphByNodeMap>(\n controller.subgraphByNodeStore.subscribe,\n controller.subgraphByNodeStore.getSnapshot\n );\n\n const values = computed(() => rootSignal().values);\n const messages = computed(() => rootSignal().messages);\n const toolCalls = computed(\n () => rootSignal().toolCalls as InferToolCalls<T>[]\n );\n const interrupts = computed(() =>\n filterOutHeadlessToolInterrupts(rootSignal().interrupts)\n );\n const interrupt = computed(() => interrupts()[0]);\n const isLoading = computed(() => rootSignal().isLoading);\n const isThreadLoading = computed(() => rootSignal().isThreadLoading);\n const error = computed(() => rootSignal().error);\n const threadId = computed(() => rootSignal().threadId);\n\n // `hydrationPromise` is a property on the controller that gets\n // swapped on every `hydrate()` call. Exposing it as a signal lets\n // templates `await stream.hydrationPromise()` reactively; we\n // refresh the reference when the root store settles a new promise.\n const hydrationPromise = computed(() => {\n rootSignal();\n return controller.hydrationPromise;\n });\n\n // ─── threadId reactivity ────────────────────────────────────────────\n //\n // Re-hydrate whenever the caller's threadId input changes after\n // construction. The initial hydrate already fired synchronously in\n // the controller constructor, so we compare against the snapshot\n // captured at construction time rather than blindly skipping the\n // first run — in Angular, `@Input()` bindings apply *between*\n // construction and the first effect tick, so the first read can\n // legitimately be a different (updated) value that needs to\n // hydrate.\n const initialThreadId = untracked(() => threadIdInput()) ?? null;\n let lastAppliedThreadId: string | null = initialThreadId;\n effect(() => {\n const next = threadIdInput() ?? null;\n if (next === lastAppliedThreadId) return;\n lastAppliedThreadId = next;\n untracked(() => {\n void controller.hydrate(next);\n });\n });\n\n // ─── Headless-tool handling ─────────────────────────────────────────\n const tools = options.tools;\n const onTool = options.onTool;\n if (tools?.length) {\n const handledTools = new Set<string>();\n\n // Clear the dedup set whenever the thread id changes.\n effect(() => {\n threadIdInput();\n untracked(() => handledTools.clear());\n });\n\n effect(() => {\n rootSignal();\n untracked(() => {\n scheduleCoalescedHeadlessToolFlush(handledTools, () => {\n const snapshot = rootSignal();\n const bag = snapshot.values as unknown as Record<string, unknown>;\n const protocolInterrupts =\n snapshot.interrupts as unknown as Interrupt[];\n const valuesInterrupts = Array.isArray(bag?.__interrupt__)\n ? (bag.__interrupt__ as Interrupt[])\n : [];\n const headlessInterrupts =\n protocolInterrupts.length > 0\n ? protocolInterrupts\n : valuesInterrupts;\n if (headlessInterrupts.length === 0) return;\n flushPendingHeadlessToolInterrupts(\n { ...bag, __interrupt__: headlessInterrupts },\n tools,\n handledTools,\n {\n onTool,\n defer: (run) => {\n void Promise.resolve().then(run);\n },\n resumeSubmit: (command) =>\n applyHeadlessToolResumeCommand(controller, command),\n }\n );\n });\n });\n });\n }\n\n const handle: UseStreamReturn<T, InterruptType, ConfigurableType> = {\n values: values as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"values\"],\n messages,\n toolCalls,\n interrupts,\n interrupt,\n isLoading,\n isThreadLoading,\n error,\n threadId,\n hydrationPromise,\n subagents: subagentSignal as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"subagents\"],\n subgraphs: subgraphSignal,\n subgraphsByNode: subgraphByNodeSignal,\n submit: (input, submitOptions) => controller.submit(input, submitOptions),\n stop: (options) => controller.stop(options),\n disconnect: () => controller.disconnect(),\n respond: (response, options) => controller.respond(response, options),\n respondAll: (responsesById, options) =>\n controller.respondAll(responsesById, options),\n getThread: () => controller.getThread(),\n client,\n assistantId,\n [STREAM_CONTROLLER]: controller,\n };\n\n return handle;\n}\n\n/**\n * Helper used by the selector primitives to reach the underlying\n * {@link ChannelRegistry} from a stream handle. Kept internal —\n * application code should call `injectMessages`, `injectToolCalls`,\n * etc. instead of reading this directly.\n *\n * @internal\n */\nexport function getRegistry(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: UseStreamReturn<any, any, any>\n): ChannelRegistry {\n return stream[STREAM_CONTROLLER].registry;\n}\n\nexport type { ThreadStream };\n"],"mappings":";;;;;;;;;;;;AA2EA,MAAa,oBAAmC,OAAO,IACrD,gCACD;;;;;;;;;;AAqTD,SAAgB,UAKd,SACA,YACqD;CAsBrD,MAAM,QAAQ;CAEd,MAAM,mBACJ,MAAM,aAAa,QAAQ,OA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,WAAW,YAAY;KAC7B,MAAM,MAAM,SAAS;KACrB,MAAM,qBACJ,SAAS;KACX,MAAM,mBAAmB,MAAM,QAAQ,KAAK,cAAc,GACrD,IAAI,gBACL,EAAE;KACN,MAAM,qBACJ,mBAAmB,SAAS,IACxB,qBACA;AACN,SAAI,mBAAmB,WAAW,EAAG;AACrC,MAAA,GAAA,yBAAA,oCACE;MAAE,GAAG;MAAK,eAAe;MAAoB,EAC7C,OACA,cACA;MACE;MACA,QAAQ,QAAQ;AACT,eAAQ,SAAS,CAAC,KAAK,IAAI;;MAElC,eAAe,aAAA,GAAA,yBAAA,gCACkB,YAAY,QAAQ;MACtD,CACF;MACD;KACF;IACF;;AAqCJ,QAlCoE;EAC1D;EAKR;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,WAAW;EAKX,WAAW;EACX,iBAAiB;EACjB,SAAS,OAAO,kBAAkB,WAAW,OAAO,OAAO,cAAc;EACzE,OAAO,YAAY,WAAW,KAAK,QAAQ;EAC3C,kBAAkB,WAAW,YAAY;EACzC,UAAU,UAAU,YAAY,WAAW,QAAQ,UAAU,QAAQ;EACrE,aAAa,eAAe,YAC1B,WAAW,WAAW,eAAe,QAAQ;EAC/C,iBAAiB,WAAW,WAAW;EACvC;EACA;GACC,oBAAoB;EACtB;;;;;;;;;;AAaH,SAAgB,YAEd,QACiB;AACjB,QAAO,OAAO,mBAAmB"}
|
|
1
|
+
{"version":3,"file":"use-stream.cjs","names":["ClientCtor","StreamController","DestroyRef"],"sources":["../src/use-stream.ts"],"sourcesContent":["import {\n DestroyRef,\n computed,\n effect,\n inject,\n isSignal,\n signal,\n untracked,\n type Signal,\n} from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Client, Interrupt } from \"@langchain/langgraph-sdk\";\nimport {\n applyHeadlessToolResumeCommand,\n filterOutHeadlessToolInterrupts,\n flushPendingHeadlessToolInterrupts,\n scheduleCoalescedHeadlessToolFlush,\n type AnyHeadlessToolImplementation,\n type OnToolCallback,\n} from \"@langchain/langgraph-sdk\";\nimport {\n Client as ClientCtor,\n type ClientConfig,\n type ThreadStream,\n} from \"@langchain/langgraph-sdk/client\";\nimport {\n StreamController,\n type AgentServerAdapter,\n type AgentServerOptions as StreamAgentServerOptions,\n type ChannelRegistry,\n type CustomAdapterOptions as StreamCustomAdapterOptions,\n type InferStateType,\n type InferToolCalls,\n type InferSubagentStates,\n type RootSnapshot,\n type RunCompletedInfo,\n type RunExecutionInfo,\n type StreamRespondAllOptions,\n type StreamRespondOptions,\n type StreamStopOptions,\n type StreamSubmitOptions,\n type SubagentDiscoverySnapshot,\n type SubagentMap,\n type SubgraphByNodeMap,\n type SubgraphDiscoverySnapshot,\n type SubgraphMap,\n type UseStreamOptions as StreamUseStreamOptions,\n type WidenUpdateMessages,\n} from \"@langchain/langgraph-sdk/stream\";\n\ntype AngularThreadId = string | null | Signal<string | null | undefined>;\n\nexport type AgentServerOptions<StateType extends object> =\n StreamAgentServerOptions<StateType, AngularThreadId>;\n\nexport type CustomAdapterOptions<StateType extends object> =\n StreamCustomAdapterOptions<StateType, AngularThreadId, string>;\n\nexport type UseStreamOptions<\n StateType extends object = Record<string, unknown>,\n> = StreamUseStreamOptions<\n StateType,\n AngularThreadId,\n string | undefined,\n string | undefined,\n string\n>;\n\n/**\n * Private field on the handle that carries the\n * {@link StreamController} reference. Selector primitives read this\n * to reach the shared {@link ChannelRegistry}. Use the companion\n * `inject*` selectors (`injectMessages`, `injectToolCalls`,\n * `injectValues`, …) instead of reading this directly.\n */\nexport const STREAM_CONTROLLER: unique symbol = Symbol.for(\n \"@langchain/angular/controller\"\n);\n\n/**\n * Return shape of {@link useStream} — the Angular `StreamApi`.\n *\n * Reactivity primitives follow Angular conventions:\n *\n * - Data projections are `Signal<T>`; call them as functions in\n * templates (`stream.messages()`). They are snapshots — never\n * mutate the returned arrays / maps.\n * - Imperative methods (`submit` / `stop` / `respond`) are plain\n * functions. No `WritableSignal`s are exposed on the root handle.\n * - Identity values captured at construction time (`client`,\n * `assistantId`) are exposed as plain values; remount the\n * component to swap them.\n */\nexport interface UseStreamReturn<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n StateType extends object = InferStateType<T>,\n SubagentStates = InferSubagentStates<T>,\n> {\n // ----- always-on root projections -----\n /**\n * The most recent `values`-channel snapshot emitted at the root\n * namespace — i.e. the thread-level state as the server sees it\n * after each superstep. Updated on every root `values` event, not\n * on token-level deltas: if you render `stream.values().messages`\n * directly you'll see full turns appear at once instead of\n * streaming token-by-token. Use {@link messages} (or\n * `injectMessages`) for the token-streamed view.\n *\n * Equivalent to calling `injectValues(stream)`.\n */\n readonly values: Signal<StateType>;\n /**\n * The root message projection. Assembled from two sources and\n * merged in real time:\n *\n * 1. `messages`-channel deltas — token-level streaming events\n * (`message-start`, `content-block-delta`, `message-finish`)\n * emitted by the runtime. These drive live, token-by-token\n * updates.\n * 2. `values.messages` snapshots — the authoritative ordering\n * and any messages the agent produces without token streaming\n * (human turns, tool results, echoes from subagents).\n *\n * If the backend only emits `values` events (no `messages`\n * channel), every message will appear fully-formed on each\n * values update rather than streaming. This is a backend/runtime\n * concern — the Angular layer faithfully renders whatever the\n * server sends.\n *\n * Equivalent to calling `injectMessages(stream)` with no target.\n */\n readonly messages: Signal<BaseMessage[]>;\n /**\n * Root-namespace tool calls assembled from the `tools` channel.\n * Each entry is a fully parsed {@link AssembledToolCall} with\n * name, args, and id — suitable for rendering approval UIs or\n * forwarding to headless tool handlers.\n *\n * When the stream is typed with an agent brand or tool list,\n * entries are narrowed via {@link InferToolCalls}. Equivalent to\n * calling `injectToolCalls(stream)` with no target.\n */\n readonly toolCalls: Signal<InferToolCalls<T>[]>;\n /**\n * All unresolved protocol interrupts observed on the root\n * namespace during the active thread. Populated from lifecycle /\n * input events and seeded on hydration from `thread.getState()`.\n * Cleared optimistically when a new run starts or an interrupt is\n * resolved via {@link respond}.\n */\n readonly interrupts: Signal<Interrupt<InterruptType>[]>;\n /**\n * Convenience alias for {@link interrupts}[0] — the primary\n * interrupt most UIs should act on when only one is pending.\n * `undefined` when no interrupt is active.\n */\n readonly interrupt: Signal<Interrupt<InterruptType> | undefined>;\n /**\n * `true` while a run is active or being started on the current\n * thread. Driven by root-namespace lifecycle events (`running` →\n * `true`, terminal phases → `false`). Use this to disable submit\n * buttons and show in-flight spinners.\n */\n readonly isLoading: Signal<boolean>;\n /**\n * `true` while the initial `thread.getState()` hydration for the\n * active thread is in flight. Distinct from {@link isLoading} —\n * thread loading covers the one-time fetch that seeds\n * {@link values} / {@link messages} before any user submit.\n */\n readonly isThreadLoading: Signal<boolean>;\n /**\n * The last error observed on the active run or hydration attempt.\n * `undefined` when no error has occurred. Cleared optimistically\n * when a new {@link submit} starts.\n */\n readonly error: Signal<unknown>;\n /**\n * Id of the thread the controller is bound to. `null` until the\n * first {@link submit} creates or selects a thread (or until an\n * explicit `threadId` option is provided and hydrated).\n */\n readonly threadId: Signal<string | null>;\n /**\n * Promise that settles when the active thread's initial hydration\n * completes. Exposed so SSR/render-before-flush pipelines can\n * `await stream.hydrationPromise()` before serialising. A fresh\n * promise is installed on every `threadId` change.\n */\n readonly hydrationPromise: Signal<Promise<void>>;\n\n // ----- always-on discovery -----\n /**\n * Subagents discovered on the root run. For DeepAgent-typed\n * streams the key set is narrowed to the subagent names declared\n * on the agent brand (`keyof InferSubagentStates<T>`).\n */\n readonly subagents: Signal<\n ReadonlyMap<\n keyof SubagentStates & string extends never\n ? string\n : keyof SubagentStates & string,\n SubagentDiscoverySnapshot\n >\n >;\n /**\n * Subgraphs discovered on the root run.\n *\n * A namespace is classified as a subgraph iff at least one\n * strictly-deeper namespace has been observed with it as a prefix.\n * This is inferred from the lifecycle event stream — plain function\n * nodes (`orchestrator`, `writer` in the nested-stategraph example)\n * never appear here even though the server emits namespaced\n * lifecycle events for them. Promotion is monotonic and retroactive;\n * an entry appears as soon as the first descendant event lands.\n */\n readonly subgraphs: Signal<ReadonlyMap<string, SubgraphDiscoverySnapshot>>;\n /**\n * Subgraphs indexed by the graph node that produced them\n * (`addNode(\"visualizer_0\", …)`). Each value is an array because\n * parallel fan-outs and loops can spawn multiple invocations of\n * the same node; arrays preserve insertion order. Updates in\n * lock-step with {@link subgraphs}.\n */\n readonly subgraphsByNode: Signal<\n ReadonlyMap<string, readonly SubgraphDiscoverySnapshot[]>\n >;\n\n // ----- imperatives -----\n /**\n * Dispatch a new run on the bound thread.\n *\n * `input` is typed as `Partial<StateType>` so IDE autocompletion\n * surfaces the state keys declared on the root primitive.\n */\n submit(\n input: WidenUpdateMessages<Partial<StateType>> | null | undefined,\n options?: StreamSubmitOptions<StateType, ConfigurableType>\n ): Promise<void>;\n /**\n * Stop the active run on the current thread. By default cancels the\n * run server-side and disconnects the client; pass `{ cancel: false }`\n * or use {@link disconnect} for join/rejoin. Sets {@link isLoading} to\n * `false` immediately; {@link values} and {@link messages} are preserved.\n */\n stop(options?: StreamStopOptions): Promise<void>;\n /**\n * Disconnect the client without cancelling the run server-side.\n * Alias for `stop({ cancel: false })`.\n */\n disconnect(): Promise<void>;\n /**\n * Resume a pending protocol interrupt by sending a response payload\n * back to the interrupted namespace.\n *\n * When `options.interruptId` is omitted, walks `getThread()?.interrupts`\n * from newest to oldest and resumes the first not yet resolved by a prior\n * `respond()` call. That may be a root or subgraph interrupt and is\n * **not** necessarily {@link interrupt} (`interrupts()[0]`, root-only).\n * Safe when exactly one interrupt is pending; otherwise pass an explicit\n * `options.interruptId` (and `options.namespace` for subgraph\n * interrupts).\n *\n * The server validates `namespace` against the pending interrupt. Root\n * interrupts use `namespace: []` (default when omitted). For subgraph\n * interrupts, copy `namespace` from `getThread()?.interrupts`.\n *\n * @example\n * ```ts\n * // Single pending interrupt\n * await stream.respond({ approved: true });\n * ```\n *\n * @example\n * ```ts\n * // Multiple root interrupts\n * for (const intr of stream.interrupts()) {\n * await stream.respond(decide(intr.value), { interruptId: intr.id! });\n * }\n * ```\n *\n * @example\n * ```ts\n * // Subgraph interrupt — namespace from `getThread()`\n * const thread = stream.getThread();\n * for (const entry of thread?.interrupts ?? []) {\n * await stream.respond(buildResponse(entry.payload), {\n * interruptId: entry.interruptId,\n * namespace: entry.namespace,\n * });\n * }\n * ```\n *\n * To resume several interrupts pending at the same checkpoint in one\n * command, use {@link respondAll}.\n */\n respond(\n response: unknown,\n options?: StreamRespondOptions<ConfigurableType>\n ): Promise<void>;\n\n /**\n * Resume several pending interrupts at the same checkpoint in a single\n * command — required when a run pauses on multiple interrupts at once\n * (e.g. parallel tool-authorization prompts), which sequential\n * {@link respond} calls cannot handle. `responsesById` maps each pending\n * `interruptId` to its response, so different interrupts can receive\n * different payloads. Pass `options.config` / `options.metadata` to fold\n * run-level config and metadata into the resumed run, mirroring\n * `submit()`.\n *\n * @example\n * ```ts\n * await stream.respondAll({\n * [interruptA.id]: { approved: true },\n * [interruptB.id]: { approved: false },\n * });\n * ```\n */\n respondAll(\n responsesById: Record<string, unknown>,\n options?: StreamRespondAllOptions<ConfigurableType>\n ): Promise<void>;\n\n // ----- identity -----\n /** LangGraph SDK client used to construct thread streams. */\n readonly client: Client;\n /** Assistant id the thread is bound to for its lifetime. */\n readonly assistantId: string;\n\n /**\n * Returns the bound {@link ThreadStream}, if one exists (`undefined`\n * until the thread is hydrated or the first submit completes). Prefer\n * the projections and selector primitives for UI work; use this for\n * low-level protocol access (raw subscriptions, state commands, etc.).\n */\n getThread(): ThreadStream | undefined;\n\n /** @internal Used by selector primitives. */\n readonly [STREAM_CONTROLLER]: StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >;\n}\n\n/**\n * Erased handle useful as a parameter type for helper components that\n * pass a `stream` through to selector primitives without reading\n * `values` directly. Mirrors the React/Vue `AnyStream` alias.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyStream = UseStreamReturn<any, any, any>;\n\n/**\n * Convenience alias — the fully-resolved return type of\n * {@link useStream} for a given source type `T`.\n */\nexport type StreamApi<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * React-compatible alias for the fully-resolved stream handle type.\n * Angular docs prefer {@link StreamApi}, but shared libraries can use\n * this name across framework bindings.\n */\nexport type UseStreamResult<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * Framework-free factory that constructs a {@link StreamController}\n * and wraps its stores in Angular Signals. Callers must supply the\n * {@link DestroyRef} that owns the controller's lifetime — it's\n * already captured by the public `injectStream` helper.\n *\n * Exported for advanced callers (e.g. testing utilities, custom\n * factories) that prefer to manage injection scope themselves.\n */\nexport function useStream<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n>(\n options: UseStreamOptions<InferStateType<T>>,\n destroyRef?: DestroyRef\n): UseStreamReturn<T, InterruptType, ConfigurableType> {\n type StateType = InferStateType<T>;\n\n interface OptionsBag {\n assistantId?: string;\n threadId?: string | null | Signal<string | null | undefined>;\n client?: Client;\n apiUrl?: string;\n apiKey?: string;\n callerOptions?: ClientConfig[\"callerOptions\"];\n defaultHeaders?: ClientConfig[\"defaultHeaders\"];\n transport?: \"sse\" | \"websocket\" | AgentServerAdapter;\n fetch?: typeof fetch;\n webSocketFactory?: (url: string) => WebSocket;\n onThreadId?: (threadId: string) => void;\n onCreated?: (info: RunExecutionInfo) => void;\n onCompleted?: (info: RunCompletedInfo) => void;\n initialValues?: StateType;\n messagesKey?: string;\n tools?: AnyHeadlessToolImplementation[];\n onTool?: OnToolCallback;\n optimistic?: boolean;\n }\n const asBag = options as OptionsBag;\n\n const hasCustomAdapter =\n asBag.transport != null && typeof asBag.transport !== \"string\";\n const transport = asBag.transport;\n\n const client: Client =\n asBag.client ??\n (new ClientCtor({\n apiUrl: asBag.apiUrl,\n apiKey: asBag.apiKey,\n callerOptions: asBag.callerOptions,\n defaultHeaders: asBag.defaultHeaders,\n }) as unknown as Client);\n\n // Custom adapters may omit `assistantId`; the controller still\n // requires one so it has something to forward to `threads.stream`.\n const sentinel = \"_\";\n const assistantId =\n \"assistantId\" in options ? (options.assistantId ?? sentinel) : sentinel;\n\n // Normalize threadId input to a signal — callers may pass plain\n // values, nulls, or their own signals.\n const threadIdInput: Signal<string | null> = (() => {\n const raw = asBag.threadId;\n if (isSignal(raw)) {\n return computed(\n () => (raw as Signal<string | null | undefined>)() ?? null\n );\n }\n const initial: string | null = (raw as string | null | undefined) ?? null;\n return signal(initial) as unknown as Signal<string | null>;\n })();\n\n const controller = new StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >({\n assistantId,\n // Cast: the runtime `Client` is state-shape agnostic, but the\n // controller declares `client: Client<StateType>` for its own\n // typings. Same cast is applied in the React/Vue bindings.\n client: client as unknown as Client<StateType>,\n threadId: untracked(() => threadIdInput()),\n transport,\n fetch: hasCustomAdapter ? undefined : asBag.fetch,\n webSocketFactory: hasCustomAdapter ? undefined : asBag.webSocketFactory,\n onThreadId: options.onThreadId,\n onCreated: options.onCreated,\n onCompleted: options.onCompleted,\n initialValues: options.initialValues,\n messagesKey: options.messagesKey,\n optimistic: asBag.optimistic,\n });\n\n // Deferred dispose — matches the React `useEffect(() =>\n // controller.activate())` and Vue `onScopeDispose(deactivate)`\n // patterns. HMR / scope-reuse scenarios stay clean because\n // `activate()` cancels the pending dispose if the scope survives.\n const deactivate = controller.activate();\n const ref = destroyRef ?? inject(DestroyRef);\n ref.onDestroy(deactivate);\n\n // ─── Reactivity bridge: StreamStore → Signal ────────────────────────\n function bindStore<S>(\n subscribe: (listener: () => void) => () => void,\n getSnapshot: () => S\n ): Signal<S> {\n const s = signal<S>(getSnapshot());\n const unsubscribe = subscribe(() => {\n s.set(getSnapshot());\n });\n ref.onDestroy(unsubscribe);\n return computed(() => s());\n }\n\n const rootSignal = bindStore<RootSnapshot<StateType, InterruptType>>(\n controller.rootStore.subscribe,\n controller.rootStore.getSnapshot\n );\n const subagentSignal = bindStore<SubagentMap>(\n controller.subagentStore.subscribe,\n controller.subagentStore.getSnapshot\n );\n const subgraphSignal = bindStore<SubgraphMap>(\n controller.subgraphStore.subscribe,\n controller.subgraphStore.getSnapshot\n );\n const subgraphByNodeSignal = bindStore<SubgraphByNodeMap>(\n controller.subgraphByNodeStore.subscribe,\n controller.subgraphByNodeStore.getSnapshot\n );\n\n const values = computed(() => rootSignal().values);\n const messages = computed(() => rootSignal().messages);\n const toolCalls = computed(\n () => rootSignal().toolCalls as InferToolCalls<T>[]\n );\n const interrupts = computed(() =>\n filterOutHeadlessToolInterrupts(rootSignal().interrupts)\n );\n const interrupt = computed(() => interrupts()[0]);\n const isLoading = computed(() => rootSignal().isLoading);\n const isThreadLoading = computed(() => rootSignal().isThreadLoading);\n const error = computed(() => rootSignal().error);\n const threadId = computed(() => rootSignal().threadId);\n\n // `hydrationPromise` is a property on the controller that gets\n // swapped on every `hydrate()` call. Exposing it as a signal lets\n // templates `await stream.hydrationPromise()` reactively; we\n // refresh the reference when the root store settles a new promise.\n const hydrationPromise = computed(() => {\n rootSignal();\n return controller.hydrationPromise;\n });\n\n // ─── threadId reactivity ────────────────────────────────────────────\n //\n // Re-hydrate whenever the caller's threadId input changes after\n // construction. The initial hydrate already fired synchronously in\n // the controller constructor, so we compare against the snapshot\n // captured at construction time rather than blindly skipping the\n // first run — in Angular, `@Input()` bindings apply *between*\n // construction and the first effect tick, so the first read can\n // legitimately be a different (updated) value that needs to\n // hydrate.\n const initialThreadId = untracked(() => threadIdInput()) ?? null;\n let lastAppliedThreadId: string | null = initialThreadId;\n effect(() => {\n const next = threadIdInput() ?? null;\n if (next === lastAppliedThreadId) return;\n lastAppliedThreadId = next;\n untracked(() => {\n void controller.hydrate(next);\n });\n });\n\n // ─── Headless-tool handling ─────────────────────────────────────────\n const tools = options.tools;\n const onTool = options.onTool;\n if (tools?.length) {\n const handledTools = new Set<string>();\n\n // Clear the dedup set whenever the thread id changes.\n effect(() => {\n threadIdInput();\n untracked(() => handledTools.clear());\n });\n\n effect(() => {\n rootSignal();\n untracked(() => {\n scheduleCoalescedHeadlessToolFlush(handledTools, () => {\n const snapshot = rootSignal();\n const bag = snapshot.values as unknown as Record<string, unknown>;\n const protocolInterrupts =\n snapshot.interrupts as unknown as Interrupt[];\n const valuesInterrupts = Array.isArray(bag?.__interrupt__)\n ? (bag.__interrupt__ as Interrupt[])\n : [];\n const headlessInterrupts =\n protocolInterrupts.length > 0\n ? protocolInterrupts\n : valuesInterrupts;\n if (headlessInterrupts.length === 0) return;\n flushPendingHeadlessToolInterrupts(\n { ...bag, __interrupt__: headlessInterrupts },\n tools,\n handledTools,\n {\n onTool,\n defer: (run) => {\n void Promise.resolve().then(run);\n },\n resumeSubmit: (command) =>\n applyHeadlessToolResumeCommand(controller, command),\n }\n );\n });\n });\n });\n }\n\n const handle: UseStreamReturn<T, InterruptType, ConfigurableType> = {\n values: values as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"values\"],\n messages,\n toolCalls,\n interrupts,\n interrupt,\n isLoading,\n isThreadLoading,\n error,\n threadId,\n hydrationPromise,\n subagents: subagentSignal as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"subagents\"],\n subgraphs: subgraphSignal,\n subgraphsByNode: subgraphByNodeSignal,\n submit: (input, submitOptions) => controller.submit(input, submitOptions),\n stop: (options) => controller.stop(options),\n disconnect: () => controller.disconnect(),\n respond: (response, options) => controller.respond(response, options),\n respondAll: (responsesById, options) =>\n controller.respondAll(responsesById, options),\n getThread: () => controller.getThread(),\n client,\n assistantId,\n [STREAM_CONTROLLER]: controller,\n };\n\n return handle;\n}\n\n/**\n * Helper used by the selector primitives to reach the underlying\n * {@link ChannelRegistry} from a stream handle. Kept internal —\n * application code should call `injectMessages`, `injectToolCalls`,\n * etc. instead of reading this directly.\n *\n * @internal\n */\nexport function getRegistry(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: UseStreamReturn<any, any, any>\n): ChannelRegistry {\n return stream[STREAM_CONTROLLER].registry;\n}\n\nexport type { ThreadStream };\n"],"mappings":";;;;;;;;;;;;AA2EA,MAAa,oBAAmC,OAAO,IACrD,gCACD;;;;;;;;;;AAqTD,SAAgB,UAKd,SACA,YACqD;CAuBrD,MAAM,QAAQ;CAEd,MAAM,mBACJ,MAAM,aAAa,QAAQ,OAAO,MAAM,cAAc;CACxD,MAAM,YAAY,MAAM;CAExB,MAAM,SACJ,MAAM,UACL,IAAIA,gCAAAA,OAAW;EACd,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,eAAe,MAAM;EACrB,gBAAgB,MAAM;EACvB,CAAC;CAIJ,MAAM,WAAW;CACjB,MAAM,cACJ,iBAAiB,UAAW,QAAQ,eAAe,WAAY;CAIjE,MAAM,uBAA8C;EAClD,MAAM,MAAM,MAAM;AAClB,OAAA,GAAA,cAAA,UAAa,IAAI,CACf,SAAA,GAAA,cAAA,gBACS,KAA2C,IAAI,KACvD;AAGH,UAAA,GAAA,cAAA,QADgC,OAAqC,KAC/C;KACpB;CAEJ,MAAM,aAAa,IAAIC,gCAAAA,iBAIrB;EACA;EAIQ;EACR,WAAA,GAAA,cAAA,iBAA0B,eAAe,CAAC;EAC1C;EACA,OAAO,mBAAmB,KAAA,IAAY,MAAM;EAC5C,kBAAkB,mBAAmB,KAAA,IAAY,MAAM;EACvD,YAAY,QAAQ;EACpB,WAAW,QAAQ;EACnB,aAAa,QAAQ;EACrB,eAAe,QAAQ;EACvB,aAAa,QAAQ;EACrB,YAAY,MAAM;EACnB,CAAC;CAMF,MAAM,aAAa,WAAW,UAAU;CACxC,MAAM,MAAM,eAAA,GAAA,cAAA,QAAqBC,cAAAA,WAAW;AAC5C,KAAI,UAAU,WAAW;CAGzB,SAAS,UACP,WACA,aACW;EACX,MAAM,KAAA,GAAA,cAAA,QAAc,aAAa,CAAC;EAClC,MAAM,cAAc,gBAAgB;AAClC,KAAE,IAAI,aAAa,C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reated,
|
|
49
49
|
onCompleted: options.onCompleted,
|
|
50
50
|
initialValues: options.initialValues,
|
|
51
|
-
messagesKey: options.messagesKey
|
|
51
|
+
messagesKey: options.messagesKey,
|
|
52
|
+
optimistic: asBag.optimistic
|
|
52
53
|
});
|
|
53
54
|
const deactivate = controller.activate();
|
|
54
55
|
const ref = destroyRef ?? inject(DestroyRef);
|
package/dist/use-stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-stream.js","names":["ClientCtor"],"sources":["../src/use-stream.ts"],"sourcesContent":["import {\n DestroyRef,\n computed,\n effect,\n inject,\n isSignal,\n signal,\n untracked,\n type Signal,\n} from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Client, Interrupt } from \"@langchain/langgraph-sdk\";\nimport {\n applyHeadlessToolResumeCommand,\n filterOutHeadlessToolInterrupts,\n flushPendingHeadlessToolInterrupts,\n scheduleCoalescedHeadlessToolFlush,\n type AnyHeadlessToolImplementation,\n type OnToolCallback,\n} from \"@langchain/langgraph-sdk\";\nimport {\n Client as ClientCtor,\n type ClientConfig,\n type ThreadStream,\n} from \"@langchain/langgraph-sdk/client\";\nimport {\n StreamController,\n type AgentServerAdapter,\n type AgentServerOptions as StreamAgentServerOptions,\n type ChannelRegistry,\n type CustomAdapterOptions as StreamCustomAdapterOptions,\n type InferStateType,\n type InferToolCalls,\n type InferSubagentStates,\n type RootSnapshot,\n type RunCompletedInfo,\n type RunExecutionInfo,\n type StreamRespondAllOptions,\n type StreamRespondOptions,\n type StreamStopOptions,\n type StreamSubmitOptions,\n type SubagentDiscoverySnapshot,\n type SubagentMap,\n type SubgraphByNodeMap,\n type SubgraphDiscoverySnapshot,\n type SubgraphMap,\n type UseStreamOptions as StreamUseStreamOptions,\n type WidenUpdateMessages,\n} from \"@langchain/langgraph-sdk/stream\";\n\ntype AngularThreadId = string | null | Signal<string | null | undefined>;\n\nexport type AgentServerOptions<StateType extends object> =\n StreamAgentServerOptions<StateType, AngularThreadId>;\n\nexport type CustomAdapterOptions<StateType extends object> =\n StreamCustomAdapterOptions<StateType, AngularThreadId, string>;\n\nexport type UseStreamOptions<\n StateType extends object = Record<string, unknown>,\n> = StreamUseStreamOptions<\n StateType,\n AngularThreadId,\n string | undefined,\n string | undefined,\n string\n>;\n\n/**\n * Private field on the handle that carries the\n * {@link StreamController} reference. Selector primitives read this\n * to reach the shared {@link ChannelRegistry}. Use the companion\n * `inject*` selectors (`injectMessages`, `injectToolCalls`,\n * `injectValues`, …) instead of reading this directly.\n */\nexport const STREAM_CONTROLLER: unique symbol = Symbol.for(\n \"@langchain/angular/controller\"\n);\n\n/**\n * Return shape of {@link useStream} — the Angular `StreamApi`.\n *\n * Reactivity primitives follow Angular conventions:\n *\n * - Data projections are `Signal<T>`; call them as functions in\n * templates (`stream.messages()`). They are snapshots — never\n * mutate the returned arrays / maps.\n * - Imperative methods (`submit` / `stop` / `respond`) are plain\n * functions. No `WritableSignal`s are exposed on the root handle.\n * - Identity values captured at construction time (`client`,\n * `assistantId`) are exposed as plain values; remount the\n * component to swap them.\n */\nexport interface UseStreamReturn<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n StateType extends object = InferStateType<T>,\n SubagentStates = InferSubagentStates<T>,\n> {\n // ----- always-on root projections -----\n /**\n * The most recent `values`-channel snapshot emitted at the root\n * namespace — i.e. the thread-level state as the server sees it\n * after each superstep. Updated on every root `values` event, not\n * on token-level deltas: if you render `stream.values().messages`\n * directly you'll see full turns appear at once instead of\n * streaming token-by-token. Use {@link messages} (or\n * `injectMessages`) for the token-streamed view.\n *\n * Equivalent to calling `injectValues(stream)`.\n */\n readonly values: Signal<StateType>;\n /**\n * The root message projection. Assembled from two sources and\n * merged in real time:\n *\n * 1. `messages`-channel deltas — token-level streaming events\n * (`message-start`, `content-block-delta`, `message-finish`)\n * emitted by the runtime. These drive live, token-by-token\n * updates.\n * 2. `values.messages` snapshots — the authoritative ordering\n * and any messages the agent produces without token streaming\n * (human turns, tool results, echoes from subagents).\n *\n * If the backend only emits `values` events (no `messages`\n * channel), every message will appear fully-formed on each\n * values update rather than streaming. This is a backend/runtime\n * concern — the Angular layer faithfully renders whatever the\n * server sends.\n *\n * Equivalent to calling `injectMessages(stream)` with no target.\n */\n readonly messages: Signal<BaseMessage[]>;\n /**\n * Root-namespace tool calls assembled from the `tools` channel.\n * Each entry is a fully parsed {@link AssembledToolCall} with\n * name, args, and id — suitable for rendering approval UIs or\n * forwarding to headless tool handlers.\n *\n * When the stream is typed with an agent brand or tool list,\n * entries are narrowed via {@link InferToolCalls}. Equivalent to\n * calling `injectToolCalls(stream)` with no target.\n */\n readonly toolCalls: Signal<InferToolCalls<T>[]>;\n /**\n * All unresolved protocol interrupts observed on the root\n * namespace during the active thread. Populated from lifecycle /\n * input events and seeded on hydration from `thread.getState()`.\n * Cleared optimistically when a new run starts or an interrupt is\n * resolved via {@link respond}.\n */\n readonly interrupts: Signal<Interrupt<InterruptType>[]>;\n /**\n * Convenience alias for {@link interrupts}[0] — the primary\n * interrupt most UIs should act on when only one is pending.\n * `undefined` when no interrupt is active.\n */\n readonly interrupt: Signal<Interrupt<InterruptType> | undefined>;\n /**\n * `true` while a run is active or being started on the current\n * thread. Driven by root-namespace lifecycle events (`running` →\n * `true`, terminal phases → `false`). Use this to disable submit\n * buttons and show in-flight spinners.\n */\n readonly isLoading: Signal<boolean>;\n /**\n * `true` while the initial `thread.getState()` hydration for the\n * active thread is in flight. Distinct from {@link isLoading} —\n * thread loading covers the one-time fetch that seeds\n * {@link values} / {@link messages} before any user submit.\n */\n readonly isThreadLoading: Signal<boolean>;\n /**\n * The last error observed on the active run or hydration attempt.\n * `undefined` when no error has occurred. Cleared optimistically\n * when a new {@link submit} starts.\n */\n readonly error: Signal<unknown>;\n /**\n * Id of the thread the controller is bound to. `null` until the\n * first {@link submit} creates or selects a thread (or until an\n * explicit `threadId` option is provided and hydrated).\n */\n readonly threadId: Signal<string | null>;\n /**\n * Promise that settles when the active thread's initial hydration\n * completes. Exposed so SSR/render-before-flush pipelines can\n * `await stream.hydrationPromise()` before serialising. A fresh\n * promise is installed on every `threadId` change.\n */\n readonly hydrationPromise: Signal<Promise<void>>;\n\n // ----- always-on discovery -----\n /**\n * Subagents discovered on the root run. For DeepAgent-typed\n * streams the key set is narrowed to the subagent names declared\n * on the agent brand (`keyof InferSubagentStates<T>`).\n */\n readonly subagents: Signal<\n ReadonlyMap<\n keyof SubagentStates & string extends never\n ? string\n : keyof SubagentStates & string,\n SubagentDiscoverySnapshot\n >\n >;\n /**\n * Subgraphs discovered on the root run.\n *\n * A namespace is classified as a subgraph iff at least one\n * strictly-deeper namespace has been observed with it as a prefix.\n * This is inferred from the lifecycle event stream — plain function\n * nodes (`orchestrator`, `writer` in the nested-stategraph example)\n * never appear here even though the server emits namespaced\n * lifecycle events for them. Promotion is monotonic and retroactive;\n * an entry appears as soon as the first descendant event lands.\n */\n readonly subgraphs: Signal<ReadonlyMap<string, SubgraphDiscoverySnapshot>>;\n /**\n * Subgraphs indexed by the graph node that produced them\n * (`addNode(\"visualizer_0\", …)`). Each value is an array because\n * parallel fan-outs and loops can spawn multiple invocations of\n * the same node; arrays preserve insertion order. Updates in\n * lock-step with {@link subgraphs}.\n */\n readonly subgraphsByNode: Signal<\n ReadonlyMap<string, readonly SubgraphDiscoverySnapshot[]>\n >;\n\n // ----- imperatives -----\n /**\n * Dispatch a new run on the bound thread.\n *\n * `input` is typed as `Partial<StateType>` so IDE autocompletion\n * surfaces the state keys declared on the root primitive.\n */\n submit(\n input: WidenUpdateMessages<Partial<StateType>> | null | undefined,\n options?: StreamSubmitOptions<StateType, ConfigurableType>\n ): Promise<void>;\n /**\n * Stop the active run on the current thread. By default cancels the\n * run server-side and disconnects the client; pass `{ cancel: false }`\n * or use {@link disconnect} for join/rejoin. Sets {@link isLoading} to\n * `false` immediately; {@link values} and {@link messages} are preserved.\n */\n stop(options?: StreamStopOptions): Promise<void>;\n /**\n * Disconnect the client without cancelling the run server-side.\n * Alias for `stop({ cancel: false })`.\n */\n disconnect(): Promise<void>;\n /**\n * Resume a pending protocol interrupt by sending a response payload\n * back to the interrupted namespace.\n *\n * When `options.interruptId` is omitted, walks `getThread()?.interrupts`\n * from newest to oldest and resumes the first not yet resolved by a prior\n * `respond()` call. That may be a root or subgraph interrupt and is\n * **not** necessarily {@link interrupt} (`interrupts()[0]`, root-only).\n * Safe when exactly one interrupt is pending; otherwise pass an explicit\n * `options.interruptId` (and `options.namespace` for subgraph\n * interrupts).\n *\n * The server validates `namespace` against the pending interrupt. Root\n * interrupts use `namespace: []` (default when omitted). For subgraph\n * interrupts, copy `namespace` from `getThread()?.interrupts`.\n *\n * @example\n * ```ts\n * // Single pending interrupt\n * await stream.respond({ approved: true });\n * ```\n *\n * @example\n * ```ts\n * // Multiple root interrupts\n * for (const intr of stream.interrupts()) {\n * await stream.respond(decide(intr.value), { interruptId: intr.id! });\n * }\n * ```\n *\n * @example\n * ```ts\n * // Subgraph interrupt — namespace from `getThread()`\n * const thread = stream.getThread();\n * for (const entry of thread?.interrupts ?? []) {\n * await stream.respond(buildResponse(entry.payload), {\n * interruptId: entry.interruptId,\n * namespace: entry.namespace,\n * });\n * }\n * ```\n *\n * To resume several interrupts pending at the same checkpoint in one\n * command, use {@link respondAll}.\n */\n respond(\n response: unknown,\n options?: StreamRespondOptions<ConfigurableType>\n ): Promise<void>;\n\n /**\n * Resume several pending interrupts at the same checkpoint in a single\n * command — required when a run pauses on multiple interrupts at once\n * (e.g. parallel tool-authorization prompts), which sequential\n * {@link respond} calls cannot handle. `responsesById` maps each pending\n * `interruptId` to its response, so different interrupts can receive\n * different payloads. Pass `options.config` / `options.metadata` to fold\n * run-level config and metadata into the resumed run, mirroring\n * `submit()`.\n *\n * @example\n * ```ts\n * await stream.respondAll({\n * [interruptA.id]: { approved: true },\n * [interruptB.id]: { approved: false },\n * });\n * ```\n */\n respondAll(\n responsesById: Record<string, unknown>,\n options?: StreamRespondAllOptions<ConfigurableType>\n ): Promise<void>;\n\n // ----- identity -----\n /** LangGraph SDK client used to construct thread streams. */\n readonly client: Client;\n /** Assistant id the thread is bound to for its lifetime. */\n readonly assistantId: string;\n\n /**\n * Returns the bound {@link ThreadStream}, if one exists (`undefined`\n * until the thread is hydrated or the first submit completes). Prefer\n * the projections and selector primitives for UI work; use this for\n * low-level protocol access (raw subscriptions, state commands, etc.).\n */\n getThread(): ThreadStream | undefined;\n\n /** @internal Used by selector primitives. */\n readonly [STREAM_CONTROLLER]: StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >;\n}\n\n/**\n * Erased handle useful as a parameter type for helper components that\n * pass a `stream` through to selector primitives without reading\n * `values` directly. Mirrors the React/Vue `AnyStream` alias.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyStream = UseStreamReturn<any, any, any>;\n\n/**\n * Convenience alias — the fully-resolved return type of\n * {@link useStream} for a given source type `T`.\n */\nexport type StreamApi<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * React-compatible alias for the fully-resolved stream handle type.\n * Angular docs prefer {@link StreamApi}, but shared libraries can use\n * this name across framework bindings.\n */\nexport type UseStreamResult<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * Framework-free factory that constructs a {@link StreamController}\n * and wraps its stores in Angular Signals. Callers must supply the\n * {@link DestroyRef} that owns the controller's lifetime — it's\n * already captured by the public `injectStream` helper.\n *\n * Exported for advanced callers (e.g. testing utilities, custom\n * factories) that prefer to manage injection scope themselves.\n */\nexport function useStream<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n>(\n options: UseStreamOptions<InferStateType<T>>,\n destroyRef?: DestroyRef\n): UseStreamReturn<T, InterruptType, ConfigurableType> {\n type StateType = InferStateType<T>;\n\n interface OptionsBag {\n assistantId?: string;\n threadId?: string | null | Signal<string | null | undefined>;\n client?: Client;\n apiUrl?: string;\n apiKey?: string;\n callerOptions?: ClientConfig[\"callerOptions\"];\n defaultHeaders?: ClientConfig[\"defaultHeaders\"];\n transport?: \"sse\" | \"websocket\" | AgentServerAdapter;\n fetch?: typeof fetch;\n webSocketFactory?: (url: string) => WebSocket;\n onThreadId?: (threadId: string) => void;\n onCreated?: (info: RunExecutionInfo) => void;\n onCompleted?: (info: RunCompletedInfo) => void;\n initialValues?: StateType;\n messagesKey?: string;\n tools?: AnyHeadlessToolImplementation[];\n onTool?: OnToolCallback;\n }\n const asBag = options as OptionsBag;\n\n const hasCustomAdapter =\n asBag.transport != null && typeof asBag.transport !== \"string\";\n const transport = asBag.transport;\n\n const client: Client =\n asBag.client ??\n (new ClientCtor({\n apiUrl: asBag.apiUrl,\n apiKey: asBag.apiKey,\n callerOptions: asBag.callerOptions,\n defaultHeaders: asBag.defaultHeaders,\n }) as unknown as Client);\n\n // Custom adapters may omit `assistantId`; the controller still\n // requires one so it has something to forward to `threads.stream`.\n const sentinel = \"_\";\n const assistantId =\n \"assistantId\" in options ? (options.assistantId ?? sentinel) : sentinel;\n\n // Normalize threadId input to a signal — callers may pass plain\n // values, nulls, or their own signals.\n const threadIdInput: Signal<string | null> = (() => {\n const raw = asBag.threadId;\n if (isSignal(raw)) {\n return computed(\n () => (raw as Signal<string | null | undefined>)() ?? null\n );\n }\n const initial: string | null = (raw as string | null | undefined) ?? null;\n return signal(initial) as unknown as Signal<string | null>;\n })();\n\n const controller = new StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >({\n assistantId,\n // Cast: the runtime `Client` is state-shape agnostic, but the\n // controller declares `client: Client<StateType>` for its own\n // typings. Same cast is applied in the React/Vue bindings.\n client: client as unknown as Client<StateType>,\n threadId: untracked(() => threadIdInput()),\n transport,\n fetch: hasCustomAdapter ? undefined : asBag.fetch,\n webSocketFactory: hasCustomAdapter ? undefined : asBag.webSocketFactory,\n onThreadId: options.onThreadId,\n onCreated: options.onCreated,\n onCompleted: options.onCompleted,\n initialValues: options.initialValues,\n messagesKey: options.messagesKey,\n });\n\n // Deferred dispose — matches the React `useEffect(() =>\n // controller.activate())` and Vue `onScopeDispose(deactivate)`\n // patterns. HMR / scope-reuse scenarios stay clean because\n // `activate()` cancels the pending dispose if the scope survives.\n const deactivate = controller.activate();\n const ref = destroyRef ?? inject(DestroyRef);\n ref.onDestroy(deactivate);\n\n // ─── Reactivity bridge: StreamStore → Signal ────────────────────────\n function bindStore<S>(\n subscribe: (listener: () => void) => () => void,\n getSnapshot: () => S\n ): Signal<S> {\n const s = signal<S>(getSnapshot());\n const unsubscribe = subscribe(() => {\n s.set(getSnapshot());\n });\n ref.onDestroy(unsubscribe);\n return computed(() => s());\n }\n\n const rootSignal = bindStore<RootSnapshot<StateType, InterruptType>>(\n controller.rootStore.subscribe,\n controller.rootStore.getSnapshot\n );\n const subagentSignal = bindStore<SubagentMap>(\n controller.subagentStore.subscribe,\n controller.subagentStore.getSnapshot\n );\n const subgraphSignal = bindStore<SubgraphMap>(\n controller.subgraphStore.subscribe,\n controller.subgraphStore.getSnapshot\n );\n const subgraphByNodeSignal = bindStore<SubgraphByNodeMap>(\n controller.subgraphByNodeStore.subscribe,\n controller.subgraphByNodeStore.getSnapshot\n );\n\n const values = computed(() => rootSignal().values);\n const messages = computed(() => rootSignal().messages);\n const toolCalls = computed(\n () => rootSignal().toolCalls as InferToolCalls<T>[]\n );\n const interrupts = computed(() =>\n filterOutHeadlessToolInterrupts(rootSignal().interrupts)\n );\n const interrupt = computed(() => interrupts()[0]);\n const isLoading = computed(() => rootSignal().isLoading);\n const isThreadLoading = computed(() => rootSignal().isThreadLoading);\n const error = computed(() => rootSignal().error);\n const threadId = computed(() => rootSignal().threadId);\n\n // `hydrationPromise` is a property on the controller that gets\n // swapped on every `hydrate()` call. Exposing it as a signal lets\n // templates `await stream.hydrationPromise()` reactively; we\n // refresh the reference when the root store settles a new promise.\n const hydrationPromise = computed(() => {\n rootSignal();\n return controller.hydrationPromise;\n });\n\n // ─── threadId reactivity ────────────────────────────────────────────\n //\n // Re-hydrate whenever the caller's threadId input changes after\n // construction. The initial hydrate already fired synchronously in\n // the controller constructor, so we compare against the snapshot\n // captured at construction time rather than blindly skipping the\n // first run — in Angular, `@Input()` bindings apply *between*\n // construction and the first effect tick, so the first read can\n // legitimately be a different (updated) value that needs to\n // hydrate.\n const initialThreadId = untracked(() => threadIdInput()) ?? null;\n let lastAppliedThreadId: string | null = initialThreadId;\n effect(() => {\n const next = threadIdInput() ?? null;\n if (next === lastAppliedThreadId) return;\n lastAppliedThreadId = next;\n untracked(() => {\n void controller.hydrate(next);\n });\n });\n\n // ─── Headless-tool handling ─────────────────────────────────────────\n const tools = options.tools;\n const onTool = options.onTool;\n if (tools?.length) {\n const handledTools = new Set<string>();\n\n // Clear the dedup set whenever the thread id changes.\n effect(() => {\n threadIdInput();\n untracked(() => handledTools.clear());\n });\n\n effect(() => {\n rootSignal();\n untracked(() => {\n scheduleCoalescedHeadlessToolFlush(handledTools, () => {\n const snapshot = rootSignal();\n const bag = snapshot.values as unknown as Record<string, unknown>;\n const protocolInterrupts =\n snapshot.interrupts as unknown as Interrupt[];\n const valuesInterrupts = Array.isArray(bag?.__interrupt__)\n ? (bag.__interrupt__ as Interrupt[])\n : [];\n const headlessInterrupts =\n protocolInterrupts.length > 0\n ? protocolInterrupts\n : valuesInterrupts;\n if (headlessInterrupts.length === 0) return;\n flushPendingHeadlessToolInterrupts(\n { ...bag, __interrupt__: headlessInterrupts },\n tools,\n handledTools,\n {\n onTool,\n defer: (run) => {\n void Promise.resolve().then(run);\n },\n resumeSubmit: (command) =>\n applyHeadlessToolResumeCommand(controller, command),\n }\n );\n });\n });\n });\n }\n\n const handle: UseStreamReturn<T, InterruptType, ConfigurableType> = {\n values: values as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"values\"],\n messages,\n toolCalls,\n interrupts,\n interrupt,\n isLoading,\n isThreadLoading,\n error,\n threadId,\n hydrationPromise,\n subagents: subagentSignal as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"subagents\"],\n subgraphs: subgraphSignal,\n subgraphsByNode: subgraphByNodeSignal,\n submit: (input, submitOptions) => controller.submit(input, submitOptions),\n stop: (options) => controller.stop(options),\n disconnect: () => controller.disconnect(),\n respond: (response, options) => controller.respond(response, options),\n respondAll: (responsesById, options) =>\n controller.respondAll(responsesById, options),\n getThread: () => controller.getThread(),\n client,\n assistantId,\n [STREAM_CONTROLLER]: controller,\n };\n\n return handle;\n}\n\n/**\n * Helper used by the selector primitives to reach the underlying\n * {@link ChannelRegistry} from a stream handle. Kept internal —\n * application code should call `injectMessages`, `injectToolCalls`,\n * etc. instead of reading this directly.\n *\n * @internal\n */\nexport function getRegistry(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: UseStreamReturn<any, any, any>\n): ChannelRegistry {\n return stream[STREAM_CONTROLLER].registry;\n}\n\nexport type { ThreadStream };\n"],"mappings":";;;;;;;;;;;;AA2EA,MAAa,oBAAmC,OAAO,IACrD,gCACD;;;;;;;;;;AAqTD,SAAgB,UAKd,SACA,YACqD;CAsBrD,MAAM,QAAQ;CAEd,MAAM,mBACJ,MAAM,aAAa,QAAQ,OAAO,MAAM,cAAc;CACxD,MAAM,YAAY,MAAM;CAExB,MAAM,SACJ,MAAM,UACL,IAAIA,SAAW;EACd,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,eAAe,MAAM;EACrB,gBAAgB,MAAM;EACvB,CAAC;CAIJ,MAAM,WAAW;CACjB,MAAM,cACJ,iBAAiB,UAAW,QAAQ,eAAe,WAAY;CAIjE,MAAM,uBAA8C;EAClD,MAAM,MAAM,MAAM;AAClB,MAAI,SAAS,IAAI,CACf,QAAO,eACE,KAA2C,IAAI,KACvD;AAGH,SAAO,OADyB,OAAqC,KAC/C;KACpB;CAEJ,M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B,YAEd,QACiB;AACjB,QAAO,OAAO,mBAAmB"}
|
|
1
|
+
{"version":3,"file":"use-stream.js","names":["ClientCtor"],"sources":["../src/use-stream.ts"],"sourcesContent":["import {\n DestroyRef,\n computed,\n effect,\n inject,\n isSignal,\n signal,\n untracked,\n type Signal,\n} from \"@angular/core\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Client, Interrupt } from \"@langchain/langgraph-sdk\";\nimport {\n applyHeadlessToolResumeCommand,\n filterOutHeadlessToolInterrupts,\n flushPendingHeadlessToolInterrupts,\n scheduleCoalescedHeadlessToolFlush,\n type AnyHeadlessToolImplementation,\n type OnToolCallback,\n} from \"@langchain/langgraph-sdk\";\nimport {\n Client as ClientCtor,\n type ClientConfig,\n type ThreadStream,\n} from \"@langchain/langgraph-sdk/client\";\nimport {\n StreamController,\n type AgentServerAdapter,\n type AgentServerOptions as StreamAgentServerOptions,\n type ChannelRegistry,\n type CustomAdapterOptions as StreamCustomAdapterOptions,\n type InferStateType,\n type InferToolCalls,\n type InferSubagentStates,\n type RootSnapshot,\n type RunCompletedInfo,\n type RunExecutionInfo,\n type StreamRespondAllOptions,\n type StreamRespondOptions,\n type StreamStopOptions,\n type StreamSubmitOptions,\n type SubagentDiscoverySnapshot,\n type SubagentMap,\n type SubgraphByNodeMap,\n type SubgraphDiscoverySnapshot,\n type SubgraphMap,\n type UseStreamOptions as StreamUseStreamOptions,\n type WidenUpdateMessages,\n} from \"@langchain/langgraph-sdk/stream\";\n\ntype AngularThreadId = string | null | Signal<string | null | undefined>;\n\nexport type AgentServerOptions<StateType extends object> =\n StreamAgentServerOptions<StateType, AngularThreadId>;\n\nexport type CustomAdapterOptions<StateType extends object> =\n StreamCustomAdapterOptions<StateType, AngularThreadId, string>;\n\nexport type UseStreamOptions<\n StateType extends object = Record<string, unknown>,\n> = StreamUseStreamOptions<\n StateType,\n AngularThreadId,\n string | undefined,\n string | undefined,\n string\n>;\n\n/**\n * Private field on the handle that carries the\n * {@link StreamController} reference. Selector primitives read this\n * to reach the shared {@link ChannelRegistry}. Use the companion\n * `inject*` selectors (`injectMessages`, `injectToolCalls`,\n * `injectValues`, …) instead of reading this directly.\n */\nexport const STREAM_CONTROLLER: unique symbol = Symbol.for(\n \"@langchain/angular/controller\"\n);\n\n/**\n * Return shape of {@link useStream} — the Angular `StreamApi`.\n *\n * Reactivity primitives follow Angular conventions:\n *\n * - Data projections are `Signal<T>`; call them as functions in\n * templates (`stream.messages()`). They are snapshots — never\n * mutate the returned arrays / maps.\n * - Imperative methods (`submit` / `stop` / `respond`) are plain\n * functions. No `WritableSignal`s are exposed on the root handle.\n * - Identity values captured at construction time (`client`,\n * `assistantId`) are exposed as plain values; remount the\n * component to swap them.\n */\nexport interface UseStreamReturn<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n StateType extends object = InferStateType<T>,\n SubagentStates = InferSubagentStates<T>,\n> {\n // ----- always-on root projections -----\n /**\n * The most recent `values`-channel snapshot emitted at the root\n * namespace — i.e. the thread-level state as the server sees it\n * after each superstep. Updated on every root `values` event, not\n * on token-level deltas: if you render `stream.values().messages`\n * directly you'll see full turns appear at once instead of\n * streaming token-by-token. Use {@link messages} (or\n * `injectMessages`) for the token-streamed view.\n *\n * Equivalent to calling `injectValues(stream)`.\n */\n readonly values: Signal<StateType>;\n /**\n * The root message projection. Assembled from two sources and\n * merged in real time:\n *\n * 1. `messages`-channel deltas — token-level streaming events\n * (`message-start`, `content-block-delta`, `message-finish`)\n * emitted by the runtime. These drive live, token-by-token\n * updates.\n * 2. `values.messages` snapshots — the authoritative ordering\n * and any messages the agent produces without token streaming\n * (human turns, tool results, echoes from subagents).\n *\n * If the backend only emits `values` events (no `messages`\n * channel), every message will appear fully-formed on each\n * values update rather than streaming. This is a backend/runtime\n * concern — the Angular layer faithfully renders whatever the\n * server sends.\n *\n * Equivalent to calling `injectMessages(stream)` with no target.\n */\n readonly messages: Signal<BaseMessage[]>;\n /**\n * Root-namespace tool calls assembled from the `tools` channel.\n * Each entry is a fully parsed {@link AssembledToolCall} with\n * name, args, and id — suitable for rendering approval UIs or\n * forwarding to headless tool handlers.\n *\n * When the stream is typed with an agent brand or tool list,\n * entries are narrowed via {@link InferToolCalls}. Equivalent to\n * calling `injectToolCalls(stream)` with no target.\n */\n readonly toolCalls: Signal<InferToolCalls<T>[]>;\n /**\n * All unresolved protocol interrupts observed on the root\n * namespace during the active thread. Populated from lifecycle /\n * input events and seeded on hydration from `thread.getState()`.\n * Cleared optimistically when a new run starts or an interrupt is\n * resolved via {@link respond}.\n */\n readonly interrupts: Signal<Interrupt<InterruptType>[]>;\n /**\n * Convenience alias for {@link interrupts}[0] — the primary\n * interrupt most UIs should act on when only one is pending.\n * `undefined` when no interrupt is active.\n */\n readonly interrupt: Signal<Interrupt<InterruptType> | undefined>;\n /**\n * `true` while a run is active or being started on the current\n * thread. Driven by root-namespace lifecycle events (`running` →\n * `true`, terminal phases → `false`). Use this to disable submit\n * buttons and show in-flight spinners.\n */\n readonly isLoading: Signal<boolean>;\n /**\n * `true` while the initial `thread.getState()` hydration for the\n * active thread is in flight. Distinct from {@link isLoading} —\n * thread loading covers the one-time fetch that seeds\n * {@link values} / {@link messages} before any user submit.\n */\n readonly isThreadLoading: Signal<boolean>;\n /**\n * The last error observed on the active run or hydration attempt.\n * `undefined` when no error has occurred. Cleared optimistically\n * when a new {@link submit} starts.\n */\n readonly error: Signal<unknown>;\n /**\n * Id of the thread the controller is bound to. `null` until the\n * first {@link submit} creates or selects a thread (or until an\n * explicit `threadId` option is provided and hydrated).\n */\n readonly threadId: Signal<string | null>;\n /**\n * Promise that settles when the active thread's initial hydration\n * completes. Exposed so SSR/render-before-flush pipelines can\n * `await stream.hydrationPromise()` before serialising. A fresh\n * promise is installed on every `threadId` change.\n */\n readonly hydrationPromise: Signal<Promise<void>>;\n\n // ----- always-on discovery -----\n /**\n * Subagents discovered on the root run. For DeepAgent-typed\n * streams the key set is narrowed to the subagent names declared\n * on the agent brand (`keyof InferSubagentStates<T>`).\n */\n readonly subagents: Signal<\n ReadonlyMap<\n keyof SubagentStates & string extends never\n ? string\n : keyof SubagentStates & string,\n SubagentDiscoverySnapshot\n >\n >;\n /**\n * Subgraphs discovered on the root run.\n *\n * A namespace is classified as a subgraph iff at least one\n * strictly-deeper namespace has been observed with it as a prefix.\n * This is inferred from the lifecycle event stream — plain function\n * nodes (`orchestrator`, `writer` in the nested-stategraph example)\n * never appear here even though the server emits namespaced\n * lifecycle events for them. Promotion is monotonic and retroactive;\n * an entry appears as soon as the first descendant event lands.\n */\n readonly subgraphs: Signal<ReadonlyMap<string, SubgraphDiscoverySnapshot>>;\n /**\n * Subgraphs indexed by the graph node that produced them\n * (`addNode(\"visualizer_0\", …)`). Each value is an array because\n * parallel fan-outs and loops can spawn multiple invocations of\n * the same node; arrays preserve insertion order. Updates in\n * lock-step with {@link subgraphs}.\n */\n readonly subgraphsByNode: Signal<\n ReadonlyMap<string, readonly SubgraphDiscoverySnapshot[]>\n >;\n\n // ----- imperatives -----\n /**\n * Dispatch a new run on the bound thread.\n *\n * `input` is typed as `Partial<StateType>` so IDE autocompletion\n * surfaces the state keys declared on the root primitive.\n */\n submit(\n input: WidenUpdateMessages<Partial<StateType>> | null | undefined,\n options?: StreamSubmitOptions<StateType, ConfigurableType>\n ): Promise<void>;\n /**\n * Stop the active run on the current thread. By default cancels the\n * run server-side and disconnects the client; pass `{ cancel: false }`\n * or use {@link disconnect} for join/rejoin. Sets {@link isLoading} to\n * `false` immediately; {@link values} and {@link messages} are preserved.\n */\n stop(options?: StreamStopOptions): Promise<void>;\n /**\n * Disconnect the client without cancelling the run server-side.\n * Alias for `stop({ cancel: false })`.\n */\n disconnect(): Promise<void>;\n /**\n * Resume a pending protocol interrupt by sending a response payload\n * back to the interrupted namespace.\n *\n * When `options.interruptId` is omitted, walks `getThread()?.interrupts`\n * from newest to oldest and resumes the first not yet resolved by a prior\n * `respond()` call. That may be a root or subgraph interrupt and is\n * **not** necessarily {@link interrupt} (`interrupts()[0]`, root-only).\n * Safe when exactly one interrupt is pending; otherwise pass an explicit\n * `options.interruptId` (and `options.namespace` for subgraph\n * interrupts).\n *\n * The server validates `namespace` against the pending interrupt. Root\n * interrupts use `namespace: []` (default when omitted). For subgraph\n * interrupts, copy `namespace` from `getThread()?.interrupts`.\n *\n * @example\n * ```ts\n * // Single pending interrupt\n * await stream.respond({ approved: true });\n * ```\n *\n * @example\n * ```ts\n * // Multiple root interrupts\n * for (const intr of stream.interrupts()) {\n * await stream.respond(decide(intr.value), { interruptId: intr.id! });\n * }\n * ```\n *\n * @example\n * ```ts\n * // Subgraph interrupt — namespace from `getThread()`\n * const thread = stream.getThread();\n * for (const entry of thread?.interrupts ?? []) {\n * await stream.respond(buildResponse(entry.payload), {\n * interruptId: entry.interruptId,\n * namespace: entry.namespace,\n * });\n * }\n * ```\n *\n * To resume several interrupts pending at the same checkpoint in one\n * command, use {@link respondAll}.\n */\n respond(\n response: unknown,\n options?: StreamRespondOptions<ConfigurableType>\n ): Promise<void>;\n\n /**\n * Resume several pending interrupts at the same checkpoint in a single\n * command — required when a run pauses on multiple interrupts at once\n * (e.g. parallel tool-authorization prompts), which sequential\n * {@link respond} calls cannot handle. `responsesById` maps each pending\n * `interruptId` to its response, so different interrupts can receive\n * different payloads. Pass `options.config` / `options.metadata` to fold\n * run-level config and metadata into the resumed run, mirroring\n * `submit()`.\n *\n * @example\n * ```ts\n * await stream.respondAll({\n * [interruptA.id]: { approved: true },\n * [interruptB.id]: { approved: false },\n * });\n * ```\n */\n respondAll(\n responsesById: Record<string, unknown>,\n options?: StreamRespondAllOptions<ConfigurableType>\n ): Promise<void>;\n\n // ----- identity -----\n /** LangGraph SDK client used to construct thread streams. */\n readonly client: Client;\n /** Assistant id the thread is bound to for its lifetime. */\n readonly assistantId: string;\n\n /**\n * Returns the bound {@link ThreadStream}, if one exists (`undefined`\n * until the thread is hydrated or the first submit completes). Prefer\n * the projections and selector primitives for UI work; use this for\n * low-level protocol access (raw subscriptions, state commands, etc.).\n */\n getThread(): ThreadStream | undefined;\n\n /** @internal Used by selector primitives. */\n readonly [STREAM_CONTROLLER]: StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >;\n}\n\n/**\n * Erased handle useful as a parameter type for helper components that\n * pass a `stream` through to selector primitives without reading\n * `values` directly. Mirrors the React/Vue `AnyStream` alias.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyStream = UseStreamReturn<any, any, any>;\n\n/**\n * Convenience alias — the fully-resolved return type of\n * {@link useStream} for a given source type `T`.\n */\nexport type StreamApi<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * React-compatible alias for the fully-resolved stream handle type.\n * Angular docs prefer {@link StreamApi}, but shared libraries can use\n * this name across framework bindings.\n */\nexport type UseStreamResult<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n> = UseStreamReturn<T, InterruptType, ConfigurableType>;\n\n/**\n * Framework-free factory that constructs a {@link StreamController}\n * and wraps its stores in Angular Signals. Callers must supply the\n * {@link DestroyRef} that owns the controller's lifetime — it's\n * already captured by the public `injectStream` helper.\n *\n * Exported for advanced callers (e.g. testing utilities, custom\n * factories) that prefer to manage injection scope themselves.\n */\nexport function useStream<\n T = Record<string, unknown>,\n InterruptType = unknown,\n ConfigurableType extends object = Record<string, unknown>,\n>(\n options: UseStreamOptions<InferStateType<T>>,\n destroyRef?: DestroyRef\n): UseStreamReturn<T, InterruptType, ConfigurableType> {\n type StateType = InferStateType<T>;\n\n interface OptionsBag {\n assistantId?: string;\n threadId?: string | null | Signal<string | null | undefined>;\n client?: Client;\n apiUrl?: string;\n apiKey?: string;\n callerOptions?: ClientConfig[\"callerOptions\"];\n defaultHeaders?: ClientConfig[\"defaultHeaders\"];\n transport?: \"sse\" | \"websocket\" | AgentServerAdapter;\n fetch?: typeof fetch;\n webSocketFactory?: (url: string) => WebSocket;\n onThreadId?: (threadId: string) => void;\n onCreated?: (info: RunExecutionInfo) => void;\n onCompleted?: (info: RunCompletedInfo) => void;\n initialValues?: StateType;\n messagesKey?: string;\n tools?: AnyHeadlessToolImplementation[];\n onTool?: OnToolCallback;\n optimistic?: boolean;\n }\n const asBag = options as OptionsBag;\n\n const hasCustomAdapter =\n asBag.transport != null && typeof asBag.transport !== \"string\";\n const transport = asBag.transport;\n\n const client: Client =\n asBag.client ??\n (new ClientCtor({\n apiUrl: asBag.apiUrl,\n apiKey: asBag.apiKey,\n callerOptions: asBag.callerOptions,\n defaultHeaders: asBag.defaultHeaders,\n }) as unknown as Client);\n\n // Custom adapters may omit `assistantId`; the controller still\n // requires one so it has something to forward to `threads.stream`.\n const sentinel = \"_\";\n const assistantId =\n \"assistantId\" in options ? (options.assistantId ?? sentinel) : sentinel;\n\n // Normalize threadId input to a signal — callers may pass plain\n // values, nulls, or their own signals.\n const threadIdInput: Signal<string | null> = (() => {\n const raw = asBag.threadId;\n if (isSignal(raw)) {\n return computed(\n () => (raw as Signal<string | null | undefined>)() ?? null\n );\n }\n const initial: string | null = (raw as string | null | undefined) ?? null;\n return signal(initial) as unknown as Signal<string | null>;\n })();\n\n const controller = new StreamController<\n StateType,\n InterruptType,\n ConfigurableType\n >({\n assistantId,\n // Cast: the runtime `Client` is state-shape agnostic, but the\n // controller declares `client: Client<StateType>` for its own\n // typings. Same cast is applied in the React/Vue bindings.\n client: client as unknown as Client<StateType>,\n threadId: untracked(() => threadIdInput()),\n transport,\n fetch: hasCustomAdapter ? undefined : asBag.fetch,\n webSocketFactory: hasCustomAdapter ? undefined : asBag.webSocketFactory,\n onThreadId: options.onThreadId,\n onCreated: options.onCreated,\n onCompleted: options.onCompleted,\n initialValues: options.initialValues,\n messagesKey: options.messagesKey,\n optimistic: asBag.optimistic,\n });\n\n // Deferred dispose — matches the React `useEffect(() =>\n // controller.activate())` and Vue `onScopeDispose(deactivate)`\n // patterns. HMR / scope-reuse scenarios stay clean because\n // `activate()` cancels the pending dispose if the scope survives.\n const deactivate = controller.activate();\n const ref = destroyRef ?? inject(DestroyRef);\n ref.onDestroy(deactivate);\n\n // ─── Reactivity bridge: StreamStore → Signal ────────────────────────\n function bindStore<S>(\n subscribe: (listener: () => void) => () => void,\n getSnapshot: () => S\n ): Signal<S> {\n const s = signal<S>(getSnapshot());\n const unsubscribe = subscribe(() => {\n s.set(getSnapshot());\n });\n ref.onDestroy(unsubscribe);\n return computed(() => s());\n }\n\n const rootSignal = bindStore<RootSnapshot<StateType, InterruptType>>(\n controller.rootStore.subscribe,\n controller.rootStore.getSnapshot\n );\n const subagentSignal = bindStore<SubagentMap>(\n controller.subagentStore.subscribe,\n controller.subagentStore.getSnapshot\n );\n const subgraphSignal = bindStore<SubgraphMap>(\n controller.subgraphStore.subscribe,\n controller.subgraphStore.getSnapshot\n );\n const subgraphByNodeSignal = bindStore<SubgraphByNodeMap>(\n controller.subgraphByNodeStore.subscribe,\n controller.subgraphByNodeStore.getSnapshot\n );\n\n const values = computed(() => rootSignal().values);\n const messages = computed(() => rootSignal().messages);\n const toolCalls = computed(\n () => rootSignal().toolCalls as InferToolCalls<T>[]\n );\n const interrupts = computed(() =>\n filterOutHeadlessToolInterrupts(rootSignal().interrupts)\n );\n const interrupt = computed(() => interrupts()[0]);\n const isLoading = computed(() => rootSignal().isLoading);\n const isThreadLoading = computed(() => rootSignal().isThreadLoading);\n const error = computed(() => rootSignal().error);\n const threadId = computed(() => rootSignal().threadId);\n\n // `hydrationPromise` is a property on the controller that gets\n // swapped on every `hydrate()` call. Exposing it as a signal lets\n // templates `await stream.hydrationPromise()` reactively; we\n // refresh the reference when the root store settles a new promise.\n const hydrationPromise = computed(() => {\n rootSignal();\n return controller.hydrationPromise;\n });\n\n // ─── threadId reactivity ────────────────────────────────────────────\n //\n // Re-hydrate whenever the caller's threadId input changes after\n // construction. The initial hydrate already fired synchronously in\n // the controller constructor, so we compare against the snapshot\n // captured at construction time rather than blindly skipping the\n // first run — in Angular, `@Input()` bindings apply *between*\n // construction and the first effect tick, so the first read can\n // legitimately be a different (updated) value that needs to\n // hydrate.\n const initialThreadId = untracked(() => threadIdInput()) ?? null;\n let lastAppliedThreadId: string | null = initialThreadId;\n effect(() => {\n const next = threadIdInput() ?? null;\n if (next === lastAppliedThreadId) return;\n lastAppliedThreadId = next;\n untracked(() => {\n void controller.hydrate(next);\n });\n });\n\n // ─── Headless-tool handling ─────────────────────────────────────────\n const tools = options.tools;\n const onTool = options.onTool;\n if (tools?.length) {\n const handledTools = new Set<string>();\n\n // Clear the dedup set whenever the thread id changes.\n effect(() => {\n threadIdInput();\n untracked(() => handledTools.clear());\n });\n\n effect(() => {\n rootSignal();\n untracked(() => {\n scheduleCoalescedHeadlessToolFlush(handledTools, () => {\n const snapshot = rootSignal();\n const bag = snapshot.values as unknown as Record<string, unknown>;\n const protocolInterrupts =\n snapshot.interrupts as unknown as Interrupt[];\n const valuesInterrupts = Array.isArray(bag?.__interrupt__)\n ? (bag.__interrupt__ as Interrupt[])\n : [];\n const headlessInterrupts =\n protocolInterrupts.length > 0\n ? protocolInterrupts\n : valuesInterrupts;\n if (headlessInterrupts.length === 0) return;\n flushPendingHeadlessToolInterrupts(\n { ...bag, __interrupt__: headlessInterrupts },\n tools,\n handledTools,\n {\n onTool,\n defer: (run) => {\n void Promise.resolve().then(run);\n },\n resumeSubmit: (command) =>\n applyHeadlessToolResumeCommand(controller, command),\n }\n );\n });\n });\n });\n }\n\n const handle: UseStreamReturn<T, InterruptType, ConfigurableType> = {\n values: values as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"values\"],\n messages,\n toolCalls,\n interrupts,\n interrupt,\n isLoading,\n isThreadLoading,\n error,\n threadId,\n hydrationPromise,\n subagents: subagentSignal as UseStreamReturn<\n T,\n InterruptType,\n ConfigurableType\n >[\"subagents\"],\n subgraphs: subgraphSignal,\n subgraphsByNode: subgraphByNodeSignal,\n submit: (input, submitOptions) => controller.submit(input, submitOptions),\n stop: (options) => controller.stop(options),\n disconnect: () => controller.disconnect(),\n respond: (response, options) => controller.respond(response, options),\n respondAll: (responsesById, options) =>\n controller.respondAll(responsesById, options),\n getThread: () => controller.getThread(),\n client,\n assistantId,\n [STREAM_CONTROLLER]: controller,\n };\n\n return handle;\n}\n\n/**\n * Helper used by the selector primitives to reach the underlying\n * {@link ChannelRegistry} from a stream handle. Kept internal —\n * application code should call `injectMessages`, `injectToolCalls`,\n * etc. instead of reading this directly.\n *\n * @internal\n */\nexport function getRegistry(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n stream: UseStreamReturn<any, any, any>\n): ChannelRegistry {\n return stream[STREAM_CONTROLLER].registry;\n}\n\nexport type { ThreadStream };\n"],"mappings":";;;;;;;;;;;;AA2EA,MAAa,oBAAmC,OAAO,IACrD,gCACD;;;;;;;;;;AAqTD,SAAgB,UAKd,SACA,YACqD;CAuBrD,MAAM,QAAQ;CAEd,MAAM,mBACJ,MAAM,aAAa,QAAQ,OAAO,MAAM,cAAc;CACxD,MAAM,YAAY,MAAM;CAExB,MAAM,SACJ,MAAM,UACL,IAAIA,SAAW;EACd,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,eAAe,MAAM;EACrB,gBAAgB,MAAM;EACvB,CAAC;CAIJ,MAAM,WAAW;CACjB,MAAM,cACJ,iBAAiB,UAAW,QAAQ,eAAe,WAAY;CAIjE,MAAM,uBAA8C;EAClD,MAAM,MAAM,MAAM;AAClB,MAAI,SAAS,IAAI,CACf,QAAO,eACE,KAA2C,IAAI,KACvD;AAGH,SAAO,OADyB,OAAqC,KAC/C;KACpB;CAEJ,MAAM,aAAa,IAAI,iBAIrB;EACA;EAIQ;EACR,UAAU,gBAAgB,eAAe,CAAC;EAC1C;EACA,OAAO,mBAAmB,KAAA,IAAY,MAAM;EAC5C,kBAAkB,mBAAmB,KAAA,IAAY,MAAM;EACvD,YAAY,QAAQ;EACpB,WAAW,QAAQ;EACnB,a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
|
"description": "Angular integration for LangGraph & LangChain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"directory": "libs/sdk-angular"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@langchain/langgraph-sdk": "1.9.
|
|
13
|
+
"@langchain/langgraph-sdk": "1.9.17"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@analogjs/vite-plugin-angular": "^2.6.0",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@vitest/browser": "^4.1.8",
|
|
28
28
|
"@vitest/browser-webdriverio": "^4.1.8",
|
|
29
29
|
"deepagents": "^1.9.0",
|
|
30
|
-
"hono": "^4.12.
|
|
30
|
+
"hono": "^4.12.21",
|
|
31
31
|
"langchain": "^1.4.4",
|
|
32
32
|
"rxjs": "^7.8.2",
|
|
33
33
|
"tslib": "^2.8.1",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"vitest-browser-angular": "^0.4.0",
|
|
38
38
|
"webdriverio": "^9.27.0",
|
|
39
39
|
"zod": "^4.3.6",
|
|
40
|
-
"@langchain/langgraph": "^1.3.
|
|
41
|
-
"@langchain/langgraph-api": "^1.2.
|
|
40
|
+
"@langchain/langgraph": "^1.3.6",
|
|
41
|
+
"@langchain/langgraph-api": "^1.2.5",
|
|
42
42
|
"@langchain/langgraph-checkpoint": "^1.0.4"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|