import { ChatCompletionRequestMessageRoleEnum, MessageStreamingListener, MessageStreamingStartListener, MessageStreamingStopListener, MessageUpdateListener, FunctionCallMessage, FunctionMessage, CompletionMessage, CreateChatCompletionMessage, CreateChatCompletionFunctionCallMessage, CreateChatCompletionFunctionMessage, MessageContentStreamListener } from "../utils/types/index.js"; import { MessageModel } from "../schemas/message.schema.js"; import { ConversationRequestOptionsModel } from "../schemas/conversationRequestOptions.schema.js"; /** * A message in a Conversation. */ export declare class Message { /** * A UUID generated for this message by the library. */ id: string; private _role; private _model; private _content; private _name; private _functionCall; private _flags; private _isStreaming; private messageUpdateEvents; private messageStreamingEvents; private messageStreamingStartEvents; private messageStreamingStopEvents; private messageContentStreamEvents; /** * Creates a new Message instance. * * @param role The role of who this message is from. Either "user", "assistant" or "system". * @param content The content of the message. * @param model The model used for processing this message. */ constructor(role?: ChatCompletionRequestMessageRoleEnum, content?: string | null, model?: string); /** * Creates a new `Message` instance from a serialized message. * * @param json The JSON object of the Message instance. * @returns A new `Message` instance */ static fromJSON(json: MessageModel): Message; /** * Serializes the message to JSON. * * @returns The `Message` as a JSON object. */ toJSON(): MessageModel; /** * Call the OpenAI moderation API to check if the message is flagged. Only called once for the same content. * * @param apiKey The OpenAI API key * @param requestOptions The request options to pass to fetch * @returns The flags applied on the message */ moderate(apiKey: string, requestOptions?: ConversationRequestOptionsModel): Promise; /** * Progessively adds content to the message from a streamed response type. * * @param response The ReadableStream from the OpenAI API */ readContentFromStream(response: ReadableStream): Promise; /** * Whether the message is a function call by the assistant */ isFunctionCall(): this is FunctionCallMessage; /** * Whether the message is a function call result by the user */ isFunction(): this is FunctionMessage; /** * Whether the message is a regular chat completion message */ isCompletion(): this is CompletionMessage; /** The role of who this message is from. */ get role(): ChatCompletionRequestMessageRoleEnum; private set role(value); /** The model used to generate this message. */ get model(): string; private set model(value); /** The content of the message. */ get content(): string | null; set content(content: string | null); get name(): string | undefined; set name(name: string | undefined); get functionCall(): { name: string; arguments: Record; } | undefined; set functionCall(functionCall: { name: string; arguments: Record; } | undefined); /** The flags detected by OpenAI's moderation API. Only set after calling `moderate`. */ get flags(): string[] | null; private set flags(value); /** Whether the message is flagged by OpenAI's moderation API. Always `false` unless `moderate` has been called. */ get isFlagged(): boolean; /** Whether the message is currently being streamed. */ get isStreaming(): boolean; private set isStreaming(value); /** * JSON definition of the message as consumed by OpenAI's API. */ get chatCompletionMessage(): CreateChatCompletionMessage | CreateChatCompletionFunctionCallMessage | CreateChatCompletionFunctionMessage; /** * Add a listener for message content changes. * * @param listener The listener to trigger when `content` changes * @returns An unsubscribe function for this `listener` */ onUpdate(listener: MessageUpdateListener): () => void; /** * Add a listener that is called only once when the message content changes. * * @param listener The listener to trigger when `content` changes * @returns An unsubscribe function for this `listener` */ onceUpdate(listener: MessageUpdateListener): () => void; /** * Removes a message update listener, previously set with `onUpdate`. * * @param listener The previously added listener */ offUpdate(listener: MessageUpdateListener): void; /** * Adds a listener for message streaming state changes. * * @param listener The listener to trigger when `isStreaming` changes * @returns An unsubscribe function for this `listener` */ onStreamingUpdate(listener: MessageStreamingListener): () => void; /** * Adds a listener that is called only once when the message streaming state changes. * * @param listener The listener to trigger when `isStreaming` changes * @returns An unsubscribe function for this `listener` */ onceStreamingUpdate(listener: MessageStreamingListener): () => void; /** * Removes a message streaming listener, previously set with `onStreamingUpdate`. * * @param listener The previously added listener */ offStreaming(listener: MessageStreamingListener): void; /** * Adds a listener for message streaming start. * * @param listener The listener to trigger when `isStreaming` is set to `true` * @returns An unsubscribe function for this `listener` */ onStreamingStart(listener: MessageStreamingStartListener): () => void; /** * Adds a listener that is called only once when the message streaming starts. * * @param listener The listener to trigger when `isStreaming` is set to `true` * @returns An unsubscribe function for this `listener` */ onceStreamingStart(listener: MessageStreamingStartListener): () => void; /** * Removes a message streaming start listener, previously set with `onStreamingStart`. * * @param listener The previously added listener */ offStreamingStart(listener: MessageStreamingStartListener): void; /** * Adds a listener for message streaming stop. * * @param listener The listener to trigger when `isStreaming` is set to `false` * @returns An unsubscribe function for this `listener` */ onStreamingStop(listener: MessageStreamingStopListener): () => void; /** * Adds a listener that is called only once when the message streaming stops. * * @param listener The listener to trigger when `isStreaming` is set to `false` * @returns An unsubscribe function for this `listener` */ onceStreamingStop(listener: MessageStreamingStopListener): () => void; /** * Removes a message streaming stop listener, previously set with `onStreamingStop`. * * @param listener The previously added listener */ offStreamingStop(listener: MessageStreamingStopListener): void; /** * Adds a listener that is fired whenever the message content is updated during streaming. Also fires when streaming starts/ends. * * @remarks * Unlike the other listeners which behave like normal event listeners, this special listener is unsubscribed automatically when streaming ends. * If this is not desired, use `onUpdate` and `onStreamingStart`/`onStreamingStop` instead. * * @param listener The listener to trigger when `content` changes during streaming * @returns An unsubscribe function for this `listener` */ onContentStream(listener: MessageContentStreamListener): () => void; /** * Adds a listener that is fired only once whenever the message content is updated during streaming. Also fires when streaming starts/ends. * * @remarks * Unlike the other listeners which behave like normal event listeners, this special listener is unsubscribed automatically when streaming ends, regardless of whether it was called once or not. * If this is not desired, use `onceUpdate` and `onceStreamingStart`/`onceStreamingStop` instead. * * @param listener The listener to trigger when `content` changes during streaming * @returns An unsubscribe function for this `listener` */ onceContentStream(listener: MessageContentStreamListener): () => void; /** * Removes a message streaming content listener, previously set with `onContentStream`. * * @remarks * You do not need to call this method manually, as this listener automatically unsubscribes all listeners when streaming ends. * You should use this if you want to unsubscribe a listener before streaming ends. * * @param listener The previously added listener */ offContentStream(listener: MessageContentStreamListener): void; }