@tiens.nguyen/gu-cli 1.0.917 → 1.0.918
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/gu-cli.mjs +18 -0
- package/package.json +2 -1
- package/setup-command.mjs +259 -0
package/gu-cli.mjs
CHANGED
|
@@ -120,6 +120,7 @@ Usage:
|
|
|
120
120
|
gu-cli login [--api-base <url>] # browser sign-in → writes ~/.gonext/worker.env
|
|
121
121
|
gu-cli token [<token>] # show this computer's gu token, or save one pasted from
|
|
122
122
|
# the Account page (Client Expert machines)
|
|
123
|
+
gu-cli setup --provider <name|url> --key <key> [--model <name>] # first-run key setup in one line (deepseek, claude, openai, kimi, qwen-…)
|
|
123
124
|
gu-cli set <workerKey> [--api-base <url>]
|
|
124
125
|
gu-cli start | stop | status | restart # run the polling daemon in the background
|
|
125
126
|
gu-cli status # daemon + a table of the running model servers
|
|
@@ -288,6 +289,23 @@ if (args[0] === "hooks") {
|
|
|
288
289
|
process.exit(code);
|
|
289
290
|
}
|
|
290
291
|
|
|
292
|
+
if (args[0] === "setup") {
|
|
293
|
+
// #494: the first-run key questions as one line — see setup-command.mjs. Needs gu already installed here.
|
|
294
|
+
const sc = await import("./setup-command.mjs");
|
|
295
|
+
const { hostname } = await import("node:os");
|
|
296
|
+
const apiBase = process.env.GONEXT_API_BASE;
|
|
297
|
+
const workerKey = process.env.GONEXT_WORKER_KEY;
|
|
298
|
+
const code = await sc.runSetup({
|
|
299
|
+
args: args.slice(1),
|
|
300
|
+
env: process.env,
|
|
301
|
+
api: sc.localApi({ apiBase, workerKey }),
|
|
302
|
+
say: (line, tone) => (tone === "err" ? console.error : console.log)(line),
|
|
303
|
+
backupDir: join(homedir(), ".gonext", "preset-backups"),
|
|
304
|
+
host: hostname(),
|
|
305
|
+
apiReady: Boolean(apiBase && workerKey),
|
|
306
|
+
});
|
|
307
|
+
process.exit(code);
|
|
308
|
+
}
|
|
291
309
|
if (args[0] === "full-log") {
|
|
292
310
|
// #250. An administrator switches Enable Full Log (#249) from a terminal — after typing an admin
|
|
293
311
|
// token minted on the web admin page. Same code as /full-log in the terminal UI.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiens.nguyen/gu-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.918",
|
|
4
4
|
"description": "gu: the coding agent in your terminal. It reads your repo, makes the change and runs the tests, with your own models (Ollama or any OpenAI-compatible provider). Free installs keep the agent, its API and its database on your machine. macOS, Linux and Windows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -84,6 +84,7 @@
|
|
|
84
84
|
"turn-queue.mjs",
|
|
85
85
|
"free-model.mjs",
|
|
86
86
|
"first-hi.mjs",
|
|
87
|
+
"setup-command.mjs",
|
|
87
88
|
"terminal-viewport.mjs",
|
|
88
89
|
"terminal-copy.mjs",
|
|
89
90
|
"version-check.mjs",
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `gu setup --provider deepseek --key sk-… [--model NAME] [--dry-run]` (GH #494)
|
|
3
|
+
*
|
|
4
|
+
* The first-run questions, as ONE line — for servers over SSH, a support answer, or the website's "copy this".
|
|
5
|
+
* It runs on a machine gu is already installed on (the local API is up); on a new one, start gu once first.
|
|
6
|
+
*
|
|
7
|
+
* It does what the first-run key path does, with the same safeguards, and nothing more:
|
|
8
|
+
* 1. the provider: a name (deepseek, claude, openai, kimi, qwen-token-plan, qwen-payg), a URL, or — with no
|
|
9
|
+
* --provider — what the key's shape says (never guessed for a shape several providers share);
|
|
10
|
+
* 2. the key is checked by listing the provider's models with it (a refusal is explained, not retried);
|
|
11
|
+
* 3. the model: --model, else the first of a few known families the provider ACTUALLY lists, else the only
|
|
12
|
+
* one it lists; otherwise the command stops and shows the list. A name is never invented;
|
|
13
|
+
* 4. one server is added through the same code as /use-presets (backup first, keys bound to the server);
|
|
14
|
+
* 5. unless --no-check, the model is asked "hi" once (first-hi.mjs).
|
|
15
|
+
*
|
|
16
|
+
* THE KEY: taken from --key, or from the environment (GU_SETUP_KEY) — the latter keeps it out of the shell
|
|
17
|
+
* history and the process list. It is never printed, and is scrubbed from every error.
|
|
18
|
+
*/
|
|
19
|
+
import { providerPresets, detectProvider, modelListRequest, parseProviderModels, codingModelCandidates, normalizeProviderUrl } from "./coding-provider-setup.mjs";
|
|
20
|
+
import { validateFullPreset } from "./presets-transfer.mjs";
|
|
21
|
+
import { applyTransfer } from "./preset-apply.mjs";
|
|
22
|
+
import { checkModel, scrub, explain, resultLine } from "./first-hi.mjs";
|
|
23
|
+
|
|
24
|
+
export const SETUP_USAGE = [
|
|
25
|
+
"usage: gu setup --provider <name|url> --key <key> [--model <name>] [--dry-run] [--no-check]",
|
|
26
|
+
" providers: deepseek, claude, openai, kimi, qwen-token-plan, qwen-payg, or an https:// URL",
|
|
27
|
+
" the key may also come from the environment: GU_SETUP_KEY=… gu setup --provider deepseek",
|
|
28
|
+
" (a key on the command line stays in your shell history)",
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/** Names people type → the preset's own name. */
|
|
32
|
+
const ALIASES = new Map([
|
|
33
|
+
["deepseek", "DeepSeek"],
|
|
34
|
+
["claude", "Claude API (Anthropic)"],
|
|
35
|
+
["anthropic", "Claude API (Anthropic)"],
|
|
36
|
+
["openai", "OpenAI"],
|
|
37
|
+
["kimi", "Kimi (Moonshot)"],
|
|
38
|
+
["moonshot", "Kimi (Moonshot)"],
|
|
39
|
+
["qwen-token-plan", "Qwen Token Plan (OpenAI-compatible)"],
|
|
40
|
+
["qwen-payg", "Qwen pay-as-you-go (OpenAI-compatible)"],
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
/** The model to use when --model is not given: the first pattern that matches something the provider LISTS. */
|
|
44
|
+
const RECOMMENDED = new Map([
|
|
45
|
+
["DeepSeek", [/^deepseek-chat$/, /^deepseek-v[\d.]+$/]],
|
|
46
|
+
["Claude API (Anthropic)", [/sonnet/]],
|
|
47
|
+
["Kimi (Moonshot)", [/^kimi-k[\d.]+$/]],
|
|
48
|
+
["OpenAI", []],
|
|
49
|
+
["Qwen Token Plan (OpenAI-compatible)", [/coder/]],
|
|
50
|
+
["Qwen pay-as-you-go (OpenAI-compatible)", [/coder/]],
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
/** → { provider, key, model, dryRun, check, error } */
|
|
54
|
+
export function parseSetupArgs(args = [], env = {}) {
|
|
55
|
+
const out = { provider: "", key: "", model: "", dryRun: false, check: true, error: "" };
|
|
56
|
+
const list = Array.isArray(args) ? [...args] : [];
|
|
57
|
+
const need = (flag, i) => {
|
|
58
|
+
const v = list[i + 1];
|
|
59
|
+
if (v === undefined || v.startsWith("--")) {
|
|
60
|
+
out.error = `${flag} needs a value.`;
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
return v;
|
|
64
|
+
};
|
|
65
|
+
for (let i = 0; i < list.length && !out.error; i++) {
|
|
66
|
+
const a = list[i];
|
|
67
|
+
const eq = a.indexOf("=");
|
|
68
|
+
const [flag, inline] = a.startsWith("--") && eq > 0 ? [a.slice(0, eq), a.slice(eq + 1)] : [a, null];
|
|
69
|
+
if (flag === "--provider" || flag === "--key" || flag === "--model") {
|
|
70
|
+
const v = inline ?? need(flag, i);
|
|
71
|
+
if (inline === null) i++;
|
|
72
|
+
if (out.error) break;
|
|
73
|
+
out[flag.slice(2)] = String(v).trim();
|
|
74
|
+
} else if (flag === "--dry-run") out.dryRun = true;
|
|
75
|
+
else if (flag === "--no-check") out.check = false;
|
|
76
|
+
else out.error = `unknown option ${a}.`;
|
|
77
|
+
}
|
|
78
|
+
if (!out.error && !out.key) out.key = String(env.GU_SETUP_KEY ?? "").trim();
|
|
79
|
+
if (!out.error && !out.key) out.error = "no key: give --key, or set GU_SETUP_KEY.";
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The provider asked for → { preset } | { custom: url } | { error }. */
|
|
84
|
+
export function resolveProvider(name, key, presets = providerPresets("coding")) {
|
|
85
|
+
const n = String(name ?? "").trim();
|
|
86
|
+
if (!n) {
|
|
87
|
+
const guess = detectProvider(key, presets);
|
|
88
|
+
return guess
|
|
89
|
+
? { preset: guess, guessed: true }
|
|
90
|
+
: { error: "can't tell the provider from that key — add --provider (deepseek, claude, openai, kimi, qwen-token-plan, qwen-payg, or a URL)." };
|
|
91
|
+
}
|
|
92
|
+
const aliased = ALIASES.get(n.toLowerCase());
|
|
93
|
+
if (aliased) return { preset: presets.find((p) => p.name === aliased) };
|
|
94
|
+
if (n.toLowerCase() === "qwen") {
|
|
95
|
+
const g = detectProvider(key, presets);
|
|
96
|
+
return g && /^Qwen/.test(g.name)
|
|
97
|
+
? { preset: g, guessed: true }
|
|
98
|
+
: { error: "Qwen has two plans: use --provider qwen-token-plan (key sk-sp-…) or qwen-payg (key sk-ws-…)." };
|
|
99
|
+
}
|
|
100
|
+
if (/^https?:\/\//i.test(n)) {
|
|
101
|
+
if (!/^https:\/\//i.test(n)) return { error: "a key is only sent over https://." };
|
|
102
|
+
const norm = normalizeProviderUrl(n);
|
|
103
|
+
return norm.ok ? { custom: norm.url.replace(/\/+$/, "") } : { error: `${n} is not a usable address (${norm.why}).` };
|
|
104
|
+
}
|
|
105
|
+
return { error: `unknown provider "${n}". Known: ${[...ALIASES.keys()].join(", ")}, or an https:// URL.` };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The model to use → { model } | { error, list }. `ids` = what the provider lists. */
|
|
109
|
+
export function chooseModel({ asked, ids, presetName }) {
|
|
110
|
+
const { models } = codingModelCandidates(ids);
|
|
111
|
+
if (asked) {
|
|
112
|
+
if (ids.length && !ids.includes(asked)) {
|
|
113
|
+
return { error: `${asked} is not one of the models it lists.`, list: models.slice(0, 15) };
|
|
114
|
+
}
|
|
115
|
+
return { model: asked };
|
|
116
|
+
}
|
|
117
|
+
for (const re of RECOMMENDED.get(presetName) ?? []) {
|
|
118
|
+
const hit = models.filter((m) => re.test(m)).sort().pop();
|
|
119
|
+
if (hit) return { model: hit, recommended: true };
|
|
120
|
+
}
|
|
121
|
+
if (models.length === 1) return { model: models[0], recommended: true };
|
|
122
|
+
return { error: "pick a model with --model NAME.", list: models.slice(0, 15) };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The version-2 preset for one server. */
|
|
126
|
+
export function buildSetupPreset({ url, model, key, host }) {
|
|
127
|
+
return {
|
|
128
|
+
gu: "preset",
|
|
129
|
+
version: 2,
|
|
130
|
+
name: "gu setup",
|
|
131
|
+
codingDefault: { url, model, kind: "openai" },
|
|
132
|
+
codingBackends: [{ kind: "openai", url, defaultModel: model, models: [model], enabled: true }],
|
|
133
|
+
agentModel: { url: "", name: "" },
|
|
134
|
+
embedModel: { url: "", name: "" },
|
|
135
|
+
keys: [{ name: host, backendUrl: url, default: true, key }],
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The REST calls preset-apply needs, over the local API. */
|
|
140
|
+
export function localApi({ apiBase, workerKey, fetchImpl = fetch }) {
|
|
141
|
+
const h = { "X-Worker-Key": workerKey };
|
|
142
|
+
const j = async (path, init = {}) => {
|
|
143
|
+
try {
|
|
144
|
+
const r = await fetchImpl(`${apiBase}${path}`, { ...init, headers: { ...h, ...(init.headers ?? {}) }, signal: AbortSignal.timeout(15_000) });
|
|
145
|
+
return r.ok ? await r.json().catch(() => ({})) : null;
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const body = (b) => ({ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(b) });
|
|
151
|
+
return {
|
|
152
|
+
settings: () => j("/api/worker/settings"),
|
|
153
|
+
keys: async () => (await j("/api/worker/coding-keys"))?.keys ?? null,
|
|
154
|
+
keyFor: async (id) => {
|
|
155
|
+
const r = await j(`/api/worker/coding-key?backendId=${encodeURIComponent(id)}`);
|
|
156
|
+
return typeof r?.apiKey === "string" && r.apiKey ? r : null;
|
|
157
|
+
},
|
|
158
|
+
saveSettings: async (patch) => (await j("/api/worker/settings", body(patch))) !== null,
|
|
159
|
+
addKey: async (name, apiKey, backendId = "") => {
|
|
160
|
+
const r = await j("/api/worker/coding-keys", body({ name, apiKey, ...(backendId ? { backendId } : {}) }));
|
|
161
|
+
return typeof r?.id === "string" ? r.id : "";
|
|
162
|
+
},
|
|
163
|
+
setDefaultKey: async (backendId, keyId) =>
|
|
164
|
+
(await j("/api/worker/server-default-key", { ...body({ backendId, keyId }), method: "PUT" })) !== null,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Run it. → exit code. `say(line, tone)` prints; `api` is localApi() (or a fake in tests).
|
|
170
|
+
*/
|
|
171
|
+
export async function runSetup({ args, env = {}, api, say, fetchImpl = fetch, backupDir = "", host = "", apiReady = true }) {
|
|
172
|
+
const a = parseSetupArgs(args, env);
|
|
173
|
+
if (a.error) {
|
|
174
|
+
say(a.error, "err");
|
|
175
|
+
for (const l of SETUP_USAGE) say(l);
|
|
176
|
+
return 2;
|
|
177
|
+
}
|
|
178
|
+
const key = a.key;
|
|
179
|
+
const clean = (t) => scrub(t, key);
|
|
180
|
+
if (!apiReady) {
|
|
181
|
+
say("gu is not set up on this machine yet — run `gu` once to finish the install, then run this again.", "err");
|
|
182
|
+
return 1;
|
|
183
|
+
}
|
|
184
|
+
const who = resolveProvider(a.provider, key);
|
|
185
|
+
if (who.error) {
|
|
186
|
+
say(who.error, "err");
|
|
187
|
+
return 2;
|
|
188
|
+
}
|
|
189
|
+
const url = who.custom ?? who.preset.url;
|
|
190
|
+
const presetName = who.preset?.name ?? "";
|
|
191
|
+
const hostName = (() => { try { return new URL(url).host; } catch { return url; } })();
|
|
192
|
+
if (who.guessed) say(`the key looks like a ${presetName} key — using ${hostName}.`);
|
|
193
|
+
|
|
194
|
+
// 2. the key, checked by listing the models with it
|
|
195
|
+
const req = modelListRequest(url, key);
|
|
196
|
+
let status = 0;
|
|
197
|
+
let body = null;
|
|
198
|
+
try {
|
|
199
|
+
const res = await fetchImpl(req.url, { headers: req.headers, signal: AbortSignal.timeout(15_000) });
|
|
200
|
+
status = res.status;
|
|
201
|
+
body = res.ok ? await res.json().catch(() => null) : null;
|
|
202
|
+
} catch (e) {
|
|
203
|
+
say(clean(`could not reach ${hostName}: ${e instanceof Error ? e.message : String(e)}`), "err");
|
|
204
|
+
return 1;
|
|
205
|
+
}
|
|
206
|
+
if (status && !(status >= 200 && status < 300)) {
|
|
207
|
+
say(clean(`${hostName}: ${explain(status)}`), "err");
|
|
208
|
+
return 1;
|
|
209
|
+
}
|
|
210
|
+
const ids = parseProviderModels(body).models;
|
|
211
|
+
|
|
212
|
+
// 3. the model
|
|
213
|
+
const m = chooseModel({ asked: a.model, ids, presetName });
|
|
214
|
+
if (m.error) {
|
|
215
|
+
say(m.error, "err");
|
|
216
|
+
if (m.list?.length) {
|
|
217
|
+
say(`${hostName} lists:`);
|
|
218
|
+
for (const id of m.list) say(` ${id}`);
|
|
219
|
+
say("run again with --model NAME.");
|
|
220
|
+
}
|
|
221
|
+
return 2;
|
|
222
|
+
}
|
|
223
|
+
say(`model: ${m.model}${m.recommended ? " (change it with --model NAME, or /model in gu)" : ""}`);
|
|
224
|
+
|
|
225
|
+
// 4. add it — the same path as /use-presets, so a backup is made first
|
|
226
|
+
const doc = buildSetupPreset({ url, model: m.model, key, host: hostName });
|
|
227
|
+
const parsed = validateFullPreset(doc);
|
|
228
|
+
if (!parsed.ok) {
|
|
229
|
+
say(clean(parsed.problem), "err");
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
if (a.dryRun) {
|
|
233
|
+
say(`dry run: would add ${hostName} with ${m.model} as the coding model. Nothing changed.`, "ok");
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
const r = await applyTransfer(parsed.value, {
|
|
237
|
+
api,
|
|
238
|
+
io: { ask: async () => "y", say: (l, t) => say(clean(l), t) },
|
|
239
|
+
codingDefault: parsed.codingDefault,
|
|
240
|
+
warnings: parsed.warnings,
|
|
241
|
+
carriesSecrets: false,
|
|
242
|
+
backupName: "setup",
|
|
243
|
+
backupDir,
|
|
244
|
+
host,
|
|
245
|
+
});
|
|
246
|
+
if (!r.applied || r.step !== "done" || r.failed.length) {
|
|
247
|
+
say("setup did not finish — see above. Nothing is left half-set: the previous setup is backed up.", "err");
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// 5. one real "hi"
|
|
252
|
+
if (a.check) {
|
|
253
|
+
const c = await checkModel({ url, model: m.model, key, fetchImpl });
|
|
254
|
+
say(clean(resultLine(c, m.model)), c.ok ? "ok" : "err");
|
|
255
|
+
if (!c.ok) return 1;
|
|
256
|
+
}
|
|
257
|
+
say("done. Start gu and ask a question.", "ok");
|
|
258
|
+
return 0;
|
|
259
|
+
}
|