@tiens.nguyen/gu-cli 1.0.765 → 1.0.767

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.
@@ -23,6 +23,7 @@ import { spawnSurviving, pidOnPort } from "./platform-tools.mjs";
23
23
  import { DEFAULT_API_BASE as HOSTED_API_BASE } from "./device-login.mjs";
24
24
  import { spawn } from "node:child_process";
25
25
  import { writeFile, mkdir, readFile, access } from "node:fs/promises";
26
+ import { readFileSync } from "node:fs";
26
27
  import { homedir } from "node:os";
27
28
  import { join } from "node:path";
28
29
 
@@ -32,6 +33,29 @@ export const LOCAL_API_BASE = `http://127.0.0.1:${LOCAL_API_PORT}`;
32
33
  export const LOCAL_MONGO_URI = process.env.GONEXT_LOCAL_MONGO_URI || "mongodb://127.0.0.1:27017";
33
34
  export const LOCAL_DB_NAME = process.env.GONEXT_LOCAL_DB_NAME || "gu";
34
35
 
36
+ /**
37
+ * THE URI THIS MACHINE'S MONGODB ACTUALLY ACCEPTS — credentials included (task #15).
38
+ *
39
+ * Setup asks for a username and password when 27017 already belongs to a MongoDB with its own
40
+ * admin, and saves the working URI as localMongoUri in client-expert.json. Every later caller
41
+ * used LOCAL_MONGO_URI instead — the empty pair — so on such a machine the model seed failed
42
+ * with "Command find requires authentication" (reported on a fresh Ubuntu box: coder "—", "agent
43
+ * model is not set"), and every API restart started a server that could not read its own
44
+ * database. Read here, once, as every default, so no caller can forget it.
45
+ *
46
+ * GONEXT_LOCAL_MONGO_URI still wins: setup treats a forced URI as deliberate and never saves one.
47
+ */
48
+ export function localMongoUri({ env = process.env, home = homedir() } = {}) {
49
+ if (env.GONEXT_LOCAL_MONGO_URI) return env.GONEXT_LOCAL_MONGO_URI;
50
+ try {
51
+ const st = JSON.parse(readFileSync(join(home, ".gonext", "client-expert.json"), "utf8"));
52
+ if (typeof st?.localMongoUri === "string" && st.localMongoUri) return st.localMongoUri;
53
+ } catch {
54
+ /* no setup yet, or unreadable — the default is the only thing there is to try */
55
+ }
56
+ return LOCAL_MONGO_URI;
57
+ }
58
+
35
59
  const GONEXT_DIR = join(homedir(), ".gonext");
36
60
  export const API_PID_FILE = join(GONEXT_DIR, "local-api.pid");
37
61
  export const API_LOG_FILE = join(GONEXT_DIR, "local-api.log");
@@ -62,7 +86,7 @@ export function generateLocalUserId() {
62
86
  */
63
87
  export function apiEnv({
64
88
  port = LOCAL_API_PORT,
65
- mongoUri = LOCAL_MONGO_URI,
89
+ mongoUri = localMongoUri(),
66
90
  dbName = LOCAL_DB_NAME,
67
91
  base = {},
68
92
  hostedBase = hostedApiBase(),
@@ -201,7 +225,7 @@ export function installApiDeps(apiDir, { onLine } = {}) {
201
225
  */
202
226
  export async function seedDatabase({
203
227
  apiDir,
204
- mongoUri = LOCAL_MONGO_URI,
228
+ mongoUri = localMongoUri(),
205
229
  dbName = LOCAL_DB_NAME,
206
230
  userId,
207
231
  workerKey,
@@ -245,7 +269,7 @@ export async function seedDatabase({
245
269
  export async function startLocalApi({
246
270
  apiDir,
247
271
  port = LOCAL_API_PORT,
248
- mongoUri = LOCAL_MONGO_URI,
272
+ mongoUri = localMongoUri(),
249
273
  dbName = LOCAL_DB_NAME,
250
274
  } = {}) {
251
275
  await mkdir(GONEXT_DIR, { recursive: true });
@@ -700,7 +724,7 @@ export function settingsPatch({ existing = {}, models = CLIENT_EXPERT_MODELS } =
700
724
  */
701
725
  export async function modelsAlreadyConfigured({
702
726
  apiDir,
703
- mongoUri = LOCAL_MONGO_URI,
727
+ mongoUri = localMongoUri(),
704
728
  dbName = LOCAL_DB_NAME,
705
729
  userId,
706
730
  } = {}) {
@@ -721,7 +745,7 @@ export async function modelsAlreadyConfigured({
721
745
 
722
746
  export async function seedSettings({
723
747
  apiDir,
724
- mongoUri = LOCAL_MONGO_URI,
748
+ mongoUri = localMongoUri(),
725
749
  dbName = LOCAL_DB_NAME,
726
750
  userId,
727
751
  // The box the user chose at setup (task #220), or the shared-Ollama defaults when the
@@ -894,7 +894,7 @@ export async function runClientExpertSetup({ ask }) {
894
894
  // machine alone (that rule protects anyone who set their models up by hand), so on a re-run
895
895
  // of this step the answer would be collected and silently discarded. Point at `/model`
896
896
  // instead — it is the one path that CAN change a machine that is already set up.
897
- const already = await modelsAlreadyConfigured({ apiDir, userId: creds.userId });
897
+ const already = await modelsAlreadyConfigured({ apiDir, mongoUri, userId: creds.userId });
898
898
  if (already) {
899
899
  console.log("");
900
900
  console.log(dim(" Models are already configured on this machine — leaving them alone."));
@@ -946,6 +946,7 @@ export async function runClientExpertSetup({ ask }) {
946
946
  try {
947
947
  const written = await seedSettings({
948
948
  apiDir,
949
+ mongoUri,
949
950
  userId: creds.userId,
950
951
  ...(chosenModels ? { models: chosenModels } : {}),
951
952
  });
package/gu-cli.mjs CHANGED
@@ -1229,7 +1229,7 @@ if (args[0] === "clear" || args[0] === "reset") {
1229
1229
  try {
1230
1230
  const ce = await import("./client-expert-api.mjs");
1231
1231
  const { MongoClient } = ce.loadMongoDriver(apiDirForDrop);
1232
- const client = new MongoClient(ce.LOCAL_MONGO_URI, { serverSelectionTimeoutMS: 8000 });
1232
+ const client = new MongoClient(ce.localMongoUri(), { serverSelectionTimeoutMS: 8000 });
1233
1233
  await client.connect();
1234
1234
  await client.db(ce.LOCAL_DB_NAME).dropDatabase();
1235
1235
  await client.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiens.nguyen/gu-cli",
3
- "version": "1.0.765",
3
+ "version": "1.0.767",
4
4
  "description": "Gu CLI — the gu terminal plus the local worker that runs agent / OCR / PDF / embedding jobs on your Mac (Ollama / MLX / OpenAI-compatible).",
5
5
  "type": "module",
6
6
  "license": "MIT",