forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuse-agent-update.ts
402 lines (352 loc) · 10.8 KB
/
use-agent-update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import { usePartialUpdate } from '@/hooks/use-partial-update';
import type { Agent } from '@elizaos/core';
import { useCallback, useRef } from 'react';
/**
* A custom hook for handling Agent updates with specific handling for JSONb fields.
* This hook builds on usePartialUpdate but adds Agent-specific convenience methods
* organized by the UI tabs (Basic Info, Content, Style, Plugins, etc.).
*
* @param initialAgent The initial Agent object
* @returns Object with agent state and update methods
*/
export function useAgentUpdate(initialAgent: Agent) {
// Keep reference to the initial state for comparison
const initialAgentRef = useRef<Agent>(JSON.parse(JSON.stringify(initialAgent)));
const {
value: agent,
updateField,
addArrayItem,
removeArrayItem,
reset,
updateSettings,
} = usePartialUpdate(initialAgent);
// ==================== Basic Info Tab ====================
/**
* Updates a field in the Agent's settings object
*
* @param path Path within settings (e.g., 'voice.model')
* @param value New value
*/
const updateSetting = useCallback(
<T>(path: string, value: T) => {
updateField(`settings.${path}`, value);
},
[updateField]
);
/**
* Updates the entire settings object
*
* @param settings The new settings object
*/
const setSettings = useCallback(
(settings: any) => {
updateSettings(settings);
},
[updateSettings]
);
/**
* Updates the agent's system prompt
*
* @param systemPrompt The new system prompt
*/
const updateSystemPrompt = useCallback(
(systemPrompt: string) => {
updateField('system', systemPrompt);
},
[updateField]
);
// ==================== Secrets Tab ====================
/**
* Updates a secret in the Agent's settings.secrets object
*
* @param key Secret key
* @param value Secret value
*/
const updateSecret = useCallback(
(key: string, value: string) => {
// Handle nested secrets object properly
const currentSettings = agent.settings || {};
const currentSecrets = currentSettings.secrets || {};
const newSecrets = {
...currentSecrets,
[key]: value,
};
// Update entire settings object for better change detection
updateSettings({
...currentSettings,
secrets: newSecrets,
});
},
[agent.settings, updateSettings]
);
/**
* Removes a secret from the Agent's settings.secrets object
*
* @param key Secret key to remove
*/
const removeSecret = useCallback(
(key: string) => {
// Get the current secrets object
const currentSettings = agent.settings || {};
const currentSecrets = currentSettings.secrets || {};
// Create a new secrets object without the removed key
const newSecrets = { ...currentSecrets };
delete newSecrets[key];
// Update the entire settings object to ensure nested changes are detected
const updatedSettings = {
...currentSettings,
secrets: newSecrets,
};
// Use updateSettings instead of updateField for better change detection
updateSettings(updatedSettings);
},
[agent.settings, updateSettings]
);
// ==================== Content Tab ====================
/**
* Adds an item to a content array (bio, topics, adjectives)
*
* @param arrayName The name of the array field
* @param item The item to add
*/
const addContentItem = useCallback(
(arrayName: 'bio' | 'topics' | 'adjectives', item: string) => {
addArrayItem(arrayName, item);
},
[addArrayItem]
);
/**
* Removes an item from a content array
*
* @param arrayName The name of the array field
* @param index The index of the item to remove
*/
const removeContentItem = useCallback(
(arrayName: 'bio' | 'topics' | 'adjectives', index: number) => {
removeArrayItem(arrayName, index);
},
[removeArrayItem]
);
/**
* Updates an item in a content array
*
* @param arrayName The name of the array field
* @param index The index of the item to update
* @param value The new value
*/
const updateContentItem = useCallback(
(arrayName: 'bio' | 'topics' | 'adjectives', index: number, value: string) => {
updateField(`${arrayName}.${index}`, value);
},
[updateField]
);
// ==================== Style Tab ====================
/**
* Adds a style rule to one of the style arrays
*
* @param styleType Type of style ('all', 'chat', 'post')
* @param rule The style rule to add
*/
const addStyleRule = useCallback(
(styleType: 'all' | 'chat' | 'post', rule: string) => {
addArrayItem(`style.${styleType}`, rule);
},
[addArrayItem]
);
/**
* Removes a style rule from one of the style arrays
*
* @param styleType Type of style ('all', 'chat', 'post')
* @param index The index of the rule to remove
*/
const removeStyleRule = useCallback(
(styleType: 'all' | 'chat' | 'post', index: number) => {
removeArrayItem(`style.${styleType}`, index);
},
[removeArrayItem]
);
/**
* Updates a style rule in one of the style arrays
*
* @param styleType Type of style ('all', 'chat', 'post')
* @param index The index of the rule to update
* @param value The new rule value
*/
const updateStyleRule = useCallback(
(styleType: 'all' | 'chat' | 'post', index: number, value: string) => {
updateField(`style.${styleType}.${index}`, value);
},
[updateField]
);
/**
* Sets a complete style array
*
* @param styleType Type of style ('all', 'chat', 'post')
* @param values Array of style values
*/
const setStyleArray = useCallback(
(styleType: 'all' | 'chat' | 'post', values: string[]) => {
updateField(`style.${styleType}`, values);
},
[updateField]
);
// ==================== Plugins Tab ====================
/**
* Adds a plugin to the agent's plugins array
*
* @param pluginId The plugin ID to add
*/
const addPlugin = useCallback(
(pluginId: string) => {
addArrayItem('plugins', pluginId);
},
[addArrayItem]
);
/**
* Removes a plugin from the agent's plugins array
*
* @param index The index of the plugin to remove
*/
const removePlugin = useCallback(
(index: number) => {
removeArrayItem('plugins', index);
},
[removeArrayItem]
);
/**
* Sets the entire plugins array
*
* @param plugins Array of plugin IDs
*/
const setPlugins = useCallback(
(plugins: string[]) => {
updateField('plugins', plugins);
},
[updateField]
);
// ==================== Avatar Tab ====================
/**
* Updates the agent's avatar
*
* @param avatarUrl The URL of the avatar image
*/
const updateAvatar = useCallback(
(avatarUrl: string) => {
updateSetting('avatar', avatarUrl);
},
[updateSetting]
);
/**
* Returns an object containing only the fields that have changed
* compared to the initial agent state
*/
const getChangedFields = useCallback(() => {
const changedFields: Partial<Agent> = {};
const current = agent;
const initial = initialAgentRef.current;
// Compare scalar properties
const scalarProps = ['name', 'username', 'system'] as const;
scalarProps.forEach((prop) => {
if (current[prop] !== initial[prop]) {
changedFields[prop] = current[prop];
}
});
if (current.enabled !== initial.enabled) {
changedFields.enabled = current.enabled;
}
// Compare array properties with type safety
if (JSON.stringify(current.bio) !== JSON.stringify(initial.bio)) {
changedFields.bio = current.bio;
}
if (JSON.stringify(current.topics) !== JSON.stringify(initial.topics)) {
changedFields.topics = current.topics;
}
if (JSON.stringify(current.adjectives) !== JSON.stringify(initial.adjectives)) {
changedFields.adjectives = current.adjectives;
}
if (JSON.stringify(current.plugins) !== JSON.stringify(initial.plugins)) {
changedFields.plugins = current.plugins;
}
// Compare style object
if (JSON.stringify(current.style) !== JSON.stringify(initial.style)) {
changedFields.style = current.style;
}
// More granular comparison for settings object
const initialSettings = initial.settings || {};
const currentSettings = current.settings || {};
// Check if any settings changed
if (JSON.stringify(currentSettings) !== JSON.stringify(initialSettings)) {
// Create a partial settings object with only changed fields
changedFields.settings = {};
// Check avatar separately
if (currentSettings.avatar !== initialSettings.avatar) {
changedFields.settings.avatar = currentSettings.avatar;
}
// Check voice settings
if (JSON.stringify(currentSettings.voice) !== JSON.stringify(initialSettings.voice)) {
changedFields.settings.voice = currentSettings.voice;
}
// Check secrets with special handling
if (JSON.stringify(currentSettings.secrets) !== JSON.stringify(initialSettings.secrets)) {
const initialSecrets = initialSettings.secrets || {};
const currentSecrets = currentSettings.secrets || {};
// Only include secrets that were added or modified
const changedSecrets: Record<string, any> = {};
let hasSecretChanges = false;
// Find added or modified secrets
Object.entries(currentSecrets).forEach(([key, value]) => {
if (initialSecrets[key] !== value) {
changedSecrets[key] = value;
hasSecretChanges = true;
}
});
// Find deleted secrets (null values indicate deletion)
Object.keys(initialSecrets).forEach((key) => {
if (currentSecrets[key] === undefined) {
changedSecrets[key] = null;
hasSecretChanges = true;
}
});
if (hasSecretChanges) {
if (!changedFields.settings) changedFields.settings = {};
changedFields.settings.secrets = changedSecrets;
}
}
// If no specific settings changed, don't include settings object
if (Object.keys(changedFields.settings).length === 0) {
delete changedFields.settings;
}
}
return changedFields;
}, [agent]);
return {
agent,
updateField,
reset,
updateSettings,
setSettings,
// Method to get only changed fields
getChangedFields,
// Basic Info Tab
updateSetting,
updateSystemPrompt,
// Secrets Tab
updateSecret,
removeSecret,
// Content Tab
addContentItem,
removeContentItem,
updateContentItem,
// Style Tab
addStyleRule,
removeStyleRule,
updateStyleRule,
setStyleArray,
// Plugins Tab
addPlugin,
removePlugin,
setPlugins,
// Avatar Tab
updateAvatar,
};
}