@illuma-ai/agents 1.1.25 → 1.3.0

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.
Files changed (272) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +20 -3
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/spawnPath.cjs +104 -0
  4. package/dist/cjs/common/spawnPath.cjs.map +1 -0
  5. package/dist/cjs/graphs/Graph.cjs +87 -31
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/graphs/HandoffRegistry.cjs +143 -0
  8. package/dist/cjs/graphs/HandoffRegistry.cjs.map +1 -0
  9. package/dist/cjs/graphs/MultiAgentGraph.cjs +587 -184
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  11. package/dist/cjs/graphs/phases/flushLoop.cjs +214 -0
  12. package/dist/cjs/graphs/phases/flushLoop.cjs.map +1 -0
  13. package/dist/cjs/graphs/phases/memoryFlushPhase.cjs +102 -0
  14. package/dist/cjs/graphs/phases/memoryFlushPhase.cjs.map +1 -0
  15. package/dist/cjs/llm/bedrock/index.cjs +4 -3
  16. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  17. package/dist/cjs/main.cjs +115 -0
  18. package/dist/cjs/main.cjs.map +1 -1
  19. package/dist/cjs/memory/citations.cjs +69 -0
  20. package/dist/cjs/memory/citations.cjs.map +1 -0
  21. package/dist/cjs/memory/compositeBackend.cjs +60 -0
  22. package/dist/cjs/memory/compositeBackend.cjs.map +1 -0
  23. package/dist/cjs/memory/constants.cjs +232 -0
  24. package/dist/cjs/memory/constants.cjs.map +1 -0
  25. package/dist/cjs/memory/embeddings.cjs +151 -0
  26. package/dist/cjs/memory/embeddings.cjs.map +1 -0
  27. package/dist/cjs/memory/factory.cjs +95 -0
  28. package/dist/cjs/memory/factory.cjs.map +1 -0
  29. package/dist/cjs/memory/migrate.cjs +81 -0
  30. package/dist/cjs/memory/migrate.cjs.map +1 -0
  31. package/dist/cjs/memory/mmr.cjs +138 -0
  32. package/dist/cjs/memory/mmr.cjs.map +1 -0
  33. package/dist/cjs/memory/paths.cjs +217 -0
  34. package/dist/cjs/memory/paths.cjs.map +1 -0
  35. package/dist/cjs/memory/pgvectorStore.cjs +225 -0
  36. package/dist/cjs/memory/pgvectorStore.cjs.map +1 -0
  37. package/dist/cjs/memory/recallTracking.cjs +98 -0
  38. package/dist/cjs/memory/recallTracking.cjs.map +1 -0
  39. package/dist/cjs/memory/schema.sql +51 -0
  40. package/dist/cjs/memory/temporalDecay.cjs +118 -0
  41. package/dist/cjs/memory/temporalDecay.cjs.map +1 -0
  42. package/dist/cjs/nodes/ApprovalGateNode.cjs +1 -1
  43. package/dist/cjs/nodes/ApprovalGateNode.cjs.map +1 -1
  44. package/dist/cjs/prompts/memoryFlushPrompt.cjs +49 -0
  45. package/dist/cjs/prompts/memoryFlushPrompt.cjs.map +1 -0
  46. package/dist/cjs/run.cjs +16 -3
  47. package/dist/cjs/run.cjs.map +1 -1
  48. package/dist/cjs/stream.cjs +4 -4
  49. package/dist/cjs/stream.cjs.map +1 -1
  50. package/dist/cjs/tools/AskUser.cjs +6 -1
  51. package/dist/cjs/tools/AskUser.cjs.map +1 -1
  52. package/dist/cjs/tools/BrowserTools.cjs +1 -1
  53. package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
  54. package/dist/cjs/tools/ToolNode.cjs +127 -10
  55. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  56. package/dist/cjs/tools/approval/constants.cjs +2 -2
  57. package/dist/cjs/tools/approval/constants.cjs.map +1 -1
  58. package/dist/cjs/tools/memory/index.cjs +58 -0
  59. package/dist/cjs/tools/memory/index.cjs.map +1 -0
  60. package/dist/cjs/tools/memory/memoryAppendTool.cjs +69 -0
  61. package/dist/cjs/tools/memory/memoryAppendTool.cjs.map +1 -0
  62. package/dist/cjs/tools/memory/memoryGetTool.cjs +49 -0
  63. package/dist/cjs/tools/memory/memoryGetTool.cjs.map +1 -0
  64. package/dist/cjs/tools/memory/memorySearchTool.cjs +65 -0
  65. package/dist/cjs/tools/memory/memorySearchTool.cjs.map +1 -0
  66. package/dist/cjs/tools/memory/shared.cjs +106 -0
  67. package/dist/cjs/tools/memory/shared.cjs.map +1 -0
  68. package/dist/cjs/types/graph.cjs.map +1 -1
  69. package/dist/cjs/utils/childAgentContext.cjs +242 -0
  70. package/dist/cjs/utils/childAgentContext.cjs.map +1 -0
  71. package/dist/cjs/utils/events.cjs +36 -4
  72. package/dist/cjs/utils/events.cjs.map +1 -1
  73. package/dist/cjs/utils/finishReasons.cjs +44 -0
  74. package/dist/cjs/utils/finishReasons.cjs.map +1 -0
  75. package/dist/cjs/utils/llm.cjs.map +1 -1
  76. package/dist/cjs/utils/logging.cjs +34 -0
  77. package/dist/cjs/utils/logging.cjs.map +1 -0
  78. package/dist/cjs/utils/toolCallNormalization.cjs +250 -0
  79. package/dist/cjs/utils/toolCallNormalization.cjs.map +1 -0
  80. package/dist/esm/agents/AgentContext.mjs +20 -3
  81. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  82. package/dist/esm/common/spawnPath.mjs +95 -0
  83. package/dist/esm/common/spawnPath.mjs.map +1 -0
  84. package/dist/esm/graphs/Graph.mjs +87 -31
  85. package/dist/esm/graphs/Graph.mjs.map +1 -1
  86. package/dist/esm/graphs/HandoffRegistry.mjs +141 -0
  87. package/dist/esm/graphs/HandoffRegistry.mjs.map +1 -0
  88. package/dist/esm/graphs/MultiAgentGraph.mjs +587 -184
  89. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  90. package/dist/esm/graphs/phases/flushLoop.mjs +209 -0
  91. package/dist/esm/graphs/phases/flushLoop.mjs.map +1 -0
  92. package/dist/esm/graphs/phases/memoryFlushPhase.mjs +99 -0
  93. package/dist/esm/graphs/phases/memoryFlushPhase.mjs.map +1 -0
  94. package/dist/esm/llm/bedrock/index.mjs +4 -3
  95. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  96. package/dist/esm/main.mjs +21 -0
  97. package/dist/esm/main.mjs.map +1 -1
  98. package/dist/esm/memory/citations.mjs +64 -0
  99. package/dist/esm/memory/citations.mjs.map +1 -0
  100. package/dist/esm/memory/compositeBackend.mjs +58 -0
  101. package/dist/esm/memory/compositeBackend.mjs.map +1 -0
  102. package/dist/esm/memory/constants.mjs +198 -0
  103. package/dist/esm/memory/constants.mjs.map +1 -0
  104. package/dist/esm/memory/embeddings.mjs +148 -0
  105. package/dist/esm/memory/embeddings.mjs.map +1 -0
  106. package/dist/esm/memory/factory.mjs +93 -0
  107. package/dist/esm/memory/factory.mjs.map +1 -0
  108. package/dist/esm/memory/migrate.mjs +78 -0
  109. package/dist/esm/memory/migrate.mjs.map +1 -0
  110. package/dist/esm/memory/mmr.mjs +130 -0
  111. package/dist/esm/memory/mmr.mjs.map +1 -0
  112. package/dist/esm/memory/paths.mjs +207 -0
  113. package/dist/esm/memory/paths.mjs.map +1 -0
  114. package/dist/esm/memory/pgvectorStore.mjs +223 -0
  115. package/dist/esm/memory/pgvectorStore.mjs.map +1 -0
  116. package/dist/esm/memory/recallTracking.mjs +94 -0
  117. package/dist/esm/memory/recallTracking.mjs.map +1 -0
  118. package/dist/esm/memory/schema.sql +51 -0
  119. package/dist/esm/memory/temporalDecay.mjs +110 -0
  120. package/dist/esm/memory/temporalDecay.mjs.map +1 -0
  121. package/dist/esm/nodes/ApprovalGateNode.mjs +1 -1
  122. package/dist/esm/nodes/ApprovalGateNode.mjs.map +1 -1
  123. package/dist/esm/prompts/memoryFlushPrompt.mjs +44 -0
  124. package/dist/esm/prompts/memoryFlushPrompt.mjs.map +1 -0
  125. package/dist/esm/run.mjs +16 -3
  126. package/dist/esm/run.mjs.map +1 -1
  127. package/dist/esm/stream.mjs +4 -4
  128. package/dist/esm/stream.mjs.map +1 -1
  129. package/dist/esm/tools/AskUser.mjs +6 -1
  130. package/dist/esm/tools/AskUser.mjs.map +1 -1
  131. package/dist/esm/tools/BrowserTools.mjs +1 -1
  132. package/dist/esm/tools/BrowserTools.mjs.map +1 -1
  133. package/dist/esm/tools/ToolNode.mjs +128 -11
  134. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  135. package/dist/esm/tools/approval/constants.mjs +2 -2
  136. package/dist/esm/tools/approval/constants.mjs.map +1 -1
  137. package/dist/esm/tools/memory/index.mjs +46 -0
  138. package/dist/esm/tools/memory/index.mjs.map +1 -0
  139. package/dist/esm/tools/memory/memoryAppendTool.mjs +67 -0
  140. package/dist/esm/tools/memory/memoryAppendTool.mjs.map +1 -0
  141. package/dist/esm/tools/memory/memoryGetTool.mjs +47 -0
  142. package/dist/esm/tools/memory/memoryGetTool.mjs.map +1 -0
  143. package/dist/esm/tools/memory/memorySearchTool.mjs +63 -0
  144. package/dist/esm/tools/memory/memorySearchTool.mjs.map +1 -0
  145. package/dist/esm/tools/memory/shared.mjs +98 -0
  146. package/dist/esm/tools/memory/shared.mjs.map +1 -0
  147. package/dist/esm/types/graph.mjs.map +1 -1
  148. package/dist/esm/utils/childAgentContext.mjs +237 -0
  149. package/dist/esm/utils/childAgentContext.mjs.map +1 -0
  150. package/dist/esm/utils/events.mjs +36 -5
  151. package/dist/esm/utils/events.mjs.map +1 -1
  152. package/dist/esm/utils/finishReasons.mjs +41 -0
  153. package/dist/esm/utils/finishReasons.mjs.map +1 -0
  154. package/dist/esm/utils/llm.mjs.map +1 -1
  155. package/dist/esm/utils/logging.mjs +31 -0
  156. package/dist/esm/utils/logging.mjs.map +1 -0
  157. package/dist/esm/utils/toolCallNormalization.mjs +247 -0
  158. package/dist/esm/utils/toolCallNormalization.mjs.map +1 -0
  159. package/dist/types/common/index.d.ts +1 -0
  160. package/dist/types/common/spawnPath.d.ts +59 -0
  161. package/dist/types/graphs/HandoffRegistry.d.ts +97 -0
  162. package/dist/types/graphs/MultiAgentGraph.d.ts +58 -18
  163. package/dist/types/graphs/index.d.ts +1 -0
  164. package/dist/types/graphs/phases/flushLoop.d.ts +106 -0
  165. package/dist/types/graphs/phases/memoryFlushPhase.d.ts +100 -0
  166. package/dist/types/index.d.ts +7 -0
  167. package/dist/types/memory/__tests__/mockBackend.d.ts +40 -0
  168. package/dist/types/memory/citations.d.ts +39 -0
  169. package/dist/types/memory/compositeBackend.d.ts +30 -0
  170. package/dist/types/memory/constants.d.ts +121 -0
  171. package/dist/types/memory/embeddings.d.ts +15 -0
  172. package/dist/types/memory/factory.d.ts +23 -0
  173. package/dist/types/memory/index.d.ts +21 -0
  174. package/dist/types/memory/migrate.d.ts +14 -0
  175. package/dist/types/memory/mmr.d.ts +50 -0
  176. package/dist/types/memory/paths.d.ts +107 -0
  177. package/dist/types/memory/pgvectorStore.d.ts +56 -0
  178. package/dist/types/memory/recallTracking.d.ts +30 -0
  179. package/dist/types/memory/temporalDecay.d.ts +53 -0
  180. package/dist/types/memory/types.d.ts +182 -0
  181. package/dist/types/prompts/memoryFlushPrompt.d.ts +54 -0
  182. package/dist/types/run.d.ts +1 -0
  183. package/dist/types/tools/AskUser.d.ts +1 -1
  184. package/dist/types/tools/BrowserTools.d.ts +2 -2
  185. package/dist/types/tools/approval/constants.d.ts +2 -2
  186. package/dist/types/tools/memory/index.d.ts +39 -0
  187. package/dist/types/tools/memory/memoryAppendTool.d.ts +27 -0
  188. package/dist/types/tools/memory/memoryGetTool.d.ts +22 -0
  189. package/dist/types/tools/memory/memorySearchTool.d.ts +22 -0
  190. package/dist/types/tools/memory/shared.d.ts +106 -0
  191. package/dist/types/types/graph.d.ts +16 -3
  192. package/dist/types/utils/childAgentContext.d.ts +99 -0
  193. package/dist/types/utils/events.d.ts +21 -0
  194. package/dist/types/utils/finishReasons.d.ts +32 -0
  195. package/dist/types/utils/logging.d.ts +2 -0
  196. package/dist/types/utils/toolCallNormalization.d.ts +44 -0
  197. package/package.json +6 -4
  198. package/src/agents/AgentContext.ts +26 -3
  199. package/src/common/__tests__/enum.test.ts +4 -2
  200. package/src/common/__tests__/spawnPath.test.ts +110 -0
  201. package/src/common/index.ts +1 -0
  202. package/src/common/spawnPath.ts +101 -0
  203. package/src/graphs/Graph.ts +94 -43
  204. package/src/graphs/HandoffRegistry.ts +199 -0
  205. package/src/graphs/MultiAgentGraph.ts +694 -226
  206. package/src/graphs/__tests__/HandoffRegistry.test.ts +410 -0
  207. package/src/graphs/__tests__/multi-agent-delegate.test.ts +61 -16
  208. package/src/graphs/__tests__/multi-agent-edges.test.ts +4 -2
  209. package/src/graphs/__tests__/multi-agent-nested-subgraph.test.ts +221 -0
  210. package/src/graphs/__tests__/structured-output.integration.test.ts +212 -118
  211. package/src/graphs/contextManagement.e2e.test.ts +1 -1
  212. package/src/graphs/index.ts +1 -0
  213. package/src/graphs/phases/__tests__/flushLoop.test.ts +264 -0
  214. package/src/graphs/phases/__tests__/memoryFlushPhase.test.ts +37 -0
  215. package/src/graphs/phases/__tests__/runMemoryFlush.test.ts +150 -0
  216. package/src/graphs/phases/flushLoop.ts +303 -0
  217. package/src/graphs/phases/memoryFlushPhase.ts +209 -0
  218. package/src/index.ts +30 -1
  219. package/src/llm/bedrock/index.ts +4 -5
  220. package/src/memory/__tests__/citations.test.ts +61 -0
  221. package/src/memory/__tests__/compositeBackend.test.ts +79 -0
  222. package/src/memory/__tests__/isolation.test.ts +206 -0
  223. package/src/memory/__tests__/mmr.test.ts +148 -0
  224. package/src/memory/__tests__/mockBackend.ts +161 -0
  225. package/src/memory/__tests__/paths.test.ts +168 -0
  226. package/src/memory/__tests__/recallTracking.test.ts +96 -0
  227. package/src/memory/__tests__/temporalDecay.test.ts +151 -0
  228. package/src/memory/citations.ts +80 -0
  229. package/src/memory/compositeBackend.ts +99 -0
  230. package/src/memory/constants.ts +229 -0
  231. package/src/memory/embeddings.ts +188 -0
  232. package/src/memory/factory.ts +111 -0
  233. package/src/memory/index.ts +46 -0
  234. package/src/memory/migrate.ts +116 -0
  235. package/src/memory/mmr.ts +161 -0
  236. package/src/memory/paths.ts +258 -0
  237. package/src/memory/pgvectorStore.ts +324 -0
  238. package/src/memory/recallTracking.ts +127 -0
  239. package/src/memory/schema.sql +51 -0
  240. package/src/memory/temporalDecay.ts +134 -0
  241. package/src/memory/types.ts +185 -0
  242. package/src/nodes/ApprovalGateNode.ts +4 -10
  243. package/src/nodes/__tests__/ApprovalGateNode.test.ts +11 -20
  244. package/src/prompts/memoryFlushPrompt.ts +78 -0
  245. package/src/run.ts +17 -6
  246. package/src/scripts/test-bedrock-handoff-autonomous.ts +56 -20
  247. package/src/specs/agent-handoffs-bedrock.integration.test.ts +8 -5
  248. package/src/specs/agent-handoffs.test.ts +8 -2
  249. package/src/stream.ts +4 -6
  250. package/src/tools/AskUser.ts +7 -2
  251. package/src/tools/BrowserTools.ts +3 -5
  252. package/src/tools/ToolNode.ts +150 -13
  253. package/src/tools/__tests__/ToolApproval.test.ts +22 -9
  254. package/src/tools/approval/__tests__/constants.test.ts +4 -4
  255. package/src/tools/approval/constants.ts +2 -2
  256. package/src/tools/memory/__tests__/memoryTools.test.ts +205 -0
  257. package/src/tools/memory/index.ts +96 -0
  258. package/src/tools/memory/memoryAppendTool.ts +101 -0
  259. package/src/tools/memory/memoryGetTool.ts +53 -0
  260. package/src/tools/memory/memorySearchTool.ts +80 -0
  261. package/src/tools/memory/shared.ts +169 -0
  262. package/src/tools/search/search.test.ts +6 -1
  263. package/src/types/graph.ts +16 -3
  264. package/src/utils/__tests__/childAgentContext.test.ts +217 -0
  265. package/src/utils/__tests__/finishReasons.test.ts +55 -0
  266. package/src/utils/__tests__/toolCallNormalization.test.ts +181 -0
  267. package/src/utils/childAgentContext.ts +259 -0
  268. package/src/utils/events.ts +37 -4
  269. package/src/utils/finishReasons.ts +40 -0
  270. package/src/utils/llm.ts +0 -1
  271. package/src/utils/logging.ts +45 -8
  272. package/src/utils/toolCallNormalization.ts +271 -0
