chatgpt-api/src/task.ts

52 wiersze
1.5 KiB
TypeScript
Czysty Zwykły widok Historia

2023-05-26 19:06:39 +00:00
import { ZodRawShape, ZodTypeAny, z } from 'zod'
import * as types from './types'
export abstract class BaseTaskCallBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = z.ZodTypeAny
> {
2023-05-26 19:16:13 +00:00
protected _inputSchema: TInput
protected _outputSchema: TOutput
protected _timeoutMs: number
protected _retryConfig: types.RetryConfig
2023-05-26 19:06:39 +00:00
constructor(options: types.BaseTaskOptions<TInput, TOutput>) {
2023-05-26 19:16:13 +00:00
this._inputSchema = options.inputSchema
this._outputSchema = options.outputSchema
this._timeoutMs = options.timeoutMs
this._retryConfig = options.retryConfig
2023-05-26 19:06:39 +00:00
}
input<U extends ZodRawShape | ZodTypeAny = TInput>(
inputSchema: U
): BaseTaskCallBuilder<U, TOutput> {
2023-05-26 19:16:13 +00:00
;(this as unknown as BaseTaskCallBuilder<U, TOutput>)._inputSchema =
2023-05-26 19:06:39 +00:00
inputSchema
return this as unknown as BaseTaskCallBuilder<U, TOutput>
}
output<U extends ZodRawShape | ZodTypeAny = TOutput>(
outputSchema: U
): BaseTaskCallBuilder<TInput, U> {
2023-05-26 19:16:13 +00:00
;(this as unknown as BaseTaskCallBuilder<TInput, U>)._outputSchema =
2023-05-26 19:06:39 +00:00
outputSchema
return this as unknown as BaseTaskCallBuilder<TInput, U>
}
retry(retryConfig: types.RetryConfig) {
2023-05-26 19:16:13 +00:00
this._retryConfig = retryConfig
2023-05-26 19:06:39 +00:00
return this
}
abstract call(
input?: types.ParsedData<TInput>
): Promise<types.ParsedData<TOutput>>
// TODO
// abstract stream({
// input: TInput,
// onProgress: types.ProgressFunction
// }): Promise<TOutput>
}