libretto 0.3.0 → 0.3.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.
Files changed (162) hide show
  1. package/README.md +41 -125
  2. package/dist/cli/cli.js +298 -0
  3. package/dist/cli/commands/ai.js +21 -0
  4. package/dist/cli/commands/browser.js +82 -0
  5. package/dist/cli/commands/execution.js +490 -0
  6. package/dist/cli/commands/init.js +166 -0
  7. package/dist/cli/commands/logs.js +93 -0
  8. package/dist/cli/commands/snapshot.js +106 -0
  9. package/dist/cli/core/ai-config.js +149 -0
  10. package/dist/cli/core/browser.js +652 -0
  11. package/dist/cli/core/context.js +117 -0
  12. package/dist/cli/core/pause-signals.js +29 -0
  13. package/dist/cli/core/session-telemetry.js +491 -0
  14. package/dist/cli/core/session.js +183 -0
  15. package/dist/cli/core/snapshot-analyzer.js +492 -0
  16. package/dist/cli/core/telemetry.js +362 -0
  17. package/dist/cli/index.js +13 -0
  18. package/dist/cli/workers/run-integration-runtime.js +234 -0
  19. package/dist/cli/workers/run-integration-worker-protocol.js +12 -0
  20. package/dist/cli/workers/run-integration-worker.js +67 -0
  21. package/dist/index.cjs +144 -0
  22. package/dist/index.d.cts +21 -0
  23. package/dist/index.d.ts +21 -0
  24. package/dist/index.js +114 -0
  25. package/dist/runtime/download/download.cjs +70 -0
  26. package/dist/runtime/download/download.d.cts +35 -0
  27. package/dist/runtime/download/download.d.ts +35 -0
  28. package/dist/runtime/download/download.js +45 -0
  29. package/dist/runtime/download/index.cjs +30 -0
  30. package/dist/runtime/download/index.d.cts +3 -0
  31. package/dist/runtime/download/index.d.ts +3 -0
  32. package/dist/runtime/download/index.js +8 -0
  33. package/dist/runtime/extract/extract.cjs +88 -0
  34. package/dist/runtime/extract/extract.d.cts +23 -0
  35. package/dist/runtime/extract/extract.d.ts +23 -0
  36. package/dist/runtime/extract/extract.js +64 -0
  37. package/dist/runtime/extract/index.cjs +28 -0
  38. package/dist/runtime/extract/index.d.cts +5 -0
  39. package/dist/runtime/extract/index.d.ts +5 -0
  40. package/dist/runtime/extract/index.js +4 -0
  41. package/dist/runtime/network/index.cjs +28 -0
  42. package/dist/runtime/network/index.d.cts +4 -0
  43. package/dist/runtime/network/index.d.ts +4 -0
  44. package/dist/runtime/network/index.js +6 -0
  45. package/dist/runtime/network/network.cjs +91 -0
  46. package/dist/runtime/network/network.d.cts +28 -0
  47. package/dist/runtime/network/network.d.ts +28 -0
  48. package/dist/runtime/network/network.js +67 -0
  49. package/dist/runtime/recovery/agent.cjs +223 -0
  50. package/dist/runtime/recovery/agent.d.cts +13 -0
  51. package/dist/runtime/recovery/agent.d.ts +13 -0
  52. package/dist/runtime/recovery/agent.js +199 -0
  53. package/dist/runtime/recovery/errors.cjs +124 -0
  54. package/dist/runtime/recovery/errors.d.cts +31 -0
  55. package/dist/runtime/recovery/errors.d.ts +31 -0
  56. package/dist/runtime/recovery/errors.js +100 -0
  57. package/dist/runtime/recovery/index.cjs +34 -0
  58. package/dist/runtime/recovery/index.d.cts +7 -0
  59. package/dist/runtime/recovery/index.d.ts +7 -0
  60. package/dist/runtime/recovery/index.js +10 -0
  61. package/dist/runtime/recovery/recovery.cjs +55 -0
  62. package/dist/runtime/recovery/recovery.d.cts +12 -0
  63. package/dist/runtime/recovery/recovery.d.ts +12 -0
  64. package/dist/runtime/recovery/recovery.js +31 -0
  65. package/dist/shared/config/config.cjs +44 -0
  66. package/dist/shared/config/config.d.cts +10 -0
  67. package/dist/shared/config/config.d.ts +10 -0
  68. package/dist/shared/config/config.js +18 -0
  69. package/dist/shared/config/index.cjs +32 -0
  70. package/dist/shared/config/index.d.cts +1 -0
  71. package/dist/shared/config/index.d.ts +1 -0
  72. package/dist/shared/config/index.js +10 -0
  73. package/dist/shared/debug/index.cjs +30 -0
  74. package/dist/shared/debug/index.d.cts +1 -0
  75. package/dist/shared/debug/index.d.ts +1 -0
  76. package/dist/shared/debug/index.js +5 -0
  77. package/dist/shared/debug/pause.cjs +90 -0
  78. package/dist/shared/debug/pause.d.cts +16 -0
  79. package/dist/shared/debug/pause.d.ts +16 -0
  80. package/dist/shared/debug/pause.js +55 -0
  81. package/dist/shared/instrumentation/errors.cjs +81 -0
  82. package/dist/shared/instrumentation/errors.d.cts +12 -0
  83. package/dist/shared/instrumentation/errors.d.ts +12 -0
  84. package/dist/shared/instrumentation/errors.js +57 -0
  85. package/dist/shared/instrumentation/index.cjs +35 -0
  86. package/dist/shared/instrumentation/index.d.cts +6 -0
  87. package/dist/shared/instrumentation/index.d.ts +6 -0
  88. package/dist/shared/instrumentation/index.js +12 -0
  89. package/dist/shared/instrumentation/instrument.cjs +206 -0
  90. package/dist/shared/instrumentation/instrument.d.cts +32 -0
  91. package/dist/shared/instrumentation/instrument.d.ts +32 -0
  92. package/dist/shared/instrumentation/instrument.js +190 -0
  93. package/dist/shared/llm/ai-sdk-adapter.cjs +67 -0
  94. package/dist/shared/llm/ai-sdk-adapter.d.cts +22 -0
  95. package/dist/shared/llm/ai-sdk-adapter.d.ts +22 -0
  96. package/dist/shared/llm/ai-sdk-adapter.js +43 -0
  97. package/dist/shared/llm/client.cjs +139 -0
  98. package/dist/shared/llm/client.d.cts +6 -0
  99. package/dist/shared/llm/client.d.ts +6 -0
  100. package/dist/shared/llm/client.js +115 -0
  101. package/dist/shared/llm/index.cjs +31 -0
  102. package/dist/shared/llm/index.d.cts +5 -0
  103. package/dist/shared/llm/index.d.ts +5 -0
  104. package/dist/shared/llm/index.js +6 -0
  105. package/dist/shared/llm/types.cjs +16 -0
  106. package/dist/shared/llm/types.d.cts +66 -0
  107. package/dist/shared/llm/types.d.ts +66 -0
  108. package/dist/shared/llm/types.js +0 -0
  109. package/dist/shared/logger/index.cjs +37 -0
  110. package/dist/shared/logger/index.d.cts +2 -0
  111. package/dist/shared/logger/index.d.ts +2 -0
  112. package/dist/shared/logger/index.js +13 -0
  113. package/dist/shared/logger/logger.cjs +232 -0
  114. package/dist/shared/logger/logger.d.cts +86 -0
  115. package/dist/shared/logger/logger.d.ts +86 -0
  116. package/dist/shared/logger/logger.js +207 -0
  117. package/dist/shared/logger/sinks.cjs +160 -0
  118. package/dist/shared/logger/sinks.d.cts +9 -0
  119. package/dist/shared/logger/sinks.d.ts +9 -0
  120. package/dist/shared/logger/sinks.js +124 -0
  121. package/dist/shared/paths/paths.cjs +104 -0
  122. package/dist/shared/paths/paths.d.cts +10 -0
  123. package/dist/shared/paths/paths.d.ts +10 -0
  124. package/dist/shared/paths/paths.js +73 -0
  125. package/dist/shared/run/api.cjs +28 -0
  126. package/dist/shared/run/api.d.cts +2 -0
  127. package/dist/shared/run/api.d.ts +2 -0
  128. package/dist/shared/run/api.js +4 -0
  129. package/dist/shared/run/browser.cjs +98 -0
  130. package/dist/shared/run/browser.d.cts +22 -0
  131. package/dist/shared/run/browser.d.ts +22 -0
  132. package/dist/shared/run/browser.js +74 -0
  133. package/dist/shared/state/index.cjs +38 -0
  134. package/dist/shared/state/index.d.cts +2 -0
  135. package/dist/shared/state/index.d.ts +2 -0
  136. package/dist/shared/state/index.js +16 -0
  137. package/dist/shared/state/session-state.cjs +85 -0
  138. package/dist/shared/state/session-state.d.cts +34 -0
  139. package/dist/shared/state/session-state.d.ts +34 -0
  140. package/dist/shared/state/session-state.js +56 -0
  141. package/dist/shared/visualization/ghost-cursor.cjs +174 -0
  142. package/dist/shared/visualization/ghost-cursor.d.cts +37 -0
  143. package/dist/shared/visualization/ghost-cursor.d.ts +37 -0
  144. package/dist/shared/visualization/ghost-cursor.js +145 -0
  145. package/dist/shared/visualization/highlight.cjs +134 -0
  146. package/dist/shared/visualization/highlight.d.cts +22 -0
  147. package/dist/shared/visualization/highlight.d.ts +22 -0
  148. package/dist/shared/visualization/highlight.js +108 -0
  149. package/dist/shared/visualization/index.cjs +45 -0
  150. package/dist/shared/visualization/index.d.cts +3 -0
  151. package/dist/shared/visualization/index.d.ts +3 -0
  152. package/dist/shared/visualization/index.js +24 -0
  153. package/dist/shared/workflow/workflow.cjs +47 -0
  154. package/dist/shared/workflow/workflow.d.cts +21 -0
  155. package/dist/shared/workflow/workflow.d.ts +21 -0
  156. package/dist/shared/workflow/workflow.js +21 -0
  157. package/package.json +11 -70
  158. package/bin/libretto.mjs +0 -18
  159. package/scripts/postinstall.mjs +0 -48
  160. /package/{skill → .agents/skills/libretto}/SKILL.md +0 -0
  161. /package/{skill → .agents/skills/libretto}/code-generation-rules.md +0 -0
  162. /package/{skill → .agents/skills/libretto}/integration-approach-selection.md +0 -0
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var logger_exports = {};
20
+ __export(logger_exports, {
21
+ Logger: () => Logger,
22
+ defaultLogger: () => defaultLogger
23
+ });
24
+ module.exports = __toCommonJS(logger_exports);
25
+ function generateId() {
26
+ return Math.random().toString(36).substring(2, 15);
27
+ }
28
+ const defaultLogger = {
29
+ info(event, data) {
30
+ console.log(`[INFO] ${event}`, data ?? "");
31
+ },
32
+ warn(event, data) {
33
+ console.warn(`[WARN] ${event}`, data ?? "");
34
+ },
35
+ error(event, data) {
36
+ console.error(`[ERROR] ${event}`, data ?? "");
37
+ }
38
+ };
39
+ const sinkLifecycleState = /* @__PURE__ */ new WeakMap();
40
+ function getSinkLifecycleState(sink) {
41
+ const existingState = sinkLifecycleState.get(sink);
42
+ if (existingState) {
43
+ return existingState;
44
+ }
45
+ const initialState = { closed: false };
46
+ sinkLifecycleState.set(sink, initialState);
47
+ return initialState;
48
+ }
49
+ function isSinkClosedOrClosing(sink) {
50
+ const state = sinkLifecycleState.get(sink);
51
+ return Boolean(state?.closed || state?.closing);
52
+ }
53
+ async function closeSinkOnce(sink) {
54
+ if (!sink.close) {
55
+ return;
56
+ }
57
+ const state = getSinkLifecycleState(sink);
58
+ if (state.closed) {
59
+ return;
60
+ }
61
+ if (state.closing) {
62
+ return state.closing;
63
+ }
64
+ state.closing = (async () => {
65
+ try {
66
+ await sink.close?.();
67
+ } catch {
68
+ } finally {
69
+ state.closed = true;
70
+ state.closing = void 0;
71
+ }
72
+ })();
73
+ return state.closing;
74
+ }
75
+ function isObject(value) {
76
+ return typeof value === "object" && value !== null;
77
+ }
78
+ function removeUndefined(data) {
79
+ if (typeof data === "object" && data !== null) {
80
+ return Object.fromEntries(
81
+ Object.entries(data).filter(([_, value]) => value !== void 0)
82
+ );
83
+ }
84
+ return data;
85
+ }
86
+ class Logger {
87
+ constructor(scopes = [], sinks = [], scopeData = {}) {
88
+ this.scopes = scopes;
89
+ this.sinks = sinks;
90
+ this.scopeData = scopeData;
91
+ this.prefix = scopes.join(".");
92
+ }
93
+ prefix;
94
+ entry(entry) {
95
+ this.sinks.forEach((sink) => {
96
+ if (isSinkClosedOrClosing(sink)) {
97
+ return;
98
+ }
99
+ sink.write({
100
+ id: generateId(),
101
+ scope: this.prefix,
102
+ level: entry.level,
103
+ event: entry.event,
104
+ data: removeUndefined({ ...this.scopeData, ...entry.data }),
105
+ options: entry.options
106
+ });
107
+ });
108
+ }
109
+ log(event, data, options) {
110
+ this.entry({ level: "log", event, data, options });
111
+ }
112
+ error(event, dataOrError, options) {
113
+ const data = dataOrError instanceof Error ? {
114
+ error: {
115
+ type: dataOrError.constructor.name,
116
+ message: dataOrError.message,
117
+ stack: dataOrError.stack || null
118
+ }
119
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
120
+ ...dataOrError,
121
+ error: {
122
+ type: dataOrError.error.constructor.name,
123
+ message: dataOrError.error.message,
124
+ stack: dataOrError.error.stack || null
125
+ }
126
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
127
+ this.entry({
128
+ level: "error",
129
+ event,
130
+ data,
131
+ options
132
+ });
133
+ if (dataOrError instanceof Error) {
134
+ return dataOrError;
135
+ }
136
+ if (isObject(dataOrError) && dataOrError.error instanceof Error) {
137
+ return dataOrError.error;
138
+ }
139
+ let message = event;
140
+ if (data !== void 0) {
141
+ try {
142
+ message += "\n" + JSON.stringify(data, void 0, 2);
143
+ } catch {
144
+ message += "\n[Unserializable error data]";
145
+ }
146
+ }
147
+ return new Error(message);
148
+ }
149
+ warn(event, dataOrError, options) {
150
+ const data = dataOrError instanceof Error ? {
151
+ error: {
152
+ type: dataOrError.constructor.name,
153
+ message: dataOrError.message,
154
+ stack: dataOrError.stack || null
155
+ }
156
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
157
+ ...dataOrError,
158
+ error: {
159
+ type: dataOrError.error.constructor.name,
160
+ message: dataOrError.error.message,
161
+ stack: dataOrError.error.stack || null
162
+ }
163
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
164
+ this.entry({
165
+ level: "warn",
166
+ event,
167
+ data,
168
+ options
169
+ });
170
+ }
171
+ info(event, dataOrError, options) {
172
+ const data = dataOrError instanceof Error ? {
173
+ error: {
174
+ type: dataOrError.constructor.name,
175
+ message: dataOrError.message,
176
+ stack: dataOrError.stack || null
177
+ }
178
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
179
+ ...dataOrError,
180
+ error: {
181
+ type: dataOrError.error.constructor.name,
182
+ message: dataOrError.error.message,
183
+ stack: dataOrError.error.stack || null
184
+ }
185
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
186
+ this.entry({
187
+ level: "info",
188
+ event,
189
+ data,
190
+ options
191
+ });
192
+ }
193
+ withScope(scope, context = {}) {
194
+ return new Logger([...this.scopes, scope], this.sinks, {
195
+ ...this.scopeData,
196
+ ...context
197
+ });
198
+ }
199
+ withContext(context) {
200
+ return new Logger(this.scopes, this.sinks, {
201
+ ...this.scopeData,
202
+ ...context
203
+ });
204
+ }
205
+ withSink(sink) {
206
+ return new Logger(this.scopes, [...this.sinks, sink]);
207
+ }
208
+ async flush() {
209
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
210
+ const sink = this.sinks[i];
211
+ if (!sink) continue;
212
+ if (isSinkClosedOrClosing(sink)) continue;
213
+ try {
214
+ await sink.flush?.();
215
+ } catch {
216
+ }
217
+ }
218
+ }
219
+ async close() {
220
+ await this.flush();
221
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
222
+ const sink = this.sinks[i];
223
+ if (!sink) continue;
224
+ await closeSinkOnce(sink);
225
+ }
226
+ }
227
+ }
228
+ // Annotate the CommonJS export names for ESM import in node:
229
+ 0 && (module.exports = {
230
+ Logger,
231
+ defaultLogger
232
+ });
@@ -0,0 +1,86 @@
1
+ type LogOptions = {
2
+ timestamp?: Date;
3
+ };
4
+ /**
5
+ * Minimal logger interface accepted by public-facing runtime functions.
6
+ * Any logger with info/warn/error methods satisfies this — no need to
7
+ * implement withScope, withContext, flush, etc.
8
+ */
9
+ type MinimalLogger = {
10
+ info: (event: string, data?: any) => void;
11
+ warn: (event: string, data?: any) => void;
12
+ error: (event: string, data?: any) => any;
13
+ };
14
+ /** Default console logger used when callers omit the logger option. */
15
+ declare const defaultLogger: MinimalLogger;
16
+ type LoggerApi = {
17
+ log: (event: string, data?: Record<string, any>, options?: LogOptions) => void;
18
+ /**
19
+ * Logs an error and returns an Error object that can be thrown
20
+ *
21
+ * either pass in an Error directly as data or as { error: Error, ...other_data }
22
+ */
23
+ error: (event: string, data?: Error | ({
24
+ error: Error;
25
+ } & Record<string, any>) | unknown, options?: LogOptions) => Error;
26
+ warn: (event: string, data?: Error | ({
27
+ error: Error;
28
+ } & Record<string, any>) | unknown, options?: LogOptions) => void;
29
+ info: (event: string, data?: Error | ({
30
+ error: Error;
31
+ } & Record<string, any>) | unknown, options?: LogOptions) => void;
32
+ /**
33
+ * Context passed in will be attached to all entries in this scope.
34
+ */
35
+ withScope: (scope: string, context?: Record<string, any>) => LoggerApi;
36
+ /**
37
+ * Context passed in will be attached to all entries.
38
+ */
39
+ withContext: (context: Record<string, any>) => LoggerApi;
40
+ /**
41
+ * Flushes all sinks in reverse order (most recently added first).
42
+ */
43
+ flush: () => Promise<void>;
44
+ };
45
+ type LoggerSink = {
46
+ write: (args: {
47
+ id: string;
48
+ scope: string;
49
+ level: "log" | "error" | "warn" | "info";
50
+ event: string;
51
+ data: Record<string, any>;
52
+ options?: LogOptions;
53
+ }) => void;
54
+ flush?: () => Promise<void>;
55
+ close?: () => Promise<void>;
56
+ };
57
+ declare class Logger implements LoggerApi {
58
+ private readonly scopes;
59
+ private readonly sinks;
60
+ private readonly scopeData;
61
+ private readonly prefix;
62
+ constructor(scopes?: string[], sinks?: LoggerSink[], scopeData?: Record<string, any>);
63
+ entry(entry: {
64
+ level: "log" | "error" | "warn" | "info";
65
+ event: string;
66
+ data?: Record<string, any>;
67
+ options?: LogOptions;
68
+ }): void;
69
+ log(event: string, data?: Record<string, any>, options?: LogOptions): void;
70
+ error(event: string, dataOrError?: Error | ({
71
+ error: Error;
72
+ } & Record<string, any>) | unknown, options?: LogOptions): Error;
73
+ warn(event: string, dataOrError?: Error | ({
74
+ error: Error;
75
+ } & Record<string, any>) | unknown, options?: LogOptions): void;
76
+ info(event: string, dataOrError?: Error | ({
77
+ error: Error;
78
+ } & Record<string, any>) | unknown, options?: LogOptions): void;
79
+ withScope(scope: string, context?: Record<string, any>): LoggerApi;
80
+ withContext(context: Record<string, any>): LoggerApi;
81
+ withSink(sink: LoggerSink): Logger;
82
+ flush(): Promise<void>;
83
+ close(): Promise<void>;
84
+ }
85
+
86
+ export { type LogOptions, Logger, type LoggerApi, type LoggerSink, type MinimalLogger, defaultLogger };
@@ -0,0 +1,86 @@
1
+ type LogOptions = {
2
+ timestamp?: Date;
3
+ };
4
+ /**
5
+ * Minimal logger interface accepted by public-facing runtime functions.
6
+ * Any logger with info/warn/error methods satisfies this — no need to
7
+ * implement withScope, withContext, flush, etc.
8
+ */
9
+ type MinimalLogger = {
10
+ info: (event: string, data?: any) => void;
11
+ warn: (event: string, data?: any) => void;
12
+ error: (event: string, data?: any) => any;
13
+ };
14
+ /** Default console logger used when callers omit the logger option. */
15
+ declare const defaultLogger: MinimalLogger;
16
+ type LoggerApi = {
17
+ log: (event: string, data?: Record<string, any>, options?: LogOptions) => void;
18
+ /**
19
+ * Logs an error and returns an Error object that can be thrown
20
+ *
21
+ * either pass in an Error directly as data or as { error: Error, ...other_data }
22
+ */
23
+ error: (event: string, data?: Error | ({
24
+ error: Error;
25
+ } & Record<string, any>) | unknown, options?: LogOptions) => Error;
26
+ warn: (event: string, data?: Error | ({
27
+ error: Error;
28
+ } & Record<string, any>) | unknown, options?: LogOptions) => void;
29
+ info: (event: string, data?: Error | ({
30
+ error: Error;
31
+ } & Record<string, any>) | unknown, options?: LogOptions) => void;
32
+ /**
33
+ * Context passed in will be attached to all entries in this scope.
34
+ */
35
+ withScope: (scope: string, context?: Record<string, any>) => LoggerApi;
36
+ /**
37
+ * Context passed in will be attached to all entries.
38
+ */
39
+ withContext: (context: Record<string, any>) => LoggerApi;
40
+ /**
41
+ * Flushes all sinks in reverse order (most recently added first).
42
+ */
43
+ flush: () => Promise<void>;
44
+ };
45
+ type LoggerSink = {
46
+ write: (args: {
47
+ id: string;
48
+ scope: string;
49
+ level: "log" | "error" | "warn" | "info";
50
+ event: string;
51
+ data: Record<string, any>;
52
+ options?: LogOptions;
53
+ }) => void;
54
+ flush?: () => Promise<void>;
55
+ close?: () => Promise<void>;
56
+ };
57
+ declare class Logger implements LoggerApi {
58
+ private readonly scopes;
59
+ private readonly sinks;
60
+ private readonly scopeData;
61
+ private readonly prefix;
62
+ constructor(scopes?: string[], sinks?: LoggerSink[], scopeData?: Record<string, any>);
63
+ entry(entry: {
64
+ level: "log" | "error" | "warn" | "info";
65
+ event: string;
66
+ data?: Record<string, any>;
67
+ options?: LogOptions;
68
+ }): void;
69
+ log(event: string, data?: Record<string, any>, options?: LogOptions): void;
70
+ error(event: string, dataOrError?: Error | ({
71
+ error: Error;
72
+ } & Record<string, any>) | unknown, options?: LogOptions): Error;
73
+ warn(event: string, dataOrError?: Error | ({
74
+ error: Error;
75
+ } & Record<string, any>) | unknown, options?: LogOptions): void;
76
+ info(event: string, dataOrError?: Error | ({
77
+ error: Error;
78
+ } & Record<string, any>) | unknown, options?: LogOptions): void;
79
+ withScope(scope: string, context?: Record<string, any>): LoggerApi;
80
+ withContext(context: Record<string, any>): LoggerApi;
81
+ withSink(sink: LoggerSink): Logger;
82
+ flush(): Promise<void>;
83
+ close(): Promise<void>;
84
+ }
85
+
86
+ export { type LogOptions, Logger, type LoggerApi, type LoggerSink, type MinimalLogger, defaultLogger };
@@ -0,0 +1,207 @@
1
+ function generateId() {
2
+ return Math.random().toString(36).substring(2, 15);
3
+ }
4
+ const defaultLogger = {
5
+ info(event, data) {
6
+ console.log(`[INFO] ${event}`, data ?? "");
7
+ },
8
+ warn(event, data) {
9
+ console.warn(`[WARN] ${event}`, data ?? "");
10
+ },
11
+ error(event, data) {
12
+ console.error(`[ERROR] ${event}`, data ?? "");
13
+ }
14
+ };
15
+ const sinkLifecycleState = /* @__PURE__ */ new WeakMap();
16
+ function getSinkLifecycleState(sink) {
17
+ const existingState = sinkLifecycleState.get(sink);
18
+ if (existingState) {
19
+ return existingState;
20
+ }
21
+ const initialState = { closed: false };
22
+ sinkLifecycleState.set(sink, initialState);
23
+ return initialState;
24
+ }
25
+ function isSinkClosedOrClosing(sink) {
26
+ const state = sinkLifecycleState.get(sink);
27
+ return Boolean(state?.closed || state?.closing);
28
+ }
29
+ async function closeSinkOnce(sink) {
30
+ if (!sink.close) {
31
+ return;
32
+ }
33
+ const state = getSinkLifecycleState(sink);
34
+ if (state.closed) {
35
+ return;
36
+ }
37
+ if (state.closing) {
38
+ return state.closing;
39
+ }
40
+ state.closing = (async () => {
41
+ try {
42
+ await sink.close?.();
43
+ } catch {
44
+ } finally {
45
+ state.closed = true;
46
+ state.closing = void 0;
47
+ }
48
+ })();
49
+ return state.closing;
50
+ }
51
+ function isObject(value) {
52
+ return typeof value === "object" && value !== null;
53
+ }
54
+ function removeUndefined(data) {
55
+ if (typeof data === "object" && data !== null) {
56
+ return Object.fromEntries(
57
+ Object.entries(data).filter(([_, value]) => value !== void 0)
58
+ );
59
+ }
60
+ return data;
61
+ }
62
+ class Logger {
63
+ constructor(scopes = [], sinks = [], scopeData = {}) {
64
+ this.scopes = scopes;
65
+ this.sinks = sinks;
66
+ this.scopeData = scopeData;
67
+ this.prefix = scopes.join(".");
68
+ }
69
+ prefix;
70
+ entry(entry) {
71
+ this.sinks.forEach((sink) => {
72
+ if (isSinkClosedOrClosing(sink)) {
73
+ return;
74
+ }
75
+ sink.write({
76
+ id: generateId(),
77
+ scope: this.prefix,
78
+ level: entry.level,
79
+ event: entry.event,
80
+ data: removeUndefined({ ...this.scopeData, ...entry.data }),
81
+ options: entry.options
82
+ });
83
+ });
84
+ }
85
+ log(event, data, options) {
86
+ this.entry({ level: "log", event, data, options });
87
+ }
88
+ error(event, dataOrError, options) {
89
+ const data = dataOrError instanceof Error ? {
90
+ error: {
91
+ type: dataOrError.constructor.name,
92
+ message: dataOrError.message,
93
+ stack: dataOrError.stack || null
94
+ }
95
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
96
+ ...dataOrError,
97
+ error: {
98
+ type: dataOrError.error.constructor.name,
99
+ message: dataOrError.error.message,
100
+ stack: dataOrError.error.stack || null
101
+ }
102
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
103
+ this.entry({
104
+ level: "error",
105
+ event,
106
+ data,
107
+ options
108
+ });
109
+ if (dataOrError instanceof Error) {
110
+ return dataOrError;
111
+ }
112
+ if (isObject(dataOrError) && dataOrError.error instanceof Error) {
113
+ return dataOrError.error;
114
+ }
115
+ let message = event;
116
+ if (data !== void 0) {
117
+ try {
118
+ message += "\n" + JSON.stringify(data, void 0, 2);
119
+ } catch {
120
+ message += "\n[Unserializable error data]";
121
+ }
122
+ }
123
+ return new Error(message);
124
+ }
125
+ warn(event, dataOrError, options) {
126
+ const data = dataOrError instanceof Error ? {
127
+ error: {
128
+ type: dataOrError.constructor.name,
129
+ message: dataOrError.message,
130
+ stack: dataOrError.stack || null
131
+ }
132
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
133
+ ...dataOrError,
134
+ error: {
135
+ type: dataOrError.error.constructor.name,
136
+ message: dataOrError.error.message,
137
+ stack: dataOrError.error.stack || null
138
+ }
139
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
140
+ this.entry({
141
+ level: "warn",
142
+ event,
143
+ data,
144
+ options
145
+ });
146
+ }
147
+ info(event, dataOrError, options) {
148
+ const data = dataOrError instanceof Error ? {
149
+ error: {
150
+ type: dataOrError.constructor.name,
151
+ message: dataOrError.message,
152
+ stack: dataOrError.stack || null
153
+ }
154
+ } : isObject(dataOrError) && dataOrError.error instanceof Error ? {
155
+ ...dataOrError,
156
+ error: {
157
+ type: dataOrError.error.constructor.name,
158
+ message: dataOrError.error.message,
159
+ stack: dataOrError.error.stack || null
160
+ }
161
+ } : isObject(dataOrError) ? dataOrError : dataOrError !== void 0 ? { error: dataOrError } : void 0;
162
+ this.entry({
163
+ level: "info",
164
+ event,
165
+ data,
166
+ options
167
+ });
168
+ }
169
+ withScope(scope, context = {}) {
170
+ return new Logger([...this.scopes, scope], this.sinks, {
171
+ ...this.scopeData,
172
+ ...context
173
+ });
174
+ }
175
+ withContext(context) {
176
+ return new Logger(this.scopes, this.sinks, {
177
+ ...this.scopeData,
178
+ ...context
179
+ });
180
+ }
181
+ withSink(sink) {
182
+ return new Logger(this.scopes, [...this.sinks, sink]);
183
+ }
184
+ async flush() {
185
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
186
+ const sink = this.sinks[i];
187
+ if (!sink) continue;
188
+ if (isSinkClosedOrClosing(sink)) continue;
189
+ try {
190
+ await sink.flush?.();
191
+ } catch {
192
+ }
193
+ }
194
+ }
195
+ async close() {
196
+ await this.flush();
197
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
198
+ const sink = this.sinks[i];
199
+ if (!sink) continue;
200
+ await closeSinkOnce(sink);
201
+ }
202
+ }
203
+ }
204
+ export {
205
+ Logger,
206
+ defaultLogger
207
+ };