@lelemondev/sdk 0.6.0 → 0.6.2

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