@elementumai/edk 0.9.11 → 0.9.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -5
- package/dist-sdk/src/catalog/factories.d.ts +35 -14
- package/dist-sdk/src/catalog/factories.js +64 -13
- package/dist-sdk/src/catalog/from-authoring.js +4 -4
- package/dist-sdk/src/catalog/index.d.ts +1 -1
- package/dist-sdk/src/catalog/org.js +3 -2
- package/dist-sdk/src/catalog/token.js +4 -0
- package/dist-sdk/src/catalog/types.d.ts +13 -3
- package/dist-sdk/src/core/triggers/on-demand.d.ts +4 -28
- package/dist-sdk/src/core/triggers/on-demand.js +2 -4
- package/dist-sdk/src/engine/entity.d.ts +2 -0
- package/dist-sdk/src/engine/logging.d.ts +90 -0
- package/dist-sdk/src/engine/logging.js +124 -0
- package/dist-sdk/src/engine/progress.d.ts +119 -0
- package/dist-sdk/src/engine/progress.js +647 -0
- package/dist-sdk/src/engine/pull/result-output.d.ts +24 -0
- package/dist-sdk/src/engine/pull/result-output.js +48 -0
- package/dist-sdk/src/entities/agent/tools.d.ts +2 -2
- package/dist-sdk/src/entities/app/build.d.ts +8 -1
- package/dist-sdk/src/entities/app/from-tf.js +4 -4
- package/dist-sdk/src/entities/app/to-tf.js +9 -13
- package/dist-sdk/src/entities/automation/build.d.ts +11 -16
- package/dist-sdk/src/entities/automation/build.js +38 -8
- package/dist-sdk/src/entities/automation/catalog-render.js +1 -1
- package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +1 -2
- package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +23 -5
- package/dist-sdk/src/entities/automation/deserializer/print-automation.js +239 -34
- package/dist-sdk/src/entities/automation/from-tf.js +168 -23
- package/dist-sdk/src/entities/automation/index.d.ts +2 -2
- package/dist-sdk/src/entities/automation/module.js +2 -2
- package/dist-sdk/src/entities/automation/output-field-types.js +25 -13
- package/dist-sdk/src/entities/automation/script-code.d.ts +12 -4
- package/dist-sdk/src/entities/automation/script-code.js +659 -101
- package/dist-sdk/src/entities/automation/tasks.d.ts +60 -28
- package/dist-sdk/src/entities/automation/tasks.js +31 -15
- package/dist-sdk/src/entities/automation/to-tf.js +362 -58
- package/dist-sdk/src/entities/automation/triggers.d.ts +15 -9
- package/dist-sdk/src/entities/automation/triggers.js +4 -2
- package/dist-sdk/src/entities/chart/build.d.ts +15 -3
- package/dist-sdk/src/entities/chart/build.js +6 -1
- package/dist-sdk/src/entities/chart/from-tf.d.ts +8 -2
- package/dist-sdk/src/entities/chart/from-tf.js +71 -8
- package/dist-sdk/src/entities/chart/index.d.ts +1 -1
- package/dist-sdk/src/entities/chart/index.js +1 -1
- package/dist-sdk/src/entities/chart/printer.js +27 -7
- package/dist-sdk/src/entities/chart/to-tf.js +56 -2
- package/dist-sdk/src/entities/datamine/from-tf.js +11 -1
- package/dist-sdk/src/entities/element/build.d.ts +6 -5
- package/dist-sdk/src/entities/element/from-tf.js +3 -2
- package/dist-sdk/src/entities/element/module.js +1 -0
- package/dist-sdk/src/entities/element/to-tf.d.ts +3 -0
- package/dist-sdk/src/entities/element/to-tf.js +9 -1
- package/dist-sdk/src/entities/field/from-tf.d.ts +24 -0
- package/dist-sdk/src/entities/field/from-tf.js +73 -1
- package/dist-sdk/src/entities/field/metadata.d.ts +18 -0
- package/dist-sdk/src/entities/field/metadata.js +34 -0
- package/dist-sdk/src/entities/relationship/build.d.ts +17 -6
- package/dist-sdk/src/entities/relationship/column-contract.d.ts +24 -0
- package/dist-sdk/src/entities/relationship/column-contract.js +41 -0
- package/dist-sdk/src/entities/relationship/from-tf.js +19 -34
- package/dist-sdk/src/entities/relationship/to-tf.js +15 -2
- package/dist-sdk/src/entities/shared/context.d.ts +6 -3
- package/dist-sdk/src/entities/shared/context.js +19 -1
- package/dist-sdk/src/entities/shared/filters/index.d.ts +7 -0
- package/dist-sdk/src/entities/shared/filters/index.js +28 -0
- package/dist-sdk/src/entities/shared/filters/print.js +2 -0
- package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +8 -1
- package/dist-sdk/src/entities/shared/filters/tf-codec.js +10 -6
- package/dist-sdk/src/entities/shared/filters/types.d.ts +1 -1
- package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +5 -0
- package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +36 -23
- package/dist-sdk/src/entities/skill/module.js +1 -0
- package/dist-sdk/src/entities/skill/to-tf.d.ts +1 -1
- package/dist-sdk/src/entities/skill/to-tf.js +17 -0
- package/dist-sdk/src/entities/skill/tools.d.ts +38 -12
- package/dist-sdk/src/entities/skill/tools.js +2 -1
- package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +8 -12
- package/dist-sdk/src/entities/skill-tool/from-tf.js +28 -44
- package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +4 -0
- package/dist-sdk/src/entities/skill-tool/to-tf.js +45 -7
- package/dist-sdk/src/entities/table/build.js +10 -2
- package/dist-sdk/src/entities/view/to-tf.js +5 -7
- package/dist-sdk/src/entities/widget/build.d.ts +26 -0
- package/dist-sdk/src/entities/widget/from-tf.js +30 -5
- package/dist-sdk/src/entities/widget/to-tf.js +111 -7
- package/dist-sdk/src/graph/types.d.ts +5 -0
- package/package.json +1 -1
- package/dist-sdk/src/core/types/platform.d.ts +0 -2
- package/dist-sdk/src/core/types/platform.js +0 -1
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where human-facing progress goes.
|
|
3
|
+
*
|
|
4
|
+
* Commands like `elementum pull app` reserve stdout for a single JSON summary,
|
|
5
|
+
* so every status line historically went to stderr. Terminals routinely color
|
|
6
|
+
* the entire stderr stream red, which made a normal interactive pull read as a
|
|
7
|
+
* wall of errors.
|
|
8
|
+
*
|
|
9
|
+
* Technical progress belongs to stderr unconditionally. This keeps stdout
|
|
10
|
+
* available for user data and messages, including when EDK is itself piped.
|
|
11
|
+
*
|
|
12
|
+
* Diagnostics are NOT progress. Warnings and errors stay on stderr
|
|
13
|
+
* unconditionally, because there the red is the point.
|
|
14
|
+
*/
|
|
15
|
+
/** The stream human-facing progress should be written to right now. */
|
|
16
|
+
export function progressStream() {
|
|
17
|
+
return process.stderr;
|
|
18
|
+
}
|
|
19
|
+
let machineReadableLogs = false;
|
|
20
|
+
/** Configure the global machine-readable diagnostic mode. */
|
|
21
|
+
export function configureMachineReadableLogs(enabled) {
|
|
22
|
+
machineReadableLogs = enabled;
|
|
23
|
+
}
|
|
24
|
+
export function machineReadableLogsEnabled() {
|
|
25
|
+
return machineReadableLogs;
|
|
26
|
+
}
|
|
27
|
+
/** Write one newline-terminated progress line to {@link progressStream}. */
|
|
28
|
+
export function writeProgress(line, textEffects) {
|
|
29
|
+
if (machineReadableLogs)
|
|
30
|
+
return;
|
|
31
|
+
const live = activeProgress;
|
|
32
|
+
if (live !== undefined) {
|
|
33
|
+
live.writeLog(line, textEffects);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const stream = progressStream();
|
|
37
|
+
stream.write(`${stream.isTTY === true ? renderTextEffects(line, textEffects) : line}\n`);
|
|
38
|
+
}
|
|
39
|
+
/** Render one EDK-owned technical log through the same live-output arbiter. */
|
|
40
|
+
export function writeTechnicalLog(level, source, message, timestamp = new Date().toISOString(), messageEffects) {
|
|
41
|
+
if (level === "DEBUG" && !debugLoggingEnabled())
|
|
42
|
+
return;
|
|
43
|
+
if (machineReadableLogs) {
|
|
44
|
+
const event = {
|
|
45
|
+
timestamp,
|
|
46
|
+
level,
|
|
47
|
+
component: source,
|
|
48
|
+
message,
|
|
49
|
+
...(messageEffects === undefined || messageEffects.length === 0 ? {} : { text_effects: messageEffects }),
|
|
50
|
+
};
|
|
51
|
+
process.stderr.write(`${JSON.stringify(event)}\n`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const normalizedLevel = level.padEnd(5, " ");
|
|
55
|
+
const sourceGap = " ".repeat(Math.max(0, 6 - source.length));
|
|
56
|
+
const formattedTimestamp = formatLogTimestamp(timestamp);
|
|
57
|
+
const prefix = `${formattedTimestamp} ${normalizedLevel}${sourceGap}[${source}] `;
|
|
58
|
+
const effects = [{
|
|
59
|
+
start: formattedTimestamp.length + 1,
|
|
60
|
+
end: formattedTimestamp.length + 1 + level.length,
|
|
61
|
+
color: logLevelColor(level),
|
|
62
|
+
bold: true,
|
|
63
|
+
}];
|
|
64
|
+
for (const effect of messageEffects ?? [])
|
|
65
|
+
effects.push({
|
|
66
|
+
...effect,
|
|
67
|
+
start: effect.start + prefix.length,
|
|
68
|
+
end: effect.end + prefix.length,
|
|
69
|
+
});
|
|
70
|
+
writeProgress(prefix + message, effects.sort((a, b) => a.start - b.start));
|
|
71
|
+
}
|
|
72
|
+
function debugLoggingEnabled() {
|
|
73
|
+
const configured = process.env["ELEMENTUM_LOG_LEVEL"]?.toLowerCase();
|
|
74
|
+
return process.env["EDK_DEBUG"] === "1" || configured === "debug" || configured === "trace" ||
|
|
75
|
+
process.argv.some((arg) => arg === "--debug" || arg === "--log-level=debug" || arg === "--log-level=trace");
|
|
76
|
+
}
|
|
77
|
+
/** Write user-owned output without allowing it to collide with the live row. */
|
|
78
|
+
export function writeUserOutput(text, destination = process.stdout) {
|
|
79
|
+
const live = activeProgress;
|
|
80
|
+
if (live !== undefined) {
|
|
81
|
+
live.writeUserOutput(text, destination);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
destination.write(text);
|
|
85
|
+
}
|
|
86
|
+
/** Write a final user-facing operation status without contaminating pipes. */
|
|
87
|
+
export function writeUserStatus(message, color) {
|
|
88
|
+
if (machineReadableLogs)
|
|
89
|
+
return;
|
|
90
|
+
const rendered = process.stdout.isTTY === true && process.env["NO_COLOR"] === undefined
|
|
91
|
+
? renderTextEffects(message, [{ start: 0, end: message.length, color, bold: true }])
|
|
92
|
+
: message;
|
|
93
|
+
writeUserOutput(`${rendered}\n`);
|
|
94
|
+
}
|
|
95
|
+
const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
96
|
+
let activeProgress;
|
|
97
|
+
/**
|
|
98
|
+
* A single, terminal-aware status line. It is always rendered at the current
|
|
99
|
+
* cursor position, immediately after the durable output already displayed;
|
|
100
|
+
* output arriving from logs or child processes first clears and then redraws
|
|
101
|
+
* that row. Non-TTY streams deliberately render nothing.
|
|
102
|
+
*/
|
|
103
|
+
export class LiveProgress {
|
|
104
|
+
stream;
|
|
105
|
+
total;
|
|
106
|
+
intervalMs;
|
|
107
|
+
maxLabelLength;
|
|
108
|
+
retainCountOnLabelChange;
|
|
109
|
+
persistLabelChanges;
|
|
110
|
+
label;
|
|
111
|
+
value = 0;
|
|
112
|
+
frame = 0;
|
|
113
|
+
startedAt = 0;
|
|
114
|
+
timer;
|
|
115
|
+
running = false;
|
|
116
|
+
suspended = false;
|
|
117
|
+
textEffects;
|
|
118
|
+
bridgeSection;
|
|
119
|
+
writing = false;
|
|
120
|
+
batchDepth = 0;
|
|
121
|
+
outputQueue = [];
|
|
122
|
+
handleTerminationSignal = (signal) => {
|
|
123
|
+
if (!this.running)
|
|
124
|
+
return;
|
|
125
|
+
this.running = false;
|
|
126
|
+
if (this.timer !== undefined)
|
|
127
|
+
clearInterval(this.timer);
|
|
128
|
+
this.timer = undefined;
|
|
129
|
+
this.outputQueue = [];
|
|
130
|
+
this.detachTerminationHandlers();
|
|
131
|
+
if (activeProgress === this)
|
|
132
|
+
activeProgress = undefined;
|
|
133
|
+
// Bypass the normal output queue: on termination there may never be a
|
|
134
|
+
// drain event, but the terminal must see the cursor-show sequence before
|
|
135
|
+
// the process dies. Re-raise on the next turn so other signal listeners
|
|
136
|
+
// (notably child-process forwarding) can run first.
|
|
137
|
+
this.stream.write("\r\x1b[2K\x1b[?25h");
|
|
138
|
+
setImmediate(() => process.kill(process.pid, signal));
|
|
139
|
+
};
|
|
140
|
+
constructor(options) {
|
|
141
|
+
this.maxLabelLength = options.maxLabelLength;
|
|
142
|
+
this.retainCountOnLabelChange = options.retainCountOnLabelChange ?? false;
|
|
143
|
+
this.label = truncateLabel(options.label, this.maxLabelLength);
|
|
144
|
+
this.total = options.total;
|
|
145
|
+
this.intervalMs = options.intervalMs ?? 100;
|
|
146
|
+
this.persistLabelChanges = options.persistLabelChanges ?? true;
|
|
147
|
+
this.stream = progressStream();
|
|
148
|
+
}
|
|
149
|
+
get tty() {
|
|
150
|
+
return this.stream.isTTY === true;
|
|
151
|
+
}
|
|
152
|
+
start() {
|
|
153
|
+
if (this.running)
|
|
154
|
+
return this;
|
|
155
|
+
if (machineReadableLogs)
|
|
156
|
+
return this;
|
|
157
|
+
this.running = true;
|
|
158
|
+
this.startedAt = Date.now();
|
|
159
|
+
if (!this.tty)
|
|
160
|
+
return this;
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias -- the singleton coordinates output from asynchronous subprocess readers
|
|
162
|
+
activeProgress = this;
|
|
163
|
+
this.attachTerminationHandlers();
|
|
164
|
+
this.configureTerminal();
|
|
165
|
+
this.render();
|
|
166
|
+
this.timer = setInterval(() => this.render(), this.intervalMs);
|
|
167
|
+
this.timer.unref();
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
update(label) {
|
|
171
|
+
this.setState({ label });
|
|
172
|
+
}
|
|
173
|
+
setState(state) {
|
|
174
|
+
const nextLabel = state.label === undefined ? undefined : truncateLabel(state.label, this.maxLabelLength);
|
|
175
|
+
const labelChanged = nextLabel !== undefined && nextLabel !== this.label;
|
|
176
|
+
if (state.label !== undefined) {
|
|
177
|
+
this.finalizeLabel(nextLabel);
|
|
178
|
+
this.label = nextLabel;
|
|
179
|
+
}
|
|
180
|
+
// A label-only update starts a new phase. Do not carry the previous
|
|
181
|
+
// phase's determinate counter into an unrelated status line.
|
|
182
|
+
if (labelChanged && !this.retainCountOnLabelChange && !("current" in state) && !("total" in state)) {
|
|
183
|
+
this.value = 0;
|
|
184
|
+
this.total = undefined;
|
|
185
|
+
}
|
|
186
|
+
if (state.current !== undefined)
|
|
187
|
+
this.value = state.current;
|
|
188
|
+
if ("total" in state)
|
|
189
|
+
this.total = state.total;
|
|
190
|
+
if ("textEffects" in state)
|
|
191
|
+
this.textEffects = state.textEffects;
|
|
192
|
+
// State updates are ephemeral just like timer-driven spinner frames.
|
|
193
|
+
// Durable output is written through writeLog(); treating every bridge
|
|
194
|
+
// progress event as priority 0 would queue one terminal redraw per tofu
|
|
195
|
+
// resource and eventually dump the whole history at once.
|
|
196
|
+
if (this.running && !this.suspended)
|
|
197
|
+
this.render();
|
|
198
|
+
}
|
|
199
|
+
/** Temporarily let an EI-owned section replace the command-owned status. */
|
|
200
|
+
enterBridgeSection(event) {
|
|
201
|
+
if (this.bridgeSection === undefined) {
|
|
202
|
+
this.bridgeSection = {
|
|
203
|
+
sectionId: event.section_id,
|
|
204
|
+
previous: {
|
|
205
|
+
label: this.label,
|
|
206
|
+
current: this.value,
|
|
207
|
+
total: this.total,
|
|
208
|
+
textEffects: this.textEffects,
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
const state = {
|
|
213
|
+
label: event.label,
|
|
214
|
+
textEffects: event.text_effects,
|
|
215
|
+
};
|
|
216
|
+
if (event.current !== undefined)
|
|
217
|
+
state.current = event.current;
|
|
218
|
+
if (event.total !== undefined)
|
|
219
|
+
state.total = event.total;
|
|
220
|
+
this.setState(state);
|
|
221
|
+
}
|
|
222
|
+
/** Finish only EI's section; the pull command still owns the live renderer. */
|
|
223
|
+
leaveBridgeSection(sectionId) {
|
|
224
|
+
const section = this.bridgeSection;
|
|
225
|
+
if (section === undefined || (sectionId !== undefined && section.sectionId !== sectionId))
|
|
226
|
+
return;
|
|
227
|
+
this.bridgeSection = undefined;
|
|
228
|
+
this.setState({
|
|
229
|
+
label: section.previous.label,
|
|
230
|
+
current: section.previous.current,
|
|
231
|
+
total: section.previous.total,
|
|
232
|
+
textEffects: section.previous.textEffects,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
finalizeLabel(nextLabel) {
|
|
236
|
+
if (this.persistLabelChanges && nextLabel !== this.label && this.running && !this.suspended && this.tty && this.label !== "") {
|
|
237
|
+
// Keep the previous state as an ordinary terminal line so the user can
|
|
238
|
+
// reconstruct the sequence of pull stages after the live row advances.
|
|
239
|
+
this.writeLog(this.label);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
increment(amount = 1) {
|
|
243
|
+
this.value = this.total === undefined ? this.value + amount : Math.min(this.total, this.value + amount);
|
|
244
|
+
if (this.running && !this.suspended)
|
|
245
|
+
this.render();
|
|
246
|
+
}
|
|
247
|
+
/** Temporarily return terminal ownership to an interactive child process. */
|
|
248
|
+
suspend() {
|
|
249
|
+
if (!this.running || this.suspended || !this.tty)
|
|
250
|
+
return;
|
|
251
|
+
this.suspended = true;
|
|
252
|
+
this.clearStatus();
|
|
253
|
+
this.restoreTerminal();
|
|
254
|
+
}
|
|
255
|
+
resume() {
|
|
256
|
+
if (!this.running || !this.suspended || !this.tty)
|
|
257
|
+
return;
|
|
258
|
+
this.suspended = false;
|
|
259
|
+
this.configureTerminal();
|
|
260
|
+
this.render();
|
|
261
|
+
}
|
|
262
|
+
stop() {
|
|
263
|
+
if (!this.running)
|
|
264
|
+
return;
|
|
265
|
+
this.running = false;
|
|
266
|
+
if (this.timer !== undefined)
|
|
267
|
+
clearInterval(this.timer);
|
|
268
|
+
this.timer = undefined;
|
|
269
|
+
this.detachTerminationHandlers();
|
|
270
|
+
if (this.tty && !this.suspended) {
|
|
271
|
+
this.clearStatus();
|
|
272
|
+
this.restoreTerminal();
|
|
273
|
+
}
|
|
274
|
+
if (activeProgress === this)
|
|
275
|
+
activeProgress = undefined;
|
|
276
|
+
}
|
|
277
|
+
/** Write a durable log line above the status row. */
|
|
278
|
+
writeLog(line, textEffects) {
|
|
279
|
+
if (!this.tty || !this.running) {
|
|
280
|
+
this.stream.write(`${line}\n`);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (this.suspended) {
|
|
284
|
+
this.stream.write(`${renderTextEffects(line, textEffects)}\n`);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
this.enqueue(`\r\x1b[2K${renderTextEffects(line, textEffects)}\n`, 0, this.stream);
|
|
288
|
+
this.enqueue(this.currentStatusRendered(), 1, this.stream);
|
|
289
|
+
}
|
|
290
|
+
writeUserOutput(text, destination = process.stdout) {
|
|
291
|
+
const normalized = text.replace(/\r\n/g, "\n");
|
|
292
|
+
if (destination === process.stdout && process.stdout.isTTY === true && normalized.endsWith("\n") && normalized.slice(0, -1) === this.label) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
// A redirected destination must remain redirected. It cannot be routed
|
|
296
|
+
// through the terminal arbiter because that would silently move stdout to
|
|
297
|
+
// stderr when stdout is a file or pipe.
|
|
298
|
+
if (destination !== this.stream) {
|
|
299
|
+
if (destination === process.stdout && this.running && !this.suspended && process.stdout.isTTY === true) {
|
|
300
|
+
// stdout remains the user-message channel, but it shares the terminal
|
|
301
|
+
// cursor with the live row rendered on stderr. The clear must be sent
|
|
302
|
+
// through the same fd as the message: writes to stdout and stderr can
|
|
303
|
+
// otherwise be reordered by the terminal even when scheduled here.
|
|
304
|
+
destination.write("\r\x1b[2K");
|
|
305
|
+
destination.write(text);
|
|
306
|
+
this.enqueue(this.currentStatusRendered(), 1, this.stream);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
destination.write(text);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if ((!this.tty || !this.running || this.suspended) && destination !== process.stdout) {
|
|
313
|
+
destination.write(text);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if ((!this.tty || !this.running || this.suspended) && process.stdout.isTTY !== true) {
|
|
317
|
+
this.stream.write(text);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (!this.tty || !this.running || this.suspended) {
|
|
321
|
+
destination.write(text);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
// Some bridge versions still mirror a transient stage to stdout while
|
|
325
|
+
// also sending its progress event. The event already owns this label, so
|
|
326
|
+
// forwarding an identical line would print every stage twice. Keep all
|
|
327
|
+
// other stdout untouched: it may be a meaningful user message.
|
|
328
|
+
this.enqueue("\r\x1b[2K", 0, this.stream);
|
|
329
|
+
// While a live row owns the terminal, keep the clear/message/redraw
|
|
330
|
+
// sequence on one file descriptor. Mixing stdout and stderr lets the
|
|
331
|
+
// kernel reorder ToFu diagnostics next to the spinner even though the
|
|
332
|
+
// logical queue is serialized.
|
|
333
|
+
this.enqueue(text, 0, this.stream);
|
|
334
|
+
if (!text.endsWith("\n"))
|
|
335
|
+
this.enqueue("\n", 0, this.stream);
|
|
336
|
+
this.enqueue(this.currentStatusRendered(), 1, this.stream);
|
|
337
|
+
}
|
|
338
|
+
configureTerminal() {
|
|
339
|
+
this.enqueue("\x1b[?25l", 0, this.stream);
|
|
340
|
+
}
|
|
341
|
+
restoreTerminal() {
|
|
342
|
+
this.enqueue("\r\x1b[2K\x1b[?25h", 0, this.stream);
|
|
343
|
+
}
|
|
344
|
+
clearStatus() {
|
|
345
|
+
this.enqueue("\r\x1b[2K", 0, this.stream);
|
|
346
|
+
}
|
|
347
|
+
attachTerminationHandlers() {
|
|
348
|
+
process.once("SIGINT", this.handleTerminationSignal);
|
|
349
|
+
process.once("SIGTERM", this.handleTerminationSignal);
|
|
350
|
+
}
|
|
351
|
+
detachTerminationHandlers() {
|
|
352
|
+
process.off("SIGINT", this.handleTerminationSignal);
|
|
353
|
+
process.off("SIGTERM", this.handleTerminationSignal);
|
|
354
|
+
}
|
|
355
|
+
render(priority = 1) {
|
|
356
|
+
if (!this.tty || !this.running || this.suspended)
|
|
357
|
+
return;
|
|
358
|
+
this.enqueue(`\r\x1b[2K${this.currentStatusRendered()}`, priority, this.stream);
|
|
359
|
+
}
|
|
360
|
+
currentStatusRendered() {
|
|
361
|
+
const spinner = SPINNER[this.frame++ % SPINNER.length];
|
|
362
|
+
// Some terminal wrappers expose `isTTY` but omit `columns`. Rendering an
|
|
363
|
+
// unbounded status in that case is worse than using a conservative width:
|
|
364
|
+
// once it wraps, clearing with CR only affects the last physical row and
|
|
365
|
+
// every spinner frame becomes visible. Keep the live row single-line.
|
|
366
|
+
const columns = this.stream.columns !== undefined && this.stream.columns > 0 ? this.stream.columns : 80;
|
|
367
|
+
if (this.total === undefined) {
|
|
368
|
+
const elapsed = formatElapsed(Date.now() - this.startedAt);
|
|
369
|
+
const suffix = elapsed === "" ? "" : ` ${applyEffect(elapsed, { start: 0, end: elapsed.length, dim: true })}`;
|
|
370
|
+
const labelWidth = Math.max(0, columns - 2 - visibleWidth(suffix));
|
|
371
|
+
return `${spinner} ${renderFittedText(this.label, this.textEffects, labelWidth)}${suffix}`;
|
|
372
|
+
}
|
|
373
|
+
let barWidth = 20;
|
|
374
|
+
const fixedWidth = 2 + 1 + String(this.value).length + 1 + String(this.total).length + 1 + 2;
|
|
375
|
+
barWidth = Math.max(4, Math.min(20, columns - fixedWidth - 2));
|
|
376
|
+
const suffix = ` ${this.value}/${this.total} ${this.bar(barWidth)}`;
|
|
377
|
+
const labelWidth = Math.max(0, columns - 2 - visibleWidth(suffix));
|
|
378
|
+
return `${spinner} ${renderFittedText(this.label, this.textEffects, labelWidth)}${suffix}`;
|
|
379
|
+
}
|
|
380
|
+
bar(width = 20) {
|
|
381
|
+
const total = this.total ?? 0;
|
|
382
|
+
const filled = total === 0 ? 0 : Math.round((this.value / total) * width);
|
|
383
|
+
return `[${"█".repeat(filled)}${"░".repeat(width - filled)}]`;
|
|
384
|
+
}
|
|
385
|
+
/** Single terminal writer. Priority 0 is protocol/log output; priority 1 is animation. */
|
|
386
|
+
enqueue(text, priority, destination) {
|
|
387
|
+
// Animation is ephemeral: never let stale frames accumulate behind a
|
|
388
|
+
// slow terminal. Durable log/protocol writes are retained in FIFO order.
|
|
389
|
+
// This also applies when a durable write arrives: any pending redraw can
|
|
390
|
+
// be discarded because the durable write will redraw the current row.
|
|
391
|
+
this.outputQueue = this.outputQueue.filter((item) => item.priority === 0);
|
|
392
|
+
this.outputQueue.push({ text, priority, destination });
|
|
393
|
+
if (this.batchDepth === 0)
|
|
394
|
+
this.flushQueue();
|
|
395
|
+
}
|
|
396
|
+
beginBatch() {
|
|
397
|
+
this.batchDepth += 1;
|
|
398
|
+
}
|
|
399
|
+
endBatch() {
|
|
400
|
+
if (this.batchDepth === 0)
|
|
401
|
+
return;
|
|
402
|
+
this.batchDepth -= 1;
|
|
403
|
+
if (this.batchDepth === 0)
|
|
404
|
+
this.flushQueue();
|
|
405
|
+
}
|
|
406
|
+
flushQueue() {
|
|
407
|
+
if (this.writing)
|
|
408
|
+
return;
|
|
409
|
+
const next = this.outputQueue.shift();
|
|
410
|
+
if (next === undefined)
|
|
411
|
+
return;
|
|
412
|
+
this.writing = true;
|
|
413
|
+
if (next.destination.write(next.text)) {
|
|
414
|
+
this.writing = false;
|
|
415
|
+
this.flushQueue();
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
next.destination.once("drain", () => {
|
|
419
|
+
this.writing = false;
|
|
420
|
+
this.flushQueue();
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/** Coalesce all live-progress writes caused by one child-stream drain. */
|
|
425
|
+
export function withProgressBatch(fn) {
|
|
426
|
+
const progress = activeProgress;
|
|
427
|
+
progress?.beginBatch();
|
|
428
|
+
try {
|
|
429
|
+
return fn();
|
|
430
|
+
}
|
|
431
|
+
finally {
|
|
432
|
+
progress?.endBatch();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** Current live renderer, used by subprocess wrappers to protect TTY UI. */
|
|
436
|
+
export function suspendProgress() {
|
|
437
|
+
activeProgress?.suspend();
|
|
438
|
+
}
|
|
439
|
+
export function resumeProgress() {
|
|
440
|
+
activeProgress?.resume();
|
|
441
|
+
}
|
|
442
|
+
export function updateProgress(label) {
|
|
443
|
+
activeProgress?.update(label);
|
|
444
|
+
}
|
|
445
|
+
export function incrementProgress(amount = 1) {
|
|
446
|
+
activeProgress?.increment(amount);
|
|
447
|
+
}
|
|
448
|
+
export function setProgressCount(label, current, total) {
|
|
449
|
+
activeProgress?.setState({ label, current, total });
|
|
450
|
+
}
|
|
451
|
+
export function liveProgressActive() {
|
|
452
|
+
return activeProgress !== undefined;
|
|
453
|
+
}
|
|
454
|
+
/** Apply a bridge lifecycle event without allowing the producer to write to the terminal. */
|
|
455
|
+
export function applyProgressEvent(event) {
|
|
456
|
+
switch (event.event) {
|
|
457
|
+
case "progress_enter":
|
|
458
|
+
activeProgress?.enterBridgeSection(event);
|
|
459
|
+
break;
|
|
460
|
+
case "progress_update":
|
|
461
|
+
if (activeProgress !== undefined) {
|
|
462
|
+
const state = {
|
|
463
|
+
label: event.label,
|
|
464
|
+
textEffects: event.text_effects,
|
|
465
|
+
};
|
|
466
|
+
if (event.current !== undefined)
|
|
467
|
+
state.current = event.current;
|
|
468
|
+
if (event.total !== undefined)
|
|
469
|
+
state.total = event.total;
|
|
470
|
+
activeProgress.setState(state);
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
case "progress_leave":
|
|
474
|
+
activeProgress?.leaveBridgeSection(event.section_id);
|
|
475
|
+
break;
|
|
476
|
+
case "progress_suspend":
|
|
477
|
+
suspendProgress();
|
|
478
|
+
break;
|
|
479
|
+
case "progress_resume":
|
|
480
|
+
resumeProgress();
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
export function renderTextEffects(text, effects) {
|
|
485
|
+
if (effects === undefined || effects.length === 0 || process.env["NO_COLOR"] !== undefined)
|
|
486
|
+
return text;
|
|
487
|
+
const boundaries = new Set([0, text.length]);
|
|
488
|
+
for (const effect of effects) {
|
|
489
|
+
boundaries.add(Math.min(effect.start, text.length));
|
|
490
|
+
boundaries.add(Math.min(effect.end, text.length));
|
|
491
|
+
}
|
|
492
|
+
const points = [...boundaries].sort((a, b) => a - b);
|
|
493
|
+
let result = "";
|
|
494
|
+
for (let index = 0; index < points.length - 1; index += 1) {
|
|
495
|
+
const start = points[index];
|
|
496
|
+
const end = points[index + 1];
|
|
497
|
+
const active = effects.filter((effect) => effect.start < end && effect.end > start);
|
|
498
|
+
result += active.length === 0 ? text.slice(start, end) : applyEffect(text.slice(start, end), mergeEffects(active));
|
|
499
|
+
}
|
|
500
|
+
return result;
|
|
501
|
+
}
|
|
502
|
+
function withoutAnsi(text) {
|
|
503
|
+
return text.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "");
|
|
504
|
+
}
|
|
505
|
+
function visibleWidth(text) {
|
|
506
|
+
return Array.from(withoutAnsi(text)).length;
|
|
507
|
+
}
|
|
508
|
+
function renderFittedText(text, effects, maxWidth) {
|
|
509
|
+
if (maxWidth === undefined || visibleWidth(text) <= maxWidth)
|
|
510
|
+
return renderTextEffects(text, effects);
|
|
511
|
+
if (maxWidth <= 0)
|
|
512
|
+
return "";
|
|
513
|
+
const suffix = "...";
|
|
514
|
+
let end = text.length;
|
|
515
|
+
while (end > 0 && visibleWidth(text.slice(0, end) + suffix) > maxWidth)
|
|
516
|
+
end -= 1;
|
|
517
|
+
const clippedEffects = (effects ?? [])
|
|
518
|
+
.filter((effect) => effect.start < end && effect.end > 0)
|
|
519
|
+
.map((effect) => ({ ...effect, start: Math.min(effect.start, end), end: Math.min(effect.end, end) }));
|
|
520
|
+
return `${renderTextEffects(text.slice(0, end), clippedEffects)}${suffix}`;
|
|
521
|
+
}
|
|
522
|
+
function truncateLabel(label, maxLength) {
|
|
523
|
+
if (maxLength === undefined || label.length <= maxLength)
|
|
524
|
+
return label;
|
|
525
|
+
if (maxLength <= 3)
|
|
526
|
+
return ".".repeat(maxLength);
|
|
527
|
+
return `${label.slice(0, maxLength - 3)}...`;
|
|
528
|
+
}
|
|
529
|
+
function formatLogTimestamp(timestamp) {
|
|
530
|
+
const date = new Date(timestamp);
|
|
531
|
+
if (Number.isNaN(date.getTime()))
|
|
532
|
+
return timestamp.padEnd(23, " ").slice(0, 23);
|
|
533
|
+
return date.toISOString().slice(0, 23).replace("T", " ");
|
|
534
|
+
}
|
|
535
|
+
function formatElapsed(milliseconds) {
|
|
536
|
+
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1000));
|
|
537
|
+
const hours = Math.floor(totalSeconds / 3600);
|
|
538
|
+
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
539
|
+
const seconds = totalSeconds % 60;
|
|
540
|
+
const parts = [];
|
|
541
|
+
const unit = (value, singular, plural) => `${value} ${value === 1 ? singular : plural}`;
|
|
542
|
+
if (hours > 0)
|
|
543
|
+
parts.push(unit(hours, "hour", "hours"));
|
|
544
|
+
if (minutes > 0)
|
|
545
|
+
parts.push(unit(minutes, "minute", "minutes"));
|
|
546
|
+
if (seconds > 0)
|
|
547
|
+
parts.push(unit(seconds, "second", "seconds"));
|
|
548
|
+
return parts.length === 0 ? "" : `(elapsed ${parts.join(" ")})`;
|
|
549
|
+
}
|
|
550
|
+
function logLevelColor(level) {
|
|
551
|
+
switch (level) {
|
|
552
|
+
case "INFO": return "cyan";
|
|
553
|
+
case "WARN": return "ansi:209";
|
|
554
|
+
case "ERROR": return "red";
|
|
555
|
+
case "DEBUG": return "magenta";
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function mergeEffects(effects) {
|
|
559
|
+
return effects.reduce((merged, effect) => ({
|
|
560
|
+
...merged,
|
|
561
|
+
color: effect.color ?? merged.color,
|
|
562
|
+
bold: merged.bold || effect.bold,
|
|
563
|
+
italic: merged.italic || effect.italic,
|
|
564
|
+
underline: merged.underline || effect.underline,
|
|
565
|
+
dim: merged.dim || effect.dim,
|
|
566
|
+
strikethrough: merged.strikethrough || effect.strikethrough,
|
|
567
|
+
inverse: merged.inverse || effect.inverse,
|
|
568
|
+
blink: merged.blink || effect.blink,
|
|
569
|
+
rapid_blink: merged.rapid_blink || effect.rapid_blink,
|
|
570
|
+
hidden: merged.hidden || effect.hidden,
|
|
571
|
+
overline: merged.overline || effect.overline,
|
|
572
|
+
double_underline: merged.double_underline || effect.double_underline,
|
|
573
|
+
framed: merged.framed || effect.framed,
|
|
574
|
+
encircled: merged.encircled || effect.encircled,
|
|
575
|
+
}), {
|
|
576
|
+
start: effects[0].start,
|
|
577
|
+
end: effects[effects.length - 1].end,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
function applyEffect(text, style) {
|
|
581
|
+
const codes = [];
|
|
582
|
+
if (style.bold)
|
|
583
|
+
codes.push("1");
|
|
584
|
+
if (style.dim)
|
|
585
|
+
codes.push("2");
|
|
586
|
+
if (style.italic)
|
|
587
|
+
codes.push("3");
|
|
588
|
+
if (style.underline)
|
|
589
|
+
codes.push("4");
|
|
590
|
+
if (style.inverse)
|
|
591
|
+
codes.push("7");
|
|
592
|
+
if (style.blink)
|
|
593
|
+
codes.push("5");
|
|
594
|
+
if (style.rapid_blink)
|
|
595
|
+
codes.push("6");
|
|
596
|
+
if (style.hidden)
|
|
597
|
+
codes.push("8");
|
|
598
|
+
if (style.strikethrough)
|
|
599
|
+
codes.push("9");
|
|
600
|
+
if (style.framed)
|
|
601
|
+
codes.push("51");
|
|
602
|
+
if (style.encircled)
|
|
603
|
+
codes.push("52");
|
|
604
|
+
if (style.overline)
|
|
605
|
+
codes.push("53");
|
|
606
|
+
if (style.double_underline)
|
|
607
|
+
codes.push("21");
|
|
608
|
+
if (style.color !== undefined) {
|
|
609
|
+
const named = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, gray: 90 };
|
|
610
|
+
const code = named[style.color.toLowerCase()];
|
|
611
|
+
if (code !== undefined)
|
|
612
|
+
codes.push(String(code));
|
|
613
|
+
else {
|
|
614
|
+
const ansi = /^ansi:(\d+)$/.exec(style.color);
|
|
615
|
+
if (ansi !== null) {
|
|
616
|
+
codes.push(`38;5;${ansi[1]}`);
|
|
617
|
+
return codes.length === 0 ? text : `\x1b[${codes.join(";")}m${text}\x1b[0m`;
|
|
618
|
+
}
|
|
619
|
+
const match = /^#([0-9a-f]{6})$/i.exec(style.color);
|
|
620
|
+
if (match !== null) {
|
|
621
|
+
const hex = match[1];
|
|
622
|
+
codes.push(`38;2;${Number.parseInt(hex.slice(0, 2), 16)};${Number.parseInt(hex.slice(2, 4), 16)};${Number.parseInt(hex.slice(4, 6), 16)}`);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return codes.length === 0 ? text : `\x1b[${codes.join(";")}m${text}\x1b[0m`;
|
|
627
|
+
}
|
|
628
|
+
export function green(text) {
|
|
629
|
+
return {
|
|
630
|
+
text,
|
|
631
|
+
textEffects: [{ start: 0, end: text.length, color: "green" }],
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
;
|
|
635
|
+
export function red(text) {
|
|
636
|
+
return {
|
|
637
|
+
text,
|
|
638
|
+
textEffects: [{ start: 0, end: text.length, color: "red" }]
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
;
|
|
642
|
+
export function bold(text) {
|
|
643
|
+
return {
|
|
644
|
+
text,
|
|
645
|
+
textEffects: [{ start: 0, end: text.length, bold: true }]
|
|
646
|
+
};
|
|
647
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { TextEffect } from "../logging.js";
|
|
2
|
+
export interface PullResultSegment {
|
|
3
|
+
readonly text: string;
|
|
4
|
+
readonly textEffects?: readonly TextEffect[];
|
|
5
|
+
}
|
|
6
|
+
export type PullResultLine = string | readonly PullResultSegment[];
|
|
7
|
+
export type ManagedOutcome = "success" | "warning" | "failure";
|
|
8
|
+
export interface ManagedResultStatus {
|
|
9
|
+
readonly operation: string;
|
|
10
|
+
readonly outcome: ManagedOutcome;
|
|
11
|
+
}
|
|
12
|
+
export interface ManagedResultVerdict {
|
|
13
|
+
readonly failed?: boolean;
|
|
14
|
+
readonly skipped?: number;
|
|
15
|
+
readonly gaps?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function managedResultStatus(operation: string, verdict: ManagedResultVerdict): ManagedResultStatus;
|
|
18
|
+
/**
|
|
19
|
+
* Pull commands are pleasant for a person on a terminal and remain stable for
|
|
20
|
+
* scripts: interactive stdout gets a concise user summary, while a pipe or
|
|
21
|
+
* redirect gets the complete machine-readable result object.
|
|
22
|
+
*/
|
|
23
|
+
export declare function writePullResult(result: unknown, humanLines: readonly PullResultLine[], status?: ManagedResultStatus): void;
|
|
24
|
+
export declare function countSummary(entries: readonly (readonly [singular: string, count: number, plural?: string])[]): string;
|