|
2 | 2 | import { onMount } from 'svelte'; |
3 | 3 | import Swal from 'sweetalert2'; |
4 | 4 | import { Card, CardBody, Input, Button } from '@sveltestrap/sveltestrap'; |
5 | | - import { getAgentRuleOptions, generateAgentCodeScript } from '$lib/services/agent-service'; |
| 5 | + import { getAgentRuleOptions, generateAgentCodeScript, getAgentRuleActions } from '$lib/services/agent-service'; |
6 | 6 | import Markdown from '$lib/common/markdown/Markdown.svelte'; |
7 | 7 | import BotsharpTooltip from '$lib/common/tooltip/BotsharpTooltip.svelte'; |
8 | 8 | import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte'; |
| 9 | + import CodeScript from '$lib/common/shared/CodeScript.svelte'; |
9 | 10 | import { ADMIN_ROLES, AI_PROGRAMMER_AGENT_ID, RULE_TRIGGER_CODE_GENERATE_TEMPLATE } from '$lib/helpers/constants'; |
10 | 11 | import { AgentCodeScriptType } from '$lib/helpers/enums'; |
11 | 12 | import { scrollToBottom } from '$lib/helpers/utils/common'; |
|
42 | 43 | return { |
43 | 44 | trigger_name: x.trigger_name, |
44 | 45 | disabled: x.disabled, |
45 | | - criteria: x.criteria?.trim() |
| 46 | + criteria: x.criteria?.trim(), |
| 47 | + action: !!x.action?.name ? x.action : null |
46 | 48 | }; |
47 | 49 | }); |
48 | 50 |
|
|
63 | 65 | /** @type {any[]} */ |
64 | 66 | let ruleOptions = []; |
65 | 67 |
|
| 68 | + /** @type {any[]} */ |
| 69 | + let actionOptions = []; |
| 70 | +
|
66 | 71 | /** @type {import('$agentTypes').AgentRule[]} */ |
67 | 72 | let innerRules = []; |
68 | 73 |
|
|
71 | 76 |
|
72 | 77 | onMount(async () =>{ |
73 | 78 | resizeWindow(); |
74 | | - getAgentRuleOptions().then(data => { |
75 | | - const list = data?.map(x => { |
76 | | - return { |
77 | | - name: x.trigger_name, |
78 | | - displayName: "", |
79 | | - output_args: x.output_args, |
80 | | - json_args: x.json_args, |
81 | | - statement: x.statement |
82 | | - }; |
83 | | - }) || []; |
84 | | - ruleOptions = [{ |
85 | | - name: "", |
86 | | - displayName: "" |
87 | | - }, ...list]; |
88 | | - init(); |
89 | | - }); |
| 79 | + Promise.all([ |
| 80 | + loadAgentRuleOptions(), |
| 81 | + loadAgentRuleActions() |
| 82 | + ]); |
90 | 83 | }); |
91 | 84 |
|
| 85 | + function loadAgentRuleOptions() { |
| 86 | + return new Promise((resolve, reject) => { |
| 87 | + getAgentRuleOptions().then(data => { |
| 88 | + const list = data?.map(x => { |
| 89 | + return { |
| 90 | + name: x.trigger_name, |
| 91 | + displayName: "", |
| 92 | + output_args: x.output_args, |
| 93 | + json_args: x.json_args, |
| 94 | + statement: x.statement |
| 95 | + }; |
| 96 | + }) || []; |
| 97 | + ruleOptions = [{ |
| 98 | + name: "", |
| 99 | + displayName: "" |
| 100 | + }, ...list]; |
| 101 | + init(); |
| 102 | + resolve('done'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + } |
| 106 | +
|
92 | 107 | function init() { |
93 | 108 | const list = agent.rules?.map(x => { |
94 | 109 | return { |
|
98 | 113 | }) || []; |
99 | 114 | innerRefresh(list); |
100 | 115 | } |
| 116 | +
|
| 117 | + function loadAgentRuleActions() { |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + getAgentRuleActions().then(data => { |
| 120 | + const list = data?.map(x => ({ name: x })) || []; |
| 121 | + actionOptions = [{ |
| 122 | + name: "" |
| 123 | + }, ...list]; |
| 124 | + resolve('done'); |
| 125 | + }); |
| 126 | + }); |
| 127 | + } |
101 | 128 | |
102 | 129 | /** |
103 | 130 | * @param {any} e |
|
155 | 182 | const found = innerRules.find((_, index) => index === uid); |
156 | 183 | if (!found) return; |
157 | 184 |
|
158 | | - const val = e.target.value; |
159 | 185 | if (field === 'criteria') { |
160 | | - found.criteria = val; |
| 186 | + found.criteria = e.target.value; |
| 187 | + innerRefresh(innerRules); |
| 188 | + handleAgentChange(); |
| 189 | + } else if (field === 'action-config') { |
| 190 | + if (found.action == null) { |
| 191 | + found.action = { |
| 192 | + name: '', |
| 193 | + disabled: false, |
| 194 | + config: {} |
| 195 | + }; |
| 196 | + } |
| 197 | + try { |
| 198 | + found.action.config = JSON.parse(e.detail?.text || '{}'); |
| 199 | + handleAgentChange(); |
| 200 | + } catch { |
| 201 | + // ignore invalid JSON while typing |
| 202 | + } |
161 | 203 | } |
162 | | - innerRefresh(innerRules); |
163 | | - handleAgentChange(); |
164 | 204 | } |
165 | 205 |
|
166 | 206 | /** |
|
258 | 298 | return scriptName; |
259 | 299 | } |
260 | 300 |
|
| 301 | + /** |
| 302 | + * @param {any} e |
| 303 | + * @param {number} idx |
| 304 | + */ |
| 305 | + function changeAction(e, idx) { |
| 306 | + const found = innerRules.find((_, index) => index === idx); |
| 307 | + if (!found) return; |
| 308 | + |
| 309 | + const val = e.target.value; |
| 310 | + found.action = { |
| 311 | + ...found.action || {}, |
| 312 | + name: val, |
| 313 | + disabled: found.action?.disabled || false |
| 314 | + }; |
| 315 | + innerRefresh(innerRules); |
| 316 | + handleAgentChange(); |
| 317 | + } |
| 318 | +
|
| 319 | + /** |
| 320 | + * @param {any} e |
| 321 | + * @param {number} uid |
| 322 | + */ |
| 323 | + function toggleAction(e, uid) { |
| 324 | + const found = innerRules.find((_, index) => index === uid); |
| 325 | + if (!found) return; |
| 326 | +
|
| 327 | + if (!found.action) { |
| 328 | + found.action = { |
| 329 | + name: '', |
| 330 | + disabled: false |
| 331 | + }; |
| 332 | + } |
| 333 | +
|
| 334 | + found.action.disabled = !e.target.checked; |
| 335 | + innerRefresh(innerRules); |
| 336 | + handleAgentChange(); |
| 337 | + } |
| 338 | +
|
261 | 339 | function resizeWindow() { |
262 | 340 | windowWidth = window.innerWidth; |
263 | 341 | windowHeight = window.innerHeight; |
|
421 | 499 | </div> |
422 | 500 | </div> |
423 | 501 | </div> |
| 502 | +
|
| 503 | + <div class="utility-row utility-row-secondary"> |
| 504 | + <div class="utility-content"> |
| 505 | + <div class="utility-list-item"> |
| 506 | + <div class="utility-label line-align-center"> |
| 507 | + <div class="d-flex gap-1"> |
| 508 | + <div class="line-align-center"> |
| 509 | + {'Action'} |
| 510 | + </div> |
| 511 | + <div class="line-align-center"> |
| 512 | + <Input |
| 513 | + type="checkbox" |
| 514 | + checked={!rule.action?.disabled} |
| 515 | + on:change={e => toggleAction(e, uid)} |
| 516 | + /> |
| 517 | + </div> |
| 518 | + </div> |
| 519 | + </div> |
| 520 | + <div class="utility-value"> |
| 521 | + <div class="utility-input line-align-center"> |
| 522 | + <Input |
| 523 | + type="select" |
| 524 | + disabled={rule.disabled} |
| 525 | + on:change={e => changeAction(e, uid)} |
| 526 | + > |
| 527 | + {#each [...actionOptions] as option} |
| 528 | + <option value={option.name} selected={option.name == rule.action?.name}> |
| 529 | + {option.name} |
| 530 | + </option> |
| 531 | + {/each} |
| 532 | + </Input> |
| 533 | + </div> |
| 534 | + <div class="utility-delete line-align-center"></div> |
| 535 | + </div> |
| 536 | + </div> |
| 537 | + {#if rule.action?.name} |
| 538 | + <div class="utility-list-item"> |
| 539 | + <div class="utility-label line-align-center"> |
| 540 | + <div class="d-flex gap-1"> |
| 541 | + <div class="line-align-center"> |
| 542 | + {'Config'} |
| 543 | + </div> |
| 544 | + </div> |
| 545 | + </div> |
| 546 | + <div class="utility-value"> |
| 547 | + <div class="utility-input line-align-center"> |
| 548 | + <CodeScript |
| 549 | + language="json" |
| 550 | + containerClasses="agent-action-config" |
| 551 | + scriptText={JSON.stringify(rule.action?.config || {}, null, 2)} |
| 552 | + on:change={(e) => changeContent(e, uid, 'action-config')} |
| 553 | + /> |
| 554 | + </div> |
| 555 | + <div class="utility-delete line-align-center"></div> |
| 556 | + </div> |
| 557 | + </div> |
| 558 | + {/if} |
| 559 | + </div> |
| 560 | + </div> |
424 | 561 | </div> |
425 | 562 | {/each} |
426 | 563 |
|
|
0 commit comments