Repository navigation
Expand file tree
/
Copy pathconfig.ts
More file actions
105 lines (91 loc) · 3.67 KB
/
Copy pathconfig.ts
File metadata and controls
105 lines (91 loc) · 3.67 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
/**
* config.ts — environment, base URL, defaults, and credential storage.
*
* Mirrors the Smriti pattern: a single place for env vars and paths so no
* other file reads process.env or the filesystem for config directly.
*/
import { homedir } from "os";
import { join } from "path";
import { readFileSync, writeFileSync, mkdirSync, rmSync, existsSync, chmodSync } from "fs";
/** API base URL: --base-url flag > TABSTACK_BASE_URL env > default. */
let baseUrl = (process.env.TABSTACK_BASE_URL || "https://api.tabstack.ai").replace(/\/+$/, "");
export function getBaseUrl(): string {
return baseUrl;
}
/** Apply a --base-url override (called once from the CLI entry point). */
export function setBaseUrl(url: string): void {
baseUrl = url.replace(/\/+$/, "");
}
/** Where `tabstack login` stores the key. Honors XDG; override for tests. */
export const CONFIG_DIR =
process.env.TABSTACK_CONFIG_DIR ||
join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "tabstack");
export const CONFIG_FILE = join(CONFIG_DIR, "config.json");
/** The Tabstack web console — where users create API keys. */
export const CONSOLE_URL = "https://console.tabstack.ai/";
/** Endpoint paths, kept in one place. */
export const ENDPOINTS = {
extractMarkdown: "/v1/extract/markdown",
extractJson: "/v1/extract/json",
generateJson: "/v1/generate/json",
research: "/v1/research",
automate: "/v1/automate",
automateInput: (requestId: string) => `/v1/automate/${encodeURIComponent(requestId)}/input`,
} as const;
/**
* Resolve the API key from (in order):
* 1. an explicit --api-key override
* 2. the TABSTACK_API_KEY environment variable
* 3. the stored credential from `tabstack login`
*
* Throws a clear, actionable error if none are present.
*/
export function getApiKey(override?: string): string {
const resolved = resolveKey(override);
if (!resolved) {
throw new Error(
"Not authenticated. Run 'tabstack login', set TABSTACK_API_KEY, or pass --api-key <key>.\n" +
"Create a key at " + CONSOLE_URL + " (API Keys → Create New API Key).",
);
}
return resolved.key;
}
export type KeySource = "--api-key flag" | "TABSTACK_API_KEY env" | "config file";
/**
* Resolve the key together with where it came from, for `tabstack status`.
* Never returns or prints the key value to the user — only its source.
*/
export function resolveKey(override?: string): { key: string; source: KeySource } | undefined {
if (override) return { key: override, source: "--api-key flag" };
if (process.env.TABSTACK_API_KEY)
return { key: process.env.TABSTACK_API_KEY, source: "TABSTACK_API_KEY env" };
const stored = readStoredKey();
if (stored) return { key: stored, source: "config file" };
return undefined;
}
/** Read the stored API key, or undefined if not logged in / unreadable. */
export function readStoredKey(): string | undefined {
try {
const parsed = JSON.parse(readFileSync(CONFIG_FILE, "utf8")) as { apiKey?: unknown };
return typeof parsed.apiKey === "string" && parsed.apiKey ? parsed.apiKey : undefined;
} catch {
return undefined;
}
}
/** Persist the API key to the config file with owner-only (0600) permissions. */
export function saveKey(apiKey: string): string {
mkdirSync(CONFIG_DIR, { recursive: true });
writeFileSync(CONFIG_FILE, JSON.stringify({ apiKey }, null, 2) + "\n", { mode: 0o600 });
try {
chmodSync(CONFIG_FILE, 0o600); // ensure perms even if the file pre-existed
} catch {
/* best effort */
}
return CONFIG_FILE;
}
/** Remove the stored credential. Returns true if a file was deleted. */
export function clearKey(): boolean {
if (!existsSync(CONFIG_FILE)) return false;
rmSync(CONFIG_FILE);
return true;
}