Skip to content

Commit

Permalink
[editor][client] render array objects in settings properly
Browse files Browse the repository at this point in the history
Upon loading the OpenAI function calling aiconfig, the editor client does not render the function data. This is because the functions are an array, and the client does not initialize array data.

In this diff, if the Settings data is an array with an initial value, properly set/intiailize and render the array data (inside SettingsPropertyRenderer)

Also updated the object renderer/settings to render json editor for an unspecified Json

## Testplan

Load the function calling aiconfig and open the model settings drawer. Notice the functions get rendered.

![testplan](https://github.com/lastmile-ai/aiconfig/assets/141073967/db69dd24-c792-43c1-9341-84eb235c007b)
  • Loading branch information
Ankush Pala ankush@lastmileai.dev committed Jan 12, 2024
1 parent 02bb333 commit e1a962c
Showing 1 changed file with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import UnionPropertyControl, {
UnionProperty,
} from "./property_controls/UnionPropertyControl";
import { JSONObject, JSONValue } from "aiconfig";
import JSONEditor from "./JSONEditor";

export type StateSetFromPrevFn = (prev: JSONValue) => void;
export type SetStateFn = (val: StateSetFromPrevFn | JSONValue) => void;
Expand Down Expand Up @@ -89,8 +90,38 @@ export default function SettingsPropertyRenderer({

// Used in the case the property is an array
// TODO: Should initialize with values from settings if available
const [itemControls, setItemControls] = useState<JSX.Element[]>([]);
const itemValues = useRef(new Map<string, JSONValue>());
const [itemControls, setItemControls] = useState<JSX.Element[]>(() => {

if (!Array.isArray(initialValue)){
return []
}

return initialValue.map((arrayItem) => {
const key = uniqueId();
return (
<Group key={key}>
<SettingsPropertyRenderer
propertyName=""
property={property.items}
initialValue={arrayItem}
setValue={newItem => {
itemValues.current.set(key, newItem);
setAndPropagateValue(Array.from(itemValues.current.values()));
}}
/>
<ActionIcon onClick={() => removeItemFromList(key)}>
<IconTrash size={16} />
</ActionIcon>
</Group>
);
});



});
const itemValues = useRef(
Array.isArray(propertyValue) ? new Map(propertyValue.map(val => [uniqueId(), val])) :
new Map<string, JSONValue>());

const removeItemFromList = useCallback(
async (key: string) => {
Expand Down Expand Up @@ -367,8 +398,22 @@ export default function SettingsPropertyRenderer({
<Stack>{subpropertyControls}</Stack>
</>
);
} else {
propertyControl = (
<Stack>
<PropertyLabel
propertyName={propertyName}
propertyDescription={propertyDescription}
/>
<div style={{minWidth: "350px"}}>
<JSONEditor
content={initialValue as JSONObject}
onChangeContent={setAndPropagateValue}
/>
</div>
</Stack>
);
}

break;
}
case "select": {
Expand Down

0 comments on commit e1a962c

Please sign in to comment.