Plugin
Plugin คืออะไร
Section titled “Plugin คืออะไร”Plugin คือส่วนขยายที่ช่วยให้คุณสามารถเพิ่มความสามารถให้กับ Opencode ได้ โดยการเชื่อมโยงกับเหตุการณ์ (events) ต่างๆ และปรับแต่งลักษณะการทำงานของมัน
วิธีการติดตั้ง Plugin
Section titled “วิธีการติดตั้ง Plugin”มี 2 วิธีหลักในการโหลด Plugin:
จากไฟล์ในเครื่อง
Section titled “จากไฟล์ในเครื่อง”วางไฟล์ JavaScript หรือ TypeScript ในไดเร็กทอรี Plugin:
.opencode/plugins/- Plugin ระดับโครงการ~/.config/opencode/plugins/- Plugin ระดับ Global
จาก npm
Section titled “จาก npm”ระบุแพ็คเกจ npm ในไฟล์ปรับแต่งของคุณ:
{ "$schema": "https://opencode.ai/config.json", "plugin": [ "opencode-helicone-session", "opencode-wakatime", "@my-org/custom-plugin" ]}Hooks คืออะไร
Section titled “Hooks คืออะไร”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 สำเร็จ”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”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") } }, }} การป้องกัน .env เป็นสิ่งสำคัญมาก! ป้องกันไม่ให้ AI อ่าน secrets ของคุณ
3. ฉีด Environment Variables
Section titled “3. ฉีด Environment Variables”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 อย่างน้อยหนึ่งรายการ:
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
รองรับ TypeScript
Section titled “รองรับ TypeScript”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 ของคุณ
เมื่อเข้าใจ Plugin แล้ว คุณจะสามารถปรับ Opencode ให้เป็นเครื่องมือที่เหมาะกับคุณได้เลย!
ทดสอบความเข้าใจเกี่ยวกับ Plugin
ข้อ 1 / 50%