←新闻详情

goose 官方博客更新于 10月12日 19:22

goose 上线生命周期钩子,可在每个事件触发自定义脚本

Hooks: run your own scripts on every goose event | goose | Your open source AI agent

AI 导读

开源 AI 智能体 goose 支持生命周期钩子,把插件放进 ~/.agents/plugins/ 或项目级 .agents/plugins/ 目录并包含 hooks/hooks.json 即可在启动时自动发现。钩子覆盖工具调用前后、用户提交提示词、会话开始与结束等事件,goose 会执行命令、设置 PLUGIN_ROOT 并通过 stdin 传入 JSON 载荷,matcher 字段用正则匹配工具名、文件路径或 shell 命令。该功能遵循 Open Plugins 钩子规范,失败或超时的钩子只记录日志而不会中断宿主工具,仓库 examples/plugins/hello-hooks 提供了可运行的示例插件。

推荐理由

精选AI 评分 62

原文给出钩子的事件类型、插件目录约定和可直接复用的示例,读者可据此把 goose 的会话过程接入自己的脚本。

主题

标签

正文

当前显示原文,中文全文译文尚未就绪。

goose now supports lifecycle hooks. Drop a plugin into a directory on disk and goose will run your shell scripts when things happen during a session: a tool is about to fire, a tool just finished, the user submitted a prompt, the session started, the session ended.

If you've used Claude Code's hooks or git hooks, it's the same idea. If you haven't: the agent loop is now scriptable from the outside, without writing any Rust or any MCP server.

How it works

goose follows the Open Plugins hooks specification. Any plugin directory under ~/.agents/plugins/ / (user scope) or /.agents/plugins/ / (project scope) that contains a hooks/hooks.json file is auto-discovered at startup.

A minimal hook config looks like this:

When the event fires, goose runs the command, sets PLUGIN_ROOT in the environment, and pipes a JSON payload to the script on stdin:

The supported events are:

The matcher field is a regex tested against the most relevant string for the event (tool name, file path, or shell command). Leave it off and the hook fires for every event of that type. Hooks that fail or time out are logged but won't crash the host tool, so your scripts can be as scrappy as you want.

A few things to try

1. Have goose talk to you when it actually needs you

Pick a handful of events that mean "the human's attention would be useful right now" — a tool failed, the session wrapped, a long-running command finished — and have goose speak a line when one of them fires:

Then notify.sh branches on the payload and picks a line:

Tune the matcher regex to whatever counts as "long-running" in your world — test suites, builds, deploys, terraform apply.

2. The "goose is doing something" desk light 🪿💡

If you have a smart bulb with an HTTP API (Hue, LIFX, Home Assistant, etc.), turn it on when goose starts a tool call and off when it finishes:

Now your desk lamp is a status indicator for the agent. Walk away, glance back, and if it's on, goose is still working.

3. Auto-format every file goose edits

Hook AfterFileEdit and run the formatter yourself so the agent doesn't have to remember:

scripts/format.sh reads the file path from stdin and runs prettier --write against it.

4. Daily session journal

Hook SessionEnd and append a one-line summary to a markdown file:

Capture UserPromptSubmit payloads too and you've got a log of every question you asked your agent today.

5. Make goose sound like a submarine

Because you can:

Useful when goose is working on a long task in another window. The audio cue tells you it's actually doing things instead of sitting there waiting on you.

Try the example

There's a working example in the repo at examples/plugins/hello-hooks — a plugin that wires up SessionStart, UserPromptSubmit, PreToolUse, and PostToolUse and prints a friendly emoji to stderr for each one. Copy it to ~/.agents/plugins/, start a session, and watch the events fly by:

Every event also gets appended to ~/.agents/plugins/hello-hooks/last-event.log so you can see the exact JSON your scripts receive. Fire some events, tail the log, build from there.

Why this matters

MCP servers give goose new tools. Hooks go the other direction: they give you a way to react to what goose is doing, in real time, with whatever language you already know. Bash, Python, a Go binary, a one-line curl. It's all just a command on stdin.

The plugin model is small on purpose: a folder, a JSON file, a script. No registration step, no daemon, no rebuild. Drop it in, start goose, it works. Take it out and goose doesn't notice it's gone.

If you build something fun, share it. The examples/plugins/ directory is a good home for community plugins, and the Open Plugins spec means anything you build here works with other agents that adopt it.

Happy hooking. 🪝

来源

goose 官方博客