@lelemondev/sdk 0.9.3 → 0.9.5

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
@@ -1,264 +1,40 @@
1
- /**
2
- * Core types for Lelemon SDK
3
- */
4
- interface ServiceConfig {
5
- /** Service name (e.g., "my-ai-chatbot") */
6
- name?: string;
7
- /** Service version (e.g., "1.2.3") */
8
- version?: string;
9
- /** Deployment environment (e.g., "production", "staging", "development") */
10
- environment?: string;
11
- }
12
- interface LelemonConfig {
13
- /** API key (or set LELEMON_API_KEY env var) */
14
- apiKey?: string;
15
- /** API endpoint (default: https://lelemon.dev) */
16
- endpoint?: string;
17
- /** Enable debug logging */
18
- debug?: boolean;
19
- /** Disable tracing */
20
- disabled?: boolean;
21
- /** Batch size before flush (default: 10) */
22
- batchSize?: number;
23
- /** Auto-flush interval in ms (default: 1000) */
24
- flushIntervalMs?: number;
25
- /** Request timeout in ms (default: 10000) */
26
- requestTimeoutMs?: number;
27
- /** Service metadata for telemetry */
28
- service?: ServiceConfig;
29
- }
30
- interface SDKTelemetry {
31
- /** SDK name */
32
- 'telemetry.sdk.name': string;
33
- /** SDK version */
34
- 'telemetry.sdk.version': string;
35
- /** SDK language */
36
- 'telemetry.sdk.language': string;
37
- /** Runtime name (nodejs, deno, bun, browser) */
38
- 'process.runtime.name'?: string;
39
- /** Runtime version */
40
- 'process.runtime.version'?: string;
41
- /** OS type (linux, darwin, windows) */
42
- 'os.type'?: string;
43
- /** Service name */
44
- 'service.name'?: string;
45
- /** Service version */
46
- 'service.version'?: string;
47
- /** Deployment environment */
48
- 'deployment.environment'?: string;
49
- }
50
- type ProviderName = 'openai' | 'anthropic' | 'gemini' | 'bedrock' | 'openrouter' | 'agent' | 'unknown';
51
- interface ObserveOptions {
52
- /** Session ID to group related calls */
53
- sessionId?: string;
54
- /** User ID for the end user */
55
- userId?: string;
56
- /** Custom metadata added to all traces */
57
- metadata?: Record<string, unknown>;
58
- /** Tags for filtering */
59
- tags?: string[];
60
- }
61
- type SpanType = 'llm' | 'agent' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
62
- interface CaptureSpanOptions {
63
- /** Span type */
64
- type: SpanType;
65
- /** Span name (tool name, retrieval source, custom name) */
66
- name: string;
67
- /** Input data */
68
- input: unknown;
69
- /** Output data */
70
- output: unknown;
71
- /** Duration in milliseconds */
72
- durationMs: number;
73
- /** Status */
74
- status?: 'success' | 'error';
75
- /** Error message if status is 'error' */
76
- errorMessage?: string;
77
- /** Tool call ID (for linking tool results) */
78
- toolCallId?: string;
79
- /** Custom metadata */
80
- metadata?: Record<string, unknown>;
81
- }
1
+ import { O as ObserveOptions } from './capture-ChybLulj.mjs';
2
+ export { C as CaptureSpanOptions, L as LelemonConfig, P as ProviderName, b as SDKTelemetry, S as ServiceConfig, h as SpanOptions, d as SpanType, T as TraceContext, e as TraceOptions, c as captureSpan, f as flush, g as getTraceContext, i as init, a as isEnabled, s as span, t as trace } from './capture-ChybLulj.mjs';
82
3
 
83
4
  /**
84
- * Global Configuration
5
+ * Observe Function
85
6
  *
86
- * Manages SDK configuration and transport instance.
7
+ * Main entry point for wrapping LLM clients with automatic tracing.
87
8
  */
88
9
 
