@neurcode-ai/cli 0.6.0 ā 0.7.1
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/dist/commands/watch.d.ts.map +1 -1
- package/dist/commands/watch.js +12 -0
- package/dist/commands/watch.js.map +1 -1
- package/dist/services/watch/CommandPoller.d.ts +76 -0
- package/dist/services/watch/CommandPoller.d.ts.map +1 -0
- package/dist/services/watch/CommandPoller.js +298 -0
- package/dist/services/watch/CommandPoller.js.map +1 -0
- package/dist/utils/restore.d.ts +14 -0
- package/dist/utils/restore.d.ts.map +1 -0
- package/dist/utils/restore.js +89 -0
- package/dist/utils/restore.js.map +1 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../../src/commands/watch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../../src/commands/watch.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAsB,YAAY,kBAyDjC"}
|
package/dist/commands/watch.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.watchCommand = watchCommand;
|
|
4
4
|
const Sentinel_1 = require("../services/watch/Sentinel");
|
|
5
|
+
const CommandPoller_1 = require("../services/watch/CommandPoller");
|
|
5
6
|
/**
|
|
6
7
|
* Watch command - Start the Neurcode Watch service
|
|
7
8
|
*
|
|
@@ -15,6 +16,9 @@ async function watchCommand() {
|
|
|
15
16
|
// Create and initialize Sentinel
|
|
16
17
|
const sentinel = new Sentinel_1.Sentinel(projectRoot);
|
|
17
18
|
await sentinel.initialize();
|
|
19
|
+
// Create and start CommandPoller for remote commands
|
|
20
|
+
const commandPoller = new CommandPoller_1.CommandPoller(projectRoot);
|
|
21
|
+
commandPoller.start();
|
|
18
22
|
// Start watching
|
|
19
23
|
await sentinel.start();
|
|
20
24
|
console.log('ā
Neurcode Watch is running...');
|
|
@@ -27,10 +31,18 @@ async function watchCommand() {
|
|
|
27
31
|
console.log('š¦ Cloud sync: DISABLED (local-only mode)');
|
|
28
32
|
console.log(' Run "neurcode config --key <your_api_key>" to enable cloud sync');
|
|
29
33
|
}
|
|
34
|
+
// Check if command polling is configured
|
|
35
|
+
if (commandPoller.isConfigured()) {
|
|
36
|
+
console.log('š Remote commands: ENABLED (will execute revert commands from dashboard)');
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.log('š¦ Remote commands: DISABLED (no API key configured)');
|
|
40
|
+
}
|
|
30
41
|
console.log(' Press Ctrl+C to stop\n');
|
|
31
42
|
// Handle graceful shutdown
|
|
32
43
|
const shutdown = async () => {
|
|
33
44
|
console.log('\nš Shutting down...');
|
|
45
|
+
commandPoller.stop();
|
|
34
46
|
await sentinel.stop();
|
|
35
47
|
process.exit(0);
|
|
36
48
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch.js","sourceRoot":"","sources":["../../src/commands/watch.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"watch.js","sourceRoot":"","sources":["../../src/commands/watch.ts"],"names":[],"mappings":";;AASA,oCAyDC;AAlED,yDAAsD;AACtD,mEAAgE;AAEhE;;;;;GAKG;AACI,KAAK,UAAU,YAAY;IAChC,IAAI,CAAC;QACH,+CAA+C;QAC/C,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAElC,iCAAiC;QACjC,MAAM,QAAQ,GAAG,IAAI,mBAAQ,CAAC,WAAW,CAAC,CAAC;QAC3C,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC;QAE5B,qDAAqD;QACrD,MAAM,aAAa,GAAG,IAAI,6BAAa,CAAC,WAAW,CAAC,CAAC;QACrD,aAAa,CAAC,KAAK,EAAE,CAAC;QAEtB,iBAAiB;QACjB,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC;QAEvB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kB,EAAE,KAAK,CAAC,CAAC;QAC3C,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommandPoller - Polls for remote commands and executes them locally
|
|
3
|
+
*
|
|
4
|
+
* Polls the cloud API every 3 seconds for pending commands (like file reverts).
|
|
5
|
+
* When a command is received, executes it locally and updates the status.
|
|
6
|
+
*/
|
|
7
|
+
export interface Command {
|
|
8
|
+
id: string;
|
|
9
|
+
userId: string;
|
|
10
|
+
organizationId: string;
|
|
11
|
+
type: string;
|
|
12
|
+
payload: {
|
|
13
|
+
filePath: string;
|
|
14
|
+
blobHash: string;
|
|
15
|
+
};
|
|
16
|
+
status: string;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
updatedAt: string;
|
|
19
|
+
}
|
|
20
|
+
export interface PollResponse {
|
|
21
|
+
command: Command | null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* CommandPoller - Handles polling and execution of remote commands
|
|
25
|
+
*/
|
|
26
|
+
export declare class CommandPoller {
|
|
27
|
+
private apiUrl;
|
|
28
|
+
private apiKey;
|
|
29
|
+
private projectRoot;
|
|
30
|
+
private pollInterval;
|
|
31
|
+
private readonly pollIntervalMs;
|
|
32
|
+
private isRunning;
|
|
33
|
+
private blobStore;
|
|
34
|
+
constructor(projectRoot: string);
|
|
35
|
+
/**
|
|
36
|
+
* Start polling for commands
|
|
37
|
+
*/
|
|
38
|
+
start(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Stop polling for commands
|
|
41
|
+
*/
|
|
42
|
+
stop(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Poll for pending commands and execute them
|
|
45
|
+
*/
|
|
46
|
+
private poll;
|
|
47
|
+
/**
|
|
48
|
+
* Execute a command locally
|
|
49
|
+
*/
|
|
50
|
+
private executeCommand;
|
|
51
|
+
/**
|
|
52
|
+
* Compute the hash of the current file content
|
|
53
|
+
*/
|
|
54
|
+
private computeCurrentFileHash;
|
|
55
|
+
/**
|
|
56
|
+
* Execute a FILE_REVERT command
|
|
57
|
+
*/
|
|
58
|
+
private executeFileRevert;
|
|
59
|
+
/**
|
|
60
|
+
* Fetch blob content from cloud API and store it locally
|
|
61
|
+
*/
|
|
62
|
+
private fetchBlobFromCloud;
|
|
63
|
+
/**
|
|
64
|
+
* Update command status on the server
|
|
65
|
+
*/
|
|
66
|
+
private updateCommandStatus;
|
|
67
|
+
/**
|
|
68
|
+
* Check if poller is configured (has API key)
|
|
69
|
+
*/
|
|
70
|
+
isConfigured(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Reload API key from config (useful if user logs in after watch starts)
|
|
73
|
+
*/
|
|
74
|
+
reloadConfig(): void;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=CommandPoller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CommandPoller.d.ts","sourceRoot":"","sources":["../../../src/services/watch/CommandPoller.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE;QACP,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IACF,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CACzB;AAED;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,YAAY,CAA+B;IACnD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAQ;IACvC,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,SAAS,CAAY;gBAEjB,WAAW,EAAE,MAAM;IAQ/B;;OAEG;IACH,KAAK,IAAI,IAAI;IAsBb;;OAEG;IACH,IAAI,IAAI,IAAI;IASZ;;OAEG;YACW,IAAI;IA6ClB;;OAEG;YACW,cAAc;IAe5B;;OAEG;YACW,sBAAsB;IAmBpC;;OAEG;YACW,iBAAiB;IAyC/B;;OAEG;YACW,kBAAkB;IAwChC;;OAEG;YACW,mBAAmB;IA+BjC;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,YAAY,IAAI,IAAI;CAUrB"}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CommandPoller - Polls for remote commands and executes them locally
|
|
4
|
+
*
|
|
5
|
+
* Polls the cloud API every 3 seconds for pending commands (like file reverts).
|
|
6
|
+
* When a command is received, executes it locally and updates the status.
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.CommandPoller = void 0;
|
|
43
|
+
const config_1 = require("../../config");
|
|
44
|
+
const restore_1 = require("../../utils/restore");
|
|
45
|
+
const BlobStore_1 = require("./BlobStore");
|
|
46
|
+
const path = __importStar(require("path"));
|
|
47
|
+
const fs_1 = require("fs");
|
|
48
|
+
const crypto_1 = require("crypto");
|
|
49
|
+
/**
|
|
50
|
+
* CommandPoller - Handles polling and execution of remote commands
|
|
51
|
+
*/
|
|
52
|
+
class CommandPoller {
|
|
53
|
+
apiUrl;
|
|
54
|
+
apiKey;
|
|
55
|
+
projectRoot;
|
|
56
|
+
pollInterval = null;
|
|
57
|
+
pollIntervalMs = 3000; // Poll every 3 seconds
|
|
58
|
+
isRunning = false;
|
|
59
|
+
blobStore;
|
|
60
|
+
constructor(projectRoot) {
|
|
61
|
+
this.projectRoot = projectRoot;
|
|
62
|
+
const config = (0, config_1.loadConfig)();
|
|
63
|
+
this.apiUrl = config.apiUrl || config_1.DEFAULT_API_URL;
|
|
64
|
+
this.apiKey = (0, config_1.getApiKey)();
|
|
65
|
+
this.blobStore = new BlobStore_1.BlobStore(projectRoot);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Start polling for commands
|
|
69
|
+
*/
|
|
70
|
+
start() {
|
|
71
|
+
if (this.isRunning) {
|
|
72
|
+
console.warn('ā ļø CommandPoller is already running');
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// If no API key, skip silently (local-only mode)
|
|
76
|
+
if (!this.apiKey) {
|
|
77
|
+
console.log('š¦ Command polling: DISABLED (no API key configured)');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.isRunning = true;
|
|
81
|
+
console.log('š Command polling: ENABLED (checking every 3s)');
|
|
82
|
+
// Start polling immediately, then every 3 seconds
|
|
83
|
+
this.poll();
|
|
84
|
+
this.pollInterval = setInterval(() => {
|
|
85
|
+
this.poll();
|
|
86
|
+
}, this.pollIntervalMs);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Stop polling for commands
|
|
90
|
+
*/
|
|
91
|
+
stop() {
|
|
92
|
+
if (this.pollInterval) {
|
|
93
|
+
clearInterval(this.pollInterval);
|
|
94
|
+
this.pollInterval = null;
|
|
95
|
+
}
|
|
96
|
+
this.isRunning = false;
|
|
97
|
+
console.log('š Command polling stopped');
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Poll for pending commands and execute them
|
|
101
|
+
*/
|
|
102
|
+
async poll() {
|
|
103
|
+
if (!this.apiKey) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
// Poll for pending commands
|
|
108
|
+
const response = await fetch(`${this.apiUrl}/api/v1/commands/poll`, {
|
|
109
|
+
method: 'GET',
|
|
110
|
+
headers: {
|
|
111
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
if (response.status === 401 || response.status === 403) {
|
|
116
|
+
// API key invalid, stop polling
|
|
117
|
+
console.warn('ā ļø Command polling: API key invalid, stopping');
|
|
118
|
+
this.stop();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// For other errors, log but continue polling
|
|
122
|
+
console.warn(`ā ļø Command poll failed: ${response.status}`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const data = await response.json();
|
|
126
|
+
if (!data.command) {
|
|
127
|
+
// No pending commands - log occasionally for debugging (every 20 polls = ~1 minute)
|
|
128
|
+
if (Math.random() < 0.05) {
|
|
129
|
+
console.log('š Polling for commands... (no pending commands)');
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// Execute the command
|
|
134
|
+
console.log(`š„ Found pending command: ${data.command.type} (${data.command.id.substring(0, 8)}...)`);
|
|
135
|
+
await this.executeCommand(data.command);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
// Log error but continue polling (network issues, etc.)
|
|
139
|
+
console.warn(`ā ļø Command poll error: ${error.message}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Execute a command locally
|
|
144
|
+
*/
|
|
145
|
+
async executeCommand(command) {
|
|
146
|
+
console.log(`š„ Received command: ${command.type} (${command.id.substring(0, 8)}...)`);
|
|
147
|
+
try {
|
|
148
|
+
if (command.type === 'FILE_REVERT') {
|
|
149
|
+
await this.executeFileRevert(command);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
throw new Error(`Unknown command type: ${command.type}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
console.error(`ā Command execution failed: ${error.message}`);
|
|
157
|
+
await this.updateCommandStatus(command.id, 'FAILED', error.message);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Compute the hash of the current file content
|
|
162
|
+
*/
|
|
163
|
+
async computeCurrentFileHash(filePath) {
|
|
164
|
+
const resolvedPath = path.resolve(this.projectRoot, filePath);
|
|
165
|
+
try {
|
|
166
|
+
// Check if file exists
|
|
167
|
+
await fs_1.promises.access(resolvedPath);
|
|
168
|
+
// Read file content
|
|
169
|
+
const content = await fs_1.promises.readFile(resolvedPath, 'utf-8');
|
|
170
|
+
// Compute SHA-256 hash (same as BlobStore)
|
|
171
|
+
const hash = (0, crypto_1.createHash)('sha256').update(content, 'utf8').digest('hex');
|
|
172
|
+
return hash;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// File doesn't exist
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Execute a FILE_REVERT command
|
|
181
|
+
*/
|
|
182
|
+
async executeFileRevert(command) {
|
|
183
|
+
const { filePath, blobHash } = command.payload;
|
|
184
|
+
if (!filePath || !blobHash) {
|
|
185
|
+
throw new Error('Missing filePath or blobHash in command payload');
|
|
186
|
+
}
|
|
187
|
+
// Check if blob exists locally
|
|
188
|
+
const blobExists = await this.blobStore.exists(blobHash);
|
|
189
|
+
if (!blobExists) {
|
|
190
|
+
// Blob doesn't exist locally, fetch it from cloud
|
|
191
|
+
console.log(`š„ Blob not found locally, fetching from cloud: ${blobHash.substring(0, 12)}...`);
|
|
192
|
+
await this.fetchBlobFromCloud(blobHash);
|
|
193
|
+
}
|
|
194
|
+
// CRITICAL: Compare target hash with current file hash before reverting
|
|
195
|
+
const currentHash = await this.computeCurrentFileHash(filePath);
|
|
196
|
+
if (currentHash === blobHash) {
|
|
197
|
+
console.log(`ā ļø Skipped revert: Target hash (${blobHash.substring(0, 8)}...) is identical to current file`);
|
|
198
|
+
console.log(` File ${filePath} is already at the requested version`);
|
|
199
|
+
// Mark as completed (no-op, but successful)
|
|
200
|
+
await this.updateCommandStatus(command.id, 'COMPLETED');
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// Restore the file using existing restoreFile utility
|
|
204
|
+
// Note: restoreFile expects hash and targetPath, and handles decompression
|
|
205
|
+
await (0, restore_1.restoreFile)(blobHash, filePath, this.projectRoot);
|
|
206
|
+
console.log(`ā
File reverted: ${filePath} (${blobHash.substring(0, 8)}...)`);
|
|
207
|
+
if (currentHash) {
|
|
208
|
+
console.log(` Previous: ${currentHash.substring(0, 8)}... ā New: ${blobHash.substring(0, 8)}...`);
|
|
209
|
+
}
|
|
210
|
+
// Update command status to COMPLETED
|
|
211
|
+
await this.updateCommandStatus(command.id, 'COMPLETED');
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Fetch blob content from cloud API and store it locally
|
|
215
|
+
*/
|
|
216
|
+
async fetchBlobFromCloud(hash) {
|
|
217
|
+
if (!this.apiKey) {
|
|
218
|
+
throw new Error('API key not found. Please run "neurcode login" first.');
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
const response = await fetch(`${this.apiUrl}/api/v1/blobs/${hash}`, {
|
|
222
|
+
method: 'GET',
|
|
223
|
+
headers: {
|
|
224
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
225
|
+
'Content-Type': 'application/json',
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
if (!response.ok) {
|
|
229
|
+
if (response.status === 404) {
|
|
230
|
+
throw new Error(`Blob not found in cloud storage: ${hash.substring(0, 12)}...`);
|
|
231
|
+
}
|
|
232
|
+
if (response.status === 401 || response.status === 403) {
|
|
233
|
+
throw new Error('API key invalid. Please run "neurcode login" again.');
|
|
234
|
+
}
|
|
235
|
+
throw new Error(`Failed to fetch blob: ${response.status} ${response.statusText}`);
|
|
236
|
+
}
|
|
237
|
+
const data = await response.json();
|
|
238
|
+
// Decode base64 content to get GZIP compressed buffer
|
|
239
|
+
const compressedContent = Buffer.from(data.content, 'base64');
|
|
240
|
+
// Store the blob locally (BlobStore will handle the directory creation)
|
|
241
|
+
await this.blobStore.initialize();
|
|
242
|
+
const blobPath = this.blobStore.getBlobPath(hash);
|
|
243
|
+
await fs_1.promises.writeFile(blobPath, compressedContent);
|
|
244
|
+
console.log(`ā
Blob fetched and stored locally: ${hash.substring(0, 12)}...`);
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
throw new Error(`Failed to fetch blob from cloud: ${error.message}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Update command status on the server
|
|
252
|
+
*/
|
|
253
|
+
async updateCommandStatus(commandId, status, errorMessage) {
|
|
254
|
+
if (!this.apiKey) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const response = await fetch(`${this.apiUrl}/api/v1/commands/${commandId}/status`, {
|
|
259
|
+
method: 'POST',
|
|
260
|
+
headers: {
|
|
261
|
+
'Content-Type': 'application/json',
|
|
262
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
263
|
+
},
|
|
264
|
+
body: JSON.stringify({
|
|
265
|
+
status,
|
|
266
|
+
errorMessage,
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
269
|
+
if (!response.ok) {
|
|
270
|
+
const errorText = await response.text();
|
|
271
|
+
console.warn(`ā ļø Failed to update command status: ${response.status} ${errorText}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
console.warn(`ā ļø Failed to update command status: ${error.message}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Check if poller is configured (has API key)
|
|
280
|
+
*/
|
|
281
|
+
isConfigured() {
|
|
282
|
+
return this.apiKey !== null;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Reload API key from config (useful if user logs in after watch starts)
|
|
286
|
+
*/
|
|
287
|
+
reloadConfig() {
|
|
288
|
+
const config = (0, config_1.loadConfig)();
|
|
289
|
+
this.apiUrl = config.apiUrl || config_1.DEFAULT_API_URL;
|
|
290
|
+
this.apiKey = (0, config_1.getApiKey)();
|
|
291
|
+
// Restart polling if we now have an API key and weren't running before
|
|
292
|
+
if (this.apiKey && !this.isRunning) {
|
|
293
|
+
this.start();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
exports.CommandPoller = CommandPoller;
|
|
298
|
+
//# sourceMappingURL=CommandPoller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CommandPoller.js","sourceRoot":"","sources":["../../../src/services/watch/CommandPoller.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,yCAAsE;AACtE,iDAAkD;AAClD,2CAAwC;AACxC,2CAA6B;AAC7B,2BAAoC;AACpC,mCAAoC;AAqBpC;;GAEG;AACH,MAAa,aAAa;IAChB,MAAM,CAAS;IACf,MAAM,CAAgB;IACtB,WAAW,CAAS;IACpB,YAAY,GAA0B,IAAI,CAAC;IAClC,cAAc,GAAG,IAAI,CAAC,CAAC,uBAAuB;IACvD,SAAS,GAAY,KAAK,CAAC;IAC3B,SAAS,CAAY;IAE7B,YAAY,WAAmB;QAC7B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,MAAM,MAAM,GAAG,IAAA,mBAAU,GAAE,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,wBAAe,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAA,kBAAS,GAAE,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,qBAAS,CAAC,WAAW,CAAC,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACnB,OAAO,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAC;YACrD,OAAO;QACT,CAAC;QAED,iDAAiD;QACjD,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,CAAC,GAAG,CAAC,sDAAsD,CAAC,CAAC;YACpE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,OAAO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,CAAC;QAE1B,uEAAuE;QACvE,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACnC,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC;IACH,CAAC;CACF;AA3RD,sCA2RC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore a file from a blob hash
|
|
3
|
+
*
|
|
4
|
+
* Reads a GZIP-compressed blob from .neurcode/blobs/<hash> and
|
|
5
|
+
* writes it to the target file path.
|
|
6
|
+
*
|
|
7
|
+
* @param hash - SHA-256 hash of the file content
|
|
8
|
+
* @param targetPath - Relative path to the file to restore (e.g., "src/components/Button.tsx")
|
|
9
|
+
* @param projectRoot - Root directory of the project
|
|
10
|
+
* @returns Promise that resolves when the file is restored
|
|
11
|
+
* @throws Error if the blob doesn't exist, path is invalid, or write fails
|
|
12
|
+
*/
|
|
13
|
+
export declare function restoreFile(hash: string, targetPath: string, projectRoot: string): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=restore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.d.ts","sourceRoot":"","sources":["../../src/utils/restore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC,CAyCf"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Restore a file from a blob hash
|
|
4
|
+
*
|
|
5
|
+
* Reads a GZIP-compressed blob from .neurcode/blobs/<hash> and
|
|
6
|
+
* writes it to the target file path.
|
|
7
|
+
*
|
|
8
|
+
* @param hash - SHA-256 hash of the file content
|
|
9
|
+
* @param targetPath - Relative path to the file to restore (e.g., "src/components/Button.tsx")
|
|
10
|
+
* @param projectRoot - Root directory of the project
|
|
11
|
+
* @returns Promise that resolves when the file is restored
|
|
12
|
+
* @throws Error if the blob doesn't exist, path is invalid, or write fails
|
|
13
|
+
*/
|
|
14
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
17
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
18
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
19
|
+
}
|
|
20
|
+
Object.defineProperty(o, k2, desc);
|
|
21
|
+
}) : (function(o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
}));
|
|
25
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
26
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
27
|
+
}) : function(o, v) {
|
|
28
|
+
o["default"] = v;
|
|
29
|
+
});
|
|
30
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
31
|
+
var ownKeys = function(o) {
|
|
32
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
33
|
+
var ar = [];
|
|
34
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
35
|
+
return ar;
|
|
36
|
+
};
|
|
37
|
+
return ownKeys(o);
|
|
38
|
+
};
|
|
39
|
+
return function (mod) {
|
|
40
|
+
if (mod && mod.__esModule) return mod;
|
|
41
|
+
var result = {};
|
|
42
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
})();
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.restoreFile = restoreFile;
|
|
49
|
+
const zlib_1 = require("zlib");
|
|
50
|
+
const fs_1 = require("fs");
|
|
51
|
+
const path = __importStar(require("path"));
|
|
52
|
+
const fsExtra = __importStar(require("fs-extra"));
|
|
53
|
+
async function restoreFile(hash, targetPath, projectRoot) {
|
|
54
|
+
// Resolve the target file path relative to project root
|
|
55
|
+
const resolvedTargetPath = path.resolve(projectRoot, targetPath);
|
|
56
|
+
// Security: Ensure the target path is within the project root
|
|
57
|
+
// This prevents path traversal attacks (e.g., ../../../etc/passwd)
|
|
58
|
+
const resolvedProjectRoot = path.resolve(projectRoot);
|
|
59
|
+
if (!resolvedTargetPath.startsWith(resolvedProjectRoot)) {
|
|
60
|
+
throw new Error(`Invalid file path: ${targetPath} - Path traversal detected`);
|
|
61
|
+
}
|
|
62
|
+
// Resolve blob path
|
|
63
|
+
const blobPath = path.join(projectRoot, '.neurcode', 'blobs', hash);
|
|
64
|
+
// Check if blob exists
|
|
65
|
+
try {
|
|
66
|
+
await fs_1.promises.access(blobPath);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new Error(`Blob not found: ${hash}`);
|
|
70
|
+
}
|
|
71
|
+
// Read the compressed blob
|
|
72
|
+
const compressedData = await fs_1.promises.readFile(blobPath);
|
|
73
|
+
// Decompress using GZIP
|
|
74
|
+
let decompressedData;
|
|
75
|
+
try {
|
|
76
|
+
decompressedData = (0, zlib_1.gunzipSync)(compressedData);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw new Error(`Failed to decompress blob: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
80
|
+
}
|
|
81
|
+
// Convert to string (assuming UTF-8 encoding)
|
|
82
|
+
const fileContent = decompressedData.toString('utf-8');
|
|
83
|
+
// Ensure the target directory exists
|
|
84
|
+
const targetDir = path.dirname(resolvedTargetPath);
|
|
85
|
+
await fsExtra.ensureDir(targetDir);
|
|
86
|
+
// Write the file
|
|
87
|
+
await fs_1.promises.writeFile(resolvedTargetPath, fileContent, 'utf-8');
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=restore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.js","sourceRoot":"","sources":["../../src/utils/restore.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOH,kCA6CC;AAlDD,+BAAkC;AAClC,2BAAoC;AACpC,2CAA6B;AAC7B,kDAAoC;AAE7B,KAAK,UAAU,WAAW,CAC/B,IAAY,EACZ,UAAkB,EAClB,WAAmB;IAEnB,wDAAwD;IACxD,MAAM,kBAAkB,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IAEjE,8DAA8D;IAC9D,mEAAmE;IACnE,MAAM,mBAAmB,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACtD,IAAI,CAAC,kBAAkB,CAAC,UAAU,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACxD,MAAM,IAAI,KAAK,CAAC,sBAAsB,UAAU,4BAA4B,CAAC,CAAC;IAChF,CAAC;IAED,oBAAoB;IACpB,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAEpE,uBAAuB;IACvB,IAAI,CAAC;QACH,MAAM,aAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,mBAAmB,IAAI,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,2BAA2B;IAC3B,MAAM,cAAc,GAAG,MAAM,aAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAEnD,wBAAwB;IACxB,IAAI,gBAAwB,CAAC;IAC7B,IAAI,CAAC;QACH,gBAAgB,GAAG,IAAA,iBAAU,EAAC,cAAc,CAA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