Skip to content

feat: add opencode plugin support - #46

Open
sfc-gh-vjatania wants to merge 1 commit into
Snowflake-Labs:mainfrom
sfc-gh-vjatania:feat/opencode-plugin
Open

feat: add opencode plugin support#46
sfc-gh-vjatania wants to merge 1 commit into
Snowflake-Labs:mainfrom
sfc-gh-vjatania:feat/opencode-plugin

Conversation

@sfc-gh-vjatania

Copy link
Copy Markdown

Summary

  • Adds a native opencode plugin that routes Snowflake prompts to Cortex Code CLI, bringing opencode to parity with the existing Claude Code and Codex integrations
  • New cortex_run custom tool (TypeScript, @opencode-ai/plugin) calls execute_cortex.py in --codex mode via Bun shell — reusing all existing Python router scripts unchanged
  • Hybrid routing: LLM-driven via a rules file (opencode has no UserPromptSubmit hook) + a tool.execute.before hook that intercepts direct snow sql/snowsql bash calls
  • bash install.sh --with-opencode installs globally to ~/.config/opencode/plugins/ and ~/.config/opencode/rules/

Files changed

File Change
plugins/cortex-code/.opencode/plugins/snowflake-cortex.ts Newcortex_run tool + tool.execute.before hook
plugins/cortex-code/.opencode/rules/snowflake.md New — LLM routing rules (replaces UserPromptSubmit hook)
plugins/cortex-code/scripts/router/execute_cortex.py Add OPENCODE env var detection → CORTEX_CODE_ENTRYPOINT="OpenCode Plugin"
install.sh Add --with-opencode flag + install_opencode_plugin() function
tests/run-tests.sh New "OpenCode plugin" section with 8 structural checks
plugins/cortex-code/README.md Add opencode install instructions
README.md Add opencode to quick-start and installer flags table

Architecture note

opencode has no pre-LLM hook equivalent to Claude Code's UserPromptSubmit, so the keyword filter cannot intercept prompts before the LLM sees them. The routing is instead:

  1. Rules file (snowflake.md) — loaded globally, instructs the LLM when to call cortex_run using the same keyword set as prompt_filter.py
  2. tool.execute.before hook — hard-blocks direct snow sql / snowsql bash calls, redirecting to cortex_run (hard enforcement layer)

In practice the LLM reliably routes to cortex_run given the rules file. The hook catches the edge case where it tries to use Snowflake CLI directly.

Test plan

  • bash tests/run-tests.sh — all 37 structural checks pass (8 new opencode checks included)
  • bash tests/run-tests.sh --unit — envelope policy + plugin unit tests pass
  • Manual: bash install.sh --with-opencode → confirm files land in ~/.config/opencode/plugins/ and ~/.config/opencode/rules/
  • Manual: open opencode in a project, ask "show me my Snowflake warehouses" → cortex_run is called
  • Manual: attempt bash with snow sql -q "SELECT 1" → hook blocks and redirects

.... Generated with Cortex Code

Adds a native opencode plugin that routes Snowflake prompts to Cortex
Code CLI, bringing opencode to parity with the existing Claude Code and
Codex integrations.

New files:
- plugins/cortex-code/.opencode/plugins/snowflake-cortex.ts
  Custom tool (cortex_run) using @opencode-ai/plugin's tool() helper.
  Calls execute_cortex.py via Bun shell in --codex mode.
  Also exports a tool.execute.before hook that intercepts direct
  snow sql / snowsql bash calls and redirects them through cortex.
  Script dir resolved via multi-location fallback (repo dev vs installed).

- plugins/cortex-code/.opencode/rules/snowflake.md
  LLM routing rules that replace the UserPromptSubmit hook (opencode
  has no pre-LLM hook equivalent). Mirrors prompt_filter.py keyword
  logic. Instructs the LLM to call cortex_run for Snowflake work.

Modified files:
- execute_cortex.py: detect OPENCODE env var, set
  CORTEX_CODE_ENTRYPOINT="OpenCode Plugin" for analytics/tracking.
- install.sh: --with-opencode flag; copies plugin TS + router scripts
  to ~/.config/opencode/plugins/ and rules to ~/.config/opencode/rules/.
- tests/run-tests.sh: new "OpenCode plugin" section with 8 checks.
- README.md + plugins/cortex-code/README.md: opencode quick-start and
  install instructions.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>

@sfc-gh-ddesai sfc-gh-ddesai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!

Thanks for your contribution. Overall it's a solid addition; opencode parity makes sense and the architecture (rules file + hook) is pragmatic given no UserPromptSubmit equivalent.

Questions / Concerns:

  • Bun dependency — snowflake-cortex.ts uses Bun.$ shell syntax. Does opencode always run on Bun? If someone has opencode on Node, this breaks silently. Should document or add a Node fallback.

  • Router script duplication — install.sh --with-opencode copies entire scripts/router/ to ~/.config/opencode/plugins/cortex-code-router/. Updates require re-running installer. No auto-update. Worth noting in docs.

  • No functional tests for TS plugin — 8 structural checks verify files exist, but nothing tests actual cortex_run execution or hook behavior.

  • Type assertion in hook — (output as { args?: { command?: string } }).args?.command is a blind cast. If opencode changes hook shapes, this silently fails.

  • Version sync — PR doesn't bump version. Since v3.3.1 just landed, rebase and bump needed.

  • Hook UX — tool.execute.before throws an error on direct snow sql calls. Would silent re-route (calling cortex_run internally) be better UX than erroring?

Cheers,
Dash

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.

2 participants