Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/app/agents/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 11 additions & 8 deletions src/app/api/user/link-agent/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Loading