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.

6.4 KB
Raw Blame
import type { FC } from 'react'
import type { InputValueTypes, Task, TextGenerationRunControl } from './types'
import type { PromptConfig } from '@/models/debug'
import type { SiteInfo } from '@/models/share'
import type { AppSourceType } from '@/service/share'
import type { VisionFile, VisionSettings } from '@/types/app'
import { Alert, AlertActions, AlertTitle } from '@langgenius/dify-ui/alert'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import { useTranslation } from 'react-i18next'
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
import Res from '@/app/components/share/text-generation/result'
import ResDownload from './run-batch/res-download'
import { TaskStatus } from './types'

type TextGenerationResultPanelProps = {
  allFailedTaskList: Task[]
  allSuccessTaskList: Task[]
  allTaskList: Task[]
  appId: string
  appSourceType: AppSourceType
  completionFiles: VisionFile[]
  controlRetry: number
  controlSend: number
  controlStopResponding: number
  exportRes: Record<string, string>[]
  handleCompleted: (completionRes: string, taskId?: number, isSuccess?: boolean) => void
  handleRetryAllFailedTask: () => void
  handleSaveMessage: (messageId: string) => Promise<void>
  inputs: Record<string, InputValueTypes>
  isCallBatchAPI: boolean
  isBelowMd: boolean
  isShowResultPanel: boolean
  isWorkflow: boolean
  moreLikeThisEnabled: boolean
  noPendingTask: boolean
  onHideResultPanel: () => void
  onRunControlChange: (control: TextGenerationRunControl | null) => void
  onRunStart: () => void
  onShowResultPanel: () => void
  promptConfig: PromptConfig
  resultExisted: boolean
  showTaskList: Task[]
  siteInfo: SiteInfo
  textToSpeechEnabled: boolean
  visionConfig: VisionSettings
}

const TextGenerationResultPanel: FC<TextGenerationResultPanelProps> = ({
  allFailedTaskList,
  allSuccessTaskList,
  allTaskList,
  appId,
  appSourceType,
  completionFiles,
  controlRetry,
  controlSend,
  controlStopResponding,
  exportRes,
  handleCompleted,
  handleRetryAllFailedTask,
  handleSaveMessage,
  inputs,
  isCallBatchAPI,
  isBelowMd,
  isShowResultPanel,
  isWorkflow,
  moreLikeThisEnabled,
  noPendingTask,
  onHideResultPanel,
  onRunControlChange,
  onRunStart,
  onShowResultPanel,
  promptConfig,
  resultExisted,
  showTaskList,
  siteInfo,
  textToSpeechEnabled,
  visionConfig,
}) => {
  const { t } = useTranslation(['share'])

  const renderResult = (task?: Task) => (
    <Res
      key={task?.id}
      isWorkflow={isWorkflow}
      isCallBatchAPI={isCallBatchAPI}
      isBelowMd={isBelowMd}
      appSourceType={appSourceType}
      appId={appId}
      isError={task?.status === TaskStatus.failed}
      promptConfig={promptConfig}
      moreLikeThisEnabled={moreLikeThisEnabled}
      inputs={isCallBatchAPI && task ? task.params.inputs : inputs}
      controlSend={controlSend}
      controlRetry={task?.status === TaskStatus.failed ? controlRetry : 0}
      controlStopResponding={controlStopResponding}
      onShowRes={onShowResultPanel}
      handleSaveMessage={handleSaveMessage}
      taskId={task?.id}
      onCompleted={handleCompleted}
      visionConfig={visionConfig}
      completionFiles={completionFiles}
      isShowTextToSpeech={textToSpeechEnabled}
      siteInfo={siteInfo}
      onRunStart={onRunStart}
      onRunControlChange={!isCallBatchAPI ? onRunControlChange : undefined}
      hideInlineStopButton={!isCallBatchAPI}
    />
  )

  return (
    <div
      className={cn(
        isBelowMd
          ? isShowResultPanel
            ? 'fixed inset-0 z-50 bg-background-overlay backdrop-blur-xs'
            : resultExisted
              ? 'relative h-16 shrink-0 overflow-hidden bg-background-default-burn pt-2.5'
              : ''
          : 'h-full w-0 grow',
      )}
    >
      {isBelowMd && (
        <div
          className={cn(
            isShowResultPanel
              ? 'flex items-center justify-center p-2 pt-6'
              : 'absolute inset-s-0 top-0 z-10 flex w-full items-center justify-center px-2 pt-0.75 pb-14.25',
          )}
          onClick={() => {
            if (isShowResultPanel) onHideResultPanel()
            else onShowResultPanel()
          }}
        >
          <div className="h-1 w-8 cursor-grab rounded-sm bg-divider-solid" />
        </div>
      )}
      <div
        className={cn(
          'relative flex h-full flex-col',
          isBelowMd && 'h-[calc(100vh-36px)] rounded-t-2xl shadow-lg backdrop-blur-xs',
          isBelowMd
            ? isShowResultPanel
              ? 'bg-background-default-burn'
              : 'border-t-[0.5px] border-divider-regular bg-components-panel-bg'
            : 'bg-chatbot-bg',
        )}
      >
        {isCallBatchAPI && (
          <div
            className={cn(
              'flex shrink-0 items-center justify-between px-14 pt-9 pb-2',
              isBelowMd && 'px-4 pt-3 pb-1',
            )}
          >
            <div className="system-md-semibold-uppercase text-text-primary">
              {t(($) => $['generation.executions'], { ns: 'share', num: allTaskList.length })}
            </div>
            {allSuccessTaskList.length > 0 && (
              <ResDownload compact={isBelowMd} values={exportRes} />
            )}
          </div>
        )}
        <div
          className={cn(
            'flex h-0 grow flex-col overflow-y-auto',
            isBelowMd ? 'p-0 pb-2' : 'px-14 py-8',
            !isBelowMd && isCallBatchAPI && 'pt-0',
          )}
        >
          {isCallBatchAPI ? showTaskList.map((task) => renderResult(task)) : renderResult()}
          {!noPendingTask && (
            <div className="mt-4">
              <LoadingPlaceholder />
            </div>
          )}
        </div>
        {isCallBatchAPI && allFailedTaskList.length > 0 && (
          <div className="absolute bottom-6 left-1/2 z-10 -translate-x-1/2">
            <Alert type="error" className="w-max max-w-105">
              <AlertTitle>
                {t(($) => $['generation.batchFailed.info'], {
                  ns: 'share',
                  num: allFailedTaskList.length,
                })}
              </AlertTitle>
              <AlertActions>
                <Button size="small" onClick={handleRetryAllFailedTask}>
                  {t(($) => $['generation.batchFailed.retry'], { ns: 'share' })}
                </Button>
              </AlertActions>
            </Alert>
          </div>
        )}
      </div>
    </div>
  )
}

export default TextGenerationResultPanel