opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat: add ctx.log() for custom tools

Open elithrar opened this issue 1 week ago • 0 comments

Fixes #6830 and #6833 by adding ctx.log() for custom tools to avoid console.log output leaking to stdout.

problem:

  • Custom plugins had ctx.client.app.log available to them, but custom tools did not - there was no logger exposed to tools (making it hard to debug)
  • the docs still guided console.log (missed this in #6833)

solution:

  • Added ctx.log(level, message, extra) to ToolContext for custom tool implementations
  • Logs using OpenCode's internal Log.create() API (appropriate since tools execute in-process)
  • Logger instances created once per context to avoid overhead
  • Updated plugin docs to show ctx.log() for custom tools and client.app.log() for plugin initialization
  • Clarified that console.log leaks to stdout

I also had to update test sites for built-in tools to plumb log through into the ToolContext, hence the larger number of changed files here.

Custom tools now have structured logging that writes to OpenCode's log files instead of polluting terminal output.

elithrar avatar Jan 04 '26 18:01 elithrar