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.

3.1 KB
Raw Blame
import type { SeedOptions } from './seed-runner.ts'

export type RunOptions = {
  downloadWebBuild: boolean
  forwardArgs: string[]
  full: boolean
  headed: boolean
  seed?: SeedOptions
  seedOnly: boolean
}

const readOptionValue = (argv: string[], index: number, option: string) => {
  const value = argv[index + 1]
  if (!value || value.startsWith('--')) throw new Error(`${option} requires a value.`)

  return value
}

export const parseRunOptions = (argv: string[]): RunOptions => {
  let allowBlocked = false
  let dryRun = false
  let downloadWebBuild = false
  let full = false
  let headed = false
  let pack = 'agent-v2'
  let profile: string | undefined
  let seedOnly = false
  const forwardArgs: string[] = []

  for (let index = 0; index < argv.length; index += 1) {
    const arg = argv[index]!

    if (arg === '--') {
      forwardArgs.push(...argv.slice(index + 1))
      break
    }

    if (arg === '--full') {
      full = true
      continue
    }

    if (arg === '--download-web-build') {
      downloadWebBuild = true
      continue
    }

    if (arg === '--headed') {
      headed = true
      continue
    }

    if (arg === '--seed-only') {
      seedOnly = true
      continue
    }

    if (arg === '--allow-blocked') {
      allowBlocked = true
      continue
    }

    if (arg === '--dry-run') {
      dryRun = true
      continue
    }

    if (arg === '--pack') {
      pack = readOptionValue(argv, index, '--pack')
      index += 1
      continue
    }

    if (arg.startsWith('--pack=')) {
      pack = arg.slice('--pack='.length)
      if (!pack) throw new Error('--pack requires a value.')
      continue
    }

    if (arg === '--profile') {
      profile = readOptionValue(argv, index, '--profile')
      index += 1
      continue
    }

    if (arg.startsWith('--profile=')) {
      profile = arg.slice('--profile='.length)
      if (!profile) throw new Error('--profile requires a value.')
      continue
    }

    forwardArgs.push(arg)
  }

  const shouldSeed = seedOnly || profile !== undefined
  if (!shouldSeed && (allowBlocked || dryRun || pack !== 'agent-v2'))
    throw new Error('Seed options require --seed-only or --profile.')
  if (dryRun && !seedOnly) throw new Error('--dry-run requires --seed-only.')
  if (downloadWebBuild && !full) throw new Error('--download-web-build requires --full.')

  return {
    downloadWebBuild,
    forwardArgs,
    full,
    headed,
    seed: shouldSeed
      ? {
          allowBlocked,
          dryRun,
          pack,
          profile: profile ?? 'post-merge',
        }
      : undefined,
    seedOnly,
  }
}

const isTruthyEnv = (value: string | undefined) => value === '1' || value === 'true'

export const shouldStartManagedAgentBackend = (env: NodeJS.ProcessEnv = process.env) => {
  const shouldStart = isTruthyEnv(env.E2E_START_AGENT_BACKEND)
  const externalUrl = env.E2E_AGENT_BACKEND_URL?.trim() || env.AGENT_BACKEND_BASE_URL?.trim()

  if (shouldStart && externalUrl) {
    throw new Error(
      'E2E_START_AGENT_BACKEND cannot be enabled when E2E_AGENT_BACKEND_URL or AGENT_BACKEND_BASE_URL is set.',
    )
  }

  return shouldStart
}