@elizaos/client 1.5.5-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +350 -0
  3. package/dist/assets/empty-module-CLMscLYw.js +1 -0
  4. package/dist/assets/main-BBZ_3lkn.css +5999 -0
  5. package/dist/assets/main-C5zNUkXH.js +7 -0
  6. package/dist/assets/main-Dz64ENQg.js +614 -0
  7. package/dist/assets/react-vendor-DM5m98rr.js +545 -0
  8. package/dist/assets/ui-vendor-BQCqNqg0.js +1 -0
  9. package/dist/elizaos-avatar.png +0 -0
  10. package/dist/elizaos-icon.png +0 -0
  11. package/dist/elizaos-logo-light.png +0 -0
  12. package/dist/elizaos.webp +0 -0
  13. package/dist/favicon.ico +0 -0
  14. package/dist/images/agents/agent1.png +0 -0
  15. package/dist/images/agents/agent2.png +0 -0
  16. package/dist/images/agents/agent3.png +0 -0
  17. package/dist/images/agents/agent4.png +0 -0
  18. package/dist/images/agents/agent5.png +0 -0
  19. package/dist/index.html +14 -0
  20. package/index.html +24 -0
  21. package/package.json +159 -0
  22. package/postcss.config.js +3 -0
  23. package/public/elizaos-avatar.png +0 -0
  24. package/public/elizaos-icon.png +0 -0
  25. package/public/elizaos-logo-light.png +0 -0
  26. package/public/elizaos.webp +0 -0
  27. package/public/favicon.ico +0 -0
  28. package/public/images/agents/agent1.png +0 -0
  29. package/public/images/agents/agent2.png +0 -0
  30. package/public/images/agents/agent3.png +0 -0
  31. package/public/images/agents/agent4.png +0 -0
  32. package/public/images/agents/agent5.png +0 -0
  33. package/src/App.tsx +222 -0
  34. package/src/components/AgentDetailsPanel.tsx +147 -0
  35. package/src/components/ChatInputArea.tsx +196 -0
  36. package/src/components/ChatMessageListComponent.tsx +139 -0
  37. package/src/components/actionTool.tsx +186 -0
  38. package/src/components/add-agent-card.tsx +77 -0
  39. package/src/components/agent-action-viewer.tsx +816 -0
  40. package/src/components/agent-avatar-stack.tsx +121 -0
  41. package/src/components/agent-card.cy.tsx +259 -0
  42. package/src/components/agent-card.tsx +177 -0
  43. package/src/components/agent-creator.tsx +142 -0
  44. package/src/components/agent-log-viewer.tsx +645 -0
  45. package/src/components/agent-memory-edit-overlay.tsx +461 -0
  46. package/src/components/agent-memory-viewer.tsx +504 -0
  47. package/src/components/agent-settings.tsx +270 -0
  48. package/src/components/agent-sidebar.tsx +178 -0
  49. package/src/components/api-key-dialog.tsx +113 -0
  50. package/src/components/app-sidebar.tsx +685 -0
  51. package/src/components/array-input.tsx +116 -0
  52. package/src/components/audio-recorder.tsx +292 -0
  53. package/src/components/avatar-panel.tsx +141 -0
  54. package/src/components/character-form.tsx +1138 -0
  55. package/src/components/chat.tsx +1813 -0
  56. package/src/components/combobox.tsx +187 -0
  57. package/src/components/confirmation-dialog.tsx +59 -0
  58. package/src/components/connection-error-banner.tsx +101 -0
  59. package/src/components/connection-status.cy.tsx +73 -0
  60. package/src/components/connection-status.tsx +155 -0
  61. package/src/components/copy-button.tsx +35 -0
  62. package/src/components/delete-button.tsx +24 -0
  63. package/src/components/env-settings.tsx +261 -0
  64. package/src/components/group-card.tsx +160 -0
  65. package/src/components/group-panel.tsx +543 -0
  66. package/src/components/input-copy.tsx +21 -0
  67. package/src/components/logs-page.tsx +41 -0
  68. package/src/components/media-content.tsx +385 -0
  69. package/src/components/memory-graph.tsx +170 -0
  70. package/src/components/missing-secrets-dialog.tsx +72 -0
  71. package/src/components/onboarding-tour.tsx +247 -0
  72. package/src/components/page-title.tsx +8 -0
  73. package/src/components/plugins-panel.tsx +383 -0
  74. package/src/components/profile-card.tsx +66 -0
  75. package/src/components/profile-overlay.tsx +283 -0
  76. package/src/components/retry-button.tsx +28 -0
  77. package/src/components/secret-panel.tsx +1505 -0
  78. package/src/components/server-management.tsx +264 -0
  79. package/src/components/split-button.tsx +148 -0
  80. package/src/components/stop-agent-button.tsx +99 -0
  81. package/src/components/ui/alert-dialog.cy.tsx +333 -0
  82. package/src/components/ui/alert-dialog.tsx +115 -0
  83. package/src/components/ui/alert.tsx +49 -0
  84. package/src/components/ui/avatar.cy.tsx +180 -0
  85. package/src/components/ui/avatar.tsx +57 -0
  86. package/src/components/ui/badge.cy.tsx +146 -0
  87. package/src/components/ui/badge.tsx +43 -0
  88. package/src/components/ui/button.cy.tsx +177 -0
  89. package/src/components/ui/button.tsx +56 -0
  90. package/src/components/ui/card.cy.tsx +160 -0
  91. package/src/components/ui/card.tsx +73 -0
  92. package/src/components/ui/chat/animated-markdown.tsx +59 -0
  93. package/src/components/ui/chat/chat-bubble.tsx +178 -0
  94. package/src/components/ui/chat/chat-container.tsx +51 -0
  95. package/src/components/ui/chat/chat-input.cy.tsx +169 -0
  96. package/src/components/ui/chat/chat-input.tsx +47 -0
  97. package/src/components/ui/chat/chat-message-list.tsx +61 -0
  98. package/src/components/ui/chat/chat-tts-button.tsx +199 -0
  99. package/src/components/ui/chat/code-block.tsx +79 -0
  100. package/src/components/ui/chat/expandable-chat.tsx +131 -0
  101. package/src/components/ui/chat/hooks/useAutoScroll.ts +86 -0
  102. package/src/components/ui/chat/markdown.tsx +209 -0
  103. package/src/components/ui/chat/message-loading.tsx +48 -0
  104. package/src/components/ui/checkbox.cy.tsx +170 -0
  105. package/src/components/ui/checkbox.tsx +30 -0
  106. package/src/components/ui/collapsible.cy.tsx +283 -0
  107. package/src/components/ui/collapsible.tsx +9 -0
  108. package/src/components/ui/command.cy.tsx +313 -0
  109. package/src/components/ui/command.tsx +143 -0
  110. package/src/components/ui/dialog.cy.tsx +279 -0
  111. package/src/components/ui/dialog.tsx +104 -0
  112. package/src/components/ui/dropdown-menu.cy.tsx +273 -0
  113. package/src/components/ui/dropdown-menu.tsx +281 -0
  114. package/src/components/ui/input.cy.tsx +82 -0
  115. package/src/components/ui/input.tsx +27 -0
  116. package/src/components/ui/label.cy.tsx +157 -0
  117. package/src/components/ui/label.tsx +19 -0
  118. package/src/components/ui/resizable.tsx +42 -0
  119. package/src/components/ui/scroll-area.cy.tsx +242 -0
  120. package/src/components/ui/scroll-area.tsx +46 -0
  121. package/src/components/ui/select.cy.tsx +277 -0
  122. package/src/components/ui/select.tsx +155 -0
  123. package/src/components/ui/separator.cy.tsx +145 -0
  124. package/src/components/ui/separator.tsx +29 -0
  125. package/src/components/ui/sheet.cy.tsx +324 -0
  126. package/src/components/ui/sheet.tsx +119 -0
  127. package/src/components/ui/sidebar.tsx +734 -0
  128. package/src/components/ui/skeleton.cy.tsx +149 -0
  129. package/src/components/ui/skeleton.tsx +17 -0
  130. package/src/components/ui/split-button.cy.tsx +274 -0
  131. package/src/components/ui/split-button.tsx +112 -0
  132. package/src/components/ui/switch.tsx +28 -0
  133. package/src/components/ui/tabs.cy.tsx +271 -0
  134. package/src/components/ui/tabs.tsx +53 -0
  135. package/src/components/ui/textarea.cy.tsx +136 -0
  136. package/src/components/ui/textarea.tsx +26 -0
  137. package/src/components/ui/toast.cy.tsx +209 -0
  138. package/src/components/ui/toast.tsx +126 -0
  139. package/src/components/ui/toaster.tsx +29 -0
  140. package/src/components/ui/tooltip.cy.tsx +244 -0
  141. package/src/components/ui/tooltip.tsx +30 -0
  142. package/src/config/agent-templates.ts +349 -0
  143. package/src/config/voice-models.ts +181 -0
  144. package/src/constants.ts +23 -0
  145. package/src/context/AuthContext.tsx +44 -0
  146. package/src/context/ConnectionContext.tsx +194 -0
  147. package/src/entry.tsx +9 -0
  148. package/src/hooks/__tests__/use-agent-tab-state.test.ts +137 -0
  149. package/src/hooks/__tests__/use-agent-update.test.tsx +250 -0
  150. package/src/hooks/__tests__/use-character-convert.test.ts +102 -0
  151. package/src/hooks/__tests__/use-panel-width-state.test.ts +243 -0
  152. package/src/hooks/__tests__/use-sidebar-state.test.ts +117 -0
  153. package/src/hooks/use-agent-management.ts +130 -0
  154. package/src/hooks/use-agent-tab-state.ts +74 -0
  155. package/src/hooks/use-agent-update.ts +469 -0
  156. package/src/hooks/use-character-convert.ts +138 -0
  157. package/src/hooks/use-confirmation.ts +55 -0
  158. package/src/hooks/use-delete-agent.ts +123 -0
  159. package/src/hooks/use-dm-channels.ts +198 -0
  160. package/src/hooks/use-elevenlabs-voices.ts +83 -0
  161. package/src/hooks/use-file-upload.ts +224 -0
  162. package/src/hooks/use-mobile.tsx +19 -0
  163. package/src/hooks/use-onboarding.tsx +49 -0
  164. package/src/hooks/use-panel-width-state.ts +147 -0
  165. package/src/hooks/use-partial-update.ts +288 -0
  166. package/src/hooks/use-plugin-details.ts +462 -0
  167. package/src/hooks/use-plugins.ts +119 -0
  168. package/src/hooks/use-query-hooks.ts +1263 -0
  169. package/src/hooks/use-server-agents.ts +62 -0
  170. package/src/hooks/use-server-version.tsx +47 -0
  171. package/src/hooks/use-sidebar-state.ts +50 -0
  172. package/src/hooks/use-socket-chat.ts +264 -0
  173. package/src/hooks/use-toast.ts +260 -0
  174. package/src/hooks/use-version.tsx +64 -0
  175. package/src/index.css +146 -0
  176. package/src/lib/api-client-config.ts +53 -0
  177. package/src/lib/api-type-mappers.ts +196 -0
  178. package/src/lib/export-utils.ts +123 -0
  179. package/src/lib/logger.ts +19 -0
  180. package/src/lib/media-utils.ts +170 -0
  181. package/src/lib/pca.test.ts +17 -0
  182. package/src/lib/pca.ts +52 -0
  183. package/src/lib/socketio-manager.ts +664 -0
  184. package/src/lib/utils.ts +168 -0
  185. package/src/main.tsx +16 -0
  186. package/src/mocks/empty-module.ts +12 -0
  187. package/src/mocks/node-module.ts +57 -0
  188. package/src/polyfills.ts +37 -0
  189. package/src/routes/agent-detail.tsx +30 -0
  190. package/src/routes/agent-list.tsx +27 -0
  191. package/src/routes/agent-settings.tsx +48 -0
  192. package/src/routes/character-detail.tsx +52 -0
  193. package/src/routes/character-form.tsx +79 -0
  194. package/src/routes/character-list.tsx +38 -0
  195. package/src/routes/chat.tsx +128 -0
  196. package/src/routes/createAgent.tsx +13 -0
  197. package/src/routes/group-new.tsx +50 -0
  198. package/src/routes/group.tsx +29 -0
  199. package/src/routes/home.tsx +218 -0
  200. package/src/routes/not-found.tsx +71 -0
  201. package/src/test/setup.ts +154 -0
  202. package/src/types/crypto-browserify.d.ts +4 -0
  203. package/src/types/index.ts +13 -0
  204. package/src/types/rooms.ts +8 -0
  205. package/src/types.ts +84 -0
  206. package/src/vite-env.d.ts +40 -0
  207. package/tailwind.config.ts +90 -0
  208. package/tsconfig.json +10 -0
  209. package/vite.config.ts +102 -0
