Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform) : single input validation lock #8856

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import Optional

from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema
from backend.data.model import SchemaField


class TestMutualExclusiveBlock(Block):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is a good addition - for the tests however, I wonder if you could put the tests in one of the real blocks to show it working?

class Input(BlockSchema):
text_input_1: Optional[str] = SchemaField(
title="Text Input 1",
description="First mutually exclusive input",
mutually_exclusive="group_1",
)

text_input_2: Optional[str] = SchemaField(
title="Text Input 2",
description="Second mutually exclusive input",
mutually_exclusive="group_1",
)

text_input_3: Optional[str] = SchemaField(
title="Text Input 3",
description="Third mutually exclusive input",
mutually_exclusive="group_1",
)

number_input_1: Optional[int] = SchemaField(
title="Number Input 1",
description="First number input (mutually exclusive)",
mutually_exclusive="group2",
)

number_input_2: Optional[int] = SchemaField(
title="Number Input 2",
description="Second number input (mutually exclusive)",
mutually_exclusive="group2",
)

independent_input: str = SchemaField(
title="Independent Input",
description="This input is not mutually exclusive with others",
default="This can be filled anytime",
)

class Output(BlockSchema):
result: str = SchemaField(description="Shows which inputs were filled")

def __init__(self):
super().__init__(
id="b7faa910-b074-11ef-bee7-477f51db4711",
description="A test block to demonstrate mutually exclusive inputs",
categories={BlockCategory.BASIC},
input_schema=TestMutualExclusiveBlock.Input,
output_schema=TestMutualExclusiveBlock.Output,
)

def run(self, input_data: Input, **kwargs) -> BlockOutput:
filled_inputs = []

if input_data.text_input_1:
filled_inputs.append(f"Text Input 1: {input_data.text_input_1}")
if input_data.text_input_2:
filled_inputs.append(f"Text Input 2: {input_data.text_input_2}")
if input_data.text_input_3:
filled_inputs.append(f"Text Input 3: {input_data.text_input_3}")

if input_data.number_input_1:
filled_inputs.append(f"Number Input 1: {input_data.number_input_1}")
if input_data.number_input_2:
filled_inputs.append(f"Number Input 2: {input_data.number_input_2}")

filled_inputs.append(f"Independent Input: {input_data.independent_input}")

