Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions assets/javascripts/discourse/components/ai-persona-llm-selector.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import Component from "@glimmer/component";
Copy link
Contributor

@jjaffeux jjaffeux Apr 23, 2025

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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"] = id we have this.args.setPersonaId(id)

import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { next } from "@ember/runloop";
import { service } from "@ember/service";
import KeyValueStore from "discourse/lib/key-value-store";
import DropdownSelectBox from "select-kit/components/dropdown-select-box";

export default class AiPersonaLlmSelector extends Component {
@service currentUser;

@tracked llm;
@tracked allowLLMSelector = true;

STORE_NAMESPACE = "discourse_ai_persona_selector_";
LLM_STORE_NAMESPACE = "discourse_ai_llm_selector_";

preferredPersonaStore = new KeyValueStore(this.STORE_NAMESPACE);
preferredLlmStore = new KeyValueStore(this.LLM_STORE_NAMESPACE);

constructor() {
super(...arguments);

if (this.botOptions && this.botOptions.length) {
let personaId = this.preferredPersonaStore.getObject("id");

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);
this.setAllowLLMSelector();

if (this.hasLlmSelector) {
let llm = this.preferredLlmStore.getObject("id");

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;
});
}
}

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) {
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 > 4;
}

get value() {
return this._value;
}

set value(newValue) {
this._value = newValue;
this.preferredPersonaStore.setObject({ key: "id", value: 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.preferredLlmStore.setObject({ key: "id", value: 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));
}

<template>
<div class="persona-llm-selector">
<div class="gpt-persona">
<DropdownSelectBox
class="persona-llm-selector__persona-dropdown"
@value={{this.value}}
@content={{this.botOptions}}
@options={{hash icon="robot" filterable=this.filterable}}
/>
</div>
{{#if this.allowLLMSelector}}
<div class="llm-selector">
<DropdownSelectBox
class="persona-llm-selector__llm-dropdown"
@value={{this.currentLlm}}
@content={{this.llmOptions}}
@options={{hash icon="globe"}}
/>
</div>
{{/if}}
</div>
</template>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { next } from "@ember/runloop";
import { action } from "@ember/object";
import { service } from "@ember/service";
import KeyValueStore from "discourse/lib/key-value-store";
import DropdownSelectBox from "select-kit/components/dropdown-select-box";
import AiPersonaLlmSelector from "discourse/plugins/discourse-ai/discourse/components/ai-persona-llm-selector";

function isBotMessage(composer, currentUser) {
if (
Expand All @@ -30,175 +27,21 @@ export default class BotSelector extends Component {
}

@service currentUser;
@service siteSettings;

@tracked llm;
@tracked allowLLMSelector = true;

STORE_NAMESPACE = "discourse_ai_persona_selector_";
LLM_STORE_NAMESPACE = "discourse_ai_llm_selector_";

preferredPersonaStore = new KeyValueStore(this.STORE_NAMESPACE);
preferredLlmStore = new KeyValueStore(this.LLM_STORE_NAMESPACE);

constructor() {
super(...arguments);

if (this.botOptions && this.botOptions.length && this.composer) {
let personaId = this.preferredPersonaStore.getObject("id");

this._value = this.botOptions[0].id;
if (personaId) {
personaId = parseInt(personaId, 10);
if (this.botOptions.any((bot) => bot.id === personaId)) {
this._value = personaId;
}
}

this.composer.metaData = { ai_persona_id: this._value };
this.setAllowLLMSelector();

if (this.hasLlmSelector) {
let llm = this.preferredLlmStore.getObject("id");

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;
});
}
}

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) {
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,
};
});
}
@action
setPersonaIdOnComposer(id) {
this.args.outletArgs.model.metaData = { ai_persona_id: id };
}

get filterable() {
return this.botOptions.length > 4;
}

get value() {
return this._value;
}

set value(newValue) {
this._value = newValue;
this.preferredPersonaStore.setObject({ key: "id", value: newValue });
this.composer.metaData = { ai_persona_id: 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.preferredLlmStore.setObject({ key: "id", value: newValue });

this.resetTargetRecipients();
}

resetTargetRecipients() {
if (this.allowLLMSelector) {
const botUsername = this.currentUser.ai_enabled_chat_bots.find(
(bot) => bot.id === this.llm
).username;
this.composer.set("targetRecipients", botUsername);
} else {
const persona = this.currentUser.ai_enabled_personas.find(
(innerPersona) => innerPersona.id === this._value
);
this.composer.set("targetRecipients", 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));
@action
setTargetRecipientsOnComposer(username) {
this.args.outletArgs.model.set("targetRecipients", username);
}

<template>
<div class="persona-llm-selector">
<div class="gpt-persona">
<DropdownSelectBox
class="persona-llm-selector__persona-dropdown"
@value={{this.value}}
@content={{this.botOptions}}
@options={{hash icon="robot" filterable=this.filterable}}
/>
</div>
{{#if this.allowLLMSelector}}
<div class="llm-selector">
<DropdownSelectBox
class="persona-llm-selector__llm-dropdown"
@value={{this.currentLlm}}
@content={{this.llmOptions}}
@options={{hash icon="globe"}}
/>
</div>
{{/if}}
</div>
<AiPersonaLlmSelector
@setPersonaId={{this.setPersonaIdOnComposer}}
@setTargetRecipient={{this.setTargetRecipientsOnComposer}}
/>
</template>
}
Loading