@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,371 @@
1
+ import { AnthropicMessageCreateParams } from '../anthropic/index.js';
2
+ import '../spans-DZtMuBvc.js';
3
+
4
+ /**
5
+ * Structural types for `@anthropic-ai/claude-agent-sdk` integration.
6
+ *
7
+ * We deliberately decline to import the real types so the SDK works even
8
+ * when `@anthropic-ai/claude-agent-sdk` isn't installed. Each interface
9
+ * captures the minimum surface our wrappers need to observe.
10
+ *
11
+ * Mirrors the pattern in `src/anthropic/index.ts` (low-level Messages
12
+ * API adapter) and `src/openai-agents/types.ts` / `src/langgraph/types.ts`.
13
+ */
14
+
15
+ /**
16
+ * One SDK event yielded from `ClaudeSDKClient.query()`. The real SDK emits
17
+ * an `SDKMessage` discriminated union; we keep the structural shape minimal.
18
+ */
19
+ interface SDKMessageLike {
20
+ type: string;
21
+ message?: {
22
+ id?: string;
23
+ model?: string;
24
+ usage?: {
25
+ input_tokens?: number;
26
+ output_tokens?: number;
27
+ cache_creation_input_tokens?: number;
28
+ cache_read_input_tokens?: number;
29
+ [k: string]: unknown;
30
+ };
31
+ content?: Array<{
32
+ type: string;
33
+ [k: string]: unknown;
34
+ }>;
35
+ [k: string]: unknown;
36
+ };
37
+ tool_use_id?: string;
38
+ name?: string;
39
+ input?: unknown;
40
+ content?: unknown;
41
+ is_error?: boolean;
42
+ hook?: {
43
+ name: string;
44
+ kind: "pre_tool" | "post_tool" | "on_error";
45
+ [k: string]: unknown;
46
+ };
47
+ [k: string]: unknown;
48
+ }
49
+ interface ClaudeSDKQueryOptions {
50
+ systemPrompt?: string;
51
+ maxTurns?: number;
52
+ model?: string;
53
+ [k: string]: unknown;
54
+ }
55
+ /**
56
+ * Structural shape of `ClaudeSDKClient`. We only require `query()` returning
57
+ * an async iterable of `SDKMessageLike`. Hook registration is optional and
58
+ * accessed via `client.on(...)` if present.
59
+ */
60
+ interface ClaudeSDKClientLike {
61
+ query(prompt: string | AnthropicMessageCreateParams, options?: ClaudeSDKQueryOptions): AsyncIterable<SDKMessageLike> | Promise<AsyncIterable<SDKMessageLike>>;
62
+ on?(event: string, handler: (payload: unknown) => void): void;
63
+ off?(event: string, handler: (payload: unknown) => void): void;
64
+ }
65
+ /**
66
+ * Subset of the SDK module shape we probe when patching via
67
+ * `instrumentClaudeAgentSDK({ claudeAgentSdkModule })`. Best-effort.
68
+ */
69
+ interface ClaudeAgentSDKModuleLike {
70
+ ClaudeSDKClient?: {
71
+ prototype: {
72
+ query: unknown;
73
+ [k: string]: unknown;
74
+ };
75
+ };
76
+ query?: (...args: unknown[]) => unknown;
77
+ default?: unknown;
78
+ [k: string]: unknown;
79
+ }
80
+
81
+ /**
82
+ * `@morsehq-dev/sdk/anthropic-agent-sdk` — Claude Agent SDK client wrapper.
83
+ *
84
+ * This module implements ONLY the outer-span (agent) instrumentation around
85
+ * `ClaudeSDKClient.query()`. Sibling files (`llm-instrumentation.ts`,
86
+ * `tool-and-subagent.ts`, `hook-instrumentation.ts`) layer the inner llm /
87
+ * tool / hook spans; Wave 3-A composes them via `src/anthropic-agent-sdk/index.ts`.
88
+ *
89
+ * Two entry points (mirroring `src/anthropic/index.ts`):
90
+ *
91
+ * - `wrapClaudeAgentSDK(client)` — returns a Proxy-of-Proxy that
92
+ * intercepts `query()` and opens an outer `agent` span around the async
93
+ * iteration. The rest of the client surface (`on`, `off`, anything
94
+ * else) is forwarded unchanged.
95
+ *
96
+ * - `instrumentClaudeAgentSDK(clientOrOptions, options?)` — two-mode
97
+ * installer. With a client, equivalent to `wrapClaudeAgentSDK`. With
98
+ * `{ claudeAgentSdkModule }`, best-effort patches
99
+ * `ClaudeSDKClient.prototype.query`. Always returns the wrapped client
100
+ * or a boolean; never throws.
101
+ *
102
+ * All telemetry paths are wrapped in `absorbErrors*` — a tracing failure
103
+ * MUST NEVER break the customer's `query()`.
104
+ */
105
+
106
+ interface WrapClaudeAgentSDKOptions {
107
+ /** Override the agent name on the outer span. Default: `"claude-agent-sdk"`. */
108
+ defaultAgentName?: string;
109
+ /** Override the model name passed to the outer span metadata. Default: inferred. */
110
+ defaultModel?: string;
111
+ }
112
+ /**
113
+ * Wrap a `ClaudeSDKClient` so each `query()` call opens an outer `agent`
114
+ * span (via `emitAgentSpan` from `_agent-sdk-common`). The rest of the
115
+ * client's surface is forwarded unchanged via a Proxy. Idempotent.
116
+ *
117
+ * THIS WRAPPER ONLY HANDLES THE OUTER AGENT SPAN. llm / tool / hook spans
118
+ * are layered by sibling files in the same directory and composed via
119
+ * `src/anthropic-agent-sdk/index.ts` in Wave 3.
120
+ */
121
+ declare function wrapClaudeAgentSDK<C extends ClaudeSDKClientLike>(client: C, options?: WrapClaudeAgentSDKOptions): C;
122
+ /**
123
+ * Top-level installer. Two modes:
124
+ *
125
+ * - `instrumentClaudeAgentSDK(client)` — equivalent to `wrapClaudeAgentSDK(client)`.
126
+ * - `instrumentClaudeAgentSDK({ claudeAgentSdkModule: mod })` — best-effort
127
+ * prototype patch on `ClaudeSDKClient.prototype.query`. Prefer the
128
+ * wrap form when possible.
129
+ *
130
+ * Always returns the wrapped client, `true`/`false` for the module path, or
131
+ * `undefined` on absorbed error. Never throws.
132
+ */
133
+ type InstrumentClientArg = ClaudeSDKClientLike | (WrapClaudeAgentSDKOptions & {
134
+ claudeAgentSdkModule?: unknown;
135
+ }) | undefined;
136
+ declare const instrumentClaudeAgentSDK: (clientOrOptions?: InstrumentClientArg, maybeOptions?: WrapClaudeAgentSDKOptions) => Promise<unknown | undefined>;
137
+ /**
138
+ * Restore the prototype patch and clear the install sentinel. Pass the
139
+ * same module reference given to `instrumentClaudeAgentSDK`, or omit and
140
+ * the function will best-effort do nothing (no-op). Calling on a wrapped
141
+ * client (vs. a module) is a no-op — just discard the wrapped client.
142
+ */
143
+ declare const uninstallClaudeAgentSDK: (claudeAgentSdkModule?: unknown) => void | undefined;
144
+ /** True when a wrapped Claude Agent SDK client is passed in. */
145
+ declare function isClaudeAgentSDKWrapped(client: unknown): boolean;
146
+
147
+ /**
148
+ * `anthropic-agent-sdk/hook-instrumentation.ts` — W2-A.4
149
+ *
150
+ * Subscribes to `ClaudeSDKClient`'s `"hook"` event channel and emits one
151
+ * `hook`-shaped span (wire `type=tool` + `metadata.tool_kind="hook"`) per
152
+ * fired hook. Hooks logically bracket a tool invocation (`pre_tool` /
153
+ * `post_tool`) or react to a failure (`on_error`); we therefore emit them
154
+ * as SIBLINGS of the wrapped tool span (sharing the tool's parent), not
155
+ * nested under it.
156
+ *
157
+ * When the caller provides a `toolSpanLookup` (typically wired in by the
158
+ * tool-and-subagent W2-A.3 integration), we use `emitAsToolSibling` so
159
+ * the hook attaches to the tool's parent. When no lookup is provided —
160
+ * or the lookup returns null for an unknown tool_use_id — we fall back
161
+ * to `emitHookSpan` which attaches to whatever parent is on top of the
162
+ * active span stack.
163
+ *
164
+ * Hooks are synchronous from our perspective: we emit and immediately
165
+ * close the span. When the hook payload reports `duration_ms`, it lands
166
+ * in `metadata.hook.duration_ms` (the span's own `duration_ms` reflects
167
+ * our emit/close roundtrip, which is effectively zero).
168
+ *
169
+ * Every emission is wrapped in `absorbErrorsSync` per the foundational
170
+ * "telemetry must never break the host" guarantee.
171
+ */
172
+
173
+ interface InstallHookInstrumentationOptions {
174
+ /**
175
+ * Map of toolUseId → spanId for the currently-open tool spans.
176
+ * Provided by W2-A.3 (tool-and-subagent.ts) when the integration layer
177
+ * composes both. When omitted, hook spans are emitted without sibling
178
+ * linkage (they attach to whatever parent is on top of the stack).
179
+ */
180
+ toolSpanLookup?: (toolUseId: string) => string | null;
181
+ }
182
+ interface HookEventLike {
183
+ /** Hook name (user-defined identifier from their hook registration). */
184
+ name: string;
185
+ /** Hook kind. */
186
+ kind: "pre_tool" | "post_tool" | "on_error";
187
+ /** When known, the tool_use_id this hook wraps (for sibling linkage). */
188
+ tool_use_id?: string;
189
+ /** Duration in ms reported by the hook callback, if available. */
190
+ duration_ms?: number;
191
+ }
192
+ /**
193
+ * Subscribe to the client's `"hook"` event channel and emit one hook
194
+ * span per fired hook. Returns an unsubscribe function.
195
+ *
196
+ * If `client.on` is not a function (the user didn't wire hooks at all),
197
+ * returns a no-op unsubscriber.
198
+ */
199
+ declare function installHookInstrumentation(client: ClaudeSDKClientLike, options?: InstallHookInstrumentationOptions): () => void;
200
+
201
+ /**
202
+ * `anthropic-agent-sdk/llm-instrumentation.ts` — taps `message_start` /
203
+ * `message_stop` event pairs in a Claude Agent SDK stream and emits one
204
+ * `llm` span per pair.
205
+ *
206
+ * The wrapper is a passthrough generator: every event is yielded to the
207
+ * downstream caller unchanged. Span emission is a side-effect.
208
+ *
209
+ * One agent run can contain MULTIPLE llm calls (the agent loops on
210
+ * tool-use → tool-result), so we emit one span PER message_start /
211
+ * message_stop pair, not one for the whole stream.
212
+ *
213
+ * `context_segments` are extracted via `extractAnthropicSegments` from the
214
+ * low-level Anthropic adapter (`../anthropic`) when the caller provides the
215
+ * original request params. We DO NOT duplicate that logic here.
216
+ *
217
+ * Sibling W2-A files in this directory handle agent / tool / subagent /
218
+ * hook spans separately.
219
+ */
220
+
221
+ interface InstrumentLlmCallsOptions {
222
+ /** Override the model name when the SDK doesn't report one on `message_start`. */
223
+ fallbackModel?: string;
224
+ /**
225
+ * Original request params (the prompt + messages + tools the user passed
226
+ * to `query()`). When provided, allows `extractAnthropicSegments` to
227
+ * build `context_segments`. When omitted, `context_segments` is left
228
+ * unset on the emitted span.
229
+ */
230
+ requestParams?: AnthropicMessageCreateParams;
231
+ }
232
+ /**
233
+ * Wrap an async iterable of `SDKMessageLike` events: pass each event through
234
+ * unchanged, but tap `message_start` / `message_stop` pairs and emit one
235
+ * `llm` span per pair.
236
+ *
237
+ * Behaviour:
238
+ *
239
+ * - The span opens at `message_start` (model is read from `event.message.model`
240
+ * or falls back to `options.fallbackModel`).
241
+ * - Token usage is read from `event.message.usage` on `message_stop` —
242
+ * the start-event usage is only partial. `input_tokens` sums
243
+ * `input_tokens + cache_creation_input_tokens + cache_read_input_tokens`.
244
+ * - If `requestParams` is provided, `context_segments` is populated via
245
+ * `extractAnthropicSegments(requestParams, response)`.
246
+ * - Orphan `message_stop` events (with no preceding start) are tolerated:
247
+ * no span, no error.
248
+ * - If the iterator ends with an open span (a start without a matching
249
+ * stop), the span is closed as failed.
250
+ * - All telemetry is wrapped in try/catch — it MUST NOT break iteration.
251
+ */
252
+ declare function instrumentLlmCalls(events: AsyncIterable<SDKMessageLike>, options?: InstrumentLlmCallsOptions): AsyncIterable<SDKMessageLike>;
253
+
254
+ /**
255
+ * `anthropic-agent-sdk/tool-and-subagent.ts` — instrument the `tool_use` /
256
+ * `tool_result` event pairs emitted by `@anthropic-ai/claude-agent-sdk`'s
257
+ * `ClaudeSDKClient.query()`.
258
+ *
259
+ * Phase 13 W2-A.3. The Anthropic Agent SDK streams a discriminated union
260
+ * of `SDKMessage` events; this module wraps such a stream as a tap that
261
+ * (a) passes every event through unchanged and (b) emits matching
262
+ * Morse spans for tool invocations:
263
+ *
264
+ * - `tool_use` with `name !== "Task"` → `type=tool` span (held open
265
+ * until the matching `tool_result` arrives, indexed by
266
+ * `tool_use_id`).
267
+ * - `tool_use` with `name === "Task"` → `type=agent` span carrying
268
+ * `metadata.spawn_kind="subagent"` (the canonical subagent.spawn
269
+ * pattern from `_agent-sdk-common/span-emitter.ts`). The span is also
270
+ * held open until the matching `tool_result`.
271
+ * - `tool_result` → look up the open span by `tool_use_id`, stamp the
272
+ * output summary + success flag, close with the appropriate status.
273
+ *
274
+ * **Task-tool fan-out parent pinning.** When the agent loop spawns more
275
+ * than one subagent in the same turn it emits N Task `tool_use` blocks
276
+ * sequentially before any matching `tool_result`. The default
277
+ * top-of-stack parent lookup in `startSpan` would (incorrectly) make
278
+ * later subagent spans children of the earlier siblings. We snapshot
279
+ * the current top-of-stack at the first Task `tool_use` via
280
+ * `captureParentForFanOut` and route every subagent span through
281
+ * `emitWithPinnedParent` so they all share the same parent. The
282
+ * snapshot is cleared once all open Task spans close (or the iterator
283
+ * ends).
284
+ *
285
+ * **Error containment.** All emission is wrapped — telemetry MUST NOT
286
+ * break iteration. If an internal helper throws, the event still flows
287
+ * through to the caller and the offending span is best-effort skipped.
288
+ *
289
+ * **Orphan tolerance.** `tool_result` without a matching `tool_use` is
290
+ * silently ignored. `tool_use` without a matching `tool_result` is
291
+ * closed as failed when the iterator ends.
292
+ */
293
+
294
+ interface InstrumentToolsOptions {
295
+ /** Override input byte cap passed to `summarizeToolInput`. */
296
+ toolInputMaxBytes?: number;
297
+ /** Override output byte cap passed to `summarizeToolOutput`. */
298
+ toolOutputMaxBytes?: number;
299
+ }
300
+ /**
301
+ * Wrap an async iterable of `SDKMessageLike` events: yield every event
302
+ * unchanged but tap `tool_use` / `tool_result` pairs and emit matching
303
+ * `tool` and `subagent` spans against the active Morse trace.
304
+ *
305
+ * No-op outside an active trace (the emitter helpers return `null`).
306
+ * Iterator order and timing are preserved; emission errors are
307
+ * absorbed.
308
+ */
309
+ declare function instrumentTools(events: AsyncIterable<SDKMessageLike>, options?: InstrumentToolsOptions): AsyncIterable<SDKMessageLike>;
310
+
311
+ /**
312
+ * `@morsehq-dev/sdk/anthropic-agent-sdk` — public surface for the
313
+ * `@anthropic-ai/claude-agent-sdk` adapter.
314
+ *
315
+ * Composes the four W2-A surfaces:
316
+ *
317
+ * - `client-wrapper.ts` opens the outer `agent` span around `query()`.
318
+ * - `llm-instrumentation.ts` taps `message_start` / `message_stop` pairs
319
+ * and emits `llm` spans.
320
+ * - `tool-and-subagent.ts` taps `tool_use` / `tool_result` pairs and
321
+ * emits `tool` spans (regular tools) or `agent` spans with
322
+ * `metadata.spawn_kind="subagent"` (Task fan-out).
323
+ * - `hook-instrumentation.ts` subscribes to the client's `"hook"` event
324
+ * channel and emits one `tool`-typed `tool_kind="hook"` span per hook.
325
+ *
326
+ * The convenience composer `wrapClaudeAgentSDKWithFullInstrumentation`
327
+ * stacks them in the correct order: outer `wrapClaudeAgentSDK` opens the
328
+ * agent span; inner generators (`instrumentLlmCalls`, `instrumentTools`)
329
+ * are chained around the underlying query iterator so events flow once
330
+ * through both taps before reaching the caller. Hook subscription is
331
+ * event-based (decoupled).
332
+ *
333
+ * See `openspec/changes/2026-05-26-phase-13-ts-agent-sdk-adapters/`.
334
+ */
335
+
336
+ interface FullInstrumentationOptions extends WrapClaudeAgentSDKOptions, InstrumentLlmCallsOptions, InstrumentToolsOptions, InstallHookInstrumentationOptions {
337
+ }
338
+ interface FullInstrumentationResult<C extends ClaudeSDKClientLike> {
339
+ /** The Proxy-wrapped client. Drop-in replacement for the original. */
340
+ client: C;
341
+ /**
342
+ * Tear-down. Currently disconnects the hook listener. The outer-span
343
+ * Proxy and generator wrapping are inert when not invoked, so there's
344
+ * no other cleanup required — subsequent `query()` calls on the
345
+ * returned client will still emit telemetry until callers drop the
346
+ * reference.
347
+ */
348
+ dispose: () => void;
349
+ }
350
+ /**
351
+ * Compose all four W2-A instrumentation layers on a single
352
+ * `ClaudeSDKClient`. Returns the wrapped client plus a `dispose()` that
353
+ * removes the hook listener.
354
+ *
355
+ * Idempotent: re-wrapping a previously full-wrapped client returns the
356
+ * original result (same `client` reference, same `dispose`).
357
+ *
358
+ * Composition order (outer → inner):
359
+ *
360
+ * 1. `wrapClaudeAgentSDK` — opens the outer `agent` span around the
361
+ * whole iteration and ensures a trace is active.
362
+ * 2. `instrumentLlmCalls` — taps `message_start` / `message_stop`
363
+ * pairs.
364
+ * 3. `instrumentTools` — taps `tool_use` / `tool_result` pairs.
365
+ * 4. The underlying `client.query()` iterator.
366
+ *
367
+ * `installHookInstrumentation` runs in parallel via the event channel.
368
+ */
369
+ declare function wrapClaudeAgentSDKWithFullInstrumentation<C extends ClaudeSDKClientLike>(client: C, options?: FullInstrumentationOptions): FullInstrumentationResult<C>;
370
+
371
+ export { type ClaudeAgentSDKModuleLike, type ClaudeSDKClientLike, type ClaudeSDKQueryOptions, type FullInstrumentationOptions, type FullInstrumentationResult, type HookEventLike, type InstallHookInstrumentationOptions, type InstrumentLlmCallsOptions, type InstrumentToolsOptions, type SDKMessageLike, type WrapClaudeAgentSDKOptions, installHookInstrumentation, instrumentClaudeAgentSDK, instrumentLlmCalls, instrumentTools, isClaudeAgentSDKWrapped, uninstallClaudeAgentSDK, wrapClaudeAgentSDK, wrapClaudeAgentSDKWithFullInstrumentation };