From b3e62b2e4c42bf7483750f3e14df4af97bc8536d Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 23 Jun 2023 19:19:45 -0400 Subject: [PATCH] feat: remove hook names and order changing --- src/task.ts | 90 ++++++----------------------------------------------- 1 file changed, 10 insertions(+), 80 deletions(-) diff --git a/src/task.ts b/src/task.ts index 84b011b..0732b9a 100644 --- a/src/task.ts +++ b/src/task.ts @@ -38,12 +38,10 @@ export abstract class BaseTask< private _preHooks: Array<{ hook: types.TaskBeforeCallHook priority: number - name: string }> = [] private _postHooks: Array<{ hook: types.TaskAfterCallHook priority: number - name: string }> = [] constructor(options: types.BaseTaskOptions = {}) { @@ -95,14 +93,13 @@ export abstract class BaseTask< * Adds a hook to be called before the task is invoked. * * @param hook - function to be called before the task is invoked - * @param options - options for the hook; `priority` is used to determine the order in which hooks are called, with higher priority hooks being called first, and `name` is used to identify the hook + * @param priority - priority of the hook; higher priority hooks are called first */ public addBeforeCallHook( hook: types.TaskBeforeCallHook, - { priority = 0, name }: { priority?: number; name?: string } = {} + priority = 0 ): this { - const hookName = name ?? `preHook_${this._preHooks.length}` - this._preHooks.push({ hook, priority, name: hookName }) + this._preHooks.push({ hook, priority }) this._preHooks.sort((a, b) => b.priority - a.priority) // two elements that compare equal will remain in their original order (>= ECMAScript 2019) return this } @@ -111,81 +108,17 @@ export abstract class BaseTask< * Adds a hook to be called after the task is invoked. * * @param hook - function to be called after the task is invoked - * @param options - options for the hook; `priority` is used to determine the order in which hooks are called, with higher priority hooks being called first, and `name` is used to identify the hook + * @param priority - priority of the hook; higher priority hooks are called first */ public addAfterCallHook( hook: types.TaskAfterCallHook, - { priority = 0, name }: { priority?: number; name?: string } = {} + priority = 0 ): this { - const hookName = name ?? `postHook_${this._postHooks.length}` - this._postHooks.push({ hook, priority, name: hookName }) + this._postHooks.push({ hook, priority }) this._postHooks.sort((a, b) => b.priority - a.priority) // two elements that compare equal will remain in their original order (>= ECMAScript 2019) return this } - /** - * Changes the priority of a before call hook. - * - * @param hookType - `before` - * @param hookOrName - hook or the name of the hook to change the priority of - * @param newPriority - new priority of the hook - */ - public changeHookPriority( - hookType: 'before', - hookOrName: types.TaskBeforeCallHook | string, - newPriority: number - ): this - - /** - * Changes the priority of a after call hook. - * - * @param hookType - `after` - * @param hookOrName - hook or the name of the hook to change the priority of - * @param newPriority - new priority of the hook - */ - public changeHookPriority( - hookType: 'after', - hookOrName: types.TaskAfterCallHook | string, - newPriority: number - ): this - - public changeHookPriority( - hookType: 'before' | 'after', - hookOrName: - | types.TaskBeforeCallHook - | types.TaskAfterCallHook - | string, - newPriority: number - ): this { - const hooks = hookType === 'before' ? this._preHooks : this._postHooks - - let found = false - if (typeof hookOrName === 'string') { - for (const hookObj of hooks) { - if (hookObj.name === hookOrName) { - found = true - hookObj.priority = newPriority - } - } - } else { - for (const hookObj of hooks) { - if (hookObj.hook === hookOrName) { - found = true - hookObj.priority = newPriority - } - } - } - - if (!found) { - throw new Error( - `Could not find the provided ${hookType}-call hook to change its priority` - ) - } - - hooks.sort((a, b) => b.priority - a.priority) - return this - } - public validate() { if (!this._agentic) { throw new Error( @@ -232,13 +165,10 @@ export abstract class BaseTask< options }) - this.addAfterCallHook( - async (output, ctx) => { - const feedback = await feedbackMechanism.interact(output) - ctx.metadata = { ...ctx.metadata, feedback } - }, - { name: 'humanFeedback' } - ) + this.addAfterCallHook(async (output, ctx) => { + const feedback = await feedbackMechanism.interact(output) + ctx.metadata = { ...ctx.metadata, feedback } + }) return this }