@morsehq-dev/sdk 0.5.0-rc.3 → 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 (108) hide show
  1. package/README.md +51 -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-KFWCOUL3.cjs → chunk-26Q2QD5J.cjs} +5 -5
  17. package/dist/browser/{chunk-KFWCOUL3.cjs.map → chunk-26Q2QD5J.cjs.map} +1 -1
  18. package/dist/browser/{chunk-QCI2ZDQS.cjs → chunk-47GVOQB4.cjs} +12 -12
  19. package/dist/browser/{chunk-QCI2ZDQS.cjs.map → chunk-47GVOQB4.cjs.map} +1 -1
  20. package/dist/browser/{chunk-LGG5PL5U.js → chunk-64YI3IZS.js} +3 -3
  21. package/dist/browser/{chunk-LGG5PL5U.js.map → chunk-64YI3IZS.js.map} +1 -1
  22. package/dist/{chunk-HCEKAVDR.js → browser/chunk-CI7M2GHD.js} +4 -4
  23. package/dist/browser/{chunk-KGSLJLWW.js.map → chunk-CI7M2GHD.js.map} +1 -1
  24. package/dist/browser/{chunk-MI4OUWUZ.js → chunk-EH326H6U.js} +3 -3
  25. package/dist/browser/{chunk-MI4OUWUZ.js.map → chunk-EH326H6U.js.map} +1 -1
  26. package/dist/browser/{chunk-WUQXNRD3.js → chunk-GLL57K7P.js} +3 -3
  27. package/dist/browser/{chunk-WUQXNRD3.js.map → chunk-GLL57K7P.js.map} +1 -1
  28. package/dist/browser/{chunk-PM5D6VXF.cjs → chunk-IHIS62VS.cjs} +5 -4
  29. package/dist/browser/chunk-IHIS62VS.cjs.map +1 -0
  30. package/dist/browser/{chunk-N4Q2WMOE.js → chunk-MIB3EOC4.js} +5 -5
  31. package/dist/browser/{chunk-N4Q2WMOE.js.map → chunk-MIB3EOC4.js.map} +1 -1
  32. package/dist/browser/{chunk-P7XWSMFX.cjs → chunk-QAFFDJFK.cjs} +6 -6
  33. package/dist/browser/{chunk-P7XWSMFX.cjs.map → chunk-QAFFDJFK.cjs.map} +1 -1
  34. package/dist/{chunk-7PGR4NJG.js → browser/chunk-UL5TNXQL.js} +4 -4
  35. package/dist/browser/{chunk-MOBYR3AP.js.map → chunk-UL5TNXQL.js.map} +1 -1
  36. package/dist/browser/{chunk-Y3BOPSM3.js → chunk-V7AUPQIJ.js} +5 -5
  37. package/dist/browser/chunk-V7AUPQIJ.js.map +1 -0
  38. package/dist/browser/{chunk-VSG4EWNL.cjs → chunk-WGWPCVYB.cjs} +18 -18
  39. package/dist/browser/{chunk-VSG4EWNL.cjs.map → chunk-WGWPCVYB.cjs.map} +1 -1
  40. package/dist/browser/{chunk-YSJVJ3Z3.cjs → chunk-WR2SQLKP.cjs} +7 -7
  41. package/dist/browser/{chunk-YSJVJ3Z3.cjs.map → chunk-WR2SQLKP.cjs.map} +1 -1
  42. package/dist/browser/{chunk-XLQSSJUZ.cjs → chunk-ZVVUZRG7.cjs} +15 -15
  43. package/dist/browser/{chunk-XLQSSJUZ.cjs.map → chunk-ZVVUZRG7.cjs.map} +1 -1
  44. package/dist/browser/index.cjs +91 -90
  45. package/dist/browser/index.cjs.map +1 -1
  46. package/dist/browser/index.js +9 -8
  47. package/dist/browser/index.js.map +1 -1
  48. package/dist/browser/integrations/pino.cjs +4 -4
  49. package/dist/browser/integrations/pino.js +3 -3
  50. package/dist/browser/langchain/index.cjs +17 -17
  51. package/dist/browser/langchain/index.js +2 -2
  52. package/dist/browser/langgraph/index.cjs +27 -27
  53. package/dist/browser/langgraph/index.js +5 -5
  54. package/dist/browser/openai/index.cjs +11 -11
  55. package/dist/browser/openai/index.js +2 -2
  56. package/dist/browser/openai-agents/index.cjs +34 -34
  57. package/dist/browser/openai-agents/index.js +4 -4
  58. package/dist/browser/vercel-ai/index.cjs +14 -14
  59. package/dist/browser/vercel-ai/index.js +3 -3
  60. package/dist/{browser/chunk-KGSLJLWW.js → chunk-7I4ILCLY.js} +4 -4
  61. package/dist/{chunk-HCEKAVDR.js.map → chunk-7I4ILCLY.js.map} +1 -1
  62. package/dist/{chunk-3XQOZXJM.cjs → chunk-DGBQ3UI6.cjs} +7 -7
  63. package/dist/{chunk-3XQOZXJM.cjs.map → chunk-DGBQ3UI6.cjs.map} +1 -1
  64. package/dist/{chunk-YQ6ZA7OE.cjs → chunk-DPW4W7H7.cjs} +18 -18
  65. package/dist/{chunk-YQ6ZA7OE.cjs.map → chunk-DPW4W7H7.cjs.map} +1 -1
  66. package/dist/{chunk-E3G3IE6Y.cjs → chunk-DQ4T2MJE.cjs} +5 -4
  67. package/dist/chunk-DQ4T2MJE.cjs.map +1 -0
  68. package/dist/{chunk-XZRJXJFA.js → chunk-ELVBOJWY.js} +5 -5
  69. package/dist/chunk-ELVBOJWY.js.map +1 -0
  70. package/dist/{chunk-ZFE75G7S.js → chunk-HONYN2OI.js} +3 -3
  71. package/dist/{chunk-ZFE75G7S.js.map → chunk-HONYN2OI.js.map} +1 -1
  72. package/dist/{chunk-RK3JPX27.cjs → chunk-I3P5CHBV.cjs} +12 -12
  73. package/dist/{chunk-RK3JPX27.cjs.map → chunk-I3P5CHBV.cjs.map} +1 -1
  74. package/dist/{browser/chunk-MOBYR3AP.js → chunk-MNV4HASC.js} +4 -4
  75. package/dist/{chunk-7PGR4NJG.js.map → chunk-MNV4HASC.js.map} +1 -1
  76. package/dist/{chunk-XHOLQGN6.js → chunk-NR36TJMT.js} +5 -5
  77. package/dist/{chunk-XHOLQGN6.js.map → chunk-NR36TJMT.js.map} +1 -1
  78. package/dist/{chunk-T2NESDMT.js → chunk-QZDUGEIR.js} +3 -3
  79. package/dist/{chunk-T2NESDMT.js.map → chunk-QZDUGEIR.js.map} +1 -1
  80. package/dist/{chunk-TGHDV5MC.cjs → chunk-RRE2LX2A.cjs} +6 -6
  81. package/dist/{chunk-TGHDV5MC.cjs.map → chunk-RRE2LX2A.cjs.map} +1 -1
  82. package/dist/{chunk-DPGUBZVO.cjs → chunk-SJ625SQS.cjs} +15 -15
  83. package/dist/{chunk-DPGUBZVO.cjs.map → chunk-SJ625SQS.cjs.map} +1 -1
  84. package/dist/{chunk-UAJO6JM7.js → chunk-YKPZQPEW.js} +3 -3
  85. package/dist/{chunk-UAJO6JM7.js.map → chunk-YKPZQPEW.js.map} +1 -1
  86. package/dist/{chunk-NGBJJGOC.cjs → chunk-Z5MFR7UE.cjs} +5 -5
  87. package/dist/{chunk-NGBJJGOC.cjs.map → chunk-Z5MFR7UE.cjs.map} +1 -1
  88. package/dist/index.cjs +104 -98
  89. package/dist/index.cjs.map +1 -1
  90. package/dist/index.js +16 -10
  91. package/dist/index.js.map +1 -1
  92. package/dist/integrations/pino.cjs +4 -4
  93. package/dist/integrations/pino.js +3 -3
  94. package/dist/langchain/index.cjs +17 -17
  95. package/dist/langchain/index.js +2 -2
  96. package/dist/langgraph/index.cjs +27 -27
  97. package/dist/langgraph/index.js +5 -5
  98. package/dist/openai/index.cjs +11 -11
  99. package/dist/openai/index.js +2 -2
  100. package/dist/openai-agents/index.cjs +34 -34
  101. package/dist/openai-agents/index.js +4 -4
  102. package/dist/vercel-ai/index.cjs +14 -14
  103. package/dist/vercel-ai/index.js +3 -3
  104. package/package.json +14 -13
  105. package/dist/browser/chunk-PM5D6VXF.cjs.map +0 -1
  106. package/dist/browser/chunk-Y3BOPSM3.js.map +0 -1
  107. package/dist/chunk-E3G3IE6Y.cjs.map +0 -1
  108. package/dist/chunk-XZRJXJFA.js.map +0 -1
