Repository navigation
feat(cli): native hooks adapter for Droid (Factory.ai) #1130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rohitg00
merged 5 commits into
rohitg00:main
from
berthojoris:feature/droid-native-hooks
Aug 2, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dea5373
feat(cli): add native hooks adapter for Droid (Factory.ai)
berthojoris 1381178
docs: changelog entry + regenerate agentmemory-agents skill reference…
berthojoris d624e14
fix(cli): surface skipped hook results in dry-run branch, tighten Dro…
berthojoris 072dab9
fix(cli): use Droid Create tool in PreToolUse matcher
berthojoris 7fa1960
docs: drop changelog entry and trim Droid README row
berthojoris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| { | ||
| "hooks": { | ||
| "SessionStart": [ | ||
| { | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/session-start.mjs\"" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "UserPromptSubmit": [ | ||
| { | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/prompt-submit.mjs\"" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "PreToolUse": [ | ||
| { | ||
| "matcher": "Edit|Create|Read|Glob|Grep", | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/pre-tool-use.mjs\"" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "PostToolUse": [ | ||
| { | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/post-tool-use.mjs\"" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "SessionEnd": [ | ||
| { | ||
| "hooks": [ | ||
| { | ||
| "type": "command", | ||
| "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/session-end.mjs\"" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,91 @@ | ||
| import { existsSync, mkdirSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import * as p from "@clack/prompts"; | ||
| import { createJsonMcpAdapter } from "./json-mcp-adapter.js"; | ||
| import type { ConnectOptions, ConnectResult } from "./types.js"; | ||
| import { | ||
| buildMergedHooks, | ||
| findPluginRoot, | ||
| type HookManifest, | ||
| } from "./codex-hooks.js"; | ||
| import { backupFile, logBackup, logInstalled, readJsonSafe, writeJsonAtomic } from "./util.js"; | ||
|
|
||
| // Factory.ai's Droid CLI stores MCP server config at ~/.factory/mcp.json | ||
| // with the canonical `mcpServers` wrapper. Project-scoped overrides live | ||
| // at <repo>/.factory/mcp.json. Each entry adds an optional `type` field | ||
| // ("stdio" | "http") and `disabled` boolean — agentmemory's stdio block | ||
| // works against the same shape without needing the explicit type tag. | ||
| // Source: docs.factory.ai/cli/configuration/mcp | ||
| // | ||
| // Droid also ships a native, first-party hooks system (SessionStart, | ||
| // SessionEnd, UserPromptSubmit, PreToolUse, PostToolUse) configured via | ||
| // `~/.factory/hooks.json`, in the same `{ hooks: { <event>: [{ matcher?, | ||
| // hooks: [{ type, command }] }] } }` shape Claude Code and Codex use — so | ||
| // `--with-hooks` reuses `buildMergedHooks` from codex-hooks.ts with the | ||
| // bundled `hooks.droid.json` manifest instead of writing a new merge | ||
| // strategy. Source: docs.factory.ai/cli/configuration/hooks-guide | ||
| const DROID_DIR = join(homedir(), ".factory"); | ||
| const DROID_HOOKS = join(DROID_DIR, "hooks.json"); | ||
|
|
||
| export const adapter = createJsonMcpAdapter({ | ||
| name: "droid", | ||
| displayName: "Droid (Factory.ai)", | ||
| detectDir: join(homedir(), ".factory"), | ||
| configPath: join(homedir(), ".factory", "mcp.json"), | ||
| detectDir: DROID_DIR, | ||
| configPath: join(DROID_DIR, "mcp.json"), | ||
| docs: "https://github.com/rohitg00/agentmemory#other-agents", | ||
| protocolNote: | ||
| "→ Using MCP via ~/.factory/mcp.json. The `/mcp` slash command inside droid lists configured servers.", | ||
| "→ Using MCP via ~/.factory/mcp.json. The `/mcp` slash command inside droid lists configured servers. Pass --with-hooks to also install the native ~/.factory/hooks.json auto-capture hooks.", | ||
| // Droid requires `type` per the documented schema. stdio for npx-spawned shim. | ||
| extraEntryFields: { type: "stdio" }, | ||
| installHooks: installDroidHooks, | ||
| }); | ||
|
|
||
| /** | ||
| * Merge the bundled `plugin/hooks/hooks.droid.json` into | ||
| * `~/.factory/hooks.json`. Same idempotent merge/strip-by-script-path | ||
| * behavior as the Codex and Claude Code hook installers: re-running keeps | ||
| * user-authored hook entries and only replaces the ones agentmemory owns. | ||
| */ | ||
| function installDroidHooks(opts: ConnectOptions): ConnectResult { | ||
| let pluginRoot: string; | ||
| try { | ||
| pluginRoot = findPluginRoot(); | ||
| } catch (err) { | ||
| return { | ||
| kind: "skipped", | ||
| reason: err instanceof Error ? err.message : String(err), | ||
| }; | ||
| } | ||
|
|
||
| const existing = readJsonSafe<HookManifest>(DROID_HOOKS); | ||
| const merged = buildMergedHooks(existing, pluginRoot, "hooks.droid.json"); | ||
|
|
||
| if (opts.dryRun) { | ||
| p.log.info( | ||
| `[dry-run] Would ${existing ? "merge" : "create"} ${DROID_HOOKS} with ${Object.keys(merged.hooks).length} event(s)`, | ||
| ); | ||
| return { kind: "installed", mutatedPath: DROID_HOOKS }; | ||
| } | ||
|
|
||
| let backupPath: string | undefined; | ||
| if (existsSync(DROID_HOOKS)) { | ||
| backupPath = backupFile(DROID_HOOKS, "droid-hooks", "json"); | ||
| logBackup(backupPath); | ||
| } else { | ||
| mkdirSync(DROID_DIR, { recursive: true }); | ||
| } | ||
|
|
||
| writeJsonAtomic(DROID_HOOKS, merged); | ||
|
|
||
| logInstalled("Droid hooks", DROID_HOOKS); | ||
| p.log.info( | ||
| "User-scope hooks reference absolute paths under the bundled plugin/ dir. Re-run `agentmemory connect droid --with-hooks` after upgrading agentmemory to refresh them.", | ||
| ); | ||
|
|
||
| return { | ||
| kind: "installed", | ||
| mutatedPath: DROID_HOOKS, | ||
| ...(backupPath !== undefined && { backupPath }), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.