@lelemondev/sdk 0.6.3 → 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,6 +28,27 @@ interface ObserveOptions {
28
28
  /** Tags for filtering */
29
29
  tags?: string[];
30
30
  }
31
+ type SpanType = 'llm' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
32
+ interface CaptureSpanOptions {
33
+ /** Span type */
34
+ type: SpanType;
35
+ /** Span name (tool name, retrieval source, custom name) */
36
+ name: string;
37
+ /** Input data */
38
+ input: unknown;
39
+ /** Output data */
40
+ output: unknown;
41
+ /** Duration in milliseconds */
42
+ durationMs: number;
43
+ /** Status */
44
+ status?: 'success' | 'error';
45
+ /** Error message if status is 'error' */
46
+ errorMessage?: string;
47
+ /** Tool call ID (for linking tool results) */
48
+ toolCallId?: string;
49
+ /** Custom metadata */
50
+ metadata?: Record<string, unknown>;
51
+ }
31
52
 
32
53
  /**
33
54
  * Global Configuration
@@ -85,4 +106,151 @@ declare function observe<T>(client: T, options?: ObserveOptions): T;
85
106
  */
86
107
  declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
87
108
 
88
- export { type LelemonConfig, type ObserveOptions, type ProviderName, createObserve, flush, init, isEnabled, observe };
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
+
223
+ /**
224
+ * Capture Module
225
+ *
226
+ * Handles trace capture and batching.
227
+ * Called by providers to record LLM calls.
228
+ */
229
+
230
+ /**
231
+ * Manually capture a span (tool call, retrieval, custom)
232
+ * Use this when auto-detection doesn't cover your use case
233
+ *
234
+ * @example
235
+ * // Capture a tool call
236
+ * captureSpan({
237
+ * type: 'tool',
238
+ * name: 'get_weather',
239
+ * input: { location: 'San Francisco' },
240
+ * output: { temperature: 72, conditions: 'sunny' },
241
+ * durationMs: 150,
242
+ * });
243
+ *
244
+ * @example
245
+ * // Capture a retrieval/RAG operation
246
+ * captureSpan({
247
+ * type: 'retrieval',
248
+ * name: 'vector_search',
249
+ * input: { query: 'user question', k: 5 },
250
+ * output: { documents: [...] },
251
+ * durationMs: 50,
252
+ * });
253
+ */
254
+ declare function captureSpan(options: CaptureSpanOptions): void;
255
+
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,6 +28,27 @@ interface ObserveOptions {
28
28
  /** Tags for filtering */
29
29
  tags?: string[];
30
30
  }
31
+ type SpanType = 'llm' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
32
+ interface CaptureSpanOptions {
33
+ /** Span type */
34
+ type: SpanType;
35
+ /** Span name (tool name, retrieval source, custom name) */
36
+ name: string;
37
+ /** Input data */
38
+ input: unknown;
39
+ /** Output data */
40
+ output: unknown;
41
+ /** Duration in milliseconds */
42
+ durationMs: number;
43
+ /** Status */
44
+ status?: 'success' | 'error';
45
+ /** Error message if status is 'error' */
46
+ errorMessage?: string;
47
+ /** Tool call ID (for linking tool results) */
48
+ toolCallId?: string;
49
+ /** Custom metadata */
50
+ metadata?: Record<string, unknown>;
51
+ }
31
52
 
32
53
  /**
33
54
  * Global Configuration
@@ -85,4 +106,151 @@ declare function observe<T>(client: T, options?: ObserveOptions): T;
85
106
  */
86
107
  declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
87
108
 
88
- export { type LelemonConfig, type ObserveOptions, type ProviderName, createObserve, flush, init, isEnabled, observe };
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
+
223
+ /**
224
+ * Capture Module
225
+ *
226
+ * Handles trace capture and batching.
227
+ * Called by providers to record LLM calls.
228
+ */
229
+
230
+ /**
231
+ * Manually capture a span (tool call, retrieval, custom)
232
+ * Use this when auto-detection doesn't cover your use case
233
+ *
234
+ * @example
235
+ * // Capture a tool call
236
+ * captureSpan({
237
+ * type: 'tool',
238
+ * name: 'get_weather',
239
+ * input: { location: 'San Francisco' },
240
+ * output: { temperature: 72, conditions: 'sunny' },
241
+ * durationMs: 150,
242
+ * });
243
+ *
244
+ * @example
245
+ * // Capture a retrieval/RAG operation
246
+ * captureSpan({
247
+ * type: 'retrieval',
248
+ * name: 'vector_search',
249
+ * input: { query: 'user question', k: 5 },
250
+ * output: { documents: [...] },
251
+ * durationMs: 50,
252
+ * });
253
+ */
254
+ declare function captureSpan(options: CaptureSpanOptions): void;
255
+
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 };