Skip to content

Commit

Permalink
One API call
Browse files Browse the repository at this point in the history
  • Loading branch information
lasryaric committed Dec 22, 2023
1 parent bb3fce9 commit ea35e55
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 163 deletions.
33 changes: 5 additions & 28 deletions front/lib/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { GetRunBlockResponseBody } from "@app/pages/api/w/[wId]/apps/[aId]/runs/
import { GetRunStatusResponseBody } from "@app/pages/api/w/[wId]/apps/[aId]/runs/[runId]/status";
import { GetAgentConfigurationsResponseBody } from "@app/pages/api/w/[wId]/assistant/agent_configurations";
import { GetAgentUsageResponseBody } from "@app/pages/api/w/[wId]/assistant/agent_configurations/[aId]/usage";
import { GetAgentConfigurationsLeaderboardResponseBody } from "@app/pages/api/w/[wId]/assistant/agent_configurations/leaderboard";
import { GetDataSourcesResponseBody } from "@app/pages/api/w/[wId]/data_sources";
import { GetDocumentsResponseBody } from "@app/pages/api/w/[wId]/data_sources/[name]/documents";
import { GetOrPostBotEnabledResponseBody } from "@app/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled";
Expand Down Expand Up @@ -445,9 +444,11 @@ export function useConversationReactions({
export function useAgentConfigurations({
workspaceId,
agentsGetView,
withUsage,
}: {
workspaceId: string;
agentsGetView: AgentsGetViewType;
withUsage?: boolean;
}) {
const agentConfigurationsFetcher: Fetcher<GetAgentConfigurationsResponseBody> =
fetcher;
Expand All @@ -456,7 +457,9 @@ export function useAgentConfigurations({
? `view=${agentsGetView}`
: `conversationId=${agentsGetView.conversationId}`;
const { data, error, mutate } = useSWR(
`/api/w/${workspaceId}/assistant/agent_configurations?${viewQueryString}`,
`/api/w/${workspaceId}/assistant/agent_configurations?${viewQueryString}&withUsage=${
withUsage ? true : false
}`,
agentConfigurationsFetcher
);

Expand All @@ -468,32 +471,6 @@ export function useAgentConfigurations({
};
}

export function useAgentsLeaderboard({
workspaceId,
agentsGetView,
}: {
workspaceId: string;
agentsGetView: AgentsGetViewType;
}) {
const agentsLeaderboardFetcher: Fetcher<GetAgentConfigurationsLeaderboardResponseBody> =
fetcher;
const viewQueryString =
typeof agentsGetView === "string"
? `view=${agentsGetView}`
: `conversationId=${agentsGetView.conversationId}`;
const { data, error, mutate } = useSWR(
`/api/w/${workspaceId}/assistant/agent_configurations/leaderboard?${viewQueryString}`,
agentsLeaderboardFetcher
);

return {
agentsLeaderboard: data ? data.agentConfigurationsWithUsage : [],
isAgentsLeaderboardLoading: !error && !data,
isAgentsLeaderboardError: error,
mutateAgentsLeaderboard: mutate,
};
}

export function useAgentUsage({
workspaceId,
agentConfigurationId,
Expand Down
25 changes: 23 additions & 2 deletions front/pages/api/w/[wId]/assistant/agent_configurations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import * as t from "io-ts";
import * as reporter from "io-ts-reporters";
import { NextApiRequest, NextApiResponse } from "next";

import { getAgentUsage } from "@app/lib/api/assistant/agent_usage";
import {
createAgentActionConfiguration,
createAgentConfiguration,
createAgentGenerationConfiguration,
getAgentConfigurations,
} from "@app/lib/api/assistant/configuration";
import { Authenticator, getSession } from "@app/lib/auth";
import { safeRedisClient } from "@app/lib/redis";
import { apiError, withLogging } from "@app/logger/withlogging";

export type GetAgentConfigurationsResponseBody = {
Expand Down Expand Up @@ -68,6 +70,7 @@ async function handler(
const queryValidation = GetAgentConfigurationsQuerySchema.decode(
req.query
);
console.log("!!!!!!!!!!!!!!!", req.query);
if (isLeft(queryValidation)) {
const pathError = reporter.formatValidationErrors(queryValidation.left);
return apiError(req, res, {
Expand All @@ -79,7 +82,7 @@ async function handler(
});
}

const { view, conversationId } = queryValidation.right;
const { view, conversationId, withUsage } = queryValidation.right;
const viewParam = view
? view
: conversationId
Expand All @@ -94,7 +97,25 @@ async function handler(
},
});
}
const agentConfigurations = await getAgentConfigurations(auth, viewParam);
let agentConfigurations = await getAgentConfigurations(auth, viewParam);
if (withUsage) {
agentConfigurations = await safeRedisClient(async (redis) => {
return Promise.all(
agentConfigurations.map(
async (agentConfiguration): Promise<AgentConfigurationType> => {
return {
...agentConfiguration,
usage: await getAgentUsage({
providedRedis: redis,
agentConfigurationId: agentConfiguration.sId,
workspaceId: owner.sId,
}),
};
}
)
);
});
}
return res.status(200).json({
agentConfigurations,
});
Expand Down
114 changes: 0 additions & 114 deletions front/pages/api/w/[wId]/assistant/agent_configurations/leaderboard.ts

This file was deleted.

40 changes: 24 additions & 16 deletions front/pages/w/[wId]/assistant/gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import AppLayout from "@app/components/sparkle/AppLayout";
import { AppLayoutSimpleCloseTitle } from "@app/components/sparkle/AppLayoutTitle";
import { subNavigationConversations } from "@app/components/sparkle/navigation";
import { Authenticator, getSession, getUserFromSession } from "@app/lib/auth";
import { useAgentConfigurations, useAgentsLeaderboard } from "@app/lib/swr";
import { useAgentConfigurations } from "@app/lib/swr";
import { subFilter } from "@app/lib/utils";

const { GA_TRACKING_ID = "" } = process.env;
Expand Down Expand Up @@ -85,19 +85,15 @@ export default function AssistantsGallery({
gaTrackingId,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const router = useRouter();
const [orderBy, setOrderBy] = useState<"name" | "usage">("name");

const { agentConfigurations, mutateAgentConfigurations } =
useAgentConfigurations({
workspaceId: owner.sId,
agentsGetView,
withUsage: orderBy === "usage",
});

const { agentsLeaderboard } = useAgentsLeaderboard({
workspaceId: owner.sId,
agentsGetView,
});

const [orderBy, setOrderBy] = useState<"name" | "usage">("name");
const [assistantSearch, setAssistantSearch] = useState<string>("");

let agentsToDisplay: AgentConfigurationType[] = [];
Expand All @@ -117,16 +113,28 @@ export default function AssistantsGallery({
break;
}
case "usage": {
agentsToDisplay = agentsLeaderboard
.filter((a) => {
return (
subFilter(assistantSearch.toLowerCase(), a.name.toLowerCase()) &&
a.status === "active"
);
})
.sort((a, b) => {
return b.usage.messageCount - a.usage.messageCount;
agentsToDisplay = agentConfigurations.filter((a) => {
return (
subFilter(assistantSearch.toLowerCase(), a.name.toLowerCase()) &&
a.status === "active"
);
});
let allHaveUsage = true;
agentsToDisplay.forEach((a) => {
if (!a.usage) {
allHaveUsage = false;
}
});
if (allHaveUsage) {
agentsToDisplay.sort((a, b) => {
if (a.usage && b.usage) {
return b.usage.messageCount - a.usage.messageCount;
} else {
// Need that to be type safe
return a.name.localeCompare(b.name);
}
});
}
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const GetAgentConfigurationsQuerySchema = t.type({
t.undefined,
]),
conversationId: t.union([t.string, t.undefined]),
withUsage: t.union([t.literal("true"), t.literal("false"), t.undefined]),
});

export const GetAgentConfigurationsLeaderboardQuerySchema = t.type({
Expand Down
5 changes: 2 additions & 3 deletions types/src/front/assistant/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ export type AgentConfigurationType = {

// If undefined, no text generation.
generation: AgentGenerationConfigurationType | null;
};

export type AgentConfigurationTypeWithUsage = AgentConfigurationType & {
usage: AgentUsageType;
// Usage is expensive to compute, so we only compute it when needed.
usage?: AgentUsageType;
};

export type AgentUsageType = {
Expand Down

0 comments on commit ea35e55

Please sign in to comment.