@ckeditor/ckeditor5-ai
Version:
AI features for CKEditor 5.
269 lines (268 loc) • 9.06 kB
TypeScript
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/
/**
* @module ai/aichat/model/aichatconversation
*/
import { CKEditorError } from 'ckeditor5/src/utils.js';
import { type AIConnector } from '../../aicore/aiconnector.js';
import { AIChatContext, type AIChatContextConfig, type AIContextProvider, type AIContextResource, type AIContextResourceState } from './aichatcontext.js';
import { type AIContextItem } from '../../aicore/model/aicontext.js';
import { AICapabilities, type AICapabilitiesConfig } from '../../aicore/model/aicapabilities.js';
import type { AIChatModel, AIChatModels } from './aichatmodels.js';
import { type Editor } from 'ckeditor5/src/core.js';
import { AIChatInteraction } from './aichatinteraction.js';
import { type AIQuickActionRequestData } from '../../aiquickactions/aiquickactions.js';
declare const AIChatConversation_base: {
new (): import("ckeditor5/src/utils.js").Emitter;
prototype: import("ckeditor5/src/utils.js").Emitter;
};
/**
* Represents a conversation between the user and the AI endpoint on top of the
* {@link module:ai/aichat/model/aichatconnector~AIChatConnector} in the network layer.
*
* A conversation hosts a collection of {@link module:ai/aichat/model/aichatinteraction~AIChatInteraction user interactions}.
*
* The {@link module:ai/aichat/model/aichatconversation~AIChatConversation#currentInteraction current interaction} in the conversation
* is the one that is currently being processed by the AI.
*
* Note: The conversation funnels (delegates) all events from the
* {@link module:ai/aichat/model/aichatconversation~AIChatConversation#interactions} and their
* {@link module:ai/aichat/model/aichatinteraction~AIChatInteraction#replies replies}.
*/
export declare class AIChatConversation extends /* #__PURE__ */ AIChatConversation_base {
/**
* The unique conversation ID.
*/
id: string;
/**
* Indicates whether the conversation has been started.
*
* This is set to `true` when {@link #start} is called and the conversation is successfully started.
*/
isStarted: boolean;
/**
* The interactions in the conversation.
*/
interactions: Array<AIChatInteraction>;
/**
* The current interaction in the conversation. Set and unset by {@link #handleUserInteraction}.
*/
currentInteraction?: AIChatInteraction;
/**
* The selected AI chat model for the conversation.
*
* This is set when the user selects a model from the list of available models.
* If not set, the default model will be used.
*/
selectedModel?: AIChatModel;
/**
* Service for managing the conversation context.
*/
chatContext: AIChatContext;
/**
* Service for managing the conversation capabilities.
*/
chatCapabilities: AICapabilities;
/**
* Service for providing the list of available AI chat models.
*/
chatModels: AIChatModels;
/**
* @inheritDoc
*/
constructor(options: AIChatConversationOptions);
/**
* Initializes the conversation.
*/
init(contextConfig?: AIChatContextConfig): Promise<void>;
start(): Promise<void>;
/**
* Loads a conversation from the backend using the provided conversation ID.
*/
load(conversationId: string, sessionId: string): Promise<AIChatConversation>;
/**
* Processes messages to create interactions and replies.
*/
private _processMessagesToInteractions;
/**
* Handles a user interaction by creating a new interaction and starting it.
*/
handleUserInteraction({ userMessage, quickActionData }: {
userMessage: string;
quickActionData?: AIQuickActionRequestData;
}): Promise<void>;
/**
* Creates a new interaction and stores it in the conversation.
*/
createInteraction({ userMessage, contextItems, capabilities, modelId, quickActionData }: {
userMessage: string;
contextItems: Map<string, AIContextItem>;
capabilities: AICapabilitiesConfig;
modelId: string;
quickActionData?: AIQuickActionRequestData;
}): AIChatInteraction;
/**
* Gets an interaction in the conversation by its ID.
*/
getInteraction(id: string): AIChatInteraction | undefined;
/**
* Removes an interaction from the conversation by its ID.
*/
removeInteraction(id: string): void;
/**
* Gets the last of {@link #interactions} in the conversation.
*/
get lastInteraction(): AIChatInteraction | undefined;
/**
* Adds a current document to the context.
*/
addCurrentDocumentToContext(label: string): void;
/**
* Updates the current document in the context to contain the provided content and version.
*/
updateCurrentDocumentInContext({ content, version, sessionId, selections }: {
content: string | undefined;
version: number;
sessionId: string | null;
selections: Array<{
start: number;
end: number;
htmlFragment: string;
}>;
}): Promise<void>;
/**
* Returns the content of the current interaction's document.
*/
getCurrentDocumentContext(): string;
/**
* Adds a selection to the context.
*/
addSelectionToContext(selection: string): void;
removeSelectionFromContext(): void;
/**
* Adds files to the context.
*/
addFilesToContext(files: Array<File>): void;
/**
* Adds a URL to the context.
*/
addUrlToContext(url: string): void;
/**
* Adds a resource from a source provider to the context.
*/
addResourceToContext(source: AIContextProvider, resource: AIContextResource): void;
/**
* Loads resources for a given source provider.
*/
loadSourceResources(sourceId: string, query: string, uid: string): Promise<Array<AIContextResourceState>>;
/**
* Removes an item from the conversation context by its ID.
*/
removeFromContext(id: string): void;
/**
* Toggles the web search functionality on or off.
*/
toggleWebSearch(): void;
/**
* Toggles the reasoning functionality on or off.
*/
toggleReasoning(): void;
/**
* Checks if the current document is already in the conversation context.
*/
isCurrentDocumentInContext(): boolean;
/**
* Checks if the current document is already in the conversation context.
*/
isCurrentDocumentInConversation(): boolean;
/**
* Checks if the selection is already in the conversation context.
*/
isSelectionInContext(): boolean;
/**
* Sets the AI model for the conversation.
*/
setModel(model: AIChatModel): Promise<void>;
}
/**
* An event emitted by {@link module:ai/aichat/model/aichatconversation~AIChatConversation} when a new interaction is created.
*
* @eventName ~AIChatConversation#interactionCreated
*/
export type AIChatInteractionCreatedEvent = {
name: 'interactionCreated';
args: [interaction: AIChatInteraction];
};
/**
* An event emitted by {@link module:ai/aichat/model/aichatconversation~AIChatConversation} when an error occurs.
*
* @eventName ~AIChatConversation#error
*/
export type AIChatConversationError = {
name: 'error';
args: [
{
interactionId?: string;
error: CKEditorError;
fileName?: string;
url?: string;
sourceId?: string;
sourceLabel?: string;
}
];
};
/**
* An event emitted by {@link module:ai/aichat/model/aichatconversation~AIChatConversation#init} once the conversation was initialized.
*
* @eventName ~AIChatConversation#conversationInitialized
*/
export type AIChatConversationInitializedEvent = {
name: 'conversationInitialized';
args: [
{
availableModels: Array<AIChatModel>;
}
];
};
/**
* An event emitted by {@link module:ai/aichat/model/aichatconversation~AIChatConversation#start} once the conversation was started.
*
* @eventName ~AIChatConversation#conversationStarted
*/
export type AIChatConversationStartedEvent = {
name: 'conversationStarted';
args: [
{
conversationId: string;
selectedModel: AIChatModel;
}
];
};
/**
* An event emitted by {@link module:ai/aichat/model/aichatconversation~AIChatConversation} when a model is selected.
*
* @eventName ~AIChatConversation#modelSelected
*/
export type AIChatConversationModelSelectedEvent = {
name: 'modelSelected';
args: [
{
selectedModel: AIChatModel;
isDisabled?: boolean;
}
];
};
/**
* Options for the AI chat conversation.
*/
export type AIChatConversationOptions = {
connector: AIConnector;
chatModels: AIChatModels;
getEditor?: () => Editor;
conversationId: string;
group: string;
selectedModelId?: string;
};
export {};