@lelemondev/sdk 0.7.0 → 0.7.1

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/dist/index.d.mts CHANGED
@@ -28,7 +28,7 @@ interface ObserveOptions {
28
28
  /** Tags for filtering */
29
29
  tags?: string[];
30
30
  }
31
- type SpanType = 'llm' | 'tool' | 'retrieval' | 'custom';
31
+ type SpanType = 'llm' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
32
32
  interface CaptureSpanOptions {
33
33
  /** Span type */
34
34
  type: SpanType;
@@ -106,6 +106,120 @@ declare function observe<T>(client: T, options?: ObserveOptions): T;
106
106
  */
107
107
  declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
108
108
 
109
+ /**
110
+ * Trace Context Module
111
+ *
112
+ * Provides AsyncLocalStorage-based context for grouping spans under a parent trace.
113
+ * This enables hierarchical tracing without modifying the simple observe() API.
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * import { trace, span } from '@lelemondev/sdk';
118
+ *
119
+ * await trace('sales-agent', async () => {
120
+ * await client.send(new ConverseCommand({...})); // Span 1
121
+ * await client.send(new ConverseCommand({...})); // Span 2
122
+ * span({ type: 'retrieval', name: 'pinecone', output: { count: 5 } });
123
+ * });
124
+ * ```
125
+ */
126
+ interface TraceContext {
127
+ /** Unique trace ID (shared by all spans in this trace) */
128
+ traceId: string;
129
+ /** Root span ID (the agent/workflow span) */
130
+ rootSpanId: string;
131
+ /** Current span ID (for nesting - LLM calls become children of this) */
132
+ currentSpanId: string;
133
+ /** Parent span ID (for nested trace() calls) */
134
+ parentSpanId?: string;
135
+ /** Trace name */
136
+ name: string;
137
+ /** Start time in ms */
138
+ startTime: number;
139
+ /** Input data */
140
+ input?: unknown;
141
+ /** Trace metadata */
142
+ metadata?: Record<string, unknown>;
143
+ /** Trace tags */
144
+ tags?: string[];
145
+ }
146
+ interface TraceOptions {
147
+ /** Name for the trace (e.g., 'sales-agent', 'rag-query') */
148
+ name: string;
149
+ /** Input data for the trace */
150
+ input?: unknown;
151
+ /** Custom metadata */
152
+ metadata?: Record<string, unknown>;
153
+ /** Tags for filtering */
154
+ tags?: string[];
155
+ }
156
+ interface SpanOptions {
157
+ /** Span type */
158
+ type: 'retrieval' | 'embedding' | 'tool' | 'guardrail' | 'rerank' | 'custom';
159
+ /** Span name (e.g., 'pinecone-search', 'cohere-rerank') */
160
+ name: string;
161
+ /** Input data */
162
+ input?: unknown;
163
+ /** Output data */
164
+ output?: unknown;
165
+ /** Duration in milliseconds (optional, will be set automatically if not provided) */
166
+ durationMs?: number;
167
+ /** Status */
168
+ status?: 'success' | 'error';
169
+ /** Error message if status is 'error' */
170
+ errorMessage?: string;
171
+ /** Custom metadata */
172
+ metadata?: Record<string, unknown>;
173
+ }
174
+ /**
175
+ * Get the current trace context, if any
176
+ */
177
+ declare function getTraceContext(): TraceContext | undefined;
178
+ /**
179
+ * Execute a function within a trace context.
180
+ * All LLM calls made within this function will be grouped under the same trace.
181
+ *
182
+ * @example Simple usage (just name)
183
+ * ```typescript
184
+ * await trace('sales-agent', async () => {
185
+ * await client.send(new ConverseCommand({...}));
186
+ * await client.send(new ConverseCommand({...}));
187
+ * });
188
+ * ```
189
+ *
190
+ * @example With options
191
+ * ```typescript
192
+ * await trace({ name: 'rag-query', input: question, tags: ['production'] }, async () => {
193
+ * const docs = await search(question);
194
+ * span({ type: 'retrieval', name: 'pinecone', output: { count: docs.length } });
195
+ * return client.send(new ConverseCommand({...}));
196
+ * });
197
+ * ```
198
+ */
199
+ declare function trace<T>(nameOrOptions: string | TraceOptions, fn: () => Promise<T>): Promise<T>;
200
+ /**
201
+ * Manually capture a span for non-LLM operations (retrieval, embedding, etc.)
202
+ * Must be called within a trace() block.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * await trace('rag-query', async () => {
207
+ * const t0 = Date.now();
208
+ * const docs = await pinecone.query({ vector, topK: 5 });
209
+ * span({
210
+ * type: 'retrieval',
211
+ * name: 'pinecone-search',
212
+ * input: { topK: 5 },
213
+ * output: { count: docs.length },
214
+ * durationMs: Date.now() - t0,
215
+ * });
216
+ *
217
+ * return client.send(new ConverseCommand({...}));
218
+ * });
219
+ * ```
220
+ */
221
+ declare function span(options: SpanOptions): void;
222
+
109
223
  /**
110
224
  * Capture Module
111
225
  *
@@ -139,4 +253,4 @@ declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, o
139
253
  */
140
254
  declare function captureSpan(options: CaptureSpanOptions): void;
141
255
 
142
- export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SpanType, captureSpan, createObserve, flush, init, isEnabled, observe };
256
+ export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SpanOptions, type SpanType, type TraceOptions, captureSpan, createObserve, flush, getTraceContext, init, isEnabled, observe, span, trace };
package/dist/index.d.ts CHANGED
@@ -28,7 +28,7 @@ interface ObserveOptions {
28
28
  /** Tags for filtering */
29
29
  tags?: string[];
30
30
  }
