stoa/apps/web/lib/services.ts
stoa df91ac630d feat(web+structure): Stoa web app (pages+APIs) vendored from x402, plan.md buildout
- apps/web: Next.js dashboard — pages (/, /skills, /marketplace, /protected) + API routes
  (/api/health, /api/skills, /api/services, /api/x402/weather) adapted from the real x402
  next fullstack example; reads a generated static skills catalog (251 skills, 62 domains)
- generated skills.json catalog (gen-catalog now emits JSON for web + tooling)
- contracts: interfaces/, libraries/ (SafeTransferLib, SignatureLib, StoaTypes), mocks/,
  DeployAtlantic/DeployLocal/ExportAbis scripts — all compile
- sdk: types, errors, addresses, chains, abis modules (wired into barrel; 0 tsc errors)
- mercator: planner (seller scoring), scenario, trace, wallet, demo runners
- examples: numbered demos (05-08,10) + x402 seller server
- config/docs: turbo.json, .prettierrc, eslint.config.js, SUBMISSION.md,
  docs/HACKATHON_SUBMISSION.md, deployments/{pharos-atlantic,localhost}.json
- oss/ (gitignored): cloned x402, pharos-agent-kit, pharos-examples, pharos-skill-engine
2026-06-12 01:03:39 +03:00

116 lines
3.3 KiB
TypeScript

// Server-only helper that loads agent services from the on-chain ServiceRegistry
// (if configured via env) and otherwise returns a small demo dataset. Shared by
// the /api/services route and the /marketplace page so neither has to do an
// absolute self-fetch. Never throws — on any error it falls back to demo data.
import "server-only";
import { PHAROS } from "./stoa";
export interface ServiceRecord {
serviceId: number;
provider: string;
capability: string;
endpoint: string;
price: string;
demo?: boolean;
}
/// Minimal ServiceRegistry ABI — just what we read here.
const serviceRegistryAbi = [
{
type: "function",
name: "servicesByCapability",
stateMutability: "view",
inputs: [{ name: "capability", type: "string" }],
outputs: [{ name: "", type: "uint256[]" }],
},
{
type: "function",
name: "getService",
stateMutability: "view",
inputs: [{ name: "serviceId", type: "uint256" }],
outputs: [
{
name: "",
type: "tuple",
components: [
{ name: "provider", type: "address" },
{ name: "capability", type: "string" },
{ name: "endpoint", type: "string" },
{ name: "price", type: "uint256" },
],
},
],
},
] as const;
function demoServices(capability: string): ServiceRecord[] {
return [
{
serviceId: 1,
provider: "0x1111111111111111111111111111111111111111",
capability,
endpoint: "https://mercator.stoa.dev/api/research",
price: "0.001 PHRS",
demo: true,
},
{
serviceId: 2,
provider: "0x2222222222222222222222222222222222222222",
capability,
endpoint: "https://mercator.stoa.dev/api/summarize",
price: "0.002 PHRS",
demo: true,
},
];
}
/// Load services for a capability. Reads the chain when STOA_SERVICES_ADDRESS +
/// PHAROS_RPC_URL are set; otherwise returns demo data. Always resolves.
export async function loadServices(capability = "research"): Promise<ServiceRecord[]> {
const address = process.env.STOA_SERVICES_ADDRESS;
const rpcUrl = process.env.PHAROS_RPC_URL ?? PHAROS.rpcUrl;
if (!address) {
return demoServices(capability);
}
try {
const { createPublicClient, http, formatUnits } = await import("viem");
const client = createPublicClient({ transport: http(rpcUrl) });
const ids = (await client.readContract({
address: address as `0x${string}`,
abi: serviceRegistryAbi,
functionName: "servicesByCapability",
args: [capability],
})) as bigint[];
if (!ids || ids.length === 0) return [];
const services = await Promise.all(
ids.map(async (id) => {
const svc = (await client.readContract({
address: address as `0x${string}`,
abi: serviceRegistryAbi,
functionName: "getService",
args: [id],
})) as { provider: string; capability: string; endpoint: string; price: bigint };
return {
serviceId: Number(id),
provider: svc.provider,
capability: svc.capability,
endpoint: svc.endpoint,
price: `${formatUnits(svc.price, 18)} PHRS`,
} satisfies ServiceRecord;
}),
);
return services;
} catch {
// On-chain read failed (bad RPC, wrong address, etc.) — degrade to demo.
return demoServices(capability);
}
}