@morsehq-dev/sdk 0.4.0-rc.1

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 (161) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +294 -0
  3. package/dist/anthropic/index.cjs +39 -0
  4. package/dist/anthropic/index.cjs.map +1 -0
  5. package/dist/anthropic/index.d.cts +213 -0
  6. package/dist/anthropic/index.d.ts +213 -0
  7. package/dist/anthropic/index.js +6 -0
  8. package/dist/anthropic/index.js.map +1 -0
  9. package/dist/anthropic-agent-sdk/index.cjs +744 -0
  10. package/dist/anthropic-agent-sdk/index.cjs.map +1 -0
  11. package/dist/anthropic-agent-sdk/index.d.cts +371 -0
  12. package/dist/anthropic-agent-sdk/index.d.ts +371 -0
  13. package/dist/anthropic-agent-sdk/index.js +735 -0
  14. package/dist/anthropic-agent-sdk/index.js.map +1 -0
  15. package/dist/browser/anthropic/index.cjs +39 -0
  16. package/dist/browser/anthropic/index.cjs.map +1 -0
  17. package/dist/browser/anthropic/index.js +6 -0
  18. package/dist/browser/anthropic/index.js.map +1 -0
  19. package/dist/browser/anthropic-agent-sdk/index.cjs +744 -0
  20. package/dist/browser/anthropic-agent-sdk/index.cjs.map +1 -0
  21. package/dist/browser/anthropic-agent-sdk/index.js +735 -0
  22. package/dist/browser/anthropic-agent-sdk/index.js.map +1 -0
  23. package/dist/browser/chunk-3643DC7K.cjs +365 -0
  24. package/dist/browser/chunk-3643DC7K.cjs.map +1 -0
  25. package/dist/browser/chunk-4FALQMOZ.js +232 -0
  26. package/dist/browser/chunk-4FALQMOZ.js.map +1 -0
  27. package/dist/browser/chunk-5J2QBK75.js +884 -0
  28. package/dist/browser/chunk-5J2QBK75.js.map +1 -0
  29. package/dist/browser/chunk-7MSXWGAH.js +53 -0
  30. package/dist/browser/chunk-7MSXWGAH.js.map +1 -0
  31. package/dist/browser/chunk-A24L5N5N.js +596 -0
  32. package/dist/browser/chunk-A24L5N5N.js.map +1 -0
  33. package/dist/browser/chunk-B7DEUDP6.cjs +177 -0
  34. package/dist/browser/chunk-B7DEUDP6.cjs.map +1 -0
  35. package/dist/browser/chunk-F6CJACNH.cjs +604 -0
  36. package/dist/browser/chunk-F6CJACNH.cjs.map +1 -0
  37. package/dist/browser/chunk-JBOYFQSB.js +412 -0
  38. package/dist/browser/chunk-JBOYFQSB.js.map +1 -0
  39. package/dist/browser/chunk-KRBADE6R.cjs +183 -0
  40. package/dist/browser/chunk-KRBADE6R.cjs.map +1 -0
  41. package/dist/browser/chunk-MCJYZH6W.cjs +415 -0
  42. package/dist/browser/chunk-MCJYZH6W.cjs.map +1 -0
  43. package/dist/browser/chunk-NQG2IAQS.js +178 -0
  44. package/dist/browser/chunk-NQG2IAQS.js.map +1 -0
  45. package/dist/browser/chunk-O4HG3OSK.cjs +929 -0
  46. package/dist/browser/chunk-O4HG3OSK.cjs.map +1 -0
  47. package/dist/browser/chunk-QWRQJO57.js +363 -0
  48. package/dist/browser/chunk-QWRQJO57.js.map +1 -0
  49. package/dist/browser/chunk-SWQOPFE4.cjs +234 -0
  50. package/dist/browser/chunk-SWQOPFE4.cjs.map +1 -0
  51. package/dist/browser/chunk-TYDG747E.js +171 -0
  52. package/dist/browser/chunk-TYDG747E.js.map +1 -0
  53. package/dist/browser/chunk-TZRSDDFP.cjs +56 -0
  54. package/dist/browser/chunk-TZRSDDFP.cjs.map +1 -0
  55. package/dist/browser/index.cjs +587 -0
  56. package/dist/browser/index.cjs.map +1 -0
  57. package/dist/browser/index.js +522 -0
  58. package/dist/browser/index.js.map +1 -0
  59. package/dist/browser/integrations/pino.cjs +89 -0
  60. package/dist/browser/integrations/pino.cjs.map +1 -0
  61. package/dist/browser/integrations/pino.js +86 -0
  62. package/dist/browser/integrations/pino.js.map +1 -0
  63. package/dist/browser/langchain/index.cjs +535 -0
  64. package/dist/browser/langchain/index.cjs.map +1 -0
  65. package/dist/browser/langchain/index.js +528 -0
  66. package/dist/browser/langchain/index.js.map +1 -0
  67. package/dist/browser/langgraph/index.cjs +377 -0
  68. package/dist/browser/langgraph/index.cjs.map +1 -0
  69. package/dist/browser/langgraph/index.js +371 -0
  70. package/dist/browser/langgraph/index.js.map +1 -0
  71. package/dist/browser/openai/index.cjs +125 -0
  72. package/dist/browser/openai/index.cjs.map +1 -0
  73. package/dist/browser/openai/index.js +122 -0
  74. package/dist/browser/openai/index.js.map +1 -0
  75. package/dist/browser/openai-agents/index.cjs +689 -0
  76. package/dist/browser/openai-agents/index.cjs.map +1 -0
  77. package/dist/browser/openai-agents/index.js +678 -0
  78. package/dist/browser/openai-agents/index.js.map +1 -0
  79. package/dist/browser/vercel-ai/index.cjs +233 -0
  80. package/dist/browser/vercel-ai/index.cjs.map +1 -0
  81. package/dist/browser/vercel-ai/index.js +231 -0
  82. package/dist/browser/vercel-ai/index.js.map +1 -0
  83. package/dist/chunk-4R4SHGOK.js +363 -0
  84. package/dist/chunk-4R4SHGOK.js.map +1 -0
  85. package/dist/chunk-7EO7MQBA.cjs +183 -0
  86. package/dist/chunk-7EO7MQBA.cjs.map +1 -0
  87. package/dist/chunk-7YCENA54.cjs +604 -0
  88. package/dist/chunk-7YCENA54.cjs.map +1 -0
  89. package/dist/chunk-CKFOGDUF.js +596 -0
  90. package/dist/chunk-CKFOGDUF.js.map +1 -0
  91. package/dist/chunk-FJUNILZT.cjs +1324 -0
  92. package/dist/chunk-FJUNILZT.cjs.map +1 -0
  93. package/dist/chunk-HDAFUKQ3.js +171 -0
  94. package/dist/chunk-HDAFUKQ3.js.map +1 -0
  95. package/dist/chunk-KBWPNIH4.cjs +234 -0
  96. package/dist/chunk-KBWPNIH4.cjs.map +1 -0
  97. package/dist/chunk-KJEO52QS.cjs +365 -0
  98. package/dist/chunk-KJEO52QS.cjs.map +1 -0
  99. package/dist/chunk-KZBCOZIQ.cjs +177 -0
  100. package/dist/chunk-KZBCOZIQ.cjs.map +1 -0
  101. package/dist/chunk-ME5JALGT.js +53 -0
  102. package/dist/chunk-ME5JALGT.js.map +1 -0
  103. package/dist/chunk-PVHDEPRE.cjs +56 -0
  104. package/dist/chunk-PVHDEPRE.cjs.map +1 -0
  105. package/dist/chunk-RTL23YOQ.js +178 -0
  106. package/dist/chunk-RTL23YOQ.js.map +1 -0
  107. package/dist/chunk-TQWI4UYO.js +1277 -0
  108. package/dist/chunk-TQWI4UYO.js.map +1 -0
  109. package/dist/chunk-VXDBDPDR.cjs +415 -0
  110. package/dist/chunk-VXDBDPDR.cjs.map +1 -0
  111. package/dist/chunk-XTKMUJWI.js +232 -0
  112. package/dist/chunk-XTKMUJWI.js.map +1 -0
  113. package/dist/chunk-ZKUGOWER.js +412 -0
  114. package/dist/chunk-ZKUGOWER.js.map +1 -0
  115. package/dist/index.cjs +843 -0
  116. package/dist/index.cjs.map +1 -0
  117. package/dist/index.d.cts +464 -0
  118. package/dist/index.d.ts +464 -0
  119. package/dist/index.js +778 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/integrations/pino.cjs +89 -0
  122. package/dist/integrations/pino.cjs.map +1 -0
  123. package/dist/integrations/pino.d.cts +65 -0
  124. package/dist/integrations/pino.d.ts +65 -0
  125. package/dist/integrations/pino.js +86 -0
  126. package/dist/integrations/pino.js.map +1 -0
  127. package/dist/langchain/index.cjs +535 -0
  128. package/dist/langchain/index.cjs.map +1 -0
  129. package/dist/langchain/index.d.cts +265 -0
  130. package/dist/langchain/index.d.ts +265 -0
  131. package/dist/langchain/index.js +528 -0
  132. package/dist/langchain/index.js.map +1 -0
  133. package/dist/langgraph/index.cjs +377 -0
  134. package/dist/langgraph/index.cjs.map +1 -0
  135. package/dist/langgraph/index.d.cts +324 -0
  136. package/dist/langgraph/index.d.ts +324 -0
  137. package/dist/langgraph/index.js +371 -0
  138. package/dist/langgraph/index.js.map +1 -0
  139. package/dist/openai/index.cjs +125 -0
  140. package/dist/openai/index.cjs.map +1 -0
  141. package/dist/openai/index.d.cts +136 -0
  142. package/dist/openai/index.d.ts +136 -0
  143. package/dist/openai/index.js +122 -0
  144. package/dist/openai/index.js.map +1 -0
  145. package/dist/openai-agents/index.cjs +689 -0
  146. package/dist/openai-agents/index.cjs.map +1 -0
  147. package/dist/openai-agents/index.d.cts +502 -0
  148. package/dist/openai-agents/index.d.ts +502 -0
  149. package/dist/openai-agents/index.js +678 -0
  150. package/dist/openai-agents/index.js.map +1 -0
  151. package/dist/spans-DZtMuBvc.d.cts +73 -0
  152. package/dist/spans-DZtMuBvc.d.ts +73 -0
  153. package/dist/tracing-BYAqjT5Q.d.cts +114 -0
  154. package/dist/tracing-rz9cWQ8d.d.ts +114 -0
  155. package/dist/vercel-ai/index.cjs +233 -0
  156. package/dist/vercel-ai/index.cjs.map +1 -0
  157. package/dist/vercel-ai/index.d.cts +93 -0
  158. package/dist/vercel-ai/index.d.ts +93 -0
  159. package/dist/vercel-ai/index.js +231 -0
  160. package/dist/vercel-ai/index.js.map +1 -0
  161. package/package.json +182 -0
