Skip to content

Commit c8b20cd

Browse files
authored
Merge pull request #451 from visagang/main
Improvement: Update rule trigger api routing by agentId
2 parents 7a969eb + af0d818 commit c8b20cd

3 files changed

Lines changed: 82 additions & 12 deletions

File tree

src/lib/services/agent-service.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ export async function getAgentRuleOptions() {
115115
return response.data;
116116
}
117117

118+
/**
119+
* Get agent rule options by agent id
120+
* @param {string} agentId
121+
* @returns {Promise<import('$agentTypes').AgentRule[]>}
122+
*/
123+
export async function getAgentRuleOptionsById(agentId) {
124+
const url = endpoints.agentRuleOptionsByIdUrl.replace("{agentId}", agentId);
125+
const response = await axios.get(url);
126+
return response.data;
127+
}
128+
118129
/**
119130
* Get agent rule config options
120131
* @returns {Promise<any>}

src/lib/services/api-endpoints.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const endpoints = {
4040
agentCreateUrl: `${host}/agent`,
4141
agentUtilityOptionsUrl: `${host}/agent/utility/options`,
4242
agentRuleOptionsUrl: `${host}/rule/triggers`,
43+
agentRuleOptionsByIdUrl: `${host}/rule/triggers/{agentId}`,
4344
agentRuleConfigOptionsUrl: `${host}/rule/config/options`,
4445
agentLabelsUrl: `${host}/agent/labels`,
4546

src/routes/page/agent/[agentId]/agent-components/rules/agent-rule.svelte

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script>
2-
import { onMount } from 'svelte';
3-
import { getAgentRuleOptions, getAgentRuleConfigOptions } from '$lib/services/agent-service';
2+
import { getAgentRuleOptionsById, getAgentRuleConfigOptions } from '$lib/services/agent-service';
43
import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte';
54
import { scrollToBottom } from '$lib/helpers/utils/common';
65
import AgentRuleItem from './agent-rule-item.svelte';
@@ -79,17 +78,52 @@
7978
/** @type {HTMLElement} */
8079
let scrollContainer;
8180
82-
onMount(async () =>{
81+
// React to agent.id changes to reload agent-specific trigger options
82+
$effect(() => {
83+
if (agent?.id) {
84+
// Capture current agent.id to prevent race conditions
85+
const requestedAgentId = agent.id;
86+
87+
// Reset state when agent changes
88+
ruleOptions = [];
89+
innerRules = [];
90+
91+
// Load agent-specific data with error handling
92+
void (async () => {
93+
try {
94+
await Promise.all([
95+
loadAgentRuleOptions(requestedAgentId),
96+
loadAgentRuleConfigOptions(requestedAgentId)
97+
]);
98+
} catch (error) {
99+
// Error already logged in individual loaders
100+
// Ensure init() is called even if options fail to load
101+
// so existing agent rules still render
102+
if (agent.id === requestedAgentId && innerRules.length === 0) {
103+
init();
104+
}
105+
}
106+
})();
107+
}
108+
});
109+
110+
// Initialize window size on mount
111+
$effect(() => {
83112
resizeWindow();
84-
Promise.all([
85-
loadAgentRuleOptions(),
86-
loadAgentRuleConfigOptions()
87-
]);
88113
});
89114
90-
function loadAgentRuleOptions() {
91-
return new Promise((resolve) => {
92-
getAgentRuleOptions().then(data => {
115+
/**
116+
* @param {string} requestedAgentId - The agent ID for which we're loading options
117+
*/
118+
function loadAgentRuleOptions(requestedAgentId) {
119+
return new Promise((resolve, reject) => {
120+
getAgentRuleOptionsById(requestedAgentId).then(data => {
121+
// Guard: only apply results if agent hasn't changed
122+
if (agent.id !== requestedAgentId) {
123+
resolve('stale');
124+
return;
125+
}
126+
93127
const list = data?.map(x => {
94128
return {
95129
name: x.trigger_name,
@@ -105,6 +139,13 @@
105139
}, ...list];
106140
init();
107141
resolve('done');
142+
}).catch(error => {
143+
console.error('Failed to load agent rule options:', error);
144+
// Guard: only apply error state if agent hasn't changed
145+
if (agent.id === requestedAgentId) {
146+
ruleOptions = [{ name: "", displayName: "" }];
147+
}
148+
reject(error);
108149
});
109150
});
110151
}
@@ -119,9 +160,18 @@
119160
innerRefresh(list);
120161
}
121162
122-
function loadAgentRuleConfigOptions() {
123-
return new Promise((resolve) => {
163+
/**
164+
* @param {string} requestedAgentId - The agent ID for which we're loading config options
165+
*/
166+
function loadAgentRuleConfigOptions(requestedAgentId) {
167+
return new Promise((resolve, reject) => {
124168
getAgentRuleConfigOptions().then(data => {
169+
// Guard: only apply results if agent hasn't changed
170+
if (agent.id !== requestedAgentId) {
171+
resolve('stale');
172+
return;
173+
}
174+
125175
ruleConfigs = data || {};
126176
const keys = Object.keys(data || {});
127177
const list = keys?.map(x => ({ name: x })) || [];
@@ -130,6 +180,14 @@
130180
...list
131181
];
132182
resolve('done');
183+
}).catch(error => {
184+
console.error('Failed to load agent rule config options:', error);
185+
// Guard: only apply error state if agent hasn't changed
186+
if (agent.id === requestedAgentId) {
187+
ruleConfigs = {};
188+
configOptions = [{ name: '' }];
189+
}
190+
reject(error);
133191
});
134192
});
135193
}

0 commit comments

Comments
 (0)