This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 41
FEATURE: Allow for persona & llm selection in bot conversations page #1276
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
331e68f
Move persona/llm selector to component
markvanlan 2214aa6
remove composer reliance.. simply some shiz
markvanlan 592db11
fix up the CSS
markvanlan 21c5e16
freaking css linting
markvanlan 2db0a16
Spec for choosing persona and llm
markvanlan 6d0ff19
review feedback - loading
markvanlan 611aa24
Adjust icons and add labels when showLabels is passed to component
markvanlan d3eb2a6
remove testing hack
markvanlan b69e530
Use the keyValueStore service
megothss 7fcbb0e
Update assets/javascripts/discourse/components/ai-persona-llm-selecto…
markvanlan cc72e09
Refactor the constructor of the AiPersonaSelector to split the logic …
megothss e28a4b3
Generate custom field server-side (capture all cases)
markvanlan 6611646
whitespace
markvanlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
assets/javascripts/discourse/components/ai-persona-llm-selector.gjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import Component from "@glimmer/component"; | ||
| import { tracked } from "@glimmer/tracking"; | ||
| import { hash } from "@ember/helper"; | ||
| import { next } from "@ember/runloop"; | ||
| import { service } from "@ember/service"; | ||
| import { i18n } from "discourse-i18n"; | ||
| import DropdownSelectBox from "select-kit/components/dropdown-select-box"; | ||
|
|
||
| const PERSONA_SELECTOR_KEY = "ai_persona_selector_id"; | ||
| const LLM_SELECTOR_KEY = "ai_llm_selector_id"; | ||
|
|
||
| export default class AiPersonaLlmSelector extends Component { | ||
| @service currentUser; | ||
| @service keyValueStore; | ||
|
|
||
| @tracked llm; | ||
| @tracked allowLLMSelector = true; | ||
|
|
||
| constructor() { | ||
| super(...arguments); | ||
|
|
||
| if (this.botOptions?.length) { | ||
| this.#loadStoredPersona(); | ||
| this.#loadStoredLlm(); | ||
|
|
||
| next(() => { | ||
| this.resetTargetRecipients(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| get composer() { | ||
| return this.args?.outletArgs?.model; | ||
| } | ||
|
|
||
| get hasLlmSelector() { | ||
| return this.currentUser.ai_enabled_chat_bots.any((bot) => !bot.is_persona); | ||
| } | ||
|
|
||
| get botOptions() { | ||
| if (!this.currentUser.ai_enabled_personas) { | ||
| return; | ||
| } | ||
|
|
||
| let enabledPersonas = this.currentUser.ai_enabled_personas; | ||
|
|
||
| if (!this.hasLlmSelector) { | ||
| enabledPersonas = enabledPersonas.filter((persona) => persona.username); | ||
| } | ||
|
|
||
| return enabledPersonas.map((persona) => { | ||
| return { | ||
| id: persona.id, | ||
| name: persona.name, | ||
| description: persona.description, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| get filterable() { | ||
| return this.botOptions.length > 8; | ||
| } | ||
|
|
||
| get value() { | ||
| return this._value; | ||
| } | ||
|
|
||
| set value(newValue) { | ||
| this._value = newValue; | ||
| this.keyValueStore.setItem(PERSONA_SELECTOR_KEY, newValue); | ||
| this.args.setPersonaId(newValue); | ||
| this.setAllowLLMSelector(); | ||
| this.resetTargetRecipients(); | ||
| } | ||
|
|
||
| setAllowLLMSelector() { | ||
| if (!this.hasLlmSelector) { | ||
| this.allowLLMSelector = false; | ||
| return; | ||
| } | ||
|
|
||
| const persona = this.currentUser.ai_enabled_personas.find( | ||
| (innerPersona) => innerPersona.id === this._value | ||
| ); | ||
|
|
||
| this.allowLLMSelector = !persona?.force_default_llm; | ||
| } | ||
|
|
||
| get currentLlm() { | ||
| return this.llm; | ||
| } | ||
|
|
||
| set currentLlm(newValue) { | ||
| this.llm = newValue; | ||
| this.keyValueStore.setItem(LLM_SELECTOR_KEY, newValue); | ||
|
|
||
| this.resetTargetRecipients(); | ||
| } | ||
|
|
||
| resetTargetRecipients() { | ||
| if (this.allowLLMSelector) { | ||
| const botUsername = this.currentUser.ai_enabled_chat_bots.find( | ||
| (bot) => bot.id === this.llm | ||
| ).username; | ||
| this.args.setTargetRecipient(botUsername); | ||
| } else { | ||
| const persona = this.currentUser.ai_enabled_personas.find( | ||
| (innerPersona) => innerPersona.id === this._value | ||
| ); | ||
| this.args.setTargetRecipient(persona.username || ""); | ||
| } | ||
| } | ||
|
|
||
| get llmOptions() { | ||
| const availableBots = this.currentUser.ai_enabled_chat_bots | ||
| .filter((bot) => !bot.is_persona) | ||
| .filter(Boolean); | ||
|
|
||
| return availableBots | ||
| .map((bot) => { | ||
| return { | ||
| id: bot.id, | ||
| name: bot.display_name, | ||
| }; | ||
| }) | ||
| .sort((a, b) => a.name.localeCompare(b.name)); | ||
| } | ||
|
|
||
| get showLLMSelector() { | ||
| return this.allowLLMSelector && this.llmOptions.length > 1; | ||
| } | ||
|
|
||
| #loadStoredPersona() { | ||
| let personaId = this.keyValueStore.getItem(PERSONA_SELECTOR_KEY); | ||
|
|
||
| this._value = this.botOptions[0].id; | ||
| if (personaId) { | ||
| personaId = parseInt(personaId, 10); | ||
| if (this.botOptions.any((bot) => bot.id === personaId)) { | ||
| this._value = personaId; | ||
| } | ||
| } | ||
|
|
||
| this.args.setPersonaId(this._value); | ||
| } | ||
|
|
||
| #loadStoredLlm() { | ||
| this.setAllowLLMSelector(); | ||
|
|
||
| if (this.hasLlmSelector) { | ||
| let llm = this.keyValueStore.getItem(LLM_SELECTOR_KEY); | ||
|
|
||
| const llmOption = | ||
| this.llmOptions.find((innerLlmOption) => innerLlmOption.id === llm) || | ||
| this.llmOptions[0]; | ||
|
|
||
| if (llmOption) { | ||
| llm = llmOption.id; | ||
| } else { | ||
| llm = ""; | ||
| } | ||
|
|
||
| if (llm) { | ||
| next(() => { | ||
| this.currentLlm = llm; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| <template> | ||
| <div class="persona-llm-selector"> | ||
| <div class="persona-llm-selector__selection-wrapper gpt-persona"> | ||
| {{#if @showLabels}} | ||
| <label>{{i18n "discourse_ai.ai_bot.persona"}}</label> | ||
| {{/if}} | ||
| <DropdownSelectBox | ||
| class="persona-llm-selector__persona-dropdown" | ||
| @value={{this.value}} | ||
| @content={{this.botOptions}} | ||
| @options={{hash | ||
| icon=(if @showLabels "angle-down" "robot") | ||
| filterable=this.filterable | ||
| }} | ||
| /> | ||
| </div> | ||
| {{#if this.showLLMSelector}} | ||
| <div class="persona-llm-selector__selection-wrapper llm-selector"> | ||
| {{#if @showLabels}} | ||
| <label>{{i18n "discourse_ai.ai_bot.llm"}}</label> | ||
| {{/if}} | ||
| <DropdownSelectBox | ||
| class="persona-llm-selector__llm-dropdown" | ||
| @value={{this.currentLlm}} | ||
| @content={{this.llmOptions}} | ||
| @options={{hash icon=(if @showLabels "angle-down" "globe")}} | ||
| /> | ||
| </div> | ||
| {{/if}} | ||
| </div> | ||
| </template> | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are the big changes in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing just copy/pasted into its own component?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply abstracting out the "setting" of values -- they're passed in now. instead of
this.composer.metaData["personaId"] = idwe havethis.args.setPersonaId(id)