@noldova/terminal 0.0.17 → 0.0.18

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/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "terminal",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "displayName": "Terminal",
5
5
  "description": "Runs shells in terminal panes, and lets AI agents run commands in them through permission requests.",
6
6
  "parts": ["runtime", "window"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noldova/terminal",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "The terminal module for apps on Noldova Desktop: terminal panes over real shells, and agent tools that run commands through permission requests.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,8 +37,8 @@
37
37
  "registry": "https://registry.npmjs.org/"
38
38
  },
39
39
  "dependencies": {
40
- "@noldova/terminal.protocol": "0.0.17",
41
- "@noldova/terminal.window": "0.0.17",
40
+ "@noldova/terminal.protocol": "0.0.18",
41
+ "@noldova/terminal.window": "0.0.18",
42
42
  "@xterm/addon-serialize": "0.14.0",
43
43
  "@xterm/headless": "6.0.0",
44
44
  "node-pty": "1.2.0-beta.15"
@@ -50,5 +50,5 @@
50
50
  "@noldova/context.exceptions": ">=0.0.20 <0.1.0",
51
51
  "@noldova/context.json": ">=0.0.20 <0.1.0"
52
52
  },
53
- "gitHead": "ab9f326bb1734fd375be8d93dc51d15953aea4bc"
53
+ "gitHead": "1a1346cfe20c4335e58fccc58eb2c7b5889fcc91"
54
54
  }