yield "result", "\n".join(filled_inputs)
2 changes: 2 additions & 0 deletions autogpt_platform/backend/backend/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def SchemaField(
secret: bool = False,
exclude: bool = False,
hidden: Optional[bool] = None,
mutually_exclusive: Optional[str] = None,
**kwargs,
) -> T:
json_extra = {
Expand All @@ -133,6 +134,7 @@ def SchemaField(
"secret": secret,
"advanced": advanced,
"hidden": hidden,
"mutually_exclusive": mutually_exclusive,
}.items()
if v is not None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { LocalValuedInput } from "./ui/input";
import NodeHandle from "./NodeHandle";
import { ConnectionData } from "./CustomNode";
import { CredentialsInput } from "./integrations/credentials-input";
import { useNodes } from "@xyflow/react";

type NodeObjectInputTreeProps = {
nodeId: string;
Expand Down Expand Up @@ -116,6 +117,42 @@ export const NodeGenericInputField: FC<{
className,
displayName,
}) => {
const nodes = useNodes();

const isDisabledByMutualExclusion = useCallback(() => {
if (!propSchema.mutually_exclusive) return false;

// Find all inputs in the same node with the same mutuallyExclusive group
const node = nodes.find((n) => n.id === nodeId);
if (!node) return false;
const inputSchema = node.data.inputSchema as {
properties?: Record<string, BlockIOSubSchema>;
};
if (!inputSchema?.properties) return false;

// Check if any other input in the same group has a value
return Object.entries(inputSchema.properties).some(([key, schema]) => {
const otherSchema = schema;
return (
key !== propKey &&
otherSchema.mutually_exclusive === propSchema.mutually_exclusive &&
(node.data.hardcodedValues as Record<string, unknown>)[key] !==
undefined &&
(node.data.hardcodedValues as Record<string, unknown>)[key] !== ""
);
});
}, [nodeId, propKey, propSchema, nodes]);

const isDisabled = isDisabledByMutualExclusion();

const handleMutualExclusiveInputChange = (key: string, value: any) => {
if (isDisabled) {
console.warn("Input is disabled due to mutual exclusivity");
return;
}
handleInputChange(key, value);
};

className = cn(className, "my-2");
displayName ||= propSchema.title || beautifyString(propKey);

Expand Down Expand Up @@ -219,7 +256,8 @@ export const NodeGenericInputField: FC<{
error={errors[propKey]}
className={className}
displayName={displayName}
handleInputChange={handleInputChange}
disabled={isDisabled}
handleInputChange={handleMutualExclusiveInputChange}
handleInputClick={handleInputClick}
/>
);
Expand All @@ -244,7 +282,8 @@ export const NodeGenericInputField: FC<{
error={errors[propKey]}
className={className}
displayName={displayName}
handleInputChange={handleInputChange}
disabled={isDisabled}
handleInputChange={handleMutualExclusiveInputChange}
handleInputClick={handleInputClick}
/>
);
Expand Down Expand Up @@ -443,7 +482,7 @@ const NodeKeyValueInput: FC<{
>
<div>
{keyValuePairs.map(({ key, value }, index) => (
/*
/*
The `index` is used as a DOM key instead of the actual `key`
because the `key` can change with each input, causing the input to lose focus.
*/
Expand Down Expand Up @@ -693,6 +732,7 @@ const NodeStringInput: FC<{
handleInputClick: NodeObjectInputTreeProps["handleInputClick"];
className?: string;
displayName: string;
disabled?: boolean;
}> = ({
selfKey,
schema,
Expand All @@ -702,6 +742,7 @@ const NodeStringInput: FC<{
handleInputClick,
className,
displayName,
disabled,
}) => {
if (!value) {
value = schema.default || "";
Expand All @@ -718,8 +759,11 @@ const NodeStringInput: FC<{
<Select
defaultValue={value}
onValueChange={(newValue) => handleInputChange(selfKey, newValue)}
disabled={disabled}
>
<SelectTrigger>
<SelectTrigger
className={disabled ? "cursor-not-allowed opacity-50" : ""}
>
<SelectValue placeholder={schema.placeholder || displayName} />
</SelectTrigger>
<SelectContent className="nodrag">
Expand All @@ -733,28 +777,38 @@ const NodeStringInput: FC<{
) : (
<div
className="nodrag relative"
onClick={schema.secret ? () => handleInputClick(selfKey) : undefined}
onClick={
schema.secret && !disabled
? () => handleInputClick(selfKey)
: undefined
}
>
<LocalValuedInput
type="text"
id={selfKey}
value={schema.secret && value ? "*".repeat(value.length) : value}
onChange={(e) => handleInputChange(selfKey, e.target.value)}
readOnly={schema.secret}
disabled={disabled}
placeholder={
schema?.placeholder || `Enter ${beautifyString(displayName)}`
}
className="pr-8 read-only:cursor-pointer read-only:text-gray-500"
className={cn(
"pr-8 read-only:cursor-pointer read-only:text-gray-500",
disabled && "cursor-not-allowed opacity-50",
)}
/>
<Button
variant="ghost"
size="icon"
className="absolute inset-1 left-auto h-7 w-7 rounded-[0.25rem]"
onClick={() => handleInputClick(selfKey)}
title="Open a larger textbox input"
>
<Pencil2Icon className="m-0 p-0" />
</Button>
{!disabled && (
<Button
variant="ghost"
size="icon"
className="absolute inset-1 left-auto h-7 w-7 rounded-[0.25rem]"
onClick={() => handleInputClick(selfKey)}
title="Open a larger textbox input"
>
<Pencil2Icon className="m-0 p-0" />
</Button>
)}
</div>
)}
{error && <span className="error-message">{error}</span>}
Expand Down Expand Up @@ -888,6 +942,7 @@ const NodeFallbackInput: FC<{
handleInputClick: NodeObjectInputTreeProps["handleInputClick"];
className?: string;
displayName: string;
disabled?: boolean;
}> = ({
selfKey,
schema,
Expand All @@ -897,6 +952,7 @@ const NodeFallbackInput: FC<{
handleInputClick,
className,
displayName,
disabled,
}) => {
value ||= (schema as BlockIOStringSubSchema)?.default;
return (
Expand All @@ -906,6 +962,7 @@ const NodeFallbackInput: FC<{
value={value}
error={error}
handleInputChange={handleInputChange}
disabled={disabled}
handleInputClick={handleInputClick}
className={className}
displayName={displayName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type BlockIOSubSchemaMeta = {
placeholder?: string;
advanced?: boolean;
hidden?: boolean;
mutually_exclusive?: string;
};

export type BlockIOObjectSubSchema = BlockIOSubSchemaMeta & {
Expand Down
Loading