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,20 @@
|
||||
import type { CreateAppPayload, PostAppsResponse } from '@dify/contracts/api/console/apps/types.gen'
|
||||
import type { ConsoleClient } from './console-client.ts'
|
||||
import { assertE2EResourceName, createE2EResourceName } from '../naming.ts'
|
||||
|
||||
export async function createTestApp(
|
||||
client: ConsoleClient,
|
||||
name = createE2EResourceName('App'),
|
||||
mode: CreateAppPayload['mode'] = 'workflow',
|
||||
): Promise<PostAppsResponse> {
|
||||
assertE2EResourceName(name, 'App')
|
||||
const body = {
|
||||
name,
|
||||
mode,
|
||||
icon_type: 'emoji',
|
||||
icon: '🤖',
|
||||
icon_background: '#FFEAD5',
|
||||
} satisfies CreateAppPayload
|
||||
|
||||
return client.apps.post({ body })
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { ContractRouterClient } from '@orpc/contract'
|
||||
import type { JsonifiedClient } from '@orpc/openapi-client'
|
||||
import type { APIRequestContext } from '@playwright/test'
|
||||
import type { ConsoleClientContext } from './playwright-fetch.ts'
|
||||
import { consoleRouterContract } from '@dify/contracts/api/console/router.gen'
|
||||
import { createORPCClient } from '@orpc/client'
|
||||
import { RequestValidationPlugin, ResponseValidationPlugin } from '@orpc/contract/plugins'
|
||||
import { OpenAPILink } from '@orpc/openapi-client/fetch'
|
||||
import { apiURL } from '../../test-env.ts'
|
||||
import { createPlaywrightFetch } from './playwright-fetch.ts'
|
||||
|
||||
type ConsoleRequestContext = Pick<APIRequestContext, 'fetch' | 'storageState'>
|
||||
|
||||
export type ConsoleClient = JsonifiedClient<
|
||||
ContractRouterClient<typeof consoleRouterContract, ConsoleClientContext>
|
||||
>
|
||||
|
||||
export type CreateConsoleClientOptions = {
|
||||
apiBaseURL?: string
|
||||
requestContext: ConsoleRequestContext
|
||||
requireCsrfToken?: boolean
|
||||
}
|
||||
|
||||
const getCsrfToken = async (requestContext: ConsoleRequestContext) => {
|
||||
const state = await requestContext.storageState()
|
||||
return state.cookies.find((cookie) => cookie.name.endsWith('csrf_token'))?.value
|
||||
}
|
||||
|
||||
export function createConsoleClient({
|
||||
apiBaseURL = apiURL,
|
||||
requestContext,
|
||||
requireCsrfToken = true,
|
||||
}: CreateConsoleClientOptions): ConsoleClient {
|
||||
const link = new OpenAPILink<ConsoleClientContext>(consoleRouterContract, {
|
||||
fetch: createPlaywrightFetch(requestContext),
|
||||
headers: async () => {
|
||||
const headers = new Headers({ Accept: 'application/json' })
|
||||
const csrfToken = await getCsrfToken(requestContext)
|
||||
if (!csrfToken && requireCsrfToken)
|
||||
throw new Error('The Console API client requires an authenticated CSRF token.')
|
||||
if (csrfToken) headers.set('X-CSRF-Token', csrfToken)
|
||||
return headers
|
||||
},
|
||||
plugins: [
|
||||
new RequestValidationPlugin<ConsoleClientContext>(consoleRouterContract),
|
||||
new ResponseValidationPlugin<ConsoleClientContext>(consoleRouterContract),
|
||||
],
|
||||
url: new URL('/console/api/', apiBaseURL).toString(),
|
||||
})
|
||||
|
||||
return createORPCClient<ConsoleClient>(link)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { request } from '@playwright/test'
|
||||
import { authStatePath } from '../../fixtures/auth.ts'
|
||||
import { apiURL } from '../../test-env.ts'
|
||||
import { createConsoleClient } from './console-client.ts'
|
||||
|
||||
export async function createStandaloneConsoleSession() {
|
||||
const requestContext = await request.newContext({
|
||||
baseURL: apiURL,
|
||||
storageState: authStatePath,
|
||||
})
|
||||
|
||||
return {
|
||||
client: createConsoleClient({ requestContext }),
|
||||
dispose: () => requestContext.dispose(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import type { OpenAPILinkOptions } from '@orpc/openapi-client/fetch'
|
||||
import type { APIRequestContext } from '@playwright/test'
|
||||
import { Buffer } from 'node:buffer'
|
||||
|
||||
export type ConsoleClientContext = {
|
||||
timeoutMs?: number
|
||||
}
|
||||
|
||||
type PlaywrightRequestContext = Pick<APIRequestContext, 'fetch'>
|
||||
type OpenAPIFetch = NonNullable<OpenAPILinkOptions<ConsoleClientContext>['fetch']>
|
||||
|
||||
const defaultRequestTimeoutMs = 30_000
|
||||
const bodylessResponseStatuses = new Set([204, 205, 304])
|
||||
|
||||
export function createPlaywrightFetch(requestContext: PlaywrightRequestContext): OpenAPIFetch {
|
||||
return async (request, _init, options, path) => {
|
||||
request.signal.throwIfAborted()
|
||||
|
||||
const headers = Object.fromEntries(request.headers.entries())
|
||||
delete headers['content-length']
|
||||
|
||||
const data = request.body ? Buffer.from(await request.arrayBuffer()) : undefined
|
||||
const apiResponse = await requestContext.fetch(request.url, {
|
||||
...(data === undefined ? {} : { data }),
|
||||
failOnStatusCode: false,
|
||||
headers,
|
||||
maxRedirects: 0,
|
||||
method: request.method,
|
||||
timeout: options.context.timeoutMs ?? defaultRequestTimeoutMs,
|
||||
})
|
||||
|
||||
try {
|
||||
const status = apiResponse.status()
|
||||
if (status >= 300 && status < 400) {
|
||||
const location = apiResponse.headers().location
|
||||
throw new Error(
|
||||
`Console API ${path.join('.')} redirected with ${status}${location ? ` to ${location}` : ''}.`,
|
||||
)
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers()
|
||||
for (const { name, value } of apiResponse.headersArray()) responseHeaders.append(name, value)
|
||||
responseHeaders.delete('content-encoding')
|
||||
responseHeaders.delete('content-length')
|
||||
responseHeaders.delete('transfer-encoding')
|
||||
|
||||
const body =
|
||||
request.method === 'HEAD' || bodylessResponseStatuses.has(status)
|
||||
? null
|
||||
: Uint8Array.from(await apiResponse.body())
|
||||
|
||||
return new Response(body, {
|
||||
headers: responseHeaders,
|
||||
status,
|
||||
statusText: apiResponse.statusText(),
|
||||
})
|
||||
} finally {
|
||||
await apiResponse.dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { AppDetailWithSite } from '@dify/contracts/api/console/apps/types.gen'
|
||||
|
||||
export function getAppSiteURL({ mode, site }: AppDetailWithSite): string {
|
||||
if (!site?.app_base_url || !site.access_token)
|
||||
throw new Error('App detail does not include a Web App URL.')
|
||||
|
||||
const webAppMode = (() => {
|
||||
if (mode === 'completion' || mode === 'workflow') return mode
|
||||
if (mode === 'advanced-chat' || mode === 'agent-chat' || mode === 'chat') return 'chat'
|
||||
throw new Error(`Unsupported Web App mode: ${mode}`)
|
||||
})()
|
||||
|
||||
return `${site.app_base_url}/${webAppMode}/${site.access_token}`
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import type { SyncDraftWorkflowPayload } from '@dify/contracts/api/console/apps/types.gen'
|
||||
import type { ConsoleClient } from './console-client.ts'
|
||||
|
||||
export async function syncMinimalWorkflowDraft(
|
||||
client: ConsoleClient,
|
||||
appId: string,
|
||||
): Promise<void> {
|
||||
const body = {
|
||||
graph: {
|
||||
nodes: [
|
||||
{
|
||||
id: '1',
|
||||
type: 'custom',
|
||||
position: { x: 80, y: 282 },
|
||||
data: { id: '1', type: 'start', title: 'Start', variables: [] },
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
viewport: { x: 0, y: 0, zoom: 1 },
|
||||
},
|
||||
features: {},
|
||||
conversation_variables: [],
|
||||
} satisfies SyncDraftWorkflowPayload
|
||||
await client.apps.byAppId.workflows.draft.post({ body, params: { app_id: appId } })
|
||||
}
|
||||
|
||||
export async function syncRunnableWorkflowDraft(
|
||||
client: ConsoleClient,
|
||||
appId: string,
|
||||
{
|
||||
mode = 'workflow',
|
||||
variables = [],
|
||||
}: {
|
||||
mode?: 'workflow' | 'advanced-chat'
|
||||
variables?: Record<string, unknown>[]
|
||||
} = {},
|
||||
): Promise<void> {
|
||||
const body = {
|
||||
graph: {
|
||||
nodes: [
|
||||
{
|
||||
id: 'start',
|
||||
type: 'custom',
|
||||
position: { x: 80, y: 282 },
|
||||
data: { id: 'start', type: 'start', title: 'Start', variables },
|
||||
},
|
||||
{
|
||||
id: 'end',
|
||||
type: 'custom',
|
||||
position: { x: 480, y: 282 },
|
||||
data:
|
||||
mode === 'advanced-chat'
|
||||
? { id: 'end', type: 'answer', title: 'Answer', answer: 'E2E response' }
|
||||
: {
|
||||
id: 'end',
|
||||
type: 'end',
|
||||
title: 'End',
|
||||
outputs: [{ variable: 'result', value_selector: ['sys', 'workflow_run_id'] }],
|
||||
},
|
||||
},
|
||||
],
|
||||
edges: [
|
||||
{
|
||||
id: 'start-end',
|
||||
type: 'custom',
|
||||
source: 'start',
|
||||
target: 'end',
|
||||
sourceHandle: 'source',
|
||||
targetHandle: 'target',
|
||||
},
|
||||
],
|
||||
viewport: { x: 0, y: 0, zoom: 1 },
|
||||
},
|
||||
features: {},
|
||||
conversation_variables: [],
|
||||
} satisfies SyncDraftWorkflowPayload
|
||||
await client.apps.byAppId.workflows.draft.post({ body, params: { app_id: appId } })
|
||||
}
|
||||
Reference in new issue
Block a user