@@ -0,0 +1,324 @@
1
+ import { S as SpanHandle } from '../tracing-BYAqjT5Q.cjs';
2
+ import '../spans-DZtMuBvc.cjs';
3
+
4
+ /**
5
+ * Structural types for `@langchain/langgraph` (langgraphjs) + `@langchain/core`
6
+ * callback handlers.
7
+ *
8
+ * No hard import — both are optional peer deps. We mirror the
9
+ * `BaseCallbackHandler` method surface our `MorseCallbackHandler`
10
+ * needs to extend (or duck-type when the base class isn't installed).
11
+ */
12
+ /** Subset of `Serialized` from `@langchain/core/load/serializable`. */
13
+ interface SerializedLike {
14
+ id?: string[];
15
+ name?: string;
16
+ kwargs?: Record<string, unknown>;
17
+ [k: string]: unknown;
18
+ }
19
+ /** Subset of `LLMResult` from `@langchain/core/outputs`. */
20
+ interface LLMResultLike {
21
+ generations?: Array<Array<{
22
+ text?: string;
23
+ [k: string]: unknown;
24
+ }>>;
25
+ llmOutput?: {
26
+ tokenUsage?: {
27
+ promptTokens?: number;
28
+ completionTokens?: number;
29
+ totalTokens?: number;
30
+ [k: string]: unknown;
31
+ };
32
+ [k: string]: unknown;
33
+ };
34
+ [k: string]: unknown;
35
+ }
36
+ /** Subset of `BaseMessage` shape passed to `handleChatModelStart`. */
37
+ interface BaseMessageLike {
38
+ _getType?(): string;
39
+ content?: string | unknown[];
40
+ [k: string]: unknown;
41
+ }
42
+ /**
43
+ * The methods we override on `BaseCallbackHandler`. We list them with
44
+ * loose signatures because the real base class is conditionally imported.
45
+ */
46
+ interface CallbackHandlerSurface {
47
+ handleChainStart?(chain: SerializedLike, inputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): void | Promise<void>;
48
+ handleChainEnd?(outputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], kwargs?: Record<string, unknown>): void | Promise<void>;
49
+ handleChainError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
50
+ handleLLMStart?(llm: SerializedLike, prompts: string[], runId: string, parentRunId?: string, extra?: Record<string, unknown>): void | Promise<void>;
51
+ handleChatModelStart?(llm: SerializedLike, messages: BaseMessageLike[][], runId: string, parentRunId?: string, extra?: Record<string, unknown>): void | Promise<void>;
52
+ handleLLMEnd?(output: LLMResultLike, runId: string, parentRunId?: string): void | Promise<void>;
53
+ handleLLMError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
54
+ handleLLMNewToken?(token: string, idx: {
55
+ prompt: number;
56
+ completion: number;
57
+ }, runId: string, parentRunId?: string, tags?: string[]): void | Promise<void>;
58
+ handleToolStart?(tool: SerializedLike, input: string, runId: string, parentRunId?: string): void | Promise<void>;
59
+ handleToolEnd?(output: string, runId: string, parentRunId?: string): void | Promise<void>;
60
+ handleToolError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
61
+ }
62
+ /** Module-shape probe for `@langchain/core/callbacks/base`. */
63
+ interface LangChainCoreCallbacksModuleLike {
64
+ BaseCallbackHandler?: {
65
+ new (...args: unknown[]): CallbackHandlerSurface & {
66
+ name: string;
67
+ };
68
+ prototype: CallbackHandlerSurface;
69
+ };
70
+ [k: string]: unknown;
71
+ }
72
+
73
+ /**
74
+ * `@morsehq-dev/sdk/langgraph` — `MorseCallbackHandler` (W2-L.1).
75
+ *
76
+ * The handler is registered by LangGraph users via:
77
+ *
78
+ * ```ts
79
+ * import { MorseCallbackHandler } from "@morsehq-dev/sdk/langgraph";
80
+ * await app.invoke(input, { callbacks: [new MorseCallbackHandler()] });
81
+ * ```
82
+ *
83
+ * LangGraph type-checks handlers structurally (by method presence on the
84
+ * instance), not via `instanceof BaseCallbackHandler`. We therefore:
85
+ *
86
+ * 1. Always export a duck-typed class implementing `CallbackHandlerSurface`
87
+ * — works without `@langchain/core` installed.
88
+ * 2. Provide `getCallbackHandlerClass()` for users who rely on
89
+ * `instanceof BaseCallbackHandler` — it dynamically loads
90
+ * `@langchain/core/callbacks/base` (optional peer dep) and returns a
91
+ * subclass whose prototype chain includes the real base class.
92
+ *
93
+ * Scope of THIS file (W2-L.1): chain spans only. `handleChainStart` opens
94
+ * an `agent` span (the LangGraph chain IS the agent run); `handleChainEnd`
95
+ * / `handleChainError` closes it. Nested chains nest via the per-runId
96
+ * span map — LangGraph guarantees well-ordered start/end pairs so the
97
+ * tracing-state span stack handles `parent_span_id` linkage for us.
98
+ *
99
+ * `handleLLMStart/End`, `handleToolStart/End`, and streaming callbacks
100
+ * are implemented in W2-L.2 / W2-L.3 as separate methods on the same
101
+ * class (composed in Wave 3-L).
102
+ */
103
+
104
+ interface MorseCallbackHandlerOptions {
105
+ /**
106
+ * Override the agent name on the outer chain span. Falls back, in order,
107
+ * to: `runName` (LangGraph-supplied per-invocation name), `chain.name`
108
+ * (the serialized class name), then `"langgraph-agent"`.
109
+ */
110
+ defaultAgentName?: string;
111
+ }
112
+ /**
113
+ * Duck-typed LangGraph `BaseCallbackHandler`. Implements `CallbackHandlerSurface`
114
+ * with chain-start / chain-end / chain-error wired to the Morse trace.
115
+ *
116
+ * LangGraph routes callbacks to instance methods by name; it does NOT require
117
+ * an `instanceof` check against the real `BaseCallbackHandler`. Users who
118
+ * need that (rare; `instanceof` checks in custom middleware) should obtain
119
+ * the class via `getCallbackHandlerClass()` instead.
120
+ */
121
+ declare class MorseCallbackHandler$1 implements CallbackHandlerSurface {
122
+ /** Required by LangGraph's `BaseCallbackHandler` interface. */
123
+ readonly name: string;
124
+ private readonly defaultAgentName;
125
+ /** Open chain spans, keyed by LangGraph `runId`. */
126
+ private readonly chainSpans;
127
+ constructor(options?: MorseCallbackHandlerOptions);
128
+ handleChainStart(chain: SerializedLike, _inputs: Record<string, unknown>, runId: string, _parentRunId?: string, _tags?: string[], _metadata?: Record<string, unknown>, _runType?: string, runName?: string): void;
129
+ handleChainEnd(_outputs: Record<string, unknown>, runId: string, parentRunId?: string): void;
130
+ handleChainError(err: unknown, runId: string, parentRunId?: string): void;
131
+ /** @internal */
132
+ _setSpan(runId: string, handle: SpanHandle): void;
133
+ /** @internal */
134
+ _takeSpan(runId: string): SpanHandle | undefined;
135
+ /** @internal */
136
+ _resolveAgentName(chain: SerializedLike | undefined, runName: string | undefined): string;
137
+ }
138
+ /**
139
+ * Best-effort: if `@langchain/core/callbacks/base` is installed at runtime,
140
+ * return an `MorseCallbackHandler`-shaped class whose prototype chain
141
+ * includes the real `BaseCallbackHandler` (for users relying on
142
+ * `instanceof BaseCallbackHandler` checks). Otherwise return the
143
+ * duck-typed `MorseCallbackHandler` defined above.
144
+ */
145
+ declare function getCallbackHandlerClass(): Promise<typeof MorseCallbackHandler$1>;
146
+
147
+ /**
148
+ * `langgraph/llm-and-tool-callbacks.ts` — W2-L.2.
149
+ *
150
+ * LangGraph JS callback handler fragment that owns the llm + tool surface
151
+ * of `BaseCallbackHandler`. The class shell (constructor + chain methods)
152
+ * is owned by W2-L.1 (`callback-handler.ts`); streaming + checkpoint by
153
+ * W2-L.3 (`streaming-and-checkpoint.ts`). All three are composed together
154
+ * by W3-L (`langgraph/index.ts`).
155
+ *
156
+ * To stay file-disjoint across the three W2-L tasks, each fragment exports
157
+ * a `createX` factory that returns plain method implementations operating
158
+ * on a shared mutable `LangGraphRunState`. The host class threads its
159
+ * `state` reference through each factory in its constructor and copies the
160
+ * methods onto its prototype/instance.
161
+ *
162
+ * Logical span mapping (per design.md §LangGraph JS):
163
+ * handleLLMStart / handleChatModelStart → open `type=llm` span
164
+ * handleLLMEnd / handleLLMError → close active llm span
165
+ * handleToolStart → open `type=tool` span
166
+ * handleToolEnd / handleToolError → close active tool span
167
+ *
168
+ * MHQ-750: `handleChatModelStart` also decomposes the flattened messages
169
+ * (+ any bound `tools`) into `context_segments` via the shared
170
+ * `_agent-sdk-common/context-decomposer`, mirroring the Python adapter's
171
+ * `on_chat_model_start` (`adapters/langgraph.py`), which calls the same
172
+ * `decompose_openai_messages` for the same reason: LangGraph JS messages
173
+ * are `@langchain/core` `BaseMessage`s, which decompose cleanly under the
174
+ * OpenAI-chat-format decomposer's `_getType()`/`.type` duck-typing.
175
+ *
176
+ * All bodies are wrapped in `absorbErrorsSync` so telemetry can never
177
+ * throw at the host LangGraph runtime.
178
+ */
179
+
180
+ /**
181
+ * Shared per-instance state for the composed `MorseCallbackHandler`.
182
+ * Owned/instantiated by W3-L (`langgraph/index.ts`); each W2-L fragment
183
+ * mutates it in place.
184
+ */
185
+ interface LangGraphRunState {
186
+ /** Map of runId → SpanHandle for open llm spans. */
187
+ llmSpans: Map<string, SpanHandle>;
188
+ /** Map of runId → SpanHandle for open tool spans. */
189
+ toolSpans: Map<string, SpanHandle>;
190
+ /** Map of runId → SpanHandle for open chain spans (populated by W2-L.1). */
191
+ chainSpans: Map<string, SpanHandle>;
192
+ }
193
+ interface LlmAndToolHandlerOptions {
194
+ /** Max bytes for tool input summary. Falls through to module default if unset. */
195
+ toolInputMaxBytes?: number;
196
+ /** Max bytes for tool output summary. Falls through to module default if unset. */
197
+ toolOutputMaxBytes?: number;
198
+ }
199
+ /** Convenient public type alias for the slice of methods this file owns. */
200
+ type LlmAndToolHandlers = Pick<CallbackHandlerSurface, "handleLLMStart" | "handleChatModelStart" | "handleLLMEnd" | "handleLLMError" | "handleToolStart" | "handleToolEnd" | "handleToolError">;
201
+ /**
202
+ * Build the llm + tool slice of an `MorseCallbackHandler`. The
203
+ * returned object can be merged onto the host class instance (W3-L); each
204
+ * method mutates `state` in place so all three fragments share the same
205
+ * `runId → SpanHandle` maps.
206
+ *
207
+ * Every method body is wrapped in `absorbErrorsSync` — telemetry never
208
+ * throws into the LangGraph runtime.
209
+ */
210
+ declare function createLlmAndToolHandlers(state: LangGraphRunState, options?: LlmAndToolHandlerOptions): LlmAndToolHandlers;
211
+
212
+ /**
213
+ * `langgraph/streaming-and-checkpoint.ts` — Phase 13 W2-L.3.
214
+ *
215
+ * Implements streaming-token aggregation for LangGraph JS. Per the design
216
+ * doc, streaming tokens fire `handleLLMNewToken` at very high frequency —
217
+ * we do NOT emit a span per token (would explode volume). Instead we
218
+ * aggregate them onto the parent llm span's `metadata.tokens_streamed`
219
+ * counter so downstream consumers can observe streaming activity without
220
+ * the cardinality blowup.
221
+ *
222
+ * Checkpoint hooks (PostgresSaver / SqliteSaver / MemorySaver) are
223
+ * deferred to v1.5 per the design doc. The surface is left empty here;
224
+ * W3-L composes this module with the others and may add checkpoint
225
+ * instrumentation in a follow-up wave without touching the wiring.
226
+ *
227
+ * All wrappers route through `absorbErrorsSync` — telemetry MUST NOT
228
+ * throw at the caller.
229
+ */
230
+
231
+ /** State carried across streaming-token events for an in-flight chain run. */
232
+ interface StreamingState {
233
+ /** Map of `runId` → SpanHandle (the in-flight llm span W2-L.2 opened). */
234
+ llmSpans: Map<string, SpanHandle>;
235
+ }
236
+ interface StreamingHandlerOptions {
237
+ /**
238
+ * If true, emit a single `tokens_streamed` metadata count on the parent
239
+ * llm span; default true. If false, the handler is a no-op (useful when
240
+ * the host already tracks streaming elsewhere or wants to minimise
241
+ * mutation under hot streaming loops).
242
+ */
243
+ trackStreamingTokens?: boolean;
244
+ }
245
+ /**
246
+ * Returns the streaming-related callback method implementations.
247
+ *
248
+ * Currently exposes only `handleLLMNewToken`. Checkpoint methods
249
+ * (`handleCheckpointStart` / `handleCheckpointEnd`) are not on the
250
+ * `CallbackHandlerSurface` for v1 — they will land in v1.5 when we wire
251
+ * the PostgresSaver/SqliteSaver hooks. Keeping this module in place
252
+ * means W3-L's composition does not change shape when those land.
253
+ */
254
+ declare function createStreamingHandlers(state: StreamingState, options?: StreamingHandlerOptions): Pick<CallbackHandlerSurface, "handleLLMNewToken">;
255
+
256
+ /**
257
+ * `@morsehq-dev/sdk/langgraph` — adapter for `@langchain/langgraph` (langgraphjs).
258
+ *
259
+ * W3-L: composes the three W2-L surfaces into a single
260
+ * `MorseCallbackHandler` class exposing the full
261
+ * `BaseCallbackHandler` shape LangGraph requires:
262
+ *
263
+ * - chain start/end/error — from W2-L.1 (`callback-handler.ts`)
264
+ * - llm + tool start/end/error — from W2-L.2 (`llm-and-tool-callbacks.ts`)
265
+ * - llm new-token streaming — from W2-L.3 (`streaming-and-checkpoint.ts`)
266
+ *
267
+ * Users get a single class to register with LangGraph:
268
+ *
269
+ * ```ts
270
+ * import { MorseCallbackHandler } from "@morsehq-dev/sdk/langgraph";
271
+ * await app.invoke(input, { callbacks: [new MorseCallbackHandler()] });
272
+ * ```
273
+ *
274
+ * The W2-L.1 base class keeps its own private map of open chain spans
275
+ * (since it predates the shared `LangGraphRunState`). To keep things
276
+ * coherent for advanced consumers reading `state`, the composed class
277
+ * overrides `handleChainStart` / `handleChainEnd` / `handleChainError`
278
+ * to ALSO maintain `state.chainSpans` in addition to calling the
279
+ * super-class method. The base-class behavior is unchanged.
280
+ */
281
+
282
+ /** Combined options for the composed handler. */
283
+ type ComposedMorseCallbackHandlerOptions = MorseCallbackHandlerOptions & LlmAndToolHandlerOptions & StreamingHandlerOptions;
284
+ /**
285
+ * Composed `MorseCallbackHandler` — the user-facing class.
286
+ *
287
+ * Inherits chain handlers from `BaseMorseCallbackHandler` (W2-L.1)
288
+ * and attaches the W2-L.2 llm/tool methods and W2-L.3 streaming method
289
+ * onto each instance. All four sources share a single
290
+ * `LangGraphRunState` so future cross-method correlation can read
291
+ * llm/tool/chain maps from one place.
292
+ */
293
+ declare class MorseCallbackHandler extends MorseCallbackHandler$1 implements CallbackHandlerSurface {
294
+ private readonly _state;
295
+ handleLLMStart: NonNullable<CallbackHandlerSurface["handleLLMStart"]>;
296
+ handleChatModelStart: NonNullable<CallbackHandlerSurface["handleChatModelStart"]>;
297
+ handleLLMEnd: NonNullable<CallbackHandlerSurface["handleLLMEnd"]>;
298
+ handleLLMError: NonNullable<CallbackHandlerSurface["handleLLMError"]>;
299
+ handleToolStart: NonNullable<CallbackHandlerSurface["handleToolStart"]>;
300
+ handleToolEnd: NonNullable<CallbackHandlerSurface["handleToolEnd"]>;
301
+ handleToolError: NonNullable<CallbackHandlerSurface["handleToolError"]>;
302
+ handleLLMNewToken: NonNullable<CallbackHandlerSurface["handleLLMNewToken"]>;
303
+ constructor(options?: ComposedMorseCallbackHandlerOptions);
304
+ /**
305
+ * The W2-L.1 base maintains its own private chain-span map. We
306
+ * mirror chain handles into the shared `LangGraphRunState` so future
307
+ * cross-fragment logic can correlate by runId. Never throws — the
308
+ * map mutation is in-process and the super method already absorbs
309
+ * errors via `absorbErrorsSync`.
310
+ */
311
+ handleChainStart(chain: SerializedLike, inputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): void;
312
+ handleChainEnd(outputs: Record<string, unknown>, runId: string, parentRunId?: string): void;
313
+ handleChainError(err: unknown, runId: string, parentRunId?: string): void;
314
+ /**
315
+ * Marker insertion for the shared chainSpans map. The W2-L.1 base
316
+ * holds the real `SpanHandle` privately; for the shared map we just
317
+ * need *presence* keyed by runId so downstream fragments can detect
318
+ * an in-flight chain. We stash a sentinel handle whose `end()` is a
319
+ * no-op — the base owns the real lifecycle.
320
+ */
321
+ private _shadowSetChainSpan;
322
+ }
323
+
324
+ export { type BaseMessageLike, MorseCallbackHandler$1 as BaseMorseCallbackHandler, type CallbackHandlerSurface, type ComposedMorseCallbackHandlerOptions, type LLMResultLike, type LangChainCoreCallbacksModuleLike, type LangGraphRunState, type LlmAndToolHandlerOptions, MorseCallbackHandler, type MorseCallbackHandlerOptions, type SerializedLike, type StreamingHandlerOptions, createLlmAndToolHandlers, createStreamingHandlers, getCallbackHandlerClass };
@@ -0,0 +1,324 @@
1
+ import { S as SpanHandle } from '../tracing-rz9cWQ8d.js';
2
+ import '../spans-DZtMuBvc.js';
3
+
4
+ /**
5
+ * Structural types for `@langchain/langgraph` (langgraphjs) + `@langchain/core`
6
+ * callback handlers.
7
+ *
8
+ * No hard import — both are optional peer deps. We mirror the
9
+ * `BaseCallbackHandler` method surface our `MorseCallbackHandler`
10
+ * needs to extend (or duck-type when the base class isn't installed).
11
+ */
12
+ /** Subset of `Serialized` from `@langchain/core/load/serializable`. */
13
+ interface SerializedLike {
14
+ id?: string[];
15
+ name?: string;
16
+ kwargs?: Record<string, unknown>;
17
+ [k: string]: unknown;
18
+ }
19
+ /** Subset of `LLMResult` from `@langchain/core/outputs`. */
20
+ interface LLMResultLike {
21
+ generations?: Array<Array<{
22
+ text?: string;
23
+ [k: string]: unknown;
24
+ }>>;
25
+ llmOutput?: {
26
+ tokenUsage?: {
27
+ promptTokens?: number;
28
+ completionTokens?: number;
29
+ totalTokens?: number;
30
+ [k: string]: unknown;
31
+ };
32
+ [k: string]: unknown;
33
+ };
34
+ [k: string]: unknown;
35
+ }
36
+ /** Subset of `BaseMessage` shape passed to `handleChatModelStart`. */
37
+ interface BaseMessageLike {
38
+ _getType?(): string;
39
+ content?: string | unknown[];
40
+ [k: string]: unknown;
41
+ }
42
+ /**
43
+ * The methods we override on `BaseCallbackHandler`. We list them with
44
+ * loose signatures because the real base class is conditionally imported.
45
+ */
46
+ interface CallbackHandlerSurface {
47
+ handleChainStart?(chain: SerializedLike, inputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): void | Promise<void>;
48
+ handleChainEnd?(outputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], kwargs?: Record<string, unknown>): void | Promise<void>;
49
+ handleChainError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
50
+ handleLLMStart?(llm: SerializedLike, prompts: string[], runId: string, parentRunId?: string, extra?: Record<string, unknown>): void | Promise<void>;
51
+ handleChatModelStart?(llm: SerializedLike, messages: BaseMessageLike[][], runId: string, parentRunId?: string, extra?: Record<string, unknown>): void | Promise<void>;
52
+ handleLLMEnd?(output: LLMResultLike, runId: string, parentRunId?: string): void | Promise<void>;
53
+ handleLLMError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
54
+ handleLLMNewToken?(token: string, idx: {
55
+ prompt: number;
56
+ completion: number;
57
+ }, runId: string, parentRunId?: string, tags?: string[]): void | Promise<void>;
58
+ handleToolStart?(tool: SerializedLike, input: string, runId: string, parentRunId?: string): void | Promise<void>;
59
+ handleToolEnd?(output: string, runId: string, parentRunId?: string): void | Promise<void>;
60
+ handleToolError?(err: unknown, runId: string, parentRunId?: string): void | Promise<void>;
61
+ }
62
+ /** Module-shape probe for `@langchain/core/callbacks/base`. */
63
+ interface LangChainCoreCallbacksModuleLike {
64
+ BaseCallbackHandler?: {
65
+ new (...args: unknown[]): CallbackHandlerSurface & {
66
+ name: string;
67
+ };
68
+ prototype: CallbackHandlerSurface;
69
+ };
70
+ [k: string]: unknown;
71
+ }
72
+
73
+ /**
74
+ * `@morsehq-dev/sdk/langgraph` — `MorseCallbackHandler` (W2-L.1).
75
+ *
76
+ * The handler is registered by LangGraph users via:
77
+ *
78
+ * ```ts
79
+ * import { MorseCallbackHandler } from "@morsehq-dev/sdk/langgraph";
80
+ * await app.invoke(input, { callbacks: [new MorseCallbackHandler()] });
81
+ * ```
82
+ *
83
+ * LangGraph type-checks handlers structurally (by method presence on the
84
+ * instance), not via `instanceof BaseCallbackHandler`. We therefore:
85
+ *
86
+ * 1. Always export a duck-typed class implementing `CallbackHandlerSurface`
87
+ * — works without `@langchain/core` installed.
88
+ * 2. Provide `getCallbackHandlerClass()` for users who rely on
89
+ * `instanceof BaseCallbackHandler` — it dynamically loads
90
+ * `@langchain/core/callbacks/base` (optional peer dep) and returns a
91
+ * subclass whose prototype chain includes the real base class.
92
+ *
93
+ * Scope of THIS file (W2-L.1): chain spans only. `handleChainStart` opens
94
+ * an `agent` span (the LangGraph chain IS the agent run); `handleChainEnd`
95
+ * / `handleChainError` closes it. Nested chains nest via the per-runId
96
+ * span map — LangGraph guarantees well-ordered start/end pairs so the
97
+ * tracing-state span stack handles `parent_span_id` linkage for us.
98
+ *
99
+ * `handleLLMStart/End`, `handleToolStart/End`, and streaming callbacks
100
+ * are implemented in W2-L.2 / W2-L.3 as separate methods on the same
101
+ * class (composed in Wave 3-L).
102
+ */
103
+
104
+ interface MorseCallbackHandlerOptions {
105
+ /**
106
+ * Override the agent name on the outer chain span. Falls back, in order,
107
+ * to: `runName` (LangGraph-supplied per-invocation name), `chain.name`
108
+ * (the serialized class name), then `"langgraph-agent"`.
109
+ */
110
+ defaultAgentName?: string;
111
+ }
112
+ /**
113
+ * Duck-typed LangGraph `BaseCallbackHandler`. Implements `CallbackHandlerSurface`
114
+ * with chain-start / chain-end / chain-error wired to the Morse trace.
115
+ *
116
+ * LangGraph routes callbacks to instance methods by name; it does NOT require
117
+ * an `instanceof` check against the real `BaseCallbackHandler`. Users who
118
+ * need that (rare; `instanceof` checks in custom middleware) should obtain
119
+ * the class via `getCallbackHandlerClass()` instead.
120
+ */
121
+ declare class MorseCallbackHandler$1 implements CallbackHandlerSurface {
122
+ /** Required by LangGraph's `BaseCallbackHandler` interface. */
123
+ readonly name: string;
124
+ private readonly defaultAgentName;
125
+ /** Open chain spans, keyed by LangGraph `runId`. */
126
+ private readonly chainSpans;
127
+ constructor(options?: MorseCallbackHandlerOptions);
128
+ handleChainStart(chain: SerializedLike, _inputs: Record<string, unknown>, runId: string, _parentRunId?: string, _tags?: string[], _metadata?: Record<string, unknown>, _runType?: string, runName?: string): void;
129
+ handleChainEnd(_outputs: Record<string, unknown>, runId: string, parentRunId?: string): void;
130
+ handleChainError(err: unknown, runId: string, parentRunId?: string): void;
131
+ /** @internal */
132
+ _setSpan(runId: string, handle: SpanHandle): void;
133
+ /** @internal */
134
+ _takeSpan(runId: string): SpanHandle | undefined;
135
+ /** @internal */
136
+ _resolveAgentName(chain: SerializedLike | undefined, runName: string | undefined): string;
137
+ }
138
+ /**
139
+ * Best-effort: if `@langchain/core/callbacks/base` is installed at runtime,
140
+ * return an `MorseCallbackHandler`-shaped class whose prototype chain
141
+ * includes the real `BaseCallbackHandler` (for users relying on
142
+ * `instanceof BaseCallbackHandler` checks). Otherwise return the
143
+ * duck-typed `MorseCallbackHandler` defined above.
144
+ */
145
+ declare function getCallbackHandlerClass(): Promise<typeof MorseCallbackHandler$1>;
146
+
147
+ /**
148
+ * `langgraph/llm-and-tool-callbacks.ts` — W2-L.2.
149
+ *
150
+ * LangGraph JS callback handler fragment that owns the llm + tool surface
151
+ * of `BaseCallbackHandler`. The class shell (constructor + chain methods)
152
+ * is owned by W2-L.1 (`callback-handler.ts`); streaming + checkpoint by
153
+ * W2-L.3 (`streaming-and-checkpoint.ts`). All three are composed together
154
+ * by W3-L (`langgraph/index.ts`).
155
+ *
156
+ * To stay file-disjoint across the three W2-L tasks, each fragment exports
157
+ * a `createX` factory that returns plain method implementations operating
158
+ * on a shared mutable `LangGraphRunState`. The host class threads its
159
+ * `state` reference through each factory in its constructor and copies the
160
+ * methods onto its prototype/instance.
161
+ *
162
+ * Logical span mapping (per design.md §LangGraph JS):
163
+ * handleLLMStart / handleChatModelStart → open `type=llm` span
164
+ * handleLLMEnd / handleLLMError → close active llm span
165
+ * handleToolStart → open `type=tool` span
166
+ * handleToolEnd / handleToolError → close active tool span
167
+ *
168
+ * MHQ-750: `handleChatModelStart` also decomposes the flattened messages
169
+ * (+ any bound `tools`) into `context_segments` via the shared
170
+ * `_agent-sdk-common/context-decomposer`, mirroring the Python adapter's
171
+ * `on_chat_model_start` (`adapters/langgraph.py`), which calls the same
172
+ * `decompose_openai_messages` for the same reason: LangGraph JS messages
173
+ * are `@langchain/core` `BaseMessage`s, which decompose cleanly under the
174
+ * OpenAI-chat-format decomposer's `_getType()`/`.type` duck-typing.
175
+ *
176
+ * All bodies are wrapped in `absorbErrorsSync` so telemetry can never
177
+ * throw at the host LangGraph runtime.
178
+ */
179
+
180
+ /**
181
+ * Shared per-instance state for the composed `MorseCallbackHandler`.
182
+ * Owned/instantiated by W3-L (`langgraph/index.ts`); each W2-L fragment
183
+ * mutates it in place.
184
+ */
185
+ interface LangGraphRunState {
186
+ /** Map of runId → SpanHandle for open llm spans. */
187
+ llmSpans: Map<string, SpanHandle>;
188
+ /** Map of runId → SpanHandle for open tool spans. */
189
+ toolSpans: Map<string, SpanHandle>;
190
+ /** Map of runId → SpanHandle for open chain spans (populated by W2-L.1). */
191
+ chainSpans: Map<string, SpanHandle>;
192
+ }
193
+ interface LlmAndToolHandlerOptions {
194
+ /** Max bytes for tool input summary. Falls through to module default if unset. */
195
+ toolInputMaxBytes?: number;
196
+ /** Max bytes for tool output summary. Falls through to module default if unset. */
197
+ toolOutputMaxBytes?: number;
198
+ }
199
+ /** Convenient public type alias for the slice of methods this file owns. */
200
+ type LlmAndToolHandlers = Pick<CallbackHandlerSurface, "handleLLMStart" | "handleChatModelStart" | "handleLLMEnd" | "handleLLMError" | "handleToolStart" | "handleToolEnd" | "handleToolError">;
201
+ /**
202
+ * Build the llm + tool slice of an `MorseCallbackHandler`. The
203
+ * returned object can be merged onto the host class instance (W3-L); each
204
+ * method mutates `state` in place so all three fragments share the same
205
+ * `runId → SpanHandle` maps.
206
+ *
207
+ * Every method body is wrapped in `absorbErrorsSync` — telemetry never
208
+ * throws into the LangGraph runtime.
209
+ */
210
+ declare function createLlmAndToolHandlers(state: LangGraphRunState, options?: LlmAndToolHandlerOptions): LlmAndToolHandlers;
211
+
212
+ /**
213
+ * `langgraph/streaming-and-checkpoint.ts` — Phase 13 W2-L.3.
214
+ *
215
+ * Implements streaming-token aggregation for LangGraph JS. Per the design
216
+ * doc, streaming tokens fire `handleLLMNewToken` at very high frequency —
217
+ * we do NOT emit a span per token (would explode volume). Instead we
218
+ * aggregate them onto the parent llm span's `metadata.tokens_streamed`
219
+ * counter so downstream consumers can observe streaming activity without
220
+ * the cardinality blowup.
221
+ *
222
+ * Checkpoint hooks (PostgresSaver / SqliteSaver / MemorySaver) are
223
+ * deferred to v1.5 per the design doc. The surface is left empty here;
224
+ * W3-L composes this module with the others and may add checkpoint
225
+ * instrumentation in a follow-up wave without touching the wiring.
226
+ *
227
+ * All wrappers route through `absorbErrorsSync` — telemetry MUST NOT
228
+ * throw at the caller.
229
+ */
230
+
231
+ /** State carried across streaming-token events for an in-flight chain run. */
232
+ interface StreamingState {
233
+ /** Map of `runId` → SpanHandle (the in-flight llm span W2-L.2 opened). */
234
+ llmSpans: Map<string, SpanHandle>;
235
+ }
236
+ interface StreamingHandlerOptions {
237
+ /**
238
+ * If true, emit a single `tokens_streamed` metadata count on the parent
239
+ * llm span; default true. If false, the handler is a no-op (useful when
240
+ * the host already tracks streaming elsewhere or wants to minimise
241
+ * mutation under hot streaming loops).
242
+ */
243
+ trackStreamingTokens?: boolean;
244
+ }
245
+ /**
246
+ * Returns the streaming-related callback method implementations.
247
+ *
248
+ * Currently exposes only `handleLLMNewToken`. Checkpoint methods
249
+ * (`handleCheckpointStart` / `handleCheckpointEnd`) are not on the
250
+ * `CallbackHandlerSurface` for v1 — they will land in v1.5 when we wire
251
+ * the PostgresSaver/SqliteSaver hooks. Keeping this module in place
252
+ * means W3-L's composition does not change shape when those land.
253
+ */
254
+ declare function createStreamingHandlers(state: StreamingState, options?: StreamingHandlerOptions): Pick<CallbackHandlerSurface, "handleLLMNewToken">;
255
+
256
+ /**
257
+ * `@morsehq-dev/sdk/langgraph` — adapter for `@langchain/langgraph` (langgraphjs).
258
+ *
259
+ * W3-L: composes the three W2-L surfaces into a single
260
+ * `MorseCallbackHandler` class exposing the full
261
+ * `BaseCallbackHandler` shape LangGraph requires:
262
+ *
263
+ * - chain start/end/error — from W2-L.1 (`callback-handler.ts`)
264
+ * - llm + tool start/end/error — from W2-L.2 (`llm-and-tool-callbacks.ts`)
265
+ * - llm new-token streaming — from W2-L.3 (`streaming-and-checkpoint.ts`)
266
+ *
267
+ * Users get a single class to register with LangGraph:
268
+ *
269
+ * ```ts
270
+ * import { MorseCallbackHandler } from "@morsehq-dev/sdk/langgraph";
271
+ * await app.invoke(input, { callbacks: [new MorseCallbackHandler()] });
272
+ * ```
273
+ *
274
+ * The W2-L.1 base class keeps its own private map of open chain spans
275
+ * (since it predates the shared `LangGraphRunState`). To keep things
276
+ * coherent for advanced consumers reading `state`, the composed class
277
+ * overrides `handleChainStart` / `handleChainEnd` / `handleChainError`
278
+ * to ALSO maintain `state.chainSpans` in addition to calling the
279
+ * super-class method. The base-class behavior is unchanged.
280
+ */
281
+
282
+ /** Combined options for the composed handler. */
283
+ type ComposedMorseCallbackHandlerOptions = MorseCallbackHandlerOptions & LlmAndToolHandlerOptions & StreamingHandlerOptions;
284
+ /**
285
+ * Composed `MorseCallbackHandler` — the user-facing class.
286
+ *
287
+ * Inherits chain handlers from `BaseMorseCallbackHandler` (W2-L.1)
288
+ * and attaches the W2-L.2 llm/tool methods and W2-L.3 streaming method
289
+ * onto each instance. All four sources share a single
290
+ * `LangGraphRunState` so future cross-method correlation can read
291
+ * llm/tool/chain maps from one place.
292
+ */
293
+ declare class MorseCallbackHandler extends MorseCallbackHandler$1 implements CallbackHandlerSurface {
294
+ private readonly _state;
295
+ handleLLMStart: NonNullable<CallbackHandlerSurface["handleLLMStart"]>;
296
+ handleChatModelStart: NonNullable<CallbackHandlerSurface["handleChatModelStart"]>;
297
+ handleLLMEnd: NonNullable<CallbackHandlerSurface["handleLLMEnd"]>;
298
+ handleLLMError: NonNullable<CallbackHandlerSurface["handleLLMError"]>;
299
+ handleToolStart: NonNullable<CallbackHandlerSurface["handleToolStart"]>;
300
+ handleToolEnd: NonNullable<CallbackHandlerSurface["handleToolEnd"]>;
301
+ handleToolError: NonNullable<CallbackHandlerSurface["handleToolError"]>;
302
+ handleLLMNewToken: NonNullable<CallbackHandlerSurface["handleLLMNewToken"]>;
303
+ constructor(options?: ComposedMorseCallbackHandlerOptions);
304
+ /**
305
+ * The W2-L.1 base maintains its own private chain-span map. We
306
+ * mirror chain handles into the shared `LangGraphRunState` so future
307
+ * cross-fragment logic can correlate by runId. Never throws — the
308
+ * map mutation is in-process and the super method already absorbs
309
+ * errors via `absorbErrorsSync`.
310
+ */
311
+ handleChainStart(chain: SerializedLike, inputs: Record<string, unknown>, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): void;
312
+ handleChainEnd(outputs: Record<string, unknown>, runId: string, parentRunId?: string): void;
313
+ handleChainError(err: unknown, runId: string, parentRunId?: string): void;
314
+ /**
315
+ * Marker insertion for the shared chainSpans map. The W2-L.1 base
316
+ * holds the real `SpanHandle` privately; for the shared map we just
317
+ * need *presence* keyed by runId so downstream fragments can detect
318
+ * an in-flight chain. We stash a sentinel handle whose `end()` is a
319
+ * no-op — the base owns the real lifecycle.
320
+ */
321
+ private _shadowSetChainSpan;
322
+ }
323
+
324
+ export { type BaseMessageLike, MorseCallbackHandler$1 as BaseMorseCallbackHandler, type CallbackHandlerSurface, type ComposedMorseCallbackHandlerOptions, type LLMResultLike, type LangChainCoreCallbacksModuleLike, type LangGraphRunState, type LlmAndToolHandlerOptions, MorseCallbackHandler, type MorseCallbackHandlerOptions, type SerializedLike, type StreamingHandlerOptions, createLlmAndToolHandlers, createStreamingHandlers, getCallbackHandlerClass };