2024-05-13 11:46:42 -04:00
|
|
|
import Component from "@glimmer/component";
|
2024-06-19 01:49:36 -04:00
|
|
|
import { concat, fn } from "@ember/helper";
|
|
|
|
import { on } from "@ember/modifier";
|
|
|
|
import { action } from "@ember/object";
|
2024-05-13 11:46:42 -04:00
|
|
|
import { LinkTo } from "@ember/routing";
|
2024-07-09 20:56:13 -04:00
|
|
|
import { inject as service } from "@ember/service";
|
|
|
|
import DBreadcrumbsItem from "discourse/components/d-breadcrumbs-item";
|
2024-06-19 01:49:36 -04:00
|
|
|
import DToggleSwitch from "discourse/components/d-toggle-switch";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-05-13 11:46:42 -04:00
|
|
|
import icon from "discourse-common/helpers/d-icon";
|
|
|
|
import i18n from "discourse-common/helpers/i18n";
|
|
|
|
import I18n from "discourse-i18n";
|
2024-09-30 03:15:11 -04:00
|
|
|
import AdminPageSubheader from "admin/components/admin-page-subheader";
|
2024-10-02 01:31:48 -04:00
|
|
|
import AdminSectionLandingItem from "admin/components/admin-section-landing-item";
|
|
|
|
import AdminSectionLandingWrapper from "admin/components/admin-section-landing-wrapper";
|
2024-05-13 11:46:42 -04:00
|
|
|
import AiLlmEditor from "./ai-llm-editor";
|
|
|
|
|
|
|
|
export default class AiLlmsListEditor extends Component {
|
2024-07-09 20:56:13 -04:00
|
|
|
@service adminPluginNavManager;
|
2024-09-30 03:15:11 -04:00
|
|
|
@service router;
|
2024-07-09 20:56:13 -04:00
|
|
|
|
2024-09-30 03:15:11 -04:00
|
|
|
@action
|
|
|
|
modelDescription(llm) {
|
|
|
|
// this is a bit of an odd object, it can be an llm model or a preset model
|
|
|
|
// handle both flavors
|
|
|
|
|
|
|
|
// in the case of model
|
|
|
|
let key = "";
|
|
|
|
if (typeof llm.id === "number") {
|
|
|
|
key = `${llm.provider}-${llm.name}`;
|
|
|
|
} else {
|
|
|
|
// case of preset
|
|
|
|
key = llm.id.replace(/\./g, "-");
|
|
|
|
}
|
|
|
|
|
|
|
|
key = `discourse_ai.llms.model_description.${key}`;
|
|
|
|
if (I18n.lookup(key, { ignoreMissing: true })) {
|
|
|
|
return I18n.t(key);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitizedTranslationKey(id) {
|
|
|
|
return id.replace(/\./g, "-");
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasLlmElements() {
|
2024-05-16 13:28:57 -04:00
|
|
|
return this.args.llms.length !== 0;
|
2024-05-13 11:46:42 -04:00
|
|
|
}
|
|
|
|
|
2024-09-30 03:15:11 -04:00
|
|
|
get preconfiguredTitle() {
|
|
|
|
if (this.hasLlmElements) {
|
|
|
|
return "discourse_ai.llms.preconfigured.title";
|
|
|
|
} else {
|
|
|
|
return "discourse_ai.llms.preconfigured.title_no_llms";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get preConfiguredLlms() {
|
|
|
|
const options = [
|
|
|
|
{
|
|
|
|
id: "none",
|
|
|
|
name: I18n.t("discourse_ai.llms.preconfigured.fake"),
|
|
|
|
provider: "fake",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const llmsContent = this.args.llms.content.map((llm) => ({
|
|
|
|
provider: llm.provider,
|
|
|
|
name: llm.name,
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.args.llms.resultSetMeta.presets.forEach((llm) => {
|
|
|
|
if (llm.models) {
|
|
|
|
llm.models.forEach((model) => {
|
|
|
|
const id = `${llm.id}-${model.name}`;
|
|
|
|
const isConfigured = llmsContent.some(
|
|
|
|
(content) =>
|
|
|
|
content.provider === llm.provider && content.name === model.name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isConfigured) {
|
|
|
|
options.push({
|
|
|
|
id,
|
|
|
|
name: model.display_name,
|
|
|
|
provider: llm.provider,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
transitionToLlmEditor(llmTemplate) {
|
|
|
|
this.router.transitionTo("adminPlugins.show.discourse-ai-llms.new", {
|
|
|
|
queryParams: { llmTemplate },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-19 01:49:36 -04:00
|
|
|
@action
|
|
|
|
async toggleEnabledChatBot(llm) {
|
|
|
|
const oldValue = llm.enabled_chat_bot;
|
|
|
|
const newValue = !oldValue;
|
|
|
|
try {
|
|
|
|
llm.set("enabled_chat_bot", newValue);
|
|
|
|
await llm.update({
|
|
|
|
enabled_chat_bot: newValue,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
llm.set("enabled_chat_bot", oldValue);
|
|
|
|
popupAjaxError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 11:46:42 -04:00
|
|
|
<template>
|
2024-07-09 20:56:13 -04:00
|
|
|
<DBreadcrumbsItem
|
|
|
|
@path="/admin/plugins/{{this.adminPluginNavManager.currentPlugin.name}}/ai-llms"
|
|
|
|
@label={{i18n "discourse_ai.llms.short_title"}}
|
|
|
|
/>
|
2024-09-30 03:15:11 -04:00
|
|
|
<section class="ai-llm-list-editor admin-detail">
|
2024-05-16 13:28:57 -04:00
|
|
|
{{#if @currentLlm}}
|
2024-09-30 03:15:11 -04:00
|
|
|
<AiLlmEditor
|
|
|
|
@model={{@currentLlm}}
|
|
|
|
@llms={{@llms}}
|
|
|
|
@llmTemplate={{@llmTemplate}}
|
|
|
|
/>
|
2024-05-16 13:28:57 -04:00
|
|
|
{{else}}
|
2024-09-30 03:15:11 -04:00
|
|
|
{{#if this.hasLlmElements}}
|
|
|
|
<section class="ai-llms-list-editor__configured">
|
|
|
|
<AdminPageSubheader
|
|
|
|
@titleLabel="discourse_ai.llms.configured.title"
|
|
|
|
/>
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>{{i18n "discourse_ai.llms.display_name"}}</th>
|
|
|
|
<th>{{i18n "discourse_ai.llms.provider"}}</th>
|
|
|
|
<th>{{i18n "discourse_ai.llms.enabled_chat_bot"}}</th>
|
|
|
|
<th></th>
|
2024-05-16 13:28:57 -04:00
|
|
|
</tr>
|
2024-09-30 03:15:11 -04:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{{#each @llms as |llm|}}
|
|
|
|
<tr data-persona-id={{llm.id}} class="ai-llm-list__row">
|
|
|
|
<td class="column-name">
|
|
|
|
<h3>{{llm.display_name}}</h3>
|
|
|
|
<p>
|
|
|
|
{{this.modelDescription llm}}
|
|
|
|
</p>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
{{i18n
|
|
|
|
(concat "discourse_ai.llms.providers." llm.provider)
|
|
|
|
}}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<DToggleSwitch
|
|
|
|
@state={{llm.enabled_chat_bot}}
|
|
|
|
{{on "click" (fn this.toggleEnabledChatBot llm)}}
|
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
<td class="column-edit">
|
|
|
|
<LinkTo
|
|
|
|
@route="adminPlugins.show.discourse-ai-llms.show"
|
|
|
|
class="btn btn-default"
|
|
|
|
@model={{llm.id}}
|
|
|
|
>
|
|
|
|
{{icon "wrench"}}
|
|
|
|
<div class="d-button-label">
|
|
|
|
{{i18n "discourse_ai.llms.edit"}}
|
|
|
|
</div>
|
|
|
|
</LinkTo>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
{{/each}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</section>
|
2024-05-13 11:46:42 -04:00
|
|
|
{{/if}}
|
2024-09-30 03:15:11 -04:00
|
|
|
<section class="ai-llms-list-editor__templates">
|
|
|
|
<AdminPageSubheader @titleLabel={{this.preconfiguredTitle}} />
|
2024-10-02 01:31:48 -04:00
|
|
|
|
|
|
|
<AdminSectionLandingWrapper
|
|
|
|
class="ai-llms-list-editor__templates-list"
|
|
|
|
>
|
2024-09-30 03:15:11 -04:00
|
|
|
{{#each this.preConfiguredLlms as |llm|}}
|
2024-10-02 01:31:48 -04:00
|
|
|
<AdminSectionLandingItem
|
|
|
|
@titleLabelTranslated={{llm.name}}
|
|
|
|
@descriptionLabelTranslated={{this.modelDescription llm}}
|
|
|
|
@taglineLabel={{concat
|
|
|
|
"discourse_ai.llms.providers."
|
|
|
|
llm.provider
|
|
|
|
}}
|
2024-09-30 03:15:11 -04:00
|
|
|
data-llm-id={{llm.id}}
|
|
|
|
class="ai-llms-list-editor__templates-list-item"
|
|
|
|
>
|
2024-10-02 01:31:48 -04:00
|
|
|
<:buttons as |buttons|>
|
|
|
|
<buttons.Default
|
|
|
|
@action={{fn this.transitionToLlmEditor llm.id}}
|
|
|
|
@icon="gear"
|
|
|
|
@label="discourse_ai.llms.preconfigured.button"
|
|
|
|
/>
|
|
|
|
</:buttons>
|
|
|
|
</AdminSectionLandingItem>
|
2024-09-30 03:15:11 -04:00
|
|
|
{{/each}}
|
2024-10-02 01:31:48 -04:00
|
|
|
</AdminSectionLandingWrapper>
|
2024-09-30 03:15:11 -04:00
|
|
|
</section>
|
2024-05-16 13:28:57 -04:00
|
|
|
{{/if}}
|
2024-05-13 11:46:42 -04:00
|
|
|
</section>
|
|
|
|
</template>
|
|
|
|
}
|