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.2 KB
Raw Blame
'use client'
import type { WorkflowGeneratorMode } from './types'
import { cn } from '@langgenius/dify-ui/cn'
import { useSessionStorageState } from 'ahooks'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { fetchWorkflowInstructionSuggestions } from '@/service/workflow-generator'

type Props = Readonly<{
  mode: WorkflowGeneratorMode
  onSelect: (prompt: string) => void
}>

const SUGGESTION_COUNT = 4
// Placeholder pill widths (px) while suggestions stream in — varied so the
// skeleton reads like a row of chips rather than a progress bar.
const SKELETONS = [
  { id: 'short', width: 88 },
  { id: 'long', width: 132 },
  { id: 'medium', width: 104 },
  { id: 'wide', width: 120 },
] as const

/**
 * "Ideas for you" chips under the instruction textarea.
 *
 * Primary content is AI-generated, workspace-grounded suggestions fetched when
 * the modal opens (cached per session per mode); a ↻ refresh pulls a fresh set.
 * When the backend can't generate (no default model, quota, parse failure) it
 * returns an empty list and we silently fall back to a curated static list, so
 * the row is never empty. Create-only — the parent hides it in refine mode.
 */
const ExamplePrompts = ({ mode, onSelect }: Props) => {
  const { t, i18n } = useTranslation(['workflow', 'workflowGenerator'])

  // Curated fallback, shown until AI suggestions arrive and whenever generation
  // is unavailable. The spread per mode covers a range of workflow shapes.
  const staticPrompts = useMemo(() => {
    if (mode === 'workflow') {
      return [
        t(($) => $['workflowGenerator.examples.workflow.summarize'], { ns: 'workflowGenerator' }),
        t(($) => $['workflowGenerator.examples.workflow.translate'], { ns: 'workflowGenerator' }),
        t(($) => $['workflowGenerator.examples.workflow.rag'], { ns: 'workflowGenerator' }),
        t(($) => $['workflowGenerator.examples.workflow.classify'], { ns: 'workflowGenerator' }),
      ]
    }
    return [
      t(($) => $['workflowGenerator.examples.chatflow.support'], { ns: 'workflowGenerator' }),
      t(($) => $['workflowGenerator.examples.chatflow.tutor'], { ns: 'workflowGenerator' }),
      t(($) => $['workflowGenerator.examples.chatflow.triage'], { ns: 'workflowGenerator' }),
    ]
  }, [mode, t])

  // Session-cached AI suggestions, keyed per mode so Workflow / Chatflow don't
  // clobber each other and a reopen within the same session skips the refetch.
  const [cached, setCached] = useSessionStorageState<string[]>(`workflow-gen-suggestions-${mode}`, {
    defaultValue: [],
  })
  const [isLoading, setIsLoading] = useState(false)
  const abortRef = useRef<AbortController | null>(null)
  const requestIdRef = useRef(0)

  const fetchSuggestions = useCallback(async () => {
    const requestId = ++requestIdRef.current
    abortRef.current?.abort()
    setIsLoading(true)
    try {
      const res = await fetchWorkflowInstructionSuggestions(
        { mode, language: i18n.language, count: SUGGESTION_COUNT },
        {
          getAbortController: (c) => {
            if (requestId === requestIdRef.current) abortRef.current = c
            else c.abort()
          },
        },
      )
      const next = (res?.suggestions ?? []).map((s) => s.trim()).filter(Boolean)
      // Keep the previous set on an empty refresh so the row never flashes empty.
      if (requestId === requestIdRef.current && next.length) setCached(next)
    } catch {
      // Silent: the static fallback keeps the row populated.
    } finally {
      if (requestId === requestIdRef.current) {
        setIsLoading(false)
        abortRef.current = null
      }
    }
  }, [mode, i18n.language, setCached])

  // Auto-fetch once on open when nothing is cached for this mode yet. ``mode``
  // is fixed per open (the modal remounts each time), so a mount-only effect is
  // correct here.
  useEffect(() => {
    if (!cached || cached.length === 0) void fetchSuggestions()
    return () => {
      requestIdRef.current += 1
      abortRef.current?.abort()
      abortRef.current = null
    }
    // oxlint-disable-next-line react/exhaustive-deps
  }, [mode])

  const aiPrompts = cached ?? []
  const prompts = aiPrompts.length ? aiPrompts : staticPrompts

  return (
    <div className="mt-3">
      <div className="mb-1.5 flex items-center gap-1">
        <span className="system-xs-medium-uppercase text-text-tertiary">
          {t(($) => $['workflowGenerator.examples.label'], { ns: 'workflowGenerator' })}
        </span>
        <button
          type="button"
          aria-label={t(($) => $['workflowGenerator.examples.refresh'], {
            ns: 'workflowGenerator',
          })}
          title={t(($) => $['workflowGenerator.examples.refresh'], { ns: 'workflowGenerator' })}
          className="flex size-4 cursor-pointer items-center justify-center rounded text-text-quaternary outline-hidden hover:text-text-tertiary focus-visible:ring-2 focus-visible:ring-state-accent-solid disabled:cursor-not-allowed disabled:opacity-50"
          onClick={() => {
            void fetchSuggestions()
          }}
          disabled={isLoading}
        >
          <span
            aria-hidden="true"
            className={cn('i-ri-refresh-line size-3.5', isLoading && 'animate-spin')}
          />
        </button>
      </div>
      <div className="flex flex-wrap gap-1.5">
        {isLoading
          ? SKELETONS.map(({ id, width }) => (
              <div
                key={id}
                className="h-6.5 animate-pulse rounded-md bg-components-button-secondary-bg"
                style={{ width }}
              />
            ))
          : prompts.map((prompt) => (
              <button
                key={prompt}
                type="button"
                className="cursor-pointer rounded-md border-[0.5px] border-divider-regular bg-components-button-secondary-bg px-2 py-1 system-xs-regular text-text-secondary outline-hidden hover:bg-components-button-secondary-bg-hover focus-visible:ring-2 focus-visible:ring-state-accent-solid"
                onClick={() => onSelect(prompt)}
              >
                {prompt}
              </button>
            ))}
      </div>
    </div>
  )
}

export default memo(ExamplePrompts)