Skip to content

Commit

Permalink
[front] - feat(front): popular assistants tab (#5649)
Browse files Browse the repository at this point in the history
* [front/lib/api/assistant] - feature: implement and store popular agents data

 - Added a new function to compute the most popular agents based on mentions within a specified timeframe
 - Store the popular agents in Redis with an expiry, refreshing the data when outdated

[front/lib/api/assistant] - refactor: extend usage of safeRedisClient for Redis interactions

 - Modified existing Redis interactions to use the safeRedisClient for better error handling and consistency
 - Included expiration time for popular agents data to ensure up-to-date information is maintained in cache

[front/lib/api/assistant] - feature: add retrieval of popular agents from Redis

 - Implemented a function to fetch the most popular agents from Redis, updating the cache if data is stale
 - Ensured that only relevant data is retrieved and parsed from Redis for the list of popular agents

* [front/lib/api/assistant] - refactor: streamline agent usage tracking and ranking in Redis

 - Removed the individual sorting and storage of agent messages, switching to a hash based count per workspace for efficiency
 - Added logic to update and fetch agent message counts and ranking only when data is outdated
 - Removed the dependency on the uuid package by eliminating the need for unique message identifiers
 - Implemented a function to retrieve the usage stats of agents, considering message counts within a set ranking timeframe
 - Improved code maintainability by introducing utility functions and constants related to agent popularity metrics

* [assistant/agent_usage] - refactor: streamline agent usage data structure in Redis

 - Removed the 'index' field from the agent usage data structure as it's no longer needed
 - Adjusted the data parsing logic to work with the simplified data structure, parsing counts as integers directly
 - Updated the Redis transaction to store just the counts without JSON stringification

* [front/lib/api/assistant] - refactor: streamline agent usage update logic

 - Replaced last updated timestamp check with TTL based validation to manage agent usage updates
 - Implemented a stale data check, where outdated agent mention counts trigger an asynchronous update without blocking the retrieval
 - Doubled the expiration time for both agent message and user counts in Redis for extended data availability

* [assistant] - refactor: remove placeholder for most popular agents

 - Eliminate the static slice operation placeholder which was limiting the popular agents' implementation

[api/assistant] - refactor: clean up agent popularity code

 - Remove the unused function `getMostPopularAgents` and its associated constants

[assistant/new] - feature: include usage data in assistant loading calls

 - Extend the data includes to fetch agent usage information in addition to the authors data in the new assistant page
 - Adjust the agents API query parameters to fetch both authors and usage data for comprehensive data loading on assistant instantiation

* [front/lib/api/assistant] - feature: implement agent usage signaling functionality

 - Added a new function `signalAgentUsage` which appears to be a placeholder for future implementation

[front/pages/api/w/[wId]/assistant/agent_configurations] - refactor: streamline agent configurations usage fetching

 - Replaced the `getAgentUsage` call with `getAgentUsage` to fetch usage data in batch
 - Introduced a Map for efficient lookups of message counts by agent configuration ID
 - Agent configurations now include a default structure for usage data with userCount, messageCount, usersWithAgentInListCount, and timePeriodSec fields

* [assistant] - refactor: streamline agent usage signaling process

 - Removed parameters from signalAgentUsage function to simplify its signature
 - Modified postUserMessage and editUserMessage to not send unnecessary data to signalAgentError
 - Adjusted storeCountsInRedis function to directly increment message count in Redis without extra parameters

[assistant/conversation] - refactor: remove deprecated userId and timestamp from agent messages

 - Eliminated sending of userId and timestamp when signaling agent usage
 - Updated associated agent message handling functions to reflect changes in data flow

[assistant/agent_configurations] - style: clean up import statement for getAgentsUsage function

 - Removed unnecessary line breaks from the getAgentsUsage import statement for better readability

* [types] - feature: introduce LightAgentUsageType and update usage property

 - Added a new type LightAgentUsageType for more efficient usage tracking
 - Extended the usage property to accept either AgentUsageType or LightAgentUsageType

* [front/components/assistant] - feature: enhance assistant usage message with light agent type support

 - Extend assistant usage message functionality to handle 'LightAgentUsageType'
 - Utilize the newly imported 'isAgentUsageType' to check for proper usage type before rendering the message

[front/lib/api/assistant] - refactor: enrich agent usage data with time period information

 - Add 'timePeriodSec' to the return object in agent usage related API calls
 - Ensure that the usage data includes the time period for better context in usage metrics

[front/pages/api/w/[wId]/assistant] - fix: correct agent configuration usage assignment

 - Fix the mapping of agent usage data to agent configurations, ensuring accurate data representation
 - Address the incorrect assignment of 'timePeriodSec' to messageCount, ensuring correct mapping for time period
 - Refactor the creation of the usageMap to properly store and retrieve 'mentionCount' objects

* [assistant] - refactor: clean up code and update agent configurations API

 - Removed outdated TODO comment from AssistantBrowser component
 - Refactored import statements in agent configurations API handler
 - Updated `agentsGetView` parameter to "assistants-search" for assistant creation page

* [front/assistant] - refactor: streamline most_popular agents and add limit to usage retrieval

 - Removed sorting from the `most_popular` field as sorting is now handled in the `getAgentsUsage` function
 - Added limit parameter to `getAgentsUsage` function to support fetching a limited number of agent usage records

[front/api] - refactor: change fetching and mapping logic for agent usage

 - Replaced manual Map creation with lodash's `keyBy` for better readability and efficiency
 - Updated agent configurations API to accept a limit query parameter for the usage data

[types/assistant] - refactor: remove redundant comment about usage computation in agent types

 - Removed outdated comment as the agent usage is now conditionally computed based on the presence of the `limit` query parameter

* [front/assistant] - refactor: streamline agent usage data and related components

 - Removed checks for usersWithAgentInListCount when newScope is "private" in Sharing component
 - Simplified assistantUsageMessage by removing isAgentUsageType and related conditionals
 - Updated getAgentsUsage function to include user count and removed outdated agent list count logic
 - Cleaned up imports and unused code across frontend components related to agent usage

[types/assistant] - refactor: remove deprecated LightAgentUsageType and related function

 - Deleted LightAgentUsageType and isAgentUsageType check as they are no longer used in the overall agent usage context

* [api/assistant] - refactor: secure agent usage query with authentication

- Added authentication parameter to getAgentUsage to ensure requests are authenticated
- Added check to confirm the workspace ID matches the authenticated user's workspace ID

[api/assistant] - feature: add graceful shutdown for Redis connections

- Implemented closing of Redis connections to prevent potential resource leaks
- Ensured Redis connections are closed after usage in agent usage data retrieval functions

[api] - fix: correct API endpoint to use authentication for agent usage

- Updated agent usage API handler to pass the authenticator to the getAgentUsage function
- Refined agent usage data query logic to align with new authentication requirement

* [assistant] - feature: improve most popular agents sorting and clarify code

 - Sort the 'most_popular' agents by their message count in descending order for better relevance
 - Replace the magic number `-2` with a named constant `TTL_KEY_NOT_EXIST` to increase code readability and maintainability

* [front/components/assistant] - fix: handle possible undefined usage in AssistantBrowser sorting

- Ensure sorting by messageCount doesn't break when a.usage or b.usage is undefined by coalescing to zero.

* [front/lib/api/assistant] - fix: handle redis connection cleanup on usage signal

 - Ensure redis client is properly closed using try-finally to prevent potential resource leaks
 - Code refactor to enhance the reliability of agent usage signaling by including connection cleanup logic

---------

Co-authored-by: Jules <[email protected]>
Co-authored-by: Jules <[email protected]>
  • Loading branch information
3 people authored Jun 19, 2024
1 parent 670203b commit 1145654
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 310 deletions.
7 changes: 2 additions & 5 deletions front/components/assistant/AssistantBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,17 @@ export function AssistantBrowser({
list: filteredAgents.filter(
(a) => a.scope === "published" && a.userListStatus === "in-list"
),
// TODO: Implement most popular agents (upcoming PR for issue #5454)
most_popular: filteredAgents
.filter((a) => a.usage && a.usage.messageCount > 0)
.sort(
(a, b) => (b.usage?.messageCount || 0) - (a.usage?.messageCount || 0)
)
.slice(0, 0), // Placeholder -- most popular agents are not implemented yet
(a, b) => (b.usage?.messageCount ?? 0) - (a.usage?.messageCount ?? 0)
),
};
}, [assistantSearch, loadingStatus, agents]);

