memoryblock 0.0.1 → 0.1.0-beta

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 (144) hide show
  1. package/LICENSE +21 -0
  2. package/bin/mblk.js +6 -0
  3. package/dist/bin/mblk.d.ts +3 -0
  4. package/dist/bin/mblk.d.ts.map +1 -0
  5. package/dist/bin/mblk.js +339 -0
  6. package/dist/bin/mblk.js.map +1 -0
  7. package/dist/cli/commands/create.d.ts +2 -0
  8. package/dist/cli/commands/create.d.ts.map +1 -0
  9. package/dist/cli/commands/create.js +48 -0
  10. package/dist/cli/commands/create.js.map +1 -0
  11. package/dist/cli/commands/delete.d.ts +5 -0
  12. package/dist/cli/commands/delete.d.ts.map +1 -0
  13. package/dist/cli/commands/delete.js +147 -0
  14. package/dist/cli/commands/delete.js.map +1 -0
  15. package/dist/cli/commands/init.d.ts +9 -0
  16. package/dist/cli/commands/init.d.ts.map +1 -0
  17. package/dist/cli/commands/init.js +209 -0
  18. package/dist/cli/commands/init.js.map +1 -0
  19. package/dist/cli/commands/permissions.d.ts +13 -0
  20. package/dist/cli/commands/permissions.d.ts.map +1 -0
  21. package/dist/cli/commands/permissions.js +60 -0
  22. package/dist/cli/commands/permissions.js.map +1 -0
  23. package/dist/cli/commands/plugin-settings.d.ts +6 -0
  24. package/dist/cli/commands/plugin-settings.d.ts.map +1 -0
  25. package/dist/cli/commands/plugin-settings.js +118 -0
  26. package/dist/cli/commands/plugin-settings.js.map +1 -0
  27. package/dist/cli/commands/plugins.d.ts +3 -0
  28. package/dist/cli/commands/plugins.d.ts.map +1 -0
  29. package/dist/cli/commands/plugins.js +83 -0
  30. package/dist/cli/commands/plugins.js.map +1 -0
  31. package/dist/cli/commands/reset.d.ts +8 -0
  32. package/dist/cli/commands/reset.d.ts.map +1 -0
  33. package/dist/cli/commands/reset.js +96 -0
  34. package/dist/cli/commands/reset.js.map +1 -0
  35. package/dist/cli/commands/server.d.ts +25 -0
  36. package/dist/cli/commands/server.d.ts.map +1 -0
  37. package/dist/cli/commands/server.js +295 -0
  38. package/dist/cli/commands/server.js.map +1 -0
  39. package/dist/cli/commands/service.d.ts +18 -0
  40. package/dist/cli/commands/service.d.ts.map +1 -0
  41. package/dist/cli/commands/service.js +309 -0
  42. package/dist/cli/commands/service.js.map +1 -0
  43. package/dist/cli/commands/start.d.ts +11 -0
  44. package/dist/cli/commands/start.d.ts.map +1 -0
  45. package/dist/cli/commands/start.js +801 -0
  46. package/dist/cli/commands/start.js.map +1 -0
  47. package/dist/cli/commands/status.d.ts +2 -0
  48. package/dist/cli/commands/status.d.ts.map +1 -0
  49. package/dist/cli/commands/status.js +78 -0
  50. package/dist/cli/commands/status.js.map +1 -0
  51. package/dist/cli/commands/stop.d.ts +9 -0
  52. package/dist/cli/commands/stop.d.ts.map +1 -0
  53. package/dist/cli/commands/stop.js +83 -0
  54. package/dist/cli/commands/stop.js.map +1 -0
  55. package/dist/cli/commands/web.d.ts +5 -0
  56. package/dist/cli/commands/web.d.ts.map +1 -0
  57. package/dist/cli/commands/web.js +63 -0
  58. package/dist/cli/commands/web.js.map +1 -0
  59. package/dist/cli/constants.d.ts +38 -0
  60. package/dist/cli/constants.d.ts.map +1 -0
  61. package/dist/cli/constants.js +80 -0
  62. package/dist/cli/constants.js.map +1 -0
  63. package/dist/cli/logger.d.ts +12 -0
  64. package/dist/cli/logger.d.ts.map +1 -0
  65. package/dist/cli/logger.js +40 -0
  66. package/dist/cli/logger.js.map +1 -0
  67. package/dist/engine/agent.d.ts +15 -0
  68. package/dist/engine/agent.d.ts.map +1 -0
  69. package/dist/engine/agent.js +19 -0
  70. package/dist/engine/agent.js.map +1 -0
  71. package/dist/engine/conversation-log.d.ts +35 -0
  72. package/dist/engine/conversation-log.d.ts.map +1 -0
  73. package/dist/engine/conversation-log.js +83 -0
  74. package/dist/engine/conversation-log.js.map +1 -0
  75. package/dist/engine/cost-tracker.d.ts +52 -0
  76. package/dist/engine/cost-tracker.d.ts.map +1 -0
  77. package/dist/engine/cost-tracker.js +110 -0
  78. package/dist/engine/cost-tracker.js.map +1 -0
  79. package/dist/engine/gatekeeper.d.ts +20 -0
  80. package/dist/engine/gatekeeper.d.ts.map +1 -0
  81. package/dist/engine/gatekeeper.js +43 -0
  82. package/dist/engine/gatekeeper.js.map +1 -0
  83. package/dist/engine/memory.d.ts +28 -0
  84. package/dist/engine/memory.d.ts.map +1 -0
  85. package/dist/engine/memory.js +69 -0
  86. package/dist/engine/memory.js.map +1 -0
  87. package/dist/engine/monitor.d.ts +81 -0
  88. package/dist/engine/monitor.d.ts.map +1 -0
  89. package/dist/engine/monitor.js +610 -0
  90. package/dist/engine/monitor.js.map +1 -0
  91. package/dist/engine/prompts.d.ts +31 -0
  92. package/dist/engine/prompts.d.ts.map +1 -0
  93. package/dist/engine/prompts.js +93 -0
  94. package/dist/engine/prompts.js.map +1 -0
  95. package/dist/index.d.ts +12 -0
  96. package/dist/index.d.ts.map +1 -0
  97. package/dist/index.js +15 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/schemas.d.ts +544 -0
  100. package/dist/schemas.d.ts.map +1 -0
  101. package/dist/schemas.js +111 -0
  102. package/dist/schemas.js.map +1 -0
  103. package/dist/types.d.ts +188 -0
  104. package/dist/types.d.ts.map +1 -0
  105. package/dist/types.js +2 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/utils/config.d.ts +24 -0
  108. package/dist/utils/config.d.ts.map +1 -0
  109. package/dist/utils/config.js +86 -0
  110. package/dist/utils/config.js.map +1 -0
  111. package/dist/utils/fs.d.ts +18 -0
  112. package/dist/utils/fs.d.ts.map +1 -0
  113. package/dist/utils/fs.js +65 -0
  114. package/dist/utils/fs.js.map +1 -0
  115. package/package.json +37 -8
  116. package/src/bin/mblk.ts +347 -0
  117. package/src/cli/commands/create.ts +62 -0
  118. package/src/cli/commands/delete.ts +165 -0
  119. package/src/cli/commands/init.ts +241 -0
  120. package/src/cli/commands/permissions.ts +77 -0
  121. package/src/cli/commands/plugin-settings.ts +111 -0
  122. package/src/cli/commands/plugins.ts +89 -0
  123. package/src/cli/commands/reset.ts +97 -0
  124. package/src/cli/commands/server.ts +327 -0
  125. package/src/cli/commands/service.ts +300 -0
  126. package/src/cli/commands/start.ts +843 -0
  127. package/src/cli/commands/status.ts +90 -0
  128. package/src/cli/commands/stop.ts +91 -0
  129. package/src/cli/commands/web.ts +74 -0
  130. package/src/cli/constants.ts +88 -0
  131. package/src/cli/logger.ts +48 -0
  132. package/src/engine/agent.ts +19 -0
  133. package/src/engine/conversation-log.ts +90 -0
  134. package/src/engine/cost-tracker.ts +134 -0
  135. package/src/engine/gatekeeper.ts +53 -0
  136. package/src/engine/memory.ts +87 -0
  137. package/src/engine/monitor.ts +719 -0
  138. package/src/engine/prompts.ts +102 -0
  139. package/src/index.ts +88 -0
  140. package/src/schemas.ts +126 -0
  141. package/src/types.ts +220 -0
  142. package/src/utils/config.ts +106 -0
  143. package/src/utils/fs.ts +64 -0
  144. package/tsconfig.json +10 -0
