Repository navigation
feat(opencode): sync built-in and agent hooks as an OpenCode plugin - #326
Merged
Merged
Conversation
OpenCode has no settings.json hook list; it auto-loads JS/TS plugins from .opencode/plugin (project) or ~/.config/opencode/plugin (user). Generate a teamai-hooks.ts plugin there that subscribes to OpenCode's own events and shells out to the same `teamai hook-dispatch` entry point every other tool uses. Fire-and-forget (OpenCode cannot inject a hook's stdout back into the session), matching the Hermes/OpenClaw adapters. Event mapping mirrors the Claude built-in set: session.created -> session-start session.idle -> stop chat.message -> prompt-submit tool.execute.after -> post-tool-use (Skill/TodoWrite matchers forwarded) - new src/opencode-hooks.ts: plugin generator + inject/remove + agent-hook pair - hooks.ts: route opencode through the plugin adapter in reconcile/inject-all, threading scope so project vs user lands in the right config dir - hooks-cmd.ts: pass scope on inject/remove (global ~/.config/opencode/plugin) - local-agent.ts: dispatch HTTP-source agent hooks to the opencode backend - uninstall.ts: discover and remove opencode plugin files (main + agent hooks) - pull.ts: thread scope through hook auto-migration - docs: README (both langs) feature matrix hooks column, usage-guide OpenCode section Closes Tencent#303 (final remaining piece: OpenCode hooks).
…ize agent hooks The OpenCode plugin adapter had four defects, all verified against a real OpenCode 1.15.13 runtime (tool ids come back lowercase; named hooks are awaited): 1. Matcher casing: the plugin tested `tool === 'Skill' || 'TodoWrite'`, but OpenCode passes lowercase ids (`skill`/`todowrite`), so the Skill-usage and TodoWrite-hint handlers never fired. Map the id back to the PascalCase matcher the handler registry keys on. 2. Empty STDIN: the plugin shelled out with no payload, so track/track-slash/ todowrite-hint (which read tool_name/tool_input/prompt) no-op'd, and the provider-config gate fell back to the user config because cwd was undefined. Forward a JSON payload on STDIN (cwd from the plugin ctx + per-event fields) via a Response (Bun shell can't redirect a bare string). 3. Agent-hook matcher ignored: applyOpencodeAgentHook dropped matcher/timeout, so a tool-scoped enterprise hook ran after every tool call. Thread matcher through and gate tool.execute.after on the (case-insensitive) tool id. 4. Slug path traversal: the agent-hook filename was built from an unsanitized backend slug, so `../../evil` wrote outside the plugin dir. Add assertSafeSlug. Docs (both languages) corrected: OpenCode awaits its named hooks, so the dispatch is not truly non-blocking for chat.message/tool.execute.after.
Collaborator
Author
|
Pushed a fix commit ( Fixes
Docs (both languages) corrected: OpenCode awaits its named hooks, so the dispatch is not truly non-blocking for Verification (real OpenCode 1.15.13)
All 2214 unit tests pass; |
OpenCode auto-loads plugins from both ~/.config/opencode/plugin and <project>/.opencode/plugin. pull/init wrote the plugin at the active teamai scope while `hooks inject` and agent hooks always wrote the user dir, so a project-scope setup ended up with two identical plugins loaded in the same workspace — every event dispatched twice (sync, dashboard, session-start pull). Route every path through one reconciler that writes only the user plugin dir and deletes a project-scope copy from the earlier layout, and drop the now-unused scope plumbing. Uninstall inspects both locations, and its agent-hook sweep joins the plugin dir onto the relative paths listFilesRecursive returns instead of deleting cwd-relative ones. Also make every user-facing uninstall string English, per the repo's output-language rule. Co-authored-by: Cursor <[email protected]>
5 of 6 tasks
jeff-r2026
added a commit
that referenced
this pull request
Aug 26, 2026
#326 translated the uninstall command's user-facing output to English, but two e2e assertions still expected the old Chinese strings, so CI on main went red: e2e.test.ts '卸载完成' -> 'teamai uninstalled' scope-isolation-e2e.test.ts '正在卸载 project scope(项目级)' -> 'Uninstalling project scope' The English output is the intended behavior (CLI output must be English); only the stale assertions needed updating. These never failed on #326's own runs because its PR-level e2e job was queued behind the e2e-fixture-repo concurrency lock and got dropped on merge, and its post-merge run on main was cancelled by the next push. Co-authored-by: Cursor <[email protected]>
Walternater
pushed a commit
to Walternater/teamai-cli
that referenced
this pull request
Nov 2, 2026
…encent#326) * feat(opencode): sync built-in and agent hooks as an OpenCode plugin OpenCode has no settings.json hook list; it auto-loads JS/TS plugins from .opencode/plugin (project) or ~/.config/opencode/plugin (user). Generate a teamai-hooks.ts plugin there that subscribes to OpenCode's own events and shells out to the same `teamai hook-dispatch` entry point every other tool uses. Fire-and-forget (OpenCode cannot inject a hook's stdout back into the session), matching the Hermes/OpenClaw adapters. Event mapping mirrors the Claude built-in set: session.created -> session-start session.idle -> stop chat.message -> prompt-submit tool.execute.after -> post-tool-use (Skill/TodoWrite matchers forwarded) - new src/opencode-hooks.ts: plugin generator + inject/remove + agent-hook pair - hooks.ts: route opencode through the plugin adapter in reconcile/inject-all, threading scope so project vs user lands in the right config dir - hooks-cmd.ts: pass scope on inject/remove (global ~/.config/opencode/plugin) - local-agent.ts: dispatch HTTP-source agent hooks to the opencode backend - uninstall.ts: discover and remove opencode plugin files (main + agent hooks) - pull.ts: thread scope through hook auto-migration - docs: README (both langs) feature matrix hooks column, usage-guide OpenCode section Closes Tencent#303 (final remaining piece: OpenCode hooks). * fix(opencode): forward hook STDIN payload, map tool ids, gate & sanitize agent hooks The OpenCode plugin adapter had four defects, all verified against a real OpenCode 1.15.13 runtime (tool ids come back lowercase; named hooks are awaited): 1. Matcher casing: the plugin tested `tool === 'Skill' || 'TodoWrite'`, but OpenCode passes lowercase ids (`skill`/`todowrite`), so the Skill-usage and TodoWrite-hint handlers never fired. Map the id back to the PascalCase matcher the handler registry keys on. 2. Empty STDIN: the plugin shelled out with no payload, so track/track-slash/ todowrite-hint (which read tool_name/tool_input/prompt) no-op'd, and the provider-config gate fell back to the user config because cwd was undefined. Forward a JSON payload on STDIN (cwd from the plugin ctx + per-event fields) via a Response (Bun shell can't redirect a bare string). 3. Agent-hook matcher ignored: applyOpencodeAgentHook dropped matcher/timeout, so a tool-scoped enterprise hook ran after every tool call. Thread matcher through and gate tool.execute.after on the (case-insensitive) tool id. 4. Slug path traversal: the agent-hook filename was built from an unsanitized backend slug, so `../../evil` wrote outside the plugin dir. Add assertSafeSlug. Docs (both languages) corrected: OpenCode awaits its named hooks, so the dispatch is not truly non-blocking for chat.message/tool.execute.after. * fix(opencode): keep one plugin copy and fix uninstall sweep path OpenCode auto-loads plugins from both ~/.config/opencode/plugin and <project>/.opencode/plugin. pull/init wrote the plugin at the active teamai scope while `hooks inject` and agent hooks always wrote the user dir, so a project-scope setup ended up with two identical plugins loaded in the same workspace — every event dispatched twice (sync, dashboard, session-start pull). Route every path through one reconciler that writes only the user plugin dir and deletes a project-scope copy from the earlier layout, and drop the now-unused scope plumbing. Uninstall inspects both locations, and its agent-hook sweep joins the plugin dir onto the relative paths listFilesRecursive returns instead of deleting cwd-relative ones. Also make every user-facing uninstall string English, per the repo's output-language rule. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
Walternater
pushed a commit
to Walternater/teamai-cli
that referenced
this pull request
Nov 2, 2026
) Tencent#326 translated the uninstall command's user-facing output to English, but two e2e assertions still expected the old Chinese strings, so CI on main went red: e2e.test.ts '卸载完成' -> 'teamai uninstalled' scope-isolation-e2e.test.ts '正在卸载 project scope(项目级)' -> 'Uninstalling project scope' The English output is the intended behavior (CLI output must be English); only the stale assertions needed updating. These never failed on Tencent#326's own runs because its PR-level e2e job was queued behind the e2e-fixture-repo concurrency lock and got dropped on merge, and its post-merge run on main was cancelled by the next push. Co-authored-by: Cursor <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Implements the final remaining piece of #303: OpenCode hooks. PR #306 shipped OpenCode skills/rules/subagents/MCP but explicitly deferred hooks (SessionStart, etc.) to a follow-up — this is that follow-up.
Why it's different from every other agent
OpenCode has no
settings.jsonhook list. It auto-loads JS/TS plugins — the config loader scans{plugin,plugins}/*.{ts,js}under.opencode/(project) and~/.config/opencode/(user). So teamai generates ateamai-hooks.tsplugin that subscribes to OpenCode's own events and shells out to the sameteamai hook-dispatchentry point every other tool uses. Like the Hermes/OpenClaw adapters it is fire-and-forget — OpenCode has no channel to inject a hook's stdout back into the session, so hooks run for their side effects (status report / sync / update) and never block the agent.Event mapping (mirrors the Claude built-in set)
session-startsession.createdstopsession.idleprompt-submitchat.messagepost-tool-usetool.execute.after(Skill/TodoWrite matchers forwarded)Changes
src/opencode-hooks.ts(new): plugin generator +inject/remove+ agent-hookapply/removepair; project vs user scope resolution.hooks.ts: routeopencodethrough the plugin adapter inreconcileHooksToAllTools/injectHooksToAllTools(like Hermes), threading ascopeparam so the plugin lands in the right config dir.hooks-cmd.ts: pass scope onhooks inject/remove— baseDir is HOME, so these install to the global~/.config/opencode/plugin(covers every project, matching how settings.json hooks live in HOME).local-agent.ts: dispatch HTTP-source agent hooks (feat(http): support install_hooks / uninstall_hooks sync commands #238) to the opencode backend (4 sites) instead of erroring on the missing settings path.uninstall.ts: discover and remove opencode plugin files (main hook + leftover agent-hook plugins).pull.ts: thread scope through hook auto-migration.—→✓; usage-guide (both languages) OpenCode section gains a Hooks bullet.Test Plan
All executed and passing:
npx tsc --noEmit— cleannpx vitest run— 2209 passing (added 22 cases inopencode-hooks.test.ts, incl. generated-plugin JS syntax validity across quote/backtick/hyphen-slug edge cases; updated 4hooks-cmdassertions for the newscopearg)npm run build— successnpm pack→npm installthe tarball into a clean project → drive the real installedteamaibinary against a local git team repo:teamai pull(project scope) → plugin written to<root>/.opencode/plugin/teamai-hooks.ts, all 4 event mappings presentteamai hooks inject(global) → plugin written to~/.config/opencode/plugin/teamai-hooks.tsnode --checkon the generated plugin → valid JSteamai hooks remove→ both plugins removedteamai uninstall --agent opencode→ project plugin cleaned upCloses #303