-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
19 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
114 changes: 114 additions & 0 deletions
114
front/pages/api/w/[wId]/assistant/agent_configurations/leaderboard.ts
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,114 @@ | ||
import { | ||
AgentConfigurationTypeWithUsage, | ||
GetAgentConfigurationsLeaderboardQuerySchema, | ||
} from "@dust-tt/types"; | ||
import { ReturnedAPIErrorType } from "@dust-tt/types"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as reporter from "io-ts-reporters"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getAgentUsage } from "@app/lib/api/assistant/agent_usage"; | ||
import { 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 GetAgentConfigurationsLeaderboardResponseBody = { | ||
agentConfigurationsWithUsage: AgentConfigurationTypeWithUsage[]; | ||
}; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse< | ||
GetAgentConfigurationsLeaderboardResponseBody | ReturnedAPIErrorType | ||
> | ||
): Promise<void> { | ||
const session = await getSession(req, res); | ||
const auth = await Authenticator.fromSession( | ||
session, | ||
req.query.wId as string | ||
); | ||
const owner = auth.workspace(); | ||
if (!owner) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "workspace_not_found", | ||
message: "The workspace you're trying to modify was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "GET": { | ||
if (!auth.isUser()) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "app_auth_error", | ||
message: "Only the workspace users can see Assistants.", | ||
}, | ||
}); | ||
} | ||
const queryValidation = | ||
GetAgentConfigurationsLeaderboardQuerySchema.decode(req.query); | ||
if (isLeft(queryValidation)) { | ||
const pathError = reporter.formatValidationErrors(queryValidation.left); | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid query parameters: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
const { view } = queryValidation.right; | ||
|
||
if (view === "admin_internal" && !auth.isDustSuperUser()) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "app_auth_error", | ||
message: "Only Dust Super Users can see admin_internal agents.", | ||
}, | ||
}); | ||
} | ||
const agentConfigurations = await getAgentConfigurations(auth, view); | ||
const agentConfigurationsWithUsage = await safeRedisClient( | ||
async (client) => { | ||
return await Promise.all( | ||
agentConfigurations.map( | ||
async ( | ||
agentConfiguration | ||
): Promise<AgentConfigurationTypeWithUsage> => { | ||
return { | ||
...agentConfiguration, | ||
usage: await getAgentUsage({ | ||
providedRedis: client, | ||
workspaceId: owner.sId, | ||
agentConfigurationId: agentConfiguration.sId, | ||
}), | ||
}; | ||
} | ||
) | ||
); | ||
} | ||
); | ||
|
||
return res | ||
.status(200) | ||
.json({ agentConfigurationsWithUsage: agentConfigurationsWithUsage }); | ||
} | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, GET is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |
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