@lelemondev/sdk 0.1.0 → 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.
package/dist/index.d.ts CHANGED
@@ -20,6 +20,18 @@ interface LelemonConfig {
20
20
  * Disable tracing (useful for testing)
21
21
  */
22
22
  disabled?: boolean;
23
+ /**
24
+ * Number of items to batch before sending (default: 10)
25
+ */
26
+ batchSize?: number;
27
+ /**
28
+ * Interval in ms to flush pending items (default: 1000)
29
+ */
30
+ flushIntervalMs?: number;
31
+ /**
32
+ * Request timeout in ms (default: 10000)
33
+ */
34
+ requestTimeoutMs?: number;
23
35
  }
24
36
  interface TraceOptions {
25
37
  /**
@@ -125,7 +137,14 @@ interface CompleteTraceRequest {
125
137
  }
126
138
 
127
139
  /**
128
- * Transport layer for sending data to Lelemon API
140
+ * Transport layer with queue-based batching
141
+ *
142
+ * Features:
143
+ * - Fire-and-forget API (sync enqueue)
144
+ * - Automatic batching (by size or interval)
145
+ * - Single flush promise (no duplicate requests)
146
+ * - Graceful error handling (never crashes caller)
147
+ * - Request timeout protection
129
148
  */
130
149
 
131
150
  interface TransportConfig {
@@ -133,97 +152,146 @@ interface TransportConfig {
133
152
  endpoint: string;
134
153
  debug: boolean;
135
154
  disabled: boolean;
155
+ batchSize?: number;
156
+ flushIntervalMs?: number;
157
+ requestTimeoutMs?: number;
136
158
  }
137
159
  declare class Transport {
138
- private config;
160
+ private readonly config;
161
+ private queue;
162
+ private flushPromise;
163
+ private flushTimer;
164
+ private pendingResolvers;
165
+ private idCounter;
139
166
  constructor(config: TransportConfig);
140
167
  /**
141
168
  * Check if transport is enabled
142
169
  */
143
170
  isEnabled(): boolean;
144
171
  /**
145
- * Create a new trace
172
+ * Enqueue trace creation (returns promise that resolves to trace ID)
173
+ */
174
+ enqueueCreate(data: CreateTraceRequest): Promise<string | null>;
175
+ /**
176
+ * Enqueue trace completion (fire-and-forget)
146
177
  */
147
- createTrace(data: CreateTraceRequest): Promise<{
148
- id: string;
149
- }>;
178
+ enqueueComplete(traceId: string, data: CompleteTraceRequest): void;
150
179
  /**
151
- * Complete a trace (success or error)
180
+ * Flush all pending items
181
+ * Safe to call multiple times (deduplicates)
152
182
  */
153
- completeTrace(traceId: string, data: CompleteTraceRequest): Promise<void>;
183
+ flush(): Promise<void>;
154
184
  /**
155
- * Make HTTP request to API
185
+ * Get pending item count (for testing/debugging)
156
186
  */
187
+ getPendingCount(): number;
188
+ private generateTempId;
189
+ private enqueue;
190
+ private scheduleFlush;
191
+ private cancelScheduledFlush;
192
+ private sendBatch;
157
193
  private request;
194
+ private log;
158
195
  }
159
196
 
160
197
  /**
161
- * Lelemon Tracer - Simple, low-friction API
198
+ * Lelemon Tracer - Fire-and-forget LLM observability
162
199
  *
163
200
  * Usage:
164
201
  * const t = trace({ input: userMessage });
165
202
  * try {
166
- * // your agent code
167
- * t.success(messages);
203
+ * const result = await myAgent(userMessage);
204
+ * t.success(result.messages);
168
205
  * } catch (error) {
169
- * t.error(error, messages);
206
+ * t.error(error);
207
+ * throw error;
170
208
  * }
209
+ *
210
+ * For serverless:
211
+ * await flush(); // Before response
171
212
  */
172
213
 
173
214
  /**
174
- * Initialize the SDK globally (optional)
175
- * If not called, trace() will auto-initialize with env vars
215
+ * Initialize the SDK (optional, will auto-init with env vars)
216
+ *
217
+ * @example
218
+ * init({ apiKey: 'le_xxx' });
219
+ * init({ apiKey: 'le_xxx', debug: true });
176
220
  */
177
221
  declare function init(config?: LelemonConfig): void;
178
222
  /**
179
- * Active trace handle returned by trace()
223
+ * Start a new trace
224
+ *
225
+ * @example
226
+ * const t = trace({ input: userMessage });
227
+ * try {
228
+ * const result = await myAgent(userMessage);
229
+ * t.success(result.messages);
230
+ * } catch (error) {
231
+ * t.error(error);
232
+ * throw error;
233
+ * }
234
+ */
235
+ declare function trace(options: TraceOptions): Trace;
236
+ /**
237
+ * Flush all pending traces to the server
238
+ * Call this before process exit in serverless environments
239
+ *
240
+ * @example
241
+ * // In Next.js API route
242
+ * export async function POST(req: Request) {
243
+ * // ... your code with traces ...
244
+ * await flush();
245
+ * return Response.json(result);
246
+ * }
247
+ *
248
+ * // With Vercel waitUntil
249
+ * import { waitUntil } from '@vercel/functions';
250
+ * waitUntil(flush());
180
251
  */
252
+ declare function flush(): Promise<void>;
253
+ /**
254
+ * Check if SDK is enabled
255
+ */
256
+ declare function isEnabled(): boolean;
181
257
  declare class Trace {
182
258
  private id;
183
- private transport;
184
- private options;
185
- private startTime;
259
+ private idPromise;
260
+ private readonly transport;
261
+ private readonly startTime;
262
+ private readonly debug;
263
+ private readonly disabled;
186
264
  private completed;
187
- private debug;
188
- private disabled;
189
265
  private llmCalls;
190
266
  constructor(options: TraceOptions, transport: Transport, debug: boolean, disabled: boolean);
191
267
  /**
192
- * Initialize trace on server (called internally)
268
+ * Log an LLM response for token tracking
269
+ * Optional - use if you want per-call token counts
193
270
  */
194
- init(): Promise<void>;
271
+ log(response: unknown): this;
195
272
  /**
196
- * Log an LLM response (optional - for tracking individual calls)
197
- * Use this if you want to track tokens per call, not just at the end
273
+ * Complete trace successfully (fire-and-forget)
274
+ *
275
+ * @param messages - Full message history (OpenAI/Anthropic format)
198
276
  */
199
- log(response: unknown): void;
277
+ success(messages: unknown): void;
200
278
  /**
201
- * Complete trace successfully
202
- * @param messages - The full message history (OpenAI/Anthropic format)
279
+ * Complete trace with error (fire-and-forget)
280
+ *
281
+ * @param error - The error that occurred
282
+ * @param messages - Optional message history up to failure
203
283
  */
204
- success(messages: unknown): Promise<void>;
284
+ error(error: Error | unknown, messages?: unknown): void;
205
285
  /**
206
- * Complete trace with error
207
- * @param error - The error that occurred
208
- * @param messages - The message history up to the failure (optional)
286
+ * Get the trace ID (may be null if not yet created or failed)
287
+ */
288
+ getId(): string | null;
289
+ /**
290
+ * Wait for trace ID to be available
209
291
  */
210
- error(error: Error | unknown, messages?: unknown): Promise<void>;
292
+ waitForId(): Promise<string | null>;
293
+ private aggregateCalls;
211
294
  }
212
- /**
213
- * Start a new trace
214
- *
215
- * @example
216
- * const t = trace({ input: userMessage });
217
- * try {
218
- * const messages = [...];
219
- * // ... your agent code ...
220
- * await t.success(messages);
221
- * } catch (error) {
222
- * await t.error(error, messages);
223
- * throw error;
224
- * }
225
- */
226
- declare function trace(options: TraceOptions): Trace;
227
295
 
228
296
  /**
229
297
  * Message Parser
@@ -245,4 +313,4 @@ declare function parseResponse(response: unknown): Partial<ParsedLLMCall>;
245
313
  */
246
314
  declare function parseBedrockResponse(body: unknown): Partial<ParsedLLMCall>;
247
315
 
248
- export { type AnthropicMessage, type LelemonConfig, type Message, type OpenAIMessage, type ParsedLLMCall, type ParsedToolCall, type ParsedTrace, Trace, type TraceOptions, init, parseBedrockResponse, parseMessages, parseResponse, trace };
316
+ export { type AnthropicMessage, type LelemonConfig, type Message, type OpenAIMessage, type ParsedLLMCall, type ParsedToolCall, type ParsedTrace, Trace, type TraceOptions, flush, init, isEnabled, parseBedrockResponse, parseMessages, parseResponse, trace };