한국어
문서
사용자 정의 도구

사용자 정의 도구

opencode에서 LLM이 호출할 수 있는 도구를 만드세요.

사용자 정의 도구는 대화 중에 LLM이 호출할 수 있는 함수입니다. opencode의 기본 제공 도구read, write, bash와 함께 작동합니다.

도구 만들기

도구는 TypeScript 또는 JavaScript 파일로 정의됩니다. 그러나 도구 정의는 어떤 언어로든 작성된 스크립트를 호출할 수 있습니다 — TypeScript 또는 JavaScript는 도구 정의 자체에만 사용됩니다.

위치

다음 위치에 정의할 수 있습니다:

  • 프로젝트의 .opencode/tools/ 디렉토리에 배치하여 로컬로 정의.
  • 또는 ~/.config/opencode/tools/에 배치하여 전역으로 정의.

구조

도구를 만드는 가장 쉬운 방법은 타입 안전성과 유효성 검사를 제공하는 tool() 헬퍼를 사용하는 것입니다.

.opencode/tools/database.ts
import { tool } from "@opencode-ai/plugin"
 
export default tool({
  description: "Query the project database",
  args: {
    query: tool.schema.string().describe("SQL query to execute"),
  },
  async execute(args) {
    // Your database logic here
    return `Executed query: ${args.query}`
  },
})

파일 이름도구 이름이 됩니다. 위 예시는 database 도구를 생성합니다.

파일당 여러 도구

하나의 파일에서 여러 도구를 내보낼 수도 있습니다. 각 내보내기는 <filename>_<exportname> 이름의 별도 도구가 됩니다:

.opencode/tools/math.ts
import { tool } from "@opencode-ai/plugin"
 
export const add = tool({
  description: "Add two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a + args.b
  },
})
 
export const multiply = tool({
  description: "Multiply two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a * args.b
  },
})

이렇게 하면 두 개의 도구가 생성됩니다: math_addmath_multiply.

기본 제공 도구와의 이름 충돌

사용자 정의 도구는 도구 이름으로 식별됩니다. 사용자 정의 도구가 기본 제공 도구와 같은 이름을 사용하면 사용자 정의 도구가 우선합니다.

예를 들어, 이 파일은 기본 제공 bash 도구를 대체합니다:

.opencode/tools/bash.ts
import { tool } from "@opencode-ai/plugin"
 
export default tool({
  description: "Restricted bash wrapper",
  args: {
    command: tool.schema.string(),
  },
  async execute(args) {
    return `blocked: ${args.command}`
  },
})

의도적으로 기본 제공 도구를 대체하려는 것이 아니라면 고유한 이름을 사용하세요. 기본 제공 도구를 비활성화하되 재정의하지 않으려면 권한을 사용하세요.

인수

tool.schema를 사용하여 인수 타입을 정의할 수 있습니다. 이는 Zod (opens in a new tab)입니다.

args: {
  query: tool.schema.string().describe("SQL query to execute")
}

Zod (opens in a new tab)를 직접 가져와서 일반 객체를 반환할 수도 있습니다:

import { z } from "zod"
 
export default {
  description: "Tool description",
  args: {
    param: z.string().describe("Parameter description"),
  },
  async execute(args, context) {
    // Tool implementation
    return "result"
  },
}

컨텍스트

도구는 현재 세션에 대한 컨텍스트를 받습니다:

.opencode/tools/project.ts
import { tool } from "@opencode-ai/plugin"
 
export default tool({
  description: "Get project information",
  args: {},
  async execute(args, context) {
    // Access context information
    const { agent, sessionID, messageID, directory, worktree } = context
    return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
  },
})

세션 작업 디렉토리에는 context.directory를 사용하세요. Git worktree 루트에는 context.worktree를 사용하세요.

예시

Python으로 도구 작성

어떤 언어로든 도구를 작성할 수 있습니다. Python을 사용하여 두 숫자를 더하는 예시입니다.

먼저 Python 스크립트로 도구를 만드세요:

.opencode/tools/add.py
import sys
 
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)

그런 다음 이를 호출하는 도구 정의를 만드세요:

.opencode/tools/python-add.ts
import { tool } from "@opencode-ai/plugin"
import path from "path"
 
export default tool({
  description: "Add two numbers using Python",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args, context) {
    const script = path.join(context.worktree, ".opencode/tools/add.py")
    const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
    return result.trim()
  },
})

여기서는 Python 스크립트를 실행하기 위해 Bun.$ (opens in a new tab) 유틸리티를 사용합니다.