-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Managing Agent Usage counter from backend (#293)
* feat: sending new header of last user message id on /v1/chat & storing counter in state.chat & resetting prevent_send on restored chat from cache * wip: cut off enableSend * fix: fixed types and tests * fix: sending last user message id within body of request, not as header * rework * wip: receiving agent usage counter from backend, using those values in application TODO: remove unused code from useAgentUsage() hook * feat: refactored useAgentUsage hook, created middleware for chatResponse action dispatching, storing agent usage metadata in separate slice, not in chat one, receiving data on v1/login, asserting to state * fix: created separate action for initial set of data, removed outdated comments * fix: disabling agentic models for free users in other modes, not agent * fix: adjusted UserSurvey test & made proper type guarding for User type * chore: fixed npm run types * fix: adjusted mocks for pro/non-pro users --------- Co-authored-by: Kirill Starkov <[email protected]>
- Loading branch information
1 parent
e5ae850
commit 9198a99
Showing
18 changed files
with
179 additions
and
100 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
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 |
---|---|---|
|
@@ -6,18 +6,18 @@ import { Theme } from "../../components/Theme"; | |
import { AgentUsage } from "./AgentUsage"; | ||
import { nonProUser } from "../../__fixtures__/msw"; | ||
|
||
const items = Array.from({ length: 100 }).map(() => ({ | ||
user: "[email protected]", | ||
time: Date.now(), | ||
})); | ||
|
||
const Template: React.FC = () => { | ||
const store = setUpStore({ | ||
tour: { | ||
type: "finished", | ||
}, | ||
agentUsage: { | ||
items, | ||
agent_usage: 5, | ||
agent_max_usage_amount: 20, | ||
_persist: { | ||
rehydrated: true, | ||
version: 1, | ||
}, | ||
}, | ||
config: { | ||
apiKey: "foo", | ||
|
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 |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
|
||
export type AgentUsageItem = { | ||
time: number; | ||
user: string; | ||
export type AgentUsageMeta = { | ||
agent_usage: null | number; // null if plan is PRO or ROBOT | ||
agent_max_usage_amount: number; // maximum amount of agent usage allowed per UTC day for users with FREE plan | ||
}; | ||
|
||
const initialState: { items: AgentUsageItem[] } = { items: [] }; | ||
|
||
const oneDay = 24 * 60 * 60 * 1000; | ||
const initialState: AgentUsageMeta = { | ||
agent_usage: null, | ||
agent_max_usage_amount: 20, | ||
}; | ||
|
||
export const agentUsageSlice = createSlice({ | ||
name: "agentUsage", | ||
initialState, | ||
reducers: { | ||
addAgentUsageItem: (state, action: PayloadAction<{ user: string }>) => { | ||
const now = Date.now(); | ||
const todaysItems = state.items.filter( | ||
(item) => item.time + oneDay > now, | ||
); | ||
const item = { time: now, user: action.payload.user }; | ||
state.items = [...todaysItems, item]; | ||
updateAgentUsage: ( | ||
state, | ||
action: PayloadAction<AgentUsageMeta["agent_usage"]>, | ||
) => { | ||
state.agent_usage = action.payload; | ||
}, | ||
updateMaxAgentUsageAmount: (state, action: PayloadAction<number>) => { | ||
state.agent_max_usage_amount = action.payload; | ||
}, | ||
setInitialAgentUsage: (state, action: PayloadAction<AgentUsageMeta>) => { | ||
const { agent_max_usage_amount, agent_usage } = action.payload; | ||
state.agent_usage = agent_usage; | ||
state.agent_max_usage_amount = agent_max_usage_amount; | ||
}, | ||
}, | ||
|
||
selectors: { | ||
selectAgentUsageItems: (state) => { | ||
return state.items; | ||
}, | ||
selectAgentUsage: (state) => state.agent_usage, | ||
selectMaxAgentUsageAmount: (state) => state.agent_max_usage_amount, | ||
}, | ||
}); | ||
|
||
export const { addAgentUsageItem } = agentUsageSlice.actions; | ||
export const { selectAgentUsageItems } = agentUsageSlice.selectors; | ||
export const { | ||
updateAgentUsage, | ||
updateMaxAgentUsageAmount, | ||
setInitialAgentUsage, | ||
} = agentUsageSlice.actions; | ||
export const { selectAgentUsage, selectMaxAgentUsageAmount } = | ||
agentUsageSlice.selectors; |
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
Oops, something went wrong.