# AIInteraction

classexperimental

An interaction is a single request to the AI endpoint. It is created when a user message is sent and finishes when the all responses are received (interaction is finished) or interaction is stopped (by an error or directly by the user).

An interaction hosts a collection of [`replies`](#member-replies) and fires various events that may be handled to update the UI accordingly.

**Experimental:** This is a production-ready API but may change in minor releases without the standard deprecation policy. Check the changelog for migration guidance.

<a id="properties">

## Properties

<a id="member-currentReply">

### `currentReply?: AIReply`

The current reply being returned by the AI endpoint and handled by `AIInteraction`. This property is set when the interaction is started and becomes `undefined` when the interaction is [stopped](#function-stop). It changes as data for new replies are received from the AI endpoint.

<a id="member-id">

### `id: string` _(readonly)_

The unique ID of the interaction.

<a id="member-replies">

### `replies: Array<AIReply>`

The replies created for this interaction.

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( __namedParameters )`

#### Parameters

* `__namedParameters: AIInteractionOptions`

<a id="function-createReply">

### `createReply( options = { options.areActionsDisabled?, options.channelsToEditors, options.documentContextContent?, options.id?, options.interactionId, options.isComplete?, options.isDone?, options.isFromHistory?, options.locale?, options.outdatedReasonByDocumentId?, options.type } ) → AIReply` _(experimental)_

Creates a reply and adds it to the interaction.

**Experimental:** This is a production-ready API but may change in minor releases without the standard deprecation policy. Check the changelog for migration guidance.

#### Parameters

* `options: object`

  Properties

  * `options.areActionsDisabled?: boolean`
  * `options.channelsToEditors: Map<string, Editor>`
  * `options.documentContextContent?: Array<object>`
  * `options.id?: string`
  * `options.interactionId: string`
  * `options.isComplete?: boolean`
  * `options.isDone?: boolean`
  * `options.isFromHistory?: boolean`
  * `options.locale?: Locale`
  * `options.outdatedReasonByDocumentId?: ReadonlyMap<string, AIReplyChangeGroupOutdatedReason>`
  * `options.type: AIReplyType`

#### Returns

* `AIReply`

<a id="function-delegate">

### `delegate( events ) → EmitterMixinDelegateChain` _(inherited)_

Delegates selected events to another [`Emitter`](module_utils_emittermixin-Emitter.md). For instance:

```typescript
emitterA.delegate( 'eventX' ).to( emitterB );
emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
```

then `eventX` is delegated (fired by) `emitterB` and `emitterC` along with `data`:

```typescript
emitterA.fire( 'eventX', data );
```

and `eventY` is delegated (fired by) `emitterC` along with `data`:

```typescript
emitterA.fire( 'eventY', data );
```

#### Parameters

* `events: Array<string>`

  Event names that will be delegated to another emitter.

#### Returns

* `EmitterMixinDelegateChain`

<a id="function-destroy">

### `destroy() → void` _(experimental)_

Destroys the interaction. It marks the last reply as done and aborts the current request to the AI endpoint.

**Experimental:** This is a production-ready API but may change in minor releases without the standard deprecation policy. Check the changelog for migration guidance.

#### Returns

* `void`

<a id="function-fire">

### `fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]` _(inherited)_

Fires an event, executing all callbacks registered for it.

The first parameter passed to callbacks is an [`EventInfo`](module_utils_eventinfo-EventInfo.md) object, followed by the optional `args` provided in the `fire()` method call.

#### Type parameters

* `TEvent: extends BaseEvent`

  The type describing the event. See [`BaseEvent`](module_utils_emittermixin-BaseEvent.md).

#### Parameters

* `eventOrInfo: GetNameOrEventInfo<TEvent>`

  The name of the event or `EventInfo` object if event is delegated.

* `args: TEvent[ 'args' ]`

  Additional arguments to be passed to the callbacks.

#### Returns

* `GetEventInfo<TEvent>[ 'return' ]`

  By default the method returns `undefined`. However, the return value can be changed by listeners through modification of the [`evt.return`](module_utils_eventinfo-EventInfo.md#member-return)'s property (the event info is the first param of every callback).

<a id="function-getReply">

### `getReply( id ) → AIReply | undefined` _(experimental)_

Gets a reply by its ID.

**Experimental:** This is a production-ready API but may change in minor releases without the standard deprecation policy. Check the changelog for migration guidance.

#### Parameters

* `id: string`

#### Returns

* `AIReply | undefined`

<a id="function-listenTo:BASE_EMITTER">

### `listenTo( emitter, event, callback, options? ) → void` _(inherited)_

Registers a callback function to be executed when an event is fired in a specific (emitter) object.

Events can be grouped in namespaces using `:`. When namespaced event is fired, it additionally fires all callbacks for that namespace.

```typescript
// myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
myEmitter.on( 'myGroup', genericCallback );
myEmitter.on( 'myGroup:myEvent', specificCallback );

// genericCallback is fired.
myEmitter.fire( 'myGroup' );
// both genericCallback and specificCallback are fired.
myEmitter.fire( 'myGroup:myEvent' );
// genericCallback is fired even though there are no callbacks for "foo".
myEmitter.fire( 'myGroup:foo' );
```

An event callback can [stop the event](module_utils_eventinfo-EventInfo.md#member-stop) and set the [return value](module_utils_eventinfo-EventInfo.md#member-return) of the [`fire`](#function-fire) method.

#### Type parameters

* `TEvent: extends BaseEvent`

  The type describing the event. See [`BaseEvent`](module_utils_emittermixin-BaseEvent.md).

#### Parameters

* `emitter: Emitter`

  The object that fires the event.

* `event: TEvent[ 'name' ]`

  The name of the event.

* `callback: GetCallback<TEvent>`

  The function to be called on event.

* `options?: GetCallbackOptions<TEvent>`

  Additional options.

#### Returns

* `void`

<a id="function-off">

### `off( event, callback ) → void` _(inherited)_

Stops executing the callback on the given event. Shorthand for [`this.stopListening( this, event, callback )`](#function-stopListening:BASE_STOP).

#### Parameters

* `event: string`

  The name of the event.

* `callback: Function`

  The function to stop being called.

#### Returns

* `void`

<a id="function-on">

### `on( event, callback, options? ) → void` _(inherited)_

Registers a callback function to be executed when an event is fired.

Shorthand for [`this.listenTo( this, event, callback, options )`](#function-listenTo:BASE_EMITTER) (it makes the emitter listen on itself).

#### Type parameters

* `TEvent: extends BaseEvent`

  The type descibing the event. See [`BaseEvent`](module_utils_emittermixin-BaseEvent.md).

#### Parameters

* `event: TEvent[ 'name' ]`

  The name of the event.

* `callback: GetCallback<TEvent>`

  The function to be called on event.

* `options?: GetCallbackOptions<TEvent>`

  Additional options.

#### Returns

* `void`

<a id="function-once">

### `once( event, callback, options? ) → void` _(inherited)_

Registers a callback function to be executed on the next time the event is fired only. This is similar to calling [`on`](#function-on) followed by [`off`](#function-off) in the callback.

#### Type parameters

* `TEvent: extends BaseEvent`

  The type descibing the event. See [`BaseEvent`](module_utils_emittermixin-BaseEvent.md).

#### Parameters

* `event: TEvent[ 'name' ]`

  The name of the event.

* `callback: GetCallback<TEvent>`

  The function to be called on event.

* `options?: GetCallbackOptions<TEvent>`

  Additional options.

#### Returns

* `void`

<a id="function-stop">

### `stop() → void` _(experimental)_

Stops the interaction. It marks the last reply as done and aborts the current request to the AI endpoint.

**Experimental:** This is a production-ready API but may change in minor releases without the standard deprecation policy. Check the changelog for migration guidance.

#### Returns

* `void`

<a id="function-stopDelegating">

### `stopDelegating( event?, emitter? ) → void` _(inherited)_

Stops delegating events. It can be used at different levels:

* To stop delegating all events.
* To stop delegating a specific event to all emitters.
* To stop delegating a specific event to a specific emitter.

#### Parameters

* `event?: string`

  The name of the event to stop delegating. If omitted, stops it all delegations.

* `emitter?: Emitter`

  (requires `event`) The object to stop delegating a particular event to. If omitted, stops delegation of `event` to all emitters.

#### Returns

* `void`

<a id="function-stopListening:BASE_STOP">

### `stopListening( emitter?, event?, callback? ) → void` _(inherited)_

Stops listening for events. It can be used at different levels:

* To stop listening to a specific callback.
* To stop listening to a specific event.
* To stop listening to all events fired by a specific object.
* To stop listening to all events fired by all objects.

#### Parameters

* `emitter?: Emitter`

  The object to stop listening to. If omitted, stops it for all objects.

* `event?: string`

  (Requires the `emitter`) The name of the event to stop listening to. If omitted, stops it for all events from `emitter`.

* `callback?: Function`

  (Requires the `event`) The function to be removed from the call list for the given `event`.

#### Returns

* `void`

<a id="events">

## Events

<a id="event-interactionDestroyed">

### `interactionDestroyed( eventInfo, interaction )`

An event emitted by [`AIInteraction`](module_ai_aicore_model_aiinteraction-AIInteraction.md) when it is destroyed.

#### Parameters

* `eventInfo: EventInfo`

  An object containing information about the fired event.

* `interaction: TInteraction`

<a id="event-interactionFinished">

### `interactionFinished( eventInfo, interaction )`

An event emitted by [`AIInteraction`](module_ai_aicore_model_aiinteraction-AIInteraction.md) when an interaction ran out of content to process or crashed during processing.

#### Parameters

* `eventInfo: EventInfo`

  An object containing information about the fired event.

* `interaction: TInteraction`

<a id="event-interactionStarted">

### `interactionStarted( eventInfo, interaction )`

An event emitted by [`AIInteraction`](module_ai_aicore_model_aiinteraction-AIInteraction.md) when started, which means that the request to the AI endpoint has been sent.

#### Parameters

* `eventInfo: EventInfo`

  An object containing information about the fired event.

* `interaction: TInteraction`

<a id="event-interactionStopped">

### `interactionStopped( eventInfo, interaction )`

An event emitted by [`AIInteraction`](module_ai_aicore_model_aiinteraction-AIInteraction.md) when a user stopped the current interaction.

#### Parameters

* `eventInfo: EventInfo`

  An object containing information about the fired event.

* `interaction: TInteraction`

<a id="event-replyCreated">

### `replyCreated( eventInfo, reply )`

An event emitted by an [`AIInteraction`](module_ai_aicore_model_aiinteraction-AIInteraction.md) when an AI reply is added to the interaction (usually because received from the AI endpoint).

#### Parameters

* `eventInfo: EventInfo`

  An object containing information about the fired event.

* `reply: AIReply`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
