@innovatorved/code-lab 0.3.1 → 0.4.0

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 (171) hide show
  1. package/ARCHITECTURE.md +8 -4
  2. package/CHANGELOG.md +45 -0
  3. package/README.md +70 -158
  4. package/dist/apps/web/src/index.d.ts +34 -0
  5. package/dist/code-lab-sql.css +482 -0
  6. package/dist/code-lab-sql.d.ts +179 -0
  7. package/dist/code-lab-sql.esm.js +542 -0
  8. package/dist/code-lab-sql.umd.js +531 -0
  9. package/dist/code-lab.css +298 -2
  10. package/dist/code-lab.d.ts +110 -15
  11. package/dist/code-lab.esm.js +35284 -18253
  12. package/dist/code-lab.js +40434 -23431
  13. package/dist/code-lab.umd.js +65502 -0
  14. package/dist/diagnostics.d.ts +5 -0
  15. package/dist/extensions/cpp.esm.js +19532 -3772
  16. package/dist/extensions/cpp.js +19529 -3769
  17. package/dist/extensions/guided-lab.esm.js +19401 -3725
  18. package/dist/extensions/guided-lab.js +19400 -3724
  19. package/dist/extensions/python.d.ts +1 -0
  20. package/dist/extensions/python.esm.js +19666 -3843
  21. package/dist/extensions/python.js +19668 -3845
  22. package/dist/extensions/umbrella.d.ts +5 -0
  23. package/dist/index.d.ts +1 -1
  24. package/dist/modular-kernel.d.ts +11 -0
  25. package/dist/packages/commands/src/index.d.ts +96 -0
  26. package/dist/packages/core/src/di.d.ts +36 -0
  27. package/dist/packages/core/src/disposable.d.ts +27 -0
  28. package/dist/packages/core/src/errors.d.ts +69 -0
  29. package/dist/packages/core/src/events.d.ts +38 -0
  30. package/dist/packages/core/src/index.d.ts +6 -0
  31. package/dist/packages/core/src/path.d.ts +14 -0
  32. package/dist/packages/core/src/registry.d.ts +23 -0
  33. package/dist/packages/editor/src/index.d.ts +118 -0
  34. package/dist/packages/fs/src/backends.d.ts +37 -0
  35. package/dist/packages/fs/src/index.d.ts +3 -0
  36. package/dist/packages/fs/src/types.d.ts +40 -0
  37. package/dist/packages/fs/src/vfs-service.d.ts +33 -0
  38. package/dist/packages/preview/src/index.d.ts +34 -0
  39. package/dist/packages/runtime/src/index.d.ts +47 -0
  40. package/dist/packages/settings/src/builtin-schemas.d.ts +15 -0
  41. package/dist/packages/settings/src/configuration-service.d.ts +100 -0
  42. package/dist/packages/settings/src/index.d.ts +4 -0
  43. package/dist/packages/settings/src/jsonc.d.ts +20 -0
  44. package/dist/packages/settings/src/schema.d.ts +28 -0
  45. package/dist/packages/sql/src/completions.d.ts +8 -0
  46. package/dist/packages/sql/src/diagnostics.d.ts +2 -0
  47. package/dist/packages/sql/src/formatter.d.ts +2 -0
  48. package/dist/packages/sql/src/index.d.ts +7 -0
  49. package/dist/packages/sql/src/sql-editor.d.ts +2 -0
  50. package/dist/packages/sql/src/styles.d.ts +2 -0
  51. package/dist/packages/sql/src/tokenizer.d.ts +13 -0
  52. package/dist/packages/sql/src/types.d.ts +130 -0
  53. package/dist/packages/terminal/src/index.d.ts +54 -0
  54. package/dist/packages/ui/src/index.d.ts +4 -0
  55. package/dist/packages/ui/src/observability.d.ts +22 -0
  56. package/dist/packages/ui/src/plugin-registry.d.ts +89 -0
  57. package/dist/packages/ui/src/settings-dialog.d.ts +25 -0
  58. package/dist/packages/ui/src/themes.d.ts +35 -0
  59. package/dist/preview-sw.js +82 -24
  60. package/dist/runtime.d.ts +3 -3
  61. package/dist/runtime.esm.js +503 -73
  62. package/dist/runtime.js +501 -73
  63. package/dist/schemas.d.ts +234 -814
  64. package/dist/schemas.esm.js +19401 -3725
  65. package/dist/schemas.js +19404 -3728
  66. package/dist/scm.d.ts +13 -2
  67. package/dist/search.d.ts +5 -1
  68. package/dist/shell.d.ts +1 -1
  69. package/dist/shell.esm.js +435 -54
  70. package/dist/shell.js +435 -54
  71. package/dist/terminal.esm.js +242 -54
  72. package/dist/terminal.js +242 -54
  73. package/dist/workbench/diff-editor.d.ts +1 -0
  74. package/dist/workbench/index.d.ts +1 -1
  75. package/dist/workbench/styles.d.ts +1 -1
  76. package/dist/zip.d.ts +4 -3
  77. package/package.json +37 -8
  78. package/packages/commands/README.md +7 -0
  79. package/packages/commands/package.json +16 -0
  80. package/packages/commands/src/index.ts +467 -0
  81. package/packages/commands/tsconfig.json +22 -0
  82. package/packages/core/README.md +12 -0
  83. package/packages/core/package.json +13 -0
  84. package/packages/core/src/di.ts +147 -0
  85. package/packages/core/src/disposable.ts +112 -0
  86. package/packages/core/src/errors.ts +205 -0
  87. package/packages/core/src/events.ts +157 -0
  88. package/packages/core/src/index.ts +49 -0
  89. package/packages/core/src/path.ts +118 -0
  90. package/packages/core/src/registry.ts +61 -0
  91. package/packages/core/tsconfig.json +19 -0
  92. package/packages/editor/README.md +9 -0
  93. package/packages/editor/package.json +18 -0
  94. package/packages/editor/src/index.ts +344 -0
  95. package/packages/editor/tsconfig.json +24 -0
  96. package/packages/fs/README.md +7 -0
  97. package/packages/fs/package.json +16 -0
  98. package/packages/fs/src/backends.ts +301 -0
  99. package/packages/fs/src/index.ts +18 -0
  100. package/packages/fs/src/types.ts +44 -0
  101. package/packages/fs/src/vfs-service.ts +257 -0
  102. package/packages/fs/tsconfig.json +22 -0
  103. package/packages/preview/README.md +3 -0
  104. package/packages/preview/package.json +16 -0
  105. package/packages/preview/src/index.ts +125 -0
  106. package/packages/preview/tsconfig.json +22 -0
  107. package/packages/runtime/README.md +3 -0
  108. package/packages/runtime/package.json +17 -0
  109. package/packages/runtime/src/index.ts +134 -0
  110. package/packages/runtime/tsconfig.json +23 -0
  111. package/packages/settings/README.md +7 -0
  112. package/packages/settings/package.json +16 -0
  113. package/packages/settings/src/builtin-schemas.ts +235 -0
  114. package/packages/settings/src/configuration-service.ts +543 -0
  115. package/packages/settings/src/index.ts +37 -0
  116. package/packages/settings/src/jsonc.ts +189 -0
  117. package/packages/settings/src/schema.ts +164 -0
  118. package/packages/settings/tsconfig.json +22 -0
  119. package/packages/sql/README.md +12 -0
  120. package/packages/sql/package.json +13 -0
  121. package/packages/sql/src/completions.ts +359 -0
  122. package/packages/sql/src/diagnostics.ts +200 -0
  123. package/packages/sql/src/formatter.ts +271 -0
  124. package/packages/sql/src/index.ts +54 -0
  125. package/packages/sql/src/sql-editor.ts +802 -0
  126. package/packages/sql/src/styles.ts +501 -0
  127. package/packages/sql/src/tokenizer.ts +457 -0
  128. package/packages/sql/src/types.ts +148 -0
  129. package/packages/sql/tsconfig.json +19 -0
  130. package/packages/terminal/README.md +3 -0
  131. package/packages/terminal/package.json +16 -0
  132. package/packages/terminal/src/index.ts +183 -0
  133. package/packages/terminal/tsconfig.json +22 -0
  134. package/packages/ui/README.md +8 -0
  135. package/packages/ui/package.json +18 -0
  136. package/packages/ui/src/index.ts +36 -0
  137. package/packages/ui/src/observability.ts +175 -0
  138. package/packages/ui/src/plugin-registry.ts +274 -0
  139. package/packages/ui/src/settings-dialog.ts +293 -0
  140. package/packages/ui/src/themes.ts +174 -0
  141. package/packages/ui/tsconfig.json +24 -0
  142. package/src/code-lab.css +298 -2
  143. package/src/csp.ts +7 -10
  144. package/src/diagnostics.ts +42 -3
  145. package/src/extensions/cpp.ts +105 -24
  146. package/src/extensions/python.ts +260 -116
  147. package/src/extensions/umbrella.ts +5 -0
  148. package/src/index.ts +1 -1
  149. package/src/modular-kernel.ts +11 -0
  150. package/src/preview-sw.js +73 -12
  151. package/src/runtime.ts +501 -74
  152. package/src/schemas.ts +62 -62
  153. package/src/scm.ts +106 -19
  154. package/src/search.ts +71 -27
  155. package/src/shell.ts +433 -53
  156. package/src/terminal.ts +239 -52
  157. package/src/workbench/diff-editor.ts +106 -18
  158. package/src/workbench/drawer.ts +17 -4
  159. package/src/workbench/guided-pane.ts +12 -2
  160. package/src/workbench/icons.ts +5 -2
  161. package/src/workbench/share.ts +37 -8
  162. package/src/workbench/styles.ts +2 -1
  163. package/src/workbench/toolbar.ts +15 -4
  164. package/src/workbench.ts +2877 -284
  165. package/src/zip.ts +29 -7
  166. package/dist/runtime/index.d.ts +0 -4
  167. package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
  168. package/dist/shell/index.d.ts +0 -4
  169. package/src/runtime/index.ts +0 -5
  170. package/src/shell/coreutils/compilers-cli.ts +0 -103
  171. package/src/shell/index.ts +0 -5
