mirror of
https://github.com/anomalyco/opencode.git
synced 2026-02-01 22:48:16 +00:00
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import z from "zod"
|
|
import type { MessageV2 } from "../session/message-v2"
|
|
|
|
export namespace Tool {
|
|
interface Metadata {
|
|
[key: string]: any
|
|
}
|
|
|
|
export type Context<M extends Metadata = Metadata> = {
|
|
sessionID: string
|
|
messageID: string
|
|
agent: string
|
|
abort: AbortSignal
|
|
callID?: string
|
|
extra?: { [key: string]: any }
|
|
metadata(input: { title?: string; metadata?: M }): void
|
|
}
|
|
export interface Info<Parameters extends z.ZodType = z.ZodType, M extends Metadata = Metadata> {
|
|
id: string
|
|
init: () => Promise<{
|
|
description: string
|
|
parameters: Parameters
|
|
execute(
|
|
args: z.infer<Parameters>,
|
|
ctx: Context,
|
|
): Promise<{
|
|
title: string
|
|
metadata: M
|
|
output: string
|
|
attachments?: MessageV2.FilePart[]
|
|
}>
|
|
formatValidationError?(error: z.ZodError): string
|
|
}>
|
|
}
|
|
|
|
export type InferParameters<T extends Info> = T extends Info<infer P> ? z.infer<P> : never
|
|
export type InferMetadata<T extends Info> = T extends Info<any, infer M> ? M : never
|
|
|
|
export function define<Parameters extends z.ZodType, Result extends Metadata>(
|
|
id: string,
|
|
init: Info<Parameters, Result>["init"] | Awaited<ReturnType<Info<Parameters, Result>["init"]>>,
|
|
): Info<Parameters, Result> {
|
|
return {
|
|
id,
|
|
init: async () => {
|
|
const toolInfo = init instanceof Function ? await init() : init
|
|
const execute = toolInfo.execute
|
|
toolInfo.execute = (args, ctx) => {
|
|
try {
|
|
toolInfo.parameters.parse(args)
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError && toolInfo.formatValidationError) {
|
|
throw new Error(toolInfo.formatValidationError(error))
|
|
}
|
|
throw error
|
|
}
|
|
return execute(args, ctx)
|
|
}
|
|
return toolInfo
|
|
},
|
|
}
|
|
}
|
|
}
|