@morsehq-dev/sdk 0.5.0-rc.4 → 0.5.0-rc.5

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 (106) hide show
  1. package/README.md +17 -12
  2. package/dist/anthropic/index.cjs +11 -11
  3. package/dist/anthropic/index.js +4 -4
  4. package/dist/anthropic-agent-sdk/index.cjs +157 -102
  5. package/dist/anthropic-agent-sdk/index.cjs.map +1 -1
  6. package/dist/anthropic-agent-sdk/index.d.cts +57 -34
  7. package/dist/anthropic-agent-sdk/index.d.ts +57 -34
  8. package/dist/anthropic-agent-sdk/index.js +126 -72
  9. package/dist/anthropic-agent-sdk/index.js.map +1 -1
  10. package/dist/browser/anthropic/index.cjs +11 -11
  11. package/dist/browser/anthropic/index.js +4 -4
  12. package/dist/browser/anthropic-agent-sdk/index.cjs +157 -102
  13. package/dist/browser/anthropic-agent-sdk/index.cjs.map +1 -1
  14. package/dist/browser/anthropic-agent-sdk/index.js +126 -72
  15. package/dist/browser/anthropic-agent-sdk/index.js.map +1 -1
  16. package/dist/browser/{chunk-VOZBYSWK.cjs → chunk-26Q2QD5J.cjs} +5 -5
  17. package/dist/browser/{chunk-VOZBYSWK.cjs.map → chunk-26Q2QD5J.cjs.map} +1 -1
  18. package/dist/browser/{chunk-36QTJJT7.cjs → chunk-47GVOQB4.cjs} +12 -12
  19. package/dist/browser/{chunk-36QTJJT7.cjs.map → chunk-47GVOQB4.cjs.map} +1 -1
  20. package/dist/browser/{chunk-E2OVLWAF.js → chunk-64YI3IZS.js} +3 -3
  21. package/dist/browser/{chunk-E2OVLWAF.js.map → chunk-64YI3IZS.js.map} +1 -1
  22. package/dist/browser/{chunk-JR6JN7IZ.js → chunk-CI7M2GHD.js} +4 -4
  23. package/dist/browser/{chunk-JR6JN7IZ.js.map → chunk-CI7M2GHD.js.map} +1 -1
  24. package/dist/browser/{chunk-WIWUEI5Z.js → chunk-EH326H6U.js} +3 -3
  25. package/dist/browser/{chunk-WIWUEI5Z.js.map → chunk-EH326H6U.js.map} +1 -1
  26. package/dist/browser/{chunk-KXVSMY6L.js → chunk-GLL57K7P.js} +3 -3
  27. package/dist/browser/{chunk-KXVSMY6L.js.map → chunk-GLL57K7P.js.map} +1 -1
  28. package/dist/browser/{chunk-3H2OJLC2.cjs → chunk-IHIS62VS.cjs} +4 -4
  29. package/dist/browser/chunk-IHIS62VS.cjs.map +1 -0
  30. package/dist/browser/{chunk-PIWO5L5N.js → chunk-MIB3EOC4.js} +5 -5
  31. package/dist/browser/{chunk-PIWO5L5N.js.map → chunk-MIB3EOC4.js.map} +1 -1
  32. package/dist/browser/{chunk-3STFITZF.cjs → chunk-QAFFDJFK.cjs} +6 -6
  33. package/dist/browser/{chunk-3STFITZF.cjs.map → chunk-QAFFDJFK.cjs.map} +1 -1
  34. package/dist/{chunk-BNMTY7PE.js → browser/chunk-UL5TNXQL.js} +4 -4
  35. package/dist/browser/{chunk-VZA2EPY3.js.map → chunk-UL5TNXQL.js.map} +1 -1
  36. package/dist/browser/{chunk-XD4VWCAC.js → chunk-V7AUPQIJ.js} +4 -4
  37. package/dist/browser/chunk-V7AUPQIJ.js.map +1 -0
  38. package/dist/browser/{chunk-YD7TO4PD.cjs → chunk-WGWPCVYB.cjs} +18 -18
  39. package/dist/browser/{chunk-YD7TO4PD.cjs.map → chunk-WGWPCVYB.cjs.map} +1 -1
  40. package/dist/browser/{chunk-6J6F5LJI.cjs → chunk-WR2SQLKP.cjs} +7 -7
  41. package/dist/browser/{chunk-6J6F5LJI.cjs.map → chunk-WR2SQLKP.cjs.map} +1 -1
  42. package/dist/browser/{chunk-255AHH2W.cjs → chunk-ZVVUZRG7.cjs} +15 -15
  43. package/dist/browser/{chunk-255AHH2W.cjs.map → chunk-ZVVUZRG7.cjs.map} +1 -1
  44. package/dist/browser/index.cjs +91 -91
  45. package/dist/browser/index.js +8 -8
  46. package/dist/browser/integrations/pino.cjs +4 -4
  47. package/dist/browser/integrations/pino.js +3 -3
  48. package/dist/browser/langchain/index.cjs +17 -17
  49. package/dist/browser/langchain/index.js +2 -2
  50. package/dist/browser/langgraph/index.cjs +27 -27
  51. package/dist/browser/langgraph/index.js +5 -5
  52. package/dist/browser/openai/index.cjs +11 -11
  53. package/dist/browser/openai/index.js +2 -2
  54. package/dist/browser/openai-agents/index.cjs +34 -34
  55. package/dist/browser/openai-agents/index.js +4 -4
  56. package/dist/browser/vercel-ai/index.cjs +14 -14
  57. package/dist/browser/vercel-ai/index.js +3 -3
  58. package/dist/{chunk-MBOISGKD.js → chunk-7I4ILCLY.js} +4 -4
  59. package/dist/{chunk-MBOISGKD.js.map → chunk-7I4ILCLY.js.map} +1 -1
  60. package/dist/{chunk-7ZNGY236.cjs → chunk-DGBQ3UI6.cjs} +7 -7
  61. package/dist/{chunk-7ZNGY236.cjs.map → chunk-DGBQ3UI6.cjs.map} +1 -1
  62. package/dist/{chunk-VM3QM22Z.cjs → chunk-DPW4W7H7.cjs} +18 -18
  63. package/dist/{chunk-VM3QM22Z.cjs.map → chunk-DPW4W7H7.cjs.map} +1 -1
  64. package/dist/{chunk-HA5RY4N5.cjs → chunk-DQ4T2MJE.cjs} +4 -4
  65. package/dist/chunk-DQ4T2MJE.cjs.map +1 -0
  66. package/dist/{chunk-ELBY4SJ7.js → chunk-ELVBOJWY.js} +4 -4
  67. package/dist/chunk-ELVBOJWY.js.map +1 -0
  68. package/dist/{chunk-JFFZBCTP.js → chunk-HONYN2OI.js} +3 -3
  69. package/dist/{chunk-JFFZBCTP.js.map → chunk-HONYN2OI.js.map} +1 -1
  70. package/dist/{chunk-ULWGK6F5.cjs → chunk-I3P5CHBV.cjs} +12 -12
  71. package/dist/{chunk-ULWGK6F5.cjs.map → chunk-I3P5CHBV.cjs.map} +1 -1
  72. package/dist/{browser/chunk-VZA2EPY3.js → chunk-MNV4HASC.js} +4 -4
  73. package/dist/{chunk-BNMTY7PE.js.map → chunk-MNV4HASC.js.map} +1 -1
  74. package/dist/{chunk-2O2FFJLK.js → chunk-NR36TJMT.js} +5 -5
  75. package/dist/{chunk-2O2FFJLK.js.map → chunk-NR36TJMT.js.map} +1 -1
  76. package/dist/{chunk-YPP5DDPO.js → chunk-QZDUGEIR.js} +3 -3
  77. package/dist/{chunk-YPP5DDPO.js.map → chunk-QZDUGEIR.js.map} +1 -1
  78. package/dist/{chunk-RDFDPK2X.cjs → chunk-RRE2LX2A.cjs} +6 -6
  79. package/dist/{chunk-RDFDPK2X.cjs.map → chunk-RRE2LX2A.cjs.map} +1 -1
  80. package/dist/{chunk-WLNADB4J.cjs → chunk-SJ625SQS.cjs} +15 -15
  81. package/dist/{chunk-WLNADB4J.cjs.map → chunk-SJ625SQS.cjs.map} +1 -1
  82. package/dist/{chunk-Q6NQQ2LO.js → chunk-YKPZQPEW.js} +3 -3
  83. package/dist/{chunk-Q6NQQ2LO.js.map → chunk-YKPZQPEW.js.map} +1 -1
  84. package/dist/{chunk-HZKXILBF.cjs → chunk-Z5MFR7UE.cjs} +5 -5
  85. package/dist/{chunk-HZKXILBF.cjs.map → chunk-Z5MFR7UE.cjs.map} +1 -1
  86. package/dist/index.cjs +104 -99
  87. package/dist/index.cjs.map +1 -1
  88. package/dist/index.js +15 -10
  89. package/dist/index.js.map +1 -1
  90. package/dist/integrations/pino.cjs +4 -4
  91. package/dist/integrations/pino.js +3 -3
  92. package/dist/langchain/index.cjs +17 -17
  93. package/dist/langchain/index.js +2 -2
  94. package/dist/langgraph/index.cjs +27 -27
  95. package/dist/langgraph/index.js +5 -5
  96. package/dist/openai/index.cjs +11 -11
  97. package/dist/openai/index.js +2 -2
  98. package/dist/openai-agents/index.cjs +34 -34
  99. package/dist/openai-agents/index.js +4 -4
  100. package/dist/vercel-ai/index.cjs +14 -14
  101. package/dist/vercel-ai/index.js +3 -3
  102. package/package.json +1 -1
  103. package/dist/browser/chunk-3H2OJLC2.cjs.map +0 -1
  104. package/dist/browser/chunk-XD4VWCAC.js.map +0 -1
  105. package/dist/chunk-ELBY4SJ7.js.map +0 -1
  106. package/dist/chunk-HA5RY4N5.cjs.map +0 -1
