From ca9738e43ed0f7c3903d76524ab0d73414ea9a5b Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 10 Nov 2026 03:14:15 +0000 Subject: [PATCH] fix: preserve agent publication status when renaming (#45949) --- web/service/console/client.spec.ts | 67 +++++++++++++++++++++++++++ web/service/console/query-policies.ts | 4 +- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/web/service/console/client.spec.ts b/web/service/console/client.spec.ts index a056d523ed5..892ec173897 100644 --- a/web/service/console/client.spec.ts +++ b/web/service/console/client.spec.ts @@ -1,3 +1,4 @@ +import type { AgentAppPagination } from '@dify/contracts/api/console/agent/types.gen' import type { ApiBasedExtensionResponse } from '@dify/contracts/api/console/api-based-extension/types.gen' import type { AppSiteResponse } from '@dify/contracts/api/console/apps/types.gen' import type { TagResponse as Tag } from '@dify/contracts/api/console/tags/types.gen' @@ -1475,6 +1476,72 @@ describe('consoleQuery agent mutation defaults', () => { }) }) + it.each([true, false])( + 'preserves roster publication and reference data when renaming an agent with published state %s', + async (isPublished) => { + const consoleQuery = await loadConsoleQuery() + const queryClient = createQueryClient() + const agent = { + ...createAgent(), + active_config_is_published: isPublished, + hidden_app_backed: false, + is_starred: false, + published_reference_count: 1, + published_references: [{ app_id: 'workflow-1', app_name: 'Research Workflow' }], + } + const otherAgent = { + ...createAgent({ id: 'agent-2', name: 'Other Agent' }), + active_config_is_published: false, + hidden_app_backed: false, + is_starred: false, + published_reference_count: 0, + } + const rosterPage = { + data: [agent, otherAgent], + has_more: false, + limit: 30, + page: 1, + publication_counts: { drafts: isPublished ? 1 : 2, published: isPublished ? 1 : 0 }, + total: 2, + } satisfies AgentAppPagination + const listQueryKey = consoleQuery.agent.get.queryKey({ + input: { query: { page: 1, limit: 30 } }, + }) + const infiniteQueryKey = consoleQuery.agent.get.infiniteOptions({ + input: (pageParam) => ({ query: { page: Number(pageParam), limit: 30 } }), + initialPageParam: 1, + getNextPageParam: (lastPage) => (lastPage.has_more ? lastPage.page + 1 : undefined), + }).queryKey + queryClient.setQueryData(listQueryKey, rosterPage) + queryClient.setQueryData(infiniteQueryKey, { pages: [rosterPage], pageParams: [1] }) + const updatedAgent = createAgent({ name: 'Updated Agent' }) + + await consoleQuery.agent.byAgentId.put.mutationOptions().onSuccess!( + updatedAgent, + { params: { agent_id: agent.id }, body: { name: updatedAgent.name } }, + undefined, + createMutationContext(queryClient), + ) + + const expectedPage = { + ...rosterPage, + data: [{ ...agent, name: updatedAgent.name }, otherAgent], + } + expect(queryClient.getQueryData(listQueryKey)).toEqual(expectedPage) + expect(queryClient.getQueryData(infiniteQueryKey)).toEqual({ + pages: [expectedPage], + pageParams: [1], + }) + expect( + queryClient.getQueryData( + consoleQuery.agent.byAgentId.get.queryKey({ + input: { params: { agent_id: agent.id } }, + }), + ), + ).toEqual(updatedAgent) + }, + ) + it('should invalidate invite option lists after updating an agent', async () => { const consoleQuery = await loadConsoleQuery() const queryClient = createQueryClient() diff --git a/web/service/console/query-policies.ts b/web/service/console/query-policies.ts index 460f2e132af..71db5921111 100644 --- a/web/service/console/query-policies.ts +++ b/web/service/console/query-policies.ts @@ -2380,7 +2380,7 @@ export function createConsoleQuery(consoleClient: ConsoleClient) { return { ...oldList, data: oldList.data.map((item) => - item.id === updatedAgent.id ? updatedAgent : item, + item.id === updatedAgent.id ? { ...item, ...updatedAgent } : item, ), } }, @@ -2402,7 +2402,7 @@ export function createConsoleQuery(consoleClient: ConsoleClient) { pages: oldList.pages.map((page) => ({ ...page, data: page.data.map((item) => - item.id === updatedAgent.id ? updatedAgent : item, + item.id === updatedAgent.id ? { ...item, ...updatedAgent } : item, ), })), }