opencode - creating plugin and distributing it via npm

We can easily create a plugin for opencode. In this example, we are just going to write a simple output.

First we need to create a nodejs project using the following code. Please refer to this code repo here for typescript setup and other codes.  https://github.com/kepungnzai/appcoreopc-num-mastery.

In this code, the most important thing is it must inherit from Plugin (quite important) otherwise your agent might not be able to figur out.


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

export const AppcoreopcNumMastery: Plugin = async () => {
  const myTool = tool({
    description: "A simple plugin.",
    args: {
      query: tool.schema.string().describe("Question or topic to submit to Google AI Mode"),
      timeout: tool.schema.number().min(5).max(120).optional().describe("Timeout in seconds (default: 30, max: 120)"),
      followUp: tool.schema.boolean().optional().describe("Treat the query as a follow-up in the same session"),
    },
    async execute(args, _ctx) {
      return `Hello ${args.query} winner!`
    },
  })

  return {
    tool: {
      " ": myTool,
    },
  }
}


You don't really need to publish to npm to use this plugin. You can always check into git. Please make sure you have the right configuration. In this case, it will be "appcoreopc-num-mastery@git+https://github.com/kepungnzai/appcoreopc-num-mastery.git".

Opencode will not download a specific version of npm for you but will pull the code from github instead (main branch in this setup). It is important that you check in the artifact for your build here :- index.js. 

Then you can run your opencode and it will setup the plugin.

opencode.json 

{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "mode": "primary",
      "model": "",
      "prompt": "",
      "tools": {
        "write": true,
        "edit": true,
        "bash": true
      }
    }
  },
  "command": {
  },
  "plugin": ["superpowers@git+https://github.com/obra/superpowers.git",
"appcoreopc-num-mastery@git+https://github.com/kepungnzai/appcoreopc-num-mastery.git"]
}


Publishing to npm

Then you need to publish it using "bun publish". And here is my code. https://www.npmjs.com/package/appcoreopc-num-mastery

Then how do you use it? Let's create a new directory and then create a file call opencode.json that looks like this

{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "mode": "primary",
      "model": "",
      "prompt": "",
      "tools": {
        "write": true,
        "edit": true,
        "bash": true
      }
    }
  },
  "command": {
  },
  "plugin": ["appcoreopc-num-mastery"]
}

And then fire your opencode, and put this in the chatbox "appcoreopc-num-mastery  trigger with tfooooo", then provide the following prompt here. 





Troubleshooting

You plugin not registered?
Try to run /status and here you can check if the plugin is loaded

Your plugin registered but not working as expected - typically looks something like this?
For example, you have to provide alot of instructions to get it to work as shown here. 

In that case, please try to run 

opencode run --print-logs "hello" 2>&1 | grep -i appcoreopc

This will no run opencode but will load any issue related to your plugin. As you can see here, the plugin successfully loaded.







Comments

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20