Skip to content

Commit bf1b040

Browse files
committed
feat: re-read agent definition from disk on editing
- Added refreshAssistants method to useAssistant hook to fetch fresh data from backend - Modified assistant editing flow to refresh assistants before opening edit dialog - Ensures latest assistant definitions are loaded when editing - Addresses issue where edits might show stale data from previous cache
1 parent 0350e76 commit bf1b040

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

web-app/src/hooks/useAssistant.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface AssistantState {
1717
) => void
1818
setDefaultAssistant: (id: string) => void
1919
setAssistants: (assistants: Assistant[] | null) => void
20+
refreshAssistants: () => Promise<void>
2021
}
2122

2223
const setLastUsedAssistantId = (assistantId: string) => {
@@ -215,4 +216,19 @@ export const useAssistant = create<AssistantState>((set, get) => ({
215216
set({ loading: false })
216217
}
217218
},
219+
refreshAssistants: async () => {
220+
try {
221+
const assistants = await getServiceHub().assistants().getAssistants()
222+
if (assistants) {
223+
// Update the state with fresh assistants
224+
set({
225+
assistants,
226+
loading: false
227+
})
228+
}
229+
} catch (error) {
230+
console.error('Failed to refresh assistants:', error)
231+
set({ loading: false })
232+
}
233+
},
218234
}))

web-app/src/routes/settings/assistant.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createFileRoute } from '@tanstack/react-router'
22
import { route } from '@/constants/routes'
3-
import { useState } from 'react'
3+
import { useState, useEffect } from 'react'
44

55
import { useAssistant } from '@/hooks/useAssistant'
66

@@ -30,18 +30,20 @@ export const Route = createFileRoute(route.settings.assistant as any)({
3030

3131
function AssistantContent() {
3232
const { t } = useTranslation()
33-
const {
33+
const {
3434
assistants,
3535
addAssistant,
3636
updateAssistant,
3737
deleteAssistant,
3838
defaultAssistantId,
39-
setDefaultAssistant
39+
setDefaultAssistant,
40+
refreshAssistants
4041
} = useAssistant()
4142
const [open, setOpen] = useState(false)
4243
const [editingKey, setEditingKey] = useState<string | null>(null)
4344
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false)
4445
const [deletingId, setDeletingId] = useState<string | null>(null)
46+
const [editingAssistant, setEditingAssistant] = useState<Assistant | null>(null)
4547

4648
const handleDelete = (id: string) => {
4749
setDeletingId(id)
@@ -66,9 +68,27 @@ function AssistantContent() {
6668
setEditingKey(null)
6769
}
6870

71+
const handleEditClick = (assistantId: string) => {
72+
// Before opening the edit dialog, refresh the assistants to get the latest data
73+
refreshAssistants().then(() => {
74+
setEditingKey(assistantId)
75+
setOpen(true)
76+
})
77+
}
78+
6979
const sortedAssistants = assistants.slice().sort((a, b) => a.created_at - b.created_at)
7080
const defaultAssistant = sortedAssistants.find((a) => a.id === defaultAssistantId)
7181

82+
// When editingKey changes, we need to get the latest assistant data
83+
useEffect(() => {
84+
if (editingKey) {
85+
const assistant = assistants.find(a => a.id === editingKey)
86+
setEditingAssistant(assistant || null)
87+
} else {
88+
setEditingAssistant(null)
89+
}
90+
}, [editingKey, assistants])
91+
7292
return (
7393
<div className="flex flex-col h-svh w-full">
7494
<HeaderPage>
@@ -174,10 +194,7 @@ function AssistantContent() {
174194
variant="ghost"
175195
size="icon-xs"
176196
title={t('assistants:editAssistant')}
177-
onClick={() => {
178-
setEditingKey(assistant.id)
179-
setOpen(true)
180-
}}
197+
onClick={() => handleEditClick(assistant.id)}
181198
>
182199
<IconPencil className="text-muted-foreground size-4" />
183200
</Button>
@@ -198,11 +215,7 @@ function AssistantContent() {
198215
open={open}
199216
onOpenChange={setOpen}
200217
editingKey={editingKey}
201-
initialData={
202-
editingKey
203-
? assistants.find((a) => a.id === editingKey)
204-
: undefined
205-
}
218+
initialData={editingAssistant || undefined}
206219
onSave={handleSave}
207220
/>
208221
<DeleteAssistantDialog

0 commit comments

Comments
 (0)