@noldova/terminal 0.0.5 → 0.0.6

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.5",
3
+ "version": "0.0.6",
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.5",
3
+ "version": "0.0.6",
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",
@@ -36,8 +36,8 @@
36
36
  "registry": "https://registry.npmjs.org/"
37
37
  },
38
38
  "dependencies": {
39
- "@noldova/terminal.protocol": "0.0.5",
40
- "@noldova/terminal.window": "0.0.5",
39
+ "@noldova/terminal.protocol": "0.0.6",
40
+ "@noldova/terminal.window": "0.0.6",
41
41
  "@xterm/addon-serialize": "0.14.0",
42
42
  "@xterm/headless": "6.0.0",
43
43
  "node-pty": "1.2.0-beta.15"
@@ -45,8 +45,8 @@
45
45
  "peerDependencies": {
46
46
  "@noldova/desktop.protocol": ">=0.0.11 <0.1.0",
47
47
  "@noldova/desktop.runtime": ">=0.0.11 <0.1.0",
48
- "@noldova/context.exceptions": ">=0.0.10 <0.1.0",
49
- "@noldova/context.json": ">=0.0.7 <0.1.0"
48
+ "@noldova/context.exceptions": ">=0.0.12 <0.1.0",
49
+ "@noldova/context.json": ">=0.0.12 <0.1.0"
50
50
  },
51
- "gitHead": "82ac135f2ac5abd327cf7fbf6b8556d4d129df47"
51
+ "gitHead": "f57fcc215b0be7cabefe6bb1a45a5299aa5c87b6"
52
52
  }
