@langchain/langgraph-sdk 1.9.23 → 1.9.24

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.
@@ -4,6 +4,37 @@ const require_messages = require("../ui/messages.cjs");
4
4
  function isBaseMessageInstance(value) {
5
5
  return value != null && typeof value.getType === "function";
6
6
  }
7
+ /**
8
+ * Serialize `BaseMessage` instances under a state update's message key
9
+ * into plain message dicts so they survive JSON transport.
10
+ *
11
+ * `respond({ update })` / `respondAll({}, { update })` fold `update` into
12
+ * `Command(update=...)` on the wire. A `BaseMessage`'s default JSON form
13
+ * is the `lc` "constructor" envelope (`{ lc, type: "constructor", id,
14
+ * kwargs }`), which the server's `add_messages` reducer does **not**
15
+ * coerce — whereas the flat `{ type, content, ... }` dict that
16
+ * {@link toMessageDict} emits (and that `submit()` already sends) does.
17
+ * Mirroring the `submit()` path lets
18
+ * `respond({ update: { messages: [new AIMessage(...)] } })` behave like
19
+ * `submit({ messages: [new AIMessage(...)] })`.
20
+ *
21
+ * Only the configured `messagesKey` is touched — in both the object form
22
+ * and the `[key, value][]` tuple form. Every other key, and any already
23
+ * plain (non-`BaseMessage`) entry, passes through untouched.
24
+ */
25
+ function serializeUpdateMessages(update, messagesKey) {
26
+ if (Array.isArray(update)) return update.map((entry) => Array.isArray(entry) && entry[0] === messagesKey ? [entry[0], serializeMessageValue(entry[1])] : entry);
27
+ if (!(messagesKey in update)) return update;
28
+ return {
29
+ ...update,
30
+ [messagesKey]: serializeMessageValue(update[messagesKey])
31
+ };
32
+ }
33
+ function serializeMessageValue(value) {
34
+ if (isBaseMessageInstance(value)) return require_messages.toMessageDict(value);
35
+ if (Array.isArray(value)) return value.map((item) => isBaseMessageInstance(item) ? require_messages.toMessageDict(item) : item);
36
+ return value;
37
+ }
7
38
  function extractId(value) {
8
39
  const id = value?.id;
9
40
  return typeof id === "string" && id.length > 0 ? id : void 0;
@@ -82,5 +113,6 @@ function prepareOptimisticInput(raw, messagesKey, mintId) {
82
113
  }
83
114
  //#endregion
84
115
  exports.prepareOptimisticInput = prepareOptimisticInput;
116
+ exports.serializeUpdateMessages = serializeUpdateMessages;
85
117
 
86
118
  //# sourceMappingURL=optimistic-input.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"optimistic-input.cjs","names":["toMessageDict","ensureMessageInstances"],"sources":["../../src/stream/optimistic-input.ts"],"sourcesContent":["/**\n * Pure helpers for the optimistic `submit()` path.\n *\n * Splitting the input-shaping logic out of {@link StreamController}\n * keeps it unit-testable in isolation: given a raw submit input it\n * produces (a) the payload to dispatch to the server — with stable ids\n * minted for any id-less message so the server echo reconciles by id —\n * and (b) the coerced `BaseMessage` instances to append to the root\n * projection immediately.\n *\n * No mutation of the caller's input is performed: message entries are\n * rebuilt as fresh dicts before ids are injected, and the top-level\n * object is shallow-cloned.\n */\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Message } from \"../types.messages.js\";\nimport { toMessageDict } from \"../ui/messages.js\";\nimport { ensureMessageInstances } from \"./message-coercion.js\";\n\n/**\n * Pre-submit snapshot of a single non-message `values` key, captured so\n * it can be rolled back if the run fails before the server echoes any\n * `values`.\n */\nexport interface OptimisticKeySnapshot {\n readonly key: string;\n /** Whether the key existed in `values` before the optimistic merge. */\n readonly hadKey: boolean;\n /** The pre-submit value (meaningful only when `hadKey` is true). */\n readonly prevValue: unknown;\n}\n\n/**\n * Opaque handle returned by the controller's optimistic apply step and\n * threaded back through the submit coordinator to the terminal\n * reconciliation step. Carries the echoed message ids (to transition\n * `pending` → `sent` / `failed`) and the non-message key snapshot (to\n * roll back on failure-before-echo).\n */\nexport interface OptimisticHandle {\n readonly echoedIds: string[];\n readonly restoreKeys: OptimisticKeySnapshot[];\n}\n\n/**\n * Result of preparing a raw submit input for optimistic dispatch.\n */\nexport interface PreparedOptimisticInput {\n /**\n * Input to actually send to the server. The messages key (when\n * present) is normalized to an array of message dicts, each carrying\n * a stable id; all other keys are copied verbatim.\n */\n readonly dispatchInput: Record<string, unknown>;\n /** Coerced message instances (with ids) to append to the projection. */\n readonly optimisticMessages: BaseMessage[];\n /** Ids of the messages echoed optimistically (minted or pre-existing). */\n readonly echoedIds: string[];\n /** Non-message input keys to shallow-merge into `values`. */\n readonly extraValues: Record<string, unknown>;\n}\n\nfunction isBaseMessageInstance(value: unknown): value is BaseMessage {\n return (\n value != null &&\n typeof (value as { getType?: unknown }).getType === \"function\"\n );\n}\n\nfunction extractId(value: unknown): string | undefined {\n const id = (value as { id?: unknown } | null)?.id;\n return typeof id === \"string\" && id.length > 0 ? id : undefined;\n}\n\n/**\n * Normalize a message-key value into an array of entries. Mirrors the\n * server's `add_messages` coercion: a bare string or single message\n * object is treated as a one-element list.\n */\nfunction toEntryArray(value: unknown): unknown[] {\n if (Array.isArray(value)) return value;\n return [value];\n}\n\n/**\n * Build a message dict carrying `id` from an arbitrary input entry,\n * without mutating the original.\n */\nfunction toDispatchDict(entry: unknown, id: string): Message {\n if (typeof entry === \"string\") {\n return { type: \"human\", content: entry, id } as unknown as Message;\n }\n if (isBaseMessageInstance(entry)) {\n return { ...toMessageDict(entry), id } as Message;\n }\n return { ...(entry as object), id } as Message;\n}\n\n/**\n * Prepare a raw submit input for optimistic dispatch.\n *\n * @param raw - Raw input passed to `submit()`. Must be a\n * non-null, non-array object (caller guards this).\n * @param messagesKey - State key holding the message array.\n * @param mintId - Factory for stable client message ids.\n * @returns The dispatch payload, optimistic messages, echoed ids, and\n * the non-message portion of the input.\n */\nexport function prepareOptimisticInput(\n raw: Record<string, unknown>,\n messagesKey: string,\n mintId: () => string\n): PreparedOptimisticInput {\n const dispatchInput: Record<string, unknown> = { ...raw };\n const extraValues: Record<string, unknown> = {};\n for (const key of Object.keys(raw)) {\n if (key !== messagesKey) extraValues[key] = raw[key];\n }\n\n const echoedIds: string[] = [];\n const messagesValue = raw[messagesKey];\n if (messagesValue == null) {\n return { dispatchInput, optimisticMessages: [], echoedIds, extraValues };\n }\n\n const entries = toEntryArray(messagesValue);\n const dispatchEntries: unknown[] = [];\n const optimisticDicts: Message[] = [];\n for (const entry of entries) {\n const echoable =\n typeof entry === \"string\" ||\n isBaseMessageInstance(entry) ||\n (entry != null && typeof entry === \"object\" && !Array.isArray(entry));\n if (!echoable) {\n // Non-message-shaped entry (number/bool/null): forward as-is,\n // nothing to echo.\n dispatchEntries.push(entry);\n continue;\n }\n const id = extractId(entry) ?? mintId();\n const dict = toDispatchDict(entry, id);\n dispatchEntries.push(dict);\n optimisticDicts.push(dict);\n echoedIds.push(id);\n }\n\n dispatchInput[messagesKey] = dispatchEntries;\n const optimisticMessages = ensureMessageInstances(\n optimisticDicts\n ) as BaseMessage[];\n return { dispatchInput, optimisticMessages, echoedIds, extraValues };\n}\n"],"mappings":";;;AA8DA,SAAS,sBAAsB,OAAsC;AACnE,QACE,SAAS,QACT,OAAQ,MAAgC,YAAY;;AAIxD,SAAS,UAAU,OAAoC;CACrD,MAAM,KAAM,OAAmC;AAC/C,QAAO,OAAO,OAAO,YAAY,GAAG,SAAS,IAAI,KAAK,KAAA;;;;;;;AAQxD,SAAS,aAAa,OAA2B;AAC/C,KAAI,MAAM,QAAQ,MAAM,CAAE,QAAO;AACjC,QAAO,CAAC,MAAM;;;;;;AAOhB,SAAS,eAAe,OAAgB,IAAqB;AAC3D,KAAI,OAAO,UAAU,SACnB,QAAO;EAAE,MAAM;EAAS,SAAS;EAAO;EAAI;AAE9C,KAAI,sBAAsB,MAAM,CAC9B,QAAO;EAAE,GAAGA,iBAAAA,cAAc,MAAM;EAAE;EAAI;AAExC,QAAO;EAAE,GAAI;EAAkB;EAAI;;;;;;;;;;;;AAarC,SAAgB,uBACd,KACA,aACA,QACyB;CACzB,MAAM,gBAAyC,EAAE,GAAG,KAAK;CACzD,MAAM,cAAuC,EAAE;AAC/C,MAAK,MAAM,OAAO,OAAO,KAAK,IAAI,CAChC,KAAI,QAAQ,YAAa,aAAY,OAAO,IAAI;CAGlD,MAAM,YAAsB,EAAE;CAC9B,MAAM,gBAAgB,IAAI;AAC1B,KAAI,iBAAiB,KACnB,QAAO;EAAE;EAAe,oBAAoB,EAAE;EAAE;EAAW;EAAa;CAG1E,MAAM,UAAU,aAAa,cAAc;CAC3C,MAAM,kBAA6B,EAAE;CACrC,MAAM,kBAA6B,EAAE;AACrC,MAAK,MAAM,SAAS,SAAS;AAK3B,MAAI,EAHF,OAAO,UAAU,YACjB,sBAAsB,MAAM,IAC3B,SAAS,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM,GACvD;AAGb,mBAAgB,KAAK,MAAM;AAC3B;;EAEF,MAAM,KAAK,UAAU,MAAM,IAAI,QAAQ;EACvC,MAAM,OAAO,eAAe,OAAO,GAAG;AACtC,kBAAgB,KAAK,KAAK;AAC1B,kBAAgB,KAAK,KAAK;AAC1B,YAAU,KAAK,GAAG;;AAGpB,eAAc,eAAe;AAI7B,QAAO;EAAE;EAAe,oBAHGC,yBAAAA,uBACzB,gBACD;EAC2C;EAAW;EAAa"}
1
+ {"version":3,"file":"optimistic-input.cjs","names":["toMessageDict","ensureMessageInstances"],"sources":["../../src/stream/optimistic-input.ts"],"sourcesContent":["/**\n * Pure helpers for the optimistic `submit()` path.\n *\n * Splitting the input-shaping logic out of {@link StreamController}\n * keeps it unit-testable in isolation: given a raw submit input it\n * produces (a) the payload to dispatch to the server — with stable ids\n * minted for any id-less message so the server echo reconciles by id —\n * and (b) the coerced `BaseMessage` instances to append to the root\n * projection immediately.\n *\n * No mutation of the caller's input is performed: message entries are\n * rebuilt as fresh dicts before ids are injected, and the top-level\n * object is shallow-cloned.\n */\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Message } from \"../types.messages.js\";\nimport { toMessageDict } from \"../ui/messages.js\";\nimport { ensureMessageInstances } from \"./message-coercion.js\";\n\n/**\n * Pre-submit snapshot of a single non-message `values` key, captured so\n * it can be rolled back if the run fails before the server echoes any\n * `values`.\n */\nexport interface OptimisticKeySnapshot {\n readonly key: string;\n /** Whether the key existed in `values` before the optimistic merge. */\n readonly hadKey: boolean;\n /** The pre-submit value (meaningful only when `hadKey` is true). */\n readonly prevValue: unknown;\n}\n\n/**\n * Opaque handle returned by the controller's optimistic apply step and\n * threaded back through the submit coordinator to the terminal\n * reconciliation step. Carries the echoed message ids (to transition\n * `pending` → `sent` / `failed`) and the non-message key snapshot (to\n * roll back on failure-before-echo).\n */\nexport interface OptimisticHandle {\n readonly echoedIds: string[];\n readonly restoreKeys: OptimisticKeySnapshot[];\n}\n\n/**\n * Result of preparing a raw submit input for optimistic dispatch.\n */\nexport interface PreparedOptimisticInput {\n /**\n * Input to actually send to the server. The messages key (when\n * present) is normalized to an array of message dicts, each carrying\n * a stable id; all other keys are copied verbatim.\n */\n readonly dispatchInput: Record<string, unknown>;\n /** Coerced message instances (with ids) to append to the projection. */\n readonly optimisticMessages: BaseMessage[];\n /** Ids of the messages echoed optimistically (minted or pre-existing). */\n readonly echoedIds: string[];\n /** Non-message input keys to shallow-merge into `values`. */\n readonly extraValues: Record<string, unknown>;\n}\n\nfunction isBaseMessageInstance(value: unknown): value is BaseMessage {\n return (\n value != null &&\n typeof (value as { getType?: unknown }).getType === \"function\"\n );\n}\n\n/**\n * Serialize `BaseMessage` instances under a state update's message key\n * into plain message dicts so they survive JSON transport.\n *\n * `respond({ update })` / `respondAll({}, { update })` fold `update` into\n * `Command(update=...)` on the wire. A `BaseMessage`'s default JSON form\n * is the `lc` \"constructor\" envelope (`{ lc, type: \"constructor\", id,\n * kwargs }`), which the server's `add_messages` reducer does **not**\n * coerce — whereas the flat `{ type, content, ... }` dict that\n * {@link toMessageDict} emits (and that `submit()` already sends) does.\n * Mirroring the `submit()` path lets\n * `respond({ update: { messages: [new AIMessage(...)] } })` behave like\n * `submit({ messages: [new AIMessage(...)] })`.\n *\n * Only the configured `messagesKey` is touched — in both the object form\n * and the `[key, value][]` tuple form. Every other key, and any already\n * plain (non-`BaseMessage`) entry, passes through untouched.\n */\nexport function serializeUpdateMessages(\n update: Record<string, unknown> | [string, unknown][],\n messagesKey: string\n): Record<string, unknown> | [string, unknown][] {\n if (Array.isArray(update)) {\n return update.map((entry) =>\n Array.isArray(entry) && entry[0] === messagesKey\n ? [entry[0], serializeMessageValue(entry[1])]\n : entry\n ) as [string, unknown][];\n }\n if (!(messagesKey in update)) return update;\n return {\n ...update,\n [messagesKey]: serializeMessageValue(update[messagesKey]),\n };\n}\n\nfunction serializeMessageValue(value: unknown): unknown {\n if (isBaseMessageInstance(value)) return toMessageDict(value);\n if (Array.isArray(value)) {\n return value.map((item) =>\n isBaseMessageInstance(item) ? toMessageDict(item) : item\n );\n }\n return value;\n}\n\nfunction extractId(value: unknown): string | undefined {\n const id = (value as { id?: unknown } | null)?.id;\n return typeof id === \"string\" && id.length > 0 ? id : undefined;\n}\n\n/**\n * Normalize a message-key value into an array of entries. Mirrors the\n * server's `add_messages` coercion: a bare string or single message\n * object is treated as a one-element list.\n */\nfunction toEntryArray(value: unknown): unknown[] {\n if (Array.isArray(value)) return value;\n return [value];\n}\n\n/**\n * Build a message dict carrying `id` from an arbitrary input entry,\n * without mutating the original.\n */\nfunction toDispatchDict(entry: unknown, id: string): Message {\n if (typeof entry === \"string\") {\n return { type: \"human\", content: entry, id } as unknown as Message;\n }\n if (isBaseMessageInstance(entry)) {\n return { ...toMessageDict(entry), id } as Message;\n }\n return { ...(entry as object), id } as Message;\n}\n\n/**\n * Prepare a raw submit input for optimistic dispatch.\n *\n * @param raw - Raw input passed to `submit()`. Must be a\n * non-null, non-array object (caller guards this).\n * @param messagesKey - State key holding the message array.\n * @param mintId - Factory for stable client message ids.\n * @returns The dispatch payload, optimistic messages, echoed ids, and\n * the non-message portion of the input.\n */\nexport function prepareOptimisticInput(\n raw: Record<string, unknown>,\n messagesKey: string,\n mintId: () => string\n): PreparedOptimisticInput {\n const dispatchInput: Record<string, unknown> = { ...raw };\n const extraValues: Record<string, unknown> = {};\n for (const key of Object.keys(raw)) {\n if (key !== messagesKey) extraValues[key] = raw[key];\n }\n\n const echoedIds: string[] = [];\n const messagesValue = raw[messagesKey];\n if (messagesValue == null) {\n return { dispatchInput, optimisticMessages: [], echoedIds, extraValues };\n }\n\n const entries = toEntryArray(messagesValue);\n const dispatchEntries: unknown[] = [];\n const optimisticDicts: Message[] = [];\n for (const entry of entries) {\n const echoable =\n typeof entry === \"string\" ||\n isBaseMessageInstance(entry) ||\n (entry != null && typeof entry === \"object\" && !Array.isArray(entry));\n if (!echoable) {\n // Non-message-shaped entry (number/bool/null): forward as-is,\n // nothing to echo.\n dispatchEntries.push(entry);\n continue;\n }\n const id = extractId(entry) ?? mintId();\n const dict = toDispatchDict(entry, id);\n dispatchEntries.push(dict);\n optimisticDicts.push(dict);\n echoedIds.push(id);\n }\n\n dispatchInput[messagesKey] = dispatchEntries;\n const optimisticMessages = ensureMessageInstances(\n optimisticDicts\n ) as BaseMessage[];\n return { dispatchInput, optimisticMessages, echoedIds, extraValues };\n}\n"],"mappings":";;;AA8DA,SAAS,sBAAsB,OAAsC;AACnE,QACE,SAAS,QACT,OAAQ,MAAgC,YAAY;;;;;;;;;;;;;;;;;;;;AAsBxD,SAAgB,wBACd,QACA,aAC+C;AAC/C,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,KAAK,UACjB,MAAM,QAAQ,MAAM,IAAI,MAAM,OAAO,cACjC,CAAC,MAAM,IAAI,sBAAsB,MAAM,GAAG,CAAC,GAC3C,MACL;AAEH,KAAI,EAAE,eAAe,QAAS,QAAO;AACrC,QAAO;EACL,GAAG;GACF,cAAc,sBAAsB,OAAO,aAAa;EAC1D;;AAGH,SAAS,sBAAsB,OAAyB;AACtD,KAAI,sBAAsB,MAAM,CAAE,QAAOA,iBAAAA,cAAc,MAAM;AAC7D,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,KAAK,SAChB,sBAAsB,KAAK,GAAGA,iBAAAA,cAAc,KAAK,GAAG,KACrD;AAEH,QAAO;;AAGT,SAAS,UAAU,OAAoC;CACrD,MAAM,KA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AA,uBACzB,gBACD;EAC2C;EAAW;EAAa"}
@@ -4,6 +4,37 @@ import { toMessageDict } from "../ui/messages.js";
4
4
  function isBaseMessageInstance(value) {
5
5
  return value != null && typeof value.getType === "function";
6
6
  }
7
+ /**
8
+ * Serialize `BaseMessage` instances under a state update's message key
9
+ * into plain message dicts so they survive JSON transport.
10
+ *
11
+ * `respond({ update })` / `respondAll({}, { update })` fold `update` into
12
+ * `Command(update=...)` on the wire. A `BaseMessage`'s default JSON form
13
+ * is the `lc` "constructor" envelope (`{ lc, type: "constructor", id,
14
+ * kwargs }`), which the server's `add_messages` reducer does **not**
15
+ * coerce — whereas the flat `{ type, content, ... }` dict that
16
+ * {@link toMessageDict} emits (and that `submit()` already sends) does.
17
+ * Mirroring the `submit()` path lets
18
+ * `respond({ update: { messages: [new AIMessage(...)] } })` behave like
19
+ * `submit({ messages: [new AIMessage(...)] })`.
20
+ *
21
+ * Only the configured `messagesKey` is touched — in both the object form
22
+ * and the `[key, value][]` tuple form. Every other key, and any already
23
+ * plain (non-`BaseMessage`) entry, passes through untouched.
24
+ */
25
+ function serializeUpdateMessages(update, messagesKey) {
26
+ if (Array.isArray(update)) return update.map((entry) => Array.isArray(entry) && entry[0] === messagesKey ? [entry[0], serializeMessageValue(entry[1])] : entry);
27
+ if (!(messagesKey in update)) return update;
28
+ return {
29
+ ...update,
30
+ [messagesKey]: serializeMessageValue(update[messagesKey])
31
+ };
32
+ }
33
+ function serializeMessageValue(value) {
34
+ if (isBaseMessageInstance(value)) return toMessageDict(value);
35
+ if (Array.isArray(value)) return value.map((item) => isBaseMessageInstance(item) ? toMessageDict(item) : item);
36
+ return value;
37
+ }
7
38
  function extractId(value) {
8
39
  const id = value?.id;
9
40
  return typeof id === "string" && id.length > 0 ? id : void 0;
@@ -81,6 +112,6 @@ function prepareOptimisticInput(raw, messagesKey, mintId) {
81
112
  };
82
113
  }
83
114
  //#endregion
84
- export { prepareOptimisticInput };
115
+ export { prepareOptimisticInput, serializeUpdateMessages };
85
116
 
86
117
  //# sourceMappingURL=optimistic-input.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"optimistic-input.js","names":[],"sources":["../../src/stream/optimistic-input.ts"],"sourcesContent":["/**\n * Pure helpers for the optimistic `submit()` path.\n *\n * Splitting the input-shaping logic out of {@link StreamController}\n * keeps it unit-testable in isolation: given a raw submit input it\n * produces (a) the payload to dispatch to the server — with stable ids\n * minted for any id-less message so the server echo reconciles by id —\n * and (b) the coerced `BaseMessage` instances to append to the root\n * projection immediately.\n *\n * No mutation of the caller's input is performed: message entries are\n * rebuilt as fresh dicts before ids are injected, and the top-level\n * object is shallow-cloned.\n */\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Message } from \"../types.messages.js\";\nimport { toMessageDict } from \"../ui/messages.js\";\nimport { ensureMessageInstances } from \"./message-coercion.js\";\n\n/**\n * Pre-submit snapshot of a single non-message `values` key, captured so\n * it can be rolled back if the run fails before the server echoes any\n * `values`.\n */\nexport interface OptimisticKeySnapshot {\n readonly key: string;\n /** Whether the key existed in `values` before the optimistic merge. */\n readonly hadKey: boolean;\n /** The pre-submit value (meaningful only when `hadKey` is true). */\n readonly prevValue: unknown;\n}\n\n/**\n * Opaque handle returned by the controller's optimistic apply step and\n * threaded back through the submit coordinator to the terminal\n * reconciliation step. Carries the echoed message ids (to transition\n * `pending` → `sent` / `failed`) and the non-message key snapshot (to\n * roll back on failure-before-echo).\n */\nexport interface OptimisticHandle {\n readonly echoedIds: string[];\n readonly restoreKeys: OptimisticKeySnapshot[];\n}\n\n/**\n * Result of preparing a raw submit input for optimistic dispatch.\n */\nexport interface PreparedOptimisticInput {\n /**\n * Input to actually send to the server. The messages key (when\n * present) is normalized to an array of message dicts, each carrying\n * a stable id; all other keys are copied verbatim.\n */\n readonly dispatchInput: Record<string, unknown>;\n /** Coerced message instances (with ids) to append to the projection. */\n readonly optimisticMessages: BaseMessage[];\n /** Ids of the messages echoed optimistically (minted or pre-existing). */\n readonly echoedIds: string[];\n /** Non-message input keys to shallow-merge into `values`. */\n readonly extraValues: Record<string, unknown>;\n}\n\nfunction isBaseMessageInstance(value: unknown): value is BaseMessage {\n return (\n value != null &&\n typeof (value as { getType?: unknown }).getType === \"function\"\n );\n}\n\nfunction extractId(value: unknown): string | undefined {\n const id = (value as { id?: unknown } | null)?.id;\n return typeof id === \"string\" && id.length > 0 ? id : undefined;\n}\n\n/**\n * Normalize a message-key value into an array of entries. Mirrors the\n * server's `add_messages` coercion: a bare string or single message\n * object is treated as a one-element list.\n */\nfunction toEntryArray(value: unknown): unknown[] {\n if (Array.isArray(value)) return value;\n return [value];\n}\n\n/**\n * Build a message dict carrying `id` from an arbitrary input entry,\n * without mutating the original.\n */\nfunction toDispatchDict(entry: unknown, id: string): Message {\n if (typeof entry === \"string\") {\n return { type: \"human\", content: entry, id } as unknown as Message;\n }\n if (isBaseMessageInstance(entry)) {\n return { ...toMessageDict(entry), id } as Message;\n }\n return { ...(entry as object), id } as Message;\n}\n\n/**\n * Prepare a raw submit input for optimistic dispatch.\n *\n * @param raw - Raw input passed to `submit()`. Must be a\n * non-null, non-array object (caller guards this).\n * @param messagesKey - State key holding the message array.\n * @param mintId - Factory for stable client message ids.\n * @returns The dispatch payload, optimistic messages, echoed ids, and\n * the non-message portion of the input.\n */\nexport function prepareOptimisticInput(\n raw: Record<string, unknown>,\n messagesKey: string,\n mintId: () => string\n): PreparedOptimisticInput {\n const dispatchInput: Record<string, unknown> = { ...raw };\n const extraValues: Record<string, unknown> = {};\n for (const key of Object.keys(raw)) {\n if (key !== messagesKey) extraValues[key] = raw[key];\n }\n\n const echoedIds: string[] = [];\n const messagesValue = raw[messagesKey];\n if (messagesValue == null) {\n return { dispatchInput, optimisticMessages: [], echoedIds, extraValues };\n }\n\n const entries = toEntryArray(messagesValue);\n const dispatchEntries: unknown[] = [];\n const optimisticDicts: Message[] = [];\n for (const entry of entries) {\n const echoable =\n typeof entry === \"string\" ||\n isBaseMessageInstance(entry) ||\n (entry != null && typeof entry === \"object\" && !Array.isArray(entry));\n if (!echoable) {\n // Non-message-shaped entry (number/bool/null): forward as-is,\n // nothing to echo.\n dispatchEntries.push(entry);\n continue;\n }\n const id = extractId(entry) ?? mintId();\n const dict = toDispatchDict(entry, id);\n dispatchEntries.push(dict);\n optimisticDicts.push(dict);\n echoedIds.push(id);\n }\n\n dispatchInput[messagesKey] = dispatchEntries;\n const optimisticMessages = ensureMessageInstances(\n optimisticDicts\n ) as BaseMessage[];\n return { dispatchInput, optimisticMessages, echoedIds, extraValues };\n}\n"],"mappings":";;;AA8DA,SAAS,sBAAsB,OAAsC;AACnE,QACE,SAAS,QACT,OAAQ,MAAgC,YAAY;;AAIxD,SAAS,UAAU,OAAoC;CACrD,MAAM,KAAM,OAAmC;AAC/C,QAAO,OAAO,OAAO,YAAY,GAAG,SAAS,IAAI,KAAK,KAAA;;;;;;;AAQxD,SAAS,aAAa,OAA2B;AAC/C,KAAI,MAAM,QAAQ,MAAM,CAAE,QAAO;AACjC,QAAO,CAAC,MAAM;;;;;;AAOhB,SAAS,eAAe,OAAgB,IAAqB;AAC3D,KAAI,OAAO,UAAU,SACnB,QAAO;EAAE,MAAM;EAAS,SAAS;EAAO;EAAI;AAE9C,KAAI,sBAAsB,MAAM,CAC9B,QAAO;EAAE,GAAG,cAAc,MAAM;EAAE;EAAI;AAExC,QAAO;EAAE,GAAI;EAAkB;EAAI;;;;;;;;;;;;AAarC,SAAgB,uBACd,KACA,aACA,QACyB;CACzB,MAAM,gBAAyC,EAAE,GAAG,KAAK;CACzD,MAAM,cAAuC,EAAE;AAC/C,MAAK,MAAM,OAAO,OAAO,KAAK,IAAI,CAChC,KAAI,QAAQ,YAAa,aAAY,OAAO,IAAI;CAGlD,MAAM,YAAsB,EAAE;CAC9B,MAAM,gBAAgB,IAAI;AAC1B,KAAI,iBAAiB,KACnB,QAAO;EAAE;EAAe,oBAAoB,EAAE;EAAE;EAAW;EAAa;CAG1E,MAAM,UAAU,aAAa,cAAc;CAC3C,MAAM,kBAA6B,EAAE;CACrC,MAAM,kBAA6B,EAAE;AACrC,MAAK,MAAM,SAAS,SAAS;AAK3B,MAAI,EAHF,OAAO,UAAU,YACjB,sBAAsB,MAAM,IAC3B,SAAS,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM,GACvD;AAGb,mBAAgB,KAAK,MAAM;AAC3B;;EAEF,MAAM,KAAK,UAAU,MAAM,IAAI,QAAQ;EACvC,MAAM,OAAO,eAAe,OAAO,GAAG;AACtC,kBAAgB,KAAK,KAAK;AAC1B,kBAAgB,KAAK,KAAK;AAC1B,YAAU,KAAK,GAAG;;AAGpB,eAAc,eAAe;AAI7B,QAAO;EAAE;EAAe,oBAHG,uBACzB,gBACD;EAC2C;EAAW;EAAa"}
1
+ {"version":3,"file":"optimistic-input.js","names":[],"sources":["../../src/stream/optimistic-input.ts"],"sourcesContent":["/**\n * Pure helpers for the optimistic `submit()` path.\n *\n * Splitting the input-shaping logic out of {@link StreamController}\n * keeps it unit-testable in isolation: given a raw submit input it\n * produces (a) the payload to dispatch to the server — with stable ids\n * minted for any id-less message so the server echo reconciles by id —\n * and (b) the coerced `BaseMessage` instances to append to the root\n * projection immediately.\n *\n * No mutation of the caller's input is performed: message entries are\n * rebuilt as fresh dicts before ids are injected, and the top-level\n * object is shallow-cloned.\n */\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport type { Message } from \"../types.messages.js\";\nimport { toMessageDict } from \"../ui/messages.js\";\nimport { ensureMessageInstances } from \"./message-coercion.js\";\n\n/**\n * Pre-submit snapshot of a single non-message `values` key, captured so\n * it can be rolled back if the run fails before the server echoes any\n * `values`.\n */\nexport interface OptimisticKeySnapshot {\n readonly key: string;\n /** Whether the key existed in `values` before the optimistic merge. */\n readonly hadKey: boolean;\n /** The pre-submit value (meaningful only when `hadKey` is true). */\n readonly prevValue: unknown;\n}\n\n/**\n * Opaque handle returned by the controller's optimistic apply step and\n * threaded back through the submit coordinator to the terminal\n * reconciliation step. Carries the echoed message ids (to transition\n * `pending` → `sent` / `failed`) and the non-message key snapshot (to\n * roll back on failure-before-echo).\n */\nexport interface OptimisticHandle {\n readonly echoedIds: string[];\n readonly restoreKeys: OptimisticKeySnapshot[];\n}\n\n/**\n * Result of preparing a raw submit input for optimistic dispatch.\n */\nexport interface PreparedOptimisticInput {\n /**\n * Input to actually send to the server. The messages key (when\n * present) is normalized to an array of message dicts, each carrying\n * a stable id; all other keys are copied verbatim.\n */\n readonly dispatchInput: Record<string, unknown>;\n /** Coerced message instances (with ids) to append to the projection. */\n readonly optimisticMessages: BaseMessage[];\n /** Ids of the messages echoed optimistically (minted or pre-existing). */\n readonly echoedIds: string[];\n /** Non-message input keys to shallow-merge into `values`. */\n readonly extraValues: Record<string, unknown>;\n}\n\nfunction isBaseMessageInstance(value: unknown): value is BaseMessage {\n return (\n value != null &&\n typeof (value as { getType?: unknown }).getType === \"function\"\n );\n}\n\n/**\n * Serialize `BaseMessage` instances under a state update's message key\n * into plain message dicts so they survive JSON transport.\n *\n * `respond({ update })` / `respondAll({}, { update })` fold `update` into\n * `Command(update=...)` on the wire. A `BaseMessage`'s default JSON form\n * is the `lc` \"constructor\" envelope (`{ lc, type: \"constructor\", id,\n * kwargs }`), which the server's `add_messages` reducer does **not**\n * coerce — whereas the flat `{ type, content, ... }` dict that\n * {@link toMessageDict} emits (and that `submit()` already sends) does.\n * Mirroring the `submit()` path lets\n * `respond({ update: { messages: [new AIMessage(...)] } })` behave like\n * `submit({ messages: [new AIMessage(...)] })`.\n *\n * Only the configured `messagesKey` is touched — in both the object form\n * and the `[key, value][]` tuple form. Every other key, and any already\n * plain (non-`BaseMessage`) entry, passes through untouched.\n */\nexport function serializeUpdateMessages(\n update: Record<string, unknown> | [string, unknown][],\n messagesKey: string\n): Record<string, unknown> | [string, unknown][] {\n if (Array.isArray(update)) {\n return update.map((entry) =>\n Array.isArray(entry) && entry[0] === messagesKey\n ? [entry[0], serializeMessageValue(entry[1])]\n : entry\n ) as [string, unknown][];\n }\n if (!(messagesKey in update)) return update;\n return {\n ...update,\n [messagesKey]: serializeMessageValue(update[messagesKey]),\n };\n}\n\nfunction serializeMessageValue(value: unknown): unknown {\n if (isBaseMessageInstance(value)) return toMessageDict(value);\n if (Array.isArray(value)) {\n return value.map((item) =>\n isBaseMessageInstance(item) ? toMessageDict(item) : item\n );\n }\n return value;\n}\n\nfunction extractId(value: unknown): string | undefined {\n const id = (value as { id?: unknown } | null)?.id;\n return typeof id === \"string\" && id.length > 0 ? id : undefined;\n}\n\n/**\n * Normalize a message-key value into an array of entries. Mirrors the\n * server's `add_messages` coercion: a bare string or single message\n * object is treated as a one-element list.\n */\nfunction toEntryArray(value: unknown): unknown[] {\n if (Array.isArray(value)) return value;\n return [value];\n}\n\n/**\n * Build a message dict carrying `id` from an arbitrary input entry,\n * without mutating the original.\n */\nfunction toDispatchDict(entry: unknown, id: string): Message {\n if (typeof entry === \"string\") {\n return { type: \"human\", content: entry, id } as unknown as Message;\n }\n if (isBaseMessageInstance(entry)) {\n return { ...toMessageDict(entry), id } as Message;\n }\n return { ...(entry as object), id } as Message;\n}\n\n/**\n * Prepare a raw submit input for optimistic dispatch.\n *\n * @param raw - Raw input passed to `submit()`. Must be a\n * non-null, non-array object (caller guards this).\n * @param messagesKey - State key holding the message array.\n * @param mintId - Factory for stable client message ids.\n * @returns The dispatch payload, optimistic messages, echoed ids, and\n * the non-message portion of the input.\n */\nexport function prepareOptimisticInput(\n raw: Record<string, unknown>,\n messagesKey: string,\n mintId: () => string\n): PreparedOptimisticInput {\n const dispatchInput: Record<string, unknown> = { ...raw };\n const extraValues: Record<string, unknown> = {};\n for (const key of Object.keys(raw)) {\n if (key !== messagesKey) extraValues[key] = raw[key];\n }\n\n const echoedIds: string[] = [];\n const messagesValue = raw[messagesKey];\n if (messagesValue == null) {\n return { dispatchInput, optimisticMessages: [], echoedIds, extraValues };\n }\n\n const entries = toEntryArray(messagesValue);\n const dispatchEntries: unknown[] = [];\n const optimisticDicts: Message[] = [];\n for (const entry of entries) {\n const echoable =\n typeof entry === \"string\" ||\n isBaseMessageInstance(entry) ||\n (entry != null && typeof entry === \"object\" && !Array.isArray(entry));\n if (!echoable) {\n // Non-message-shaped entry (number/bool/null): forward as-is,\n // nothing to echo.\n dispatchEntries.push(entry);\n continue;\n }\n const id = extractId(entry) ?? mintId();\n const dict = toDispatchDict(entry, id);\n dispatchEntries.push(dict);\n optimisticDicts.push(dict);\n echoedIds.push(id);\n }\n\n dispatchInput[messagesKey] = dispatchEntries;\n const optimisticMessages = ensureMessageInstances(\n optimisticDicts\n ) as BaseMessage[];\n return { dispatchInput, optimisticMessages, echoedIds, extraValues };\n}\n"],"mappings":";;;AA8DA,SAAS,sBAAsB,OAAsC;AACnE,QACE,SAAS,QACT,OAAQ,MAAgC,YAAY;;;;;;;;;;;;;;;;;;;;AAsBxD,SAAgB,wBACd,QACA,aAC+C;AAC/C,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,KAAK,UACjB,MAAM,QAAQ,MAAM,IAAI,MAAM,OAAO,cACjC,CAAC,MAAM,IAAI,sBAAsB,MAAM,GAAG,CAAC,GAC3C,MACL;AAEH,KAAI,EAAE,eAAe,QAAS,QAAO;AACrC,QAAO;EACL,GAAG;GACF,cAAc,sBAAsB,OAAO,aAAa;EAC1D;;AAGH,SAAS,sBAAsB,OAAyB;AACtD,KAAI,sBAAsB,MAAM,CAAE,QAAO,cAAc,MAAM;AAC7D,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,KAAK,SAChB,sBAAsB,KAAK,GAAG,cAAc,KAAK,GAAG,KACrD;AAEH,QAAO;;AAGT,SAAS,UAAU,OAAoC;CACrD,MAAM,KAAM,OAAmC;AAC/C,QAAO,OAAO,OAAO,YAAY,GAAG,SAAS,IAAI,KAAK,KAAA;;;;;;;AAQxD,SAAS,aAAa,OAA2B;AAC/C,KAAI,MAAM,QAAQ,MAAM,CAAE,QAAO;AACjC,QAAO,CAAC,MAAM;;;;;;AAOhB,SAAS,eAAe,OAAgB,IAAqB;AAC3D,KAAI,OAAO,UAAU,SACnB,QAAO;EAAE,MAAM;EAAS,SAAS;EAAO;EAAI;AAE9C,KAAI,sBAAsB,MAAM,CAC9B,QAAO;EAAE,GAAG,cAAc,MAAM;EAAE;EAAI;AAExC,QAAO;EAAE,GAAI;EAAkB;EAAI;;;;;;;;;;;;AAarC,SAAgB,uBACd,KACA,aACA,QACyB;CACzB,MAAM,gBAAyC,EAAE,GAAG,KAAK;CACzD,MAAM,cAAuC,EAAE;AAC/C,MAAK,MAAM,OAAO,OAAO,KAAK,IAAI,CAChC,KAAI,QAAQ,YAAa,aAAY,OAAO,IAAI;CAGlD,MAAM,YAAsB,EAAE;CAC9B,MAAM,gBAAgB,IAAI;AAC1B,KAAI,iBAAiB,KACnB,QAAO;EAAE;EAAe,oBAAoB,EAAE;EAAE;EAAW;EAAa;CAG1E,MAAM,UAAU,aAAa,cAAc;CAC3C,MAAM,kBAA6B,EAAE;CACrC,MAAM,kBAA6B,EAAE;AACrC,MAAK,MAAM,SAAS,SAAS;AAK3B,MAAI,EAHF,OAAO,UAAU,YACjB,sBAAsB,MAAM,IAC3B,SAAS,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM,GACvD;AAGb,mBAAgB,KAAK,MAAM;AAC3B;;EAEF,MAAM,KAAK,UAAU,MAAM,IAAI,QAAQ;EACvC,MAAM,OAAO,eAAe,OAAO,GAAG;AACtC,kBAAgB,KAAK,KAAK;AAC1B,kBAAgB,KAAK,KAAK;AAC1B,YAAU,KAAK,GAAG;;AAGpB,eAAc,eAAe;AAI7B,QAAO;EAAE;EAAe,oBAHG,uBACzB,gBACD;EAC2C;EAAW;EAAa"}
@@ -300,8 +300,19 @@ var RootMessageProjection = class {
300
300
  * `BaseMessage` instances, each carrying a stable id).
301
301
  * @param extraValues - Non-message input keys to shallow-merge into
302
302
  * `values`.
303
+ * @param options - When `sync` is true the staged write is
304
+ * committed to the store *synchronously* instead of being coalesced
305
+ * onto the next macrotask. Used for discrete, user-initiated
306
+ * optimistic writes (`submit()` / `respond()` / `respondAll()`):
307
+ * committing in the same tick as the triggering event lets the
308
+ * framework render the optimistic message in the *same* commit as
309
+ * any local state the caller flipped alongside it (e.g. a HITL form
310
+ * hiding its inputs), so the pushed card never blinks out for the
311
+ * one-macrotask window before the flush lands. Streaming writes
312
+ * (`handleMessage` / `applyValues`) keep the default macrotask
313
+ * coalescing, which is what tames high-frequency SSE bursts.
303
314
  */
304
- appendOptimistic(messages, extraValues) {
315
+ appendOptimistic(messages, extraValues, options) {
305
316
  let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;
306
317
  let mutated = false;
307
318
  for (const message of messages) {
@@ -333,7 +344,8 @@ var RootMessageProjection = class {
333
344
  if (!mutated && values === baselineValues) return;
334
345
  this.#pendingMessages = working;
335
346
  if (values !== baselineValues) this.#pendingValues = values;
336
- this.#scheduleFlush();
347
+ if (options?.sync) this.#flushPending();
348
+ else this.#scheduleFlush();
337
349
  }
338
350
  /**
339
351
  * Drop optimistic messages by id without disturbing the rest of the
@@ -1 +1 @@
1
- {"version":3,"file":"root-message-projection.cjs","names":["#messagesKey","#store","MessageAssembler","#roles","#indexById","#toolCallIdByNamespace","#sealedMessageIds","#assembler","#valuesMessageIds","#pendingMessages","#pendingValues","#flushScheduled","#maxStep","#sealStep","namespaceKey","assembledMessageToBaseMessage","messagesEqual","#scheduleFlush","reconcileMessagesFromValues","shouldPreferValuesMessageForToolCalls","buildMessageIndex","#flushPending"],"sources":["../../src/stream/root-message-projection.ts"],"sourcesContent":["/**\n * Root-namespace message projection.\n *\n * # What this module is\n *\n * The {@link RootMessageProjection} is the piece of the\n * {@link StreamController} that owns \"what messages does the root\n * namespace currently contain?\". It assembles streamed message deltas\n * via {@link MessageAssembler}, reconciles them against authoritative\n * `values.messages` snapshots from the server, and writes the merged\n * list back into the controller's root snapshot store.\n *\n * # Two streams of truth\n *\n * Root messages arrive on two channels and need to merge cleanly:\n *\n * - **`messages` channel.** Token-level deltas that build messages\n * incrementally. The {@link MessageAssembler} keeps partial\n * messages by id and emits an updated `BaseMessage` per delta.\n * - **`values` channel.** Periodic full-state snapshots that include\n * the authoritative messages array. Used for ordering, removals,\n * and forks (where the streamed messages may pre-date the new\n * timeline).\n *\n * The reconciliation rules (delegated to\n * {@link reconcileMessagesFromValues}) preserve in-flight streamed\n * content while letting values dictate ordering and removals.\n *\n * # Tool-message namespace correlation\n *\n * Tool messages arrive on `messages-start` events with `role: \"tool\"`\n * but the start event doesn't always include a `tool_call_id`. We\n * recover it via three fallbacks:\n *\n * 1. The start event itself, when the server includes it.\n * 2. The legacy `<id>-tool-<call_id>` message id format.\n * 3. The most recent `tool-started` event recorded under the same\n * namespace via {@link recordToolCallNamespace}.\n *\n * Without this correlation, tool messages render with empty\n * `tool_call_id` and downstream UIs can't pair them with the\n * originating tool call.\n *\n * # Store-write batching\n *\n * Every {@link handleMessage} / {@link applyValues} call updates the\n * in-projection bookkeeping (assembler state, id index, role cache)\n * synchronously, then stages the new `messages` / `values` into a\n * pending buffer and schedules a `setTimeout(0)` flush. A single\n * coalesced `store.setState` runs at the next macrotask boundary.\n *\n * The motivation is the long-replay freeze: a thread with hundreds\n * of messages replays through the `messages` channel on refresh or\n * mid-run join. Those events drain through the controller's\n * `for await` pump as a long microtask chain. Per-event\n * `store.setState` notifies `useSyncExternalStore` per event, and\n * after enough notifications React's `nestedUpdateCount` guard trips\n * with \"Maximum update depth exceeded\", permanently freezing the UI\n * on the first few messages. Coalescing to one notification per\n * macrotask lets React's scheduler commit between flushes.\n *\n * # Lifecycle\n *\n * - `handleMessage(event)` — apply a `messages` event delta.\n * - `applyValues(values, msgs)` — merge a `values` snapshot.\n * - `recordToolCallNamespace(ns, id)` — capture `namespace → tool_call_id`\n * so subsequent tool message starts can recover the id.\n * - `reset()` — clear all state on thread rebind.\n */\nimport type {\n MessagesEvent,\n MessageRole,\n MessageStartData,\n} from \"@langchain/protocol\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { MessageAssembler } from \"../client/stream/messages.js\";\nimport {\n assembledMessageToBaseMessage,\n type ExtendedMessageRole,\n} from \"./assembled-to-message.js\";\nimport type { StreamStore } from \"./store.js\";\nimport type { RootSnapshot } from \"./types.js\";\nimport { namespaceKey } from \"./namespace.js\";\nimport {\n buildMessageIndex,\n messagesEqual,\n reconcileMessagesFromValues,\n shouldPreferValuesMessageForToolCalls,\n} from \"./message-reconciliation.js\";\n\n/**\n * Root-namespace message projection. Owns the merge between the\n * `messages` (streamed deltas) and `values` (authoritative\n * snapshots) channels for the root namespace.\n *\n * @typeParam StateType - Root state shape; the messages array is read\n * from `values[messagesKey]`.\n * @typeParam InterruptType - Shape of root protocol interrupts (forwarded\n * into `RootSnapshot` updates).\n */\nexport class RootMessageProjection<\n StateType extends object,\n InterruptType = unknown,\n> {\n /**\n * Key inside `values` that holds the message array. Defaults to\n * `\"messages\"` in the controller; configurable for state graphs\n * that surface messages under a different slot.\n */\n readonly #messagesKey: string;\n\n /** Root snapshot store written to on every merge. */\n readonly #store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n\n /**\n * Stateful chunk assembler for in-flight messages. Reset (via a\n * fresh instance) on every {@link reset} so a new thread starts\n * with no half-built messages from the previous one.\n */\n #assembler = new MessageAssembler();\n\n /**\n * `messageId → role/toolCallId` captured from `message-start` events.\n * The assembler's intermediate output drops these fields, so we cache\n * them at start-time and reapply when projecting to a `BaseMessage`.\n */\n readonly #roles = new Map<\n string,\n { role: ExtendedMessageRole; toolCallId?: string }\n >();\n\n /**\n * `messageId → position in #store.messages` for fast in-place\n * updates as deltas arrive. Rebuilt on every full reconciliation\n * driven by a `values` event.\n */\n readonly #indexById = new Map<string, number>();\n\n /**\n * Ids observed in the most recent `values.messages` snapshot.\n * Reconciliation uses this to detect server-side removals: a\n * previously-seen id missing from the next snapshot means it was\n * removed by the server (and should drop from the projection).\n */\n #valuesMessageIds = new Set<string>();\n\n /**\n * `namespaceKey → tool_call_id` captured from root `tool-started`\n * events. Used as a fallback when a tool-role `message-start` is\n * missing its `tool_call_id` field.\n */\n readonly #toolCallIdByNamespace = new Map<string, string>();\n\n /**\n * Coalescing buffer for store writes. {@link handleMessage} and\n * {@link applyValues} stage their computed `messages` / `values`\n * here instead of calling `store.setState` per event. A single\n * `setTimeout(0)` flush commits them in one `setState`, so a\n * burst of SSE events draining as a microtask chain becomes one\n * store notification at the next macrotask boundary.\n *\n * `null` means \"no staged write\" — once a flush settles, the\n * slots are cleared so the next call starts from the latest\n * committed store snapshot.\n */\n #pendingMessages: BaseMessage[] | null = null;\n #pendingValues: StateType | null = null;\n #flushScheduled = false;\n\n /**\n * Highest checkpoint `step` whose `values` snapshot has been applied.\n * Seeded by {@link StreamController.hydrate} from `getState()` and\n * advanced by live `values` events. A snapshot arriving with a lower\n * step is an older checkpoint replayed by the content pump on\n * reconnect; it is reconciled in add-only mode so it cannot remove\n * the seeded message tail (the final assistant turn). `undefined`\n * until the first step-bearing snapshot, where the legacy\n * remove-on-absence behavior is preserved.\n */\n #maxStep: number | undefined = undefined;\n\n /**\n * Message ids seeded as complete-and-final from an idle thread's\n * `getState()` snapshot. An idle thread defers its root SSE pump, and\n * the first `submit()` brings it up — at which point the transport\n * replays the finished run from `seq=0`. Unlike the `values` channel\n * (guarded by {@link #maxStep}), `messages`-channel deltas carry no\n * step, so that replay would otherwise rebuild each already-complete\n * message from an empty `message-start` and re-stream the whole turn\n * token-by-token, clobbering the seeded tail (a visible \"messages\n * replay\" on the first submit). Deltas for a sealed id are dropped in\n * {@link handleMessage}. The seal is lifted once a checkpoint advances\n * strictly past {@link #sealStep} (see {@link applyValues}) or on\n * thread rebind ({@link reset}). New ids from the next run are never\n * sealed, so they stream normally.\n */\n readonly #sealedMessageIds = new Set<string>();\n\n /**\n * High-water {@link #maxStep} captured when {@link sealMessageIds} ran,\n * i.e. the seed checkpoint's step (or `undefined` when `getState()`\n * carried no `metadata.step`). It is the boundary between the replayed\n * idle history (steps `<= #sealStep`, emitted by the deferred pump's\n * `seq=0` replay) and the new run (steps `> #sealStep`); only a\n * checkpoint strictly past it lifts the seal. Without this boundary the\n * replayed old-run checkpoints — which themselves carry increasing\n * steps — would advance {@link #maxStep} and lift the seal mid-replay,\n * reopening the clobber. When the seed step is unknown the boundary\n * stays `undefined` and the seal holds until {@link reset}; the\n * `values` channel (which ignores the seal) still reconciles any\n * genuine change to a sealed id, only its streamed deltas are dropped.\n */\n #sealStep: number | undefined = undefined;\n\n /**\n * @param params.messagesKey - Key inside `values` that holds the\n * message array.\n * @param params.store - Root snapshot store to mutate.\n */\n constructor(params: {\n messagesKey: string;\n store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n }) {\n this.#messagesKey = params.messagesKey;\n this.#store = params.store;\n }\n\n /**\n * Drop all per-thread state. Called by the controller on thread\n * rebind / dispose so a swap doesn't surface stale messages.\n */\n reset(): void {\n this.#assembler = new MessageAssembler();\n this.#roles.clear();\n this.#indexById.clear();\n this.#valuesMessageIds = new Set();\n this.#toolCallIdByNamespace.clear();\n // Drop any unflushed pending writes — they were computed against\n // the previous thread's baseline and committing them after a\n // rebind would bleed stale messages into the new thread.\n this.#pendingMessages = null;\n this.#pendingValues = null;\n this.#flushScheduled = false;\n this.#maxStep = undefined;\n this.#sealedMessageIds.clear();\n this.#sealStep = undefined;\n }\n\n /**\n * Seal message ids so the streamed `messages` channel cannot downgrade\n * them to partial re-streams. Called by {@link StreamController.hydrate}\n * after seeding an idle thread, whose deferred pump replays the finished\n * run from `seq=0` on the first submit.\n *\n * Captures the current {@link #maxStep} as the lift boundary\n * ({@link #sealStep}). The seal is applied immediately after the seed's\n * `getState()` snapshot is reconciled, so `#maxStep` here is the seed\n * step (or `undefined` when `getState()` carried no `metadata.step`).\n * The seal is lifted once a checkpoint advances strictly past that\n * boundary (see {@link applyValues}) or on thread rebind\n * ({@link reset}).\n *\n * @param ids - Complete message ids from the idle `getState()` seed.\n */\n sealMessageIds(ids: Iterable<string>): void {\n for (const id of ids) this.#sealedMessageIds.add(id);\n if (this.#sealStep == null) this.#sealStep = this.#maxStep;\n }\n\n /**\n * Record a `namespace → tool_call_id` mapping captured from a root\n * `tool-started` event.\n *\n * The companion tool-role `message-start` event may not carry a\n * `tool_call_id`, so we fall back to the most recent value recorded\n * here for the same namespace.\n *\n * @param namespace - Event namespace from the `tool-started` event.\n * @param toolCallId - Tool call id from the same event.\n */\n recordToolCallNamespace(\n namespace: readonly string[],\n toolCallId: string\n ): void {\n this.#toolCallIdByNamespace.set(namespaceKey(namespace), toolCallId);\n }\n\n /**\n * Apply a `messages` channel event to the projection.\n *\n * Captures role/tool metadata on `message-start`, feeds the chunk\n * to the assembler, projects the assembled output to a\n * {@link BaseMessage}, and either appends or in-place updates the\n * pending messages buffer based on whether the id was seen before.\n *\n * @param event - The `messages` channel event to consume.\n */\n handleMessage(event: MessagesEvent): void {\n const data = event.params.data;\n if (data.event === \"message-start\") {\n const startData = data as MessageStartData;\n const role = (startData.role ?? \"ai\") as MessageRole;\n const extendedRole =\n (startData as { role?: ExtendedMessageRole }).role ?? role;\n let toolCallId = (startData as { tool_call_id?: string }).tool_call_id;\n // Tool messages need a tool_call_id to render. Fall back through:\n // 1. legacy `<id>-tool-<call_id>` message id format\n // 2. namespace-recorded tool_call_id (from #recordToolCallNamespace)\n if (extendedRole === \"tool\" && toolCallId == null) {\n const messageId = startData.id;\n if (messageId != null) {\n const match = /-tool-(.+)$/.exec(messageId);\n if (match != null) toolCallId = match[1];\n }\n if (toolCallId == null) {\n toolCallId = this.#toolCallIdByNamespace.get(\n namespaceKey(event.params.namespace)\n );\n }\n }\n if (startData.id != null) {\n this.#roles.set(startData.id, {\n role: extendedRole,\n toolCallId,\n });\n }\n }\n\n const update = this.#assembler.consume(event);\n if (update == null) return;\n const id = update.message.id;\n if (id == null) return;\n // A sealed id belongs to a message seeded complete from an idle\n // thread's `getState()`; the deferred pump's `seq=0` replay would\n // otherwise rebuild it from an empty start and re-stream the whole\n // turn. Drop the replayed delta — the authoritative seed already\n // holds the final content (see {@link #sealedMessageIds}).\n if (this.#sealedMessageIds.has(id)) return;\n const captured = this.#roles.get(id) ?? { role: \"ai\" as const };\n const base = assembledMessageToBaseMessage(update.message, captured.role, {\n toolCallId: captured.toolCallId,\n });\n\n // Compute against the pending baseline if we have one (so an\n // earlier handleMessage in the same tick is the input to this\n // one), else against the latest committed store snapshot.\n // `#indexById` is the synchronous source of truth for \"where is\n // each id in the current messages list\" — every code path below\n // keeps it in sync before returning.\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const existingIdx = this.#indexById.get(id);\n let messages: BaseMessage[];\n if (existingIdx == null) {\n this.#indexById.set(id, baselineMessages.length);\n messages = [...baselineMessages, base];\n } else if (messagesEqual(baselineMessages[existingIdx], base)) {\n // Identical re-emission — skip the store write to keep\n // snapshot identity stable.\n return;\n } else {\n messages = baselineMessages.slice();\n messages[existingIdx] = base;\n }\n\n // Mirror the new messages list into `values[messagesKey]` so\n // direct `values` reads (used by some hooks and by the eventual\n // `values` reconciliation) stay in sync.\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const values = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n messages\n );\n this.#pendingMessages = messages;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Reconcile a full `values` snapshot into the projection.\n *\n * Delegates the merge to {@link reconcileMessagesFromValues}:\n * values stays authoritative for ordering and removals, while\n * streamed in-flight messages keep their content until the server\n * echoes them back. Empty messages just refresh the values blob.\n *\n * Rebuilds {@link #indexById} after the merge so subsequent delta\n * applications target the new positions.\n *\n * @param nextValues - Full values snapshot from the `values` event.\n * @param nextMessages - The messages array extracted from\n * `values[messagesKey]` and coerced to `BaseMessage` instances.\n * @param opts.step - Checkpoint superstep for this snapshot, when\n * known. A snapshot whose step is below the highest applied step is\n * treated as a stale reconnect replay and reconciled add-only.\n */\n applyValues(\n nextValues: StateType,\n nextMessages: BaseMessage[],\n opts?: { step?: number }\n ): void {\n const baselineSnapshot = this.#store.getSnapshot();\n const baselineMessages = this.#pendingMessages ?? baselineSnapshot.messages;\n const baselineValues = this.#pendingValues ?? baselineSnapshot.values;\n\n const step = opts?.step;\n // Stale only when we have both a prior high-water step and a lower\n // incoming step. A missing step preserves the legacy semantics.\n const addOnly =\n step != null && this.#maxStep != null && step < this.#maxStep;\n if (step != null && (this.#maxStep == null || step > this.#maxStep)) {\n this.#maxStep = step;\n }\n // Lift the replay seal only when a checkpoint advances strictly past\n // the step captured when the ids were sealed (the seed step). That\n // boundary separates the replayed idle history (steps <= #sealStep,\n // emitted by the deferred pump's seq=0 replay) from the new run\n // (steps > #sealStep), so crossing it means seeded ids may now take\n // genuine streamed updates. Replayed old-run checkpoints advance\n // #maxStep but never reach past #sealStep, so they can't lift it. A\n // `null` boundary (the seed step was unknown) keeps the seal until\n // reset() — we can't tell replay from live, and the values channel\n // still reconciles a sealed id even while its streamed deltas drop.\n if (\n this.#sealedMessageIds.size > 0 &&\n step != null &&\n this.#sealStep != null &&\n step > this.#sealStep\n ) {\n this.#sealedMessageIds.clear();\n }\n\n if (nextMessages.length === 0) {\n if (\n stateValuesShallowEqual(baselineValues, nextValues, this.#messagesKey)\n ) {\n return;\n }\n // Mirror the current `messages` list back into the values slot\n // so the staged snapshot stays consistent with the (separately\n // tracked) messages array.\n this.#pendingValues = syncMessagesIntoValues(\n nextValues,\n this.#messagesKey,\n baselineMessages\n );\n this.#scheduleFlush();\n return;\n }\n\n const reconciliation = reconcileMessagesFromValues({\n valueMessages: nextMessages,\n currentMessages: baselineMessages,\n currentIndexById: this.#indexById,\n previousValueMessageIds: this.#valuesMessageIds,\n preferValuesMessage: shouldPreferValuesMessageForToolCalls,\n addOnly,\n });\n // A stale replay snapshot must not shrink the authoritative id set:\n // keep the (larger) seeded set so a genuinely-newer removal is still\n // detected once the timeline advances past the seed.\n if (!addOnly) this.#valuesMessageIds = reconciliation.valueMessageIds;\n const messages = reconciliation.messages as BaseMessage[];\n const values = {\n ...(nextValues as Record<string, unknown>),\n [this.#messagesKey]: messages,\n } as StateType;\n if (\n messages === baselineMessages &&\n stateValuesShallowEqual(baselineValues, values, this.#messagesKey)\n ) {\n return;\n }\n\n // Reconciliation may reorder, drop, or substitute messages, so\n // rebuild the id → index map to match the new array.\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(messages)) {\n this.#indexById.set(id, idx);\n }\n this.#pendingMessages = messages;\n this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Append messages applied optimistically by a local `submit()`,\n * keyed by id so the eventual server echo reconciles cleanly.\n *\n * Unlike {@link applyValues}, the supplied messages are *not* treated\n * as an authoritative ordered snapshot: they are appended to the end\n * of the current projection (or replaced in place when the id already\n * exists), preserving prior history ordering. When the server later\n * emits a `values` snapshot containing the same ids,\n * {@link applyValues} → {@link reconcileMessagesFromValues} takes over\n * (server ordering wins, the echoed message replaces the optimistic\n * one).\n *\n * Non-message input keys are shallow-merged into `values` via\n * `extraValues`; they are dropped/overwritten automatically by the\n * first server `values` event (which rebuilds `values` from the\n * server snapshot), or rolled back via {@link restoreValueKeys} when\n * the run fails before any echo.\n *\n * @param messages - Optimistic messages (already coerced to\n * `BaseMessage` instances, each carrying a stable id).\n * @param extraValues - Non-message input keys to shallow-merge into\n * `values`.\n */\n appendOptimistic(\n messages: BaseMessage[],\n extraValues?: Record<string, unknown>\n ): void {\n let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;\n let mutated = false;\n for (const message of messages) {\n const id = message.id;\n if (id == null) continue;\n const existingIdx = this.#indexById.get(id);\n if (existingIdx == null) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n this.#indexById.set(id, working.length);\n working.push(message);\n } else if (!messagesEqual(working[existingIdx], message)) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n working[existingIdx] = message;\n }\n }\n\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n let values = baselineValues;\n if (extraValues != null && Object.keys(extraValues).length > 0) {\n values = { ...(baselineValues as object), ...extraValues } as StateType;\n }\n values = syncMessagesIntoValues(values, this.#messagesKey, working);\n if (!mutated && values === baselineValues) return;\n this.#pendingMessages = working;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Drop optimistic messages by id without disturbing the rest of the\n * projection. Used by {@link StreamController.hydrate} to remove\n * never-persisted optimistic messages (`pending` / `failed`) so a\n * reload converges to server truth.\n *\n * @param ids - Message ids to remove.\n */\n dropOptimisticMessages(ids: ReadonlySet<string>): void {\n if (ids.size === 0) return;\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const next = baselineMessages.filter((m) => m.id == null || !ids.has(m.id));\n if (next.length === baselineMessages.length) return;\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(next)) {\n this.#indexById.set(id, idx);\n }\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n this.#pendingMessages = next;\n this.#pendingValues = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n next\n );\n this.#scheduleFlush();\n }\n\n /**\n * Restore (or delete) `values` keys that were optimistically merged\n * by {@link appendOptimistic} but never echoed by the server — i.e.\n * roll back non-message optimistic state when the run fails before\n * any `values` event lands. Messages are left untouched (kept on\n * failure per the optimistic contract).\n *\n * @param restore - Per-key pre-submit snapshot: when `hadKey` is\n * false the key is deleted, otherwise it is reset to `prevValue`.\n */\n restoreValueKeys(\n restore: ReadonlyArray<{\n key: string;\n hadKey: boolean;\n prevValue: unknown;\n }>\n ): void {\n if (restore.length === 0) return;\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const next = { ...(baselineValues as Record<string, unknown>) };\n let changed = false;\n for (const { key, hadKey, prevValue } of restore) {\n if (key === this.#messagesKey) continue;\n if (hadKey) {\n if (!Object.is(next[key], prevValue)) {\n next[key] = prevValue;\n changed = true;\n }\n } else if (Object.prototype.hasOwnProperty.call(next, key)) {\n delete next[key];\n changed = true;\n }\n }\n if (!changed) return;\n this.#pendingValues = next as StateType;\n this.#scheduleFlush();\n }\n\n /**\n * Schedule a coalesced flush on the next macrotask. Idempotent\n * within a tick — multiple `handleMessage` / `applyValues` calls\n * before the flush fires collapse into one store write.\n *\n * `setTimeout(0)` is a macrotask: it runs after the current\n * microtask chain drains, so a burst of SSE events processed by\n * the controller's `for await` pump becomes one `store.setState`\n * (and therefore one `useSyncExternalStore` notification).\n */\n #scheduleFlush = (): void => {\n if (this.#flushScheduled) return;\n this.#flushScheduled = true;\n setTimeout(this.#flushPending, 0);\n };\n\n /**\n * Drain `#pendingMessages` / `#pendingValues` to the store in a\n * single `setState` call.\n */\n #flushPending = (): void => {\n this.#flushScheduled = false;\n const messages = this.#pendingMessages;\n const values = this.#pendingValues;\n this.#pendingMessages = null;\n this.#pendingValues = null;\n if (messages == null && values == null) return;\n this.#store.setState((s) => {\n // Other rootStore mutators (controller-driven `isLoading`,\n // `interrupts`, `toolCalls`, etc.) do not touch `s.messages`\n // / `s.values`, so a last-write-wins commit on those two\n // fields is safe.\n if (messages == null) {\n return values == null ? s : { ...s, values };\n }\n if (values == null) return { ...s, messages };\n return { ...s, messages, values };\n });\n };\n}\n\n/**\n * Mirror a freshly-updated message list into `values[messagesKey]`.\n *\n * Returns the same `values` reference when the list is already\n * equal-by-content so the caller can keep the existing snapshot\n * identity (and avoid spurious `setSnapshot` notifications).\n */\nfunction syncMessagesIntoValues<StateType extends object>(\n values: StateType,\n messagesKey: string,\n messages: BaseMessage[]\n): StateType {\n const record = values as Record<string, unknown>;\n const current = record[messagesKey];\n if (Array.isArray(current) && messagesEqualList(current, messages)) {\n return values;\n }\n return {\n ...record,\n [messagesKey]: messages,\n } as StateType;\n}\n\n/**\n * True when two `BaseMessage` arrays carry the same per-message\n * content (using {@link messagesEqual}).\n */\nfunction messagesEqualList(\n previous: readonly BaseMessage[],\n next: readonly BaseMessage[]\n): boolean {\n if (previous === next) return true;\n if (previous.length !== next.length) return false;\n for (let i = 0; i < previous.length; i += 1) {\n if (!messagesEqual(previous[i], next[i])) return false;\n }\n return true;\n}\n\n/**\n * Shallow-equal for `values` objects, *ignoring* the messages slot.\n *\n * The messages array is compared separately by the caller (via\n * {@link messagesEqualList}) because both arrays contain class\n * instances whose JSON representation is not stable across reads.\n */\nfunction stateValuesShallowEqual(\n previous: object,\n next: object,\n messagesKey: string\n): boolean {\n if (previous === next) return true;\n const previousRecord = previous as Record<string, unknown>;\n const nextRecord = next as Record<string, unknown>;\n const previousKeys = Object.keys(previousRecord);\n const nextKeys = Object.keys(nextRecord);\n if (previousKeys.length !== nextKeys.length) return false;\n for (const key of previousKeys) {\n if (!Object.prototype.hasOwnProperty.call(nextRecord, key)) return false;\n const previousValue = previousRecord[key];\n const nextValue = nextRecord[key];\n if (\n key === messagesKey &&\n Array.isArray(previousValue) &&\n Array.isArray(nextValue)\n ) {\n continue;\n }\n if (!Object.is(previousValue, nextValue)) return false;\n }\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoGA,IAAa,wBAAb,MAGE;;;;;;CAMA;;CAGA;;;;;;CAOA,aAAa,IAAIE,iBAAAA,kBAAkB;;;;;;CAOnC,yBAAkB,IAAI,KAGnB;;;;;;CAOH,6BAAsB,IAAI,KAAqB;;;;;;;CAQ/C,oCAAoB,IAAI,KAAa;;;;;;CAOrC,yCAAkC,IAAI,KAAqB;;;;;;;;;;;;;CAc3D,mBAAyC;CACzC,iBAAmC;CACnC,kBAAkB;;;;;;;;;;;CAYlB,WAA+B,KAAA;;;;;;;;;;;;;;;;CAiB/B,oCAA6B,IAAI,KAAa;;;;;;;;;;;;;;;CAgB9C,YAAgC,KAAA;;;;;;CAOhC,YAAY,QAGT;AACD,QAAA,cAAoB,OAAO;AAC3B,QAAA,QAAc,OAAO;;;;;;CAOvB,QAAc;AACZ,QAAA,YAAkB,IAAIA,iBAAAA,kBAAkB;AACxC,QAAA,MAAY,OAAO;AACnB,QAAA,UAAgB,OAAO;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E,QAAQ,eACR,MAAM,QAAQ,cAAc,IAC5B,MAAM,QAAQ,UAAU,CAExB;AAEF,MAAI,CAAC,OAAO,GAAG,eAAe,UAAU,CAAE,QAAO;;AAEnD,QAAO"}
1
+ {"version":3,"file":"root-message-projection.cjs","names":["#messagesKey","#store","MessageAssembler","#roles","#indexById","#toolCallIdByNamespace","#sealedMessageIds","#assembler","#valuesMessageIds","#pendingMessages","#pendingValues","#flushScheduled","#maxStep","#sealStep","namespaceKey","assembledMessageToBaseMessage","messagesEqual","#scheduleFlush","reconcileMessagesFromValues","shouldPreferValuesMessageForToolCalls","buildMessageIndex","#flushPending"],"sources":["../../src/stream/root-message-projection.ts"],"sourcesContent":["/**\n * Root-namespace message projection.\n *\n * # What this module is\n *\n * The {@link RootMessageProjection} is the piece of the\n * {@link StreamController} that owns \"what messages does the root\n * namespace currently contain?\". It assembles streamed message deltas\n * via {@link MessageAssembler}, reconciles them against authoritative\n * `values.messages` snapshots from the server, and writes the merged\n * list back into the controller's root snapshot store.\n *\n * # Two streams of truth\n *\n * Root messages arrive on two channels and need to merge cleanly:\n *\n * - **`messages` channel.** Token-level deltas that build messages\n * incrementally. The {@link MessageAssembler} keeps partial\n * messages by id and emits an updated `BaseMessage` per delta.\n * - **`values` channel.** Periodic full-state snapshots that include\n * the authoritative messages array. Used for ordering, removals,\n * and forks (where the streamed messages may pre-date the new\n * timeline).\n *\n * The reconciliation rules (delegated to\n * {@link reconcileMessagesFromValues}) preserve in-flight streamed\n * content while letting values dictate ordering and removals.\n *\n * # Tool-message namespace correlation\n *\n * Tool messages arrive on `messages-start` events with `role: \"tool\"`\n * but the start event doesn't always include a `tool_call_id`. We\n * recover it via three fallbacks:\n *\n * 1. The start event itself, when the server includes it.\n * 2. The legacy `<id>-tool-<call_id>` message id format.\n * 3. The most recent `tool-started` event recorded under the same\n * namespace via {@link recordToolCallNamespace}.\n *\n * Without this correlation, tool messages render with empty\n * `tool_call_id` and downstream UIs can't pair them with the\n * originating tool call.\n *\n * # Store-write batching\n *\n * Every {@link handleMessage} / {@link applyValues} call updates the\n * in-projection bookkeeping (assembler state, id index, role cache)\n * synchronously, then stages the new `messages` / `values` into a\n * pending buffer and schedules a `setTimeout(0)` flush. A single\n * coalesced `store.setState` runs at the next macrotask boundary.\n *\n * The motivation is the long-replay freeze: a thread with hundreds\n * of messages replays through the `messages` channel on refresh or\n * mid-run join. Those events drain through the controller's\n * `for await` pump as a long microtask chain. Per-event\n * `store.setState` notifies `useSyncExternalStore` per event, and\n * after enough notifications React's `nestedUpdateCount` guard trips\n * with \"Maximum update depth exceeded\", permanently freezing the UI\n * on the first few messages. Coalescing to one notification per\n * macrotask lets React's scheduler commit between flushes.\n *\n * # Lifecycle\n *\n * - `handleMessage(event)` — apply a `messages` event delta.\n * - `applyValues(values, msgs)` — merge a `values` snapshot.\n * - `recordToolCallNamespace(ns, id)` — capture `namespace → tool_call_id`\n * so subsequent tool message starts can recover the id.\n * - `reset()` — clear all state on thread rebind.\n */\nimport type {\n MessagesEvent,\n MessageRole,\n MessageStartData,\n} from \"@langchain/protocol\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { MessageAssembler } from \"../client/stream/messages.js\";\nimport {\n assembledMessageToBaseMessage,\n type ExtendedMessageRole,\n} from \"./assembled-to-message.js\";\nimport type { StreamStore } from \"./store.js\";\nimport type { RootSnapshot } from \"./types.js\";\nimport { namespaceKey } from \"./namespace.js\";\nimport {\n buildMessageIndex,\n messagesEqual,\n reconcileMessagesFromValues,\n shouldPreferValuesMessageForToolCalls,\n} from \"./message-reconciliation.js\";\n\n/**\n * Root-namespace message projection. Owns the merge between the\n * `messages` (streamed deltas) and `values` (authoritative\n * snapshots) channels for the root namespace.\n *\n * @typeParam StateType - Root state shape; the messages array is read\n * from `values[messagesKey]`.\n * @typeParam InterruptType - Shape of root protocol interrupts (forwarded\n * into `RootSnapshot` updates).\n */\nexport class RootMessageProjection<\n StateType extends object,\n InterruptType = unknown,\n> {\n /**\n * Key inside `values` that holds the message array. Defaults to\n * `\"messages\"` in the controller; configurable for state graphs\n * that surface messages under a different slot.\n */\n readonly #messagesKey: string;\n\n /** Root snapshot store written to on every merge. */\n readonly #store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n\n /**\n * Stateful chunk assembler for in-flight messages. Reset (via a\n * fresh instance) on every {@link reset} so a new thread starts\n * with no half-built messages from the previous one.\n */\n #assembler = new MessageAssembler();\n\n /**\n * `messageId → role/toolCallId` captured from `message-start` events.\n * The assembler's intermediate output drops these fields, so we cache\n * them at start-time and reapply when projecting to a `BaseMessage`.\n */\n readonly #roles = new Map<\n string,\n { role: ExtendedMessageRole; toolCallId?: string }\n >();\n\n /**\n * `messageId → position in #store.messages` for fast in-place\n * updates as deltas arrive. Rebuilt on every full reconciliation\n * driven by a `values` event.\n */\n readonly #indexById = new Map<string, number>();\n\n /**\n * Ids observed in the most recent `values.messages` snapshot.\n * Reconciliation uses this to detect server-side removals: a\n * previously-seen id missing from the next snapshot means it was\n * removed by the server (and should drop from the projection).\n */\n #valuesMessageIds = new Set<string>();\n\n /**\n * `namespaceKey → tool_call_id` captured from root `tool-started`\n * events. Used as a fallback when a tool-role `message-start` is\n * missing its `tool_call_id` field.\n */\n readonly #toolCallIdByNamespace = new Map<string, string>();\n\n /**\n * Coalescing buffer for store writes. {@link handleMessage} and\n * {@link applyValues} stage their computed `messages` / `values`\n * here instead of calling `store.setState` per event. A single\n * `setTimeout(0)` flush commits them in one `setState`, so a\n * burst of SSE events draining as a microtask chain becomes one\n * store notification at the next macrotask boundary.\n *\n * `null` means \"no staged write\" — once a flush settles, the\n * slots are cleared so the next call starts from the latest\n * committed store snapshot.\n */\n #pendingMessages: BaseMessage[] | null = null;\n #pendingValues: StateType | null = null;\n #flushScheduled = false;\n\n /**\n * Highest checkpoint `step` whose `values` snapshot has been applied.\n * Seeded by {@link StreamController.hydrate} from `getState()` and\n * advanced by live `values` events. A snapshot arriving with a lower\n * step is an older checkpoint replayed by the content pump on\n * reconnect; it is reconciled in add-only mode so it cannot remove\n * the seeded message tail (the final assistant turn). `undefined`\n * until the first step-bearing snapshot, where the legacy\n * remove-on-absence behavior is preserved.\n */\n #maxStep: number | undefined = undefined;\n\n /**\n * Message ids seeded as complete-and-final from an idle thread's\n * `getState()` snapshot. An idle thread defers its root SSE pump, and\n * the first `submit()` brings it up — at which point the transport\n * replays the finished run from `seq=0`. Unlike the `values` channel\n * (guarded by {@link #maxStep}), `messages`-channel deltas carry no\n * step, so that replay would otherwise rebuild each already-complete\n * message from an empty `message-start` and re-stream the whole turn\n * token-by-token, clobbering the seeded tail (a visible \"messages\n * replay\" on the first submit). Deltas for a sealed id are dropped in\n * {@link handleMessage}. The seal is lifted once a checkpoint advances\n * strictly past {@link #sealStep} (see {@link applyValues}) or on\n * thread rebind ({@link reset}). New ids from the next run are never\n * sealed, so they stream normally.\n */\n readonly #sealedMessageIds = new Set<string>();\n\n /**\n * High-water {@link #maxStep} captured when {@link sealMessageIds} ran,\n * i.e. the seed checkpoint's step (or `undefined` when `getState()`\n * carried no `metadata.step`). It is the boundary between the replayed\n * idle history (steps `<= #sealStep`, emitted by the deferred pump's\n * `seq=0` replay) and the new run (steps `> #sealStep`); only a\n * checkpoint strictly past it lifts the seal. Without this boundary the\n * replayed old-run checkpoints — which themselves carry increasing\n * steps — would advance {@link #maxStep} and lift the seal mid-replay,\n * reopening the clobber. When the seed step is unknown the boundary\n * stays `undefined` and the seal holds until {@link reset}; the\n * `values` channel (which ignores the seal) still reconciles any\n * genuine change to a sealed id, only its streamed deltas are dropped.\n */\n #sealStep: number | undefined = undefined;\n\n /**\n * @param params.messagesKey - Key inside `values` that holds the\n * message array.\n * @param params.store - Root snapshot store to mutate.\n */\n constructor(params: {\n messagesKey: string;\n store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n }) {\n this.#messagesKey = params.messagesKey;\n this.#store = params.store;\n }\n\n /**\n * Drop all per-thread state. Called by the controller on thread\n * rebind / dispose so a swap doesn't surface stale messages.\n */\n reset(): void {\n this.#assembler = new MessageAssembler();\n this.#roles.clear();\n this.#indexById.clear();\n this.#valuesMessageIds = new Set();\n this.#toolCallIdByNamespace.clear();\n // Drop any unflushed pending writes — they were computed against\n // the previous thread's baseline and committing them after a\n // rebind would bleed stale messages into the new thread.\n this.#pendingMessages = null;\n this.#pendingValues = null;\n this.#flushScheduled = false;\n this.#maxStep = undefined;\n this.#sealedMessageIds.clear();\n this.#sealStep = undefined;\n }\n\n /**\n * Seal message ids so the streamed `messages` channel cannot downgrade\n * them to partial re-streams. Called by {@link StreamController.hydrate}\n * after seeding an idle thread, whose deferred pump replays the finished\n * run from `seq=0` on the first submit.\n *\n * Captures the current {@link #maxStep} as the lift boundary\n * ({@link #sealStep}). The seal is applied immediately after the seed's\n * `getState()` snapshot is reconciled, so `#maxStep` here is the seed\n * step (or `undefined` when `getState()` carried no `metadata.step`).\n * The seal is lifted once a checkpoint advances strictly past that\n * boundary (see {@link applyValues}) or on thread rebind\n * ({@link reset}).\n *\n * @param ids - Complete message ids from the idle `getState()` seed.\n */\n sealMessageIds(ids: Iterable<string>): void {\n for (const id of ids) this.#sealedMessageIds.add(id);\n if (this.#sealStep == null) this.#sealStep = this.#maxStep;\n }\n\n /**\n * Record a `namespace → tool_call_id` mapping captured from a root\n * `tool-started` event.\n *\n * The companion tool-role `message-start` event may not carry a\n * `tool_call_id`, so we fall back to the most recent value recorded\n * here for the same namespace.\n *\n * @param namespace - Event namespace from the `tool-started` event.\n * @param toolCallId - Tool call id from the same event.\n */\n recordToolCallNamespace(\n namespace: readonly string[],\n toolCallId: string\n ): void {\n this.#toolCallIdByNamespace.set(namespaceKey(namespace), toolCallId);\n }\n\n /**\n * Apply a `messages` channel event to the projection.\n *\n * Captures role/tool metadata on `message-start`, feeds the chunk\n * to the assembler, projects the assembled output to a\n * {@link BaseMessage}, and either appends or in-place updates the\n * pending messages buffer based on whether the id was seen before.\n *\n * @param event - The `messages` channel event to consume.\n */\n handleMessage(event: MessagesEvent): void {\n const data = event.params.data;\n if (data.event === \"message-start\") {\n const startData = data as MessageStartData;\n const role = (startData.role ?? \"ai\") as MessageRole;\n const extendedRole =\n (startData as { role?: ExtendedMessageRole }).role ?? role;\n let toolCallId = (startData as { tool_call_id?: string }).tool_call_id;\n // Tool messages need a tool_call_id to render. Fall back through:\n // 1. legacy `<id>-tool-<call_id>` message id format\n // 2. namespace-recorded tool_call_id (from #recordToolCallNamespace)\n if (extendedRole === \"tool\" && toolCallId == null) {\n const messageId = startData.id;\n if (messageId != null) {\n const match = /-tool-(.+)$/.exec(messageId);\n if (match != null) toolCallId = match[1];\n }\n if (toolCallId == null) {\n toolCallId = this.#toolCallIdByNamespace.get(\n namespaceKey(event.params.namespace)\n );\n }\n }\n if (startData.id != null) {\n this.#roles.set(startData.id, {\n role: extendedRole,\n toolCallId,\n });\n }\n }\n\n const update = this.#assembler.consume(event);\n if (update == null) return;\n const id = update.message.id;\n if (id == null) return;\n // A sealed id belongs to a message seeded complete from an idle\n // thread's `getState()`; the deferred pump's `seq=0` replay would\n // otherwise rebuild it from an empty start and re-stream the whole\n // turn. Drop the replayed delta — the authoritative seed already\n // holds the final content (see {@link #sealedMessageIds}).\n if (this.#sealedMessageIds.has(id)) return;\n const captured = this.#roles.get(id) ?? { role: \"ai\" as const };\n const base = assembledMessageToBaseMessage(update.message, captured.role, {\n toolCallId: captured.toolCallId,\n });\n\n // Compute against the pending baseline if we have one (so an\n // earlier handleMessage in the same tick is the input to this\n // one), else against the latest committed store snapshot.\n // `#indexById` is the synchronous source of truth for \"where is\n // each id in the current messages list\" — every code path below\n // keeps it in sync before returning.\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const existingIdx = this.#indexById.get(id);\n let messages: BaseMessage[];\n if (existingIdx == null) {\n this.#indexById.set(id, baselineMessages.length);\n messages = [...baselineMessages, base];\n } else if (messagesEqual(baselineMessages[existingIdx], base)) {\n // Identical re-emission — skip the store write to keep\n // snapshot identity stable.\n return;\n } else {\n messages = baselineMessages.slice();\n messages[existingIdx] = base;\n }\n\n // Mirror the new messages list into `values[messagesKey]` so\n // direct `values` reads (used by some hooks and by the eventual\n // `values` reconciliation) stay in sync.\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const values = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n messages\n );\n this.#pendingMessages = messages;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Reconcile a full `values` snapshot into the projection.\n *\n * Delegates the merge to {@link reconcileMessagesFromValues}:\n * values stays authoritative for ordering and removals, while\n * streamed in-flight messages keep their content until the server\n * echoes them back. Empty messages just refresh the values blob.\n *\n * Rebuilds {@link #indexById} after the merge so subsequent delta\n * applications target the new positions.\n *\n * @param nextValues - Full values snapshot from the `values` event.\n * @param nextMessages - The messages array extracted from\n * `values[messagesKey]` and coerced to `BaseMessage` instances.\n * @param opts.step - Checkpoint superstep for this snapshot, when\n * known. A snapshot whose step is below the highest applied step is\n * treated as a stale reconnect replay and reconciled add-only.\n */\n applyValues(\n nextValues: StateType,\n nextMessages: BaseMessage[],\n opts?: { step?: number }\n ): void {\n const baselineSnapshot = this.#store.getSnapshot();\n const baselineMessages = this.#pendingMessages ?? baselineSnapshot.messages;\n const baselineValues = this.#pendingValues ?? baselineSnapshot.values;\n\n const step = opts?.step;\n // Stale only when we have both a prior high-water step and a lower\n // incoming step. A missing step preserves the legacy semantics.\n const addOnly =\n step != null && this.#maxStep != null && step < this.#maxStep;\n if (step != null && (this.#maxStep == null || step > this.#maxStep)) {\n this.#maxStep = step;\n }\n // Lift the replay seal only when a checkpoint advances strictly past\n // the step captured when the ids were sealed (the seed step). That\n // boundary separates the replayed idle history (steps <= #sealStep,\n // emitted by the deferred pump's seq=0 replay) from the new run\n // (steps > #sealStep), so crossing it means seeded ids may now take\n // genuine streamed updates. Replayed old-run checkpoints advance\n // #maxStep but never reach past #sealStep, so they can't lift it. A\n // `null` boundary (the seed step was unknown) keeps the seal until\n // reset() — we can't tell replay from live, and the values channel\n // still reconciles a sealed id even while its streamed deltas drop.\n if (\n this.#sealedMessageIds.size > 0 &&\n step != null &&\n this.#sealStep != null &&\n step > this.#sealStep\n ) {\n this.#sealedMessageIds.clear();\n }\n\n if (nextMessages.length === 0) {\n if (\n stateValuesShallowEqual(baselineValues, nextValues, this.#messagesKey)\n ) {\n return;\n }\n // Mirror the current `messages` list back into the values slot\n // so the staged snapshot stays consistent with the (separately\n // tracked) messages array.\n this.#pendingValues = syncMessagesIntoValues(\n nextValues,\n this.#messagesKey,\n baselineMessages\n );\n this.#scheduleFlush();\n return;\n }\n\n const reconciliation = reconcileMessagesFromValues({\n valueMessages: nextMessages,\n currentMessages: baselineMessages,\n currentIndexById: this.#indexById,\n previousValueMessageIds: this.#valuesMessageIds,\n preferValuesMessage: shouldPreferValuesMessageForToolCalls,\n addOnly,\n });\n // A stale replay snapshot must not shrink the authoritative id set:\n // keep the (larger) seeded set so a genuinely-newer removal is still\n // detected once the timeline advances past the seed.\n if (!addOnly) this.#valuesMessageIds = reconciliation.valueMessageIds;\n const messages = reconciliation.messages as BaseMessage[];\n const values = {\n ...(nextValues as Record<string, unknown>),\n [this.#messagesKey]: messages,\n } as StateType;\n if (\n messages === baselineMessages &&\n stateValuesShallowEqual(baselineValues, values, this.#messagesKey)\n ) {\n return;\n }\n\n // Reconciliation may reorder, drop, or substitute messages, so\n // rebuild the id → index map to match the new array.\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(messages)) {\n this.#indexById.set(id, idx);\n }\n this.#pendingMessages = messages;\n this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Append messages applied optimistically by a local `submit()`,\n * keyed by id so the eventual server echo reconciles cleanly.\n *\n * Unlike {@link applyValues}, the supplied messages are *not* treated\n * as an authoritative ordered snapshot: they are appended to the end\n * of the current projection (or replaced in place when the id already\n * exists), preserving prior history ordering. When the server later\n * emits a `values` snapshot containing the same ids,\n * {@link applyValues} → {@link reconcileMessagesFromValues} takes over\n * (server ordering wins, the echoed message replaces the optimistic\n * one).\n *\n * Non-message input keys are shallow-merged into `values` via\n * `extraValues`; they are dropped/overwritten automatically by the\n * first server `values` event (which rebuilds `values` from the\n * server snapshot), or rolled back via {@link restoreValueKeys} when\n * the run fails before any echo.\n *\n * @param messages - Optimistic messages (already coerced to\n * `BaseMessage` instances, each carrying a stable id).\n * @param extraValues - Non-message input keys to shallow-merge into\n * `values`.\n * @param options - When `sync` is true the staged write is\n * committed to the store *synchronously* instead of being coalesced\n * onto the next macrotask. Used for discrete, user-initiated\n * optimistic writes (`submit()` / `respond()` / `respondAll()`):\n * committing in the same tick as the triggering event lets the\n * framework render the optimistic message in the *same* commit as\n * any local state the caller flipped alongside it (e.g. a HITL form\n * hiding its inputs), so the pushed card never blinks out for the\n * one-macrotask window before the flush lands. Streaming writes\n * (`handleMessage` / `applyValues`) keep the default macrotask\n * coalescing, which is what tames high-frequency SSE bursts.\n */\n appendOptimistic(\n messages: BaseMessage[],\n extraValues?: Record<string, unknown>,\n options?: { sync?: boolean }\n ): void {\n let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;\n let mutated = false;\n for (const message of messages) {\n const id = message.id;\n if (id == null) continue;\n const existingIdx = this.#indexById.get(id);\n if (existingIdx == null) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n this.#indexById.set(id, working.length);\n working.push(message);\n } else if (!messagesEqual(working[existingIdx], message)) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n working[existingIdx] = message;\n }\n }\n\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n let values = baselineValues;\n if (extraValues != null && Object.keys(extraValues).length > 0) {\n values = { ...(baselineValues as object), ...extraValues } as StateType;\n }\n values = syncMessagesIntoValues(values, this.#messagesKey, working);\n if (!mutated && values === baselineValues) return;\n this.#pendingMessages = working;\n if (values !== baselineValues) this.#pendingValues = values;\n if (options?.sync) {\n // Commit now so the optimistic message is visible in the same tick\n // as the user event that produced it (no one-macrotask blink). Any\n // flush already scheduled by a prior streaming write is absorbed\n // here; its pending timer fires later as a no-op.\n this.#flushPending();\n } else {\n this.#scheduleFlush();\n }\n }\n\n /**\n * Drop optimistic messages by id without disturbing the rest of the\n * projection. Used by {@link StreamController.hydrate} to remove\n * never-persisted optimistic messages (`pending` / `failed`) so a\n * reload converges to server truth.\n *\n * @param ids - Message ids to remove.\n */\n dropOptimisticMessages(ids: ReadonlySet<string>): void {\n if (ids.size === 0) return;\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const next = baselineMessages.filter((m) => m.id == null || !ids.has(m.id));\n if (next.length === baselineMessages.length) return;\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(next)) {\n this.#indexById.set(id, idx);\n }\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n this.#pendingMessages = next;\n this.#pendingValues = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n next\n );\n this.#scheduleFlush();\n }\n\n /**\n * Restore (or delete) `values` keys that were optimistically merged\n * by {@link appendOptimistic} but never echoed by the server — i.e.\n * roll back non-message optimistic state when the run fails before\n * any `values` event lands. Messages are left untouched (kept on\n * failure per the optimistic contract).\n *\n * @param restore - Per-key pre-submit snapshot: when `hadKey` is\n * false the key is deleted, otherwise it is reset to `prevValue`.\n */\n restoreValueKeys(\n restore: ReadonlyArray<{\n key: string;\n hadKey: boolean;\n prevValue: unknown;\n }>\n ): void {\n if (restore.length === 0) return;\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const next = { ...(baselineValues as Record<string, unknown>) };\n let changed = false;\n for (const { key, hadKey, prevValue } of restore) {\n if (key === this.#messagesKey) continue;\n if (hadKey) {\n if (!Object.is(next[key], prevValue)) {\n next[key] = prevValue;\n changed = true;\n }\n } else if (Object.prototype.hasOwnProperty.call(next, key)) {\n delete next[key];\n changed = true;\n }\n }\n if (!changed) return;\n this.#pendingValues = next as StateType;\n this.#scheduleFlush();\n }\n\n /**\n * Schedule a coalesced flush on the next macrotask. Idempotent\n * within a tick — multiple `handleMessage` / `applyValues` calls\n * before the flush fires collapse into one store write.\n *\n * `setTimeout(0)` is a macrotask: it runs after the current\n * microtask chain drains, so a burst of SSE events processed by\n * the controller's `for await` pump becomes one `store.setState`\n * (and therefore one `useSyncExternalStore` notification).\n */\n #scheduleFlush = (): void => {\n if (this.#flushScheduled) return;\n this.#flushScheduled = true;\n setTimeout(this.#flushPending, 0);\n };\n\n /**\n * Drain `#pendingMessages` / `#pendingValues` to the store in a\n * single `setState` call.\n */\n #flushPending = (): void => {\n this.#flushScheduled = false;\n const messages = this.#pendingMessages;\n const values = this.#pendingValues;\n this.#pendingMessages = null;\n this.#pendingValues = null;\n if (messages == null && values == null) return;\n this.#store.setState((s) => {\n // Other rootStore mutators (controller-driven `isLoading`,\n // `interrupts`, `toolCalls`, etc.) do not touch `s.messages`\n // / `s.values`, so a last-write-wins commit on those two\n // fields is safe.\n if (messages == null) {\n return values == null ? s : { ...s, values };\n }\n if (values == null) return { ...s, messages };\n return { ...s, messages, values };\n });\n };\n}\n\n/**\n * Mirror a freshly-updated message list into `values[messagesKey]`.\n *\n * Returns the same `values` reference when the list is already\n * equal-by-content so the caller can keep the existing snapshot\n * identity (and avoid spurious `setSnapshot` notifications).\n */\nfunction syncMessagesIntoValues<StateType extends object>(\n values: StateType,\n messagesKey: string,\n messages: BaseMessage[]\n): StateType {\n const record = values as Record<string, unknown>;\n const current = record[messagesKey];\n if (Array.isArray(current) && messagesEqualList(current, messages)) {\n return values;\n }\n return {\n ...record,\n [messagesKey]: messages,\n } as StateType;\n}\n\n/**\n * True when two `BaseMessage` arrays carry the same per-message\n * content (using {@link messagesEqual}).\n */\nfunction messagesEqualList(\n previous: readonly BaseMessage[],\n next: readonly BaseMessage[]\n): boolean {\n if (previous === next) return true;\n if (previous.length !== next.length) return false;\n for (let i = 0; i < previous.length; i += 1) {\n if (!messagesEqual(previous[i], next[i])) return false;\n }\n return true;\n}\n\n/**\n * Shallow-equal for `values` objects, *ignoring* the messages slot.\n *\n * The messages array is compared separately by the caller (via\n * {@link messagesEqualList}) because both arrays contain class\n * instances whose JSON representation is not stable across reads.\n */\nfunction stateValuesShallowEqual(\n previous: object,\n next: object,\n messagesKey: string\n): boolean {\n if (previous === next) return true;\n const previousRecord = previous as Record<string, unknown>;\n const nextRecord = next as Record<string, unknown>;\n const previousKeys = Object.keys(previousRecord);\n const nextKeys = Object.keys(nextRecord);\n if (previousKeys.length !== nextKeys.length) return false;\n for (const key of previousKeys) {\n if (!Object.prototype.hasOwnProperty.call(nextRecord, key)) return false;\n const previousValue = previousRecord[key];\n const nextValue = nextRecord[key];\n if (\n key === messagesKey &&\n Array.isArray(previousValue) &&\n Array.isArray(nextValue)\n ) {\n continue;\n }\n if (!Object.is(previousValue, nextValue)) return false;\n }\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoGA,IAAa,wBAAb,MAGE;;;;;;CAMA;;CAGA;;;;;;CAOA,aAAa,IAAIE,iBAAAA,kBAAkB;;;;;;CAOnC,yBAAkB,IAAI,KAGnB;;;;;;CAOH,6BAAsB,IAAI,KAAqB;;;;;;;CAQ/C,oCAAoB,IAAI,KAAa;;;;;;CAOrC,yCAAkC,IAAI,KAAqB;;;;;;;;;;;;;CAc3D,mBAAyC;CACzC,iBAAmC;CACnC,kBAAkB;;;;;;;;;;;CAYlB,WAA+B,KAAA;;;;;;;;;;;;;;;;CAiB/B,oCAA6B,IAAI,KAAa;;;;;;;;;;;;;;;CAgB9C,YAAgC,KAAA;;;;;;CAOhC,YAAY,QAGT;AACD,QAAA,cAAoB,OAAO;AAC3B,QAAA,QAAc,OAAO;;;;;;CAOvB,QAAc;AACZ,QAAA,YAAkB,IAAIA,iBAAAA,kBAAkB;AACxC,QAAA,MAAY,OAAO;AACnB,QAAA,UAAgB,OAAO;AACvB,QAAA,mCAAyB,IAAI,KAAK;AAClC,QAAA,sBAA4B,OAAO;AAInC,QAAA,kBAAwB;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AQ,cAAc,IAC5B,MAAM,QAAQ,UAAU,CAExB;AAEF,MAAI,CAAC,OAAO,GAAG,eAAe,UAAU,CAAE,QAAO;;AAEnD,QAAO"}
@@ -300,8 +300,19 @@ var RootMessageProjection = class {
300
300
  * `BaseMessage` instances, each carrying a stable id).
301
301
  * @param extraValues - Non-message input keys to shallow-merge into
302
302
  * `values`.
303
+ * @param options - When `sync` is true the staged write is
304
+ * committed to the store *synchronously* instead of being coalesced
305
+ * onto the next macrotask. Used for discrete, user-initiated
306
+ * optimistic writes (`submit()` / `respond()` / `respondAll()`):
307
+ * committing in the same tick as the triggering event lets the
308
+ * framework render the optimistic message in the *same* commit as
309
+ * any local state the caller flipped alongside it (e.g. a HITL form
310
+ * hiding its inputs), so the pushed card never blinks out for the
311
+ * one-macrotask window before the flush lands. Streaming writes
312
+ * (`handleMessage` / `applyValues`) keep the default macrotask
313
+ * coalescing, which is what tames high-frequency SSE bursts.
303
314
  */
304
- appendOptimistic(messages, extraValues) {
315
+ appendOptimistic(messages, extraValues, options) {
305
316
  let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;
306
317
  let mutated = false;
307
318
  for (const message of messages) {
@@ -333,7 +344,8 @@ var RootMessageProjection = class {
333
344
  if (!mutated && values === baselineValues) return;
334
345
  this.#pendingMessages = working;
335
346
  if (values !== baselineValues) this.#pendingValues = values;
336
- this.#scheduleFlush();
347
+ if (options?.sync) this.#flushPending();
348
+ else this.#scheduleFlush();
337
349
  }
338
350
  /**
339
351
  * Drop optimistic messages by id without disturbing the rest of the
@@ -1 +1 @@
1
- {"version":3,"file":"root-message-projection.js","names":["#messagesKey","#store","#roles","#indexById","#toolCallIdByNamespace","#sealedMessageIds","#assembler","#valuesMessageIds","#pendingMessages","#pendingValues","#flushScheduled","#maxStep","#sealStep","#scheduleFlush","#flushPending"],"sources":["../../src/stream/root-message-projection.ts"],"sourcesContent":["/**\n * Root-namespace message projection.\n *\n * # What this module is\n *\n * The {@link RootMessageProjection} is the piece of the\n * {@link StreamController} that owns \"what messages does the root\n * namespace currently contain?\". It assembles streamed message deltas\n * via {@link MessageAssembler}, reconciles them against authoritative\n * `values.messages` snapshots from the server, and writes the merged\n * list back into the controller's root snapshot store.\n *\n * # Two streams of truth\n *\n * Root messages arrive on two channels and need to merge cleanly:\n *\n * - **`messages` channel.** Token-level deltas that build messages\n * incrementally. The {@link MessageAssembler} keeps partial\n * messages by id and emits an updated `BaseMessage` per delta.\n * - **`values` channel.** Periodic full-state snapshots that include\n * the authoritative messages array. Used for ordering, removals,\n * and forks (where the streamed messages may pre-date the new\n * timeline).\n *\n * The reconciliation rules (delegated to\n * {@link reconcileMessagesFromValues}) preserve in-flight streamed\n * content while letting values dictate ordering and removals.\n *\n * # Tool-message namespace correlation\n *\n * Tool messages arrive on `messages-start` events with `role: \"tool\"`\n * but the start event doesn't always include a `tool_call_id`. We\n * recover it via three fallbacks:\n *\n * 1. The start event itself, when the server includes it.\n * 2. The legacy `<id>-tool-<call_id>` message id format.\n * 3. The most recent `tool-started` event recorded under the same\n * namespace via {@link recordToolCallNamespace}.\n *\n * Without this correlation, tool messages render with empty\n * `tool_call_id` and downstream UIs can't pair them with the\n * originating tool call.\n *\n * # Store-write batching\n *\n * Every {@link handleMessage} / {@link applyValues} call updates the\n * in-projection bookkeeping (assembler state, id index, role cache)\n * synchronously, then stages the new `messages` / `values` into a\n * pending buffer and schedules a `setTimeout(0)` flush. A single\n * coalesced `store.setState` runs at the next macrotask boundary.\n *\n * The motivation is the long-replay freeze: a thread with hundreds\n * of messages replays through the `messages` channel on refresh or\n * mid-run join. Those events drain through the controller's\n * `for await` pump as a long microtask chain. Per-event\n * `store.setState` notifies `useSyncExternalStore` per event, and\n * after enough notifications React's `nestedUpdateCount` guard trips\n * with \"Maximum update depth exceeded\", permanently freezing the UI\n * on the first few messages. Coalescing to one notification per\n * macrotask lets React's scheduler commit between flushes.\n *\n * # Lifecycle\n *\n * - `handleMessage(event)` — apply a `messages` event delta.\n * - `applyValues(values, msgs)` — merge a `values` snapshot.\n * - `recordToolCallNamespace(ns, id)` — capture `namespace → tool_call_id`\n * so subsequent tool message starts can recover the id.\n * - `reset()` — clear all state on thread rebind.\n */\nimport type {\n MessagesEvent,\n MessageRole,\n MessageStartData,\n} from \"@langchain/protocol\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { MessageAssembler } from \"../client/stream/messages.js\";\nimport {\n assembledMessageToBaseMessage,\n type ExtendedMessageRole,\n} from \"./assembled-to-message.js\";\nimport type { StreamStore } from \"./store.js\";\nimport type { RootSnapshot } from \"./types.js\";\nimport { namespaceKey } from \"./namespace.js\";\nimport {\n buildMessageIndex,\n messagesEqual,\n reconcileMessagesFromValues,\n shouldPreferValuesMessageForToolCalls,\n} from \"./message-reconciliation.js\";\n\n/**\n * Root-namespace message projection. Owns the merge between the\n * `messages` (streamed deltas) and `values` (authoritative\n * snapshots) channels for the root namespace.\n *\n * @typeParam StateType - Root state shape; the messages array is read\n * from `values[messagesKey]`.\n * @typeParam InterruptType - Shape of root protocol interrupts (forwarded\n * into `RootSnapshot` updates).\n */\nexport class RootMessageProjection<\n StateType extends object,\n InterruptType = unknown,\n> {\n /**\n * Key inside `values` that holds the message array. Defaults to\n * `\"messages\"` in the controller; configurable for state graphs\n * that surface messages under a different slot.\n */\n readonly #messagesKey: string;\n\n /** Root snapshot store written to on every merge. */\n readonly #store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n\n /**\n * Stateful chunk assembler for in-flight messages. Reset (via a\n * fresh instance) on every {@link reset} so a new thread starts\n * with no half-built messages from the previous one.\n */\n #assembler = new MessageAssembler();\n\n /**\n * `messageId → role/toolCallId` captured from `message-start` events.\n * The assembler's intermediate output drops these fields, so we cache\n * them at start-time and reapply when projecting to a `BaseMessage`.\n */\n readonly #roles = new Map<\n string,\n { role: ExtendedMessageRole; toolCallId?: string }\n >();\n\n /**\n * `messageId → position in #store.messages` for fast in-place\n * updates as deltas arrive. Rebuilt on every full reconciliation\n * driven by a `values` event.\n */\n readonly #indexById = new Map<string, number>();\n\n /**\n * Ids observed in the most recent `values.messages` snapshot.\n * Reconciliation uses this to detect server-side removals: a\n * previously-seen id missing from the next snapshot means it was\n * removed by the server (and should drop from the projection).\n */\n #valuesMessageIds = new Set<string>();\n\n /**\n * `namespaceKey → tool_call_id` captured from root `tool-started`\n * events. Used as a fallback when a tool-role `message-start` is\n * missing its `tool_call_id` field.\n */\n readonly #toolCallIdByNamespace = new Map<string, string>();\n\n /**\n * Coalescing buffer for store writes. {@link handleMessage} and\n * {@link applyValues} stage their computed `messages` / `values`\n * here instead of calling `store.setState` per event. A single\n * `setTimeout(0)` flush commits them in one `setState`, so a\n * burst of SSE events draining as a microtask chain becomes one\n * store notification at the next macrotask boundary.\n *\n * `null` means \"no staged write\" — once a flush settles, the\n * slots are cleared so the next call starts from the latest\n * committed store snapshot.\n */\n #pendingMessages: BaseMessage[] | null = null;\n #pendingValues: StateType | null = null;\n #flushScheduled = false;\n\n /**\n * Highest checkpoint `step` whose `values` snapshot has been applied.\n * Seeded by {@link StreamController.hydrate} from `getState()` and\n * advanced by live `values` events. A snapshot arriving with a lower\n * step is an older checkpoint replayed by the content pump on\n * reconnect; it is reconciled in add-only mode so it cannot remove\n * the seeded message tail (the final assistant turn). `undefined`\n * until the first step-bearing snapshot, where the legacy\n * remove-on-absence behavior is preserved.\n */\n #maxStep: number | undefined = undefined;\n\n /**\n * Message ids seeded as complete-and-final from an idle thread's\n * `getState()` snapshot. An idle thread defers its root SSE pump, and\n * the first `submit()` brings it up — at which point the transport\n * replays the finished run from `seq=0`. Unlike the `values` channel\n * (guarded by {@link #maxStep}), `messages`-channel deltas carry no\n * step, so that replay would otherwise rebuild each already-complete\n * message from an empty `message-start` and re-stream the whole turn\n * token-by-token, clobbering the seeded tail (a visible \"messages\n * replay\" on the first submit). Deltas for a sealed id are dropped in\n * {@link handleMessage}. The seal is lifted once a checkpoint advances\n * strictly past {@link #sealStep} (see {@link applyValues}) or on\n * thread rebind ({@link reset}). New ids from the next run are never\n * sealed, so they stream normally.\n */\n readonly #sealedMessageIds = new Set<string>();\n\n /**\n * High-water {@link #maxStep} captured when {@link sealMessageIds} ran,\n * i.e. the seed checkpoint's step (or `undefined` when `getState()`\n * carried no `metadata.step`). It is the boundary between the replayed\n * idle history (steps `<= #sealStep`, emitted by the deferred pump's\n * `seq=0` replay) and the new run (steps `> #sealStep`); only a\n * checkpoint strictly past it lifts the seal. Without this boundary the\n * replayed old-run checkpoints — which themselves carry increasing\n * steps — would advance {@link #maxStep} and lift the seal mid-replay,\n * reopening the clobber. When the seed step is unknown the boundary\n * stays `undefined` and the seal holds until {@link reset}; the\n * `values` channel (which ignores the seal) still reconciles any\n * genuine change to a sealed id, only its streamed deltas are dropped.\n */\n #sealStep: number | undefined = undefined;\n\n /**\n * @param params.messagesKey - Key inside `values` that holds the\n * message array.\n * @param params.store - Root snapshot store to mutate.\n */\n constructor(params: {\n messagesKey: string;\n store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n }) {\n this.#messagesKey = params.messagesKey;\n this.#store = params.store;\n }\n\n /**\n * Drop all per-thread state. Called by the controller on thread\n * rebind / dispose so a swap doesn't surface stale messages.\n */\n reset(): void {\n this.#assembler = new MessageAssembler();\n this.#roles.clear();\n this.#indexById.clear();\n this.#valuesMessageIds = new Set();\n this.#toolCallIdByNamespace.clear();\n // Drop any unflushed pending writes — they were computed against\n // the previous thread's baseline and committing them after a\n // rebind would bleed stale messages into the new thread.\n this.#pendingMessages = null;\n this.#pendingValues = null;\n this.#flushScheduled = false;\n this.#maxStep = undefined;\n this.#sealedMessageIds.clear();\n this.#sealStep = undefined;\n }\n\n /**\n * Seal message ids so the streamed `messages` channel cannot downgrade\n * them to partial re-streams. Called by {@link StreamController.hydrate}\n * after seeding an idle thread, whose deferred pump replays the finished\n * run from `seq=0` on the first submit.\n *\n * Captures the current {@link #maxStep} as the lift boundary\n * ({@link #sealStep}). The seal is applied immediately after the seed's\n * `getState()` snapshot is reconciled, so `#maxStep` here is the seed\n * step (or `undefined` when `getState()` carried no `metadata.step`).\n * The seal is lifted once a checkpoint advances strictly past that\n * boundary (see {@link applyValues}) or on thread rebind\n * ({@link reset}).\n *\n * @param ids - Complete message ids from the idle `getState()` seed.\n */\n sealMessageIds(ids: Iterable<string>): void {\n for (const id of ids) this.#sealedMessageIds.add(id);\n if (this.#sealStep == null) this.#sealStep = this.#maxStep;\n }\n\n /**\n * Record a `namespace → tool_call_id` mapping captured from a root\n * `tool-started` event.\n *\n * The companion tool-role `message-start` event may not carry a\n * `tool_call_id`, so we fall back to the most recent value recorded\n * here for the same namespace.\n *\n * @param namespace - Event namespace from the `tool-started` event.\n * @param toolCallId - Tool call id from the same event.\n */\n recordToolCallNamespace(\n namespace: readonly string[],\n toolCallId: string\n ): void {\n this.#toolCallIdByNamespace.set(namespaceKey(namespace), toolCallId);\n }\n\n /**\n * Apply a `messages` channel event to the projection.\n *\n * Captures role/tool metadata on `message-start`, feeds the chunk\n * to the assembler, projects the assembled output to a\n * {@link BaseMessage}, and either appends or in-place updates the\n * pending messages buffer based on whether the id was seen before.\n *\n * @param event - The `messages` channel event to consume.\n */\n handleMessage(event: MessagesEvent): void {\n const data = event.params.data;\n if (data.event === \"message-start\") {\n const startData = data as MessageStartData;\n const role = (startData.role ?? \"ai\") as MessageRole;\n const extendedRole =\n (startData as { role?: ExtendedMessageRole }).role ?? role;\n let toolCallId = (startData as { tool_call_id?: string }).tool_call_id;\n // Tool messages need a tool_call_id to render. Fall back through:\n // 1. legacy `<id>-tool-<call_id>` message id format\n // 2. namespace-recorded tool_call_id (from #recordToolCallNamespace)\n if (extendedRole === \"tool\" && toolCallId == null) {\n const messageId = startData.id;\n if (messageId != null) {\n const match = /-tool-(.+)$/.exec(messageId);\n if (match != null) toolCallId = match[1];\n }\n if (toolCallId == null) {\n toolCallId = this.#toolCallIdByNamespace.get(\n namespaceKey(event.params.namespace)\n );\n }\n }\n if (startData.id != null) {\n this.#roles.set(startData.id, {\n role: extendedRole,\n toolCallId,\n });\n }\n }\n\n const update = this.#assembler.consume(event);\n if (update == null) return;\n const id = update.message.id;\n if (id == null) return;\n // A sealed id belongs to a message seeded complete from an idle\n // thread's `getState()`; the deferred pump's `seq=0` replay would\n // otherwise rebuild it from an empty start and re-stream the whole\n // turn. Drop the replayed delta — the authoritative seed already\n // holds the final content (see {@link #sealedMessageIds}).\n if (this.#sealedMessageIds.has(id)) return;\n const captured = this.#roles.get(id) ?? { role: \"ai\" as const };\n const base = assembledMessageToBaseMessage(update.message, captured.role, {\n toolCallId: captured.toolCallId,\n });\n\n // Compute against the pending baseline if we have one (so an\n // earlier handleMessage in the same tick is the input to this\n // one), else against the latest committed store snapshot.\n // `#indexById` is the synchronous source of truth for \"where is\n // each id in the current messages list\" — every code path below\n // keeps it in sync before returning.\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const existingIdx = this.#indexById.get(id);\n let messages: BaseMessage[];\n if (existingIdx == null) {\n this.#indexById.set(id, baselineMessages.length);\n messages = [...baselineMessages, base];\n } else if (messagesEqual(baselineMessages[existingIdx], base)) {\n // Identical re-emission — skip the store write to keep\n // snapshot identity stable.\n return;\n } else {\n messages = baselineMessages.slice();\n messages[existingIdx] = base;\n }\n\n // Mirror the new messages list into `values[messagesKey]` so\n // direct `values` reads (used by some hooks and by the eventual\n // `values` reconciliation) stay in sync.\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const values = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n messages\n );\n this.#pendingMessages = messages;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Reconcile a full `values` snapshot into the projection.\n *\n * Delegates the merge to {@link reconcileMessagesFromValues}:\n * values stays authoritative for ordering and removals, while\n * streamed in-flight messages keep their content until the server\n * echoes them back. Empty messages just refresh the values blob.\n *\n * Rebuilds {@link #indexById} after the merge so subsequent delta\n * applications target the new positions.\n *\n * @param nextValues - Full values snapshot from the `values` event.\n * @param nextMessages - The messages array extracted from\n * `values[messagesKey]` and coerced to `BaseMessage` instances.\n * @param opts.step - Checkpoint superstep for this snapshot, when\n * known. A snapshot whose step is below the highest applied step is\n * treated as a stale reconnect replay and reconciled add-only.\n */\n applyValues(\n nextValues: StateType,\n nextMessages: BaseMessage[],\n opts?: { step?: number }\n ): void {\n const baselineSnapshot = this.#store.getSnapshot();\n const baselineMessages = this.#pendingMessages ?? baselineSnapshot.messages;\n const baselineValues = this.#pendingValues ?? baselineSnapshot.values;\n\n const step = opts?.step;\n // Stale only when we have both a prior high-water step and a lower\n // incoming step. A missing step preserves the legacy semantics.\n const addOnly =\n step != null && this.#maxStep != null && step < this.#maxStep;\n if (step != null && (this.#maxStep == null || step > this.#maxStep)) {\n this.#maxStep = step;\n }\n // Lift the replay seal only when a checkpoint advances strictly past\n // the step captured when the ids were sealed (the seed step). That\n // boundary separates the replayed idle history (steps <= #sealStep,\n // emitted by the deferred pump's seq=0 replay) from the new run\n // (steps > #sealStep), so crossing it means seeded ids may now take\n // genuine streamed updates. Replayed old-run checkpoints advance\n // #maxStep but never reach past #sealStep, so they can't lift it. A\n // `null` boundary (the seed step was unknown) keeps the seal until\n // reset() — we can't tell replay from live, and the values channel\n // still reconciles a sealed id even while its streamed deltas drop.\n if (\n this.#sealedMessageIds.size > 0 &&\n step != null &&\n this.#sealStep != null &&\n step > this.#sealStep\n ) {\n this.#sealedMessageIds.clear();\n }\n\n if (nextMessages.length === 0) {\n if (\n stateValuesShallowEqual(baselineValues, nextValues, this.#messagesKey)\n ) {\n return;\n }\n // Mirror the current `messages` list back into the values slot\n // so the staged snapshot stays consistent with the (separately\n // tracked) messages array.\n this.#pendingValues = syncMessagesIntoValues(\n nextValues,\n this.#messagesKey,\n baselineMessages\n );\n this.#scheduleFlush();\n return;\n }\n\n const reconciliation = reconcileMessagesFromValues({\n valueMessages: nextMessages,\n currentMessages: baselineMessages,\n currentIndexById: this.#indexById,\n previousValueMessageIds: this.#valuesMessageIds,\n preferValuesMessage: shouldPreferValuesMessageForToolCalls,\n addOnly,\n });\n // A stale replay snapshot must not shrink the authoritative id set:\n // keep the (larger) seeded set so a genuinely-newer removal is still\n // detected once the timeline advances past the seed.\n if (!addOnly) this.#valuesMessageIds = reconciliation.valueMessageIds;\n const messages = reconciliation.messages as BaseMessage[];\n const values = {\n ...(nextValues as Record<string, unknown>),\n [this.#messagesKey]: messages,\n } as StateType;\n if (\n messages === baselineMessages &&\n stateValuesShallowEqual(baselineValues, values, this.#messagesKey)\n ) {\n return;\n }\n\n // Reconciliation may reorder, drop, or substitute messages, so\n // rebuild the id → index map to match the new array.\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(messages)) {\n this.#indexById.set(id, idx);\n }\n this.#pendingMessages = messages;\n this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Append messages applied optimistically by a local `submit()`,\n * keyed by id so the eventual server echo reconciles cleanly.\n *\n * Unlike {@link applyValues}, the supplied messages are *not* treated\n * as an authoritative ordered snapshot: they are appended to the end\n * of the current projection (or replaced in place when the id already\n * exists), preserving prior history ordering. When the server later\n * emits a `values` snapshot containing the same ids,\n * {@link applyValues} → {@link reconcileMessagesFromValues} takes over\n * (server ordering wins, the echoed message replaces the optimistic\n * one).\n *\n * Non-message input keys are shallow-merged into `values` via\n * `extraValues`; they are dropped/overwritten automatically by the\n * first server `values` event (which rebuilds `values` from the\n * server snapshot), or rolled back via {@link restoreValueKeys} when\n * the run fails before any echo.\n *\n * @param messages - Optimistic messages (already coerced to\n * `BaseMessage` instances, each carrying a stable id).\n * @param extraValues - Non-message input keys to shallow-merge into\n * `values`.\n */\n appendOptimistic(\n messages: BaseMessage[],\n extraValues?: Record<string, unknown>\n ): void {\n let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;\n let mutated = false;\n for (const message of messages) {\n const id = message.id;\n if (id == null) continue;\n const existingIdx = this.#indexById.get(id);\n if (existingIdx == null) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n this.#indexById.set(id, working.length);\n working.push(message);\n } else if (!messagesEqual(working[existingIdx], message)) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n working[existingIdx] = message;\n }\n }\n\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n let values = baselineValues;\n if (extraValues != null && Object.keys(extraValues).length > 0) {\n values = { ...(baselineValues as object), ...extraValues } as StateType;\n }\n values = syncMessagesIntoValues(values, this.#messagesKey, working);\n if (!mutated && values === baselineValues) return;\n this.#pendingMessages = working;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Drop optimistic messages by id without disturbing the rest of the\n * projection. Used by {@link StreamController.hydrate} to remove\n * never-persisted optimistic messages (`pending` / `failed`) so a\n * reload converges to server truth.\n *\n * @param ids - Message ids to remove.\n */\n dropOptimisticMessages(ids: ReadonlySet<string>): void {\n if (ids.size === 0) return;\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const next = baselineMessages.filter((m) => m.id == null || !ids.has(m.id));\n if (next.length === baselineMessages.length) return;\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(next)) {\n this.#indexById.set(id, idx);\n }\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n this.#pendingMessages = next;\n this.#pendingValues = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n next\n );\n this.#scheduleFlush();\n }\n\n /**\n * Restore (or delete) `values` keys that were optimistically merged\n * by {@link appendOptimistic} but never echoed by the server — i.e.\n * roll back non-message optimistic state when the run fails before\n * any `values` event lands. Messages are left untouched (kept on\n * failure per the optimistic contract).\n *\n * @param restore - Per-key pre-submit snapshot: when `hadKey` is\n * false the key is deleted, otherwise it is reset to `prevValue`.\n */\n restoreValueKeys(\n restore: ReadonlyArray<{\n key: string;\n hadKey: boolean;\n prevValue: unknown;\n }>\n ): void {\n if (restore.length === 0) return;\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const next = { ...(baselineValues as Record<string, unknown>) };\n let changed = false;\n for (const { key, hadKey, prevValue } of restore) {\n if (key === this.#messagesKey) continue;\n if (hadKey) {\n if (!Object.is(next[key], prevValue)) {\n next[key] = prevValue;\n changed = true;\n }\n } else if (Object.prototype.hasOwnProperty.call(next, key)) {\n delete next[key];\n changed = true;\n }\n }\n if (!changed) return;\n this.#pendingValues = next as StateType;\n this.#scheduleFlush();\n }\n\n /**\n * Schedule a coalesced flush on the next macrotask. Idempotent\n * within a tick — multiple `handleMessage` / `applyValues` calls\n * before the flush fires collapse into one store write.\n *\n * `setTimeout(0)` is a macrotask: it runs after the current\n * microtask chain drains, so a burst of SSE events processed by\n * the controller's `for await` pump becomes one `store.setState`\n * (and therefore one `useSyncExternalStore` notification).\n */\n #scheduleFlush = (): void => {\n if (this.#flushScheduled) return;\n this.#flushScheduled = true;\n setTimeout(this.#flushPending, 0);\n };\n\n /**\n * Drain `#pendingMessages` / `#pendingValues` to the store in a\n * single `setState` call.\n */\n #flushPending = (): void => {\n this.#flushScheduled = false;\n const messages = this.#pendingMessages;\n const values = this.#pendingValues;\n this.#pendingMessages = null;\n this.#pendingValues = null;\n if (messages == null && values == null) return;\n this.#store.setState((s) => {\n // Other rootStore mutators (controller-driven `isLoading`,\n // `interrupts`, `toolCalls`, etc.) do not touch `s.messages`\n // / `s.values`, so a last-write-wins commit on those two\n // fields is safe.\n if (messages == null) {\n return values == null ? s : { ...s, values };\n }\n if (values == null) return { ...s, messages };\n return { ...s, messages, values };\n });\n };\n}\n\n/**\n * Mirror a freshly-updated message list into `values[messagesKey]`.\n *\n * Returns the same `values` reference when the list is already\n * equal-by-content so the caller can keep the existing snapshot\n * identity (and avoid spurious `setSnapshot` notifications).\n */\nfunction syncMessagesIntoValues<StateType extends object>(\n values: StateType,\n messagesKey: string,\n messages: BaseMessage[]\n): StateType {\n const record = values as Record<string, unknown>;\n const current = record[messagesKey];\n if (Array.isArray(current) && messagesEqualList(current, messages)) {\n return values;\n }\n return {\n ...record,\n [messagesKey]: messages,\n } as StateType;\n}\n\n/**\n * True when two `BaseMessage` arrays carry the same per-message\n * content (using {@link messagesEqual}).\n */\nfunction messagesEqualList(\n previous: readonly BaseMessage[],\n next: readonly BaseMessage[]\n): boolean {\n if (previous === next) return true;\n if (previous.length !== next.length) return false;\n for (let i = 0; i < previous.length; i += 1) {\n if (!messagesEqual(previous[i], next[i])) return false;\n }\n return true;\n}\n\n/**\n * Shallow-equal for `values` objects, *ignoring* the messages slot.\n *\n * The messages array is compared separately by the caller (via\n * {@link messagesEqualList}) because both arrays contain class\n * instances whose JSON representation is not stable across reads.\n */\nfunction stateValuesShallowEqual(\n previous: object,\n next: object,\n messagesKey: string\n): boolean {\n if (previous === next) return true;\n const previousRecord = previous as Record<string, unknown>;\n const nextRecord = next as Record<string, unknown>;\n const previousKeys = Object.keys(previousRecord);\n const nextKeys = Object.keys(nextRecord);\n if (previousKeys.length !== nextKeys.length) return false;\n for (const key of previousKeys) {\n if (!Object.prototype.hasOwnProperty.call(nextRecord, key)) return false;\n const previousValue = previousRecord[key];\n const nextValue = nextRecord[key];\n if (\n key === messagesKey &&\n Array.isArray(previousValue) &&\n Array.isArray(nextValue)\n ) {\n continue;\n }\n if (!Object.is(previousValue, nextValue)) return false;\n }\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoGA,IAAa,wBAAb,MAGE;;;;;;CAMA;;CAGA;;;;;;CAOA,aAAa,IAAI,kBAAkB;;;;;;CAOnC,yBAAkB,IAAI,KAGnB;;;;;;CAOH,6BAAsB,IAAI,KAAqB;;;;;;;CAQ/C,oCAAoB,IAAI,KAAa;;;;;;CAOrC,yCAAkC,IAAI,KAAqB;;;;;;;;;;;;;CAc3D,mBAAyC;CACzC,iBAAmC;CACnC,kBAAkB;;;;;;;;;;;CAYlB,WAA+B,KAAA;;;;;;;;;;;;;;;;CAiB/B,oCAA6B,IAAI,KAAa;;;;;;;;;;;;;;;CAgB9C,YAAgC,KAAA;;;;;;CAOh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CAAE,QAAO;EACnE,MAAM,gBAAgB,eAAe;EACrC,MAAM,YAAY,WAAW;AAC7B,MACE,QAAQ,eACR,MAAM,QAAQ,cAAc,IAC5B,MAAM,QAAQ,UAAU,CAExB;AAEF,MAAI,CAAC,OAAO,GAAG,eAAe,UAAU,CAAE,QAAO;;AAEnD,QAAO"}
1
+ {"version":3,"file":"root-message-projection.js","names":["#messagesKey","#store","#roles","#indexById","#toolCallIdByNamespace","#sealedMessageIds","#assembler","#valuesMessageIds","#pendingMessages","#pendingValues","#flushScheduled","#maxStep","#sealStep","#scheduleFlush","#flushPending"],"sources":["../../src/stream/root-message-projection.ts"],"sourcesContent":["/**\n * Root-namespace message projection.\n *\n * # What this module is\n *\n * The {@link RootMessageProjection} is the piece of the\n * {@link StreamController} that owns \"what messages does the root\n * namespace currently contain?\". It assembles streamed message deltas\n * via {@link MessageAssembler}, reconciles them against authoritative\n * `values.messages` snapshots from the server, and writes the merged\n * list back into the controller's root snapshot store.\n *\n * # Two streams of truth\n *\n * Root messages arrive on two channels and need to merge cleanly:\n *\n * - **`messages` channel.** Token-level deltas that build messages\n * incrementally. The {@link MessageAssembler} keeps partial\n * messages by id and emits an updated `BaseMessage` per delta.\n * - **`values` channel.** Periodic full-state snapshots that include\n * the authoritative messages array. Used for ordering, removals,\n * and forks (where the streamed messages may pre-date the new\n * timeline).\n *\n * The reconciliation rules (delegated to\n * {@link reconcileMessagesFromValues}) preserve in-flight streamed\n * content while letting values dictate ordering and removals.\n *\n * # Tool-message namespace correlation\n *\n * Tool messages arrive on `messages-start` events with `role: \"tool\"`\n * but the start event doesn't always include a `tool_call_id`. We\n * recover it via three fallbacks:\n *\n * 1. The start event itself, when the server includes it.\n * 2. The legacy `<id>-tool-<call_id>` message id format.\n * 3. The most recent `tool-started` event recorded under the same\n * namespace via {@link recordToolCallNamespace}.\n *\n * Without this correlation, tool messages render with empty\n * `tool_call_id` and downstream UIs can't pair them with the\n * originating tool call.\n *\n * # Store-write batching\n *\n * Every {@link handleMessage} / {@link applyValues} call updates the\n * in-projection bookkeeping (assembler state, id index, role cache)\n * synchronously, then stages the new `messages` / `values` into a\n * pending buffer and schedules a `setTimeout(0)` flush. A single\n * coalesced `store.setState` runs at the next macrotask boundary.\n *\n * The motivation is the long-replay freeze: a thread with hundreds\n * of messages replays through the `messages` channel on refresh or\n * mid-run join. Those events drain through the controller's\n * `for await` pump as a long microtask chain. Per-event\n * `store.setState` notifies `useSyncExternalStore` per event, and\n * after enough notifications React's `nestedUpdateCount` guard trips\n * with \"Maximum update depth exceeded\", permanently freezing the UI\n * on the first few messages. Coalescing to one notification per\n * macrotask lets React's scheduler commit between flushes.\n *\n * # Lifecycle\n *\n * - `handleMessage(event)` — apply a `messages` event delta.\n * - `applyValues(values, msgs)` — merge a `values` snapshot.\n * - `recordToolCallNamespace(ns, id)` — capture `namespace → tool_call_id`\n * so subsequent tool message starts can recover the id.\n * - `reset()` — clear all state on thread rebind.\n */\nimport type {\n MessagesEvent,\n MessageRole,\n MessageStartData,\n} from \"@langchain/protocol\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { MessageAssembler } from \"../client/stream/messages.js\";\nimport {\n assembledMessageToBaseMessage,\n type ExtendedMessageRole,\n} from \"./assembled-to-message.js\";\nimport type { StreamStore } from \"./store.js\";\nimport type { RootSnapshot } from \"./types.js\";\nimport { namespaceKey } from \"./namespace.js\";\nimport {\n buildMessageIndex,\n messagesEqual,\n reconcileMessagesFromValues,\n shouldPreferValuesMessageForToolCalls,\n} from \"./message-reconciliation.js\";\n\n/**\n * Root-namespace message projection. Owns the merge between the\n * `messages` (streamed deltas) and `values` (authoritative\n * snapshots) channels for the root namespace.\n *\n * @typeParam StateType - Root state shape; the messages array is read\n * from `values[messagesKey]`.\n * @typeParam InterruptType - Shape of root protocol interrupts (forwarded\n * into `RootSnapshot` updates).\n */\nexport class RootMessageProjection<\n StateType extends object,\n InterruptType = unknown,\n> {\n /**\n * Key inside `values` that holds the message array. Defaults to\n * `\"messages\"` in the controller; configurable for state graphs\n * that surface messages under a different slot.\n */\n readonly #messagesKey: string;\n\n /** Root snapshot store written to on every merge. */\n readonly #store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n\n /**\n * Stateful chunk assembler for in-flight messages. Reset (via a\n * fresh instance) on every {@link reset} so a new thread starts\n * with no half-built messages from the previous one.\n */\n #assembler = new MessageAssembler();\n\n /**\n * `messageId → role/toolCallId` captured from `message-start` events.\n * The assembler's intermediate output drops these fields, so we cache\n * them at start-time and reapply when projecting to a `BaseMessage`.\n */\n readonly #roles = new Map<\n string,\n { role: ExtendedMessageRole; toolCallId?: string }\n >();\n\n /**\n * `messageId → position in #store.messages` for fast in-place\n * updates as deltas arrive. Rebuilt on every full reconciliation\n * driven by a `values` event.\n */\n readonly #indexById = new Map<string, number>();\n\n /**\n * Ids observed in the most recent `values.messages` snapshot.\n * Reconciliation uses this to detect server-side removals: a\n * previously-seen id missing from the next snapshot means it was\n * removed by the server (and should drop from the projection).\n */\n #valuesMessageIds = new Set<string>();\n\n /**\n * `namespaceKey → tool_call_id` captured from root `tool-started`\n * events. Used as a fallback when a tool-role `message-start` is\n * missing its `tool_call_id` field.\n */\n readonly #toolCallIdByNamespace = new Map<string, string>();\n\n /**\n * Coalescing buffer for store writes. {@link handleMessage} and\n * {@link applyValues} stage their computed `messages` / `values`\n * here instead of calling `store.setState` per event. A single\n * `setTimeout(0)` flush commits them in one `setState`, so a\n * burst of SSE events draining as a microtask chain becomes one\n * store notification at the next macrotask boundary.\n *\n * `null` means \"no staged write\" — once a flush settles, the\n * slots are cleared so the next call starts from the latest\n * committed store snapshot.\n */\n #pendingMessages: BaseMessage[] | null = null;\n #pendingValues: StateType | null = null;\n #flushScheduled = false;\n\n /**\n * Highest checkpoint `step` whose `values` snapshot has been applied.\n * Seeded by {@link StreamController.hydrate} from `getState()` and\n * advanced by live `values` events. A snapshot arriving with a lower\n * step is an older checkpoint replayed by the content pump on\n * reconnect; it is reconciled in add-only mode so it cannot remove\n * the seeded message tail (the final assistant turn). `undefined`\n * until the first step-bearing snapshot, where the legacy\n * remove-on-absence behavior is preserved.\n */\n #maxStep: number | undefined = undefined;\n\n /**\n * Message ids seeded as complete-and-final from an idle thread's\n * `getState()` snapshot. An idle thread defers its root SSE pump, and\n * the first `submit()` brings it up — at which point the transport\n * replays the finished run from `seq=0`. Unlike the `values` channel\n * (guarded by {@link #maxStep}), `messages`-channel deltas carry no\n * step, so that replay would otherwise rebuild each already-complete\n * message from an empty `message-start` and re-stream the whole turn\n * token-by-token, clobbering the seeded tail (a visible \"messages\n * replay\" on the first submit). Deltas for a sealed id are dropped in\n * {@link handleMessage}. The seal is lifted once a checkpoint advances\n * strictly past {@link #sealStep} (see {@link applyValues}) or on\n * thread rebind ({@link reset}). New ids from the next run are never\n * sealed, so they stream normally.\n */\n readonly #sealedMessageIds = new Set<string>();\n\n /**\n * High-water {@link #maxStep} captured when {@link sealMessageIds} ran,\n * i.e. the seed checkpoint's step (or `undefined` when `getState()`\n * carried no `metadata.step`). It is the boundary between the replayed\n * idle history (steps `<= #sealStep`, emitted by the deferred pump's\n * `seq=0` replay) and the new run (steps `> #sealStep`); only a\n * checkpoint strictly past it lifts the seal. Without this boundary the\n * replayed old-run checkpoints — which themselves carry increasing\n * steps — would advance {@link #maxStep} and lift the seal mid-replay,\n * reopening the clobber. When the seed step is unknown the boundary\n * stays `undefined` and the seal holds until {@link reset}; the\n * `values` channel (which ignores the seal) still reconciles any\n * genuine change to a sealed id, only its streamed deltas are dropped.\n */\n #sealStep: number | undefined = undefined;\n\n /**\n * @param params.messagesKey - Key inside `values` that holds the\n * message array.\n * @param params.store - Root snapshot store to mutate.\n */\n constructor(params: {\n messagesKey: string;\n store: StreamStore<RootSnapshot<StateType, InterruptType>>;\n }) {\n this.#messagesKey = params.messagesKey;\n this.#store = params.store;\n }\n\n /**\n * Drop all per-thread state. Called by the controller on thread\n * rebind / dispose so a swap doesn't surface stale messages.\n */\n reset(): void {\n this.#assembler = new MessageAssembler();\n this.#roles.clear();\n this.#indexById.clear();\n this.#valuesMessageIds = new Set();\n this.#toolCallIdByNamespace.clear();\n // Drop any unflushed pending writes — they were computed against\n // the previous thread's baseline and committing them after a\n // rebind would bleed stale messages into the new thread.\n this.#pendingMessages = null;\n this.#pendingValues = null;\n this.#flushScheduled = false;\n this.#maxStep = undefined;\n this.#sealedMessageIds.clear();\n this.#sealStep = undefined;\n }\n\n /**\n * Seal message ids so the streamed `messages` channel cannot downgrade\n * them to partial re-streams. Called by {@link StreamController.hydrate}\n * after seeding an idle thread, whose deferred pump replays the finished\n * run from `seq=0` on the first submit.\n *\n * Captures the current {@link #maxStep} as the lift boundary\n * ({@link #sealStep}). The seal is applied immediately after the seed's\n * `getState()` snapshot is reconciled, so `#maxStep` here is the seed\n * step (or `undefined` when `getState()` carried no `metadata.step`).\n * The seal is lifted once a checkpoint advances strictly past that\n * boundary (see {@link applyValues}) or on thread rebind\n * ({@link reset}).\n *\n * @param ids - Complete message ids from the idle `getState()` seed.\n */\n sealMessageIds(ids: Iterable<string>): void {\n for (const id of ids) this.#sealedMessageIds.add(id);\n if (this.#sealStep == null) this.#sealStep = this.#maxStep;\n }\n\n /**\n * Record a `namespace → tool_call_id` mapping captured from a root\n * `tool-started` event.\n *\n * The companion tool-role `message-start` event may not carry a\n * `tool_call_id`, so we fall back to the most recent value recorded\n * here for the same namespace.\n *\n * @param namespace - Event namespace from the `tool-started` event.\n * @param toolCallId - Tool call id from the same event.\n */\n recordToolCallNamespace(\n namespace: readonly string[],\n toolCallId: string\n ): void {\n this.#toolCallIdByNamespace.set(namespaceKey(namespace), toolCallId);\n }\n\n /**\n * Apply a `messages` channel event to the projection.\n *\n * Captures role/tool metadata on `message-start`, feeds the chunk\n * to the assembler, projects the assembled output to a\n * {@link BaseMessage}, and either appends or in-place updates the\n * pending messages buffer based on whether the id was seen before.\n *\n * @param event - The `messages` channel event to consume.\n */\n handleMessage(event: MessagesEvent): void {\n const data = event.params.data;\n if (data.event === \"message-start\") {\n const startData = data as MessageStartData;\n const role = (startData.role ?? \"ai\") as MessageRole;\n const extendedRole =\n (startData as { role?: ExtendedMessageRole }).role ?? role;\n let toolCallId = (startData as { tool_call_id?: string }).tool_call_id;\n // Tool messages need a tool_call_id to render. Fall back through:\n // 1. legacy `<id>-tool-<call_id>` message id format\n // 2. namespace-recorded tool_call_id (from #recordToolCallNamespace)\n if (extendedRole === \"tool\" && toolCallId == null) {\n const messageId = startData.id;\n if (messageId != null) {\n const match = /-tool-(.+)$/.exec(messageId);\n if (match != null) toolCallId = match[1];\n }\n if (toolCallId == null) {\n toolCallId = this.#toolCallIdByNamespace.get(\n namespaceKey(event.params.namespace)\n );\n }\n }\n if (startData.id != null) {\n this.#roles.set(startData.id, {\n role: extendedRole,\n toolCallId,\n });\n }\n }\n\n const update = this.#assembler.consume(event);\n if (update == null) return;\n const id = update.message.id;\n if (id == null) return;\n // A sealed id belongs to a message seeded complete from an idle\n // thread's `getState()`; the deferred pump's `seq=0` replay would\n // otherwise rebuild it from an empty start and re-stream the whole\n // turn. Drop the replayed delta — the authoritative seed already\n // holds the final content (see {@link #sealedMessageIds}).\n if (this.#sealedMessageIds.has(id)) return;\n const captured = this.#roles.get(id) ?? { role: \"ai\" as const };\n const base = assembledMessageToBaseMessage(update.message, captured.role, {\n toolCallId: captured.toolCallId,\n });\n\n // Compute against the pending baseline if we have one (so an\n // earlier handleMessage in the same tick is the input to this\n // one), else against the latest committed store snapshot.\n // `#indexById` is the synchronous source of truth for \"where is\n // each id in the current messages list\" — every code path below\n // keeps it in sync before returning.\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const existingIdx = this.#indexById.get(id);\n let messages: BaseMessage[];\n if (existingIdx == null) {\n this.#indexById.set(id, baselineMessages.length);\n messages = [...baselineMessages, base];\n } else if (messagesEqual(baselineMessages[existingIdx], base)) {\n // Identical re-emission — skip the store write to keep\n // snapshot identity stable.\n return;\n } else {\n messages = baselineMessages.slice();\n messages[existingIdx] = base;\n }\n\n // Mirror the new messages list into `values[messagesKey]` so\n // direct `values` reads (used by some hooks and by the eventual\n // `values` reconciliation) stay in sync.\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const values = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n messages\n );\n this.#pendingMessages = messages;\n if (values !== baselineValues) this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Reconcile a full `values` snapshot into the projection.\n *\n * Delegates the merge to {@link reconcileMessagesFromValues}:\n * values stays authoritative for ordering and removals, while\n * streamed in-flight messages keep their content until the server\n * echoes them back. Empty messages just refresh the values blob.\n *\n * Rebuilds {@link #indexById} after the merge so subsequent delta\n * applications target the new positions.\n *\n * @param nextValues - Full values snapshot from the `values` event.\n * @param nextMessages - The messages array extracted from\n * `values[messagesKey]` and coerced to `BaseMessage` instances.\n * @param opts.step - Checkpoint superstep for this snapshot, when\n * known. A snapshot whose step is below the highest applied step is\n * treated as a stale reconnect replay and reconciled add-only.\n */\n applyValues(\n nextValues: StateType,\n nextMessages: BaseMessage[],\n opts?: { step?: number }\n ): void {\n const baselineSnapshot = this.#store.getSnapshot();\n const baselineMessages = this.#pendingMessages ?? baselineSnapshot.messages;\n const baselineValues = this.#pendingValues ?? baselineSnapshot.values;\n\n const step = opts?.step;\n // Stale only when we have both a prior high-water step and a lower\n // incoming step. A missing step preserves the legacy semantics.\n const addOnly =\n step != null && this.#maxStep != null && step < this.#maxStep;\n if (step != null && (this.#maxStep == null || step > this.#maxStep)) {\n this.#maxStep = step;\n }\n // Lift the replay seal only when a checkpoint advances strictly past\n // the step captured when the ids were sealed (the seed step). That\n // boundary separates the replayed idle history (steps <= #sealStep,\n // emitted by the deferred pump's seq=0 replay) from the new run\n // (steps > #sealStep), so crossing it means seeded ids may now take\n // genuine streamed updates. Replayed old-run checkpoints advance\n // #maxStep but never reach past #sealStep, so they can't lift it. A\n // `null` boundary (the seed step was unknown) keeps the seal until\n // reset() — we can't tell replay from live, and the values channel\n // still reconciles a sealed id even while its streamed deltas drop.\n if (\n this.#sealedMessageIds.size > 0 &&\n step != null &&\n this.#sealStep != null &&\n step > this.#sealStep\n ) {\n this.#sealedMessageIds.clear();\n }\n\n if (nextMessages.length === 0) {\n if (\n stateValuesShallowEqual(baselineValues, nextValues, this.#messagesKey)\n ) {\n return;\n }\n // Mirror the current `messages` list back into the values slot\n // so the staged snapshot stays consistent with the (separately\n // tracked) messages array.\n this.#pendingValues = syncMessagesIntoValues(\n nextValues,\n this.#messagesKey,\n baselineMessages\n );\n this.#scheduleFlush();\n return;\n }\n\n const reconciliation = reconcileMessagesFromValues({\n valueMessages: nextMessages,\n currentMessages: baselineMessages,\n currentIndexById: this.#indexById,\n previousValueMessageIds: this.#valuesMessageIds,\n preferValuesMessage: shouldPreferValuesMessageForToolCalls,\n addOnly,\n });\n // A stale replay snapshot must not shrink the authoritative id set:\n // keep the (larger) seeded set so a genuinely-newer removal is still\n // detected once the timeline advances past the seed.\n if (!addOnly) this.#valuesMessageIds = reconciliation.valueMessageIds;\n const messages = reconciliation.messages as BaseMessage[];\n const values = {\n ...(nextValues as Record<string, unknown>),\n [this.#messagesKey]: messages,\n } as StateType;\n if (\n messages === baselineMessages &&\n stateValuesShallowEqual(baselineValues, values, this.#messagesKey)\n ) {\n return;\n }\n\n // Reconciliation may reorder, drop, or substitute messages, so\n // rebuild the id → index map to match the new array.\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(messages)) {\n this.#indexById.set(id, idx);\n }\n this.#pendingMessages = messages;\n this.#pendingValues = values;\n this.#scheduleFlush();\n }\n\n /**\n * Append messages applied optimistically by a local `submit()`,\n * keyed by id so the eventual server echo reconciles cleanly.\n *\n * Unlike {@link applyValues}, the supplied messages are *not* treated\n * as an authoritative ordered snapshot: they are appended to the end\n * of the current projection (or replaced in place when the id already\n * exists), preserving prior history ordering. When the server later\n * emits a `values` snapshot containing the same ids,\n * {@link applyValues} → {@link reconcileMessagesFromValues} takes over\n * (server ordering wins, the echoed message replaces the optimistic\n * one).\n *\n * Non-message input keys are shallow-merged into `values` via\n * `extraValues`; they are dropped/overwritten automatically by the\n * first server `values` event (which rebuilds `values` from the\n * server snapshot), or rolled back via {@link restoreValueKeys} when\n * the run fails before any echo.\n *\n * @param messages - Optimistic messages (already coerced to\n * `BaseMessage` instances, each carrying a stable id).\n * @param extraValues - Non-message input keys to shallow-merge into\n * `values`.\n * @param options - When `sync` is true the staged write is\n * committed to the store *synchronously* instead of being coalesced\n * onto the next macrotask. Used for discrete, user-initiated\n * optimistic writes (`submit()` / `respond()` / `respondAll()`):\n * committing in the same tick as the triggering event lets the\n * framework render the optimistic message in the *same* commit as\n * any local state the caller flipped alongside it (e.g. a HITL form\n * hiding its inputs), so the pushed card never blinks out for the\n * one-macrotask window before the flush lands. Streaming writes\n * (`handleMessage` / `applyValues`) keep the default macrotask\n * coalescing, which is what tames high-frequency SSE bursts.\n */\n appendOptimistic(\n messages: BaseMessage[],\n extraValues?: Record<string, unknown>,\n options?: { sync?: boolean }\n ): void {\n let working = this.#pendingMessages ?? this.#store.getSnapshot().messages;\n let mutated = false;\n for (const message of messages) {\n const id = message.id;\n if (id == null) continue;\n const existingIdx = this.#indexById.get(id);\n if (existingIdx == null) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n this.#indexById.set(id, working.length);\n working.push(message);\n } else if (!messagesEqual(working[existingIdx], message)) {\n if (!mutated) {\n working = working.slice();\n mutated = true;\n }\n working[existingIdx] = message;\n }\n }\n\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n let values = baselineValues;\n if (extraValues != null && Object.keys(extraValues).length > 0) {\n values = { ...(baselineValues as object), ...extraValues } as StateType;\n }\n values = syncMessagesIntoValues(values, this.#messagesKey, working);\n if (!mutated && values === baselineValues) return;\n this.#pendingMessages = working;\n if (values !== baselineValues) this.#pendingValues = values;\n if (options?.sync) {\n // Commit now so the optimistic message is visible in the same tick\n // as the user event that produced it (no one-macrotask blink). Any\n // flush already scheduled by a prior streaming write is absorbed\n // here; its pending timer fires later as a no-op.\n this.#flushPending();\n } else {\n this.#scheduleFlush();\n }\n }\n\n /**\n * Drop optimistic messages by id without disturbing the rest of the\n * projection. Used by {@link StreamController.hydrate} to remove\n * never-persisted optimistic messages (`pending` / `failed`) so a\n * reload converges to server truth.\n *\n * @param ids - Message ids to remove.\n */\n dropOptimisticMessages(ids: ReadonlySet<string>): void {\n if (ids.size === 0) return;\n const baselineMessages =\n this.#pendingMessages ?? this.#store.getSnapshot().messages;\n const next = baselineMessages.filter((m) => m.id == null || !ids.has(m.id));\n if (next.length === baselineMessages.length) return;\n this.#indexById.clear();\n for (const [id, idx] of buildMessageIndex(next)) {\n this.#indexById.set(id, idx);\n }\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n this.#pendingMessages = next;\n this.#pendingValues = syncMessagesIntoValues(\n baselineValues,\n this.#messagesKey,\n next\n );\n this.#scheduleFlush();\n }\n\n /**\n * Restore (or delete) `values` keys that were optimistically merged\n * by {@link appendOptimistic} but never echoed by the server — i.e.\n * roll back non-message optimistic state when the run fails before\n * any `values` event lands. Messages are left untouched (kept on\n * failure per the optimistic contract).\n *\n * @param restore - Per-key pre-submit snapshot: when `hadKey` is\n * false the key is deleted, otherwise it is reset to `prevValue`.\n */\n restoreValueKeys(\n restore: ReadonlyArray<{\n key: string;\n hadKey: boolean;\n prevValue: unknown;\n }>\n ): void {\n if (restore.length === 0) return;\n const baselineValues =\n this.#pendingValues ?? this.#store.getSnapshot().values;\n const next = { ...(baselineValues as Record<string, unknown>) };\n let changed = false;\n for (const { key, hadKey, prevValue } of restore) {\n if (key === this.#messagesKey) continue;\n if (hadKey) {\n if (!Object.is(next[key], prevValue)) {\n next[key] = prevValue;\n changed = true;\n }\n } else if (Object.prototype.hasOwnProperty.call(next, key)) {\n delete next[key];\n changed = true;\n }\n }\n if (!changed) return;\n this.#pendingValues = next as StateType;\n this.#scheduleFlush();\n }\n\n /**\n * Schedule a coalesced flush on the next macrotask. Idempotent\n * within a tick — multiple `handleMessage` / `applyValues` calls\n * before the flush fires collapse into one store write.\n *\n * `setTimeout(0)` is a macrotask: it runs after the current\n * microtask chain drains, so a burst of SSE events processed by\n * the controller's `for await` pump becomes one `store.setState`\n * (and therefore one `useSyncExternalStore` notification).\n */\n #scheduleFlush = (): void => {\n if (this.#flushScheduled) return;\n this.#flushScheduled = true;\n setTimeout(this.#flushPending, 0);\n };\n\n /**\n * Drain `#pendingMessages` / `#pendingValues` to the store in a\n * single `setState` call.\n */\n #flushPending = (): void => {\n this.#flushScheduled = false;\n const messages = this.#pendingMessages;\n const values = this.#pendingValues;\n this.#pendingMessages = null;\n this.#pendingValues = null;\n if (messages == null && values == null) return;\n this.#store.setState((s) => {\n // Other rootStore mutators (controller-driven `isLoading`,\n // `interrupts`, `toolCalls`, etc.) do not touch `s.messages`\n // / `s.values`, so a last-write-wins commit on those two\n // fields is safe.\n if (messages == null) {\n return values == null ? s : { ...s, values };\n }\n if (values == null) return { ...s, messages };\n return { ...s, messages, values };\n });\n };\n}\n\n/**\n * Mirror a freshly-updated message list into `values[messagesKey]`.\n *\n * Returns the same `values` reference when the list is already\n * equal-by-content so the caller can keep the existing snapshot\n * identity (and avoid spurious `setSnapshot` notifications).\n */\nfunction syncMessagesIntoValues<StateType extends object>(\n values: StateType,\n messagesKey: string,\n messages: BaseMessage[]\n): StateType {\n const record = values as Record<string, unknown>;\n const current = record[messagesKey];\n if (Array.isArray(current) && messagesEqualList(current, messages)) {\n return values;\n }\n return {\n ...record,\n [messagesKey]: messages,\n } as StateType;\n}\n\n/**\n * True when two `BaseMessage` arrays carry the same per-message\n * content (using {@link messagesEqual}).\n */\nfunction messagesEqualList(\n previous: readonly BaseMessage[],\n next: readonly BaseMessage[]\n): boolean {\n if (previous === next) return true;\n if (previous.length !== next.length) return false;\n for (let i = 0; i < previous.length; i += 1) {\n if (!messagesEqual(previous[i], next[i])) return false;\n }\n return true;\n}\n\n/**\n * Shallow-equal for `values` objects, *ignoring* the messages slot.\n *\n * The messages array is compared separately by the caller (via\n * {@link messagesEqualList}) because both arrays contain class\n * instances whose JSON representation is not stable across reads.\n */\nfunction stateValuesShallowEqual(\n previous: object,\n next: object,\n messagesKey: string\n): boolean {\n if (previous === next) return true;\n const previousRecord = previous as Record<string, unknown>;\n const nextRecord = next as Record<string, unknown>;\n const previousKeys = Object.keys(previousRecord);\n const nextKeys = Object.keys(nextRecord);\n if (previousKeys.length !== nextKeys.length) return false;\n for (const key of previousKeys) {\n if (!Object.prototype.hasOwnProperty.call(nextRecord, key)) return false;\n const previousValue = previousRecord[key];\n const nextValue = nextRecord[key];\n if (\n key === messagesKey &&\n Array.isArray(previousValue) &&\n Array.isArray(nextValue)\n ) {\n continue;\n }\n if (!Object.is(previousValue, nextValue)) return false;\n }\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoGA,IAAa,wBAAb,MAGE;;;;;;CAMA;;CAGA;;;;;;CAOA,aAAa,IAAI,kBAAkB;;;;;;CAOnC,yBAAkB,IAAI,KAGnB;;;;;;CAOH,6BAAsB,IAAI,KAAqB;;;;;;;CAQ/C,oCAAoB,IAAI,KAAa;;;;;;CAOrC,yCAAkC,IAAI,KAAqB;;;;;;;;;;;;;CAc3D,mBAAyC;CACzC,iBAAmC;CACnC,kBAAkB;;;;;;;;;;;CAYlB,WAA+B,KAAA;;;;;;;;;;;;;;;;CAiB/B,oCAA6B,IAAI,KAAa;;;;;;;;;;;;;;;CAgB9C,YAAgC,KAAA;;;;;;CAOhC,YAAY,QAGT;AACD,QAAA,cAAoB,OAAO;AAC3B,QAAA,QAAc,OAAO;;;;;;CAOvB,QAAc;AACZ,QAAA,YAAkB,IAAI,kBAAkB;AACxC,QAAA,MAAY,OAAO;AACnB,QAAA,UAAgB,OAAO;AACvB,QAAA,mCAAyB,IAAI,KAAK;AAClC,QAAA,sBAA4B,OAAO;AAInC,QAAA,kBAAwB;AACxB,QAAA,gBAAsB;AACtB,QAAA,iBAAuB;A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