@kya-os/cli
Version:
CLI for MCP-I setup and management
97 lines • 4.22 kB
JavaScript
import chalk from "chalk";
import ora from "ora";
/**
* Poll for agent claim status
*
* Polls the KTA API to check if an agent has been claimed.
* Returns true when claimed, false if timeout or interrupted.
*
* @param agentName - The agent's name (slug) to check
* @param did - The agent's DID
* @param endpoint - API endpoint URL (default: https://knowthat.ai)
* @param options - Polling options
*/
export async function pollForClaimStatus(agentName, did, endpoint = "https://knowthat.ai", options = {}) {
const { verbose = false, maxAttempts = 60, // 5 minutes at 5 second intervals
pollInterval = 5000, // 5 seconds
} = options;
const spinner = ora({
text: "Waiting for you to claim your agent...",
color: "yellow",
}).start();
let attempts = 0;
let skipPolling = false;
// Handle Ctrl+C to skip polling gracefully
const handleInterrupt = () => {
skipPolling = true;
spinner.warn(chalk.yellow("⏭️ Skipping claim check..."));
};
process.on('SIGINT', handleInterrupt);
try {
while (attempts < maxAttempts && !skipPolling) {
try {
const statusCheckUrl = new URL("/api/agents/" + encodeURIComponent(agentName), endpoint);
const response = await fetch(statusCheckUrl.toString(), {
method: "GET",
headers: {
"User-Agent": "kya-os-cli",
Accept: "application/json",
},
});
if (response.ok) {
const data = await response.json();
if (verbose) {
console.log(chalk.gray(`\n[DEBUG] Agent status:`));
console.log(chalk.gray(JSON.stringify(data, null, 2)));
}
// Check if claimed (multiple possible indicators)
if (data &&
(data.userId ||
data.status === 'active' ||
data.owner ||
data.claimedAt ||
data.isDraft === false)) {
spinner.succeed(chalk.green("✅ Agent claimed successfully!"));
process.removeListener('SIGINT', handleInterrupt);
return true;
}
}
else if (response.status === 404) {
// Agent not found - might not be indexed yet, continue polling
if (verbose) {
console.log(chalk.gray(`\n[DEBUG] Agent not found yet (404), continuing...`));
}
}
}
catch (fetchError) {
// Ignore network errors during polling, they might be transient
if (verbose) {
console.warn(chalk.yellow(`\n[DEBUG] Poll attempt ${attempts + 1} failed: ${fetchError instanceof Error ? fetchError.message : "Unknown error"}`));
}
}
await new Promise((resolve) => setTimeout(resolve, pollInterval));
attempts++;
// Update spinner with remaining time
const remainingTime = (maxAttempts - attempts) * (pollInterval / 1000);
const minutes = Math.floor(remainingTime / 60);
const seconds = Math.floor(remainingTime % 60);
spinner.text = `Waiting for you to claim your agent... (${minutes}:${seconds
.toString()
.padStart(2, "0")} remaining)`;
}
process.removeListener('SIGINT', handleInterrupt);
if (skipPolling) {
console.log(chalk.gray("\n You can complete the claim process later."));
return false;
}
spinner.warn(chalk.yellow("⏱️ Claim check timed out"));
console.log(chalk.gray("\n Don't worry! You can still claim your agent."));
return false;
}
catch (error) {
process.removeListener('SIGINT', handleInterrupt);
spinner.fail("Claim polling failed");
throw error;
}
}
//# sourceMappingURL=claim-polling.js.map