import { ATHClient } from "ath-client-sdk";
import { generateKeyPair } from "jose";
const { privateKey } = await generateKeyPair("ES256");
const client = new ATHClient({
gatewayUrl: "http://localhost:3000",
agentId: "https://my-agent.example.com/.well-known/agent.json",
privateKey,
});
// 1. 发现可用的服务提供者
const discovery = await client.discover();
console.log("Available providers:", discovery.supported_providers);
// 2. 注册你的代理(一次性操作,阶段 A)
const reg = await client.register({
developer: { name: "My Company", id: "dev-001" },
providers: [{ provider_id: "github", scopes: ["repo", "read:user"] }],
purpose: "Code review assistant",
});
console.log("Registered:", reg.client_id);
console.log("Approved scopes:", reg.approved_providers);
// 3. 发起授权(按用户进行,阶段 B)
const auth = await client.authorize("github", ["repo", "read:user"]);
console.log("User should visit:", auth.authorization_url);
// 4. 用户批准后,兑换 ATH 令牌
const token = await client.exchangeToken(code, auth.ath_session_id);
console.log("Effective scopes:", token.effective_scopes);
console.log("Scope intersection:", token.scope_intersection);
// 5. 通过 ATH 代理调用 API
const user = await client.proxy("github", "GET", "/user");
console.log("GitHub user:", user);