Skip to content

feat: add OpenCode v2 plugin API entry (src/index.v2.ts) - #1

Open
famewolf wants to merge 3 commits into
ai-space-lab:mainfrom
famewolf:feat/v2-plugin-api
Open

famewolf wants to merge 3 commits into
ai-space-lab:mainfrom
famewolf:feat/v2-plugin-api

Conversation

@famewolf

Copy link
Copy Markdown

opencode-token-optimizer 0.1.0 is a v1 plugin: it builds a v1 hooks object (hooks["tool.execute.before"] = ...) and relies on the v1 plugin loader. On OpenCode 2.0.x that plugin shape doesn't register, so the three tools (track_task, check_duplicate, validate_task_scope) and the pre-computation system-prompt guidance don't load.

What this PR does

Adds src/index.v2.ts — a port to the v2 plugin API ({id, setup(ctx)}):

  • Tools registered via ctx.tool.transform(editor => editor.add({...})).
  • System-prompt pre-computation guidance injected via ctx.session.hook("context").

tsc compiles it to dist/index.v2.js alongside the existing build; the default v1 entry is untouched.

On 2.0.16 (where this is the live, in-production reference implementation):

  • track_task(taskId, description) — in-memory registry; returns {duplicate, existingTask} so a second delegation of the same work is flagged.
  • check_duplicate(query) — keyword-overlap (significant-word Jaccard, threshold 0.5) against the registry; returns {isDuplicate, matchedTask}.
  • validate_task_scope(prompt, maxConcerns=1) — splits the prompt into concerns (file-path / function-call / capitalized-segment heuristics); if more than maxConcerns, returns {valid:false, concerns, suggestion:"Split into N separate tasks…"}.

Notes for the maintainer

  • The registry is in-memory per process; persistence across sessions would be a follow-up (file-backed store). Current v1 behavior is also in-memory, so this matches.
  • Guards degrade gracefully: if ctx.tool.transform or ctx.session.hook is absent (pre-2.0 host), the tools simply aren't registered (no crash).

— famewolf

OpenCode v2 validates the context event's `system` array against
LLM.SystemPart, which is { type: "text", text: string }. This pushed the
PRE_COMPUTE_GUIDANCE template literal directly, which fails schema
validation. Once such an entry lands at system[4] or later, every request
from the session fails, so the failure looks like a dead session rather
than a bad plugin.

Refs anomalyco/opencode#51269
The v2 entry had no test coverage at all, which is how the bare-string
push reached main. These four cases drive setup() with a mock context and
assert that the "context" hook appends a SystemPart rather than a string,
that the guidance text survives, that existing entries are left alone,
and that an event without a system array is not mutated.

Verified to fail against the pre-fix code (Expected "object", Received
"string") and pass after it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant