Commit 08126a0
committed
fix: add tool.definition hook to bridge zod registry descriptions to host
## High level changes
- `packages/opencode-plugin/src/plugin.ts`: Added `'tool.definition'` hook that dynamically
imports `'zod'` at hook-call time and registers each tool parameter description into the
host's (OpenCode's) `globalRegistry`, making descriptions visible to `z.toJSONSchema()`
## Motivation
The previous peerDependency fix (moving `zod` from `dependencies` to `peerDependencies`) was
insufficient: OpenCode is distributed as a compiled Bun binary with zod bundled internally, so
module hoisting / peerDep resolution does not apply. As a result, the plugin and OpenCode continued
to use separate zod instances with separate `globalRegistry` singletons, meaning `.describe()`
annotations on tool parameters never reached the LLM's JSON Schema.
## Details
- The `tool.definition` hook fires after OpenCode wraps plugin args with `z.object(def.args)`
(its own zod) but before `z.toJSONSchema(item.parameters)` is called in `prompt.ts`
- `output.parameters._zod.def.shape` contains the original plugin field schemas; their
`.description` getter reads from the plugin's `globalRegistry` and works cross-instance
- Dynamic `import('zod')` at runtime: when the plugin has no local `node_modules/zod`
(peerDependency, correctly installed), ESM resolution walks up to the host's (OpenCode's) zod
module, which is already in the module cache — returning the same `globalRegistry` singleton
that `z.toJSONSchema()` will read from
- Errors are silently swallowed — descriptions are a nice-to-have for LLM guidance, not critical
for tool execution
- All 64 existing tests pass1 parent 3890751 commit 08126a0
2 files changed
Lines changed: 73 additions & 0 deletions
File tree
- .vibe
- packages/opencode-plugin/src
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
58 | 86 | | |
59 | 87 | | |
60 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
797 | 797 | | |
798 | 798 | | |
799 | 799 | | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
800 | 845 | | |
801 | 846 | | |
802 | 847 | | |
| |||
0 commit comments