@struct-ai/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +168 -0
  3. package/dist/commonjs/content-capture.d.ts +10 -0
  4. package/dist/commonjs/content-capture.js +23 -0
  5. package/dist/commonjs/context.d.ts +30 -0
  6. package/dist/commonjs/context.js +94 -0
  7. package/dist/commonjs/core.d.ts +120 -0
  8. package/dist/commonjs/core.js +408 -0
  9. package/dist/commonjs/events.d.ts +12 -0
  10. package/dist/commonjs/events.js +103 -0
  11. package/dist/commonjs/index.d.ts +6 -0
  12. package/dist/commonjs/index.js +10 -0
  13. package/dist/commonjs/integrations/anthropic-content.d.ts +24 -0
  14. package/dist/commonjs/integrations/anthropic-content.js +178 -0
  15. package/dist/commonjs/integrations/anthropic.d.ts +34 -0
  16. package/dist/commonjs/integrations/anthropic.js +377 -0
  17. package/dist/commonjs/integrations/index.d.ts +5 -0
  18. package/dist/commonjs/integrations/index.js +47 -0
  19. package/dist/commonjs/integrations/langchain-callback.d.ts +134 -0
  20. package/dist/commonjs/integrations/langchain-callback.js +723 -0
  21. package/dist/commonjs/integrations/langchain-content.d.ts +35 -0
  22. package/dist/commonjs/integrations/langchain-content.js +307 -0
  23. package/dist/commonjs/integrations/langchain.d.ts +11 -0
  24. package/dist/commonjs/integrations/langchain.js +100 -0
  25. package/dist/commonjs/package.json +3 -0
  26. package/dist/commonjs/semconv.d.ts +54 -0
  27. package/dist/commonjs/semconv.js +74 -0
  28. package/dist/commonjs/truncation.d.ts +10 -0
  29. package/dist/commonjs/truncation.js +103 -0
  30. package/dist/esm/content-capture.d.ts +10 -0
  31. package/dist/esm/content-capture.js +17 -0
  32. package/dist/esm/context.d.ts +30 -0
  33. package/dist/esm/context.js +80 -0
  34. package/dist/esm/core.d.ts +120 -0
  35. package/dist/esm/core.js +403 -0
  36. package/dist/esm/events.d.ts +12 -0
  37. package/dist/esm/events.js +99 -0
  38. package/dist/esm/index.d.ts +6 -0
  39. package/dist/esm/index.js +5 -0
  40. package/dist/esm/integrations/anthropic-content.d.ts +24 -0
  41. package/dist/esm/integrations/anthropic-content.js +168 -0
  42. package/dist/esm/integrations/anthropic.d.ts +34 -0
  43. package/dist/esm/integrations/anthropic.js +370 -0
  44. package/dist/esm/integrations/index.d.ts +5 -0
  45. package/dist/esm/integrations/index.js +42 -0
  46. package/dist/esm/integrations/langchain-callback.d.ts +134 -0
  47. package/dist/esm/integrations/langchain-callback.js +719 -0
  48. package/dist/esm/integrations/langchain-content.d.ts +35 -0
  49. package/dist/esm/integrations/langchain-content.js +294 -0
  50. package/dist/esm/integrations/langchain.d.ts +11 -0
  51. package/dist/esm/integrations/langchain.js +95 -0
  52. package/dist/esm/package.json +3 -0
  53. package/dist/esm/semconv.d.ts +54 -0
  54. package/dist/esm/semconv.js +71 -0
  55. package/dist/esm/truncation.d.ts +10 -0
  56. package/dist/esm/truncation.js +96 -0
  57. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/content-capture.d.ts +10 -0
  58. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/context.d.ts +30 -0
  59. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/core.d.ts +120 -0
  60. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/events.d.ts +12 -0
  61. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/index.d.ts +6 -0
  62. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/anthropic-content.d.ts +24 -0
  63. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/anthropic.d.ts +34 -0
  64. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/index.d.ts +5 -0
  65. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/langchain-callback.d.ts +134 -0
  66. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/langchain-content.d.ts +35 -0
  67. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/integrations/langchain.d.ts +11 -0
  68. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/package.json +3 -0
  69. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/semconv.d.ts +54 -0
  70. package/dist/node_modules/@struct-ai/sdk/dist/commonjs/truncation.d.ts +10 -0
  71. package/dist/node_modules/@struct-ai/sdk/dist/esm/content-capture.d.ts +10 -0
  72. package/dist/node_modules/@struct-ai/sdk/dist/esm/context.d.ts +30 -0
  73. package/dist/node_modules/@struct-ai/sdk/dist/esm/core.d.ts +120 -0
  74. package/dist/node_modules/@struct-ai/sdk/dist/esm/events.d.ts +12 -0
  75. package/dist/node_modules/@struct-ai/sdk/dist/esm/index.d.ts +6 -0
  76. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/anthropic-content.d.ts +24 -0
  77. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/anthropic.d.ts +34 -0
  78. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/index.d.ts +5 -0
  79. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/langchain-callback.d.ts +134 -0
  80. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/langchain-content.d.ts +35 -0
  81. package/dist/node_modules/@struct-ai/sdk/dist/esm/integrations/langchain.d.ts +11 -0
  82. package/dist/node_modules/@struct-ai/sdk/dist/esm/package.json +3 -0
  83. package/dist/node_modules/@struct-ai/sdk/dist/esm/semconv.d.ts +54 -0
  84. package/dist/node_modules/@struct-ai/sdk/dist/esm/truncation.d.ts +10 -0
  85. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/dist/commonjs/package.json +3 -0
  86. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/dist/esm/package.json +3 -0
  87. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/package.json +100 -0
  88. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/package.json +103 -0
  89. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/anthropic/package.json +118 -0
  90. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/package.json +916 -0
  91. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/package.json +188 -0
  92. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/api/package.json +96 -0
  93. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/api-logs/package.json +91 -0
  94. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/core/package.json +96 -0
  95. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/exporter-logs-otlp-proto/package.json +108 -0
  96. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/exporter-trace-otlp-proto/package.json +105 -0
  97. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/resources/package.json +100 -0
  98. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-logs/package.json +109 -0
  99. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-trace-base/package.json +100 -0
  100. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-trace-node/package.json +76 -0
  101. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/package.json +140 -0
  102. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/nock/package.json +89 -0
  103. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/tshy/package.json +81 -0
  104. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/typescript/package.json +120 -0
  105. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/node_modules/vitest/package.json +239 -0
  106. package/dist/node_modules/@struct-ai/sdk/dist/node_modules/@struct-ai/sdk/package.json +100 -0
  107. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/core.d.ts +244 -0
  108. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/error.d.ts +54 -0
  109. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/index.d.ts +188 -0
  110. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/package.json +103 -0
  111. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/pagination.d.ts +26 -0
  112. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/resource.d.ts +6 -0
  113. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/streaming.d.ts +41 -0
  114. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/uploads.d.ts +75 -0
  115. package/dist/node_modules/@struct-ai/sdk/node_modules/@anthropic-ai/sdk/version.d.ts +2 -0
  116. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/anthropic/experimental.d.ts +1 -0
  117. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/anthropic/index.d.ts +1 -0
  118. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/anthropic/package.json +118 -0
  119. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/agents.d.ts +1 -0
  120. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/caches.d.ts +1 -0
  121. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/chat_history.d.ts +1 -0
  122. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/context.d.ts +1 -0
  123. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/documents.d.ts +1 -0
  124. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/embeddings.d.ts +1 -0
  125. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/example_selectors.d.ts +1 -0
  126. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/index.d.ts +1 -0
  127. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/indexing.d.ts +1 -0
  128. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/load.d.ts +1 -0
  129. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/memory.d.ts +1 -0
  130. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/messages.d.ts +1 -0
  131. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/output_parsers.d.ts +1 -0
  132. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/outputs.d.ts +1 -0
  133. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/package.json +916 -0
  134. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/prompt_values.d.ts +1 -0
  135. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/prompts.d.ts +1 -0
  136. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/retrievers.d.ts +1 -0
  137. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/runnables.d.ts +1 -0
  138. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/singletons.d.ts +1 -0
  139. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/stores.d.ts +1 -0
  140. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/structured_query.d.ts +1 -0
  141. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/tools.d.ts +1 -0
  142. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/core/vectorstores.d.ts +1 -0
  143. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/index.d.ts +1 -0
  144. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/package.json +188 -0
  145. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/prebuilt.d.ts +1 -0
  146. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/pregel.d.ts +1 -0
  147. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/remote.d.ts +1 -0
  148. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/web.d.ts +1 -0
  149. package/dist/node_modules/@struct-ai/sdk/node_modules/@langchain/langgraph/zod.d.ts +1 -0
  150. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/api/package.json +96 -0
  151. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/api-logs/package.json +91 -0
  152. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/core/package.json +96 -0
  153. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/exporter-logs-otlp-proto/package.json +108 -0
  154. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/exporter-trace-otlp-proto/package.json +105 -0
  155. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/resources/package.json +100 -0
  156. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-logs/package.json +109 -0
  157. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-trace-base/package.json +100 -0
  158. package/dist/node_modules/@struct-ai/sdk/node_modules/@opentelemetry/sdk-trace-node/package.json +76 -0
  159. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/assert.d.ts +1062 -0
  160. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/async_hooks.d.ts +605 -0
  161. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/buffer.buffer.d.ts +471 -0
  162. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/buffer.d.ts +1936 -0
  163. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/child_process.d.ts +1475 -0
  164. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/cluster.d.ts +577 -0
  165. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/console.d.ts +452 -0
  166. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/constants.d.ts +21 -0
  167. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/crypto.d.ts +4590 -0
  168. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/dgram.d.ts +597 -0
  169. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/diagnostics_channel.d.ts +578 -0
  170. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/dns.d.ts +871 -0
  171. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/domain.d.ts +170 -0
  172. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/events.d.ts +977 -0
  173. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/fs.d.ts +4375 -0
  174. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/globals.d.ts +172 -0
  175. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/globals.typedarray.d.ts +38 -0
  176. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/http.d.ts +2049 -0
  177. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/http2.d.ts +2631 -0
  178. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/https.d.ts +578 -0
  179. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/index.d.ts +93 -0
  180. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/inspector.generated.d.ts +3966 -0
  181. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/module.d.ts +539 -0
  182. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/net.d.ts +1031 -0
  183. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/os.d.ts +506 -0
  184. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/package.json +140 -0
  185. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/path.d.ts +200 -0
  186. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/perf_hooks.d.ts +961 -0
  187. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/process.d.ts +1961 -0
  188. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/punycode.d.ts +117 -0
  189. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/querystring.d.ts +152 -0
  190. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/readline.d.ts +589 -0
  191. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/repl.d.ts +430 -0
  192. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/sea.d.ts +153 -0
  193. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/stream.d.ts +1698 -0
  194. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/string_decoder.d.ts +67 -0
  195. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/test.d.ts +1787 -0
  196. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/timers.d.ts +286 -0
  197. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/tls.d.ts +1259 -0
  198. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/trace_events.d.ts +197 -0
  199. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/tty.d.ts +208 -0
  200. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/url.d.ts +964 -0
  201. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/util.d.ts +2331 -0
  202. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/v8.d.ts +809 -0
  203. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/vm.d.ts +1001 -0
  204. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/wasi.d.ts +181 -0
  205. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/worker_threads.d.ts +715 -0
  206. package/dist/node_modules/@struct-ai/sdk/node_modules/@types/node/zlib.d.ts +598 -0
  207. package/dist/node_modules/@struct-ai/sdk/node_modules/nock/package.json +89 -0
  208. package/dist/node_modules/@struct-ai/sdk/node_modules/tshy/package.json +81 -0
  209. package/dist/node_modules/@struct-ai/sdk/node_modules/typescript/package.json +120 -0
  210. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/config.d.ts +3 -0
  211. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/coverage.d.ts +1 -0
  212. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/environments.d.ts +1 -0
  213. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/globals.d.ts +22 -0
  214. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/import-meta.d.ts +5 -0
  215. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/importMeta.d.ts +4 -0
  216. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/jsdom.d.ts +6 -0
  217. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/mocker.d.ts +1 -0
  218. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/node.d.ts +1 -0
  219. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/optional-runtime-types.d.ts +6 -0
  220. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/optional-types.d.ts +7 -0
  221. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/package.json +239 -0
  222. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/reporters.d.ts +1 -0
  223. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/runners.d.ts +1 -0
  224. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/snapshot.d.ts +1 -0
  225. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/suite.d.ts +1 -0
  226. package/dist/node_modules/@struct-ai/sdk/node_modules/vitest/worker.d.ts +1 -0
  227. package/dist/node_modules/@struct-ai/sdk/package.json +100 -0
  228. package/package.json +100 -0
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.truncateField = void 0;
4
+ exports.iterToolUses = iterToolUses;
5
+ exports.contentToParts = contentToParts;
6
+ exports.toInputMessages = toInputMessages;
7
+ exports.toOutputMessages = toOutputMessages;
8
+ exports.toSystemInstructions = toSystemInstructions;
9
+ exports.safeJsonForTool = safeJsonForTool;
10
+ exports.lastUserMessageParts = lastUserMessageParts;
11
+ const truncation_js_1 = require("../truncation.js");
12
+ Object.defineProperty(exports, "truncateField", { enumerable: true, get: function () { return truncation_js_1.truncateField; } });
13
+ const semconv_js_1 = require("../semconv.js");
14
+ /**
15
+ * Walk Anthropic response content blocks, return (name, id) pairs for tool_use blocks.
16
+ * Port of _iter_tool_uses from anthropic.py.
17
+ */
18
+ function iterToolUses(blocks) {
19
+ const out = [];
20
+ if (!Array.isArray(blocks))
21
+ return out;
22
+ for (const block of blocks) {
23
+ if (!block || typeof block !== "object")
24
+ continue;
25
+ const b = block;
26
+ if (b.type !== "tool_use")
27
+ continue;
28
+ const name = typeof b.name === "string" ? b.name : "";
29
+ const id = typeof b.id === "string" ? b.id : "";
30
+ if (name && id)
31
+ out.push([name, id]);
32
+ }
33
+ return out;
34
+ }
35
+ /**
36
+ * Convert arbitrary content (string | block[] | unknown) → GenAI parts.
37
+ * Port of _content_to_parts from anthropic.py.
38
+ */
39
+ function contentToParts(content) {
40
+ if (content == null)
41
+ return [];
42
+ if (typeof content === "string") {
43
+ return [{ type: "text", content }];
44
+ }
45
+ if (!Array.isArray(content)) {
46
+ return [{ type: "text", content: String(content) }];
47
+ }
48
+ const parts = [];
49
+ for (const item of content) {
50
+ if (item == null)
51
+ continue;
52
+ if (typeof item === "string") {
53
+ parts.push({ type: "text", content: item });
54
+ continue;
55
+ }
56
+ if (typeof item !== "object")
57
+ continue;
58
+ const block = item;
59
+ const blockType = typeof block.type === "string" ? block.type : undefined;
60
+ if (blockType === "text") {
61
+ const text = typeof block.text === "string" ? block.text : "";
62
+ parts.push({ type: "text", content: text });
63
+ }
64
+ else if (blockType === "tool_use") {
65
+ const name = typeof block.name === "string" ? block.name : "";
66
+ const part = { type: "tool_call", name };
67
+ if (block.id)
68
+ part.id = block.id;
69
+ if (block.input !== undefined)
70
+ part.arguments = block.input;
71
+ parts.push(part);
72
+ }
73
+ else if (blockType === "tool_result") {
74
+ const part = { type: "tool_call_response" };
75
+ if (block.tool_use_id)
76
+ part.id = block.tool_use_id;
77
+ part.response = block.content ?? "";
78
+ parts.push(part);
79
+ }
80
+ else if (blockType === "thinking") {
81
+ const thinking = typeof block.thinking === "string" ? block.thinking : "";
82
+ parts.push({ type: "reasoning", content: thinking });
83
+ }
84
+ else if (blockType === "image") {
85
+ const source = block.source ?? {};
86
+ if (source.type === "base64" && typeof source.data === "string") {
87
+ parts.push({
88
+ type: "blob",
89
+ modality: "image",
90
+ content: source.data.slice(0, 256) + "...",
91
+ mime_type: source.media_type,
92
+ });
93
+ }
94
+ else if (source.type === "url" && typeof source.url === "string") {
95
+ parts.push({
96
+ type: "uri",
97
+ modality: "image",
98
+ uri: source.url,
99
+ });
100
+ }
101
+ else {
102
+ parts.push({ type: "image" });
103
+ }
104
+ }
105
+ else {
106
+ parts.push({ type: blockType || "unknown" });
107
+ }
108
+ }
109
+ return parts;
110
+ }
111
+ /** Convert Anthropic message list → GenAI input-messages JSON string. */
112
+ function toInputMessages(messages) {
113
+ if (!Array.isArray(messages))
114
+ return "[]";
115
+ try {
116
+ const result = messages.map((msg) => {
117
+ const m = (msg ?? {});
118
+ const role = typeof m.role === "string" ? m.role : "user";
119
+ const parts = contentToParts(m.content);
120
+ return { role, parts };
121
+ });
122
+ return (0, truncation_js_1.truncateAndSerialize)(result);
123
+ }
124
+ catch {
125
+ return "[]";
126
+ }
127
+ }
128
+ /** Convert response content blocks → GenAI output-messages JSON string. */
129
+ function toOutputMessages(contentBlocks, stopReason) {
130
+ try {
131
+ const parts = contentToParts(contentBlocks);
132
+ const msg = { role: "assistant", parts };
133
+ if (stopReason) {
134
+ msg.finish_reason = semconv_js_1.ANTHROPIC_FINISH_REASON_MAP[stopReason] ?? stopReason;
135
+ }
136
+ return (0, truncation_js_1.truncateAndSerialize)([msg]);
137
+ }
138
+ catch {
139
+ return "[]";
140
+ }
141
+ }
142
+ /** Convert system prompt (string | block[]) → GenAI system_instructions JSON string. */
143
+ function toSystemInstructions(system) {
144
+ try {
145
+ if (typeof system === "string") {
146
+ return (0, truncation_js_1.truncateAndSerialize)([{ type: "text", content: system }]);
147
+ }
148
+ if (Array.isArray(system)) {
149
+ const parts = contentToParts(system);
150
+ return (0, truncation_js_1.truncateAndSerialize)(parts);
151
+ }
152
+ return "[]";
153
+ }
154
+ catch {
155
+ return "[]";
156
+ }
157
+ }
158
+ function safeJsonForTool(obj) {
159
+ try {
160
+ return (0, truncation_js_1.truncateAndSerialize)(obj);
161
+ }
162
+ catch {
163
+ return (0, truncation_js_1.safeJsonStringify)(obj);
164
+ }
165
+ }
166
+ /** Extract the last user message's parts for parent-span propagation. */
167
+ function lastUserMessageParts(messages) {
168
+ if (!Array.isArray(messages))
169
+ return undefined;
170
+ for (let i = messages.length - 1; i >= 0; i--) {
171
+ const msg = messages[i];
172
+ if (msg && msg.role === "user") {
173
+ return contentToParts(msg.content);
174
+ }
175
+ }
176
+ return undefined;
177
+ }
178
+ //# sourceMappingURL=anthropic-content.js.map
@@ -0,0 +1,34 @@
1
+ import { type Tracer } from "@opentelemetry/api";
2
+ import type { Logger } from "@opentelemetry/api-logs";
3
+ import { type StructSDK } from "../core.js";
4
+ interface PatchContext {
5
+ tracer: Tracer;
6
+ sdk: StructSDK;
7
+ logger: Logger | undefined;
8
+ }
9
+ interface CreateParams {
10
+ model?: string;
11
+ max_tokens?: number;
12
+ temperature?: number;
13
+ top_p?: number;
14
+ top_k?: number;
15
+ stop_sequences?: string[];
16
+ messages?: unknown[];
17
+ system?: unknown;
18
+ tools?: unknown[];
19
+ stream?: boolean;
20
+ }
21
+ export declare function patch(sdk: StructSDK): Promise<void>;
22
+ export declare function unpatch(): Promise<void>;
23
+ type CreateMethod = (this: unknown, params: CreateParams, opts?: unknown) => unknown;
24
+ type StreamMethod = (this: unknown, params: CreateParams, opts?: unknown) => unknown;
25
+ /** @internal */
26
+ export type _PatchContextForTest = PatchContext;
27
+ /** @internal */
28
+ export declare function _wrapCreateForTest(original: CreateMethod): CreateMethod;
29
+ /** @internal */
30
+ export declare function _wrapStreamForTest(original: StreamMethod): StreamMethod;
31
+ /** @internal */
32
+ export declare function _setActivePatchCtxForTest(ctx: PatchContext | undefined): void;
33
+ export {};
34
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1,377 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.patch = patch;
4
+ exports.unpatch = unpatch;
5
+ exports._wrapCreateForTest = _wrapCreateForTest;
6
+ exports._wrapStreamForTest = _wrapStreamForTest;
7
+ exports._setActivePatchCtxForTest = _setActivePatchCtxForTest;
8
+ const api_1 = require("@opentelemetry/api");
9
+ const context_js_1 = require("../context.js");
10
+ function childParentContext() {
11
+ const agentSpan = (0, context_js_1.getAgentSpan)();
12
+ return agentSpan
13
+ ? api_1.trace.setSpan(api_1.context.active(), agentSpan)
14
+ : api_1.context.active();
15
+ }
16
+ const core_js_1 = require("../core.js");
17
+ const events_js_1 = require("../events.js");
18
+ const semconv_js_1 = require("../semconv.js");
19
+ const truncation_js_1 = require("../truncation.js");
20
+ const anthropic_content_js_1 = require("./anthropic-content.js");
21
+ const STRUCT_WRAPPED = Symbol.for("struct.wrapped");
22
+ const STRUCT_ORIGINAL = Symbol.for("struct.original");
23
+ /**
24
+ * Mutable reference updated on each patch() call. Wrappers dereference this
25
+ * at call time, so the most recently initialized SDK always wins — matches
26
+ * the fact that `struct` is a singleton in production, and keeps tests with
27
+ * multiple StructSDK instances working correctly.
28
+ */
29
+ const activePatchCtx = { value: undefined };
30
+ async function patch(sdk) {
31
+ const anthropicMod = (await import("@anthropic-ai/sdk"));
32
+ activePatchCtx.value = {
33
+ tracer: sdk.getTracer("struct-sdk-anthropic"),
34
+ sdk,
35
+ logger: sdk.emitEvents ? sdk.getLogger("struct-sdk-anthropic") : undefined,
36
+ };
37
+ const classes = collectMessagesClasses(anthropicMod);
38
+ for (const cls of classes) {
39
+ wrapMethod(cls.prototype, "create", false);
40
+ wrapMethod(cls.prototype, "stream", true);
41
+ }
42
+ // Best-effort: also patch bedrock + vertex packages if installed.
43
+ await tryPatchCompanion("@anthropic-ai/bedrock-sdk");
44
+ await tryPatchCompanion("@anthropic-ai/vertex-sdk");
45
+ }
46
+ async function unpatch() {
47
+ activePatchCtx.value = undefined;
48
+ try {
49
+ const anthropicMod = (await import("@anthropic-ai/sdk"));
50
+ const classes = collectMessagesClasses(anthropicMod);
51
+ for (const cls of classes) {
52
+ restoreMethod(cls.prototype, "create");
53
+ restoreMethod(cls.prototype, "stream");
54
+ }
55
+ }
56
+ catch {
57
+ /* not installed */
58
+ }
59
+ }
60
+ function collectMessagesClasses(mod) {
61
+ const classes = [];
62
+ const Anthropic = mod.default ?? mod.Anthropic;
63
+ if (typeof Anthropic === "function") {
64
+ const ctor = Anthropic;
65
+ if (typeof ctor.Messages === "function") {
66
+ classes.push(ctor.Messages);
67
+ }
68
+ if (ctor.Beta && typeof ctor.Beta.Messages === "function") {
69
+ classes.push(ctor.Beta.Messages);
70
+ }
71
+ }
72
+ return classes;
73
+ }
74
+ async function tryPatchCompanion(moduleName) {
75
+ try {
76
+ const mod = (await import(moduleName));
77
+ const classes = collectMessagesClasses(mod);
78
+ for (const cls of classes) {
79
+ wrapMethod(cls.prototype, "create", false);
80
+ wrapMethod(cls.prototype, "stream", true);
81
+ }
82
+ }
83
+ catch {
84
+ /* not installed */
85
+ }
86
+ }
87
+ function wrapMethod(proto, methodName, isStream) {
88
+ const original = proto[methodName];
89
+ if (typeof original !== "function")
90
+ return;
91
+ const existing = original;
92
+ if (existing[STRUCT_WRAPPED])
93
+ return;
94
+ const wrapper = isStream
95
+ ? wrapStream(original)
96
+ : wrapCreate(original);
97
+ wrapper[STRUCT_WRAPPED] = true;
98
+ wrapper[STRUCT_ORIGINAL] = original;
99
+ proto[methodName] = wrapper;
100
+ }
101
+ function restoreMethod(proto, methodName) {
102
+ const current = proto[methodName];
103
+ if (current && current[STRUCT_WRAPPED] && current[STRUCT_ORIGINAL]) {
104
+ proto[methodName] = current[STRUCT_ORIGINAL];
105
+ }
106
+ }
107
+ function wrapCreate(original) {
108
+ return function wrappedCreate(params, opts) {
109
+ const patchCtx = activePatchCtx.value;
110
+ if (!patchCtx) {
111
+ return original.call(this, params, opts);
112
+ }
113
+ // If params.stream is true, Messages.create delegates to the streaming path.
114
+ // We still need to instrument the returned stream like we do in messages.stream.
115
+ if (params && params.stream === true) {
116
+ return wrapStream(original).call(this, params, opts);
117
+ }
118
+ const { tracer, sdk, logger } = patchCtx;
119
+ const internalLogger = sdk.getInternalLogger();
120
+ const model = params?.model ?? "unknown";
121
+ // Span creation itself can fail (custom tracer, broken context). If it
122
+ // does, fall through to calling `original` directly so the user's API
123
+ // call always runs. Mirrors the Python `_wrap_create` fall-through.
124
+ let span;
125
+ let ctx;
126
+ (0, core_js_1.safe)(() => {
127
+ const parentCtx = childParentContext();
128
+ span = tracer.startSpan(`chat ${model}`, { kind: api_1.SpanKind.CLIENT }, parentCtx);
129
+ ctx = api_1.trace.setSpan(parentCtx, span);
130
+ }, "anthropic.create.start_span", internalLogger);
131
+ if (!span || !ctx) {
132
+ return original.call(this, params, opts);
133
+ }
134
+ const liveSpan = span;
135
+ const liveCtx = ctx;
136
+ // Enter the span's context BEFORE emitting request-side log records so
137
+ // `logger.emit({ context: otelContext.active() })` picks up the chat
138
+ // span as the log's TraceId / SpanId. Without this the UI can't link
139
+ // user/system message logs back to the chat span.
140
+ return api_1.context.with(liveCtx, () => {
141
+ (0, core_js_1.safe)(() => setChatRequestAttrs(liveSpan, params, sdk, logger), "anthropic.create.set_request_attrs", internalLogger);
142
+ const promise = Promise.resolve(original.call(this, params, opts));
143
+ return promise.then((result) => {
144
+ (0, core_js_1.safe)(() => setChatResponseAttrs(liveSpan, sdk, result, logger), "anthropic.create.set_response_attrs", internalLogger);
145
+ (0, core_js_1.safe)(() => liveSpan.setStatus({ code: api_1.SpanStatusCode.OK }), "anthropic.create.set_ok_status", internalLogger);
146
+ (0, core_js_1.safe)(() => liveSpan.end(), "anthropic.create.span_end", internalLogger);
147
+ return result;
148
+ }, (err) => {
149
+ (0, core_js_1.safe)(() => recordErrorOnSpan(liveSpan, err), "anthropic.create.record_error", internalLogger);
150
+ (0, core_js_1.safe)(() => liveSpan.end(), "anthropic.create.span_end_on_error", internalLogger);
151
+ // User's API exception always propagates unchanged.
152
+ throw err;
153
+ });
154
+ });
155
+ };
156
+ }
157
+ function wrapStream(original) {
158
+ return function wrappedStream(params, opts) {
159
+ const patchCtx = activePatchCtx.value;
160
+ if (!patchCtx) {
161
+ return original.call(this, params, opts);
162
+ }
163
+ const { tracer, sdk, logger } = patchCtx;
164
+ const internalLogger = sdk.getInternalLogger();
165
+ const model = params?.model ?? "unknown";
166
+ // Span creation itself can fail. If it does, fall through to calling
167
+ // `original` directly so the user's stream call always runs. Mirrors
168
+ // the Python `_wrap_stream` fall-through pattern.
169
+ let span;
170
+ let ctx;
171
+ (0, core_js_1.safe)(() => {
172
+ const parentCtx = childParentContext();
173
+ span = tracer.startSpan(`chat ${model}`, { kind: api_1.SpanKind.CLIENT }, parentCtx);
174
+ ctx = api_1.trace.setSpan(parentCtx, span);
175
+ }, "anthropic.stream.start_span", internalLogger);
176
+ if (!span || !ctx) {
177
+ return original.call(this, params, opts);
178
+ }
179
+ const liveSpan = span;
180
+ const liveCtx = ctx;
181
+ const storeSnapshot = (0, context_js_1.snapshotStore)();
182
+ // Enter the span's context BEFORE request-side log emission so logs
183
+ // link back to the chat span via TraceId. Same rationale as wrapCreate.
184
+ // @anthropic-ai/sdk's `stream()` returns a MessageStream synchronously.
185
+ const stream = api_1.context.with(liveCtx, () => {
186
+ (0, core_js_1.safe)(() => setChatRequestAttrs(liveSpan, params, sdk, logger), "anthropic.stream.set_request_attrs", internalLogger);
187
+ return original.call(this, params, opts);
188
+ });
189
+ let finalized = false;
190
+ const finalize = (result, err) => {
191
+ if (finalized)
192
+ return;
193
+ finalized = true;
194
+ (0, context_js_1.runWithStore)(storeSnapshot, () => {
195
+ if (err) {
196
+ (0, core_js_1.safe)(() => recordErrorOnSpan(liveSpan, err), "anthropic.stream.record_error", internalLogger);
197
+ }
198
+ else if (result) {
199
+ (0, core_js_1.safe)(() => api_1.context.with(liveCtx, () => setChatResponseAttrs(liveSpan, sdk, result, logger)), "anthropic.stream.set_response_attrs", internalLogger);
200
+ (0, core_js_1.safe)(() => liveSpan.setStatus({ code: api_1.SpanStatusCode.OK }), "anthropic.stream.set_ok_status", internalLogger);
201
+ }
202
+ else {
203
+ (0, core_js_1.safe)(() => liveSpan.setStatus({ code: api_1.SpanStatusCode.OK }), "anthropic.stream.set_ok_status", internalLogger);
204
+ }
205
+ (0, core_js_1.safe)(() => liveSpan.end(), "anthropic.stream.span_end", internalLogger);
206
+ });
207
+ };
208
+ const ee = stream;
209
+ if (ee && typeof ee.on === "function") {
210
+ ee.on("finalMessage", (msg) => {
211
+ finalize(msg, undefined);
212
+ });
213
+ ee.on("error", (err) => {
214
+ finalize(undefined, err);
215
+ });
216
+ // If the stream is created and never iterated / finalized, at least
217
+ // don't hang on test exit — "end" fires after iteration completes.
218
+ ee.on("end", () => {
219
+ // Only fires if no finalMessage / error fired first (rare path).
220
+ finalize(undefined, undefined);
221
+ });
222
+ }
223
+ // Wrap the async iterator so each .next() runs inside the captured ALS store.
224
+ if (ee && typeof ee[Symbol.asyncIterator] === "function") {
225
+ const originalIter = ee[Symbol.asyncIterator].bind(ee);
226
+ ee[Symbol.asyncIterator] = () => {
227
+ const inner = originalIter();
228
+ const wrapped = {
229
+ next() {
230
+ return (0, context_js_1.runWithStore)(storeSnapshot, () => inner.next());
231
+ },
232
+ return(value) {
233
+ return (0, context_js_1.runWithStore)(storeSnapshot, () => inner.return ? inner.return(value) : Promise.resolve({ value, done: true }));
234
+ },
235
+ throw(err) {
236
+ return (0, context_js_1.runWithStore)(storeSnapshot, () => inner.throw
237
+ ? inner.throw(err)
238
+ : Promise.reject(err));
239
+ },
240
+ };
241
+ return wrapped;
242
+ };
243
+ }
244
+ return stream;
245
+ };
246
+ }
247
+ function setChatRequestAttrs(span, params, sdk, logger) {
248
+ span.setAttribute(semconv_js_1.GEN_AI.OPERATION_NAME, "chat");
249
+ span.setAttribute(semconv_js_1.GEN_AI.PROVIDER_NAME, "anthropic");
250
+ const model = params?.model ?? "unknown";
251
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_MODEL, model);
252
+ const sessionId = (0, context_js_1.getSessionId)();
253
+ if (sessionId) {
254
+ span.setAttribute(semconv_js_1.GEN_AI.CONVERSATION_ID, sessionId);
255
+ }
256
+ if (params?.max_tokens !== undefined) {
257
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_MAX_TOKENS, params.max_tokens);
258
+ }
259
+ if (params?.temperature !== undefined) {
260
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_TEMPERATURE, params.temperature);
261
+ }
262
+ if (params?.top_p !== undefined) {
263
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_TOP_P, params.top_p);
264
+ }
265
+ if (params?.top_k !== undefined) {
266
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_TOP_K, params.top_k);
267
+ }
268
+ if (params?.stop_sequences && params.stop_sequences.length > 0) {
269
+ span.setAttribute(semconv_js_1.GEN_AI.REQUEST_STOP_SEQUENCES, params.stop_sequences);
270
+ }
271
+ if (Array.isArray(params?.messages)) {
272
+ span.setAttribute(semconv_js_1.STRUCT.INPUT_MESSAGE_COUNT, params.messages.length);
273
+ propagateUserPromptToParent(params.messages);
274
+ }
275
+ if (sdk.emitEvents && logger && Array.isArray(params?.messages)) {
276
+ (0, events_js_1.emitAnthropicMessageEvents)(logger, params.messages, params.system);
277
+ }
278
+ if (sdk.emitSpanContent) {
279
+ if (Array.isArray(params?.messages)) {
280
+ span.setAttribute(semconv_js_1.GEN_AI.INPUT_MESSAGES, (0, anthropic_content_js_1.toInputMessages)(params.messages));
281
+ }
282
+ if (params?.system) {
283
+ span.setAttribute(semconv_js_1.GEN_AI.SYSTEM_INSTRUCTIONS, (0, anthropic_content_js_1.toSystemInstructions)(params.system));
284
+ }
285
+ if (params?.tools && Array.isArray(params.tools)) {
286
+ span.setAttribute(semconv_js_1.GEN_AI.TOOL_DEFINITIONS, (0, anthropic_content_js_1.safeJsonForTool)(params.tools));
287
+ }
288
+ }
289
+ }
290
+ function setChatResponseAttrs(span, sdk, response, logger) {
291
+ const usage = response.usage;
292
+ if (usage) {
293
+ const inputTokens = usage.input_tokens ?? 0;
294
+ const outputTokens = usage.output_tokens ?? 0;
295
+ const cacheRead = usage.cache_read_input_tokens ?? 0;
296
+ const cacheCreation = usage.cache_creation_input_tokens ?? 0;
297
+ // Anthropic's input_tokens excludes cached tokens — add them back for the true total.
298
+ span.setAttribute(semconv_js_1.GEN_AI.USAGE_INPUT_TOKENS, inputTokens + cacheRead + cacheCreation);
299
+ span.setAttribute(semconv_js_1.GEN_AI.USAGE_OUTPUT_TOKENS, outputTokens);
300
+ if (cacheRead) {
301
+ span.setAttribute(semconv_js_1.GEN_AI.USAGE_CACHE_READ_INPUT_TOKENS, cacheRead);
302
+ }
303
+ if (cacheCreation) {
304
+ span.setAttribute(semconv_js_1.GEN_AI.USAGE_CACHE_CREATION_INPUT_TOKENS, cacheCreation);
305
+ }
306
+ }
307
+ if (response.model) {
308
+ span.setAttribute(semconv_js_1.GEN_AI.RESPONSE_MODEL, response.model);
309
+ }
310
+ if (response.stop_reason) {
311
+ span.setAttribute(semconv_js_1.GEN_AI.RESPONSE_FINISH_REASONS, [response.stop_reason]);
312
+ }
313
+ if (response.id) {
314
+ span.setAttribute(semconv_js_1.GEN_AI.RESPONSE_ID, response.id);
315
+ }
316
+ if (response.content) {
317
+ // Always runs — populates pending queue regardless of content-capture mode.
318
+ recordPendingToolCalls(response.content);
319
+ if (sdk.emitEvents && logger) {
320
+ (0, events_js_1.emitAnthropicChoiceEvent)(logger, response.content, response.stop_reason);
321
+ }
322
+ if (sdk.emitSpanContent) {
323
+ span.setAttribute(semconv_js_1.GEN_AI.OUTPUT_MESSAGES, (0, anthropic_content_js_1.toOutputMessages)(response.content, response.stop_reason));
324
+ }
325
+ }
326
+ }
327
+ function recordPendingToolCalls(contentBlocks) {
328
+ const pairs = (0, anthropic_content_js_1.iterToolUses)(contentBlocks);
329
+ if (pairs.length === 0)
330
+ return;
331
+ // Ensure the current store has a slot — initializes a transient dict if we're
332
+ // not inside a struct.agent() scope (matches Python behavior).
333
+ (0, context_js_1.ensurePendingToolCallsSlot)();
334
+ (0, context_js_1.pushPendingToolCalls)(pairs);
335
+ }
336
+ function propagateUserPromptToParent(messages) {
337
+ try {
338
+ const agentSpan = (0, context_js_1.getAgentSpan)();
339
+ if (!agentSpan)
340
+ return;
341
+ // The SDK span type doesn't expose `attributes` — we use a brand check on
342
+ // the SDK's ReadableSpan-like shape.
343
+ const agentAttrs = agentSpan
344
+ .attributes;
345
+ if (agentAttrs && agentAttrs[semconv_js_1.GEN_AI.INPUT_MESSAGES])
346
+ return;
347
+ const parts = (0, anthropic_content_js_1.lastUserMessageParts)(messages);
348
+ if (!parts)
349
+ return;
350
+ agentSpan.setAttribute(semconv_js_1.GEN_AI.INPUT_MESSAGES, (0, truncation_js_1.truncateAndSerialize)([{ role: "user", parts }]));
351
+ }
352
+ catch {
353
+ /* never fail the application for telemetry */
354
+ }
355
+ }
356
+ function recordErrorOnSpan(span, err) {
357
+ const errorType = err instanceof Error ? err.constructor.name : typeof err;
358
+ const message = err instanceof Error ? err.message : String(err);
359
+ span.setAttribute(semconv_js_1.ERROR_TYPE, errorType);
360
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message });
361
+ if (err instanceof Error) {
362
+ span.recordException(err);
363
+ }
364
+ }
365
+ /** @internal */
366
+ function _wrapCreateForTest(original) {
367
+ return wrapCreate(original);
368
+ }
369
+ /** @internal */
370
+ function _wrapStreamForTest(original) {
371
+ return wrapStream(original);
372
+ }
373
+ /** @internal */
374
+ function _setActivePatchCtxForTest(ctx) {
375
+ activePatchCtx.value = ctx;
376
+ }
377
+ //# sourceMappingURL=anthropic.js.map
@@ -0,0 +1,5 @@
1
+ import type { StructSDK } from "../core.js";
2
+ export declare const patchedIntegrations: Set<string>;
3
+ export declare function resetPatchedIntegrations(): void;
4
+ export declare function autoInstrument(sdk: StructSDK): Promise<void>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.patchedIntegrations = void 0;
4
+ exports.resetPatchedIntegrations = resetPatchedIntegrations;
5
+ exports.autoInstrument = autoInstrument;
6
+ exports.patchedIntegrations = new Set();
7
+ function resetPatchedIntegrations() {
8
+ exports.patchedIntegrations.clear();
9
+ }
10
+ const INTEGRATIONS = [
11
+ {
12
+ name: "anthropic",
13
+ probe: () => import("@anthropic-ai/sdk"),
14
+ apply: async (sdk) => {
15
+ const mod = await import("./anthropic.js");
16
+ await mod.patch(sdk);
17
+ },
18
+ },
19
+ {
20
+ name: "langchain",
21
+ probe: () => import("@langchain/core/language_models/chat_models"),
22
+ apply: async (sdk) => {
23
+ const mod = await import("./langchain.js");
24
+ await mod.patch(sdk);
25
+ },
26
+ },
27
+ ];
28
+ async function autoInstrument(sdk) {
29
+ for (const integration of INTEGRATIONS) {
30
+ if (exports.patchedIntegrations.has(integration.name))
31
+ continue;
32
+ try {
33
+ await integration.probe();
34
+ }
35
+ catch {
36
+ continue;
37
+ }
38
+ try {
39
+ await integration.apply(sdk);
40
+ exports.patchedIntegrations.add(integration.name);
41
+ }
42
+ catch (err) {
43
+ sdk.getInternalLogger().debug(`Failed to instrument ${integration.name}`, err);
44
+ }
45
+ }
46
+ }
47
+ //# sourceMappingURL=index.js.map