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,69 @@
|
||||
import type { TestWorld } from '@test/fixtures/kernel'
|
||||
import type { BaseError } from '@/errors/base'
|
||||
import { testContext } from '@test/fixtures/kernel'
|
||||
import { afterEach, expect, it } from 'vite-plus/test'
|
||||
import { ExitCode } from '@/errors/codes'
|
||||
import { commands } from '@/plugins/commands'
|
||||
import { ENV } from '@/plugins/env'
|
||||
import { session } from '@/plugins/session'
|
||||
|
||||
const worlds: TestWorld[] = []
|
||||
afterEach(async () => {
|
||||
for (const w of worlds.splice(0)) await w.stop()
|
||||
})
|
||||
|
||||
it('pins a workspace the account belongs to and names it', async () => {
|
||||
const w = await testContext({ login: true, argv: ['use', 'workspace', 'ws-2'] })
|
||||
worlds.push(w)
|
||||
expect(await (await w.ctx.get(commands)).run()).toBe(0)
|
||||
expect(JSON.parse(w.io.outBuf())).toEqual({ workspace_id: 'ws-2', name: 'Other' })
|
||||
expect(await (await w.ctx.get(session)).workspaceId()).toBe('ws-2')
|
||||
})
|
||||
|
||||
it('describe workspace with no id shows the pinned workspace', async () => {
|
||||
const w = await testContext({ login: true, argv: ['describe', 'workspace'] })
|
||||
worlds.push(w)
|
||||
expect(await (await w.ctx.get(commands)).run()).toBe(0)
|
||||
expect(JSON.parse(w.io.outBuf())).toMatchObject({ id: 'ws-1', name: 'Default' })
|
||||
})
|
||||
|
||||
it('refuses a workspace the account does not belong to and keeps the old pin', async () => {
|
||||
const w = await testContext({ login: true, argv: ['use', 'workspace', 'ws-9'] })
|
||||
worlds.push(w)
|
||||
const err = await (await w.ctx.get(commands)).run().catch((e: unknown) => e)
|
||||
expect(err).toMatchObject({ message: 'workspace not found' })
|
||||
expect((err as BaseError).exit()).toBe(ExitCode.Generic)
|
||||
expect(await (await w.ctx.get(session)).workspaceId()).toBe('ws-1')
|
||||
})
|
||||
|
||||
it('refuses under an env login and without a login', async () => {
|
||||
const a = await testContext({ login: false, env: true, argv: ['use', 'workspace', 'ws-9'] })
|
||||
worlds.push(a)
|
||||
await expect((await a.ctx.get(commands)).run()).rejects.toMatchObject({
|
||||
code: 'usage_invalid_flag',
|
||||
message: 'the login comes from DIFY_TOKEN; set DIFY_WORKSPACE_ID instead',
|
||||
})
|
||||
const b = await testContext({ login: false, argv: ['use', 'workspace', 'ws-9'] })
|
||||
worlds.push(b)
|
||||
await expect((await b.ctx.get(commands)).run()).rejects.toMatchObject({ code: 'not_logged_in' })
|
||||
})
|
||||
|
||||
it('refuses an empty workspace id, exit 2, and changes nothing', async () => {
|
||||
const w = await testContext({ login: true, argv: ['use', 'workspace', ''] })
|
||||
worlds.push(w)
|
||||
await expect((await w.ctx.get(commands)).run()).rejects.toMatchObject({
|
||||
code: 'input_invalid',
|
||||
details: [expect.objectContaining({ loc: ['workspace_id'] })],
|
||||
})
|
||||
expect(await (await w.ctx.get(session)).workspaceId()).toBe('ws-1')
|
||||
})
|
||||
|
||||
it('refuses when DIFY_WORKSPACE_ID overrides the pin', async () => {
|
||||
const w = await testContext({ login: true, argv: ['use', 'workspace', 'ws-9'] })
|
||||
worlds.push(w)
|
||||
process.env[ENV.WorkspaceId] = 'ws-9'
|
||||
await expect((await w.ctx.get(commands)).run()).rejects.toMatchObject({
|
||||
code: 'usage_invalid_flag',
|
||||
message: 'DIFY_WORKSPACE_ID is set; unset it to pin locally',
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { CommandContext } from '@/plugins/base'
|
||||
import { z } from 'zod'
|
||||
import { assertNotEnvLogin } from '@/auth/logout'
|
||||
import { LOCAL_FILES } from '@/call/files'
|
||||
import { parseJsonBody } from '@/call/json-body'
|
||||
import { buildRequest } from '@/call/request'
|
||||
import { BaseError } from '@/errors/base'
|
||||
import { ErrorCode } from '@/errors/codes'
|
||||
import { Command } from '@/plugins/commands/command'
|
||||
import { env } from '@/plugins/env'
|
||||
import { http } from '@/plugins/http'
|
||||
import { session } from '@/plugins/session'
|
||||
|
||||
const INPUT = z.object({ workspace_id: z.string().min(1) })
|
||||
const WORKSPACE = z.object({ id: z.string(), name: z.string() })
|
||||
|
||||
const WORKSPACE_DESCRIBE_OP = 'describe.workspace'
|
||||
const ENV_WORKSPACE_ID_MESSAGE = 'DIFY_WORKSPACE_ID is set; unset it to pin locally'
|
||||
const ENV_LOGIN_MESSAGE = 'the login comes from DIFY_TOKEN; set DIFY_WORKSPACE_ID instead'
|
||||
const MALFORMED_WORKSPACE_MESSAGE = 'describe workspace response has no id and name'
|
||||
|
||||
export default class WorkspaceUse extends Command<typeof INPUT> {
|
||||
static override summary =
|
||||
'Pin this device to a workspace; the web console keeps its own current workspace'
|
||||
static override effect = 'write' as const
|
||||
static override input = INPUT
|
||||
static override positional = ['workspace_id'] as const
|
||||
|
||||
async run(input: z.infer<typeof INPUT>, ctx: CommandContext) {
|
||||
const envService = await ctx.get(env)
|
||||
if (envService.workspaceId !== undefined) {
|
||||
throw new BaseError({ code: ErrorCode.UsageInvalidFlag, message: ENV_WORKSPACE_ID_MESSAGE })
|
||||
}
|
||||
|
||||
const sessionService = await ctx.get(session)
|
||||
const login = await sessionService.require()
|
||||
assertNotEnvLogin(sessionService.fromEnv, ENV_LOGIN_MESSAGE)
|
||||
|
||||
// Check membership first so a mistyped id is never pinned.
|
||||
const httpService = await ctx.get(http)
|
||||
const res = await httpService.request(async (cat) =>
|
||||
buildRequest(
|
||||
cat.opOrThrow(WORKSPACE_DESCRIBE_OP),
|
||||
{ workspace_id: input.workspace_id },
|
||||
LOCAL_FILES,
|
||||
),
|
||||
)
|
||||
const workspace = WORKSPACE.safeParse(await parseJsonBody(res))
|
||||
if (!workspace.success)
|
||||
throw new BaseError({ code: ErrorCode.ServerError, message: MALFORMED_WORKSPACE_MESSAGE })
|
||||
|
||||
await sessionService.save({ ...login, workspaceId: workspace.data.id })
|
||||
|
||||
return { workspace_id: workspace.data.id, name: workspace.data.name }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user