@agentuity/telemetry 3.0.0-alpha.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.
Files changed (87) hide show
  1. package/dist/console.d.ts +33 -0
  2. package/dist/console.d.ts.map +1 -0
  3. package/dist/console.js +86 -0
  4. package/dist/console.js.map +1 -0
  5. package/dist/exporters/index.d.ts +4 -0
  6. package/dist/exporters/index.d.ts.map +1 -0
  7. package/dist/exporters/index.js +4 -0
  8. package/dist/exporters/index.js.map +1 -0
  9. package/dist/exporters/jsonl-log-exporter.d.ts +36 -0
  10. package/dist/exporters/jsonl-log-exporter.d.ts.map +1 -0
  11. package/dist/exporters/jsonl-log-exporter.js +103 -0
  12. package/dist/exporters/jsonl-log-exporter.js.map +1 -0
  13. package/dist/exporters/jsonl-metric-exporter.d.ts +40 -0
  14. package/dist/exporters/jsonl-metric-exporter.d.ts.map +1 -0
  15. package/dist/exporters/jsonl-metric-exporter.js +104 -0
  16. package/dist/exporters/jsonl-metric-exporter.js.map +1 -0
  17. package/dist/exporters/jsonl-trace-exporter.d.ts +36 -0
  18. package/dist/exporters/jsonl-trace-exporter.d.ts.map +1 -0
  19. package/dist/exporters/jsonl-trace-exporter.js +111 -0
  20. package/dist/exporters/jsonl-trace-exporter.js.map +1 -0
  21. package/dist/fetch.d.ts +12 -0
  22. package/dist/fetch.d.ts.map +1 -0
  23. package/dist/fetch.js +82 -0
  24. package/dist/fetch.js.map +1 -0
  25. package/dist/globals.d.ts +9 -0
  26. package/dist/globals.d.ts.map +1 -0
  27. package/dist/globals.js +13 -0
  28. package/dist/globals.js.map +1 -0
  29. package/dist/http.d.ts +16 -0
  30. package/dist/http.d.ts.map +1 -0
  31. package/dist/http.js +44 -0
  32. package/dist/http.js.map +1 -0
  33. package/dist/index.d.ts +50 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +62 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/logger/console.d.ts +69 -0
  38. package/dist/logger/console.d.ts.map +1 -0
  39. package/dist/logger/console.js +278 -0
  40. package/dist/logger/console.js.map +1 -0
  41. package/dist/logger/index.d.ts +4 -0
  42. package/dist/logger/index.d.ts.map +1 -0
  43. package/dist/logger/index.js +3 -0
  44. package/dist/logger/index.js.map +1 -0
  45. package/dist/logger/internal.d.ts +79 -0
  46. package/dist/logger/internal.d.ts.map +1 -0
  47. package/dist/logger/internal.js +133 -0
  48. package/dist/logger/internal.js.map +1 -0
  49. package/dist/logger/user.d.ts +8 -0
  50. package/dist/logger/user.d.ts.map +1 -0
  51. package/dist/logger/user.js +7 -0
  52. package/dist/logger/user.js.map +1 -0
  53. package/dist/logger/util.d.ts +11 -0
  54. package/dist/logger/util.d.ts.map +1 -0
  55. package/dist/logger/util.js +77 -0
  56. package/dist/logger/util.js.map +1 -0
  57. package/dist/logger.d.ts +40 -0
  58. package/dist/logger.d.ts.map +1 -0
  59. package/dist/logger.js +259 -0
  60. package/dist/logger.js.map +1 -0
  61. package/dist/telemetry.d.ts +71 -0
  62. package/dist/telemetry.d.ts.map +1 -0
  63. package/dist/telemetry.js +274 -0
  64. package/dist/telemetry.js.map +1 -0
  65. package/dist/tracestate.d.ts +44 -0
  66. package/dist/tracestate.d.ts.map +1 -0
  67. package/dist/tracestate.js +84 -0
  68. package/dist/tracestate.js.map +1 -0
  69. package/package.json +58 -0
  70. package/src/console.ts +91 -0
  71. package/src/exporters/README.md +217 -0
  72. package/src/exporters/index.ts +3 -0
  73. package/src/exporters/jsonl-log-exporter.ts +113 -0
  74. package/src/exporters/jsonl-metric-exporter.ts +120 -0
  75. package/src/exporters/jsonl-trace-exporter.ts +121 -0
  76. package/src/fetch.ts +105 -0
  77. package/src/globals.ts +18 -0
  78. package/src/http.ts +53 -0
  79. package/src/index.ts +82 -0
  80. package/src/logger/console.ts +322 -0
  81. package/src/logger/index.ts +3 -0
  82. package/src/logger/internal.ts +165 -0
  83. package/src/logger/user.ts +15 -0
  84. package/src/logger/util.ts +80 -0
  85. package/src/logger.ts +285 -0
  86. package/src/telemetry.ts +403 -0
  87. package/src/tracestate.ts +108 -0
