opencode creating plugins and exposing it via opencode command line

 Let's create a plugin that you can call from opencode command interface for example by typing

 /custom-plugin 

to get opencode to execute your plugin. To do that, we need to create a plugin and then create command that goes into opencode.json

The plugin 

The plugin code - you need to create a folder ".opencode/plugins/custom-tools.ts". 

import { type Plugin, tool } from "@opencode-ai/plugin"

export const CustomToolsPlugin: Plugin = async (ctx) => {
  return {
    tool: {
      mymagictool: tool({
        description: "This is a custom tool",
        args: {
          foo: tool.schema.string(),
        },
        async execute(args, context) {
          const { directory, worktree } = context
          return `Hello ${args.foo} from ${directory} (worktree: ${worktree})`
        },
      }),
    },
  }
}  

With this you would have a plugin defined. You also need to inherit from Plugin (this is the proper) one and so opencode would know about it. 

If you restart opencode and go to /status - you will be able to see your plugin 


Command 

Next, we will wire up our command and this could look like this. 

{
  "$schema": "https://opencode.ai/config.json", 
  "command": {
    "custom-plugin": {
      "template": "Use the CustomToolsPlugin with the argument foo: $ARGUMENTS",
      "description": "Run the custom mytool plugin"
    }
  },
  "plugin": ["superpowers@git+https://github.com/obra/superpowers.git", "opencode-devcontainers", "./plugins/my-plugin.ts"]
}

And then you can call it here 


And then fire away. 








Comments

Popular posts from this blog

gemini cli getting file not defined error

mongosh install properly

llama cpp running it in google colab