diff --git a/front/lib/api/assistant/configuration.ts b/front/lib/api/assistant/configuration.ts index 0b016144ae4d3..12b6f6f653a8f 100644 --- a/front/lib/api/assistant/configuration.ts +++ b/front/lib/api/assistant/configuration.ts @@ -366,12 +366,35 @@ async function fetchWorkspaceAgentConfigurationsWithoutActions( }); default: if (typeof agentsGetView === "object" && "agentIds" in agentsGetView) { - return AgentConfiguration.findAll({ + if (agentsGetView.allVersions) { + return AgentConfiguration.findAll({ + where: { + workspaceId: owner.id, + sId: agentsGetView.agentIds.filter((id) => !isGlobalAgentId(id)), + }, + order: [["version", "DESC"]], + }); + } + const latestVersions = (await AgentConfiguration.findAll({ + attributes: [ + "sId", + [Sequelize.fn("MAX", Sequelize.col("version")), "max_version"], + ], where: { workspaceId: owner.id, - ...(agentPrefix ? { name: { [Op.iLike]: `${agentPrefix}%` } } : {}), sId: agentsGetView.agentIds.filter((id) => !isGlobalAgentId(id)), - ...(agentsGetView.allVersions ? {} : { status: "active" }), + }, + group: ["sId"], + raw: true, + })) as unknown as { sId: string; max_version: number }[]; + + return AgentConfiguration.findAll({ + where: { + workspaceId: owner.id, + sId: latestVersions.map((v) => v.sId), + version: { + [Op.in]: latestVersions.map((v) => v.max_version), + }, }, order: [["version", "DESC"]], });