mirror of
https://github.com/langgenius/dify.git
synced 2026-07-28 07:28:34 +08:00
Signed-off-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: 盐粒 Yanli <mail@yanli.one> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: 林玮 (Jade Lin) <linw1995@icloud.com>
112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
import type { AgentProviderTool, AgentSoulConfigFormState, AgentTool } from '../form-state'
|
|
import type { DraftFieldUpdate } from './utils'
|
|
import { atom, useSetAtom } from 'jotai'
|
|
import { useCallback } from 'react'
|
|
import { syncCliToolReferenceLabels } from '../reference-labels'
|
|
import { agentComposerDraftAtom } from '../store'
|
|
import { resolveDraftFieldUpdate } from './utils'
|
|
|
|
export const agentComposerToolsAtom = atom(
|
|
get => get(agentComposerDraftAtom).tools,
|
|
(get, set, toolsUpdate: DraftFieldUpdate<AgentTool[]>) => {
|
|
const draft = get(agentComposerDraftAtom)
|
|
const tools = resolveDraftFieldUpdate(draft.tools, toolsUpdate)
|
|
|
|
set(agentComposerDraftAtom, {
|
|
...draft,
|
|
prompt: syncCliToolReferenceLabels({
|
|
prompt: draft.prompt,
|
|
currentTools: draft.tools,
|
|
nextTools: tools,
|
|
}),
|
|
tools,
|
|
})
|
|
},
|
|
)
|
|
|
|
export const agentComposerToolSettingsAtom = atom(
|
|
get => get(agentComposerDraftAtom).toolSettings,
|
|
(get, set, toolSettingsUpdate: DraftFieldUpdate<Record<string, Record<string, unknown>>>) => {
|
|
const draft = get(agentComposerDraftAtom)
|
|
|
|
set(agentComposerDraftAtom, {
|
|
...draft,
|
|
toolSettings: resolveDraftFieldUpdate(draft.toolSettings, toolSettingsUpdate),
|
|
})
|
|
},
|
|
)
|
|
|
|
const omitToolSettings = (
|
|
toolSettings: AgentSoulConfigFormState['toolSettings'],
|
|
actionIds: string[],
|
|
) => {
|
|
const nextToolSettings = { ...toolSettings }
|
|
|
|
actionIds.forEach((actionId) => {
|
|
delete nextToolSettings[actionId]
|
|
})
|
|
|
|
return nextToolSettings
|
|
}
|
|
|
|
export function useRemoveProviderTool() {
|
|
const setDraft = useSetAtom(agentComposerDraftAtom)
|
|
|
|
return useCallback((toolId: string) => {
|
|
setDraft((draft) => {
|
|
const toolToRemove = draft.tools.find(tool => tool.kind === 'provider' && tool.id === toolId)
|
|
const actionIds = toolToRemove?.kind === 'provider'
|
|
? toolToRemove.actions.map(action => action.id)
|
|
: []
|
|
|
|
return {
|
|
...draft,
|
|
tools: draft.tools.filter(tool => tool.id !== toolId),
|
|
toolSettings: omitToolSettings(draft.toolSettings, actionIds),
|
|
}
|
|
})
|
|
}, [setDraft])
|
|
}
|
|
|
|
export function useRemoveProviderToolAction() {
|
|
const setDraft = useSetAtom(agentComposerDraftAtom)
|
|
|
|
return useCallback((toolId: string, actionId: string) => {
|
|
setDraft(draft => ({
|
|
...draft,
|
|
tools: draft.tools.flatMap((tool) => {
|
|
if (tool.kind !== 'provider' || tool.id !== toolId)
|
|
return [tool]
|
|
|
|
const nextActions = tool.actions.filter(action => action.id !== actionId)
|
|
return nextActions.length > 0
|
|
? [{ ...tool, actions: nextActions }]
|
|
: []
|
|
}),
|
|
toolSettings: omitToolSettings(draft.toolSettings, [actionId]),
|
|
}))
|
|
}, [setDraft])
|
|
}
|
|
|
|
export function useSetProviderToolCredential() {
|
|
const setTools = useSetAtom(agentComposerToolsAtom)
|
|
|
|
return useCallback((toolId: string, credentialId?: string, credentialType?: AgentProviderTool['credentialType']) => {
|
|
setTools(tools => tools.map((tool) => {
|
|
if (tool.kind !== 'provider' || tool.id !== toolId)
|
|
return tool
|
|
|
|
const nextCredentialType = credentialType === 'oauth2' || tool.credentialType === 'oauth2'
|
|
? 'oauth2'
|
|
: 'api-key'
|
|
|
|
return {
|
|
...tool,
|
|
credentialId,
|
|
credentialType: nextCredentialType,
|
|
credentialVariant: 'authorized',
|
|
}
|
|
}))
|
|
}, [setTools])
|
|
}
|