@noldova/terminal 0.0.5 → 0.0.7

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,12 +1,12 @@
1
1
  {
2
2
  "id": "terminal",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
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"],
7
7
  "pseudoTerminals": true,
8
8
  "dependencies": {},
9
- "noldovaDesktop": ">=0.0.11 <0.1.0",
9
+ "noldovaDesktop": ">=0.0.13 <0.1.0",
10
10
  "contributes": {
11
11
  "methods": [
12
12
  "terminal.shells", "terminal.open", "terminal.close", "terminal.restart", "terminal.sessions", "terminal.attach", "terminal.input", "terminal.resize",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noldova/terminal",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
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,17 +36,17 @@
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.7",
40
+ "@noldova/terminal.window": "0.0.7",
41
41
  "@xterm/addon-serialize": "0.14.0",
42
42
  "@xterm/headless": "6.0.0",
43
43
  "node-pty": "1.2.0-beta.15"
44
44
  },
45
45
  "peerDependencies": {
46
- "@noldova/desktop.protocol": ">=0.0.11 <0.1.0",
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"
46
+ "@noldova/desktop.protocol": ">=0.0.13 <0.1.0",
47
+ "@noldova/desktop.runtime": ">=0.0.13 <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": "c47cd5c1e4ab96359a68fcdf36f6e80c246e81b4"
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;
@@ -25,6 +28,7 @@ export class HostedTerminal {
25
28
  keyCount = 0;
26
29
  inputReceivedLength = null;
27
30
  inputTime = 0;
31
+ typedTime = 0;
28
32
  inputPrompt = null;
29
33
  inputCommandMarks = 0;
30
34
  restarts = Promise.resolve();
@@ -54,9 +58,27 @@ export class HostedTerminal {
54
58
  this.environment = environment;
55
59
  this.opening = new TerminalOpening(owner, connection);
56
60
  }
61
+ keepFolderReports(isOwnShell, isOwnFolder) {
62
+ this.countsFolderReports = isOwnShell;
63
+ this.isFolderReportHeld = !isOwnFolder;
64
+ }
65
+ countsFolderReport(cwd) {
66
+ if (!this.countsFolderReports)
67
+ return false;
68
+ if (this.isFolderReportHeld) {
69
+ this.heldFolderReport ??= cwd;
70
+ if (cwd === this.heldFolderReport)
71
+ return false;
72
+ this.isFolderReportHeld = false;
73
+ }
74
+ return true;
75
+ }
57
76
  get hasRecentInput() {
58
77
  return Date.now() - this.inputTime < Resources.echoSettleMilliseconds;
59
78
  }
79
+ get hasRecentTyping() {
80
+ return Date.now() - this.typedTime < Resources.echoSettleMilliseconds;
81
+ }
60
82
  get hasUnreadInput() {
61
83
  const commands = this.session.commands;
62
84
  const prompt = commands.promptIdentity;
@@ -71,16 +93,16 @@ export class HostedTerminal {
71
93
  return this.ownerSessionId === null && !this.session.hasExited && !this.isRestarting && this.pendingCommand === null
72
94
  && this.session.commands.isDrawingPrompt;
73
95
  }
74
- recordInput(isReply = false, data = "") {
96
+ recordInput(isReply = false, data = "", isAgent = false) {
75
97
  this.inputReceivedLength = this.session.receivedLength;
76
98
  if (isReply)
77
99
  return;
78
100
  const commands = this.session.commands;
79
- this.record(commands.isWaitingAtPrompt && !HostedTerminal.hasTextAfterLineBreak(data) ? commands.promptIdentity : null);
101
+ this.record(commands.isWaitingAtPrompt && !HostedTerminal.hasTextAfterLineBreak(data) ? commands.promptIdentity : null, isAgent);
80
102
  }
81
103
  recordRunInput() {
82
104
  this.inputReceivedLength = this.session.receivedLength;
83
- this.record(this.typedEnds === 1 ? this.session.commands.promptIdentity : null);
105
+ this.record(this.typedEnds === 1 ? this.session.commands.promptIdentity : null, false);
84
106
  }
85
107
  addRun(runId, startLine) {
86
108
  this.runs.set(runId, startLine);
@@ -108,9 +130,11 @@ export class HostedTerminal {
108
130
  isReachedBy(connection, isApp) {
109
131
  return this.opening.isReachedBy(connection, isApp);
110
132
  }
111
- record(prompt) {
133
+ record(prompt, isAgent) {
112
134
  this.inputCount++;
113
135
  this.inputTime = Date.now();
136
+ if (!isAgent)
137
+ this.typedTime = this.inputTime;
114
138
  this.inputPrompt = prompt;
115
139
  this.inputCommandMarks = this.session.commands.commandMarks;
116
140
  }
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
  }
@@ -69,7 +69,7 @@ export class AgentTerminal {
69
69
  return this.isRunning && !this.session.commands.isAtPrompt && this.session.text.isAwaitingSecret;
70
70
  }
71
71
  get hasPendingLine() {
72
- return this.hosted.hasRecentInput || (this.hasShellIntegration && (this.session.readPromptLine() ?? "").length > 0);
72
+ return this.hosted.hasRecentTyping || (this.hasShellIntegration && (this.session.readPromptLine() ?? "").length > 0);
73
73
  }
74
74
  get hasUnechoedInput() {
75
75
  const received = this.hosted.inputReceivedLength;
@@ -125,8 +125,10 @@ export class AgentTerminal {
125
125
  send(data) {
126
126
  if (this.oneShot?.isRunning === true)
127
127
  this.oneShot.write(data);
128
- else
128
+ else {
129
+ this.hosted.recordInput(false, data, true);
129
130
  this.session.write(data);
131
+ }
130
132
  }
131
133
  openLogFile() {
132
134
  if (this.logFile === undefined) {
@@ -5,6 +5,7 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import { addAbortListener } from "node:events";
8
9
  import path from "node:path";
9
10
  import { ProcessRequest, ProcessStartException } from "@noldova/desktop.runtime";
10
11
  import { CommandExitedException } from "../exceptions/command-exited.exception.js";
@@ -28,11 +29,11 @@ export class CommandRunner {
28
29
  const answer = Promise.withResolvers();
29
30
  const late = setTimeout(() => answer.reject(new CommandTimedOutException(name, timeoutMilliseconds)), timeoutMilliseconds);
30
31
  const ending = setTimeout(() => stopping.abort(), Math.max(timeoutMilliseconds, endMilliseconds));
31
- const stopTimers = () => {
32
+ const ended = this.readAsync(name, request, stopping, timeoutMilliseconds, () => clearTimeout(late)).then(answer.resolve, answer.reject)
33
+ .finally(() => {
32
34
  clearTimeout(late);
33
35
  clearTimeout(ending);
34
- };
35
- const ended = this.readAsync(name, request, stopping, timeoutMilliseconds, stopTimers).then(answer.resolve, answer.reject).finally(stopTimers);
36
+ });
36
37
  return new CommandRun(answer.promise, ended);
37
38
  }
38
39
  async readAsync(name, request, stopping, timeoutMilliseconds, exited) {
@@ -47,6 +48,10 @@ export class CommandRunner {
47
48
  throw error;
48
49
  throw new CommandNotStartedException(name, error);
49
50
  }
51
+ addAbortListener(stopping.signal, () => {
52
+ process.output.destroy();
53
+ process.errors.destroy();
54
+ });
50
55
  let isTooLong = false;
51
56
  let output = "";
52
57
  process.output.setEncoding(Resources.utf8Encoding);
@@ -64,7 +69,8 @@ export class CommandRunner {
64
69
  });
65
70
  const closed = new Promise(resolve => process.output.once(Resources.closeEvent, () => resolve()));
66
71
  const errorsClosed = new Promise(resolve => process.errors.once(Resources.closeEvent, () => resolve()));
67
- const [exit] = await Promise.all([process.exited.finally(exited), closed, errorsClosed]);
72
+ const exit = await process.exited.finally(exited);
73
+ await Promise.all([closed, errorsClosed]);
68
74
  if (isTooLong)
69
75
  throw new CommandOutputTooLongException(name, Resources.maximumCommandOutputLength);
70
76
  if (stopping.signal.aborted)
@@ -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);
@@ -36,6 +39,9 @@ export class PseudoTerminal {
36
39
  get pid() {
37
40
  return this.process.pid;
38
41
  }
42
+ get isHoldingKeys() {
43
+ return this.openRequests > 0;
44
+ }
39
45
  write(data) {
40
46
  if (this.hasExited)
41
47
  return;
@@ -55,8 +61,14 @@ export class PseudoTerminal {
55
61
  this.releaseKeys();
56
62
  }
57
63
  resize(size) {
58
- if (!this.hasExited)
59
- this.process.resize(size.columns, size.rows);
64
+ if (this.hasExited)
65
+ return;
66
+ this.process.resize(size.columns, size.rows);
67
+ if (!this.isConPty || !this.hasOutput || this.isResizeAsking)
68
+ return;
69
+ this.isResizeAsking = true;
70
+ this.openRequests++;
71
+ this.limitAnswers();
60
72
  }
61
73
  async readForegroundProcessAsync() {
62
74
  return this.hasExited ? null : await this.process.readForegroundProcessAsync();
@@ -80,14 +92,26 @@ export class PseudoTerminal {
80
92
  this.outputEnd = text.slice(1 - Resources.cursorRequest.length);
81
93
  if (requests === 0)
82
94
  return;
95
+ this.limitAnswers();
96
+ this.openRequests += this.isResizeAsking ? requests - 1 : requests;
97
+ this.isResizeAsking = false;
98
+ }
99
+ limitAnswers() {
83
100
  clearTimeout(this.answerLimit);
84
- this.answerLimit = setTimeout(() => this.releaseKeys(), Resources.cursorAnswerMilliseconds);
85
- this.openRequests += requests;
101
+ this.answerLimit = setTimeout(() => this.releaseUnanswered(), Resources.cursorAnswerMilliseconds);
102
+ }
103
+ releaseUnanswered() {
104
+ if (this.isResizeAsking)
105
+ this.listener.onResizeUnasked?.();
106
+ this.releaseKeys();
86
107
  }
87
108
  releaseKeys() {
88
109
  clearTimeout(this.answerLimit);
89
110
  this.answerLimit = undefined;
90
111
  this.openRequests = 0;
112
+ this.isResizeAsking = false;
113
+ if (this.heldKeys.length > 0)
114
+ this.listener.onReleasingKeys?.();
91
115
  for (const key of this.heldKeys.splice(0))
92
116
  this.write(key);
93
117
  }
@@ -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)
@@ -136,7 +136,7 @@ export class TerminalSession {
136
136
  write(data, isKey = true) {
137
137
  if (isKey)
138
138
  this.options.cancel();
139
- this.wakeup?.beforeInput();
139
+ this.beforeInput();
140
140
  for (let lines = data.replace(Resources.bracketedPastes, "").split(Resources.lineBreaks).length - 1; lines > 0; lines--)
141
141
  this.commands.enterLine();
142
142
  this.terminal.write(data);
@@ -161,7 +161,7 @@ export class TerminalSession {
161
161
  return ShellOptionsReply.none;
162
162
  const reply = this.options.askAsync(this.hasAnsweredOptions ? Resources.optionsMilliseconds
163
163
  : Resources.firstOptionsMilliseconds);
164
- this.wakeup?.beforeInput();
164
+ this.beforeInput();
165
165
  this.terminal.write(keys);
166
166
  const answered = await reply;
167
167
  if (answered.isTimedOut)
@@ -258,19 +258,18 @@ export class TerminalSession {
258
258
  }
259
259
  receive(data) {
260
260
  const marks = this.scanner?.scan(data, this.receivedOffset) ?? [];
261
- const edges = marks.map(t => this.marksInput ? TerminalSession.toInputEdge(t.kind) : null);
262
- this.batcher.append(data, marks.flatMap((t, index) => {
263
- const edge = edges[index] ?? null;
261
+ this.batcher.append(data, marks.flatMap(t => {
262
+ const edge = this.readInputEdge(t);
264
263
  return edge === null ? [] : [new TerminalInputMark(t.offset - this.receivedOffset, edge)];
265
264
  }));
266
265
  this.flow.produce(data.length);
267
266
  let position = 0;
268
267
  let mirrored = 0;
269
- for (const [index, mark] of marks.entries()) {
268
+ for (const mark of marks) {
270
269
  const end = mark.offset - this.receivedOffset;
271
270
  this.text.append(data.slice(position, end));
272
271
  position = end;
273
- const edge = edges[index] ?? null;
272
+ const edge = this.readInputEdge(mark);
274
273
  if (mark.kind === MarkKind.PromptStart || edge !== null) {
275
274
  this.mirror.write(data.slice(mirrored, end));
276
275
  mirrored = end;
@@ -354,6 +353,11 @@ export class TerminalSession {
354
353
  onExit: t => {
355
354
  if (this.terminal === terminal)
356
355
  this.end(t);
356
+ },
357
+ onResizeUnasked: () => this.listener.onResizeUnasked(this.sessionId),
358
+ onReleasingKeys: () => {
359
+ if (this.terminal === terminal)
360
+ this.wakeup?.beforeInput();
357
361
  }
358
362
  }, this.windowsPty !== null);
359
363
  return terminal;
@@ -373,6 +377,10 @@ export class TerminalSession {
373
377
  this.terminal.resize(this.size);
374
378
  this.watchResize();
375
379
  }
380
+ beforeInput() {
381
+ if (!this.terminal.isHoldingKeys)
382
+ this.wakeup?.beforeInput();
383
+ }
376
384
  watchResize() {
377
385
  if (this.commands.isWaitingAtPrompt)
378
386
  this.wakeup?.watch();
@@ -385,6 +393,9 @@ export class TerminalSession {
385
393
  this.listener.onCommandStarted(TerminalSession.toCommand(command) ?? t);
386
394
  }).catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
387
395
  }
396
+ readInputEdge(mark) {
397
+ return this.marksInput ? TerminalSession.toInputEdge(mark.kind) : null;
398
+ }
388
399
  static toCommand(value) {
389
400
  if (value?.startsWith(Resources.commandProperty) !== true)
390
401
  return null;