Skip to content

Commit 656854f

Browse files
author
Visagan Guruparan
committed
Update rule trigger api routing by agentId #3
1 parent 5674561 commit 656854f

1 file changed

Lines changed: 49 additions & 11 deletions

File tree

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

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,29 @@
8181
// React to agent.id changes to reload agent-specific trigger options
8282
$effect(() => {
8383
if (agent?.id) {
84+
// Capture current agent.id to prevent race conditions
85+
const requestedAgentId = agent.id;
86+
8487
// Reset state when agent changes
8588
ruleOptions = [];
8689
innerRules = [];
8790
88-
// Load agent-specific data
89-
Promise.all([
90-
loadAgentRuleOptions(),
91-
loadAgentRuleConfigOptions()
92-
]);
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+
})();
93107
}
94108
});
95109
@@ -98,9 +112,18 @@
98112
resizeWindow();
99113
});
100114
101-
function loadAgentRuleOptions() {
115+
/**
116+
* @param {string} requestedAgentId - The agent ID for which we're loading options
117+
*/
118+
function loadAgentRuleOptions(requestedAgentId) {
102119
return new Promise((resolve, reject) => {
103-
getAgentRuleOptionsById(agent.id).then(data => {
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+
104127
const list = data?.map(x => {
105128
return {
106129
name: x.trigger_name,
@@ -118,7 +141,10 @@
118141
resolve('done');
119142
}).catch(error => {
120143
console.error('Failed to load agent rule options:', error);
121-
ruleOptions = [{ name: "", displayName: "" }];
144+
// Guard: only apply error state if agent hasn't changed
145+
if (agent.id === requestedAgentId) {
146+
ruleOptions = [{ name: "", displayName: "" }];
147+
}
122148
reject(error);
123149
});
124150
});
@@ -134,9 +160,18 @@
134160
innerRefresh(list);
135161
}
136162
137-
function loadAgentRuleConfigOptions() {
163+
/**
164+
* @param {string} requestedAgentId - The agent ID for which we're loading config options
165+
*/
166+
function loadAgentRuleConfigOptions(requestedAgentId) {
138167
return new Promise((resolve, reject) => {
139168
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+
140175
ruleConfigs = data || {};
141176
const keys = Object.keys(data || {});
142177
const list = keys?.map(x => ({ name: x })) || [];
@@ -147,8 +182,11 @@
147182
resolve('done');
148183
}).catch(error => {
149184
console.error('Failed to load agent rule config options:', error);
150-
ruleConfigs = {};
151-
configOptions = [{ name: '' }];
185+
// Guard: only apply error state if agent hasn't changed
186+
if (agent.id === requestedAgentId) {
187+
ruleConfigs = {};
188+
configOptions = [{ name: '' }];
189+
}
152190
reject(error);
153191
});
154192
});

0 commit comments

Comments
 (0)