memax-cli 0.1.0-alpha.11 → 0.1.0-alpha.13
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/assets/memax-memory-skill.md +154 -0
- package/dist/commands/auth.d.ts +1 -0
- package/dist/commands/auth.d.ts.map +1 -1
- package/dist/commands/auth.js +9 -2
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/capture.d.ts +1 -1
- package/dist/commands/capture.d.ts.map +1 -1
- package/dist/commands/capture.js +3 -3
- package/dist/commands/capture.js.map +1 -1
- package/dist/commands/delete.js +5 -5
- package/dist/commands/delete.js.map +1 -1
- package/dist/commands/hub.d.ts +4 -0
- package/dist/commands/hub.d.ts.map +1 -0
- package/dist/commands/hub.js +52 -0
- package/dist/commands/hub.js.map +1 -0
- package/dist/commands/list.d.ts.map +1 -1
- package/dist/commands/list.js +14 -14
- package/dist/commands/list.js.map +1 -1
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +22 -2
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/mcp.d.ts.map +1 -1
- package/dist/commands/mcp.js +37 -35
- package/dist/commands/mcp.js.map +1 -1
- package/dist/commands/push.d.ts +1 -0
- package/dist/commands/push.d.ts.map +1 -1
- package/dist/commands/push.js +69 -6
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/recall.d.ts +1 -0
- package/dist/commands/recall.d.ts.map +1 -1
- package/dist/commands/recall.js +62 -24
- package/dist/commands/recall.js.map +1 -1
- package/dist/commands/setup.d.ts +1 -0
- package/dist/commands/setup.d.ts.map +1 -1
- package/dist/commands/setup.js +76 -4
- package/dist/commands/setup.js.map +1 -1
- package/dist/commands/show.js +9 -9
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/sync.js +3 -3
- package/dist/commands/sync.js.map +1 -1
- package/dist/index.js +23 -19
- package/dist/index.js.map +1 -1
- package/dist/lib/api.d.ts +4 -3
- package/dist/lib/api.d.ts.map +1 -1
- package/dist/lib/api.js +83 -40
- package/dist/lib/api.js.map +1 -1
- package/package.json +6 -1
- package/.vscode/mcp.json +0 -8
- package/src/commands/auth.ts +0 -92
- package/src/commands/capture.ts +0 -86
- package/src/commands/config.ts +0 -27
- package/src/commands/delete.ts +0 -58
- package/src/commands/hook.ts +0 -243
- package/src/commands/list.ts +0 -92
- package/src/commands/login.ts +0 -164
- package/src/commands/mcp.ts +0 -528
- package/src/commands/push.ts +0 -137
- package/src/commands/recall.ts +0 -163
- package/src/commands/setup.ts +0 -1129
- package/src/commands/show.ts +0 -35
- package/src/commands/sync.ts +0 -506
- package/src/index.ts +0 -224
- package/src/lib/api.ts +0 -110
- package/src/lib/config.ts +0 -61
- package/src/lib/credentials.ts +0 -42
- package/tsconfig.json +0 -9
package/src/index.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { Command } from "commander";
|
|
3
|
-
import { readFileSync } from "node:fs";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { join, dirname } from "node:path";
|
|
6
|
-
import { pushCommand } from "./commands/push.js";
|
|
7
|
-
import { recallCommand } from "./commands/recall.js";
|
|
8
|
-
import { listCommand } from "./commands/list.js";
|
|
9
|
-
import { showCommand } from "./commands/show.js";
|
|
10
|
-
import { deleteCommand } from "./commands/delete.js";
|
|
11
|
-
import { syncCommand, syncAgentMemoryCommand } from "./commands/sync.js";
|
|
12
|
-
import { hookCommand } from "./commands/hook.js";
|
|
13
|
-
import { configGetCommand, configSetCommand } from "./commands/config.js";
|
|
14
|
-
import {
|
|
15
|
-
loginCommand,
|
|
16
|
-
logoutCommand,
|
|
17
|
-
whoamiCommand,
|
|
18
|
-
} from "./commands/login.js";
|
|
19
|
-
import {
|
|
20
|
-
createKeyCommand,
|
|
21
|
-
listKeysCommand,
|
|
22
|
-
revokeKeyCommand,
|
|
23
|
-
} from "./commands/auth.js";
|
|
24
|
-
import { registerMcpCommand } from "./commands/mcp.js";
|
|
25
|
-
import { setupCommand, teardownCommand } from "./commands/setup.js";
|
|
26
|
-
import { captureSessionCommand } from "./commands/capture.js";
|
|
27
|
-
|
|
28
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
29
|
-
const pkg = JSON.parse(
|
|
30
|
-
readFileSync(join(__dirname, "..", "package.json"), "utf-8"),
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
const program = new Command();
|
|
34
|
-
|
|
35
|
-
program
|
|
36
|
-
.name("memax")
|
|
37
|
-
.description("Universal context & memory hub for AI agents")
|
|
38
|
-
.version(pkg.version);
|
|
39
|
-
|
|
40
|
-
// --- Core commands ---
|
|
41
|
-
|
|
42
|
-
program
|
|
43
|
-
.command("push [content]")
|
|
44
|
-
.description("Save knowledge to your Memax workspace")
|
|
45
|
-
.option("-f, --file <path>", "File to push")
|
|
46
|
-
.option("-c, --category <category>", "Category (auto-detected if omitted)")
|
|
47
|
-
.option("-t, --tags <tags>", "Comma-separated tags")
|
|
48
|
-
.option("--title <title>", "Note title")
|
|
49
|
-
.option("--ttl <duration>", "Auto-archive after duration (e.g., 7d, 30d)")
|
|
50
|
-
.option("--stdin", "Read content from stdin")
|
|
51
|
-
.action(pushCommand);
|
|
52
|
-
|
|
53
|
-
program
|
|
54
|
-
.command("recall [query]")
|
|
55
|
-
.description("Ask your knowledge a question")
|
|
56
|
-
.option("-c, --category <category>", "Filter by category")
|
|
57
|
-
.option("-t, --tags <tags>", "Filter by tags")
|
|
58
|
-
.option("-l, --limit <n>", "Max results", "5")
|
|
59
|
-
.option("--format <format>", "Output format: text, json", "text")
|
|
60
|
-
.option("--hook", "Output in agent-injectable format")
|
|
61
|
-
.option("--max-tokens <number>", "Maximum tokens to output (approximate)")
|
|
62
|
-
.option("--include-archived", "Include archived notes")
|
|
63
|
-
.action(recallCommand);
|
|
64
|
-
|
|
65
|
-
program
|
|
66
|
-
.command("list")
|
|
67
|
-
.description("List your knowledge notes")
|
|
68
|
-
.option("-c, --category <category>", "Filter by category")
|
|
69
|
-
.option("-s, --sort <sort>", "Sort by: newest, relevant", "newest")
|
|
70
|
-
.option("-l, --limit <n>", "Max results per page", "20")
|
|
71
|
-
.option("--all", "Fetch all pages (not just the first)")
|
|
72
|
-
.action(listCommand);
|
|
73
|
-
|
|
74
|
-
program
|
|
75
|
-
.command("show <id>")
|
|
76
|
-
.description("Show a specific note")
|
|
77
|
-
.action(showCommand);
|
|
78
|
-
|
|
79
|
-
program
|
|
80
|
-
.command("delete <id>")
|
|
81
|
-
.description("Delete a note")
|
|
82
|
-
.option("-y, --yes", "Skip confirmation")
|
|
83
|
-
.action(deleteCommand);
|
|
84
|
-
|
|
85
|
-
// Aliases
|
|
86
|
-
program
|
|
87
|
-
.command("remember [content]")
|
|
88
|
-
.description("Alias for push — save knowledge to your Memax workspace")
|
|
89
|
-
.option("-f, --file <path>", "File to push")
|
|
90
|
-
.option("-c, --category <category>", "Category (auto-detected if omitted)")
|
|
91
|
-
.option("-t, --tags <tags>", "Comma-separated tags")
|
|
92
|
-
.option("--title <title>", "Note title")
|
|
93
|
-
.option("--stdin", "Read content from stdin")
|
|
94
|
-
.action(pushCommand);
|
|
95
|
-
|
|
96
|
-
program
|
|
97
|
-
.command("forget <id>")
|
|
98
|
-
.description("Alias for delete — remove a note from your workspace")
|
|
99
|
-
.option("-y, --yes", "Skip confirmation")
|
|
100
|
-
.action(deleteCommand);
|
|
101
|
-
|
|
102
|
-
// --- Sync ---
|
|
103
|
-
|
|
104
|
-
const syncCmd = program
|
|
105
|
-
.command("sync [directory]")
|
|
106
|
-
.description("Sync a directory or agent memory to your Memax workspace")
|
|
107
|
-
.option("-w, --watch", "Watch for changes (coming soon)")
|
|
108
|
-
.option(
|
|
109
|
-
"-b, --boundary <level>",
|
|
110
|
-
"Visibility level: private, team, org",
|
|
111
|
-
"private",
|
|
112
|
-
)
|
|
113
|
-
.option(
|
|
114
|
-
"-c, --category <category>",
|
|
115
|
-
"Default category (auto-detected if omitted)",
|
|
116
|
-
)
|
|
117
|
-
.option("--ignore <patterns>", "Comma-separated directories to ignore")
|
|
118
|
-
.option(
|
|
119
|
-
"--agent-memory",
|
|
120
|
-
"Sync native AI agent memory files (Claude Code, Cursor, Codex)",
|
|
121
|
-
)
|
|
122
|
-
.option("-y, --yes", "Skip confirmation for large syncs")
|
|
123
|
-
.action(syncCommand);
|
|
124
|
-
|
|
125
|
-
syncCmd
|
|
126
|
-
.command("agents")
|
|
127
|
-
.description("Sync native AI agent memory files to Memax")
|
|
128
|
-
.action(syncAgentMemoryCommand);
|
|
129
|
-
|
|
130
|
-
// --- Session capture ---
|
|
131
|
-
|
|
132
|
-
program
|
|
133
|
-
.command("capture-session")
|
|
134
|
-
.description("Capture an agent session — extract decisions and learnings")
|
|
135
|
-
.option("--summary <text>", "Session summary text (alternative to stdin)")
|
|
136
|
-
.option("--agent <name>", "Agent name (claude-code, gemini, etc.)")
|
|
137
|
-
.action(captureSessionCommand);
|
|
138
|
-
|
|
139
|
-
// --- Agent integration setup ---
|
|
140
|
-
|
|
141
|
-
program
|
|
142
|
-
.command("setup")
|
|
143
|
-
.description("Set up AI agent integrations (auto-detects installed agents)")
|
|
144
|
-
.option("--mcp", "Enable MCP server (agent tools)")
|
|
145
|
-
.option("--hooks", "Enable context injection hooks")
|
|
146
|
-
.option("--instructions", "Inject memax instructions into agent config files")
|
|
147
|
-
.option("--all", "Enable MCP + hooks + instructions")
|
|
148
|
-
.option("--local", "Use local stdio MCP instead of remote server")
|
|
149
|
-
.option("--print", "Print MCP config JSON to copy/paste (no changes made)")
|
|
150
|
-
.option("--only <agents>", "Only configure these agents (comma-separated)")
|
|
151
|
-
.option("--skip <agents>", "Skip these agents (comma-separated)")
|
|
152
|
-
.action(setupCommand);
|
|
153
|
-
|
|
154
|
-
program
|
|
155
|
-
.command("teardown")
|
|
156
|
-
.description("Remove Memax integrations from agents")
|
|
157
|
-
.option("--only <agents>", "Only remove from these agents (comma-separated)")
|
|
158
|
-
.action(teardownCommand);
|
|
159
|
-
|
|
160
|
-
program
|
|
161
|
-
.command("hook <action> <agent>")
|
|
162
|
-
.description("Manage agent hooks (install/uninstall claude-code)")
|
|
163
|
-
.action(hookCommand);
|
|
164
|
-
|
|
165
|
-
// --- Auth ---
|
|
166
|
-
|
|
167
|
-
program
|
|
168
|
-
.command("login")
|
|
169
|
-
.description("Log in to Memax via GitHub")
|
|
170
|
-
.action(loginCommand);
|
|
171
|
-
|
|
172
|
-
program
|
|
173
|
-
.command("logout")
|
|
174
|
-
.description("Clear saved credentials")
|
|
175
|
-
.action(logoutCommand);
|
|
176
|
-
|
|
177
|
-
program
|
|
178
|
-
.command("whoami")
|
|
179
|
-
.description("Show current user")
|
|
180
|
-
.action(whoamiCommand);
|
|
181
|
-
|
|
182
|
-
// --- API Keys ---
|
|
183
|
-
|
|
184
|
-
const authCmd = program
|
|
185
|
-
.command("auth")
|
|
186
|
-
.description("Manage authentication and API keys");
|
|
187
|
-
|
|
188
|
-
authCmd
|
|
189
|
-
.command("create-key <name>")
|
|
190
|
-
.description("Create an API key for CI/CD or non-interactive use")
|
|
191
|
-
.option("--expires <days>", "Expire after N days (default: never)")
|
|
192
|
-
.action(createKeyCommand);
|
|
193
|
-
|
|
194
|
-
authCmd
|
|
195
|
-
.command("list-keys")
|
|
196
|
-
.description("List your API keys")
|
|
197
|
-
.action(listKeysCommand);
|
|
198
|
-
|
|
199
|
-
authCmd
|
|
200
|
-
.command("revoke-key <id>")
|
|
201
|
-
.description("Revoke an API key")
|
|
202
|
-
.action(revokeKeyCommand);
|
|
203
|
-
|
|
204
|
-
// --- MCP Server ---
|
|
205
|
-
|
|
206
|
-
registerMcpCommand(program);
|
|
207
|
-
|
|
208
|
-
// --- Config ---
|
|
209
|
-
|
|
210
|
-
const configCmd = program
|
|
211
|
-
.command("config")
|
|
212
|
-
.description("Manage Memax configuration");
|
|
213
|
-
|
|
214
|
-
configCmd
|
|
215
|
-
.command("get [key]")
|
|
216
|
-
.description("Get config value (or all values)")
|
|
217
|
-
.action(configGetCommand);
|
|
218
|
-
|
|
219
|
-
configCmd
|
|
220
|
-
.command("set <key> <value>")
|
|
221
|
-
.description("Set a config value")
|
|
222
|
-
.action(configSetCommand);
|
|
223
|
-
|
|
224
|
-
program.parse();
|
package/src/lib/api.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { loadConfig } from "./config.js";
|
|
2
|
-
import {
|
|
3
|
-
loadCredentials,
|
|
4
|
-
saveCredentials,
|
|
5
|
-
isTokenExpired,
|
|
6
|
-
} from "./credentials.js";
|
|
7
|
-
|
|
8
|
-
interface ApiEnvelope {
|
|
9
|
-
data?: unknown;
|
|
10
|
-
error?: { code: string; message: string };
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function getApiUrl(): string {
|
|
14
|
-
return loadConfig().api_url;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async function authHeaders(): Promise<Record<string, string>> {
|
|
18
|
-
// 1. MEMAX_API_KEY env var takes priority (CI/CD, non-interactive)
|
|
19
|
-
const envKey = process.env.MEMAX_API_KEY;
|
|
20
|
-
if (envKey) {
|
|
21
|
-
return { Authorization: `Bearer ${envKey}` };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 2. Stored credentials with auto-refresh
|
|
25
|
-
const creds = loadCredentials();
|
|
26
|
-
if (!creds?.access_token) return {};
|
|
27
|
-
|
|
28
|
-
if (isTokenExpired() && creds.refresh_token) {
|
|
29
|
-
// Attempt to refresh the access token
|
|
30
|
-
try {
|
|
31
|
-
const url = `${getApiUrl()}/v1/auth/refresh`;
|
|
32
|
-
const res = await fetch(url, {
|
|
33
|
-
method: "POST",
|
|
34
|
-
headers: { "Content-Type": "application/json" },
|
|
35
|
-
body: JSON.stringify({ refresh_token: creds.refresh_token }),
|
|
36
|
-
});
|
|
37
|
-
const json = (await res.json()) as ApiEnvelope;
|
|
38
|
-
if (json.data) {
|
|
39
|
-
const tokens = json.data as {
|
|
40
|
-
access_token: string;
|
|
41
|
-
refresh_token: string;
|
|
42
|
-
expires_in: number;
|
|
43
|
-
};
|
|
44
|
-
saveCredentials({
|
|
45
|
-
access_token: tokens.access_token,
|
|
46
|
-
refresh_token: tokens.refresh_token,
|
|
47
|
-
expires_at: Date.now() + tokens.expires_in * 1000,
|
|
48
|
-
});
|
|
49
|
-
return { Authorization: `Bearer ${tokens.access_token}` };
|
|
50
|
-
}
|
|
51
|
-
} catch {
|
|
52
|
-
// Refresh failed — fall through to use stale token (server will reject)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return { Authorization: `Bearer ${creds.access_token}` };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export async function apiPost<T>(path: string, body: unknown): Promise<T> {
|
|
60
|
-
const url = `${getApiUrl()}${path}`;
|
|
61
|
-
let res: Response;
|
|
62
|
-
try {
|
|
63
|
-
res = await fetch(url, {
|
|
64
|
-
method: "POST",
|
|
65
|
-
headers: { "Content-Type": "application/json", ...(await authHeaders()) },
|
|
66
|
-
body: JSON.stringify(body),
|
|
67
|
-
});
|
|
68
|
-
} catch {
|
|
69
|
-
throw new Error(`Cannot reach API at ${url} — is the server running?`);
|
|
70
|
-
}
|
|
71
|
-
const json = (await res.json()) as ApiEnvelope;
|
|
72
|
-
if (json.error) {
|
|
73
|
-
throw new Error(json.error.message);
|
|
74
|
-
}
|
|
75
|
-
return json.data as T;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export async function apiGet<T>(path: string): Promise<T> {
|
|
79
|
-
const url = `${getApiUrl()}${path}`;
|
|
80
|
-
let res: Response;
|
|
81
|
-
try {
|
|
82
|
-
res = await fetch(url, {
|
|
83
|
-
headers: { ...(await authHeaders()) },
|
|
84
|
-
});
|
|
85
|
-
} catch {
|
|
86
|
-
throw new Error(`Cannot reach API at ${url} — is the server running?`);
|
|
87
|
-
}
|
|
88
|
-
const json = (await res.json()) as ApiEnvelope;
|
|
89
|
-
if (json.error) {
|
|
90
|
-
throw new Error(json.error.message);
|
|
91
|
-
}
|
|
92
|
-
return json.data as T;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export async function apiDelete(path: string): Promise<void> {
|
|
96
|
-
const url = `${getApiUrl()}${path}`;
|
|
97
|
-
let res: Response;
|
|
98
|
-
try {
|
|
99
|
-
res = await fetch(url, {
|
|
100
|
-
method: "DELETE",
|
|
101
|
-
headers: { ...(await authHeaders()) },
|
|
102
|
-
});
|
|
103
|
-
} catch {
|
|
104
|
-
throw new Error(`Cannot reach API at ${url} — is the server running?`);
|
|
105
|
-
}
|
|
106
|
-
const json = (await res.json()) as ApiEnvelope;
|
|
107
|
-
if (json.error) {
|
|
108
|
-
throw new Error(json.error.message);
|
|
109
|
-
}
|
|
110
|
-
}
|
package/src/lib/config.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
|
|
5
|
-
const CONFIG_DIR = join(homedir(), ".memax");
|
|
6
|
-
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
7
|
-
|
|
8
|
-
export interface MemaxConfig {
|
|
9
|
-
api_url: string;
|
|
10
|
-
default_hub: string;
|
|
11
|
-
default_boundary: string;
|
|
12
|
-
auto_categorize: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const DEFAULT_CONFIG: MemaxConfig = {
|
|
16
|
-
api_url: "http://localhost:8080",
|
|
17
|
-
default_hub: "default",
|
|
18
|
-
default_boundary: "private",
|
|
19
|
-
auto_categorize: true,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export function getConfigDir(): string {
|
|
23
|
-
return CONFIG_DIR;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function loadConfig(): MemaxConfig {
|
|
27
|
-
// Env var overrides everything
|
|
28
|
-
const envUrl = process.env.MEMAX_API_URL;
|
|
29
|
-
|
|
30
|
-
if (!existsSync(CONFIG_FILE)) {
|
|
31
|
-
return envUrl ? { ...DEFAULT_CONFIG, api_url: envUrl } : DEFAULT_CONFIG;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
const raw = readFileSync(CONFIG_FILE, "utf-8");
|
|
36
|
-
const config = { ...DEFAULT_CONFIG, ...JSON.parse(raw) };
|
|
37
|
-
if (envUrl) config.api_url = envUrl;
|
|
38
|
-
return config;
|
|
39
|
-
} catch {
|
|
40
|
-
return envUrl ? { ...DEFAULT_CONFIG, api_url: envUrl } : DEFAULT_CONFIG;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function saveConfig(config: Partial<MemaxConfig>): void {
|
|
45
|
-
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
46
|
-
|
|
47
|
-
let existing = DEFAULT_CONFIG;
|
|
48
|
-
if (existsSync(CONFIG_FILE)) {
|
|
49
|
-
try {
|
|
50
|
-
existing = {
|
|
51
|
-
...DEFAULT_CONFIG,
|
|
52
|
-
...JSON.parse(readFileSync(CONFIG_FILE, "utf-8")),
|
|
53
|
-
};
|
|
54
|
-
} catch {
|
|
55
|
-
// ignore parse errors
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const merged = { ...existing, ...config };
|
|
60
|
-
writeFileSync(CONFIG_FILE, JSON.stringify(merged, null, 2) + "\n");
|
|
61
|
-
}
|
package/src/lib/credentials.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { getConfigDir } from "./config.js";
|
|
4
|
-
|
|
5
|
-
const CRED_FILE = join(getConfigDir(), "credentials.json");
|
|
6
|
-
|
|
7
|
-
interface Credentials {
|
|
8
|
-
access_token: string;
|
|
9
|
-
refresh_token: string;
|
|
10
|
-
expires_at?: number; // unix timestamp (ms)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function loadCredentials(): Credentials | null {
|
|
14
|
-
if (!existsSync(CRED_FILE)) return null;
|
|
15
|
-
try {
|
|
16
|
-
const creds = JSON.parse(readFileSync(CRED_FILE, "utf-8")) as Credentials;
|
|
17
|
-
if (!creds.access_token) return null;
|
|
18
|
-
return creds;
|
|
19
|
-
} catch {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function saveCredentials(creds: Credentials): void {
|
|
25
|
-
mkdirSync(getConfigDir(), { recursive: true });
|
|
26
|
-
writeFileSync(CRED_FILE, JSON.stringify(creds, null, 2) + "\n", {
|
|
27
|
-
mode: 0o600,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function isTokenExpired(): boolean {
|
|
32
|
-
const creds = loadCredentials();
|
|
33
|
-
if (!creds?.expires_at) return false; // no expiry info → assume valid
|
|
34
|
-
// Treat as expired 5 minutes early to avoid edge cases
|
|
35
|
-
return Date.now() >= creds.expires_at - 5 * 60 * 1000;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function clearCredentials(): void {
|
|
39
|
-
if (existsSync(CRED_FILE)) {
|
|
40
|
-
writeFileSync(CRED_FILE, "{}\n", { mode: 0o600 });
|
|
41
|
-
}
|
|
42
|
-
}
|