@innovatorved/code-lab 0.3.2 → 0.5.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 (164) hide show
  1. package/ARCHITECTURE.md +5 -3
  2. package/CHANGELOG.md +41 -0
  3. package/README.md +53 -175
  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 +1799 -98
  10. package/dist/code-lab.d.ts +110 -15
  11. package/dist/code-lab.esm.js +49678 -23253
  12. package/dist/code-lab.js +49648 -23251
  13. package/dist/code-lab.umd.js +75766 -0
  14. package/dist/diagnostics.d.ts +3 -0
  15. package/dist/extensions/cpp.esm.js +19504 -3788
  16. package/dist/extensions/cpp.js +19501 -3785
  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 +19597 -3788
  21. package/dist/extensions/python.js +19597 -3788
  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 +107 -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 +90 -0
  57. package/dist/packages/ui/src/settings-dialog.d.ts +53 -0
  58. package/dist/packages/ui/src/themes.d.ts +40 -0
  59. package/dist/preview-sw.js +53 -12
  60. package/dist/runtime.d.ts +3 -3
  61. package/dist/runtime.esm.js +467 -44
  62. package/dist/runtime.js +465 -44
  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 +92 -11
  67. package/dist/shell.d.ts +1 -1
  68. package/dist/shell.esm.js +1300 -151
  69. package/dist/shell.js +1300 -151
  70. package/dist/storage.d.ts +47 -0
  71. package/dist/terminal.esm.js +62 -3
  72. package/dist/terminal.js +62 -3
  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/package.json +38 -9
  77. package/packages/commands/README.md +7 -0
  78. package/packages/commands/package.json +16 -0
  79. package/packages/commands/src/index.ts +467 -0
  80. package/packages/commands/tsconfig.json +22 -0
  81. package/packages/core/README.md +12 -0
  82. package/packages/core/package.json +13 -0
  83. package/packages/core/src/di.ts +147 -0
  84. package/packages/core/src/disposable.ts +112 -0
  85. package/packages/core/src/errors.ts +205 -0
  86. package/packages/core/src/events.ts +157 -0
  87. package/packages/core/src/index.ts +49 -0
  88. package/packages/core/src/path.ts +118 -0
  89. package/packages/core/src/registry.ts +61 -0
  90. package/packages/core/tsconfig.json +19 -0
  91. package/packages/editor/README.md +9 -0
  92. package/packages/editor/package.json +18 -0
  93. package/packages/editor/src/index.ts +344 -0
  94. package/packages/editor/tsconfig.json +24 -0
  95. package/packages/fs/README.md +7 -0
  96. package/packages/fs/package.json +16 -0
  97. package/packages/fs/src/backends.ts +301 -0
  98. package/packages/fs/src/index.ts +18 -0
  99. package/packages/fs/src/types.ts +44 -0
  100. package/packages/fs/src/vfs-service.ts +257 -0
  101. package/packages/fs/tsconfig.json +22 -0
  102. package/packages/preview/README.md +3 -0
  103. package/packages/preview/package.json +16 -0
  104. package/packages/preview/src/index.ts +125 -0
  105. package/packages/preview/tsconfig.json +22 -0
  106. package/packages/runtime/README.md +3 -0
  107. package/packages/runtime/package.json +17 -0
  108. package/packages/runtime/src/index.ts +134 -0
  109. package/packages/runtime/tsconfig.json +23 -0
  110. package/packages/settings/README.md +7 -0
  111. package/packages/settings/package.json +16 -0
  112. package/packages/settings/src/builtin-schemas.ts +246 -0
  113. package/packages/settings/src/configuration-service.ts +609 -0
  114. package/packages/settings/src/index.ts +38 -0
  115. package/packages/settings/src/jsonc.ts +189 -0
  116. package/packages/settings/src/schema.ts +164 -0
  117. package/packages/settings/tsconfig.json +22 -0
  118. package/packages/sql/README.md +12 -0
  119. package/packages/sql/package.json +13 -0
  120. package/packages/sql/src/completions.ts +359 -0
  121. package/packages/sql/src/diagnostics.ts +200 -0
  122. package/packages/sql/src/formatter.ts +271 -0
  123. package/packages/sql/src/index.ts +54 -0
  124. package/packages/sql/src/sql-editor.ts +802 -0
  125. package/packages/sql/src/styles.ts +501 -0
  126. package/packages/sql/src/tokenizer.ts +457 -0
  127. package/packages/sql/src/types.ts +148 -0
  128. package/packages/sql/tsconfig.json +19 -0
  129. package/packages/terminal/README.md +3 -0
  130. package/packages/terminal/package.json +16 -0
  131. package/packages/terminal/src/index.ts +183 -0
  132. package/packages/terminal/tsconfig.json +22 -0
  133. package/packages/ui/README.md +8 -0
  134. package/packages/ui/package.json +18 -0
  135. package/packages/ui/src/index.ts +40 -0
  136. package/packages/ui/src/observability.ts +175 -0
  137. package/packages/ui/src/plugin-registry.ts +289 -0
  138. package/packages/ui/src/settings-dialog.ts +575 -0
  139. package/packages/ui/src/themes.ts +267 -0
  140. package/packages/ui/tsconfig.json +24 -0
  141. package/src/code-lab.css +1799 -98
  142. package/src/diagnostics.ts +139 -7
  143. package/src/extensions/cpp.ts +88 -45
  144. package/src/extensions/python.ts +186 -53
  145. package/src/extensions/umbrella.ts +5 -0
  146. package/src/index.ts +1 -1
  147. package/src/modular-kernel.ts +11 -0
  148. package/src/preview-sw.js +52 -11
  149. package/src/runtime.ts +466 -46
  150. package/src/schemas.ts +62 -62
  151. package/src/scm.ts +781 -58
  152. package/src/shell.ts +1299 -151
  153. package/src/storage.ts +336 -3
  154. package/src/terminal.ts +60 -2
  155. package/src/workbench/diff-editor.ts +66 -6
  156. package/src/workbench/icons.ts +12 -2
  157. package/src/workbench/styles.ts +25 -19
  158. package/src/workbench.ts +17059 -6673
  159. package/dist/runtime/index.d.ts +0 -4
  160. package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
  161. package/dist/shell/index.d.ts +0 -4
  162. package/src/runtime/index.ts +0 -5
  163. package/src/shell/coreutils/compilers-cli.ts +0 -103
  164. package/src/shell/index.ts +0 -5
