Files

152 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

import { tracked } from "@glimmer/tracking";
import { ajax } from "discourse/lib/ajax";
import RestModel from "discourse/models/rest";
const CREATE_ATTRIBUTES = [
"id",
"name",
"description",
2024-06-11 18:14:14 +10:00
"tools",
"system_prompt",
"allowed_group_ids",
"enabled",
"system",
"priority",
"top_p",
"temperature",
"user_id",
"mentionable",
"default_llm",
"user",
"max_context_posts",
"vision_enabled",
"vision_max_pixels",
2024-04-01 13:43:34 -03:00
"rag_uploads",
"rag_chunk_tokens",
"rag_chunk_overlap_tokens",
"rag_conversation_chunks",
"question_consolidator_llm",
"allow_chat",
2024-06-11 18:14:14 +10:00
"tool_details",
];
const SYSTEM_ATTRIBUTES = [
"id",
"allowed_group_ids",
"enabled",
"system",
"priority",
"user_id",
"mentionable",
"default_llm",
"user",
"max_context_posts",
"vision_enabled",
"vision_max_pixels",
2024-04-01 13:43:34 -03:00
"rag_uploads",
"rag_chunk_tokens",
"rag_chunk_overlap_tokens",
"rag_conversation_chunks",
"question_consolidator_llm",
"allow_chat",
2024-06-11 18:14:14 +10:00
"tool_details",
];
2024-06-11 18:14:14 +10:00
class ToolOption {
@tracked value = null;
}
export default class AiPersona extends RestModel {
// this code is here to convert the wire schema to easier to work with object
2024-06-11 18:14:14 +10:00
// on the wire we pass in/out tools as an Array.
// [[ToolName, {option1: value, option2: value}], ToolName2, ToolName3]
// So we rework this into a "tools" property and nested toolOptions
init(properties) {
2024-06-11 18:14:14 +10:00
if (properties.tools) {
properties.tools = properties.tools.map((tool) => {
if (typeof tool === "string") {
return tool;
} else {
2024-06-11 18:14:14 +10:00
let [toolId, options] = tool;
for (let optionId in options) {
if (!options.hasOwnProperty(optionId)) {
continue;
}
2024-06-11 18:14:14 +10:00
this.getToolOption(toolId, optionId).value = options[optionId];
}
2024-06-11 18:14:14 +10:00
return toolId;
}
});
}
super.init(properties);
2024-06-11 18:14:14 +10:00
this.tools = properties.tools;
}
async createUser() {
const result = await ajax(
`/admin/plugins/discourse-ai/ai-personas/${this.id}/create-user.json`,
{
type: "POST",
}
);
this.user = result.user;
this.user_id = this.user.id;
return this.user;
}
2024-06-11 18:14:14 +10:00
getToolOption(toolId, optionId) {
this.toolOptions ||= {};
this.toolOptions[toolId] ||= {};
return (this.toolOptions[toolId][optionId] ||= new ToolOption());
}
2024-06-11 18:14:14 +10:00
populateToolOptions(attrs) {
if (!attrs.tools) {
return;
}
2024-06-11 18:14:14 +10:00
let toolsWithOptions = [];
attrs.tools.forEach((toolId) => {
if (typeof toolId !== "string") {
toolId = toolId[0];
}
2024-06-11 18:14:14 +10:00
if (this.toolOptions && this.toolOptions[toolId]) {
let options = this.toolOptions[toolId];
let optionsWithValues = {};
for (let optionId in options) {
if (!options.hasOwnProperty(optionId)) {
continue;
}
let option = options[optionId];
optionsWithValues[optionId] = option.value;
}
2024-06-11 18:14:14 +10:00
toolsWithOptions.push([toolId, optionsWithValues]);
} else {
2024-06-11 18:14:14 +10:00
toolsWithOptions.push(toolId);
}
});
2024-06-11 18:14:14 +10:00
attrs.tools = toolsWithOptions;
}
updateProperties() {
let attrs = this.system
? this.getProperties(SYSTEM_ATTRIBUTES)
: this.getProperties(CREATE_ATTRIBUTES);
attrs.id = this.id;
2024-06-11 18:14:14 +10:00
this.populateToolOptions(attrs);
return attrs;
}
createProperties() {
let attrs = this.getProperties(CREATE_ATTRIBUTES);
2024-06-11 18:14:14 +10:00
this.populateToolOptions(attrs);
return attrs;
}
workingCopy() {
let attrs = this.getProperties(CREATE_ATTRIBUTES);
2024-06-11 18:14:14 +10:00
this.populateToolOptions(attrs);
return AiPersona.create(attrs);
}
}