-
-
Notifications
You must be signed in to change notification settings - Fork 762
Expand file tree
/
Copy pathcli.ts
More file actions
173 lines (150 loc) · 4.93 KB
/
cli.ts
File metadata and controls
173 lines (150 loc) · 4.93 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
import { createInterface } from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import {
showAuthMenu,
showAccountDetails,
isTTY,
type AccountInfo,
type AccountStatus,
} from "./ui/auth-menu";
import { updateOpencodeConfig } from "./config/updater";
export async function promptProjectId(): Promise<string> {
const rl = createInterface({ input, output });
try {
const answer = await rl.question("Project ID (leave blank to use your default project): ");
return answer.trim();
} finally {
rl.close();
}
}
export async function promptAddAnotherAccount(currentCount: number): Promise<boolean> {
const rl = createInterface({ input, output });
try {
const answer = await rl.question(`Add another account? (${currentCount} added) (y/n): `);
const normalized = answer.trim().toLowerCase();
return normalized === "y" || normalized === "yes";
} finally {
rl.close();
}
}
export async function promptContinue(): Promise<void> {
if (!isTTY()) return;
const rl = createInterface({ input, output });
try {
await rl.question("Press Enter to return to menu...");
} finally {
rl.close();
}
}
export type LoginMode = "add" | "fresh" | "manage" | "check" | "verify" | "verify-all" | "cancel";
export interface ExistingAccountInfo {
email?: string;
index: number;
addedAt?: number;
lastUsed?: number;
status?: AccountStatus;
isCurrentAccount?: boolean;
enabled?: boolean;
}
export interface LoginMenuResult {
mode: LoginMode;
deleteAccountIndex?: number;
refreshAccountIndex?: number;
toggleAccountIndex?: number;
verifyAccountIndex?: number;
verifyAll?: boolean;
deleteAll?: boolean;
}
async function promptLoginModeFallback(existingAccounts: ExistingAccountInfo[]): Promise<LoginMenuResult> {
const rl = createInterface({ input, output });
try {
console.log(`\n${existingAccounts.length} account(s) saved:`);
for (const acc of existingAccounts) {
const label = acc.email || `Account ${acc.index + 1}`;
console.log(` ${acc.index + 1}. ${label}`);
}
console.log("");
while (true) {
const answer = await rl.question("(a)dd new, (f)resh start, (c)heck quotas, (v)erify account, (va) verify all? [a/f/c/v/va]: ");
const normalized = answer.trim().toLowerCase();
if (normalized === "a" || normalized === "add") {
return { mode: "add" };
}
if (normalized === "f" || normalized === "fresh") {
return { mode: "fresh" };
}
if (normalized === "c" || normalized === "check") {
return { mode: "check" };
}
if (normalized === "v" || normalized === "verify") {
return { mode: "verify" };
}
if (normalized === "va" || normalized === "verify-all" || normalized === "all") {
return { mode: "verify-all", verifyAll: true };
}
console.log("Please enter 'a', 'f', 'c', 'v', or 'va'.");
}
} finally {
rl.close();
}
}
export async function promptLoginMode(existingAccounts: ExistingAccountInfo[]): Promise<LoginMenuResult> {
if (!isTTY()) {
return promptLoginModeFallback(existingAccounts);
}
const accounts: AccountInfo[] = existingAccounts.map(acc => ({
email: acc.email,
index: acc.index,
addedAt: acc.addedAt,
lastUsed: acc.lastUsed,
status: acc.status,
isCurrentAccount: acc.isCurrentAccount,
enabled: acc.enabled,
}));
console.log("");
while (true) {
const action = await showAuthMenu(accounts);
switch (action.type) {
case "add":
return { mode: "add" };
case "check":
return { mode: "check" };
case "verify":
return { mode: "verify" };
case "verify-all":
return { mode: "verify-all", verifyAll: true };
case "select-account": {
const accountAction = await showAccountDetails(action.account);
if (accountAction === "delete") {
return { mode: "add", deleteAccountIndex: action.account.index };
}
if (accountAction === "refresh") {
return { mode: "add", refreshAccountIndex: action.account.index };
}
if (accountAction === "toggle") {
return { mode: "manage", toggleAccountIndex: action.account.index };
}
if (accountAction === "verify") {
return { mode: "verify", verifyAccountIndex: action.account.index };
}
continue;
}
case "delete-all":
return { mode: "fresh", deleteAll: true };
case "configure-models": {
const result = await updateOpencodeConfig();
if (result.success) {
console.log(`\n✓ Models configured in ${result.configPath}\n`);
} else {
console.log(`\n✗ Failed to configure models: ${result.error}\n`);
}
await promptContinue();
continue;
}
case "cancel":
return { mode: "cancel" };
}
}
}
export { isTTY } from "./ui/auth-menu";
export type { AccountStatus } from "./ui/auth-menu";