From 1dfae15806ab11226c0b5140b49a730074932a65 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 26 Jan 2026 16:33:16 -0600 Subject: [PATCH 1/6] add agent rule action --- package-lock.json | 7 +- package.json | 1 + src/lib/helpers/types/agentTypes.js | 8 + src/lib/scss/custom/pages/_agent.scss | 5 + src/lib/services/agent-service.js | 10 + src/lib/services/api-endpoints.js | 1 + .../agent-components/agent-rule.svelte | 181 +++++++++++++++--- 7 files changed, 188 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index aab7e87d..3f242cdc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@codemirror/commands": "^6.10.0", "@codemirror/lang-javascript": "^6.2.4", + "@codemirror/lang-json": "^6.0.2", "@codemirror/lang-python": "^6.2.1", "@codemirror/language": "^6.11.3", "@codemirror/state": "^6.5.2", @@ -133,9 +134,9 @@ } }, "node_modules/@codemirror/lang-json": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/@codemirror/lang-json/-/lang-json-6.0.1.tgz", - "integrity": "sha512-+T1flHdgpqDDlJZ2Lkil/rLiRy684WMLc74xUnjJH48GQdfJo/pudlTRreZmKwzP8/tGdKf83wlbAdOCzlJOGQ==", + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/@codemirror/lang-json/-/lang-json-6.0.2.tgz", + "integrity": "sha512-x2OtO+AvwEHrEwR0FyyPtfDUiloG3rnVTSZV1W8UteaLL8/MajQd8DpvUb2YVzC+/T18aSDv0H9mu+xw0EStoQ==", "dependencies": { "@codemirror/language": "^6.0.0", "@lezer/json": "^1.0.0" diff --git a/package.json b/package.json index 222400ea..ecd73242 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "dependencies": { "@codemirror/commands": "^6.10.0", "@codemirror/lang-javascript": "^6.2.4", + "@codemirror/lang-json": "^6.0.2", "@codemirror/lang-python": "^6.2.1", "@codemirror/language": "^6.11.3", "@codemirror/state": "^6.5.2", diff --git a/src/lib/helpers/types/agentTypes.js b/src/lib/helpers/types/agentTypes.js index be941c2a..fb100d57 100644 --- a/src/lib/helpers/types/agentTypes.js +++ b/src/lib/helpers/types/agentTypes.js @@ -229,11 +229,19 @@ * @property {string} criteria * @property {string?} [displayName] * @property {boolean} disabled + * @property {AgentAction?} [action] * @property {any?} [output_args] * @property {string?} [json_args] * @property {string?} [statement] */ +/** + * @typedef {Object} AgentAction + * @property {string?} [name] + * @property {boolean} disabled + * @property {any} [config] + */ + /** * @typedef {Object} AgentTaskSearchOption diff --git a/src/lib/scss/custom/pages/_agent.scss b/src/lib/scss/custom/pages/_agent.scss index 1117ffab..97e5dd9a 100644 --- a/src/lib/scss/custom/pages/_agent.scss +++ b/src/lib/scss/custom/pages/_agent.scss @@ -323,6 +323,11 @@ } } +.agent-action-config { + min-height: 0px; + max-height: 200px; +} + .code-editor { max-height: 500px; overflow-y: auto; diff --git a/src/lib/services/agent-service.js b/src/lib/services/agent-service.js index caa9e172..a8018f37 100644 --- a/src/lib/services/agent-service.js +++ b/src/lib/services/agent-service.js @@ -115,6 +115,16 @@ export async function getAgentRuleOptions() { return response.data; } +/** + * Get agent rule actions + * @returns {Promise} + */ +export async function getAgentRuleActions() { + const url = endpoints.agentRuleActionsUrl; + const response = await axios.get(url); + return response.data; +} + /** * Get agent labels * @param {number?} [size] diff --git a/src/lib/services/api-endpoints.js b/src/lib/services/api-endpoints.js index 38d18ac1..bd275f08 100644 --- a/src/lib/services/api-endpoints.js +++ b/src/lib/services/api-endpoints.js @@ -40,6 +40,7 @@ export const endpoints = { agentCreateUrl: `${host}/agent`, agentUtilityOptionsUrl: `${host}/agent/utility/options`, agentRuleOptionsUrl: `${host}/rule/triggers`, + agentRuleActionsUrl: `${host}/rule/actions`, agentLabelsUrl: `${host}/agent/labels`, // agent code script: diff --git a/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte b/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte index a93d2962..03220cce 100644 --- a/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte +++ b/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte @@ -2,10 +2,11 @@ import { onMount } from 'svelte'; import Swal from 'sweetalert2'; import { Card, CardBody, Input, Button } from '@sveltestrap/sveltestrap'; - import { getAgentRuleOptions, generateAgentCodeScript } from '$lib/services/agent-service'; + import { getAgentRuleOptions, generateAgentCodeScript, getAgentRuleActions } from '$lib/services/agent-service'; import Markdown from '$lib/common/markdown/Markdown.svelte'; import BotsharpTooltip from '$lib/common/tooltip/BotsharpTooltip.svelte'; import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte'; + import CodeScript from '$lib/common/shared/CodeScript.svelte'; import { ADMIN_ROLES, AI_PROGRAMMER_AGENT_ID, RULE_TRIGGER_CODE_GENERATE_TEMPLATE } from '$lib/helpers/constants'; import { AgentCodeScriptType } from '$lib/helpers/enums'; import { scrollToBottom } from '$lib/helpers/utils/common'; @@ -42,7 +43,8 @@ return { trigger_name: x.trigger_name, disabled: x.disabled, - criteria: x.criteria?.trim() + criteria: x.criteria?.trim(), + action: !!x.action?.name ? x.action : null }; }); @@ -63,6 +65,9 @@ /** @type {any[]} */ let ruleOptions = []; + /** @type {any[]} */ + let actionOptions = []; + /** @type {import('$agentTypes').AgentRule[]} */ let innerRules = []; @@ -71,24 +76,34 @@ onMount(async () =>{ resizeWindow(); - getAgentRuleOptions().then(data => { - const list = data?.map(x => { - return { - name: x.trigger_name, - displayName: "", - output_args: x.output_args, - json_args: x.json_args, - statement: x.statement - }; - }) || []; - ruleOptions = [{ - name: "", - displayName: "" - }, ...list]; - init(); - }); + Promise.all([ + loadAgentRuleOptions(), + loadAgentRuleActions() + ]); }); + function loadAgentRuleOptions() { + return new Promise((resolve, reject) => { + getAgentRuleOptions().then(data => { + const list = data?.map(x => { + return { + name: x.trigger_name, + displayName: "", + output_args: x.output_args, + json_args: x.json_args, + statement: x.statement + }; + }) || []; + ruleOptions = [{ + name: "", + displayName: "" + }, ...list]; + init(); + resolve('done'); + }); + }); + } + function init() { const list = agent.rules?.map(x => { return { @@ -98,6 +113,18 @@ }) || []; innerRefresh(list); } + + function loadAgentRuleActions() { + return new Promise((resolve, reject) => { + getAgentRuleActions().then(data => { + const list = data?.map(x => ({ name: x })) || []; + actionOptions = [{ + name: "" + }, ...list]; + resolve('done'); + }); + }); + } /** * @param {any} e @@ -155,12 +182,25 @@ const found = innerRules.find((_, index) => index === uid); if (!found) return; - const val = e.target.value; if (field === 'criteria') { - found.criteria = val; + found.criteria = e.target.value; + innerRefresh(innerRules); + handleAgentChange(); + } else if (field === 'action-config') { + if (found.action == null) { + found.action = { + name: '', + disabled: false, + config: {} + }; + } + try { + found.action.config = JSON.parse(e.detail?.text || '{}'); + handleAgentChange(); + } catch { + // ignore invalid JSON while typing + } } - innerRefresh(innerRules); - handleAgentChange(); } /** @@ -258,6 +298,44 @@ return scriptName; } + /** + * @param {any} e + * @param {number} idx + */ + function changeAction(e, idx) { + const found = innerRules.find((_, index) => index === idx); + if (!found) return; + + const val = e.target.value; + found.action = { + ...found.action || {}, + name: val, + disabled: found.action?.disabled || false + }; + innerRefresh(innerRules); + handleAgentChange(); + } + + /** + * @param {any} e + * @param {number} uid + */ + function toggleAction(e, uid) { + const found = innerRules.find((_, index) => index === uid); + if (!found) return; + + if (!found.action) { + found.action = { + name: '', + disabled: false + }; + } + + found.action.disabled = !e.target.checked; + innerRefresh(innerRules); + handleAgentChange(); + } + function resizeWindow() { windowWidth = window.innerWidth; windowHeight = window.innerHeight; @@ -421,6 +499,65 @@ + +
+
+
+
+
+
+ {'Action'} +
+
+ toggleAction(e, uid)} + /> +
+
+
+
+
+ changeAction(e, uid)} + > + {#each [...actionOptions] as option} + + {/each} + +
+
+
+
+ {#if rule.action?.name} +
+
+
+
+ {'Config'} +
+
+
+
+
+ changeContent(e, uid, 'action-config')} + /> +
+
+
+
+ {/if} +
+
{/each} From 238e0e09132db62ec9df24fb0cda1c8145d853f5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 27 Jan 2026 13:38:59 -0600 Subject: [PATCH 2/6] refine code script --- src/lib/common/shared/CodeScript.svelte | 9 ++++++++- .../agent/[agentId]/agent-components/agent-rule.svelte | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/common/shared/CodeScript.svelte b/src/lib/common/shared/CodeScript.svelte index 4a1c42fe..0414fbd0 100644 --- a/src/lib/common/shared/CodeScript.svelte +++ b/src/lib/common/shared/CodeScript.svelte @@ -20,6 +20,12 @@ /** @type {string} */ export let containerClasses = ''; + /** @type {boolean} */ + export let darkTheme = true; + + /** @type {boolean} */ + export let useBasicSetup = true; + /** @type {import("@codemirror/state").Extension[]} */ const baseExtensions = [ @@ -73,8 +79,9 @@ handleChange(e)} diff --git a/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte b/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte index 03220cce..f587bac2 100644 --- a/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte +++ b/src/routes/page/agent/[agentId]/agent-components/agent-rule.svelte @@ -548,6 +548,7 @@ changeContent(e, uid, 'action-config')} /> From 54080f9c28b91489bbadfa56f23587d6d53f8390 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 28 Jan 2026 14:37:08 -0600 Subject: [PATCH 3/6] refine agent rule structure --- src/lib/common/shared/CodeScript.svelte | 26 +- src/lib/helpers/types/agentTypes.js | 14 +- src/lib/scss/custom/pages/_agent.scss | 2 +- src/lib/services/agent-service.js | 10 + src/lib/services/api-endpoints.js | 1 + .../[conversationId]/chat-box.svelte | 1 + .../agent-components/agent-rule.svelte | 232 ++++++++++++------ 7 files changed, 207 insertions(+), 79 deletions(-) diff --git a/src/lib/common/shared/CodeScript.svelte b/src/lib/common/shared/CodeScript.svelte index 0414fbd0..a95851fc 100644 --- a/src/lib/common/shared/CodeScript.svelte +++ b/src/lib/common/shared/CodeScript.svelte @@ -1,7 +1,7 @@