@@ -0,0 +1,147 @@
1
+ import { useState, useEffect, useCallback } from 'react';
2
+ import clientLogger from '@/lib/logger';
3
+
4
+ // Keys for storing panel width state in localStorage
5
+ const MAIN_PANEL_WIDTH_KEY = 'eliza-main-panel-width';
6
+ const SIDEBAR_PANEL_WIDTH_KEY = 'eliza-sidebar-panel-width';
7
+ const FLOATING_THRESHOLD_KEY = 'eliza-floating-threshold';
8
+
9
+ // Default panel sizes
10
+ const DEFAULT_MAIN_PANEL_SIZE = 70;
11
+ const DEFAULT_SIDEBAR_PANEL_SIZE = 30;
12
+ const DEFAULT_FLOATING_THRESHOLD = 1400; // Switch to floating mode below this width
13
+
14
+ /**
15
+ * Custom hook to manage panel width state with localStorage persistence
16
+ * Includes resize detection for automatic floating mode switching
17
+ */
18
+ export function usePanelWidthState() {
19
+ const [mainPanelSize, setMainPanelSize] = useState<number>(DEFAULT_MAIN_PANEL_SIZE);
20
+ const [sidebarPanelSize, setSidebarPanelSize] = useState<number>(DEFAULT_SIDEBAR_PANEL_SIZE);
21
+ const [isFloatingMode, setIsFloatingMode] = useState<boolean>(false);
22
+ const [floatingThreshold, setFloatingThreshold] = useState<number>(DEFAULT_FLOATING_THRESHOLD);
23
+
24
+ // Load panel sizes from localStorage on mount
25
+ useEffect(() => {
26
+ try {
27
+ const storedMainSize = localStorage.getItem(MAIN_PANEL_WIDTH_KEY);
28
+ const storedSidebarSize = localStorage.getItem(SIDEBAR_PANEL_WIDTH_KEY);
29
+ const storedThreshold = localStorage.getItem(FLOATING_THRESHOLD_KEY);
30
+
31
+ if (storedMainSize !== null) {
32
+ const parsedMainSize = parseFloat(storedMainSize);
33
+ if (!isNaN(parsedMainSize) && parsedMainSize > 0 && parsedMainSize <= 100) {
34
+ setMainPanelSize(parsedMainSize);
35
+ }
36
+ }
37
+
38
+ if (storedSidebarSize !== null) {
39
+ const parsedSidebarSize = parseFloat(storedSidebarSize);
40
+ if (!isNaN(parsedSidebarSize) && parsedSidebarSize > 0 && parsedSidebarSize <= 100) {
41
+ setSidebarPanelSize(parsedSidebarSize);
42
+ }
43
+ }
44
+
45
+ if (storedThreshold !== null) {
46
+ const parsedThreshold = parseFloat(storedThreshold);
47
+ if (!isNaN(parsedThreshold) && parsedThreshold > 0) {
48
+ setFloatingThreshold(parsedThreshold);
49
+ }
50
+ }
51
+ } catch (error) {
52
+ clientLogger.error('Error reading panel sizes from localStorage:', error);
53
+ // Use defaults if there's an error
54
+ setMainPanelSize(DEFAULT_MAIN_PANEL_SIZE);
55
+ setSidebarPanelSize(DEFAULT_SIDEBAR_PANEL_SIZE);
56
+ setFloatingThreshold(DEFAULT_FLOATING_THRESHOLD);
57
+ }
58
+ }, []);
59
+
60
+ // Resize listener to detect when to switch to floating mode
61
+ useEffect(() => {
62
+ const checkFloatingMode = () => {
63
+ const shouldFloat = window.innerWidth < floatingThreshold;
64
+ setIsFloatingMode(shouldFloat);
65
+ };
66
+
67
+ // Check on mount
68
+ checkFloatingMode();
69
+
70
+ // Add resize listener
71
+ window.addEventListener('resize', checkFloatingMode);
72
+
73
+ return () => {
74
+ window.removeEventListener('resize', checkFloatingMode);
75
+ };
76
+ }, [floatingThreshold]);
77
+
78
+ // Update main panel size and persist to localStorage
79
+ const setMainPanelSizePersistent = useCallback((size: number) => {
80
+ // Validate size is within reasonable bounds
81
+ const validatedSize = Math.max(20, Math.min(80, size));
82
+ setMainPanelSize(validatedSize);
83
+
84
+ try {
85
+ localStorage.setItem(MAIN_PANEL_WIDTH_KEY, validatedSize.toString());
86
+ } catch (error) {
87
+ clientLogger.error('Error saving main panel size to localStorage:', error);
88
+ }
89
+ }, []);
90
+
91
+ // Update sidebar panel size and persist to localStorage
92
+ const setSidebarPanelSizePersistent = useCallback((size: number) => {
93
+ // Validate size is within reasonable bounds
94
+ const validatedSize = Math.max(20, Math.min(80, size));
95
+ setSidebarPanelSize(validatedSize);
96
+
97
+ try {
98
+ localStorage.setItem(SIDEBAR_PANEL_WIDTH_KEY, validatedSize.toString());
99
+ } catch (error) {
100
+ clientLogger.error('Error saving sidebar panel size to localStorage:', error);
101
+ }
102
+ }, []);
103
+
104
+ // Update floating threshold and persist to localStorage
105
+ const setFloatingThresholdPersistent = useCallback((threshold: number) => {
106
+ const validatedThreshold = Math.max(320, Math.min(2560, threshold));
107
+ setFloatingThreshold(validatedThreshold);
108
+
109
+ try {
110
+ localStorage.setItem(FLOATING_THRESHOLD_KEY, validatedThreshold.toString());
111
+ } catch (error) {
112
+ clientLogger.error('Error saving floating threshold to localStorage:', error);
113
+ }
114
+ }, []);
115
+
116
+ // Manual toggle for floating mode (overrides automatic detection)
117
+ const toggleFloatingMode = useCallback(() => {
118
+ setIsFloatingMode((prev) => !prev);
119
+ }, []);
120
+
121
+ // Reset to default sizes
122
+ const resetPanelSizes = useCallback(() => {
123
+ setMainPanelSize(DEFAULT_MAIN_PANEL_SIZE);
124
+ setSidebarPanelSize(DEFAULT_SIDEBAR_PANEL_SIZE);
125
+ setFloatingThreshold(DEFAULT_FLOATING_THRESHOLD);
126
+
127
+ try {
128
+ localStorage.removeItem(MAIN_PANEL_WIDTH_KEY);
129
+ localStorage.removeItem(SIDEBAR_PANEL_WIDTH_KEY);
130
+ localStorage.removeItem(FLOATING_THRESHOLD_KEY);
131
+ } catch (error) {
132
+ clientLogger.error('Error clearing panel sizes from localStorage:', error);
133
+ }
134
+ }, []);
135
+
136
+ return {
137
+ mainPanelSize,
138
+ sidebarPanelSize,
139
+ isFloatingMode,
140
+ floatingThreshold,
141
+ setMainPanelSize: setMainPanelSizePersistent,
142
+ setSidebarPanelSize: setSidebarPanelSizePersistent,
143
+ setFloatingThreshold: setFloatingThresholdPersistent,
144
+ toggleFloatingMode,
145
+ resetPanelSizes,
146
+ };
147
+ }
@@ -0,0 +1,288 @@
1
+ import { useState, useCallback } from 'react';
2
+
3
+ /**
4
+ * A custom hook for handling partial updates of objects with nested JSONb fields.
5
+ * This hook ensures that updates to nested objects and arrays are properly
6
+ * managed when sending updates to the server.
7
+ *
8
+ * @param initialValue The initial state object
9
+ * @returns A tuple containing:
10
+ * - The current state object
11
+ * - A function to update a specific field (handles nested paths)
12
+ * - A function to add an item to an array field
13
+ * - A function to remove an item from an array field
14
+ * - A function to set the entire object
15
+ * - A function to reset to initial state
16
+ */
17
+ export function usePartialUpdate<T extends object>(initialValue: T) {
18
+ const [value, setValue] = useState<T>(initialValue);
19
+
20
+ /**
21
+ * Updates a specific field in the object, handling nested paths
22
+ *
23
+ * @param path The path to the field to update (e.g., 'settings.voice.model')
24
+ * @param newValue The new value for the field
25
+ */
26
+ const updateField = useCallback(<K>(path: string, newValue: K) => {
27
+ setValue((prevValue) => {
28
+ // Handle simple (non-nested) case
29
+ if (!path.includes('.')) {
30
+ return {
31
+ ...prevValue,
32
+ [path]: newValue,
33
+ } as T;
34
+ }
35
+
36
+ // Handle nested paths
37
+ const pathParts = path.split('.');
38
+ const fieldToUpdate = pathParts[0];
39
+ const remainingPath = pathParts.slice(1).join('.');
40
+
41
+ // Handle arrays in path (e.g., 'style.all.0')
42
+ const isArrayIndex = !isNaN(Number(pathParts[1]));
43
+
44
+ if (isArrayIndex) {
45
+ const arrayName = pathParts[0];
46
+ const index = Number(pathParts[1]);
47
+ // Ensure we're working with an array and handle it safely
48
+ const currentValue = prevValue[arrayName as keyof T];
49
+ const array = Array.isArray(currentValue) ? [...currentValue] : [];
50
+
51
+ if (pathParts.length === 2) {
52
+ // Direct array item update
53
+ array[index] = newValue;
54
+ } else {
55
+ // Updating a property of an object in an array
56
+ const deeperPath = pathParts.slice(2).join('.');
57
+ array[index] = updateNestedObject(array[index], deeperPath, newValue);
58
+ }
59
+
60
+ return {
61
+ ...prevValue,
62
+ [arrayName]: array,
63
+ } as T;
64
+ }
65
+
66
+ // Special case for settings.secrets path
67
+ if (path.startsWith('settings.secrets.')) {
68
+ const secretKey = path.split('.')[2];
69
+
70
+ const currentSettings = (prevValue as any).settings || {};
71
+ const currentSecrets = currentSettings.secrets || {};
72
+
73
+ const newSecrets = {
74
+ ...currentSecrets,
75
+ [secretKey]: newValue,
76
+ };
77
+
78
+ return {
79
+ ...prevValue,
80
+ settings: {
81
+ ...currentSettings,
82
+ secrets: newSecrets,
83
+ },
84
+ } as T;
85
+ }
86
+
87
+ // Handle regular nested objects
88
+ const result = {
89
+ ...prevValue,
90
+ [fieldToUpdate]: updateNestedObject(
91
+ prevValue[fieldToUpdate as keyof T],
92
+ remainingPath,
93
+ newValue
94
+ ),
95
+ } as T;
96
+
97
+ return result;
98
+ });
99
+ }, []);
100
+
101
+ /**
102
+ * Helper function to update a nested object
103
+ */
104
+ const updateNestedObject = <K, V>(obj: K, path: string, value: V): K => {
105
+ if (!path.includes('.')) {
106
+ return {
107
+ ...obj,
108
+ [path]: value,
109
+ } as unknown as K;
110
+ }
111
+
112
+ const [field, ...remainingPath] = path.split('.');
113
+ const nextPath = remainingPath.join('.');
114
+
115
+ return {
116
+ ...obj,
117
+ [field]: updateNestedObject((obj as any)[field] || {}, nextPath, value),
118
+ } as unknown as K;
119
+ };
120
+
121
+ /**
122
+ * Adds an item to an array field
123
+ *
124
+ * @param path Path to the array field
125
+ * @param item Item to add
126
+ */
127
+ const addArrayItem = useCallback(<V>(path: string, item: V) => {
128
+ setValue((prevValue) => {
129
+ const pathParts = path.split('.');
130
+
131
+ // Handle simple array field
132
+ if (pathParts.length === 1) {
133
+ const fieldName = pathParts[0];
134
+ const currentArray = Array.isArray(prevValue[fieldName as keyof T])
135
+ ? [...(prevValue[fieldName as keyof T] as unknown as V[])]
136
+ : [];
137
+
138
+ return {
139
+ ...prevValue,
140
+ [fieldName]: [...currentArray, item],
141
+ } as T;
142
+ }
143
+
144
+ // Handle nested array field
145
+ const updatePath = path;
146
+ const currentValue = getNestedValue(prevValue, updatePath);
147
+ const currentArray = Array.isArray(currentValue) ? [...currentValue] : [];
148
+
149
+ return setNestedValue(prevValue, updatePath, [...currentArray, item]);
150
+ });
151
+ }, []);
152
+
153
+ /**
154
+ * Removes an item from an array field
155
+ *
156
+ * @param path Path to the array field
157
+ * @param index Index of the item to remove
158
+ */
159
+ const removeArrayItem = useCallback((path: string, index: number) => {
160
+ setValue((prevValue) => {
161
+ const pathParts = path.split('.');
162
+
163
+ // Handle simple array field
164
+ if (pathParts.length === 1) {
165
+ const fieldName = pathParts[0];
166
+ const currentArray = Array.isArray(prevValue[fieldName as keyof T])
167
+ ? [...(prevValue[fieldName as keyof T] as unknown as any[])]
168
+ : [];
169
+
170
+ if (index < 0 || index >= currentArray.length) return prevValue;
171
+
172
+ return {
173
+ ...prevValue,
174
+ [fieldName]: [...currentArray.slice(0, index), ...currentArray.slice(index + 1)],
175
+ } as T;
176
+ }
177
+
178
+ // Handle nested array field
179
+ const updatePath = path;
180
+ const currentValue = getNestedValue(prevValue, updatePath);
181
+
182
+ if (!Array.isArray(currentValue) || index < 0 || index >= currentValue.length) {
183
+ return prevValue;
184
+ }
185
+
186
+ const newArray = [...currentValue.slice(0, index), ...currentValue.slice(index + 1)];
187
+ return setNestedValue(prevValue, updatePath, newArray);
188
+ });
189
+ }, []);
190
+
191
+ /**
192
+ * Helper function to get a nested value from an object
193
+ */
194
+ const getNestedValue = (obj: any, path: string): any => {
195
+ const parts = path.split('.');
196
+ let current = obj;
197
+
198
+ for (const part of parts) {
199
+ if (current === null || current === undefined) {
200
+ return undefined;
201
+ }
202
+ current = current[part];
203
+ }
204
+
205
+ return current;
206
+ };
207
+
208
+ /**
209
+ * Helper function to set a nested value in an object
210
+ */
211
+ const setNestedValue = <O, V>(obj: O, path: string, value: V): O => {
212
+ const parts = path.split('.');
213
+
214
+ if (parts.length === 1) {
215
+ return {
216
+ ...obj,
217
+ [parts[0]]: value,
218
+ } as O;
219
+ }
220
+
221
+ const [first, ...rest] = parts;
222
+ const nextObj = (obj as any)[first] || {};
223
+
224
+ return {
225
+ ...obj,
226
+ [first]: setNestedValue(nextObj, rest.join('.'), value),
227
+ } as O;
228
+ };
229
+
230
+ /**
231
+ * Resets to the initial state
232
+ */
233
+ const reset = useCallback(() => {
234
+ setValue(initialValue);
235
+ }, [initialValue]);
236
+
237
+ // Special handling for updating the entire settings object
238
+ const updateSettings = useCallback(
239
+ (settings: any) => {
240
+ setValue((prevValue) => {
241
+ // Extract settings but remove 'secrets' key to avoid duplication
242
+ const { secrets, avatar, ...otherSettings } = settings;
243
+
244
+ // Create the updated settings object
245
+ const updatedSettings = {
246
+ ...(prevValue as any).settings, // Start with existing settings
247
+ ...otherSettings, // Add other settings (not secrets)
248
+ };
249
+
250
+ if (typeof avatar === 'string') {
251
+ updatedSettings.avatar = avatar;
252
+ }
253
+
254
+ // Only add secrets if it was included in the update
255
+ if (secrets) {
256
+ // Create a new secrets object that only contains non-null values
257
+ const filteredSecrets: Record<string, any> = {};
258
+
259
+ Object.entries(secrets).forEach(([key, value]) => {
260
+ // If value is null, don't include it (this is how we delete)
261
+ if (value !== null) {
262
+ filteredSecrets[key] = value;
263
+ }
264
+ });
265
+
266
+ updatedSettings.secrets = filteredSecrets;
267
+ }
268
+
269
+ const result = {
270
+ ...prevValue,
271
+ settings: updatedSettings,
272
+ } as T;
273
+
274
+ return result;
275
+ });
276
+ },
277
+ [] // Remove value from dependencies to avoid unnecessary rerenders
278
+ );
279
+
280
+ return {
281
+ value,
282
+ updateField,
283
+ addArrayItem,
284
+ removeArrayItem,
285
+ reset,
286
+ updateSettings,
287
+ };
288
+ }