@introspection-sdk/introspection-node 0.19.1 → 0.21.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.
- package/README.md +28 -8
- package/dist/auth.d.ts +1 -1
- package/dist/client.d.ts +4 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +16 -7
- package/dist/client.js.map +1 -1
- package/dist/dev-target.js +1 -1
- package/dist/index.d.ts +4 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/dist/otel/index.d.ts +1 -14
- package/dist/otel/index.d.ts.map +1 -1
- package/dist/otel/index.js +10 -18
- package/dist/otel/index.js.map +1 -1
- package/dist/otel/init.d.ts +19 -21
- package/dist/otel/init.d.ts.map +1 -1
- package/dist/otel/init.js +62 -48
- package/dist/otel/init.js.map +1 -1
- package/dist/otel/integrations/base.d.ts +17 -37
- package/dist/otel/integrations/base.d.ts.map +1 -1
- package/dist/otel/integrations/base.js +6 -13
- package/dist/otel/integrations/base.js.map +1 -1
- package/dist/otel/integrations/index.d.ts +3 -4
- package/dist/otel/integrations/index.d.ts.map +1 -1
- package/dist/otel/integrations/index.js +6 -25
- package/dist/otel/integrations/index.js.map +1 -1
- package/dist/otel/integrations/pi.d.ts.map +1 -1
- package/dist/otel/integrations/pi.js +9 -1
- package/dist/otel/integrations/pi.js.map +1 -1
- package/dist/otel/logs.d.ts +41 -8
- package/dist/otel/logs.d.ts.map +1 -1
- package/dist/otel/logs.js +95 -66
- package/dist/otel/logs.js.map +1 -1
- package/dist/otel/pi.d.ts +18 -5
- package/dist/otel/pi.d.ts.map +1 -1
- package/dist/otel/pi.js +38 -9
- package/dist/otel/pi.js.map +1 -1
- package/dist/otel/register.d.ts +2 -2
- package/dist/otel/register.d.ts.map +1 -1
- package/dist/otel/register.js +7 -9
- package/dist/otel/register.js.map +1 -1
- package/dist/otel/setup.d.ts +2 -2
- package/dist/otel/setup.d.ts.map +1 -1
- package/dist/otel/setup.js +16 -6
- package/dist/otel/setup.js.map +1 -1
- package/dist/otel/span-processor.d.ts +18 -5
- package/dist/otel/span-processor.d.ts.map +1 -1
- package/dist/otel/span-processor.js +50 -65
- package/dist/otel/span-processor.js.map +1 -1
- package/dist/resources/experiments.d.ts +10 -5
- package/dist/resources/experiments.d.ts.map +1 -1
- package/dist/resources/experiments.js +9 -25
- package/dist/resources/experiments.js.map +1 -1
- package/dist/resources/recipes.d.ts +6 -6
- package/dist/resources/recipes.d.ts.map +1 -1
- package/dist/resources/recipes.js +5 -23
- package/dist/resources/recipes.js.map +1 -1
- package/dist/resources/runtimes.d.ts +2 -2
- package/dist/resources/runtimes.d.ts.map +1 -1
- package/dist/resources/runtimes.js +2 -2
- package/dist/resources/runtimes.js.map +1 -1
- package/dist/runner.d.ts +8 -4
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +26 -18
- package/dist/runner.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +45 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +61 -2
- package/dist/utils.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +18 -60
- package/.turbo/turbo-build.log +0 -4
- package/dist/converters/claude.d.ts +0 -150
- package/dist/converters/claude.d.ts.map +0 -1
- package/dist/converters/claude.js +0 -211
- package/dist/converters/claude.js.map +0 -1
- package/dist/converters/gemini.d.ts +0 -90
- package/dist/converters/gemini.d.ts.map +0 -1
- package/dist/converters/gemini.js +0 -171
- package/dist/converters/gemini.js.map +0 -1
- package/dist/converters/index.d.ts +0 -9
- package/dist/converters/index.d.ts.map +0 -1
- package/dist/converters/index.js +0 -9
- package/dist/converters/index.js.map +0 -1
- package/dist/converters/openai.d.ts +0 -51
- package/dist/converters/openai.d.ts.map +0 -1
- package/dist/converters/openai.js +0 -269
- package/dist/converters/openai.js.map +0 -1
- package/dist/converters/openinference.d.ts +0 -107
- package/dist/converters/openinference.d.ts.map +0 -1
- package/dist/converters/openinference.js +0 -553
- package/dist/converters/openinference.js.map +0 -1
- package/dist/converters/vercel.d.ts +0 -21
- package/dist/converters/vercel.d.ts.map +0 -1
- package/dist/converters/vercel.js +0 -236
- package/dist/converters/vercel.js.map +0 -1
- package/dist/otel/anthropic.d.ts +0 -63
- package/dist/otel/anthropic.d.ts.map +0 -1
- package/dist/otel/anthropic.js +0 -475
- package/dist/otel/anthropic.js.map +0 -1
- package/dist/otel/claude-hooks.d.ts +0 -346
- package/dist/otel/claude-hooks.d.ts.map +0 -1
- package/dist/otel/claude-hooks.js +0 -647
- package/dist/otel/claude-hooks.js.map +0 -1
- package/dist/otel/claude-wrapper.d.ts +0 -93
- package/dist/otel/claude-wrapper.d.ts.map +0 -1
- package/dist/otel/claude-wrapper.js +0 -155
- package/dist/otel/claude-wrapper.js.map +0 -1
- package/dist/otel/gemini.d.ts +0 -56
- package/dist/otel/gemini.d.ts.map +0 -1
- package/dist/otel/gemini.js +0 -350
- package/dist/otel/gemini.js.map +0 -1
- package/dist/otel/integrations/anthropic.d.ts +0 -10
- package/dist/otel/integrations/anthropic.d.ts.map +0 -1
- package/dist/otel/integrations/anthropic.js +0 -26
- package/dist/otel/integrations/anthropic.js.map +0 -1
- package/dist/otel/integrations/claude-agent.d.ts +0 -12
- package/dist/otel/integrations/claude-agent.d.ts.map +0 -1
- package/dist/otel/integrations/claude-agent.js +0 -28
- package/dist/otel/integrations/claude-agent.js.map +0 -1
- package/dist/otel/integrations/gemini.d.ts +0 -11
- package/dist/otel/integrations/gemini.d.ts.map +0 -1
- package/dist/otel/integrations/gemini.js +0 -23
- package/dist/otel/integrations/gemini.js.map +0 -1
- package/dist/otel/integrations/langchain.d.ts +0 -15
- package/dist/otel/integrations/langchain.d.ts.map +0 -1
- package/dist/otel/integrations/langchain.js +0 -43
- package/dist/otel/integrations/langchain.js.map +0 -1
- package/dist/otel/integrations/mastra.d.ts +0 -14
- package/dist/otel/integrations/mastra.d.ts.map +0 -1
- package/dist/otel/integrations/mastra.js +0 -31
- package/dist/otel/integrations/mastra.js.map +0 -1
- package/dist/otel/integrations/openai-agents.d.ts +0 -11
- package/dist/otel/integrations/openai-agents.d.ts.map +0 -1
- package/dist/otel/integrations/openai-agents.js +0 -30
- package/dist/otel/integrations/openai-agents.js.map +0 -1
- package/dist/otel/integrations/vercel.d.ts +0 -14
- package/dist/otel/integrations/vercel.d.ts.map +0 -1
- package/dist/otel/integrations/vercel.js +0 -20
- package/dist/otel/integrations/vercel.js.map +0 -1
- package/dist/otel/langchain-handler.d.ts +0 -112
- package/dist/otel/langchain-handler.d.ts.map +0 -1
- package/dist/otel/langchain-handler.js +0 -561
- package/dist/otel/langchain-handler.js.map +0 -1
- package/dist/otel/mastra-exporter.d.ts +0 -87
- package/dist/otel/mastra-exporter.d.ts.map +0 -1
- package/dist/otel/mastra-exporter.js +0 -530
- package/dist/otel/mastra-exporter.js.map +0 -1
- package/dist/otel/openai.d.ts +0 -20
- package/dist/otel/openai.d.ts.map +0 -1
- package/dist/otel/openai.js +0 -55
- package/dist/otel/openai.js.map +0 -1
- package/dist/otel/tracing-processor.d.ts +0 -139
- package/dist/otel/tracing-processor.d.ts.map +0 -1
- package/dist/otel/tracing-processor.js +0 -434
- package/dist/otel/tracing-processor.js.map +0 -1
- package/eslint.config.mjs +0 -4
- package/src/auth.ts +0 -236
- package/src/client.ts +0 -163
- package/src/converters/claude.ts +0 -343
- package/src/converters/gemini.ts +0 -243
- package/src/converters/index.ts +0 -50
- package/src/converters/openai.ts +0 -300
- package/src/converters/openinference.ts +0 -665
- package/src/converters/vercel.ts +0 -302
- package/src/dev-target.ts +0 -79
- package/src/http.ts +0 -43
- package/src/index.ts +0 -266
- package/src/otel/anthropic.ts +0 -619
- package/src/otel/claude-hooks.ts +0 -1116
- package/src/otel/claude-wrapper.ts +0 -239
- package/src/otel/gemini.ts +0 -483
- package/src/otel/index.ts +0 -128
- package/src/otel/init.ts +0 -411
- package/src/otel/integrations/anthropic.ts +0 -36
- package/src/otel/integrations/base.ts +0 -158
- package/src/otel/integrations/claude-agent.ts +0 -36
- package/src/otel/integrations/gemini.ts +0 -30
- package/src/otel/integrations/index.ts +0 -152
- package/src/otel/integrations/langchain.ts +0 -60
- package/src/otel/integrations/mastra.ts +0 -48
- package/src/otel/integrations/openai-agents.ts +0 -47
- package/src/otel/integrations/pi.ts +0 -28
- package/src/otel/integrations/vercel.ts +0 -26
- package/src/otel/langchain-handler.ts +0 -804
- package/src/otel/logs.ts +0 -395
- package/src/otel/mastra-exporter.ts +0 -714
- package/src/otel/openai.ts +0 -93
- package/src/otel/pi.ts +0 -94
- package/src/otel/register.ts +0 -50
- package/src/otel/setup.ts +0 -215
- package/src/otel/span-processor.ts +0 -512
- package/src/otel/tracing-processor.ts +0 -637
- package/src/pagination.ts +0 -10
- package/src/proxy-from-env.d.ts +0 -7
- package/src/resources/experiments.ts +0 -176
- package/src/resources/recipes.ts +0 -74
- package/src/resources/runtimes.ts +0 -31
- package/src/runner.ts +0 -213
- package/src/types.ts +0 -13
- package/src/utils.ts +0 -95
- package/src/version.ts +0 -2
- package/tsconfig.json +0 -8
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Claude Agent SDK Wrapper
|
|
3
|
-
*
|
|
4
|
-
* Provides a zero-friction `withIntrospection()` wrapper for the Claude Agent SDK.
|
|
5
|
-
* Automatically instruments all query() calls with OpenTelemetry tracing via
|
|
6
|
-
* IntrospectionClaudeHooks — no manual hook wiring or recordUsage() calls needed.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import * as sdk from "@anthropic-ai/claude-agent-sdk";
|
|
11
|
-
* import { withIntrospection } from "@introspection-sdk/introspection-node";
|
|
12
|
-
*
|
|
13
|
-
* const tracedSdk = withIntrospection(sdk, { serviceName: "my-agent" });
|
|
14
|
-
*
|
|
15
|
-
* for await (const message of tracedSdk.query({ prompt: "Hello" })) {
|
|
16
|
-
* // messages yielded normally — tracing happens automatically
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* await tracedSdk.shutdown();
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
import { IntrospectionClaudeHooks, type IntrospectionClaudeHooksOptions } from "./claude-hooks.js";
|
|
23
|
-
/**
|
|
24
|
-
* Configuration for {@link withIntrospection}.
|
|
25
|
-
*
|
|
26
|
-
* Alias of {@link IntrospectionClaudeHooksOptions}.
|
|
27
|
-
*/
|
|
28
|
-
export type WithIntrospectionOptions = IntrospectionClaudeHooksOptions;
|
|
29
|
-
/**
|
|
30
|
-
* Minimal interface for the Claude Agent SDK module.
|
|
31
|
-
* Accepts `import * as sdk from "@anthropic-ai/claude-agent-sdk"`.
|
|
32
|
-
*
|
|
33
|
-
* Uses `any` for the query signature to accept any SDK version's types
|
|
34
|
-
* without requiring the SDK as a compile-time dependency.
|
|
35
|
-
*/
|
|
36
|
-
export interface ClaudeAgentSDKModule {
|
|
37
|
-
query: (...args: any[]) => any;
|
|
38
|
-
[key: string]: unknown;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* The instrumented SDK returned by {@link withIntrospection}.
|
|
42
|
-
*
|
|
43
|
-
* Exposes the same surface as the original SDK module plus tracing helpers.
|
|
44
|
-
*/
|
|
45
|
-
export interface InstrumentedClaudeAgentSDK {
|
|
46
|
-
/** Instrumented `query()` — auto-captures prompt, hooks, and usage. */
|
|
47
|
-
query: (...args: any[]) => any;
|
|
48
|
-
/**
|
|
49
|
-
* Flush pending spans without shutting down.
|
|
50
|
-
*
|
|
51
|
-
* @returns A promise that resolves once the flush completes.
|
|
52
|
-
*/
|
|
53
|
-
forceFlush(): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
* Shut down tracing and flush all pending spans.
|
|
56
|
-
*
|
|
57
|
-
* @returns A promise that resolves once shutdown is complete.
|
|
58
|
-
*/
|
|
59
|
-
shutdown(): Promise<void>;
|
|
60
|
-
/** The underlying {@link IntrospectionClaudeHooks} instance for advanced usage. */
|
|
61
|
-
readonly hooks: IntrospectionClaudeHooks;
|
|
62
|
-
/** All other SDK exports pass through unchanged. */
|
|
63
|
-
[key: string]: unknown;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Wrap the Claude Agent SDK module to automatically instrument every
|
|
67
|
-
* `query()` call with OpenTelemetry tracing.
|
|
68
|
-
*
|
|
69
|
-
* The returned SDK:
|
|
70
|
-
* - Auto-captures `prompt` and `systemPrompt` from query params
|
|
71
|
-
* - Injects instrumentation hooks (merged with any user-provided hooks)
|
|
72
|
-
* - Calls {@link IntrospectionClaudeHooks.recordUsage | recordUsage()} on
|
|
73
|
-
* every streamed message automatically
|
|
74
|
-
* - Flushes spans when each query stream completes
|
|
75
|
-
*
|
|
76
|
-
* @param sdk - The Claude Agent SDK module
|
|
77
|
-
* (`import * as sdk from "@anthropic-ai/claude-agent-sdk"`).
|
|
78
|
-
* @param options - Introspection configuration (token, serviceName, baseUrl, etc.).
|
|
79
|
-
* @returns An {@link InstrumentedClaudeAgentSDK} with the same API plus
|
|
80
|
-
* `forceFlush()`, `shutdown()`, and `hooks`.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```ts
|
|
84
|
-
* import * as sdk from "@anthropic-ai/claude-agent-sdk";
|
|
85
|
-
* import { withIntrospection } from "@introspection-sdk/introspection-node";
|
|
86
|
-
*
|
|
87
|
-
* const traced = withIntrospection(sdk, { serviceName: "my-agent" });
|
|
88
|
-
* for await (const msg of traced.query({ prompt: "Hello" })) { /* … *\/ }
|
|
89
|
-
* await traced.shutdown();
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
export declare function withIntrospection(sdk: ClaudeAgentSDKModule, options?: WithIntrospectionOptions): InstrumentedClaudeAgentSDK;
|
|
93
|
-
//# sourceMappingURL=claude-wrapper.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"claude-wrapper.d.ts","sourceRoot":"","sources":["../../src/otel/claude-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EACL,wBAAwB,EACxB,KAAK,+BAA+B,EAGrC,MAAM,mBAAmB,CAAC;AAK3B;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,GAAG,+BAA+B,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,WAAW,oBAAoB;IAEnC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IACzC,uEAAuE;IAEvE,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAC/B;;;;OAIG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B;;;;OAIG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mFAAmF;IACnF,QAAQ,CAAC,KAAK,EAAE,wBAAwB,CAAC;IACzC,oDAAoD;IACpD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,oBAAoB,EACzB,OAAO,CAAC,EAAE,wBAAwB,GACjC,0BAA0B,CAwC5B"}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Claude Agent SDK Wrapper
|
|
3
|
-
*
|
|
4
|
-
* Provides a zero-friction `withIntrospection()` wrapper for the Claude Agent SDK.
|
|
5
|
-
* Automatically instruments all query() calls with OpenTelemetry tracing via
|
|
6
|
-
* IntrospectionClaudeHooks — no manual hook wiring or recordUsage() calls needed.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import * as sdk from "@anthropic-ai/claude-agent-sdk";
|
|
11
|
-
* import { withIntrospection } from "@introspection-sdk/introspection-node";
|
|
12
|
-
*
|
|
13
|
-
* const tracedSdk = withIntrospection(sdk, { serviceName: "my-agent" });
|
|
14
|
-
*
|
|
15
|
-
* for await (const message of tracedSdk.query({ prompt: "Hello" })) {
|
|
16
|
-
* // messages yielded normally — tracing happens automatically
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* await tracedSdk.shutdown();
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
import { IntrospectionClaudeHooks, } from "./claude-hooks.js";
|
|
23
|
-
import { logger } from "../utils.js";
|
|
24
|
-
// --- Implementation ---
|
|
25
|
-
/**
|
|
26
|
-
* Wrap the Claude Agent SDK module to automatically instrument every
|
|
27
|
-
* `query()` call with OpenTelemetry tracing.
|
|
28
|
-
*
|
|
29
|
-
* The returned SDK:
|
|
30
|
-
* - Auto-captures `prompt` and `systemPrompt` from query params
|
|
31
|
-
* - Injects instrumentation hooks (merged with any user-provided hooks)
|
|
32
|
-
* - Calls {@link IntrospectionClaudeHooks.recordUsage | recordUsage()} on
|
|
33
|
-
* every streamed message automatically
|
|
34
|
-
* - Flushes spans when each query stream completes
|
|
35
|
-
*
|
|
36
|
-
* @param sdk - The Claude Agent SDK module
|
|
37
|
-
* (`import * as sdk from "@anthropic-ai/claude-agent-sdk"`).
|
|
38
|
-
* @param options - Introspection configuration (token, serviceName, baseUrl, etc.).
|
|
39
|
-
* @returns An {@link InstrumentedClaudeAgentSDK} with the same API plus
|
|
40
|
-
* `forceFlush()`, `shutdown()`, and `hooks`.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* import * as sdk from "@anthropic-ai/claude-agent-sdk";
|
|
45
|
-
* import { withIntrospection } from "@introspection-sdk/introspection-node";
|
|
46
|
-
*
|
|
47
|
-
* const traced = withIntrospection(sdk, { serviceName: "my-agent" });
|
|
48
|
-
* for await (const msg of traced.query({ prompt: "Hello" })) { /* … *\/ }
|
|
49
|
-
* await traced.shutdown();
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function withIntrospection(sdk, options) {
|
|
53
|
-
const hooks = new IntrospectionClaudeHooks(options);
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
-
function wrappedQuery(params) {
|
|
56
|
-
// Auto-capture prompt
|
|
57
|
-
if (typeof params?.prompt === "string") {
|
|
58
|
-
hooks.setInputPrompt(params.prompt);
|
|
59
|
-
}
|
|
60
|
-
// Auto-capture systemPrompt from options
|
|
61
|
-
const opts = (params?.options || {});
|
|
62
|
-
if (typeof opts.systemPrompt === "string") {
|
|
63
|
-
hooks.setSystemInstructions(opts.systemPrompt);
|
|
64
|
-
}
|
|
65
|
-
// Merge instrumentation hooks with any user-provided hooks
|
|
66
|
-
const introspectionHooks = hooks.getHooks();
|
|
67
|
-
const userHooks = opts.hooks;
|
|
68
|
-
const mergedHooks = mergeHooks(introspectionHooks, userHooks);
|
|
69
|
-
// Call original query with merged hooks
|
|
70
|
-
const modifiedOptions = { ...opts, hooks: mergedHooks };
|
|
71
|
-
const originalQuery = sdk.query({
|
|
72
|
-
...params,
|
|
73
|
-
options: modifiedOptions,
|
|
74
|
-
});
|
|
75
|
-
// Wrap the async generator to intercept messages
|
|
76
|
-
return wrapQueryResult(originalQuery, hooks);
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
...sdk,
|
|
80
|
-
query: wrappedQuery,
|
|
81
|
-
hooks,
|
|
82
|
-
forceFlush: () => hooks.forceFlush(),
|
|
83
|
-
shutdown: () => hooks.shutdown(),
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Merges introspection hooks with user-provided hooks.
|
|
88
|
-
* Introspection hooks fire first, then user hooks.
|
|
89
|
-
* User hooks on events we don't handle pass through untouched.
|
|
90
|
-
*/
|
|
91
|
-
function mergeHooks(introspectionHooks, userHooks) {
|
|
92
|
-
if (!userHooks) {
|
|
93
|
-
return introspectionHooks;
|
|
94
|
-
}
|
|
95
|
-
const merged = {};
|
|
96
|
-
const allEvents = new Set([
|
|
97
|
-
...Object.keys(introspectionHooks),
|
|
98
|
-
...Object.keys(userHooks),
|
|
99
|
-
]);
|
|
100
|
-
for (const event of allEvents) {
|
|
101
|
-
const ours = introspectionHooks[event] || [];
|
|
102
|
-
const theirs = userHooks[event] || [];
|
|
103
|
-
merged[event] = [...ours, ...theirs];
|
|
104
|
-
}
|
|
105
|
-
return merged;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Wraps a Query async generator to intercept messages for usage recording.
|
|
109
|
-
* Uses a Proxy to transparently forward all non-iterator properties
|
|
110
|
-
* (interrupt, close, setModel, etc.) to the original Query object.
|
|
111
|
-
*/
|
|
112
|
-
function wrapQueryResult(originalQuery, hooks) {
|
|
113
|
-
const query = originalQuery;
|
|
114
|
-
async function* instrumentedIterator() {
|
|
115
|
-
try {
|
|
116
|
-
for await (const message of query) {
|
|
117
|
-
hooks.recordUsage(message);
|
|
118
|
-
yield message;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
finally {
|
|
122
|
-
try {
|
|
123
|
-
await hooks.forceFlush();
|
|
124
|
-
}
|
|
125
|
-
catch (flushError) {
|
|
126
|
-
logger.warn("Failed to flush spans after query completed", flushError);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const wrapped = instrumentedIterator();
|
|
131
|
-
// Use Proxy to transparently delegate non-iterator properties to original query.
|
|
132
|
-
// Iterator protocol methods come from our wrapper (bound to the actual generator
|
|
133
|
-
// to avoid "incompatible receiver" errors); everything else (interrupt, close,
|
|
134
|
-
// setModel, setPermissionMode, etc.) delegates to the original Query.
|
|
135
|
-
return new Proxy(wrapped, {
|
|
136
|
-
get(target, prop) {
|
|
137
|
-
if (prop === Symbol.asyncIterator ||
|
|
138
|
-
prop === "next" ||
|
|
139
|
-
prop === "return" ||
|
|
140
|
-
prop === "throw") {
|
|
141
|
-
const value = Reflect.get(target, prop);
|
|
142
|
-
if (typeof value === "function") {
|
|
143
|
-
return value.bind(target);
|
|
144
|
-
}
|
|
145
|
-
return value;
|
|
146
|
-
}
|
|
147
|
-
const original = query[prop];
|
|
148
|
-
if (typeof original === "function") {
|
|
149
|
-
return original.bind(query);
|
|
150
|
-
}
|
|
151
|
-
return original;
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
//# sourceMappingURL=claude-wrapper.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"claude-wrapper.js","sourceRoot":"","sources":["../../src/otel/claude-wrapper.ts"],"names":[],"mappings":"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"}
|
package/dist/otel/gemini.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lightweight Google Gemini (`@google/genai`) instrumentor for Introspection SDK.
|
|
3
|
-
*
|
|
4
|
-
* Captures the full Gemini response including per-part `thoughtSignature`
|
|
5
|
-
* payloads (Gemini 3.x extended thinking) as gen_ai `reasoning` parts with
|
|
6
|
-
* `signature` set — mirroring how the {@link AnthropicInstrumentor} captures
|
|
7
|
-
* Anthropic thinking blocks.
|
|
8
|
-
*
|
|
9
|
-
* Supports both non-streaming (`client.models.generateContent`) and streaming
|
|
10
|
-
* (`client.models.generateContentStream`) calls.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* import { GoogleGenAI } from "@google/genai";
|
|
15
|
-
* import { GeminiInstrumentor } from "@introspection-sdk/introspection-node";
|
|
16
|
-
*
|
|
17
|
-
* const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
|
|
18
|
-
* new GeminiInstrumentor().instrument({ tracerProvider: provider, client });
|
|
19
|
-
* // All client.models.generateContent / generateContentStream calls are now traced
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @see https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
23
|
-
*/
|
|
24
|
-
import type { TracerProvider } from "@opentelemetry/api";
|
|
25
|
-
export declare class GeminiInstrumentor {
|
|
26
|
-
private tracer;
|
|
27
|
-
private restores;
|
|
28
|
-
private conversationId;
|
|
29
|
-
instrument(opts: {
|
|
30
|
-
tracerProvider?: TracerProvider;
|
|
31
|
-
/** A `@google/genai` `GoogleGenAI` client instance. */
|
|
32
|
-
client: any;
|
|
33
|
-
/**
|
|
34
|
-
* Conversation ID shared across all spans produced by this instrumentor.
|
|
35
|
-
* Defaults to a random UUID generated at `instrument()` time.
|
|
36
|
-
*/
|
|
37
|
-
conversationId?: string;
|
|
38
|
-
}): void;
|
|
39
|
-
/**
|
|
40
|
-
* Patch `Models.prototype.generateContentInternal` / `…StreamInternal` so
|
|
41
|
-
* **every** `GoogleGenAI` client is traced — the one-liner path used by
|
|
42
|
-
* `introspection.init()`. The public `generateContent` /
|
|
43
|
-
* `generateContentStream` are per-instance bound class fields that delegate
|
|
44
|
-
* to these prototype methods, so patching the prototype covers all clients.
|
|
45
|
-
* Idempotent: a second call is a no-op.
|
|
46
|
-
*
|
|
47
|
-
* @param opts.genai The `@google/genai` module (must expose `Models`).
|
|
48
|
-
*/
|
|
49
|
-
instrumentClass(opts: {
|
|
50
|
-
genai: any;
|
|
51
|
-
tracerProvider?: TracerProvider;
|
|
52
|
-
conversationId?: string;
|
|
53
|
-
}): void;
|
|
54
|
-
uninstrument(): void;
|
|
55
|
-
}
|
|
56
|
-
//# sourceMappingURL=gemini.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../src/otel/gemini.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAuVvE,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,QAAQ,CAAyB;IACzC,OAAO,CAAC,cAAc,CAAqB;IAE3C,UAAU,CAAC,IAAI,EAAE;QACf,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,uDAAuD;QAEvD,MAAM,EAAE,GAAG,CAAC;QACZ;;;WAGG;QACH,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,GAAG,IAAI;IAsCR;;;;;;;;;OASG;IACH,eAAe,CAAC,IAAI,EAAE;QAEpB,KAAK,EAAE,GAAG,CAAC;QACX,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,GAAG,IAAI;IAyCR,YAAY,IAAI,IAAI;CAKrB"}
|
package/dist/otel/gemini.js
DELETED
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lightweight Google Gemini (`@google/genai`) instrumentor for Introspection SDK.
|
|
3
|
-
*
|
|
4
|
-
* Captures the full Gemini response including per-part `thoughtSignature`
|
|
5
|
-
* payloads (Gemini 3.x extended thinking) as gen_ai `reasoning` parts with
|
|
6
|
-
* `signature` set — mirroring how the {@link AnthropicInstrumentor} captures
|
|
7
|
-
* Anthropic thinking blocks.
|
|
8
|
-
*
|
|
9
|
-
* Supports both non-streaming (`client.models.generateContent`) and streaming
|
|
10
|
-
* (`client.models.generateContentStream`) calls.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* import { GoogleGenAI } from "@google/genai";
|
|
15
|
-
* import { GeminiInstrumentor } from "@introspection-sdk/introspection-node";
|
|
16
|
-
*
|
|
17
|
-
* const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
|
|
18
|
-
* new GeminiInstrumentor().instrument({ tracerProvider: provider, client });
|
|
19
|
-
* // All client.models.generateContent / generateContentStream calls are now traced
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @see https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
23
|
-
*/
|
|
24
|
-
import { trace, SpanKind, SpanStatusCode } from "@opentelemetry/api";
|
|
25
|
-
import { providerCostAttributes } from "@introspection-sdk/types";
|
|
26
|
-
import { convertGeminiContentsToInputMessages, convertGeminiCandidatesToOutputMessages, convertGeminiSystemInstructionToSemconv, convertGeminiToolsToToolDefinitions, GEMINI_PROVIDER_NAME, } from "../converters/gemini.js";
|
|
27
|
-
// ---------------------------------------------------------------------------
|
|
28
|
-
// Span helpers
|
|
29
|
-
// ---------------------------------------------------------------------------
|
|
30
|
-
function serializeMessages(msgs) {
|
|
31
|
-
return JSON.stringify(msgs.map((m) => {
|
|
32
|
-
const obj = { role: m.role, parts: m.parts };
|
|
33
|
-
if ("finish_reason" in m && m.finish_reason) {
|
|
34
|
-
obj.finish_reason = m.finish_reason;
|
|
35
|
-
}
|
|
36
|
-
return obj;
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
function startSpan(tracer, model, conversationId) {
|
|
40
|
-
return tracer.startSpan("chat", {
|
|
41
|
-
kind: SpanKind.CLIENT,
|
|
42
|
-
attributes: {
|
|
43
|
-
"gen_ai.provider.name": GEMINI_PROVIDER_NAME,
|
|
44
|
-
"gen_ai.operation.name": "chat",
|
|
45
|
-
"gen_ai.request.model": model,
|
|
46
|
-
"openinference.span.kind": "LLM",
|
|
47
|
-
...(conversationId
|
|
48
|
-
? { "gen_ai.conversation.id": conversationId }
|
|
49
|
-
: undefined),
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function setRequestAttrs(span, kwargs) {
|
|
54
|
-
const contents = kwargs.contents;
|
|
55
|
-
const inputMsgs = convertGeminiContentsToInputMessages(contents);
|
|
56
|
-
if (inputMsgs.length > 0) {
|
|
57
|
-
span.setAttribute("gen_ai.input.messages", serializeMessages(inputMsgs));
|
|
58
|
-
}
|
|
59
|
-
const config = kwargs.config || {};
|
|
60
|
-
const systemInstruction = config.systemInstruction;
|
|
61
|
-
const systemInstructions = convertGeminiSystemInstructionToSemconv(systemInstruction);
|
|
62
|
-
if (systemInstructions && systemInstructions.length > 0) {
|
|
63
|
-
span.setAttribute("gen_ai.system_instructions", JSON.stringify(systemInstructions));
|
|
64
|
-
}
|
|
65
|
-
const tools = config.tools;
|
|
66
|
-
const toolDefs = convertGeminiToolsToToolDefinitions(tools);
|
|
67
|
-
if (toolDefs && toolDefs.length > 0) {
|
|
68
|
-
span.setAttribute("gen_ai.tool.definitions", JSON.stringify(toolDefs));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
function setUsageAttrs(span, usage) {
|
|
72
|
-
if (!usage)
|
|
73
|
-
return;
|
|
74
|
-
const promptTokens = usage.promptTokenCount;
|
|
75
|
-
const candidateTokens = usage.candidatesTokenCount;
|
|
76
|
-
const cachedTokens = usage.cachedContentTokenCount;
|
|
77
|
-
const thoughtTokens = usage.thoughtsTokenCount;
|
|
78
|
-
if (typeof promptTokens === "number") {
|
|
79
|
-
span.setAttribute("gen_ai.usage.input_tokens", promptTokens);
|
|
80
|
-
}
|
|
81
|
-
if (typeof candidateTokens === "number") {
|
|
82
|
-
// Gemini reports candidatesTokenCount separately from thoughtsTokenCount;
|
|
83
|
-
// sum them so output_tokens reflects everything billed as model output.
|
|
84
|
-
const total = candidateTokens + (thoughtTokens ?? 0);
|
|
85
|
-
span.setAttribute("gen_ai.usage.output_tokens", total);
|
|
86
|
-
}
|
|
87
|
-
else if (typeof thoughtTokens === "number") {
|
|
88
|
-
span.setAttribute("gen_ai.usage.output_tokens", thoughtTokens);
|
|
89
|
-
}
|
|
90
|
-
if (typeof cachedTokens === "number") {
|
|
91
|
-
span.setAttribute("gen_ai.usage.cache_read.input_tokens", cachedTokens);
|
|
92
|
-
}
|
|
93
|
-
// Provider-reported cost (e.g. OpenRouter usage.cost) when present.
|
|
94
|
-
span.setAttributes(providerCostAttributes(usage));
|
|
95
|
-
}
|
|
96
|
-
function setResponseAttrs(span, response) {
|
|
97
|
-
const candidates = response.candidates;
|
|
98
|
-
const outputMsgs = convertGeminiCandidatesToOutputMessages(candidates);
|
|
99
|
-
if (outputMsgs.length > 0) {
|
|
100
|
-
span.setAttribute("gen_ai.output.messages", serializeMessages(outputMsgs));
|
|
101
|
-
const finishReasons = outputMsgs
|
|
102
|
-
.map((m) => m.finish_reason)
|
|
103
|
-
.filter((r) => typeof r === "string");
|
|
104
|
-
if (finishReasons.length > 0) {
|
|
105
|
-
span.setAttribute("gen_ai.response.finish_reasons", finishReasons);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const responseId = response.responseId;
|
|
109
|
-
if (responseId)
|
|
110
|
-
span.setAttribute("gen_ai.response.id", responseId);
|
|
111
|
-
const modelVersion = response.modelVersion;
|
|
112
|
-
if (modelVersion)
|
|
113
|
-
span.setAttribute("gen_ai.response.model", modelVersion);
|
|
114
|
-
setUsageAttrs(span, response.usageMetadata);
|
|
115
|
-
span.setStatus({ code: SpanStatusCode.OK });
|
|
116
|
-
}
|
|
117
|
-
/** True if a streamed part is just incremental text with no metadata to preserve. */
|
|
118
|
-
function isPlainTextPart(part) {
|
|
119
|
-
return (typeof part.text === "string" &&
|
|
120
|
-
part.thought !== true &&
|
|
121
|
-
!part.thoughtSignature &&
|
|
122
|
-
!part.functionCall &&
|
|
123
|
-
!part.functionResponse);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Async-iterable wrapper that aggregates streamed `GenerateContentResponse`
|
|
127
|
-
* chunks so the underlying span captures the complete final message — including
|
|
128
|
-
* the final per-part `thoughtSignature` values that only arrive on the last
|
|
129
|
-
* chunk of a streamed turn.
|
|
130
|
-
*/
|
|
131
|
-
class TracedStream {
|
|
132
|
-
inner;
|
|
133
|
-
span;
|
|
134
|
-
aggregated = new Map();
|
|
135
|
-
usage;
|
|
136
|
-
responseId;
|
|
137
|
-
modelVersion;
|
|
138
|
-
finalized = false;
|
|
139
|
-
constructor(inner, span) {
|
|
140
|
-
this.inner = inner;
|
|
141
|
-
this.span = span;
|
|
142
|
-
}
|
|
143
|
-
async *[Symbol.asyncIterator]() {
|
|
144
|
-
try {
|
|
145
|
-
for await (const chunk of this.inner) {
|
|
146
|
-
this.processChunk(chunk);
|
|
147
|
-
yield chunk;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
catch (err) {
|
|
151
|
-
this.span.setStatus({
|
|
152
|
-
code: SpanStatusCode.ERROR,
|
|
153
|
-
message: String(err),
|
|
154
|
-
});
|
|
155
|
-
this.span.end();
|
|
156
|
-
this.finalized = true;
|
|
157
|
-
throw err;
|
|
158
|
-
}
|
|
159
|
-
finally {
|
|
160
|
-
this.finalize();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
processChunk(chunk) {
|
|
164
|
-
if (chunk.responseId)
|
|
165
|
-
this.responseId = chunk.responseId;
|
|
166
|
-
if (chunk.modelVersion)
|
|
167
|
-
this.modelVersion = chunk.modelVersion;
|
|
168
|
-
if (chunk.usageMetadata)
|
|
169
|
-
this.usage = chunk.usageMetadata;
|
|
170
|
-
for (const cand of chunk.candidates ?? []) {
|
|
171
|
-
const idx = cand.index ?? 0;
|
|
172
|
-
const existing = this.aggregated.get(idx);
|
|
173
|
-
if (!existing) {
|
|
174
|
-
this.aggregated.set(idx, {
|
|
175
|
-
index: idx,
|
|
176
|
-
finishReason: cand.finishReason,
|
|
177
|
-
content: {
|
|
178
|
-
role: cand.content?.role,
|
|
179
|
-
parts: [...(cand.content?.parts ?? [])],
|
|
180
|
-
},
|
|
181
|
-
});
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (cand.finishReason)
|
|
185
|
-
existing.finishReason = cand.finishReason;
|
|
186
|
-
if (cand.content?.role && !existing.content?.role) {
|
|
187
|
-
existing.content.role = cand.content.role;
|
|
188
|
-
}
|
|
189
|
-
this.mergeParts(existing.content.parts, cand.content?.parts ?? []);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Merge streamed parts into the aggregated parts list.
|
|
194
|
-
*
|
|
195
|
-
* Gemini streams text incrementally as separate `text`-only parts; thought
|
|
196
|
-
* signatures and function calls arrive as their own parts. Consecutive
|
|
197
|
-
* plain-text parts are coalesced into a single accumulating text part so the
|
|
198
|
-
* final thought signature on a block survives on its trailing part.
|
|
199
|
-
*/
|
|
200
|
-
mergeParts(target, incoming) {
|
|
201
|
-
for (const part of incoming) {
|
|
202
|
-
const last = target[target.length - 1];
|
|
203
|
-
if (isPlainTextPart(part) && last && isPlainTextPart(last)) {
|
|
204
|
-
last.text = (last.text || "") + (part.text || "");
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
target.push({ ...part });
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
finalize() {
|
|
211
|
-
if (this.finalized)
|
|
212
|
-
return;
|
|
213
|
-
this.finalized = true;
|
|
214
|
-
setResponseAttrs(this.span, {
|
|
215
|
-
candidates: Array.from(this.aggregated.values()),
|
|
216
|
-
usageMetadata: this.usage,
|
|
217
|
-
responseId: this.responseId,
|
|
218
|
-
modelVersion: this.modelVersion,
|
|
219
|
-
});
|
|
220
|
-
this.span.end();
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Auto-instrumentor that wraps a `@google/genai` client instance to add
|
|
225
|
-
* Introspection tracing with full thought-signature capture.
|
|
226
|
-
*
|
|
227
|
-
* Captures all content parts including thought summaries and signed
|
|
228
|
-
* `thoughtSignature` payloads required for multi-turn replay.
|
|
229
|
-
*/
|
|
230
|
-
/**
|
|
231
|
-
* Build the traced replacement for a non-streaming `generateContent`-style
|
|
232
|
-
* method. Shared by {@link GeminiInstrumentor.instrument} (patches a client's
|
|
233
|
-
* public `models.generateContent`) and {@link GeminiInstrumentor.instrumentClass}
|
|
234
|
-
* (patches `Models.prototype.generateContentInternal` so every client is
|
|
235
|
-
* auto-traced). Preserves the call-site `this`.
|
|
236
|
-
*/
|
|
237
|
-
function buildTracedGenerate(origGenerate, tracer, conversationId) {
|
|
238
|
-
return async function tracedGenerate(...args) {
|
|
239
|
-
const kwargs = args[0] || {};
|
|
240
|
-
const span = startSpan(tracer, kwargs.model || "unknown", conversationId);
|
|
241
|
-
setRequestAttrs(span, kwargs);
|
|
242
|
-
try {
|
|
243
|
-
const response = (await origGenerate.apply(this, args));
|
|
244
|
-
setResponseAttrs(span, response);
|
|
245
|
-
return response;
|
|
246
|
-
}
|
|
247
|
-
catch (err) {
|
|
248
|
-
span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
|
|
249
|
-
throw err;
|
|
250
|
-
}
|
|
251
|
-
finally {
|
|
252
|
-
span.end();
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
/** Build the traced replacement for a streaming `generateContentStream`-style method. */
|
|
257
|
-
function buildTracedGenerateStream(origStream, tracer, conversationId) {
|
|
258
|
-
return async function tracedGenerateStream(...args) {
|
|
259
|
-
const kwargs = args[0] || {};
|
|
260
|
-
const span = startSpan(tracer, kwargs.model || "unknown", conversationId);
|
|
261
|
-
setRequestAttrs(span, kwargs);
|
|
262
|
-
try {
|
|
263
|
-
const stream = (await origStream.apply(this, args));
|
|
264
|
-
return new TracedStream(stream, span);
|
|
265
|
-
}
|
|
266
|
-
catch (err) {
|
|
267
|
-
span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
|
|
268
|
-
span.end();
|
|
269
|
-
throw err;
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Marker placed on a patched `Models.prototype` method so repeated
|
|
275
|
-
* `instrumentClass()` calls (e.g. a second `introspection.init()`) are no-ops.
|
|
276
|
-
*/
|
|
277
|
-
const PATCHED = Symbol.for("introspection.gemini.patched");
|
|
278
|
-
export class GeminiInstrumentor {
|
|
279
|
-
tracer = null;
|
|
280
|
-
restores = [];
|
|
281
|
-
conversationId;
|
|
282
|
-
instrument(opts) {
|
|
283
|
-
const { client } = opts;
|
|
284
|
-
if (!client?.models?.generateContent) {
|
|
285
|
-
throw new Error("Invalid client — pass a GoogleGenAI client instance (new GoogleGenAI({...}))");
|
|
286
|
-
}
|
|
287
|
-
this.conversationId = opts.conversationId ?? crypto.randomUUID();
|
|
288
|
-
const provider = opts.tracerProvider ?? trace.getTracerProvider();
|
|
289
|
-
this.tracer = provider.getTracer("introspection-gemini");
|
|
290
|
-
const tracer = this.tracer;
|
|
291
|
-
const conversationId = this.conversationId;
|
|
292
|
-
const api = client.models;
|
|
293
|
-
const origGenerate = api.generateContent.bind(api);
|
|
294
|
-
const origGenerateStream = api.generateContentStream?.bind(api);
|
|
295
|
-
this.restores.push(() => {
|
|
296
|
-
api.generateContent = origGenerate;
|
|
297
|
-
if (origGenerateStream)
|
|
298
|
-
api.generateContentStream = origGenerateStream;
|
|
299
|
-
});
|
|
300
|
-
api.generateContent = buildTracedGenerate(origGenerate, tracer, conversationId);
|
|
301
|
-
if (origGenerateStream) {
|
|
302
|
-
api.generateContentStream = buildTracedGenerateStream(origGenerateStream, tracer, conversationId);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Patch `Models.prototype.generateContentInternal` / `…StreamInternal` so
|
|
307
|
-
* **every** `GoogleGenAI` client is traced — the one-liner path used by
|
|
308
|
-
* `introspection.init()`. The public `generateContent` /
|
|
309
|
-
* `generateContentStream` are per-instance bound class fields that delegate
|
|
310
|
-
* to these prototype methods, so patching the prototype covers all clients.
|
|
311
|
-
* Idempotent: a second call is a no-op.
|
|
312
|
-
*
|
|
313
|
-
* @param opts.genai The `@google/genai` module (must expose `Models`).
|
|
314
|
-
*/
|
|
315
|
-
instrumentClass(opts) {
|
|
316
|
-
const proto = opts.genai?.Models?.prototype;
|
|
317
|
-
if (typeof proto?.generateContentInternal !== "function") {
|
|
318
|
-
throw new Error("Invalid @google/genai module — expected `Models.prototype.generateContentInternal`");
|
|
319
|
-
}
|
|
320
|
-
if (proto.generateContentInternal[PATCHED])
|
|
321
|
-
return;
|
|
322
|
-
this.conversationId = opts.conversationId ?? crypto.randomUUID();
|
|
323
|
-
const provider = opts.tracerProvider ?? trace.getTracerProvider();
|
|
324
|
-
this.tracer = provider.getTracer("introspection-gemini");
|
|
325
|
-
const conversationId = this.conversationId;
|
|
326
|
-
const origGenerate = proto.generateContentInternal;
|
|
327
|
-
const patchedGenerate = buildTracedGenerate(origGenerate, this.tracer, conversationId);
|
|
328
|
-
patchedGenerate[PATCHED] = true;
|
|
329
|
-
proto.generateContentInternal = patchedGenerate;
|
|
330
|
-
this.restores.push(() => {
|
|
331
|
-
proto.generateContentInternal = origGenerate;
|
|
332
|
-
});
|
|
333
|
-
if (typeof proto.generateContentStreamInternal === "function") {
|
|
334
|
-
const origStream = proto.generateContentStreamInternal;
|
|
335
|
-
const patchedStream = buildTracedGenerateStream(origStream, this.tracer, conversationId);
|
|
336
|
-
patchedStream[PATCHED] = true;
|
|
337
|
-
proto.generateContentStreamInternal = patchedStream;
|
|
338
|
-
this.restores.push(() => {
|
|
339
|
-
proto.generateContentStreamInternal = origStream;
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
uninstrument() {
|
|
344
|
-
for (const restore of this.restores)
|
|
345
|
-
restore();
|
|
346
|
-
this.restores = [];
|
|
347
|
-
this.tracer = null;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
//# sourceMappingURL=gemini.js.map
|