const visibleTabs = useMemo(() => {
return ALL_AGENTS_TABS.filter((tab) => agentsByTab[tab.id].length > 0);
}, [agentsByTab]);

const [selectedTab, setSelectedTab] = useState<TabId | null>(
visibleTabs[0]?.id || null
);
Expand Down
6 changes: 1 addition & 5 deletions front/components/assistant/Sharing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,7 @@ export function SharingButton({
<div className="text-sm text-element-700">
<div>
{SCOPE_INFO[newScope].text}{" "}
{agentUsage &&
agentUsage.usersWithAgentInListCount > 0 &&
newScope !== "private"
? usageText
: null}
{agentUsage && newScope !== "private" ? usageText : null}
</div>
</div>
</div>
Expand Down
14 changes: 3 additions & 11 deletions front/components/assistant/Usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ export function assistantUsageMessage({
</>
);
}

const usersWithAgentInListCount = boldIfRequested(
`${usage.usersWithAgentInListCount} member${pluralize(
usage.usersWithAgentInListCount
)}`
);
const messageCount = boldIfRequested(
`${usage.messageCount} time${pluralize(usage.messageCount)}`
);
Expand All @@ -58,11 +52,9 @@ export function assistantUsageMessage({

return (
<>
{`${
assistantName ? "@" + assistantName : "This assistant"
} is active for`}{" "}
{usersWithAgentInListCount} and has been used {messageCount} by{" "}
{userCount} in the last {usage.timePeriodSec / (60 * 60 * 24)} days.
{assistantName ? "@" + assistantName : "This assistant"} has been used{" "}
{messageCount} by {userCount} in the last{" "}
{usage.timePeriodSec / (60 * 60 * 24)} days.
</>
);
}
Expand Down
Loading

0 comments on commit 1145654

Please sign in to comment.