package/src/terminal.ts CHANGED
@@ -303,6 +303,14 @@
303
303
 
304
304
  const parseAnsiToHtml = ansiToHtml;
305
305
 
306
+ const MAX_SCROLLBACK = 2000;
307
+ const VALID_LOG_TYPES = new Set(['stdout', 'stderr', 'system', 'info', 'warn', 'error', 'success']);
308
+
309
+ function sanitizeLogType(type) {
310
+ const t = String(type || 'stdout').trim();
311
+ return VALID_LOG_TYPES.has(t) ? t : 'stdout';
312
+ }
313
+
306
314
  class PTYTerminalEngine {
307
315
  constructor(container, options = {}) {
308
316
  if (
@@ -317,6 +325,12 @@
317
325
  }
318
326
  this.container = container || null;
319
327
  this.options = options || {};
328
+ this.maxScrollback = typeof this.options.maxScrollback === 'number' && this.options.maxScrollback > 0
329
+ ? this.options.maxScrollback
330
+ : MAX_SCROLLBACK;
331
+ this.maxHistory = typeof this.options.maxHistory === 'number' && this.options.maxHistory > 0
332
+ ? this.options.maxHistory
333
+ : 1000;
320
334
  this.logs = Array.isArray(this.options.logs) ? this.options.logs : [];
321
335
  this.cwd = this.options.cwd || '/workspace';
322
336
  this.replMode = Boolean(this.options.replMode);
@@ -330,6 +344,7 @@
330
344
  this.searchQuery = '';
331
345
  this.searchMatchIdx = -1;
332
346
  this._readLineQueue = [];
347
+ this.isBusy = false;
333
348
  this.onCommand = typeof this.options.onCommand === 'function' ? this.options.onCommand : null;
334
349
  this.getCompletions = typeof this.options.getCompletions === 'function' ? this.options.getCompletions : null;
335
350
  this.onKey = typeof this.options.onKey === 'function' ? this.options.onKey : null;
@@ -480,13 +495,40 @@
480
495
  this.writeStreamChunk(String(data) + '\n', type);
481
496
  }
482
497
 
498
+ isCommandRunning() {
499
+ return Boolean(this.isBusy && (!this._readLineQueue || this._readLineQueue.length === 0));
500
+ }
501
+
502
+ _pushLog(entry) {
503
+ const normalized = {
504
+ type: sanitizeLogType(entry && entry.type),
505
+ text: String(entry && entry.text != null ? entry.text : '')
506
+ };
507
+ this.logs.push(normalized);
508
+ if (this.logs.length > this.maxScrollback) {
509
+ const excess = this.logs.length - this.maxScrollback;
510
+ this.logs.splice(0, excess);
511
+ if (this._targetLineIdx >= 0) {
512
+ this._targetLineIdx = Math.max(0, this._targetLineIdx - excess);
513
+ }
514
+ if (this.container && typeof this.container.querySelector === 'function') {
515
+ const logEl = this.container.querySelector('#cl-terminal-log');
516
+ if (logEl && logEl.children && logEl.children.length > this.maxScrollback) {
517
+ while (logEl.children.length > this.maxScrollback && typeof logEl.removeChild === 'function' && logEl.firstChild) {
518
+ logEl.removeChild(logEl.firstChild);
519
+ }
520
+ }
521
+ }
522
+ }
523
+ return normalized;
524
+ }
525
+
483
526
  _getActiveLine(type) {
484
527
  if (this._targetLineIdx >= 0 && this._targetLineIdx < this.logs.length) {
485
528
  return this.logs[this._targetLineIdx];
486
529
  }
487
530
  if (this._activeLineCommitted || this.logs.length === 0) {
488
- const newLine = { text: '', type };
489
- this.logs.push(newLine);
531
+ const newLine = this._pushLog({ text: '', type });
490
532
  this._activeLineCommitted = false;
491
533
  this._cursorCol = 0;
492
534
  this._targetLineIdx = this.logs.length - 1;
@@ -503,7 +545,7 @@
503
545
  const logEl = this.container.querySelector('#cl-terminal-log');
504
546
  if (!logEl) return;
505
547
  const lineEl = document.createElement('div');
506
- lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
548
+ lineEl.className = 'codelab-log-line ' + sanitizeLogType(line.type);
507
549
  lineEl.innerHTML = ansiToHtml(line.text);
508
550
  logEl.appendChild(lineEl);
509
551
  this._activeLineEl = lineEl;
@@ -526,7 +568,7 @@
526
568
  }
527
569
  }
528
570
  if (lineEl) {
529
- lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
571
+ lineEl.className = 'codelab-log-line ' + sanitizeLogType(line.type);
530
572
  lineEl.innerHTML = ansiToHtml(line.text);
531
573
  }
532
574
  }
