kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add before and after hook types
rodzic
b4ffc2baf8
commit
d7321f1a3f
14
src/task.ts
14
src/task.ts
|
@ -29,10 +29,8 @@ export abstract class BaseTask<
|
||||||
protected _timeoutMs?: number
|
protected _timeoutMs?: number
|
||||||
protected _retryConfig: types.RetryConfig
|
protected _retryConfig: types.RetryConfig
|
||||||
|
|
||||||
private _preHooks: Array<(ctx: types.TaskCallContext<TInput>) => void> = []
|
private _preHooks: Array<types.BeforeCallHook<TInput>> = []
|
||||||
private _postHooks: Array<
|
private _postHooks: Array<types.AfterCallHook<TInput, TOutput>> = []
|
||||||
(output: TOutput, ctx: types.TaskCallContext<TInput>) => void
|
|
||||||
> = []
|
|
||||||
|
|
||||||
constructor(options: types.BaseTaskOptions = {}) {
|
constructor(options: types.BaseTaskOptions = {}) {
|
||||||
this._agentic = options.agentic ?? globalThis.__agentic?.deref()
|
this._agentic = options.agentic ?? globalThis.__agentic?.deref()
|
||||||
|
@ -79,16 +77,12 @@ export abstract class BaseTask<
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
public addBeforeCallHook(
|
public addBeforeCallHook(hook: types.BeforeCallHook<TInput>): this {
|
||||||
hook: (ctx: types.TaskCallContext<TInput>) => Promise<void>
|
|
||||||
): this {
|
|
||||||
this._preHooks.push(hook)
|
this._preHooks.push(hook)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public addAfterCallHook(
|
public addAfterCallHook(hook: types.AfterCallHook<TInput, TOutput>): this {
|
||||||
hook: (output: TOutput, ctx: types.TaskCallContext<TInput>) => Promise<void>
|
|
||||||
): this {
|
|
||||||
this._postHooks.push(hook)
|
this._postHooks.push(hook)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
|
@ -152,3 +152,12 @@ export declare class CancelablePromise<T> extends Promise<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// export type ProgressFunction = (partialResponse: ChatMessage) => void
|
// export type ProgressFunction = (partialResponse: ChatMessage) => void
|
||||||
|
|
||||||
|
export type BeforeCallHook<TInput extends TaskInput = void> = (
|
||||||
|
ctx: TaskCallContext<TInput>
|
||||||
|
) => void | Promise<void>
|
||||||
|
|
||||||
|
export type AfterCallHook<
|
||||||
|
TInput extends TaskInput = void,
|
||||||
|
TOutput extends TaskOutput = string
|
||||||
|
> = (output: TOutput, ctx: TaskCallContext<TInput>) => void | Promise<void>
|
||||||
|
|
Ładowanie…
Reference in New Issue