31
- type SpanType = 'llm' | 'tool' | 'retrieval' | 'custom';
31
+ type SpanType = 'llm' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
32
32
  interface CaptureSpanOptions {
33
33
  /** Span type */
34
34
  type: SpanType;
@@ -106,6 +106,120 @@ declare function observe<T>(client: T, options?: ObserveOptions): T;
106
106
  */
107
107
  declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
108
108
 
109
+ /**
110
+ * Trace Context Module
111
+ *
112
+ * Provides AsyncLocalStorage-based context for grouping spans under a parent trace.
113
+ * This enables hierarchical tracing without modifying the simple observe() API.
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * import { trace, span } from '@lelemondev/sdk';
118
+ *
119
+ * await trace('sales-agent', async () => {
120
+ * await client.send(new ConverseCommand({...})); // Span 1
121
+ * await client.send(new ConverseCommand({...})); // Span 2
122
+ * span({ type: 'retrieval', name: 'pinecone', output: { count: 5 } });
123
+ * });
124
+ * ```
125
+ */
126
+ interface TraceContext {
127
+ /** Unique trace ID (shared by all spans in this trace) */
128
+ traceId: string;
129
+ /** Root span ID (the agent/workflow span) */
130
+ rootSpanId: string;
131
+ /** Current span ID (for nesting - LLM calls become children of this) */
132
+ currentSpanId: string;
133
+ /** Parent span ID (for nested trace() calls) */
134
+ parentSpanId?: string;
135
+ /** Trace name */
136
+ name: string;
137
+ /** Start time in ms */
138
+ startTime: number;
139
+ /** Input data */
140
+ input?: unknown;
141
+ /** Trace metadata */
142
+ metadata?: Record<string, unknown>;
143
+ /** Trace tags */
144
+ tags?: string[];
145
+ }
146
+ interface TraceOptions {
147
+ /** Name for the trace (e.g., 'sales-agent', 'rag-query') */
148
+ name: string;
149
+ /** Input data for the trace */
150
+ input?: unknown;
151
+ /** Custom metadata */
152
+ metadata?: Record<string, unknown>;
153
+ /** Tags for filtering */
154
+ tags?: string[];
155
+ }
156
+ interface SpanOptions {
157
+ /** Span type */
158
+ type: 'retrieval' | 'embedding' | 'tool' | 'guardrail' | 'rerank' | 'custom';
159
+ /** Span name (e.g., 'pinecone-search', 'cohere-rerank') */
160
+ name: string;
161
+ /** Input data */
162
+ input?: unknown;
163
+ /** Output data */
164
+ output?: unknown;
165
+ /** Duration in milliseconds (optional, will be set automatically if not provided) */
166
+ durationMs?: number;
167
+ /** Status */
168
+ status?: 'success' | 'error';
169
+ /** Error message if status is 'error' */
170
+ errorMessage?: string;
171
+ /** Custom metadata */
172
+ metadata?: Record<string, unknown>;
173
+ }
174
+ /**
175
+ * Get the current trace context, if any
176
+ */
177
+ declare function getTraceContext(): TraceContext | undefined;
178
+ /**
179
+ * Execute a function within a trace context.
180
+ * All LLM calls made within this function will be grouped under the same trace.
181
+ *
182
+ * @example Simple usage (just name)
183
+ * ```typescript
184
+ * await trace('sales-agent', async () => {
185
+ * await client.send(new ConverseCommand({...}));
186
+ * await client.send(new ConverseCommand({...}));
187
+ * });
188
+ * ```
189
+ *
190
+ * @example With options
191
+ * ```typescript
192
+ * await trace({ name: 'rag-query', input: question, tags: ['production'] }, async () => {
193
+ * const docs = await search(question);
194
+ * span({ type: 'retrieval', name: 'pinecone', output: { count: docs.length } });
195
+ * return client.send(new ConverseCommand({...}));
196
+ * });
197
+ * ```
198
+ */
199
+ declare function trace<T>(nameOrOptions: string | TraceOptions, fn: () => Promise<T>): Promise<T>;
200
+ /**
201
+ * Manually capture a span for non-LLM operations (retrieval, embedding, etc.)
202
+ * Must be called within a trace() block.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * await trace('rag-query', async () => {
207
+ * const t0 = Date.now();
208
+ * const docs = await pinecone.query({ vector, topK: 5 });
209
+ * span({
210
+ * type: 'retrieval',
211
+ * name: 'pinecone-search',
212
+ * input: { topK: 5 },
213
+ * output: { count: docs.length },
214
+ * durationMs: Date.now() - t0,
215
+ * });
216
+ *
217
+ * return client.send(new ConverseCommand({...}));
218
+ * });
219
+ * ```
220
+ */
221
+ declare function span(options: SpanOptions): void;
222
+
109
223
  /**
110
224
  * Capture Module
111
225
  *
@@ -139,4 +253,4 @@ declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, o
139
253
  */
140
254
  declare function captureSpan(options: CaptureSpanOptions): void;
141
255
 
142
- export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SpanType, captureSpan, createObserve, flush, init, isEnabled, observe };
256
+ export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SpanOptions, type SpanType, type TraceOptions, captureSpan, createObserve, flush, getTraceContext, init, isEnabled, observe, span, trace };