package/src/storage.ts CHANGED
@@ -38,9 +38,285 @@ export interface WorkspaceStorageOptions {
38
38
  preferredBackend?: StorageBackendType;
39
39
  }
40
40
 
41
+ export type WorkspaceKind = 'blank' | 'repo' | 'demo' | 'custom';
42
+
43
+ export interface WorkspaceMeta {
44
+ labId: string;
45
+ title: string;
46
+ kind: WorkspaceKind;
47
+ repo?: string;
48
+ updatedAt: number;
49
+ fileCount?: number;
50
+ }
51
+
52
+ export const WORKSPACE_REGISTRY_KEY = 'codelab:workspace-registry:v1';
53
+
41
54
  const DEFAULT_DB_NAME = 'codelab_workspace_db';
42
55
  const DEFAULT_STORE_NAME = 'projects';
43
56
  const DEFAULT_LS_PREFIX = 'codelab:project:';
57
+ const WORKBENCH_STATE_PREFIX = 'sd-codelab-v1:';
58
+ const LEGACY_WORKBENCH_PREFIX = 'codelab_';
59
+
60
+ const memoryRegistry: Map<string, WorkspaceMeta> = new Map();
61
+ const memoryOrIdbNonEmptyProjects: Set<string> = new Set();
62
+ let registryWriteFailed = false;
63
+ let lastBlankTs = 0;
64
+ let blankSeq = 0;
65
+
66
+ function getSafeLocalStorage(): Storage | null {
67
+ try {
68
+ if (typeof globalThis === 'undefined' || !('localStorage' in globalThis)) {
69
+ return null;
70
+ }
71
+ const ls = globalThis.localStorage;
72
+ if (
73
+ !ls ||
74
+ typeof ls.getItem !== 'function' ||
75
+ typeof ls.setItem !== 'function' ||
76
+ typeof ls.removeItem !== 'function'
77
+ ) {
78
+ return null;
79
+ }
80
+ return ls;
81
+ } catch (err) {
82
+ const _ignored = err;
83
+ void _ignored;
84
+ return null;
85
+ }
86
+ }
87
+
88
+ function isPlainRecord(val: unknown): val is Record<string, unknown> {
89
+ return Boolean(val && typeof val === 'object' && !Array.isArray(val));
90
+ }
91
+
92
+ function normalizeWorkspaceMeta(raw: unknown, existing?: WorkspaceMeta): WorkspaceMeta | null {
93
+ if (!isPlainRecord(raw)) return null;
94
+ const labId = typeof raw.labId === 'string' ? raw.labId.trim() : '';
95
+ if (!labId) return null;
96
+
97
+ const rawKind = raw.kind;
98
+ const kind: WorkspaceKind =
99
+ rawKind === 'blank' || rawKind === 'repo' || rawKind === 'demo' || rawKind === 'custom'
100
+ ? rawKind
101
+ : (existing?.kind ?? 'custom');
102
+
103
+ const title =
104
+ typeof raw.title === 'string' && raw.title.trim()
105
+ ? raw.title.trim()
106
+ : (existing?.title || labId);
107
+
108
+ const updatedAt =
109
+ typeof raw.updatedAt === 'number' && Number.isFinite(raw.updatedAt)
110
+ ? raw.updatedAt
111
+ : Date.now();
112
+
113
+ const repo =
114
+ typeof raw.repo === 'string'
115
+ ? (raw.repo.trim() || undefined)
116
+ : existing?.repo;
117
+
118
+ const fileCount =
119
+ typeof raw.fileCount === 'number' && Number.isFinite(raw.fileCount) && raw.fileCount >= 0
120
+ ? Math.floor(raw.fileCount)
121
+ : existing?.fileCount;
122
+
123
+ return {
124
+ labId,
125
+ title,
126
+ kind,
127
+ updatedAt,
128
+ ...(repo !== undefined ? { repo } : {}),
129
+ ...(fileCount !== undefined ? { fileCount } : {})
130
+ };
131
+ }
132
+
133
+ function readRegistryEntries(): Map<string, WorkspaceMeta> {
134
+ const ls = getSafeLocalStorage();
135
+ if (ls && !registryWriteFailed) {
136
+ try {
137
+ const raw = ls.getItem(WORKSPACE_REGISTRY_KEY);
138
+ if (raw === null) {
139
+ memoryRegistry.clear();
140
+ return new Map();
141
+ }
142
+ const parsed = JSON.parse(raw) as unknown;
143
+ if (!Array.isArray(parsed)) {
144
+ return new Map();
145
+ }
146
+ const map = new Map<string, WorkspaceMeta>();
147
+ for (const entry of parsed) {
148
+ const normalized = normalizeWorkspaceMeta(entry);
149
+ if (!normalized) continue;
150
+ const prev = map.get(normalized.labId);
151
+ if (!prev || normalized.updatedAt >= prev.updatedAt) {
152
+ map.set(normalized.labId, normalized);
153
+ }
154
+ }
155
+ memoryRegistry.clear();
156
+ for (const [k, v] of map.entries()) {
157
+ memoryRegistry.set(k, { ...v });
158
+ }
159
+ return map;
160
+ } catch (err) {
161
+ const _ignored = err;
162
+ void _ignored;
163
+ return new Map();
164
+ }
165
+ }
166
+
167
+ const out = new Map<string, WorkspaceMeta>();
168
+ for (const [k, v] of memoryRegistry.entries()) {
169
+ out.set(k, { ...v });
170
+ }
171
+ return out;
172
+ }
173
+
174
+ function persistRegistryEntries(map: Map<string, WorkspaceMeta>): void {
175
+ const sorted = Array.from(map.values()).sort((a, b) => b.updatedAt - a.updatedAt);
176
+ memoryRegistry.clear();
177
+ for (const item of sorted) {
178
+ memoryRegistry.set(item.labId, { ...item });
179
+ }
180
+ const ls = getSafeLocalStorage();
181
+ if (ls) {
182
+ try {
183
+ ls.setItem(WORKSPACE_REGISTRY_KEY, JSON.stringify(sorted));
184
+ registryWriteFailed = false;
185
+ } catch (err) {
186
+ const _ignored = err;
187
+ void _ignored;
188
+ registryWriteFailed = true;
189
+ }
190
+ }
191
+ }
192
+
193
+ export function listWorkspaces(): WorkspaceMeta[] {
194
+ return Array.from(readRegistryEntries().values()).sort((a, b) => b.updatedAt - a.updatedAt);
195
+ }
196
+
197
+ export function getWorkspaceMeta(labId: string): WorkspaceMeta | null {
198
+ if (typeof labId !== 'string') return null;
199
+ const trimmed = labId.trim();
200
+ if (!trimmed) return null;
201
+ const found = readRegistryEntries().get(trimmed);
202
+ return found ? { ...found } : null;
203
+ }
204
+
205
+ export function upsertWorkspaceMeta(meta: WorkspaceMeta): void {
206
+ if (!isPlainRecord(meta)) return;
207
+ const trimmedId = typeof meta.labId === 'string' ? meta.labId.trim() : '';
208
+ if (!trimmedId) return;
209
+ const map = readRegistryEntries();
210
+ const normalized = normalizeWorkspaceMeta(meta, map.get(trimmedId));
211
+ if (!normalized) return;
212
+ map.set(normalized.labId, normalized);
213
+ persistRegistryEntries(map);
214
+ }
215
+
216
+ export function removeWorkspaceMeta(labId: string): void {
217
+ if (typeof labId !== 'string') return;
218
+ const trimmed = labId.trim();
219
+ if (!trimmed) return;
220
+ const map = readRegistryEntries();
221
+ if (map.delete(trimmed)) {
222
+ persistRegistryEntries(map);
223
+ } else {
224
+ memoryRegistry.delete(trimmed);
225
+ }
226
+ }
227
+
228
+ function payloadHasSavedFiles(rawJson: string | null): boolean {
229
+ if (!rawJson) return false;
230
+ try {
231
+ const parsed = JSON.parse(rawJson) as unknown;
232
+ if (!isPlainRecord(parsed)) return false;
233
+
234
+ if (isPlainRecord(parsed.files) && Object.keys(parsed.files).length > 0) {
235
+ return true;
236
+ }
237
+
238
+ if (isPlainRecord(parsed.customFiles)) {
239
+ for (const val of Object.values(parsed.customFiles)) {
240
+ if (typeof val === 'string') {
241
+ return true;
242
+ }
243
+ if (isPlainRecord(val) && Object.keys(val).length > 0) {
244
+ return true;
245
+ }
246
+ }
247
+ }
248
+ } catch (err) {
249
+ const _ignored = err;
250
+ void _ignored;
251
+ }
252
+ return false;
253
+ }
254
+
255
+ function checkHasWorkspaceData(
256
+ labId: string,
257
+ extraPrefix?: string,
258
+ instanceMemory?: ReadonlyMap<string, StoredProject>
259
+ ): boolean {
260
+ if (typeof labId !== 'string') return false;
261
+ const trimmed = labId.trim();
262
+ if (!trimmed) return false;
263
+
264
+ if (instanceMemory) {
265
+ const proj = instanceMemory.get(trimmed);
266
+ if (proj && isPlainRecord(proj.files) && Object.keys(proj.files).length > 0) {
267
+ return true;
268
+ }
269
+ }
270
+
271
+ if (memoryOrIdbNonEmptyProjects.has(trimmed)) {
272
+ return true;
273
+ }
274
+
275
+ const ls = getSafeLocalStorage();
276
+ if (ls) {
277
+ try {
278
+ if (payloadHasSavedFiles(ls.getItem(`${WORKBENCH_STATE_PREFIX}${trimmed}`))) {
279
+ return true;
280
+ }
281
+ if (payloadHasSavedFiles(ls.getItem(`${LEGACY_WORKBENCH_PREFIX}${trimmed}`))) {
282
+ return true;
283
+ }
284
+ if (payloadHasSavedFiles(ls.getItem(`${DEFAULT_LS_PREFIX}${trimmed}`))) {
285
+ return true;
286
+ }
287
+ if (
288
+ typeof extraPrefix === 'string' &&
289
+ extraPrefix &&
290
+ extraPrefix !== DEFAULT_LS_PREFIX &&
291
+ payloadHasSavedFiles(ls.getItem(`${extraPrefix}${trimmed}`))
292
+ ) {
293
+ return true;
294
+ }
295
+ } catch (err) {
296
+ const _ignored = err;
297
+ void _ignored;
298
+ }
299
+ }
300
+
301
+ return false;
302
+ }
303
+
304
+ export function hasWorkspaceData(labId: string): boolean {
305
+ return checkHasWorkspaceData(labId);
306
+ }
307
+
308
+ export function createBlankWorkspaceId(): string {
309
+ const now = Date.now();
310
+ if (now === lastBlankTs) {
311
+ blankSeq += 1;
312
+ } else {
313
+ lastBlankTs = now;
314
+ blankSeq = 0;
315
+ }
316
+ const rand = Math.random().toString(36).slice(2, 6).padEnd(4, '0');
317
+ const suffix = blankSeq > 0 ? `${blankSeq.toString(36)}${rand}` : rand;
318
+ return `blank-workspace-${now}-${suffix}`;
319
+ }
44
320
 
