mirror of
https://github.com/langgenius/dify.git
synced 2026-05-02 16:38:04 +08:00
fix: populate sequence in workflow LLM generation and render outputs_as_generation
This commit is contained in:
@ -36,6 +36,9 @@ export const useWorkflowFinished = () => {
|
||||
draft.resultTabActive = true
|
||||
draft.resultText = firstOutputVal
|
||||
}
|
||||
else if (out && typeof out === 'object' && Object.keys(out).length > 0) {
|
||||
draft.resultTabActive = true
|
||||
}
|
||||
}))
|
||||
}, [workflowStore])
|
||||
|
||||
|
||||
@ -50,7 +50,7 @@ const createRunDetail = (overrides: Partial<WorkflowRunDetailResponse> = {}): Wo
|
||||
inputs: '{}',
|
||||
inputs_truncated: false,
|
||||
status: 'succeeded',
|
||||
outputs: '{}',
|
||||
outputs: {},
|
||||
outputs_truncated: false,
|
||||
total_steps: 1,
|
||||
created_by_role: 'account',
|
||||
|
||||
@ -153,6 +153,7 @@ const RunPanel: FC<RunProps> = ({
|
||||
{!loading && currentTab === 'RESULT' && runDetail && (
|
||||
<OutputPanel
|
||||
outputs={runDetail.outputs}
|
||||
outputsAsGeneration={runDetail.outputs_as_generation}
|
||||
error={runDetail.error}
|
||||
height={height}
|
||||
/>
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { JsonValue } from '@/app/components/workflow/types'
|
||||
import type { FileResponse, WorkflowGenerationValue } from '@/types/workflow'
|
||||
import { useMemo } from 'react'
|
||||
import GenerationContent from '@/app/components/base/chat/chat/answer/generation-content'
|
||||
import LoadingAnim from '@/app/components/base/chat/chat/loading-anim'
|
||||
import { buildLLMGenerationItemsFromWorkflowOutputs } from '@/app/components/base/chat/utils'
|
||||
import { FileList } from '@/app/components/base/file-uploader'
|
||||
import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
|
||||
import { Markdown } from '@/app/components/base/markdown'
|
||||
@ -9,9 +13,13 @@ import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/
|
||||
import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
|
||||
import StatusContainer from '@/app/components/workflow/run/status-container'
|
||||
|
||||
const isDifyFile = (val: JsonValue): val is JsonValue & { dify_model_identity: '__dify__file__' } =>
|
||||
typeof val === 'object' && val !== null && !Array.isArray(val) && 'dify_model_identity' in val && val.dify_model_identity === '__dify__file__'
|
||||
|
||||
type OutputPanelProps = {
|
||||
isRunning?: boolean
|
||||
outputs?: any
|
||||
outputs?: Record<string, JsonValue>
|
||||
outputsAsGeneration?: boolean
|
||||
error?: string
|
||||
height?: number
|
||||
}
|
||||
@ -19,10 +27,24 @@ type OutputPanelProps = {
|
||||
const OutputPanel: FC<OutputPanelProps> = ({
|
||||
isRunning,
|
||||
outputs,
|
||||
outputsAsGeneration,
|
||||
error,
|
||||
height,
|
||||
}) => {
|
||||
const generationResult = useMemo(() => {
|
||||
if (!outputsAsGeneration || !outputs || typeof outputs !== 'object')
|
||||
return null
|
||||
try {
|
||||
return buildLLMGenerationItemsFromWorkflowOutputs(outputs as Record<string, WorkflowGenerationValue>)
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
}
|
||||
}, [outputs, outputsAsGeneration])
|
||||
|
||||
const isTextOutput = useMemo(() => {
|
||||
if (generationResult)
|
||||
return false
|
||||
if (!outputs || typeof outputs !== 'object')
|
||||
return false
|
||||
const keys = Object.keys(outputs)
|
||||
@ -31,28 +53,38 @@ const OutputPanel: FC<OutputPanelProps> = ({
|
||||
typeof value === 'string'
|
||||
|| (Array.isArray(value) && value.every(item => typeof item === 'string'))
|
||||
)
|
||||
}, [outputs])
|
||||
}, [outputs, generationResult])
|
||||
|
||||
const fileList = useMemo(() => {
|
||||
const fileList: any[] = []
|
||||
if (!outputs)
|
||||
return fileList
|
||||
if (Object.keys(outputs).length > 1)
|
||||
return fileList
|
||||
if (!outputs || Object.keys(outputs).length > 1)
|
||||
return []
|
||||
const matched: FileResponse[] = []
|
||||
for (const key in outputs) {
|
||||
if (Array.isArray(outputs[key])) {
|
||||
outputs[key].map((output: any) => {
|
||||
if (output?.dify_model_identity === '__dify__file__')
|
||||
fileList.push(output)
|
||||
return null
|
||||
})
|
||||
const val = outputs[key]
|
||||
if (Array.isArray(val)) {
|
||||
for (const item of val) {
|
||||
if (isDifyFile(item))
|
||||
matched.push(item as unknown as FileResponse)
|
||||
}
|
||||
}
|
||||
else if (outputs[key]?.dify_model_identity === '__dify__file__') {
|
||||
fileList.push(outputs[key])
|
||||
else if (isDifyFile(val)) {
|
||||
matched.push(val as unknown as FileResponse)
|
||||
}
|
||||
}
|
||||
return getProcessedFilesFromResponse(fileList)
|
||||
return getProcessedFilesFromResponse(matched)
|
||||
}, [outputs])
|
||||
|
||||
const hasGenerationToolOrThought = generationResult?.llmGenerationItems.some(
|
||||
item => item.type === 'tool' || item.type === 'thought',
|
||||
)
|
||||
|
||||
const textOutputContent = useMemo(() => {
|
||||
if (!isTextOutput || !outputs)
|
||||
return ''
|
||||
const firstVal = outputs[Object.keys(outputs)[0]]
|
||||
return Array.isArray(firstVal) ? firstVal.join('\n') : String(firstVal ?? '')
|
||||
}, [isTextOutput, outputs])
|
||||
|
||||
return (
|
||||
<div className="p-2">
|
||||
{isRunning && (
|
||||
@ -70,15 +102,22 @@ const OutputPanel: FC<OutputPanelProps> = ({
|
||||
<Markdown content="No Output" />
|
||||
</div>
|
||||
)}
|
||||
{generationResult && generationResult.llmGenerationItems.length > 0 && (
|
||||
hasGenerationToolOrThought
|
||||
? (
|
||||
<div className="px-2 py-1">
|
||||
<GenerationContent llmGenerationItems={generationResult.llmGenerationItems} />
|
||||
</div>
|
||||
)
|
||||
: (
|
||||
<div className="px-4 py-2">
|
||||
<Markdown content={generationResult.message} />
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
{isTextOutput && (
|
||||
<div className="px-4 py-2">
|
||||
<Markdown
|
||||
content={
|
||||
Array.isArray(outputs[Object.keys(outputs)[0]])
|
||||
? outputs[Object.keys(outputs)[0]].join('\n')
|
||||
: (outputs[Object.keys(outputs)[0]] || '')
|
||||
}
|
||||
/>
|
||||
<Markdown content={textOutputContent} />
|
||||
</div>
|
||||
)}
|
||||
{fileList.length > 0 && (
|
||||
@ -91,7 +130,7 @@ const OutputPanel: FC<OutputPanelProps> = ({
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{!isTextOutput && outputs && Object.keys(outputs).length > 0 && height! > 0 && (
|
||||
{!isTextOutput && !generationResult && outputs && Object.keys(outputs).length > 0 && height! > 0 && (
|
||||
<div className="flex flex-col gap-2">
|
||||
<CodeEditor
|
||||
showFileList
|
||||
|
||||
Reference in New Issue
Block a user