@noldova/terminal 0.0.6 → 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.6",
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.6",
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.6",
40
- "@noldova/terminal.window": "0.0.6",
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",
46
+ "@noldova/desktop.protocol": ">=0.0.13 <0.1.0",
47
+ "@noldova/desktop.runtime": ">=0.0.13 <0.1.0",
48
48
  "@noldova/context.exceptions": ">=0.0.12 <0.1.0",
49
49
  "@noldova/context.json": ">=0.0.12 <0.1.0"
50
50
  },
51
- "gitHead": "f57fcc215b0be7cabefe6bb1a45a5299aa5c87b6"
51
+ "gitHead": "c47cd5c1e4ab96359a68fcdf36f6e80c246e81b4"
52
52
  }
@@ -28,6 +28,7 @@ export class HostedTerminal {
28
28
  keyCount = 0;
29
29
  inputReceivedLength = null;
30
30
  inputTime = 0;
31
+ typedTime = 0;
31
32
  inputPrompt = null;
32
33
  inputCommandMarks = 0;
33
34
  restarts = Promise.resolve();
@@ -75,6 +76,9 @@ export class HostedTerminal {
75
76
  get hasRecentInput() {
76
77
  return Date.now() - this.inputTime < Resources.echoSettleMilliseconds;
77
78
  }
79
+ get hasRecentTyping() {
80
+ return Date.now() - this.typedTime < Resources.echoSettleMilliseconds;
81
+ }
78
82
  get hasUnreadInput() {
79
83
  const commands = this.session.commands;
80
84
  const prompt = commands.promptIdentity;
@@ -89,16 +93,16 @@ export class HostedTerminal {
89
93
  return this.ownerSessionId === null && !this.session.hasExited && !this.isRestarting && this.pendingCommand === null
90
94
  && this.session.commands.isDrawingPrompt;
91
95
  }
92
- recordInput(isReply = false, data = "") {
96
+ recordInput(isReply = false, data = "", isAgent = false) {
93
97
  this.inputReceivedLength = this.session.receivedLength;
94
98
  if (isReply)
95
99
  return;
96
100
  const commands = this.session.commands;
97
- this.record(commands.isWaitingAtPrompt && !HostedTerminal.hasTextAfterLineBreak(data) ? commands.promptIdentity : null);
101
+ this.record(commands.isWaitingAtPrompt && !HostedTerminal.hasTextAfterLineBreak(data) ? commands.promptIdentity : null, isAgent);
98
102
  }
99
103
  recordRunInput() {
100
104
  this.inputReceivedLength = this.session.receivedLength;
101
- this.record(this.typedEnds === 1 ? this.session.commands.promptIdentity : null);
105
+ this.record(this.typedEnds === 1 ? this.session.commands.promptIdentity : null, false);
102
106
  }
103
107
  addRun(runId, startLine) {
104
108
  this.runs.set(runId, startLine);
@@ -126,9 +130,11 @@ export class HostedTerminal {
126
130
  isReachedBy(connection, isApp) {
127
131
  return this.opening.isReachedBy(connection, isApp);
128
132
  }
129
- record(prompt) {
133
+ record(prompt, isAgent) {
130
134
  this.inputCount++;
131
135
  this.inputTime = Date.now();
136
+ if (!isAgent)
137
+ this.typedTime = this.inputTime;
132
138
  this.inputPrompt = prompt;
133
139
  this.inputCommandMarks = this.session.commands.commandMarks;
134
140
  }
@@ -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)
@@ -39,6 +39,9 @@ export class PseudoTerminal {
39
39
  get pid() {
40
40
  return this.process.pid;
41
41
  }
42
+ get isHoldingKeys() {
43
+ return this.openRequests > 0;
44
+ }
42
45
  write(data) {
43
46
  if (this.hasExited)
44
47
  return;
@@ -107,6 +110,8 @@ export class PseudoTerminal {
107
110
  this.answerLimit = undefined;
108
111
  this.openRequests = 0;
109
112
  this.isResizeAsking = false;
113
+ if (this.heldKeys.length > 0)
114
+ this.listener.onReleasingKeys?.();
110
115
  for (const key of this.heldKeys.splice(0))
111
116
  this.write(key);
112
117
  }
@@ -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;
@@ -355,7 +354,11 @@ export class TerminalSession {
355
354
  if (this.terminal === terminal)
356
355
  this.end(t);
357
356
  },
358
- onResizeUnasked: () => this.listener.onResizeUnasked(this.sessionId)
357
+ onResizeUnasked: () => this.listener.onResizeUnasked(this.sessionId),
358
+ onReleasingKeys: () => {
359
+ if (this.terminal === terminal)
360
+ this.wakeup?.beforeInput();
361
+ }
359
362
  }, this.windowsPty !== null);
360
363
  return terminal;
361
364
  }
@@ -374,6 +377,10 @@ export class TerminalSession {
374
377
  this.terminal.resize(this.size);
375
378
  this.watchResize();
376
379
  }
380
+ beforeInput() {
381
+ if (!this.terminal.isHoldingKeys)
382
+ this.wakeup?.beforeInput();
383
+ }
377
384
  watchResize() {
378
385
  if (this.commands.isWaitingAtPrompt)
379
386
  this.wakeup?.watch();
@@ -386,6 +393,9 @@ export class TerminalSession {
386
393
  this.listener.onCommandStarted(TerminalSession.toCommand(command) ?? t);
387
394
  }).catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
388
395
  }
396
+ readInputEdge(mark) {
397
+ return this.marksInput ? TerminalSession.toInputEdge(mark.kind) : null;
398
+ }
389
399
  static toCommand(value) {
390
400
  if (value?.startsWith(Resources.commandProperty) !== true)
391
401
  return null;