lightrace 0.1.10 → 0.1.14

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 CHANGED
@@ -1,11 +1,27 @@
1
- # lightrace-js
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/SKE-Labs/lightrace/main/packages/frontend/public/white_transparent.png" alt="LightRace" width="280" />
3
+ </p>
2
4
 
3
- Lightweight LLM tracing SDK for TypeScript/JavaScript with remote tool invocation.
5
+ <h1 align="center">lightrace-js</h1>
6
+
7
+ <p align="center">
8
+ <a href="https://www.npmjs.com/package/lightrace"><img src="https://img.shields.io/npm/v/lightrace?style=flat-square&color=ff1a1a" alt="npm version" /></a>
9
+ <a href="https://github.com/SKE-Labs/lightrace-js/stargazers"><img src="https://img.shields.io/github/stars/SKE-Labs/lightrace-js?style=flat-square" alt="GitHub stars" /></a>
10
+ <a href="https://github.com/SKE-Labs/lightrace-js/blob/main/LICENSE"><img src="https://img.shields.io/github/license/SKE-Labs/lightrace-js?style=flat-square" alt="License" /></a>
11
+ </p>
12
+
13
+ <p align="center">Lightweight LLM tracing SDK for TypeScript/JavaScript with remote tool invocation.</p>
14
+
15
+ ---
4
16
 
5
17
  ## Install
6
18
 
7
19
  ```bash
20
+ npm install lightrace
21
+ # or
8
22
  yarn add lightrace
23
+ # or
24
+ pnpm add lightrace
9
25
  ```
10
26
 
11
27
  ## Quick Start
@@ -74,10 +90,122 @@ trace(name, options, fn);
74
90
  | `inputSchema` | `ZodType` | `undefined` | Optional Zod schema for tool input |
75
91
  | `metadata` | `Record` | `undefined` | Static metadata attached to every call |
76
92
 
93
+ ## Integrations
94
+
95
+ ### Anthropic
96
+
97
+ ```typescript
98
+ import Anthropic from "@anthropic-ai/sdk";
99
+ import { Lightrace, trace } from "lightrace";
100
+ import { LightraceAnthropicInstrumentor } from "lightrace/integrations/anthropic";
101
+
102
+ const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
103
+
104
+ const anthropic = new Anthropic();
105
+ const instrumentor = new LightraceAnthropicInstrumentor({ client: lt });
106
+ instrumentor.instrument(anthropic);
107
+
108
+ const response = await anthropic.messages.create({
109
+ model: "claude-sonnet-4-20250514",
110
+ max_tokens: 256,
111
+ messages: [{ role: "user", content: "What is the capital of Mongolia?" }],
112
+ });
113
+
114
+ lt.flush();
115
+ await lt.shutdown();
116
+ ```
117
+
118
+ ### OpenAI
119
+
120
+ ```typescript
121
+ import OpenAI from "openai";
122
+ import { Lightrace, trace } from "lightrace";
123
+ import { LightraceOpenAIInstrumentor } from "lightrace/integrations/openai";
124
+
125
+ const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
126
+
127
+ const openai = new OpenAI();
128
+ const instrumentor = new LightraceOpenAIInstrumentor({ client: lt });
129
+ instrumentor.instrument(openai);
130
+
131
+ const response = await openai.chat.completions.create({
132
+ model: "gpt-4o-mini",
133
+ max_tokens: 256,
134
+ messages: [{ role: "user", content: "What is the speed of light?" }],
135
+ });
136
+
137
+ lt.flush();
138
+ await lt.shutdown();
139
+ ```
140
+
141
+ ### LangChain
142
+
143
+ ```typescript
144
+ import { ChatOpenAI } from "@langchain/openai";
145
+ import { Lightrace } from "lightrace";
146
+ import { LightraceCallbackHandler } from "lightrace/integrations/langchain";
147
+
148
+ const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
149
+
150
+ const handler = new LightraceCallbackHandler({ client: lt });
151
+ const model = new ChatOpenAI({ model: "gpt-4o-mini", maxTokens: 256 });
152
+
153
+ const response = await model.invoke("What is the speed of light?", {
154
+ callbacks: [handler],
155
+ });
156
+
157
+ lt.flush();
158
+ await lt.shutdown();
159
+ ```
160
+
161
+ ### Claude Agent SDK
162
+
163
+ ```typescript
164
+ import { Lightrace } from "lightrace";
165
+ import { tracedQuery } from "lightrace/integrations/claude-agent-sdk";
166
+
167
+ const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
168
+
169
+ for await (const message of tracedQuery({
170
+ prompt: "What files are in the current directory?",
171
+ options: { maxTurns: 3 },
172
+ client: lt,
173
+ traceName: "file-lister",
174
+ })) {
175
+ if (message.type === "result") {
176
+ const r = message as Record<string, unknown>;
177
+ console.log(r.result);
178
+ console.log(`Cost: $${r.total_cost_usd}`);
179
+ }
180
+ }
181
+
182
+ lt.flush();
183
+ await lt.shutdown();
184
+ ```
185
+
186
+ You can also use the handler directly for more control:
187
+
188
+ ```typescript
189
+ import { query } from "@anthropic-ai/claude-agent-sdk";
190
+ import { LightraceAgentHandler } from "lightrace/integrations/claude-agent-sdk";
191
+
192
+ const handler = new LightraceAgentHandler({ prompt: "Hello", client: lt, traceName: "my-agent" });
193
+
194
+ for await (const message of query({ prompt: "Hello" })) {
195
+ handler.handle(message);
196
+ }
197
+ ```
198
+
77
199
  ## Compatibility
