Skip to content

Plugin

Plugin คือส่วนขยายที่ช่วยให้คุณสามารถเพิ่มความสามารถให้กับ Opencode ได้ โดยการเชื่อมโยงกับเหตุการณ์ (events) ต่างๆ และปรับแต่งลักษณะการทำงานของมัน

วิธีการติดตั้ง Plugin

Section titled “วิธีการติดตั้ง Plugin”

มี 2 วิธีหลักในการโหลด Plugin:

จากไฟล์ในเครื่อง

Section titled “จากไฟล์ในเครื่อง”

วางไฟล์ JavaScript หรือ TypeScript ในไดเร็กทอรี Plugin:

  • .opencode/plugins/ - Plugin ระดับโครงการ
  • ~/.config/opencode/plugins/ - Plugin ระดับ Global

ระบุแพ็คเกจ npm ในไฟล์ปรับแต่งของคุณ:

{
"$schema": "https://opencode.ai/config.json",
"plugin": [
"opencode-helicone-session",
"opencode-wakatime",
"@my-org/custom-plugin"
]
}

Hooks คือจุดเชื่อมต่อที่ช่วยให้ Plugin สามารถ “แทรก” ตัวเองเข้าไปในกระบวนการทำงานของ Opencode ได้ ทำให้สามารถ:

  • ตรวจสอบการทำงานก่อน/หลังที่เกิดขึ้น
  • แก้ไขข้อมูลระหว่างทาง
  • ส่งการแจ้งเตือนเมื่อมีเหตุการณ์บางอย่างเกิดขึ้น

ตัวอย่าง Hooks ที่ใช้บ่อย

Section titled “ตัวอย่าง Hooks ที่ใช้บ่อย”
Hookคำอธิบาย
session.createdเมื่อสร้าง session ใหม่
session.idleเมื่อ session ว่างาน
tool.execute.beforeก่อนเรียกใช้ tool
tool.execute.afterหลังเรียกใช้ tool
shell.envก่อนรัน shell command
file.editedเมื่อแก้ไขไฟล์

ตัวอย่างการสร้าง Plugin

Section titled “ตัวอย่างการสร้าง Plugin”

1. ส่ง Notification เมื่อสร้าง Session สำเร็จ

Section titled “1. ส่ง Notification เมื่อสร้าง Session สำเร็จ”
.opencode/plugins/notification.js
export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
return {
event: async ({ event }) => {
if (event.type === "session.created") {
await $`osascript -e 'display notification "Session started!" with title "opencode"'`
}
},
}
}

2. ป้องกันการอ่านไฟล์ .env

Section titled “2. ป้องกันการอ่านไฟล์ .env”
.opencode/plugins/env-protection.js
export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
return {
"tool.execute.before": async (input, output) => {
if (input.tool === "read" && output.args.filePath.includes(".env")) {
throw new Error("Do not read .env files")
}
},
}
}
Avatar blackCat
การป้องกัน .env เป็นสิ่งสำคัญมาก! ป้องกันไม่ให้ AI อ่าน secrets ของคุณ
.opencode/plugins/inject-env.js
export const InjectEnvPlugin = async () => {
return {
"shell.env": async (input, output) => {
output.env.MY_API_KEY = "secret"
output.env.PROJECT_ROOT = input.cwd
},
}
}

โครงสร้างพื้นฐานของ Plugin

Section titled “โครงสร้างพื้นฐานของ Plugin”

Plugin คือ JavaScript/TypeScript module ที่ส่งออก function อย่างน้อยหนึ่งรายการ:

.opencode/plugins/example.js
export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
console.log("Plugin initialized!")
return {
// Hook implementations go here
}
}

พารามิเตอร์ที่ได้รับ

Section titled “พารามิเตอร์ที่ได้รับ”
  • project: ข้อมูลโครงการปัจจุบัน
  • directory: ไดเร็กทอรีการทำงานปัจจุบัน
  • worktree: เส้นทางเวิร์กทรีคอมไพล์
  • client: ไคลเอนต์ Opencode SDK
  • $: shell API ของ Bun
import type { Plugin } from "@opencode-ai/plugin"
export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
return {
// Type-safe hook implementations
}
}

Plugin และ Hooks เป็นเครื่องมือที่ทรงพลังมากสำหรับการปรับแต่ง Opencode ให้เหมาะกับ workflow ของคุณ

Avatar whiteCat
เมื่อเข้าใจ Plugin แล้ว คุณจะสามารถปรับ Opencode ให้เป็นเครื่องมือที่เหมาะกับคุณได้เลย!

ทดสอบความเข้าใจเกี่ยวกับ Plugin

ข้อ 1 / 50%

Plugin ใน Opencode ทำหน้าที่อะไร?