memax-cli 0.1.0-alpha.8 → 0.1.0
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/skills/memax-memory/SKILL.md +173 -0
- package/dist/commands/agent-configs.d.ts +41 -0
- package/dist/commands/agent-configs.d.ts.map +1 -0
- package/dist/commands/agent-configs.js +1290 -0
- package/dist/commands/agent-configs.js.map +1 -0
- package/dist/commands/agent-configs.test.d.ts +2 -0
- package/dist/commands/agent-configs.test.d.ts.map +1 -0
- package/dist/commands/agent-configs.test.js +122 -0
- package/dist/commands/agent-configs.test.js.map +1 -0
- package/dist/commands/agent-sessions.d.ts +74 -0
- package/dist/commands/agent-sessions.d.ts.map +1 -0
- package/dist/commands/agent-sessions.js +1513 -0
- package/dist/commands/agent-sessions.js.map +1 -0
- package/dist/commands/agent-sessions.test.d.ts +2 -0
- package/dist/commands/agent-sessions.test.d.ts.map +1 -0
- package/dist/commands/agent-sessions.test.js +255 -0
- package/dist/commands/agent-sessions.test.js.map +1 -0
- package/dist/commands/agents.d.ts +3 -0
- package/dist/commands/agents.d.ts.map +1 -0
- package/dist/commands/agents.js +36 -0
- package/dist/commands/agents.js.map +1 -0
- package/dist/commands/ask.d.ts +15 -0
- package/dist/commands/ask.d.ts.map +1 -0
- package/dist/commands/ask.js +483 -0
- package/dist/commands/ask.js.map +1 -0
- package/dist/commands/auth.d.ts +7 -0
- package/dist/commands/auth.d.ts.map +1 -1
- package/dist/commands/auth.js +103 -8
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/capture.d.ts +19 -0
- package/dist/commands/capture.d.ts.map +1 -0
- package/dist/commands/capture.js +69 -0
- package/dist/commands/capture.js.map +1 -0
- package/dist/commands/config.d.ts +2 -0
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +13 -0
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/delete.d.ts +2 -0
- package/dist/commands/delete.d.ts.map +1 -1
- package/dist/commands/delete.js +15 -18
- package/dist/commands/delete.js.map +1 -1
- package/dist/commands/dreams.d.ts +22 -0
- package/dist/commands/dreams.d.ts.map +1 -0
- package/dist/commands/dreams.js +251 -0
- package/dist/commands/dreams.js.map +1 -0
- package/dist/commands/dreams.test.d.ts +2 -0
- package/dist/commands/dreams.test.d.ts.map +1 -0
- package/dist/commands/dreams.test.js +39 -0
- package/dist/commands/dreams.test.js.map +1 -0
- package/dist/commands/hook.d.ts +2 -0
- package/dist/commands/hook.d.ts.map +1 -1
- package/dist/commands/hook.js +25 -103
- package/dist/commands/hook.js.map +1 -1
- package/dist/commands/hub.d.ts +37 -0
- package/dist/commands/hub.d.ts.map +1 -0
- package/dist/commands/hub.js +347 -0
- package/dist/commands/hub.js.map +1 -0
- package/dist/commands/hub.test.d.ts +2 -0
- package/dist/commands/hub.test.d.ts.map +1 -0
- package/dist/commands/hub.test.js +62 -0
- package/dist/commands/hub.test.js.map +1 -0
- package/dist/commands/import.d.ts +13 -0
- package/dist/commands/import.d.ts.map +1 -0
- package/dist/commands/import.js +257 -0
- package/dist/commands/import.js.map +1 -0
- package/dist/commands/import.test.d.ts +2 -0
- package/dist/commands/import.test.d.ts.map +1 -0
- package/dist/commands/import.test.js +11 -0
- package/dist/commands/import.test.js.map +1 -0
- package/dist/commands/list.d.ts +9 -2
- package/dist/commands/list.d.ts.map +1 -1
- package/dist/commands/list.js +118 -9
- package/dist/commands/list.js.map +1 -1
- package/dist/commands/list.test.d.ts +2 -0
- package/dist/commands/list.test.d.ts.map +1 -0
- package/dist/commands/list.test.js +20 -0
- package/dist/commands/list.test.js.map +1 -0
- package/dist/commands/login.d.ts +7 -1
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +81 -20
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/mcp.d.ts.map +1 -1
- package/dist/commands/mcp.js +386 -65
- package/dist/commands/mcp.js.map +1 -1
- package/dist/commands/push.d.ts +6 -1
- package/dist/commands/push.d.ts.map +1 -1
- package/dist/commands/push.js +42 -8
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/recall.d.ts +11 -1
- package/dist/commands/recall.d.ts.map +1 -1
- package/dist/commands/recall.js +228 -41
- package/dist/commands/recall.js.map +1 -1
- package/dist/commands/recall.test.d.ts +2 -0
- package/dist/commands/recall.test.d.ts.map +1 -0
- package/dist/commands/recall.test.js +31 -0
- package/dist/commands/recall.test.js.map +1 -0
- package/dist/commands/setup-hooks.d.ts +13 -0
- package/dist/commands/setup-hooks.d.ts.map +1 -0
- package/dist/commands/setup-hooks.js +193 -0
- package/dist/commands/setup-hooks.js.map +1 -0
- package/dist/commands/setup-instructions.d.ts +21 -0
- package/dist/commands/setup-instructions.d.ts.map +1 -0
- package/dist/commands/setup-instructions.js +172 -0
- package/dist/commands/setup-instructions.js.map +1 -0
- package/dist/commands/setup-mcp.d.ts +40 -0
- package/dist/commands/setup-mcp.d.ts.map +1 -0
- package/dist/commands/setup-mcp.js +414 -0
- package/dist/commands/setup-mcp.js.map +1 -0
- package/dist/commands/setup-types.d.ts +33 -0
- package/dist/commands/setup-types.d.ts.map +1 -0
- package/dist/commands/setup-types.js +60 -0
- package/dist/commands/setup-types.js.map +1 -0
- package/dist/commands/setup.d.ts +10 -1
- package/dist/commands/setup.d.ts.map +1 -1
- package/dist/commands/setup.js +216 -532
- package/dist/commands/setup.js.map +1 -1
- package/dist/commands/show.d.ts +5 -1
- package/dist/commands/show.d.ts.map +1 -1
- package/dist/commands/show.js +36 -14
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/topic.d.ts +32 -0
- package/dist/commands/topic.d.ts.map +1 -0
- package/dist/commands/topic.js +265 -0
- package/dist/commands/topic.js.map +1 -0
- package/dist/commands/topic.test.d.ts +2 -0
- package/dist/commands/topic.test.d.ts.map +1 -0
- package/dist/commands/topic.test.js +114 -0
- package/dist/commands/topic.test.js.map +1 -0
- package/dist/index.js +35 -144
- package/dist/index.js.map +1 -1
- package/dist/lib/client.d.ts +10 -0
- package/dist/lib/client.d.ts.map +1 -0
- package/dist/lib/client.js +104 -0
- package/dist/lib/client.js.map +1 -0
- package/dist/lib/client.test.d.ts +2 -0
- package/dist/lib/client.test.d.ts.map +1 -0
- package/dist/lib/client.test.js +44 -0
- package/dist/lib/client.test.js.map +1 -0
- package/dist/lib/config.d.ts +43 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +72 -1
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/credentials.d.ts +3 -0
- package/dist/lib/credentials.d.ts.map +1 -1
- package/dist/lib/credentials.js +24 -2
- package/dist/lib/credentials.js.map +1 -1
- package/dist/lib/hubs.d.ts +7 -0
- package/dist/lib/hubs.d.ts.map +1 -0
- package/dist/lib/hubs.js +33 -0
- package/dist/lib/hubs.js.map +1 -0
- package/dist/lib/hubs.test.d.ts +2 -0
- package/dist/lib/hubs.test.d.ts.map +1 -0
- package/dist/lib/hubs.test.js +58 -0
- package/dist/lib/hubs.test.js.map +1 -0
- package/dist/lib/project-context.d.ts +56 -0
- package/dist/lib/project-context.d.ts.map +1 -0
- package/dist/lib/project-context.js +225 -0
- package/dist/lib/project-context.js.map +1 -0
- package/dist/lib/project-context.test.d.ts +2 -0
- package/dist/lib/project-context.test.d.ts.map +1 -0
- package/dist/lib/project-context.test.js +75 -0
- package/dist/lib/project-context.test.js.map +1 -0
- package/dist/lib/prompt.d.ts +7 -0
- package/dist/lib/prompt.d.ts.map +1 -0
- package/dist/lib/prompt.js +41 -0
- package/dist/lib/prompt.js.map +1 -0
- package/dist/lib/trash.d.ts +6 -0
- package/dist/lib/trash.d.ts.map +1 -0
- package/dist/lib/trash.js +28 -0
- package/dist/lib/trash.js.map +1 -0
- package/package.json +17 -13
- package/.vscode/mcp.json +0 -8
- package/dist/commands/sync.d.ts +0 -12
- package/dist/commands/sync.d.ts.map +0 -1
- package/dist/commands/sync.js +0 -414
- package/dist/commands/sync.js.map +0 -1
- package/dist/lib/api.d.ts +0 -4
- package/dist/lib/api.d.ts.map +0 -1
- package/dist/lib/api.js +0 -95
- package/dist/lib/api.js.map +0 -1
- package/src/commands/auth.ts +0 -92
- 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 -38
- package/src/commands/login.ts +0 -164
- package/src/commands/mcp.ts +0 -405
- package/src/commands/push.ts +0 -137
- package/src/commands/recall.ts +0 -163
- package/src/commands/setup.ts +0 -1075
- package/src/commands/show.ts +0 -35
- package/src/commands/sync.ts +0 -506
- package/src/index.ts +0 -213
- 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/dist/commands/setup.js
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
-
import {
|
|
3
|
-
import { join
|
|
4
|
-
import { homedir
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
5
|
import { execSync } from "node:child_process";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { createInterface } from "node:readline";
|
|
7
|
+
import { commandExists, resolveMemaxBin, } from "./setup-types.js";
|
|
8
|
+
import { ensureApiKey, ensureLocalAgentKey, setupMcpRemote, setupMcpOAuth, setupMcp, printMcpConfigs, removeMcpJson, removeMcpToml, } from "./setup-mcp.js";
|
|
9
|
+
import { setupHooks, removeHooks } from "./setup-hooks.js";
|
|
10
|
+
import { injectInstructions, removeInstructions, installSkills, removeSkills, } from "./setup-instructions.js";
|
|
11
|
+
// --- Agent definitions ---
|
|
8
12
|
function getAgents() {
|
|
9
13
|
const home = homedir();
|
|
10
14
|
const cwd = process.cwd();
|
|
15
|
+
// Helper: standard { type: "url", url, headers? } shape used by Cursor, Windsurf, OpenClaw
|
|
16
|
+
const stdUrlEntry = (url, headers) => ({
|
|
17
|
+
type: "url",
|
|
18
|
+
url,
|
|
19
|
+
...(headers ? { headers } : {}),
|
|
20
|
+
});
|
|
21
|
+
// Helper: { type: "http", url, headers? } shape used by VS Code, Copilot CLI
|
|
22
|
+
const httpEntry = (url, headers) => ({
|
|
23
|
+
type: "http",
|
|
24
|
+
url,
|
|
25
|
+
...(headers ? { headers } : {}),
|
|
26
|
+
});
|
|
11
27
|
return [
|
|
12
28
|
{
|
|
13
29
|
name: "Claude Code",
|
|
@@ -18,6 +34,8 @@ function getAgents() {
|
|
|
18
34
|
hasHooks: true,
|
|
19
35
|
globalInstructionFile: join(home, ".claude", "CLAUDE.md"),
|
|
20
36
|
detect: () => existsSync(join(home, ".claude")) || commandExists("claude"),
|
|
37
|
+
// Claude Code uses `claude mcp add` CLI, not JSON config — this is a fallback
|
|
38
|
+
remoteEntry: stdUrlEntry,
|
|
21
39
|
},
|
|
22
40
|
{
|
|
23
41
|
name: "Cursor",
|
|
@@ -26,8 +44,9 @@ function getAgents() {
|
|
|
26
44
|
format: "json-mcpServers",
|
|
27
45
|
mcpKey: "mcpServers",
|
|
28
46
|
hasHooks: false,
|
|
29
|
-
globalInstructionFile: null,
|
|
47
|
+
globalInstructionFile: null,
|
|
30
48
|
detect: () => existsSync(join(home, ".cursor")) || commandExists("cursor"),
|
|
49
|
+
remoteEntry: stdUrlEntry,
|
|
31
50
|
},
|
|
32
51
|
{
|
|
33
52
|
name: "Windsurf",
|
|
@@ -36,9 +55,10 @@ function getAgents() {
|
|
|
36
55
|
format: "json-mcpServers",
|
|
37
56
|
mcpKey: "mcpServers",
|
|
38
57
|
hasHooks: false,
|
|
39
|
-
globalInstructionFile: null,
|
|
58
|
+
globalInstructionFile: null,
|
|
40
59
|
detect: () => existsSync(join(home, ".codeium", "windsurf")) ||
|
|
41
60
|
commandExists("windsurf"),
|
|
61
|
+
remoteEntry: stdUrlEntry,
|
|
42
62
|
},
|
|
43
63
|
{
|
|
44
64
|
name: "Gemini CLI",
|
|
@@ -49,6 +69,11 @@ function getAgents() {
|
|
|
49
69
|
hasHooks: true,
|
|
50
70
|
globalInstructionFile: join(home, ".gemini", "GEMINI.md"),
|
|
51
71
|
detect: () => existsSync(join(home, ".gemini")) || commandExists("gemini"),
|
|
72
|
+
// Gemini CLI uses httpUrl for streamable HTTP; url is its SSE field
|
|
73
|
+
remoteEntry: (url, headers) => ({
|
|
74
|
+
httpUrl: url,
|
|
75
|
+
...(headers ? { headers } : {}),
|
|
76
|
+
}),
|
|
52
77
|
},
|
|
53
78
|
{
|
|
54
79
|
name: "GitHub Copilot CLI",
|
|
@@ -57,8 +82,20 @@ function getAgents() {
|
|
|
57
82
|
format: "json-mcpServers",
|
|
58
83
|
mcpKey: "mcpServers",
|
|
59
84
|
hasHooks: false,
|
|
60
|
-
globalInstructionFile: null,
|
|
85
|
+
globalInstructionFile: null,
|
|
61
86
|
detect: () => existsSync(join(home, ".copilot")) || commandExists("gh copilot"),
|
|
87
|
+
// Copilot CLI requires type + url + tools for both remote and local servers
|
|
88
|
+
remoteEntry: (url, headers) => ({
|
|
89
|
+
type: "http",
|
|
90
|
+
url,
|
|
91
|
+
tools: [{ type: "function" }],
|
|
92
|
+
...(headers ? { headers } : {}),
|
|
93
|
+
}),
|
|
94
|
+
localEntry: (command, args) => ({
|
|
95
|
+
command,
|
|
96
|
+
args,
|
|
97
|
+
tools: [{ type: "function" }],
|
|
98
|
+
}),
|
|
62
99
|
},
|
|
63
100
|
{
|
|
64
101
|
name: "Copilot (VS Code)",
|
|
@@ -69,6 +106,8 @@ function getAgents() {
|
|
|
69
106
|
hasHooks: false,
|
|
70
107
|
globalInstructionFile: null,
|
|
71
108
|
detect: () => existsSync(".vscode") || commandExists("code"),
|
|
109
|
+
// VS Code uses type: "http" for remote MCP servers
|
|
110
|
+
remoteEntry: httpEntry,
|
|
72
111
|
},
|
|
73
112
|
{
|
|
74
113
|
name: "Codex CLI",
|
|
@@ -79,6 +118,8 @@ function getAgents() {
|
|
|
79
118
|
hasHooks: false,
|
|
80
119
|
globalInstructionFile: join(home, ".codex", "AGENTS.md"),
|
|
81
120
|
detect: () => existsSync(join(home, ".codex")) || commandExists("codex"),
|
|
121
|
+
// Codex TOML is handled separately; this is for JSON fallback reference
|
|
122
|
+
remoteEntry: stdUrlEntry,
|
|
82
123
|
},
|
|
83
124
|
{
|
|
84
125
|
name: "OpenClaw",
|
|
@@ -87,8 +128,9 @@ function getAgents() {
|
|
|
87
128
|
format: "json-mcpServers",
|
|
88
129
|
mcpKey: "mcp.servers",
|
|
89
130
|
hasHooks: false,
|
|
90
|
-
globalInstructionFile: null,
|
|
131
|
+
globalInstructionFile: null,
|
|
91
132
|
detect: () => existsSync(join(home, ".openclaw")) || commandExists("openclaw"),
|
|
133
|
+
remoteEntry: stdUrlEntry,
|
|
92
134
|
},
|
|
93
135
|
{
|
|
94
136
|
name: "OpenCode",
|
|
@@ -97,8 +139,19 @@ function getAgents() {
|
|
|
97
139
|
format: "json-mcpServers",
|
|
98
140
|
mcpKey: "mcp",
|
|
99
141
|
hasHooks: false,
|
|
100
|
-
globalInstructionFile: null,
|
|
142
|
+
globalInstructionFile: null,
|
|
101
143
|
detect: () => existsSync(join(cwd, ".opencode")) || commandExists("opencode"),
|
|
144
|
+
// OpenCode uses type: "remote" for remote, type: "local" for local
|
|
145
|
+
remoteEntry: (url, headers) => ({
|
|
146
|
+
type: "remote",
|
|
147
|
+
url,
|
|
148
|
+
...(headers ? { headers } : {}),
|
|
149
|
+
}),
|
|
150
|
+
localEntry: (command, args) => ({
|
|
151
|
+
type: "local",
|
|
152
|
+
command,
|
|
153
|
+
args,
|
|
154
|
+
}),
|
|
102
155
|
},
|
|
103
156
|
];
|
|
104
157
|
}
|
|
@@ -110,22 +163,50 @@ export async function setupCommand(options) {
|
|
|
110
163
|
printUsage();
|
|
111
164
|
return;
|
|
112
165
|
}
|
|
166
|
+
if (options.hub && options.agentSync) {
|
|
167
|
+
console.error(chalk.red("\n --agent-sync cannot be combined with --hub because hub-scoped MCP keys are restricted to that hub only.\n"));
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
// Hub scoping requires API key mode — OAuth grants are configured during
|
|
171
|
+
// the consent screen, not at setup time.
|
|
172
|
+
if (options.hub && !options.apiKey) {
|
|
173
|
+
console.error(chalk.red("\n --hub requires --api-key because hub scoping is configured per API key.\n Use: memax setup --mcp --api-key --hub <id>\n"));
|
|
174
|
+
process.exit(1);
|
|
175
|
+
}
|
|
176
|
+
// Permission flags only apply to remote API key mode — reject in OAuth and local mode
|
|
177
|
+
const permissionFlags = [
|
|
178
|
+
options.readOnly && "--read-only",
|
|
179
|
+
options.allowDelete && "--allow-delete",
|
|
180
|
+
options.allowOrganize && "--allow-organize",
|
|
181
|
+
options.agentSync && "--agent-sync",
|
|
182
|
+
].filter(Boolean);
|
|
183
|
+
if (permissionFlags.length > 0 && (!options.apiKey || options.local)) {
|
|
184
|
+
console.error(chalk.red(`\n ${permissionFlags.join(", ")} only apply to remote API key mode.\n Use: memax setup --mcp --api-key ${permissionFlags.join(" ")}\n`));
|
|
185
|
+
process.exit(1);
|
|
186
|
+
}
|
|
187
|
+
// --api-key is meaningless with --local
|
|
188
|
+
if (options.apiKey && options.local) {
|
|
189
|
+
console.error(chalk.red("\n --api-key and --local are mutually exclusive.\n Use --api-key for remote API key mode, or --local for local CLI mode.\n"));
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
113
192
|
// --print: just output config JSON for manual copy/paste
|
|
114
193
|
if (options.print) {
|
|
115
|
-
await printMcpConfigs(
|
|
194
|
+
await printMcpConfigs({
|
|
195
|
+
local: options.local ?? false,
|
|
196
|
+
apiKey: options.apiKey ?? false,
|
|
197
|
+
hub: options.hub,
|
|
198
|
+
readOnly: options.readOnly,
|
|
199
|
+
allowDelete: options.allowDelete,
|
|
200
|
+
allowOrganize: options.allowOrganize,
|
|
201
|
+
agentSync: options.agentSync,
|
|
202
|
+
});
|
|
116
203
|
return;
|
|
117
204
|
}
|
|
118
|
-
// Remote mode (default):
|
|
205
|
+
// Remote mode (default): OAuth auto-discovery (no API key in config)
|
|
206
|
+
// --api-key: use per-agent API keys (legacy mode, for CI/CD or agents without OAuth)
|
|
207
|
+
// --local: use local CLI binary (memax mcp serve)
|
|
119
208
|
const useRemote = !options.local;
|
|
120
|
-
|
|
121
|
-
if (useRemote && enableMcp) {
|
|
122
|
-
apiKey = await ensureApiKey();
|
|
123
|
-
if (!apiKey) {
|
|
124
|
-
console.error(chalk.red("\n Could not create API key. Log in first: memax login\n" +
|
|
125
|
-
" Or use --local for local MCP server.\n"));
|
|
126
|
-
process.exit(1);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
209
|
+
const useApiKey = options.apiKey ?? false;
|
|
129
210
|
// Local mode: need memax binary
|
|
130
211
|
let memaxBin = null;
|
|
131
212
|
if (!useRemote || enableHooks) {
|
|
@@ -160,8 +241,11 @@ export async function setupCommand(options) {
|
|
|
160
241
|
return;
|
|
161
242
|
}
|
|
162
243
|
console.log(chalk.bold("\n Memax Setup\n"));
|
|
163
|
-
if (useRemote) {
|
|
164
|
-
console.log(chalk.gray(" Mode: remote server (
|
|
244
|
+
if (useRemote && useApiKey) {
|
|
245
|
+
console.log(chalk.gray(" Mode: remote server (API key)\n"));
|
|
246
|
+
}
|
|
247
|
+
else if (useRemote) {
|
|
248
|
+
console.log(chalk.gray(" Mode: remote server (OAuth)\n"));
|
|
165
249
|
}
|
|
166
250
|
else {
|
|
167
251
|
console.log(chalk.gray(" Mode: local CLI\n"));
|
|
@@ -169,16 +253,38 @@ export async function setupCommand(options) {
|
|
|
169
253
|
const results = [];
|
|
170
254
|
for (const agent of agents) {
|
|
171
255
|
const changes = [];
|
|
172
|
-
// MCP setup
|
|
256
|
+
// MCP setup — OAuth (default), API key (--api-key), or local (--local)
|
|
173
257
|
if (enableMcp) {
|
|
174
258
|
try {
|
|
175
|
-
if (useRemote) {
|
|
176
|
-
|
|
259
|
+
if (useRemote && useApiKey) {
|
|
260
|
+
// Legacy API key mode — creates per-agent key with agent_name
|
|
261
|
+
const agentKey = await ensureApiKey(options.hub, agent.id, {
|
|
262
|
+
readOnly: options.readOnly,
|
|
263
|
+
allowDelete: options.allowDelete,
|
|
264
|
+
allowOrganize: options.allowOrganize,
|
|
265
|
+
agentSync: options.agentSync,
|
|
266
|
+
});
|
|
267
|
+
if (!agentKey) {
|
|
268
|
+
console.error(chalk.red(` ✗ ${agent.name}: Could not create API key. Run: memax login`));
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
setupMcpRemote(agent, agentKey);
|
|
272
|
+
changes.push("MCP server (API key)");
|
|
273
|
+
}
|
|
274
|
+
else if (useRemote) {
|
|
275
|
+
// OAuth mode (default) — no API key in config, agent auto-discovers auth
|
|
276
|
+
setupMcpOAuth(agent);
|
|
277
|
+
changes.push("MCP server (OAuth)");
|
|
177
278
|
}
|
|
178
279
|
else {
|
|
280
|
+
const localAgentKey = await ensureLocalAgentKey(agent.id);
|
|
281
|
+
if (!localAgentKey) {
|
|
282
|
+
console.error(chalk.red(` ✗ ${agent.name}: Could not provision local agent key. Run: memax login`));
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
179
285
|
setupMcp(agent, memaxBin);
|
|
286
|
+
changes.push("MCP server (local agent auth)");
|
|
180
287
|
}
|
|
181
|
-
changes.push(useRemote ? "MCP server (remote)" : "MCP server (local)");
|
|
182
288
|
}
|
|
183
289
|
catch (err) {
|
|
184
290
|
console.log(chalk.red(` ✗ ${agent.name}: MCP setup failed — ${err.message}`));
|
|
@@ -203,6 +309,16 @@ export async function setupCommand(options) {
|
|
|
203
309
|
catch (err) {
|
|
204
310
|
console.log(chalk.red(` ✗ ${agent.name}: Instruction injection failed — ${err.message}`));
|
|
205
311
|
}
|
|
312
|
+
// Install memax skills for agents that support skill directories
|
|
313
|
+
try {
|
|
314
|
+
const skillCount = await installSkills(agent);
|
|
315
|
+
if (skillCount > 0) {
|
|
316
|
+
changes.push(`${skillCount} skill${skillCount > 1 ? "s" : ""} installed`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
catch (err) {
|
|
320
|
+
console.log(chalk.red(` ✗ ${agent.name}: Skill install failed — ${err.message}`));
|
|
321
|
+
}
|
|
206
322
|
}
|
|
207
323
|
if (changes.length > 0) {
|
|
208
324
|
results.push({ agent: agent.name, changes });
|
|
@@ -223,8 +339,8 @@ export async function setupCommand(options) {
|
|
|
223
339
|
console.log(chalk.gray("\n MCP tools available to all configured agents:"));
|
|
224
340
|
console.log(chalk.gray(" • memax_recall — semantic search your knowledge"));
|
|
225
341
|
console.log(chalk.gray(" • memax_push — save knowledge from sessions"));
|
|
226
|
-
console.log(chalk.gray(" • memax_get — read full
|
|
227
|
-
console.log(chalk.gray(" •
|
|
342
|
+
console.log(chalk.gray(" • memax_get — read full memory by ID"));
|
|
343
|
+
console.log(chalk.gray(" • memax_list — browse memories"));
|
|
228
344
|
if (enableHooks) {
|
|
229
345
|
const hookAgents = results.filter((r) => r.changes.includes("Context injection hook"));
|
|
230
346
|
if (hookAgents.length > 0) {
|
|
@@ -232,8 +348,32 @@ export async function setupCommand(options) {
|
|
|
232
348
|
console.log(chalk.gray(" Every prompt gets relevant context injected automatically."));
|
|
233
349
|
}
|
|
234
350
|
}
|
|
235
|
-
console.log(chalk.gray("\n Restart your agents for changes to take effect
|
|
351
|
+
console.log(chalk.gray("\n Restart your agents for changes to take effect."));
|
|
352
|
+
// Offer to restore configs from cloud if this looks like a new device
|
|
353
|
+
if (enableInstructions || options.all) {
|
|
354
|
+
try {
|
|
355
|
+
const { syncAgentMemoryCommand } = await import("./agent-configs.js");
|
|
356
|
+
const rl = createInterface({
|
|
357
|
+
input: process.stdin,
|
|
358
|
+
output: process.stdout,
|
|
359
|
+
});
|
|
360
|
+
const restore = await new Promise((resolve) => {
|
|
361
|
+
rl.question(chalk.gray("\n Restore agent configs from Memax cloud? [Y/n] "), (answer) => {
|
|
362
|
+
rl.close();
|
|
363
|
+
resolve(answer.trim().toLowerCase() !== "n");
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
if (restore) {
|
|
367
|
+
await syncAgentMemoryCommand({ skipConflicts: true });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
catch {
|
|
371
|
+
// Sync not available (not logged in, etc.) — skip silently
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
console.log();
|
|
236
375
|
}
|
|
376
|
+
// --- Teardown command ---
|
|
237
377
|
export async function teardownCommand(options) {
|
|
238
378
|
const allAgents = getAgents();
|
|
239
379
|
const onlySet = options.only
|
|
@@ -248,13 +388,19 @@ export async function teardownCommand(options) {
|
|
|
248
388
|
// Claude Code uses its own CLI
|
|
249
389
|
if (agent.id === "claude-code") {
|
|
250
390
|
if (commandExists("claude")) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
391
|
+
// Remove from both user and project scope to clean up old
|
|
392
|
+
// (pre-scope) and new (user-scoped) installs
|
|
393
|
+
for (const scope of ["--scope user", ""]) {
|
|
394
|
+
try {
|
|
395
|
+
execSync(`claude mcp remove memax ${scope}`.trim(), {
|
|
396
|
+
stdio: "pipe",
|
|
397
|
+
});
|
|
398
|
+
console.log(chalk.gray(` Removed MCP from ${agent.name}${scope ? " (user scope)" : " (project scope)"}`));
|
|
399
|
+
removed = true;
|
|
400
|
+
}
|
|
401
|
+
catch {
|
|
402
|
+
// Not installed in this scope
|
|
403
|
+
}
|
|
258
404
|
}
|
|
259
405
|
}
|
|
260
406
|
if (agent.hasHooks && existsSync(agent.configPath)) {
|
|
@@ -285,6 +431,10 @@ export async function teardownCommand(options) {
|
|
|
285
431
|
console.log(chalk.gray(` Removed instructions from ${agent.name}`));
|
|
286
432
|
removed = true;
|
|
287
433
|
}
|
|
434
|
+
if (removeSkills(agent)) {
|
|
435
|
+
console.log(chalk.gray(` Removed skills from ${agent.name}`));
|
|
436
|
+
removed = true;
|
|
437
|
+
}
|
|
288
438
|
}
|
|
289
439
|
catch {
|
|
290
440
|
// Skip agents we can't clean up
|
|
@@ -296,480 +446,7 @@ export async function teardownCommand(options) {
|
|
|
296
446
|
}
|
|
297
447
|
console.log(chalk.green("\n Memax integrations removed.\n Restart your agents for changes to take effect.\n"));
|
|
298
448
|
}
|
|
299
|
-
// ---
|
|
300
|
-
async function ensureApiKey() {
|
|
301
|
-
try {
|
|
302
|
-
const result = await apiPost("/v1/auth/api-keys", {
|
|
303
|
-
name: "mcp-setup",
|
|
304
|
-
expires_in_days: 0, // no expiry
|
|
305
|
-
});
|
|
306
|
-
return result.key;
|
|
307
|
-
}
|
|
308
|
-
catch {
|
|
309
|
-
return undefined;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
function getApiUrl() {
|
|
313
|
-
return loadConfig().api_url;
|
|
314
|
-
}
|
|
315
|
-
function setupMcpRemote(agent, apiKey) {
|
|
316
|
-
const mcpUrl = `${getApiUrl()}/mcp`;
|
|
317
|
-
// Claude Code uses its own CLI
|
|
318
|
-
if (agent.id === "claude-code") {
|
|
319
|
-
if (!commandExists("claude")) {
|
|
320
|
-
throw new Error("claude CLI not found in PATH");
|
|
321
|
-
}
|
|
322
|
-
try {
|
|
323
|
-
execSync("claude mcp remove memax", { stdio: "pipe" });
|
|
324
|
-
}
|
|
325
|
-
catch {
|
|
326
|
-
// Not installed yet
|
|
327
|
-
}
|
|
328
|
-
// Claude Code HTTP transport
|
|
329
|
-
execSync(`claude mcp add memax --transport http ${mcpUrl} --header "Authorization: Bearer ${apiKey}"`, { stdio: "pipe" });
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
// Codex TOML
|
|
333
|
-
if (agent.format === "toml") {
|
|
334
|
-
mkdirSync(dirname(agent.configPath), { recursive: true });
|
|
335
|
-
let content = "";
|
|
336
|
-
if (existsSync(agent.configPath)) {
|
|
337
|
-
content = readFileSync(agent.configPath, "utf-8");
|
|
338
|
-
}
|
|
339
|
-
content = content.replace(/\[mcp_servers\.memax\][\s\S]*?(?=\n\[|$)/, "");
|
|
340
|
-
content = content.trim();
|
|
341
|
-
if (content)
|
|
342
|
-
content += "\n\n";
|
|
343
|
-
content += `[mcp_servers.memax]\ntype = "url"\nurl = "${mcpUrl}"\n\n[mcp_servers.memax.headers]\nAuthorization = "Bearer ${apiKey}"\n`;
|
|
344
|
-
writeFileSync(agent.configPath, content);
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
// JSON-based agents
|
|
348
|
-
mkdirSync(dirname(agent.configPath), { recursive: true });
|
|
349
|
-
let config = {};
|
|
350
|
-
if (existsSync(agent.configPath)) {
|
|
351
|
-
try {
|
|
352
|
-
config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
353
|
-
}
|
|
354
|
-
catch {
|
|
355
|
-
// Start fresh
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
const servers = (getNestedKey(config, agent.mcpKey) ?? {});
|
|
359
|
-
servers.memax = {
|
|
360
|
-
type: "url",
|
|
361
|
-
url: mcpUrl,
|
|
362
|
-
headers: {
|
|
363
|
-
Authorization: `Bearer ${apiKey}`,
|
|
364
|
-
},
|
|
365
|
-
};
|
|
366
|
-
setNestedKey(config, agent.mcpKey, servers);
|
|
367
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
368
|
-
}
|
|
369
|
-
async function printMcpConfigs(local) {
|
|
370
|
-
const apiUrl = getApiUrl();
|
|
371
|
-
console.log(chalk.bold("\n Memax MCP Configuration\n"));
|
|
372
|
-
if (local) {
|
|
373
|
-
const bin = resolveMemaxBin();
|
|
374
|
-
const cmd = bin ? bin.command : "memax";
|
|
375
|
-
const args = bin ? [...bin.args, "mcp", "serve"] : ["mcp", "serve"];
|
|
376
|
-
console.log(chalk.gray(" Mode: local (stdio)\n"));
|
|
377
|
-
console.log(chalk.white(" For most agents (Claude Code, Cursor, Gemini, etc.):\n"));
|
|
378
|
-
console.log(JSON.stringify({
|
|
379
|
-
mcpServers: {
|
|
380
|
-
memax: { command: cmd, args },
|
|
381
|
-
},
|
|
382
|
-
}, null, 2)
|
|
383
|
-
.split("\n")
|
|
384
|
-
.map((l) => " " + l)
|
|
385
|
-
.join("\n"));
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
let apiKey;
|
|
389
|
-
try {
|
|
390
|
-
apiKey = await ensureApiKey();
|
|
391
|
-
}
|
|
392
|
-
catch {
|
|
393
|
-
// Not logged in
|
|
394
|
-
}
|
|
395
|
-
const keyDisplay = apiKey ?? "mxk_your_api_key_here";
|
|
396
|
-
const mcpUrl = `${apiUrl}/mcp`;
|
|
397
|
-
console.log(chalk.gray(" Mode: remote server (recommended)\n"));
|
|
398
|
-
console.log(chalk.white(" For Claude Code:\n"));
|
|
399
|
-
console.log(chalk.gray(` claude mcp add memax --transport http ${mcpUrl} --header "Authorization: Bearer ${keyDisplay}"`));
|
|
400
|
-
console.log(chalk.white("\n For Cursor, Copilot, Gemini, Windsurf:\n"));
|
|
401
|
-
console.log(JSON.stringify({
|
|
402
|
-
mcpServers: {
|
|
403
|
-
memax: {
|
|
404
|
-
type: "url",
|
|
405
|
-
url: mcpUrl,
|
|
406
|
-
headers: {
|
|
407
|
-
Authorization: `Bearer ${keyDisplay}`,
|
|
408
|
-
},
|
|
409
|
-
},
|
|
410
|
-
},
|
|
411
|
-
}, null, 2)
|
|
412
|
-
.split("\n")
|
|
413
|
-
.map((l) => " " + l)
|
|
414
|
-
.join("\n"));
|
|
415
|
-
console.log(chalk.white("\n For Codex CLI (~/.codex/config.toml):\n"));
|
|
416
|
-
console.log(chalk.gray(` [mcp_servers.memax]`));
|
|
417
|
-
console.log(chalk.gray(` type = "url"`));
|
|
418
|
-
console.log(chalk.gray(` url = "${mcpUrl}"`));
|
|
419
|
-
console.log(chalk.gray(`\n [mcp_servers.memax.headers]`));
|
|
420
|
-
console.log(chalk.gray(` Authorization = "Bearer ${keyDisplay}"`));
|
|
421
|
-
if (apiKey) {
|
|
422
|
-
console.log(chalk.yellow("\n API key created: mcp-setup"));
|
|
423
|
-
console.log(chalk.gray(" Manage keys: memax auth list-keys"));
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
console.log(chalk.yellow("\n Not logged in — replace mxk_your_api_key_here with a real key."));
|
|
427
|
-
console.log(chalk.gray(" Run: memax login && memax auth create-key --name mcp"));
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
console.log();
|
|
431
|
-
}
|
|
432
|
-
// --- Local MCP setup per agent ---
|
|
433
|
-
function setupMcp(agent, bin) {
|
|
434
|
-
// Claude Code has its own CLI for MCP management
|
|
435
|
-
if (agent.id === "claude-code") {
|
|
436
|
-
setupMcpClaudeCode(bin);
|
|
437
|
-
return;
|
|
438
|
-
}
|
|
439
|
-
mkdirSync(dirname(agent.configPath), { recursive: true });
|
|
440
|
-
if (agent.format === "toml") {
|
|
441
|
-
setupMcpToml(agent, bin);
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
// JSON-based agents
|
|
445
|
-
let config = {};
|
|
446
|
-
if (existsSync(agent.configPath)) {
|
|
447
|
-
try {
|
|
448
|
-
config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
449
|
-
}
|
|
450
|
-
catch {
|
|
451
|
-
// Start fresh
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
const servers = (getNestedKey(config, agent.mcpKey) ?? {});
|
|
455
|
-
servers.memax = {
|
|
456
|
-
command: bin.command,
|
|
457
|
-
args: [...bin.args, "mcp", "serve"],
|
|
458
|
-
};
|
|
459
|
-
setNestedKey(config, agent.mcpKey, servers);
|
|
460
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
461
|
-
}
|
|
462
|
-
function setupMcpClaudeCode(bin) {
|
|
463
|
-
// Claude Code uses its own CLI for MCP — settings.json mcpServers is ignored
|
|
464
|
-
if (!commandExists("claude")) {
|
|
465
|
-
throw new Error("claude CLI not found in PATH");
|
|
466
|
-
}
|
|
467
|
-
// Remove existing first (idempotent)
|
|
468
|
-
try {
|
|
469
|
-
execSync("claude mcp remove memax", { stdio: "pipe" });
|
|
470
|
-
}
|
|
471
|
-
catch {
|
|
472
|
-
// Not installed yet — fine
|
|
473
|
-
}
|
|
474
|
-
// claude mcp add <name> -- <command> [args...]
|
|
475
|
-
const allArgs = [...bin.args, "mcp", "serve"];
|
|
476
|
-
const cmd = `claude mcp add memax -- ${bin.command} ${allArgs.join(" ")}`;
|
|
477
|
-
try {
|
|
478
|
-
execSync(cmd, { stdio: "pipe" });
|
|
479
|
-
}
|
|
480
|
-
catch (err) {
|
|
481
|
-
throw new Error(`claude mcp add failed: ${err.message}`);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
function setupMcpToml(agent, bin) {
|
|
485
|
-
// Codex uses TOML — append or update the memax section
|
|
486
|
-
let content = "";
|
|
487
|
-
if (existsSync(agent.configPath)) {
|
|
488
|
-
content = readFileSync(agent.configPath, "utf-8");
|
|
489
|
-
}
|
|
490
|
-
// Remove existing memax section if present
|
|
491
|
-
content = content.replace(/\[mcp_servers\.memax\][\s\S]*?(?=\n\[|$)/, "");
|
|
492
|
-
const args = [...bin.args, "mcp", "serve"].map((a) => `"${a}"`).join(", ");
|
|
493
|
-
content = content.trim();
|
|
494
|
-
if (content)
|
|
495
|
-
content += "\n\n";
|
|
496
|
-
content += `[mcp_servers.memax]\ncommand = "${bin.command}"\nargs = [${args}]\n`;
|
|
497
|
-
writeFileSync(agent.configPath, content);
|
|
498
|
-
}
|
|
499
|
-
// --- Hook setup ---
|
|
500
|
-
function setupHooks(agent, bin) {
|
|
501
|
-
if (agent.id === "claude-code") {
|
|
502
|
-
setupClaudeCodeHooks(agent, bin);
|
|
503
|
-
}
|
|
504
|
-
else if (agent.id === "gemini") {
|
|
505
|
-
setupGeminiHooks(agent, bin);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
function setupClaudeCodeHooks(agent, bin) {
|
|
509
|
-
const hookScript = writeHookScript(bin);
|
|
510
|
-
let config = {};
|
|
511
|
-
if (existsSync(agent.configPath)) {
|
|
512
|
-
try {
|
|
513
|
-
config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
514
|
-
}
|
|
515
|
-
catch {
|
|
516
|
-
// Start fresh
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
const hooks = (config.hooks ?? {});
|
|
520
|
-
// Remove existing memax hooks
|
|
521
|
-
if (hooks["UserPromptSubmit"]) {
|
|
522
|
-
hooks["UserPromptSubmit"] = hooks["UserPromptSubmit"].filter((h) => !h.hooks?.some((hh) => hh.command?.includes("memax")));
|
|
523
|
-
}
|
|
524
|
-
hooks["UserPromptSubmit"] = [
|
|
525
|
-
...(hooks["UserPromptSubmit"] ?? []),
|
|
526
|
-
{
|
|
527
|
-
matcher: "",
|
|
528
|
-
hooks: [{ type: "command", command: hookScript, timeout: 30 }],
|
|
529
|
-
},
|
|
530
|
-
];
|
|
531
|
-
config.hooks = hooks;
|
|
532
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
533
|
-
}
|
|
534
|
-
function setupGeminiHooks(agent, bin) {
|
|
535
|
-
const hookScript = writeHookScript(bin);
|
|
536
|
-
let config = {};
|
|
537
|
-
if (existsSync(agent.configPath)) {
|
|
538
|
-
try {
|
|
539
|
-
config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
540
|
-
}
|
|
541
|
-
catch {
|
|
542
|
-
// Start fresh
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
const hooks = (config.hooks ?? {});
|
|
546
|
-
// Remove existing memax hooks from both old ("Startup") and correct event
|
|
547
|
-
for (const event of ["Startup", "BeforeAgent"]) {
|
|
548
|
-
if (hooks[event]) {
|
|
549
|
-
hooks[event] = hooks[event].filter((h) => !h.hooks?.some((hh) => hh.command?.includes("memax")));
|
|
550
|
-
if (hooks[event].length === 0)
|
|
551
|
-
delete hooks[event];
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
// BeforeAgent fires after user submits a prompt — equivalent to Claude Code's PrePromptSubmit
|
|
555
|
-
hooks["BeforeAgent"] = [
|
|
556
|
-
...(hooks["BeforeAgent"] ?? []),
|
|
557
|
-
{
|
|
558
|
-
matcher: "",
|
|
559
|
-
hooks: [{ type: "command", command: hookScript, timeout: 30 }],
|
|
560
|
-
},
|
|
561
|
-
];
|
|
562
|
-
config.hooks = hooks;
|
|
563
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
564
|
-
}
|
|
565
|
-
// --- Teardown helpers ---
|
|
566
|
-
function removeMcpJson(agent) {
|
|
567
|
-
if (!existsSync(agent.configPath))
|
|
568
|
-
return false;
|
|
569
|
-
try {
|
|
570
|
-
const config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
571
|
-
const servers = getNestedKey(config, agent.mcpKey);
|
|
572
|
-
if (!servers?.memax)
|
|
573
|
-
return false;
|
|
574
|
-
delete servers.memax;
|
|
575
|
-
if (Object.keys(servers).length === 0)
|
|
576
|
-
deleteNestedKey(config, agent.mcpKey);
|
|
577
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
578
|
-
console.log(chalk.gray(` Removed MCP from ${agent.name}`));
|
|
579
|
-
return true;
|
|
580
|
-
}
|
|
581
|
-
catch {
|
|
582
|
-
return false;
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
function removeMcpToml(agent) {
|
|
586
|
-
if (!existsSync(agent.configPath))
|
|
587
|
-
return false;
|
|
588
|
-
let content = readFileSync(agent.configPath, "utf-8");
|
|
589
|
-
const before = content;
|
|
590
|
-
content = content.replace(/\[mcp_servers\.memax\][\s\S]*?(?=\n\[|$)/, "");
|
|
591
|
-
if (content === before)
|
|
592
|
-
return false;
|
|
593
|
-
writeFileSync(agent.configPath, content.trim() + "\n");
|
|
594
|
-
console.log(chalk.gray(` Removed MCP from ${agent.name}`));
|
|
595
|
-
return true;
|
|
596
|
-
}
|
|
597
|
-
function removeHooks(agent) {
|
|
598
|
-
if (!existsSync(agent.configPath))
|
|
599
|
-
return false;
|
|
600
|
-
try {
|
|
601
|
-
const config = JSON.parse(readFileSync(agent.configPath, "utf-8"));
|
|
602
|
-
const hooks = config.hooks;
|
|
603
|
-
if (!hooks)
|
|
604
|
-
return false;
|
|
605
|
-
let removed = false;
|
|
606
|
-
for (const event of Object.keys(hooks)) {
|
|
607
|
-
const before = hooks[event].length;
|
|
608
|
-
hooks[event] = hooks[event].filter((h) => !h.command?.includes("memax") &&
|
|
609
|
-
!h.hooks?.some((hh) => hh.command?.includes("memax")));
|
|
610
|
-
if (hooks[event].length < before)
|
|
611
|
-
removed = true;
|
|
612
|
-
if (hooks[event].length === 0)
|
|
613
|
-
delete hooks[event];
|
|
614
|
-
}
|
|
615
|
-
if (Object.keys(hooks).length === 0)
|
|
616
|
-
delete config.hooks;
|
|
617
|
-
if (removed) {
|
|
618
|
-
writeFileSync(agent.configPath, JSON.stringify(config, null, 2) + "\n");
|
|
619
|
-
console.log(chalk.gray(` Removed hooks from ${agent.name}`));
|
|
620
|
-
}
|
|
621
|
-
return removed;
|
|
622
|
-
}
|
|
623
|
-
catch {
|
|
624
|
-
return false;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
function resolveMemaxBin() {
|
|
628
|
-
// 1. Global install — use absolute path so agents find it without shell PATH
|
|
629
|
-
if (commandExists("memax")) {
|
|
630
|
-
try {
|
|
631
|
-
const which = platform() === "win32" ? "where memax" : "which memax";
|
|
632
|
-
const absPath = execSync(which, { encoding: "utf-8", stdio: "pipe" })
|
|
633
|
-
.trim()
|
|
634
|
-
.split("\n")[0];
|
|
635
|
-
if (absPath) {
|
|
636
|
-
return { command: absPath, args: [], shell: absPath };
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
catch {
|
|
640
|
-
// fall through
|
|
641
|
-
}
|
|
642
|
-
return { command: "memax", args: [], shell: "memax" };
|
|
643
|
-
}
|
|
644
|
-
// 2. Local repo build (faster than npx, always up-to-date during dev)
|
|
645
|
-
const localBuild = join(process.cwd(), "packages", "cli", "dist", "index.js");
|
|
646
|
-
if (existsSync(localBuild)) {
|
|
647
|
-
return {
|
|
648
|
-
command: "node",
|
|
649
|
-
args: [localBuild],
|
|
650
|
-
shell: `node ${localBuild}`,
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
// 3. npx as last resort (slow startup — agents may timeout on first run)
|
|
654
|
-
try {
|
|
655
|
-
execSync("npx --yes memax-cli --version", {
|
|
656
|
-
encoding: "utf-8",
|
|
657
|
-
timeout: 15000,
|
|
658
|
-
stdio: "pipe",
|
|
659
|
-
});
|
|
660
|
-
return {
|
|
661
|
-
command: "npx",
|
|
662
|
-
args: ["-y", "memax-cli"],
|
|
663
|
-
shell: "npx -y memax-cli",
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
catch {
|
|
667
|
-
// npx failed
|
|
668
|
-
}
|
|
669
|
-
return null;
|
|
670
|
-
}
|
|
671
|
-
function commandExists(cmd) {
|
|
672
|
-
try {
|
|
673
|
-
const which = platform() === "win32" ? "where" : "which";
|
|
674
|
-
execSync(`${which} ${cmd}`, { stdio: "pipe" });
|
|
675
|
-
return true;
|
|
676
|
-
}
|
|
677
|
-
catch {
|
|
678
|
-
return false;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
// Nested key helpers for configs like openclaw's "mcp.servers"
|
|
682
|
-
function getNestedKey(obj, key) {
|
|
683
|
-
const parts = key.split(".");
|
|
684
|
-
let current = obj;
|
|
685
|
-
for (const part of parts) {
|
|
686
|
-
if (current == null || typeof current !== "object")
|
|
687
|
-
return undefined;
|
|
688
|
-
current = current[part];
|
|
689
|
-
}
|
|
690
|
-
return current;
|
|
691
|
-
}
|
|
692
|
-
function setNestedKey(obj, key, value) {
|
|
693
|
-
const parts = key.split(".");
|
|
694
|
-
let current = obj;
|
|
695
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
696
|
-
if (!(parts[i] in current) || typeof current[parts[i]] !== "object") {
|
|
697
|
-
current[parts[i]] = {};
|
|
698
|
-
}
|
|
699
|
-
current = current[parts[i]];
|
|
700
|
-
}
|
|
701
|
-
current[parts[parts.length - 1]] = value;
|
|
702
|
-
}
|
|
703
|
-
function deleteNestedKey(obj, key) {
|
|
704
|
-
const parts = key.split(".");
|
|
705
|
-
let current = obj;
|
|
706
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
707
|
-
if (!(parts[i] in current) || typeof current[parts[i]] !== "object")
|
|
708
|
-
return;
|
|
709
|
-
current = current[parts[i]];
|
|
710
|
-
}
|
|
711
|
-
delete current[parts[parts.length - 1]];
|
|
712
|
-
}
|
|
713
|
-
// --- Instruction injection ---
|
|
714
|
-
const MEMAX_INSTRUCTION_BLOCK = `
|
|
715
|
-
<!-- memax:start -->
|
|
716
|
-
## Memax — Persistent Memory
|
|
717
|
-
|
|
718
|
-
You have access to Memax, a persistent cloud knowledge hub shared across all your AI agents.
|
|
719
|
-
Use it proactively — don't wait for the user to ask.
|
|
720
|
-
|
|
721
|
-
**At session start:** Use memax_recall to check for relevant context about the current project or task.
|
|
722
|
-
**During work:** When you discover important decisions, architecture details, debugging solutions,
|
|
723
|
-
or useful context — use memax_push to save them for future sessions.
|
|
724
|
-
**At session end:** Summarize key decisions, learnings, or context worth remembering and push them.
|
|
725
|
-
|
|
726
|
-
**What to remember:** Architecture decisions, API conventions, deployment processes, debugging
|
|
727
|
-
solutions, team preferences, project-specific knowledge. If you'd want to know it in a future
|
|
728
|
-
session, push it now.
|
|
729
|
-
|
|
730
|
-
**What NOT to remember:** Ephemeral task details, file contents (they're in git), obvious things.
|
|
731
|
-
|
|
732
|
-
Available tools: memax_recall (search), memax_push (save), memax_get (read full note),
|
|
733
|
-
memax_search (browse), memax_forget (delete outdated memories).
|
|
734
|
-
<!-- memax:end -->
|
|
735
|
-
`.trim();
|
|
736
|
-
function injectInstructions(filePath) {
|
|
737
|
-
mkdirSync(dirname(filePath), { recursive: true });
|
|
738
|
-
let content = "";
|
|
739
|
-
if (existsSync(filePath)) {
|
|
740
|
-
content = readFileSync(filePath, "utf-8");
|
|
741
|
-
}
|
|
742
|
-
// Remove existing memax block (idempotent)
|
|
743
|
-
content = content.replace(/\n?<!-- memax:start -->[\s\S]*?<!-- memax:end -->\n?/, "");
|
|
744
|
-
// Append the block
|
|
745
|
-
content = content.trimEnd() + "\n\n" + MEMAX_INSTRUCTION_BLOCK + "\n";
|
|
746
|
-
writeFileSync(filePath, content);
|
|
747
|
-
}
|
|
748
|
-
function removeInstructions(filePath) {
|
|
749
|
-
if (!existsSync(filePath))
|
|
750
|
-
return false;
|
|
751
|
-
const content = readFileSync(filePath, "utf-8");
|
|
752
|
-
const cleaned = content.replace(/\n?<!-- memax:start -->[\s\S]*?<!-- memax:end -->\n?/, "");
|
|
753
|
-
if (cleaned === content)
|
|
754
|
-
return false;
|
|
755
|
-
writeFileSync(filePath, cleaned.trimEnd() + "\n");
|
|
756
|
-
return true;
|
|
757
|
-
}
|
|
758
|
-
function writeHookScript(bin) {
|
|
759
|
-
const hooksDir = join(homedir(), ".memax", "hooks");
|
|
760
|
-
mkdirSync(hooksDir, { recursive: true });
|
|
761
|
-
const isWindows = platform() === "win32";
|
|
762
|
-
const scriptName = isWindows ? "context-inject.cmd" : "context-inject.sh";
|
|
763
|
-
const scriptPath = join(hooksDir, scriptName);
|
|
764
|
-
if (isWindows) {
|
|
765
|
-
writeFileSync(scriptPath, WIN_HOOK.replace(/\$MEMAX/g, bin.shell));
|
|
766
|
-
}
|
|
767
|
-
else {
|
|
768
|
-
writeFileSync(scriptPath, UNIX_HOOK.replace(/\$MEMAX/g, bin.shell));
|
|
769
|
-
chmodSync(scriptPath, 0o755);
|
|
770
|
-
}
|
|
771
|
-
return scriptPath;
|
|
772
|
-
}
|
|
449
|
+
// --- Usage ---
|
|
773
450
|
function printUsage() {
|
|
774
451
|
const agents = getAgents();
|
|
775
452
|
const detected = agents.filter((a) => a.detect());
|
|
@@ -787,8 +464,11 @@ function printUsage() {
|
|
|
787
464
|
console.log(chalk.gray(" memax setup --instructions Inject memax usage instructions into agent configs"));
|
|
788
465
|
console.log(chalk.gray(" memax setup --all MCP + hooks + instructions"));
|
|
789
466
|
console.log(chalk.gray(" memax setup --mcp --local Use local CLI instead of remote server"));
|
|
467
|
+
console.log(chalk.gray(" memax setup --mcp --api-key Use API keys instead of OAuth (CI/CD)"));
|
|
790
468
|
console.log(chalk.gray(" memax setup --print Print MCP config to copy/paste"));
|
|
791
469
|
console.log(chalk.gray(" memax setup --mcp --only claude-code,cursor"));
|
|
470
|
+
console.log(chalk.gray(" memax setup --mcp --read-only"));
|
|
471
|
+
console.log(chalk.gray(" memax setup --mcp --hub memax-team --allow-organize"));
|
|
792
472
|
console.log(chalk.gray(" memax teardown Remove all integrations\n"));
|
|
793
473
|
console.log(chalk.gray(" Supported agents:"));
|
|
794
474
|
for (const a of agents) {
|
|
@@ -799,25 +479,29 @@ function printUsage() {
|
|
|
799
479
|
}
|
|
800
480
|
console.log();
|
|
801
481
|
}
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
482
|
+
export function registerSetupCommands(program) {
|
|
483
|
+
program
|
|
484
|
+
.command("setup")
|
|
485
|
+
.description("Set up AI agent integrations (auto-detects installed agents)")
|
|
486
|
+
.option("--mcp", "Enable MCP server (agent tools)")
|
|
487
|
+
.option("--hooks", "Enable context injection hooks")
|
|
488
|
+
.option("--instructions", "Inject memax instructions into agent config files")
|
|
489
|
+
.option("--all", "Enable MCP + hooks + instructions")
|
|
490
|
+
.option("--local", "Use local stdio MCP instead of remote server")
|
|
491
|
+
.option("--api-key", "Use per-agent API keys instead of OAuth (for CI/CD or agents without OAuth support)")
|
|
492
|
+
.option("--print", "Print MCP config JSON to copy/paste (no changes made)")
|
|
493
|
+
.option("--only <agents>", "Only configure these agents (comma-separated)")
|
|
494
|
+
.option("--skip <agents>", "Skip these agents (comma-separated)")
|
|
495
|
+
.option("--hub <id>", "Scope MCP key to a specific hub")
|
|
496
|
+
.option("--read-only", "Create remote MCP keys without write access")
|
|
497
|
+
.option("--allow-delete", "Allow remote MCP keys to delete memories")
|
|
498
|
+
.option("--allow-organize", "Allow remote MCP keys to organize topics and run dreams")
|
|
499
|
+
.option("--agent-sync", "Allow remote MCP keys to sync agent configs and sessions")
|
|
500
|
+
.action(setupCommand);
|
|
501
|
+
program
|
|
502
|
+
.command("teardown")
|
|
503
|
+
.description("Remove Memax integrations from agents")
|
|
504
|
+
.option("--only <agents>", "Only remove from these agents (comma-separated)")
|
|
505
|
+
.action(teardownCommand);
|
|
506
|
+
}
|
|
823
507
|
//# sourceMappingURL=setup.js.map
|