2024-05-23 23:19:38 +00:00
|
|
|
import './symbol-polyfill.js'
|
2024-05-21 13:52:06 +00:00
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
import type * as z from 'zod'
|
2024-05-21 13:52:06 +00:00
|
|
|
|
|
|
|
import type * as types from './types.js'
|
2024-05-24 23:55:26 +00:00
|
|
|
import { createAIFunction } from './ai-function.js'
|
|
|
|
import { AIFunctionSet } from './ai-function-set.js'
|
|
|
|
import { AIToolSet } from './ai-tool-set.js'
|
|
|
|
import { assert } from './utils.js'
|
2024-05-21 13:52:06 +00:00
|
|
|
|
|
|
|
export abstract class AIToolsProvider {
|
2024-05-24 23:55:26 +00:00
|
|
|
private _tools?: AIToolSet
|
|
|
|
private _functions?: AIFunctionSet
|
2024-05-21 13:52:06 +00:00
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
get tools(): AIToolSet {
|
2024-05-21 13:52:06 +00:00
|
|
|
if (!this._tools) {
|
2024-05-24 23:55:26 +00:00
|
|
|
this._tools = AIToolSet.fromAIFunctionSet(this.functions)
|
2024-05-21 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._tools
|
|
|
|
}
|
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
get functions(): AIFunctionSet {
|
2024-05-21 13:52:06 +00:00
|
|
|
if (!this._functions) {
|
2024-05-22 08:11:36 +00:00
|
|
|
const metadata = this.constructor[Symbol.metadata]
|
2024-06-02 00:34:25 +00:00
|
|
|
assert(metadata)
|
2024-05-22 08:11:36 +00:00
|
|
|
const invocables = (metadata?.invocables as Invocable[]) ?? []
|
2024-06-02 00:34:25 +00:00
|
|
|
console.log({ metadata, invocables })
|
2024-05-22 08:11:36 +00:00
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
const aiFunctions = invocables.map((invocable) => {
|
|
|
|
const impl = (this as any)[invocable.methodName]?.bind(this)
|
|
|
|
assert(impl)
|
|
|
|
|
|
|
|
return createAIFunction(invocable, impl)
|
|
|
|
})
|
2024-05-22 08:11:36 +00:00
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
this._functions = new AIFunctionSet(aiFunctions)
|
2024-05-21 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._functions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 08:11:36 +00:00
|
|
|
export interface Invocable {
|
|
|
|
name: string
|
|
|
|
description?: string
|
2024-05-24 23:55:26 +00:00
|
|
|
inputSchema: z.AnyZodObject
|
|
|
|
methodName: string
|
2024-05-21 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 08:11:36 +00:00
|
|
|
export function aiFunction<
|
|
|
|
This,
|
2024-05-24 23:55:26 +00:00
|
|
|
InputSchema extends z.SomeZodObject,
|
|
|
|
OptionalArgs extends Array<undefined>,
|
|
|
|
Return extends types.MaybePromise<any>
|
2024-05-22 08:11:36 +00:00
|
|
|
>({
|
2024-05-21 13:52:06 +00:00
|
|
|
name,
|
|
|
|
description,
|
|
|
|
inputSchema
|
|
|
|
}: {
|
|
|
|
name?: string
|
|
|
|
description?: string
|
2024-05-24 23:55:26 +00:00
|
|
|
inputSchema: InputSchema
|
2024-05-21 13:52:06 +00:00
|
|
|
}) {
|
2024-05-22 08:11:36 +00:00
|
|
|
return (
|
2024-05-24 23:55:26 +00:00
|
|
|
_targetMethod: (
|
|
|
|
this: This,
|
|
|
|
input: z.infer<InputSchema>,
|
|
|
|
...optionalArgs: OptionalArgs
|
|
|
|
) => Return,
|
2024-05-22 08:11:36 +00:00
|
|
|
context: ClassMethodDecoratorContext<
|
|
|
|
This,
|
2024-05-24 23:55:26 +00:00
|
|
|
(
|
|
|
|
this: This,
|
|
|
|
input: z.infer<InputSchema>,
|
|
|
|
...optionalArgs: OptionalArgs
|
|
|
|
) => Return
|
2024-05-23 23:19:38 +00:00
|
|
|
>
|
2024-05-22 08:11:36 +00:00
|
|
|
) => {
|
|
|
|
const methodName = String(context.name)
|
|
|
|
if (!context.metadata.invocables) {
|
|
|
|
context.metadata.invocables = []
|
|
|
|
}
|
2024-05-23 23:19:38 +00:00
|
|
|
;(context.metadata.invocables as Invocable[]).push({
|
2024-05-22 08:11:36 +00:00
|
|
|
name: name ?? methodName,
|
2024-05-21 13:52:06 +00:00
|
|
|
description,
|
2024-05-22 08:11:36 +00:00
|
|
|
inputSchema,
|
2024-05-24 23:55:26 +00:00
|
|
|
methodName
|
2024-05-21 13:52:06 +00:00
|
|
|
})
|
2024-06-02 00:34:25 +00:00
|
|
|
console.log({
|
|
|
|
name,
|
|
|
|
methodName,
|
|
|
|
context
|
|
|
|
})
|
2024-05-21 13:52:06 +00:00
|
|
|
|
2024-05-24 23:55:26 +00:00
|
|
|
// context.addInitializer(function () {
|
|
|
|
// ;(this as any)[methodName] = (this as any)[methodName].bind(this)
|
|
|
|
// })
|
2024-05-22 08:11:36 +00:00
|
|
|
}
|
2024-05-21 13:52:06 +00:00
|
|
|
}
|