From 7eb4146ca96abac39e451ddb2951f5a9f89daafe Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Fri, 24 Apr 2026 14:49:32 +0900 Subject: [PATCH] Fix agent name population + Manage tab linked agent detection - link-agent API: accept agentName/agentDescription/agentGenre from request body as fallback when RPC metadata lookup fails - agents page: check linked_agent_wallet on human user row to detect linked OWS agent, show in Manage tab (#991) Co-Authored-By: Claude Opus 4.6 (1M context) --- src/app/agents/page.tsx | 15 ++++++++++++--- src/app/api/user/link-agent/route.ts | 19 +++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/app/agents/page.tsx b/src/app/agents/page.tsx index 39678b73..37d59514 100644 --- a/src/app/agents/page.tsx +++ b/src/app/agents/page.tsx @@ -11,7 +11,7 @@ import { AgentBuild } from "../../components/AgentBuild"; import { AgentDashboard } from "../../components/AgentDashboard"; import { erc8004Abi } from "../../../lib/contracts/erc8004"; import { ERC8004_REGISTRY } from "../../../lib/contracts/constants"; -import { getAgentUserFromDB, checkUserExists, cacheAgentById } from "../../../lib/actions"; +import { getAgentUserFromDB, checkUserExists, cacheAgentById, getUserFromDB } from "../../../lib/actions"; type Tab = "register" | "build" | "dashboard"; @@ -44,6 +44,15 @@ function AgentsPageInner() { const dbIsAgentWallet = dbAgentId != null && dbUser?.agent_wallet?.toLowerCase() === address?.toLowerCase(); const dbDetected = dbAgentId != null; + // Check if user has a linked OWS agent (human → OWS wallet link) + const { data: humanUser, isLoading: humanUserLoading } = useQuery({ + queryKey: ["human-user", address], + queryFn: () => getUserFromDB(address!), + enabled: !!address && !dbDetected, + }); + const linkedAgentWallet = humanUser?.linked_agent_wallet ?? null; + const hasLinkedAgent = linkedAgentWallet !== null; + // Check if user exists in DB at all (even without agent_id) const { data: userExists, isLoading: userExistsLoading } = useQuery({ queryKey: ["user-exists", address], @@ -100,8 +109,8 @@ function AgentsPageInner() { detectedRole = "agentWallet"; } - const hasExistingAgent = (detectedAgentId !== undefined && detectedRole !== undefined) || rpcHasNft; - const detectLoading = dbLoading || (!dbDetected && rpcBalanceLoading) || (needsRpcFallback && (rpcWalletLoading || (rpcHasNft && rpcTokenLoading))); + const hasExistingAgent = (detectedAgentId !== undefined && detectedRole !== undefined) || rpcHasNft || hasLinkedAgent; + const detectLoading = dbLoading || (!dbDetected && humanUserLoading) || (!dbDetected && rpcBalanceLoading) || (needsRpcFallback && (rpcWalletLoading || (rpcHasNft && rpcTokenLoading))); // Auto-cache: when RPC fallback detects an agent not in DB, persist it const cachedRef = useRef(false); diff --git a/src/app/api/user/link-agent/route.ts b/src/app/api/user/link-agent/route.ts index d0a1abec..8b882b88 100644 --- a/src/app/api/user/link-agent/route.ts +++ b/src/app/api/user/link-agent/route.ts @@ -18,7 +18,7 @@ import type { Address } from "viem"; export async function POST(request: NextRequest) { try { const body = await request.json(); - const { humanWallet, owsWallet, signature, humanSignature, agentId: providedAgentId } = body; + const { humanWallet, owsWallet, signature, humanSignature, agentId: providedAgentId, agentName, agentDescription, agentGenre } = body; if (!humanWallet || !owsWallet || !signature || !humanSignature) { return NextResponse.json( @@ -218,15 +218,18 @@ export async function POST(request: NextRequest) { } } + // Fill missing fields from client-provided values (from plotlink-ows config.json) + if (!agentFields.agent_name && agentName) agentFields.agent_name = agentName; + if (!agentFields.agent_description && agentDescription) agentFields.agent_description = agentDescription; + if (!agentFields.agent_genre && agentGenre) agentFields.agent_genre = agentGenre; + try { if (owsUser) { - if (Object.keys(agentFields).length > 0) { - await supabase.from("users").update({ - agent_owner: normalizedHuman, - agent_type: "ows-writer", - ...agentFields, - }).eq("id", owsUser.id); - } + await supabase.from("users").update({ + agent_owner: normalizedHuman, + agent_type: "ows-writer", + ...agentFields, + }).eq("id", owsUser.id); } else { await supabase.from("users").insert({ primary_address: normalizedOws,