libretto 0.1.5 → 0.2.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 (168) hide show
  1. package/README.md +215 -17
  2. package/bin/libretto.mjs +18 -0
  3. package/dist/cli/cli.js +203 -0
  4. package/dist/cli/commands/ai.js +21 -0
  5. package/dist/cli/commands/browser.js +59 -0
  6. package/dist/cli/commands/execution.js +422 -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 +523 -0
  11. package/dist/cli/core/context.js +113 -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 +222 -0
  19. package/dist/cli/workers/run-integration-worker-protocol.js +0 -0
  20. package/dist/cli/workers/run-integration-worker.js +83 -0
  21. package/dist/index.cjs +123 -0
  22. package/dist/index.d.cts +19 -0
  23. package/dist/index.d.ts +19 -0
  24. package/dist/index.js +107 -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 +32 -0
  74. package/dist/shared/debug/index.d.cts +2 -0
  75. package/dist/shared/debug/index.d.ts +2 -0
  76. package/dist/shared/debug/index.js +10 -0
  77. package/dist/shared/debug/pause.cjs +56 -0
  78. package/dist/shared/debug/pause.d.cts +23 -0
  79. package/dist/shared/debug/pause.d.ts +23 -0
  80. package/dist/shared/debug/pause.js +30 -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/client.cjs +139 -0
  94. package/dist/shared/llm/client.d.cts +6 -0
  95. package/dist/shared/llm/client.d.ts +6 -0
  96. package/dist/shared/llm/client.js +115 -0
  97. package/dist/shared/llm/index.cjs +28 -0
  98. package/dist/shared/llm/index.d.cts +3 -0
  99. package/dist/shared/llm/index.d.ts +3 -0
  100. package/dist/shared/llm/index.js +4 -0
  101. package/dist/shared/llm/types.cjs +16 -0
  102. package/dist/shared/llm/types.d.cts +34 -0
  103. package/dist/shared/llm/types.d.ts +34 -0
  104. package/dist/shared/llm/types.js +0 -0
  105. package/dist/shared/logger/index.cjs +37 -0
  106. package/dist/shared/logger/index.d.cts +2 -0
  107. package/dist/shared/logger/index.d.ts +2 -0
  108. package/dist/shared/logger/index.js +13 -0
  109. package/dist/shared/logger/logger.cjs +213 -0
  110. package/dist/shared/logger/logger.d.cts +82 -0
  111. package/dist/shared/logger/logger.d.ts +82 -0
  112. package/dist/shared/logger/logger.js +188 -0
  113. package/dist/shared/logger/sinks.cjs +160 -0
  114. package/dist/shared/logger/sinks.d.cts +9 -0
  115. package/dist/shared/logger/sinks.d.ts +9 -0
  116. package/dist/shared/logger/sinks.js +124 -0
  117. package/dist/shared/paths/paths.cjs +104 -0
  118. package/dist/shared/paths/paths.d.cts +10 -0
  119. package/dist/shared/paths/paths.d.ts +10 -0
  120. package/dist/shared/paths/paths.js +73 -0
  121. package/dist/shared/run/api.cjs +35 -0
  122. package/dist/shared/run/api.d.cts +3 -0
  123. package/dist/shared/run/api.d.ts +3 -0
  124. package/dist/shared/run/api.js +12 -0
  125. package/dist/shared/run/browser.cjs +98 -0
  126. package/dist/shared/run/browser.d.cts +22 -0
  127. package/dist/shared/run/browser.d.ts +22 -0
  128. package/dist/shared/run/browser.js +74 -0
  129. package/dist/shared/state/index.cjs +38 -0
  130. package/dist/shared/state/index.d.cts +2 -0
  131. package/dist/shared/state/index.d.ts +2 -0
  132. package/dist/shared/state/index.js +16 -0
  133. package/dist/shared/state/session-state.cjs +85 -0
  134. package/dist/shared/state/session-state.d.cts +34 -0
  135. package/dist/shared/state/session-state.d.ts +34 -0
  136. package/dist/shared/state/session-state.js +56 -0
  137. package/dist/shared/visualization/ghost-cursor.cjs +174 -0
  138. package/dist/shared/visualization/ghost-cursor.d.cts +37 -0
  139. package/dist/shared/visualization/ghost-cursor.d.ts +37 -0
  140. package/dist/shared/visualization/ghost-cursor.js +145 -0
  141. package/dist/shared/visualization/highlight.cjs +134 -0
  142. package/dist/shared/visualization/highlight.d.cts +22 -0
  143. package/dist/shared/visualization/highlight.d.ts +22 -0
  144. package/dist/shared/visualization/highlight.js +108 -0
  145. package/dist/shared/visualization/index.cjs +45 -0
  146. package/dist/shared/visualization/index.d.cts +3 -0
  147. package/dist/shared/visualization/index.d.ts +3 -0
  148. package/dist/shared/visualization/index.js +24 -0
  149. package/dist/shared/workflow/workflow.cjs +47 -0
  150. package/dist/shared/workflow/workflow.d.cts +33 -0
  151. package/dist/shared/workflow/workflow.d.ts +33 -0
  152. package/dist/shared/workflow/workflow.js +21 -0
  153. package/package.json +125 -26
  154. package/.npmignore +0 -2
  155. package/bin/libretto +0 -31
  156. package/lib/connect.js +0 -34
  157. package/lib/export.js +0 -224
  158. package/lib/import.js +0 -166
  159. package/lib/index.js +0 -8
  160. package/lib/log.js +0 -9
  161. package/lib/validate.js +0 -20
  162. package/makefile +0 -8
  163. package/src/connect.coffee +0 -25
  164. package/src/export.coffee +0 -222
  165. package/src/import.coffee +0 -166
  166. package/src/index.coffee +0 -3
  167. package/src/log.coffee +0 -3
  168. package/src/validate.coffee +0 -10
