fix(frontend): Fill defaults from schema to hardcodedValues at node creation (#9632)

Defaults need to be handled as special cases every time there's no
`hardcodedValues` in node data. This causes multiple issues such as
`useCredentials` not taking into account default model and requiring
user to manually switch model back and forth.

### Changes 🏗️

- Set default values from `inputSchema` to `hardcodedValues` when new
node is placed.

### Checklist 📋

#### For code changes:
- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
  - [x] Newly placed node has defaults set as `hardcodedValues`
- [x] AI Blocks: Model is recognised, node shows price and credentials
work correctly without the need to switch

---------

Co-authored-by: Zamil Majdy <zamil.majdy@agpt.co>
This commit is contained in:
Krzysztof Czerwinski
2025-03-23 23:48:28 +01:00
committed by GitHub
parent f01b31873f
commit d694ccd50f

View File

@@ -461,6 +461,37 @@ const FlowEditor: React.FC<{
});
}, [nodes, setViewport, x, y]);
const fillDefaults = useCallback((obj: any, schema: any) => {
// Iterate over the schema properties
for (const key in schema.properties) {
if (schema.properties.hasOwnProperty(key)) {
const propertySchema = schema.properties[key];
// If the property is not in the object, initialize it with the default value
if (!obj.hasOwnProperty(key)) {
if (propertySchema.default !== undefined) {
obj[key] = propertySchema.default;
} else if (propertySchema.type === "object") {
// Recursively fill defaults for nested objects
obj[key] = fillDefaults({}, propertySchema);
} else if (propertySchema.type === "array") {
// Recursively fill defaults for arrays
obj[key] = fillDefaults([], propertySchema);
}
} else {
// If the property exists, recursively fill defaults for nested objects/arrays
if (propertySchema.type === "object") {
obj[key] = fillDefaults(obj[key], propertySchema);
} else if (propertySchema.type === "array") {
obj[key] = fillDefaults(obj[key], propertySchema);
}
}
}
}
return obj;
}, []);
const addNode = useCallback(
(blockId: string, nodeType: string, hardcodedValues: any = {}) => {
const nodeSchema = availableNodes.find((node) => node.id === blockId);
@@ -507,7 +538,10 @@ const FlowEditor: React.FC<{
categories: nodeSchema.categories,
inputSchema: nodeSchema.inputSchema,
outputSchema: nodeSchema.outputSchema,
hardcodedValues: hardcodedValues,
hardcodedValues: {
...fillDefaults({}, nodeSchema.inputSchema),
...hardcodedValues,
},
connections: [],
isOutputOpen: false,
block_id: blockId,