opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(plugin): add ToolResult type for structured tool responses

Open aryasaatvik opened this issue 2 weeks ago • 2 comments

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

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 #7590

aryasaatvik avatar Dec 31 '25 20:12 aryasaatvik