@@ -0,0 +1,347 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from 'commander';
4
+ import { log } from '../cli/logger.js';
5
+ import { initCommand } from '../cli/commands/init.js';
6
+ import { createCommand } from '../cli/commands/create.js';
7
+ import { startCommand } from '../cli/commands/start.js';
8
+ import { stopCommand } from '../cli/commands/stop.js';
9
+ import { statusCommand } from '../cli/commands/status.js';
10
+ import { resetCommand } from '../cli/commands/reset.js';
11
+ import { serverStartCommand, serverStopCommand, serverStatusCommand, serverTokenCommand, shutdownCommand, restartCommand } from '../cli/commands/server.js';
12
+ import { addCommand, removeCommand } from '../cli/commands/plugins.js';
13
+ import { deleteCommand, restoreCommand } from '../cli/commands/delete.js';
14
+ import { permissionsCommand } from '../cli/commands/permissions.js';
15
+ import { pluginSettingsCommand } from '../cli/commands/plugin-settings.js';
16
+ import { serviceInstallCommand, serviceUninstallCommand, serviceStatusCommand } from '../cli/commands/service.js';
17
+ import { getVersion, DEFAULT_PORT } from '../cli/constants.js';
18
+
19
+ (async () => {
20
+
21
+ const version = await getVersion();
22
+ const program = new Command();
23
+
24
+ program
25
+ .name('mblk')
26
+ .description('Deploy isolated, multi-agent AI assistants with extreme resource efficiency.')
27
+ .version(version)
28
+ .exitOverride(() => process.exit(0)) // Don't throw ELIFECYCLE when showing help
29
+ .configureOutput({
30
+ writeOut: (str) => process.stdout.write(str),
31
+ writeErr: (str) => process.stdout.write(str), // Prevent stderr noise from Commander
32
+ });
33
+
34
+ program
35
+ .command('init')
36
+ .description('Interactive setup — configure credentials, verify connections, create your first block.')
37
+ .option('-y, --yes', 'Non-interactive mode: create defaults without prompts')
38
+ .action(async (opts: { yes?: boolean }) => {
39
+ try {
40
+ await initCommand({ nonInteractive: opts.yes });
41
+ } catch (err) {
42
+ log.error((err as Error).message);
43
+ process.exit(0);
44
+ }
45
+ });
46
+
47
+ program
48
+ .command('create <name>')
49
+ .description('Create a new block (isolated AI workspace).')
50
+ .action(async (name: string) => {
51
+ try {
52
+ await createCommand(name);
53
+ } catch (err) {
54
+ log.error((err as Error).message);
55
+ process.exit(0);
56
+ }
57
+ });
58
+
59
+ program
60
+ .command('start [block]')
61
+ .description('Start the monitor loop for a block (or all blocks).')
62
+ .option('-d, --daemon', 'Run the monitor in the background')
63
+ .option('-c, --channel <channel>', 'Specify override channel (e.g. web, cli)')
64
+ .action(async (block: string | undefined, opts: { daemon?: boolean; channel?: string }) => {
65
+ try {
66
+ await startCommand(block, opts);
67
+ } catch (err) {
68
+ const msg = (err as Error).message;
69
+ // If not initialized, auto-route to init and then retry
70
+ if (msg.includes('not initialized') || msg.includes('mblk init')) {
71
+ log.warn('Workspace not initialized. Running setup first...\n');
72
+ try {
73
+ await initCommand();
74
+ // Retry start after init completes
75
+ await startCommand(block, opts);
76
+ } catch (initErr) {
77
+ log.error((initErr as Error).message);
78
+ process.exit(0);
79
+ }
80
+ return;
81
+ }
82
+ log.error(msg);
83
+ process.exit(0);
84
+ }
85
+ });
86
+
87
+ program
88
+ .command('stop [block]')
89
+ .description('Stop a running block monitor (or all blocks).')
90
+ .action(async (block?: string) => {
91
+ try {
92
+ await stopCommand(block);
93
+ } catch (err) {
94
+ log.error((err as Error).message);
95
+ process.exit(0);
96
+ }
97
+ });
98
+
99
+ program
100
+ .command('status')
101
+ .description('Show the status of all blocks.')
102
+ .action(async () => {
103
+ try {
104
+ await statusCommand();
105
+ } catch (err) {
106
+ log.error((err as Error).message);
107
+ process.exit(0);
108
+ }
109
+ });
110
+
111
+ program
112
+ .command('reset <block>')
113
+ .description('Reset a block state (memory, pulse, costs). Use --hard to wipe logs.')
114
+ .option('--hard', 'Wipe logs directory as well')
115
+ .action(async (block: string, opts: { hard?: boolean }) => {
116
+ try {
117
+ await resetCommand(block, opts);
118
+ } catch (err) {
119
+ log.error((err as Error).message);
120
+ process.exit(0);
121
+ }
122
+ });
123
+
124
+
125
+ program
126
+ .command('delete <block>')
127
+ .description('Archive a block to prevent data loss. Use --hard to permanently delete.')
128
+ .option('--hard', 'Permanently wipe the block from disk')
129
+ .action(async (block: string, opts: { hard?: boolean }) => {
130
+ try {
131
+ await deleteCommand(block, opts);
132
+ } catch (err) {
133
+ log.error((err as Error).message);
134
+ process.exit(0);
135
+ }
136
+ });
137
+
138
+ program
139
+ .command('restore <name>')
140
+ .description('Restore an archived block by name.')
141
+ .action(async (archive: string) => {
142
+ try {
143
+ await restoreCommand(archive);
144
+ } catch (err) {
145
+ log.error((err as Error).message);
146
+ process.exit(0);
147
+ }
148
+ });
149
+ program
150
+ .command('permissions <block>')
151
+ .description('View or update block permissions (CLI-only).')
152
+ .option('-s, --scope <level>', 'Set scope: block, workspace, or system')
153
+ .option('--allow-shell', 'Allow shell command execution')
154
+ .option('--deny-shell', 'Deny shell command execution')
155
+ .option('--allow-network', 'Allow network/fetch access')
156
+ .option('--deny-network', 'Deny network/fetch access')
157
+ .option('--max-timeout <seconds>', 'Max command timeout in seconds')
158
+ .action(async (block: string, opts: any) => {
159
+ try {
160
+ await permissionsCommand(block, opts);
161
+ } catch (err) {
162
+ log.error((err as Error).message);
163
+ process.exit(0);
164
+ }
165
+ });
166
+ program
167
+ .command('settings [plugin]')
168
+ .description('View or edit plugin settings.')
169
+ .action(async (pluginId?: string) => {
170
+ try {
171
+ await pluginSettingsCommand(pluginId);
172
+ } catch (err) {
173
+ log.error((err as Error).message);
174
+ process.exit(0);
175
+ }
176
+ });
177
+
178
+ // ===== Server Subcommand Group =====
179
+
180
+ const server = program
181
+ .command('server')
182
+ .description('Manage the web/API server.');
183
+
184
+ server
185
+ .command('start')
186
+ .description('Start the web UI and API server.')
187
+ .option('-p, --port <port>', 'Port to listen on', DEFAULT_PORT)
188
+ .option('--new-token', 'Generate a new auth token')
189
+ .option('-d, --daemon', 'Run the server in the background')
190
+ .action(async (opts: { port?: string; newToken?: boolean; daemon?: boolean }) => {
191
+ try {
192
+ await serverStartCommand(opts);
193
+ } catch (err) {
194
+ log.error((err as Error).message);
195
+ process.exit(0);
196
+ }
197
+ });
198
+
199
+ server
200
+ .command('stop')
201
+ .description('Stop the running server.')
202
+ .action(async () => {
203
+ try {
204
+ await serverStopCommand();
205
+ } catch (err) {
206
+ log.error((err as Error).message);
207
+ process.exit(0);
208
+ }
209
+ });
210
+
211
+ server
212
+ .command('status')
213
+ .description('Show server status (PID, port, running state).')
214
+ .action(async () => {
215
+ try {
216
+ await serverStatusCommand();
217
+ } catch (err) {
218
+ log.error((err as Error).message);
219
+ process.exit(0);
220
+ }
221
+ });
222
+
223
+ server
224
+ .command('token')
225
+ .description('View the current API token, or generate a new one.')
226
+ .option('--new-token', 'Generate and set a new API token')
227
+ .action(async (opts: { newToken?: boolean }) => {
228
+ try {
229
+ await serverTokenCommand(opts);
230
+ } catch (err) {
231
+ log.error((err as Error).message);
232
+ process.exit(0);
233
+ }
234
+ });
235
+
236
+ // ===== Service Subcommand Group =====
237
+
238
+ const service = program
239
+ .command('service')
240
+ .description('Manage OS-level auto-start (launchd/systemd).');
241
+
242
+ service
243
+ .command('install')
244
+ .description('Register memoryblock to start on boot/login.')
245
+ .action(async () => {
246
+ try {
247
+ await serviceInstallCommand();
248
+ } catch (err) {
249
+ log.error((err as Error).message);
250
+ process.exit(0);
251
+ }
252
+ });
253
+
254
+ service
255
+ .command('uninstall')
256
+ .description('Remove memoryblock from system auto-start.')
257
+ .action(async () => {
258
+ try {
259
+ await serviceUninstallCommand();
260
+ } catch (err) {
261
+ log.error((err as Error).message);
262
+ process.exit(0);
263
+ }
264
+ });
265
+
266
+ service
267
+ .command('status')
268
+ .description('Check if the auto-start service is installed.')
269
+ .action(async () => {
270
+ try {
271
+ await serviceStatusCommand();
272
+ } catch (err) {
273
+ log.error((err as Error).message);
274
+ process.exit(0);
275
+ }
276
+ });
277
+
278
+ // ===== Lifecycle Commands =====
279
+
280
+ program
281
+ .command('shutdown')
282
+ .description('Stop all blocks and the server.')
283
+ .action(async () => {
284
+ try {
285
+ await shutdownCommand();
286
+ } catch (err) {
287
+ log.error((err as Error).message);
288
+ process.exit(0);
289
+ }
290
+ });
291
+
292
+ program
293
+ .command('restart')
294
+ .description('Restart: stop everything, then start server as daemon.')
295
+ .option('-p, --port <port>', 'Port to listen on', DEFAULT_PORT)
296
+ .action(async (opts: { port?: string }) => {
297
+ try {
298
+ await restartCommand(opts);
299
+ } catch (err) {
300
+ log.error((err as Error).message);
301
+ process.exit(0);
302
+ }
303
+ });
304
+
305
+ // Backward-compatible alias: `mblk web` = `mblk server start`
306
+ program
307
+ .command('web')
308
+ .description('Alias for `mblk server start`.')
309
+ .option('-p, --port <port>', 'Port to listen on', DEFAULT_PORT)
310
+ .option('--new-token', 'Generate a new auth token')
311
+ .option('-d, --daemon', 'Run the server in the background')
312
+ .action(async (opts: { port?: string; newToken?: boolean; daemon?: boolean }) => {
313
+ try {
314
+ await serverStartCommand(opts);
315
+ } catch (err) {
316
+ log.error((err as Error).message);
317
+ process.exit(0);
318
+ }
319
+ });
320
+
321
+ program
322
+ .command('add [plugin]')
323
+ .description('Install a plugin (run without args to list available plugins).')
324
+ .action(async (plugin: string) => {
325
+ try {
326
+ await addCommand(plugin);
327
+ } catch (err) {
328
+ log.error((err as Error).message);
329
+ process.exit(0);
330
+ }
331
+ });
332
+
333
+ program
334
+ .command('remove <plugin>')
335
+ .description('Remove an installed plugin.')
336
+ .action(async (plugin: string) => {
337
+ try {
338
+ await removeCommand(plugin);
339
+ } catch (err) {
340
+ log.error((err as Error).message);
341
+ process.exit(0);
342
+ }
343
+ });
344
+
345
+ program.parse();
346
+
347
+ })();
@@ -0,0 +1,62 @@
1
+ import { join } from 'node:path';
2
+ import { ensureDir, writeJson, atomicWrite, pathExists } from '../../utils/fs.js';
3
+ import {
4
+ loadGlobalConfig, resolveBlockPath, isInitialized,
5
+ } from '../../utils/config.js';
6
+ import { BlockConfigSchema, PulseStateSchema } from '../../schemas.js';
7
+ import { log } from '../logger.js';
8
+ import { FILE_TEMPLATES } from '../../engine/prompts.js';
9
+
10
+
11
+ // Templates moved to prompts.ts
12
+
13
+ export async function createCommand(blockName: string): Promise<void> {
14
+ if (!(await isInitialized())) {
15
+ throw new Error('Not initialized. Run `mblk init` first.');
16
+ }
17
+
18
+ if (!/^[a-z0-9][a-z0-9-]{0,31}$/.test(blockName)) {
19
+ throw new Error('Block name must start with a letter/number and contain only lowercase letters, numbers, and hyphens (max 32 chars).');
20
+ }
21
+
22
+ const globalConfig = await loadGlobalConfig();
23
+ const blockPath = resolveBlockPath(globalConfig, blockName);
24
+
25
+ if (await pathExists(blockPath)) {
26
+ throw new Error(`Block "${blockName}" already exists at ${blockPath}`);
27
+ }
28
+
29
+ log.brand(`Creating block: ${blockName}\n`);
30
+
31
+ // Directory structure
32
+ await ensureDir(blockPath);
33
+ await ensureDir(join(blockPath, 'agents'));
34
+ await ensureDir(join(blockPath, 'logs'));
35
+
36
+ // Block config (inherits global defaults)
37
+ const blockConfig = BlockConfigSchema.parse({
38
+ name: blockName,
39
+ adapter: globalConfig.defaults.adapter,
40
+ memory: globalConfig.defaults.memory,
41
+ pulse: globalConfig.defaults.pulse,
42
+ });
43
+ await writeJson(join(blockPath, 'config.json'), blockConfig);
44
+ log.success('Created config.json');
45
+
46
+ // Initial pulse state
47
+ const pulse = PulseStateSchema.parse({});
48
+ await writeJson(join(blockPath, 'pulse.json'), pulse);
49
+ log.success('Created pulse.json');
50
+
51
+ // Core identity files
52
+ await atomicWrite(join(blockPath, 'memory.md'), FILE_TEMPLATES.MEMORY_MD);
53
+ log.success('Created memory.md');
54
+
55
+ await atomicWrite(join(blockPath, 'monitor.md'), FILE_TEMPLATES.MONITOR_MD(blockName));
56
+ log.success('Created monitor.md');
57
+
58
+ console.log('');
59
+ log.brand(`Block "${blockName}" is ready.`);
60
+ log.dim(` Path: ${blockPath}`);
61
+ log.dim(` Start: mblk start ${blockName}`);
62
+ }
@@ -0,0 +1,165 @@
1
+ import { join } from 'node:path';
2
+ import { promises as fsp } from 'node:fs';
3
+ import * as p from '@clack/prompts';
4
+ import chalk from 'chalk';
5
+ import { loadGlobalConfig, resolveBlocksDir, isInitialized } from '../../utils/config.js';
6
+ import { ensureDir, pathExists } from '../../utils/fs.js';
7
+ import { log } from '../logger.js';
8
+ import { t } from '@memoryblock/locale';
9
+
10
+ /**
11
+ * Find archived folders matching a block name.
12
+ * Supports both:
13
+ * - Exact archive name: "dev-pal_2026-03-21T10-33-24-242Z"
14
+ * - Block name prefix: "dev-pal" (matches all archives of that block)
15
+ */
16
+ async function findArchives(archiveDir: string, query: string): Promise<string[]> {
17
+ // Strip _archive/ prefix if user pastes it
18
+ const name = query.replace(/^_archive\//, '');
19
+
20
+ try {
21
+ const entries = await fsp.readdir(archiveDir);
22
+
23
+ // 1. Exact match
24
+ if (entries.includes(name)) return [name];
25
+
26
+ // 2. Prefix match: "dev-pal" matches "dev-pal_2026-03-21T10-33-24-242Z"
27
+ const matches = entries
28
+ .filter(e => e.startsWith(`${name}_`))
29
+ .sort()
30
+ .reverse(); // newest first
31
+
32
+ return matches;
33
+ } catch {
34
+ return [];
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Resolve a single archive — if multiple exist, let user pick.
40
+ */
41
+ async function resolveArchive(archiveDir: string, query: string): Promise<string | null> {
42
+ const matches = await findArchives(archiveDir, query);
43
+
44
+ if (matches.length === 0) return null;
45
+ if (matches.length === 1) return matches[0];
46
+
47
+ // Multiple archives — let the user pick
48
+ const selection = await p.select({
49
+ message: `Multiple archives found for "${query}". Which one?`,
50
+ options: matches.map(m => {
51
+ // Extract the timestamp for a cleaner label
52
+ const tsMatch = m.match(/_(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2})/);
53
+ const hint = tsMatch ? tsMatch[1].replace(/-/g, ':').replace('T', ' ').slice(0, 16) : '';
54
+ return { value: m, label: m, hint };
55
+ }),
56
+ });
57
+
58
+ if (p.isCancel(selection)) return null;
59
+ return selection as string;
60
+ }
61
+
62
+ export async function deleteCommand(blockName: string, options?: { hard?: boolean }): Promise<void> {
63
+ if (!(await isInitialized())) {
64
+ throw new Error(t.general.notInitialized);
65
+ }
66
+
67
+ const globalConfig = await loadGlobalConfig();
68
+ const blocksDir = resolveBlocksDir(globalConfig);
69
+ const blockPath = join(blocksDir, blockName);
70
+
71
+ // Check if it's a direct block path first
72
+ if (await pathExists(blockPath)) {
73
+ if (blockName.startsWith('_archive/')) {
74
+ if (!options?.hard) {
75
+ throw new Error(t.archive.mustUseHard);
76
+ }
77
+ }
78
+
79
+ if (options?.hard) {
80
+ try {
81
+ await fsp.rm(blockPath, { recursive: true, force: true });
82
+ log.success(t.archive.hardDeleteSuccess(blockName));
83
+ } catch (err) {
84
+ throw new Error(`Failed to delete: ${(err as Error).message}`);
85
+ }
86
+ return;
87
+ }
88
+
89
+ // Soft delete — move to _archive directory
90
+ const archiveDir = join(blocksDir, '_archive');
91
+ await ensureDir(archiveDir);
92
+
93
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
94
+ const archiveName = `${blockName}_${timestamp}`;
95
+ const archivePath = join(archiveDir, archiveName);
96
+
97
+ try {
98
+ await fsp.rename(blockPath, archivePath);
99
+ log.success(t.archive.success(blockName));
100
+ log.dim(` ${t.archive.location(`_archive/${archiveName}`)}`);
101
+ log.dim(` ${t.archive.restoreCmd(blockName)}`);
102
+ log.dim(` ${t.archive.deleteCmd(blockName)}`);
103
+ } catch (err) {
104
+ throw new Error(`Failed to archive block: ${(err as Error).message}`);
105
+ }
106
+ return;
107
+ }
108
+
109
+ // Not a live block — maybe user wants to hard-delete an archive by name?
110
+ if (options?.hard) {
111
+ const archiveDir = join(blocksDir, '_archive');
112
+ const resolved = await resolveArchive(archiveDir, blockName);
113
+
114
+ if (!resolved) {
115
+ throw new Error(`Block or archive "${blockName}" not found.`);
116
+ }
117
+
118
+ const archivePath = join(archiveDir, resolved);
119
+ try {
120
+ await fsp.rm(archivePath, { recursive: true, force: true });
121
+ log.success(`"${resolved}" permanently deleted from archive.`);
122
+ } catch (err) {
123
+ throw new Error(`Failed to delete: ${(err as Error).message}`);
124
+ }
125
+ return;
126
+ }
127
+
128
+ throw new Error(`Block "${blockName}" not found. Run \`mblk status\` to see available blocks.`);
129
+ }
130
+
131
+ export async function restoreCommand(archiveName: string): Promise<void> {
132
+ if (!(await isInitialized())) {
133
+ throw new Error(t.general.notInitialized);
134
+ }
135
+
136
+ const globalConfig = await loadGlobalConfig();
137
+ const blocksDir = resolveBlocksDir(globalConfig);
138
+ const archiveDir = join(blocksDir, '_archive');
139
+
140
+ // Resolve the archive — supports block name, full name, or prefix
141
+ const resolved = await resolveArchive(archiveDir, archiveName);
142
+
143
+ if (!resolved) {
144
+ throw new Error(`No archive found for "${archiveName}". Run \`mblk status\` to check archives.`);
145
+ }
146
+
147
+ const archivePath = join(archiveDir, resolved);
148
+
149
+ // Extract original block name (strip timestamp suffix)
150
+ const match = resolved.match(/^(.*?)_\d{4}-\d{2}-\d{2}T.*/);
151
+ const targetName = match ? match[1] : resolved;
152
+ const targetPath = join(blocksDir, targetName);
153
+
154
+ if (await pathExists(targetPath)) {
155
+ throw new Error(`Cannot restore: A block named "${targetName}" already exists. Delete or rename it first.`);
156
+ }
157
+
158
+ try {
159
+ await fsp.rename(archivePath, targetPath);
160
+ log.success(`Block "${targetName}" restored successfully.`);
161
+ log.dim(` Start with: ${chalk.bold(`mblk start ${targetName}`)}`);
162
+ } catch (err) {
163
+ throw new Error(`Failed to restore block: ${(err as Error).message}`);
164
+ }
165
+ }