@synth-coder/memhub 0.2.3 → 0.2.5
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/.github/workflows/ci.yml +48 -12
- package/.github/workflows/release.yml +70 -0
- package/AGENTS.md +71 -73
- package/README.md +284 -195
- package/README.zh-CN.md +90 -30
- package/dist/src/cli/agents/claude-code.d.ts +5 -0
- package/dist/src/cli/agents/claude-code.d.ts.map +1 -0
- package/dist/src/cli/agents/claude-code.js +14 -0
- package/dist/src/cli/agents/claude-code.js.map +1 -0
- package/dist/src/cli/agents/cline.d.ts +5 -0
- package/dist/src/cli/agents/cline.d.ts.map +1 -0
- package/dist/src/cli/agents/cline.js +14 -0
- package/dist/src/cli/agents/cline.js.map +1 -0
- package/dist/src/cli/agents/cursor.d.ts +5 -0
- package/dist/src/cli/agents/cursor.d.ts.map +1 -0
- package/dist/src/cli/agents/cursor.js +14 -0
- package/dist/src/cli/agents/cursor.js.map +1 -0
- package/dist/src/cli/agents/factory-droid.d.ts +5 -0
- package/dist/src/cli/agents/factory-droid.d.ts.map +1 -0
- package/dist/src/cli/agents/factory-droid.js +14 -0
- package/dist/src/cli/agents/factory-droid.js.map +1 -0
- package/dist/src/cli/agents/gemini-cli.d.ts +5 -0
- package/dist/src/cli/agents/gemini-cli.d.ts.map +1 -0
- package/dist/src/cli/agents/gemini-cli.js +14 -0
- package/dist/src/cli/agents/gemini-cli.js.map +1 -0
- package/dist/src/cli/agents/index.d.ts +13 -0
- package/dist/src/cli/agents/index.d.ts.map +1 -0
- package/dist/src/cli/agents/index.js +27 -0
- package/dist/src/cli/agents/index.js.map +1 -0
- package/dist/src/cli/agents/windsurf.d.ts +5 -0
- package/dist/src/cli/agents/windsurf.d.ts.map +1 -0
- package/dist/src/cli/agents/windsurf.js +14 -0
- package/dist/src/cli/agents/windsurf.js.map +1 -0
- package/dist/src/cli/index.d.ts +8 -0
- package/dist/src/cli/index.d.ts.map +1 -0
- package/dist/src/cli/index.js +168 -0
- package/dist/src/cli/index.js.map +1 -0
- package/dist/src/cli/init.d.ts +34 -0
- package/dist/src/cli/init.d.ts.map +1 -0
- package/dist/src/cli/init.js +140 -0
- package/dist/src/cli/init.js.map +1 -0
- package/dist/src/cli/instructions.d.ts +29 -0
- package/dist/src/cli/instructions.d.ts.map +1 -0
- package/dist/src/cli/instructions.js +141 -0
- package/dist/src/cli/instructions.js.map +1 -0
- package/dist/src/cli/types.d.ts +22 -0
- package/dist/src/cli/types.d.ts.map +1 -0
- package/dist/src/cli/types.js +75 -0
- package/dist/src/cli/types.js.map +1 -0
- package/dist/src/contracts/mcp.js +34 -34
- package/dist/src/contracts/schemas.js.map +1 -1
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/mcp-server.js +7 -14
- package/dist/src/server/mcp-server.js.map +1 -1
- package/dist/src/services/embedding-service.d.ts.map +1 -1
- package/dist/src/services/embedding-service.js +1 -1
- package/dist/src/services/embedding-service.js.map +1 -1
- package/dist/src/services/memory-service.d.ts.map +1 -1
- package/dist/src/services/memory-service.js.map +1 -1
- package/dist/src/storage/markdown-storage.d.ts.map +1 -1
- package/dist/src/storage/markdown-storage.js +1 -1
- package/dist/src/storage/markdown-storage.js.map +1 -1
- package/dist/src/storage/vector-index.d.ts.map +1 -1
- package/dist/src/storage/vector-index.js +4 -5
- package/dist/src/storage/vector-index.js.map +1 -1
- package/docs/README.md +21 -0
- package/docs/mcp-tools.md +136 -0
- package/docs/user-guide.md +184 -0
- package/package.json +61 -59
- package/src/cli/agents/claude-code.ts +14 -0
- package/src/cli/agents/cline.ts +14 -0
- package/src/cli/agents/codex.ts +14 -0
- package/src/cli/agents/cursor.ts +14 -0
- package/src/cli/agents/factory-droid.ts +14 -0
- package/src/cli/agents/gemini-cli.ts +14 -0
- package/src/cli/agents/index.ts +36 -0
- package/src/cli/agents/windsurf.ts +14 -0
- package/src/cli/index.ts +192 -0
- package/src/cli/init.ts +218 -0
- package/src/cli/instructions.ts +156 -0
- package/src/cli/types.ts +112 -0
- package/src/contracts/index.ts +12 -12
- package/src/contracts/mcp.ts +223 -223
- package/src/contracts/schemas.ts +307 -307
- package/src/contracts/types.ts +410 -410
- package/src/index.ts +8 -8
- package/src/server/index.ts +5 -5
- package/src/server/mcp-server.ts +169 -186
- package/src/services/embedding-service.ts +114 -114
- package/src/services/index.ts +5 -5
- package/src/services/memory-service.ts +656 -663
- package/src/storage/frontmatter-parser.ts +243 -243
- package/src/storage/index.ts +6 -6
- package/src/storage/markdown-storage.ts +228 -236
- package/src/storage/vector-index.ts +159 -160
- package/src/utils/index.ts +5 -5
- package/src/utils/slugify.ts +63 -63
- package/test/cli/init.test.ts +402 -0
- package/test/contracts/schemas.test.ts +313 -313
- package/test/contracts/types.test.ts +21 -21
- package/test/frontmatter-parser-more.test.ts +94 -94
- package/test/server/mcp-server.test.ts +211 -210
- package/test/services/memory-service-edge.test.ts +248 -248
- package/test/services/memory-service.test.ts +291 -279
- package/test/storage/frontmatter-parser.test.ts +223 -223
- package/test/storage/markdown-storage.test.ts +226 -217
- package/test/storage/storage-edge.test.ts +238 -238
- package/test/storage/vector-index.test.ts +149 -153
- package/test/utils/slugify-edge.test.ts +94 -94
- package/test/utils/slugify.test.ts +72 -68
- package/docs/architecture-diagrams.md +0 -368
- package/docs/architecture.md +0 -381
- package/docs/contracts.md +0 -190
- package/docs/prompt-template.md +0 -33
- package/docs/proposals/mcp-typescript-sdk-refactor.md +0 -568
- package/docs/proposals/proposal-close-gates.md +0 -58
- package/docs/tool-calling-policy.md +0 -101
- package/docs/vector-search.md +0 -306
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent configuration generators index
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { generateCursorConfig } from './cursor.js';
|
|
6
|
+
export { generateClaudeCodeConfig } from './claude-code.js';
|
|
7
|
+
export { generateClineConfig } from './cline.js';
|
|
8
|
+
export { generateWindsurfConfig } from './windsurf.js';
|
|
9
|
+
export { generateFactoryDroidConfig } from './factory-droid.js';
|
|
10
|
+
export { generateGeminiCliConfig } from './gemini-cli.js';
|
|
11
|
+
export { generateCodexConfig } from './codex.js';
|
|
12
|
+
|
|
13
|
+
import type { AgentType } from '../types.js';
|
|
14
|
+
import { generateCursorConfig } from './cursor.js';
|
|
15
|
+
import { generateClaudeCodeConfig } from './claude-code.js';
|
|
16
|
+
import { generateClineConfig } from './cline.js';
|
|
17
|
+
import { generateWindsurfConfig } from './windsurf.js';
|
|
18
|
+
import { generateFactoryDroidConfig } from './factory-droid.js';
|
|
19
|
+
import { generateGeminiCliConfig } from './gemini-cli.js';
|
|
20
|
+
import { generateCodexConfig } from './codex.js';
|
|
21
|
+
|
|
22
|
+
export type ConfigGenerator = (memhubPath: string) => Record<string, unknown>;
|
|
23
|
+
|
|
24
|
+
const generators: Record<AgentType, ConfigGenerator> = {
|
|
25
|
+
cursor: generateCursorConfig,
|
|
26
|
+
'claude-code': generateClaudeCodeConfig,
|
|
27
|
+
cline: generateClineConfig,
|
|
28
|
+
windsurf: generateWindsurfConfig,
|
|
29
|
+
'factory-droid': generateFactoryDroidConfig,
|
|
30
|
+
'gemini-cli': generateGeminiCliConfig,
|
|
31
|
+
codex: generateCodexConfig,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function getConfigGenerator(agentId: AgentType): ConfigGenerator {
|
|
35
|
+
return generators[agentId];
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windsurf MCP configuration generator
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function generateWindsurfConfig(_memhubPath: string): Record<string, unknown> {
|
|
6
|
+
return {
|
|
7
|
+
mcpServers: {
|
|
8
|
+
memhub: {
|
|
9
|
+
command: 'npx',
|
|
10
|
+
args: ['-y', '@synth-coder/memhub@latest'],
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* MemHub CLI entry point
|
|
4
|
+
* - No args: start MCP server
|
|
5
|
+
* - With args: run CLI commands
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { readFileSync, existsSync } from 'fs';
|
|
9
|
+
import { join, dirname } from 'path';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
import * as p from '@clack/prompts';
|
|
12
|
+
import { AGENTS, type AgentType } from './types.js';
|
|
13
|
+
import { initAgent, selectAgentInteractive } from './init.js';
|
|
14
|
+
import { createMcpServer } from '../server/mcp-server.js';
|
|
15
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
16
|
+
|
|
17
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
18
|
+
const __dirname = dirname(__filename);
|
|
19
|
+
|
|
20
|
+
interface PackageJson {
|
|
21
|
+
version?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Get package version
|
|
25
|
+
let packageJsonPath = join(__dirname, '../../../package.json');
|
|
26
|
+
if (!existsSync(packageJsonPath)) {
|
|
27
|
+
packageJsonPath = join(__dirname, '../../package.json');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')) as PackageJson;
|
|
31
|
+
const VERSION = packageJson.version || '0.0.0';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Start MCP server (no args mode)
|
|
35
|
+
*/
|
|
36
|
+
async function startMcpServer(): Promise<void> {
|
|
37
|
+
const server = createMcpServer();
|
|
38
|
+
const transport = new StdioServerTransport();
|
|
39
|
+
await server.connect(transport);
|
|
40
|
+
console.error('MemHub MCP Server running on stdio');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function printHelp(): void {
|
|
44
|
+
// eslint-disable-next-line no-console
|
|
45
|
+
console.log(`
|
|
46
|
+
MemHub CLI v${VERSION} - Git-friendly memory for AI agents
|
|
47
|
+
|
|
48
|
+
Usage:
|
|
49
|
+
memhub Start MCP server (default)
|
|
50
|
+
memhub init [options] Configure MemHub for your AI agent
|
|
51
|
+
memhub --help Show this help message
|
|
52
|
+
memhub --version Show version
|
|
53
|
+
|
|
54
|
+
Options:
|
|
55
|
+
-a, --agent <agent> Agent type (skip interactive selection)
|
|
56
|
+
-f, --force Update existing configuration
|
|
57
|
+
-l, --local Configure for current project (default: global)
|
|
58
|
+
|
|
59
|
+
Supported agents:
|
|
60
|
+
${AGENTS.map(a => ` ${a.id.padEnd(15)} ${a.name}`).join('\n')}
|
|
61
|
+
|
|
62
|
+
Examples:
|
|
63
|
+
memhub # Start MCP server
|
|
64
|
+
memhub init # Interactive selection (global)
|
|
65
|
+
memhub init --local # Interactive selection (project)
|
|
66
|
+
memhub init --agent cursor # Configure for Cursor (global)
|
|
67
|
+
memhub init -a claude-code -l # Configure for Claude Code (project)
|
|
68
|
+
`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function printVersion(): void {
|
|
72
|
+
// eslint-disable-next-line no-console
|
|
73
|
+
console.log(`MemHub CLI v${VERSION}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseAgent(value: string): AgentType | null {
|
|
77
|
+
const validAgents = AGENTS.map(a => a.id);
|
|
78
|
+
if (validAgents.includes(value as AgentType)) {
|
|
79
|
+
return value as AgentType;
|
|
80
|
+
}
|
|
81
|
+
console.error(`Invalid agent: ${value}`);
|
|
82
|
+
console.error(`Valid agents: ${validAgents.join(', ')}`);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function runCli(args: string[]): Promise<void> {
|
|
87
|
+
// Parse command
|
|
88
|
+
const command = args[0];
|
|
89
|
+
|
|
90
|
+
if (command === '--help' || command === '-h') {
|
|
91
|
+
printHelp();
|
|
92
|
+
process.exit(0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (command === '--version' || command === '-v') {
|
|
96
|
+
printVersion();
|
|
97
|
+
process.exit(0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (command !== 'init') {
|
|
101
|
+
p.log.error(`Unknown command: ${command}`);
|
|
102
|
+
p.log.info('Run "memhub --help" for usage information.');
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Parse init options
|
|
107
|
+
let agent: AgentType | undefined;
|
|
108
|
+
let force = false;
|
|
109
|
+
let local = false;
|
|
110
|
+
|
|
111
|
+
for (let i = 1; i < args.length; i++) {
|
|
112
|
+
const arg = args[i];
|
|
113
|
+
|
|
114
|
+
if (arg === '-a' || arg === '--agent') {
|
|
115
|
+
const value = args[++i];
|
|
116
|
+
if (!value) {
|
|
117
|
+
p.log.error('--agent requires a value');
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
const parsed = parseAgent(value);
|
|
121
|
+
if (!parsed) {
|
|
122
|
+
process.exit(1);
|
|
123
|
+
}
|
|
124
|
+
agent = parsed;
|
|
125
|
+
} else if (arg === '-f' || arg === '--force') {
|
|
126
|
+
force = true;
|
|
127
|
+
} else if (arg === '-l' || arg === '--local') {
|
|
128
|
+
local = true;
|
|
129
|
+
} else {
|
|
130
|
+
p.log.error(`Unknown option: ${arg}`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Start interactive session
|
|
136
|
+
p.intro(`MemHub v${VERSION}`);
|
|
137
|
+
|
|
138
|
+
// If no agent specified, run interactive selection
|
|
139
|
+
if (!agent) {
|
|
140
|
+
const selectedAgent = await selectAgentInteractive();
|
|
141
|
+
if (!selectedAgent) {
|
|
142
|
+
p.cancel('Operation cancelled.');
|
|
143
|
+
process.exit(0);
|
|
144
|
+
}
|
|
145
|
+
agent = selectedAgent;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Run init with spinner
|
|
149
|
+
const s = p.spinner();
|
|
150
|
+
s.start(`Configuring MemHub for ${agent}...`);
|
|
151
|
+
|
|
152
|
+
const result = initAgent({
|
|
153
|
+
agent,
|
|
154
|
+
force,
|
|
155
|
+
local,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
if (result.success) {
|
|
159
|
+
s.stop(`Configured for ${result.agent.name}`);
|
|
160
|
+
p.log.success(`MCP config: ${result.configPath}`);
|
|
161
|
+
|
|
162
|
+
if (result.instructionsUpdated) {
|
|
163
|
+
p.log.success(`Instructions: ${result.instructionsPath} (${result.instructionsReason})`);
|
|
164
|
+
} else {
|
|
165
|
+
p.log.info(`Instructions: ${result.instructionsPath} (${result.instructionsReason})`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
p.outro(`Restart your agent to start using MemHub.`);
|
|
169
|
+
} else {
|
|
170
|
+
s.stop('Configuration failed');
|
|
171
|
+
p.log.error(result.error);
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function main(): Promise<void> {
|
|
177
|
+
const args = process.argv.slice(2);
|
|
178
|
+
|
|
179
|
+
// No args: start MCP server
|
|
180
|
+
if (args.length === 0) {
|
|
181
|
+
await startMcpServer();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// With args: run CLI
|
|
186
|
+
await runCli(args);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
main().catch(error => {
|
|
190
|
+
console.error('Fatal error:', error);
|
|
191
|
+
process.exit(1);
|
|
192
|
+
});
|
package/src/cli/init.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Init command implementation
|
|
3
|
+
* Generates MCP configuration for different AI agents
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
7
|
+
import { dirname, join } from 'path';
|
|
8
|
+
import { homedir } from 'os';
|
|
9
|
+
import * as p from '@clack/prompts';
|
|
10
|
+
import { AGENTS, type AgentType, type AgentConfig, getAgentById } from './types.js';
|
|
11
|
+
import { getConfigGenerator } from './agents/index.js';
|
|
12
|
+
import { updateInstructionsContent } from './instructions.js';
|
|
13
|
+
import { parse as parseToml, stringify as stringifyToml } from 'smol-toml';
|
|
14
|
+
|
|
15
|
+
export interface InitOptions {
|
|
16
|
+
readonly agent: AgentType;
|
|
17
|
+
/** @deprecated Use local instead */
|
|
18
|
+
readonly projectPath?: string;
|
|
19
|
+
readonly force?: boolean;
|
|
20
|
+
readonly local?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface InitResult {
|
|
24
|
+
readonly success: true;
|
|
25
|
+
readonly configPath: string;
|
|
26
|
+
readonly instructionsPath: string;
|
|
27
|
+
readonly instructionsUpdated: boolean;
|
|
28
|
+
readonly instructionsReason: string;
|
|
29
|
+
readonly agent: AgentConfig;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface InitError {
|
|
33
|
+
readonly success: false;
|
|
34
|
+
readonly error: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type InitOutcome = InitResult | InitError;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parse config file content based on format
|
|
41
|
+
*/
|
|
42
|
+
function parseConfig(
|
|
43
|
+
content: string,
|
|
44
|
+
format: 'json' | 'markdown' | 'toml'
|
|
45
|
+
): Record<string, unknown> {
|
|
46
|
+
if (format === 'toml') {
|
|
47
|
+
return parseToml(content) as Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
return JSON.parse(content) as Record<string, unknown>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Stringify config to string based on format
|
|
54
|
+
*/
|
|
55
|
+
function stringifyConfig(
|
|
56
|
+
config: Record<string, unknown>,
|
|
57
|
+
format: 'json' | 'markdown' | 'toml'
|
|
58
|
+
): string {
|
|
59
|
+
if (format === 'toml') {
|
|
60
|
+
return stringifyToml(config);
|
|
61
|
+
}
|
|
62
|
+
return JSON.stringify(config, null, 2);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
66
|
+
return typeof value === 'object' && value !== null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getTargetMcpKey(config: Record<string, unknown>): 'mcpServers' | 'mcp_servers' {
|
|
70
|
+
return isRecord(config.mcp_servers) ? 'mcp_servers' : 'mcpServers';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Merge memhub config into existing config
|
|
75
|
+
* Preserves all existing servers, adds/updates memhub
|
|
76
|
+
*/
|
|
77
|
+
function mergeMcpConfig(
|
|
78
|
+
existing: Record<string, unknown>,
|
|
79
|
+
newConfig: Record<string, unknown>
|
|
80
|
+
): Record<string, unknown> {
|
|
81
|
+
const result = { ...existing };
|
|
82
|
+
const targetKey = getTargetMcpKey(newConfig);
|
|
83
|
+
const existingServers = isRecord(result[targetKey]) ? result[targetKey] : {};
|
|
84
|
+
const newServers = (newConfig[targetKey] as Record<string, unknown>) ?? {};
|
|
85
|
+
result[targetKey] = { ...existingServers, ...newServers };
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Run interactive agent selection using clack
|
|
92
|
+
*/
|
|
93
|
+
export async function selectAgentInteractive(): Promise<AgentType | null> {
|
|
94
|
+
const selection = await p.select({
|
|
95
|
+
message: 'Select your AI agent',
|
|
96
|
+
options: AGENTS.map(agent => ({
|
|
97
|
+
value: agent.id,
|
|
98
|
+
label: agent.name,
|
|
99
|
+
hint: agent.description,
|
|
100
|
+
})),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (p.isCancel(selection)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return selection;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Generate and write MCP configuration for the specified agent
|
|
112
|
+
*/
|
|
113
|
+
export function initAgent(options: InitOptions): InitOutcome {
|
|
114
|
+
const { agent, force = false, local = false, projectPath } = options;
|
|
115
|
+
|
|
116
|
+
const agentConfig = getAgentById(agent);
|
|
117
|
+
if (!agentConfig) {
|
|
118
|
+
return {
|
|
119
|
+
success: false,
|
|
120
|
+
error: `Unknown agent: ${agent}. Valid agents: ${AGENTS.map(a => a.id).join(', ')}`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Determine base path and file paths based on local flag
|
|
125
|
+
const basePath = projectPath ?? (local ? process.cwd() : homedir());
|
|
126
|
+
const configFile = local ? agentConfig.configFile : agentConfig.globalConfigFile;
|
|
127
|
+
const instructionsFile = local
|
|
128
|
+
? agentConfig.instructionsFile
|
|
129
|
+
: agentConfig.globalInstructionsFile;
|
|
130
|
+
|
|
131
|
+
const configPath = join(basePath, configFile);
|
|
132
|
+
const configDir = dirname(configPath);
|
|
133
|
+
const instructionsPath = join(basePath, instructionsFile);
|
|
134
|
+
const instructionsDir = dirname(instructionsPath);
|
|
135
|
+
|
|
136
|
+
// Generate MCP configuration
|
|
137
|
+
const generator = getConfigGenerator(agent);
|
|
138
|
+
const newConfig = generator(basePath);
|
|
139
|
+
|
|
140
|
+
let finalConfig: Record<string, unknown>;
|
|
141
|
+
|
|
142
|
+
// Check if config already exists
|
|
143
|
+
const configFormat = agentConfig.configFormat;
|
|
144
|
+
if (existsSync(configPath)) {
|
|
145
|
+
if (force) {
|
|
146
|
+
// Force: still merge, but this updates memhub entry
|
|
147
|
+
try {
|
|
148
|
+
const existingContent = readFileSync(configPath, 'utf-8');
|
|
149
|
+
const existingConfig = parseConfig(existingContent, configFormat);
|
|
150
|
+
finalConfig = mergeMcpConfig(existingConfig, newConfig);
|
|
151
|
+
} catch {
|
|
152
|
+
// Invalid config, use new config
|
|
153
|
+
finalConfig = newConfig;
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
// No force: check if memhub already exists
|
|
157
|
+
try {
|
|
158
|
+
const existingContent = readFileSync(configPath, 'utf-8');
|
|
159
|
+
const existingConfig = parseConfig(existingContent, configFormat);
|
|
160
|
+
const targetKey = getTargetMcpKey(newConfig);
|
|
161
|
+
const servers = existingConfig[targetKey] as Record<string, unknown> | undefined;
|
|
162
|
+
|
|
163
|
+
if (servers && 'memhub' in servers) {
|
|
164
|
+
return {
|
|
165
|
+
success: false,
|
|
166
|
+
error: `MemHub is already configured in ${configFile}. Use --force to update.`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Merge with existing config
|
|
171
|
+
finalConfig = mergeMcpConfig(existingConfig, newConfig);
|
|
172
|
+
} catch {
|
|
173
|
+
return {
|
|
174
|
+
success: false,
|
|
175
|
+
error: `Failed to parse existing config at ${configFile}. Use --force to overwrite.`,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
finalConfig = newConfig;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Ensure directories exist
|
|
184
|
+
if (!existsSync(configDir)) {
|
|
185
|
+
mkdirSync(configDir, { recursive: true });
|
|
186
|
+
}
|
|
187
|
+
if (!existsSync(instructionsDir)) {
|
|
188
|
+
mkdirSync(instructionsDir, { recursive: true });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Write MCP configuration
|
|
192
|
+
writeFileSync(configPath, stringifyConfig(finalConfig, configFormat), 'utf-8');
|
|
193
|
+
|
|
194
|
+
// Handle instructions file
|
|
195
|
+
let existingContent = '';
|
|
196
|
+
if (existsSync(instructionsPath)) {
|
|
197
|
+
existingContent = readFileSync(instructionsPath, 'utf-8');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const {
|
|
201
|
+
content: updatedContent,
|
|
202
|
+
updated: instructionsUpdated,
|
|
203
|
+
reason: instructionsReason,
|
|
204
|
+
} = updateInstructionsContent(existingContent, agentConfig);
|
|
205
|
+
|
|
206
|
+
if (instructionsUpdated || !existsSync(instructionsPath)) {
|
|
207
|
+
writeFileSync(instructionsPath, updatedContent, 'utf-8');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
success: true,
|
|
212
|
+
configPath: configFile,
|
|
213
|
+
instructionsPath: instructionsFile,
|
|
214
|
+
instructionsUpdated,
|
|
215
|
+
instructionsReason,
|
|
216
|
+
agent: agentConfig,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemHub instructions generator
|
|
3
|
+
* Generates agent-specific instruction content with versioned tags
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { AgentConfig } from './types.js';
|
|
7
|
+
|
|
8
|
+
const MEMHUB_VERSION = '0.2.3';
|
|
9
|
+
|
|
10
|
+
const START_TAG = `<!-- MEMHUB:v${MEMHUB_VERSION}:START -->`;
|
|
11
|
+
const END_TAG = '<!-- MEMHUB:END -->';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Extract MemHub version from content if present
|
|
15
|
+
*/
|
|
16
|
+
export function extractMemHubVersion(content: string): string | null {
|
|
17
|
+
const match = content.match(/<!-- MEMHUB:v([\d.]+):START -->/);
|
|
18
|
+
return match ? match[1] : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Check if MemHub instructions exist and need update
|
|
23
|
+
*/
|
|
24
|
+
export function needsUpdate(content: string): boolean {
|
|
25
|
+
const version = extractMemHubVersion(content);
|
|
26
|
+
if (!version) return true;
|
|
27
|
+
return version !== MEMHUB_VERSION;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Generate MemHub instructions content
|
|
32
|
+
*/
|
|
33
|
+
export function generateInstructionsContent(agent: AgentConfig): string {
|
|
34
|
+
const isMarkdown = agent.instructionsFormat === 'markdown';
|
|
35
|
+
|
|
36
|
+
if (isMarkdown) {
|
|
37
|
+
return `${START_TAG}
|
|
38
|
+
## MemHub - AI Memory System
|
|
39
|
+
|
|
40
|
+
MemHub is a Git-friendly memory system for storing decisions, preferences, and reusable knowledge.
|
|
41
|
+
|
|
42
|
+
### When to Call memory_load
|
|
43
|
+
|
|
44
|
+
**Proactively call** memory_load in these scenarios:
|
|
45
|
+
|
|
46
|
+
- Starting a new conversation or task
|
|
47
|
+
- User mentions "before", "remember", "last time" keywords
|
|
48
|
+
- Uncertain about user preferences or constraints
|
|
49
|
+
- Need project context (tech stack, conventions, architecture)
|
|
50
|
+
|
|
51
|
+
### When to Call memory_update
|
|
52
|
+
|
|
53
|
+
**Proactively store** memories in these scenarios:
|
|
54
|
+
|
|
55
|
+
- User explicitly expresses a preference ("I prefer functional components")
|
|
56
|
+
- User makes a decision with reasoning ("Using PostgreSQL because...")
|
|
57
|
+
- Discover important project context (tech stack, constraints, patterns)
|
|
58
|
+
- User corrects your assumptions ("Actually, we don't use Redux")
|
|
59
|
+
|
|
60
|
+
### Usage Principles
|
|
61
|
+
|
|
62
|
+
1. **Load on demand** - Call memory_load at task start to get context
|
|
63
|
+
2. **Store timely** - Call memory_update when learning valuable information
|
|
64
|
+
3. **Query precisely** - Use tags to filter relevant memories
|
|
65
|
+
4. **Describe concisely** - Be specific in content, helpful in title
|
|
66
|
+
|
|
67
|
+
### Memory Types
|
|
68
|
+
|
|
69
|
+
| entryType | Purpose |
|
|
70
|
+
|-----------|---------|
|
|
71
|
+
| preference | User preferences (coding style, framework choices) |
|
|
72
|
+
| decision | Architecture decisions, technology choices |
|
|
73
|
+
| context | Project context (team, processes, constraints) |
|
|
74
|
+
| fact | Learned facts, important notes |
|
|
75
|
+
|
|
76
|
+
${END_TAG}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Plain text format (for .cursorrules, .clinerules, etc.)
|
|
80
|
+
return `${START_TAG}
|
|
81
|
+
# MemHub - AI Memory System
|
|
82
|
+
|
|
83
|
+
MemHub is a Git-friendly memory system for storing decisions, preferences, and reusable knowledge.
|
|
84
|
+
|
|
85
|
+
## When to Call memory_load
|
|
86
|
+
|
|
87
|
+
- Starting a new conversation or task
|
|
88
|
+
- User mentions "before", "remember", "last time" keywords
|
|
89
|
+
- Uncertain about user preferences or constraints
|
|
90
|
+
- Need project context (tech stack, conventions, architecture)
|
|
91
|
+
|
|
92
|
+
## When to Call memory_update
|
|
93
|
+
|
|
94
|
+
- User explicitly expresses a preference
|
|
95
|
+
- User makes a decision with reasoning
|
|
96
|
+
- Discover important project context
|
|
97
|
+
- User corrects your assumptions
|
|
98
|
+
|
|
99
|
+
## Memory Types
|
|
100
|
+
|
|
101
|
+
- preference: User preferences
|
|
102
|
+
- decision: Architecture decisions
|
|
103
|
+
- context: Project context
|
|
104
|
+
- fact: Learned facts
|
|
105
|
+
|
|
106
|
+
## Principle
|
|
107
|
+
|
|
108
|
+
Will my future self benefit from knowing this? If yes, store it.
|
|
109
|
+
|
|
110
|
+
${END_TAG}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Update instructions file content
|
|
115
|
+
* - If no MemHub section: prepend new instructions
|
|
116
|
+
* - If MemHub section exists and outdated: replace with new version
|
|
117
|
+
* - If MemHub section exists and current: no change
|
|
118
|
+
*/
|
|
119
|
+
export function updateInstructionsContent(
|
|
120
|
+
existingContent: string,
|
|
121
|
+
agent: AgentConfig
|
|
122
|
+
): { content: string; updated: boolean; reason: string } {
|
|
123
|
+
const trimmedContent = existingContent.trim();
|
|
124
|
+
const hasMemHub = trimmedContent.includes('<!-- MEMHUB:');
|
|
125
|
+
|
|
126
|
+
if (!hasMemHub) {
|
|
127
|
+
// No MemHub section: prepend
|
|
128
|
+
const newInstructions = generateInstructionsContent(agent);
|
|
129
|
+
return {
|
|
130
|
+
content: `${newInstructions}\n\n${trimmedContent}`,
|
|
131
|
+
updated: true,
|
|
132
|
+
reason: 'MemHub instructions added',
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const currentVersion = extractMemHubVersion(trimmedContent);
|
|
137
|
+
if (currentVersion === MEMHUB_VERSION) {
|
|
138
|
+
// Already up to date
|
|
139
|
+
return {
|
|
140
|
+
content: existingContent,
|
|
141
|
+
updated: false,
|
|
142
|
+
reason: 'Already up to date',
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Need to update: replace old section with new
|
|
147
|
+
const newInstructions = generateInstructionsContent(agent);
|
|
148
|
+
const pattern = /<!-- MEMHUB:v[\d.]+:START -->[\s\S]*?<!-- MEMHUB:END -->/;
|
|
149
|
+
const updatedContent = trimmedContent.replace(pattern, newInstructions);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
content: updatedContent,
|
|
153
|
+
updated: true,
|
|
154
|
+
reason: `Updated from v${currentVersion} to v${MEMHUB_VERSION}`,
|
|
155
|
+
};
|
|
156
|
+
}
|