chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,276 @@
|
||||
import type { Buffer } from 'node:buffer'
|
||||
import type { ChildProcess } from 'node:child_process'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { access, copyFile, readFile, writeFile } from 'node:fs/promises'
|
||||
import net from 'node:net'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url'
|
||||
import { sleep } from '../support/process.ts'
|
||||
|
||||
type RunCommandOptions = {
|
||||
command: string
|
||||
args: string[]
|
||||
cwd: string
|
||||
env?: NodeJS.ProcessEnv
|
||||
stdio?: 'inherit' | 'pipe'
|
||||
}
|
||||
|
||||
type RunCommandResult = {
|
||||
exitCode: number
|
||||
stdout: string
|
||||
stderr: string
|
||||
}
|
||||
|
||||
type ForegroundProcessOptions = {
|
||||
command: string
|
||||
args: string[]
|
||||
cwd: string
|
||||
env?: NodeJS.ProcessEnv
|
||||
}
|
||||
|
||||
export const rootDir = fileURLToPath(new URL('../..', import.meta.url))
|
||||
export const e2eDir = path.join(rootDir, 'e2e')
|
||||
export const apiDir = path.join(rootDir, 'api')
|
||||
export const difyAgentDir = path.join(rootDir, 'dify-agent')
|
||||
export const difyAgentRuntimeDir = path.join(rootDir, 'dify-agent-runtime')
|
||||
export const dockerDir = path.join(rootDir, 'docker')
|
||||
export const webDir = path.join(rootDir, 'web')
|
||||
|
||||
export const middlewareComposeFile = path.join(dockerDir, 'docker-compose.middleware.yaml')
|
||||
export const middlewareEnvFile = path.join(dockerDir, 'middleware.env')
|
||||
export const middlewareEnvExampleFile = path.join(dockerDir, 'envs', 'middleware.env.example')
|
||||
export const webEnvLocalFile = path.join(webDir, '.env.local')
|
||||
export const webEnvExampleFile = path.join(webDir, '.env.example')
|
||||
export const apiEnvExampleFile = path.join(
|
||||
apiDir,
|
||||
'tests',
|
||||
'e2e_tests',
|
||||
'middleware',
|
||||
'.env.example',
|
||||
)
|
||||
export const e2eWebEnvOverrides = {
|
||||
NEXT_PUBLIC_API_PREFIX: 'http://127.0.0.1:5001/console/api',
|
||||
NEXT_PUBLIC_ENABLE_AGENT_V2: 'true',
|
||||
NEXT_PUBLIC_PUBLIC_API_PREFIX: 'http://127.0.0.1:5001/api',
|
||||
} satisfies Record<string, string>
|
||||
|
||||
const formatCommand = (command: string, args: string[]) => [command, ...args].join(' ')
|
||||
|
||||
export const isMainModule = (metaUrl: string) => {
|
||||
const entrypoint = process.argv[1]
|
||||
if (!entrypoint) return false
|
||||
|
||||
return pathToFileURL(entrypoint).href === metaUrl
|
||||
}
|
||||
|
||||
export const runCommand = async ({
|
||||
command,
|
||||
args,
|
||||
cwd,
|
||||
env,
|
||||
stdio = 'inherit',
|
||||
}: RunCommandOptions): Promise<RunCommandResult> => {
|
||||
const childProcess = spawn(command, args, {
|
||||
cwd,
|
||||
env: {
|
||||
...process.env,
|
||||
...env,
|
||||
},
|
||||
stdio: stdio === 'inherit' ? 'inherit' : 'pipe',
|
||||
})
|
||||
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
|
||||
if (stdio === 'pipe') {
|
||||
childProcess.stdout?.on('data', (chunk: Buffer | string) => {
|
||||
stdout += chunk.toString()
|
||||
})
|
||||
childProcess.stderr?.on('data', (chunk: Buffer | string) => {
|
||||
stderr += chunk.toString()
|
||||
})
|
||||
}
|
||||
|
||||
return await new Promise<RunCommandResult>((resolve, reject) => {
|
||||
childProcess.once('error', reject)
|
||||
childProcess.once('exit', (code) => {
|
||||
resolve({
|
||||
exitCode: code ?? 1,
|
||||
stdout,
|
||||
stderr,
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const runCommandOrThrow = async (options: RunCommandOptions) => {
|
||||
const result = await runCommand(options)
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
throw new Error(
|
||||
`Command failed (${result.exitCode}): ${formatCommand(options.command, options.args)}`,
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const getTcpPortListenerDescription = async (port: number) => {
|
||||
if (process.platform === 'win32') return ''
|
||||
|
||||
const result = await runCommand({
|
||||
command: 'lsof',
|
||||
args: ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN'],
|
||||
cwd: rootDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
if (result.exitCode !== 0) return ''
|
||||
|
||||
return result.stdout.trim()
|
||||
}
|
||||
|
||||
const forwardSignalsToChild = (childProcess: ChildProcess) => {
|
||||
const handleSignal = (signal: NodeJS.Signals) => {
|
||||
if (childProcess.exitCode === null) childProcess.kill(signal)
|
||||
}
|
||||
|
||||
const onSigint = () => handleSignal('SIGINT')
|
||||
const onSigterm = () => handleSignal('SIGTERM')
|
||||
|
||||
process.on('SIGINT', onSigint)
|
||||
process.on('SIGTERM', onSigterm)
|
||||
|
||||
return () => {
|
||||
process.off('SIGINT', onSigint)
|
||||
process.off('SIGTERM', onSigterm)
|
||||
}
|
||||
}
|
||||
|
||||
export const runForegroundProcess = async ({
|
||||
command,
|
||||
args,
|
||||
cwd,
|
||||
env,
|
||||
}: ForegroundProcessOptions) => {
|
||||
const childProcess = spawn(command, args, {
|
||||
cwd,
|
||||
env: {
|
||||
...process.env,
|
||||
...env,
|
||||
},
|
||||
stdio: 'inherit',
|
||||
})
|
||||
|
||||
const cleanupSignals = forwardSignalsToChild(childProcess)
|
||||
const exitCode = await new Promise<number>((resolve, reject) => {
|
||||
childProcess.once('error', reject)
|
||||
childProcess.once('exit', (code) => {
|
||||
resolve(code ?? 1)
|
||||
})
|
||||
})
|
||||
|
||||
cleanupSignals()
|
||||
process.exit(exitCode)
|
||||
}
|
||||
|
||||
export const ensureFileExists = async (filePath: string, exampleFilePath: string) => {
|
||||
try {
|
||||
await access(filePath)
|
||||
} catch {
|
||||
await copyFile(exampleFilePath, filePath)
|
||||
}
|
||||
}
|
||||
|
||||
export const ensureLineInFile = async (filePath: string, line: string) => {
|
||||
const fileContent = await readFile(filePath, 'utf8')
|
||||
const lines = fileContent.split(/\r?\n/)
|
||||
const assignmentPrefix = line.includes('=') ? `${line.slice(0, line.indexOf('='))}=` : null
|
||||
|
||||
if (lines.includes(line)) return
|
||||
|
||||
if (assignmentPrefix && lines.some((existingLine) => existingLine.startsWith(assignmentPrefix)))
|
||||
return
|
||||
|
||||
const normalizedContent = fileContent.endsWith('\n') ? fileContent : `${fileContent}\n`
|
||||
await writeFile(filePath, `${normalizedContent}${line}\n`, 'utf8')
|
||||
}
|
||||
|
||||
export const getWebEnvLocalHash = async () => {
|
||||
const fileContent = await readFile(webEnvLocalFile, 'utf8').catch(() => '')
|
||||
return createHash('sha256')
|
||||
.update(
|
||||
JSON.stringify({
|
||||
envLocal: fileContent,
|
||||
overrides: e2eWebEnvOverrides,
|
||||
}),
|
||||
)
|
||||
.digest('hex')
|
||||
}
|
||||
|
||||
export const readSimpleDotenv = async (filePath: string): Promise<Record<string, string>> => {
|
||||
const fileContent = await readFile(filePath, 'utf8')
|
||||
const entries = fileContent
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line && !line.startsWith('#'))
|
||||
.map<[string, string]>((line) => {
|
||||
const separatorIndex = line.indexOf('=')
|
||||
const key = separatorIndex === -1 ? line : line.slice(0, separatorIndex).trim()
|
||||
const rawValue = separatorIndex === -1 ? '' : line.slice(separatorIndex + 1).trim()
|
||||
|
||||
if (
|
||||
(rawValue.startsWith('"') && rawValue.endsWith('"')) ||
|
||||
(rawValue.startsWith("'") && rawValue.endsWith("'"))
|
||||
) {
|
||||
return [key, rawValue.slice(1, -1)]
|
||||
}
|
||||
|
||||
return [key, rawValue]
|
||||
})
|
||||
|
||||
return Object.fromEntries(entries)
|
||||
}
|
||||
|
||||
export const waitForCondition = async ({
|
||||
check,
|
||||
description,
|
||||
intervalMs,
|
||||
timeoutMs,
|
||||
}: {
|
||||
check: () => Promise<boolean> | boolean
|
||||
description: string
|
||||
intervalMs: number
|
||||
timeoutMs: number
|
||||
}) => {
|
||||
const deadline = Date.now() + timeoutMs
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
if (await check()) return
|
||||
|
||||
await sleep(intervalMs)
|
||||
}
|
||||
|
||||
throw new Error(`Timed out waiting for ${description} after ${timeoutMs}ms.`)
|
||||
}
|
||||
|
||||
export const isTcpPortReachable = async (host: string, port: number, timeoutMs = 1_000) => {
|
||||
return await new Promise<boolean>((resolve) => {
|
||||
const socket = net.createConnection({
|
||||
host,
|
||||
port,
|
||||
})
|
||||
|
||||
const finish = (result: boolean) => {
|
||||
socket.removeAllListeners()
|
||||
socket.destroy()
|
||||
resolve(result)
|
||||
}
|
||||
|
||||
socket.setTimeout(timeoutMs)
|
||||
socket.once('connect', () => finish(true))
|
||||
socket.once('timeout', () => finish(false))
|
||||
socket.once('error', () => finish(false))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { loadE2eEnv, validateE2eEnv } from './env.ts'
|
||||
|
||||
// Entry points import this before test-env.ts so e2e/.env.local can affect base URLs and runner flags.
|
||||
loadE2eEnv()
|
||||
validateE2eEnv()
|
||||
@@ -0,0 +1,140 @@
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { createEnv } from '@t3-oss/env-core'
|
||||
import * as z from 'zod'
|
||||
import { e2eDir } from './common.ts'
|
||||
|
||||
const booleanString = z.enum(['0', '1', 'false', 'true'])
|
||||
const jsonObjectString = z.string().refine((value) => {
|
||||
try {
|
||||
const parsed = JSON.parse(value) as unknown
|
||||
return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}, 'must be a JSON object string')
|
||||
|
||||
const parseEnvLine = (line: string) => {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed || trimmed.startsWith('#')) return undefined
|
||||
|
||||
const normalized = trimmed.startsWith('export ')
|
||||
? trimmed.slice('export '.length).trim()
|
||||
: trimmed
|
||||
const separatorIndex = normalized.indexOf('=')
|
||||
if (separatorIndex === -1) return undefined
|
||||
|
||||
const key = normalized.slice(0, separatorIndex).trim()
|
||||
let value = normalized.slice(separatorIndex + 1).trim()
|
||||
if (!key) return undefined
|
||||
|
||||
if (
|
||||
(value.startsWith('"') && value.endsWith('"')) ||
|
||||
(value.startsWith("'") && value.endsWith("'"))
|
||||
) {
|
||||
value = value.slice(1, -1)
|
||||
}
|
||||
|
||||
return [key, value] as const
|
||||
}
|
||||
|
||||
export const loadE2eEnv = () => {
|
||||
const envFilePath = process.env.E2E_ENV_FILE
|
||||
? path.resolve(process.env.E2E_ENV_FILE)
|
||||
: path.join(e2eDir, '.env.local')
|
||||
|
||||
if (!existsSync(envFilePath)) return
|
||||
|
||||
const content = readFileSync(envFilePath, 'utf8')
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
const entry = parseEnvLine(line)
|
||||
if (!entry) continue
|
||||
|
||||
const [key, value] = entry
|
||||
process.env[key] ??= value
|
||||
}
|
||||
}
|
||||
|
||||
export const validateE2eEnv = () =>
|
||||
createEnv({
|
||||
client: {},
|
||||
clientPrefix: 'NEXT_PUBLIC_',
|
||||
emptyStringAsUndefined: true,
|
||||
onValidationError: (issues) => {
|
||||
const messages = issues.map((issue) => {
|
||||
const path =
|
||||
Array.isArray(issue.path) && issue.path.length > 0 ? issue.path.join('.') : '(root)'
|
||||
return `- ${path}: ${issue.message}`
|
||||
})
|
||||
|
||||
throw new Error(`Invalid E2E env:\n${messages.join('\n')}`)
|
||||
},
|
||||
runtimeEnv: {
|
||||
CUCUMBER_HEADLESS: process.env.CUCUMBER_HEADLESS,
|
||||
E2E_ADMIN_EMAIL: process.env.E2E_ADMIN_EMAIL,
|
||||
E2E_ADMIN_NAME: process.env.E2E_ADMIN_NAME,
|
||||
E2E_ADMIN_PASSWORD: process.env.E2E_ADMIN_PASSWORD,
|
||||
E2E_AGENT_BACKEND_PORT: process.env.E2E_AGENT_BACKEND_PORT,
|
||||
E2E_AGENT_BACKEND_URL: process.env.E2E_AGENT_BACKEND_URL,
|
||||
E2E_AGENT_DECISION_MODEL_NAME: process.env.E2E_AGENT_DECISION_MODEL_NAME,
|
||||
E2E_AGENT_DECISION_MODEL_PROVIDER: process.env.E2E_AGENT_DECISION_MODEL_PROVIDER,
|
||||
E2E_AGENT_DECISION_MODEL_TYPE: process.env.E2E_AGENT_DECISION_MODEL_TYPE,
|
||||
E2E_AGENT_OUTPUT_ROUTES_PREVIEW_APP_ID: process.env.E2E_AGENT_OUTPUT_ROUTES_PREVIEW_APP_ID,
|
||||
E2E_API_URL: process.env.E2E_API_URL,
|
||||
E2E_BASE_URL: process.env.E2E_BASE_URL,
|
||||
E2E_BROWSER: process.env.E2E_BROWSER,
|
||||
E2E_CUCUMBER_TAGS: process.env.E2E_CUCUMBER_TAGS,
|
||||
E2E_FORCE_WEB_BUILD: process.env.E2E_FORCE_WEB_BUILD,
|
||||
E2E_INIT_PASSWORD: process.env.E2E_INIT_PASSWORD,
|
||||
E2E_MARKETPLACE_API_URL: process.env.E2E_MARKETPLACE_API_URL,
|
||||
E2E_MARKETPLACE_PLUGIN_IDS: process.env.E2E_MARKETPLACE_PLUGIN_IDS,
|
||||
E2E_MODEL_PROVIDER_CREDENTIALS_JSON: process.env.E2E_MODEL_PROVIDER_CREDENTIALS_JSON,
|
||||
E2E_REUSE_WEB_SERVER: process.env.E2E_REUSE_WEB_SERVER,
|
||||
E2E_SLOW_MO: process.env.E2E_SLOW_MO,
|
||||
E2E_SHELLCTL_AUTH_TOKEN: process.env.E2E_SHELLCTL_AUTH_TOKEN,
|
||||
E2E_SHELLCTL_CONTAINER_NAME: process.env.E2E_SHELLCTL_CONTAINER_NAME,
|
||||
E2E_SHELLCTL_IMAGE: process.env.E2E_SHELLCTL_IMAGE,
|
||||
E2E_SHELLCTL_PORT: process.env.E2E_SHELLCTL_PORT,
|
||||
E2E_SHELLCTL_URL: process.env.E2E_SHELLCTL_URL,
|
||||
E2E_SPEECH_TO_TEXT_MODEL_NAME: process.env.E2E_SPEECH_TO_TEXT_MODEL_NAME,
|
||||
E2E_SPEECH_TO_TEXT_MODEL_PROVIDER: process.env.E2E_SPEECH_TO_TEXT_MODEL_PROVIDER,
|
||||
E2E_START_AGENT_BACKEND: process.env.E2E_START_AGENT_BACKEND,
|
||||
E2E_STABLE_MODEL_NAME: process.env.E2E_STABLE_MODEL_NAME,
|
||||
E2E_STABLE_MODEL_PROVIDER: process.env.E2E_STABLE_MODEL_PROVIDER,
|
||||
E2E_STABLE_MODEL_TYPE: process.env.E2E_STABLE_MODEL_TYPE,
|
||||
},
|
||||
server: {
|
||||
CUCUMBER_HEADLESS: booleanString.optional(),
|
||||
E2E_ADMIN_EMAIL: z.email().optional(),
|
||||
E2E_ADMIN_NAME: z.string().min(1).optional(),
|
||||
E2E_ADMIN_PASSWORD: z.string().min(1).optional(),
|
||||
E2E_AGENT_BACKEND_PORT: z.coerce.number().int().positive().max(65535).optional(),
|
||||
E2E_AGENT_BACKEND_URL: z.url().optional(),
|
||||
E2E_AGENT_DECISION_MODEL_NAME: z.string().min(1).optional(),
|
||||
E2E_AGENT_DECISION_MODEL_PROVIDER: z.string().min(1).optional(),
|
||||
E2E_AGENT_DECISION_MODEL_TYPE: z.string().min(1).optional(),
|
||||
E2E_AGENT_OUTPUT_ROUTES_PREVIEW_APP_ID: z.uuid().optional(),
|
||||
E2E_API_URL: z.url().optional(),
|
||||
E2E_BASE_URL: z.url().optional(),
|
||||
E2E_BROWSER: z.enum(['chromium', 'webkit']).optional(),
|
||||
E2E_CUCUMBER_TAGS: z.string().min(1).optional(),
|
||||
E2E_FORCE_WEB_BUILD: z.literal('1').optional(),
|
||||
E2E_INIT_PASSWORD: z.string().min(1).optional(),
|
||||
E2E_MARKETPLACE_API_URL: z.url().optional(),
|
||||
E2E_MARKETPLACE_PLUGIN_IDS: z.string().min(1).optional(),
|
||||
E2E_MODEL_PROVIDER_CREDENTIALS_JSON: jsonObjectString.optional(),
|
||||
E2E_REUSE_WEB_SERVER: booleanString.optional(),
|
||||
E2E_SLOW_MO: z.coerce.number().nonnegative().optional(),
|
||||
E2E_SHELLCTL_AUTH_TOKEN: z.string().min(1).optional(),
|
||||
E2E_SHELLCTL_CONTAINER_NAME: z.string().min(1).optional(),
|
||||
E2E_SHELLCTL_IMAGE: z.string().min(1).optional(),
|
||||
E2E_SHELLCTL_PORT: z.coerce.number().int().positive().max(65535).optional(),
|
||||
E2E_SHELLCTL_URL: z.url().optional(),
|
||||
E2E_SPEECH_TO_TEXT_MODEL_NAME: z.string().min(1).optional(),
|
||||
E2E_SPEECH_TO_TEXT_MODEL_PROVIDER: z.string().min(1).optional(),
|
||||
E2E_START_AGENT_BACKEND: booleanString.optional(),
|
||||
E2E_STABLE_MODEL_NAME: z.string().min(1).optional(),
|
||||
E2E_STABLE_MODEL_PROVIDER: z.string().min(1).optional(),
|
||||
E2E_STABLE_MODEL_TYPE: z.string().min(1).optional(),
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,267 @@
|
||||
import type { ManagedProcess } from '../support/process.ts'
|
||||
import { mkdir, readFile, rm } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { runCleanupTasks } from '../support/cleanup.ts'
|
||||
import { assertCucumberScenariosStarted } from '../support/cucumber-messages.ts'
|
||||
import { startLoggedProcess, stopManagedProcess, waitForUrl } from '../support/process.ts'
|
||||
import { startWebBuildDownload } from '../support/web-build.ts'
|
||||
import { startWebServer, stopWebServer } from '../support/web-server.ts'
|
||||
import { apiURL, baseURL, reuseExistingWebServer } from '../test-env.ts'
|
||||
import { e2eDir, isMainModule, runCommand } from './common.ts'
|
||||
import { parseRunOptions, shouldStartManagedAgentBackend } from './run-options.ts'
|
||||
import { runSeed } from './seed-runner.ts'
|
||||
import {
|
||||
getManagedSandboxEnvironment,
|
||||
resetState,
|
||||
startMiddleware,
|
||||
stopMiddleware,
|
||||
} from './setup.ts'
|
||||
import './env-register.ts'
|
||||
|
||||
const hasCustomTags = (forwardArgs: string[]) =>
|
||||
forwardArgs.some((arg) => arg === '--tags' || arg.startsWith('--tags='))
|
||||
|
||||
const fullNonExternalTags =
|
||||
'not @axe and not @prepared and not @external-model and not @external-tool and not @cloud-catalog-runtime'
|
||||
const seedCeleryQueues = 'dataset,priority_dataset,workflow_based_app_execution'
|
||||
|
||||
const readLogTail = async (logFilePath: string) => {
|
||||
const content = await readFile(logFilePath, 'utf8').catch(() => '')
|
||||
|
||||
return content.trim().split(/\r?\n/).slice(-20).join('\n')
|
||||
}
|
||||
|
||||
const waitForUnexpectedProcessExit = async (
|
||||
managedProcess: ManagedProcess,
|
||||
shouldIgnoreExit: () => boolean,
|
||||
) => {
|
||||
const { childProcess, label, logFilePath } = managedProcess
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
if (childProcess.exitCode !== null) {
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
|
||||
childProcess.once('exit', () => resolve())
|
||||
})
|
||||
|
||||
if (shouldIgnoreExit()) return
|
||||
|
||||
const logTail = await readLogTail(logFilePath)
|
||||
const logTailMessage = logTail ? `\n\nLast ${label} log lines:\n${logTail}` : ''
|
||||
|
||||
throw new Error(`${label} exited before becoming ready. See ${logFilePath}.${logTailMessage}`)
|
||||
}
|
||||
|
||||
const waitForManagedProcess = async ({
|
||||
errorMessage,
|
||||
managedProcess,
|
||||
url,
|
||||
}: {
|
||||
errorMessage: string
|
||||
managedProcess: ManagedProcess
|
||||
url: string
|
||||
}) => {
|
||||
let waiting = true
|
||||
try {
|
||||
await Promise.race([
|
||||
waitForUrl(url, 180_000, 1_000),
|
||||
waitForUnexpectedProcessExit(managedProcess, () => !waiting),
|
||||
])
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('exited before becoming ready'))
|
||||
throw error
|
||||
|
||||
throw new Error(`${errorMessage} See ${managedProcess.logFilePath}.`)
|
||||
} finally {
|
||||
waiting = false
|
||||
}
|
||||
}
|
||||
|
||||
export const runCucumber = async (argv: string[]) => {
|
||||
const { downloadWebBuild, forwardArgs, full, headed, seed, seedOnly } = parseRunOptions(argv)
|
||||
const startAgentBackendForRun = shouldStartManagedAgentBackend()
|
||||
const sandboxEnvironment = startAgentBackendForRun ? getManagedSandboxEnvironment() : {}
|
||||
const cucumberReportDir = path.join(e2eDir, 'cucumber-report')
|
||||
const logDir = path.join(e2eDir, '.logs')
|
||||
let apiProcess: ManagedProcess | undefined
|
||||
let celeryProcess: ManagedProcess | undefined
|
||||
let difyAgentProcess: ManagedProcess | undefined
|
||||
let middlewareStarted = false
|
||||
let shellctlProcess: ManagedProcess | undefined
|
||||
let webBuild: Awaited<ReturnType<typeof startWebBuildDownload>> | undefined
|
||||
|
||||
let cleanupPromise: Promise<void> | undefined
|
||||
const cleanup = async () => {
|
||||
if (!cleanupPromise) {
|
||||
cleanupPromise = (async () => {
|
||||
const cleanupErrors = await runCleanupTasks([
|
||||
{ label: 'Stop Web build download', run: () => stopManagedProcess(webBuild?.process) },
|
||||
{ label: 'Stop web server', run: stopWebServer },
|
||||
{ label: 'Stop celery worker', run: () => stopManagedProcess(celeryProcess) },
|
||||
{ label: 'Stop API server', run: () => stopManagedProcess(apiProcess) },
|
||||
{ label: 'Stop agent backend', run: () => stopManagedProcess(difyAgentProcess) },
|
||||
{ label: 'Stop shellctl sandbox', run: () => stopManagedProcess(shellctlProcess) },
|
||||
...(middlewareStarted ? [{ label: 'Stop middleware', run: stopMiddleware }] : []),
|
||||
])
|
||||
|
||||
if (cleanupErrors.length > 0)
|
||||
throw new Error(`E2E teardown errors:\n${cleanupErrors.join('\n')}`)
|
||||
})()
|
||||
}
|
||||
|
||||
await cleanupPromise
|
||||
}
|
||||
|
||||
const onTerminate = () => {
|
||||
void cleanup()
|
||||
.catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
})
|
||||
.finally(() => {
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
process.once('SIGINT', onTerminate)
|
||||
process.once('SIGTERM', onTerminate)
|
||||
|
||||
try {
|
||||
if (full) await resetState()
|
||||
if (!seedOnly) await rm(cucumberReportDir, { force: true, recursive: true })
|
||||
await mkdir(logDir, { recursive: true })
|
||||
|
||||
// The runner owns both preparation branches and tears them down on failure.
|
||||
// Download the shared build while middleware and backend services start.
|
||||
if (downloadWebBuild) webBuild = await startWebBuildDownload(logDir)
|
||||
|
||||
if (full) {
|
||||
middlewareStarted = true
|
||||
await startMiddleware()
|
||||
}
|
||||
|
||||
if (
|
||||
startAgentBackendForRun &&
|
||||
!process.env.DIFY_AGENT_LOCAL_SANDBOX_ENDPOINT?.trim() &&
|
||||
!process.env.DIFY_AGENT_SHELLCTL_ENTRYPOINT?.trim() &&
|
||||
!process.env.E2E_SHELLCTL_URL?.trim()
|
||||
) {
|
||||
shellctlProcess = await startLoggedProcess({
|
||||
command: process.execPath,
|
||||
args: ['./scripts/setup.ts', 'shellctl-sandbox'],
|
||||
cwd: e2eDir,
|
||||
env: sandboxEnvironment,
|
||||
label: 'shellctl sandbox',
|
||||
logFilePath: path.join(logDir, 'cucumber-shellctl-sandbox.log'),
|
||||
})
|
||||
const shellctlPort = process.env.E2E_SHELLCTL_PORT || '5004'
|
||||
await waitForManagedProcess({
|
||||
errorMessage: 'Shellctl sandbox did not become ready.',
|
||||
managedProcess: shellctlProcess,
|
||||
url: `http://127.0.0.1:${shellctlPort}/healthz`,
|
||||
})
|
||||
}
|
||||
|
||||
if (startAgentBackendForRun) {
|
||||
difyAgentProcess = await startLoggedProcess({
|
||||
command: process.execPath,
|
||||
args: ['./scripts/setup.ts', 'agent-backend'],
|
||||
cwd: e2eDir,
|
||||
env: { ...sandboxEnvironment, E2E_START_AGENT_BACKEND: '1' },
|
||||
label: 'agent backend',
|
||||
logFilePath: path.join(logDir, 'cucumber-agent-backend.log'),
|
||||
})
|
||||
const agentBackendPort = process.env.E2E_AGENT_BACKEND_PORT || '5050'
|
||||
await waitForManagedProcess({
|
||||
errorMessage: 'Agent backend did not become ready.',
|
||||
managedProcess: difyAgentProcess,
|
||||
url: `http://127.0.0.1:${agentBackendPort}/openapi.json`,
|
||||
})
|
||||
}
|
||||
|
||||
apiProcess = await startLoggedProcess({
|
||||
command: process.execPath,
|
||||
args: ['./scripts/setup.ts', 'api'],
|
||||
cwd: e2eDir,
|
||||
env: startAgentBackendForRun ? { E2E_START_AGENT_BACKEND: '1' } : undefined,
|
||||
label: 'api server',
|
||||
logFilePath: path.join(logDir, 'cucumber-api.log'),
|
||||
})
|
||||
await waitForManagedProcess({
|
||||
errorMessage: `API did not become ready at ${apiURL}/health.`,
|
||||
managedProcess: apiProcess,
|
||||
url: `${apiURL}/health`,
|
||||
})
|
||||
|
||||
celeryProcess = await startLoggedProcess({
|
||||
command: process.execPath,
|
||||
args: ['./scripts/setup.ts', 'celery', ...(seed ? ['--queues', seedCeleryQueues] : [])],
|
||||
cwd: e2eDir,
|
||||
label: 'celery worker',
|
||||
logFilePath: path.join(logDir, 'cucumber-celery.log'),
|
||||
})
|
||||
|
||||
if (webBuild) {
|
||||
const waitStarted = Date.now()
|
||||
console.log('Backend services are ready; waiting for the shared Web build.')
|
||||
const error = await webBuild.completed
|
||||
if (error) throw error
|
||||
console.log(
|
||||
`Shared Web build ready after ${Math.round((Date.now() - waitStarted) / 1000)}s of additional waiting.`,
|
||||
)
|
||||
}
|
||||
|
||||
await startWebServer({
|
||||
baseURL,
|
||||
command: process.execPath,
|
||||
args: ['./scripts/setup.ts', 'web'],
|
||||
cwd: e2eDir,
|
||||
logFilePath: path.join(logDir, 'cucumber-web.log'),
|
||||
reuseExistingServer: reuseExistingWebServer,
|
||||
timeoutMs: 300_000,
|
||||
})
|
||||
|
||||
if (seed) await runSeed(seed)
|
||||
|
||||
if (!seedOnly) {
|
||||
const cucumberEnv: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
...sandboxEnvironment,
|
||||
CUCUMBER_HEADLESS: headed ? '0' : '1',
|
||||
}
|
||||
|
||||
if (full && !hasCustomTags(forwardArgs)) cucumberEnv.E2E_CUCUMBER_TAGS = fullNonExternalTags
|
||||
|
||||
const result = await runCommand({
|
||||
command: process.execPath,
|
||||
args: [
|
||||
'./node_modules/@cucumber/cucumber/bin/cucumber.js',
|
||||
'--config',
|
||||
'./cucumber.config.ts',
|
||||
...forwardArgs,
|
||||
],
|
||||
cwd: e2eDir,
|
||||
env: cucumberEnv,
|
||||
})
|
||||
|
||||
if (result.exitCode === 0) {
|
||||
const messages = await readFile(path.join(cucumberReportDir, 'report.ndjson'), 'utf8')
|
||||
assertCucumberScenariosStarted(messages)
|
||||
}
|
||||
|
||||
process.exitCode = result.exitCode
|
||||
}
|
||||
} finally {
|
||||
process.off('SIGINT', onTerminate)
|
||||
process.off('SIGTERM', onTerminate)
|
||||
await cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
if (isMainModule(import.meta.url)) {
|
||||
void runCucumber(process.argv.slice(2)).catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { e2eDir, isMainModule, runForegroundProcess } from './common.ts'
|
||||
import './env-register.ts'
|
||||
|
||||
const defaultExternalRuntimeTags = '@external-model or @external-tool'
|
||||
|
||||
const main = async () => {
|
||||
await runForegroundProcess({
|
||||
command: process.execPath,
|
||||
args: [
|
||||
'./scripts/run-cucumber.ts',
|
||||
'--full',
|
||||
'--profile',
|
||||
'external-runtime',
|
||||
'--',
|
||||
'--tags',
|
||||
defaultExternalRuntimeTags,
|
||||
],
|
||||
cwd: e2eDir,
|
||||
})
|
||||
}
|
||||
|
||||
if (isMainModule(import.meta.url)) void main()
|
||||
@@ -0,0 +1,131 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { e2eDir, isMainModule, runForegroundProcess } from './common.ts'
|
||||
import './env-register.ts'
|
||||
|
||||
const postMergeTags = '@prepared or @external-model or @external-tool'
|
||||
|
||||
const main = async () => {
|
||||
await runForegroundProcess({
|
||||
command: process.execPath,
|
||||
args: [
|
||||
'./scripts/run-cucumber.ts',
|
||||
'--full',
|
||||
'--profile',
|
||||
'post-merge',
|
||||
'--',
|
||||
'--tags',
|
||||
postMergeTags,
|
||||
],
|
||||
cwd: e2eDir,
|
||||
})
|
||||
}
|
||||
|
||||
if (isMainModule(import.meta.url)) void main()
|
||||
@@ -0,0 +1,22 @@
|
||||
import { e2eDir, isMainModule, runForegroundProcess } from './common.ts'
|
||||
import './env-register.ts'
|
||||
|
||||
const preparedTags = '@prepared'
|
||||
|
||||
const main = async () => {
|
||||
await runForegroundProcess({
|
||||
command: process.execPath,
|
||||
args: [
|
||||
'./scripts/run-cucumber.ts',
|
||||
'--full',
|
||||
'--profile',
|
||||
'prepared',
|
||||
'--',
|
||||
'--tags',
|
||||
preparedTags,
|
||||
],
|
||||
cwd: e2eDir,
|
||||
})
|
||||
}
|
||||
|
||||
if (isMainModule(import.meta.url)) void main()
|
||||
@@ -0,0 +1,54 @@
|
||||
import { chromium } from '@playwright/test'
|
||||
import { createAgentV2SeedTasks } from '../features/agent-v2/support/seed.ts'
|
||||
import { ensureAuthenticatedState } from '../fixtures/auth.ts'
|
||||
import { createStandaloneConsoleSession } from '../support/api/console-session.ts'
|
||||
import { runSeedTasks, writeSeedReport } from '../support/seed.ts'
|
||||
import { baseURL } from '../test-env.ts'
|
||||
|
||||
export type SeedOptions = {
|
||||
allowBlocked: boolean
|
||||
dryRun: boolean
|
||||
pack: string
|
||||
profile: string
|
||||
}
|
||||
|
||||
const getTasks = (pack: string, profile: string) => {
|
||||
if (pack === 'agent-v2') return createAgentV2SeedTasks(profile)
|
||||
|
||||
throw new Error(`Unknown seed pack "${pack}".`)
|
||||
}
|
||||
|
||||
const ensureAuth = async () => {
|
||||
const browser = await chromium.launch({ headless: true })
|
||||
try {
|
||||
await ensureAuthenticatedState(browser, baseURL)
|
||||
} finally {
|
||||
await browser.close()
|
||||
}
|
||||
}
|
||||
|
||||
export const runSeed = async ({ allowBlocked, dryRun, pack, profile }: SeedOptions) => {
|
||||
console.warn(`[seed] bootstrapping auth state against ${baseURL}`)
|
||||
await ensureAuth()
|
||||
|
||||
const consoleSession = await createStandaloneConsoleSession()
|
||||
try {
|
||||
const results = await runSeedTasks(getTasks(pack, profile), {
|
||||
consoleClient: consoleSession.client,
|
||||
dryRun,
|
||||
resources: new Map(),
|
||||
})
|
||||
const reportName = `${pack}-${profile}`
|
||||
const reportPath = await writeSeedReport(reportName, results)
|
||||
const blockedCount = results.filter((result) => result.status === 'blocked').length
|
||||
|
||||
console.warn(`[seed] report ${reportPath}`)
|
||||
if (blockedCount > 0 && !allowBlocked) {
|
||||
throw new Error(
|
||||
`${blockedCount} seed task${blockedCount === 1 ? '' : 's'} blocked. Re-run with --allow-blocked only when partial readiness is intentional.`,
|
||||
)
|
||||
}
|
||||
} finally {
|
||||
await consoleSession.dispose()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,651 @@
|
||||
import { createHash, randomBytes } from 'node:crypto'
|
||||
import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { waitForUrl } from '../support/process.ts'
|
||||
import {
|
||||
apiDir,
|
||||
apiEnvExampleFile,
|
||||
difyAgentDir,
|
||||
difyAgentRuntimeDir,
|
||||
dockerDir,
|
||||
e2eDir,
|
||||
e2eWebEnvOverrides,
|
||||
ensureFileExists,
|
||||
ensureLineInFile,
|
||||
getTcpPortListenerDescription,
|
||||
getWebEnvLocalHash,
|
||||
isMainModule,
|
||||
isTcpPortReachable,
|
||||
middlewareComposeFile,
|
||||
middlewareEnvExampleFile,
|
||||
middlewareEnvFile,
|
||||
readSimpleDotenv,
|
||||
rootDir,
|
||||
runCommand,
|
||||
runCommandOrThrow,
|
||||
runForegroundProcess,
|
||||
waitForCondition,
|
||||
webDir,
|
||||
} from './common.ts'
|
||||
import './env-register.ts'
|
||||
|
||||
const buildIdPath = path.join(webDir, '.next', 'BUILD_ID')
|
||||
const webBuildStampPath = path.join(webDir, '.next', 'e2e-web-build.sha256')
|
||||
const apiLoopbackHost = '127.0.0.1'
|
||||
const apiBindHost = '0.0.0.0'
|
||||
const apiPort = 5001
|
||||
const agentBackendHost = '127.0.0.1'
|
||||
const agentBackendBindHost = '0.0.0.0'
|
||||
const agentBackendPort = Number(process.env.E2E_AGENT_BACKEND_PORT || 5050)
|
||||
const shellctlHost = '127.0.0.1'
|
||||
const shellctlPort = Number(process.env.E2E_SHELLCTL_PORT || 5004)
|
||||
const shellctlContainerName = process.env.E2E_SHELLCTL_CONTAINER_NAME || 'dify-agent-e2e-shellctl'
|
||||
const shellctlImage = process.env.E2E_SHELLCTL_IMAGE || 'dify-agent-local-sandbox:e2e'
|
||||
const shellctlUrl = `http://${shellctlHost}:${shellctlPort}`
|
||||
const agentStubApiBaseUrl = `http://host.docker.internal:${agentBackendPort}/agent-stub`
|
||||
const sandboxFilesBaseUrl = `http://host.docker.internal:${apiPort}`
|
||||
const defaultPluginDaemonKey = 'lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi'
|
||||
const defaultInnerApiKeyForPlugin = 'QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1'
|
||||
const defaultAgentServerSecretKey = 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY'
|
||||
|
||||
const middlewareDataPaths = [
|
||||
path.join(dockerDir, 'volumes', 'db', 'data'),
|
||||
path.join(dockerDir, 'volumes', 'plugin_daemon'),
|
||||
path.join(dockerDir, 'volumes', 'redis', 'data'),
|
||||
path.join(dockerDir, 'volumes', 'weaviate'),
|
||||
]
|
||||
|
||||
const e2eStatePaths = [
|
||||
path.join(e2eDir, '.auth'),
|
||||
path.join(e2eDir, 'cucumber-report'),
|
||||
path.join(e2eDir, 'cucumber-report-non-external'),
|
||||
path.join(e2eDir, 'cucumber-report-webkit'),
|
||||
path.join(e2eDir, '.logs'),
|
||||
path.join(e2eDir, '.logs-non-external'),
|
||||
path.join(e2eDir, '.logs-webkit'),
|
||||
path.join(e2eDir, 'playwright-report'),
|
||||
path.join(e2eDir, 'seed-report'),
|
||||
path.join(e2eDir, 'test-results'),
|
||||
]
|
||||
|
||||
const composeArgs = [
|
||||
'compose',
|
||||
'-f',
|
||||
middlewareComposeFile,
|
||||
'--profile',
|
||||
'postgresql',
|
||||
'--profile',
|
||||
'weaviate',
|
||||
]
|
||||
|
||||
const getApiEnvironment = async (): Promise<Record<string, string>> => {
|
||||
const envFromExample = await readSimpleDotenv(apiEnvExampleFile)
|
||||
const agentBackendBaseUrl = getAgentBackendBaseUrl()
|
||||
const marketplaceApiUrl = process.env.E2E_MARKETPLACE_API_URL?.trim()
|
||||
|
||||
return {
|
||||
...envFromExample,
|
||||
...(agentBackendBaseUrl ? { AGENT_BACKEND_BASE_URL: agentBackendBaseUrl } : {}),
|
||||
...(marketplaceApiUrl ? { MARKETPLACE_API_URL: marketplaceApiUrl } : {}),
|
||||
FLASK_APP: 'app.py',
|
||||
HTTP_PROXY: process.env.HTTP_PROXY || '',
|
||||
HTTPS_PROXY: process.env.HTTPS_PROXY || '',
|
||||
}
|
||||
}
|
||||
|
||||
function getAgentBackendBaseUrl() {
|
||||
const explicitApiUrl = process.env.AGENT_BACKEND_BASE_URL?.trim()
|
||||
if (explicitApiUrl) return explicitApiUrl
|
||||
|
||||
const explicitE2EUrl = process.env.E2E_AGENT_BACKEND_URL?.trim()
|
||||
if (explicitE2EUrl) return explicitE2EUrl.replace(/\/$/, '')
|
||||
|
||||
if (process.env.E2E_START_AGENT_BACKEND === '1' || process.env.E2E_START_AGENT_BACKEND === 'true')
|
||||
return `http://${agentBackendHost}:${agentBackendPort}`
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const getManagedSandboxEnvironment = () => {
|
||||
const externalSandbox =
|
||||
process.env.DIFY_AGENT_LOCAL_SANDBOX_ENDPOINT?.trim() ||
|
||||
process.env.DIFY_AGENT_SHELLCTL_ENTRYPOINT?.trim() ||
|
||||
process.env.E2E_SHELLCTL_URL?.trim()
|
||||
const token =
|
||||
process.env.E2E_SHELLCTL_AUTH_TOKEN ||
|
||||
process.env.DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN ||
|
||||
process.env.DIFY_AGENT_SHELLCTL_AUTH_TOKEN ||
|
||||
(externalSandbox ? '' : randomBytes(32).toString('hex'))
|
||||
|
||||
if (!token.trim()) {
|
||||
throw new Error(
|
||||
'A nonblank sandbox auth token (E2E_SHELLCTL_AUTH_TOKEN or the Agent sandbox token) is required for an existing sandbox or when explicitly configured.',
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
E2E_SHELLCTL_AUTH_TOKEN: token,
|
||||
DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN: token,
|
||||
DIFY_AGENT_SHELLCTL_AUTH_TOKEN: token,
|
||||
}
|
||||
}
|
||||
|
||||
export const getAgentBackendEnvironment = async () => {
|
||||
const apiEnv = await getApiEnvironment()
|
||||
const redisPassword = process.env.REDIS_PASSWORD || apiEnv.REDIS_PASSWORD || 'difyai123456'
|
||||
const sandboxToken =
|
||||
process.env.E2E_SHELLCTL_AUTH_TOKEN ||
|
||||
process.env.DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN ||
|
||||
process.env.DIFY_AGENT_SHELLCTL_AUTH_TOKEN ||
|
||||
''
|
||||
|
||||
return {
|
||||
DIFY_AGENT_INNER_API_KEY:
|
||||
process.env.DIFY_AGENT_INNER_API_KEY ||
|
||||
process.env.INNER_API_KEY_FOR_PLUGIN ||
|
||||
process.env.PLUGIN_DIFY_INNER_API_KEY ||
|
||||
apiEnv.INNER_API_KEY_FOR_PLUGIN ||
|
||||
defaultInnerApiKeyForPlugin,
|
||||
DIFY_AGENT_INNER_API_URL:
|
||||
process.env.DIFY_AGENT_INNER_API_URL || `http://${apiLoopbackHost}:${apiPort}`,
|
||||
DIFY_AGENT_SANDBOX_FILES_BASE_URL:
|
||||
process.env.DIFY_AGENT_SANDBOX_FILES_BASE_URL || sandboxFilesBaseUrl,
|
||||
DIFY_AGENT_SERVER_SECRET_KEY:
|
||||
process.env.DIFY_AGENT_SERVER_SECRET_KEY || defaultAgentServerSecretKey,
|
||||
DIFY_AGENT_STUB_API_BASE_URL: process.env.DIFY_AGENT_STUB_API_BASE_URL || agentStubApiBaseUrl,
|
||||
DIFY_AGENT_PLUGIN_DAEMON_API_KEY:
|
||||
process.env.DIFY_AGENT_PLUGIN_DAEMON_API_KEY ||
|
||||
process.env.PLUGIN_DAEMON_KEY ||
|
||||
apiEnv.PLUGIN_DAEMON_KEY ||
|
||||
defaultPluginDaemonKey,
|
||||
DIFY_AGENT_PLUGIN_DAEMON_URL:
|
||||
process.env.DIFY_AGENT_PLUGIN_DAEMON_URL ||
|
||||
process.env.PLUGIN_DAEMON_URL ||
|
||||
'http://127.0.0.1:5002',
|
||||
DIFY_AGENT_REDIS_PREFIX: process.env.DIFY_AGENT_REDIS_PREFIX || 'dify-agent-e2e',
|
||||
DIFY_AGENT_REDIS_URL:
|
||||
process.env.DIFY_AGENT_REDIS_URL || `redis://:${redisPassword}@127.0.0.1:6379/0`,
|
||||
DIFY_AGENT_LOCAL_SANDBOX_AUTH_TOKEN: sandboxToken,
|
||||
DIFY_AGENT_SHELLCTL_AUTH_TOKEN: sandboxToken,
|
||||
DIFY_AGENT_SHELLCTL_ENTRYPOINT:
|
||||
process.env.DIFY_AGENT_SHELLCTL_ENTRYPOINT || process.env.E2E_SHELLCTL_URL || shellctlUrl,
|
||||
}
|
||||
}
|
||||
|
||||
const getServiceContainerId = async (service: string) => {
|
||||
const result = await runCommandOrThrow({
|
||||
command: 'docker',
|
||||
args: ['compose', '-f', middlewareComposeFile, 'ps', '-q', service],
|
||||
cwd: dockerDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
return result.stdout.trim()
|
||||
}
|
||||
|
||||
const getContainerHealth = async (containerId: string) => {
|
||||
const result = await runCommand({
|
||||
command: 'docker',
|
||||
args: ['inspect', '-f', '{{.State.Health.Status}}', containerId],
|
||||
cwd: dockerDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
if (result.exitCode !== 0) return ''
|
||||
|
||||
return result.stdout.trim()
|
||||
}
|
||||
|
||||
const printComposeLogs = async (services: string[]) => {
|
||||
await runCommand({
|
||||
command: 'docker',
|
||||
args: ['compose', '-f', middlewareComposeFile, 'logs', ...services],
|
||||
cwd: dockerDir,
|
||||
})
|
||||
}
|
||||
|
||||
const waitForDependency = async ({
|
||||
description,
|
||||
services,
|
||||
wait,
|
||||
}: {
|
||||
description: string
|
||||
services: string[]
|
||||
wait: () => Promise<void>
|
||||
}) => {
|
||||
console.log(`Waiting for ${description}...`)
|
||||
|
||||
try {
|
||||
await wait()
|
||||
} catch (error) {
|
||||
await printComposeLogs(services)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const webBuildSourcePaths = [
|
||||
'package.json',
|
||||
'pnpm-lock.yaml',
|
||||
'pnpm-workspace.yaml',
|
||||
'packages',
|
||||
'web',
|
||||
]
|
||||
|
||||
const getWebBuildSourceHash = async () => {
|
||||
const hash = createHash('sha256')
|
||||
const gitArgsSuffix = ['--', ...webBuildSourcePaths]
|
||||
const commands = [
|
||||
['rev-parse', 'HEAD'],
|
||||
['diff', '--binary', ...gitArgsSuffix],
|
||||
['diff', '--cached', '--binary', ...gitArgsSuffix],
|
||||
]
|
||||
|
||||
for (const args of commands) {
|
||||
const result = await runCommandOrThrow({
|
||||
command: 'git',
|
||||
args,
|
||||
cwd: rootDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
hash.update(args.join(' '))
|
||||
hash.update('\n')
|
||||
hash.update(result.stdout)
|
||||
hash.update('\n')
|
||||
}
|
||||
|
||||
const untrackedFiles = await runCommandOrThrow({
|
||||
command: 'git',
|
||||
args: ['ls-files', '--others', '--exclude-standard', '-z', ...gitArgsSuffix],
|
||||
cwd: rootDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
for (const file of untrackedFiles.stdout.split('\0').filter(Boolean)) {
|
||||
hash.update(file)
|
||||
hash.update('\0')
|
||||
hash.update(await readFile(path.join(rootDir, file)))
|
||||
hash.update('\0')
|
||||
}
|
||||
|
||||
return hash.digest('hex')
|
||||
}
|
||||
|
||||
export const ensureWebBuild = async () => {
|
||||
const envHash = await getWebEnvLocalHash()
|
||||
const sourceHash = await getWebBuildSourceHash()
|
||||
const buildStamp = createHash('sha256')
|
||||
.update(envHash)
|
||||
.update('\n')
|
||||
.update(sourceHash)
|
||||
.digest('hex')
|
||||
const buildEnv = {
|
||||
...e2eWebEnvOverrides,
|
||||
}
|
||||
|
||||
if (process.env.E2E_FORCE_WEB_BUILD === '1') {
|
||||
await runCommandOrThrow({
|
||||
command: 'pnpm',
|
||||
args: ['run', 'build'],
|
||||
cwd: webDir,
|
||||
env: buildEnv,
|
||||
})
|
||||
await writeFile(webBuildStampPath, `${buildStamp}\n`, 'utf8')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const [buildExists, previousBuildStamp] = await Promise.all([
|
||||
access(buildIdPath)
|
||||
.then(() => true)
|
||||
.catch(() => false),
|
||||
readFile(webBuildStampPath, 'utf8')
|
||||
.then((value) => value.trim())
|
||||
.catch(() => ''),
|
||||
])
|
||||
|
||||
if (buildExists && previousBuildStamp === buildStamp) {
|
||||
console.log('Reusing existing web build artifact.')
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
// Fall through to rebuild when the existing build cannot be verified.
|
||||
}
|
||||
|
||||
await runCommandOrThrow({
|
||||
command: 'pnpm',
|
||||
args: ['run', 'build'],
|
||||
cwd: webDir,
|
||||
env: buildEnv,
|
||||
})
|
||||
await writeFile(webBuildStampPath, `${buildStamp}\n`, 'utf8')
|
||||
}
|
||||
|
||||
export const startWeb = async () => {
|
||||
await ensureWebBuild()
|
||||
|
||||
await runForegroundProcess({
|
||||
command: 'pnpm',
|
||||
args: ['run', 'start'],
|
||||
cwd: webDir,
|
||||
env: {
|
||||
...e2eWebEnvOverrides,
|
||||
HOSTNAME: '127.0.0.1',
|
||||
PORT: '3000',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const startApi = async () => {
|
||||
if (await isTcpPortReachable(apiLoopbackHost, apiPort)) {
|
||||
const listenerDescription = await getTcpPortListenerDescription(apiPort)
|
||||
const listenerMessage = listenerDescription ? `\n\nPort listener:\n${listenerDescription}` : ''
|
||||
|
||||
throw new Error(
|
||||
`Cannot start the E2E API server because ${apiLoopbackHost}:${apiPort} is already in use.${listenerMessage}`,
|
||||
)
|
||||
}
|
||||
|
||||
const env = await getApiEnvironment()
|
||||
|
||||
await runCommandOrThrow({
|
||||
command: 'uv',
|
||||
args: ['run', '--project', '.', '--no-sync', 'flask', 'upgrade-db'],
|
||||
cwd: apiDir,
|
||||
env,
|
||||
})
|
||||
|
||||
await runForegroundProcess({
|
||||
command: 'uv',
|
||||
args: [
|
||||
'run',
|
||||
'--project',
|
||||
'.',
|
||||
'--no-sync',
|
||||
'flask',
|
||||
'run',
|
||||
'--host',
|
||||
apiBindHost,
|
||||
'--port',
|
||||
String(apiPort),
|
||||
],
|
||||
cwd: apiDir,
|
||||
env,
|
||||
})
|
||||
}
|
||||
|
||||
export const startAgentBackend = async () => {
|
||||
if (await isTcpPortReachable(agentBackendHost, agentBackendPort)) {
|
||||
const listenerDescription = await getTcpPortListenerDescription(agentBackendPort)
|
||||
const listenerMessage = listenerDescription ? `\n\nPort listener:\n${listenerDescription}` : ''
|
||||
|
||||
throw new Error(
|
||||
`Cannot start the E2E Agent backend because ${agentBackendHost}:${agentBackendPort} is already in use.${listenerMessage}`,
|
||||
)
|
||||
}
|
||||
|
||||
await runForegroundProcess({
|
||||
command: 'uv',
|
||||
args: [
|
||||
'run',
|
||||
'--project',
|
||||
'.',
|
||||
'--extra',
|
||||
'server',
|
||||
'uvicorn',
|
||||
'dify_agent.server.app:app',
|
||||
'--host',
|
||||
agentBackendBindHost,
|
||||
'--port',
|
||||
String(agentBackendPort),
|
||||
],
|
||||
cwd: difyAgentDir,
|
||||
env: await getAgentBackendEnvironment(),
|
||||
})
|
||||
}
|
||||
|
||||
const ensureShellctlSandboxImage = async () => {
|
||||
const inspectResult = await runCommand({
|
||||
command: 'docker',
|
||||
args: ['image', 'inspect', shellctlImage],
|
||||
cwd: rootDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
if (inspectResult.exitCode === 0 && process.env.E2E_FORCE_SHELLCTL_BUILD !== '1') return
|
||||
|
||||
await runCommandOrThrow({
|
||||
command: 'docker',
|
||||
args: [
|
||||
'build',
|
||||
'-f',
|
||||
path.join(difyAgentRuntimeDir, 'docker', 'Dockerfile'),
|
||||
'-t',
|
||||
shellctlImage,
|
||||
'.',
|
||||
],
|
||||
cwd: difyAgentRuntimeDir,
|
||||
})
|
||||
}
|
||||
|
||||
export const startShellctlSandbox = async () => {
|
||||
const { E2E_SHELLCTL_AUTH_TOKEN: token } = getManagedSandboxEnvironment()
|
||||
|
||||
if (await isTcpPortReachable(shellctlHost, shellctlPort)) {
|
||||
const listenerDescription = await getTcpPortListenerDescription(shellctlPort)
|
||||
const listenerMessage = listenerDescription ? `\n\nPort listener:\n${listenerDescription}` : ''
|
||||
|
||||
throw new Error(
|
||||
`Cannot start the E2E shellctl sandbox because ${shellctlHost}:${shellctlPort} is already in use.${listenerMessage}`,
|
||||
)
|
||||
}
|
||||
|
||||
await runCommand({
|
||||
command: 'docker',
|
||||
args: ['rm', '-f', shellctlContainerName],
|
||||
cwd: rootDir,
|
||||
stdio: 'pipe',
|
||||
})
|
||||
|
||||
await ensureShellctlSandboxImage()
|
||||
|
||||
await runForegroundProcess({
|
||||
command: 'docker',
|
||||
args: [
|
||||
'run',
|
||||
'--rm',
|
||||
'--name',
|
||||
shellctlContainerName,
|
||||
...(process.platform === 'linux' ? ['--add-host', 'host.docker.internal:host-gateway'] : []),
|
||||
'-p',
|
||||
`${shellctlHost}:${shellctlPort}:5004`,
|
||||
'-e',
|
||||
'SHELLCTL_AUTH_TOKEN',
|
||||
shellctlImage,
|
||||
],
|
||||
cwd: rootDir,
|
||||
env: { SHELLCTL_AUTH_TOKEN: token },
|
||||
})
|
||||
}
|
||||
|
||||
export const startCelery = async ({ queues = 'workflow_based_app_execution' } = {}) => {
|
||||
const env = await getApiEnvironment()
|
||||
|
||||
await runForegroundProcess({
|
||||
command: 'uv',
|
||||
args: [
|
||||
'run',
|
||||
'--project',
|
||||
'.',
|
||||
'--no-sync',
|
||||
'celery',
|
||||
'-A',
|
||||
'app.celery',
|
||||
'worker',
|
||||
'--pool',
|
||||
'solo',
|
||||
'--loglevel',
|
||||
'INFO',
|
||||
'-Q',
|
||||
queues,
|
||||
],
|
||||
cwd: apiDir,
|
||||
env,
|
||||
})
|
||||
}
|
||||
|
||||
export const stopMiddleware = async () => {
|
||||
await runCommandOrThrow({
|
||||
command: 'docker',
|
||||
args: [...composeArgs, 'down', '--remove-orphans'],
|
||||
cwd: dockerDir,
|
||||
})
|
||||
}
|
||||
|
||||
export const resetState = async () => {
|
||||
console.log('Stopping middleware services...')
|
||||
try {
|
||||
await stopMiddleware()
|
||||
} catch {
|
||||
// Reset should continue even if middleware is already stopped.
|
||||
}
|
||||
|
||||
console.log('Removing persisted middleware data...')
|
||||
await Promise.all(
|
||||
middlewareDataPaths.map(async (targetPath) => {
|
||||
await rm(targetPath, { force: true, recursive: true })
|
||||
await mkdir(targetPath, { recursive: true })
|
||||
}),
|
||||
)
|
||||
|
||||
console.log('Removing E2E local state...')
|
||||
await Promise.all(
|
||||
e2eStatePaths.map((targetPath) => rm(targetPath, { force: true, recursive: true })),
|
||||
)
|
||||
|
||||
console.log('E2E state reset complete.')
|
||||
}
|
||||
|
||||
export const startMiddleware = async () => {
|
||||
await ensureFileExists(middlewareEnvFile, middlewareEnvExampleFile)
|
||||
await ensureLineInFile(middlewareEnvFile, 'COMPOSE_PROFILES=postgresql,weaviate')
|
||||
|
||||
console.log('Starting middleware services...')
|
||||
await runCommandOrThrow({
|
||||
command: 'docker',
|
||||
args: [
|
||||
...composeArgs,
|
||||
'up',
|
||||
'-d',
|
||||
'db_postgres',
|
||||
'redis',
|
||||
'weaviate',
|
||||
'sandbox',
|
||||
'ssrf_proxy',
|
||||
'plugin_daemon',
|
||||
],
|
||||
cwd: dockerDir,
|
||||
})
|
||||
|
||||
const [postgresContainerId, redisContainerId] = await Promise.all([
|
||||
getServiceContainerId('db_postgres'),
|
||||
getServiceContainerId('redis'),
|
||||
])
|
||||
|
||||
await waitForDependency({
|
||||
description: 'PostgreSQL and Redis health checks',
|
||||
services: ['db_postgres', 'redis'],
|
||||
wait: () =>
|
||||
waitForCondition({
|
||||
check: async () => {
|
||||
const [postgresStatus, redisStatus] = await Promise.all([
|
||||
getContainerHealth(postgresContainerId),
|
||||
getContainerHealth(redisContainerId),
|
||||
])
|
||||
|
||||
return postgresStatus === 'healthy' && redisStatus === 'healthy'
|
||||
},
|
||||
description: 'PostgreSQL and Redis health checks',
|
||||
intervalMs: 2_000,
|
||||
timeoutMs: 240_000,
|
||||
}),
|
||||
})
|
||||
|
||||
await waitForDependency({
|
||||
description: 'Weaviate readiness',
|
||||
services: ['weaviate'],
|
||||
wait: () => waitForUrl('http://127.0.0.1:8080/v1/.well-known/ready', 120_000, 2_000),
|
||||
})
|
||||
|
||||
await waitForDependency({
|
||||
description: 'sandbox health',
|
||||
services: ['sandbox', 'ssrf_proxy'],
|
||||
wait: () => waitForUrl('http://127.0.0.1:8194/health', 120_000, 2_000),
|
||||
})
|
||||
|
||||
await waitForDependency({
|
||||
description: 'plugin daemon port',
|
||||
services: ['plugin_daemon'],
|
||||
wait: () =>
|
||||
waitForCondition({
|
||||
check: async () => isTcpPortReachable('127.0.0.1', 5002),
|
||||
description: 'plugin daemon port',
|
||||
intervalMs: 2_000,
|
||||
timeoutMs: 120_000,
|
||||
}),
|
||||
})
|
||||
|
||||
console.log('Full middleware stack is ready.')
|
||||
}
|
||||
|
||||
const printUsage = () => {
|
||||
console.log(
|
||||
'Usage: node ./scripts/setup.ts <reset|middleware-up|middleware-down|shellctl-sandbox|agent-backend|api|celery [--queues queues]|web|web-build>',
|
||||
)
|
||||
}
|
||||
|
||||
const main = async () => {
|
||||
const command = process.argv[2]
|
||||
const queuesIndex = process.argv.indexOf('--queues')
|
||||
const queues = queuesIndex === -1 ? undefined : process.argv[queuesIndex + 1]
|
||||
|
||||
switch (command) {
|
||||
case 'agent-backend':
|
||||
await startAgentBackend()
|
||||
return
|
||||
case 'api':
|
||||
await startApi()
|
||||
return
|
||||
case 'celery':
|
||||
await startCelery({ queues })
|
||||
return
|
||||
case 'middleware-down':
|
||||
await stopMiddleware()
|
||||
return
|
||||
case 'middleware-up':
|
||||
await startMiddleware()
|
||||
return
|
||||
case 'reset':
|
||||
await resetState()
|
||||
return
|
||||
case 'shellctl-sandbox':
|
||||
await startShellctlSandbox()
|
||||
return
|
||||
case 'web':
|
||||
await startWeb()
|
||||
return
|
||||
case 'web-build':
|
||||
await ensureWebBuild()
|
||||
return
|
||||
default:
|
||||
printUsage()
|
||||
process.exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
if (isMainModule(import.meta.url)) {
|
||||
void main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user