File size: 2,024 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
import { join, dirname } from "node:path";
import { resolveDataDir } from "./data-dir.mjs";

const CONFIG_VERSION = 1;

export function configPath() {
  return join(resolveDataDir(), "config.json");
}

function defaultConfig() {
  return {
    version: CONFIG_VERSION,
    currentContext: "default",
    contexts: {
      default: { baseUrl: `http://localhost:${process.env.PORT || "20128"}`, apiKey: null },
    },
  };
}

export function loadContexts() {
  try {
    if (!existsSync(configPath())) return defaultConfig();
    return JSON.parse(readFileSync(configPath(), "utf8"));
  } catch {
    return defaultConfig();
  }
}

export function saveContexts(cfg) {
  const path = configPath();
  mkdirSync(dirname(path), { recursive: true });
  writeFileSync(path, JSON.stringify(cfg, null, 2));
  try {
    chmodSync(path, 0o600);
  } catch {}
}

/**
 * Resolve the active context for a CLI invocation.
 *
 * Canonical schema is `{ currentContext, contexts }` (written by
 * `omniroute contexts ...`). For backward compatibility we also read the legacy
 * `{ activeProfile, profiles }` shape and a bare top-level `baseUrl` — older
 * configs and `api.mjs::getBaseUrl` used those before remote-mode unified the
 * store. `overrideName` (from `--context`/`OMNIROUTE_CONTEXT`) wins when set.
 *
 * A context may carry `{ baseUrl, accessToken?, apiKey?, scope?, description? }`.
 * `accessToken` is the scoped CLI access token (preferred); `apiKey` is the
 * legacy inference key kept for back-compat.
 */
export function resolveActiveContext(overrideName) {
  const cfg = loadContexts();
  const contexts = cfg.contexts || cfg.profiles || {};
  const name = overrideName || cfg.currentContext || cfg.activeProfile || "default";
  const found = contexts[name] || contexts.default;
  if (found) return found;
  if (cfg.baseUrl) return { baseUrl: cfg.baseUrl };
  return { baseUrl: `http://localhost:${process.env.PORT || "20128"}` };
}