@@ -0,0 +1,51 @@
1
+ -- Autonomous memory — Postgres schema (v2: layered tier scoping).
2
+ --
3
+ -- Two-tier canonical-document model:
4
+ --
5
+ -- memory/agent/* → shared across every user of the agent (user_id = NULL)
6
+ -- memory/user/* → private to a specific caller (user_id = <callerId>)
7
+ --
8
+ -- Uniqueness key is `(agent_id, user_id, path)` with NULLS NOT DISTINCT
9
+ -- so two NULL user_id rows on the same path collide (exactly-one
10
+ -- agent-tier row per path) while per-user rows on the same path coexist
11
+ -- (one row per user for user-tier paths).
12
+ --
13
+ -- The read path filters with `(user_id IS NULL OR user_id = $caller)`,
14
+ -- so callers see agent-tier rows + only their own user-tier rows —
15
+ -- never another user's private memory. Enforcement is in SQL, not just
16
+ -- the UI.
17
+ --
18
+ -- NULLS NOT DISTINCT requires PostgreSQL 15+. Host's production
19
+ -- pgvector image is PG16, so this is safe. The migration in
20
+ -- `migrate.ts` will drop the legacy `(agent_id, path)` constraint
21
+ -- before creating the new one if it exists on an older database.
22
+
23
+ CREATE EXTENSION IF NOT EXISTS vector;
24
+
25
+ CREATE TABLE IF NOT EXISTS agent_memories (
26
+ id BIGSERIAL PRIMARY KEY,
27
+ agent_id TEXT NOT NULL,
28
+ user_id TEXT, -- NULL = agent-tier, shared
29
+ path TEXT NOT NULL,
30
+ content TEXT NOT NULL,
31
+ embedding VECTOR(1024),
32
+ tsv TSVECTOR GENERATED ALWAYS AS (to_tsvector('english', content)) STORED,
33
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
34
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
35
+ last_user_id TEXT, -- latest session to append
36
+ CONSTRAINT agent_memories_agent_user_path_uq
37
+ UNIQUE NULLS NOT DISTINCT (agent_id, user_id, path)
38
+ );
39
+
40
+ -- Primary lookup index for list/search/generate-prompt — matches the
41
+ -- exact filter shape of every read query.
42
+ CREATE INDEX IF NOT EXISTS agent_memories_scope_idx
43
+ ON agent_memories (agent_id, user_id, path);
44
+
45
+ -- Vector ANN index — untouched by the scoping change.
46
+ CREATE INDEX IF NOT EXISTS agent_memories_vector_idx
47
+ ON agent_memories USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
48
+
49
+ -- Full-text GIN index — untouched.
50
+ CREATE INDEX IF NOT EXISTS agent_memories_tsv_idx
51
+ ON agent_memories USING GIN (tsv);
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Temporal decay — Phase 2.
5
+ *
6
+ * Ported from upstream `extensions/memory-core/src/memory/temporal-decay.ts`.
7
+ * Ages dated memory files (`memory/YYYY-MM-DD.md`) using exponential decay
8
+ * `multiplier = exp(-ln(2) / halfLifeDays * ageInDays)`. At half-life, the
9
+ * score is exactly halved.
10
+ *
11
+ * Evergreen files (MEMORY.md, memory/topics.md, any non-dated file inside
12
+ * memory/) do NOT decay — they represent durable knowledge and should stay
13
+ * hot regardless of age. This mirrors upstream's `isEvergreenMemoryPath`.
14
+ *
15
+ * Since our pgvector rows carry `createdAt`, we don't need filesystem stat
16
+ * fallback — the row timestamp is authoritative for any file without a
17
+ * date in the path.
18
+ */
19
+ const DEFAULT_TEMPORAL_DECAY_CONFIG = {
20
+ enabled: false,
21
+ halfLifeDays: 30,
22
+ };
23
+ const DAY_MS = 24 * 60 * 60 * 1000;
24
+ const DATED_MEMORY_PATH_RE = /(?:^|\/)memory\/(\d{4})-(\d{2})-(\d{2})\.md$/;
25
+ function toDecayLambda(halfLifeDays) {
26
+ if (!Number.isFinite(halfLifeDays) || halfLifeDays <= 0)
27
+ return 0;
28
+ return Math.LN2 / halfLifeDays;
29
+ }
30
+ function calculateTemporalDecayMultiplier(params) {
31
+ const lambda = toDecayLambda(params.halfLifeDays);
32
+ const age = Math.max(0, params.ageInDays);
33
+ if (lambda <= 0 || !Number.isFinite(age))
34
+ return 1;
35
+ return Math.exp(-lambda * age);
36
+ }
37
+ function applyTemporalDecayToScore(params) {
38
+ return params.score * calculateTemporalDecayMultiplier(params);
39
+ }
40
+ function normalizePath(p) {
41
+ return (p ?? '').replace(/\\/g, '/').replace(/^\.\//, '');
42
+ }
43
+ /** Parse a date out of `memory/YYYY-MM-DD.md` — returns null on non-match or invalid date. */
44
+ function parseMemoryDateFromPath(filePath) {
45
+ const m = DATED_MEMORY_PATH_RE.exec(normalizePath(filePath));
46
+ if (!m)
47
+ return null;
48
+ const y = Number(m[1]);
49
+ const mo = Number(m[2]);
50
+ const d = Number(m[3]);
51
+ if (!Number.isInteger(y) || !Number.isInteger(mo) || !Number.isInteger(d))
52
+ return null;
53
+ const ts = Date.UTC(y, mo - 1, d);
54
+ const parsed = new Date(ts);
55
+ if (parsed.getUTCFullYear() !== y ||
56
+ parsed.getUTCMonth() !== mo - 1 ||
57
+ parsed.getUTCDate() !== d) {
58
+ return null;
59
+ }
60
+ return parsed;
61
+ }
62
+ /**
63
+ * Evergreen = durable knowledge file that should not decay.
64
+ * - `MEMORY.md` / `memory.md` at root
65
+ * - anything inside `memory/` that is NOT a dated `YYYY-MM-DD.md` file
66
+ */
67
+ function isEvergreenMemoryPath(filePath) {
68
+ const n = normalizePath(filePath);
69
+ if (n === 'MEMORY.md' || n === 'memory.md')
70
+ return true;
71
+ if (!n.startsWith('memory/'))
72
+ return false;
73
+ return !DATED_MEMORY_PATH_RE.test(n);
74
+ }
75
+ function ageInDays(timestamp, nowMs) {
76
+ return Math.max(0, nowMs - timestamp.getTime()) / DAY_MS;
77
+ }
78
+ /**
79
+ * Apply temporal decay to a list of memory hits.
80
+ *
81
+ * Priority for the effective timestamp:
82
+ * 1. Dated path (`memory/YYYY-MM-DD.md`) — use the date in the path
83
+ * 2. Otherwise, if the path is evergreen — NO decay
84
+ * 3. Otherwise, use the row's `createdAt`
85
+ */
86
+ function applyTemporalDecayToHits(hits, config = {}, nowMs = Date.now()) {
87
+ const merged = { ...DEFAULT_TEMPORAL_DECAY_CONFIG, ...config };
88
+ if (!merged.enabled)
89
+ return [...hits];
90
+ return hits.map((h) => {
91
+ const datedTs = parseMemoryDateFromPath(h.path);
92
+ let ts = datedTs;
93
+ if (!ts) {
94
+ if (isEvergreenMemoryPath(h.path))
95
+ return h;
96
+ ts = h.createdAt ?? null;
97
+ }
98
+ if (!ts)
99
+ return h;
100
+ return {
101
+ ...h,
102
+ score: applyTemporalDecayToScore({
103
+ score: h.score,
104
+ ageInDays: ageInDays(ts, nowMs),
105
+ halfLifeDays: merged.halfLifeDays,
106
+ }),
107
+ };
108
+ });
109
+ }
110
+
111
+ exports.DEFAULT_TEMPORAL_DECAY_CONFIG = DEFAULT_TEMPORAL_DECAY_CONFIG;
112
+ exports.applyTemporalDecayToHits = applyTemporalDecayToHits;
113
+ exports.applyTemporalDecayToScore = applyTemporalDecayToScore;
114
+ exports.calculateTemporalDecayMultiplier = calculateTemporalDecayMultiplier;
115
+ exports.isEvergreenMemoryPath = isEvergreenMemoryPath;
116
+ exports.parseMemoryDateFromPath = parseMemoryDateFromPath;
117
+ exports.toDecayLambda = toDecayLambda;
118
+ //# sourceMappingURL=temporalDecay.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temporalDecay.cjs","sources":["../../../src/memory/temporalDecay.ts"],"sourcesContent":["/**\n * Temporal decay — Phase 2.\n *\n * Ported from upstream `extensions/memory-core/src/memory/temporal-decay.ts`.\n * Ages dated memory files (`memory/YYYY-MM-DD.md`) using exponential decay\n * `multiplier = exp(-ln(2) / halfLifeDays * ageInDays)`. At half-life, the\n * score is exactly halved.\n *\n * Evergreen files (MEMORY.md, memory/topics.md, any non-dated file inside\n * memory/) do NOT decay — they represent durable knowledge and should stay\n * hot regardless of age. This mirrors upstream's `isEvergreenMemoryPath`.\n *\n * Since our pgvector rows carry `createdAt`, we don't need filesystem stat\n * fallback — the row timestamp is authoritative for any file without a\n * date in the path.\n */\n\nexport interface TemporalDecayConfig {\n enabled: boolean;\n halfLifeDays: number;\n}\n\nexport const DEFAULT_TEMPORAL_DECAY_CONFIG: TemporalDecayConfig = {\n enabled: false,\n halfLifeDays: 30,\n};\n\nconst DAY_MS = 24 * 60 * 60 * 1000;\nconst DATED_MEMORY_PATH_RE = /(?:^|\\/)memory\\/(\\d{4})-(\\d{2})-(\\d{2})\\.md$/;\n\nexport function toDecayLambda(halfLifeDays: number): number {\n if (!Number.isFinite(halfLifeDays) || halfLifeDays <= 0) return 0;\n return Math.LN2 / halfLifeDays;\n}\n\nexport function calculateTemporalDecayMultiplier(params: {\n ageInDays: number;\n halfLifeDays: number;\n}): number {\n const lambda = toDecayLambda(params.halfLifeDays);\n const age = Math.max(0, params.ageInDays);\n if (lambda <= 0 || !Number.isFinite(age)) return 1;\n return Math.exp(-lambda * age);\n}\n\nexport function applyTemporalDecayToScore(params: {\n score: number;\n ageInDays: number;\n halfLifeDays: number;\n}): number {\n return params.score * calculateTemporalDecayMultiplier(params);\n}\n\nfunction normalizePath(p: string): string {\n return (p ?? '').replace(/\\\\/g, '/').replace(/^\\.\\//, '');\n}\n\n/** Parse a date out of `memory/YYYY-MM-DD.md` — returns null on non-match or invalid date. */\nexport function parseMemoryDateFromPath(filePath: string): Date | null {\n const m = DATED_MEMORY_PATH_RE.exec(normalizePath(filePath));\n if (!m) return null;\n const y = Number(m[1]);\n const mo = Number(m[2]);\n const d = Number(m[3]);\n if (!Number.isInteger(y) || !Number.isInteger(mo) || !Number.isInteger(d))\n return null;\n const ts = Date.UTC(y, mo - 1, d);\n const parsed = new Date(ts);\n if (\n parsed.getUTCFullYear() !== y ||\n parsed.getUTCMonth() !== mo - 1 ||\n parsed.getUTCDate() !== d\n ) {\n return null;\n }\n return parsed;\n}\n\n/**\n * Evergreen = durable knowledge file that should not decay.\n * - `MEMORY.md` / `memory.md` at root\n * - anything inside `memory/` that is NOT a dated `YYYY-MM-DD.md` file\n */\nexport function isEvergreenMemoryPath(filePath: string): boolean {\n const n = normalizePath(filePath);\n if (n === 'MEMORY.md' || n === 'memory.md') return true;\n if (!n.startsWith('memory/')) return false;\n return !DATED_MEMORY_PATH_RE.test(n);\n}\n\nfunction ageInDays(timestamp: Date, nowMs: number): number {\n return Math.max(0, nowMs - timestamp.getTime()) / DAY_MS;\n}\n\nexport interface DecayCandidate {\n path: string;\n score: number;\n createdAt?: Date;\n}\n\n/**\n * Apply temporal decay to a list of memory hits.\n *\n * Priority for the effective timestamp:\n * 1. Dated path (`memory/YYYY-MM-DD.md`) — use the date in the path\n * 2. Otherwise, if the path is evergreen — NO decay\n * 3. Otherwise, use the row's `createdAt`\n */\nexport function applyTemporalDecayToHits<T extends DecayCandidate>(\n hits: T[],\n config: Partial<TemporalDecayConfig> = {},\n nowMs: number = Date.now()\n): T[] {\n const merged = { ...DEFAULT_TEMPORAL_DECAY_CONFIG, ...config };\n if (!merged.enabled) return [...hits];\n\n return hits.map((h) => {\n const datedTs = parseMemoryDateFromPath(h.path);\n let ts: Date | null = datedTs;\n if (!ts) {\n if (isEvergreenMemoryPath(h.path)) return h;\n ts = h.createdAt ?? null;\n }\n if (!ts) return h;\n return {\n ...h,\n score: applyTemporalDecayToScore({\n score: h.score,\n ageInDays: ageInDays(ts, nowMs),\n halfLifeDays: merged.halfLifeDays,\n }),\n };\n });\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;AAeG;AAOI,MAAM,6BAA6B,GAAwB;AAChE,IAAA,OAAO,EAAE,KAAK;AACd,IAAA,YAAY,EAAE,EAAE;;AAGlB,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI;AAClC,MAAM,oBAAoB,GAAG,8CAA8C;AAErE,SAAU,aAAa,CAAC,YAAoB,EAAA;IAChD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,YAAY,IAAI,CAAC;AAAE,QAAA,OAAO,CAAC;AACjE,IAAA,OAAO,IAAI,CAAC,GAAG,GAAG,YAAY;AAChC;AAEM,SAAU,gCAAgC,CAAC,MAGhD,EAAA;IACC,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,YAAY,CAAC;AACjD,IAAA,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC;IACzC,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM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AACA,QAAA,IAAI,CAAC,EAAE;AAAE,YAAA,OAAO,CAAC;QACjB,OAAO;AACL,YAAA,GAAG,CAAC;YACJ,KAAK,EAAE,yBAAyB,CAAC;gBAC/B,KAAK,EAAE,CAAC,CAAC,KAAK;AACd,gBAAA,SAAS,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,CAAC;gBAC/B,YAAY,EAAE,MAAM,CAAC,YAAY;aAClC,CAAC;SACH;AACH,IAAA,CAAC,CAAC;AACJ;;;;;;;;;;"}
@@ -25,7 +25,7 @@ var events = require('../utils/events.cjs');
25
25
  * @param destinationAgentId - The agent that follows this gate
26
26
  */
27
27
  function createApprovalGateNode(config, sourceAgentId, destinationAgentId) {
28
- const { gateId, channel = 'chat', prompt, approver, timeoutMs, } = config;
28
+ const { gateId, channel = 'chat', prompt, approver, timeoutMs } = config;
29
29
  /**
30
30
  * The gate node function. Receives the current graph state,
31
31
  * dispatches a notification, calls interrupt(), and returns
@@ -1 +1 @@
1
- {"version":3,"file":"ApprovalGateNode.cjs","sources":["../../../src/nodes/ApprovalGateNode.ts"],"sourcesContent":["import { interrupt } from '@langchain/langgraph';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ApprovalGateConfig, BaseGraphState } from '@/types/graph';\nimport type { ToolApprovalResponse } from '@/types/tools';\nimport { GraphEvents } from '@/common';\nimport { safeDispatchCustomEvent } from '@/utils/events';\n\n/**\n * Interrupt payload for approval gate nodes.\n * Passed to interrupt() and persisted in the checkpoint.\n */\nexport interface ApprovalGateInterrupt {\n /** Discriminator to distinguish from tool approval interrupts */\n type: 'approval_gate';\n /** Unique gate identifier */\n gateId: string;\n /** Approval channel (chat, outlook, telegram) */\n channel: string;\n /** Human-readable prompt for the approver */\n prompt?: string;\n /** Approver identifier */\n approver?: string;\n /** Timeout in ms */\n timeoutMs?: number;\n /** Source agent ID (who just finished) */\n sourceAgentId?: string;\n /** Destination agent ID (who will run next if approved) */\n destinationAgentId?: string;\n}\n\n/**\n * Creates a graph node function that acts as an approval gate.\n *\n * Unlike tool approval (which respects ExecutionContext and can be auto-approved\n * in scheduled/handoff modes), approval gates ALWAYS fire. They are placed by\n * the builder between agents in a sequence and represent explicit human\n * checkpoints that cannot be bypassed.\n *\n * Flow:\n * 1. Dispatch ON_APPROVAL_GATE notification (for SSE/persistence)\n * 2. Call interrupt() — graph pauses, state is checkpointed\n * 3. On resume, interrupt() returns the ToolApprovalResponse\n * 4. If approved, pass state through (next agent runs)\n * 5. If denied, return state as-is (routing handled by conditional edge)\n *\n * @param config - The approval gate configuration from the edge definition\n * @param sourceAgentId - The agent that precedes this gate\n * @param destinationAgentId - The agent that follows this gate\n */\nexport function createApprovalGateNode(\n config: ApprovalGateConfig,\n sourceAgentId: string,\n destinationAgentId: string,\n) {\n const {\n gateId,\n channel = 'chat',\n prompt,\n approver,\n timeoutMs,\n } = config;\n\n /**\n * The gate node function. Receives the current graph state,\n * dispatches a notification, calls interrupt(), and returns\n * the state with an approval result annotation.\n */\n return async function approvalGateNode(\n state: BaseGraphState,\n runnableConfig?: RunnableConfig,\n ): Promise<Partial<BaseGraphState>> {\n const interruptPayload: ApprovalGateInterrupt = {\n type: 'approval_gate',\n gateId,\n channel,\n prompt,\n approver,\n timeoutMs,\n sourceAgentId,\n destinationAgentId,\n };\n\n // Dispatch notification event so the host can:\n // 1. Persist the approval request to MongoDB\n // 2. Route to the appropriate channel adapter\n // 3. Emit SSE event for chat UI\n safeDispatchCustomEvent(\n GraphEvents.ON_APPROVAL_GATE,\n interruptPayload,\n runnableConfig,\n );\n\n // Pause the graph — state is checkpointed by the MongoDBSaver.\n // On resume via Command({ resume: ToolApprovalResponse }), interrupt()\n // returns the response value.\n const response = interrupt(interruptPayload) as ToolApprovalResponse;\n\n // Return empty state update — the graph structure (conditional edges)\n // handles routing based on the approval result. We store the response\n // in a message so downstream nodes can access it if needed.\n if (response.approved) {\n return {};\n }\n\n // On denial, we could add a system message noting the denial.\n // The conditional edge after this node will route to END or skip.\n return {};\n };\n}\n\n/**\n * Node ID for an approval gate, derived from the gate configuration.\n * Used by MultiAgentGraph when inserting gate nodes into the graph.\n */\nexport function getApprovalGateNodeId(gateId: string): string {\n return `approval_gate_${gateId}`;\n}\n"],"names":["safeDispatchCustomEvent","GraphEvents","interrupt"],"mappings":";;;;;;;AA8BA;;;;;;;;;;;;;;;;;;AAkBG;SACa,sBAAsB,CACpC,MAA0B,EAC1B,aAAqB,EACrB,kBAA0B,EAAA;AAE1B,IAAA,MAAM,EACJ,MAAM,EACN,OAAO,GAAG,MAAM,EAChB,MAAM,EACN,QAAQ,EACR,SAAS,GACV,GAAG,MAAM;AAEV;;;;AAIG;AACH,IAAA,OAAO,eAAe,gBAAgB,CACpC,KAAqB,EACrB,cAA+B,EAAA;AAE/B,QAAA,MAAM,gBAAgB,GAA0B;AAC9C,YAAA,IAAI,EAAE,eAAe;YACrB,MAAM;YACN,OAAO;YACP,MAAM;YACN,QAAQ;YACR,SAAS;YACT,aAAa;YACb,kBAAkB;SACnB;;;;;QAMDA,8BAAuB,CACrBC,iBAAW,CAAC,gBAAgB,EAC5B,gBAAgB,EAChB,cAAc,CACf;;;;AAKD,QAAA,MAAM,QAAQ,GAAGC,mBAAS,CAAC,gBAAgB,CAAyB;;;;AAKpE,QAAA,IAAI,QAAQ,CAAC,QAAQ,EAAE;AACrB,YAAA,OAAO,EAAE;QACX;;;AAIA,QAAA,OAAO,EAAE;AACX,IAAA,CAAC;AACH;AAEA;;;AAGG;AACG,SAAU,qBAAqB,CAAC,MAAc,EAAA;IAClD,OAAO,CAAA,cAAA,EAAiB,MAAM,CAAA,CAAE;AAClC;;;;;"}
1
+ {"version":3,"file":"ApprovalGateNode.cjs","sources":["../../../src/nodes/ApprovalGateNode.ts"],"sourcesContent":["import { interrupt } from '@langchain/langgraph';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ApprovalGateConfig, BaseGraphState } from '@/types/graph';\nimport type { ToolApprovalResponse } from '@/types/tools';\nimport { GraphEvents } from '@/common';\nimport { safeDispatchCustomEvent } from '@/utils/events';\n\n/**\n * Interrupt payload for approval gate nodes.\n * Passed to interrupt() and persisted in the checkpoint.\n */\nexport interface ApprovalGateInterrupt {\n /** Discriminator to distinguish from tool approval interrupts */\n type: 'approval_gate';\n /** Unique gate identifier */\n gateId: string;\n /** Approval channel (chat, outlook, telegram) */\n channel: string;\n /** Human-readable prompt for the approver */\n prompt?: string;\n /** Approver identifier */\n approver?: string;\n /** Timeout in ms */\n timeoutMs?: number;\n /** Source agent ID (who just finished) */\n sourceAgentId?: string;\n /** Destination agent ID (who will run next if approved) */\n destinationAgentId?: string;\n}\n\n/**\n * Creates a graph node function that acts as an approval gate.\n *\n * Unlike tool approval (which respects ExecutionContext and can be auto-approved\n * in scheduled/handoff modes), approval gates ALWAYS fire. They are placed by\n * the builder between agents in a sequence and represent explicit human\n * checkpoints that cannot be bypassed.\n *\n * Flow:\n * 1. Dispatch ON_APPROVAL_GATE notification (for SSE/persistence)\n * 2. Call interrupt() — graph pauses, state is checkpointed\n * 3. On resume, interrupt() returns the ToolApprovalResponse\n * 4. If approved, pass state through (next agent runs)\n * 5. If denied, return state as-is (routing handled by conditional edge)\n *\n * @param config - The approval gate configuration from the edge definition\n * @param sourceAgentId - The agent that precedes this gate\n * @param destinationAgentId - The agent that follows this gate\n */\nexport function createApprovalGateNode(\n config: ApprovalGateConfig,\n sourceAgentId: string,\n destinationAgentId: string\n) {\n const { gateId, channel = 'chat', prompt, approver, timeoutMs } = config;\n\n /**\n * The gate node function. Receives the current graph state,\n * dispatches a notification, calls interrupt(), and returns\n * the state with an approval result annotation.\n */\n return async function approvalGateNode(\n state: BaseGraphState,\n runnableConfig?: RunnableConfig\n ): Promise<Partial<BaseGraphState>> {\n const interruptPayload: ApprovalGateInterrupt = {\n type: 'approval_gate',\n gateId,\n channel,\n prompt,\n approver,\n timeoutMs,\n sourceAgentId,\n destinationAgentId,\n };\n\n // Dispatch notification event so the host can:\n // 1. Persist the approval request to MongoDB\n // 2. Route to the appropriate channel adapter\n // 3. Emit SSE event for chat UI\n safeDispatchCustomEvent(\n GraphEvents.ON_APPROVAL_GATE,\n interruptPayload,\n runnableConfig\n );\n\n // Pause the graph — state is checkpointed by the MongoDBSaver.\n // On resume via Command({ resume: ToolApprovalResponse }), interrupt()\n // returns the response value.\n const response = interrupt(interruptPayload) as ToolApprovalResponse;\n\n // Return empty state update — the graph structure (conditional edges)\n // handles routing based on the approval result. We store the response\n // in a message so downstream nodes can access it if needed.\n if (response.approved) {\n return {};\n }\n\n // On denial, we could add a system message noting the denial.\n // The conditional edge after this node will route to END or skip.\n return {};\n };\n}\n\n/**\n * Node ID for an approval gate, derived from the gate configuration.\n * Used by MultiAgentGraph when inserting gate nodes into the graph.\n */\nexport function getApprovalGateNodeId(gateId: string): string {\n return `approval_gate_${gateId}`;\n}\n"],"names":["safeDispatchCustomEvent","GraphEvents","interrupt"],"mappings":";;;;;;;AA8BA;;;;;;;;;;;;;;;;;;AAkBG;SACa,sBAAsB,CACpC,MAA0B,EAC1B,aAAqB,EACrB,kBAA0B,EAAA;AAE1B,IAAA,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM;AAExE;;;;AAIG;AACH,IAAA,OAAO,eAAe,gBAAgB,CACpC,KAAqB,EACrB,cAA+B,EAAA;AAE/B,QAAA,MAAM,gBAAgB,GAA0B;AAC9C,YAAA,IAAI,EAAE,eAAe;YACrB,MAAM;YACN,OAAO;YACP,MAAM;YACN,QAAQ;YACR,SAAS;YACT,aAAa;YACb,kBAAkB;SACnB;;;;;QAMDA,8BAAuB,CACrBC,iBAAW,CAAC,gBAAgB,EAC5B,gBAAgB,EAChB,cAAc,CACf;;;;AAKD,QAAA,MAAM,QAAQ,GAAGC,mBAAS,CAAC,gBAAgB,CAAyB;;;;AAKpE,QAAA,IAAI,QAAQ,CAAC,QAAQ,EAAE;AACrB,YAAA,OAAO,EAAE;QACX;;;AAIA,QAAA,OAAO,EAAE;AACX,IAAA,CAAC;AACH;AAEA;;;AAGG;AACG,SAAU,qBAAqB,CAAC,MAAc,EAAA;IAClD,OAAO,CAAA,cAAA,EAAiB,MAAM,CAAA,CAAE;AAClC;;;;;"}
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ var constants = require('../memory/constants.cjs');
4
+ var paths = require('../memory/paths.cjs');
5
+
6
+ /**
7
+ * Memory-flush prompt resolver.
8
+ *
9
+ * The raw prompt strings live in `src/memory/constants.ts` and contain a
10
+ * single `{{MEMORY_PATHS_RUBRIC}}` placeholder. This module substitutes
11
+ * the caller-scoped rubric (produced by `renderPathsRubric()` in
12
+ * `src/memory/paths.ts`) into that placeholder at flush time, so:
13
+ *
14
+ * - a collaborative agent with a real caller sees all 8 paths
15
+ * - an isolated/autonomous agent sees only the 4 agent-tier paths
16
+ * (user-tier rows are physically omitted from the rubric the LLM
17
+ * sees, so it cannot route writes there even by mistake)
18
+ *
19
+ * ## Why this lives in prompts/ and not memory/constants.ts
20
+ *
21
+ * The constants file is pure strings — no imports, no runtime work.
22
+ * Injecting the rubric needs access to `renderPathsRubric()` which
23
+ * needs `MemoryScope`, so the substitution has to happen one layer up.
24
+ * Keeping the placeholder in constants and the substitution here means
25
+ * tests of the raw prompt (no scope) and tests of the rendered prompt
26
+ * (scope-aware) stay independent.
27
+ */
28
+ /** Back-compat alias so existing callers keep working. */
29
+ const MEMORY_FLUSH_SYSTEM_PROMPT = constants.DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT;
30
+ /**
31
+ * Substitutes the paths rubric into the raw prompts for a given scope.
32
+ *
33
+ * Idempotent (calling it twice on already-resolved strings is a no-op
34
+ * because the placeholder will have been replaced already). Safe to
35
+ * call per-flush; no caching needed — the string concat is microseconds.
36
+ */
37
+ function resolveFlushPrompts(options) {
38
+ const rubric = paths.renderPathsRubric(options.scope);
39
+ return {
40
+ prompt: constants.DEFAULT_MEMORY_FLUSH_PROMPT.replaceAll(constants.FLUSH_PROMPT_RUBRIC_PLACEHOLDER, rubric),
41
+ systemPrompt: constants.DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT.replaceAll(constants.FLUSH_PROMPT_RUBRIC_PLACEHOLDER, rubric),
42
+ };
43
+ }
44
+
45
+ exports.DEFAULT_MEMORY_FLUSH_PROMPT = constants.DEFAULT_MEMORY_FLUSH_PROMPT;
46
+ exports.DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT = constants.DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT;
47
+ exports.MEMORY_FLUSH_SYSTEM_PROMPT = MEMORY_FLUSH_SYSTEM_PROMPT;
48
+ exports.resolveFlushPrompts = resolveFlushPrompts;
49
+ //# sourceMappingURL=memoryFlushPrompt.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoryFlushPrompt.cjs","sources":["../../../src/prompts/memoryFlushPrompt.ts"],"sourcesContent":["/**\n * Memory-flush prompt resolver.\n *\n * The raw prompt strings live in `src/memory/constants.ts` and contain a\n * single `{{MEMORY_PATHS_RUBRIC}}` placeholder. This module substitutes\n * the caller-scoped rubric (produced by `renderPathsRubric()` in\n * `src/memory/paths.ts`) into that placeholder at flush time, so:\n *\n * - a collaborative agent with a real caller sees all 8 paths\n * - an isolated/autonomous agent sees only the 4 agent-tier paths\n * (user-tier rows are physically omitted from the rubric the LLM\n * sees, so it cannot route writes there even by mistake)\n *\n * ## Why this lives in prompts/ and not memory/constants.ts\n *\n * The constants file is pure strings — no imports, no runtime work.\n * Injecting the rubric needs access to `renderPathsRubric()` which\n * needs `MemoryScope`, so the substitution has to happen one layer up.\n * Keeping the placeholder in constants and the substitution here means\n * tests of the raw prompt (no scope) and tests of the rendered prompt\n * (scope-aware) stay independent.\n */\nimport {\n DEFAULT_MEMORY_FLUSH_PROMPT,\n DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT,\n FLUSH_PROMPT_RUBRIC_PLACEHOLDER,\n} from '@/memory/constants';\nimport { renderPathsRubric } from '@/memory/paths';\nimport type { MemoryScope } from '@/memory/types';\n\nexport { DEFAULT_MEMORY_FLUSH_PROMPT, DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT };\n\n/** Back-compat alias so existing callers keep working. */\nexport const MEMORY_FLUSH_SYSTEM_PROMPT = DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT;\n\n/** Minimum scope shape needed to render the rubric. */\nexport interface ResolveFlushPromptsOptions {\n /**\n * The scope the memory tools were built with. Only `userId` matters\n * for rubric rendering — if absent, the user-tier paths are filtered\n * out of the rubric the LLM sees.\n */\n scope: Pick<MemoryScope, 'userId'>;\n}\n\n/**\n * Result shape returned to `runMemoryFlush`. `prompt` goes in the\n * HumanMessage, `systemPrompt` goes in the SystemMessage. Both already\n * have the rubric substituted — the caller should pass them through\n * verbatim.\n */\nexport interface ResolvedFlushPrompts {\n prompt: string;\n systemPrompt: string;\n}\n\n/**\n * Substitutes the paths rubric into the raw prompts for a given scope.\n *\n * Idempotent (calling it twice on already-resolved strings is a no-op\n * because the placeholder will have been replaced already). Safe to\n * call per-flush; no caching needed — the string concat is microseconds.\n */\nexport function resolveFlushPrompts(\n options: ResolveFlushPromptsOptions\n): ResolvedFlushPrompts {\n const rubric = renderPathsRubric(options.scope);\n return {\n prompt: DEFAULT_MEMORY_FLUSH_PROMPT.replaceAll(\n FLUSH_PROMPT_RUBRIC_PLACEHOLDER,\n rubric\n ),\n systemPrompt: DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT.replaceAll(\n FLUSH_PROMPT_RUBRIC_PLACEHOLDER,\n rubric\n ),\n };\n}\n"],"names":["DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT","renderPathsRubric","DEFAULT_MEMORY_FLUSH_PROMPT","FLUSH_PROMPT_RUBRIC_PLACEHOLDER"],"mappings":";;;;;AAAA;;;;;;;;;;;;;;;;;;;;;AAqBG;AAWH;AACO,MAAM,0BAA0B,GAAGA;AAuB1C;;;;;;AAMG;AACG,SAAU,mBAAmB,CACjC,OAAmC,EAAA;IAEnC,MAAM,MAAM,GAAGC,uBAAiB,CAAC,OAAO,CAAC,KAAK,CAAC;IAC/C,OAAO;QACL,MAAM,EAAEC,qCAA2B,CAAC,UAAU,CAC5CC,yCAA+B,EAC/B,MAAM,CACP;QACD,YAAY,EAAEH,4CAAkC,CAAC,UAAU,CACzDG,yCAA+B,EAC/B,MAAM,CACP;KACF;AACH;;;;;;;"}
package/dist/cjs/run.cjs CHANGED
@@ -39,6 +39,7 @@ class Run {
39
39
  Graph;
40
40
  returnContent = false;
41
41
  skipCleanup = false;
42
+ _originalCallbacksSnapshot;
42
43
  constructor(config) {
43
44
  const runId = config.runId ?? '';
44
45
  if (!runId) {
@@ -205,7 +206,19 @@ class Run {
205
206
  this.Graph.resetValues(streamOptions?.keepContent);
206
207
  /** Custom event callback to intercept and handle custom events */
207
208
  const customEventCallback = this.createCustomEventCallback();
208
- const baseCallbacks = config.callbacks ?? [];
209
+ // IMPORTANT: snapshot the ORIGINAL caller-provided callbacks once and
210
+ // reuse that snapshot on every invocation. Previously we read
211
+ // `config.callbacks` on each call and concat'd onto it — but the same
212
+ // `config` object is passed back to processStream() on HITL resume, so
213
+ // the previous run's appended callbacks became the new "base" and we
214
+ // ended up stacking duplicate stream + custom handlers on every cycle.
215
+ // That produced N-times duplicated message_delta events on the SSE
216
+ // stream (character-interleaved text in the UI after tool approval).
217
+ if (!this._originalCallbacksSnapshot) {
218
+ this._originalCallbacksSnapshot =
219
+ config.callbacks ?? [];
220
+ }
221
+ const baseCallbacks = this._originalCallbacksSnapshot;
209
222
  const streamCallbacks = streamOptions?.callbacks
210
223
  ? this.getCallbacks(streamOptions.callbacks)
211
224
  : [];
@@ -339,7 +352,7 @@ class Run {
339
352
  }
340
353
  try {
341
354
  const state = await this.graphRunnable.getState(config);
342
- return state.tasks?.some((task) => task.interrupts?.length > 0) ?? false;
355
+ return state.tasks.some((task) => task.interrupts.length > 0) ?? false;
343
356
  }
344
357
  catch {
345
358
  return false;
@@ -356,7 +369,7 @@ class Run {
356
369
  }
357
370
  try {
358
371
  const state = await this.graphRunnable.getState(config);
359
- return (state.tasks?.flatMap((task) => (task.interrupts ?? []).map((i) => i.value)) ?? []);
372
+ return (state.tasks.flatMap((task) => (task.interrupts ?? []).map((i) => i.value)) ?? []);
360
373
  }
361
374
  catch {
362
375
  return [];
@@ -1 +1 @@
1
- {"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport './instrumentation';\nimport { ObservabilityCallbackHandler } from '@illuma-ai/observability-langchain';\nimport { Command, isGraphInterrupt } from '@langchain/langgraph';\nimport { PromptTemplate } from '@langchain/core/prompts';\nimport { RunnableLambda } from '@langchain/core/runnables';\nimport { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';\nimport { BaseCallbackHandler } from '@langchain/core/callbacks/base';\nimport type {\n MessageContentComplex,\n BaseMessage,\n} from '@langchain/core/messages';\nimport type { StringPromptValue } from '@langchain/core/prompt_values';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type * as t from '@/types';\nimport {\n createCompletionTitleRunnable,\n createTitleRunnable,\n} from '@/utils/title';\nimport { createTokenCounter, encodingForModel } from '@/utils/tokens';\nimport { GraphEvents, Callback, TitleMethod } from '@/common';\nimport { MultiAgentGraph } from '@/graphs/MultiAgentGraph';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\nimport { isOpenAILike } from '@/utils/llm';\nimport { isPresent } from '@/utils/misc';\n\nexport const defaultOmitOptions = new Set([\n 'stream',\n 'thinking',\n 'streaming',\n 'maxTokens',\n 'clientOptions',\n 'thinkingConfig',\n 'thinkingBudget',\n 'includeThoughts',\n 'maxOutputTokens',\n 'additionalModelRequestFields',\n]);\n\nexport class Run<_T extends t.BaseGraphState> {\n id: string;\n private tokenCounter?: t.TokenCounter;\n private handlerRegistry?: HandlerRegistry;\n private indexTokenCountMap?: Record<string, number>;\n graphRunnable?: t.CompiledStateWorkflow;\n Graph: StandardGraph | MultiAgentGraph | undefined;\n returnContent: boolean = false;\n private skipCleanup: boolean = false;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n this.tokenCounter = config.tokenCounter;\n this.indexTokenCountMap = config.indexTokenCountMap;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(\n config.customHandlers\n )) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n /** Handle different graph types */\n if (config.graphConfig.type === 'multi-agent') {\n this.graphRunnable = this.createMultiAgentGraph(config.graphConfig);\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n } else {\n /** Default to legacy graph for 'standard' or undefined type */\n this.graphRunnable = this.createLegacyGraph(config.graphConfig);\n if (this.Graph) {\n this.Graph.compileOptions =\n config.graphConfig.compileOptions ?? this.Graph.compileOptions;\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n this.skipCleanup = config.skipCleanup ?? false;\n }\n\n private createLegacyGraph(\n config: t.LegacyGraphConfig | t.StandardGraphConfig\n ): t.CompiledStateWorkflow {\n let agentConfig: t.AgentInputs;\n let signal: AbortSignal | undefined;\n\n /** Check if this is a multi-agent style config (has agents array) */\n if ('agents' in config && Array.isArray(config.agents)) {\n if (config.agents.length === 0) {\n throw new Error('At least one agent must be provided');\n }\n agentConfig = config.agents[0];\n signal = config.signal;\n } else {\n /** Legacy path: build agent config from llmConfig */\n const {\n type: _type,\n llmConfig,\n signal: legacySignal,\n tools = [],\n ...agentInputs\n } = config as t.LegacyGraphConfig;\n const { provider, ...clientOptions } = llmConfig;\n\n agentConfig = {\n ...agentInputs,\n tools,\n provider,\n clientOptions,\n agentId: 'default',\n };\n signal = legacySignal;\n }\n\n const standardGraph = new StandardGraph({\n signal,\n runId: this.id,\n agents: [agentConfig],\n tokenCounter: this.tokenCounter,\n indexTokenCountMap: this.indexTokenCountMap,\n });\n /** Propagate compile options from graph config */\n standardGraph.compileOptions = config.compileOptions;\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n private createMultiAgentGraph(\n config: t.MultiAgentGraphConfig\n ): t.CompiledStateWorkflow {\n const { agents, edges, compileOptions, resumeFromAgentId } = config;\n\n const multiAgentGraph = new MultiAgentGraph({\n runId: this.id,\n agents,\n edges,\n resumeFromAgentId,\n tokenCounter: this.tokenCounter,\n indexTokenCountMap: this.indexTokenCountMap,\n });\n\n if (compileOptions != null) {\n multiAgentGraph.compileOptions = compileOptions;\n }\n\n this.Graph = multiAgentGraph;\n return multiAgentGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(\n config: t.RunConfig\n ): Promise<Run<T>> {\n /** Create tokenCounter if indexTokenCountMap is provided but tokenCounter is not */\n if (config.indexTokenCountMap && !config.tokenCounter) {\n const gc = config.graphConfig;\n const clientOpts =\n 'agents' in gc ? gc.agents[0]?.clientOptions : gc.clientOptions;\n const model = (clientOpts as { model?: string } | undefined)?.model ?? '';\n config.tokenCounter = await createTokenCounter(encodingForModel(model));\n }\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error(\n 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n return this.Graph.getRunMessages();\n }\n\n /**\n * Manually trigger cleanup of heavy state (messages, config, etc.).\n * Call this after all continuations are complete when using skipCleanup=true.\n */\n clearState(): void {\n if (this.Graph) {\n this.Graph.clearHeavyState();\n }\n }\n\n /**\n * Returns the normalized finish/stop reason from the last LLM invocation.\n * Delegates to the underlying Graph instance.\n */\n getLastFinishReason(): string | undefined {\n if (this.Graph && 'getLastFinishReason' in this.Graph) {\n return (this.Graph as StandardGraph).getLastFinishReason();\n }\n return undefined;\n }\n\n /**\n * Returns the ID of the last agent that produced output in a multi-agent graph.\n * Used by auto-continuation to determine which agent's context to preserve\n * when a response is truncated after an agent handoff.\n * Returns undefined for single-agent graphs.\n */\n getLastActiveAgentId(): string | undefined {\n if (this.Graph && this.Graph instanceof MultiAgentGraph) {\n return this.Graph.getLastActiveAgentId();\n }\n return undefined;\n }\n\n /**\n * Creates a custom event callback handler that intercepts custom events\n * and processes them through our handler registry instead of EventStreamCallbackHandler\n */\n private createCustomEventCallback() {\n return async (\n eventName: string,\n data: unknown,\n runId: string,\n tags?: string[],\n metadata?: Record<string, unknown>\n ): Promise<void> => {\n const handler = this.handlerRegistry?.getHandler(eventName);\n if (handler && this.Graph) {\n return await handler.handle(\n eventName,\n data as\n | t.StreamEventData\n | t.ModelEndData\n | t.RunStep\n | t.RunStepDeltaEvent\n | t.MessageDeltaEvent\n | t.ReasoningDeltaEvent\n | { result: t.ToolEndEvent },\n metadata,\n this.Graph\n );\n }\n };\n }\n\n /**\n * Processes the graph stream for a given input.\n *\n * @param inputs - Either the initial state (IState) for a new run, or a\n * Command (e.g., `new Command({ resume: ... })`) to resume from an interrupt.\n * @param config - Runnable config with version and optional run_id.\n * @param streamOptions - Optional stream event callbacks and options.\n */\n async processStream(\n inputs: t.IState | Command,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n streamOptions?: t.EventStreamOptions\n ): Promise<MessageContentComplex[] | undefined> {\n if (this.graphRunnable == null) {\n throw new Error(\n 'Run not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n if (!this.Graph) {\n throw new Error(\n 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n\n this.Graph.resetValues(streamOptions?.keepContent);\n\n /** Custom event callback to intercept and handle custom events */\n const customEventCallback = this.createCustomEventCallback();\n\n const baseCallbacks = (config.callbacks as t.ProvidedCallbacks) ?? [];\n const streamCallbacks = streamOptions?.callbacks\n ? this.getCallbacks(streamOptions.callbacks)\n : [];\n\n const customHandler = BaseCallbackHandler.fromMethods({\n [Callback.CUSTOM_EVENT]: customEventCallback,\n });\n customHandler.awaitHandlers = true;\n\n config.callbacks = baseCallbacks\n .concat(streamCallbacks)\n .concat(customHandler);\n\n const illumaSecretKey = process.env.ILLUMA_SECRET_KEY;\n const illumaPublicKey = process.env.ILLUMA_PUBLIC_KEY;\n const illumaBaseUrl = process.env.ILLUMA_BASE_URL;\n\n if (\n isPresent(illumaSecretKey) &&\n isPresent(illumaPublicKey) &&\n isPresent(illumaBaseUrl)\n ) {\n try {\n const userId = config.configurable?.user_id;\n const sessionId = config.configurable?.thread_id;\n const handler = new ObservabilityCallbackHandler({\n clientOptions: {\n secretKey: illumaSecretKey!,\n publicKey: illumaPublicKey!,\n baseUrl: illumaBaseUrl!,\n },\n userId,\n sessionId,\n metadata: {\n messageId: this.id,\n parentMessageId: config.configurable?.requestBody?.parentMessageId,\n },\n });\n config.callbacks = (\n (config.callbacks as t.ProvidedCallbacks) ?? []\n ).concat([handler]);\n } catch {\n // Gracefully skip if @illuma-ai/observability-node is not installed\n }\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, {\n run_id: this.id,\n });\n\n const stream = this.graphRunnable.streamEvents(inputs, config, {\n raiseError: true,\n /**\n * Prevent EventStreamCallbackHandler from processing custom events.\n * Custom events are already handled via our createCustomEventCallback()\n * which routes them through the handlerRegistry.\n * Without this flag, EventStreamCallbackHandler throws errors when\n * custom events are dispatched for run IDs not in its internal map\n * (due to timing issues in parallel execution or after run cleanup).\n */\n ignoreCustomEvent: true,\n });\n\n try {\n for await (const event of stream) {\n const { data, metadata, ...info } = event;\n\n const eventName: t.EventName = info.event;\n\n /** Skip custom events as they're handled by our callback */\n if (eventName === GraphEvents.ON_CUSTOM_EVENT) {\n continue;\n }\n\n const handler = this.handlerRegistry?.getHandler(eventName);\n if (handler) {\n await handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n } catch (e) {\n // GraphInterrupt is expected when interrupt() fires (HITL approval flow).\n // Exit gracefully so the host can check hasInterrupts() and resume.\n if (isGraphInterrupt(e)) {\n return undefined;\n }\n throw e;\n }\n\n /**\n * Break the reference chain that keeps heavy data alive via\n * LangGraph's internal `__pregel_scratchpad.currentTaskInput` →\n * `@langchain/core` `RunTree.extra[lc:child_config]` →\n * Node.js `AsyncLocalStorage` context captured by timers/promises.\n *\n * Without this, base64-encoded images/PDFs in message content remain\n * reachable from lingering `Timeout` handles until GC runs.\n */\n if (!this.skipCleanup) {\n if (\n (config.configurable as Record<string, unknown> | undefined) != null\n ) {\n for (const key of Object.getOwnPropertySymbols(config.configurable)) {\n const val = config.configurable[key as unknown as string] as\n | Record<string, unknown>\n | undefined;\n if (\n val != null &&\n typeof val === 'object' &&\n 'currentTaskInput' in val\n ) {\n (val as Record<string, unknown>).currentTaskInput = undefined;\n }\n delete config.configurable[key as unknown as string];\n }\n config.configurable = undefined;\n }\n config.callbacks = undefined;\n }\n\n const result = this.returnContent\n ? this.Graph.getContentParts()\n : undefined;\n\n if (!this.skipCleanup) {\n this.Graph.clearHeavyState();\n }\n return result;\n }\n\n private createSystemCallback<K extends keyof t.ClientCallbacks>(\n clientCallbacks: t.ClientCallbacks,\n key: K\n ): t.SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as t.SystemCallbacks[K];\n }\n\n /**\n * Checks whether the graph was interrupted (e.g., by HITL tool approval).\n * Call after processStream() returns to determine if the graph is waiting\n * for a resume via Command({ resume }).\n *\n * Requires a checkpointer to be configured — without one, interrupt state\n * is not persisted and this always returns false.\n */\n async hasInterrupts(\n config: Partial<RunnableConfig>\n ): Promise<boolean> {\n if (!this.graphRunnable) {\n return false;\n }\n try {\n const state = await this.graphRunnable.getState(config);\n return state.tasks?.some((task) => task.interrupts?.length > 0) ?? false;\n } catch {\n return false;\n }\n }\n\n /**\n * Returns the interrupt values from the graph state.\n * Each interrupt's `value` contains the data passed to `interrupt()` by the node\n * (e.g., a ToolApprovalRequest for HITL).\n */\n async getInterruptValues(\n config: Partial<RunnableConfig>\n ): Promise<unknown[]> {\n if (!this.graphRunnable) {\n return [];\n }\n try {\n const state = await this.graphRunnable.getState(config);\n return (\n state.tasks?.flatMap((task) =>\n (task.interrupts ?? []).map((i) => i.value)\n ) ?? []\n );\n } catch {\n return [];\n }\n }\n\n getCallbacks(clientCallbacks: t.ClientCallbacks): t.SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_ERROR\n ),\n [Callback.TOOL_START]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_START\n ),\n [Callback.TOOL_END]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_END\n ),\n };\n }\n\n async generateTitle({\n provider,\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n titleMethod = TitleMethod.COMPLETION,\n titlePromptTemplate,\n }: t.RunTitleOptions): Promise<{ language?: string; title?: string }> {\n const titleSecretKey = process.env.ILLUMA_SECRET_KEY;\n const titlePublicKey = process.env.ILLUMA_PUBLIC_KEY;\n const titleBaseUrl = process.env.ILLUMA_BASE_URL;\n\n if (\n chainOptions != null &&\n isPresent(titleSecretKey) &&\n isPresent(titlePublicKey) &&\n isPresent(titleBaseUrl)\n ) {\n try {\n const userId = chainOptions.configurable?.user_id;\n const sessionId = chainOptions.configurable?.thread_id;\n const handler = new ObservabilityCallbackHandler({\n clientOptions: {\n secretKey: titleSecretKey!,\n publicKey: titlePublicKey!,\n baseUrl: titleBaseUrl!,\n },\n userId,\n sessionId,\n metadata: {\n messageId: 'title-' + this.id,\n },\n });\n chainOptions.callbacks = (\n (chainOptions.callbacks as t.ProvidedCallbacks) ?? []\n ).concat([handler]);\n } catch {\n // Gracefully skip if @illuma-ai/observability-node is not installed\n }\n }\n\n const convoTemplate = PromptTemplate.fromTemplate(\n titlePromptTemplate ?? 'User: {input}\\nAI: {output}'\n );\n\n const response = contentParts\n .map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n })\n .join('\\n');\n\n const model = this.Graph?.getNewModel({\n provider,\n clientOptions,\n });\n if (!model) {\n return { language: '', title: '' };\n }\n if (\n isOpenAILike(provider) &&\n (model instanceof ChatOpenAI || model instanceof AzureChatOpenAI)\n ) {\n model.temperature = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.temperature as number;\n model.topP = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.topP as number;\n model.frequencyPenalty = (\n clientOptions as t.OpenAIClientOptions | undefined\n )?.frequencyPenalty as number;\n model.presencePenalty = (\n clientOptions as t.OpenAIClientOptions | undefined\n )?.presencePenalty as number;\n model.n = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.n as number;\n }\n\n const convoToTitleInput = new RunnableLambda({\n func: (\n promptValue: StringPromptValue\n ): { convo: string; inputText: string; skipLanguage?: boolean } => ({\n convo: promptValue.value,\n inputText,\n skipLanguage,\n }),\n }).withConfig({ runName: 'ConvoTransform' });\n\n const titleChain =\n titleMethod === TitleMethod.COMPLETION\n ? await createCompletionTitleRunnable(model, titlePrompt)\n : await createTitleRunnable(model, titlePrompt);\n\n /** Pipes `convoTemplate` -> `transformer` -> `titleChain` */\n const fullChain = convoTemplate\n .withConfig({ runName: 'ConvoTemplate' })\n .pipe(convoToTitleInput)\n .pipe(titleChain)\n .withConfig({ runName: 'TitleChain' });\n\n const invokeConfig = Object.assign({}, chainOptions, {\n run_id: this.id,\n runId: this.id,\n });\n\n try {\n return await fullChain.invoke(\n { input: inputText, output: response },\n invokeConfig\n );\n } catch (_e) {\n // Fallback: strip callbacks to avoid EventStream tracer errors in certain environments\n // But preserve observability handler if it exists\n const observabilityHandler = (\n invokeConfig.callbacks as t.ProvidedCallbacks\n )?.find((cb) => cb instanceof ObservabilityCallbackHandler);\n const { callbacks: _cb, ...rest } = invokeConfig;\n const safeConfig = Object.assign({}, rest, {\n callbacks: observabilityHandler ? [observabilityHandler] : [],\n });\n return await fullChain.invoke(\n { input: inputText, output: response },\n safeConfig as Partial<RunnableConfig>\n );\n }\n }\n}\n"],"names":["HandlerRegistry","StandardGraph","MultiAgentGraph","createTokenCounter","encodingForModel","BaseCallbackHandler","Callback","isPresent","ObservabilityCallbackHandler","GraphEvents","isGraphInterrupt","TitleMethod","PromptTemplate","isOpenAILike","ChatOpenAI","AzureChatOpenAI","RunnableLambda","createCompletionTitleRunnable","createTitleRunnable"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AA2BO,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IACxC,QAAQ;IACR,UAAU;IACV,WAAW;IACX,WAAW;IACX,eAAe;IACf,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;IACjB,iBAAiB;IACjB,8BAA8B;AAC/B,CAAA;MAEY,GAAG,CAAA;AACd,IAAA,EAAE;AACM,IAAA,YAAY;AACZ,IAAA,eAAe;AACf,IAAA,kBAAkB;AAC1B,IAAA,aAAa;AACb,IAAA,KAAK;IACL,aAAa,GAAY,KAAK;IACtB,WAAW,GAAY,KAAK;AAEpC,IAAA,WAAA,CAAoB,MAA4B,EAAA;AAC9C,QAAA,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE;QAChC,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC;QACxC;AAEA,QAAA,IAAI,CAAC,EAAE,GAAG,KAAK;AACf,QAAA,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY;AACvC,QAAA,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC,kBAAkB;AAEnD,QAAA,MAAM,eAAe,GAAG,IAAIA,sBAAe,EAAE;AAE7C,QAAA,IAAI,MAAM,CAAC,cAAc,EAAE;AACzB,YAAA,KAAK,MAAM,CAAC,SAAS,EA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AM,SAAS,CAAC,MAAM,CAC3B,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,EACtC,YAAY,CACb;QACH;QAAE,OAAO,EAAE,EAAE;;;AAGX,YAAA,MAAM,oBAAoB,GACxB,YAAY,CAAC,SACd,EAAE,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,YAAYV,mDAA4B,CAAC;YAC3D,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,YAAY;YAChD,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE;gBACzC,SAAS,EAAE,oBAAoB,GAAG,CAAC,oBAAoB,CAAC,GAAG,EAAE;AAC9D,aAAA,CAAC;AACF,YAAA,OAAO,MAAM,SAAS,CAAC,MAAM,CAC3B,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,EACtC,UAAqC,CACtC;QACH;IACF;AACD;;;;;"}
1
+ {"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport './instrumentation';\nimport { ObservabilityCallbackHandler } from '@illuma-ai/observability-langchain';\nimport { Command, isGraphInterrupt } from '@langchain/langgraph';\nimport { PromptTemplate } from '@langchain/core/prompts';\nimport { RunnableLambda } from '@langchain/core/runnables';\nimport { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';\nimport { BaseCallbackHandler } from '@langchain/core/callbacks/base';\nimport type {\n MessageContentComplex,\n BaseMessage,\n} from '@langchain/core/messages';\nimport type { StringPromptValue } from '@langchain/core/prompt_values';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type * as t from '@/types';\nimport {\n createCompletionTitleRunnable,\n createTitleRunnable,\n} from '@/utils/title';\nimport { createTokenCounter, encodingForModel } from '@/utils/tokens';\nimport { GraphEvents, Callback, TitleMethod } from '@/common';\nimport { MultiAgentGraph } from '@/graphs/MultiAgentGraph';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\nimport { isOpenAILike } from '@/utils/llm';\nimport { isPresent } from '@/utils/misc';\n\nexport const defaultOmitOptions = new Set([\n 'stream',\n 'thinking',\n 'streaming',\n 'maxTokens',\n 'clientOptions',\n 'thinkingConfig',\n 'thinkingBudget',\n 'includeThoughts',\n 'maxOutputTokens',\n 'additionalModelRequestFields',\n]);\n\nexport class Run<_T extends t.BaseGraphState> {\n id: string;\n private tokenCounter?: t.TokenCounter;\n private handlerRegistry?: HandlerRegistry;\n private indexTokenCountMap?: Record<string, number>;\n graphRunnable?: t.CompiledStateWorkflow;\n Graph: StandardGraph | MultiAgentGraph | undefined;\n returnContent: boolean = false;\n private skipCleanup: boolean = false;\n private _originalCallbacksSnapshot?: t.ProvidedCallbacks;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n this.tokenCounter = config.tokenCounter;\n this.indexTokenCountMap = config.indexTokenCountMap;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(\n config.customHandlers\n )) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n /** Handle different graph types */\n if (config.graphConfig.type === 'multi-agent') {\n this.graphRunnable = this.createMultiAgentGraph(config.graphConfig);\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n } else {\n /** Default to legacy graph for 'standard' or undefined type */\n this.graphRunnable = this.createLegacyGraph(config.graphConfig);\n if (this.Graph) {\n this.Graph.compileOptions =\n config.graphConfig.compileOptions ?? this.Graph.compileOptions;\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n this.skipCleanup = config.skipCleanup ?? false;\n }\n\n private createLegacyGraph(\n config: t.LegacyGraphConfig | t.StandardGraphConfig\n ): t.CompiledStateWorkflow {\n let agentConfig: t.AgentInputs;\n let signal: AbortSignal | undefined;\n\n /** Check if this is a multi-agent style config (has agents array) */\n if ('agents' in config && Array.isArray(config.agents)) {\n if (config.agents.length === 0) {\n throw new Error('At least one agent must be provided');\n }\n agentConfig = config.agents[0];\n signal = config.signal;\n } else {\n /** Legacy path: build agent config from llmConfig */\n const {\n type: _type,\n llmConfig,\n signal: legacySignal,\n tools = [],\n ...agentInputs\n } = config as t.LegacyGraphConfig;\n const { provider, ...clientOptions } = llmConfig;\n\n agentConfig = {\n ...agentInputs,\n tools,\n provider,\n clientOptions,\n agentId: 'default',\n };\n signal = legacySignal;\n }\n\n const standardGraph = new StandardGraph({\n signal,\n runId: this.id,\n agents: [agentConfig],\n tokenCounter: this.tokenCounter,\n indexTokenCountMap: this.indexTokenCountMap,\n });\n /** Propagate compile options from graph config */\n standardGraph.compileOptions = config.compileOptions;\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n private createMultiAgentGraph(\n config: t.MultiAgentGraphConfig\n ): t.CompiledStateWorkflow {\n const { agents, edges, compileOptions, resumeFromAgentId } = config;\n\n const multiAgentGraph = new MultiAgentGraph({\n runId: this.id,\n agents,\n edges,\n resumeFromAgentId,\n tokenCounter: this.tokenCounter,\n indexTokenCountMap: this.indexTokenCountMap,\n });\n\n if (compileOptions != null) {\n multiAgentGraph.compileOptions = compileOptions;\n }\n\n this.Graph = multiAgentGraph;\n return multiAgentGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(\n config: t.RunConfig\n ): Promise<Run<T>> {\n /** Create tokenCounter if indexTokenCountMap is provided but tokenCounter is not */\n if (config.indexTokenCountMap && !config.tokenCounter) {\n const gc = config.graphConfig;\n const clientOpts =\n 'agents' in gc ? gc.agents[0]?.clientOptions : gc.clientOptions;\n const model = (clientOpts as { model?: string } | undefined)?.model ?? '';\n config.tokenCounter = await createTokenCounter(encodingForModel(model));\n }\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error(\n 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n return this.Graph.getRunMessages();\n }\n\n /**\n * Manually trigger cleanup of heavy state (messages, config, etc.).\n * Call this after all continuations are complete when using skipCleanup=true.\n */\n clearState(): void {\n if (this.Graph) {\n this.Graph.clearHeavyState();\n }\n }\n\n /**\n * Returns the normalized finish/stop reason from the last LLM invocation.\n * Delegates to the underlying Graph instance.\n */\n getLastFinishReason(): string | undefined {\n if (this.Graph && 'getLastFinishReason' in this.Graph) {\n return (this.Graph as StandardGraph).getLastFinishReason();\n }\n return undefined;\n }\n\n /**\n * Returns the ID of the last agent that produced output in a multi-agent graph.\n * Used by auto-continuation to determine which agent's context to preserve\n * when a response is truncated after an agent handoff.\n * Returns undefined for single-agent graphs.\n */\n getLastActiveAgentId(): string | undefined {\n if (this.Graph && this.Graph instanceof MultiAgentGraph) {\n return this.Graph.getLastActiveAgentId();\n }\n return undefined;\n }\n\n /**\n * Creates a custom event callback handler that intercepts custom events\n * and processes them through our handler registry instead of EventStreamCallbackHandler\n */\n private createCustomEventCallback() {\n return async (\n eventName: string,\n data: unknown,\n runId: string,\n tags?: string[],\n metadata?: Record<string, unknown>\n ): Promise<void> => {\n const handler = this.handlerRegistry?.getHandler(eventName);\n if (handler && this.Graph) {\n return await handler.handle(\n eventName,\n data as\n | t.StreamEventData\n | t.ModelEndData\n | t.RunStep\n | t.RunStepDeltaEvent\n | t.MessageDeltaEvent\n | t.ReasoningDeltaEvent\n | { result: t.ToolEndEvent },\n metadata,\n this.Graph\n );\n }\n };\n }\n\n /**\n * Processes the graph stream for a given input.\n *\n * @param inputs - Either the initial state (IState) for a new run, or a\n * Command (e.g., `new Command({ resume: ... })`) to resume from an interrupt.\n * @param config - Runnable config with version and optional run_id.\n * @param streamOptions - Optional stream event callbacks and options.\n */\n async processStream(\n inputs: t.IState | Command,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n streamOptions?: t.EventStreamOptions\n ): Promise<MessageContentComplex[] | undefined> {\n if (this.graphRunnable == null) {\n throw new Error(\n 'Run not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n if (!this.Graph) {\n throw new Error(\n 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'\n );\n }\n\n this.Graph.resetValues(streamOptions?.keepContent);\n\n /** Custom event callback to intercept and handle custom events */\n const customEventCallback = this.createCustomEventCallback();\n\n // IMPORTANT: snapshot the ORIGINAL caller-provided callbacks once and\n // reuse that snapshot on every invocation. Previously we read\n // `config.callbacks` on each call and concat'd onto it — but the same\n // `config` object is passed back to processStream() on HITL resume, so\n // the previous run's appended callbacks became the new \"base\" and we\n // ended up stacking duplicate stream + custom handlers on every cycle.\n // That produced N-times duplicated message_delta events on the SSE\n // stream (character-interleaved text in the UI after tool approval).\n if (!this._originalCallbacksSnapshot) {\n this._originalCallbacksSnapshot =\n (config.callbacks as t.ProvidedCallbacks) ?? [];\n }\n const baseCallbacks = this._originalCallbacksSnapshot;\n const streamCallbacks = streamOptions?.callbacks\n ? this.getCallbacks(streamOptions.callbacks)\n : [];\n\n const customHandler = BaseCallbackHandler.fromMethods({\n [Callback.CUSTOM_EVENT]: customEventCallback,\n });\n customHandler.awaitHandlers = true;\n\n config.callbacks = baseCallbacks\n .concat(streamCallbacks)\n .concat(customHandler);\n\n const illumaSecretKey = process.env.ILLUMA_SECRET_KEY;\n const illumaPublicKey = process.env.ILLUMA_PUBLIC_KEY;\n const illumaBaseUrl = process.env.ILLUMA_BASE_URL;\n\n if (\n isPresent(illumaSecretKey) &&\n isPresent(illumaPublicKey) &&\n isPresent(illumaBaseUrl)\n ) {\n try {\n const userId = config.configurable?.user_id;\n const sessionId = config.configurable?.thread_id;\n const handler = new ObservabilityCallbackHandler({\n clientOptions: {\n secretKey: illumaSecretKey!,\n publicKey: illumaPublicKey!,\n baseUrl: illumaBaseUrl!,\n },\n userId,\n sessionId,\n metadata: {\n messageId: this.id,\n parentMessageId: config.configurable?.requestBody?.parentMessageId,\n },\n });\n config.callbacks = (\n (config.callbacks as t.ProvidedCallbacks) ?? []\n ).concat([handler]);\n } catch {\n // Gracefully skip if @illuma-ai/observability-node is not installed\n }\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, {\n run_id: this.id,\n });\n\n const stream = this.graphRunnable.streamEvents(inputs, config, {\n raiseError: true,\n /**\n * Prevent EventStreamCallbackHandler from processing custom events.\n * Custom events are already handled via our createCustomEventCallback()\n * which routes them through the handlerRegistry.\n * Without this flag, EventStreamCallbackHandler throws errors when\n * custom events are dispatched for run IDs not in its internal map\n * (due to timing issues in parallel execution or after run cleanup).\n */\n ignoreCustomEvent: true,\n });\n\n try {\n for await (const event of stream) {\n const { data, metadata, ...info } = event;\n\n const eventName: t.EventName = info.event;\n\n /** Skip custom events as they're handled by our callback */\n if (eventName === GraphEvents.ON_CUSTOM_EVENT) {\n continue;\n }\n\n const handler = this.handlerRegistry?.getHandler(eventName);\n if (handler) {\n await handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n } catch (e) {\n // GraphInterrupt is expected when interrupt() fires (HITL approval flow).\n // Exit gracefully so the host can check hasInterrupts() and resume.\n if (isGraphInterrupt(e)) {\n return undefined;\n }\n throw e;\n }\n\n /**\n * Break the reference chain that keeps heavy data alive via\n * LangGraph's internal `__pregel_scratchpad.currentTaskInput` →\n * `@langchain/core` `RunTree.extra[lc:child_config]` →\n * Node.js `AsyncLocalStorage` context captured by timers/promises.\n *\n * Without this, base64-encoded images/PDFs in message content remain\n * reachable from lingering `Timeout` handles until GC runs.\n */\n if (!this.skipCleanup) {\n if (\n (config.configurable as Record<string, unknown> | undefined) != null\n ) {\n for (const key of Object.getOwnPropertySymbols(config.configurable)) {\n const val = config.configurable[key as unknown as string] as\n | Record<string, unknown>\n | undefined;\n if (\n val != null &&\n typeof val === 'object' &&\n 'currentTaskInput' in val\n ) {\n (val as Record<string, unknown>).currentTaskInput = undefined;\n }\n delete config.configurable[key as unknown as string];\n }\n config.configurable = undefined;\n }\n config.callbacks = undefined;\n }\n\n const result = this.returnContent\n ? this.Graph.getContentParts()\n : undefined;\n\n if (!this.skipCleanup) {\n this.Graph.clearHeavyState();\n }\n return result;\n }\n\n private createSystemCallback<K extends keyof t.ClientCallbacks>(\n clientCallbacks: t.ClientCallbacks,\n key: K\n ): t.SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as t.SystemCallbacks[K];\n }\n\n /**\n * Checks whether the graph was interrupted (e.g., by HITL tool approval).\n * Call after processStream() returns to determine if the graph is waiting\n * for a resume via Command({ resume }).\n *\n * Requires a checkpointer to be configured — without one, interrupt state\n * is not persisted and this always returns false.\n */\n async hasInterrupts(config: Partial<RunnableConfig>): Promise<boolean> {\n if (!this.graphRunnable) {\n return false;\n }\n try {\n const state = await this.graphRunnable.getState(config);\n return state.tasks.some((task) => task.interrupts.length > 0) ?? false;\n } catch {\n return false;\n }\n }\n\n /**\n * Returns the interrupt values from the graph state.\n * Each interrupt's `value` contains the data passed to `interrupt()` by the node\n * (e.g., a ToolApprovalRequest for HITL).\n */\n async getInterruptValues(\n config: Partial<RunnableConfig>\n ): Promise<unknown[]> {\n if (!this.graphRunnable) {\n return [];\n }\n try {\n const state = await this.graphRunnable.getState(config);\n return (\n state.tasks.flatMap((task) =>\n (task.interrupts ?? []).map((i) => i.value)\n ) ?? []\n );\n } catch {\n return [];\n }\n }\n\n getCallbacks(clientCallbacks: t.ClientCallbacks): t.SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_ERROR\n ),\n [Callback.TOOL_START]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_START\n ),\n [Callback.TOOL_END]: this.createSystemCallback(\n clientCallbacks,\n Callback.TOOL_END\n ),\n };\n }\n\n async generateTitle({\n provider,\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n titleMethod = TitleMethod.COMPLETION,\n titlePromptTemplate,\n }: t.RunTitleOptions): Promise<{ language?: string; title?: string }> {\n const titleSecretKey = process.env.ILLUMA_SECRET_KEY;\n const titlePublicKey = process.env.ILLUMA_PUBLIC_KEY;\n const titleBaseUrl = process.env.ILLUMA_BASE_URL;\n\n if (\n chainOptions != null &&\n isPresent(titleSecretKey) &&\n isPresent(titlePublicKey) &&\n isPresent(titleBaseUrl)\n ) {\n try {\n const userId = chainOptions.configurable?.user_id;\n const sessionId = chainOptions.configurable?.thread_id;\n const handler = new ObservabilityCallbackHandler({\n clientOptions: {\n secretKey: titleSecretKey!,\n publicKey: titlePublicKey!,\n baseUrl: titleBaseUrl!,\n },\n userId,\n sessionId,\n metadata: {\n messageId: 'title-' + this.id,\n },\n });\n chainOptions.callbacks = (\n (chainOptions.callbacks as t.ProvidedCallbacks) ?? []\n ).concat([handler]);\n } catch {\n // Gracefully skip if @illuma-ai/observability-node is not installed\n }\n }\n\n const convoTemplate = PromptTemplate.fromTemplate(\n titlePromptTemplate ?? 'User: {input}\\nAI: {output}'\n );\n\n const response = contentParts\n .map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n })\n .join('\\n');\n\n const model = this.Graph?.getNewModel({\n provider,\n clientOptions,\n });\n if (!model) {\n return { language: '', title: '' };\n }\n if (\n isOpenAILike(provider) &&\n (model instanceof ChatOpenAI || model instanceof AzureChatOpenAI)\n ) {\n model.temperature = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.temperature as number;\n model.topP = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.topP as number;\n model.frequencyPenalty = (\n clientOptions as t.OpenAIClientOptions | undefined\n )?.frequencyPenalty as number;\n model.presencePenalty = (\n clientOptions as t.OpenAIClientOptions | undefined\n )?.presencePenalty as number;\n model.n = (clientOptions as t.OpenAIClientOptions | undefined)\n ?.n as number;\n }\n\n const convoToTitleInput = new RunnableLambda({\n func: (\n promptValue: StringPromptValue\n ): { convo: string; inputText: string; skipLanguage?: boolean } => ({\n convo: promptValue.value,\n inputText,\n skipLanguage,\n }),\n }).withConfig({ runName: 'ConvoTransform' });\n\n const titleChain =\n titleMethod === TitleMethod.COMPLETION\n ? await createCompletionTitleRunnable(model, titlePrompt)\n : await createTitleRunnable(model, titlePrompt);\n\n /** Pipes `convoTemplate` -> `transformer` -> `titleChain` */\n const fullChain = convoTemplate\n .withConfig({ runName: 'ConvoTemplate' })\n .pipe(convoToTitleInput)\n .pipe(titleChain)\n .withConfig({ runName: 'TitleChain' });\n\n const invokeConfig = Object.assign({}, chainOptions, {\n run_id: this.id,\n runId: this.id,\n });\n\n try {\n return await fullChain.invoke(\n { input: inputText, output: response },\n invokeConfig\n );\n } catch (_e) {\n // Fallback: strip callbacks to avoid EventStream tracer errors in certain environments\n // But preserve observability handler if it exists\n const observabilityHandler = (\n invokeConfig.callbacks as t.ProvidedCallbacks\n )?.find((cb) => cb instanceof ObservabilityCallbackHandler);\n const { callbacks: _cb, ...rest } = invokeConfig;\n const safeConfig = Object.assign({}, rest, {\n callbacks: observabilityHandler ? [observabilityHandler] : [],\n });\n return await fullChain.invoke(\n { input: inputText, output: response },\n safeConfig as Partial<RunnableConfig>\n );\n }\n }\n}\n"],"names":["HandlerRegistry","StandardGraph","MultiAgentGraph","createTokenCounter","encodingForModel","BaseCallbackHandler","Callback","isPresent","ObservabilityCallbackHandler","GraphEvents","isGraphInterrupt","TitleMethod","PromptTemplate","isOpenAILike","ChatOpenAI","AzureChatOpenAI","RunnableLambda","createCompletionTitleRunnable","createTitleRunnable"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AA2BO,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IACxC,QAAQ;IACR,UAAU;IACV,WAAW;IACX,WAAW;IACX,eAAe;IACf,gBAAgB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AC,EAAE,EAAE,IAAI,EAAE;gBACzC,SAAS,EAAE,oBAAoB,GAAG,CAAC,oBAAoB,CAAC,GAAG,EAAE;AAC9D,aAAA,CAAC;AACF,YAAA,OAAO,MAAM,SAAS,CAAC,MAAM,CAC3B,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,EACtC,UAAqC,CACtC;QACH;IACF;AACD;;;;;"}
@@ -561,7 +561,7 @@ function createContentAggregator() {
561
561
  const messageDelta = data;
562
562
  const runStep = stepMap.get(messageDelta.id);
563
563
  if (!runStep) {
564
- console.warn('No run step or runId found for message delta event');
564
+ // Expected in handoff subgraphs reasoning/message deltas can arrive before ON_RUN_STEP
565
565
  return;
566
566
  }
567
567
  if (messageDelta.delta.content) {
@@ -583,7 +583,7 @@ function createContentAggregator() {
583
583
  const reasoningDelta = data;
584
584
  const runStep = stepMap.get(reasoningDelta.id);
585
585
  if (!runStep) {
586
- console.warn('No run step or runId found for reasoning delta event');
586
+ // Expected in handoff subgraphs reasoning deltas arrive before ON_RUN_STEP
587
587
  return;
588
588
  }
589
589
  if (reasoningDelta.delta.content) {
@@ -601,7 +601,7 @@ function createContentAggregator() {
601
601
  }
602
602
  const runStep = stepMap.get(runStepDelta.id);
603
603
  if (!runStep) {
604
- console.warn('No run step or runId found for run step delta event');
604
+ // Expected in handoff subgraphs step deltas can arrive before ON_RUN_STEP
605
605
  return;
606
606
  }
607
607
  if (runStepDelta.delta.type === _enum.StepTypes.TOOL_CALLS &&
@@ -642,7 +642,7 @@ function createContentAggregator() {
642
642
  }
643
643
  const runStep = stepMap.get(stepId);
644
644
  if (!runStep) {
645
- console.warn('No run step or runId found for completed tool call event');
645
+ // Expected in handoff subgraphs completion can arrive for untracked steps
646
646
  return;
647
647
  }
648
648
  const contentPart = {