@nandoblanco/opencode-local-backup 1.0.2 → 1.1.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.
package/index.js CHANGED
@@ -6,6 +6,7 @@ import path from "node:path";
6
6
  import os from "node:os";
7
7
  import { spawn } from "node:child_process";
8
8
  import { fileURLToPath } from "node:url";
9
+ import { BackupRpc } from "./rpc.js";
9
10
 
10
11
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
12
  const cliName = "open" + "code";
@@ -1061,6 +1062,9 @@ function restoreBackupFile(entry, options) {
1061
1062
  }
1062
1063
  const absBackupPath = path.resolve(entry.backupPath);
1063
1064
  const rootDir = entry.root ? path.resolve(entry.root) : "";
1065
+ if (!rootDir) {
1066
+ throw new Error("A project root is required to restore a backup snapshot");
1067
+ }
1064
1068
  const rawTarget = entry.originalPath || (rootDir && entry.relativePath ? path.join(rootDir, entry.relativePath) : "");
1065
1069
  if (!rawTarget) {
1066
1070
  throw new Error("Invalid restore target path");
@@ -1339,6 +1343,85 @@ function createPluginDefinition() {
1339
1343
  if (toolReg) registrations.push(toolReg);
1340
1344
  }
1341
1345
 
1346
+ if (ctx && ctx.rpc && typeof ctx.rpc.register === "function") {
1347
+ const rpcReg = await ctx.rpc.register(BackupRpc, {
1348
+ async list(input) {
1349
+ const inp = input || {};
1350
+ const includeSubprojects = inp.includeSubprojects !== undefined ? Boolean(inp.includeSubprojects) : true;
1351
+ const targetDir = inp.rootDir && String(inp.rootDir).trim()
1352
+ ? path.resolve(String(inp.rootDir).trim())
1353
+ : workspaceDir;
1354
+ const scanRoot = includeSubprojects ? resolveScanRootDir(targetDir) : targetDir;
1355
+ const settings = readBackupSettings();
1356
+ const entries = listBackups(targetDir, {
1357
+ includeSubprojects: includeSubprojects,
1358
+ limit: typeof inp.limit === "number" && inp.limit > 0 ? inp.limit : 1000
1359
+ });
1360
+ return JSON.parse(
1361
+ JSON.stringify({
1362
+ scanRoot: scanRoot,
1363
+ workspaceDir: workspaceDir,
1364
+ settings: settings,
1365
+ entries: entries
1366
+ })
1367
+ );
1368
+ },
1369
+ async diff(input) {
1370
+ const res = computeUnifiedDiff(
1371
+ input.backupPath,
1372
+ input.originalPath,
1373
+ typeof input.maxLines === "number" && input.maxLines > 0 ? input.maxLines : 400
1374
+ );
1375
+ return JSON.parse(JSON.stringify(res));
1376
+ },
1377
+ async restore(input) {
1378
+ const entry = input.entry;
1379
+ if (!entry.root) throw new Error("A project root is required to restore a backup snapshot");
1380
+ const match = listBackups(path.resolve(entry.root)).find(function (item) {
1381
+ return item.backupPath === path.resolve(entry.backupPath) && item.originalPath === path.resolve(entry.originalPath);
1382
+ });
1383
+ if (!match) throw new Error("Backup snapshot entry not found in the project backup list");
1384
+ const res = restoreBackupFile(match);
1385
+ return JSON.parse(JSON.stringify(res));
1386
+ },
1387
+ async openFile(input) {
1388
+ const res = openBackupFile(input.filePath);
1389
+ return JSON.parse(JSON.stringify(res));
1390
+ },
1391
+ async openDiff(input) {
1392
+ const res = openBackupDiff(input.backupPath, input.originalPath);
1393
+ return JSON.parse(JSON.stringify(res));
1394
+ },
1395
+ async prune(input) {
1396
+ const inp = input || {};
1397
+ const targetDir = inp.rootDir && String(inp.rootDir).trim()
1398
+ ? path.resolve(String(inp.rootDir).trim())
1399
+ : workspaceDir;
1400
+ const days = typeof inp.retentionDays === "number" && !Number.isNaN(inp.retentionDays)
1401
+ ? Math.max(0, Math.floor(inp.retentionDays))
1402
+ : resolveRetentionDays();
1403
+ if (days <= 0) {
1404
+ return { removed: 0, disabled: true, retentionDays: 0 };
1405
+ }
1406
+ const roots = collectBackupRoots(targetDir, true);
1407
+ const scanRoot = resolveScanRootDir(targetDir);
1408
+ if (!roots.includes(scanRoot)) roots.push(scanRoot);
1409
+ if (!roots.includes(targetDir)) roots.push(targetDir);
1410
+ let totalRemoved = 0;
1411
+ for (const r of roots) {
1412
+ const res = pruneOldBackups(r, days);
1413
+ totalRemoved += res.removed;
1414
+ }
1415
+ return { removed: totalRemoved, disabled: false, retentionDays: days };
1416
+ },
1417
+ async updateSettings(input) {
1418
+ const res = writeBackupSettings(input || {});
1419
+ return JSON.parse(JSON.stringify(res));
1420
+ }
1421
+ });
1422
+ if (rpcReg) registrations.push(rpcReg);
1423
+ }
1424
+
1342
1425
  return async function () {
1343
1426
  for (const reg of registrations) {
1344
1427
  if (reg && typeof reg.dispose === "function") {
@@ -1354,6 +1437,7 @@ const server = createPluginDefinition();
1354
1437
  export default server;
1355
1438
  export {
1356
1439
  server,
1440
+ BackupRpc,
1357
1441
  resolveConfigDir,
1358
1442
  getSettingsFilePath,
1359
1443
  readBackupSettings,
package/package.json CHANGED
@@ -1,29 +1,38 @@
1
1
  {
2
2
  "name": "@nandoblanco/opencode-local-backup",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "description": "Local-first, zero-token automatic pre-edit file backup plugin for OpenCode with smart project-root resolution, multi-file patch support, retention pruning, and an interactive /backups diff and restore TUI popup.",
7
+ "description": "Local-first, zero-token automatic pre-edit file backup plugin for OpenCode with smart project-root resolution, multi-file patch support, retention pruning, and an interactive /backups diff and restore Manager for TUI and OpenCode Desktop.",
8
8
  "type": "module",
9
9
  "main": "./index.js",
10
10
  "exports": {
11
11
  ".": "./index.js",
12
12
  "./server": "./index.js",
13
- "./tui": "./tui.ts"
13
+ "./tui": "./tui.ts",
14
+ "./rpc": "./rpc.js",
15
+ "./gui-experimental": "./dist/gui-experimental.cjs"
14
16
  },
15
17
  "bin": {
16
18
  "opencode-local-backup": "index.js"
17
19
  },
18
20
  "files": [
19
21
  "index.js",
22
+ "rpc.js",
23
+ "rpc.d.ts",
20
24
  "tui.ts",
25
+ "gui.tsx",
26
+ "gui.css",
27
+ "dist/gui-experimental.cjs",
21
28
  "assets/screenshot.png",
22
29
  "README.md",
23
30
  "LICENSE"
24
31
  ],
25
32
  "scripts": {
26
- "test": "node --no-warnings --test test/backup.test.js"
33
+ "build:gui": "vite build",
34
+ "test": "node --no-warnings --test test/backup.test.js",
35
+ "test:browser": "node test/gui-smoke.mjs && node test/panel-fallback.mjs"
27
36
  },
28
37
  "sideEffects": false,
29
38
  "engines": {
@@ -39,10 +48,13 @@
39
48
  "restore",
40
49
  "undo",
41
50
  "snapshots",
42
- "tui"
51
+ "tui",
52
+ "desktop",
53
+ "gui"
43
54
  ],
44
55
  "peerDependencies": {
45
56
  "@opencode-ai/plugin": ">=2.0.0",
57
+ "@opencode/plugin": ">=2.2.0",
46
58
  "@opentui/core": "*",
47
59
  "@opentui/solid": "*",
48
60
  "solid-js": "*"
@@ -51,6 +63,9 @@
51
63
  "@opencode-ai/plugin": {
52
64
  "optional": true
53
65
  },
66
+ "@opencode/plugin": {
67
+ "optional": true
68
+ },
54
69
  "@opentui/core": {
55
70
  "optional": true
56
71
  },
@@ -61,6 +76,18 @@
61
76
  "optional": true
62
77
  }
63
78
  },
79
+ "devDependencies": {
80
+ "playwright": "^1.56.0",
81
+ "@opencode/plugin": "^2.2.5",
82
+ "@opencode/theme": "^2.2.5",
83
+ "solid-js": "^1.9.18",
84
+ "vite": "^8.3.5",
85
+ "vite-plugin-solid": "^2.11.15"
86
+ },
87
+ "overrides": {
88
+ "glob": "14.0.5",
89
+ "minimatch": "11.0.8"
90
+ },
64
91
  "author": "nandoblanco",
65
92
  "license": "MIT",
66
93
  "repository": {
package/rpc.d.ts ADDED
@@ -0,0 +1,145 @@
1
+ import type { Rpc } from "@opencode/plugin/rpc";
2
+ import type { JsonSchema } from "effect";
3
+
4
+ type Schema<T> = JsonSchema.JsonSchema & { readonly __type?: T };
5
+
6
+ export interface BackupEntryDto {
7
+ id: string;
8
+ snapshotId: string;
9
+ root: string;
10
+ slug: string;
11
+ tool: string;
12
+ timeMs: number;
13
+ relativePath: string;
14
+ fileName: string;
15
+ backupPath: string;
16
+ originalPath: string;
17
+ size: number;
18
+ }
19
+
20
+ export interface BackupSettingsDto {
21
+ retentionDays: number;
22
+ debounceMs: number;
23
+ rootDir: string;
24
+ dialogWidth: string;
25
+ }
26
+
27
+ export interface BackupListInput {
28
+ rootDir?: string;
29
+ includeSubprojects?: boolean;
30
+ limit?: number;
31
+ }
32
+
33
+ export interface BackupListOutput {
34
+ scanRoot: string;
35
+ workspaceDir: string;
36
+ settings: BackupSettingsDto;
37
+ entries: BackupEntryDto[];
38
+ }
39
+
40
+ export interface BackupDiffLineDto {
41
+ type: string;
42
+ text: string;
43
+ }
44
+
45
+ export interface BackupDiffInput {
46
+ backupPath: string;
47
+ originalPath: string;
48
+ maxLines?: number;
49
+ }
50
+
51
+ export interface BackupDiffOutput {
52
+ status: string;
53
+ summary: string;
54
+ lines: BackupDiffLineDto[];
55
+ }
56
+
57
+ export interface BackupRestoreInput {
58
+ entry: BackupEntryDto;
59
+ }
60
+
61
+ export interface BackupRestoreOutput {
62
+ restoredPath: string;
63
+ fromSnapshot: string;
64
+ safetySnapshot: string | null;
65
+ }
66
+
67
+ export interface BackupOpenFileInput {
68
+ filePath: string;
69
+ }
70
+
71
+ export interface BackupOpenFileOutput {
72
+ openedPath: string;
73
+ launcher: string;
74
+ }
75
+
76
+ export interface BackupOpenDiffInput {
77
+ backupPath: string;
78
+ originalPath: string;
79
+ }
80
+
81
+ export interface BackupOpenDiffOutput {
82
+ backupPath: string;
83
+ currentPath: string;
84
+ launcher: string;
85
+ }
86
+
87
+ export interface BackupPruneInput {
88
+ rootDir?: string;
89
+ retentionDays?: number;
90
+ }
91
+
92
+ export interface BackupPruneOutput {
93
+ removed: number;
94
+ disabled: boolean;
95
+ retentionDays: number;
96
+ }
97
+
98
+ export interface BackupUpdateSettingsInput {
99
+ retentionDays?: number;
100
+ debounceMs?: number;
101
+ rootDir?: string;
102
+ dialogWidth?: string;
103
+ }
104
+
105
+ export interface BackupUpdateSettingsOutput {
106
+ filePath: string;
107
+ settings: BackupSettingsDto;
108
+ }
109
+
110
+ export declare const BackupRpc: Rpc.Definition<
111
+ "opencode-local-backup",
112
+ {
113
+ list: {
114
+ input: Schema<BackupListInput>;
115
+ output: Schema<BackupListOutput>;
116
+ };
117
+ diff: {
118
+ input: Schema<BackupDiffInput>;
119
+ output: Schema<BackupDiffOutput>;
120
+ };
121
+ restore: {
122
+ input: Schema<BackupRestoreInput>;
123
+ output: Schema<BackupRestoreOutput>;
124
+ };
125
+ openFile: {
126
+ input: Schema<BackupOpenFileInput>;
127
+ output: Schema<BackupOpenFileOutput>;
128
+ };
129
+ openDiff: {
130
+ input: Schema<BackupOpenDiffInput>;
131
+ output: Schema<BackupOpenDiffOutput>;
132
+ };
133
+ prune: {
134
+ input: Schema<BackupPruneInput>;
135
+ output: Schema<BackupPruneOutput>;
136
+ };
137
+ updateSettings: {
138
+ input: Schema<BackupUpdateSettingsInput>;
139
+ output: Schema<BackupUpdateSettingsOutput>;
140
+ };
141
+ },
142
+ Record<never, never>
143
+ >;
144
+
145
+ export default BackupRpc;
package/rpc.js ADDED
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+
3
+ export const BackupRpc = {
4
+ id: "opencode-local-backup",
5
+ methods: {
6
+ list: {
7
+ input: {
8
+ $schema: "https://json-schema.org/draft/2020-12/schema",
9
+ type: "object",
10
+ properties: {
11
+ rootDir: { type: "string" },
12
+ includeSubprojects: { type: "boolean" },
13
+ limit: { type: "number" },
14
+ },
15
+ additionalProperties: false,
16
+ },
17
+ output: {
18
+ $schema: "https://json-schema.org/draft/2020-12/schema",
19
+ type: "object",
20
+ properties: {
21
+ scanRoot: { type: "string" },
22
+ workspaceDir: { type: "string" },
23
+ settings: {
24
+ type: "object",
25
+ properties: {
26
+ retentionDays: { type: "number" },
27
+ debounceMs: { type: "number" },
28
+ rootDir: { type: "string" },
29
+ dialogWidth: { type: "string" },
30
+ },
31
+ required: ["retentionDays", "debounceMs", "rootDir", "dialogWidth"],
32
+ },
33
+ entries: {
34
+ type: "array",
35
+ items: {
36
+ type: "object",
37
+ properties: {
38
+ id: { type: "string" },
39
+ snapshotId: { type: "string" },
40
+ root: { type: "string" },
41
+ slug: { type: "string" },
42
+ tool: { type: "string" },
43
+ timeMs: { type: "number" },
44
+ relativePath: { type: "string" },
45
+ fileName: { type: "string" },
46
+ backupPath: { type: "string" },
47
+ originalPath: { type: "string" },
48
+ size: { type: "number" },
49
+ },
50
+ required: [
51
+ "id",
52
+ "snapshotId",
53
+ "root",
54
+ "slug",
55
+ "tool",
56
+ "timeMs",
57
+ "relativePath",
58
+ "fileName",
59
+ "backupPath",
60
+ "originalPath",
61
+ "size",
62
+ ],
63
+ },
64
+ },
65
+ },
66
+ required: ["scanRoot", "workspaceDir", "settings", "entries"],
67
+ },
68
+ },
69
+ diff: {
70
+ input: {
71
+ $schema: "https://json-schema.org/draft/2020-12/schema",
72
+ type: "object",
73
+ properties: {
74
+ backupPath: { type: "string" },
75
+ originalPath: { type: "string" },
76
+ maxLines: { type: "number" },
77
+ },
78
+ required: ["backupPath", "originalPath"],
79
+ additionalProperties: false,
80
+ },
81
+ output: {
82
+ $schema: "https://json-schema.org/draft/2020-12/schema",
83
+ type: "object",
84
+ properties: {
85
+ status: { type: "string" },
86
+ summary: { type: "string" },
87
+ lines: {
88
+ type: "array",
89
+ items: {
90
+ type: "object",
91
+ properties: {
92
+ type: { type: "string" },
93
+ text: { type: "string" },
94
+ },
95
+ required: ["type", "text"],
96
+ },
97
+ },
98
+ },
99
+ required: ["status", "summary", "lines"],
100
+ },
101
+ },
102
+ restore: {
103
+ input: {
104
+ $schema: "https://json-schema.org/draft/2020-12/schema",
105
+ type: "object",
106
+ properties: {
107
+ entry: {
108
+ type: "object",
109
+ properties: {
110
+ id: { type: "string" },
111
+ snapshotId: { type: "string" },
112
+ root: { type: "string" },
113
+ slug: { type: "string" },
114
+ tool: { type: "string" },
115
+ timeMs: { type: "number" },
116
+ relativePath: { type: "string" },
117
+ fileName: { type: "string" },
118
+ backupPath: { type: "string" },
119
+ originalPath: { type: "string" },
120
+ size: { type: "number" },
121
+ },
122
+ required: ["root", "backupPath", "originalPath"],
123
+ },
124
+ },
125
+ required: ["entry"],
126
+ additionalProperties: false,
127
+ },
128
+ output: {
129
+ $schema: "https://json-schema.org/draft/2020-12/schema",
130
+ type: "object",
131
+ properties: {
132
+ restoredPath: { type: "string" },
133
+ fromSnapshot: { type: "string" },
134
+ safetySnapshot: { type: ["string", "null"] },
135
+ },
136
+ required: ["restoredPath", "fromSnapshot"],
137
+ },
138
+ },
139
+ openFile: {
140
+ input: {
141
+ $schema: "https://json-schema.org/draft/2020-12/schema",
142
+ type: "object",
143
+ properties: {
144
+ filePath: { type: "string" },
145
+ },
146
+ required: ["filePath"],
147
+ additionalProperties: false,
148
+ },
149
+ output: {
150
+ $schema: "https://json-schema.org/draft/2020-12/schema",
151
+ type: "object",
152
+ properties: {
153
+ openedPath: { type: "string" },
154
+ launcher: { type: "string" },
155
+ },
156
+ required: ["openedPath", "launcher"],
157
+ },
158
+ },
159
+ openDiff: {
160
+ input: {
161
+ $schema: "https://json-schema.org/draft/2020-12/schema",
162
+ type: "object",
163
+ properties: {
164
+ backupPath: { type: "string" },
165
+ originalPath: { type: "string" },
166
+ },
167
+ required: ["backupPath", "originalPath"],
168
+ additionalProperties: false,
169
+ },
170
+ output: {
171
+ $schema: "https://json-schema.org/draft/2020-12/schema",
172
+ type: "object",
173
+ properties: {
174
+ backupPath: { type: "string" },
175
+ currentPath: { type: "string" },
176
+ launcher: { type: "string" },
177
+ },
178
+ required: ["backupPath", "currentPath", "launcher"],
179
+ },
180
+ },
181
+ prune: {
182
+ input: {
183
+ $schema: "https://json-schema.org/draft/2020-12/schema",
184
+ type: "object",
185
+ properties: {
186
+ rootDir: { type: "string" },
187
+ retentionDays: { type: "number" },
188
+ },
189
+ additionalProperties: false,
190
+ },
191
+ output: {
192
+ $schema: "https://json-schema.org/draft/2020-12/schema",
193
+ type: "object",
194
+ properties: {
195
+ removed: { type: "number" },
196
+ disabled: { type: "boolean" },
197
+ retentionDays: { type: "number" },
198
+ },
199
+ required: ["removed", "disabled", "retentionDays"],
200
+ },
201
+ },
202
+ updateSettings: {
203
+ input: {
204
+ $schema: "https://json-schema.org/draft/2020-12/schema",
205
+ type: "object",
206
+ properties: {
207
+ retentionDays: { type: "number" },
208
+ debounceMs: { type: "number" },
209
+ rootDir: { type: "string" },
210
+ dialogWidth: { type: "string" },
211
+ },
212
+ additionalProperties: false,
213
+ },
214
+ output: {
215
+ $schema: "https://json-schema.org/draft/2020-12/schema",
216
+ type: "object",
217
+ properties: {
218
+ filePath: { type: "string" },
219
+ settings: {
220
+ type: "object",
221
+ properties: {
222
+ retentionDays: { type: "number" },
223
+ debounceMs: { type: "number" },
224
+ rootDir: { type: "string" },
225
+ dialogWidth: { type: "string" },
226
+ },
227
+ required: ["retentionDays", "debounceMs", "rootDir", "dialogWidth"],
228
+ },
229
+ },
230
+ required: ["filePath", "settings"],
231
+ },
232
+ },
233
+ },
234
+ events: {},
235
+ };
236
+
237
+ export default BackupRpc;