fix(agent-v2): preserve oauth2 credential refs when converting tool c… (#38303)

This commit is contained in:
林玮 (Jade Lin) 2026-07-02 15:07:42 +08:00 committed by GitHub
parent 826b259d9f
commit 1af535a173
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 5 deletions

View File

@ -382,6 +382,79 @@ describe('agent composer store conversions', () => {
])
})
it('should preserve oauth2 credential references when saving tool config', () => {
const baseConfig = {
tools: {
dify_tools: [
{
provider: 'google',
provider_id: 'google',
provider_type: 'builtin',
tool_name: 'search',
credential_type: 'oauth2',
credential_ref: {
id: 'credential-oauth',
provider: 'google',
type: 'provider',
},
},
],
},
} satisfies AgentSoulConfig
const formState = agentSoulConfigToFormState(baseConfig)
const publishConfig = formStateToAgentSoulConfig({ baseConfig, formState })
expect(formState.tools).toEqual([
expect.objectContaining({
credentialId: 'credential-oauth',
credentialType: 'oauth2',
credentialVariant: 'authorized',
}),
])
expect(publishConfig.tools?.dify_tools).toEqual([
expect.objectContaining({
credential_type: 'oauth2',
credential_ref: {
id: 'credential-oauth',
provider: 'google',
type: 'provider',
},
}),
])
})
it('should not save credentialed tool config without a credential reference', () => {
const baseConfig = {
tools: {
dify_tools: [
{
provider: 'google',
provider_id: 'google',
provider_type: 'builtin',
tool_name: 'search',
credential_type: 'oauth2',
},
],
},
} satisfies AgentSoulConfig
const formState = agentSoulConfigToFormState(baseConfig)
const publishConfig = formStateToAgentSoulConfig({ baseConfig, formState })
expect(formState.tools).toEqual([
expect.objectContaining({
credentialId: undefined,
credentialType: 'oauth2',
credentialVariant: 'unauthorized',
}),
])
expect(publishConfig.tools?.dify_tools).toEqual([
expect.objectContaining({
credential_type: 'unauthorized',
credential_ref: undefined,
}),
])
})
it('should derive model plugin_id from the selected provider when publishing', () => {
const publishConfig = formStateToAgentSoulConfig({
baseConfig: {

View File

@ -171,13 +171,18 @@ const toToolRuntimeParameters = (settings: Record<string, unknown> | undefined)
const getDifyToolActionId = (tool: AgentSoulDifyToolConfig) => `${tool.provider_id ?? tool.provider ?? tool.plugin_id ?? 'provider'}:${tool.tool_name ?? tool.name ?? 'tool'}`
const toCredentialVariant = (credentialType: AgentSoulDifyToolConfig['credential_type']) => {
if (credentialType === 'api-key')
return 'authorized' as const
const toCredentialVariant = (tool: AgentSoulDifyToolConfig) => {
const credentialType = tool.credential_type
if (credentialType === 'unauthorized')
return 'unauthorized' as const
if (tool.credential_ref?.id)
return 'authorized' as const
if (credentialType === 'api-key' || credentialType === 'oauth2')
return 'unauthorized' as const
return 'none' as const
}
@ -222,7 +227,7 @@ const toProviderToolFormState = (config?: AgentSoulConfig): {
? 'agentDetail.configure.tools.credential.authOne'
: undefined,
credentialType: tool.credential_type,
credentialVariant: toCredentialVariant(tool.credential_type),
credentialVariant: toCredentialVariant(tool),
actions: [action],
})
}
@ -240,6 +245,10 @@ const toDifyToolConfigs = (
if (tool.kind !== 'provider')
return []
const credentialType = tool.credentialId
? tool.credentialType ?? 'api-key'
: 'unauthorized'
return tool.actions.map(action => ({
enabled: true,
provider: tool.name,
@ -247,7 +256,7 @@ const toDifyToolConfigs = (
provider_type: tool.providerType ?? 'builtin',
tool_name: action.toolName,
runtime_parameters: toToolRuntimeParameters(toolSettings[action.id]),
credential_type: tool.credentialType ?? (tool.credentialVariant === 'authorized' ? 'api-key' as const : 'unauthorized' as const),
credential_type: credentialType,
credential_ref: tool.credentialId
? {
id: tool.credentialId,