Skip to content

Instantly share code, notes, and snippets.

@cameroncking
Last active February 13, 2026 04:46
Show Gist options
  • Select an option

  • Save cameroncking/b258b65212bf4d3f1b370483664b68bc to your computer and use it in GitHub Desktop.

Select an option

Save cameroncking/b258b65212bf4d3f1b370483664b68bc to your computer and use it in GitHub Desktop.
OpenCode plugin to provide custom request instructions
// .opencode/plugins/request-instructions.js
// replaces requestInstructions (model-specific coding instructions) with
// the contents of .opencode/REQUEST_INSTRUCTIONS.md
//
// When using OpenCode for non-coding tasks (such as creative writing or
// knowledge management), this reduces coding-related context poisoning
// when paired with a general purpose prompt.
//
import fs from "fs/promises"
import path from "path"
export const RequestInstructions = async ({ worktree, client }) => {
const filePath = path.join(worktree, ".opencode", "REQUEST_INSTRUCTIONS.md")
/** @type {{ mtimeMs: number, text: string } | null} */
let cache = null
async function load() {
try {
const stat = await fs.stat(filePath)
if (cache && cache.mtimeMs === stat.mtimeMs) return cache.text
const text = await fs.readFile(filePath, "utf8")
cache = { mtimeMs: stat.mtimeMs, text }
return text
} catch (err) {
// If the file doesn't exist, treat it as an intentionally empty override.
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") return ""
try {
const message = err instanceof Error ? err.message : String(err)
await client.app.log({
body: {
service: "request-instructions",
level: "error",
message: `Failed to read ${filePath}: ${message}`,
},
})
} catch {
// ignore
}
return null
}
}
return {
"chat.params": async (_input, output) => {
// Only override when OpenCode is already using request-level instructions
// (e.g. OpenAI OAuth/Codex sessions). Avoid injecting an unsupported field
// into providers that don't accept it.
if (!output?.options) return
if (typeof output.options.instructions !== "string") return
const text = await load()
if (typeof text !== "string") return
output.options.instructions = text
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment