⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/opencode/src/tool/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export namespace ToolRegistry {
description: def.description,
execute: async (args, ctx) => {
const result = await def.execute(args as any, ctx)

if (typeof result !== 'string') {
return result
}

const out = await Truncate.output(result, {}, initCtx?.agent)
return {
title: "",
Expand Down
29 changes: 22 additions & 7 deletions packages/plugin/src/tool.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { z } from "zod"
import type { FilePart } from "@opencode-ai/sdk"

export type ToolContext = {
export type Metadata = {
[key: string]: any
}

export type ToolContext<M extends Metadata = Metadata> = {
sessionID: string
messageID: string
agent: string
abort: AbortSignal
metadata(input: { title?: string; metadata?: { [key: string]: any } }): void
ask(input: AskInput): Promise<void>
callID?: string
extra?: M
metadata(input: { title?: string; metadata?: M }): void
ask(input: AskInput<M>): Promise<void>
}

type AskInput = {
export type AskInput<M extends Metadata = Metadata> = {
permission: string
patterns: string[]
always: string[]
metadata: { [key: string]: any }
metadata: M
}

export type ExecuteResult<M extends Metadata = Metadata> = {
title: string
metadata: M
output: string
attachments?: FilePart[]
}

export function tool<Args extends z.ZodRawShape>(input: {
export function tool<Args extends z.ZodRawShape, M extends Metadata = Metadata>(input: {
description: string
args: Args
execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext): Promise<string>
execute(args: z.infer<z.ZodObject<Args>>, context: ToolContext<M>): Promise<string | ExecuteResult<M>>
formatValidationError?(error: z.ZodError): string
}) {
return input
}
Expand Down