@astropods/adapter-claude-agent-sdk 0.2.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.
@@ -0,0 +1,4 @@
1
+ import * as ClaudeAgentSDKModule from "@anthropic-ai/claude-agent-sdk";
2
+ export * from "@anthropic-ai/claude-agent-sdk";
3
+ export declare const query: typeof ClaudeAgentSDKModule.query;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,oBAAoB,MAAM,gCAAgC,CAAC;AAWvE,cAAc,gCAAgC,CAAC;AAK/C,eAAO,MAAM,KAAK,mCAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import * as ClaudeAgentSDKModule from "@anthropic-ai/claude-agent-sdk";
2
+ import { instrumentSDK } from "./instrumentation.js";
3
+ // The Claude Agent SDK is ESM and exposes a frozen namespace, so the
4
+ // OpenInference instrumentation cannot patch it in place. Spreading into a
5
+ // regular object gives us a mutable copy that `manuallyInstrument` can edit.
6
+ const patched = { ...ClaudeAgentSDKModule };
7
+ instrumentSDK(patched);
8
+ // Re-export the entire SDK surface so this package is a drop-in replacement.
9
+ // New SDK exports flow through automatically.
10
+ export * from "@anthropic-ai/claude-agent-sdk";
11
+ // Override the un-patched bindings re-exported above with the instrumented
12
+ // copies. The explicit local export takes precedence over the `export *`
13
+ // re-export, so callers get the patched version.
14
+ export const query = patched.query;
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,oBAAoB,MAAM,gCAAgC,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,qEAAqE;AACrE,2EAA2E;AAC3E,6EAA6E;AAC7E,MAAM,OAAO,GAAgC,EAAE,GAAG,oBAAoB,EAAE,CAAC;AACzE,aAAa,CAAC,OAAO,CAAC,CAAC;AAEvB,6EAA6E;AAC7E,8CAA8C;AAC9C,cAAc,gCAAgC,CAAC;AAE/C,2EAA2E;AAC3E,yEAAyE;AACzE,iDAAiD;AACjD,MAAM,CAAC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Wire OpenTelemetry into a Claude Agent SDK namespace. When
3
+ * `OTEL_EXPORTER_OTLP_ENDPOINT` is set, this:
4
+ * - Builds a `NodeTracerProvider` with the right service.name/version resource,
5
+ * exporting to `<endpoint>/v1/traces` via the OTLP HTTP exporter.
6
+ * - Registers the provider globally so any `trace.getTracer(...)` call uses it.
7
+ * - Runs `ClaudeAgentSDKInstrumentation.manuallyInstrument(sdk)`, replacing
8
+ * `sdk.query` with an OpenInference-wrapped version.
9
+ * - Installs SIGTERM/SIGINT handlers that force-flush buffered spans before
10
+ * exiting (so the tail of an in-flight trace isn't dropped on container shutdown).
11
+ *
12
+ * Returns `true` when instrumentation was applied, `false` when no endpoint
13
+ * was present (typical local-dev case — the agent runs untraced).
14
+ *
15
+ * Tests pass in a replay-SDK namespace here to verify span generation against
16
+ * recorded SDK event shapes without needing real Claude API calls.
17
+ */
18
+ export declare function instrumentSDK(sdk: Record<string, any>): boolean;
19
+ //# sourceMappingURL=instrumentation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentation.d.ts","sourceRoot":"","sources":["../src/instrumentation.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAgC/D"}
@@ -0,0 +1,52 @@
1
+ import { resourceFromAttributes } from "@opentelemetry/resources";
2
+ import { NodeTracerProvider, BatchSpanProcessor, } from "@opentelemetry/sdk-trace-node";
3
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
4
+ import { ClaudeAgentSDKInstrumentation } from "@arizeai/openinference-instrumentation-claude-agent-sdk";
5
+ /**
6
+ * Wire OpenTelemetry into a Claude Agent SDK namespace. When
7
+ * `OTEL_EXPORTER_OTLP_ENDPOINT` is set, this:
8
+ * - Builds a `NodeTracerProvider` with the right service.name/version resource,
9
+ * exporting to `<endpoint>/v1/traces` via the OTLP HTTP exporter.
10
+ * - Registers the provider globally so any `trace.getTracer(...)` call uses it.
11
+ * - Runs `ClaudeAgentSDKInstrumentation.manuallyInstrument(sdk)`, replacing
12
+ * `sdk.query` with an OpenInference-wrapped version.
13
+ * - Installs SIGTERM/SIGINT handlers that force-flush buffered spans before
14
+ * exiting (so the tail of an in-flight trace isn't dropped on container shutdown).
15
+ *
16
+ * Returns `true` when instrumentation was applied, `false` when no endpoint
17
+ * was present (typical local-dev case — the agent runs untraced).
18
+ *
19
+ * Tests pass in a replay-SDK namespace here to verify span generation against
20
+ * recorded SDK event shapes without needing real Claude API calls.
21
+ */
22
+ export function instrumentSDK(sdk) {
23
+ const endpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
24
+ if (!endpoint)
25
+ return false;
26
+ const tracesUrl = `${endpoint.replace(/\/+$/, "")}/v1/traces`;
27
+ const provider = new NodeTracerProvider({
28
+ resource: resourceFromAttributes({
29
+ "service.name": process.env.ASTRO_AGENT_NAME ?? "claude-agent",
30
+ "service.version": process.env.ASTRO_AGENT_BUILD ?? "dev",
31
+ }),
32
+ spanProcessors: [
33
+ new BatchSpanProcessor(new OTLPTraceExporter({ url: tracesUrl })),
34
+ ],
35
+ });
36
+ provider.register();
37
+ const instrumentation = new ClaudeAgentSDKInstrumentation();
38
+ instrumentation.setTracerProvider(provider);
39
+ instrumentation.manuallyInstrument(sdk);
40
+ const flushAndExit = async (signal) => {
41
+ try {
42
+ await provider.forceFlush();
43
+ await provider.shutdown();
44
+ }
45
+ catch { }
46
+ process.exit(signal === "SIGINT" ? 130 : 0);
47
+ };
48
+ process.once("SIGTERM", flushAndExit);
49
+ process.once("SIGINT", flushAndExit);
50
+ return true;
51
+ }
52
+ //# sourceMappingURL=instrumentation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../src/instrumentation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EACL,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,6BAA6B,EAAE,MAAM,yDAAyD,CAAC;AAExG;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,aAAa,CAAC,GAAwB;IACpD,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC;IACzD,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAE5B,MAAM,SAAS,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,YAAY,CAAC;IAE9D,MAAM,QAAQ,GAAG,IAAI,kBAAkB,CAAC;QACtC,QAAQ,EAAE,sBAAsB,CAAC;YAC/B,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,cAAc;YAC9D,iBAAiB,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,KAAK;SAC1D,CAAC;QACF,cAAc,EAAE;YACd,IAAI,kBAAkB,CAAC,IAAI,iBAAiB,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;SAClE;KACF,CAAC,CAAC;IACH,QAAQ,CAAC,QAAQ,EAAE,CAAC;IAEpB,MAAM,eAAe,GAAG,IAAI,6BAA6B,EAAE,CAAC;IAC5D,eAAe,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IAC5C,eAAe,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAExC,MAAM,YAAY,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE;QACpD,IAAI,CAAC;YACH,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC;YAC5B,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QACV,OAAO,CAAC,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,CAAC,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IAErC,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@astropods/adapter-claude-agent-sdk",
3
+ "license": "Apache-2.0",
4
+ "version": "0.2.0",
5
+ "description": "Drop-in replacement for @anthropic-ai/claude-agent-sdk that adds OpenTelemetry instrumentation for Astro deployments.",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "dev": "tsc --watch",
21
+ "test": "bun test"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/astropods/adapters.git",
26
+ "directory": "packages/claude-agent-sdk"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org"
31
+ },
32
+ "dependencies": {
33
+ "@anthropic-ai/claude-agent-sdk": "^0.3.142",
34
+ "@arizeai/openinference-instrumentation-claude-agent-sdk": "^0.1.0",
35
+ "@opentelemetry/api": "^1.9.0",
36
+ "@opentelemetry/exporter-trace-otlp-http": "^0.200.0",
37
+ "@opentelemetry/resources": "^2.1.0",
38
+ "@opentelemetry/sdk-trace-node": "^2.1.0"
39
+ },
40
+ "devDependencies": {
41
+ "@types/bun": "^1.3.5",
42
+ "typescript": "^5.3.0"
43
+ }
44
+ }