opencode-studio-server 1.24.0 → 1.26.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 (2) hide show
  1. package/index.js +83 -231
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1103,15 +1103,7 @@ async function ensureGitHubRepo(token, repoName) {
1103
1103
  });
1104
1104
 
1105
1105
  if (response.ok) {
1106
- const data = await response.json();
1107
- const branch = data.default_branch || 'main';
1108
- const refRes = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, {
1109
- headers: { 'Authorization': `Bearer ${token}` }
1110
- });
1111
- if (refRes.status === 404 || refRes.status === 409) {
1112
- await bootstrapEmptyRepo(token, owner, repo);
1113
- }
1114
- return data;
1106
+ return await response.json();
1115
1107
  }
1116
1108
 
1117
1109
  if (response.status === 404) {
@@ -1130,6 +1122,7 @@ async function ensureGitHubRepo(token, repoName) {
1130
1122
  });
1131
1123
 
1132
1124
  if (createRes.ok) {
1125
+ await new Promise(r => setTimeout(r, 2000));
1133
1126
  return await createRes.json();
1134
1127
  }
1135
1128
 
@@ -1141,185 +1134,34 @@ async function ensureGitHubRepo(token, repoName) {
1141
1134
  throw new Error(`Failed to check repo: ${err}`);
1142
1135
  }
1143
1136
 