@@ -62,21 +62,6 @@ interface ClaudeSDKClientLike {
62
62
  on?(event: string, handler: (payload: unknown) => void): void;
63
63
  off?(event: string, handler: (payload: unknown) => void): void;
64
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
65
 
81
66
  /**
82
67
  * `@morsehq-dev/sdk/anthropic-agent-sdk` — Claude Agent SDK client wrapper.
@@ -120,27 +105,23 @@ interface WrapClaudeAgentSDKOptions {
120
105
  */
121
106
  declare function wrapClaudeAgentSDK<C extends ClaudeSDKClientLike>(client: C, options?: WrapClaudeAgentSDKOptions): C;
122
107
  /**
123
- * Top-level installer. Two modes:
108
+ * Instance wrapper. This once had a second mode that patched
109
+ * `ClaudeSDKClient.prototype.query` on the module. Deleted in MHQ-936 —
110
+ * neither half of it could ever work in TypeScript:
124
111
  *
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.
112
+ * - `ClaudeSDKClient` is the *Python* package's API. The TS package has
113
+ * never exported it (checked at 0.1.77 and 0.3.223), so the carrier
114
+ * lookup always returned null and the install silently no-opped.
115
+ * - Patching the real `query` export is impossible regardless: it is
116
+ * read-only and non-configurable, so assignment throws — the same wall
117
+ * `ai` hit in MHQ-758.
129
118
  *
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.
119
+ * For the real package use `wrapClaudeAgentQuery(query)` (`./query-wrapper.ts`).
120
+ * This remains for any object exposing a `.query` method.
121
+ *
122
+ * Never throws; returns the wrapped client, or a falsy value otherwise.
142
123
  */
143
- declare const uninstallClaudeAgentSDK: (claudeAgentSdkModule?: unknown) => void | undefined;
124
+ declare const instrumentClaudeAgentSDK: (client?: ClaudeSDKClientLike, maybeOptions?: WrapClaudeAgentSDKOptions) => Promise<unknown | undefined>;
144
125
  /** True when a wrapped Claude Agent SDK client is passed in. */
145
126
  declare function isClaudeAgentSDKWrapped(client: unknown): boolean;
146
127
 
@@ -308,6 +289,48 @@ interface InstrumentToolsOptions {
308
289
  */
309
290
  declare function instrumentTools(events: AsyncIterable<SDKMessageLike>, options?: InstrumentToolsOptions): AsyncIterable<SDKMessageLike>;
310
291
 
292
+ /** The subset of `Query` we depend on: async iteration. Everything else is forwarded. */
293
+ interface ClaudeAgentQueryLike extends AsyncIterable<SDKMessageLike> {
294
+ [key: string]: unknown;
295
+ }
296
+ /** Real shape of the package's `query` export. One object argument. */
297
+ type ClaudeAgentQueryFn = (params: {
298
+ prompt: unknown;
299
+ options?: {
300
+ model?: unknown;
301
+ [key: string]: unknown;
302
+ };
303
+ }) => ClaudeAgentQueryLike;
304
+ interface WrapClaudeAgentQueryOptions {
305
+ /** Agent name on the outer span when none can be derived. */
306
+ defaultAgentName?: string;
307
+ /** Model for the outer span when `params.options.model` is absent. */
308
+ defaultModel?: string;
309
+ /** Max bytes of serialized tool input captured per tool span. */
310
+ toolInputMaxBytes?: number;
311
+ /** Max bytes of serialized tool output captured per tool span. */
312
+ toolOutputMaxBytes?: number;
313
+ }
314
+ /** True if `fn` is already a traced query — wrapping twice would double-count spans. */
315
+ declare function isClaudeAgentQueryWrapped(fn: unknown): boolean;
316
+ /**
317
+ * Wrap the package's `query` function. The returned function has the same
318
+ * signature and returns a `Query`-compatible object.
319
+ *
320
+ * ```ts
321
+ * import { query } from "@anthropic-ai/claude-agent-sdk";
322
+ * import { wrapClaudeAgentQuery } from "@morsehq-dev/sdk/anthropic-agent-sdk";
323
+ *
324
+ * const tracedQuery = wrapClaudeAgentQuery(query);
325
+ * for await (const message of tracedQuery({ prompt: "..." })) { ... }
326
+ * ```
327
+ *
328
+ * Telemetry never changes what the caller sees: messages are yielded through
329
+ * untouched, and a throw from the underlying query propagates unchanged after
330
+ * the span is closed with the error.
331
+ */
332
+ declare function wrapClaudeAgentQuery<Q extends ClaudeAgentQueryFn>(query: Q, options?: WrapClaudeAgentQueryOptions): Q;
333
+
311
334
  /**
312
335
  * `@morsehq-dev/sdk/anthropic-agent-sdk` — public surface for the
313
336
  * `@anthropic-ai/claude-agent-sdk` adapter.
@@ -368,4 +391,4 @@ interface FullInstrumentationResult<C extends ClaudeSDKClientLike> {
368
391
  */
369
392
  declare function wrapClaudeAgentSDKWithFullInstrumentation<C extends ClaudeSDKClientLike>(client: C, options?: FullInstrumentationOptions): FullInstrumentationResult<C>;
370
393
 
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 };
394
+ export { type ClaudeAgentQueryFn, type ClaudeAgentQueryLike, type ClaudeSDKClientLike, type ClaudeSDKQueryOptions, type FullInstrumentationOptions, type FullInstrumentationResult, type HookEventLike, type InstallHookInstrumentationOptions, type InstrumentLlmCallsOptions, type InstrumentToolsOptions, type SDKMessageLike, type WrapClaudeAgentQueryOptions, type WrapClaudeAgentSDKOptions, installHookInstrumentation, instrumentClaudeAgentSDK, instrumentLlmCalls, instrumentTools, isClaudeAgentQueryWrapped, isClaudeAgentSDKWrapped, wrapClaudeAgentQuery, wrapClaudeAgentSDK, wrapClaudeAgentSDKWithFullInstrumentation };
@@ -1,17 +1,15 @@
1
- import { extractAnthropicSegments } from '../chunk-2O2FFJLK.js';
2
- import '../chunk-YPP5DDPO.js';
3
- import { summarizeToolInput, summarizeToolOutput } from '../chunk-JFFZBCTP.js';
4
- import { emitAgentSpan, emitHookSpan, emitLlmSpan, emitToolSpan } from '../chunk-BNMTY7PE.js';
5
- import { absorbErrorsAsync, absorbErrorsSync } from '../chunk-Q6NQQ2LO.js';
1
+ import { extractAnthropicSegments } from '../chunk-NR36TJMT.js';
2
+ import '../chunk-QZDUGEIR.js';
3
+ import { summarizeToolInput, summarizeToolOutput } from '../chunk-HONYN2OI.js';
4
+ import { emitHookSpan, emitAgentSpan, emitLlmSpan, emitToolSpan } from '../chunk-MNV4HASC.js';
5
+ import { absorbErrorsAsync, absorbErrorsSync } from '../chunk-YKPZQPEW.js';
6
6
  import { calculateCost } from '../chunk-XTKMUJWI.js';
7
7
  import '../chunk-4JPZV2VZ.js';
8
- import { init_tracing, getActiveTraceState, runAsync, startSpan, init_diagnostics, report } from '../chunk-ELBY4SJ7.js';
8
+ import { init_tracing, getActiveTraceState, startSpan, init_diagnostics, runAsync, report } from '../chunk-ELVBOJWY.js';
9
9
 
10
10
  // src/anthropic-agent-sdk/client-wrapper.ts
11
11
  init_tracing();
12
12
  var WRAPPED_CLIENT = /* @__PURE__ */ Symbol.for("morse.anthropic-agent-sdk.wrapped");
13
- var INSTALL_SENTINEL = /* @__PURE__ */ Symbol.for("morse.anthropic-agent-sdk.installed");
14
- var ORIGINALS_SENTINEL = /* @__PURE__ */ Symbol.for("morse.anthropic-agent-sdk.originals");
15
13
  function wrapClaudeAgentSDK(client, options = {}) {
16
14
  if (!client || typeof client !== "object") return client;
17
15
  if (client[WRAPPED_CLIENT]) return client;
@@ -35,47 +33,12 @@ function wrapClaudeAgentSDK(client, options = {}) {
35
33
  return wrapper;
36
34
  }
37
35
  var instrumentClaudeAgentSDK = absorbErrorsAsync(
38
- async (clientOrOptions = void 0, maybeOptions = {}) => {
39
- if (clientOrOptions && isClient(clientOrOptions)) {
40
- const opts2 = maybeOptions ?? {};
41
- return wrapClaudeAgentSDK(clientOrOptions, opts2);
42
- }
43
- const opts = clientOrOptions ?? {};
44
- const mod = await loadClaudeAgentSDKModule(opts.claudeAgentSdkModule);
45
- if (!mod) return false;
46
- const installed = mod[INSTALL_SENTINEL] === true;
47
- if (installed) return true;
48
- const carrier = findClientPrototypeCarrier(mod);
49
- if (!carrier) return false;
50
- const originalQuery = carrier.prototype.query;
51
- if (typeof originalQuery !== "function") return false;
52
- const patchedQuery = buildPatchedQuery(function(prompt, callOptions) {
53
- return originalQuery.call(this, prompt, callOptions);
54
- }, opts);
55
- carrier.prototype.query = patchedQuery;
56
- mod[ORIGINALS_SENTINEL] = {
57
- protoQuery: originalQuery,
58
- protoCarrier: carrier
59
- };
60
- mod[INSTALL_SENTINEL] = true;
61
- return true;
36
+ async (client = void 0, maybeOptions = {}) => {
37
+ if (client && isClient(client)) return wrapClaudeAgentSDK(client, maybeOptions);
38
+ return false;
62
39
  },
63
40
  { fingerprint: "morse.adapters.anthropic-agent-sdk.install" }
64
41
  );
65
- var uninstallClaudeAgentSDK = absorbErrorsSync(
66
- (claudeAgentSdkModule) => {
67
- if (!claudeAgentSdkModule || typeof claudeAgentSdkModule !== "object") return;
68
- const mod = claudeAgentSdkModule;
69
- if (!mod[INSTALL_SENTINEL]) return;
70
- const originals = mod[ORIGINALS_SENTINEL];
71
- if (originals?.protoCarrier && originals.protoQuery !== void 0) {
72
- originals.protoCarrier.prototype.query = originals.protoQuery;
73
- }
74
- mod[ORIGINALS_SENTINEL] = void 0;
75
- mod[INSTALL_SENTINEL] = false;
76
- },
77
- { fingerprint: "morse.adapters.anthropic-agent-sdk.uninstall" }
78
- );
79
42
  function isClaudeAgentSDKWrapped(client) {
80
43
  if (!client || typeof client !== "object") return false;
81
44
  return client[WRAPPED_CLIENT] === true;
@@ -85,35 +48,13 @@ function isClient(x) {
85
48
  const q = x.query;
86
49
  return typeof q === "function";
87
50
  }
88
- async function loadClaudeAgentSDKModule(explicit) {
89
- if (explicit && typeof explicit === "object") return explicit;
90
- try {
91
- return await Function("m", "return import(m)")("@anthropic-ai/claude-agent-sdk");
92
- } catch {
93
- return null;
94
- }
95
- }
96
- function findClientPrototypeCarrier(mod) {
97
- if (!mod || typeof mod !== "object") return null;
98
- const m = mod;
99
- const candidates = [
100
- m.ClaudeSDKClient,
101
- m.default?.ClaudeSDKClient
102
- ];
103
- for (const c of candidates) {
104
- if (c && typeof c === "function" && c.prototype && typeof c.prototype.query === "function") {
105
- return c;
106
- }
107
- }
108
- return null;
109
- }
110
51
  function buildPatchedQuery(original, options) {
111
52
  const defaultAgentName = options.defaultAgentName ?? "claude-agent-sdk";
112
53
  const defaultModel = options.defaultModel;
113
54
  return function patchedQuery(prompt, callOptions) {
114
55
  const boundOriginal = (p, o) => original.call(this, p, o);
115
56
  const model = (callOptions && typeof callOptions.model === "string" ? callOptions.model : void 0) ?? defaultModel;
116
- async function* iterateInTrace() {
57
+ async function* iterateInTrace2() {
117
58
  const handle = emitAgentSpan({
118
59
  adapter: "anthropic-agent-sdk",
119
60
  agentName: defaultAgentName,
@@ -138,7 +79,7 @@ function buildPatchedQuery(original, options) {
138
79
  handle?.end();
139
80
  }
140
81
  if (getActiveTraceState() !== null) {
141
- return iterateInTrace();
82
+ return iterateInTrace2();
142
83
  }
143
84
  return (async function* outerTrace() {
144
85
  const queue = [];
@@ -155,7 +96,7 @@ function buildPatchedQuery(original, options) {
155
96
  });
156
97
  const runPromise = runAsync({ agentName: defaultAgentName }, async (runHandle) => {
157
98
  try {
158
- for await (const event of iterateInTrace()) {
99
+ for await (const event of iterateInTrace2()) {
159
100
  queue.push({ kind: "value", value: event });
160
101
  notify?.();
161
102
  }
@@ -681,6 +622,119 @@ function reportSwallowed2(err, fingerprint) {
681
622
  }
682
623
  }
683
624
 
625
+ // src/anthropic-agent-sdk/trace-bridge.ts
626
+ init_tracing();
627
+ function iterateInTrace(agentName, makeGenerator) {
628
+ if (getActiveTraceState() !== null) {
629
+ return makeGenerator();
630
+ }
631
+ return (async function* outerTrace() {
632
+ const queue = [];
633
+ let notify = null;
634
+ const waitForItem = () => new Promise((resolve) => {
635
+ if (queue.length > 0) {
636
+ resolve();
637
+ } else {
638
+ notify = () => {
639
+ notify = null;
640
+ resolve();
641
+ };
642
+ }
643
+ });
644
+ const runPromise = runAsync({ agentName }, async (runHandle) => {
645
+ try {
646
+ for await (const event of makeGenerator()) {
647
+ queue.push({ kind: "value", value: event });
648
+ notify?.();
649
+ }
650
+ runHandle.setOutcome(true);
651
+ } catch (err) {
652
+ queue.push({ kind: "error", error: err });
653
+ notify?.();
654
+ throw err;
655
+ } finally {
656
+ queue.push({ kind: "end" });
657
+ notify?.();
658
+ }
659
+ return null;
660
+ });
661
+ runPromise.catch(() => {
662
+ });
663
+ while (true) {
664
+ if (queue.length === 0) await waitForItem();
665
+ const item = queue.shift();
666
+ if (!item) continue;
667
+ if (item.kind === "value") {
668
+ yield item.value;
669
+ } else if (item.kind === "error") {
670
+ throw item.error;
671
+ } else {
672
+ return;
673
+ }
674
+ }
675
+ })();
676
+ }
677
+
678
+ // src/anthropic-agent-sdk/query-wrapper.ts
679
+ var WRAPPED = /* @__PURE__ */ Symbol.for("morse.claudeAgentQuery.wrapped");
680
+ function isClaudeAgentQueryWrapped(fn) {
681
+ return typeof fn === "function" && fn[WRAPPED] === true;
682
+ }
683
+ function wrapClaudeAgentQuery(query, options = {}) {
684
+ if (typeof query !== "function" || isClaudeAgentQueryWrapped(query)) return query;
685
+ const traced = function tracedQuery(params) {
686
+ const real = query(params);
687
+ const model = typeof params?.options?.model === "string" ? params.options.model : options.defaultModel;
688
+ let handle = null;
689
+ let opened = false;
690
+ async function* withAgentSpan() {
691
+ if (!opened) {
692
+ opened = true;
693
+ handle = emitAgentSpan({
694
+ adapter: "anthropic-agent-sdk",
695
+ agentName: options.defaultAgentName ?? "claude-agent-sdk",
696
+ model
697
+ });
698
+ }
699
+ try {
700
+ for await (const message of real) {
701
+ yield message;
702
+ }
703
+ } catch (err) {
704
+ handle?.end(err);
705
+ handle = null;
706
+ throw err;
707
+ }
708
+ handle?.end();
709
+ handle = null;
710
+ }
711
+ const tapOpts = {
712
+ toolInputMaxBytes: options.toolInputMaxBytes,
713
+ toolOutputMaxBytes: options.toolOutputMaxBytes
714
+ };
715
+ const instrumented = iterateInTrace(
716
+ options.defaultAgentName ?? "claude-agent-sdk",
717
+ () => instrumentTools(
718
+ instrumentLlmCalls(withAgentSpan(), { fallbackModel: model }),
719
+ tapOpts
720
+ )
721
+ );
722
+ return new Proxy(real, {
723
+ get(target, prop, receiver) {
724
+ if (prop === Symbol.asyncIterator) return () => instrumented;
725
+ if (prop === "next" || prop === "return" || prop === "throw") {
726
+ const fn = instrumented[prop];
727
+ return typeof fn === "function" ? fn.bind(instrumented) : fn;
728
+ }
729
+ const value = Reflect.get(target, prop, receiver);
730
+ return typeof value === "function" ? value.bind(target) : value;
731
+ }
732
+ });
733
+ };
734
+ Object.defineProperty(traced, WRAPPED, { value: true, enumerable: false });
735
+ return traced;
736
+ }
737
+
684
738
  // src/anthropic-agent-sdk/index.ts
685
739
  var FULL_WRAP_SENTINEL = /* @__PURE__ */ Symbol.for("morse.anthropic-agent-sdk.full-wrapped");
686
740
  function wrapClaudeAgentSDKWithFullInstrumentation(client, options = {}) {
@@ -748,6 +802,6 @@ function wrapClaudeAgentSDKWithFullInstrumentation(client, options = {}) {
748
802
  return result;
749
803
  }
750
804
 
751
- export { installHookInstrumentation, instrumentClaudeAgentSDK, instrumentLlmCalls, instrumentTools, isClaudeAgentSDKWrapped, uninstallClaudeAgentSDK, wrapClaudeAgentSDK, wrapClaudeAgentSDKWithFullInstrumentation };
805
+ export { installHookInstrumentation, instrumentClaudeAgentSDK, instrumentLlmCalls, instrumentTools, isClaudeAgentQueryWrapped, isClaudeAgentSDKWrapped, wrapClaudeAgentQuery, wrapClaudeAgentSDK, wrapClaudeAgentSDKWithFullInstrumentation };
752
806
  //# sourceMappingURL=index.js.map
753
807
  //# sourceMappingURL=index.js.map