89
10
  /**
90
- * Initialize the SDK
91
- * Call once at app startup
92
- */
93
- declare function init(config?: LelemonConfig): void;
94
- /**
95
- * Check if SDK is enabled
96
- */
97
- declare function isEnabled(): boolean;
98
- /**
99
- * Flush all pending traces
100
- */
101
- declare function flush(): Promise<void>;
102
-
103
- /**
104
- * Trace Context Module
11
+ * Wrap an LLM client with automatic tracing
105
12
  *
106
- * Provides AsyncLocalStorage-based context for grouping spans under a parent trace.
107
- * Supports hierarchical tracing where:
108
- * - trace() creates a root "agent" span
109
- * - LLM calls become children of the root
110
- * - Tool calls become children of the LLM that triggered them (via toolCallId linking)
13
+ * @param client - OpenAI, Anthropic, or Bedrock client instance
14
+ * @param options - Optional context (sessionId, userId, etc.)
15
+ * @returns The wrapped client with the same type
111
16
  *
112
17
  * @example
113
- * ```typescript
114
- * import { trace, span } from '@lelemondev/sdk';
115
- *
116
- * await trace({ name: 'sales-agent', input: userMessage }, async () => {
117
- * const response = await client.send(new ConverseCommand({...}));
118
- * // Tools automatically linked to their parent LLM via toolCallId
119
- * return response;
120
- * });
121
- * ```
122
- */
123
- interface TraceContext {
124
- /** Unique trace ID (shared by all spans in this trace) */
125
- traceId: string;
126
- /** Root span ID (the agent/workflow span) */
127
- rootSpanId: string;
128
- /** Current span ID (for nesting - LLM calls become children of this) */
129
- currentSpanId: string;
130
- /** Parent span ID (for nested trace() calls) */
131
- parentSpanId?: string;
132
- /** Trace name */
133
- name: string;
134
- /** Start time in ms */
135
- startTime: number;
136
- /** Input data */
137
- input?: unknown;
138
- /** Trace metadata */
139
- metadata?: Record<string, unknown>;
140
- /** Trace tags */
141
- tags?: string[];
142
- /** Map of toolCallId → llmSpanId for linking tool spans to their parent LLM */
143
- pendingToolCalls: Map<string, string>;
144
- }
145
- interface TraceOptions {
146
- /** Name for the trace (e.g., 'sales-agent', 'rag-query') */
147
- name: string;
148
- /** Input data for the trace */
149
- input?: unknown;
150
- /** Custom metadata */
151
- metadata?: Record<string, unknown>;
152
- /** Tags for filtering */
153
- tags?: string[];
154
- }
155
- interface SpanOptions {
156
- /** Span type */
157
- type: 'retrieval' | 'embedding' | 'tool' | 'guardrail' | 'rerank' | 'custom';
158
- /** Span name (e.g., 'pinecone-search', 'cohere-rerank') */
159
- name: string;
160
- /** Input data */
161
- input?: unknown;
162
- /** Output data */
163
- output?: unknown;
164
- /** Duration in milliseconds (optional, will be set automatically if not provided) */
165
- durationMs?: number;
166
- /** Status */
167
- status?: 'success' | 'error';
168
- /** Error message if status is 'error' */
169
- errorMessage?: string;
170
- /** Tool call ID (links this tool span to the LLM that requested it) */
171
- toolCallId?: string;
172
- /** Custom metadata */
173
- metadata?: Record<string, unknown>;
174
- }
175
- /**
176
- * Get the current trace context, if any
177
- */
178
- declare function getTraceContext(): TraceContext | undefined;
179
- /**
180
- * Execute a function within a trace context.
181
- * Creates a root "agent" span that contains all LLM calls and tool executions.
182
- * The result of the function becomes the output of the root span.
18
+ * import { observe } from '@lelemondev/sdk';
19
+ * import OpenAI from 'openai';
183
20
  *
184
- * @example Simple usage (just name)
185
- * ```typescript
186
- * await trace('sales-agent', async () => {
187
- * await client.send(new ConverseCommand({...}));
188
- * return finalResponse;
189
- * });
190
- * ```
21
+ * const openai = observe(new OpenAI());
191
22
  *
192
- * @example With options
193
- * ```typescript
194
- * await trace({ name: 'rag-query', input: question, tags: ['production'] }, async () => {
195
- * const docs = await search(question);
196
- * span({ type: 'retrieval', name: 'pinecone', output: { count: docs.length } });
197
- * return client.send(new ConverseCommand({...}));
198
- * });
199
- * ```
23
+ * // All calls are now automatically traced
24
+ * const response = await openai.chat.completions.create({...});
200
25
  */
