@kya-os/cli
Version:
CLI for KYA-OS MCP-I setup and management
258 lines • 9.32 kB
JavaScript
/**
* Agent Avatar Component
* Generates deterministic avatars for agents using DiceBear API
*
* Note: Image quality depends on terminal capabilities:
* - iTerm: Shows full-resolution images with special image support
* - Other terminals: Shows ASCII approximation patterns
* The value is in uniqueness and determinism - same DID always produces the same visual representation.
*/
import crypto from "crypto";
import fetch from "node-fetch";
import terminalImage from "terminal-image";
import chalk from "chalk";
/**
* Generate a deterministic seed from DID
*/
function generateSeedFromDID(did) {
// Create a consistent hash from the DID
const hash = crypto.createHash("sha256").update(did).digest("hex");
// Use first 16 characters as seed for consistency
return hash.substring(0, 16);
}
/**
* Calculate agent development level (0-4) based on completeness
*/
function calculateAgentLevel(level) {
let score = 0;
if (level.claimed)
score++;
if (level.verified)
score++;
if (level.hasGithub)
score++;
if (level.hasDescription)
score++;
if (level.handshakeComplete)
score++;
return score;
}
/**
* KYA-OS Symbol Library
* Professional iconography system for agent states and levels
*/
export const KYA_SYMBOLS = {
// Core identity symbols
CORE: "⬢", // Hexagon - represents core identity
VERIFIED: "◊", // Diamond - represents verification
NETWORK: "∞", // Infinity - represents network connections
ENERGY: "⚡", // Lightning - represents activation/power
SHIELD: "🛡", // Shield - represents security (fallback)
// Agent state progressions
STATES: {
DORMANT: "⬢", // Level 0: Basic geometric form
ACTIVE: "⬢⚡", // Level 1: Activated with energy
VERIFIED: "⬢◊", // Level 2: Core + verification
NETWORKED: "⬢◊∞", // Level 3: Connected to network
SOVEREIGN: "⬢◊∞⚡", // Level 4: Full authority
},
// Status indicators
STATUS: {
UNCLAIMED: "⬢",
CLAIMED: "⬢⚡",
VERIFIED: "⬢◊⚡",
ENHANCED: "⬢◊∞",
COMPLETE: "⬢◊∞⚡",
},
// Network patterns (for advanced displays)
PATTERNS: {
NODE: "●",
CONNECTION: "━",
BRANCH: "┳",
ENDPOINT: "◉",
},
};
/**
* Generate cyberpunk avatar URL using DiceBear Shapes API
* Creates geometric patterns that evolve as the agent develops
*/
export function generateAvatarUrl(options) {
const { did, claimed = false, size = 128, style = "identicon", level = { claimed }, } = options;
const seed = generateSeedFromDID(did);
const seedNum = parseInt(seed.substring(0, 8), 16);
const agentLevel = calculateAgentLevel(level);
// Base URL for identicon (using PNG format for better terminal support)
// Identicon creates geometric patterns that align with our cryptographic theme
const baseUrl = `https://api.dicebear.com/9.x/${style}/png`;
// Sophisticated color palettes based on cryptographic themes and trust indicators
const colorSchemes = {
0: {
// Dormant - Minimal presence, awaiting activation
bg: ["0a0a0b", "1a1a1d"],
shapes: ["2d2d34", "404049"],
accent: "3d3d46",
theme: "dormant",
},
1: {
// Active - Cool trust indicators, professional blue
bg: ["0b0f1a", "1a1f2c"],
shapes: ["1e3a5f", "2851a3"],
accent: "64b5f6",
theme: "activated",
},
2: {
// Verified - Sophisticated teal/cyan, security focused
bg: ["0a1a1d", "1a2d32"],
shapes: ["146b7a", "1f8a9c"],
accent: "26c6da",
theme: "verified",
},
3: {
// Networked - Deep purple/violet, connection emphasis
bg: ["140a1d", "2d1a32"],
shapes: ["5b1f7a", "7b2d9c"],
accent: "ab47bc",
theme: "networked",
},
4: {
// Sovereign - Holographic spectrum, authority indicators
bg: ["1a0a1d", "320a2d"],
shapes: ["7a1f5b", "9c2d7b", "5b7a1f", "2d9c7b"],
accent: "e1bee7",
theme: "sovereign",
},
};
const scheme = colorSchemes[agentLevel] || colorSchemes[0];
// Shape complexity increases with level
const shapeTypes = [
"ellipse",
"ellipseFilled",
"rectangle",
"rectangleFilled",
"polygon",
"polygonFilled",
];
const getShapeForLevel = (shapeIndex) => {
const complexity = Math.min(agentLevel + 1, 3); // Max 3 shape types
const availableShapes = shapeTypes.slice(0, complexity * 2);
return availableShapes[Math.abs((seedNum >> (shapeIndex * 4)) % availableShapes.length)];
};
const params = new URLSearchParams({
seed,
size: Math.max(size, 256).toString(),
// Background with cyberpunk feel
backgroundColor: scheme.bg[Math.abs(seedNum % scheme.bg.length)],
backgroundType: agentLevel >= 2 ? "gradientLinear" : "solid",
...(agentLevel >= 2 && {
backgroundRotation: "0,360",
}),
// Shape 1 - Always present (core identity)
shape1: getShapeForLevel(0),
shape1Color: scheme.shapes[Math.abs(seedNum % scheme.shapes.length)],
shape1OffsetX: "-30,30",
shape1OffsetY: "-20,20",
shape1Rotation: "-90,90",
// Shape 2 - Appears when claimed
...(agentLevel >= 1 && {
shape2: getShapeForLevel(1),
shape2Color: scheme.shapes[Math.abs((seedNum >> 8) % scheme.shapes.length)],
shape2OffsetX: "-25,25",
shape2OffsetY: "-25,25",
shape2Rotation: "-45,45",
}),
// Shape 3 - Appears when verified/enhanced
...(agentLevel >= 3 && {
shape3: getShapeForLevel(2),
shape3Color: scheme.shapes[Math.abs((seedNum >> 16) % scheme.shapes.length)],
shape3OffsetX: "-15,15",
shape3OffsetY: "-15,15",
shape3Rotation: "-30,30",
}),
// Visual effects for higher levels
radius: Math.min(agentLevel * 5, 20).toString(),
clip: "true",
randomizeIds: "false",
});
return `${baseUrl}?${params.toString()}`;
}
/**
* Fetch and display agent avatar in terminal
*/
export async function showAgentAvatar(options) {
try {
const avatarUrl = generateAvatarUrl({
...options,
level: options.level || { claimed: options.claimed || false },
});
const response = await fetch(avatarUrl);
if (!response.ok) {
throw new Error(`Failed to fetch avatar: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// Display in terminal with specified dimensions
// Use percentage-based sizing for better compatibility (recommended by terminal-image)
return await terminalImage.buffer(buffer, {
width: options.width || "15%",
height: options.height || "15%",
preserveAspectRatio: true,
});
}
catch (error) {
// Fallback to sophisticated symbol system
return getAvatarSymbol(options.claimed, options.level);
}
}
/**
* Get agent avatar as ASCII fallback (for environments without image support)
*/
export function getAvatarEmoji(claimed = false) {
return getAvatarSymbol(claimed);
}
/**
* Create a compact avatar display with DID info
*/
export async function createAvatarCard(options) {
const { did, claimed = false } = options;
try {
const avatarImage = await showAgentAvatar(options);
const status = claimed
? chalk.green("✓ CLAIMED")
: chalk.yellow("UNCLAIMED");
const seed = generateSeedFromDID(did).substring(0, 8);
return `${avatarImage}\n${chalk.gray(`ID: ${seed}`)} ${status}`;
}
catch {
// Fallback to sophisticated symbol version
const symbol = getAvatarSymbol(claimed, options.level);
const status = claimed
? chalk.green("✓ CLAIMED")
: chalk.yellow("UNCLAIMED");
const seed = generateSeedFromDID(did).substring(0, 8);
return ` ${symbol}\n${chalk.gray(`ID: ${seed}`)} ${status}`;
}
}
/**
* Get sophisticated agent symbol based on development level
*/
export function getAgentSymbol(level) {
const agentLevel = calculateAgentLevel(level);
const symbols = [
KYA_SYMBOLS.STATES.DORMANT, // 0: Unclaimed
KYA_SYMBOLS.STATES.ACTIVE, // 1: Claimed
KYA_SYMBOLS.STATES.VERIFIED, // 2: + Verified
KYA_SYMBOLS.STATES.NETWORKED, // 3: + Enhanced
KYA_SYMBOLS.STATES.SOVEREIGN, // 4: + Complete
];
return symbols[Math.min(agentLevel, symbols.length - 1)];
}
/**
* Get agent avatar as sophisticated fallback (replaces basic emoji)
*/
export function getAvatarSymbol(claimed = false, level) {
if (level) {
return getAgentSymbol(level);
}
return claimed ? KYA_SYMBOLS.STATUS.CLAIMED : KYA_SYMBOLS.STATUS.UNCLAIMED;
}
//# sourceMappingURL=agent-avatar.js.map