import { tracked } from "@glimmer/tracking"; import Component, { Input } from "@ember/component"; import { fn } from "@ember/helper"; import { on } from "@ember/modifier"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; import DButton from "discourse/components/d-button"; import { ajax } from "discourse/lib/ajax"; import UppyUploadMixin from "discourse/mixins/uppy-upload"; import icon from "discourse-common/helpers/d-icon"; import discourseDebounce from "discourse-common/lib/debounce"; import I18n from "discourse-i18n"; import RagUploadProgress from "./rag-upload-progress"; export default class PersonaRagUploader extends Component.extend( UppyUploadMixin ) { @service appEvents; @tracked term = null; @tracked filteredUploads = null; @tracked ragIndexingStatuses = null; id = "discourse-ai-persona-rag-uploader"; maxFiles = 20; uploadUrl = "/admin/plugins/discourse-ai/ai-personas/files/upload"; preventDirectS3Uploads = true; didReceiveAttrs() { super.didReceiveAttrs(...arguments); if (this.inProgressUploads?.length > 0) { this._uppyInstance?.cancelAll(); } this.filteredUploads = this.ragUploads || []; if (this.ragUploads?.length) { ajax( `/admin/plugins/discourse-ai/ai-personas/${this.persona.id}/files/status.json` ).then((statuses) => { this.set("ragIndexingStatuses", statuses); }); } } uploadDone(uploadedFile) { this.onAdd(uploadedFile.upload); this.debouncedSearch(); } @action submitFiles() { this.fileInputEl.click(); } @action cancelUploading(upload) { this.appEvents.trigger(`upload-mixin:${this.id}:cancel-upload`, { fileId: upload.id, }); } @action search() { if (this.term) { this.filteredUploads = this.ragUploads.filter((u) => { return ( u.original_filename.toUpperCase().indexOf(this.term.toUpperCase()) > -1 ); }); } else { this.filteredUploads = this.ragUploads; } } @action debouncedSearch() { discourseDebounce(this, this.search, 100); } }