@lelemondev/sdk 0.6.0 → 0.6.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.mjs CHANGED
@@ -3,6 +3,77 @@ var __defProp = Object.defineProperty;
3
3
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
 
6
+ // src/core/logger.ts
7
+ var debugEnabled = false;
8
+ function setDebug(enabled) {
9
+ debugEnabled = enabled;
10
+ }
11
+ function isDebugEnabled() {
12
+ if (debugEnabled) return true;
13
+ return getEnvVar("LELEMON_DEBUG") === "true";
14
+ }
15
+ var PREFIX = "[Lelemon]";
16
+ function debug(message, data) {
17
+ if (!isDebugEnabled()) return;
18
+ logWithPrefix("debug", message, data);
19
+ }
20
+ function info(message, data) {
21
+ if (!isDebugEnabled()) return;
22
+ logWithPrefix("info", message, data);
23
+ }
24
+ function warn(message, data) {
25
+ logWithPrefix("warn", message, data);
26
+ }
27
+ function traceCapture(provider, model, durationMs, status) {
28
+ if (!isDebugEnabled()) return;
29
+ console.log(
30
+ `${PREFIX} Captured trace: provider=${provider} model=${model} duration=${durationMs}ms status=${status}`
31
+ );
32
+ }
33
+ function traceCaptureError(provider, error) {
34
+ if (!isDebugEnabled()) return;
35
+ console.log(`${PREFIX} Failed to capture trace: provider=${provider} error=${error.message}`);
36
+ }
37
+ function clientWrapped(provider) {
38
+ if (!isDebugEnabled()) return;
39
+ console.log(`${PREFIX} Wrapped client: provider=${provider}`);
40
+ }
41
+ function batchSend(count, endpoint) {
42
+ if (!isDebugEnabled()) return;
43
+ console.log(`${PREFIX} Sending batch: count=${count} endpoint=${endpoint}`);
44
+ }
45
+ function batchSuccess(count, durationMs) {
46
+ if (!isDebugEnabled()) return;
47
+ console.log(`${PREFIX} Batch sent successfully: count=${count} duration=${durationMs}ms`);
48
+ }
49
+ function batchError(count, error) {
50
+ if (!isDebugEnabled()) return;
51
+ const message = error instanceof Error ? error.message : String(error);
52
+ console.log(`${PREFIX} Batch send failed: count=${count} error=${message}`);
53
+ }
54
+ function requestDetails(method, url, bodySize) {
55
+ if (!isDebugEnabled()) return;
56
+ console.log(`${PREFIX} Request: ${method} ${url} (${bodySize} bytes)`);
57
+ }
58
+ function responseDetails(status, durationMs) {
59
+ if (!isDebugEnabled()) return;
60
+ console.log(`${PREFIX} Response: status=${status} duration=${durationMs}ms`);
61
+ }
62
+ function logWithPrefix(level, message, data) {
63
+ const logFn = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
64
+ if (data !== void 0) {
65
+ logFn(`${PREFIX} ${message}`, data);
66
+ } else {
67
+ logFn(`${PREFIX} ${message}`);
68
+ }
69
+ }
70
+ function getEnvVar(name) {
71
+ if (typeof process !== "undefined" && process.env) {
72
+ return process.env[name];
73
+ }
74
+ return void 0;
75
+ }
76
+
6
77
  // src/core/transport.ts
7
78
  var DEFAULT_BATCH_SIZE = 10;
8
79
  var DEFAULT_FLUSH_INTERVAL_MS = 1e3;
@@ -85,19 +156,24 @@ var Transport = class {
85
156
  }
86
157
  async sendBatch(items) {
87
158
  if (items.length === 0) return;
88
- this.log(`Sending batch of ${items.length} traces`);
159
+ const startTime = Date.now();
160
+ batchSend(items.length, `${this.config.endpoint}/api/v1/ingest`);
89
161
  try {
90
162
  await this.request("POST", "/api/v1/ingest", { events: items });
163
+ batchSuccess(items.length, Date.now() - startTime);
91
164
  } catch (error) {
92
- this.log("Batch send failed", error);
165
+ batchError(items.length, error);
93
166
  }
94
167
  }
