|
18 | 18 | import Swal from 'sweetalert2' |
19 | 19 | import { goto } from '$app/navigation'; |
20 | 20 | import { AgentExtensions } from '$lib/helpers/utils/agent'; |
21 | | - import LocalStorageManager from '$lib/helpers/utils/storage-manager'; |
22 | 21 | import AgentTemplate from './agent-components/agent-template.svelte'; |
23 | 22 |
|
24 | 23 | /** @type {import('$agentTypes').AgentModel} */ |
|
31 | 30 | let agentTemplateCmp = null; |
32 | 31 | /** @type {any} */ |
33 | 32 | let agentTabsCmp = null; |
34 | | - /** @type {import('$agentTypes').AgentModel} */ |
35 | | - let originalAgent; |
36 | | - /** @type {any} */ |
37 | | - let agentDraft = null; |
38 | 33 |
|
39 | 34 | /** @type {boolean} */ |
40 | 35 | let isLoading = false; |
41 | 36 | let isComplete = false; |
42 | 37 |
|
43 | 38 | const duration = 3000; |
44 | 39 | const params = $page.params; |
45 | | - const agentStorage = new LocalStorageManager(); |
46 | | - |
47 | 40 |
|
48 | 41 | onMount(() => { |
49 | 42 | isLoading = true; |
50 | | - agentDraft = getAgentDraft(); |
51 | 43 | getAgent(params.agentId).then(data => { |
52 | | - originalAgent = { |
53 | | - ...data, |
54 | | - llm_config: data.llm_config || {} |
55 | | - }; |
56 | | - if (agentDraft) { |
57 | | - agent = agentDraft; |
58 | | - } else { |
59 | | - agent = JSON.parse(JSON.stringify(originalAgent)); |
60 | | - } |
| 44 | + agent = data; |
61 | 45 | }).finally(() => { |
62 | 46 | isLoading = false; |
63 | 47 | }); |
64 | 48 | }); |
65 | 49 |
|
66 | | - function getAgentDraft() { |
67 | | - return agentStorage.get(`agent_draft_${params.agentId}`) |
68 | | - } |
69 | | -
|
70 | | - /** @param {any} data */ |
71 | | - function saveAgentDraft(data) { |
72 | | - agentStorage.set(`agent_draft_${params.agentId}`, data, 24 * 60 * 60 * 1000) |
73 | | - } |
74 | | -
|
75 | | - function deleteAgentDraft() { |
76 | | - agentStorage.remove(`agent_draft_${params.agentId}`); |
77 | | - } |
78 | | -
|
79 | 50 | function updateCurrentAgent() { |
80 | 51 | Swal.fire({ |
81 | 52 | title: 'Are you sure?', |
|
107 | 78 | utilities: agent.utilities || [], |
108 | 79 | knowledge_bases: agent.knowledge_bases || [], |
109 | 80 | rules: agent.rules || [], |
110 | | - max_message_count: Number(agent.max_message_count) > 0 ? Number(agent.max_message_count) : null |
| 81 | + max_message_count: Number(agent.max_message_count) > 0 ? Number(agent.max_message_count) : null, |
| 82 | + llm_config: { |
| 83 | + ...agent.llm_config, |
| 84 | + max_output_tokens: Number(agent.llm_config.max_output_tokens) > 0 ? Number(agent.llm_config.max_output_tokens) : null |
| 85 | + } |
111 | 86 | }; |
112 | 87 | isLoading = true; |
113 | 88 | saveAgent(agent).then(res => { |
114 | 89 | isLoading = false; |
115 | 90 | isComplete = true; |
116 | | - deleteAgentDraft(); |
117 | | - refreshInstructions(); |
118 | | - refreshTemplates(); |
| 91 | + refresh(); |
119 | 92 | setTimeout(() => { |
120 | 93 | isComplete = false; |
121 | 94 | }, duration); |
|
144 | 117 | agent.responses = data.responses; |
145 | 118 | } |
146 | 119 |
|
147 | | - // Insturctions |
148 | | - function formatOriginalInstructions() { |
149 | | - const obj = agentInstructionCmp?.fetchOriginalInstructions(); |
150 | | - return { |
151 | | - instruction: obj.systemPrompt, |
152 | | - channel_instructions: obj.channelPrompts || [] |
153 | | - } |
154 | | - } |
155 | | -
|
156 | 120 | function fetchInstructions() { |
157 | 121 | const obj = agentInstructionCmp?.fetchInstructions(); |
158 | 122 | agent.instruction = obj.systemPrompt; |
159 | 123 | agent.channel_instructions = obj.channelPrompts || []; |
160 | 124 | } |
161 | 125 |
|
162 | | - function refreshInstructions() { |
163 | | - agentInstructionCmp?.refresh(); |
164 | | - } |
165 | | -
|
166 | | - // Templates |
167 | | - function formatOriginalTemplates() { |
168 | | - const templates = agentTemplateCmp?.fetchOriginalTemplates(); |
169 | | - return { |
170 | | - templates: templates || [] |
171 | | - } |
172 | | - } |
173 | | -
|
174 | 126 | function fetchTemplates() { |
175 | 127 | agent.templates = agentTemplateCmp?.fetchTemplates();; |
176 | 128 | } |
177 | 129 |
|
178 | | - function refreshTemplates() { |
179 | | - agentTemplateCmp?.refresh(); |
180 | | - } |
181 | | -
|
182 | | -
|
183 | 130 | // Tab data |
184 | | - function formatOriginalTabData() { |
185 | | - const data = agentTabsCmp?.fetchOriginalTabData(); |
186 | | - return data ? { |
187 | | - utilities: data.utilities || [], |
188 | | - knowledge_bases: data.knwoledgebases || [], |
189 | | - rules: data.rules || [], |
190 | | - mcp_tools: data.mcpTools || [] |
191 | | - } : null; |
192 | | - } |
193 | | -
|
194 | 131 | function fetchTabData() { |
195 | 132 | const data = agentTabsCmp?.fetchTabData(); |
196 | 133 | if (data) { |
|
221 | 158 |
|
222 | 159 | function handleAgentDelete() { |
223 | 160 | deleteAgent(agent?.id).then(res => { |
224 | | - deleteAgentDraft(); |
225 | 161 | goto(`page/agent`); |
226 | 162 | }); |
227 | 163 | } |
228 | 164 |
|
229 | | - function handleAgentChange() { |
230 | | - const data = { |
231 | | - ...agent, |
232 | | - ...formatJsonContent(), |
233 | | - ...formatOriginalInstructions(), |
234 | | - ...formatOriginalTemplates(), |
235 | | - ...formatOriginalTabData(), |
236 | | - }; |
237 | | - saveAgentDraft(data); |
238 | | - } |
239 | | -
|
240 | | - function handleAgentReset() { |
241 | | - agent = JSON.parse(JSON.stringify(originalAgent)); |
242 | | - agentDraft = null; |
243 | | - deleteAgentDraft(); |
244 | | - setTimeout(() => { |
245 | | - refreshInstructions(); |
246 | | - refreshTemplates(); |
247 | | - agentFunctionCmp?.refresh(); |
248 | | - agentTabsCmp?.refresh(); |
249 | | - }); |
| 165 | + function refresh() { |
| 166 | + agentInstructionCmp?.refresh(); |
250 | 167 | } |
251 | 168 | </script> |
252 | 169 |
|
|
263 | 180 | agent={agent} |
264 | 181 | profiles={agent.profiles || []} |
265 | 182 | labels={agent.labels || []} |
266 | | - resetable={!!agentDraft} |
267 | | - resetAgent={() => handleAgentReset()} |
268 | | - {handleAgentChange} |
269 | 183 | /> |
270 | 184 | </div> |
271 | 185 | <div class="agent-detail-section"> |
272 | 186 | <AgentTabs |
273 | 187 | bind:this={agentTabsCmp} |
274 | 188 | agent={agent} |
275 | | - {handleAgentChange} |
276 | 189 | /> |
277 | 190 | </div> |
278 | 191 | </Col> |
|
281 | 194 | <AgentInstruction |
282 | 195 | bind:this={agentInstructionCmp} |
283 | 196 | agent={agent} |
284 | | - {handleAgentChange} |
285 | 197 | /> |
286 | 198 | </div> |
287 | 199 | <div class="agent-detail-section"> |
288 | 200 | <AgentTemplate |
289 | 201 | bind:this={agentTemplateCmp} |
290 | 202 | agent={agent} |
291 | | - {handleAgentChange} |
292 | 203 | /> |
293 | 204 | </div> |
294 | 205 | <div class="agent-detail-section"> |
295 | 206 | <AgentFunction |
296 | 207 | bind:this={agentFunctionCmp} |
297 | 208 | agent={agent} |
298 | | - {handleAgentChange} |
299 | 209 | /> |
300 | 210 | </div> |
301 | 211 | </Col> |
302 | 212 | </Row> |
303 | 213 |
|
304 | | - {#if !!AgentExtensions.editable(originalAgent)} |
| 214 | + {#if !!AgentExtensions.editable(agent)} |
305 | 215 | <Row> |
306 | 216 | <div class="hstack gap-2 my-4"> |
307 | 217 | <Button class="btn btn-soft-primary" on:click={() => updateCurrentAgent()}>{$_('Save Agent')}</Button> |
|
0 commit comments