201
- declare function trace<T>(nameOrOptions: string | TraceOptions, fn: () => Promise<T>): Promise<T>;
26
+ declare function observe<T>(client: T, options?: ObserveOptions): T;
202
27
  /**
203
- * Manually capture a span for non-LLM operations (retrieval, embedding, tool, etc.)
204
- * Must be called within a trace() block.
205
- *
206
- * @example Tool with toolCallId (links to parent LLM)
207
- * ```typescript
208
- * span({
209
- * type: 'tool',
210
- * name: 'query_database',
211
- * toolCallId: 'tooluse_abc123', // Links to LLM that requested this
212
- * input: { sql: 'SELECT ...' },
213
- * output: { rows: [...] },
214
- * durationMs: 15,
215
- * });
216
- * ```
217
- *
218
- * @example Retrieval without toolCallId
219
- * ```typescript
220
- * span({
221
- * type: 'retrieval',
222
- * name: 'pinecone-search',
223
- * input: { topK: 5 },
224
- * output: { count: 10 },
225
- * durationMs: 50,
226
- * });
227
- * ```
228
- */
229
- declare function span(options: SpanOptions): void;
230
-
231
- /**
232
- * Capture Module
233
- *
234
- * Handles trace capture and batching.
235
- * Called by providers to record LLM calls.
236
- */
237
-
238
- /**
239
- * Manually capture a span (tool call, retrieval, custom)
240
- * Use this when auto-detection doesn't cover your use case
28
+ * Create a scoped observe function with preset context
241
29
  *
242
30
  * @example
243
- * // Capture a tool call
244
- * captureSpan({
245
- * type: 'tool',
246
- * name: 'get_weather',
247
- * input: { location: 'San Francisco' },
248
- * output: { temperature: 72, conditions: 'sunny' },
249
- * durationMs: 150,
31
+ * const observeWithSession = createObserve({
32
+ * sessionId: 'session-123',
33
+ * userId: 'user-456',
250
34
  * });
251
35
  *
252
- * @example
253
- * // Capture a retrieval/RAG operation
254
- * captureSpan({
255
- * type: 'retrieval',
256
- * name: 'vector_search',
257
- * input: { query: 'user question', k: 5 },
258
- * output: { documents: [...] },
259
- * durationMs: 50,
260
- * });
36
+ * const openai = observeWithSession(new OpenAI());
261
37
  */
262
- declare function captureSpan(options: CaptureSpanOptions): void;
38
+ declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
263
39
 
264
- export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SDKTelemetry, type ServiceConfig, type SpanOptions, type SpanType, type TraceContext, type TraceOptions, captureSpan, flush, getTraceContext, init, isEnabled, span, trace };
40
+ export { ObserveOptions, createObserve, observe };
package/dist/index.d.ts CHANGED
@@ -1,264 +1,40 @@
1
- /**
2
- * Core types for Lelemon SDK
3
- */
4
- interface ServiceConfig {
5
- /** Service name (e.g., "my-ai-chatbot") */
6
- name?: string;
7
- /** Service version (e.g., "1.2.3") */
8
- version?: string;
9
- /** Deployment environment (e.g., "production", "staging", "development") */
10
- environment?: string;
11
- }
12
- interface LelemonConfig {
13
- /** API key (or set LELEMON_API_KEY env var) */
14
- apiKey?: string;
15
- /** API endpoint (default: https://lelemon.dev) */
16
- endpoint?: string;
17
- /** Enable debug logging */
18
- debug?: boolean;
19
- /** Disable tracing */
20
- disabled?: boolean;
21
- /** Batch size before flush (default: 10) */
22
- batchSize?: number;
23
- /** Auto-flush interval in ms (default: 1000) */
24
- flushIntervalMs?: number;
25
- /** Request timeout in ms (default: 10000) */
26
- requestTimeoutMs?: number;
27
- /** Service metadata for telemetry */
28
- service?: ServiceConfig;
29
- }
30
- interface SDKTelemetry {
31
- /** SDK name */
32
- 'telemetry.sdk.name': string;
33
- /** SDK version */
34
- 'telemetry.sdk.version': string;
35
- /** SDK language */
36
- 'telemetry.sdk.language': string;
37
- /** Runtime name (nodejs, deno, bun, browser) */
38
- 'process.runtime.name'?: string;
39
- /** Runtime version */
40
- 'process.runtime.version'?: string;
41
- /** OS type (linux, darwin, windows) */
42
- 'os.type'?: string;
43
- /** Service name */
44
- 'service.name'?: string;
45
- /** Service version */
46
- 'service.version'?: string;
47
- /** Deployment environment */
48
- 'deployment.environment'?: string;
49
- }
50
- type ProviderName = 'openai' | 'anthropic' | 'gemini' | 'bedrock' | 'openrouter' | 'agent' | 'unknown';
51
- interface ObserveOptions {
52
- /** Session ID to group related calls */
53
- sessionId?: string;
54
- /** User ID for the end user */
55
- userId?: string;
56
- /** Custom metadata added to all traces */
57
- metadata?: Record<string, unknown>;
58
- /** Tags for filtering */
59
- tags?: string[];
60
- }
61
- type SpanType = 'llm' | 'agent' | 'tool' | 'retrieval' | 'embedding' | 'guardrail' | 'rerank' | 'custom';
62
- interface CaptureSpanOptions {
63
- /** Span type */
64
- type: SpanType;
65
- /** Span name (tool name, retrieval source, custom name) */
66
- name: string;
67
- /** Input data */
68
- input: unknown;
69
- /** Output data */
70
- output: unknown;
71
- /** Duration in milliseconds */
72
- durationMs: number;
73
- /** Status */
74
- status?: 'success' | 'error';
75
- /** Error message if status is 'error' */
76
- errorMessage?: string;
77
- /** Tool call ID (for linking tool results) */
78
- toolCallId?: string;
79
- /** Custom metadata */
80
- metadata?: Record<string, unknown>;
81
- }
1
+ import { O as ObserveOptions } from './capture-ChybLulj.js';
2
+ export { C as CaptureSpanOptions, L as LelemonConfig, P as ProviderName, b as SDKTelemetry, S as ServiceConfig, h as SpanOptions, d as SpanType, T as TraceContext, e as TraceOptions, c as captureSpan, f as flush, g as getTraceContext, i as init, a as isEnabled, s as span, t as trace } from './capture-ChybLulj.js';
82
3
 
