@introspection-sdk/introspection-node 0.4.6 → 0.4.8

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 (56) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/README.md +262 -0
  3. package/dist/otel/init.js +1 -1
  4. package/dist/otel/init.js.map +1 -1
  5. package/dist/otel/integrations/anthropic.d.ts +1 -1
  6. package/dist/otel/integrations/anthropic.d.ts.map +1 -1
  7. package/dist/otel/integrations/anthropic.js +8 -4
  8. package/dist/otel/integrations/anthropic.js.map +1 -1
  9. package/dist/otel/integrations/base.d.ts +30 -1
  10. package/dist/otel/integrations/base.d.ts.map +1 -1
  11. package/dist/otel/integrations/base.js +50 -0
  12. package/dist/otel/integrations/base.js.map +1 -1
  13. package/dist/otel/integrations/claude-agent.d.ts +1 -2
  14. package/dist/otel/integrations/claude-agent.d.ts.map +1 -1
  15. package/dist/otel/integrations/claude-agent.js +4 -3
  16. package/dist/otel/integrations/claude-agent.js.map +1 -1
  17. package/dist/otel/integrations/gemini.d.ts +1 -1
  18. package/dist/otel/integrations/gemini.d.ts.map +1 -1
  19. package/dist/otel/integrations/gemini.js +4 -2
  20. package/dist/otel/integrations/gemini.js.map +1 -1
  21. package/dist/otel/integrations/index.d.ts +6 -12
  22. package/dist/otel/integrations/index.d.ts.map +1 -1
  23. package/dist/otel/integrations/index.js +20 -24
  24. package/dist/otel/integrations/index.js.map +1 -1
  25. package/dist/otel/integrations/langchain.d.ts +1 -2
  26. package/dist/otel/integrations/langchain.d.ts.map +1 -1
  27. package/dist/otel/integrations/langchain.js +6 -4
  28. package/dist/otel/integrations/langchain.js.map +1 -1
  29. package/dist/otel/integrations/mastra.d.ts +1 -1
  30. package/dist/otel/integrations/mastra.d.ts.map +1 -1
  31. package/dist/otel/integrations/mastra.js +6 -2
  32. package/dist/otel/integrations/mastra.js.map +1 -1
  33. package/dist/otel/integrations/openai-agents.d.ts +1 -1
  34. package/dist/otel/integrations/openai-agents.d.ts.map +1 -1
  35. package/dist/otel/integrations/openai-agents.js +7 -2
  36. package/dist/otel/integrations/openai-agents.js.map +1 -1
  37. package/dist/otel/integrations/pi.d.ts +1 -2
  38. package/dist/otel/integrations/pi.d.ts.map +1 -1
  39. package/dist/otel/integrations/pi.js +4 -3
  40. package/dist/otel/integrations/pi.js.map +1 -1
  41. package/dist/otel/integrations/vercel.d.ts +1 -2
  42. package/dist/otel/integrations/vercel.d.ts.map +1 -1
  43. package/dist/otel/integrations/vercel.js +2 -2
  44. package/dist/otel/integrations/vercel.js.map +1 -1
  45. package/package.json +12 -12
  46. package/src/otel/init.ts +1 -1
  47. package/src/otel/integrations/anthropic.ts +16 -6
  48. package/src/otel/integrations/base.ts +64 -1
  49. package/src/otel/integrations/claude-agent.ts +9 -5
  50. package/src/otel/integrations/gemini.ts +9 -4
  51. package/src/otel/integrations/index.ts +25 -24
  52. package/src/otel/integrations/langchain.ts +20 -5
  53. package/src/otel/integrations/mastra.ts +21 -3
  54. package/src/otel/integrations/openai-agents.ts +20 -4
  55. package/src/otel/integrations/pi.ts +9 -5
  56. package/src/otel/integrations/vercel.ts +6 -4
@@ -9,19 +9,19 @@
9
9
  import { logger } from "../../utils.js";
10
10
  import {
11
11
  DidNotEnable,
12
+ isModuleNotFound,
12
13
  type Integration,
13
14
  type IntegrationHandles,
14
15
  type IntegrationSetupContext,
15
16
  } from "./base.js";
16
17
 
17
- export { DidNotEnable };
18
+ export { DidNotEnable, isModuleNotFound };
18
19
  export type { Integration, IntegrationHandles, IntegrationSetupContext };
19
20
 
20
21
  /**
21
- * Built-in integrations, loaded lazily. Each thunk dynamically imports an
22
- * integration module; that module imports its framework at the top level, so
23
- * if the framework is not installed the import rejects and the integration is
24
- * skipped. Order matters only for `deactivates` resolution (handled below).
22
+ * Built-in integrations, loaded lazily. Each integration probes its optional
23
+ * framework package at runtime, so importing the OTel barrel never requires
24
+ * every optional framework SDK to be installed.
25
25
  */
26
26
  const BUILTIN_INTEGRATIONS: ReadonlyArray<
27
27
  () => Promise<{ default: Integration }>
@@ -41,26 +41,27 @@ const installed = new Set<string>();
41
41
  // prototype patch), run by teardownIntegrations() on shutdown.
