|
1 | 1 | <script> |
2 | | - import { onMount } from 'svelte'; |
3 | | - import { getAgentRuleOptions, getAgentRuleConfigOptions } from '$lib/services/agent-service'; |
| 2 | + import { getAgentRuleOptionsById, getAgentRuleConfigOptions } from '$lib/services/agent-service'; |
4 | 3 | import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte'; |
5 | 4 | import { scrollToBottom } from '$lib/helpers/utils/common'; |
6 | 5 | import AgentRuleItem from './agent-rule-item.svelte'; |
|
79 | 78 | /** @type {HTMLElement} */ |
80 | 79 | let scrollContainer; |
81 | 80 |
|
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(() => { |
83 | 112 | resizeWindow(); |
84 | | - Promise.all([ |
85 | | - loadAgentRuleOptions(), |
86 | | - loadAgentRuleConfigOptions() |
87 | | - ]); |
88 | 113 | }); |
89 | 114 |
|
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 | +
|
93 | 127 | const list = data?.map(x => { |
94 | 128 | return { |
95 | 129 | name: x.trigger_name, |
|
105 | 139 | }, ...list]; |
106 | 140 | init(); |
107 | 141 | 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); |
108 | 149 | }); |
109 | 150 | }); |
110 | 151 | } |
|
119 | 160 | innerRefresh(list); |
120 | 161 | } |
121 | 162 |
|
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) => { |
124 | 168 | 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 | +
|
125 | 175 | ruleConfigs = data || {}; |
126 | 176 | const keys = Object.keys(data || {}); |
127 | 177 | const list = keys?.map(x => ({ name: x })) || []; |
|
130 | 180 | ...list |
131 | 181 | ]; |
132 | 182 | 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); |
133 | 191 | }); |
134 | 192 | }); |
135 | 193 | } |
|
0 commit comments