-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat(plugin): add ToolResult type for structured tool responses #6544
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
base: dev
Are you sure you want to change the base?
feat(plugin): add ToolResult type for structured tool responses #6544
Conversation
|
@rekram1-node can you take a look? |
0fa0047 to
d2ef4f0
Compare
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
- Add ToolResult interface to @opencode-ai/plugin
- Update execute signature to Promise<string | ToolResult>
- Enables plugin tools to return {title, metadata, output}
- Add ToolResult interface to @opencode-ai/plugin for structured tool responses - Update execute signature to accept string | ToolResult - Add runtime detection in fromPlugin to preserve title and metadata - Extract Tool.Result interface for clarity in opencode package - Maintain backward compatibility with string-only results
c0c0acf to
f35c3da
Compare
## Problem
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
## Solution
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
## Changes
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
## Usage
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
## Related
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
## Problem
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
## Solution
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
## Changes
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
## Usage
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
## Related
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
## Problem
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
## Solution
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
## Changes
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
## Usage
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
## Related
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
## Problem
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
## Solution
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
## Changes
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
## Usage
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
## Related
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
|
So I made a very similar PR here #9138 because I apparently suck at searching through GitHub issues and PRs. The only major difference is that I added a test to ensure it works as expected and added more of the missing fields to the Or if you want to keep the test and types, feel free to merge that into this PR (and then I can close mine out). Obviously I'm a fan of your solution because we basically wrote the same thing 😆! |
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
Plugin tools could only return a plain string. This prevented:
- Structured responses with titles, metadata, and attachments
- Real-time streaming updates during long-running operations
- Subagent result propagation (tool counts, progress, etc.)
Add ToolResult interface and metadata() method to plugin SDK:
- ToolResult interface: { title, metadata, output, attachments? }
- ctx.metadata(): Emit streaming updates during execution
- Backward compatible: tools can still return plain strings
- Core extraction: Tool.Result interface for unified typing
- packages/plugin/src/tool.ts - Add ToolResult interface, metadata() method
- packages/opencode/src/tool/tool.ts - Extract Tool.Result interface
- packages/opencode/src/tool/registry.ts - Handle structured results in fromPlugin
```typescript
import { tool, type ToolResult } from "@opencode-ai/plugin"
export default tool({
description: "My tool",
args: { input: z.string() },
async execute(args, ctx): Promise<ToolResult> {
ctx.metadata({ title: "Processing...", metadata: { step: 1 } })
// ... work ...
return {
title: "Done",
metadata: { count: 42 },
output: "Result text"
}
}
})
```
- Resolves anomalyco#7590
Patchwork-Source: fix/plugin-tool-metadata-persistence
Patchwork-PR: anomalyco#6544
Patchwork-Squashed: 2026-01-12T21:07:21.648Z
Problem
Plugin tools could only return a plain string. This prevented:
Solution
Add ToolResult interface and metadata() method to plugin SDK:
Changes
Usage
Related