45
321
  function cloneProject(project: StoredProject): StoredProject {
46
322
  const filesCopy: Record<string, string> = {};
@@ -95,6 +371,14 @@ function isIndexedDbAvailable(): boolean {
95
371
  }
96
372
 
97
373
  export class WorkspaceStorage {
374
+ public static readonly WORKSPACE_REGISTRY_KEY = WORKSPACE_REGISTRY_KEY;
375
+ public static readonly listWorkspaces = listWorkspaces;
376
+ public static readonly getWorkspaceMeta = getWorkspaceMeta;
377
+ public static readonly upsertWorkspaceMeta = upsertWorkspaceMeta;
378
+ public static readonly removeWorkspaceMeta = removeWorkspaceMeta;
379
+ public static readonly hasWorkspaceData = hasWorkspaceData;
380
+ public static readonly createBlankWorkspaceId = createBlankWorkspaceId;
381
+
98
382
  private readonly dbName: string;
99
383
  private readonly storeName: string;
100
384
  private readonly storagePrefix: string;
@@ -161,6 +445,7 @@ export class WorkspaceStorage {
161
445
  return false;
162
446
  }
163
447
  const normalized = cloneProject(project);
448
+ const hasFiles = Object.keys(normalized.files).length > 0;
164
449
  // Always keep memory mirror hot for resilience
165
450
  this.memoryStore.set(normalized.id, normalized);
166
451
 
@@ -182,7 +467,11 @@ export class WorkspaceStorage {
182
467
  resolve(false);
183
468
  }
184
469
  });
