milaidy 1.0.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.
Files changed (253) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +538 -0
  3. package/dist/argv-CfSowvEA.js +63 -0
  4. package/dist/config-B-mboG4v.js +4 -0
  5. package/dist/eliza-CPJjgw-e.js +1491 -0
  6. package/dist/eliza.js +2192 -0
  7. package/dist/entry.js +232 -0
  8. package/dist/index.js +209 -0
  9. package/dist/links-BFKlWqSe.js +15 -0
  10. package/dist/paths-D_yh1DEJ.js +69 -0
  11. package/dist/plugins-cli-B7kSre2c.js +134 -0
  12. package/dist/program-6KwWwKKh.js +510 -0
  13. package/dist/register.agents-CPVmSjMG.js +17 -0
  14. package/dist/register.browser-B2ooXxNx.js +15 -0
  15. package/dist/register.channels-CMYQ6K6Y.js +42 -0
  16. package/dist/register.cron-D91lY1_Y.js +9 -0
  17. package/dist/register.devices-rU5I5L_y.js +13 -0
  18. package/dist/register.gateway-82SLAvw3.js +22 -0
  19. package/dist/register.hooks-B_XTBEkt.js +9 -0
  20. package/dist/register.logs-BgEGcPd8.js +10 -0
  21. package/dist/register.models-BJt9eVgZ.js +26 -0
  22. package/dist/register.nodes-B5xY1s8a.js +9 -0
  23. package/dist/register.skills-SFQqYIhg.js +10 -0
  24. package/dist/register.subclis-uF_AsbWR.js +187 -0
  25. package/dist/run-main-XODklzS-.js +56 -0
  26. package/dist/theme-DBvtuGeq.js +36 -0
  27. package/dist/utils-C1AUpp_V.js +42 -0
  28. package/dist/version-Cpn3yr5D.js +26 -0
  29. package/dist/workspace-Co3Wul2D.js +206 -0
  30. package/dist/workspace-DCA6MNVK.js +350 -0
  31. package/docs/.i18n/README.md +31 -0
  32. package/docs/.i18n/glossary.zh-CN.json +210 -0
  33. package/docs/.i18n/zh-CN.tm.jsonl +1329 -0
  34. package/docs/CNAME +1 -0
  35. package/docs/automation/cron-jobs.md +468 -0
  36. package/docs/automation/cron-vs-heartbeat.md +254 -0
  37. package/docs/automation/gmail-pubsub.md +256 -0
  38. package/docs/automation/poll.md +69 -0
  39. package/docs/automation/webhook.md +163 -0
  40. package/docs/bedrock.md +176 -0
  41. package/docs/brave-search.md +41 -0
  42. package/docs/broadcast-groups.md +442 -0
  43. package/docs/cli/acp.md +170 -0
  44. package/docs/cli/agent.md +24 -0
  45. package/docs/cli/agents.md +75 -0
  46. package/docs/cli/approvals.md +50 -0
  47. package/docs/cli/browser.md +107 -0
  48. package/docs/cli/channels.md +79 -0
  49. package/docs/cli/config.md +50 -0
  50. package/docs/cli/configure.md +33 -0
  51. package/docs/cli/cron.md +42 -0
  52. package/docs/cli/dashboard.md +16 -0
  53. package/docs/cli/devices.md +67 -0
  54. package/docs/cli/directory.md +63 -0
  55. package/docs/cli/dns.md +23 -0
  56. package/docs/cli/docs.md +15 -0
  57. package/docs/cli/doctor.md +41 -0
  58. package/docs/cli/gateway.md +199 -0
  59. package/docs/cli/health.md +21 -0
  60. package/docs/cli/hooks.md +291 -0
  61. package/docs/cli/index.md +1029 -0
  62. package/docs/cli/logs.md +24 -0
  63. package/docs/cli/memory.md +45 -0
  64. package/docs/cli/message.md +239 -0
  65. package/docs/cli/models.md +79 -0
  66. package/docs/cli/node.md +112 -0
  67. package/docs/cli/nodes.md +73 -0
  68. package/docs/cli/onboard.md +29 -0
  69. package/docs/cli/pairing.md +21 -0
  70. package/docs/cli/plugins.md +62 -0
  71. package/docs/cli/reset.md +17 -0
  72. package/docs/cli/sandbox.md +152 -0
  73. package/docs/cli/security.md +26 -0
  74. package/docs/cli/sessions.md +16 -0
  75. package/docs/cli/setup.md +29 -0
  76. package/docs/cli/skills.md +26 -0
  77. package/docs/cli/status.md +26 -0
  78. package/docs/cli/system.md +60 -0
  79. package/docs/cli/tui.md +23 -0
  80. package/docs/cli/uninstall.md +17 -0
  81. package/docs/cli/update.md +98 -0
  82. package/docs/cli/voicecall.md +34 -0
  83. package/docs/cli/webhooks.md +25 -0
  84. package/docs/concepts/agent-loop.md +146 -0
  85. package/docs/concepts/agent-workspace.md +229 -0
  86. package/docs/concepts/agent.md +122 -0
  87. package/docs/concepts/architecture.md +129 -0
  88. package/docs/concepts/channel-routing.md +114 -0
  89. package/docs/concepts/compaction.md +61 -0
  90. package/docs/concepts/context.md +159 -0
  91. package/docs/concepts/features.md +53 -0
  92. package/docs/concepts/group-messages.md +84 -0
  93. package/docs/concepts/groups.md +373 -0
  94. package/docs/concepts/markdown-formatting.md +130 -0
  95. package/docs/concepts/memory.md +546 -0
  96. package/docs/concepts/messages.md +154 -0
  97. package/docs/concepts/model-failover.md +149 -0
  98. package/docs/concepts/model-providers.md +315 -0
  99. package/docs/concepts/models.md +208 -0
  100. package/docs/concepts/multi-agent.md +376 -0
  101. package/docs/concepts/oauth.md +145 -0
  102. package/docs/concepts/plugins.md +454 -0
  103. package/docs/concepts/presence.md +102 -0
  104. package/docs/concepts/queue.md +89 -0
  105. package/docs/concepts/retry.md +69 -0
  106. package/docs/concepts/secrets.md +300 -0
  107. package/docs/concepts/session-pruning.md +122 -0
  108. package/docs/concepts/session-tool.md +193 -0
  109. package/docs/concepts/session.md +188 -0
  110. package/docs/concepts/sessions.md +10 -0
  111. package/docs/concepts/skills.md +392 -0
  112. package/docs/concepts/streaming.md +135 -0
  113. package/docs/concepts/system-prompt.md +114 -0
  114. package/docs/concepts/timezone.md +91 -0
  115. package/docs/concepts/typebox.md +289 -0
  116. package/docs/concepts/typing-indicators.md +68 -0
  117. package/docs/concepts/usage-tracking.md +35 -0
  118. package/docs/custom.css +4 -0
  119. package/docs/date-time.md +128 -0
  120. package/docs/debugging.md +162 -0
  121. package/docs/docs.json +1599 -0
  122. package/docs/environment.md +81 -0
  123. package/docs/hooks.md +876 -0
  124. package/docs/index.md +179 -0
  125. package/docs/install/ansible.md +208 -0
  126. package/docs/install/bun.md +59 -0
  127. package/docs/install/development-channels.md +75 -0
  128. package/docs/install/docker.md +567 -0
  129. package/docs/install/index.md +185 -0
  130. package/docs/install/installer.md +123 -0
  131. package/docs/install/migrating.md +192 -0
  132. package/docs/install/nix.md +96 -0
  133. package/docs/install/node.md +78 -0
  134. package/docs/install/uninstall.md +128 -0
  135. package/docs/install/updating.md +228 -0
  136. package/docs/logging.md +350 -0
  137. package/docs/multi-agent-sandbox-tools.md +395 -0
  138. package/docs/network.md +54 -0
  139. package/docs/nodes/audio.md +114 -0
  140. package/docs/nodes/camera.md +156 -0
  141. package/docs/nodes/images.md +72 -0
  142. package/docs/nodes/index.md +341 -0
  143. package/docs/nodes/location-command.md +113 -0
  144. package/docs/nodes/media-understanding.md +379 -0
  145. package/docs/nodes/talk.md +90 -0
  146. package/docs/nodes/voicewake.md +65 -0
  147. package/docs/northflank.mdx +53 -0
  148. package/docs/perplexity.md +80 -0
  149. package/docs/platforms/android.md +129 -0
  150. package/docs/platforms/digitalocean.md +262 -0
  151. package/docs/platforms/exe-dev.md +125 -0
  152. package/docs/platforms/fly.md +486 -0
  153. package/docs/platforms/gcp.md +503 -0
  154. package/docs/platforms/hetzner.md +330 -0
  155. package/docs/platforms/index.md +53 -0
  156. package/docs/platforms/ios.md +106 -0
  157. package/docs/platforms/linux.md +94 -0
  158. package/docs/platforms/mac/bundled-gateway.md +73 -0
  159. package/docs/platforms/mac/canvas.md +125 -0
  160. package/docs/platforms/mac/child-process.md +69 -0
  161. package/docs/platforms/mac/dev-setup.md +102 -0
  162. package/docs/platforms/mac/health.md +34 -0
  163. package/docs/platforms/mac/icon.md +31 -0
  164. package/docs/platforms/mac/logging.md +57 -0
  165. package/docs/platforms/mac/menu-bar.md +81 -0
  166. package/docs/platforms/mac/peekaboo.md +65 -0
  167. package/docs/platforms/mac/permissions.md +44 -0
  168. package/docs/platforms/mac/release.md +85 -0
  169. package/docs/platforms/mac/remote.md +83 -0
  170. package/docs/platforms/mac/signing.md +47 -0
  171. package/docs/platforms/mac/skills.md +33 -0
  172. package/docs/platforms/mac/voice-overlay.md +60 -0
  173. package/docs/platforms/mac/voicewake.md +67 -0
  174. package/docs/platforms/mac/webchat.md +41 -0
  175. package/docs/platforms/mac/xpc.md +61 -0
  176. package/docs/platforms/macos-vm.md +281 -0
  177. package/docs/platforms/macos.md +203 -0
  178. package/docs/platforms/oracle.md +303 -0
  179. package/docs/platforms/raspberry-pi.md +358 -0
  180. package/docs/platforms/windows.md +159 -0
  181. package/docs/plugin.md +651 -0
  182. package/docs/plugins/agent-tools.md +99 -0
  183. package/docs/plugins/manifest.md +71 -0
  184. package/docs/plugins/voice-call.md +273 -0
  185. package/docs/plugins/zalouser.md +70 -0
  186. package/docs/providers/anthropic.md +152 -0
  187. package/docs/providers/claude-max-api-proxy.md +148 -0
  188. package/docs/providers/cloudflare-ai-gateway.md +71 -0
  189. package/docs/providers/deepgram.md +93 -0
  190. package/docs/providers/glm.md +33 -0
  191. package/docs/providers/index.md +63 -0
  192. package/docs/providers/minimax.md +208 -0
  193. package/docs/providers/models.md +51 -0
  194. package/docs/providers/moonshot.md +142 -0
  195. package/docs/providers/ollama.md +223 -0
  196. package/docs/providers/openai.md +62 -0
  197. package/docs/providers/opencode.md +36 -0
  198. package/docs/providers/openrouter.md +37 -0
  199. package/docs/providers/qwen.md +53 -0
  200. package/docs/providers/synthetic.md +99 -0
  201. package/docs/providers/venice.md +267 -0
  202. package/docs/providers/vercel-ai-gateway.md +50 -0
  203. package/docs/providers/xiaomi.md +64 -0
  204. package/docs/providers/zai.md +36 -0
  205. package/docs/railway.mdx +99 -0
  206. package/docs/reference/templates/AGENTS.md +9 -0
  207. package/docs/reference/templates/BOOTSTRAP.md +3 -0
  208. package/docs/reference/templates/HEARTBEAT.md +3 -0
  209. package/docs/reference/templates/IDENTITY.md +3 -0
  210. package/docs/reference/templates/TOOLS.md +3 -0
  211. package/docs/reference/templates/USER.md +3 -0
  212. package/docs/render.mdx +165 -0
  213. package/docs/start/docs-directory.md +63 -0
  214. package/docs/start/getting-started.md +212 -0
  215. package/docs/start/milaidy.md +247 -0
  216. package/docs/start/onboarding.md +258 -0
  217. package/docs/start/pairing.md +86 -0
  218. package/docs/start/quickstart.md +81 -0
  219. package/docs/start/setup.md +149 -0
  220. package/docs/start/showcase.md +416 -0
  221. package/docs/start/wizard.md +418 -0
  222. package/docs/testing.md +368 -0
  223. package/docs/token-use.md +112 -0
  224. package/docs/tools/agent-send.md +53 -0
  225. package/docs/tools/apply-patch.md +50 -0
  226. package/docs/tools/browser-linux-troubleshooting.md +139 -0
  227. package/docs/tools/browser-login.md +68 -0
  228. package/docs/tools/browser.md +576 -0
  229. package/docs/tools/chrome-extension.md +178 -0
  230. package/docs/tools/clawhub.md +257 -0
  231. package/docs/tools/creating-skills.md +54 -0
  232. package/docs/tools/elevated.md +57 -0
  233. package/docs/tools/exec-approvals.md +246 -0
  234. package/docs/tools/exec.md +179 -0
  235. package/docs/tools/firecrawl.md +61 -0
  236. package/docs/tools/index.md +508 -0
  237. package/docs/tools/llm-task.md +115 -0
  238. package/docs/tools/reactions.md +22 -0
  239. package/docs/tools/skills-config.md +76 -0
  240. package/docs/tools/skills.md +300 -0
  241. package/docs/tools/slash-commands.md +196 -0
  242. package/docs/tools/subagents.md +151 -0
  243. package/docs/tools/thinking.md +73 -0
  244. package/docs/tools/web.md +261 -0
  245. package/docs/tui.md +159 -0
  246. package/docs/vps.md +43 -0
  247. package/docs/web/control-ui.md +221 -0
  248. package/docs/web/dashboard.md +46 -0
  249. package/docs/web/index.md +116 -0
  250. package/docs/web/webchat.md +49 -0
  251. package/milaidy.mjs +14 -0
  252. package/package.json +271 -0
  253. package/skills/.cache/catalog.json +88519 -0
