Skip to content

Commit

Permalink
fix: 🐛 delete out of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
OdapX authored and gmpetrov committed Mar 11, 2024
1 parent 2901658 commit 41f0b1f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
39 changes: 29 additions & 10 deletions apps/dashboard/components/AgentInputs/HttpToolInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export type Fields =
}[]
| undefined;

type KeyValueNames =
| 'config.queryParameters'
| 'config.pathVariables'
| 'config.body'
| 'config.headers'
| `tools.${number}.config.queryParameters`
| `tools.${number}.config.pathVariables`
| `tools.${number}.config.body`
| `tools.${number}.config.headers`;

const KeyValueFieldArray = ({
name,
label,
Expand All @@ -77,13 +87,13 @@ const KeyValueFieldArray = ({
| 'config.pathVariables'
| 'config.body'
| 'config.headers'
| 'tools.0.config.queryParameters'
| 'tools.0.config.pathVariables'
| 'tools.0.config.body'
| 'tools.0.config.headers';
| `tools.${number}.config.queryParameters`
| `tools.${number}.config.pathVariables`
| `tools.${number}.config.body`
| `tools.${number}.config.headers`;
label?: string;
userOnly?: boolean;
prefix: '' | 'tools.0.';
prefix: `tools.${number}.` | '';
}) => {
const methods = useFormContext<HttpToolSchema | CreateAgentSchema>();

Expand Down Expand Up @@ -286,7 +296,6 @@ const KeyValueFieldArray = ({
<FormControl>
<Card variant="outlined" size="sm">
<Checkbox
id={`${field.key}-user-provided`}
size="sm"
label="Provided By User"
checked={!!field.isUserProvided}
Expand Down Expand Up @@ -317,7 +326,15 @@ const KeyValueFieldArray = ({
try {
if (name.includes('queryParameters')) {
const Url = new URL(url);
Url.searchParams.delete(field.key);
const searchParams = Url.search
.replace('?', '')
.split('&');

// use index instead of key, to avoid out_of_sync issue
const synchronizedKey = searchParams[index].split('=')[0];

Url.searchParams.delete(synchronizedKey);

methods.setValue(
`${prefix}config.url`,
decodeURI(Url.toString())
Expand All @@ -335,7 +352,8 @@ const KeyValueFieldArray = ({
// use index instead of key, to avoid out_of_sync issue
urlPaths.splice(index, 1);

const newUrl = baseUrl + '/' + urlPaths.join('');
const newUrl =
baseUrl + '/' + urlPaths.join('/') + Url.search;

methods.setValue(`${prefix}config.url`, newUrl);
}
Expand Down Expand Up @@ -417,12 +435,13 @@ const KeyValueFieldArray = ({
};

type Props = {
name?: 'tools.0';
name?: `tools.${number}`;
};

function HttpToolInput({ name }: Props) {
const methods = useFormContext<HttpToolSchema | CreateAgentSchema>();
const prefix: 'tools.0.' | '' = name ? `${name}.` : '';
// Narrow down to prevent inference as string.
const prefix: `tools.${number}.` | '' = name ? `${name}.` : '';
const templatesModal = useModal();
const [withApprovalChecked] = methods.watch([`${prefix}config.withApproval`]);
const [methodValue] = methods.watch([`${prefix}config.method`]);
Expand Down
17 changes: 9 additions & 8 deletions apps/dashboard/components/AgentInputs/ToolsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function ToolsInput({}: Props) {
`tools.${currentToolIndex}.config.method`,
`tools.${currentToolIndex}.config.pathVariables`,
`tools.${currentToolIndex}.config.queryParameters`,
] as any);
]);

const newDatastoreModal = useModal();
const newApiToolForm = useModal();
Expand Down Expand Up @@ -454,6 +454,8 @@ function ToolsInput({}: Props) {
shouldValidate: true,
});
newApiToolForm.close();
// auto save.
btnSubmitRef?.current?.click();
}}
/>
</newApiToolForm.component>
Expand Down Expand Up @@ -550,7 +552,7 @@ function ToolsInput({}: Props) {
>
{currentToolIndex >= 0 && (
<Stack gap={2}>
<HttpToolInput name={`tools.${currentToolIndex}` as `tools.0`} />
<HttpToolInput name={`tools.${currentToolIndex}`} />
<validateToolModal.component
title="Set up a request to your endpoint"
description="Send a request to your endpoint to make sure it's working well."
Expand All @@ -564,7 +566,7 @@ function ToolsInput({}: Props) {
setToolValidState={(state: boolean) => {
isToolValidRef.current = state;
}}
name={`tools.${currentToolIndex}` as `tools.0`}
name={`tools.${currentToolIndex}`}
handleCloseModal={validateToolModal.close}
/>
</validateToolModal.component>
Expand All @@ -577,9 +579,10 @@ function ToolsInput({}: Props) {
if (!isToolValidRef.current && formState.isValid) {
validateToolModal.open();
return;
} else if (isToolValidRef.current) {
editApiToolForm.close();
btnSubmitRef?.current?.click();
}
editApiToolForm.close();
btnSubmitRef?.current?.click();
}}
>
{isToolValidRef.current ? 'Update' : 'Validate Config'}
Expand All @@ -599,9 +602,7 @@ function ToolsInput({}: Props) {
>
{currentToolIndex >= 0 && (
<Stack gap={2}>
<LeadCaptureToolFormInput
name={`tools.${currentToolIndex}` as `tools.0`}
/>
<LeadCaptureToolFormInput name={`tools.${currentToolIndex}`} />
<Button
type="button"
loading={formState.isSubmitting}
Expand Down
8 changes: 4 additions & 4 deletions apps/dashboard/components/HttpToolForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ParamFields = memo(
{parameters?.map((field, index) => (
<Stack
direction="row"
key={field.key}
key={`${index}${field.key}`}
gap={2}
alignItems={'end'}
pl={2}
Expand Down Expand Up @@ -127,13 +127,13 @@ export function HttpToolTestForm<T extends HttpToolSchema | CreateAgentSchema>({
}: {
setToolValidState(arg: boolean): void;
handleCloseModal?: () => void;
name?: 'tools.0';
name?: `tools.${number}`;
}) {
const methods =
useFormContext<
T extends HttpToolSchema ? HttpToolSchema : CreateAgentSchema
>();
const prefix: 'tools.0.' | '' = name ? `${name}.` : '';
const prefix: `tools.${number}.` | '' = name ? `${name}.` : '';
const config = methods.getValues(
`${prefix}config` as any
) as HttpToolSchema['config'];
Expand Down Expand Up @@ -380,7 +380,7 @@ function HttpToolForm({ onSubmit, defaultValues }: Props) {
},
});

const config = methods.getValues([
const config = methods.watch([
'config.body',
'config.headers',
'config.method',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
} from '@chaindesk/lib/types/dtos';

type Props = {
name?: 'tools.0';
name?: `tools.${number}`;
};

export default function LeadCaptureToolFormInput({ name }: Props) {
const methods = useFormContext<LeadCaptureToolSchema | CreateAgentSchema>();
const prefix = useMemo(() => {
return name ? `${name}.` : '';
}, [name]) as 'tools.0.' | '';
}, [name]) as `tools.${number}.` | '';

const values = methods.watch(`${prefix}config` as const);

Expand Down
6 changes: 2 additions & 4 deletions apps/dashboard/pages/agents/[agentId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,7 @@ export default function AgentPage() {
>
{({ mutation }) => (
<Stack gap={2}>
<HttpToolInput
name={`tools.${state.currentToolIndex}` as `tools.0`}
/>
<HttpToolInput name={`tools.${state.currentToolIndex}`} />
<Button type="submit" loading={mutation.isMutating}>
Update
</Button>
Expand Down Expand Up @@ -543,7 +541,7 @@ export default function AgentPage() {
{({ mutation }) => (
<Stack gap={2}>
<LeadCaptureToolFormInput
name={`tools.${state.currentToolIndex}` as `tools.0`}
name={`tools.${state.currentToolIndex}`}
/>
<Button type="submit" loading={mutation.isMutating}>
Update
Expand Down

0 comments on commit 41f0b1f

Please sign in to comment.