@baseai/core
Version:
The Web AI Framework's core - BaseAI.dev
88 lines (83 loc) • 2.9 kB
text/typescript
import { ChatCompletionStream } from 'openai/lib/ChatCompletionStream';
type MessageRole = 'function' | 'assistant' | 'system' | 'user' | 'tool';
interface Function {
name: string;
arguments: string;
}
interface ToolCallResult {
id: string;
type: 'function';
function: Function;
}
interface ChoiceStream {
index: number;
delta: Delta;
logprobs: boolean | null;
finish_reason: string;
}
interface Delta {
role?: MessageRole;
content?: string;
tool_calls?: ToolCallResult[];
}
interface ChunkStream {
id: string;
object: string;
created: number;
model: string;
choices: ChoiceStream[];
}
interface Runner extends ChatCompletionStream<null> {
}
/**
* Converts a ReadableStream into a Runner.
*
* @param readableStream - The ReadableStream to convert.
* @returns The converted Runner.
*/
declare const fromReadableStream: (readableStream: ReadableStream) => Runner;
/**
* Returns a runner for the given readable stream.
*
* @param readableStream - The readable stream to create a runner for.
* @returns A runner for the given readable stream.
*/
declare const getRunner: (readableStream: ReadableStream) => Runner;
/**
* Retrieves the text part from a given ChunkStream.
*
* @param chunk - The ChunkStream object.
* @returns The text content of the first choice's delta, or an empty string if it doesn't exist.
*/
declare const getTextPart: (chunk: ChunkStream) => string;
/**
* Handles the response stream from a given `Response` object.
*
* @param {Object} params - The parameters for handling the response stream.
* @param {Response} params.response - The API response to handle.
* @param {boolean} params.rawResponse - Optional flag to include raw response headers.
*
* @returns {Object} An object containing the processed stream, thread ID, and optionally raw response headers.
* @returns {ReadableStream<any>} return.stream - The readable stream created from the response.
* @returns {string | null} return.threadId - The thread ID extracted from the response headers.
* @returns {Object} [return.rawResponse] - Optional raw response headers.
* @returns {Record<string, string>} return.rawResponse.headers - The headers from the raw response.
*/
declare function handleResponseStream({ response, rawResponse, }: {
response: Response;
rawResponse?: boolean;
}): {
stream: any;
threadId: string | null;
rawResponse?: {
headers: Record<string, string>;
};
};
/**
* Retrieves tool calls from a given readable stream.
*
* @param stream - The readable stream from which to extract tool calls.
* @returns A promise that resolves to an array of `ToolCall` objects.
*/
declare function getToolsFromStream(stream: ReadableStream<any>): Promise<ToolCallResult[]>;
export { type Runner, fromReadableStream, getRunner, getTextPart, getToolsFromStream, handleResponseStream };