78
200
 
79
201
  Lightrace server also accepts traces from Langfuse Python/JS SDKs.
80
202
 
203
+ ## Related
204
+
205
+ - [Lightrace](https://github.com/SKE-Labs/lightrace) — the main platform (backend + frontend)
206
+ - [Lightrace CLI](https://github.com/SKE-Labs/lightrace-cli) — self-host with a single command
207
+ - [lightrace-python](https://github.com/SKE-Labs/lightrace-python) — Python SDK
208
+
81
209
  ## Development
82
210
 
83
211
  ```bash
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Claude Agent SDK integration for Lightrace.
3
+ *
4
+ * Wraps the `query()` async iterator from `@anthropic-ai/claude-agent-sdk`
5
+ * to emit OTel spans for each generation, tool call, and the overall agent run.
6
+ *
7
+ * @example Wrapper (recommended)
8
+ * ```ts
9
+ * import { Lightrace } from "lightrace";
10
+ * import { tracedQuery } from "lightrace/integrations/claude-agent-sdk";
11
+ *
12
+ * const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
13
+ *
14
+ * for await (const message of tracedQuery({
15
+ * prompt: "What files are in the current directory?",
16
+ * client: lt,
17
+ * traceName: "file-lister",
18
+ * })) {
19
+ * if (message.type === "result") console.log(message.result);
20
+ * }
21
+ * ```
22
+ *
23
+ * @example Manual handler
24
+ * ```ts
25
+ * import { query } from "@anthropic-ai/claude-agent-sdk";
26
+ * import { LightraceAgentHandler } from "lightrace/integrations/claude-agent-sdk";
27
+ *
28
+ * const handler = new LightraceAgentHandler({ client: lt });
29
+ *
30
+ * for await (const message of query({ prompt: "Hello" })) {
31
+ * handler.handle(message);
32
+ * }
33
+ * ```
34
+ */
35
+ import { TracingMixin, type TracingMixinOptions } from "./_base.js";
36
+ /** Minimal message shape — avoids hard dependency on @anthropic-ai/claude-agent-sdk. */
37
+ interface SDKMessageLike {
38
+ type: string;
39
+ [key: string]: unknown;
40
+ }
41
+ export interface LightraceAgentHandlerOptions extends TracingMixinOptions {
42
+ /** The prompt sent to the agent (captured as root trace input). */
43
+ prompt?: string;
44
+ }
45
+ export interface TracedQueryOptions extends TracingMixinOptions {
46
+ /** The prompt to send to the agent. */
47
+ prompt: string;
48
+ /** `ClaudeAgentOptions` to pass through to `query()`. */
49
+ options?: unknown;
50
+ }
51
+ /**
52
+ * Processes Claude Agent SDK messages to create Lightrace traces.
53
+ *
54
+ * Call {@link handle} for each message yielded by `query()`. The handler
55
+ * automatically creates a root agent span, child generation spans for each
56
+ * `AssistantMessage`, and child tool spans for each tool call.
57
+ */
58
+ export declare class LightraceAgentHandler extends TracingMixin {
59
+ private prompt;
60
+ private agentRunId;
61
+ private toolRunIds;
62
+ private initTools;
63
+ private initModel;
64
+ private currentGenRunId;
65
+ private currentMessageId;
66
+ private currentOutputBlocks;
67
+ private currentUsage;
68
+ constructor(opts?: LightraceAgentHandlerOptions);
69
+ /** Process a single message from the agent SDK stream. */
70
+ handle(message: SDKMessageLike): void;
71
+ private onSystem;
72
+ private onAssistant;
73
+ private processContentBlocks;
74
+ private flushGeneration;
75
+ private onUser;
76
+ private onResult;
77
+ }
78
+ /**
79
+ * Drop-in wrapper around `query()` that adds Lightrace tracing.
80
+ *
81
+ * Messages are yielded through unchanged. Requires `@anthropic-ai/claude-agent-sdk`
82
+ * to be installed.
83
+ */
84
+ export declare function tracedQuery(opts: TracedQueryOptions): AsyncGenerator<SDKMessageLike, void>;
85
+ export {};
86
+ //# sourceMappingURL=claude-agent-sdk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"claude-agent-sdk.d.ts","sourceRoot":"","sources":["../../src/integrations/claude-agent-sdk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,OAAO,EAAE,YAAY,EAAkB,KAAK,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAIpF,wFAAwF;AACxF,UAAU,cAAc;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,4BAA6B,SAAQ,mBAAmB;IACvE,mEAAmE;IACnE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,kBAAmB,SAAQ,mBAAmB;IAC7D,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAID;;;;;;GAMG;AACH,qBAAa,qBAAsB,SAAQ,YAAY;IACrD,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,UAAU,CAA6B;IAC/C,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,SAAS,CAAuB;IAIxC,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,gBAAgB,CAAuB;IAC/C,OAAO,CAAC,mBAAmB,CAAiC;IAC5D,OAAO,CAAC,YAAY,CAAuC;gBAE/C,IAAI,CAAC,EAAE,4BAA4B;IAO/C,0DAA0D;IAC1D,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IA0BrC,OAAO,CAAC,QAAQ;IAahB,OAAO,CAAC,WAAW;IAiDnB,OAAO,CAAC,oBAAoB;IAgD5B,OAAO,CAAC,eAAe;IAavB,OAAO,CAAC,MAAM;IAuCd,OAAO,CAAC,QAAQ;CAkCjB;AAID;;;;;GAKG;AACH,wBAAuB,WAAW,CAAC,IAAI,EAAE,kBAAkB,GAAG,cAAc,CAAC,cAAc,EAAE,IAAI,CAAC,CA4BjG"}
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Claude Agent SDK integration for Lightrace.
3
+ *
4
+ * Wraps the `query()` async iterator from `@anthropic-ai/claude-agent-sdk`
5
+ * to emit OTel spans for each generation, tool call, and the overall agent run.
6
+ *
7
+ * @example Wrapper (recommended)
8
+ * ```ts
9
+ * import { Lightrace } from "lightrace";
10
+ * import { tracedQuery } from "lightrace/integrations/claude-agent-sdk";
11
+ *
12
+ * const lt = new Lightrace({ publicKey: "pk-lt-demo", secretKey: "sk-lt-demo" });
13
+ *
14
+ * for await (const message of tracedQuery({
15
+ * prompt: "What files are in the current directory?",
16
+ * client: lt,
17
+ * traceName: "file-lister",
18
+ * })) {
19
+ * if (message.type === "result") console.log(message.result);
20
+ * }
21
+ * ```
22
+ *
23
+ * @example Manual handler
24
+ * ```ts
25
+ * import { query } from "@anthropic-ai/claude-agent-sdk";
26
+ * import { LightraceAgentHandler } from "lightrace/integrations/claude-agent-sdk";
27
+ *
28
+ * const handler = new LightraceAgentHandler({ client: lt });
29
+ *
30
+ * for await (const message of query({ prompt: "Hello" })) {
31
+ * handler.handle(message);
32
+ * }
33
+ * ```
34
+ */
35
+ import { generateId, jsonSerializable } from "../utils.js";
36
+ import { TracingMixin, normalizeUsage } from "./_base.js";
37
+ // ── Handler ────────────────────────────────────────────────────────────────
38
+ /**
39
+ * Processes Claude Agent SDK messages to create Lightrace traces.
40
+ *
41
+ * Call {@link handle} for each message yielded by `query()`. The handler
42
+ * automatically creates a root agent span, child generation spans for each
43
+ * `AssistantMessage`, and child tool spans for each tool call.
44
+ */
45
+ export class LightraceAgentHandler extends TracingMixin {
46
+ prompt;
47
+ agentRunId = null;
48
+ toolRunIds = new Map(); // tool_use_id → runId
49
+ initTools = null;
50
+ initModel = null;
51
+ // Generation span accumulation — SDK yields multiple AssistantMessages
52
+ // per turn (thinking + tool_use) sharing the same message_id.
53
+ currentGenRunId = null;
54
+ currentMessageId = null;
55
+ currentOutputBlocks = [];
56
+ currentUsage = null;
57
+ constructor(opts) {
58
+ super(opts);
59
+ this.prompt = opts?.prompt;
60
+ }
61
+ // ── Public API ─────────────────────────────────────────────────────
62
+ /** Process a single message from the agent SDK stream. */
63
+ handle(message) {
64
+ try {
65
+ switch (message.type) {
66
+ case "assistant":
67
+ this.onAssistant(message);
68
+ break;
69
+ case "user":
70
+ this.onUser(message);
71
+ break;
72
+ case "result":
73
+ this.onResult(message);
74
+ break;
75
+ case "system":
76
+ this.onSystem(message);
77
+ break;
78
+ }
79
+ }
80
+ catch (err) {
81
+ // Tracing errors should never break the agent loop
82
+ if (typeof console !== "undefined") {
83
+ console.warn("[lightrace] Error handling message:", err);
84
+ }
85
+ }
86
+ }
87
+ // ── Message handlers ───────────────────────────────────────────────
88
+ onSystem(msg) {
89
+ if (msg.subtype === "init") {
90
+ // Tools/model are nested in a `data` dict
91
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
+ const data = msg.data;
93
+ const source = data ?? msg;
94
+ const tools = source.tools;
95
+ if (tools)
96
+ this.initTools = [...tools];
97
+ const model = source.model;
98
+ if (model)
99
+ this.initModel = model;
100
+ }
101
+ }
102
+ onAssistant(msg) {
103
+ // Start root agent span on first assistant message
104
+ if (this.agentRunId === null) {
105
+ this.agentRunId = generateId();
106
+ const input = { prompt: this.prompt };
107
+ if (this.initTools)
108
+ input.tools = this.initTools;
109
+ if (this.initModel)
110
+ input.model = this.initModel;
111
+ this.registerRun(this.agentRunId, undefined, {
112
+ type: "span",
113
+ name: this.traceName ?? "claude-agent",
114
+ startTime: new Date(),
115
+ input,
116
+ });
117
+ }
118
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
119
+ const raw = msg;
120
+ const betaMessage = raw.message;
121
+ const messageId = raw.message_id ?? betaMessage?.id;
122
+ // SDK yields multiple AssistantMessages per turn sharing the same
123
+ // message_id (e.g. thinking block, then tool_use block). Accumulate
124
+ // content blocks into a single generation span.
125
+ if (messageId && messageId === this.currentMessageId && this.currentGenRunId !== null) {
126
+ this.processContentBlocks(raw);
127
+ return;
128
+ }
129
+ // New turn — flush any pending generation from the previous turn.
130
+ this.flushGeneration();
131
+ const model = raw.model ?? betaMessage?.model;
132
+ this.currentMessageId = messageId ?? null;
133
+ this.currentGenRunId = generateId();
134
+ this.currentOutputBlocks = [];
135
+ this.currentUsage = null;
136
+ this.registerRun(this.currentGenRunId, this.agentRunId ?? undefined, {
137
+ type: "generation",
138
+ name: model ?? "claude",
139
+ startTime: new Date(),
140
+ input: undefined,
141
+ model,
142
+ });
143
+ this.processContentBlocks(raw);
144
+ }
145
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
+ processContentBlocks(raw) {
147
+ const betaMessage = raw.message;
148
+ const content = betaMessage?.content ?? raw.content ?? [];
149
+ const usage = betaMessage?.usage ?? raw.usage;
150
+ for (const block of content) {
151
+ const b = block;
152
+ const blockType = b.type;
153
+ const hasText = "text" in b && !("id" in b);
154
+ const hasTool = "id" in b && "name" in b && "input" in b;
155
+ const hasThinking = "thinking" in b;
156
+ if (blockType === "text" || (!blockType && hasText)) {
157
+ this.currentOutputBlocks.push({ type: "text", text: b.text ?? "" });
158
+ }
159
+ else if (blockType === "tool_use" || (!blockType && hasTool)) {
160
+ const toolId = b.id;
161
+ const toolName = b.name;
162
+ const toolInput = b.input;
163
+ this.currentOutputBlocks.push({
164
+ type: "tool_use",
165
+ id: toolId,
166
+ name: toolName,
167
+ input: toolInput,
168
+ });
169
+ // Start a tool span (ended when matching user message arrives)
170
+ const toolRunId = generateId();
171
+ this.toolRunIds.set(toolId, toolRunId);
172
+ this.registerRun(toolRunId, this.agentRunId ?? undefined, {
173
+ type: "tool",
174
+ name: toolName,
175
+ startTime: new Date(),
176
+ input: toolInput,
177
+ });
178
+ }
179
+ else if (blockType === "thinking" || (!blockType && hasThinking)) {
180
+ this.currentOutputBlocks.push({ type: "thinking", thinking: b.thinking ?? "" });
181
+ }
182
+ else {
183
+ this.currentOutputBlocks.push(jsonSerializable(b));
184
+ }
185
+ }
186
+ // Keep the latest usage (last AssistantMessage in this turn wins)
187
+ if (usage) {
188
+ this.currentUsage = normalizeUsage(usage);
189
+ }
190
+ }
191
+ flushGeneration() {
192
+ if (this.currentGenRunId === null)
193
+ return;
194
+ const output = {
195
+ role: "assistant",
196
+ content: this.currentOutputBlocks,
197
+ };
198
+ this.endRun(this.currentGenRunId, output, "DEFAULT", null, this.currentUsage ?? undefined);
199
+ this.currentGenRunId = null;
200
+ this.currentMessageId = null;
201
+ this.currentOutputBlocks = [];
202
+ this.currentUsage = null;
203
+ }
204
+ onUser(msg) {
205
+ this.flushGeneration();
206
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
+ const raw = msg;
208
+ const userMessage = raw.message;
209
+ const fallbackOutput = raw.tool_use_result;
210
+ // User messages contain tool results as content blocks
211
+ const content = Array.isArray(userMessage?.content)
212
+ ? userMessage.content
213
+ : Array.isArray(raw.content)
214
+ ? raw.content
215
+ : [];
216
+ for (const block of content) {
217
+ const b = block;
218
+ if ((b.type === "tool_result" || "tool_use_id" in b) && typeof b.tool_use_id === "string") {
219
+ const toolUseId = b.tool_use_id;
220
+ const toolRunId = this.toolRunIds.get(toolUseId);
221
+ if (toolRunId) {
222
+ this.toolRunIds.delete(toolUseId);
223
+ const isError = Boolean(b.is_error);
224
+ let output = b.content ?? b.output;
225
+ // Use fallback from tool_use_result when block has no output
226
+ if (output === undefined && fallbackOutput !== undefined) {
227
+ output = fallbackOutput;
228
+ }
229
+ if (isError) {
230
+ this.endRun(toolRunId, output, "ERROR", String(output));
231
+ }
232
+ else {
233
+ this.endRun(toolRunId, output);
234
+ }
235
+ }
236
+ }
237
+ }
238
+ }
239
+ onResult(msg) {
240
+ this.flushGeneration();
241
+ // End any remaining tool spans
242
+ for (const [, toolRunId] of this.toolRunIds) {
243
+ this.endRun(toolRunId, null);
244
+ }
245
+ this.toolRunIds.clear();
246
+ if (this.agentRunId === null)
247
+ return;
248
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
249
+ const raw = msg;
250
+ const output = {};
251
+ if (raw.result !== undefined)
252
+ output.result = raw.result;
253
+ if (raw.num_turns !== undefined)
254
+ output.num_turns = raw.num_turns;
255
+ if (raw.total_cost_usd !== undefined)
256
+ output.total_cost_usd = raw.total_cost_usd;
257
+ if (raw.duration_ms !== undefined)
258
+ output.duration_ms = raw.duration_ms;
259
+ if (raw.subtype !== undefined)
260
+ output.subtype = raw.subtype;
261
+ if (raw.model_usage !== undefined)
262
+ output.model_usage = jsonSerializable(raw.model_usage);
263
+ const usage = raw.usage ? normalizeUsage(raw.usage) : null;
264
+ const isError = Boolean(raw.is_error);
265
+ if (isError) {
266
+ const errorMsg = raw.result ?? raw.subtype ?? "Agent error";
267
+ this.endRun(this.agentRunId, output, "ERROR", String(errorMsg), usage ?? undefined);
268
+ }
269
+ else {
270
+ this.endRun(this.agentRunId, output, "DEFAULT", null, usage ?? undefined);
271
+ }
272
+ this.agentRunId = null;
273
+ }
274
+ }
275
+ // ── Wrapper ────────────────────────────────────────────────────────────────
276
+ /**
277
+ * Drop-in wrapper around `query()` that adds Lightrace tracing.
278
+ *
279
+ * Messages are yielded through unchanged. Requires `@anthropic-ai/claude-agent-sdk`
280
+ * to be installed.
281
+ */
282
+ export async function* tracedQuery(opts) {
283
+ // Dynamic import to avoid hard dependency
284
+ let queryFn;
285
+ try {
286
+ // Use a variable to prevent TypeScript from resolving the optional peer dep
287
+ const modName = "@anthropic-ai/claude-agent-sdk";
288
+ const mod = await import(/* webpackIgnore: true */ modName);
289
+ queryFn = mod.query;
290
+ }
291
+ catch {
292
+ throw new Error("@anthropic-ai/claude-agent-sdk is required for this integration. " +
293
+ "Install it with: npm install @anthropic-ai/claude-agent-sdk");
294
+ }
295
+ const handler = new LightraceAgentHandler({
296
+ prompt: opts.prompt,
297
+ client: opts.client,
298
+ userId: opts.userId,
299
+ sessionId: opts.sessionId,
300
+ traceName: opts.traceName,
301
+ metadata: opts.metadata,
302
+ });
303
+ for await (const message of queryFn({ prompt: opts.prompt, options: opts.options })) {
304
+ handler.handle(message);
305
+ yield message;
306
+ }
307
+ }
308
+ //# sourceMappingURL=claude-agent-sdk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"claude-agent-sdk.js","sourceRoot":"","sources":["../../src/integrations/claude-agent-sdk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,cAAc,EAA4B,MAAM,YAAY,CAAC;AAsBpF,8EAA8E;AAE9E;;;;;;GAMG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAAY;IAC7C,MAAM,CAAqB;IAC3B,UAAU,GAAkB,IAAI,CAAC;IACjC,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC,CAAC,sBAAsB;IAC9D,SAAS,GAAoB,IAAI,CAAC;IAClC,SAAS,GAAkB,IAAI,CAAC;IAExC,uEAAuE;IACvE,8DAA8D;IACtD,eAAe,GAAkB,IAAI,CAAC;IACtC,gBAAgB,GAAkB,IAAI,CAAC;IACvC,mBAAmB,GAA8B,EAAE,CAAC;IACpD,YAAY,GAAkC,IAAI,CAAC;IAE3D,YAAY,IAAmC;QAC7C,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,IAAI,CAAC,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC;IAC7B,CAAC;IAED,sEAAsE;IAEtE,0DAA0D;IAC1D,MAAM,CAAC,OAAuB;QAC5B,IAAI,CAAC;YACH,QAAQ,OA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CAAC;QACjD,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC5D,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CACb,mEAAmE;YACjE,6DAA6D,CAChE,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,qBAAqB,CAAC;QACxC,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,QAAQ,EAAE,IAAI,CAAC,QAAQ;KACxB,CAAC,CAAC;IAEH,IAAI,KAAK,EAAE,MAAM,OAAO,IAAI,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QACpF,OAAO,CAAC,MAAM,CAAC,OAAyB,CAAC,CAAC;QAC1C,MAAM,OAAyB,CAAC;IAClC,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightrace",
3
- "version": "0.1.10",
3
+ "version": "0.1.14",
4
4
  "description": "Agentic development kit — LLM tracing, tool management, and agent primitives",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -25,6 +25,10 @@
25
25
  "./integrations/llamaindex": {
26
26
  "import": "./dist/integrations/llamaindex.js",
27
27
  "types": "./dist/integrations/llamaindex.d.ts"
28
+ },
29
+ "./integrations/claude-agent-sdk": {
30
+ "import": "./dist/integrations/claude-agent-sdk.js",
31
+ "types": "./dist/integrations/claude-agent-sdk.d.ts"
28
32
  }
29
33
  },
30
34
  "files": [
@@ -69,6 +73,7 @@
69
73
  "@anthropic-ai/sdk": ">=0.30.0",
70
74
  "openai": ">=4.0.0",
71
75
  "llamaindex": ">=0.5.0",
76
+ "@anthropic-ai/claude-agent-sdk": ">=0.1.0",
72
77
  "zod": "^3.0.0"
73
78
  },
74
79
  "peerDependenciesMeta": {
@@ -86,6 +91,9 @@
86
91
  },
87
92
  "llamaindex": {
88
93
  "optional": true
94
+ },
95
+ "@anthropic-ai/claude-agent-sdk": {
96
+ "optional": true
89
97
  }
90
98
  },
91
99
  "lint-staged": {