@@ -1,17 +1,15 @@
1
- import { extractAnthropicSegments } from '../chunk-N4Q2WMOE.js';
2
- import '../chunk-WUQXNRD3.js';
3
- import { summarizeToolInput, summarizeToolOutput } from '../chunk-LGG5PL5U.js';
4
- import { emitAgentSpan, emitHookSpan, emitLlmSpan, emitToolSpan } from '../chunk-MOBYR3AP.js';
5
- import { absorbErrorsAsync, absorbErrorsSync } from '../chunk-MI4OUWUZ.js';
1
+ import { extractAnthropicSegments } from '../chunk-MIB3EOC4.js';
2
+ import '../chunk-GLL57K7P.js';
3
+ import { summarizeToolInput, summarizeToolOutput } from '../chunk-64YI3IZS.js';
4
+ import { emitHookSpan, emitAgentSpan, emitLlmSpan, emitToolSpan } from '../chunk-UL5TNXQL.js';
5
+ import { absorbErrorsAsync, absorbErrorsSync } from '../chunk-EH326H6U.js';
6
6
  import { calculateCost } from '../chunk-4FALQMOZ.js';
7
7
  import '../chunk-MLDVXMQD.js';
8
- import { init_tracing, getActiveTraceState, runAsync, startSpan, init_diagnostics, report } from '../chunk-Y3BOPSM3.js';
8
+ import { init_tracing, getActiveTraceState, startSpan, init_diagnostics, runAsync, report } from '../chunk-V7AUPQIJ.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