83
4
  /**
84
- * Global Configuration
5
+ * Observe Function
85
6
  *
86
- * Manages SDK configuration and transport instance.
7
+ * Main entry point for wrapping LLM clients with automatic tracing.
87
8
  */
88
9
 
89
10
  /**
90
- * Initialize the SDK
91
- * Call once at app startup
92
- */
93
- declare function init(config?: LelemonConfig): void;
94
- /**
95
- * Check if SDK is enabled
96
- */
97
- declare function isEnabled(): boolean;
98
- /**
99
- * Flush all pending traces
100
- */
101
- declare function flush(): Promise<void>;
102
-
103
- /**
104
- * Trace Context Module
11
+ * Wrap an LLM client with automatic tracing
105
12
  *
106
- * Provides AsyncLocalStorage-based context for grouping spans under a parent trace.
107
- * Supports hierarchical tracing where:
108
- * - trace() creates a root "agent" span
109
- * - LLM calls become children of the root
110
- * - Tool calls become children of the LLM that triggered them (via toolCallId linking)
13
+ * @param client - OpenAI, Anthropic, or Bedrock client instance
14
+ * @param options - Optional context (sessionId, userId, etc.)
15
+ * @returns The wrapped client with the same type
111
16
  *
112
17
  * @example
113
- * ```typescript
114
- * import { trace, span } from '@lelemondev/sdk';
115
- *
116
- * await trace({ name: 'sales-agent', input: userMessage }, async () => {
117
- * const response = await client.send(new ConverseCommand({...}));
118
- * // Tools automatically linked to their parent LLM via toolCallId
119
- * return response;
120
- * });
121
- * ```
122
- */
123
- interface TraceContext {
124
- /** Unique trace ID (shared by all spans in this trace) */
125
- traceId: string;
126
- /** Root span ID (the agent/workflow span) */
127
- rootSpanId: string;
128
- /** Current span ID (for nesting - LLM calls become children of this) */
129
- currentSpanId: string;
130
- /** Parent span ID (for nested trace() calls) */
131
- parentSpanId?: string;
132
- /** Trace name */
133
- name: string;
134
- /** Start time in ms */
135
- startTime: number;
136
- /** Input data */
137
- input?: unknown;
138
- /** Trace metadata */
139
- metadata?: Record<string, unknown>;
140
- /** Trace tags */
141
- tags?: string[];
142
- /** Map of toolCallId → llmSpanId for linking tool spans to their parent LLM */
143
- pendingToolCalls: Map<string, string>;
144
- }
145
- interface TraceOptions {
146
- /** Name for the trace (e.g., 'sales-agent', 'rag-query') */
147
- name: string;
148
- /** Input data for the trace */
149
- input?: unknown;
150
- /** Custom metadata */
151
- metadata?: Record<string, unknown>;
152
- /** Tags for filtering */
153
- tags?: string[];
154
- }
155
- interface SpanOptions {
156
- /** Span type */
157
- type: 'retrieval' | 'embedding' | 'tool' | 'guardrail' | 'rerank' | 'custom';
158
- /** Span name (e.g., 'pinecone-search', 'cohere-rerank') */
159
- name: string;
160
- /** Input data */
161
- input?: unknown;
162
- /** Output data */
163
- output?: unknown;
164
- /** Duration in milliseconds (optional, will be set automatically if not provided) */
165
- durationMs?: number;
166
- /** Status */
167
- status?: 'success' | 'error';
168
- /** Error message if status is 'error' */
169
- errorMessage?: string;
170
- /** Tool call ID (links this tool span to the LLM that requested it) */
171
- toolCallId?: string;
172
- /** Custom metadata */
173
- metadata?: Record<string, unknown>;
174
- }
175
- /**
176
- * Get the current trace context, if any
177
- */
178
- declare function getTraceContext(): TraceContext | undefined;
179
- /**
180
- * Execute a function within a trace context.
181
- * Creates a root "agent" span that contains all LLM calls and tool executions.
182
- * The result of the function becomes the output of the root span.
18
+ * import { observe } from '@lelemondev/sdk';
19
+ * import OpenAI from 'openai';
183
20
  *
184
- * @example Simple usage (just name)
185
- * ```typescript
186
- * await trace('sales-agent', async () => {
187
- * await client.send(new ConverseCommand({...}));
188
- * return finalResponse;
189
- * });
190
- * ```
21
+ * const openai = observe(new OpenAI());
191
22
  *
192
- * @example With options
193
- * ```typescript
194
- * await trace({ name: 'rag-query', input: question, tags: ['production'] }, async () => {
195
- * const docs = await search(question);
196
- * span({ type: 'retrieval', name: 'pinecone', output: { count: docs.length } });
197
- * return client.send(new ConverseCommand({...}));
198
- * });
199
- * ```
23
+ * // All calls are now automatically traced
24
+ * const response = await openai.chat.completions.create({...});
200
25
  */
