UNPKG

@promptbook/utils

Version:

Promptbook: Run AI apps in plain human language across multiple models and platforms

146 lines (145 loc) 4.8 kB
import type { Observable } from 'rxjs'; import { PartialDeep } from 'type-fest'; import type { number_percent } from '../types/typeAliases'; import type { task_id } from '../types/typeAliases'; import type { string_SCREAMING_CASE } from '../utils/normalization/normalizeTo_SCREAMING_CASE'; import type { string_promptbook_version } from '../version'; import type { AbstractTaskResult } from './AbstractTaskResult'; import type { PipelineExecutorResult } from './PipelineExecutorResult'; /** * Options for creating a new task */ type CreateTaskOptions<TTaskResult extends AbstractTaskResult> = { /** * The type of task to create */ readonly taskType: AbstractTask<TTaskResult>['taskType']; /** * Human-readable title of the task - used for displaying in the UI */ readonly title: AbstractTask<TTaskResult>['title']; /** * Callback that processes the task and updates the ongoing result * @param updateOngoingResult Function to update the partial result of the task processing * @param updateTldr Function to update tldr progress information * @returns The final task result */ taskProcessCallback(updateOngoingResult: (newOngoingResult: PartialDeep<TTaskResult> & { /** * Optional update of the task title */ readonly title?: AbstractTask<TTaskResult>['title']; }) => void, updateTldr: (tldrInfo: { readonly percent: number_percent; readonly message: string; }) => void): Promise<TTaskResult>; }; /** * Helper to create a new task * * @private internal helper function */ export declare function createTask<TTaskResult extends AbstractTaskResult>(options: CreateTaskOptions<TTaskResult>): AbstractTask<TTaskResult>; /** * Represents a task that executes a pipeline */ export type ExecutionTask = AbstractTask<PipelineExecutorResult> & { readonly taskType: 'EXECUTION'; readonly taskId: `exec-${task_id}`; }; /** * Represents a task that prepares a pipeline * @deprecated TODO: [🐚] Currently unused - use */ export type PreparationTask = AbstractTask<PipelineExecutorResult> & { readonly taskType: 'PREPARATION'; readonly taskId: `prep-${task_id}`; }; /** * Status of a task */ export type task_status = 'RUNNING' | 'FINISHED' | 'ERROR'; /** * Base interface for all task types */ export type AbstractTask<TTaskResult extends AbstractTaskResult> = { /** * Type of the task */ readonly taskType: string_SCREAMING_CASE; /** * Version of the promptbook used to run the task */ readonly promptbookVersion: string_promptbook_version; /** * Unique identifier for the task */ readonly taskId: task_id; /** * Human-readable title of the task - used for displaying in the UI * * For example name of the book which is being executed */ readonly title: string; /** * Status of the task */ readonly status: task_status; /** * Short summary of the task status for quick overview in the UI */ readonly tldr: { /** * Progress in percentage from 0 to 1 (100%) that can be used to display a progress bar */ readonly percent: number_percent; /** * Short summary message of the task status that can be displayed in the UI */ readonly message: string; }; /** * Date when the task was created */ readonly createdAt: Date; /** * Date when the task was last updated */ readonly updatedAt: Date; /** * Gets a promise that resolves with the task result */ asPromise(options?: { /** * Do the task throws on error * * - If `true` when error occurs the returned promise will rejects * - If `false` the promise will resolve with object with all listed errors and warnings and partial result * * @default true */ readonly isCrashedOnError?: boolean; }): Promise<TTaskResult>; /** * Gets an observable stream of partial task results */ asObservable(): Observable<PartialDeep<TTaskResult>>; /** * Gets just the current value which is mutated during the task processing */ readonly currentValue: PartialDeep<TTaskResult>; /** * List of errors that occurred during the task processing */ readonly errors: Array<Error>; /** * List of warnings that occurred during the task processing */ readonly warnings: Array<Error>; }; export type Task = ExecutionTask | PreparationTask; export {}; /** * TODO: Maybe allow to terminate the task and add getter `isFinished` or `status` * TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function */