/** * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options */ import { type Editor, type Context, ContextPlugin, type PluginDependenciesOf } from "@ckeditor/ckeditor5-core"; import { CloudServices } from "@ckeditor/ckeditor5-cloud-services"; import { type AIContextRequestPart, type AIContextItemType } from "./model/aicontext.js"; import { type AIContextRefRequestData } from "./model/aicontextref.js"; import { type AICapabilitiesRequestData } from "./model/aicapabilities.js"; import type { AIActionsNames } from "../aiactions/aiactions.js"; import { type AIEditorConfig } from "./utils/geteditorconfig.js"; /** * A plugin that handles communication with the AI API. */ export declare class AIConnector extends ContextPlugin { /** * @inheritDoc */ static get requires(): PluginDependenciesOf<[CloudServices]>; /** * @inheritDoc */ static get pluginName(): "AIConnector"; /** * @inheritDoc */ static override get isOfficialPlugin(): true; /** * @inheritDoc */ static override get isPremiumPlugin(): true; constructor(context: Context | Editor); /** * @inheritDoc */ init(): void; /** * Creates a new conversation session with the AI API. */ startConversation(id: string, group: string): AIConversationStartRequest; /** * Sends a request to the AI API '/conversations/{conversationId}/messages' endpoint and returns a response stream reader. */ queryAssistant({ message, conversationId, parts, capabilities, model, attributes }: AIConnectorQueryAssistantData): AIConnectorQueryAssistantRequest; /** * Sends a request to the /actions/custom/calls endpoint and returns a response stream reader. */ executeCustomAction({ content, message, model, outputFormat, contexts }: AIConnectorExecuteCustomActionData): AIConnectorRequest; /** * Sends a request to the /actions/system/{actionName}/calls endpoint and returns a response stream reader. */ executeAction({ actionName, content, args, contexts }: AIConnectorExecuteActionData): AIConnectorRequest; /** * Uploads a file to the AI API for a specific conversation. */ uploadFile(conversationId: string, file: File, attributes?: { resourceId: string; label: string; }): AIUploadRequest; /** * Uploads multiple documents to the AI API for a specific conversation in a single batch request. * * The endpoint returns an array of replies in the same order as the input documents. */ uploadDocuments(conversationId: string, documents: Array): AIBatchUploadRequest; /** * Fetches multiple documents by their IDs from a conversation in a single batch request. */ fetchDocuments(conversationId: string, documentIds: Array): AIBatchFetchDocumentsRequest; /** * Fetches the list of conversations from the AI API. */ fetchConversations(group: string, params?: AIFetchConversationsParams): AIFetchConversationsRequest; /** * Deletes a conversation by its ID. */ deleteConversation(id: string): AIDeleteConversationRequest; /** * Updates the title and pinned status of a conversation by its ID. */ updateConversation(id: string, updates: { title?: string; pinned?: boolean; }): AIUpdateConversationRequest; /** * Fetches a specific conversation by its ID. */ fetchConversation(id: string): AIGetConversationRequest; /** * Fetches messages for a specific conversation by its ID. */ fetchConversationMessages(conversationId: string): AIGetConversationMessagesRequest; /** * Uploads a URL to the AI API for a specific conversation. */ uploadUrl(conversationId: string, url: string, attributes?: { resourceId: string; label: string; }): AIUploadRequest; /** * Returns a list of available AI models. * * @param language Language code (e.g., 'en', 'pl') to request localized model descriptions. Defaults to 'en'. */ getModels(language?: string): AIModelRequest; /** * Returns the list of contexts available to the current token. */ listContexts(params?: AIListContextsParams): AIListContextsRequest; /** * Deletes a file from a conversation. */ deleteFile(conversationId: string, fileId: string): AIDeleteFileRequest; /** * Deletes a document from a conversation. */ deleteDocument(conversationId: string, documentId: string): AIDeleteDocumentRequest; /** * Deletes a web resource from a conversation. */ deleteWebResource(conversationId: string, webResourceId: string): AIDeleteWebResourceRequest; /** * Cancels an in-progress assistant message stream via the v1.1 DELETE endpoint. */ cancelStream(conversationId: string, messageId: string): AICancelStreamRequest; /** * Reconnects to a version 1.1 assistant reply stream and replays events after the last SSE event received * by the client. * * If the reply was cancelled, replay includes any preserved reply events that were already produced, followed * by the final error event that closes the stream. * * @param conversationId ID of the conversation that owns the message. * @param messageId ID of the assistant message whose stream should be resumed. * @param lastEventId Last SSE event ID received by the client. Use `0-0` to replay the stream from the beginning. */ resumeStream(conversationId: string, messageId: string, lastEventId?: string): AIConnectorQueryAssistantRequest; /** * Sends a request to the AI API '/reviews/system/{reviewName}/calls' endpoint and returns a response stream reader. */ callSystemReview({ reviewName, content, args, contexts }: AIConnectorSystemReviewData): AIConnectorRequest; /** * Sends a request to the AI API '/documents/process' endpoint and returns the parsed JSON reply. */ processDocument({ content, prompt, model, capabilities, contexts }: AIConnectorProcessDocumentData): AIConnectorProcessDocumentRequest; /** * Sends a request to the AI API '/reviews/custom/calls' endpoint and returns a response stream reader. */ callCustomReview({ content, prompt, model, contexts }: AIConnectorCustomReviewData): AIConnectorRequest; upsertAIChatRating(conversationId: string, messageId: string, rating: AIRating): AIUpdateRating; upsertAIActionRating(actionName: string, callId: string, rating: AIRating): AIUpdateRating; upsertCustomAIActionRating(callId: string, rating: AIRating): AIUpdateRating; upsertAIReviewRating(reviewName: string, callId: string, rating: AIRating, dataId?: string): AIUpdateRating; upsertCustomAIReviewRating(callId: string, rating: AIRating, dataId?: string): AIUpdateRating; } export type AIGenericRequest = { request: Promise; abortController: AbortController; }; export type AIConversationStartReply = { id: string; }; export type AIConversationStartRequest = { getConversation: () => Promise; abortController: AbortController; }; export type AIMessageEventError = { event: "error"; data: Record; }; export type AIMessageEventMessageMetadata = { event: "message-metadata"; data: { messageId: string; }; }; export type AIMessageEventActionMetadata = { event: "action-metadata"; data: { callId: string; }; }; export type AIMessageEventConversationTitle = { event: "conversation-title"; data: { conversationTitle: string; }; }; export type AIMessageEventSource = { event: "source"; data: { source: AISource; }; }; export type AIMessageEventWebSearch = { event: "web-search"; data: Record; }; export type AIMessageEventReasoning = { event: "reasoning"; data: Record; }; export type AIMessageEventTextDelta = { event: "text-delta"; data: { textDelta: string; }; }; export type AIMessageEventModificationDelta = { event: "modification-delta"; data: { id: string; textDelta: string; documentId: string; }; }; export type AIMessageEventMcpToolNotification = { event: "mcp-tool-notification"; data: { toolName: string; data: Record; }; }; export type AIMessageEventMcpToolResult = { event: "mcp-tool-result"; data: { toolName: string; result: string; }; }; export type AIMessageEventMcpToolResultData = { data?: Record; attributes?: Record; }; export type AIMessageEvent = AIMessageEventError | AIMessageEventMessageMetadata | AIMessageEventActionMetadata | AIMessageEventConversationTitle | AIMessageEventSource | AIMessageEventWebSearch | AIMessageEventReasoning | AIMessageEventTextDelta | AIMessageEventModificationDelta | AIMessageEventMcpToolResult | AIMessageEventMcpToolNotification; export type AIEventSourceMessageParsed = { event: string; id?: string; data: Record; }; export type AIConnectorReply = AsyncGenerator; export type AIConnectorRequest = { getStream: () => Promise; abortController: AbortController; }; export type AIConnectorQueryAssistantReply = AsyncGenerator; export type AIConnectorQueryAssistantRequest = { getStream: () => Promise; abortController: AbortController; }; export type AICancelStreamRequest = { execute: () => Promise; abortController: AbortController; }; export type AIConnectorQueryAssistantData = { message: string; conversationId: string; parts: Array; capabilities: AICapabilitiesRequestData | undefined; model: string; attributes?: Record; }; export type AIFetchDocumentReply = { id: string; content: string; attributes?: { version: number; sessionId: string | null; channelId?: string; rootName?: string; }; name?: string; description?: string; }; export type AIUploadDocumentData = { content: string | undefined; version: number; sessionId: string | null; channelId: string; rootName: string; selections: Array<{ start: number; end: number; htmlFragment: string; }>; name?: string; description?: string; }; export type AIBatchFetchDocumentsReply = Array; export type AIBatchFetchDocumentsRequest = { getDocuments: () => Promise; abortController: AbortController; }; export type AIUploadReply = { id: string; }; export type AIUploadRequest = { getId: () => Promise; abortController: AbortController; }; export type AIBatchUploadReply = Array<{ id: string; }>; export type AIBatchUploadRequest = { getIds: () => Promise; abortController: AbortController; }; export type AIConversationItem = { id: string; title: string; createdAt: string; updatedAt?: string; pinned: boolean; }; export type AIFetchConversationsParams = { limit?: number; sortBy?: "createdAt" | "updatedAt"; order?: "desc" | "asc"; excludeEmpty?: boolean; }; export type AIFetchConversationsRequest = { getConversations: () => Promise>; abortController: AbortController; }; export type AIUpdateConversationRequest = { getResult: () => Promise<{ title?: string; pinned?: boolean; }>; abortController: AbortController; }; export type AIDeleteConversationRequest = { execute: () => Promise; abortController: AbortController; }; export type AIUserMessage = { capabilities: Record; content: Array; prompt: string; model: string; id: string; role: "user"; attributes: Record; }; export type AIUserMessageContent = { id: string; type: AIContextItemType; content: string; mcpServerName: string; editorConfig?: AIEditorConfig; toolName?: string; data?: Record; name?: string; url?: string; attributes?: { resourceId: string; label: string; }; }; /** * Represents a source used in AI responses (e.g., web search results). */ export type AISource = { type: "url"; id: string; url: string; title: string; favicon: string; }; export type AIAssistantMessage = { content: Array<{ id: string; type: "text" | "modification" | "mcp-tool-result"; content: string; toolName?: string; documentId?: string; } | AISource>; id: string; role: "assistant"; status?: "streaming" | "completed" | "cancelled" | "error"; }; export type AIConversationMessage = AIUserMessage | AIAssistantMessage; export type AIGetConversationReply = { id: string; model: string; title: string; pinned: boolean; }; export type AIGetConversationRequest = { getConversation: () => Promise; abortController: AbortController; }; export type AIGetConversationMessagesRequest = { getMessages: () => Promise>; abortController: AbortController; }; export type AIModelData = { id: string; name: string; provider: string; description: string; recommended: boolean; capabilities: { webSearch: { enabled: boolean; }; reasoning: { enabled: boolean; }; }; }; export type AIModelRequest = { list: () => Promise<{ items: Array; }>; abortController: AbortController; }; /** * A single context available to the current token, as returned by {@link module:ai/aicore/aiconnector~AIConnector#listContexts}. * * A context is an admin-managed, named collection of prompts and files. It is referenced elsewhere by its `id` through an * {@link module:ai/aicore/model/aicontextref~AIContextRef}. */ export type AIContextData = { /** * The stable identifier of the context. This is the value referenced by an * {@link module:ai/aicore/model/aicontextref~AIContextRef}. */ id: string; /** * The human-readable name of the context. */ name?: string; /** * The optional description of the context. */ description?: string; /** * The optional custom attributes attached to the context. */ attributes?: Record; }; export type AIListContextsParams = { /** * The cursor of the page to fetch. When not specified, the first page is fetched. */ cursor?: string; /** * The number of items to fetch. */ limit?: number; /** * Determines the direction of sorting. */ order?: "desc" | "asc"; }; export type AIListContextsRequest = { getContexts: () => Promise>; abortController: AbortController; }; export type AIDeleteFileRequest = { execute: () => Promise; abortController: AbortController; }; export type AIDeleteDocumentRequest = { execute: () => Promise; abortController: AbortController; }; export type AIDeleteWebResourceRequest = { execute: () => Promise; abortController: AbortController; }; export type AIConnectorExecuteCustomActionData = { content: string; message?: string; model: string; outputFormat: "plainText" | "html"; contexts?: Array; }; export type AIConnectorExecuteActionData = { actionName: AIActionsNames; content: string; args?: { language?: string; }; contexts?: Array; }; export type AIConnectorSystemReviewData = { reviewName: string; content: string; args?: { language?: string; }; contexts?: Array; }; export type AIConnectorCustomReviewData = { content: string; prompt?: string; model: string; contexts?: Array; }; export type AIConnectorProcessDocumentContentDocument = { type: "document"; content: string; selection?: Array<{ start: number; end: number; }>; }; export type AIConnectorProcessDocumentData = { content: Array; prompt: string; model: string; capabilities?: AICapabilitiesRequestData; contexts?: Array; }; export type AIConnectorProcessDocumentReply = { documents: Array<{ document: string; summary: string; }>; summary?: string; }; export type AIConnectorProcessDocumentRequest = { getResult: () => Promise; abortController: AbortController; }; export type AIUpdateRating = { execute: () => Promise; abortController: AbortController; }; export type AIRating = { positiveCount: number; totalCount: number; modificationId?: string; };