mirror of
https://github.com/langgenius/dify.git
synced 2026-06-24 04:51:11 +08:00
Co-authored-by: Jingyi-Dify <jingyi.qi@dify.ai> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: Bond Zhu <783504079@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import type { DeclaredOutputConfig } from '@dify/contracts/api/console/apps/types.gen'
|
|
import type { AgentV2NodeType } from './types'
|
|
import type { Var } from '@/app/components/workflow/types'
|
|
import { VarType } from '@/app/components/workflow/types'
|
|
|
|
export const defaultAgentV2DeclaredOutputs: DeclaredOutputConfig[] = [
|
|
{
|
|
name: 'text',
|
|
type: 'string',
|
|
required: false,
|
|
description: 'Free-form text answer.',
|
|
},
|
|
{
|
|
name: 'files',
|
|
type: 'array',
|
|
required: false,
|
|
description: 'Files produced by the agent.',
|
|
array_item: {
|
|
type: 'file',
|
|
},
|
|
},
|
|
{
|
|
name: 'json',
|
|
type: 'object',
|
|
required: false,
|
|
description: 'Free-form JSON object.',
|
|
},
|
|
]
|
|
|
|
const outputTypeLabels: Record<DeclaredOutputConfig['type'], string> = {
|
|
array: 'Array',
|
|
boolean: 'Boolean',
|
|
file: 'File',
|
|
number: 'Number',
|
|
object: 'Object',
|
|
string: 'String',
|
|
}
|
|
|
|
const outputVarTypes: Record<DeclaredOutputConfig['type'], VarType> = {
|
|
array: VarType.array,
|
|
boolean: VarType.boolean,
|
|
file: VarType.file,
|
|
number: VarType.number,
|
|
object: VarType.object,
|
|
string: VarType.string,
|
|
}
|
|
|
|
const arrayItemVarTypes: Record<DeclaredOutputConfig['type'], VarType> = {
|
|
array: VarType.array,
|
|
boolean: VarType.arrayBoolean,
|
|
file: VarType.arrayFile,
|
|
number: VarType.arrayNumber,
|
|
object: VarType.arrayObject,
|
|
string: VarType.arrayString,
|
|
}
|
|
|
|
export function getAgentV2DeclaredOutputs(data: AgentV2NodeType) {
|
|
return data.agent_declared_outputs?.length
|
|
? data.agent_declared_outputs
|
|
: defaultAgentV2DeclaredOutputs
|
|
}
|
|
|
|
/**
|
|
* @public
|
|
*/
|
|
// TODO: Remove this marker after the output type label consumer is wired.
|
|
export function getDeclaredOutputTypeLabel(output: DeclaredOutputConfig) {
|
|
if (output.type === 'array')
|
|
return `Array[${output.array_item ? outputTypeLabels[output.array_item.type] : 'Object'}]`
|
|
|
|
return outputTypeLabels[output.type]
|
|
}
|
|
|
|
function getDeclaredOutputVarType(output: DeclaredOutputConfig) {
|
|
if (output.type === 'array')
|
|
return output.array_item ? arrayItemVarTypes[output.array_item.type] : VarType.arrayObject
|
|
|
|
return outputVarTypes[output.type]
|
|
}
|
|
|
|
export function getAgentV2OutputVars(data: AgentV2NodeType): Var[] {
|
|
return getAgentV2DeclaredOutputs(data).map(output => ({
|
|
variable: output.name,
|
|
type: getDeclaredOutputVarType(output),
|
|
}))
|
|
}
|