diff --git a/app/(main)/agents/page.tsx b/app/(main)/agents/page.tsx
index 5fa344bd..e7e593fb 100644
--- a/app/(main)/agents/page.tsx
+++ b/app/(main)/agents/page.tsx
@@ -1,4 +1,3 @@
-import { getTeamMembershipByAgentId } from "@/app/(auth)/lib/get-team-membership-by-agent-id";
import { getUser } from "@/lib/supabase";
import { createAgent, getAgents } from "@/services/agents";
import { CreateAgentButton } from "@/services/agents/components";
@@ -9,18 +8,7 @@ type AgentListProps = {
userId: string;
};
async function AgentList(props: AgentListProps) {
- const allAgents = await getAgents({ userId: props.userId });
- const agents = (
- await Promise.all(
- allAgents.map(async (agent) => {
- const teamMembership = await getTeamMembershipByAgentId(
- agent.id,
- props.userId,
- );
- return teamMembership ? agent : null;
- }),
- )
- ).filter((v) => v != null);
+ const agents = await getAgents({ userId: props.userId });
return (
diff --git a/services/agents/actions/get-agent.ts b/services/agents/actions/get-agent.ts
index 788545d9..ced6aba8 100644
--- a/services/agents/actions/get-agent.ts
+++ b/services/agents/actions/get-agent.ts
@@ -1,6 +1,6 @@
"use server";
-import { agents, db } from "@/drizzle";
+import { agents, db, supabaseUserMappings, teamMemberships } from "@/drizzle";
import { eq } from "drizzle-orm";
import { revalidateTag, unstable_cache } from "next/cache";
import type { AgentId } from "../types";
@@ -17,7 +17,22 @@ const getAgentsTag = (params: TagParams) => `${params.userId}.getAgents`;
export const getAgents = async (args: GetAgentsArgs) => {
const cachedAgents = unstable_cache(
- () => db.select().from(agents),
+ async () => {
+ const result = await db
+ .select({ agents })
+ .from(agents)
+ .innerJoin(
+ teamMemberships,
+ eq(agents.teamDbId, teamMemberships.teamDbId),
+ )
+ .innerJoin(
+ supabaseUserMappings,
+ eq(teamMemberships.userDbId, supabaseUserMappings.userDbId),
+ )
+ .where(eq(supabaseUserMappings.supabaseUserId, args.userId));
+
+ return result.map((row) => row.agents);
+ },
[args.userId],
{ tags: [getAgentsTag(args)] },
);