/** * @fileoverview Index export. * @license * SPDX-License-Identifier: Apache-2.0 */ import { type StreamItems, type ReadableStream, type WritableStream } from "./stream/interfaces"; // Chunk Interfaces Start /** google.protobuf.Any proto mapping. */ export declare interface Any { readonly '@type': string; readonly [key: string]: undefined | null | string | number | boolean | object; } /** Metadata for a chunk. */ export declare interface ChunkMetadata { readonly mimetype?: Mimetype; readonly role?: string; readonly captureTime?: Date; readonly experimental?: Any[]; } /** Structured mimetype */ export interface Mimetype { // Example: 'application/vnd.google.gdm.content+json' readonly type?: string; // 'application' readonly subtype?: string; // 'content' readonly prefix?: string; // 'vnd.google.gdm' the prefix such as vnd readonly suffix?: string; // 'json', from +json suffix readonly parameters?: Record; // after the ';' like '; type=aiae.v1.MyConfig' or charset=utf-8 } /** Chunk with metadata. */ export interface MetadataChunk { readonly metadata?: ChunkMetadata; } /** Chunk with bytes. */ export interface DataChunk extends MetadataChunk { readonly data: Uint8Array; readonly ref?: undefined; } /** Chunk with a reference to content elsewhere. */ export interface RefChunk extends MetadataChunk { readonly data?: undefined; readonly ref: string; } /** Smallest unit of streamable information. */ export type Chunk = DataChunk | RefChunk; // Chunk Interfaces End // ChunkStream Interfaces Start export type Content = StreamItems; export type Input = ReadableStream; export type Output = WritableStream export interface Pipe extends Input, Output { } // ChunkStream Interfaces End // Action Interfaces Start export type Dict = Readonly>; type StreamTypeOfDict> = T extends Dict ? U extends WritableStream ? V : never : never; // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters export abstract class Action = Dict, U extends Dict = Dict> { abstract run(session: Session, inputs: T, outputs: U): Promise; } export type ActionInputs = T extends Action ? U : never; export type ActionOutputs = T extends Action ? Dict>, keyof V & string> : never; export type ActionConstraints = keyof ActionOutputs & string; // Action Interfaces End // Processor Interfaces Start /** Simplified transform of a unified Chunk stream. */ export type Processor = (stream: AsyncIterable<[I, T]>) => AsyncGenerator<[O, U]>; /** Processor Chunks. */ export type ProcessorChunks = AsyncIterable<[T, U]>; /** Input dict to a processor. */ export type ProcessorInputs = T extends Processor? Record> : never; /** Output dict to a processor. */ export type ProcessorOutputs = T extends Processor? Record> : never; // export type ProcessorOutputs = T extends Processor ? // {[K in O]: ReadableStream} : // never; export type ProcessorConstraints = keyof ProcessorOutputs & string; // Processor Interfaces End // Session Interfaces Start export interface Session { // Returns a streaming pipe for reading and writing a stream in the context of the session. // Content will exist and can be read for as long as the session is open. // If content stream is provided when creating the pipe it is equivilent to create, write, and close all in one. createPipe(content?: Content): Pipe; // Runs an action. run = ActionConstraints>(action: T, inputs: ActionInputs, outputs: U[]): Pick,U>; // Runs a processor which is a convenience form transformed to an Action. // eslint-disable-next-line @typescript-eslint/no-explicit-any run, U extends ProcessorConstraints = ProcessorConstraints>( processor: T, inputs: ProcessorInputs, outputs: U[]): Pick, U>; // Closes the session and all open streams or running actions. close(): Promise; } export interface SessionWriteOptions { // Sequence in the stream of the chunk defaults to 0. seq?: number, // Is the stream still being written to defaults to false. continued?: boolean, } export interface SessionContext { read(id: string): AsyncIterable; write(id: string, chunk: Chunk, options?: SessionWriteOptions): Promise; error(id: string, reason?: string): void; close(): Promise; } export type SessionContextMiddleware = (context: SessionContext) => SessionContext; export type SessionProvider = (...middleware: SessionContextMiddleware[]) => Session; // Session Interfaces End