-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add rate limit to agnent chat
- Loading branch information
Showing
8 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Button, Checkbox, FormLabel, Stack, Typography } from '@mui/joy'; | ||
import { useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
import Input from '@app/components/Input'; | ||
import { AgentInterfaceConfig } from '@app/types/models'; | ||
|
||
const rateLimitSchema = AgentInterfaceConfig.pick({ | ||
rateLimit: true, | ||
}); | ||
|
||
export type RateLimitFields = z.infer<typeof rateLimitSchema>; | ||
|
||
interface Props extends RateLimitFields { | ||
onSubmit(args: RateLimitFields): Promise<void>; | ||
} | ||
|
||
const RateLimitForm: React.FC<Props> = ({ onSubmit, rateLimit }) => { | ||
const { register, control, handleSubmit, watch } = useForm<RateLimitFields>({ | ||
resolver: zodResolver(rateLimitSchema), | ||
defaultValues: { | ||
rateLimit, | ||
}, | ||
}); | ||
|
||
const isRateLimitEnabled = watch('rateLimit.enabled'); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<FormLabel>Rate Limit</FormLabel> | ||
<Typography | ||
level="body3" | ||
sx={{ | ||
mb: 2, | ||
}} | ||
> | ||
Limit the number of messages sent from one device on the Chat Bubble, | ||
iFrame and Standalone integrations. | ||
</Typography> | ||
|
||
<Stack gap={2}> | ||
<div className="flex space-x-4"> | ||
<Checkbox | ||
size="lg" | ||
{...register('rateLimit.enabled')} | ||
defaultChecked={isRateLimitEnabled} | ||
/> | ||
<div className="flex flex-col"> | ||
<FormLabel>Enable Rate Limit</FormLabel> | ||
<Typography level="body3"> | ||
X messages max every Y seconds | ||
</Typography> | ||
</div> | ||
</div> | ||
|
||
<Stack gap={2} pl={4}> | ||
<Input | ||
control={control as any} | ||
label="Max number of queries" | ||
disabled={!isRateLimitEnabled} | ||
placeholder="10" | ||
{...register('rateLimit.maxQueries')} | ||
/> | ||
<Input | ||
control={control as any} | ||
label="Interval (in seconds)" | ||
disabled={!isRateLimitEnabled} | ||
placeholder="60" | ||
{...register('rateLimit.interval')} | ||
/> | ||
<Input | ||
control={control as any} | ||
label="Rate Limit Reached Message" | ||
placeholder="Usage limit reached" | ||
disabled={!isRateLimitEnabled} | ||
{...register('rateLimit.limitReachedMessage')} | ||
/> | ||
</Stack> | ||
</Stack> | ||
|
||
<div style={{ display: 'flex', justifyContent: 'flex-end' }}> | ||
<Button | ||
type="submit" | ||
variant="solid" | ||
color="primary" | ||
sx={{ ml: 2, mt: 2 }} // Adjust the margin as needed | ||
> | ||
Save | ||
</Button> | ||
</div> | ||
</form> | ||
); | ||
}; | ||
|
||
export default RateLimitForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { Agent } from '@prisma/client'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import { AgentInterfaceConfig } from '@app/types/models'; | ||
import { fetcher } from '@app/utils/swr-fetcher'; | ||
|
||
const API_URL = process.env.NEXT_PUBLIC_DASHBOARD_URL; | ||
|
||
interface RateResponse { | ||
isRateExceeded: boolean; | ||
rateExceededMessage?: string; | ||
handleIncrementRateLimitCount: () => any; | ||
} | ||
|
||
const useRateLimit = ({ agentId }: { agentId?: string }): RateResponse => { | ||
const [isRateExceeded, setIsRateExceeded] = useState(false); | ||
|
||
const getAgentQuery = useSWR<Agent>( | ||
agentId ? `${API_URL}/api/agents/${agentId}` : null, | ||
fetcher | ||
); | ||
|
||
const config = getAgentQuery?.data?.interfaceConfig as AgentInterfaceConfig; | ||
const rateLimit = config?.rateLimit?.maxQueries || 0; | ||
|
||
const handleIncrementRateLimitCount = useCallback(() => { | ||
let currentRateCount = Number(localStorage.getItem('rateLimitCount')) || 0; | ||
localStorage.setItem('rateLimitCount', `${++currentRateCount}`); | ||
|
||
if (currentRateCount >= rateLimit) { | ||
setIsRateExceeded(true); | ||
} | ||
}, [rateLimit]); | ||
|
||
useEffect(() => { | ||
if (!config?.rateLimit?.interval) return; | ||
|
||
const interval = setInterval(() => { | ||
localStorage.setItem('rateLimitCount', '0'); | ||
setIsRateExceeded(false); | ||
}, config?.rateLimit?.interval * 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [config]); | ||
|
||
return { | ||
isRateExceeded: config?.rateLimit?.enabled ? isRateExceeded : false, | ||
handleIncrementRateLimitCount, | ||
rateExceededMessage: | ||
config?.rateLimit?.limitReachedMessage || 'Usage limit reached', | ||
}; | ||
}; | ||
|
||
export default useRateLimit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.