@@ -0,0 +1,188 @@
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, data, options) {
148
+ this.entry({ level: "info", event, data, options });
149
+ }
150
+ withScope(scope, context = {}) {
151
+ return new Logger([...this.scopes, scope], this.sinks, {
152
+ ...this.scopeData,
153
+ ...context
154
+ });
155
+ }
156
+ withContext(context) {
157
+ return new Logger(this.scopes, this.sinks, {
158
+ ...this.scopeData,
159
+ ...context
160
+ });
161
+ }
162
+ withSink(sink) {
163
+ return new Logger(this.scopes, [...this.sinks, sink]);
164
+ }
165
+ async flush() {
166
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
167
+ const sink = this.sinks[i];
168
+ if (!sink) continue;
169
+ if (isSinkClosedOrClosing(sink)) continue;
170
+ try {
171
+ await sink.flush?.();
172
+ } catch {
173
+ }
174
+ }
175
+ }
176
+ async close() {
177
+ await this.flush();
178
+ for (let i = this.sinks.length - 1; i >= 0; i--) {
179
+ const sink = this.sinks[i];
180
+ if (!sink) continue;
181
+ await closeSinkOnce(sink);
182
+ }
183
+ }
184
+ }
185
+ export {
186
+ Logger,
187
+ defaultLogger
188
+ };
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var sinks_exports = {};
30
+ __export(sinks_exports, {
31
+ createFileLogSink: () => createFileLogSink,
32
+ jsonlConsoleSink: () => jsonlConsoleSink,
33
+ prettyConsoleSink: () => prettyConsoleSink
34
+ });
35
+ module.exports = __toCommonJS(sinks_exports);
36
+ var fs = __toESM(require("node:fs"), 1);
37
+ var path = __toESM(require("node:path"), 1);
38
+ function createFileLogSink({
39
+ filePath
40
+ }) {
41
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
42
+ const writeStream = fs.createWriteStream(filePath, { flags: "a" });
43
+ return {
44
+ write: ({ id, scope, level, event, data, options }) => {
45
+ if (writeStream.destroyed || writeStream.writableEnded) {
46
+ return;
47
+ }
48
+ const timestamp = options?.timestamp || /* @__PURE__ */ new Date();
49
+ const logEntry = {
50
+ timestamp: timestamp.toISOString(),
51
+ id,
52
+ level,
53
+ scope,
54
+ event,
55
+ data
56
+ };
57
+ const jsonLine = JSON.stringify(logEntry) + "\n";
58
+ try {
59
+ writeStream.write(jsonLine, (error) => {
60
+ if (error) {
61
+ console.error("Failed to write to log file:", error);
62
+ console[level]({ id, scope, event, data, timestamp });
63
+ }
64
+ });
65
+ } catch (error) {
66
+ console.error("Failed to write to log file:", error);
67
+ console[level]({ id, scope, event, data, timestamp });
68
+ }
69
+ },
70
+ flush: () => new Promise((resolve, reject) => {
71
+ if (!writeStream.writable || writeStream.writableEnded || writeStream.destroyed) {
72
+ resolve();
73
+ return;
74
+ }
75
+ writeStream.write("", (error) => {
76
+ if (error) {
77
+ reject(error);
78
+ } else {
79
+ resolve();
80
+ }
81
+ });
82
+ }),
83
+ close: () => new Promise((resolve) => {
84
+ if (writeStream.destroyed || writeStream.closed) {
85
+ resolve();
86
+ return;
87
+ }
88
+ let settled = false;
89
+ const done = () => {
90
+ if (settled) return;
91
+ settled = true;
92
+ resolve();
93
+ };
94
+ writeStream.once("finish", done);
95
+ writeStream.once("close", done);
96
+ writeStream.once("error", done);
97
+ try {
98
+ writeStream.end();
99
+ } catch {
100
+ done();
101
+ }
102
+ })
103
+ };
104
+ }
105
+ const colors = {
106
+ reset: "\x1B[0m",
107
+ gray: "\x1B[90m",
108
+ red: "\x1B[31m",
109
+ yellow: "\x1B[33m",
110
+ blue: "\x1B[34m",
111
+ cyan: "\x1B[36m"
112
+ };
113
+ function formatTimestamp(date) {
114
+ return date.toISOString().replace("T", " ").replace("Z", "");
115
+ }
116
+ const prettyConsoleSink = {
117
+ write: ({ scope, level, event, data, options }) => {
118
+ const timestamp = `${colors.gray}${formatTimestamp(options?.timestamp || /* @__PURE__ */ new Date())}${colors.reset}`;
119
+ const levelColor = level === "error" ? colors.red : level === "warn" ? colors.yellow : colors.blue;
120
+ const coloredScope = scope ? `${colors.cyan}[${scope}]${colors.reset}` : "";
121
+ const logPrefix = `${timestamp} ${levelColor}${level.toUpperCase()}${colors.reset} ${coloredScope} ${event}`;
122
+ if (level === "error" && data.error) {
123
+ const { error, ...otherData } = data;
124
+ console.error(logPrefix);
125
+ if (error.stack) {
126
+ console.error(` ${error.stack}`);
127
+ } else if (error.type && error.message) {
128
+ console.error(` ${error.type}: ${error.message}`);
129
+ }
130
+ if (Object.keys(otherData).length > 0) {
131
+ console.error(JSON.stringify(otherData, null, 2));
132
+ }
133
+ } else {
134
+ console[level](logPrefix);
135
+ if (Object.keys(data).length > 0) {
136
+ console[level](JSON.stringify(data, null, 2));
137
+ }
138
+ }
139
+ }
140
+ };
141
+ const jsonlConsoleSink = {
142
+ write: ({ id, scope, level, event, data, options }) => {
143
+ const timestamp = options?.timestamp || /* @__PURE__ */ new Date();
144
+ const logEntry = {
145
+ timestamp: timestamp.toISOString(),
146
+ id,
147
+ level,
148
+ scope: scope || void 0,
149
+ event,
150
+ data
151
+ };
152
+ console.log(JSON.stringify(logEntry));
153
+ }
154
+ };
155
+ // Annotate the CommonJS export names for ESM import in node:
156
+ 0 && (module.exports = {
157
+ createFileLogSink,
158
+ jsonlConsoleSink,
159
+ prettyConsoleSink
160
+ });
@@ -0,0 +1,9 @@
1
+ import { LoggerSink } from './logger.cjs';
2
+
3
+ declare function createFileLogSink({ filePath, }: {
4
+ filePath: string;
5
+ }): LoggerSink;
6
+ declare const prettyConsoleSink: LoggerSink;
7
+ declare const jsonlConsoleSink: LoggerSink;
8
+
9
+ export { createFileLogSink, jsonlConsoleSink, prettyConsoleSink };
@@ -0,0 +1,9 @@
1
+ import { LoggerSink } from './logger.js';
2
+
3
+ declare function createFileLogSink({ filePath, }: {
4
+ filePath: string;
5
+ }): LoggerSink;
6
+ declare const prettyConsoleSink: LoggerSink;
7
+ declare const jsonlConsoleSink: LoggerSink;
8
+
9
+ export { createFileLogSink, jsonlConsoleSink, prettyConsoleSink };
@@ -0,0 +1,124 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ function createFileLogSink({
4
+ filePath
5
+ }) {
6
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
7
+ const writeStream = fs.createWriteStream(filePath, { flags: "a" });
8
+ return {
9
+ write: ({ id, scope, level, event, data, options }) => {
10
+ if (writeStream.destroyed || writeStream.writableEnded) {
11
+ return;
12
+ }
13
+ const timestamp = options?.timestamp || /* @__PURE__ */ new Date();
14
+ const logEntry = {
15
+ timestamp: timestamp.toISOString(),
16
+ id,
17
+ level,
18
+ scope,
19
+ event,
20
+ data
21
+ };
22
+ const jsonLine = JSON.stringify(logEntry) + "\n";
23
+ try {
24
+ writeStream.write(jsonLine, (error) => {
25
+ if (error) {
26
+ console.error("Failed to write to log file:", error);
27
+ console[level]({ id, scope, event, data, timestamp });
28
+ }
29
+ });
30
+ } catch (error) {
31
+ console.error("Failed to write to log file:", error);
32
+ console[level]({ id, scope, event, data, timestamp });
33
+ }
34
+ },
35
+ flush: () => new Promise((resolve, reject) => {
36
+ if (!writeStream.writable || writeStream.writableEnded || writeStream.destroyed) {
37
+ resolve();
38
+ return;
39
+ }
40
+ writeStream.write("", (error) => {
41
+ if (error) {
42
+ reject(error);
43
+ } else {
44
+ resolve();
45
+ }
46
+ });
47
+ }),
48
+ close: () => new Promise((resolve) => {
49
+ if (writeStream.destroyed || writeStream.closed) {
50
+ resolve();
51
+ return;
52
+ }
53
+ let settled = false;
54
+ const done = () => {
55
+ if (settled) return;
56
+ settled = true;
57
+ resolve();
58
+ };
59
+ writeStream.once("finish", done);
60
+ writeStream.once("close", done);
61
+ writeStream.once("error", done);
62
+ try {
63
+ writeStream.end();
64
+ } catch {
65
+ done();
66
+ }
67
+ })
68
+ };
69
+ }
70
+ const colors = {
71
+ reset: "\x1B[0m",
72
+ gray: "\x1B[90m",
73
+ red: "\x1B[31m",
74
+ yellow: "\x1B[33m",
75
+ blue: "\x1B[34m",
76
+ cyan: "\x1B[36m"
77
+ };
78
+ function formatTimestamp(date) {
79
+ return date.toISOString().replace("T", " ").replace("Z", "");
80
+ }
81
+ const prettyConsoleSink = {
82
+ write: ({ scope, level, event, data, options }) => {
83
+ const timestamp = `${colors.gray}${formatTimestamp(options?.timestamp || /* @__PURE__ */ new Date())}${colors.reset}`;
84
+ const levelColor = level === "error" ? colors.red : level === "warn" ? colors.yellow : colors.blue;
85
+ const coloredScope = scope ? `${colors.cyan}[${scope}]${colors.reset}` : "";
86
+ const logPrefix = `${timestamp} ${levelColor}${level.toUpperCase()}${colors.reset} ${coloredScope} ${event}`;
87
+ if (level === "error" && data.error) {
88
+ const { error, ...otherData } = data;
89
+ console.error(logPrefix);
90
+ if (error.stack) {
91
+ console.error(` ${error.stack}`);
92
+ } else if (error.type && error.message) {
93
+ console.error(` ${error.type}: ${error.message}`);
94
+ }
95
+ if (Object.keys(otherData).length > 0) {
96
+ console.error(JSON.stringify(otherData, null, 2));
97
+ }
98
+ } else {
99
+ console[level](logPrefix);
100
+ if (Object.keys(data).length > 0) {
101
+ console[level](JSON.stringify(data, null, 2));
102
+ }
103
+ }
104
+ }
105
+ };
106
+ const jsonlConsoleSink = {
107
+ write: ({ id, scope, level, event, data, options }) => {
108
+ const timestamp = options?.timestamp || /* @__PURE__ */ new Date();
109
+ const logEntry = {
110
+ timestamp: timestamp.toISOString(),
111
+ id,
112
+ level,
113
+ scope: scope || void 0,
114
+ event,
115
+ data
116
+ };
117
+ console.log(JSON.stringify(logEntry));
118
+ }
119
+ };
120
+ export {
121
+ createFileLogSink,
122
+ jsonlConsoleSink,
123
+ prettyConsoleSink
124
+ };
@@ -0,0 +1,104 @@
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 paths_exports = {};
20
+ __export(paths_exports, {
21
+ ensureLibrettoPauseSignalDir: () => ensureLibrettoPauseSignalDir,
22
+ ensureLibrettoRunnerLogDir: () => ensureLibrettoRunnerLogDir,
23
+ ensureLibrettoSessionStatePath: () => ensureLibrettoSessionStatePath,
24
+ getLibrettoPauseSignalDir: () => getLibrettoPauseSignalDir,
25
+ getLibrettoPausedSignalPath: () => getLibrettoPausedSignalPath,
26
+ getLibrettoResumeSignalPath: () => getLibrettoResumeSignalPath,
27
+ getPauseSignalPathForDir: () => getPauseSignalPathForDir,
28
+ getRunnerLogPathForDir: () => getRunnerLogPathForDir
29
+ });
30
+ module.exports = __toCommonJS(paths_exports);
31
+ var import_node_fs = require("node:fs");
32
+ var import_node_path = require("node:path");
33
+ const LIBRETTO_DIRNAME = ".libretto";
34
+ const LIBRETTO_SESSIONS_DIRNAME = "sessions";
35
+ const SESSION_STATE_FILENAME = "state.json";
36
+ const RUNNER_LOG_DIRNAME = "logs";
37
+ const RUNNER_LOG_FILENAME = "logs.jsonl";
38
+ const PAUSED_SIGNAL_SUFFIX = "paused";
39
+ const RESUME_SIGNAL_SUFFIX = "resume";
40
+ function getLibrettoRoot(cwd = process.cwd()) {
41
+ return (0, import_node_path.join)(cwd, LIBRETTO_DIRNAME);
42
+ }
43
+ function getLibrettoSessionsDir(cwd = process.cwd()) {
44
+ return (0, import_node_path.join)(getLibrettoRoot(cwd), LIBRETTO_SESSIONS_DIRNAME);
45
+ }
46
+ function getLibrettoSessionDir(sessionName, cwd = process.cwd()) {
47
+ return (0, import_node_path.join)(getLibrettoSessionsDir(cwd), sessionName);
48
+ }
49
+ function getLibrettoSessionStatePath(sessionName, cwd = process.cwd()) {
50
+ return (0, import_node_path.join)(getLibrettoSessionDir(sessionName, cwd), SESSION_STATE_FILENAME);
51
+ }
52
+ function getLibrettoPauseSignalDir(sessionName, cwd = process.cwd()) {
53
+ return getLibrettoSessionDir(sessionName, cwd);
54
+ }
55
+ function getLibrettoRunnerLogDir(sessionName, cwd = process.cwd()) {
56
+ return (0, import_node_path.join)(getLibrettoSessionDir(sessionName, cwd), RUNNER_LOG_DIRNAME);
57
+ }
58
+ function getRunnerLogPathForDir(logDir) {
59
+ return (0, import_node_path.join)(logDir, RUNNER_LOG_FILENAME);
60
+ }
61
+ function getPauseSignalPathForDir(signalDir, sessionName, signal) {
62
+ const suffix = signal === "paused" ? PAUSED_SIGNAL_SUFFIX : RESUME_SIGNAL_SUFFIX;
63
+ return (0, import_node_path.join)(signalDir, `${sessionName}.${suffix}`);
64
+ }
65
+ function getLibrettoPausedSignalPath(sessionName, cwd = process.cwd()) {
66
+ return getPauseSignalPathForDir(
67
+ getLibrettoPauseSignalDir(sessionName, cwd),
68
+ sessionName,
69
+ "paused"
70
+ );
71
+ }
72
+ function getLibrettoResumeSignalPath(sessionName, cwd = process.cwd()) {
73
+ return getPauseSignalPathForDir(
74
+ getLibrettoPauseSignalDir(sessionName, cwd),
75
+ sessionName,
76
+ "resume"
77
+ );
78
+ }
79
+ function ensureLibrettoSessionStatePath(sessionName, cwd = process.cwd()) {
80
+ const filePath = getLibrettoSessionStatePath(sessionName, cwd);
81
+ (0, import_node_fs.mkdirSync)((0, import_node_path.dirname)(filePath), { recursive: true });
82
+ return filePath;
83
+ }
84
+ function ensureLibrettoPauseSignalDir(sessionName, cwd = process.cwd()) {
85
+ const dir = getLibrettoPauseSignalDir(sessionName, cwd);
86
+ (0, import_node_fs.mkdirSync)(dir, { recursive: true });
87
+ return dir;
88
+ }
89
+ function ensureLibrettoRunnerLogDir(sessionName, cwd = process.cwd()) {
90
+ const dir = getLibrettoRunnerLogDir(sessionName, cwd);
91
+ (0, import_node_fs.mkdirSync)(dir, { recursive: true });
92
+ return dir;
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ ensureLibrettoPauseSignalDir,
97
+ ensureLibrettoRunnerLogDir,
98
+ ensureLibrettoSessionStatePath,
99
+ getLibrettoPauseSignalDir,
100
+ getLibrettoPausedSignalPath,
101
+ getLibrettoResumeSignalPath,
102
+ getPauseSignalPathForDir,
103
+ getRunnerLogPathForDir
104
+ });
@@ -0,0 +1,10 @@
1
+ declare function getLibrettoPauseSignalDir(sessionName: string, cwd?: string): string;
2
+ declare function getRunnerLogPathForDir(logDir: string): string;
3
+ declare function getPauseSignalPathForDir(signalDir: string, sessionName: string, signal: "paused" | "resume"): string;
4
+ declare function getLibrettoPausedSignalPath(sessionName: string, cwd?: string): string;
5
+ declare function getLibrettoResumeSignalPath(sessionName: string, cwd?: string): string;
6
+ declare function ensureLibrettoSessionStatePath(sessionName: string, cwd?: string): string;
7
+ declare function ensureLibrettoPauseSignalDir(sessionName: string, cwd?: string): string;
8
+ declare function ensureLibrettoRunnerLogDir(sessionName: string, cwd?: string): string;
9
+
10
+ export { ensureLibrettoPauseSignalDir, ensureLibrettoRunnerLogDir, ensureLibrettoSessionStatePath, getLibrettoPauseSignalDir, getLibrettoPausedSignalPath, getLibrettoResumeSignalPath, getPauseSignalPathForDir, getRunnerLogPathForDir };
@@ -0,0 +1,10 @@
1
+ declare function getLibrettoPauseSignalDir(sessionName: string, cwd?: string): string;
2
+ declare function getRunnerLogPathForDir(logDir: string): string;
3
+ declare function getPauseSignalPathForDir(signalDir: string, sessionName: string, signal: "paused" | "resume"): string;
4
+ declare function getLibrettoPausedSignalPath(sessionName: string, cwd?: string): string;
5
+ declare function getLibrettoResumeSignalPath(sessionName: string, cwd?: string): string;
6
+ declare function ensureLibrettoSessionStatePath(sessionName: string, cwd?: string): string;
7
+ declare function ensureLibrettoPauseSignalDir(sessionName: string, cwd?: string): string;
8
+ declare function ensureLibrettoRunnerLogDir(sessionName: string, cwd?: string): string;
9
+
10
+ export { ensureLibrettoPauseSignalDir, ensureLibrettoRunnerLogDir, ensureLibrettoSessionStatePath, getLibrettoPauseSignalDir, getLibrettoPausedSignalPath, getLibrettoResumeSignalPath, getPauseSignalPathForDir, getRunnerLogPathForDir };
@@ -0,0 +1,73 @@
1
+ import { mkdirSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ const LIBRETTO_DIRNAME = ".libretto";
4
+ const LIBRETTO_SESSIONS_DIRNAME = "sessions";
5
+ const SESSION_STATE_FILENAME = "state.json";
6
+ const RUNNER_LOG_DIRNAME = "logs";
7
+ const RUNNER_LOG_FILENAME = "logs.jsonl";
8
+ const PAUSED_SIGNAL_SUFFIX = "paused";
9
+ const RESUME_SIGNAL_SUFFIX = "resume";
10
+ function getLibrettoRoot(cwd = process.cwd()) {
11
+ return join(cwd, LIBRETTO_DIRNAME);
12
+ }
13
+ function getLibrettoSessionsDir(cwd = process.cwd()) {
14
+ return join(getLibrettoRoot(cwd), LIBRETTO_SESSIONS_DIRNAME);
15
+ }
16
+ function getLibrettoSessionDir(sessionName, cwd = process.cwd()) {
17
+ return join(getLibrettoSessionsDir(cwd), sessionName);
18
+ }
19
+ function getLibrettoSessionStatePath(sessionName, cwd = process.cwd()) {
20
+ return join(getLibrettoSessionDir(sessionName, cwd), SESSION_STATE_FILENAME);
21
+ }
22
+ function getLibrettoPauseSignalDir(sessionName, cwd = process.cwd()) {
23
+ return getLibrettoSessionDir(sessionName, cwd);
24
+ }
25
+ function getLibrettoRunnerLogDir(sessionName, cwd = process.cwd()) {
26
+ return join(getLibrettoSessionDir(sessionName, cwd), RUNNER_LOG_DIRNAME);
27
+ }
28
+ function getRunnerLogPathForDir(logDir) {
29
+ return join(logDir, RUNNER_LOG_FILENAME);
30
+ }
31
+ function getPauseSignalPathForDir(signalDir, sessionName, signal) {
32
+ const suffix = signal === "paused" ? PAUSED_SIGNAL_SUFFIX : RESUME_SIGNAL_SUFFIX;
33
+ return join(signalDir, `${sessionName}.${suffix}`);
34
+ }
35
+ function getLibrettoPausedSignalPath(sessionName, cwd = process.cwd()) {
36
+ return getPauseSignalPathForDir(
37
+ getLibrettoPauseSignalDir(sessionName, cwd),
38
+ sessionName,
39
+ "paused"
40
+ );
41
+ }
42
+ function getLibrettoResumeSignalPath(sessionName, cwd = process.cwd()) {
43
+ return getPauseSignalPathForDir(
44
+ getLibrettoPauseSignalDir(sessionName, cwd),
45
+ sessionName,
46
+ "resume"
47
+ );
48
+ }
49
+ function ensureLibrettoSessionStatePath(sessionName, cwd = process.cwd()) {
50
+ const filePath = getLibrettoSessionStatePath(sessionName, cwd);
51
+ mkdirSync(dirname(filePath), { recursive: true });
52
+ return filePath;
53
+ }
54
+ function ensureLibrettoPauseSignalDir(sessionName, cwd = process.cwd()) {
55
+ const dir = getLibrettoPauseSignalDir(sessionName, cwd);
56
+ mkdirSync(dir, { recursive: true });
57
+ return dir;
58
+ }
59
+ function ensureLibrettoRunnerLogDir(sessionName, cwd = process.cwd()) {
60
+ const dir = getLibrettoRunnerLogDir(sessionName, cwd);
61
+ mkdirSync(dir, { recursive: true });
62
+ return dir;
63
+ }
64
+ export {
65
+ ensureLibrettoPauseSignalDir,
66
+ ensureLibrettoRunnerLogDir,
67
+ ensureLibrettoSessionStatePath,
68
+ getLibrettoPauseSignalDir,
69
+ getLibrettoPausedSignalPath,
70
+ getLibrettoResumeSignalPath,
71
+ getPauseSignalPathForDir,
72
+ getRunnerLogPathForDir
73
+ };