Skip to content

OpenAIChatKit

A Web Component that serves as the entry point for a ChatKit integration.

  • HTMLElement
addEventListener<K>(
type: K,
listener: EventHandler<K>,
options?: boolean | AddEventListenerOptions): void;

Adds an event listener for the specified ChatKitEvents event.

Type Parameter

K extends keyof ChatKitEvents

Parameter Type Description

type

K

See ChatKitEvents for available events.

listener

EventHandler<K>

The event listener callback.

options?

boolean | AddEventListenerOptions

An object that specifies characteristics about the event listener.

See

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options

void

chatKit.addEventListener('chatkit.error', (event) => {
logToMyErrorLogger(event.detail.error);
});

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

HTMLElement.addEventListener;
addEventListener(
type: string,
listener: null | EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions): void;

Internal

Parameter Type

type

string

listener

null | EventListenerOrEventListenerObject

options?

boolean | AddEventListenerOptions

void

HTMLElement.addEventListener;

fetchUpdates(): Promise<void>;

Manually fetches updates from the server.

Use this when you’ve manually updated the thread or thread items and need to sync them with the client.

Promise<void>


focusComposer(): Promise<void>;

Focuses the composer input field.

Promise<void>


removeEventListener<K>(
type: K,
listener: EventHandler<K>,
options?: boolean | EventListenerOptions): void;

Removes an event listener for the specified event.

Type Parameter

K extends keyof ChatKitEvents

Parameter Type Description

type

K

See ChatKitEvents for available events.

listener

EventHandler<K>

The event listener callback to remove.

options?

boolean | EventListenerOptions

An object that specifies characteristics about the event listener.

See

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener#options

void

chatKit.removeEventListener('chatkit.error', myErrorListener);

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener

HTMLElement.removeEventListener;
removeEventListener(
type: string,
listener: null | EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions): void;

Internal

Parameter Type

type

string

listener

null | EventListenerOrEventListenerObject

options?

boolean | EventListenerOptions

void

HTMLElement.removeEventListener;

sendCustomAction(action: {
payload?: Record<string, unknown>;
type: string;
}, itemId?: string): Promise<void>;

Sends a custom application-defined action to your backend. See https://openai.github.io/chatkit/guides/widget-actions/ for more details.

Parameter Type Description

action

{ payload?: Record<string, unknown>; type: string; }

action.payload?

Record<string, unknown>

action.type?

string

itemId?

string

The ID of the WidgetItem that the action is associated with. You may need this if the action was triggered by a widget, gets handled client-side, and then you want to send the action back to the server to do additional handling.

Example

chatKit.options = {
// other options...
widgets: {
async onAction(action, widgetItem) {
await someClientSideHandling(action);
await chatkit.sendAction(action, widgetItem.id);
},
},
};

Promise<void>


sendUserMessage(params: {
attachments?: Attachment[];
newThread?: boolean;
reply?: string;
text: string;
}): Promise<void>;

Sends a user message.

Parameter Type

params

{ attachments?: Attachment[]; newThread?: boolean; reply?: string; text: string; }

params.attachments?

Attachment[]

params.newThread?

boolean

params.reply?

string

params.text

string

Promise<void>


setComposerValue(params: {
attachments?: Attachment[];
reply?: string;
text: string;
}): Promise<void>;

Sets the composer’s content without sending a message.

Parameter Type

params

{ attachments?: Attachment[]; reply?: string; text: string; }

params.attachments?

Attachment[]

params.reply?

string

params.text

string

Promise<void>


setOptions(options: ChatKitOptions): void;

Applies configuration options to the ChatKit instance.

IMPORTANT: New options are not merged with the existing options. You must provide a full set of options every time you call this method.

Parameter Type

options

ChatKitOptions

void


setThreadId(threadId: null | string): Promise<void>;

Changes the active thread. Pass null to switch to a new thread.

Parameter Type

threadId

null | string

Promise<void>