Skip to main content

OPENCODE_SHIM_TS

Constant OPENCODE_SHIM_TS 

Source
pub const OPENCODE_SHIM_TS: &str = "import type { Plugin } from \"@opencode-ai/plugin\"\nimport { spawnSync } from \"node:child_process\"\n\nconst AIGUARD = process.env.AIGUARD_BIN ?? \"aiguard\"\nconst call = (stage: string, payload: unknown) => {\n  const r = spawnSync(AIGUARD, [\"hook\", \"opencode\", stage], {\n    input: JSON.stringify(payload), encoding: \"utf8\", timeout: 5000,\n  })\n  return { code: r.status ?? 0, stdout: r.stdout, stderr: r.stderr }\n}\n\nexport const Aiguard: Plugin = async (ctx) => ({\n  \"tool.execute.before\": async (input, output) => {\n    const r = call(\"pre\", { tool: input.tool, args: output.args, project: ctx.project })\n    if (r.code === 2) throw new Error(r.stderr || \"blocked by aiguard\")\n    if (r.stdout) {\n      const o = JSON.parse(r.stdout)\n      if (o.updatedInput) Object.assign(output.args, o.updatedInput)\n    }\n  },\n  \"tool.execute.after\": async (input, output) => {\n    const r = call(\"post\", { tool: input.tool, args: output.args ?? null,\n      output: output.output, metadata: output.metadata, project: ctx.project })\n    if (r.stdout) {\n      const o = JSON.parse(r.stdout)\n      if (o.updatedOutput) output.output = o.updatedOutput\n    }\n  },\n  \"permission.ask\": async (perm, out) => {\n    const r = call(\"permission\", { perm })\n    if (r.code === 0 && r.stdout) {\n      const o = JSON.parse(r.stdout)\n      if (o.status) out.status = o.status\n    }\n  },\n})\n";