@assistant-ui/react
Version:
React components for AI chat.
268 lines • 8.08 kB
JavaScript
// src/api/ComposerRuntime.ts
import { LazyMemoizeSubject } from "./subscribable/LazyMemoizeSubject.mjs";
import {
EditComposerAttachmentRuntimeImpl,
ThreadComposerAttachmentRuntimeImpl
} from "./AttachmentRuntime.mjs";
import { ShallowMemoizeSubject } from "./subscribable/ShallowMemoizeSubject.mjs";
import { SKIP_UPDATE } from "./subscribable/SKIP_UPDATE.mjs";
var METHOD_NOT_SUPPORTED = () => {
throw new Error("Composer is not available");
};
var EMPTY_ARRAY = Object.freeze([]);
var getThreadComposerState = (runtime) => {
return Object.freeze({
type: "thread",
isEditing: runtime?.isEditing ?? false,
canCancel: runtime?.canCancel ?? false,
isEmpty: runtime?.isEmpty ?? true,
text: runtime?.text ?? "",
attachments: runtime?.attachments ?? EMPTY_ARRAY,
role: runtime?.role ?? "user",
value: runtime?.text ?? "",
setValue: runtime?.setText.bind(runtime) ?? METHOD_NOT_SUPPORTED,
setText: runtime?.setText.bind(runtime) ?? METHOD_NOT_SUPPORTED,
// edit: beginEdit,
send: runtime?.send.bind(runtime) ?? METHOD_NOT_SUPPORTED,
cancel: runtime?.cancel.bind(runtime) ?? METHOD_NOT_SUPPORTED,
reset: runtime?.reset.bind(runtime) ?? METHOD_NOT_SUPPORTED,
addAttachment: runtime?.addAttachment.bind(runtime) ?? METHOD_NOT_SUPPORTED,
removeAttachment: runtime?.removeAttachment.bind(runtime) ?? METHOD_NOT_SUPPORTED
});
};
var getEditComposerState = (runtime, beginEdit) => {
return Object.freeze({
type: "edit",
isEditing: runtime?.isEditing ?? false,
canCancel: runtime?.canCancel ?? false,
isEmpty: runtime?.isEmpty ?? true,
text: runtime?.text ?? "",
attachments: runtime?.attachments ?? EMPTY_ARRAY,
role: runtime?.role ?? "user",
value: runtime?.text ?? "",
setValue: runtime?.setText.bind(runtime) ?? METHOD_NOT_SUPPORTED,
setText: runtime?.setText.bind(runtime) ?? METHOD_NOT_SUPPORTED,
edit: beginEdit,
send: runtime?.send.bind(runtime) ?? METHOD_NOT_SUPPORTED,
cancel: runtime?.cancel.bind(runtime) ?? METHOD_NOT_SUPPORTED
});
};
var ComposerRuntimeImpl = class {
constructor(_core) {
this._core = _core;
}
get path() {
return this._core.path;
}
/**
* @deprecated Use `getState().isEditing` instead. This will be removed in 0.6.0.
*/
get isEditing() {
return this.getState().isEditing;
}
/**
* @deprecated Use `getState().isEmpty` instead. This will be removed in 0.6.0.
*/
get isEmpty() {
return this.getState().isEmpty;
}
/**
* @deprecated Use `getState().canCancel` instead. This will be removed in 0.6.0.
*/
get canCancel() {
return this.getState().canCancel;
}
/**
* @deprecated Use `getState().text` instead. This will be removed in 0.6.0.
*/
get text() {
return this.getState().text;
}
/**
* @deprecated Use `getState().role` instead. This will be removed in 0.6.0.
*/
get role() {
return this.getState().role;
}
/**
* @deprecated Use `getState().attachments` instead. This will be removed in 0.6.0.
*/
get attachments() {
return this.getState().attachments;
}
/**
* @deprecated Use `getState().text` instead. This will be removed in 0.6.0.
*/
get value() {
return this.text;
}
setText(text) {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
core.setText(text);
}
setValue(text) {
this.setText(text);
}
addAttachment(file) {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
return core.addAttachment(file);
}
/**
* @deprecated Use `getAttachmentById(id).removeAttachment()` instead. This will be removed in 0.6.0.
*/
removeAttachment(attachmentId) {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
return core.removeAttachment(attachmentId);
}
/**
* @deprecated This method will be removed in 0.6.0. Submit feedback if you need this functionality.
*/
reset() {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
core.reset();
}
send() {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
core.send();
}
cancel() {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
core.cancel();
}
setRole(role) {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
core.setRole(role);
}
subscribe(callback) {
return this._core.subscribe(callback);
}
getAttachmentAccept() {
const core = this._core.getState();
if (!core) throw new Error("Composer is not available");
return core.getAttachmentAccept();
}
};
var ThreadComposerRuntimeImpl = class extends ComposerRuntimeImpl {
get path() {
return this._core.path;
}
get type() {
return "thread";
}
_getState;
constructor(core) {
const stateBinding = new LazyMemoizeSubject({
path: core.path,
getState: () => getThreadComposerState(core.getState()),
subscribe: (callback) => core.subscribe(callback)
});
super({
path: core.path,
getState: () => core.getState(),
subscribe: (callback) => stateBinding.subscribe(callback)
});
this._getState = stateBinding.getState.bind(stateBinding);
}
get attachments() {
return this.getState()?.attachments ?? EMPTY_ARRAY;
}
getState() {
return this._getState();
}
getAttachmentByIndex(idx) {
return new ThreadComposerAttachmentRuntimeImpl(
new ShallowMemoizeSubject({
path: {
...this.path,
attachmentSource: "thread-composer",
attachmentSelector: { type: "index", index: idx },
ref: this.path.ref + `${this.path.ref}.attachments[${idx}]`
},
getState: () => {
const attachments = this.getState().attachments;
const attachment = attachments[idx];
if (!attachment) return SKIP_UPDATE;
return {
...attachment,
attachment,
source: "thread-composer"
};
},
subscribe: (callback) => this._core.subscribe(callback)
}),
this._core
);
}
};
var EditComposerRuntimeImpl = class extends ComposerRuntimeImpl {
constructor(core, _beginEdit) {
const stateBinding = new LazyMemoizeSubject({
path: core.path,
getState: () => getEditComposerState(core.getState(), this._beginEdit),
subscribe: (callback) => core.subscribe(callback)
});
super({
path: core.path,
getState: () => core.getState(),
subscribe: (callback) => stateBinding.subscribe(callback)
});
this._beginEdit = _beginEdit;
this._getState = stateBinding.getState.bind(stateBinding);
}
get path() {
return this._core.path;
}
get type() {
return "edit";
}
_getState;
getState() {
return this._getState();
}
beginEdit() {
this._beginEdit();
}
/**
* @deprecated Use `beginEdit()` instead. This will be removed in 0.6.0.
*/
edit() {
this.beginEdit();
}
getAttachmentByIndex(idx) {
return new EditComposerAttachmentRuntimeImpl(
new ShallowMemoizeSubject({
path: {
...this.path,
attachmentSource: "edit-composer",
attachmentSelector: { type: "index", index: idx },
ref: this.path.ref + `${this.path.ref}.attachments[${idx}]`
},
getState: () => {
const attachments = this.getState().attachments;
const attachment = attachments[idx];
if (!attachment) return SKIP_UPDATE;
return {
...attachment,
attachment,
source: "edit-composer"
};
},
subscribe: (callback) => this._core.subscribe(callback)
}),
this._core
);
}
};
export {
ComposerRuntimeImpl,
EditComposerRuntimeImpl,
ThreadComposerRuntimeImpl
};
//# sourceMappingURL=ComposerRuntime.mjs.map