+
+
+ {'Visibility expression'}
+
+
+
+ changeUtilityVisibility(e, uid)}
+ />
+
+
+
+
+
{#each utility.functions as fn, fid (fid)}
From df5eaeb6b1a57d8ab7b1eba3acbd39de5bf411e6 Mon Sep 17 00:00:00 2001
From: Jicheng Lu <103353@smsassist.com>
Date: Wed, 28 May 2025 16:17:06 -0500
Subject: [PATCH 2/7] refine utility structure
---
src/lib/helpers/types/agentTypes.js | 13 +-
src/lib/helpers/utils/common.js | 4 +-
.../agent-components/agent-utility.svelte | 359 +++++++-----------
.../page/agent/[agentId]/agent-tabs.svelte | 2 +-
4 files changed, 151 insertions(+), 227 deletions(-)
diff --git a/src/lib/helpers/types/agentTypes.js b/src/lib/helpers/types/agentTypes.js
index 11dfb291..20bfde76 100644
--- a/src/lib/helpers/types/agentTypes.js
+++ b/src/lib/helpers/types/agentTypes.js
@@ -131,11 +131,20 @@
/**
* @typedef {Object} AgentUtility
+ * @property {string} category
* @property {string} name
* @property {boolean} disabled
* @property {string?} [visibility_expression]
- * @property {import('$commonTypes').NameBase[]} functions
- * @property {import('$commonTypes').NameBase[]} templates
+ * @property {UtilityItem[]} items
+ */
+
+/**
+ * @typedef {Object} UtilityItem
+ * @property {string} function_name
+ * @property {string?} [function_display_name]
+ * @property {string?} [template_name]
+ * @property {string?} [template_display_name]
+ * @property {string?} [visibility_expression]
*/
/**
diff --git a/src/lib/helpers/utils/common.js b/src/lib/helpers/utils/common.js
index 823e8821..daa9d96d 100644
--- a/src/lib/helpers/utils/common.js
+++ b/src/lib/helpers/utils/common.js
@@ -54,8 +54,8 @@ export function formatObject(object) {
/**
- * @param {string?} str
- * @param {string?} prefix
+ * @param {string | null | undefined} str
+ * @param {string | null | undefined} prefix
*/
export function truncateByPrefix(str, prefix) {
if (!str || !prefix) {
diff --git a/src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte b/src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte
index 5d264e72..820d3351 100644
--- a/src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte
+++ b/src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte
@@ -14,39 +14,18 @@
export let handleAgentChange = () => {};
export const fetchUtilities = () => {
- const candidates = innerUtilities?.filter(x => !!x.name)?.map(x => {
- /** @type {import('$commonTypes').NameBase[]} */
- const functions = [];
- /** @type {import('$commonTypes').NameBase[]} */
- const templates = [];
-
- const uniqueFns = new Set();
- const uniqueTps = new Set();
- const fns = x.functions.filter(f => !!f.name);
- const tps = x.templates.filter(t => !!t.name);
-
- fns.forEach(f => {
- if (!uniqueFns.has(f.name)) {
- functions.push({ ...f });
- uniqueFns.add(f.name);
- }
- });
-
- tps.forEach(t => {
- if (!uniqueTps.has(t.name)) {
- templates.push({ ...t });
- uniqueTps.add(t.name);
- }
- });
-
- return {
- name: x.name,
- disabled: x.disabled,
- visibility_expression: x.visibility_expression,
- functions: functions,
- templates: templates
- };
- });
+ const list = innerUtilities?.filter(x => !!x.category && !!x.name && x.items?.length > 0) || [];
+
+ /** @type {import('$agentTypes').AgentUtility[]} */
+ const candidates = list.reduce((acc, x) => {
+ const tag = `${x.category}##${x.name}`;
+ if (!acc.visited.has(tag)) {
+ // @ts-ignore
+ acc.result.push(x);
+ acc.visited.add(tag);
+ }
+ return acc;
+ }, { result: [], visited: new Set() }).result;
innerRefresh(candidates);
return candidates;
@@ -56,64 +35,43 @@
let utilityMapper = {};
/** @type {string[]} */
- let utilityOptions = [];
+ let utilityCategoryOptions = [];
/** @type {import('$agentTypes').AgentUtility[]} */
let innerUtilities = [];
onMount(async () =>{
+ init();
getAgentUtilityOptions().then(data => {
const list = data || [];
- list.forEach(item => {
- const fns = item.functions.map(f => {
- return {
- name: f.name,
- displayName: truncateByPrefix(f.name, prefix)
- };
- });
- const tps = item.templates.map(t => {
- return {
- name: t.name,
- displayName: truncateByPrefix(t.name, prefix)
- };
- });
-
- if (!utilityMapper[item.name]) {
- utilityMapper[item.name] = {
- functions: [{
- name: "",
- displayName: ""
- }, ...fns],
- templates: [{
- name: "",
- displayName: ""
- }, ...tps]
- };
- } else {
- const prevFns = utilityMapper[item.name].functions;
- const prevTps = utilityMapper[item.name].templates;
- utilityMapper[item.name].functions = [...prevFns, fns];
- utilityMapper[item.name].templates = [...prevTps, tps];
- }
+ list.forEach(utility => {
+ const content = {
+ name: utility.name,
+ items: utility.items.map(it => ({
+ ...it,
+ function_display_name: truncateByPrefix(it.function_name, prefix),
+ template_display_name: truncateByPrefix(it.template_name, prefix)
+ }))
+ };
+
+ const contents = utilityMapper[utility.category] || [];
+ contents.push(content);
+ utilityMapper[utility.category] = contents;
});
- const names = list.map(x => x.name) || [];
- utilityOptions = ["", ...names];
+ const keys = Object.keys(utilityMapper).sort((/** @type {string} */ a, /** @type {string} */ b) => a.localeCompare(b));
+ utilityCategoryOptions = ["", ...keys];
});
- init();
});
function init() {
const list = agent.utilities?.map(x => {
return {
...x,
- functions: x.functions?.map(f => ({
- ...f,
- displayName: truncateByPrefix(f.name, prefix)
- })) || [],
- templates: x.templates?.map(t => ({
- ...t,
- displayName: truncateByPrefix(t.name, prefix)
- })) || []
+ items: x.items.map(it => ({
+ ...it,
+ function_display_name: truncateByPrefix(it.function_name, prefix),
+ template_display_name: truncateByPrefix(it.template_name, prefix)
+ }))
};
}) || [];
innerRefresh(list);
@@ -123,20 +81,32 @@
* @param {any} e
* @param {number} idx
*/
- function changeUtility(e, idx) {
+ function changeUtilityCategory(e, idx) {
+ const found = innerUtilities.find((_, index) => index === idx);
+ if (!found) return;
+
+ const category = e.target.value;
+ found.category = category;
+ found.name = '';
+ found.items = [];
+ innerRefresh(innerUtilities);
+ handleAgentChange();
+ }
+
+ /**
+ * @param {any} e
+ * @param {number} idx
+ */
+ function changeUtilityName(e, idx) {
const found = innerUtilities.find((_, index) => index === idx);
if (!found) return;
const name = e.target.value;
found.name = name;
- found.functions = [
- // @ts-ignore
- ...utilityMapper[name].functions?.filter(x => !!x.name) || []
- ];
- found.templates = [
- // @ts-ignore
- ...utilityMapper[name].templates?.filter(x => !!x.name) || []
- ];
+
+ const foundUtility = utilityMapper[found.category]?.find((/** @type {any} */ x) => x.name == name);
+ found.items = foundUtility?.items?.map((/** @type {any} */ x) => ({...x})) || [];
+
innerRefresh(innerUtilities);
handleAgentChange();
}
@@ -145,10 +115,10 @@
innerUtilities = [
...innerUtilities,
{
+ category: '',
name: '',
disabled: false,
- functions: [],
- templates: []
+ items: []
}
];
}
@@ -159,24 +129,6 @@
handleAgentChange();
}
- /**
- * @param {number} uid
- * @param {string} type
- */
- function addUtilityContent(uid, type) {
- const found = innerUtilities.find((_, index) => index === uid);
- if (!found || found.disabled) return;
-
- if (type === 'function') {
- found.functions.push({ name: '', displayName: '' });
- } else if (type === 'template') {
- found.templates.push({ name: '', displayName: '' });
- }
-
- innerRefresh(innerUtilities);
- handleAgentChange();
- }
-
/**
* @param {any} e
* @param {number} uid
@@ -191,51 +143,28 @@
}
/**
+ * @param {any} e
* @param {number} uid
- * @param {number} id
- * @param {string} type
+ * @param {number} fid
*/
- function deleteUtilityContent(uid, id, type) {
- const found = innerUtilities.find((_, index) => index === uid);
- if (!found || found.disabled) return;
+ function changeUtilityItemVisibility(e, uid, fid) {
+ const found = innerUtilities.find((_, index) => index === uid)?.items?.find((_, index) => index === fid);
+ if (!found) return;
- if (type === 'function') {
- const fns = found.functions?.filter((_, index) => index !== id) || [];
- found.functions = fns;
- } else if (type === 'template') {
- const tps = found.templates?.filter((_, index) => index !== id) || [];
- found.templates = tps;
- }
-
+ found.visibility_expression = e.target.value || null;
innerRefresh(innerUtilities);
handleAgentChange();
}
-
/**
- * @param {any} e
* @param {number} uid
- * @param {number} idx
- * @param {string} type
+ * @param {number} fid
*/
- function selectContent(e, uid, idx, type) {
+ function deleteUtilityItem(uid, fid) {
const found = innerUtilities.find((_, index) => index === uid);
if (!found) return;
- const vals = e.target.value.split("#");
- if (type === 'function') {
- const fn = found.functions?.find((_, index) => index === idx);
- if (fn) {
- fn.name = vals[0];
- fn.displayName = vals[1];
- }
- } else if (type === 'template') {
- const tp = found.templates?.find((_, index) => index === idx);
- if (tp) {
- tp.name = vals[0];
- tp.displayName = vals[1];
- }
- }
+ found.items = found.items?.filter((_, index) => index !== fid) || [];
innerRefresh(innerUtilities);
handleAgentChange();
}
@@ -258,11 +187,11 @@
function innerRefresh(list) {
innerUtilities = list?.map(x => {
return {
+ category: x.category,
name: x.name,
disabled: x.disabled,
visibility_expression: x.visibility_expression,
- functions: x.functions.map(f => ({ ...f })),
- templates: x.templates.map(t => ({ ...t }))
+ items: x.items.map(it => ({ ...it }))
}
}) || [];
}
@@ -317,7 +246,7 @@
{ toggleUtility(e, uid); }}
+ on:change={e => toggleUtility(e, uid)}
/>
{ changeUtility(e, uid); }}
+ placeholder={'Select a category'}
+ on:change={e => changeUtilityCategory(e, uid)}
>
- {#each utilityOptions as option}
-
+ {#each utilityCategoryOptions as option}
+
{/each}
@@ -349,100 +279,68 @@
role="link"
tabindex="0"
on:keydown={() => {}}
- on:click={() => { deleteUtility(uid); }}
+ on:click={() => deleteUtility(uid)}
/>