package/dist/fetch.js ADDED
@@ -0,0 +1,82 @@
1
+ import { context, propagation, SpanStatusCode, trace } from '@opentelemetry/api';
2
+ /**
3
+ * Reference to the original fetch function before instrumentation
4
+ */
5
+ export const __originalFetch = fetch; // save the original fetch before we patch it
6
+ /**
7
+ * Instruments the global fetch function with OpenTelemetry tracing
8
+ *
9
+ * Replaces the global fetch with an instrumented version that creates spans
10
+ * for each HTTP request and propagates trace context in headers
11
+ */
12
+ export function instrumentFetch() {
13
+ const patch = async (input, init) => {
14
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
15
+ const method = init?.method ||
16
+ (typeof input !== 'string' && !(input instanceof URL) ? input.method || 'GET' : 'GET');
17
+ // Get the active span if it exists
18
+ const activeSpan = trace.getActiveSpan();
19
+ // If there's no active span, just call the original fetch
20
+ if (!activeSpan) {
21
+ return __originalFetch(input, init);
22
+ }
23
+ // Get the current active context
24
+ const currentContext = context.active();
25
+ const _url = new URL(url);
26
+ // Create a child span using the current context
27
+ const childSpan = trace.getTracer('fetch').startSpan(`${method} ${_url.pathname}`, {
28
+ attributes: {
29
+ 'http.url': url,
30
+ 'http.path': _url.pathname,
31
+ 'http.method': method,
32
+ host: _url.host,
33
+ },
34
+ }, currentContext);
35
+ try {
36
+ // Prepare trace context injection
37
+ const carrier = {};
38
+ // Create a new context with the child span
39
+ const newContext = trace.setSpan(currentContext, childSpan);
40
+ // Use the new context for propagation
41
+ propagation.inject(newContext, carrier);
42
+ // Preserve original headers and add trace context
43
+ // Handle headers from both Request input and init parameter
44
+ const baseHeaders = typeof input !== 'string' && !(input instanceof URL) && input instanceof Request
45
+ ? input.headers
46
+ : undefined;
47
+ const headers = new Headers(baseHeaders ?? init?.headers ?? {});
48
+ // Add trace context headers (overwriting any already present)
49
+ for (const [key, value] of Object.entries(carrier)) {
50
+ headers.set(key, value);
51
+ }
52
+ // Create new init object with updated headers
53
+ const newInit = {
54
+ ...init,
55
+ headers,
56
+ };
57
+ const response = await __originalFetch(input, newInit);
58
+ // Add response attributes to span
59
+ childSpan.setAttributes({
60
+ 'http.status_code': response.status,
61
+ 'http.user_agent': response.headers.get('user-agent') || '',
62
+ });
63
+ if (!response.ok) {
64
+ childSpan.setStatus({ code: SpanStatusCode.ERROR });
65
+ }
66
+ else {
67
+ childSpan.setStatus({ code: SpanStatusCode.OK });
68
+ }
69
+ return response;
70
+ }
71
+ catch (error) {
72
+ childSpan.recordException(error);
73
+ childSpan.setStatus({ code: SpanStatusCode.ERROR });
74
+ throw error;
75
+ }
76
+ finally {
77
+ childSpan.end();
78
+ }
79
+ };
80
+ globalThis.fetch = patch;
81
+ }
82
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Global state for Telemetry instance (survives hot reloads)
3
+ */
4
+ import type { TelemetryResponse } from './telemetry';
5
+ export declare const telemetry: {
6
+ get: () => TelemetryResponse | undefined;
7
+ set: (v: TelemetryResponse) => void;
8
+ };
9
+ //# sourceMappingURL=globals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globals.d.ts","sourceRoot":"","sources":["../src/globals.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAOrD,eAAO,MAAM,SAAS;eACZ,iBAAiB,GAAG,SAAS;aAE7B,iBAAiB,KAAG,IAAI;CAGjC,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Global state for Telemetry instance (survives hot reloads)
3
+ */
4
+ const telemetryInstanceKey = Symbol.for('@agentuity/telemetry:instance');
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ const g = globalThis;
7
+ export const telemetry = {
8
+ get: () => g[telemetryInstanceKey],
9
+ set: (v) => {
10
+ g[telemetryInstanceKey] = v;
11
+ },
12
+ };
13
+ //# sourceMappingURL=globals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globals.js","sourceRoot":"","sources":["../src/globals.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;AAEzE,8DAA8D;AAC9D,MAAM,CAAC,GAAG,UAAiB,CAAC;AAE5B,MAAM,CAAC,MAAM,SAAS,GAAG;IACxB,GAAG,EAAE,GAAkC,EAAE,CACxC,CAAC,CAAC,oBAAoB,CAAkC;IACzD,GAAG,EAAE,CAAC,CAAoB,EAAQ,EAAE;QACnC,CAAC,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;CACD,CAAC"}
package/dist/http.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { propagation } from '@opentelemetry/api';
2
+ /**
3
+ * Injects trace context into response headers using the OpenTelemetry propagation API
4
+ *
5
+ * @param headers - Optional existing headers to include
6
+ * @returns A record of headers with trace context injected
7
+ */
8
+ export declare function injectTraceContextToHeaders(headers?: Record<string, string> | Headers): Record<string, string>;
9
+ /**
10
+ * Extracts trace context from Bun Request headers
11
+ *
12
+ * @param req - The Bun Request object
13
+ * @returns The context with trace information
14
+ */
15
+ export declare function extractTraceContextFromRequest(req: Request): ReturnType<typeof propagation.extract>;
16
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAC1C,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAY,GAC5C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAoBxB;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAC7C,GAAG,EAAE,OAAO,GACV,UAAU,CAAC,OAAO,WAAW,CAAC,OAAO,CAAC,CAYxC"}
package/dist/http.js ADDED
@@ -0,0 +1,44 @@
1
+ import { context, propagation } from '@opentelemetry/api';
2
+ /**
3
+ * Injects trace context into response headers using the OpenTelemetry propagation API
4
+ *
5
+ * @param headers - Optional existing headers to include
6
+ * @returns A record of headers with trace context injected
7
+ */
8
+ export function injectTraceContextToHeaders(headers = {}) {
9
+ let _headers;
10
+ if (headers instanceof Headers) {
11
+ _headers = {};
12
+ headers.forEach((v, k) => {
13
+ _headers[k] = v;
14
+ });
15
+ }
16
+ else {
17
+ _headers = { ...headers };
18
+ }
19
+ // Create a carrier object for the headers
20
+ const carrier = { ..._headers };
21
+ // Get the current context
22
+ const currentContext = context.active();
23
+ // Inject trace context into the carrier
24
+ propagation.inject(currentContext, carrier);
25
+ return carrier;
26
+ }
27
+ /**
28
+ * Extracts trace context from Bun Request headers
29
+ *
30
+ * @param req - The Bun Request object
31
+ * @returns The context with trace information
32
+ */
33
+ export function extractTraceContextFromRequest(req) {
34
+ // Create a carrier object from the headers
35
+ const carrier = {};
36
+ // Convert headers to the format expected by the propagator
37
+ req.headers.forEach((value, key) => {
38
+ carrier[key.toLowerCase()] = value;
39
+ });
40
+ // Extract the context using the global propagator
41
+ const activeContext = context.active();
42
+ return propagation.extract(activeContext, carrier);
43
+ }
44
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CAC1C,UAA4C,EAAE;IAE9C,IAAI,QAAgC,CAAC;IACrC,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;QAChC,QAAQ,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YACxB,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC,CAAC,CAAC;IACJ,CAAC;SAAM,CAAC;QACP,QAAQ,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC;IAC3B,CAAC;IACD,0CAA0C;IAC1C,MAAM,OAAO,GAA2B,EAAE,GAAG,QAAQ,EAA4B,CAAC;IAElF,0BAA0B;IAC1B,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAExC,wCAAwC;IACxC,WAAW,CAAC,MAAM,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAE5C,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B,CAC7C,GAAY;IAEZ,2CAA2C;IAC3C,MAAM,OAAO,GAA2B,EAAE,CAAC;IAE3C,2DAA2D;IAC3D,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAClC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,GAAG,KAAK,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,kDAAkD;IAClD,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACvC,OAAO,WAAW,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @agentuity/telemetry - OpenTelemetry telemetry for Agentuity
3
+ *
4
+ * Auto-initializes from environment variables on import (Vercel-style).
5
+ *
6
+ * @example Automatic initialization (recommended)
7
+ * ```typescript
8
+ * // Just import - auto-configures from AGENTUITY_* env vars
9
+ * import '@agentuity/telemetry';
10
+ *
11
+ * // Then access the globals anywhere
12
+ * import { tracer, logger, meter } from '@agentuity/telemetry';
13
+ * ```
14
+ *
15
+ * @example Explicit configuration
16
+ * ```typescript
17
+ * import { register } from '@agentuity/telemetry';
18
+ *
19
+ * register({
20
+ * name: 'my-app',
21
+ * version: '1.0.0',
22
+ * // ... optional overrides
23
+ * });
24
+ * ```
25
+ */
26
+ export type { Logger } from './logger';
27
+ export { __originalConsole } from './logger';
28
+ export type { TelemetryConfig, TelemetryResponse } from './telemetry';
29
+ export { injectTraceContextToHeaders, extractTraceContextFromRequest } from './http';
30
+ export { enrichContextWithTraceState, generateTraceId, generateSpanId, type TraceStateEntries, } from './tracestate';
31
+ export { register, registerTelemetry, getTelemetry, ensureInitialized } from './telemetry';
32
+ import type { Tracer, Meter } from '@opentelemetry/api';
33
+ import type { Logger } from './logger';
34
+ /**
35
+ * Get the OpenTelemetry tracer (auto-initialized)
36
+ */
37
+ export declare const tracer: Tracer;
38
+ /**
39
+ * Get the OpenTelemetry meter (auto-initialized)
40
+ */
41
+ export declare const meter: Meter;
42
+ /**
43
+ * Get the Logger instance (auto-initialized)
44
+ */
45
+ export declare const logger: Logger;
46
+ /**
47
+ * Shutdown telemetry (call on process exit)
48
+ */
49
+ export declare function shutdown(): Promise<void>;
50
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,YAAY,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC7C,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGtE,OAAO,EAAE,2BAA2B,EAAE,8BAA8B,EAAE,MAAM,QAAQ,CAAC;AAGrF,OAAO,EACN,2BAA2B,EAC3B,eAAe,EACf,cAAc,EACd,KAAK,iBAAiB,GACtB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG3F,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGvC;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,MAEnB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,KAAK,EAAE,KAElB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,MAEnB,CAAC;AAEH;;GAEG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAK9C"}
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @agentuity/telemetry - OpenTelemetry telemetry for Agentuity
3
+ *
4
+ * Auto-initializes from environment variables on import (Vercel-style).
5
+ *
6
+ * @example Automatic initialization (recommended)
7
+ * ```typescript
8
+ * // Just import - auto-configures from AGENTUITY_* env vars
9
+ * import '@agentuity/telemetry';
10
+ *
11
+ * // Then access the globals anywhere
12
+ * import { tracer, logger, meter } from '@agentuity/telemetry';
13
+ * ```
14
+ *
15
+ * @example Explicit configuration
16
+ * ```typescript
17
+ * import { register } from '@agentuity/telemetry';
18
+ *
19
+ * register({
20
+ * name: 'my-app',
21
+ * version: '1.0.0',
22
+ * // ... optional overrides
23
+ * });
24
+ * ```
25
+ */
26
+ // Re-export console reference for custom loggers
27
+ export { __originalConsole } from './logger';
28
+ // Re-export HTTP utilities for trace context propagation
29
+ export { injectTraceContextToHeaders, extractTraceContextFromRequest } from './http';
30
+ // Re-export trace state utilities
31
+ export { enrichContextWithTraceState, generateTraceId, generateSpanId, } from './tracestate';
32
+ // Core registration function
33
+ export { register, registerTelemetry, getTelemetry, ensureInitialized } from './telemetry';
34
+ import { ensureInitialized } from './telemetry';
35
+ /**
36
+ * Get the OpenTelemetry tracer (auto-initialized)
37
+ */
38
+ export const tracer = new Proxy({}, {
39
+ get: (_, prop) => ensureInitialized().tracer[prop],
40
+ });
41
+ /**
42
+ * Get the OpenTelemetry meter (auto-initialized)
43
+ */
44
+ export const meter = new Proxy({}, {
45
+ get: (_, prop) => ensureInitialized().meter[prop],
46
+ });
47
+ /**
48
+ * Get the Logger instance (auto-initialized)
49
+ */
50
+ export const logger = new Proxy({}, {
51
+ get: (_, prop) => ensureInitialized().logger[prop],
52
+ });
53
+ /**
54
+ * Shutdown telemetry (call on process exit)
55
+ */
56
+ export async function shutdown() {
57
+ const telemetry = ensureInitialized();
58
+ if (telemetry?.shutdown) {
59
+ await telemetry.shutdown();
60
+ }
61
+ }
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAKH,iDAAiD;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG7C,yDAAyD;AACzD,OAAO,EAAE,2BAA2B,EAAE,8BAA8B,EAAE,MAAM,QAAQ,CAAC;AAErF,kCAAkC;AAClC,OAAO,EACN,2BAA2B,EAC3B,eAAe,EACf,cAAc,GAEd,MAAM,cAAc,CAAC;AAEtB,6BAA6B;AAC7B,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAK3F,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAAW,IAAI,KAAK,CAAC,EAAY,EAAE;IACrD,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,EAAE,CAAC,MAAM,CAAC,IAAoB,CAAC;CAClE,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAU,IAAI,KAAK,CAAC,EAAW,EAAE;IAClD,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,EAAE,CAAC,KAAK,CAAC,IAAmB,CAAC;CAChE,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAAW,IAAI,KAAK,CAAC,EAAY,EAAE;IACrD,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,EAAE,CAAC,MAAM,CAAC,IAAoB,CAAC;CAClE,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ;IAC7B,MAAM,SAAS,GAAG,iBAAiB,EAAE,CAAC;IACtC,IAAI,SAAS,EAAE,QAAQ,EAAE,CAAC;QACzB,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC5B,CAAC;AACF,CAAC"}
@@ -0,0 +1,69 @@
1
+ import type { LogLevel, Logger } from '@agentuity/core';
2
+ /**
3
+ * Console implementation of the Logger interface
4
+ */
5
+ export default class ConsoleLogger implements Logger {
6
+ private context;
7
+ private formatContext;
8
+ private logLevel;
9
+ private colors;
10
+ private detectedTraceLoopLog;
11
+ private useColors;
12
+ /**
13
+ * Creates a new console logger
14
+ *
15
+ * @param context - Initial context for the logger
16
+ */
17
+ constructor(context?: Record<string, unknown>, formatContext?: boolean, logLevel?: LogLevel);
18
+ private shouldLog;
19
+ /**
20
+ * Log a trace message (most verbose)
21
+ *
22
+ * @param message - The message to log
23
+ * @param args - Additional arguments to log
24
+ */
25
+ trace(message: unknown, ...args: unknown[]): void;
26
+ /**
27
+ * Log a debug message
28
+ *
29
+ * @param message - The message to log
30
+ * @param args - Additional arguments to log
31
+ */
32
+ debug(message: unknown, ...args: unknown[]): void;
33
+ /**
34
+ * Log an info message
35
+ *
36
+ * @param message - The message to log
37
+ * @param args - Additional arguments to log
38
+ */
39
+ info(message: unknown, ...args: unknown[]): void;
40
+ /**
41
+ * Log a warning message
42
+ *
43
+ * @param message - The message to log
44
+ * @param args - Additional arguments to log
45
+ */
46
+ warn(message: unknown, ...args: unknown[]): void;
47
+ /**
48
+ * Log an error message
49
+ *
50
+ * @param message - The message to log
51
+ * @param args - Additional arguments to log
52
+ */
53
+ error(message: unknown, ...args: unknown[]): void;
54
+ /**
55
+ * Log a fatal error message and exit the process
56
+ *
57
+ * @param message - The message to log
58
+ * @param args - Additional arguments to log
59
+ */
60
+ fatal(message: unknown, ...args: unknown[]): never;
61
+ /**
62
+ * Create a child logger with additional context
63
+ *
64
+ * @param opts - Additional context for the child logger
65
+ * @returns A new logger instance with the additional context
66
+ */
67
+ child(opts: Record<string, unknown>): Logger;
68
+ }
69
+ //# sourceMappingURL=console.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../../src/logger/console.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAoHxD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,aAAc,YAAW,MAAM;IACnD,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,oBAAoB,CAAsB;IAClD,OAAO,CAAC,SAAS,CAAU;IAE3B;;;;OAIG;gBAEF,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,EACrC,aAAa,UAAO,EACpB,QAAQ,GAAE,QAAiB;IAW5B,OAAO,CAAC,SAAS;IAgBjB;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAkBjD;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAkBjD;;;;;OAKG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IA8BhD;;;;;OAKG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAkBhD;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;IAkBjD;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,KAAK;IAKlD;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM;CAU5C"}
@@ -0,0 +1,278 @@
1
+ import { __originalConsole } from '../logger';
2
+ import { formatMessage } from './util';
3
+ const BOLD = '\x1b[1m';
4
+ const RESET = '\x1b[0m';
5
+ // Helper to convert hex color to ANSI 24-bit color code
6
+ function hexToAnsi(hex) {
7
+ const r = parseInt(hex.slice(1, 3), 16);
8
+ const g = parseInt(hex.slice(3, 5), 16);
9
+ const b = parseInt(hex.slice(5, 7), 16);
10
+ return `\x1b[38;2;${r};${g};${b}m`;
11
+ }
12
+ function shouldUseColors() {
13
+ // FORCE_COLOR overrides all checks (used when stdout is piped but we still want colors)
14
+ if (process.env.FORCE_COLOR === '1') {
15
+ return true;
16
+ }
17
+ // Check for NO_COLOR environment variable (any non-empty value disables colors)
18
+ if (process.env.NO_COLOR) {
19
+ return false;
20
+ }
21
+ // Check for TERM=dumb
22
+ if (process.env.TERM === 'dumb') {
23
+ return false;
24
+ }
25
+ // Check if stdout is a TTY
26
+ if (!process.stdout || typeof process.stdout.isTTY === 'undefined') {
27
+ return false;
28
+ }
29
+ if (process.stdout && typeof process.stdout.isTTY !== 'undefined' && !process.stdout.isTTY) {
30
+ return false;
31
+ }
32
+ return true;
33
+ }
34
+ function getLogColors(scheme) {
35
+ if (scheme === 'light') {
36
+ // Darker, high-contrast colors for light backgrounds
37
+ return {
38
+ trace: {
39
+ level: hexToAnsi('#008B8B') + BOLD, // Dark cyan
40
+ message: hexToAnsi('#4B4B4B'), // Dark gray
41
+ },
42
+ debug: {
43
+ level: hexToAnsi('#0000CD') + BOLD, // Medium blue
44
+ message: hexToAnsi('#006400'), // Dark green
45
+ },
46
+ info: {
47
+ level: hexToAnsi('#FF8C00') + BOLD, // Dark orange
48
+ message: hexToAnsi('#0066CC') + BOLD, // Strong blue
49
+ },
50
+ warn: {
51
+ level: hexToAnsi('#9400D3') + BOLD, // Dark violet
52
+ message: hexToAnsi('#8B008B'), // Dark magenta
53
+ },
54
+ error: {
55
+ level: hexToAnsi('#DC143C') + BOLD, // Crimson
56
+ message: hexToAnsi('#8B0000') + BOLD, // Dark red
57
+ },
58
+ };
59
+ }
60
+ // Dark mode colors (brighter for dark backgrounds)
61
+ return {
62
+ trace: {
63
+ level: hexToAnsi('#00FFFF') + BOLD, // Cyan
64
+ message: hexToAnsi('#A0A0A0'), // Light gray
65
+ },
66
+ debug: {
67
+ level: hexToAnsi('#5C9CFF') + BOLD, // Blue
68
+ message: hexToAnsi('#90EE90'), // Light green
69
+ },
70
+ info: {
71
+ level: hexToAnsi('#FFD700') + BOLD, // Gold/Yellow
72
+ message: hexToAnsi('#FFFFFF') + BOLD, // White
73
+ },
74
+ warn: {
75
+ level: hexToAnsi('#FF00FF') + BOLD, // Magenta
76
+ message: hexToAnsi('#FF00FF'), // Magenta
77
+ },
78
+ error: {
79
+ level: hexToAnsi('#FF4444') + BOLD, // Red
80
+ message: hexToAnsi('#FF4444'), // Red
81
+ },
82
+ };
83
+ }
84
+ // Detect color scheme from environment
85
+ function detectColorScheme() {
86
+ const scheme = process.env.COLOR_SCHEME?.toLowerCase();
87
+ if (scheme === 'light' || scheme === 'dark') {
88
+ return scheme;
89
+ }
90
+ if (process.env.CI) {
91
+ return 'light';
92
+ }
93
+ return 'dark'; // Default to dark mode
94
+ }
95
+ const NOCOLORS = Object.freeze({ level: '', reset: '', message: '' });
96
+ /**
97
+ * Console implementation of the Logger interface
98
+ */
99
+ export default class ConsoleLogger {
100
+ context;
101
+ formatContext;
102
+ logLevel;
103
+ colors;
104
+ detectedTraceLoopLog;
105
+ useColors;
106
+ /**
107
+ * Creates a new console logger
108
+ *
109
+ * @param context - Initial context for the logger
110
+ */
111
+ constructor(context = {}, formatContext = true, logLevel = 'info') {
112
+ this.context = context;
113
+ this.formatContext = formatContext;
114
+ this.logLevel = logLevel;
115
+ this.useColors = shouldUseColors();
116
+ this.colors = this.useColors
117
+ ? getLogColors(detectColorScheme())
118
+ : {};
119
+ }
120
+ shouldLog(level) {
121
+ switch (this.logLevel) {
122
+ case 'trace':
123
+ return true;
124
+ case 'debug':
125
+ return level === 'debug' || level === 'info' || level === 'warn' || level === 'error';
126
+ case 'info':
127
+ return level === 'info' || level === 'warn' || level === 'error';
128
+ case 'warn':
129
+ return level === 'warn' || level === 'error';
130
+ case 'error':
131
+ return level === 'error';
132
+ }
133
+ return false;
134
+ }
135
+ /**
136
+ * Log a trace message (most verbose)
137
+ *
138
+ * @param message - The message to log
139
+ * @param args - Additional arguments to log
140
+ */
141
+ trace(message, ...args) {
142
+ if (!this.shouldLog('trace')) {
143
+ return;
144
+ }
145
+ try {
146
+ const colors = this.useColors ? this.colors.trace : NOCOLORS;
147
+ const formattedMessage = formatMessage(this.formatContext, this.context, message, args);
148
+ __originalConsole.debug(`${colors.level}[TRACE]${RESET} ${colors.message}${formattedMessage}${RESET}`);
149
+ }
150
+ catch (err) {
151
+ // Fallback to direct logging if formatting fails
152
+ const colors = this.colors.trace;
153
+ __originalConsole.debug(`${colors.level}[TRACE]${RESET} ${message}`, ...args);
154
+ __originalConsole.error('Error formatting log message:', err);
155
+ }
156
+ }
157
+ /**
158
+ * Log a debug message
159
+ *
160
+ * @param message - The message to log
161
+ * @param args - Additional arguments to log
162
+ */
163
+ debug(message, ...args) {
164
+ if (!this.shouldLog('debug')) {
165
+ return;
166
+ }
167
+ try {
168
+ const colors = this.useColors ? this.colors.debug : NOCOLORS;
169
+ const formattedMessage = formatMessage(this.formatContext, this.context, message, args);
170
+ __originalConsole.debug(`${colors.level}[DEBUG]${RESET} ${colors.message}${formattedMessage}${RESET}`);
171
+ }
172
+ catch (err) {
173
+ // Fallback to direct logging if formatting fails
174
+ const colors = this.colors.debug;
175
+ __originalConsole.debug(`${colors.level}[DEBUG]${RESET} ${message}`, ...args);
176
+ __originalConsole.error('Error formatting log message:', err);
177
+ }
178
+ }
179
+ /**
180
+ * Log an info message
181
+ *
182
+ * @param message - The message to log
183
+ * @param args - Additional arguments to log
184
+ */
185
+ info(message, ...args) {
186
+ if (!this.shouldLog('info')) {
187
+ return;
188
+ }
189
+ // suppress the default traceloop message at info level
190
+ if (!this.detectedTraceLoopLog &&
191
+ typeof message === 'string' &&
192
+ message.includes('Traceloop exporting traces to')) {
193
+ this.detectedTraceLoopLog = true;
194
+ if (this.shouldLog('debug')) {
195
+ this.debug(message, ...args);
196
+ }
197
+ return;
198
+ }
199
+ try {
200
+ const colors = this.useColors ? this.colors.info : NOCOLORS;
201
+ const formattedMessage = formatMessage(this.formatContext, this.context, message, args);
202
+ __originalConsole.info(`${colors.level}[INFO]${RESET} ${colors.message}${formattedMessage}${RESET}`);
203
+ }
204
+ catch (err) {
205
+ // Fallback to direct logging if formatting fails
206
+ const colors = this.colors.info;
207
+ __originalConsole.info(`${colors.level}[INFO]${RESET} ${message}`, ...args);
208
+ __originalConsole.error('Error formatting log message:', err);
209
+ }
210
+ }
211
+ /**
212
+ * Log a warning message
213
+ *
214
+ * @param message - The message to log
215
+ * @param args - Additional arguments to log
216
+ */
217
+ warn(message, ...args) {
218
+ if (!this.shouldLog('warn')) {
219
+ return;
220
+ }
221
+ try {
222
+ const colors = this.useColors ? this.colors.warn : NOCOLORS;
223
+ const formattedMessage = formatMessage(this.formatContext, this.context, message, args);
224
+ __originalConsole.warn(`${colors.level}[WARN]${RESET} ${colors.message}${formattedMessage}${RESET}`);
225
+ }
226
+ catch (err) {
227
+ // Fallback to direct logging if formatting fails
228
+ const colors = this.colors.warn;
229
+ __originalConsole.warn(`${colors.level}[WARN]${RESET} ${message}`, ...args);
230
+ __originalConsole.error('Error formatting log message:', err);
231
+ }
232
+ }
233
+ /**
234
+ * Log an error message
235
+ *
236
+ * @param message - The message to log
237
+ * @param args - Additional arguments to log
238
+ */
239
+ error(message, ...args) {
240
+ if (!this.shouldLog('error')) {
241
+ return;
242
+ }
243
+ try {
244
+ const colors = this.useColors ? this.colors.error : NOCOLORS;
245
+ const formattedMessage = formatMessage(this.formatContext, this.context, message, args);
246
+ __originalConsole.error(`${colors.level}[ERROR]${RESET} ${colors.message}${formattedMessage}${RESET}`);
247
+ }
248
+ catch (err) {
249
+ // Fallback to direct logging if formatting fails
250
+ const colors = this.colors.error;
251
+ __originalConsole.error(`${colors.level}[ERROR]${RESET} ${message}`, ...args);
252
+ __originalConsole.error('Error formatting log message:', err);
253
+ }
254
+ }
255
+ /**
256
+ * Log a fatal error message and exit the process
257
+ *
258
+ * @param message - The message to log
259
+ * @param args - Additional arguments to log
260
+ */
261
+ fatal(message, ...args) {
262
+ this.error(message, ...args);
263
+ process.exit(1);
264
+ }
265
+ /**
266
+ * Create a child logger with additional context
267
+ *
268
+ * @param opts - Additional context for the child logger
269
+ * @returns A new logger instance with the additional context
270
+ */
271
+ child(opts) {
272
+ return new ConsoleLogger({
273
+ ...this.context,
274
+ ...opts,
275
+ }, this.formatContext, this.logLevel);
276
+ }
277
+ }
278
+ //# sourceMappingURL=console.js.map