diff --git a/CHANGELOG.md b/CHANGELOG.md index 54ef5f5aa..546075cd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### New Features +- Factory Droids (the `droid` CLI) is now supported by `codegraph install`, detected and configured out of the box so the Droid agent can drive the knowledge graph like the other agents. - `codegraph status --json` now also reports the running CLI `version`, the index directory (`indexPath`), and a `lastIndexed` timestamp (ISO-8601, or null when nothing's indexed yet), so CI and scripts can pin the CLI version and check index freshness from a single command. A matching `CodeGraph.getLastIndexedAt()` library method exposes the same freshness check without shelling out. Thanks @12122J and @eddieran. (#329) ### Fixes diff --git a/__tests__/installer-targets.test.ts b/__tests__/installer-targets.test.ts index 27fcbd6e8..2b31ad9a3 100644 --- a/__tests__/installer-targets.test.ts +++ b/__tests__/installer-targets.test.ts @@ -494,6 +494,79 @@ describe('Installer targets — partial-state idempotency', () => { expect(paths.some((p) => p.endsWith('/.kiro/steering/codegraph.md'))).toBe(false); }); + it('factory: install writes ~/.factory/mcp.json (mcpServers.codegraph)', () => { + const factory = getTarget('factory')!; + const result = factory.install('global', { autoAllow: true }); + const mcp = path.join(tmpHome, '.factory', 'mcp.json'); + expect(result.files.some((f) => f.path === mcp)).toBe(true); + expect(fs.existsSync(mcp)).toBe(true); + + const cfg = JSON.parse(fs.readFileSync(mcp, 'utf-8')); + expect(cfg.mcpServers.codegraph).toEqual({ + type: 'stdio', + command: 'codegraph', + args: ['serve', '--mcp'], + }); + }); + + it('factory: install preserves a pre-existing sibling MCP server in mcp.json', () => { + const factory = getTarget('factory')!; + const mcp = path.join(tmpHome, '.factory', 'mcp.json'); + fs.mkdirSync(path.dirname(mcp), { recursive: true }); + fs.writeFileSync( + mcp, + JSON.stringify({ + mcpServers: { sibling: { command: 'uvx', args: ['other-server'] } }, + }, null, 2) + '\n' + ); + + factory.install('global', { autoAllow: true }); + + const cfg = JSON.parse(fs.readFileSync(mcp, 'utf-8')); + expect(cfg.mcpServers.sibling).toEqual({ command: 'uvx', args: ['other-server'] }); + expect(cfg.mcpServers.codegraph).toBeDefined(); + }); + + it('factory: uninstall strips codegraph but leaves sibling MCP servers intact', () => { + const factory = getTarget('factory')!; + const mcp = path.join(tmpHome, '.factory', 'mcp.json'); + fs.mkdirSync(path.dirname(mcp), { recursive: true }); + fs.writeFileSync( + mcp, + JSON.stringify({ + mcpServers: { sibling: { command: 'uvx', args: ['other-server'] } }, + }, null, 2) + '\n' + ); + + factory.install('global', { autoAllow: true }); + const result = factory.uninstall('global'); + + const cfg = JSON.parse(fs.readFileSync(mcp, 'utf-8')); + expect(cfg.mcpServers.sibling).toEqual({ command: 'uvx', args: ['other-server'] }); + expect(cfg.mcpServers.codegraph).toBeUndefined(); + expect(result.files).toContainEqual({ path: mcp, action: 'updated' }); + }); + + it('factory: uninstall deletes the mcp.json file when no other content remains', () => { + const factory = getTarget('factory')!; + const mcp = path.join(tmpHome, '.factory', 'mcp.json'); + + factory.install('global', { autoAllow: true }); + expect(fs.existsSync(mcp)).toBe(true); + + const result = factory.uninstall('global'); + expect(fs.existsSync(mcp)).toBe(false); + expect(result.files).toContainEqual({ path: mcp, action: 'removed' }); + }); + + + it('factory: local install writes ./.factory/mcp.json', () => { + const factory = getTarget('factory')!; + const result = factory.install('local', { autoAllow: true }); + const paths = result.files.map((f) => f.path.replace(/\\/g, '/')); + expect(paths.some((p) => p.endsWith('/.factory/mcp.json'))).toBe(true); + }); + it('antigravity: install writes to LEGACY ~/.gemini/antigravity/mcp_config.json when no migration marker', () => { const antigravity = getTarget('antigravity')!; antigravity.install('global', { autoAllow: true }); @@ -1098,6 +1171,7 @@ describe('Installer targets — registry', () => { expect(getTarget('gemini')?.id).toBe('gemini'); expect(getTarget('antigravity')?.id).toBe('antigravity'); expect(getTarget('kiro')?.id).toBe('kiro'); + expect(getTarget('factory')?.id).toBe('factory'); expect(getTarget('not-a-real-target')).toBeUndefined(); }); diff --git a/package-lock.json b/package-lock.json index 031b3f463..81a89c722 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1431,7 +1431,6 @@ "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "esbuild": "^0.21.3", "postcss": "^8.4.43", diff --git a/src/installer/index.ts b/src/installer/index.ts index edd48ecaf..97ee95c7e 100644 --- a/src/installer/index.ts +++ b/src/installer/index.ts @@ -3,7 +3,7 @@ * * Multi-target: writes MCP server config + instructions for the * agents the user picks (Claude Code, Cursor, Codex CLI, opencode, - * Hermes Agent, Gemini CLI, Antigravity IDE). + * Hermes Agent, Gemini CLI, Antigravity IDE, Factory Droids). * Defaults to the Claude-only behavior for backwards compatibility * when no targets are explicitly chosen and nothing else is detected. * @@ -317,8 +317,8 @@ export async function runUninstaller(opts: RunUninstallerOptions): Promise const sel = await clack.select({ message: 'Remove CodeGraph from all your projects, or just this one?', options: [ - { value: 'global' as const, label: 'All projects (global)', hint: '~/.claude, ~/.cursor, ~/.codex, ~/.config/opencode, ~/.hermes, ~/.gemini, ~/.kiro' }, - { value: 'local' as const, label: 'Just this project (local)', hint: './.claude, ./.cursor, ./opencode.jsonc, ./.gemini, ./.kiro' }, + { value: 'global' as const, label: 'All projects (global)', hint: '~/.claude, ~/.cursor, ~/.codex, ~/.config/opencode, ~/.hermes, ~/.gemini, ~/.kiro, ~/.factory' }, + { value: 'local' as const, label: 'Just this project (local)', hint: './.claude, ./.cursor, ./opencode.jsonc, ./.gemini, ./.kiro, ./.factory' }, ], initialValue: 'global' as const, }); diff --git a/src/installer/targets/factory.ts b/src/installer/targets/factory.ts new file mode 100644 index 000000000..34b5c395f --- /dev/null +++ b/src/installer/targets/factory.ts @@ -0,0 +1,135 @@ +/** + * Factory Droids target. Writes: + * + * - MCP server entry to `~/.factory/mcp.json` (global) or + * `./.factory/mcp.json` (local). Standard `mcpServers.codegraph` + * shape, same as Claude / Cursor / Gemini. + * + * No permissions concept — Factory Droids gates tool invocations through its own + * UI prompts rather than an external allowlist. `autoAllow` is silently + * ignored. + * + * Paths are identical on macOS / Linux / Windows because Factory Droids resolves + * its config root from `os.homedir()` on all three (Windows `~` → + * `%USERPROFILE%\.factory`). + * + * Docs: https://app.factory.ai + */ + +import * as fs from 'fs'; +import * as path from 'path'; +import * as os from 'os'; +import { + AgentTarget, + DetectionResult, + InstallOptions, + Location, + WriteResult, +} from './types'; +import { + getMcpServerConfig, + jsonDeepEqual, + readJsonFile, + writeJsonFile, +} from './shared'; + +function configDir(loc: Location): string { + return loc === 'global' + ? path.join(os.homedir(), '.factory') + : path.join(process.cwd(), '.factory'); +} + +function mcpJsonPath(loc: Location): string { + return path.join(configDir(loc), 'mcp.json'); +} + +class FactoryDroidsTarget implements AgentTarget { + readonly id = 'factory' as const; + readonly displayName = 'Factory Droids'; + readonly docsUrl = 'https://app.factory.ai'; + + supportsLocation(_loc: Location): boolean { + return true; + } + + detect(loc: Location): DetectionResult { + const file = mcpJsonPath(loc); + const config = readJsonFile(file); + const alreadyConfigured = !!config.mcpServers?.codegraph; + const installed = loc === 'global' + ? fs.existsSync(configDir('global')) || fs.existsSync(file) + : fs.existsSync(file) || fs.existsSync(configDir('local')); + return { installed, alreadyConfigured, configPath: file }; + } + + install(loc: Location, _opts: InstallOptions): WriteResult { + const files: WriteResult['files'] = []; + files.push(writeMcpEntry(loc)); + + return { + files, + notes: [ + 'Restart the Droid CLI for MCP changes to take effect.', + ], + }; + } + + uninstall(loc: Location): WriteResult { + const files: WriteResult['files'] = []; + + const file = mcpJsonPath(loc); + const config = readJsonFile(file); + if (config.mcpServers?.codegraph) { + delete config.mcpServers.codegraph; + if (Object.keys(config.mcpServers).length === 0) { + delete config.mcpServers; + } + + if (Object.keys(config).length === 0) { + if (fs.existsSync(file)) { + fs.unlinkSync(file); + } + files.push({ path: file, action: 'removed' }); + } else { + writeJsonFile(file, config); + files.push({ path: file, action: 'updated' }); + } + } else { + files.push({ path: file, action: 'not-found' }); + } + + return { files }; + } + + printConfig(loc: Location): string { + const target = mcpJsonPath(loc); + const snippet = JSON.stringify({ mcpServers: { codegraph: getMcpServerConfig() } }, null, 2); + return `# Add to ${target}\n\n${snippet}\n`; + } + + describePaths(loc: Location): string[] { + return [mcpJsonPath(loc)]; + } +} + +function writeMcpEntry(loc: Location): WriteResult['files'][number] { + const file = mcpJsonPath(loc); + const dir = path.dirname(file); + if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); + + const existing = readJsonFile(file); + const before = existing.mcpServers?.codegraph; + const after = getMcpServerConfig(); + + if (jsonDeepEqual(before, after)) { + return { path: file, action: 'unchanged' }; + } + const action: 'created' | 'updated' = + before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created'); + if (!existing.mcpServers) existing.mcpServers = {}; + existing.mcpServers.codegraph = after; + writeJsonFile(file, existing); + return { path: file, action }; +} + +export const factoryTarget: AgentTarget = new FactoryDroidsTarget(); diff --git a/src/installer/targets/registry.ts b/src/installer/targets/registry.ts index 5e929d468..b82122596 100644 --- a/src/installer/targets/registry.ts +++ b/src/installer/targets/registry.ts @@ -16,6 +16,7 @@ import { hermesTarget } from './hermes'; import { geminiTarget } from './gemini'; import { antigravityTarget } from './antigravity'; import { kiroTarget } from './kiro'; +import { factoryTarget } from './factory'; export const ALL_TARGETS: readonly AgentTarget[] = Object.freeze([ claudeTarget, @@ -26,6 +27,7 @@ export const ALL_TARGETS: readonly AgentTarget[] = Object.freeze([ geminiTarget, antigravityTarget, kiroTarget, + factoryTarget, ]); export function getTarget(id: string): AgentTarget | undefined { diff --git a/src/installer/targets/types.ts b/src/installer/targets/types.ts index 4b3267e97..ca3866d4a 100644 --- a/src/installer/targets/types.ts +++ b/src/installer/targets/types.ts @@ -19,7 +19,7 @@ export type Location = 'global' | 'local'; * lookup. New targets add a value here when they're added to the * registry. Keep these short and lowercase. */ -export type TargetId = 'claude' | 'cursor' | 'codex' | 'opencode' | 'hermes' | 'gemini' | 'antigravity' | 'kiro'; +export type TargetId = 'claude' | 'cursor' | 'codex' | 'opencode' | 'hermes' | 'gemini' | 'antigravity' | 'kiro' | 'factory'; /** * Result of `target.detect(location)`.