Files
twenty/packages/twenty-front/src/modules/ai/hooks/useAgentUpdateFormState.ts
T
Antoine Moreaux 153739b9c3 feat(ai): add current context to ai chat (#13315)
## TODO

- [ ] add dropdown to use records from outside the context
- [x] add loader for files chip
- [x] add roleId where it's necessary
- [x] Split AvatarChip in two components. One with the icon that will
call the second with leftComponent.
- [ ] Fix tests
- [x] Fix UI regression on Search
2025-07-22 17:27:19 +02:00

84 lines
1.7 KiB
TypeScript

import { useState } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { useDebouncedCallback } from 'use-debounce';
import {
useFindOneAgentQuery,
useUpdateOneAgentMutation,
} from '~/generated-metadata/graphql';
type AgentFormValues = {
name: string;
prompt: string;
modelId: string;
};
export const useAgentUpdateFormState = ({
agentId,
readonly = false,
}: {
agentId: string;
readonly?: boolean;
}) => {
const [formValues, setFormValues] = useState<AgentFormValues>({
name: '',
prompt: '',
modelId: '',
});
const { loading } = useFindOneAgentQuery({
variables: { id: agentId },
skip: !agentId,
onCompleted: (data) => {
if (isDefined(data?.findOneAgent)) {
const agent = data.findOneAgent;
setFormValues({
name: agent.name,
prompt: agent.prompt,
modelId: agent.modelId,
});
}
},
});
const [updateAgent] = useUpdateOneAgentMutation();
const updateAgentMutation = async (updates: AgentFormValues) => {
if (!agentId) {
return;
}
await updateAgent({
variables: {
input: {
id: agentId,
...updates,
},
},
});
};
const handleSave = useDebouncedCallback(async (formData: AgentFormValues) => {
await updateAgentMutation({
name: formData.name,
prompt: formData.prompt,
modelId: formData.modelId,
});
}, 500);
const handleFieldChange = async (field: string, value: string) => {
if (readonly) {
return;
}
setFormValues((prev) => ({ ...prev, [field]: value }));
await handleSave({ ...formValues, [field]: value });
};
return {
formValues,
handleFieldChange,
loading,
};
};