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): merge parallel tool results into their own agent thoughts (#43843)

Metehan Biçeraa0924a4a77e
diff --git a/web/app/components/base/chat/chat/__tests__/hooks.spec.tsx b/web/app/components/base/chat/chat/__tests__/hooks.spec.tsx
index fc37e657b8e..164d91fbbd5 100644
--- a/web/app/components/base/chat/chat/__tests__/hooks.spec.tsx
+++ b/web/app/components/base/chat/chat/__tests__/hooks.spec.tsx
@@ -538,6 +538,83 @@ describe('useChat', () => {
expect(lastResponse!.workflowProcess?.tracing).toHaveLength(3) // node, iteration, loop
})
+ it('should merge each parallel tool result into the thought that called the tool', async () => {
+ let callbacks: HookCallbacks
+ vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
+ callbacks = options as HookCallbacks
+ })
+
+ const { renderHookWithConsoleQuery } = await import('@/test/console/query-data')
+ const { result } = renderHookWithConsoleQuery(() => useChat())
+
+ act(() => {
+ result.current.handleSend('test-url', { query: 'parallel tools' }, {})
+ })
+
+ act(() => {
+ callbacks.onThought({ id: 'th-a', message_id: 'm-p', tool: 'tool_a', observation: '' })
+ callbacks.onThought({ id: 'th-b', message_id: 'm-p', tool: 'tool_b', observation: '' })
+ callbacks.onThought({
+ id: 'th-a',
+ message_id: 'm-p',
+ tool: 'tool_a',
+ observation: 'result a',
+ })
+ callbacks.onThought({
+ id: 'th-b',
+ message_id: 'm-p',
+ tool: 'tool_b',
+ observation: 'result b',
+ })
+ })
+
+ expect(
+ result.current.chatList[1]!.agent_thoughts!.map((thought) => [
+ thought.id,
+ thought.observation,
+ ]),
+ ).toEqual([
+ ['th-a', 'result a'],
+ ['th-b', 'result b'],
+ ])
+ })
+
+ it('should merge each parallel tool result into its thought after a resumed stream', async () => {
+ let initialCallbacks: HookCallbacks
+ let resumedCallbacks: HookCallbacks
+ vi.mocked(ssePost).mockImplementation(async (_url, _params, options) => {
+ initialCallbacks = options as HookCallbacks
+ })
+ vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
+ resumedCallbacks = options as HookCallbacks
+ })
+
+ const { renderHookWithConsoleQuery } = await import('@/test/console/query-data')
+ const { result } = renderHookWithConsoleQuery(() => useChat())
+
+ act(() => {
+ result.current.handleSend('test-url', { query: 'parallel tools after pause' }, {})
+ })
+ act(() => {
+ initialCallbacks.onWorkflowStarted({ workflow_run_id: 'wr-p', task_id: 't-p' })
+ initialCallbacks.onWorkflowPaused({ data: { workflow_run_id: 'wr-p' } })
+ })
+ act(() => {
+ resumedCallbacks.onThought({ id: 'th-a', tool: 'tool_a', observation: '' })
+ resumedCallbacks.onThought({ id: 'th-b', tool: 'tool_b', observation: '' })
+ resumedCallbacks.onThought({ id: 'th-a', tool: 'tool_a', observation: 'result a' })
+ resumedCallbacks.onThought({ id: 'th-b', tool: 'tool_b', observation: 'result b' })
+ })
+
+ const response = result.current.chatList[result.current.chatList.length - 1]
+ expect(response!.agent_thoughts!.map((thought) => [thought.id, thought.observation])).toEqual(
+ [
+ ['th-a', 'result a'],
+ ['th-b', 'result b'],
+ ],
+ )
+ })
+
it('should handle human input forms, pauses, TTS, and message ends', async () => {
let callbacks: HookCallbacks
@@ -1286,6 +1363,65 @@ describe('useChat', () => {
})
describe('handleResume', () => {
+ it('merges parallel tool results into their thoughts when resuming directly', async () => {
+ let callbacks: HookCallbacks
+ vi.mocked(sseGet).mockImplementation(async (_url, _params, options) => {
+ callbacks = options as HookCallbacks
+ })
+
+ const prevChatTree = [
+ {
+ id: 'q-1',
+ content: 'query',
+ isAnswer: false,
+ children: [
+ {
+ id: 'm-1',
+ content: '',
+ isAnswer: true,
+ workflow_run_id: 'wr-1',
+ agent_thoughts: [],
+ siblingIndex: 0,
+ },
+ ],
+ },
+ ]
+ const { renderHookWithConsoleQuery } = await import('@/test/console/query-data')
+ const { result } = renderHookWithConsoleQuery(() =>
+ useChat(undefined, undefined, prevChatTree as ChatItemInTree[]),
+ )
+
+ act(() => {
+ result.current.handleResume('m-1', 'wr-1', { isPublicAPI: true })
+ })
+
+ const toolThought = (id: string, tool: string, position: number, observation: string) => ({
+ id,
+ message_id: 'm-1',
+ conversation_id: 'c-1',
+ position,
+ thought: '',
+ tool,
+ tool_input: '{}',
+ observation,
+ message_files: [],
+ })
+
+ act(() => {
+ callbacks.onThought(toolThought('th-a', 'tool_a', 1, ''))
+ callbacks.onThought(toolThought('th-b', 'tool_b', 2, ''))
+ callbacks.onThought(toolThought('th-a', 'tool_a', 1, 'result a'))
+ callbacks.onThought(toolThought('th-b', 'tool_b', 2, 'result b'))
+ })
+
+ const thoughts = result.current.chatList[1]!.agent_thoughts!
+ expect(thoughts).toHaveLength(2)
+ expect(thoughts.map(({ id, observation }) => [id, observation])).toEqual([
+ ['th-a', 'result a'],
+ ['th-b', 'result b'],
+ ])
+ })
+
it('should call sseGet to resume a node and handle complex tracing', async () => {
let callbacks: HookCallbacks
const onNotifyError = vi.fn()
diff --git a/web/app/components/base/chat/chat/hooks.ts b/web/app/components/base/chat/chat/hooks.ts
index 7e1104db594..e06d6f5b806 100644
--- a/web/app/components/base/chat/chat/hooks.ts
+++ b/web/app/components/base/chat/chat/hooks.ts
@@ -86,6 +86,18 @@ function mergeStreamingThought(currentThought: ThoughtItem, nextThought: Thought
}
}
+// A tool result streams in under the id of the thought that made the call. With parallel tool
+// calls that thought is not always the last one, so it is looked up by id from the end.
+function upsertStreamingThought(thoughts: ThoughtItem[], thought: ThoughtItem) {
+ for (let index = thoughts.length - 1; index >= 0; index--) {
+ if (thoughts[index]!.id === thought.id) {
+ thoughts[index] = mergeStreamingThought(thoughts[index]!, thought)
+ return
+ }
+ }
+ thoughts.push(thought)
+}
+
function appendAgentResponseMessagePart(responseItem: ChatItemInTree, message: string) {
if (!responseItem.agent_response_parts) responseItem.agent_response_parts = []
@@ -741,18 +753,7 @@ export const useChat = (
if (thought.conversation_id) responseItem.conversationId = thought.conversation_id
if (!responseItem.agent_thoughts) responseItem.agent_thoughts = []
-
- if (responseItem.agent_thoughts.length === 0) {
- responseItem.agent_thoughts.push(thought)
- } else {
- const lastThought = responseItem.agent_thoughts.at(-1)
- if (lastThought?.id === thought.id) {
- responseItem.agent_thoughts[responseItem.agent_thoughts.length - 1] =
- mergeStreamingThought(lastThought, thought)
- } else {
- responseItem.agent_thoughts.push(thought)
- }
- }
+ upsertStreamingThought(responseItem.agent_thoughts, thought)
if (options.isNewAgent) {
const currentThought =
responseItem.agent_thoughts.find((item) => item.id === thought.id) ?? thought
@@ -1426,18 +1427,7 @@ export const useChat = (
if (thought.message_id && !hasSetResponseId) response.id = thought.message_id
if (thought.conversation_id) response.conversationId = thought.conversation_id
- if (response.agent_thoughts.length === 0) {
- response.agent_thoughts.push(thought)
- } else {
- const lastThought = response.agent_thoughts.at(-1)
- // thought changed but still the same thought, so update.
- if (lastThought.id === thought.id) {
- responseItem.agent_thoughts![response.agent_thoughts.length - 1] =
- mergeStreamingThought(lastThought, thought)
- } else {
- responseItem.agent_thoughts!.push(thought)
- }
- }
+ upsertStreamingThought(response.agent_thoughts, thought)
if (options.isNewAgent) {
const currentThought =
responseItem.agent_thoughts?.find((item) => item.id === thought.id) ?? thought