tool.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import type { StandardSchemaV1 } from "@standard-schema/spec"
  2. export namespace Tool {
  3. interface Metadata {
  4. [key: string]: any
  5. }
  6. export type Context<M extends Metadata = Metadata> = {
  7. sessionID: string
  8. messageID: string
  9. callID?: string
  10. abort: AbortSignal
  11. metadata(input: { title?: string; metadata?: M }): void
  12. }
  13. export interface Info<Parameters extends StandardSchemaV1 = StandardSchemaV1, M extends Metadata = Metadata> {
  14. id: string
  15. init: () => Promise<{
  16. description: string
  17. parameters: Parameters
  18. execute(
  19. args: StandardSchemaV1.InferOutput<Parameters>,
  20. ctx: Context,
  21. ): Promise<{
  22. title: string
  23. metadata: M
  24. output: string
  25. }>
  26. }>
  27. }
  28. export function define<Parameters extends StandardSchemaV1, Result extends Metadata>(
  29. id: string,
  30. init: Info<Parameters, Result>["init"] | Awaited<ReturnType<Info<Parameters, Result>["init"]>>,
  31. ): Info<Parameters, Result> {
  32. return {
  33. id,
  34. init: async () => {
  35. if (init instanceof Function) return init()
  36. return init
  37. },
  38. }
  39. }
  40. }