@assistant-ui/react
Version:
React components for AI chat.
93 lines • 2.77 kB
JavaScript
// src/runtimes/composer/BaseComposerRuntimeCore.tsx
var isAttachmentComplete = (a) => a.status.type === "complete";
var BaseComposerRuntimeCore = class {
isEditing = true;
getAttachmentAccept() {
return this.getAttachmentAdapter()?.accept ?? "*";
}
_attachments = [];
get attachments() {
return this._attachments;
}
setAttachments(value) {
this._attachments = value;
this.notifySubscribers();
}
get isEmpty() {
return !this.text.trim() && !this.attachments.length;
}
_text = "";
get text() {
return this._text;
}
_role = "user";
get role() {
return this._role;
}
setRole(role) {
this._role = role;
this.notifySubscribers();
}
setText(value) {
this._text = value;
this.notifySubscribers();
}
reset() {
this._text = "";
this._role = "user";
this._attachments = [];
this.notifySubscribers();
}
async send() {
const adapter = this.getAttachmentAdapter();
const attachments = adapter && this.attachments.length > 0 ? await Promise.all(
this.attachments.map(async (a) => {
if (isAttachmentComplete(a)) return a;
const result = await adapter.send(a);
if (result.status?.type !== "complete") {
result.status = { type: "complete" };
}
return result;
})
) : [];
const message = {
role: this.role,
content: this.text ? [{ type: "text", text: this.text }] : [],
attachments
};
this.reset();
this.handleSend(message);
}
async addAttachment(file) {
const adapter = this.getAttachmentAdapter();
if (!adapter) throw new Error("Attachments are not supported");
const attachment = await adapter.add({ file });
if (attachment.status === void 0) {
attachment.status = { type: "requires-action", reason: "composer-send" };
}
this._attachments = [...this._attachments, attachment];
this.notifySubscribers();
}
async removeAttachment(attachmentId) {
const adapter = this.getAttachmentAdapter();
if (!adapter) throw new Error("Attachments are not supported");
const index = this._attachments.findIndex((a) => a.id === attachmentId);
if (index === -1) throw new Error("Attachment not found");
const attachment = this._attachments[index];
await adapter.remove(attachment);
this._attachments = this._attachments.toSpliced(index, 1);
this.notifySubscribers();
}
_subscriptions = /* @__PURE__ */ new Set();
notifySubscribers() {
for (const callback of this._subscriptions) callback();
}
subscribe(callback) {
this._subscriptions.add(callback);
return () => this._subscriptions.delete(callback);
}
};
export {
BaseComposerRuntimeCore
};
//# sourceMappingURL=BaseComposerRuntimeCore.mjs.map