forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch.js
More file actions
78 lines (69 loc) · 3.32 KB
/
patch.js
File metadata and controls
78 lines (69 loc) · 3.32 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
const fs = require('fs');
const filePath = '/Users/uenyioha/tmp/opencode-anthropic-auth-gitea/index.mjs';
let content = fs.readFileSync(filePath, 'utf8');
const oldInstructions = 'instructions: `Accounts:\\n${lines.join("\\n")}\\n\\nEnter number to remove, or press Enter to cancel:`,';
const newInstructions = 'instructions: `Accounts:\\n${lines.join("\\n")}\\n\\nEnter a number to select as active (e.g., "1" or "sel 1"), or prefix with "rm " to remove (e.g., "rm 1"). Press Enter to cancel:`,';
const oldCallback = ` callback: async (answer) => {
if (answer) {
const index = Number.parseInt(answer, 10) - 1;
if (
!Number.isNaN(index) &&
index >= 0 &&
index < data.accounts.length
) {
removeAccount(index);
}
}
return getExistingOAuthResult();
},`;
const newCallback = ` callback: async (answer) => {
if (answer) {
const trimmed = answer.trim();
const isRm = trimmed.startsWith("rm ");
const isSel = trimmed.startsWith("sel ");
let numStr = trimmed;
if (isRm) numStr = trimmed.substring(3).trim();
else if (isSel) numStr = trimmed.substring(4).trim();
const index = Number.parseInt(numStr, 10) - 1;
if (
!Number.isNaN(index) &&
index >= 0 &&
index < data.accounts.length
) {
if (isRm) {
removeAccount(index);
} else {
const currentData = loadAccounts();
currentData.activeIndex = index;
saveAccounts(currentData);
const active = currentData.accounts[index];
if (active) {
try {
await authClient.auth.set({
path: { id: "anthropic" },
body: {
type: "oauth",
refresh: active.refresh,
access: active.access,
expires: active.expires,
},
});
} catch (e) {
console.warn("Failed to sync active account to authClient", e);
}
}
}
}
}
return getExistingOAuthResult();
},`;
if (content.includes(oldInstructions) && content.includes(oldCallback)) {
content = content.replace(oldInstructions, newInstructions);
content = content.replace(oldCallback, newCallback);
fs.writeFileSync(filePath, content, 'utf8');
console.log('Successfully patched index.mjs');
} else {
console.error('Could not find the target strings to replace in index.mjs');
if (!content.includes(oldInstructions)) console.error('oldInstructions not found');
if (!content.includes(oldCallback)) console.error('oldCallback not found');
}