Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): refresh provider credentials after editing (#42679)

Xx.Zhao139ce494306b
diff --git a/web/service/__tests__/use-models.spec.tsx b/web/service/__tests__/use-models.spec.tsx
new file mode 100644
index 00000000000..29638e20bc3
--- /dev/null
+++ b/web/service/__tests__/use-models.spec.tsx
@@ -0,0 +1,76 @@
+import type { ReactNode } from 'react'
+import type { ProviderCredential } from '@/app/components/header/account-setting/model-provider-page/declarations'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { renderHook, waitFor } from '@testing-library/react'
+import { useGetProviderCredential } from '../use-models'
+
+const mockGet = vi.hoisted(() => vi.fn())
+
+vi.mock('../base', () => ({
+ del: vi.fn(),
+ get: mockGet,
+ post: vi.fn(),
+ put: vi.fn(),
+}))
+
+const createWrapper = () => {
+ const queryClient = new QueryClient({
+ defaultOptions: {
+ queries: {
+ retry: false,
+ staleTime: 1000 * 60 * 5,
+ },
+ },
+ })
+
+ return ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ )
+}
+
+describe('useGetProviderCredential', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ it('should refetch credential details after the editor is reopened', async () => {
+ const previousCredential: ProviderCredential = {
+ credential_id: 'credential-id',
+ name: 'Tongyi',
+ credentials: {
+ api_key: '[__HIDDEN__]',
+ endpoint: 'https://old.example.com',
+ enable_request_metadata: false,
+ },
+ }
+ const updatedCredential: ProviderCredential = {
+ ...previousCredential,
+ credentials: {
+ api_key: '[__HIDDEN__]',
+ endpoint: 'https://new.example.com',
+ enable_request_metadata: true,
+ },
+ }
+ mockGet.mockResolvedValueOnce(previousCredential).mockResolvedValueOnce(updatedCredential)
+ const wrapper = createWrapper()
+
+ const firstEditor = renderHook(
+ () => useGetProviderCredential(true, 'langgenius/tongyi/tongyi', 'credential-id'),
+ { wrapper },
+ )
+ await waitFor(() => {
+ expect(firstEditor.result.current.data).toEqual(previousCredential)
+ })
+ firstEditor.unmount()
+
+ const reopenedEditor = renderHook(
+ () => useGetProviderCredential(true, 'langgenius/tongyi/tongyi', 'credential-id'),
+ { wrapper },
+ )
+
+ await waitFor(() => {
+ expect(mockGet).toHaveBeenCalledTimes(2)
+ expect(reopenedEditor.result.current.data).toEqual(updatedCredential)
+ })
+ })
+})
diff --git a/web/service/use-models.ts b/web/service/use-models.ts
index f4864e0b167..97dd930d4de 100644
--- a/web/service/use-models.ts
+++ b/web/service/use-models.ts
@@ -35,6 +35,8 @@ export const useGetProviderCredential = (
get<ProviderCredential>(
`/workspaces/current/model-providers/${provider}/credentials${credentialId ? `?credential_id=${credentialId}` : ''}`,
),
+ staleTime: 0,
+ gcTime: 0,
})
}