1144
- async function bootstrapEmptyRepo(token, owner, repo) {
1145
- const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/contents/README.md`, {
1146
- method: 'PUT',
1147
- headers: {
1148
- 'Authorization': `Bearer ${token}`,
1149
- 'Content-Type': 'application/json'
1150
- },
1151
- body: JSON.stringify({
1152
- message: 'Initial commit',
1153
- content: Buffer.from('# OpenCode Studio Backup\n').toString('base64')
1154
- })
1155
- });
1156
-
1157
- if (!res.ok) {
1158
- const err = await res.text();
1159
- throw new Error(`Failed to bootstrap repo: ${err}`);
1160
- }
1161
- }
1162
-
1163
- function collectBlobs(rootDir, basePath = '', blobs = []) {
1164
- const dir = basePath || rootDir;
1165
- if (!fs.existsSync(dir)) return blobs;
1166
- const MAX_FILE_SIZE = 1024 * 1024;
1137
+ function copyDirContents(src, dest) {
1138
+ if (!fs.existsSync(src)) return;
1139
+ fs.mkdirSync(dest, { recursive: true });
1140
+ const SKIP_DIRS = ['node_modules', '.git', '.next', 'cache'];
1167
1141
  const SKIP_EXT = ['.log', '.tmp', '.db', '.sqlite', '.cache', '.pack', '.idx'];
1168
1142
 
1169
- for (const name of fs.readdirSync(dir)) {
1170
- const fullPath = path.join(dir, name);
1171
- const stat = fs.statSync(fullPath);
1143
+ for (const name of fs.readdirSync(src)) {
1144
+ const srcPath = path.join(src, name);
1145
+ const destPath = path.join(dest, name);
1146
+ const stat = fs.statSync(srcPath);
1172
1147
 
1173
1148
  if (stat.isDirectory()) {
1174
- if (name === 'node_modules' || name === '.git' || name === '.next' || name === 'cache') continue;
1175
- collectBlobs(rootDir, fullPath, blobs);
1149
+ if (SKIP_DIRS.includes(name)) continue;
1150
+ copyDirContents(srcPath, destPath);
1176
1151
  } else {
1177
1152
  if (SKIP_EXT.some(ext => name.endsWith(ext))) continue;
1178
- if (stat.size > MAX_FILE_SIZE) continue;
1179
- try {
1180
- const content = fs.readFileSync(fullPath, 'utf8');
1181
- blobs.push({
1182
- path: path.relative(rootDir, fullPath).replace(/\\/g, '/'),
1183
- mode: '100644',
1184
- type: 'blob',
1185
- content
1186
- });
1187
- } catch (e) { }
1153
+ fs.copyFileSync(srcPath, destPath);
1188
1154
  }
1189
1155
  }
1190
- return blobs;
1191
1156
  }
1192
-
1193
- async function createGitHubBlob(token, repoName, blob) {
1194
- const [owner, repo] = repoName.split('/');
1195
- const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/blobs`, {
1196
- method: 'POST',
1197
- headers: {
1198
- 'Authorization': `Bearer ${token}`,
1199
- 'Content-Type': 'application/json'
1200
- },
1201
- body: JSON.stringify({
1202
- content: Buffer.from(blob.content).toString('base64'),
1203
- encoding: 'base64'
1204
- })
1205
- });
1206
-
1207
- if (!response.ok) {
1208
- const err = await response.text();
1209
- throw new Error(`Failed to create blob: ${err}`);
1210
- }
1211
-
1212
- const data = await response.json();
1213
- return data.sha;
1214
- }
1215
-
1216
- async function createGitHubTree(token, repoName, treeItems, baseSha = null) {
1217
- const [owner, repo] = repoName.split('/');
1218
- const body = {
1219
- tree: treeItems.map(item => ({
1220
- path: item.path,
1221
- mode: item.mode,
1222
- type: item.type,
1223
- sha: item.sha
1224
- }))
1225
- };
1226
- if (baseSha) body.base_tree = baseSha;
1227
-
1228
- const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees`, {
1229
- method: 'POST',
1230
- headers: {
1231
- 'Authorization': `Bearer ${token}`,
1232
- 'Content-Type': 'application/json'
1233
- },
1234
- body: JSON.stringify(body)
1235
- });
1236
-
1237
- if (!response.ok) {
1238
- const err = await response.text();
1239
- throw new Error(`Failed to create tree: ${err}`);
1240
- }
1241
-
1242
- const data = await response.json();
1243
- return data.sha;
1244
- }
1245
-
1246
- async function createGitHubCommit(token, repoName, treeSha, message) {
1247
- const [owner, repo] = repoName.split('/');
1248
-
1249
- const headRes = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/main`, {
1250
- headers: { 'Authorization': `Bearer ${token}` }
1251
- });
1252
-
1253
- let parentSha = null;
1254
- if (headRes.ok) {
1255
- const headData = await headRes.json();
1256
- parentSha = headData.object.sha;
1257
- }
1258
-
1259
- const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/commits`, {
1260
- method: 'POST',
1261
- headers: {
1262
- 'Authorization': `Bearer ${token}`,
1263
- 'Content-Type': 'application/json'
1264
- },
1265
- body: JSON.stringify({
1266
- message,
1267
- tree: treeSha,
1268
- parents: parentSha ? [parentSha] : []
1269
- })
1270
- });
1271
-
1272
- if (!response.ok) {
1273
- const err = await response.text();
1274
- throw new Error(`Failed to create commit: ${err}`);
1275
- }
1276
-
1277
- const data = await response.json();
1278
- return data.sha;
1279
- }
1280
-
1281
- async function updateGitHubRef(token, repoName, commitSha, branch = 'main') {
1282
- const [owner, repo] = repoName.split('/');
1283
-
1284
- const checkRes = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, {
1285
- headers: { 'Authorization': `Bearer ${token}` }
1286
- });
1287
-
1288
- if (checkRes.status === 404) {
1289
- const createRes = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs`, {
1290
- method: 'POST',
1291
- headers: {
1292
- 'Authorization': `Bearer ${token}`,
1293
- 'Content-Type': 'application/json'
1294
- },
1295
- body: JSON.stringify({
1296
- ref: `refs/heads/${branch}`,
1297
- sha: commitSha
1298
- })
1157
+
1158
+ function execPromise(cmd, opts = {}) {
1159
+ return new Promise((resolve, reject) => {
1160
+ exec(cmd, opts, (err, stdout, stderr) => {
1161
+ if (err) reject(new Error(stderr || err.message));
1162
+ else resolve(stdout.trim());
1299
1163
  });
1300
-
1301
- if (!createRes.ok) {
1302
- const err = await createRes.text();
1303
- throw new Error(`Failed to create ref: ${err}`);
1304
- }
1305
- return;
1306
- }
1307
-
1308
- const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, {
1309
- method: 'PATCH',
1310
- headers: {
1311
- 'Authorization': `Bearer ${token}`,
1312
- 'Content-Type': 'application/json'
1313
- },
1314
- body: JSON.stringify({
1315
- sha: commitSha
1316
- })
1317
1164
  });
1318
-
1319
- if (!response.ok) {
1320
- const err = await response.text();
1321
- throw new Error(`Failed to update ref: ${err}`);
1322
- }
1323
1165
  }
1324
1166
 
1325
1167
  app.get('/api/github/backup/status', async (req, res) => {
@@ -1353,67 +1195,77 @@ app.get('/api/github/backup/status', async (req, res) => {
1353
1195
  }
1354
1196
  });
1355
1197
 
1356
- app.post('/api/github/backup', async (req, res) => {
1357
- try {
1358
- const token = await getGitHubToken();
1359
- if (!token) return res.status(400).json({ error: 'Not logged in to gh CLI. Run: gh auth login' });
1360
-
1361
- const user = await getGitHubUser(token);
1362
- if (!user) return res.status(400).json({ error: 'Failed to get GitHub user' });
1363
-
1364
- const { owner, repo, branch } = req.body;
1365
- const studio = loadStudioConfig();
1366
-
1367
- const finalOwner = owner || studio.githubBackup?.owner || user.login;
1368
- const finalRepo = repo || studio.githubBackup?.repo;
1369
- const finalBranch = branch || studio.githubBackup?.branch || 'main';
1370
-
1371
- if (!finalRepo) return res.status(400).json({ error: 'No repo name provided' });
1372
-
1373
- const repoName = `${finalOwner}/${finalRepo}`;
1374
-
1375
- await ensureGitHubRepo(token, repoName);
1376
-
1377
- const opencodeConfig = getConfigPath();
1378
- if (!opencodeConfig) return res.status(400).json({ error: 'No opencode config path found' });
1379
-
1198
+ app.post('/api/github/backup', async (req, res) => {
1199
+ let tempDir = null;
1200
+ try {
1201
+ const token = await getGitHubToken();
1202
+ if (!token) return res.status(400).json({ error: 'Not logged in to gh CLI. Run: gh auth login' });
1203
+
1204
+ const user = await getGitHubUser(token);
1205
+ if (!user) return res.status(400).json({ error: 'Failed to get GitHub user' });
1206
+
1207
+ const { owner, repo, branch } = req.body;
1208
+ const studio = loadStudioConfig();
1209
+
1210
+ const finalOwner = owner || studio.githubBackup?.owner || user.login;
1211
+ const finalRepo = repo || studio.githubBackup?.repo;
1212
+ const finalBranch = branch || studio.githubBackup?.branch || 'main';
1213
+
1214
+ if (!finalRepo) return res.status(400).json({ error: 'No repo name provided' });
1215
+
1216
+ const repoName = `${finalOwner}/${finalRepo}`;
1217
+
1218
+ await ensureGitHubRepo(token, repoName);
1219
+
1220
+ const opencodeConfig = getConfigPath();
1221
+ if (!opencodeConfig) return res.status(400).json({ error: 'No opencode config path found' });
1222
+
1380
1223
  const opencodeDir = path.dirname(opencodeConfig);
1381
1224
  const studioDir = path.join(HOME_DIR, '.config', 'opencode-studio');
1382
1225
 
1383
- const opencodeBlobs = collectBlobs(opencodeDir);
1384
- const studioBlobs = collectBlobs(studioDir);
1226
+ tempDir = path.join(os.tmpdir(), `opencode-backup-${Date.now()}`);
1227
+ fs.mkdirSync(tempDir, { recursive: true });
1385
1228
 
1386
- for (const blob of opencodeBlobs) {
1387
- blob.sha = await createGitHubBlob(token, repoName, blob);
1388
- blob.path = `opencode/${blob.path}`;
1389
- delete blob.content;
1390
- }
1229
+ await execPromise(`git clone --depth 1 https://x-access-token:${token}@github.com/${repoName}.git .`, { cwd: tempDir });
1230
+
1231
+ const backupOpencodeDir = path.join(tempDir, 'opencode');
1232
+ const backupStudioDir = path.join(tempDir, 'opencode-studio');
1233
+
1234
+ if (fs.existsSync(backupOpencodeDir)) fs.rmSync(backupOpencodeDir, { recursive: true });
1235
+ if (fs.existsSync(backupStudioDir)) fs.rmSync(backupStudioDir, { recursive: true });
1236
+
1237
+ copyDirContents(opencodeDir, backupOpencodeDir);
1238
+ copyDirContents(studioDir, backupStudioDir);
1239
+
1240
+ await execPromise('git add -A', { cwd: tempDir });
1391
1241
 
1392
- for (const blob of studioBlobs) {
1393
- blob.sha = await createGitHubBlob(token, repoName, blob);
1394
- blob.path = `opencode-studio/${blob.path}`;
1395
- delete blob.content;
1242
+ const timestamp = new Date().toISOString();
1243
+ const commitMessage = `OpenCode Studio backup ${timestamp}`;
1244
+
1245
+ try {
1246
+ await execPromise(`git commit -m "${commitMessage}"`, { cwd: tempDir });
1247
+ await execPromise(`git push origin ${finalBranch}`, { cwd: tempDir });
1248
+ } catch (e) {
1249
+ if (e.message.includes('nothing to commit')) {
1250
+ fs.rmSync(tempDir, { recursive: true });
1251
+ return res.json({ success: true, timestamp, message: 'No changes to backup', url: `https://github.com/${repoName}` });
1252
+ }
1253
+ throw e;
1396
1254
  }
1397
1255
 
1398
- const allBlobs = [...opencodeBlobs, ...studioBlobs];
1399
- const rootTreeSha = await createGitHubTree(token, repoName, allBlobs);
1400
-
1401
- const timestamp = new Date().toISOString();
1402
- const commitMessage = `OpenCode Studio backup ${timestamp}`;
1403
- const commitSha = await createGitHubCommit(token, repoName, rootTreeSha, commitMessage);
1404
-
1405
- await updateGitHubRef(token, repoName, commitSha, finalBranch);
1406
-
1407
- studio.githubBackup = { owner: finalOwner, repo: finalRepo, branch: finalBranch };
1408
- studio.lastGithubBackup = timestamp;
1409
- saveStudioConfig(studio);
1410
-
1411
- res.json({ success: true, timestamp, commit: commitSha, url: `https://github.com/${repoName}` });
1412
- } catch (err) {
1413
- console.error('GitHub backup error:', err);
1414
- res.status(500).json({ error: err.message });
1415
- }
1416
- });
1256
+ fs.rmSync(tempDir, { recursive: true });
1257
+
1258
+ studio.githubBackup = { owner: finalOwner, repo: finalRepo, branch: finalBranch };
1259
+ studio.lastGithubBackup = timestamp;
1260
+ saveStudioConfig(studio);
1261
+
1262
+ res.json({ success: true, timestamp, url: `https://github.com/${repoName}` });
1263
+ } catch (err) {
1264
+ if (tempDir && fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true });
1265
+ console.error('GitHub backup error:', err);
1266
+ res.status(500).json({ error: err.message });
1267
+ }
1268
+ });
1417
1269
 
1418
1270
  const getSkillDir = () => {
1419
1271
  const cp = getConfigPath();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-studio-server",
3
- "version": "1.24.0",
3
+ "version": "1.26.0",
4
4
  "description": "Backend server for OpenCode Studio - manages opencode configurations",
5
5
  "main": "index.js",
6
6
  "bin": {