@@ -10,6 +10,9 @@ import { TerminalOpening } from "./terminal-opening.js";
10
10
  export class HostedTerminal {
11
11
  opening;
12
12
  pendingInterrupt = null;
13
+ countsFolderReports = true;
14
+ isFolderReportHeld = false;
15
+ heldFolderReport = null;
13
16
  session;
14
17
  shell;
15
18
  folder;
@@ -54,6 +57,21 @@ export class HostedTerminal {
54
57
  this.environment = environment;
55
58
  this.opening = new TerminalOpening(owner, connection);
56
59
  }
60
+ keepFolderReports(isOwnShell, isOwnFolder) {
61
+ this.countsFolderReports = isOwnShell;
62
+ this.isFolderReportHeld = !isOwnFolder;
63
+ }
64
+ countsFolderReport(cwd) {
65
+ if (!this.countsFolderReports)
66
+ return false;
67
+ if (this.isFolderReportHeld) {
68
+ this.heldFolderReport ??= cwd;
69
+ if (cwd === this.heldFolderReport)
70
+ return false;
71
+ this.isFolderReportHeld = false;
72
+ }
73
+ return true;
74
+ }
57
75
  get hasRecentInput() {
58
76
  return Date.now() - this.inputTime < Resources.echoSettleMilliseconds;
59
77
  }
package/src/resources.js CHANGED
@@ -521,6 +521,11 @@ export class Resources {
521
521
  static loginArguments = ["-l", "--login", "-login"];
522
522
  static powerShellArguments = ["-NoExit", "-EncodedCommand"];
523
523
  static powerShellIntegrationArguments = ["-NoProfile", ...Resources.powerShellArguments];
524
+ static powerShellLoginParameter = "-login";
525
+ static powerShellLoginShell = "/bin/zsh";
526
+ static powerShellLoginShellArguments = ["--emulate", "sh", "-l", "-c", "exec \"$0\" \"$@\""];
527
+ static powerShellLoginCheckedVariable = "__PWSH_LOGIN_CHECKED";
528
+ static powerShellLoginCheckedValue = "1";
524
529
  static environmentReadArguments = ["-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand"];
525
530
  static formatPowerShellLookupFailed(count, message) {
526
531
  return `PowerShell's lookup of ${count} ${count === 1 ? "name" : "names"} a command line runs gave up, so they count as found, as later ones do until one succeeds: ${message}`;
@@ -739,6 +744,9 @@ exit 0
739
744
  static formatStartedCommandUnread(sessionId, message) {
740
745
  return `The command started in terminal ${sessionId} could not be kept: ${message}`;
741
746
  }
747
+ static formatResizeUnasked(sessionId) {
748
+ 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.`;
749
+ }
742
750
  static formatLateShellLeftRunning(pid) {
743
751
  return `A shell that started as the terminals ended${pid === null ? "" : `, process ${pid}`}, or a process it started, did not end in time.`;
744
752
  }
@@ -14,6 +14,8 @@ export class PseudoTerminal {
14
14
  hasExited = false;
15
15
  isEnding = false;
16
16
  openRequests = 0;
17
+ isResizeAsking = false;
18
+ hasOutput = false;
17
19
  outputEnd = "";
18
20
  answerLimit = undefined;
19
21
  constructor(process, listener, isConPty = false) {
@@ -21,6 +23,7 @@ export class PseudoTerminal {
21
23
  this.listener = listener;
22
24
  this.isConPty = isConPty;
23
25
  this.process.onData(t => {
26
+ this.hasOutput = true;
24
27
  if (this.isConPty)
25
28
  this.watchCursorRequests(t);
26
29
  this.listener.onData(t);
@@ -55,8 +58,14 @@ export class PseudoTerminal {
55
58
  this.releaseKeys();
56
59
  }
57
60
  resize(size) {
58
- if (!this.hasExited)
59
- this.process.resize(size.columns, size.rows);
61
+ if (this.hasExited)
62
+ return;
63
+ this.process.resize(size.columns, size.rows);
64
+ if (!this.isConPty || !this.hasOutput || this.isResizeAsking)
65
+ return;
66
+ this.isResizeAsking = true;
67
+ this.openRequests++;
68
+ this.limitAnswers();
60
69
  }
61
70
  async readForegroundProcessAsync() {
62
71
  return this.hasExited ? null : await this.process.readForegroundProcessAsync();
@@ -80,14 +89,24 @@ export class PseudoTerminal {
80
89
  this.outputEnd = text.slice(1 - Resources.cursorRequest.length);
81
90
  if (requests === 0)
82
91
  return;
92
+ this.limitAnswers();
93
+ this.openRequests += this.isResizeAsking ? requests - 1 : requests;
94
+ this.isResizeAsking = false;
95
+ }
96
+ limitAnswers() {
83
97
  clearTimeout(this.answerLimit);
84
- this.answerLimit = setTimeout(() => this.releaseKeys(), Resources.cursorAnswerMilliseconds);
85
- this.openRequests += requests;
98
+ this.answerLimit = setTimeout(() => this.releaseUnanswered(), Resources.cursorAnswerMilliseconds);
99
+ }
100
+ releaseUnanswered() {
101
+ if (this.isResizeAsking)
102
+ this.listener.onResizeUnasked?.();
103
+ this.releaseKeys();
86
104
  }
87
105
  releaseKeys() {
88
106
  clearTimeout(this.answerLimit);
89
107
  this.answerLimit = undefined;
90
108
  this.openRequests = 0;
109
+ this.isResizeAsking = false;
91
110
  for (const key of this.heldKeys.splice(0))
92
111
  this.write(key);
93
112
  }
@@ -30,40 +30,9 @@ export class ShellIntegration {
30
30
  this.platform = platform;
31
31
  }
32
32
  async prepareAsync(shell, nonce, environment, note = null) {
33
- const family = ShellIntegration.toFamily(shell.file);
34
- if (family === ShellFamily.CommandPrompt)
35
- return ShellIntegration.toCommandPromptLaunch(shell, note);
36
- if (family === ShellFamily.Other)
37
- return new ShellLaunch(shell.file, shell.args, {}, null);
38
- if (family === ShellFamily.PowerShell)
39
- await this.makeSharedMemoryFolderAsync(environment);
40
- const folder = await this.writeScriptsOrNullAsync();
41
- const nonceFile = folder === null ? null : await this.writeNonceOrNullAsync(folder, nonce);
42
- if (folder === null || nonceFile === null)
43
- return ShellIntegration.toPlainLaunch(shell, family);
44
- const noteVariables = note === null ? {} : { [Resources.noteVariable]: note };
45
- if (family === ShellFamily.PowerShell)
46
- return new ShellLaunch(shell.file, [...shell.args, ...Resources.powerShellIntegrationArguments, ShellIntegration.encode([Resources.shellIntegrationPowerShellNonce, Resources.shellIntegrationPowerShellNote, Resources.shellIntegrationPowerShellProfiles, shell.startCommand,
47
- Resources.shellIntegrationPowerShellScript].filter(t => t !== null).join(Resources.lineFeed))], {
48
- [Resources.nonceFileVariable]: nonceFile,
49
- ...noteVariables
50
- }, nonce, nonceFile, note !== null);
51
- if (family === ShellFamily.Bash) {
52
- const bashScript = path.join(folder, Resources.bashScriptFile).replaceAll(Resources.windowsSeparator, Resources.posixSeparator);
53
- const isLogin = shell.args.some(t => Resources.loginArguments.includes(t));
54
- return new ShellLaunch(shell.file, [Resources.initFileArgument, bashScript, Resources.interactiveArgument], {
55
- [Resources.nonceFileVariable]: nonceFile.replaceAll(Resources.windowsSeparator, Resources.posixSeparator),
56
- ...(isLogin ? { [Resources.loginVariable]: Resources.shellIntegrationLoginValue } : {}),
57
- ...noteVariables
58
- }, nonce, nonceFile, note !== null);
59
- }
60
- const userFolder = environment[Resources.zdotdirVariable];
61
- return new ShellLaunch(shell.file, shell.args, {
62
- [Resources.nonceFileVariable]: nonceFile,
63
- [Resources.zdotdirVariable]: folder,
64
- ...(userFolder === undefined ? {} : { [Resources.userZdotdirVariable]: userFolder }),
65
- ...noteVariables
66
- }, nonce, nonceFile, note !== null);
33
+ const launch = await this.prepareShellAsync(shell, nonce, environment, note);
34
+ return this.platform === Resources.macPlatform && ShellIntegration.toFamily(shell.file) === ShellFamily.PowerShell
35
+ ? ShellIntegration.toPowerShellLoginLaunch(launch) : launch;
67
36
  }
68
37
  watch(process, launch) {
69
38
  const { nonce, nonceFile } = launch;
@@ -113,6 +82,42 @@ export class ShellIntegration {
113
82
  this.log.write(Resources.formatSuggestionsUnwatched(FailureText.read(error)));
114
83
  }
115
84
  }
85
+ async prepareShellAsync(shell, nonce, environment, note) {
86
+ const family = ShellIntegration.toFamily(shell.file);
87
+ if (family === ShellFamily.CommandPrompt)
88
+ return ShellIntegration.toCommandPromptLaunch(shell, note);
89
+ if (family === ShellFamily.Other)
90
+ return new ShellLaunch(shell.file, shell.args, {}, null);
91
+ if (family === ShellFamily.PowerShell)
92
+ await this.makeSharedMemoryFolderAsync(environment);
93
+ const folder = await this.writeScriptsOrNullAsync();
94
+ const nonceFile = folder === null ? null : await this.writeNonceOrNullAsync(folder, nonce);
95
+ if (folder === null || nonceFile === null)
96
+ return ShellIntegration.toPlainLaunch(shell, family);
97
+ const noteVariables = note === null ? {} : { [Resources.noteVariable]: note };
98
+ if (family === ShellFamily.PowerShell)
99
+ return new ShellLaunch(shell.file, [...shell.args, ...Resources.powerShellIntegrationArguments, ShellIntegration.encode([Resources.shellIntegrationPowerShellNonce, Resources.shellIntegrationPowerShellNote, Resources.shellIntegrationPowerShellProfiles, shell.startCommand,
100
+ Resources.shellIntegrationPowerShellScript].filter(t => t !== null).join(Resources.lineFeed))], {
101
+ [Resources.nonceFileVariable]: nonceFile,
102
+ ...noteVariables
103
+ }, nonce, nonceFile, note !== null);
104
+ if (family === ShellFamily.Bash) {
105
+ const bashScript = path.join(folder, Resources.bashScriptFile).replaceAll(Resources.windowsSeparator, Resources.posixSeparator);
106
+ const isLogin = shell.args.some(t => Resources.loginArguments.includes(t));
107
+ return new ShellLaunch(shell.file, [Resources.initFileArgument, bashScript, Resources.interactiveArgument], {
108
+ [Resources.nonceFileVariable]: nonceFile.replaceAll(Resources.windowsSeparator, Resources.posixSeparator),
109
+ ...(isLogin ? { [Resources.loginVariable]: Resources.shellIntegrationLoginValue } : {}),
110
+ ...noteVariables
111
+ }, nonce, nonceFile, note !== null);
112
+ }
113
+ const userFolder = environment[Resources.zdotdirVariable];
114
+ return new ShellLaunch(shell.file, shell.args, {
115
+ [Resources.nonceFileVariable]: nonceFile,
116
+ [Resources.zdotdirVariable]: folder,
117
+ ...(userFolder === undefined ? {} : { [Resources.userZdotdirVariable]: userFolder }),
118
+ ...noteVariables
119
+ }, nonce, nonceFile, note !== null);
120
+ }
116
121
  makeSharedMemoryFolderAsync(environment) {
117
122
  const runtimeFolder = environment[Resources.runtimeFolderVariable];
118
123
  if (this.platform !== Resources.linuxPlatform || runtimeFolder === undefined || !path.posix.isAbsolute(runtimeFolder))
@@ -185,6 +190,15 @@ export class ShellIntegration {
185
190
  return new ShellLaunch(shell.file, shell.args, {}, null);
186
191
  return new ShellLaunch(shell.file, [...shell.args, ...Resources.powerShellArguments, ShellIntegration.encode(shell.startCommand)], {}, null);
187
192
  }
193
+ static toPowerShellLoginLaunch(launch) {
194
+ const [first, ...rest] = [launch.args].flat();
195
+ if (first === undefined || !ShellIntegration.isPowerShellLogin(first))
196
+ return launch;
197
+ return new ShellLaunch(Resources.powerShellLoginShell, [...Resources.powerShellLoginShellArguments, launch.file, ...rest], { ...launch.environment, [Resources.powerShellLoginCheckedVariable]: Resources.powerShellLoginCheckedValue }, launch.nonce, launch.nonceFile, launch.printsNote);
198
+ }
199
+ static isPowerShellLogin(argument) {
200
+ return argument.length > 1 && Resources.powerShellLoginParameter.startsWith(argument.toLowerCase());
201
+ }
188
202
  static toFamily(file) {
189
203
  const name = path.win32.parse(file).name.toLowerCase();
190
204
  switch (name) {
@@ -176,8 +176,8 @@ export class TerminalHost {
176
176
  findTerminal(sessionId) {
177
177
  return this.sessions.get(sessionId);
178
178
  }
179
- openTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit = ProtocolResources.defaultStoredLimit, note = null, isPanelTerminal = false, owner = null, client = null, sessionId = randomUUID(), environment = null, startsAtFit = false, opensTab = true) {
180
- return this.trackAsync(this.startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab), new TerminalOpening(owner, client?.connection ?? null));
179
+ openTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit = ProtocolResources.defaultStoredLimit, note = null, isPanelTerminal = false, owner = null, client = null, sessionId = randomUUID(), environment = null, startsAtFit = false, opensTab = true, kept = null) {
180
+ return this.trackAsync(this.startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab, kept), new TerminalOpening(owner, client?.connection ?? null));
181
181
  }
182
182
  bindAgentSession(agentSessionId, owner) {
183
183
  this.agentSessionOwners.set(agentSessionId, owner);
@@ -257,7 +257,7 @@ export class TerminalHost {
257
257
  }
258
258
  await this.disposeAsync(terminal.session);
259
259
  }
260
- async startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab) {
260
+ async startTerminalAsync(shellId, folder, size, ownerSessionId, storedLimit, note, isPanelTerminal, owner, client, sessionId, environment, startsAtFit, opensTab, kept) {
261
261
  const shell = await this.locator.findAsync(shellId);
262
262
  this.ensureRunning();
263
263
  if (shell === undefined)
@@ -286,7 +286,7 @@ export class TerminalHost {
286
286
  },
287
287
  onTitleChanged: t => publish(this.events.titleChanged, t.toJson()),
288
288
  onCwdChanged: t => {
289
- if (isKept)
289
+ if (isKept && terminal.countsFolderReport(t.cwd))
290
290
  this.saved.moveTo(t.sessionId, t.cwd);
291
291
  publish(this.events.cwdChanged, t.toJson());
292
292
  },
@@ -302,8 +302,11 @@ export class TerminalHost {
302
302
  },
303
303
  onCommandStarted: t => this.addCommand(shell.id, t),
304
304
  onDetached: t => publish(this.events.detached, t.toJson()),
305
- onFailed: (id, message) => this.log.write(Resources.formatStartedCommandUnread(id, message))
305
+ onFailed: (id, message) => this.log.write(Resources.formatStartedCommandUnread(id, message)),
306
+ onResizeUnasked: t => this.log.write(Resources.formatResizeUnasked(t))
306
307
  }, Resources.maximumAgentTextLength, shell.kind, ownerSessionId === null), shell, folder, ownerSessionId, isPanelTerminal, owner, client?.connection ?? null, environment);
308
+ if (kept !== null)
309
+ terminal.keepFolderReports(kept.shellId === shell.id, kept.folder === folder);
307
310
  terminal.deferredShell = started.deferred;
308
311
  terminal.startTemporaryFolder = started.temporaryFolder;
309
312
  for (const line of started.printsNote ? [] : note?.split(Resources.lineBreaks) ?? [])
@@ -311,7 +314,7 @@ export class TerminalHost {
311
314
  terminal.opensTab = opensTab;
312
315
  this.sessions.set(sessionId, terminal);
313
316
  if (isKept)
314
- this.saved.save(new SavedTerminal(sessionId, shell.id, folder, isPanelTerminal, owner, opensTab));
317
+ this.saved.save(kept ?? new SavedTerminal(sessionId, shell.id, folder, isPanelTerminal, owner, opensTab));
315
318
  else if (ownerSessionId !== null && owner !== null)
316
319
  this.audience.publish(this.events.opened, terminal, terminal.session.toSummary(folder, true, isPanelTerminal, owner, true, null, shell.name).toJson());
317
320
  return terminal;
@@ -391,7 +394,7 @@ export class TerminalHost {
391
394
  throw new ShellKindNotFoundException(params.shellKinds);
392
395
  shellId = shell.id;
393
396
  }
394
- const terminal = await this.startTerminalAsync(shellId, folder, params.size, null, params.storedLimit, params.notice, false, null, client, randomUUID(), params.environment, false, params.opensTab);
397
+ const terminal = await this.startTerminalAsync(shellId, folder, params.size, null, params.storedLimit, params.notice, false, null, client, randomUUID(), params.environment, false, params.opensTab, null);
395
398
  if (!params.opensTab)
396
399
  terminal.launcher = client;
397
400
  if (params.command !== null)
@@ -747,7 +750,7 @@ export class TerminalHost {
747
750
  const note = hasShell
748
751
  ? hasFolder ? null : Resources.formatStartFolderNote(saved.folder)
749
752
  : hasFolder ? Resources.formatRestoredShellNote(saved.shellId) : Resources.formatRestoredShellAndFolderNote(saved.shellId, saved.folder);
750
- const terminal = await this.openTerminalAsync(hasShell ? saved.shellId : fallback, hasFolder ? saved.folder : this.homeFolder, params.size, null, params.storedLimit, note, saved.isPanelTerminal, saved.owner, client, saved.sessionId, null, false, saved.opensTab);
753
+ const terminal = await this.openTerminalAsync(hasShell ? saved.shellId : fallback, hasFolder ? saved.folder : this.homeFolder, params.size, null, params.storedLimit, note, saved.isPanelTerminal, saved.owner, client, saved.sessionId, null, false, saved.opensTab, saved);
751
754
  const name = saved.name;
752
755
  terminal.name = name !== null && name.trim() !== "" && name.length <= ProtocolResources.maximumTitleLength ? name : null;
753
756
  if (!hasShell)
@@ -354,7 +354,8 @@ export class TerminalSession {
354
354
  onExit: t => {
355
355
  if (this.terminal === terminal)
356
356
  this.end(t);
357
- }
357
+ },
358
+ onResizeUnasked: () => this.listener.onResizeUnasked(this.sessionId)
358
359
  }, this.windowsPty !== null);
359
360
  return terminal;
360
361
  }