@@ -565,17 +607,75 @@
565
607
  return [...this.history];
566
608
  }
567
609
 
568
- readLine(promptText = '') {
569
- const prevPrompt = this.promptOverride;
570
- if (promptText) {
571
- this.setPrompt(promptText);
610
+ _activateNextReadLine() {
611
+ if (!this._readLineQueue || this._readLineQueue.length === 0) return;
612
+ const head = this._readLineQueue[0];
613
+ if (head && typeof head === 'object' && !head._activated) {
614
+ head._activated = true;
615
+ head.prevPrompt = this.promptOverride;
616
+ if (head.promptText) {
617
+ this.promptOverride = head.promptText;
618
+ }
619
+ }
620
+ }
621
+
622
+ _drainReadLineQueue(result = null) {
623
+ if (!Array.isArray(this._readLineQueue) || this._readLineQueue.length === 0) return;
624
+ const queued = this._readLineQueue.splice(0, this._readLineQueue.length);
625
+ let restoredPrompt = undefined;
626
+ for (const entry of queued) {
627
+ if (entry && typeof entry === 'object' && entry._activated && restoredPrompt === undefined) {
628
+ restoredPrompt = entry.prevPrompt;
629
+ }
630
+ if (entry && typeof entry.cleanup === 'function') {
631
+ entry.cleanup();
632
+ }
633
+ }
634
+ if (restoredPrompt !== undefined) {
635
+ this.promptOverride = restoredPrompt;
636
+ }
637
+ for (const entry of queued) {
638
+ if (typeof entry === 'function') {
639
+ try { entry(result); } catch (err) { _terminalLogger.debug('readLine callback threw during drain.', err); }
640
+ } else if (entry && typeof entry.resolve === 'function') {
641
+ try { entry.resolve(result); } catch (err) { _terminalLogger.debug('readLine resolve threw during drain.', err); }
642
+ }
643
+ }
644
+ }
645
+
646
+ readLine(promptText = '', options = {}) {
647
+ if (options && options.signal && options.signal.aborted) {
648
+ return Promise.resolve(null);
572
649
  }
573
650
  return new Promise((resolve) => {
574
- this._readLineQueue.push((line) => {
575
- this.promptOverride = prevPrompt;
576
- this.updateActiveLine();
577
- resolve(line);
578
- });
651
+ const item = {
652
+ promptText: String(promptText || ''),
653
+ prevPrompt: null,
654
+ _activated: false,
655
+ cleanup: null,
656
+ resolve
657
+ };
658
+ if (options && options.signal && typeof options.signal.addEventListener === 'function') {
659
+ const onAbort = () => {
660
+ const idx = this._readLineQueue.indexOf(item);
661
+ if (idx !== -1) {
662
+ this._readLineQueue.splice(idx, 1);
663
+ if (item._activated) {
664
+ this.promptOverride = item.prevPrompt;
665
+ this._activateNextReadLine();
666
+ }
667
+ this.updateActiveLine();
668
+ resolve(null);
669
+ }
670
+ };
671
+ options.signal.addEventListener('abort', onAbort, { once: true });
672
+ item.cleanup = () => {
673
+ try { options.signal.removeEventListener('abort', onAbort); } catch (err) { _terminalLogger.debug('readLine abort listener cleanup failed.', err); }
674
+ };
675
+ }
676
+ this._readLineQueue.push(item);
677
+ this._activateNextReadLine();
678
+ this.updateActiveLine();
579
679
  });
580
680
  }
581
681
 
@@ -616,6 +716,7 @@
616
716
  }
617
717
 
618
718
  destroy() {
719
+ this._drainReadLineQueue(null);
619
720
  this._cleanupListeners();
620
721
  this._mounted = false;
621
722
  }
@@ -645,6 +746,10 @@
645
746
  if (input) {
646
747
  this._addListener(input, 'keydown', (e) => this.handleKeyDown(e));
647
748
  this._addListener(input, 'input', () => {
749
+ if (this.isCommandRunning()) {
750
+ input.value = '';
751
+ return;
752
+ }
648
753
  if (input.value !== this.buffer) {
649
754
  this.buffer = input.value;
650
755
  this.cursor = typeof input.selectionStart === 'number' ? input.selectionStart : this.buffer.length;
@@ -686,6 +791,36 @@
686
791
  const key = e.key;
687
792
  const isCtrl = Boolean(e.ctrlKey || e.metaKey);
688
793
 
794
+ if (isCtrl && (key === 'l' || key === 'L')) {
795
+ if (e.preventDefault) e.preventDefault();
796
+ this.clear();
797
+ return;
798
+ }
799
+
800
+ if (isCtrl && (key === 'c' || key === 'C')) {
801
+ if (e.preventDefault) e.preventDefault();
802
+ const wasBusy = this.isBusy || (this._readLineQueue && this._readLineQueue.length > 0);
803
+ this.isBusy = false;
804
+ this._pushLog({ type: 'system', text: wasBusy ? '^C' : `${this.getPrompt()}${this.buffer}^C` });
805
+ this.buffer = '';
806
+ this.cursor = 0;
807
+ this.historyIdx = -1;
808
+ this._drainReadLineQueue(null);
809
+ if (this.replMode) {
810
+ this.replMode = false;
811
+ }
812
+ if (this.onInterrupt) {
813
+ try { this.onInterrupt(); } catch (err) { _terminalLogger.warn('Terminal onInterrupt callback threw.', err); }
814
+ }
815
+ this.render();
816
+ return;
817
+ }
818
+
819
+ if (this.isCommandRunning()) {
820
+ if (e.preventDefault) e.preventDefault();
821
+ return;
822
+ }
823
+
689
824
  if (isCtrl && (key === 'r' || key === 'R')) {
690
825
  if (e.preventDefault) e.preventDefault();
691
826
  if (!this.searchMode) {
@@ -817,33 +952,11 @@
817
952
  return;
818
953
  }
819
954
 
820
- if (isCtrl && (key === 'l' || key === 'L')) {
821
- if (e.preventDefault) e.preventDefault();
822
- this.clear();
823
- return;
824
- }
825
-
826
- if (isCtrl && (key === 'c' || key === 'C')) {
827
- if (e.preventDefault) e.preventDefault();
828
- this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}^C` });
829
- this.buffer = '';
830
- this.cursor = 0;
831
- this.historyIdx = -1;
832
- if (this.replMode) {
833
- this.replMode = false;
834
- }
835
- if (this.onInterrupt) {
836
- try { this.onInterrupt(); } catch (err) { _terminalLogger.warn('Terminal onInterrupt callback threw.', err); }
837
- }
838
- this.render();
839
- return;
840
- }
841
-
842
955
  if (isCtrl && (key === 'd' || key === 'D')) {
843
956
  if (this.buffer === '') {
844
957
  if (e.preventDefault) e.preventDefault();
845
958
  if (this.replMode) {
846
- this.logs.push({ type: 'system', text: `${this.getPrompt()}.exit` });
959
+ this._pushLog({ type: 'system', text: `${this.getPrompt()}.exit` });
847
960
  this.replMode = false;
848
961
  this.render();
849
962
  return;
@@ -937,14 +1050,44 @@
937
1050
  }
938
1051
 
939
1052
  // Print candidate matches
940
- this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}` });
941
- this.logs.push({ type: 'stdout', text: matches.join(' ') });
1053
+ this._pushLog({ type: 'system', text: `${this.getPrompt()}${this.buffer}` });
1054
+ this._pushLog({ type: 'stdout', text: matches.join(' ') });
942
1055
  this.render();
943
1056
  }
944
1057
  }
945
1058
 
946
1059
  submitLine() {
1060
+ if (this._readLineQueue && this._readLineQueue.length > 0) {
1061
+ const line = this.buffer;
1062
+ this.buffer = '';
1063
+ this.cursor = 0;
1064
+ this.historyIdx = -1;
1065
+ this.searchMode = false;
1066
+ this.searchQuery = '';
1067
+ this.searchMatchIdx = -1;
1068
+
1069
+ const entry = this._readLineQueue.shift();
1070
+ if (entry && typeof entry.cleanup === 'function') {
1071
+ entry.cleanup();
1072
+ }
1073
+ if (typeof entry === 'function') {
1074
+ this.updateActiveLine();
1075
+ entry(line);
1076
+ } else if (entry && typeof entry.resolve === 'function') {
1077
+ this.promptOverride = entry.prevPrompt;
1078
+ this._activateNextReadLine();
1079
+ this.updateActiveLine();
1080
+ entry.resolve(line);
1081
+ }
1082
+ return;
1083
+ }
1084
+
1085
+ if (this.isBusy) {
1086
+ return;
1087
+ }
1088
+
947
1089
  const line = this.buffer;
1090
+ const promptSnap = this.getPrompt();
948
1091
  this.buffer = '';
949
1092
  this.cursor = 0;
950
1093
  this.historyIdx = -1;
@@ -952,22 +1095,53 @@
952
1095
  this.searchQuery = '';
953
1096
  this.searchMatchIdx = -1;
954
1097
 
1098
+ const echoed = this._pushLog({ type: 'system', text: `${promptSnap}${line}` });
1099
+ this._activeLineCommitted = true;
1100
+ this._cursorCol = 0;
1101
+ this._targetLineIdx = -1;
1102
+ this._needsRewrite = false;
1103
+ this._appendDomLine(echoed);
1104
+ this._activeLineEl = null;
1105
+
955
1106
  if (line.trim()) {
956
1107
  if (this.history[this.history.length - 1] !== line.trim()) {
957
1108
  this.history.push(line.trim());
1109
+ const maxHist = typeof this.maxHistory === 'number' && this.maxHistory > 0 ? this.maxHistory : 1000;
1110
+ if (this.history.length > maxHist) {
1111
+ this.history.splice(0, this.history.length - maxHist);
1112
+ }
958
1113
  }
959
1114
  }
960
1115
 
961
- if (this._readLineQueue && this._readLineQueue.length > 0) {
962
- const resolver = this._readLineQueue.shift();
1116
+ if (!this.onCommand) {
963
1117
  this.updateActiveLine();
964
- resolver(line);
965
1118
  return;
966
1119
  }
967
1120
 
1121
+ this.isBusy = true;
968
1122
  this.updateActiveLine();
969
- if (this.onCommand) {
970
- this.onCommand(line);
1123
+
1124
+ let cmdResult;
1125
+ try {
1126
+ cmdResult = this.onCommand(line);
1127
+ } catch (err) {
1128
+ this.isBusy = false;
1129
+ this.updateActiveLine();
1130
+ throw err;
1131
+ }
1132
+
1133
+ if (cmdResult && typeof cmdResult.then === 'function') {
1134
+ Promise.resolve(cmdResult)
1135
+ .catch((err) => {
1136
+ _terminalLogger.debug('Terminal onCommand promise rejected.', err);
1137
+ })
1138
+ .finally(() => {
1139
+ this.isBusy = false;
1140
+ this.updateActiveLine();
1141
+ });
1142
+ } else {
1143
+ this.isBusy = false;
1144
+ this.updateActiveLine();
971
1145
  }
972
1146
  }
973
1147
 
@@ -980,6 +1154,16 @@
980
1154
  const lineEl = this.container.querySelector('#cl-pty-active-line');
981
1155
  if (!lineEl) return;
982
1156
 
1157
+ if (this.isCommandRunning()) {
1158
+ if (typeof lineEl.setAttribute === 'function') lineEl.setAttribute('data-busy', 'true');
1159
+ if (lineEl.style) lineEl.style.display = 'none';
1160
+ lineEl.innerHTML = `<span class="codelab-pty__prompt"></span><span class="codelab-pty__before"></span><span class="codelab-pty__cursor"></span><span class="codelab-pty__after"></span>`;
1161
+ return;
1162
+ }
1163
+
1164
+ if (typeof lineEl.removeAttribute === 'function') lineEl.removeAttribute('data-busy');
1165
+ if (lineEl.style) lineEl.style.display = '';
1166
+
983
1167
  const prompt = this.searchMode
984
1168
  ? `(reverse-i-search)\`${this.searchQuery}\`: `
985
1169
  : this.getPrompt();
@@ -1000,21 +1184,24 @@
1000
1184
  );
1001
1185
 
1002
1186
  const logsHtml = this.logs
1003
- .map(item => `<div class="codelab-log-line ${escapeHtml(item.type || '')}">${ansiToHtml(item.text)}</div>`)
1187
+ .map(item => `<div class="codelab-log-line ${sanitizeLogType(item.type)}">${ansiToHtml(item.text)}</div>`)
1004
1188
  .join('');
1005
1189
 
1006
- const prompt = this.searchMode
1007
- ? `(reverse-i-search)\`${this.searchQuery}\`: `
1008
- : this.getPrompt();
1009
- const before = this.buffer.slice(0, this.cursor);
1010
- const cursorChar = this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ';
1011
- const after = this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '';
1190
+ const busy = this.isCommandRunning();
1191
+ const prompt = busy
1192
+ ? ''
1193
+ : (this.searchMode ? `(reverse-i-search)\`${this.searchQuery}\`: ` : this.getPrompt());
1194
+ const before = busy ? '' : this.buffer.slice(0, this.cursor);
1195
+ const cursorChar = busy ? '' : (this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ');
1196
+ const after = busy ? '' : (this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '');
1197
+ const busyAttr = busy ? ' data-busy="true" style="display:none;"' : '';
1198
+ const cursorClass = busy ? 'codelab-pty__cursor' : 'codelab-pty__cursor blink';
1012
1199
 
1013
1200
  this.container.innerHTML = `
1014
1201
  <div class="codelab-terminal" id="cl-terminal-surface" tabindex="0">
1015
1202
  <div class="codelab-terminal__log" id="cl-terminal-log">${logsHtml}</div>
1016
- <div class="codelab-pty__active-line" id="cl-pty-active-line">
1017
- <span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="codelab-pty__cursor blink">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>
1203
+ <div class="codelab-pty__active-line" id="cl-pty-active-line"${busyAttr}>
1204
+ <span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="${cursorClass}">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>
1018
1205
  </div>
1019
1206
  <form class="codelab-repl" id="cl-repl-form" style="position:absolute;opacity:0;pointer-events:none;height:0;overflow:hidden;">
1020
1207
  <input type="text" class="codelab-repl__input" id="cl-repl-input" autocomplete="off" spellcheck="false" aria-label="Terminal command input" value="${escapeHtml(this.buffer)}">
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&amp;')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface DiffComputeResult {
8
17
  hunks: Array<{ type: 'context' | 'add' | 'del'; text: string; oldLine: number | null; newLine: number | null }>;
9
18
  added: number;
@@ -115,11 +124,25 @@ export class CodeLabDiffEditorManager {
115
124
  if (editorHost) editorHost.style.display = 'none';
116
125
  if (fallbackTextarea) fallbackTextarea.style.display = 'none';
117
126
 
127
+ if (this.diffEditor) {
128
+ this.diffEditor.dispose();
129
+ this.diffEditor = null;
130
+ }
131
+ if (this.originalModel) {
132
+ this.originalModel.dispose();
133
+ this.originalModel = null;
134
+ }
135
+ if (this.modifiedModel) {
136
+ this.modifiedModel.dispose();
137
+ this.modifiedModel = null;
138
+ }
139
+
140
+ const safeFile = escapeHtml(file);
118
141
  diffContainer.innerHTML = `
119
142
  <div class="codelab-diff-toolbar">
120
143
  <span style="font-weight:600;display:inline-flex;align-items:center;gap:6px;">
121
144
  ${CL_ICONS.diff}
122
- <span>Diff: <code>${file}</code> (${isStarter ? 'Current vs Starter' : 'Current vs Solution'})</span>
145
+ <span>Diff: <code>${safeFile}</code> (${isStarter ? 'Current vs Starter' : 'Current vs Solution'})</span>
123
146
  </span>
124
147
  <div class="codelab-segmented" style="margin-left:16px;">
125
148
  <button type="button" class="codelab-segmented__btn ${target === 'starter' ? 'active' : ''}" id="cl-diff-toggle-starter">Starter</button>
@@ -135,16 +158,20 @@ export class CodeLabDiffEditorManager {
135
158
  `;
136
159
 
137
160
  const slot = diffContainer.querySelector('#cl-diff-editor-slot');
138
- if (!this.diffEditor) {
139
- this.diffEditor = monaco.editor.createDiffEditor(slot, {
140
- readOnly: false,
141
- originalEditable: false,
142
- theme: this.workbench.theme === 'light' ? 'vs' : 'vs-dark',
143
- renderSideBySide: true,
144
- automaticLayout: true,
145
- minimap: { enabled: false }
146
- });
147
- }
161
+ const resolvedTheme = typeof this.workbench._resolvedTheme === 'function'
162
+ ? this.workbench._resolvedTheme()
163
+ : this.workbench.theme;
164
+ const monacoTheme = resolvedTheme === 'light'
165
+ ? 'vs'
166
+ : (resolvedTheme === 'high-contrast' ? 'hc-black' : 'vs-dark');
167
+ this.diffEditor = monaco.editor.createDiffEditor(slot, {
168
+ readOnly: false,
169
+ originalEditable: false,
170
+ theme: monacoTheme,
171
+ renderSideBySide: true,
172
+ automaticLayout: true,
173
+ minimap: { enabled: false }
174
+ });
148
175
 
149
176
  const lang = file.endsWith('.ts') ? 'typescript' : file.endsWith('.cpp') ? 'cpp' : file.endsWith('.py') ? 'python' : 'javascript';
150
177
  this.originalModel = monaco.editor.createModel(refCode, lang);
@@ -155,13 +182,32 @@ export class CodeLabDiffEditorManager {
155
182
  modified: this.modifiedModel
156
183
  });
157
184
 
158
- // Wire modified model edits back to workbench
159
- this.modifiedModel.onDidChangeContent(() => {
160
- const val = this.modifiedModel.getValue();
185
+ const syncFileToWorkbench = (val: string): void => {
161
186
  this.workbench.files[file] = val;
187
+ const mainModel = this.workbench.models && typeof this.workbench.models.get === 'function'
188
+ ? this.workbench.models.get(file)
189
+ : null;
190
+ if (mainModel && typeof mainModel.getValue === 'function' && mainModel.getValue() !== val) {
191
+ mainModel.setValue(val);
192
+ }
193
+ if (this.workbench.activeFile === file && fallbackTextarea && fallbackTextarea.value !== val) {
194
+ fallbackTextarea.value = val;
195
+ }
196
+ if (this.workbench.shell && this.workbench.shell.vfs && typeof this.workbench.shell.vfs.writeFileSync === 'function') {
197
+ const vfsPath = file.startsWith('/') ? file : `/workspace/${file}`;
198
+ this.workbench.shell.vfs.writeFileSync(vfsPath, val);
199
+ }
200
+ if (typeof this.workbench._saveState === 'function') {
201
+ this.workbench._saveState();
202
+ }
162
203
  if (typeof this.workbench._markDirty === 'function') {
163
204
  this.workbench._markDirty(file);
164
205
  }
206
+ };
207
+
208
+ // Wire modified model edits back to workbench, main Monaco model, VFS, and persistence
209
+ this.modifiedModel.onDidChangeContent(() => {
210
+ syncFileToWorkbench(this.modifiedModel.getValue());
165
211
  });
166
212
 
167
213
  // Wire buttons
@@ -172,14 +218,29 @@ export class CodeLabDiffEditorManager {
172
218
  this.openInlineDiff(file, 'solution');
173
219
  });
174
220
  diffContainer.querySelector('#cl-diff-revert-btn')?.addEventListener('click', () => {
175
- const starterCode = (this.workbench.starterFiles && this.workbench.starterFiles[file]) || '';
176
- this.workbench.files[file] = starterCode;
221
+ const starterMap = this.workbench.starterFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('guided', this.workbench.lang) : {});
222
+ const starterCode = (starterMap && starterMap[file]) || '';
223
+ if (typeof this.workbench.setFile === 'function') {
224
+ this.workbench.setFile(file, starterCode);
225
+ } else {
226
+ syncFileToWorkbench(starterCode);
227
+ }
177
228
  this.closeInlineDiff();
178
229
  this.workbench.openFile(file);
179
230
  });
180
231
  diffContainer.querySelector('#cl-diff-apply-btn')?.addEventListener('click', () => {
181
- const solutionCode = (this.workbench.solutionFiles && this.workbench.solutionFiles[file]) || '';
182
- this.workbench.files[file] = solutionCode;
232
+ if (typeof this.workbench.applyReferenceSolution === 'function') {
233
+ this.closeInlineDiff();
234
+ this.workbench.applyReferenceSolution();
235
+ return;
236
+ }
237
+ const solutionMap = this.workbench.solutionFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('sandbox', this.workbench.lang) : {});
238
+ const solutionCode = (solutionMap && solutionMap[file]) || '';
239
+ if (typeof this.workbench.setFile === 'function') {
240
+ this.workbench.setFile(file, solutionCode);
241
+ } else {
242
+ syncFileToWorkbench(solutionCode);
243
+ }
183
244
  this.closeInlineDiff();
184
245
  this.workbench.openFile(file);
185
246
  });
@@ -190,6 +251,33 @@ export class CodeLabDiffEditorManager {
190
251
  return true;
191
252
  }
192
253
 
254
+ public syncFileToWorkbench(file: string, nextVal: string): void {
255
+ this.workbench.files[file] = nextVal;
256
+ const mainModel = this.workbench.models instanceof Map ? this.workbench.models.get(file) : null;
257
+ if (mainModel && typeof mainModel.getValue === 'function' && mainModel.getValue() !== nextVal) {
258
+ mainModel.setValue(nextVal);
259
+ }
260
+ const fallbackEl = this.workbench.container?.querySelector('#cl-fallback-editor');
261
+ if (fallbackEl && this.workbench.activeFile === file) {
262
+ fallbackEl.value = nextVal;
263
+ }
264
+ if (this.workbench.shell?.vfs && typeof this.workbench.shell.vfs.writeFileSync === 'function') {
265
+ try {
266
+ this.workbench.shell.vfs.writeFileSync('/workspace/' + String(file).replace(/^\/+/, ''), nextVal);
267
+ } catch (err) {
268
+ if (this.workbench.logger && typeof this.workbench.logger.debug === 'function') {
269
+ this.workbench.logger.debug('Diff VFS sync skipped.', err);
270
+ }
271
+ }
272
+ }
273
+ if (typeof this.workbench._saveState === 'function') {
274
+ this.workbench._saveState();
275
+ }
276
+ if (typeof this.workbench._markDirty === 'function') {
277
+ this.workbench._markDirty(file);
278
+ }
279
+ }
280
+
193
281
  public closeInlineDiff(): void {
194
282
  this.isActive = false;
195
283
  const diffContainer = this.workbench.container?.querySelector('#cl-inline-diff-container');
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&amp;')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface DrawerTabOptions {
8
17
  workbench: any;
9
18
  drawerTab: string;
@@ -118,11 +127,15 @@ export function renderProblemsPanelHtml(workbench: any): string {
118
127
  ${items.map((item: any) => {
119
128
  const icon = item.severity === 'error' ? '✗' : (item.severity === 'warning' ? '⚠' : 'ℹ');
120
129
  const cls = item.severity === 'error' ? 'codelab-problem-item--error' : (item.severity === 'warning' ? 'codelab-problem-item--warning' : 'codelab-problem-item--info');
130
+ const safeFile = escapeHtml(item.file);
131
+ const safeMsg = escapeHtml(item.message);
132
+ const safeLine = Number(item.line) || 1;
133
+ const safeCol = Number(item.col) || 1;
121
134
  return `
122
- <div class="codelab-problem-item ${cls}" data-cl-problem-file="${item.file}" data-cl-problem-line="${item.line}" data-cl-problem-col="${item.col || 1}">
135
+ <div class="codelab-problem-item ${cls}" data-cl-problem-file="${safeFile}" data-cl-problem-line="${safeLine}" data-cl-problem-col="${safeCol}">
123
136
  <span style="font-weight:bold;">${icon}</span>
124
- <span>${item.message}</span>
125
- <span class="codelab-problem-item__location">${item.file}:${item.line}:${item.col || 1}</span>
137
+ <span>${safeMsg}</span>
138
+ <span class="codelab-problem-item__location">${safeFile}:${safeLine}:${safeCol}</span>
126
139
  </div>
127
140
  `;
128
141
  }).join('')}
@@ -131,7 +144,7 @@ export function renderProblemsPanelHtml(workbench: any): string {
131
144
  }
132
145
 
133
146
  export function renderStdinPanelHtml(workbench: any): string {
134
- const val = workbench.customStdin || '';
147
+ const val = escapeHtml(workbench.customStdin || '');
135
148
  return `
136
149
  <div class="codelab-stdin-panel">
137
150
  <div class="codelab-stdin-panel__header">
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&amp;')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface GuidedStepData {
8
17
  id: string;
9
18
  title: string;
@@ -21,10 +30,11 @@ export function renderStepPillsHtml(steps: GuidedStepData[], activeIndex: number
21
30
  const isPassed = completedStepIds.has(step.id);
22
31
  const isActive = idx === activeIndex;
23
32
  const cls = `codelab-step-pill ${isActive ? 'active' : ''} ${isPassed ? 'passed' : ''}`;
33
+ const safeTitle = escapeHtml(step.title);
24
34
  return `
25
- <button type="button" role="tab" aria-selected="${isActive}" class="${cls}" data-cl-step="${idx}" title="Step ${idx + 1}: ${step.title}">
35
+ <button type="button" role="tab" aria-selected="${isActive}" class="${cls}" data-cl-step="${idx}" title="Step ${idx + 1}: ${safeTitle}">
26
36
  <span class="codelab-step-pill__num">${isPassed ? CL_ICONS.check : idx + 1}</span>
27
- <span class="codelab-step-pill__label">${step.title}</span>
37
+ <span class="codelab-step-pill__label">${safeTitle}</span>
28
38
  </button>
29
39
  `;
30
40
  }).join('')}