42
42
  const teardowns: Array<() => void> = [];
43
43
 
44
- /**
45
- * Distinguish "framework package not installed" (expected — skip quietly) from
46
- * a real error inside an installed integration (a bug we shouldn't hide).
47
- */
48
- export function isModuleNotFound(e: unknown): boolean {
49
- const code = (e as { code?: string } | null)?.code;
50
- if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") {
51
- return true;
44
+ async function availableIntegrations(
45
+ integrations: Integration[],
46
+ ): Promise<Integration[]> {
47
+ const available: Integration[] = [];
48
+
49
+ for (const integration of integrations) {
50
+ if (integration.isAvailable && !(await integration.isAvailable())) {
51
+ continue;
52
+ }
53
+ available.push(integration);
52
54
  }
53
- const msg = e instanceof Error ? e.message : String(e);
54
- return /Cannot find (module|package)|Failed to resolve/i.test(msg);
55
+
56
+ return available;
55
57
  }
56
58
 
57
59
  /**
58
60
  * Return the built-in integrations whose framework is importable.
59
61
  *
60
- * Each module's top-level framework import determines availability: a missing
61
- * package makes the dynamic import reject, which we log at debug and skip. Any
62
- * OTHER failure (a real error inside an installed integration) is surfaced at
63
- * warn level rather than silently swallowed — it's a bug, not an absent peer.
62
+ * Missing optional peer packages are expected and skipped quietly. Any OTHER
63
+ * failure is surfaced at warn level rather than silently swallowed — it's a
64
+ * bug, not an absent peer.
64
65
  */
65
66
  export async function discoverIntegrations(): Promise<Integration[]> {
66
67
  const found: Integration[] = [];
@@ -80,7 +81,7 @@ export async function discoverIntegrations(): Promise<Integration[]> {
80
81
  }
81
82
  }
82
83
  }
83
- return found;
84
+ return availableIntegrations(found);
84
85
  }
85
86
 
86
87
  /**
@@ -88,13 +89,13 @@ export async function discoverIntegrations(): Promise<Integration[]> {
88
89
  *
89
90
  * @returns the set of identifiers installed so far this process.
90
91
  */