95
168
  async request(method, path, body) {
96
169
  const url = `${this.config.endpoint}${path}`;
97
170
  const controller = new AbortController();
171
+ const bodyStr = body ? JSON.stringify(body) : void 0;
172
+ requestDetails(method, url, bodyStr?.length ?? 0);
98
173
  const timeoutId = setTimeout(() => {
99
174
  controller.abort();
100
175
  }, this.config.requestTimeoutMs);
176
+ const startTime = Date.now();
101
177
  try {
102
178
  const response = await fetch(url, {
103
179
  method,
@@ -105,10 +181,11 @@ var Transport = class {
105
181
  "Content-Type": "application/json",
106
182
  "Authorization": `Bearer ${this.config.apiKey}`
107
183
  },
108
- body: body ? JSON.stringify(body) : void 0,
184
+ body: bodyStr,
109
185
  signal: controller.signal
110
186
  });
111
187
  clearTimeout(timeoutId);
188
+ responseDetails(response.status, Date.now() - startTime);
112
189
  if (!response.ok) {
113
190
  const errorText = await response.text().catch(() => "Unknown error");
114
191
  throw new Error(`HTTP ${response.status}: ${errorText}`);
@@ -123,15 +200,6 @@ var Transport = class {
123
200
  throw error;
124
201
  }
125
202
  }
126
- log(message, data) {
127
- if (this.config.debug) {
128
- if (data !== void 0) {
129
- console.log(`[Lelemon] ${message}`, data);
130
- } else {
131
- console.log(`[Lelemon] ${message}`);
132
- }
133
- }
134
- }
135
203
  };
136
204
 
137
205
  // src/core/config.ts
@@ -140,7 +208,16 @@ var globalTransport = null;
140
208
  var DEFAULT_ENDPOINT = "https://lelemon.dev";
141
209
  function init(config = {}) {
142
210
  globalConfig = config;
211
+ if (config.debug) {
212
+ setDebug(true);
213
+ }
214
+ info("Initializing SDK", {
215
+ endpoint: config.endpoint ?? DEFAULT_ENDPOINT,
216
+ debug: config.debug ?? false,
217
+ disabled: config.disabled ?? false
218
+ });
143
219
  globalTransport = createTransport(config);
220
+ info("SDK initialized successfully");
144
221
  }
145
222
  function getConfig() {
146
223
  return globalConfig;
@@ -160,11 +237,9 @@ async function flush() {
160
237
  }
161
238
  }
162
239
  function createTransport(config) {
163
- const apiKey = config.apiKey ?? getEnvVar("LELEMON_API_KEY");
240
+ const apiKey = config.apiKey ?? getEnvVar2("LELEMON_API_KEY");
164
241
  if (!apiKey && !config.disabled) {
165
- console.warn(
166
- "[Lelemon] No API key provided. Set apiKey in init() or LELEMON_API_KEY env var. Tracing disabled."
167
- );
242
+ warn("No API key provided. Set apiKey in init() or LELEMON_API_KEY env var. Tracing disabled.");
168
243
  }
169
244
  return new Transport({
170
245
  apiKey: apiKey ?? "",
@@ -176,7 +251,7 @@ function createTransport(config) {
176
251
  requestTimeoutMs: config.requestTimeoutMs
177
252
  });
178
253
  }
179
- function getEnvVar(name) {
254
+ function getEnvVar2(name) {
180
255
  if (typeof process !== "undefined" && process.env) {
181
256
  return process.env[name];
182
257
  }
@@ -214,6 +289,7 @@ function isValidNumber(value) {
214
289
  var globalContext = {};
215
290
  function setGlobalContext(options) {
216
291
  globalContext = options;
292
+ debug("Global context updated", options);
217
293
  }
218
294
  function getGlobalContext() {
219
295
  return globalContext;
@@ -221,7 +297,10 @@ function getGlobalContext() {
221
297
  function captureTrace(params) {
222
298
  try {
223
299
  const transport = getTransport();
224
- if (!transport.isEnabled()) return;
300
+ if (!transport.isEnabled()) {
301
+ debug("Transport disabled, skipping trace capture");
302
+ return;
303
+ }
225
304
  const context = getGlobalContext();
226
305
  const request = {
227
306
  provider: params.provider,
@@ -238,14 +317,19 @@ function captureTrace(params) {
238
317
  metadata: { ...context.metadata, ...params.metadata },
239
318
  tags: context.tags
240
319
  };
320
+ traceCapture(params.provider, params.model, params.durationMs, params.status);
241
321
  transport.enqueue(request);
242
- } catch {
322
+ } catch (err) {
323
+ traceCaptureError(params.provider, err instanceof Error ? err : new Error(String(err)));
243
324
  }
244
325
  }
245
326
  function captureError(params) {
246
327
  try {
247
328
  const transport = getTransport();
248
- if (!transport.isEnabled()) return;
329
+ if (!transport.isEnabled()) {
330
+ debug("Transport disabled, skipping error capture");
331
+ return;
332
+ }
249
333
  const context = getGlobalContext();
250
334
  const request = {
251
335
  provider: params.provider,
@@ -264,8 +348,11 @@ function captureError(params) {
264
348
  metadata: { ...context.metadata, ...params.metadata },
265
349
  tags: context.tags
266
350
  };
351
+ traceCapture(params.provider, params.model, params.durationMs, "error");
352
+ debug("Error details", { message: params.error.message, stack: params.error.stack });
267
353
  transport.enqueue(request);
268
- } catch {
354
+ } catch (err) {
355
+ traceCaptureError(params.provider, err instanceof Error ? err : new Error(String(err)));
269
356
  }
270
357
  }
271
358
  var MAX_STRING_LENGTH = 1e5;
@@ -1684,41 +1771,30 @@ function observe(client, options) {
1684
1771
  }
1685
1772
  const config = getConfig();
1686
1773
  if (config.disabled) {
1774
+ debug("Tracing disabled, returning unwrapped client");
1687
1775
  return client;
1688
1776
  }
1689
1777
  if (canHandle5(client)) {
1690
- if (config.debug) {
1691
- console.log("[Lelemon] Wrapping OpenRouter client");
1692
- }
1778
+ clientWrapped("openrouter");
1693
1779
  return wrap3(client);
1694
1780
  }
1695
1781
  if (canHandle(client)) {
1696
- if (config.debug) {
1697
- console.log("[Lelemon] Wrapping OpenAI client");
1698
- }
1782
+ clientWrapped("openai");
1699
1783
  return wrapOpenAI(client);
1700
1784
  }
1701
1785
  if (canHandle2(client)) {
1702
- if (config.debug) {
1703
- console.log("[Lelemon] Wrapping Anthropic client");
1704
- }
1786
+ clientWrapped("anthropic");
1705
1787
  return wrapAnthropic(client);
1706
1788
  }
1707
1789
  if (canHandle3(client)) {
1708
- if (config.debug) {
1709
- console.log("[Lelemon] Wrapping Bedrock client");
1710
- }
1790
+ clientWrapped("bedrock");
1711
1791
  return wrap(client);
1712
1792
  }
1713
1793
  if (canHandle4(client)) {
1714
- if (config.debug) {
1715
- console.log("[Lelemon] Wrapping Gemini client");
1716
- }
1794
+ clientWrapped("gemini");
1717
1795
  return wrap2(client);
1718
1796
  }
1719
- console.warn(
1720
- "[Lelemon] Unknown client type. Tracing not enabled. Supported: OpenAI, OpenRouter, Anthropic, Bedrock, Gemini"
1721
- );
1797
+ warn("Unknown client type. Tracing not enabled. Supported: OpenAI, OpenRouter, Anthropic, Bedrock, Gemini");
1722
1798
  return client;
1723
1799
  }
1724
1800
  function wrapOpenAI(client) {