import { Agntor } from "@agntor/sdk";
import { generateText, tool } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const agntor = new Agntor({
apiKey: process.env.AGNTOR_API_KEY!,
agentId: "marketplace-orchestrator",
chain: "base",
});
const hireAgent = tool({
description: "Hire an agent from the marketplace for a task",
parameters: z.object({
agentId: z.string(),
task: z.string(),
maxBudget: z.number(),
}),
execute: async ({ agentId, task, maxBudget }) => {
// 1. Verify trust
const agent = await agntor.request("GET", `/api/v1/agents/${agentId}`);
const trust = (agent as any).trust;
if (!trust || trust.tier === "Bronze") {
return `Rejected: ${agentId} is unverified (${trust?.tier || "Unknown"}).`;
}
// 2. Create escrow
const escrow = await agntor.escrow.create({
counterparty: agentId,
amount: maxBudget,
condition: task,
timeout: 7200,
});
return JSON.stringify({
hired: true,
agent: agentId,
trust: { score: trust.score, tier: trust.tier },
escrow: escrow.escrowId,
budget: maxBudget,
});
},
});
const settleTask = tool({
description: "Settle a completed task -- release payment or dispute",
parameters: z.object({
escrowId: z.string(),
decision: z.enum(["release", "dispute"]),
reason: z.string(),
}),
execute: async ({ escrowId, decision, reason }) => {
if (decision === "release") {
const result = await agntor.settle.release(escrowId);
return `Payment released for ${escrowId}. ${reason}`;
} else {
const result = await agntor.settle.slash(escrowId);
return `Payment disputed for ${escrowId}. ${reason}`;
}
},
});
// Agent marketplace conversation
const { text } = await generateText({
model: openai("gpt-4o"),
tools: { hireAgent, settleTask },
system: "You are an agent marketplace assistant. Help users hire agents and settle tasks.",
prompt: "I need an agent to analyze my portfolio. Budget: 100 USDC. Find one with Gold+ trust.",
});