mirror of
https://github.com/langgenius/dify.git
synced 2026-06-26 23:01:11 +08:00
chore: split hooks frorm agent
This commit is contained in:
parent
b1feacca6b
commit
ac80d95aed
93
web/features/agent-v2/agent-detail/configure/hooks.ts
Normal file
93
web/features/agent-v2/agent-detail/configure/hooks.ts
Normal file
@ -0,0 +1,93 @@
|
||||
'use client'
|
||||
|
||||
import type { AgentConfigSnapshotDetailResponse, AgentSoulConfig } from '@dify/contracts/api/console/agent/types.gen'
|
||||
import { skipToken, useQuery } from '@tanstack/react-query'
|
||||
import { useAtom, useAtomValue } from 'jotai'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { useDefaultModel, useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
import { agentComposerAppFeaturesAtom } from '@/features/agent-v2/agent-composer/store-modules/app-features'
|
||||
import { agentComposerModelAtom } from '@/features/agent-v2/agent-composer/store-modules/model'
|
||||
import { consoleQuery } from '@/service/client'
|
||||
|
||||
export function useAgentConfigureData(agentId: string, selectedVersionId: string | null) {
|
||||
const agentQuery = useQuery(consoleQuery.agent.byAgentId.get.queryOptions({
|
||||
input: {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
},
|
||||
}))
|
||||
const composerQuery = useQuery(consoleQuery.agent.byAgentId.composer.get.queryOptions({
|
||||
input: {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
},
|
||||
}))
|
||||
const publishedVersionId = composerQuery.data?.active_config_snapshot?.id
|
||||
const shouldLoadPublishedVersion = !selectedVersionId && !composerQuery.data?.agent_soul
|
||||
const versionIdToLoad = selectedVersionId ?? (shouldLoadPublishedVersion ? publishedVersionId : undefined)
|
||||
const shouldLoadVersion = !!versionIdToLoad
|
||||
const versionQuery = useQuery(consoleQuery.agent.byAgentId.versions.byVersionId.get.queryOptions({
|
||||
input: versionIdToLoad
|
||||
? {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
version_id: versionIdToLoad,
|
||||
},
|
||||
}
|
||||
: skipToken,
|
||||
}))
|
||||
const versionDetail = versionQuery.data as AgentConfigSnapshotDetailResponse | undefined
|
||||
const activeVersionId = selectedVersionId ?? (shouldLoadPublishedVersion ? publishedVersionId : null)
|
||||
const activeConfigSnapshot = selectedVersionId ? versionDetail : (composerQuery.data?.active_config_snapshot ?? versionDetail)
|
||||
const agentSoulConfig = selectedVersionId ? versionDetail?.config_snapshot : (composerQuery.data?.agent_soul ?? versionDetail?.config_snapshot)
|
||||
|
||||
return {
|
||||
agentQuery,
|
||||
composerQuery,
|
||||
versionQuery,
|
||||
shouldLoadVersion,
|
||||
selectedVersionId,
|
||||
activeVersionId,
|
||||
activeConfigSnapshot,
|
||||
agentSoulConfig,
|
||||
}
|
||||
}
|
||||
|
||||
export function useAgentConfigureModelOptions() {
|
||||
const [model, setModel] = useAtom(agentComposerModelAtom)
|
||||
const {
|
||||
data: defaultTextGenerationModel,
|
||||
} = useDefaultModel(ModelTypeEnum.textGeneration)
|
||||
const defaultModel = defaultTextGenerationModel
|
||||
? {
|
||||
provider: defaultTextGenerationModel.provider.provider,
|
||||
model: defaultTextGenerationModel.model,
|
||||
}
|
||||
: undefined
|
||||
const currentModel = model ?? defaultModel
|
||||
const {
|
||||
textGenerationModelList,
|
||||
} = useTextGenerationCurrentProviderAndModelAndModelList(currentModel)
|
||||
|
||||
return {
|
||||
currentModel,
|
||||
setConfigureModel: setModel,
|
||||
textGenerationModelList,
|
||||
}
|
||||
}
|
||||
|
||||
export function useAgentPreviewSoulConfig(
|
||||
agentSoulConfig: AgentSoulConfig | undefined,
|
||||
) {
|
||||
const draftAppFeatures = useAtomValue(agentComposerAppFeaturesAtom)
|
||||
|
||||
if (!agentSoulConfig || !draftAppFeatures)
|
||||
return agentSoulConfig
|
||||
|
||||
return {
|
||||
...agentSoulConfig,
|
||||
app_features: draftAppFeatures,
|
||||
}
|
||||
}
|
||||
@ -1,23 +1,17 @@
|
||||
'use client'
|
||||
|
||||
import type { AgentConfigSnapshotDetailResponse, AgentIconType, AgentSoulConfig } from '@dify/contracts/api/console/agent/types.gen'
|
||||
import { skipToken, useQuery } from '@tanstack/react-query'
|
||||
import { useAtom, useAtomValue } from 'jotai'
|
||||
import type { AgentIconType, AgentSoulConfig } from '@dify/contracts/api/console/agent/types.gen'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Loading from '@/app/components/base/loading'
|
||||
import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { useDefaultModel, useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
import { AgentComposerProvider } from '@/features/agent-v2/agent-composer/provider'
|
||||
import { useHydrateAgentSoulConfigDraft } from '@/features/agent-v2/agent-composer/store'
|
||||
import { agentComposerAppFeaturesAtom } from '@/features/agent-v2/agent-composer/store-modules/app-features'
|
||||
import { agentComposerModelAtom } from '@/features/agent-v2/agent-composer/store-modules/model'
|
||||
import { consoleQuery } from '@/service/client'
|
||||
import { AgentOrchestratePanel } from './components/orchestrate'
|
||||
import { AgentPreviewChat } from './components/preview/chat'
|
||||
import { AgentChatFeaturesPanel } from './components/preview/chat-features-panel'
|
||||
import { AgentPreviewHeader } from './components/preview/header'
|
||||
import { AgentPreviewVersionsPanel } from './components/preview/versions-panel'
|
||||
import { useAgentConfigureData, useAgentConfigureModelOptions, useAgentPreviewSoulConfig } from './hooks'
|
||||
import { useAgentConfigureSync } from './use-agent-configure-sync'
|
||||
|
||||
type AgentConfigurePageProps = {
|
||||
@ -186,8 +180,7 @@ function AgentPreviewChatWithDraftConfig({
|
||||
}: Omit<Parameters<typeof AgentPreviewChat>[0], 'agentSoulConfig'> & {
|
||||
agentSoulConfig?: AgentSoulConfig
|
||||
}) {
|
||||
const draftAppFeatures = useAtomValue(agentComposerAppFeaturesAtom)
|
||||
const previewAgentSoulConfig = getPreviewAgentSoulConfig(agentSoulConfig, draftAppFeatures)
|
||||
const previewAgentSoulConfig = useAgentPreviewSoulConfig(agentSoulConfig)
|
||||
|
||||
return (
|
||||
<AgentPreviewChat
|
||||
@ -196,85 +189,3 @@ function AgentPreviewChatWithDraftConfig({
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function useAgentConfigureData(agentId: string, selectedVersionId: string | null) {
|
||||
const agentQuery = useQuery(consoleQuery.agent.byAgentId.get.queryOptions({
|
||||
input: {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
},
|
||||
}))
|
||||
const composerQuery = useQuery(consoleQuery.agent.byAgentId.composer.get.queryOptions({
|
||||
input: {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
},
|
||||
}))
|
||||
const publishedVersionId = composerQuery.data?.active_config_snapshot?.id
|
||||
const shouldLoadPublishedVersion = !selectedVersionId && !composerQuery.data?.agent_soul
|
||||
const versionIdToLoad = selectedVersionId ?? (shouldLoadPublishedVersion ? publishedVersionId : undefined)
|
||||
const shouldLoadVersion = !!versionIdToLoad
|
||||
const versionQuery = useQuery(consoleQuery.agent.byAgentId.versions.byVersionId.get.queryOptions({
|
||||
input: versionIdToLoad
|
||||
? {
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
version_id: versionIdToLoad,
|
||||
},
|
||||
}
|
||||
: skipToken,
|
||||
}))
|
||||
const versionDetail = versionQuery.data as AgentConfigSnapshotDetailResponse | undefined
|
||||
const activeVersionId = selectedVersionId ?? (shouldLoadPublishedVersion ? publishedVersionId : null)
|
||||
const activeConfigSnapshot = selectedVersionId ? versionDetail : (composerQuery.data?.active_config_snapshot ?? versionDetail)
|
||||
const agentSoulConfig = selectedVersionId ? versionDetail?.config_snapshot : (composerQuery.data?.agent_soul ?? versionDetail?.config_snapshot)
|
||||
|
||||
return {
|
||||
agentQuery,
|
||||
composerQuery,
|
||||
versionQuery,
|
||||
shouldLoadVersion,
|
||||
selectedVersionId,
|
||||
activeVersionId,
|
||||
activeConfigSnapshot,
|
||||
agentSoulConfig,
|
||||
}
|
||||
}
|
||||
|
||||
function useAgentConfigureModelOptions() {
|
||||
const [model, setModel] = useAtom(agentComposerModelAtom)
|
||||
const {
|
||||
data: defaultTextGenerationModel,
|
||||
} = useDefaultModel(ModelTypeEnum.textGeneration)
|
||||
const defaultModel = defaultTextGenerationModel
|
||||
? {
|
||||
provider: defaultTextGenerationModel.provider.provider,
|
||||
model: defaultTextGenerationModel.model,
|
||||
}
|
||||
: undefined
|
||||
const currentModel = model ?? defaultModel
|
||||
const {
|
||||
textGenerationModelList,
|
||||
} = useTextGenerationCurrentProviderAndModelAndModelList(currentModel)
|
||||
|
||||
return {
|
||||
currentModel,
|
||||
setConfigureModel: setModel,
|
||||
textGenerationModelList,
|
||||
}
|
||||
}
|
||||
|
||||
function getPreviewAgentSoulConfig(
|
||||
agentSoulConfig: AgentSoulConfig | undefined,
|
||||
draftAppFeatures: AgentSoulConfig['app_features'] | undefined,
|
||||
) {
|
||||
if (!agentSoulConfig || !draftAppFeatures)
|
||||
return agentSoulConfig
|
||||
|
||||
return {
|
||||
...agentSoulConfig,
|
||||
app_features: draftAppFeatures,
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,16 +73,24 @@ export function useAgentConfigureSync({
|
||||
const composerMutation = saveStrategy === 'save_as_new_version'
|
||||
? publishComposerMutation
|
||||
: saveComposerMutation
|
||||
const composerState = await composerMutation.mutateAsync({
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
body: {
|
||||
variant: 'agent_app',
|
||||
save_strategy: saveStrategy,
|
||||
agent_soul: configSnapshot,
|
||||
},
|
||||
})
|
||||
let composerState: Awaited<ReturnType<typeof composerMutation.mutateAsync>>
|
||||
|
||||
try {
|
||||
composerState = await composerMutation.mutateAsync({
|
||||
params: {
|
||||
agent_id: agentId,
|
||||
},
|
||||
body: {
|
||||
variant: 'agent_app',
|
||||
save_strategy: saveStrategy,
|
||||
agent_soul: configSnapshot,
|
||||
},
|
||||
})
|
||||
}
|
||||
catch {
|
||||
// Draft sync follows workflow autosave behavior: save failures are silent and keep the local draft intact.
|
||||
return
|
||||
}
|
||||
|
||||
if (saveStrategy === 'save_to_current_version') {
|
||||
setOriginalDraft(agentSoulConfigToFormState(configSnapshot))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user