From 98c89953d3c725646d4c9b9051107f5abfe375da Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 21 Nov 2023 18:02:27 +1100 Subject: [PATCH] FEATURE: remember previously selected persona (#299) People tend to keep to 1 persona when working with the bot, this adds local browser memory for the last persona you interacted with so you do not need to select it over and over again. This is per browser, not per user memory. Also... clean up tests so they do not need to require stubs which were breaking the build --------- Co-authored-by: Martin Brennan --- .../composer-fields/persona-selector.gjs | 22 +++++++++++++++---- plugin.rb | 1 + .../jobs/generate_embeddings_spec.rb | 2 -- .../embeddings/semantic_search_spec.rb | 2 -- .../all_mpnet_base_v2_spec.rb | 1 - .../multilingual_e5_large_spec.rb | 1 - .../text_embedding_ada_002_spec.rb | 1 - spec/system/ai_bot/persona_spec.rb | 18 ++++++++++++++- .../system/embeddings/semantic_search_spec.rb | 3 --- 9 files changed, 36 insertions(+), 15 deletions(-) diff --git a/assets/javascripts/discourse/connectors/composer-fields/persona-selector.gjs b/assets/javascripts/discourse/connectors/composer-fields/persona-selector.gjs index 80ee7948..e15922b0 100644 --- a/assets/javascripts/discourse/connectors/composer-fields/persona-selector.gjs +++ b/assets/javascripts/discourse/connectors/composer-fields/persona-selector.gjs @@ -1,6 +1,7 @@ import Component from "@glimmer/component"; import { hash } from "@ember/helper"; import { inject as service } from "@ember/service"; +import KeyValueStore from "discourse/lib/key-value-store"; import DropdownSelectBox from "select-kit/components/dropdown-select-box"; function isBotMessage(composer, currentUser) { @@ -27,10 +28,21 @@ export default class BotSelector extends Component { } @service currentUser; + + STORE_NAMESPACE = "discourse_ai_persona_selector_"; + preferredPersonaStore = new KeyValueStore(this.STORE_NAMESPACE); + constructor() { super(...arguments); + if (this.botOptions && this.composer) { - this._value = this.botOptions[0].id; + const personaId = this.preferredPersonaStore.getObject("id"); + if (personaId) { + this._value = parseInt(personaId, 10); + } else { + this._value = this.botOptions[0].personaId; + } + this.composer.metaData = { ai_persona_id: this._value }; } } @@ -55,14 +67,16 @@ export default class BotSelector extends Component { return this._value; } - set value(val) { - this._value = val; - this.composer.metaData = { ai_persona_id: val }; + set value(newValue) { + this._value = newValue; + this.preferredPersonaStore.setObject({ key: "id", value: newValue }); + this.composer.metaData = { ai_persona_id: newValue }; }