Skip to content

fbosch/opencode-toon-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

opencode-toon-plugin logo

opencode-toon-plugin

npm version

Compact large JSON tool output in OpenCode with TOON.

The plugin acts as a middle layer: it stores shorter TOON in the context window instead of full JSON, while preserving JSON compatibility when forwarded output is used by later bash commands.

It only replaces the original output when the TOON result is smaller.

Strongly tabular JSON can see roughly 40-60% token savings, with smaller wins on mixed or nested data.

avg tokens

JSON  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100%
TOON  |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ......            | 40-60% less

Install

Add it to your OpenCode config:

{
  "plugin": ["opencode-toon-plugin"]
}

Behavior

The plugin sits between tool output and the agent context. The agent sees compact TOON instead of large JSON, but commands can still receive JSON when that output is forwarded later.

Output compression

Only bash output is handled by default. Output is changed only when all checks pass:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ bash output β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       v
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     no
β”‚ 256+ chars and  │─────────> keep original output
β”‚ valid JSON?     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ yes
         v
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     no
β”‚ TOON is shorter │─────────> keep original output
β”‚ than JSON?      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ yes
         v
  show TOON in context
  remember original JSON in memory

Forwarded output

Before a later bash command runs, the plugin looks for TOON payloads inside quoted strings and heredocs. It restores JSON in two cases:

  • Exact match: the TOON text matches output remembered in the current OpenCode process.
  • Valid TOON fallback: no memory match exists, but the payload can be decoded as TOON.

Edited or invalid TOON is left unchanged.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ bash command contains TOON payload β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                v
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    yes
       β”‚ remembered exact │────────> restore original JSON
       β”‚ TOON output?     β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚ no
                v
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    yes
       β”‚ valid TOON       │────────> decode to JSON
       β”‚ payload?         β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚ no
                v
          run command unchanged

Example

Prompt:

Run `gh api repos/octocat/Hello-World/issues` and show me the result.
Without TOON With TOON
[
  {
    "id": 1,
    "title": "Found a bug",
    "state": "open",
    "labels": [
      { "name": "bug" }
    ],
    "assignee": "monalisa"
  },
  {
    "id": 2,
    "title": "Docs update",
    "state": "open",
    "labels": [
      { "name": "docs" }
    ],
    "assignee": "hubot"
  },
  {
    "id": 3,
    "title": "Ship release",
    "state": "closed",
    "labels": [
      { "name": "release" }
    ],
    "assignee": "octocat"
  }
]
[3]:
  - id: 1
    title: Found a bug
    state: open
    labels[1]{name}:
      bug
    assignee: monalisa
  - id: 2
    title: Docs update
    state: open
    labels[1]{name}:
      docs
    assignee: hubot
  - id: 3
    title: Ship release
    state: closed
    labels[1]{name}:
      release
    assignee: octocat

Configuration

Override eligible tools with OPENCODE_TOON_PLUGIN_TOOLS:

OPENCODE_TOON_PLUGIN_TOOLS=bash,rtk

Development

bun install
bun run test
bun run typecheck
bun run bench

License

MIT

About

πŸŽ’ Compact large JSON tool output in OpenCode with TOON.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors