-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsync-push.mjs
More file actions
246 lines (206 loc) · 6.86 KB
/
Copy pathsync-push.mjs
File metadata and controls
246 lines (206 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/**
* sync-push.mjs
*
* Pushes local Copilot CLI sessions to a remote copilot-unleashed instance.
* Computes delta: only sends sessions that are new or updated since last sync.
*
* Usage:
* node scripts/sync-push.mjs # uses BASE_URL from .env
* node scripts/sync-push.mjs https://my-app.azurecontainerapps.io
*
* Requires:
* - GH_TOKEN or GITHUB_TOKEN env var (or gh auth token)
* - COPILOT_CONFIG_DIR or ~/.copilot with session-state/
*/
import { readFile, readdir, stat, access } from 'node:fs/promises';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { execSync } from 'node:child_process';
const MAX_BATCH_SIZE = 20;
// Skip large files and binary-like files
const SKIP_FILES = new Set(['events.jsonl', 'session.db', 'session.db-shm', 'session.db-wal']);
const MAX_FILE_SIZE = 1024 * 1024; // 1MB per file
// Resolve configuration
const configDir = process.env.COPILOT_CONFIG_DIR?.replace(/^~/, homedir())
?? join(homedir(), '.copilot');
const sessionStateDir = join(configDir, 'session-state');
// Resolve remote URL
let baseUrl = process.argv[2]
|| process.env.BASE_URL
|| process.env.SYNC_REMOTE_URL
|| '';
if (!baseUrl) {
// Try to read from .env
try {
const envContent = await readFile('.env', 'utf-8');
const match = envContent.match(/^BASE_URL=(.+)$/m);
if (match) baseUrl = match[1].trim();
} catch {
// No .env file
}
}
if (!baseUrl) {
console.error('Error: No remote URL specified.');
console.error('Usage: node scripts/sync-push.mjs <remote-url>');
console.error(' or set BASE_URL in .env or SYNC_REMOTE_URL env var');
process.exit(1);
}
// Ensure no trailing slash
baseUrl = baseUrl.replace(/\/$/, '');
// Resolve GitHub token
const token = process.env.GH_TOKEN
|| process.env.GITHUB_TOKEN
|| (() => {
try {
return execSync('gh auth token', { encoding: 'utf-8' }).trim();
} catch {
return '';
}
})();
if (!token) {
console.error('Error: No GitHub token found.');
console.error('Set GH_TOKEN or GITHUB_TOKEN, or authenticate with `gh auth login`.');
process.exit(1);
}
async function pathExists(p) {
try { await access(p); return true; } catch { return false; }
}
async function readSessionFiles(sessionDir) {
const files = {};
async function walk(dir, prefix = '') {
let entries;
try {
entries = await readdir(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const relativePath = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
// Skip rewind-snapshots (large, not needed)
if (entry.name === 'rewind-snapshots') continue;
await walk(join(dir, entry.name), relativePath);
} else if (entry.isFile()) {
if (SKIP_FILES.has(entry.name)) continue;
try {
const info = await stat(join(dir, entry.name));
if (info.size > MAX_FILE_SIZE) continue;
const content = await readFile(join(dir, entry.name), 'utf-8');
files[relativePath] = content;
} catch {
// Skip unreadable files
}
}
}
}
await walk(sessionDir);
return files;
}
// Main
console.log(`Syncing sessions to ${baseUrl}`);
console.log(`Session source: ${sessionStateDir}`);
// 1. Get remote sessions
let remoteSessions;
try {
const res = await fetch(`${baseUrl}/api/sessions/sync`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!res.ok) {
const text = await res.text();
console.error(`Failed to list remote sessions (${res.status}): ${text}`);
process.exit(1);
}
const data = await res.json();
remoteSessions = new Map(data.sessions.map(s => [s.id, s.updatedAt]));
console.log(`Remote has ${remoteSessions.size} sessions`);
} catch (err) {
console.error(`Failed to connect to ${baseUrl}:`, err.message);
process.exit(1);
}
// 2. Scan local sessions
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
let localEntries;
try {
localEntries = await readdir(sessionStateDir);
} catch {
console.error(`No session-state directory at: ${sessionStateDir}`);
process.exit(1);
}
const localSessions = [];
for (const entry of localEntries) {
if (!UUID_RE.test(entry)) continue;
const sessionDir = join(sessionStateDir, entry);
const wsYaml = join(sessionDir, 'workspace.yaml');
if (!await pathExists(wsYaml)) continue;
// Read updated_at from workspace.yaml
let updatedAt;
try {
const content = await readFile(wsYaml, 'utf-8');
const match = content.match(/^updated_at:\s*(.+)$/m);
if (match) updatedAt = match[1].trim();
} catch {
continue;
}
localSessions.push({ id: entry, updatedAt, dir: sessionDir });
}
console.log(`Local has ${localSessions.length} sessions`);
// 3. Compute delta
const toSync = localSessions.filter(local => {
const remoteUpdated = remoteSessions.get(local.id);
if (!remoteUpdated) return true; // New session
if (!local.updatedAt) return false; // Can't compare
// Sync if local is newer
return new Date(local.updatedAt) > new Date(remoteUpdated);
});
if (toSync.length === 0) {
console.log('✅ Already in sync — no new sessions to push');
process.exit(0);
}
console.log(`${toSync.length} session(s) to sync`);
// 4. Push in batches
let totalCreated = 0;
let totalUpdated = 0;
let totalErrors = 0;
for (let i = 0; i < toSync.length; i += MAX_BATCH_SIZE) {
const batch = toSync.slice(i, i + MAX_BATCH_SIZE);
const batchNum = Math.floor(i / MAX_BATCH_SIZE) + 1;
const totalBatches = Math.ceil(toSync.length / MAX_BATCH_SIZE);
console.log(`Pushing batch ${batchNum}/${totalBatches} (${batch.length} sessions)…`);
const sessions = [];
for (const s of batch) {
const files = await readSessionFiles(s.dir);
sessions.push({ id: s.id, files });
process.stdout.write(` 📦 ${s.id.substring(0, 8)}… (${Object.keys(files).length} files)\n`);
}
try {
const res = await fetch(`${baseUrl}/api/sessions/sync`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ sessions }),
});
if (!res.ok) {
const text = await res.text();
console.error(` ❌ Batch ${batchNum} failed (${res.status}): ${text}`);
totalErrors += batch.length;
continue;
}
const data = await res.json();
totalCreated += data.summary.created;
totalUpdated += data.summary.updated;
totalErrors += data.summary.errors;
for (const r of data.results) {
if (r.status === 'error') {
console.error(` ❌ ${r.id}: ${r.error}`);
}
}
} catch (err) {
console.error(` ❌ Batch ${batchNum} network error:`, err.message);
totalErrors += batch.length;
}
}
console.log('');
console.log(`✅ Sync complete: ${totalCreated} created, ${totalUpdated} updated, ${totalErrors} errors`);