201
- declare function trace<T>(nameOrOptions: string | TraceOptions, fn: () => Promise<T>): Promise<T>;
26
+ declare function observe<T>(client: T, options?: ObserveOptions): T;
202
27
  /**
203
- * Manually capture a span for non-LLM operations (retrieval, embedding, tool, etc.)
204
- * Must be called within a trace() block.
205
- *
206
- * @example Tool with toolCallId (links to parent LLM)
207
- * ```typescript
208
- * span({
209
- * type: 'tool',
210
- * name: 'query_database',
211
- * toolCallId: 'tooluse_abc123', // Links to LLM that requested this
212
- * input: { sql: 'SELECT ...' },
213
- * output: { rows: [...] },
214
- * durationMs: 15,
215
- * });
216
- * ```
217
- *
218
- * @example Retrieval without toolCallId
219
- * ```typescript
220
- * span({
221
- * type: 'retrieval',
222
- * name: 'pinecone-search',
223
- * input: { topK: 5 },
224
- * output: { count: 10 },
225
- * durationMs: 50,
226
- * });
227
- * ```
228
- */
229
- declare function span(options: SpanOptions): void;
230
-
231
- /**
232
- * Capture Module
233
- *
234
- * Handles trace capture and batching.
235
- * Called by providers to record LLM calls.
236
- */
237
-
238
- /**
239
- * Manually capture a span (tool call, retrieval, custom)
240
- * Use this when auto-detection doesn't cover your use case
28
+ * Create a scoped observe function with preset context
241
29
  *
242
30
  * @example
243
- * // Capture a tool call
244
- * captureSpan({
245
- * type: 'tool',
246
- * name: 'get_weather',
247
- * input: { location: 'San Francisco' },
248
- * output: { temperature: 72, conditions: 'sunny' },
249
- * durationMs: 150,
31
+ * const observeWithSession = createObserve({
32
+ * sessionId: 'session-123',
33
+ * userId: 'user-456',
250
34
  * });
251
35
  *
252
- * @example
253
- * // Capture a retrieval/RAG operation
254
- * captureSpan({
255
- * type: 'retrieval',
256
- * name: 'vector_search',
257
- * input: { query: 'user question', k: 5 },
258
- * output: { documents: [...] },
259
- * durationMs: 50,
260
- * });
36
+ * const openai = observeWithSession(new OpenAI());
261
37
  */
262
- declare function captureSpan(options: CaptureSpanOptions): void;
38
+ declare function createObserve(defaultOptions: ObserveOptions): <T>(client: T, options?: ObserveOptions) => T;
263
39
 
264
- export { type CaptureSpanOptions, type LelemonConfig, type ObserveOptions, type ProviderName, type SDKTelemetry, type ServiceConfig, type SpanOptions, type SpanType, type TraceContext, type TraceOptions, captureSpan, flush, getTraceContext, init, isEnabled, span, trace };
40
+ export { ObserveOptions, createObserve, observe };