91
- export function setupIntegrations(
92
+ export async function setupIntegrations(
92
93
  integrations: Integration[],
93
94
  ctx: IntegrationSetupContext,
94
- ): Set<string> {
95
+ ): Promise<Set<string>> {
95
96
  // First-wins de-dupe by identifier.
96
97
  const byId = new Map<string, Integration>();
97
- for (const integration of integrations) {
98
+ for (const integration of await availableIntegrations(integrations)) {
98
99
  if (!byId.has(integration.identifier)) {
99
100
  byId.set(integration.identifier, integration);
100
101
  }
@@ -114,7 +115,7 @@ export function setupIntegrations(
114
115
  }
115
116
  if (installed.has(identifier)) continue;
116
117
  try {
117
- const teardown = integration.setupOnce(ctx);
118
+ const teardown = await integration.setupOnce(ctx);
118
119
  if (typeof teardown === "function") teardowns.push(teardown);
119
120
  installed.add(identifier);
120
121
  } catch (e) {
@@ -10,14 +10,25 @@
10
10
  * patch would double-trace it.
11
11
  */
12
12
 
13
- // Presence gate.
14
- import "@langchain/core/callbacks/base";
13
+ import {
14
+ importOptionalPeer,
15
+ isOptionalPeerInstalled,
16
+ OPTIONAL_PEERS,
17
+ type Integration,
18
+ } from "./base.js";
19
+ import type { IntrospectionCallbackHandler } from "../langchain-handler.js";
15
20
 
16
- import { IntrospectionCallbackHandler } from "../langchain-handler.js";
17
- import type { Integration } from "./base.js";
21
+ const importLocal = (specifier: string) =>
22
+ import(specifier) as Promise<{
23
+ IntrospectionCallbackHandler: new (
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ options: any,
26
+ ) => IntrospectionCallbackHandler;
27
+ }>;
18
28
 
19
29
  const integration: Integration = {
20
30
  identifier: "langchain",
31
+ isAvailable: () => isOptionalPeerInstalled(OPTIONAL_PEERS.langchainCallbacks),
21
32
  // Intentional, and matches the Python SDK: when @langchain/core is present,
22
33
  // auto-discovery skips the always-on raw-Anthropic prototype patch so a
23
34
  // LangChain → ChatAnthropic call isn't traced twice (once by LangChain's
@@ -29,7 +40,11 @@ const integration: Integration = {
29
40
  // `new AnthropicInstrumentor().instrumentClass(...)`, or call
30
41
  // `init({ autoDiscover: false, integrations: [anthropicIntegration] })`.
31
42
  deactivates: ["anthropic"],
32
- setupOnce({ token, serviceName, baseUrl, advanced, handles }) {
43
+ async setupOnce({ token, serviceName, baseUrl, advanced, handles }) {
44
+ await importOptionalPeer(OPTIONAL_PEERS.langchainCallbacks);
45
+ const { IntrospectionCallbackHandler } = await importLocal(
46
+ "../langchain-handler.js",
47
+ );
33
48
  handles.langchainHandler = new IntrospectionCallbackHandler({
34
49
  token,
35
50
  serviceName,
@@ -9,12 +9,30 @@
9
9
  * is the presence gate: a missing Mastra makes discovery skip this shim.
10
10
  */
11
11
 
12
- import { IntrospectionMastraExporter } from "../mastra-exporter.js";
13
- import type { Integration } from "./base.js";
12
+ import {
13
+ importOptionalPeer,
14
+ isOptionalPeerInstalled,
15
+ OPTIONAL_PEERS,
16
+ type Integration,
17
+ } from "./base.js";
18
+
19
+ const importLocal = (specifier: string) =>
20
+ import(specifier) as Promise<{
21
+ IntrospectionMastraExporter: new (
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ options: any,
24
+ ) => unknown;
25
+ }>;
14
26
 
15
27
  const integration: Integration = {
16
28
  identifier: "mastra",
17
- setupOnce({ token, baseUrl, advanced, handles }) {
29
+ isAvailable: () =>
30
+ isOptionalPeerInstalled(OPTIONAL_PEERS.mastraObservability),
31
+ async setupOnce({ token, baseUrl, advanced, handles }) {
32
+ await importOptionalPeer(OPTIONAL_PEERS.mastraObservability);
33
+ const { IntrospectionMastraExporter } = await importLocal(
34
+ "../mastra-exporter.js",
35
+ );
18
36
  handles.mastraExporter = new IntrospectionMastraExporter({
19
37
  token,
20
38
  baseUrl,
@@ -6,14 +6,30 @@
6
6
  * other integrations (e.g. a vendor exporter) already registered.
7
7
  */
8
8
 
9
- import { addTraceProcessor } from "@openai/agents";
10
-
11
9
  import { IntrospectionTracingProcessor } from "../tracing-processor.js";
12
- import type { Integration } from "./base.js";
10
+ import {
11
+ importOptionalPeer,
12
+ isOptionalPeerInstalled,
13
+ OPTIONAL_PEERS,
14
+ type Integration,
15
+ } from "./base.js";
16
+
17
+ interface OpenAIAgentsModule {
18
+ addTraceProcessor?: (processor: IntrospectionTracingProcessor) => void;
19
+ }
13
20
 
14
21
  const integration: Integration = {
15
22
  identifier: "openai_agents",
16
- setupOnce({ token, serviceName, baseUrl, advanced }) {
23
+ isAvailable: () => isOptionalPeerInstalled(OPTIONAL_PEERS.openaiAgents),
24
+ async setupOnce({ token, serviceName, baseUrl, advanced }) {
25
+ const { addTraceProcessor } = await importOptionalPeer<OpenAIAgentsModule>(
26
+ OPTIONAL_PEERS.openaiAgents,
27
+ );
28
+ if (!addTraceProcessor) {
29
+ throw new Error(
30
+ "Invalid @openai/agents module: addTraceProcessor missing",
31
+ );
32
+ }
17
33
  addTraceProcessor(
18
34
  new IntrospectionTracingProcessor({
19
35
  token,
@@ -6,15 +6,19 @@
6
6
  * publishes it via `introspection.instrumentPi(agent, meta)`.
7
7
  */
8
8
 
9
- // Presence gate.
10
- import "@earendil-works/pi-agent-core";
11
-
12
9
  import { IntrospectionPiInstrumentor } from "../pi.js";
13
- import type { Integration } from "./base.js";
10
+ import {
11
+ importOptionalPeer,
12
+ isOptionalPeerInstalled,
13
+ OPTIONAL_PEERS,
14
+ type Integration,
15
+ } from "./base.js";
14
16
 
15
17
  const integration: Integration = {
16
18
  identifier: "pi",
17
- setupOnce({ handles }) {
19
+ isAvailable: () => isOptionalPeerInstalled(OPTIONAL_PEERS.piAgentCore),
20
+ async setupOnce({ handles }) {
21
+ await importOptionalPeer(OPTIONAL_PEERS.piAgentCore);
18
22
  // The instrumentor emits onto the global tracer, which `init()` has just
19
23
  // registered with the shared IntrospectionSpanProcessor.
20
24
  handles.piInstrumentor = new IntrospectionPiInstrumentor();
@@ -9,13 +9,15 @@
9
9
  * (via its top-level import) is skipped when `ai` is not installed.
10
10
  */
11
11
 
12
- // Presence gate: a missing `ai` package makes this module's import reject.
13
- import "ai";
14
-
15
- import type { Integration } from "./base.js";
12
+ import {
13
+ isOptionalPeerInstalled,
14
+ OPTIONAL_PEERS,
15
+ type Integration,
16
+ } from "./base.js";
16
17
 
17
18
  const integration: Integration = {
18
19
  identifier: "vercel",
20
+ isAvailable: () => isOptionalPeerInstalled(OPTIONAL_PEERS.vercelAi),
19
21
  setupOnce() {
20
22
  // No-op: the shared IntrospectionSpanProcessor converts AI SDK spans.
21
23
  },