@dmsdc-ai/aigentry-telepty 0.1.70 → 0.1.71

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.
Files changed (2) hide show
  1. package/cli.js +34 -12
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -726,10 +726,19 @@ async function main() {
726
726
  const promptPattern = PROMPT_PATTERNS[cmdBase] || /[❯>$#%]\s*$/;
727
727
  let promptReady = false; // wait for CLI prompt before accepting inject
728
728
  const injectQueue = [];
729
+ let lastUserInputTime = 0; // timestamp of last user keystroke
730
+ const IDLE_THRESHOLD = 2000; // ms after last user input to consider idle
731
+
732
+ function isIdle() {
733
+ return promptReady && (Date.now() - lastUserInputTime > IDLE_THRESHOLD);
734
+ }
729
735
 
730
736
  let queueFlushTimer = null;
737
+ let idleCheckTimer = null;
738
+
731
739
  function flushInjectQueue() {
732
740
  if (queueFlushTimer) { clearTimeout(queueFlushTimer); queueFlushTimer = null; }
741
+ if (idleCheckTimer) { clearInterval(idleCheckTimer); idleCheckTimer = null; }
733
742
  if (injectQueue.length === 0) return;
734
743
  const batch = injectQueue.splice(0);
735
744
  let delay = 0;
@@ -739,14 +748,23 @@ async function main() {
739
748
  }
740
749
  promptReady = false;
741
750
  }
742
- function scheduleQueueFlush() {
743
- if (queueFlushTimer) return;
744
- queueFlushTimer = setTimeout(() => {
745
- queueFlushTimer = null;
746
- if (injectQueue.length > 0) {
751
+ function scheduleIdleFlush() {
752
+ if (idleCheckTimer) return;
753
+ // Poll every 500ms for idle state
754
+ idleCheckTimer = setInterval(() => {
755
+ if (isIdle() && injectQueue.length > 0) {
747
756
  flushInjectQueue();
748
757
  }
749
- }, 15000);
758
+ }, 500);
759
+ // Safety: flush after 15s regardless (prevent stuck queue)
760
+ if (!queueFlushTimer) {
761
+ queueFlushTimer = setTimeout(() => {
762
+ queueFlushTimer = null;
763
+ if (injectQueue.length > 0) {
764
+ flushInjectQueue();
765
+ }
766
+ }, 15000);
767
+ }
750
768
  }
751
769
 
752
770
  // Connect to daemon WebSocket with auto-reconnect
@@ -806,15 +824,18 @@ async function main() {
806
824
  injectQueue.push(msg.data);
807
825
  if (queueFlushTimer) { clearTimeout(queueFlushTimer); queueFlushTimer = null; }
808
826
  flushInjectQueue();
809
- } else if (isCr || promptReady) {
827
+ } else if (isCr && isIdle()) {
828
+ // CR when idle — write immediately
829
+ child.write(msg.data);
830
+ } else if (!isCr && isIdle()) {
831
+ // Text when idle — write immediately
810
832
  child.write(msg.data);
811
- if (!isCr && msg.data.length > 1) {
812
- promptReady = false;
813
- lastInjectTextTime = Date.now();
814
- }
833
+ promptReady = false;
834
+ lastInjectTextTime = Date.now();
815
835
  } else {
836
+ // Not idle (user typing or CLI busy) — queue for safe delivery
816
837
  injectQueue.push(msg.data);
817
- scheduleQueueFlush();
838
+ scheduleIdleFlush();
818
839
  }
819
840
  } else if (msg.type === 'resize') {
820
841
  child.resize(msg.cols, msg.rows);
@@ -855,6 +876,7 @@ async function main() {
855
876
 
856
877
  const cleanupTerminal = attachInteractiveTerminal(process.stdin, process.stdout, {
857
878
  onData: (data) => {
879
+ lastUserInputTime = Date.now();
858
880
  child.write(data.toString());
859
881
  },
860
882
  onResize: () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dmsdc-ai/aigentry-telepty",
3
- "version": "0.1.70",
3
+ "version": "0.1.71",
4
4
  "main": "daemon.js",
5
5
  "bin": {
6
6
  "aigentry-telepty": "install.js",