mirror of
https://github.com/langgenius/dify.git
synced 2026-07-16 01:48:40 +08:00
230 lines
6.4 KiB
TypeScript
230 lines
6.4 KiB
TypeScript
import { createApiContext, expectApiResponseOK, setAppSiteEnabled } from './api'
|
|
|
|
export type AgentSeed = {
|
|
app_id?: string
|
|
backing_app_id?: string
|
|
description?: string
|
|
enable_site?: boolean
|
|
id: string
|
|
name: string
|
|
role?: string
|
|
site?: {
|
|
access_token?: string | null
|
|
app_base_url?: string | null
|
|
code?: string | null
|
|
} | null
|
|
}
|
|
|
|
export type AgentSoulConfig = Record<string, unknown>
|
|
|
|
export type AgentComposerResponse = {
|
|
agent_soul?: AgentSoulConfig
|
|
}
|
|
|
|
export type AgentBuildDraftResponse = {
|
|
agent_soul: AgentSoulConfig
|
|
draft: Record<string, unknown>
|
|
variant: 'agent_app'
|
|
}
|
|
|
|
export type AgentApiAccess = {
|
|
api_key_count: number
|
|
api_reference_url: string
|
|
endpoint: string
|
|
enabled: boolean
|
|
files_upload_endpoint: string
|
|
}
|
|
|
|
export type AgentApiKey = {
|
|
id: string
|
|
token?: string
|
|
}
|
|
|
|
export const defaultAgentSoulConfig: AgentSoulConfig = {
|
|
prompt: {
|
|
system_prompt: 'You are a Dify Agent E2E test assistant.',
|
|
},
|
|
}
|
|
|
|
export const getAgentConfigurePath = (agentId: string) => `/roster/agent/${agentId}/configure`
|
|
export const getAgentAccessPath = (agentId: string) => `/roster/agent/${agentId}/access`
|
|
|
|
export async function createTestAgent({
|
|
description = 'Created by Dify E2E.',
|
|
name = `E2E Agent ${Date.now()}`,
|
|
role = 'E2E test assistant',
|
|
}: {
|
|
description?: string
|
|
name?: string
|
|
role?: string
|
|
} = {}): Promise<AgentSeed> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.post('/console/api/agent', {
|
|
data: {
|
|
description,
|
|
icon: '🤖',
|
|
icon_background: '#FFEAD5',
|
|
icon_type: 'emoji',
|
|
name,
|
|
role,
|
|
},
|
|
})
|
|
await expectApiResponseOK(response, 'Create Agent v2 test agent')
|
|
return (await response.json()) as AgentSeed
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function getTestAgent(agentId: string): Promise<AgentSeed> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.get(`/console/api/agent/${agentId}`)
|
|
await expectApiResponseOK(response, `Get Agent v2 test agent ${agentId}`)
|
|
return (await response.json()) as AgentSeed
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function deleteTestAgent(agentId: string): Promise<void> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.delete(`/console/api/agent/${agentId}`)
|
|
await expectApiResponseOK(response, `Delete Agent v2 test agent ${agentId}`)
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function saveAgentComposerDraft(
|
|
agentId: string,
|
|
agentSoul: AgentSoulConfig = defaultAgentSoulConfig,
|
|
): Promise<AgentComposerResponse> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.put(`/console/api/agent/${agentId}/composer`, {
|
|
data: {
|
|
agent_soul: agentSoul,
|
|
save_strategy: 'save_to_current_version',
|
|
variant: 'agent_app',
|
|
},
|
|
})
|
|
await expectApiResponseOK(response, `Save Agent v2 composer draft for ${agentId}`)
|
|
return (await response.json()) as AgentComposerResponse
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function checkoutAgentBuildDraft(agentId: string): Promise<AgentBuildDraftResponse> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.post(`/console/api/agent/${agentId}/build-draft/checkout`, {
|
|
data: { force: true },
|
|
})
|
|
await expectApiResponseOK(response, `Checkout Agent v2 build draft for ${agentId}`)
|
|
return (await response.json()) as AgentBuildDraftResponse
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function discardAgentBuildDraft(agentId: string): Promise<void> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.delete(`/console/api/agent/${agentId}/build-draft`)
|
|
await expectApiResponseOK(response, `Discard Agent v2 build draft for ${agentId}`)
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function publishAgent(agentId: string, versionNote = 'E2E publish'): Promise<void> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.post(`/console/api/agent/${agentId}/publish`, {
|
|
data: { version_note: versionNote },
|
|
})
|
|
await expectApiResponseOK(response, `Publish Agent v2 test agent ${agentId}`)
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function enableAgentSiteAndGetURL(agentId: string): Promise<string> {
|
|
const agent = await getTestAgent(agentId)
|
|
const appId = agent.app_id ?? agent.backing_app_id
|
|
if (!appId)
|
|
throw new Error(`Agent v2 ${agentId} does not expose a backing app ID.`)
|
|
|
|
const appDetail = await setAppSiteEnabled(appId, true)
|
|
const token = agent.site?.access_token ?? agent.site?.code ?? appDetail.site.access_token
|
|
const baseURL = agent.site?.app_base_url ?? appDetail.site.app_base_url
|
|
|
|
return `${baseURL.replace(/\/$/, '')}/agent/${token}`
|
|
}
|
|
|
|
export async function getAgentApiAccess(agentId: string): Promise<AgentApiAccess> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.get(`/console/api/agent/${agentId}/api-access`)
|
|
await expectApiResponseOK(response, `Get Agent v2 API access for ${agentId}`)
|
|
return (await response.json()) as AgentApiAccess
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function setAgentApiAccess(
|
|
agentId: string,
|
|
enabled: boolean,
|
|
): Promise<AgentApiAccess> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.post(`/console/api/agent/${agentId}/api-enable`, {
|
|
data: { enable_api: enabled },
|
|
})
|
|
await expectApiResponseOK(
|
|
response,
|
|
`${enabled ? 'Enable' : 'Disable'} Agent v2 API access for ${agentId}`,
|
|
)
|
|
return (await response.json()) as AgentApiAccess
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function createAgentApiKey(agentId: string): Promise<AgentApiKey> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.post(`/console/api/agent/${agentId}/api-keys`)
|
|
await expectApiResponseOK(response, `Create Agent v2 API key for ${agentId}`)
|
|
return (await response.json()) as AgentApiKey
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|
|
|
|
export async function deleteAgentApiKey(agentId: string, apiKeyId: string): Promise<void> {
|
|
const ctx = await createApiContext()
|
|
try {
|
|
const response = await ctx.delete(`/console/api/agent/${agentId}/api-keys/${apiKeyId}`)
|
|
await expectApiResponseOK(response, `Delete Agent v2 API key ${apiKeyId} for ${agentId}`)
|
|
}
|
|
finally {
|
|
await ctx.dispose()
|
|
}
|
|
}
|