package/src/resources.js CHANGED
@@ -25,6 +25,7 @@ export class Resources {
25
25
  static runInterruptWaitMilliseconds = 2_000;
26
26
  static promptDrawWaitMilliseconds = 10_000;
27
27
  static foregroundPollMilliseconds = 50;
28
+ static foregroundWorkPollMilliseconds = 1_000;
28
29
  static cursorAnswerMilliseconds = 500;
29
30
  static cursorRequestFrameMilliseconds = 100;
30
31
  static maximumKeptRuns = 100;
@@ -405,6 +406,10 @@ export class Resources {
405
406
  static zshNoStartupArgument = "-f";
406
407
  static describeShellProgram = "/bin/sh";
407
408
  static bashStartupVariable = "BASH_ENV";
409
+ static formatRunningCommand(command, terminal) {
410
+ const line = (command ?? String.empty).split(Resources.lineBreaks).find(t => t.trim() !== String.empty)?.trim();
411
+ return Object.isUndefined(line) ? `A command is running in ${terminal}.` : `${line} is running in ${terminal}.`;
412
+ }
408
413
  static formatDescribeFailed(count, message) {
409
414
  return `The lookup of ${count} ${count === 1 ? "command's description" : "commands' descriptions"} gave up, so they show none: ${message}`;
410
415
  }
@@ -941,6 +946,9 @@ exit 0
941
946
  static formatStartedCommandUnread(sessionId, message) {
942
947
  return `The command started in terminal ${sessionId} could not be kept: ${message}`;
943
948
  }
949
+ static formatForegroundUnread(sessionId, message) {
950
+ return `The program running in terminal ${sessionId} could not be read: ${message}`;
951
+ }
944
952
  static formatResizeUnasked(sessionId) {
945
953
  return `ConPTY sent terminal ${sessionId} no cursor request within ${String(Resources.cursorAnswerMilliseconds)} ms of a resize, so the keys typed meanwhile went on without one.`;
946
954
  }
@@ -57,7 +57,7 @@ export class RuntimePart {
57
57
  historyFolder.start();
58
58
  try {
59
59
  const events = new TerminalEvents(context.declareEvent(TerminalNames.outputEvent), context.declareEvent(TerminalNames.exitedEvent), context.declareEvent(TerminalNames.restartedEvent), context.declareEvent(TerminalNames.titleChangedEvent), context.declareEvent(TerminalNames.cwdChangedEvent), context.declareEvent(TerminalNames.detachedEvent), context.declareEvent(TerminalNames.closedEvent), context.declareEvent(TerminalNames.openedEvent), context.declareEvent(TerminalNames.launchCommandEndedEvent), context.declareEvent(TerminalNames.runOpenedEvent), context.declareEvent(TerminalNames.runEndedEvent), context.declareEvent(TerminalNames.renamedEvent), context.declareEvent(TerminalNames.commandAddedEvent), context.declareEvent(TerminalNames.commandStartedEvent), context.declareEvent(TerminalNames.outputStartedEvent), context.declareEvent(TerminalNames.outputPrintedEvent), context.declareEvent(TerminalNames.commandEndedEvent), context.declareEvent(TerminalNames.promptStartedEvent), context.declareEvent(TerminalNames.screenChangedEvent));
60
- const host = new TerminalHost(new ContextPtySpawner(context), reader, new ShellIntegration(writer, tmpdir(), context.log, access, context.settings, process.platform), historyFolder, new ShellLocator(process.platform, process.arch, process.env, ShellLocator.readUserShell(), reader, runner, context.log), process.platform, release(), new LatestEnvironment(environment, process.env), homedir(), context.log, events, new SavedTerminals(context.database, context.log), new CompletionSource(reader, runner, process.platform, homedir(), context.log), new CommandDescriber(runner, context.log));
60
+ const host = new TerminalHost(new ContextPtySpawner(context), reader, new ShellIntegration(writer, tmpdir(), context.log, access, context.settings, process.platform), historyFolder, new ShellLocator(process.platform, process.arch, process.env, ShellLocator.readUserShell(), reader, runner, context.log), process.platform, release(), new LatestEnvironment(environment, process.env), homedir(), context.log, events, new SavedTerminals(context.database, context.log), new CompletionSource(reader, runner, process.platform, homedir(), context.log), new CommandDescriber(runner, context.log), context);
61
61
  for (const name of TerminalNames.methods)
62
62
  context.registerMethod(name, { handleAsync: t => host.handleAsync(name, t.payload, RequestClient.fromContext(t)) });
63
63
  context.onClientClosed(t => host.closeConnectionAsync(t.connection, t.window));
@@ -49,6 +49,10 @@ export class TerminalHost {
49
49
  saved;
50
50
  completions;
51
51
  describer;
52
+ context;
53
+ commandWork = new Map();
54
+ foregroundWork = new Map();
55
+ foregroundWatches = new Map();
52
56
  folderKeys = new WeakMap();
53
57
  realFolderReads = new Map();
54
58
  sessions = new Map();
@@ -64,7 +68,7 @@ export class TerminalHost {
64
68
  appCloses = new Set();
65
69
  closings = new Map();
66
70
  closingConnections = new Set();
67
- constructor(spawner, fileSystem, integration, historyFolder, locator, platform, osRelease, environment, homeFolder, log, events, saved, completions, describer) {
71
+ constructor(spawner, fileSystem, integration, historyFolder, locator, platform, osRelease, environment, homeFolder, log, events, saved, completions, describer, context) {
68
72
  this.spawner = spawner;
69
73
  this.fileSystem = fileSystem;
70
74
  this.integration = integration;
@@ -84,6 +88,7 @@ export class TerminalHost {
84
88
  this.saved = saved;
85
89
  this.completions = completions;
86
90
  this.describer = describer;
91
+ this.context = context;
87
92
  }
88
93
  async handleAsync(method, payload, client) {
89
94
  this.audience.see(client);
@@ -289,6 +294,7 @@ export class TerminalHost {
289
294
  const terminal = new HostedTerminal(new TerminalSession(sessionId, shell.id, size, started.process, started.nonce, this.windowsPty, historyPaths, storedLimit, {
290
295
  onOutput: t => publish(this.events.output, t.toJson()),
291
296
  onExited: t => {
297
+ this.endCommandWork(t.sessionId);
292
298
  this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
293
299
  if (terminal.isEndSent)
294
300
  return;
@@ -306,11 +312,13 @@ export class TerminalHost {
306
312
  publish(this.events.cwdChanged, t.toJson());
307
313
  },
308
314
  onCommandEnded: t => {
315
+ this.endCommandWork(t.sessionId);
309
316
  this.endLaunchCommand(terminal, t.exitCode);
310
317
  this.countRunEnd(terminal, t.exitCode, true);
311
318
  publish(this.events.commandEnded, t.toJson());
312
319
  },
313
320
  onPromptStarted: t => {
321
+ this.endCommandWork(t.sessionId);
314
322
  const offset = terminal.launchCommandOffset;
315
323
  if (!Object.isNull(offset) && terminal.session.text.readRange(offset, terminal.session.text.endOffset).includes(Resources.lineFeed))
316
324
  this.endLaunchCommand(terminal, null);
@@ -318,10 +326,15 @@ export class TerminalHost {
318
326
  publish(this.events.promptStarted, t.toJson());
319
327
  },
320
328
  onCommandStarted: t => {
329
+ if (this.commandWork.has(t.sessionId))
330
+ this.beginCommandWork(terminal, t.command);
321
331
  publish(this.events.commandStarted, t.toJson());
322
332
  this.addCommand(shell.id, t.command);
323
333
  },
324
- onOutputStarted: t => publish(this.events.outputStarted, t.toJson()),
334
+ onOutputStarted: t => {
335
+ this.beginCommandWork(terminal, t.command);
336
+ publish(this.events.outputStarted, t.toJson());
337
+ },
325
338
  onOutputPrinted: t => publish(this.events.outputPrinted, t.toJson()),
326
339
  onScreenChanged: t => publish(this.events.screenChanged, t.toJson()),
327
340
  onDetached: t => publish(this.events.detached, t.toJson()),
@@ -336,6 +349,7 @@ export class TerminalHost {
336
349
  terminal.session.writeNote(line);
337
350
  terminal.opensTab = opensTab;
338
351
  this.sessions.set(sessionId, terminal);
352
+ this.watchForeground(terminal);
339
353
  if (isKept)
340
354
  this.saved.save(kept ?? new SavedTerminal(sessionId, shell.id, folder, isPanelTerminal, owner, opensTab));
341
355
  else if (!Object.isNull(ownerSessionId) && !Object.isNull(owner))
@@ -970,6 +984,7 @@ export class TerminalHost {
970
984
  throw new SessionNotFoundException(sessionId);
971
985
  }
972
986
  session.restart(started.process, started.nonce, size);
987
+ this.watchForeground(terminal);
973
988
  terminal.startTemporaryFolder = started.temporaryFolder;
974
989
  terminal.clearRuns();
975
990
  terminal.heldExit = null;
@@ -1161,9 +1176,74 @@ export class TerminalHost {
1161
1176
  return hasEnded;
1162
1177
  }
1163
1178
  async disposeAsync(session) {
1179
+ this.stopForegroundWatch(session.sessionId);
1180
+ this.endCommandWork(session.sessionId);
1164
1181
  for (const failure of await session.disposeAsync())
1165
1182
  this.log.write(Resources.formatStoredLinesRemovalFailed(session.sessionId, FailureText.read(failure)));
1166
1183
  }
1184
+ beginCommandWork(terminal, command) {
1185
+ this.beginWork(terminal.session.sessionId, Resources.formatRunningCommand(command, terminal.name ?? terminal.shell.name));
1186
+ }
1187
+ beginWork(sessionId, description) {
1188
+ const previous = this.commandWork.get(sessionId);
1189
+ this.commandWork.set(sessionId, this.context.beginWork(description));
1190
+ previous?.[Symbol.dispose]();
1191
+ }
1192
+ endCommandWork(sessionId) {
1193
+ const item = this.commandWork.get(sessionId);
1194
+ this.commandWork.delete(sessionId);
1195
+ this.foregroundWork.delete(sessionId);
1196
+ item?.[Symbol.dispose]();
1197
+ }
1198
+ watchForeground(terminal) {
1199
+ const sessionId = terminal.session.sessionId;
1200
+ if (!Object.isNull(this.windowsPty) || this.foregroundWatches.has(sessionId))
1201
+ return;
1202
+ const startedAt = Date.now();
1203
+ const schedule = () => {
1204
+ const timer = setTimeout(() => void this.readForegroundWorkAsync(terminal, startedAt).then(t => {
1205
+ if (this.foregroundWatches.get(sessionId) !== timer)
1206
+ return;
1207
+ if (t)
1208
+ schedule();
1209
+ else
1210
+ this.stopForegroundWatch(sessionId);
1211
+ }), Resources.foregroundWorkPollMilliseconds);
1212
+ this.foregroundWatches.set(sessionId, timer);
1213
+ };
1214
+ schedule();
1215
+ }
1216
+ stopForegroundWatch(sessionId) {
1217
+ clearTimeout(this.foregroundWatches.get(sessionId));
1218
+ this.foregroundWatches.delete(sessionId);
1219
+ }
1220
+ async readForegroundWorkAsync(terminal, startedAt) {
1221
+ const session = terminal.session;
1222
+ if (session.hasExited || session.commands.hasMarks)
1223
+ return false;
1224
+ if (session.hasIntegration && Date.now() - startedAt < Resources.integrationWaitMilliseconds)
1225
+ return true;
1226
+ let foreground;
1227
+ try {
1228
+ foreground = await session.readForegroundProcessAsync();
1229
+ }
1230
+ catch (error) {
1231
+ this.log.write(Resources.formatForegroundUnread(session.sessionId, FailureText.read(error)));
1232
+ return true;
1233
+ }
1234
+ if (!this.foregroundWatches.has(session.sessionId))
1235
+ return false;
1236
+ if (Object.isNull(foreground) || foreground.group === session.pid || foreground.isTerminalProgram) {
1237
+ this.endCommandWork(session.sessionId);
1238
+ return true;
1239
+ }
1240
+ const description = Resources.formatRunningCommand(foreground.commandLine ?? foreground.program, terminal.name ?? (session.title === String.empty ? terminal.shell.name : session.title));
1241
+ if (this.foregroundWork.get(session.sessionId) !== description) {
1242
+ this.beginWork(session.sessionId, description);
1243
+ this.foregroundWork.set(session.sessionId, description);
1244
+ }
1245
+ return true;
1246
+ }
1167
1247
  ensureRunning() {
1168
1248
  if (this.hasEnded)
1169
1249
  throw new HostEndedException();