mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-10 21:44:42 +00:00
Introduces custom AI tools functionality. 1. Why it was added: The PR adds the ability to create, manage, and use custom AI tools within the Discourse AI system. This feature allows for more flexibility and extensibility in the AI capabilities of the platform. 2. What it does: - Introduces a new `AiTool` model for storing custom AI tools - Adds CRUD (Create, Read, Update, Delete) operations for AI tools - Implements a tool runner system for executing custom tool scripts - Integrates custom tools with existing AI personas - Provides a user interface for managing custom tools in the admin panel 3. Possible use cases: - Creating custom tools for specific tasks or integrations (stock quotes, currency conversion etc...) - Allowing administrators to add new functionalities to AI assistants without modifying core code - Implementing domain-specific tools for particular communities or industries 4. Code structure: The PR introduces several new files and modifies existing ones: a. Models: - `app/models/ai_tool.rb`: Defines the AiTool model - `app/serializers/ai_custom_tool_serializer.rb`: Serializer for AI tools b. Controllers: - `app/controllers/discourse_ai/admin/ai_tools_controller.rb`: Handles CRUD operations for AI tools c. Views and Components: - New Ember.js components for tool management in the admin interface - Updates to existing AI persona management components to support custom tools d. Core functionality: - `lib/ai_bot/tool_runner.rb`: Implements the custom tool execution system - `lib/ai_bot/tools/custom.rb`: Defines the custom tool class e. Routes and configurations: - Updates to route configurations to include new AI tool management pages f. Migrations: - `db/migrate/20240618080148_create_ai_tools.rb`: Creates the ai_tools table g. Tests: - New test files for AI tool functionality and integration The PR integrates the custom tools system with the existing AI persona framework, allowing personas to use both built-in and custom tools. It also includes safety measures such as timeouts and HTTP request limits to prevent misuse of custom tools. Overall, this PR significantly enhances the flexibility and extensibility of the Discourse AI system by allowing administrators to create and manage custom AI tools tailored to their specific needs. Co-authored-by: Martin Brennan <martin@discourse.org>
224 lines
6.5 KiB
Plaintext
224 lines
6.5 KiB
Plaintext
import Component from "@glimmer/component";
|
|
import { tracked } from "@glimmer/tracking";
|
|
import { Input } from "@ember/component";
|
|
import { action } from "@ember/object";
|
|
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
|
|
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
|
|
import { inject as service } from "@ember/service";
|
|
import BackButton from "discourse/components/back-button";
|
|
import DButton from "discourse/components/d-button";
|
|
import Textarea from "discourse/components/d-textarea";
|
|
import DTooltip from "discourse/components/d-tooltip";
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
import I18n from "discourse-i18n";
|
|
import AceEditor from "admin/components/ace-editor";
|
|
import ComboBox from "select-kit/components/combo-box";
|
|
import AiToolParameterEditor from "./ai-tool-parameter-editor";
|
|
import AiToolTestModal from "./modal/ai-tool-test-modal";
|
|
|
|
export default class AiToolEditor extends Component {
|
|
@service router;
|
|
@service store;
|
|
@service dialog;
|
|
@service modal;
|
|
@service toasts;
|
|
|
|
@tracked isSaving = false;
|
|
@tracked editingModel = null;
|
|
@tracked showDelete = false;
|
|
|
|
@tracked selectedPreset = null;
|
|
|
|
aceEditorMode = "javascript";
|
|
aceEditorTheme = "chrome";
|
|
|
|
@action
|
|
updateModel() {
|
|
this.editingModel = this.args.model.workingCopy();
|
|
this.showDelete = !this.args.model.isNew;
|
|
}
|
|
|
|
get presets() {
|
|
return this.args.presets.map((preset) => {
|
|
return {
|
|
name: preset.preset_name,
|
|
id: preset.preset_id,
|
|
};
|
|
});
|
|
}
|
|
|
|
get showPresets() {
|
|
return !this.selectedPreset && this.args.model.isNew;
|
|
}
|
|
|
|
@action
|
|
configurePreset() {
|
|
this.selectedPreset = this.args.presets.findBy("preset_id", this.presetId);
|
|
this.editingModel = this.args.model.workingCopy();
|
|
this.editingModel.setProperties(this.selectedPreset);
|
|
this.showDelete = false;
|
|
}
|
|
|
|
@action
|
|
async save() {
|
|
this.isSaving = true;
|
|
|
|
try {
|
|
await this.args.model.save(
|
|
this.editingModel.getProperties(
|
|
"name",
|
|
"description",
|
|
"parameters",
|
|
"script",
|
|
"summary"
|
|
)
|
|
);
|
|
|
|
this.toasts.success({
|
|
data: { message: I18n.t("discourse_ai.tools.saved") },
|
|
duration: 2000,
|
|
});
|
|
if (!this.args.tools.any((tool) => tool.id === this.args.model.id)) {
|
|
this.args.tools.pushObject(this.args.model);
|
|
}
|
|
|
|
this.router.transitionTo(
|
|
"adminPlugins.show.discourse-ai-tools.show",
|
|
this.args.model
|
|
);
|
|
} catch (e) {
|
|
popupAjaxError(e);
|
|
} finally {
|
|
this.isSaving = false;
|
|
}
|
|
}
|
|
|
|
@action
|
|
delete() {
|
|
return this.dialog.confirm({
|
|
message: I18n.t("discourse_ai.tools.confirm_delete"),
|
|
didConfirm: () => {
|
|
return this.args.model.destroyRecord().then(() => {
|
|
this.args.tools.removeObject(this.args.model);
|
|
this.router.transitionTo(
|
|
"adminPlugins.show.discourse-ai-tools.index"
|
|
);
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
@action
|
|
updateScript(script) {
|
|
this.editingModel.script = script;
|
|
}
|
|
|
|
@action
|
|
openTestModal() {
|
|
this.modal.show(AiToolTestModal, {
|
|
model: {
|
|
tool: this.editingModel,
|
|
},
|
|
});
|
|
}
|
|
|
|
<template>
|
|
<BackButton
|
|
@route="adminPlugins.show.discourse-ai-tools"
|
|
@label="discourse_ai.ai_tool.back"
|
|
/>
|
|
|
|
<form
|
|
class="form-horizontal ai-tool-editor"
|
|
{{didUpdate this.updateModel @model.id}}
|
|
{{didInsert this.updateModel @model.id}}
|
|
>
|
|
{{#if this.showPresets}}
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.presets"}}</label>
|
|
<ComboBox
|
|
@value={{this.presetId}}
|
|
@content={{this.presets}}
|
|
class="ai-tool-editor__presets"
|
|
/>
|
|
</div>
|
|
|
|
<div class="control-group ai-llm-editor__action_panel">
|
|
<DButton
|
|
class="ai-tool-editor__next"
|
|
@action={{this.configurePreset}}
|
|
>
|
|
{{I18n.t "discourse_ai.tools.next.title"}}
|
|
</DButton>
|
|
</div>
|
|
{{else}}
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.name"}}</label>
|
|
<Input
|
|
@type="text"
|
|
@value={{this.editingModel.name}}
|
|
class="ai-tool-editor__name"
|
|
/>
|
|
<DTooltip
|
|
@icon="question-circle"
|
|
@content={{I18n.t "discourse_ai.tools.name_help"}}
|
|
/>
|
|
</div>
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.description"}}</label>
|
|
<Textarea
|
|
@value={{this.editingModel.description}}
|
|
class="ai-tool-editor__description input-xxlarge"
|
|
placeholder={{I18n.t "discourse_ai.tools.description_help"}}
|
|
/>
|
|
</div>
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.summary"}}</label>
|
|
<Input
|
|
@type="text"
|
|
@value={{this.editingModel.summary}}
|
|
class="ai-tool-editor__summary input-xxlarge"
|
|
/>
|
|
<DTooltip
|
|
@icon="question-circle"
|
|
@content={{I18n.t "discourse_ai.tools.summary_help"}}
|
|
/>
|
|
</div>
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.parameters"}}</label>
|
|
<AiToolParameterEditor @parameters={{this.editingModel.parameters}} />
|
|
</div>
|
|
<div class="control-group">
|
|
<label>{{I18n.t "discourse_ai.tools.script"}}</label>
|
|
<AceEditor
|
|
@content={{this.editingModel.script}}
|
|
@mode={{this.aceEditorMode}}
|
|
@theme={{this.aceEditorTheme}}
|
|
@onChange={{this.updateScript}}
|
|
@editorId="ai-tool-script-editor"
|
|
/>
|
|
</div>
|
|
<div class="control-group ai-tool-editor__action_panel">
|
|
<DButton
|
|
@action={{this.openTestModal}}
|
|
class="btn-default ai-tool-editor__test-button"
|
|
>{{I18n.t "discourse_ai.tools.test"}}</DButton>
|
|
<DButton
|
|
class="btn-primary ai-tool-editor__save"
|
|
@action={{this.save}}
|
|
@disabled={{this.isSaving}}
|
|
>{{I18n.t "discourse_ai.tools.save"}}</DButton>
|
|
{{#if this.showDelete}}
|
|
<DButton
|
|
@action={{this.delete}}
|
|
class="btn-danger ai-tool-editor__delete"
|
|
>
|
|
{{I18n.t "discourse_ai.tools.delete"}}
|
|
</DButton>
|
|
{{/if}}
|
|
</div>
|
|
{{/if}}
|
|
</form>
|
|
</template>
|
|
}
|