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.

4.4 KB
Raw Blame
'use client'

import type { FormValue } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { AgentComposerModel } from '@/features/agent-v2/agent-composer/form-state'
import { Button } from '@langgenius/dify-ui/button'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { useQuery } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
import { getMarketplacePluginIconUrl } from '@/features/marketplace/api'
import { consoleQuery } from '@/service/console'
import { isAgentCompatibleModel, isAgentSuggestedModel } from '../../../model-compatibility'
import { useAgentOrchestrateReadOnly } from '../read-only-context'

type AgentModelFieldProps = {
  currentModel?: AgentComposerModel
  onSelect: (model: AgentComposerModel) => void
}

export function AgentModelField({ currentModel, onSelect }: AgentModelFieldProps) {
  const { t } = useTranslation(['agentV2', 'common'])
  const readOnly = useAgentOrchestrateReadOnly()
  const modelListQuery = useQuery(
    consoleQuery.workspaces.current.models.modelTypes.byModelType.get.queryOptions({
      input: { params: { model_type: ModelTypeEnum.textGeneration } },
      select: (response) => response.data,
      enabled: !readOnly,
    }),
  )
  const providerQuery = useQuery(
    consoleQuery.workspaces.current.modelProviders.summary.get.queryOptions({
      enabled: !readOnly && !!currentModel?.provider,
    }),
  )
  const modelListUnavailable = modelListQuery.data === undefined
  const providerUnavailable = !!currentModel?.provider && providerQuery.data === undefined
  const modelStatusUnavailable = modelListUnavailable || providerUnavailable
  const hasLoadError =
    (modelListUnavailable && modelListQuery.isError) ||
    (providerUnavailable && providerQuery.isError)

  return (
    <Fieldset className="grid gap-1 py-3">
      <FieldsetLegend className="mb-0 w-fit py-1 system-sm-semibold-uppercase! text-text-secondary">
        {t(($) => $['agentDetail.configure.model.label'])}
      </FieldsetLegend>
      {readOnly ? (
        <div className="flex h-8 w-full min-w-0 items-center gap-1 rounded-lg bg-components-input-bg-disabled px-1 system-sm-regular text-components-input-text-filled">
          {currentModel?.plugin_id && (
            <img
              alt=""
              aria-hidden="true"
              className="size-5 shrink-0 rounded-md"
              height={20}
              src={getMarketplacePluginIconUrl(currentModel.plugin_id)}
              width={20}
            />
          )}
          <span className="truncate">{currentModel?.model}</span>
        </div>
      ) : (
        <ModelParameterModal
          triggerContainerClassName="min-w-0"
          isAdvancedMode
          modelId={currentModel?.model ?? ''}
          provider={currentModel?.provider ?? ''}
          completionParams={(currentModel?.model_settings ?? {}) as FormValue}
          hideDebugWithMultipleModel
          modelList={modelListQuery.data ?? []}
          readonly={modelStatusUnavailable}
          modelListLoading={modelStatusUnavailable}
          showModelMeta={false}
          modelPredicate={isAgentCompatibleModel}
          modelSuggestionPredicate={isAgentSuggestedModel}
          side="bottom"
          align="end"
          setModel={({ modelId, provider, plugin_id }) => {
            onSelect({
              provider,
              model: modelId,
              plugin_id,
            })
          }}
          onCompletionParamsChange={(modelSettings) => {
            if (!currentModel) return

            onSelect({
              ...currentModel,
              model_settings: modelSettings,
            })
          }}
        />
      )}
      {!readOnly && hasLoadError && (
        <div role="alert" className="flex items-center gap-2 text-text-warning">
          <span>{t(($) => $['api.actionFailed'], { ns: 'common' })}</span>
          <Button
            size="small"
            onClick={() => {
              if (modelListUnavailable) void modelListQuery.refetch()
              if (providerUnavailable) void providerQuery.refetch()
            }}
          >
            {t(($) => $['operation.retry'], { ns: 'common' })}
          </Button>
        </div>
      )}
    </Fieldset>
  )
}