The AI trust layer between agents: identity, agreements, safeguards, and outcome intelligence for A2A.

Viewing files at commit 61a3d5a92277Commit detailsView latest

apps/api/src/agent-card-image.ts 120 lines · 5.5 KB · typescript
1
import { createRequire } from 'node:module';
2
import { dirname, join } from 'node:path';
3
import { mkdirSync, writeFileSync } from 'node:fs';
4
import type { PublicAgentCard } from '../../../packages/protocol/src/index.js';
5
6
let sharpModule: Promise<typeof import('sharp')> | undefined;
7
8
function loadSharp() {
9
  if (sharpModule) return sharpModule;
10
  const require = createRequire(import.meta.url);
11
  const fontDirectory = join(dirname(require.resolve('geist/font/sans')), 'fonts/geist-sans');
12
  const configDirectory = '/tmp/openclasp-fontconfig';
13
  const cacheDirectory = join(configDirectory, 'cache');
14
  const configFile = join(configDirectory, 'fonts.conf');
15
  mkdirSync(cacheDirectory, { recursive: true });
16
  writeFileSync(
17
    configFile,
18
    `<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd"><fontconfig><dir>${fontDirectory}</dir><cachedir>${cacheDirectory}</cachedir></fontconfig>`,
19
  );
20
  process.env.FONTCONFIG_FILE = configFile;
21
  process.env.FONTCONFIG_PATH = configDirectory;
22
  sharpModule = import('sharp');
23
  return sharpModule;
24
}
25
26
const shorten = (value: string, length: number) =>
27
  value.length > length ? `${value.slice(0, length - 1).trimEnd()}…` : value;
28
29
const escapeXml = (value: string) =>
30
  value.replace(/[&<>"']/g, (character) => {
31
    const entities: Record<string, string> = {
32
      '&': '&amp;',
33
      '<': '&lt;',
34
      '>': '&gt;',
35
      '"': '&quot;',
36
      "'": '&apos;',
37
    };
38
    return entities[character]!;
39
  });
40
41
function capabilityChips(capabilities: string[]) {
42
  let x = 64;
43
  return capabilities
44
    .slice(0, 4)
45
    .map((capability) => {
46
      const label = shorten(capability, 25);
47
      const width = Math.min(250, Math.max(96, label.length * 10 + 32));
48
      const chip = `<g transform="translate(${x} 430)">
49
        <rect width="${width}" height="42" rx="8" fill="#ffffff0b" stroke="#ffffff28"/>
50
        <text x="16" y="27" fill="#ddd4d0" font-size="17">${escapeXml(label)}</text>
51
      </g>`;
52
      x += width + 12;
53
      return chip;
54
    })
55
    .join('');
56
}
57
58
function descriptionLines(value: string) {
59
  const words = value.trim().split(/\s+/);
60
  const lines: string[] = [];
61
  for (const word of words) {
62
    const current = lines.at(-1);
63
    if (!current || (current.length + word.length + 1 > 66 && lines.length < 2)) lines.push(word);
64
    else lines[lines.length - 1] = `${current} ${word}`;
65
  }
66
  if (lines.length > 2) lines.length = 2;
67
  if (lines[1] && lines[1].length > 66) lines[1] = shorten(lines[1], 66);
68
  return lines
69
    .map((line, index) => `<tspan x="220" dy="${index === 0 ? 0 : 34}">${escapeXml(line)}</tspan>`)
70
    .join('');
71
}
72
73
export async function renderAgentCardImage(card: PublicAgentCard) {
74
  const initials = card.name
75
    .split(/\s+/)
76
    .filter(Boolean)
77
    .slice(0, 2)
78
    .map((part) => part[0]?.toUpperCase())
79
    .join('');
80
  const publicReference = shorten(card.profileUrl ?? card.cardUrl, 88);
81
  const name = shorten(card.name, 42);
82
  const nameSize = name.length > 30 ? 50 : 62;
83
  const description = card.description || 'Public agent identity on OpenClasp';
84
  const svg = `<svg width="1200" height="630" viewBox="0 0 1200 630" xmlns="http://www.w3.org/2000/svg">
85
    <defs>
86
      <linearGradient id="background" x1="0" y1="0" x2="1" y2="1">
87
        <stop offset="0" stop-color="#0c0a0a"/>
88
        <stop offset="0.62" stop-color="#17100e"/>
89
        <stop offset="1" stop-color="#2a120d"/>
90
      </linearGradient>
91
      <radialGradient id="glow" cx="0" cy="0" r="1" gradientTransform="translate(1030 120) rotate(140) scale(430 340)">
92
        <stop stop-color="#f04b2d" stop-opacity="0.2"/>
93
        <stop offset="1" stop-color="#f04b2d" stop-opacity="0"/>
94
      </radialGradient>
95
    </defs>
96
    <rect width="1200" height="630" fill="url(#background)"/>
97
    <rect width="1200" height="630" fill="url(#glow)"/>
98
    <path d="M64 66h18l10 10-10 10H64l-10-10 10-10Z" fill="none" stroke="#f04b2d" stroke-width="4"/>
99
    <text x="108" y="84" fill="#f04b2d" font-family="Geist" font-size="27" font-weight="700" letter-spacing="-1">OpenClasp</text>
100
    <g transform="translate(936 58)">
101
      <rect width="200" height="42" rx="21" fill="#44d37e0d" stroke="#44d37e88"/>
102
      <path d="M18 21l5 5 9-11" fill="none" stroke="#44d37e" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
103
      <text x="42" y="27" fill="#44d37e" font-family="Geist" font-size="16">Publisher verified</text>
104
    </g>
105
    <g transform="translate(64 165)">
106
      <rect width="122" height="122" rx="28" fill="#f04b2d1a" stroke="#f04b2d80"/>
107
      <text x="61" y="78" text-anchor="middle" fill="#ff8068" font-family="Geist" font-size="42" font-weight="700">${escapeXml(initials || 'AI')}</text>
108
    </g>
109
    <text x="220" y="176" fill="#a99e99" font-family="Geist" font-size="18" letter-spacing="2">${escapeXml(shorten(card.framework, 44).toUpperCase())} · AGENT CARD</text>
110
    <text x="220" y="246" fill="#f7f2ef" font-family="Geist" font-size="${nameSize}" font-weight="700" letter-spacing="-2">${escapeXml(name)}</text>
111
    <text x="220" y="308" fill="#c7bdb8" font-family="Geist" font-size="24">${descriptionLines(description)}</text>
112
    <g font-family="Geist">${capabilityChips(card.capabilities)}</g>
113
    <line x1="64" y1="540" x2="1136" y2="540" stroke="#ffffff21"/>
114
    <text x="64" y="582" fill="#8f8580" font-family="Geist" font-size="16">${escapeXml(publicReference)}</text>
115
    <text x="1136" y="582" text-anchor="end" fill="#8f8580" font-family="Geist" font-size="16">Capabilities are self-declared</text>
116
  </svg>`;
117
118
  const { default: sharp } = await loadSharp();
119
  return sharp(Buffer.from(svg)).png().toBuffer();
120
}