support referencing agent variables in agent tool form parameters

pull/13166/head
Obada Khalili 1 year ago
parent 3c04edd73c
commit dc4f82b3ea

@ -139,6 +139,7 @@ class BaseAgentRunner(AppRunner):
tenant_id=self.tenant_id,
app_id=self.app_config.app_id,
agent_tool=tool,
variables_inputs=self.application_generate_entity.inputs,
invoke_from=self.application_generate_entity.invoke_from,
)
tool_entity.load_variables(self.variables_pool)

@ -1,7 +1,7 @@
import json
import logging
import mimetypes
from collections.abc import Generator
from collections.abc import Generator, Mapping
from os import listdir, path
from threading import Lock, Thread
from typing import Any, Optional, Union, cast
@ -236,7 +236,12 @@ class ToolManager:
@classmethod
def get_agent_tool_runtime(
cls, tenant_id: str, app_id: str, agent_tool: AgentToolEntity, invoke_from: InvokeFrom = InvokeFrom.DEBUGGER
cls,
tenant_id: str,
app_id: str,
agent_tool: AgentToolEntity,
variables_inputs: Optional[Mapping[str, Any]] = None,
invoke_from: InvokeFrom = InvokeFrom.DEBUGGER,
) -> Tool:
"""
get the agent tool runtime
@ -265,8 +270,13 @@ class ToolManager:
raise ValueError(f"file type parameter {parameter.name} not supported in agent")
if parameter.form == ToolParameter.ToolParameterForm.FORM:
# save tool parameter to tool entity memory
if variables_inputs:
for key, value in variables_inputs.items():
agent_tool.tool_parameters[parameter.name] = agent_tool.tool_parameters[parameter.name].replace(
f"{{{{{key}}}}}", str(value)
)
value = cls._init_runtime_parameter(parameter, agent_tool.tool_parameters)
# save tool parameter to tool entity memory
runtime_parameters[parameter.name] = value
# decrypt runtime parameters

@ -16,6 +16,7 @@ import Loading from '@/app/components/base/loading'
import { DiagonalDividingLine } from '@/app/components/base/icons/src/public/common'
import { getLanguage } from '@/i18n/language'
import AppIcon from '@/app/components/base/app-icon'
import ConfigContext from '@/context/debug-configuration'
type Props = {
collection: Collection
@ -38,6 +39,8 @@ const SettingBuiltInTool: FC<Props> = ({
onHide,
onSave,
}) => {
const { modelConfig } = useContext(ConfigContext)
const { locale } = useContext(I18n)
const language = getLanguage(locale)
const { t } = useTranslation()
@ -47,7 +50,15 @@ const SettingBuiltInTool: FC<Props> = ({
const currTool = tools.find(tool => tool.name === toolName)
const formSchemas = currTool ? toolParametersToFormSchemas(currTool.parameters) : []
const infoSchemas = formSchemas.filter((item: any) => item.form === 'llm')
const settingSchemas = formSchemas.filter((item: any) => item.form !== 'llm')
const settingSchemas = formSchemas
.filter((item: any) => item.form !== 'llm')
.map((item: any) => ({
...item,
inputs: modelConfig.configs.prompt_variables.map(variable => ({
name: variable.key,
value: variable.key,
})),
}))
const hasSetting = settingSchemas.length > 0
const [tempSetting, setTempSetting] = useState(setting)
const [currType, setCurrType] = useState('info')

@ -102,6 +102,7 @@ export type FormShowOnObject = {
export type CredentialFormSchemaBase = {
variable: string
inputs?: Array<{ name: string; value: string }>
label: TypeWithI18N
type: FormTypeEnum
required: boolean

@ -17,6 +17,8 @@ import cn from '@/utils/classnames'
import { SimpleSelect } from '@/app/components/base/select'
import Tooltip from '@/app/components/base/tooltip'
import Radio from '@/app/components/base/radio'
import PromptEditor from '@/app/components/base/prompt-editor'
type FormProps = {
className?: string
itemClassName?: string
@ -82,6 +84,7 @@ const Form: FC<FormProps> = ({
if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput || formSchema.type === FormTypeEnum.textNumber) {
const {
variable,
inputs = [],
label,
placeholder,
required,
@ -92,6 +95,7 @@ const Form: FC<FormProps> = ({
return null
const disabled = readonly || (isEditMode && (variable === '__model_type' || variable === '__model_name'))
const fieldValue = (isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]
return (
<div key={variable} className={cn(itemClassName, 'py-3')}>
<div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
@ -103,16 +107,30 @@ const Form: FC<FormProps> = ({
}
{tooltipContent}
</div>
<Input
className={cn(inputClassName, `${disabled && 'cursor-not-allowed opacity-60'}`)}
value={(isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]}
onChange={val => handleFormChange(variable, val)}
validated={validatedSuccess}
placeholder={placeholder?.[language] || placeholder?.en_US}
disabled={disabled}
type={formSchema.type === FormTypeEnum.textNumber ? 'number' : 'text'}
{...(formSchema.type === FormTypeEnum.textNumber ? { min: (formSchema as CredentialFormSchemaNumberInput).min, max: (formSchema as CredentialFormSchemaNumberInput).max } : {})}
/>
{inputs.length
? <div className={'rounded-lg border border-transparent bg-gray-50 px-3 py-[6px] hover:border-[rgba(0,0,0,0.08)]'}>
<PromptEditor
value={fieldValue}
onChange={val => handleFormChange(variable, val)}
placeholder={placeholder?.[language] || placeholder?.en_US || 'enter \'/\' to reference a variable'}
variableBlock={{
show: true,
variables: inputs,
}}
compact
editable
/>
</div>
: <Input
className={cn(inputClassName, `${disabled && 'cursor-not-allowed opacity-60'}`)}
value={fieldValue}
onChange={val => handleFormChange(variable, val)}
validated={validatedSuccess}
placeholder={placeholder?.[language] || placeholder?.en_US}
disabled={disabled}
type={formSchema.type === FormTypeEnum.textNumber ? 'number' : 'text'}
{...(formSchema.type === FormTypeEnum.textNumber ? { min: (formSchema as CredentialFormSchemaNumberInput).min, max: (formSchema as CredentialFormSchemaNumberInput).max } : {})}
/>}
{fieldMoreInfo?.(formSchema)}
{validating && changeKey === variable && <ValidatingTip />}
</div>

Loading…
Cancel
Save