185
- if (ok) return true;
470
+ if (ok) {
471
+ if (hasFiles) memoryOrIdbNonEmptyProjects.add(normalized.id);
472
+ else memoryOrIdbNonEmptyProjects.delete(normalized.id);
473
+ return true;
474
+ }
186
475
  this.downgradeFromIdb();
187
476
  }
188
477
  }
@@ -191,6 +480,7 @@ export class WorkspaceStorage {
191
480
  try {
192
481
  const key = `${this.storagePrefix}${normalized.id}`;
193
482
  globalThis.localStorage.setItem(key, JSON.stringify(normalized));
483
+ memoryOrIdbNonEmptyProjects.delete(normalized.id);
194
484
  return true;
195
485
  } catch (err) {
196
486
  // QuotaExceededError or disabled storage -> fallback to memory
@@ -200,6 +490,8 @@ export class WorkspaceStorage {
200
490
  }
201
491
  }
202
492
 
493
+ if (hasFiles) memoryOrIdbNonEmptyProjects.add(normalized.id);
494
+ else memoryOrIdbNonEmptyProjects.delete(normalized.id);
203
495
  return true;
204
496
  }
205
497
 
@@ -320,6 +612,7 @@ export class WorkspaceStorage {
320
612
 
321
613
  public async deleteProject(id: string): Promise<boolean> {
322
614
  if (!id || typeof id !== 'string') return false;
615
+ memoryOrIdbNonEmptyProjects.delete(id);
323
616
  let existed = this.memoryStore.delete(id);
324
617
 
325
618
  if (this.backendType === 'indexeddb') {
@@ -365,13 +658,53 @@ export class WorkspaceStorage {
365
658
 
366
659
  return existed;
367
660
  }
661
+
662
+ public listWorkspaces(): WorkspaceMeta[] {
663
+ return listWorkspaces();
664
+ }
665
+
666
+ public getWorkspaceMeta(labId: string): WorkspaceMeta | null {
667
+ return getWorkspaceMeta(labId);
668
+ }
669
+
670
+ public upsertWorkspaceMeta(meta: WorkspaceMeta): void {
671
+ upsertWorkspaceMeta(meta);
672
+ }
673
+
674
+ public removeWorkspaceMeta(labId: string): void {
675
+ removeWorkspaceMeta(labId);
676
+ }
677
+
678
+ public hasWorkspaceData(labId: string): boolean {
679
+ return checkHasWorkspaceData(labId, this.storagePrefix, this.memoryStore);
680
+ }
681
+
682
+ public createBlankWorkspaceId(): string {
683
+ return createBlankWorkspaceId();
684
+ }
368
685
  }
369
686
 
370
687
  export function createWorkspaceStorage(options: WorkspaceStorageOptions = {}): WorkspaceStorage {
371
688
  return new WorkspaceStorage(options);
372
689
  }
373
690
 
374
- export default {
691
+ export const CodeLabWorkspaceStorage = {
692
+ WORKSPACE_REGISTRY_KEY,
375
693
  WorkspaceStorage,
376
- createWorkspaceStorage
694
+ createWorkspaceStorage,
695
+ listWorkspaces,
696
+ getWorkspaceMeta,
697
+ upsertWorkspaceMeta,
698
+ removeWorkspaceMeta,
699
+ hasWorkspaceData,
700
+ createBlankWorkspaceId
701
+ };
702
+
703
+ if (typeof globalThis !== 'undefined') {
704
+ const g = globalThis as unknown as Record<string, unknown>;
705
+ g.CodeLabWorkspaceStorage = CodeLabWorkspaceStorage;
706
+ }
707
+
708
+ export default {
709
+ ...CodeLabWorkspaceStorage
377
710
  };
package/src/terminal.ts CHANGED
@@ -328,6 +328,9 @@
328
328
  this.maxScrollback = typeof this.options.maxScrollback === 'number' && this.options.maxScrollback > 0
329
329
  ? this.options.maxScrollback
330
330
  : MAX_SCROLLBACK;
331
+ this.maxHistory = typeof this.options.maxHistory === 'number' && this.options.maxHistory > 0
332
+ ? this.options.maxHistory
333
+ : 1000;
331
334
  this.logs = Array.isArray(this.options.logs) ? this.options.logs : [];
332
335
  this.cwd = this.options.cwd || '/workspace';
333
336
  this.replMode = Boolean(this.options.replMode);
@@ -616,14 +619,60 @@
616
619
  }
617
620
  }
618
621
 
619
- readLine(promptText = '') {
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);
649
+ }
620
650
  return new Promise((resolve) => {
621
651
  const item = {
622
652
  promptText: String(promptText || ''),
623
653
  prevPrompt: null,
624
654
  _activated: false,
655
+ cleanup: null,
625
656
  resolve
626
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
+ }
627
676
  this._readLineQueue.push(item);
628
677
  this._activateNextReadLine();
629
678
  this.updateActiveLine();
@@ -667,6 +716,7 @@
667
716
  }
668
717
 
669
718
  destroy() {
719
+ this._drainReadLineQueue(null);
670
720
  this._cleanupListeners();
671
721
  this._mounted = false;
672
722
  }
@@ -749,12 +799,13 @@
749
799
 
750
800
  if (isCtrl && (key === 'c' || key === 'C')) {
751
801
  if (e.preventDefault) e.preventDefault();
752
- const wasBusy = this.isBusy;
802
+ const wasBusy = this.isBusy || (this._readLineQueue && this._readLineQueue.length > 0);
753
803
  this.isBusy = false;
754
804
  this._pushLog({ type: 'system', text: wasBusy ? '^C' : `${this.getPrompt()}${this.buffer}^C` });
755
805
  this.buffer = '';
756
806
  this.cursor = 0;
757
807
  this.historyIdx = -1;
808
+ this._drainReadLineQueue(null);
758
809
  if (this.replMode) {
759
810
  this.replMode = false;
760
811
  }
@@ -1016,6 +1067,9 @@
1016
1067
  this.searchMatchIdx = -1;
1017
1068
 
1018
1069
  const entry = this._readLineQueue.shift();
1070
+ if (entry && typeof entry.cleanup === 'function') {
1071
+ entry.cleanup();
1072
+ }
1019
1073
  if (typeof entry === 'function') {
1020
1074
  this.updateActiveLine();
1021
1075
  entry(line);
@@ -1052,6 +1106,10 @@
1052
1106
  if (line.trim()) {
1053
1107
  if (this.history[this.history.length - 1] !== line.trim()) {
1054
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
+ }
1055
1113
  }
1056
1114
  }
1057
1115
 
@@ -158,10 +158,16 @@ export class CodeLabDiffEditorManager {
158
158
  `;
159
159
 
160
160
  const slot = diffContainer.querySelector('#cl-diff-editor-slot');
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');
161
167
  this.diffEditor = monaco.editor.createDiffEditor(slot, {
162
168
  readOnly: false,
163
169
  originalEditable: false,
164
- theme: this.workbench.theme === 'light' ? 'vs' : 'vs-dark',
170
+ theme: monacoTheme,
165
171
  renderSideBySide: true,
166
172
  automaticLayout: true,
167
173
  minimap: { enabled: false }
@@ -176,13 +182,32 @@ export class CodeLabDiffEditorManager {
176
182
  modified: this.modifiedModel
177
183
  });
178
184
 
179
- // Wire modified model edits back to workbench
180
- this.modifiedModel.onDidChangeContent(() => {
181
- const val = this.modifiedModel.getValue();
185
+ const syncFileToWorkbench = (val: string): void => {
182
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
+ }
183
203
  if (typeof this.workbench._markDirty === 'function') {
184
204
  this.workbench._markDirty(file);
185
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());
186
211
  });
187
212
 
188
213
  // Wire buttons
@@ -195,7 +220,11 @@ export class CodeLabDiffEditorManager {
195
220
  diffContainer.querySelector('#cl-diff-revert-btn')?.addEventListener('click', () => {
196
221
  const starterMap = this.workbench.starterFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('guided', this.workbench.lang) : {});
197
222
  const starterCode = (starterMap && starterMap[file]) || '';
198
- this.workbench.files[file] = starterCode;
223
+ if (typeof this.workbench.setFile === 'function') {
224
+ this.workbench.setFile(file, starterCode);
225
+ } else {
226
+ syncFileToWorkbench(starterCode);
227
+ }
199
228
  this.closeInlineDiff();
200
229
  this.workbench.openFile(file);
201
230
  });
@@ -207,7 +236,11 @@ export class CodeLabDiffEditorManager {
207
236
  }
208
237
  const solutionMap = this.workbench.solutionFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('sandbox', this.workbench.lang) : {});
209
238
  const solutionCode = (solutionMap && solutionMap[file]) || '';
210
- this.workbench.files[file] = solutionCode;
239
+ if (typeof this.workbench.setFile === 'function') {
240
+ this.workbench.setFile(file, solutionCode);
241
+ } else {
242
+ syncFileToWorkbench(solutionCode);
243
+ }
211
244
  this.closeInlineDiff();
212
245
  this.workbench.openFile(file);
213
246
  });
@@ -218,6 +251,33 @@ export class CodeLabDiffEditorManager {
218
251
  return true;
219
252
  }
220
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
+
221
281
  public closeInlineDiff(): void {
222
282
  this.isActive = false;
223
283
  const diffContainer = this.workbench.container?.querySelector('#cl-inline-diff-container');
@@ -43,6 +43,16 @@ export const CL_ICONS: Record<string, string> = {
43
43
  warning: '<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>',
44
44
  clock: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>',
45
45
  memory: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 19v2"/><path d="M10 19v2"/><path d="M14 19v2"/><path d="M18 19v2"/><path d="M6 3v2"/><path d="M10 3v2"/><path d="M14 3v2"/><path d="M18 3v2"/><rect x="2" y="5" width="20" height="14" rx="2"/></svg>',
46
- keyboard: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 8h.01"/><path d="M10 8h.01"/><path d="M14 8h.01"/><path d="M18 8h.01"/><path d="M6 12h.01"/><path d="M10 12h.01"/><path d="M14 12h.01"/><path d="M18 12h.01"/><path d="M7 16h10"/></svg>',
47
- share: '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>'
46
+ keyboard: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 8h.01"/><path d="M10 8h.01"/><path d="M14 8h.01"/><path d="M18 8h.01"/><path d="M6 12h.01"/><path d="M10 12h.01"/><path d="M14 12h.01"/><path d="M18 12h.01"/><path d="M7 16h10"/></svg>',
47
+ share: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>',
48
+ layout: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="9"/></svg>',
49
+ maximize: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 3 21 3 21 9"/><polyline points="9 21 3 21 3 15"/><line x1="21" y1="3" x2="14" y2="10"/><line x1="3" y1="21" x2="10" y2="14"/></svg>',
50
+ settings: '<svg aria-hidden="true" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',
51
+ files: '<svg aria-hidden="true" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H9a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V6z"/><polyline points="14 2 14 7 19 7"/><path d="M5 8v12a2 2 0 0 0 2 2h9"/></svg>',
52
+ gitBranch: '<svg aria-hidden="true" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><line x1="6" y1="3" x2="6" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/></svg>',
53
+ sidebarLeft: '<svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="9" y1="3" x2="9" y2="21"/></svg>',
54
+ panelBottom: '<svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="15" x2="21" y2="15"/></svg>',
55
+ chevronUp: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"/></svg>',
56
+ collapseAll: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="4" width="16" height="16" rx="2"/><line x1="8" y1="12" x2="16" y2="12"/></svg>',
57
+ close: '<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>'
48
58
  };