package/docs/hooks.md ADDED
@@ -0,0 +1,876 @@
1
+ ---
2
+ summary: "Hooks: event-driven automation for commands and lifecycle events"
3
+ read_when:
4
+ - You want event-driven automation for /new, /reset, /stop, and agent lifecycle events
5
+ - You want to build, install, or debug hooks
6
+ title: "Hooks"
7
+ ---
8
+
9
+ # Hooks
10
+
11
+ Hooks provide an extensible event-driven system for automating actions in response to agent commands and events. Hooks are automatically discovered from directories and can be managed via CLI commands, similar to how skills work in Milaidy.
12
+
13
+ ## Getting Oriented
14
+
15
+ Hooks are small scripts that run when something happens. There are two kinds:
16
+
17
+ - **Hooks** (this page): run inside the Gateway when agent events fire, like `/new`, `/reset`, `/stop`, or lifecycle events.
18
+ - **Webhooks**: external HTTP webhooks that let other systems trigger work in Milaidy. See [Webhook Hooks](/automation/webhook) or use `milaidy webhooks` for Gmail helper commands.
19
+
20
+ Hooks can also be bundled inside plugins; see [Plugins](/plugin#plugin-hooks).
21
+
22
+ Common uses:
23
+
24
+ - Save a memory snapshot when you reset a session
25
+ - Keep an audit trail of commands for troubleshooting or compliance
26
+ - Trigger follow-up automation when a session starts or ends
27
+ - Write files into the agent workspace or call external APIs when events fire
28
+
29
+ If you can write a small TypeScript function, you can write a hook. Hooks are discovered automatically, and you enable or disable them via the CLI.
30
+
31
+ ## Overview
32
+
33
+ The hooks system allows you to:
34
+
35
+ - Save session context to memory when `/new` is issued
36
+ - Log all commands for auditing
37
+ - Trigger custom automations on agent lifecycle events
38
+ - Extend Milaidy's behavior without modifying core code
39
+
40
+ ## Getting Started
41
+
42
+ ### Bundled Hooks
43
+
44
+ Milaidy ships with four bundled hooks that are automatically discovered:
45
+
46
+ - **💾 session-memory**: Saves session context to your agent workspace (default `~/.milaidy/workspace/memory/`) when you issue `/new`
47
+ - **📝 command-logger**: Logs all command events to `~/.milaidy/logs/commands.log`
48
+ - **🚀 boot-md**: Runs `BOOT.md` when the gateway starts (requires internal hooks enabled)
49
+
50
+ List available hooks:
51
+
52
+ ```bash
53
+ milaidy hooks list
54
+ ```
55
+
56
+ Enable a hook:
57
+
58
+ ```bash
59
+ milaidy hooks enable session-memory
60
+ ```
61
+
62
+ Check hook status:
63
+
64
+ ```bash
65
+ milaidy hooks check
66
+ ```
67
+
68
+ Get detailed information:
69
+
70
+ ```bash
71
+ milaidy hooks info session-memory
72
+ ```
73
+
74
+ ### Onboarding
75
+
76
+ During onboarding (`milaidy onboard`), you'll be prompted to enable recommended hooks. The wizard automatically discovers eligible hooks and presents them for selection.
77
+
78
+ ## Hook Discovery
79
+
80
+ Hooks are automatically discovered from three directories (in order of precedence):
81
+
82
+ 1. **Workspace hooks**: `<workspace>/hooks/` (per-agent, highest precedence)
83
+ 2. **Managed hooks**: `~/.milaidy/hooks/` (user-installed, shared across workspaces)
84
+ 3. **Bundled hooks**: `<milaidy>/dist/hooks/bundled/` (shipped with Milaidy)
85
+
86
+ Managed hook directories can be either a **single hook** or a **hook pack** (package directory).
87
+
88
+ Each hook is a directory containing:
89
+
90
+ ```
91
+ my-hook/
92
+ ├── HOOK.md # Metadata + documentation
93
+ └── handler.ts # Handler implementation
94
+ ```
95
+
96
+ ## Hook Packs (npm/archives)
97
+
98
+ Hook packs are standard npm packages that export one or more hooks via `milaidy.hooks` in
99
+ `package.json`. Install them with:
100
+
101
+ ```bash
102
+ milaidy hooks install <path-or-spec>
103
+ ```
104
+
105
+ Example `package.json`:
106
+
107
+ ```json
108
+ {
109
+ "name": "@acme/my-hooks",
110
+ "version": "0.1.0",
111
+ "milaidy": {
112
+ "hooks": ["./hooks/my-hook", "./hooks/other-hook"]
113
+ }
114
+ }
115
+ ```
116
+
117
+ Each entry points to a hook directory containing `HOOK.md` and `handler.ts` (or `index.ts`).
118
+ Hook packs can ship dependencies; they will be installed under `~/.milaidy/hooks/<id>`.
119
+
120
+ ## Hook Structure
121
+
122
+ ### HOOK.md Format
123
+
124
+ The `HOOK.md` file contains metadata in YAML frontmatter plus Markdown documentation:
125
+
126
+ ```markdown
127
+ ---
128
+ name: my-hook
129
+ description: "Short description of what this hook does"
130
+ homepage: https://docs.milaidy.ai/hooks#my-hook
131
+ metadata:
132
+ { "milaidy": { "emoji": "🔗", "events": ["command:new"], "requires": { "bins": ["node"] } } }
133
+ ---
134
+
135
+ # My Hook
136
+
137
+ Detailed documentation goes here...
138
+
139
+ ## What It Does
140
+
141
+ - Listens for `/new` commands
142
+ - Performs some action
143
+ - Logs the result
144
+
145
+ ## Requirements
146
+
147
+ - Node.js must be installed
148
+
149
+ ## Configuration
150
+
151
+ No configuration needed.
152
+ ```
153
+
154
+ ### Metadata Fields
155
+
156
+ The `metadata.milaidy` object supports:
157
+
158
+ - **`emoji`**: Display emoji for CLI (e.g., `"💾"`)
159
+ - **`events`**: Array of events to listen for (e.g., `["command:new", "command:reset"]`)
160
+ - **`export`**: Named export to use (defaults to `"default"`)
161
+ - **`homepage`**: Documentation URL
162
+ - **`requires`**: Optional requirements
163
+ - **`bins`**: Required binaries on PATH (e.g., `["git", "node"]`)
164
+ - **`anyBins`**: At least one of these binaries must be present
165
+ - **`env`**: Required environment variables
166
+ - **`config`**: Required config paths (e.g., `["workspace.dir"]`)
167
+ - **`os`**: Required platforms (e.g., `["darwin", "linux"]`)
168
+ - **`always`**: Bypass eligibility checks (boolean)
169
+ - **`install`**: Installation methods (for bundled hooks: `[{"id":"bundled","kind":"bundled"}]`)
170
+
171
+ ### Handler Implementation
172
+
173
+ The `handler.ts` file exports a `HookHandler` function:
174
+
175
+ ```typescript
176
+ import type { HookHandler } from "../../src/hooks/hooks.js";
177
+
178
+ const myHandler: HookHandler = async (event) => {
179
+ // Only trigger on 'new' command
180
+ if (event.type !== "command" || event.action !== "new") {
181
+ return;
182
+ }
183
+
184
+ console.log(`[my-hook] New command triggered`);
185
+ console.log(` Session: ${event.sessionKey}`);
186
+ console.log(` Timestamp: ${event.timestamp.toISOString()}`);
187
+
188
+ // Your custom logic here
189
+
190
+ // Optionally send message to user
191
+ event.messages.push("✨ My hook executed!");
192
+ };
193
+
194
+ export default myHandler;
195
+ ```
196
+
197
+ #### Event Context
198
+
199
+ Each event includes:
200
+
201
+ ```typescript
202
+ {
203
+ type: 'command' | 'session' | 'agent' | 'gateway',
204
+ action: string, // e.g., 'new', 'reset', 'stop'
205
+ sessionKey: string, // Session identifier
206
+ timestamp: Date, // When the event occurred
207
+ messages: string[], // Push messages here to send to user
208
+ context: {
209
+ sessionEntry?: SessionEntry,
210
+ sessionId?: string,
211
+ sessionFile?: string,
212
+ commandSource?: string, // e.g., 'whatsapp', 'telegram'
213
+ senderId?: string,
214
+ workspaceDir?: string,
215
+ bootstrapFiles?: WorkspaceBootstrapFile[],
216
+ cfg?: MilaidyConfig
217
+ }
218
+ }
219
+ ```
220
+
221
+ ## Event Types
222
+
223
+ ### Command Events
224
+
225
+ Triggered when agent commands are issued:
226
+
227
+ - **`command`**: All command events (general listener)
228
+ - **`command:new`**: When `/new` command is issued
229
+ - **`command:reset`**: When `/reset` command is issued
230
+ - **`command:stop`**: When `/stop` command is issued
231
+
232
+ ### Agent Events
233
+
234
+ - **`agent:bootstrap`**: Before workspace bootstrap files are injected (hooks may mutate `context.bootstrapFiles`)
235
+
236
+ ### Gateway Events
237
+
238
+ Triggered when the gateway starts:
239
+
240
+ - **`gateway:startup`**: After channels start and hooks are loaded
241
+
242
+ ### Tool Result Hooks (Plugin API)
243
+
244
+ These hooks are not event-stream listeners; they let plugins synchronously adjust tool results before Milaidy persists them.
245
+
246
+ - **`tool_result_persist`**: transform tool results before they are written to the session transcript. Must be synchronous; return the updated tool result payload or `undefined` to keep it as-is. See [Agent Loop](/concepts/agent-loop).
247
+
248
+ ### Future Events
249
+
250
+ Planned event types:
251
+
252
+ - **`session:start`**: When a new session begins
253
+ - **`session:end`**: When a session ends
254
+ - **`agent:error`**: When an agent encounters an error
255
+ - **`message:sent`**: When a message is sent
256
+ - **`message:received`**: When a message is received
257
+
258
+ ## Creating Custom Hooks
259
+
260
+ ### 1. Choose Location
261
+
262
+ - **Workspace hooks** (`<workspace>/hooks/`): Per-agent, highest precedence
263
+ - **Managed hooks** (`~/.milaidy/hooks/`): Shared across workspaces
264
+
265
+ ### 2. Create Directory Structure
266
+
267
+ ```bash
268
+ mkdir -p ~/.milaidy/hooks/my-hook
269
+ cd ~/.milaidy/hooks/my-hook
270
+ ```
271
+
272
+ ### 3. Create HOOK.md
273
+
274
+ ```markdown
275
+ ---
276
+ name: my-hook
277
+ description: "Does something useful"
278
+ metadata: { "milaidy": { "emoji": "🎯", "events": ["command:new"] } }
279
+ ---
280
+
281
+ # My Custom Hook
282
+
283
+ This hook does something useful when you issue `/new`.
284
+ ```
285
+
286
+ ### 4. Create handler.ts
287
+
288
+ ```typescript
289
+ import type { HookHandler } from "../../src/hooks/hooks.js";
290
+
291
+ const handler: HookHandler = async (event) => {
292
+ if (event.type !== "command" || event.action !== "new") {
293
+ return;
294
+ }
295
+
296
+ console.log("[my-hook] Running!");
297
+ // Your logic here
298
+ };
299
+
300
+ export default handler;
301
+ ```
302
+
303
+ ### 5. Enable and Test
304
+
305
+ ```bash
306
+ # Verify hook is discovered
307
+ milaidy hooks list
308
+
309
+ # Enable it
310
+ milaidy hooks enable my-hook
311
+
312
+ # Restart your gateway process (menu bar app restart on macOS, or restart your dev process)
313
+
314
+ # Trigger the event
315
+ # Send /new via your messaging channel
316
+ ```
317
+
318
+ ## Configuration
319
+
320
+ ### New Config Format (Recommended)
321
+
322
+ ```json
323
+ {
324
+ "hooks": {
325
+ "internal": {
326
+ "enabled": true,
327
+ "entries": {
328
+ "session-memory": { "enabled": true },
329
+ "command-logger": { "enabled": false }
330
+ }
331
+ }
332
+ }
333
+ }
334
+ ```
335
+
336
+ ### Per-Hook Configuration
337
+
338
+ Hooks can have custom configuration:
339
+
340
+ ```json
341
+ {
342
+ "hooks": {
343
+ "internal": {
344
+ "enabled": true,
345
+ "entries": {
346
+ "my-hook": {
347
+ "enabled": true,
348
+ "env": {
349
+ "MY_CUSTOM_VAR": "value"
350
+ }
351
+ }
352
+ }
353
+ }
354
+ }
355
+ }
356
+ ```
357
+
358
+ ### Extra Directories
359
+
360
+ Load hooks from additional directories:
361
+
362
+ ```json
363
+ {
364
+ "hooks": {
365
+ "internal": {
366
+ "enabled": true,
367
+ "load": {
368
+ "extraDirs": ["/path/to/more/hooks"]
369
+ }
370
+ }
371
+ }
372
+ }
373
+ ```
374
+
375
+ ### Legacy Config Format (Still Supported)
376
+
377
+ The old config format still works for backwards compatibility:
378
+
379
+ ```json
380
+ {
381
+ "hooks": {
382
+ "internal": {
383
+ "enabled": true,
384
+ "handlers": [
385
+ {
386
+ "event": "command:new",
387
+ "module": "./hooks/handlers/my-handler.ts",
388
+ "export": "default"
389
+ }
390
+ ]
391
+ }
392
+ }
393
+ }
394
+ ```
395
+
396
+ **Migration**: Use the new discovery-based system for new hooks. Legacy handlers are loaded after directory-based hooks.
397
+
398
+ ## CLI Commands
399
+
400
+ ### List Hooks
401
+
402
+ ```bash
403
+ # List all hooks
404
+ milaidy hooks list
405
+
406
+ # Show only eligible hooks
407
+ milaidy hooks list --eligible
408
+
409
+ # Verbose output (show missing requirements)
410
+ milaidy hooks list --verbose
411
+
412
+ # JSON output
413
+ milaidy hooks list --json
414
+ ```
415
+
416
+ ### Hook Information
417
+
418
+ ```bash
419
+ # Show detailed info about a hook
420
+ milaidy hooks info session-memory
421
+
422
+ # JSON output
423
+ milaidy hooks info session-memory --json
424
+ ```
425
+
426
+ ### Check Eligibility
427
+
428
+ ```bash
429
+ # Show eligibility summary
430
+ milaidy hooks check
431
+
432
+ # JSON output
433
+ milaidy hooks check --json
434
+ ```
435
+
436
+ ### Enable/Disable
437
+
438
+ ```bash
439
+ # Enable a hook
440
+ milaidy hooks enable session-memory
441
+
442
+ # Disable a hook
443
+ milaidy hooks disable command-logger
444
+ ```
445
+
446
+ ## Bundled Hooks
447
+
448
+ ### session-memory
449
+
450
+ Saves session context to memory when you issue `/new`.
451
+
452
+ **Events**: `command:new`
453
+
454
+ **Requirements**: `workspace.dir` must be configured
455
+
456
+ **Output**: `<workspace>/memory/YYYY-MM-DD-slug.md` (defaults to `~/.milaidy/workspace`)
457
+
458
+ **What it does**:
459
+
460
+ 1. Uses the pre-reset session entry to locate the correct transcript
461
+ 2. Extracts the last 15 lines of conversation
462
+ 3. Uses LLM to generate a descriptive filename slug
463
+ 4. Saves session metadata to a dated memory file
464
+
465
+ **Example output**:
466
+
467
+ ```markdown
468
+ # Session: 2026-01-16 14:30:00 UTC
469
+
470
+ - **Session Key**: agent:main:main
471
+ - **Session ID**: abc123def456
472
+ - **Source**: telegram
473
+ ```
474
+
475
+ **Filename examples**:
476
+
477
+ - `2026-01-16-vendor-pitch.md`
478
+ - `2026-01-16-api-design.md`
479
+ - `2026-01-16-1430.md` (fallback timestamp if slug generation fails)
480
+
481
+ **Enable**:
482
+
483
+ ```bash
484
+ milaidy hooks enable session-memory
485
+ ```
486
+
487
+ ### command-logger
488
+
489
+ Logs all command events to a centralized audit file.
490
+
491
+ **Events**: `command`
492
+
493
+ **Requirements**: None
494
+
495
+ **Output**: `~/.milaidy/logs/commands.log`
496
+
497
+ **What it does**:
498
+
499
+ 1. Captures event details (command action, timestamp, session key, sender ID, source)
500
+ 2. Appends to log file in JSONL format
501
+ 3. Runs silently in the background
502
+
503
+ **Example log entries**:
504
+
505
+ ```jsonl
506
+ {"timestamp":"2026-01-16T14:30:00.000Z","action":"new","sessionKey":"agent:main:main","senderId":"+1234567890","source":"telegram"}
507
+ {"timestamp":"2026-01-16T15:45:22.000Z","action":"stop","sessionKey":"agent:main:main","senderId":"user@example.com","source":"whatsapp"}
508
+ ```
509
+
510
+ **View logs**:
511
+
512
+ ```bash
513
+ # View recent commands
514
+ tail -n 20 ~/.milaidy/logs/commands.log
515
+
516
+ # Pretty-print with jq
517
+ cat ~/.milaidy/logs/commands.log | jq .
518
+
519
+ # Filter by action
520
+ grep '"action":"new"' ~/.milaidy/logs/commands.log | jq .
521
+ ```
522
+
523
+ **Enable**:
524
+
525
+ ```bash
526
+ milaidy hooks enable command-logger
527
+ ```
528
+
529
+ ### boot-md
530
+
531
+ Runs `BOOT.md` when the gateway starts (after channels start).
532
+ Internal hooks must be enabled for this to run.
533
+
534
+ **Events**: `gateway:startup`
535
+
536
+ **Requirements**: `workspace.dir` must be configured
537
+
538
+ **What it does**:
539
+
540
+ 1. Reads `BOOT.md` from your workspace
541
+ 2. Runs the instructions via the agent runner
542
+ 3. Sends any requested outbound messages via the message tool
543
+
544
+ **Enable**:
545
+
546
+ ```bash
547
+ milaidy hooks enable boot-md
548
+ ```
549
+
550
+ ## Best Practices
551
+
552
+ ### Keep Handlers Fast
553
+
554
+ Hooks run during command processing. Keep them lightweight:
555
+
556
+ ```typescript
557
+ // ✓ Good - async work, returns immediately
558
+ const handler: HookHandler = async (event) => {
559
+ void processInBackground(event); // Fire and forget
560
+ };
561
+
562
+ // ✗ Bad - blocks command processing
563
+ const handler: HookHandler = async (event) => {
564
+ await slowDatabaseQuery(event);
565
+ await evenSlowerAPICall(event);
566
+ };
567
+ ```
568
+
569
+ ### Handle Errors Gracefully
570
+
571
+ Always wrap risky operations:
572
+
573
+ ```typescript
574
+ const handler: HookHandler = async (event) => {
575
+ try {
576
+ await riskyOperation(event);
577
+ } catch (err) {
578
+ console.error("[my-handler] Failed:", err instanceof Error ? err.message : String(err));
579
+ // Don't throw - let other handlers run
580
+ }
581
+ };
582
+ ```
583
+
584
+ ### Filter Events Early
585
+
586
+ Return early if the event isn't relevant:
587
+
588
+ ```typescript
589
+ const handler: HookHandler = async (event) => {
590
+ // Only handle 'new' commands
591
+ if (event.type !== "command" || event.action !== "new") {
592
+ return;
593
+ }
594
+
595
+ // Your logic here
596
+ };
597
+ ```
598
+
599
+ ### Use Specific Event Keys
600
+
601
+ Specify exact events in metadata when possible:
602
+
603
+ ```yaml
604
+ metadata: { "milaidy": { "events": ["command:new"] } } # Specific
605
+ ```
606
+
607
+ Rather than:
608
+
609
+ ```yaml
610
+ metadata: { "milaidy": { "events": ["command"] } } # General - more overhead
611
+ ```
612
+
613
+ ## Debugging
614
+
615
+ ### Enable Hook Logging
616
+
617
+ The gateway logs hook loading at startup:
618
+
619
+ ```
620
+ Registered hook: session-memory -> command:new
621
+ Registered hook: command-logger -> command
622
+ Registered hook: boot-md -> gateway:startup
623
+ ```
624
+
625
+ ### Check Discovery
626
+
627
+ List all discovered hooks:
628
+
629
+ ```bash
630
+ milaidy hooks list --verbose
631
+ ```
632
+
633
+ ### Check Registration
634
+
635
+ In your handler, log when it's called:
636
+
637
+ ```typescript
638
+ const handler: HookHandler = async (event) => {
639
+ console.log("[my-handler] Triggered:", event.type, event.action);
640
+ // Your logic
641
+ };
642
+ ```
643
+
644
+ ### Verify Eligibility
645
+
646
+ Check why a hook isn't eligible:
647
+
648
+ ```bash
649
+ milaidy hooks info my-hook
650
+ ```
651
+
652
+ Look for missing requirements in the output.
653
+
654
+ ## Testing
655
+
656
+ ### Gateway Logs
657
+
658
+ Monitor gateway logs to see hook execution:
659
+
660
+ ```bash
661
+ # macOS
662
+ ./apps/shared/scripts/clawlog.sh -f
663
+
664
+ # Other platforms
665
+ tail -f ~/.milaidy/gateway.log
666
+ ```
667
+
668
+ ### Test Hooks Directly
669
+
670
+ Test your handlers in isolation:
671
+
672
+ ```typescript
673
+ import { test } from "vitest";
674
+ import { createHookEvent } from "./src/hooks/hooks.js";
675
+ import myHandler from "./hooks/my-hook/handler.js";
676
+
677
+ test("my handler works", async () => {
678
+ const event = createHookEvent("command", "new", "test-session", {
679
+ foo: "bar",
680
+ });
681
+
682
+ await myHandler(event);
683
+
684
+ // Assert side effects
685
+ });
686
+ ```
687
+
688
+ ## Architecture
689
+
690
+ ### Core Components
691
+
692
+ - **`src/hooks/types.ts`**: Type definitions
693
+ - **`src/hooks/workspace.ts`**: Directory scanning and loading
694
+ - **`src/hooks/frontmatter.ts`**: HOOK.md metadata parsing
695
+ - **`src/hooks/config.ts`**: Eligibility checking
696
+ - **`src/hooks/hooks-status.ts`**: Status reporting
697
+ - **`src/hooks/loader.ts`**: Dynamic module loader
698
+ - **`src/cli/hooks-cli.ts`**: CLI commands
699
+ - **`src/gateway/server-startup.ts`**: Loads hooks at gateway start
700
+ - **`src/auto-reply/reply/commands-core.ts`**: Triggers command events
701
+
702
+ ### Discovery Flow
703
+
704
+ ```
705
+ Gateway startup
706
+
707
+ Scan directories (workspace → managed → bundled)
708
+
709
+ Parse HOOK.md files
710
+
711
+ Check eligibility (bins, env, config, os)
712
+
713
+ Load handlers from eligible hooks
714
+
715
+ Register handlers for events
716
+ ```
717
+
718
+ ### Event Flow
719
+
720
+ ```
721
+ User sends /new
722
+
723
+ Command validation
724
+
725
+ Create hook event
726
+
727
+ Trigger hook (all registered handlers)
728
+
729
+ Command processing continues
730
+
731
+ Session reset
732
+ ```
733
+
734
+ ## Troubleshooting
735
+
736
+ ### Hook Not Discovered
737
+
738
+ 1. Check directory structure:
739
+
740
+ ```bash
741
+ ls -la ~/.milaidy/hooks/my-hook/
742
+ # Should show: HOOK.md, handler.ts
743
+ ```
744
+
745
+ 2. Verify HOOK.md format:
746
+
747
+ ```bash
748
+ cat ~/.milaidy/hooks/my-hook/HOOK.md
749
+ # Should have YAML frontmatter with name and metadata
750
+ ```
751
+
752
+ 3. List all discovered hooks:
753
+ ```bash
754
+ milaidy hooks list
755
+ ```
756
+
757
+ ### Hook Not Eligible
758
+
759
+ Check requirements:
760
+
761
+ ```bash
762
+ milaidy hooks info my-hook
763
+ ```
764
+
765
+ Look for missing:
766
+
767
+ - Binaries (check PATH)
768
+ - Environment variables
769
+ - Config values
770
+ - OS compatibility
771
+
772
+ ### Hook Not Executing
773
+
774
+ 1. Verify hook is enabled:
775
+
776
+ ```bash
777
+ milaidy hooks list
778
+ # Should show ✓ next to enabled hooks
779
+ ```
780
+
781
+ 2. Restart your gateway process so hooks reload.
782
+
783
+ 3. Check gateway logs for errors:
784
+ ```bash
785
+ ./apps/shared/scripts/clawlog.sh | grep hook
786
+ ```
787
+
788
+ ### Handler Errors
789
+
790
+ Check for TypeScript/import errors:
791
+
792
+ ```bash
793
+ # Test import directly
794
+ node -e "import('./path/to/handler.ts').then(console.log)"
795
+ ```
796
+
797
+ ## Migration Guide
798
+
799
+ ### From Legacy Config to Discovery
800
+
801
+ **Before**:
802
+
803
+ ```json
804
+ {
805
+ "hooks": {
806
+ "internal": {
807
+ "enabled": true,
808
+ "handlers": [
809
+ {
810
+ "event": "command:new",
811
+ "module": "./hooks/handlers/my-handler.ts"
812
+ }
813
+ ]
814
+ }
815
+ }
816
+ }
817
+ ```
818
+
819
+ **After**:
820
+
821
+ 1. Create hook directory:
822
+
823
+ ```bash
824
+ mkdir -p ~/.milaidy/hooks/my-hook
825
+ mv ./hooks/handlers/my-handler.ts ~/.milaidy/hooks/my-hook/handler.ts
826
+ ```
827
+
828
+ 2. Create HOOK.md:
829
+
830
+ ```markdown
831
+ ---
832
+ name: my-hook
833
+ description: "My custom hook"
834
+ metadata: { "milaidy": { "emoji": "🎯", "events": ["command:new"] } }
835
+ ---
836
+
837
+ # My Hook
838
+
839
+ Does something useful.
840
+ ```
841
+
842
+ 3. Update config:
843
+
844
+ ```json
845
+ {
846
+ "hooks": {
847
+ "internal": {
848
+ "enabled": true,
849
+ "entries": {
850
+ "my-hook": { "enabled": true }
851
+ }
852
+ }
853
+ }
854
+ }
855
+ ```
856
+
857
+ 4. Verify and restart your gateway process:
858
+ ```bash
859
+ milaidy hooks list
860
+ # Should show: 🎯 my-hook ✓
861
+ ```
862
+
863
+ **Benefits of migration**:
864
+
865
+ - Automatic discovery
866
+ - CLI management
867
+ - Eligibility checking
868
+ - Better documentation
869
+ - Consistent structure
870
+
871
+ ## See Also
872
+
873
+ - [CLI Reference: hooks](/cli/hooks)
874
+ - [Bundled Hooks README](https://github.com/milaidy/milaidy/tree/main/src/hooks/bundled)
875
+ - [Webhook Hooks](/automation/webhook)
876
+ - [Configuration](/gateway/configuration#hooks)