a2a-source-edit: write agent.py

This commit is contained in:
a2a-cloud
2026-07-18 17:00:18 +00:00
parent 5081077e6a
commit 4447d0d9ff

View File

@@ -27,8 +27,6 @@ from a2a_pack import (
RunContext,
)
ASSET_TYPES = ("character", "enemy", "item", "tile", "icon", "projectile")
STYLES = ("pixel", "flat", "outline", "neon")
DB_OPTIONS = "-c statement_timeout=5000 -c lock_timeout=2000 -c idle_in_transaction_session_timeout=10000"
DB_ENV_NAME = "DATABASE_URL"
@@ -114,7 +112,6 @@ class GeneratesSvgVideoGame9jg9vdF0f510(
style: Literal["pixel", "flat", "outline", "neon"] = "pixel",
variant_count: Annotated[int, Field(ge=1, le=4)] = 1,
) -> SvgAssetResult:
"""Create SVG game assets and save a DB receipt when production DB is configured."""
tenant = _tenant_key(ctx)
clean_theme = _clean_text(theme, max_len=80)
colors = _normalize_palette(palette)
@@ -333,6 +330,10 @@ def _slugify(value: str) -> str:
return slug or "game-asset"
def _scale(unit: float, n: float) -> str:
return f"{n * unit:.2f}"
def _render_svg(
*,
asset_type: str,
@@ -352,10 +353,6 @@ def _render_svg(
title = html.escape(f"{theme} {asset_type} {style} variant {variant + 1}")
label = html.escape(theme[:18])
pattern = int(seed[variant * 2 : variant * 2 + 2], 16)
def px(n: float) -> str:
return f"{n * unit:.2f}"
bg_shape = (
f'<rect width="{size}" height="{size}" rx="{size * 0.14:.1f}" fill="{bg}" opacity="0.18"/>'
if style != "outline"
@@ -363,58 +360,62 @@ def _render_svg(
)
glow = ""
if style == "neon":
glow = f'<filter id="glow"><feGaussianBlur stdDeviation="3" result="blur"/><feMerge><feMergeNode in="blur"/><feMergeNode in="SourceGraphic"/></feMerge></filter>'
body = _asset_body(asset_type, style, primary, accent, dark, stroke, px, pattern)
grid = ""
glow = '<filter id="glow"><feGaussianBlur stdDeviation="3" result="blur"/><feMerge><feMergeNode in="blur"/><feMergeNode in="SourceGraphic"/></feMerge></filter>'
body = _asset_body(asset_type, style, primary, accent, dark, stroke, unit, pattern)
grid_parts: list[str] = []
if style == "pixel":
grid = "".join(
f'<rect x="{px((pattern + i * 3) % 14 + 1)}" y="{px((pattern // 3 + i * 5) % 14 + 1)}" width="{px(1)}" height="{px(1)}" fill="{accent}" opacity="0.7"/>'
for i in range(5)
)
for i in range(5):
x = _scale(unit, (pattern + i * 3) % 14 + 1)
y = _scale(unit, (pattern // 3 + i * 5) % 14 + 1)
one = _scale(unit, 1)
grid_parts.append(f'<rect x="{x}" y="{y}" width="{one}" height="{one}" fill="{accent}" opacity="0.7"/>')
filter_value = "url(#glow)" if style == "neon" else "none"
return (
f'<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 {size} {size}" width="{size}" height="{size}">'
f'<title>{title}</title><defs>{glow}</defs>{bg_shape}'
f'<g filter="{"url(#glow)" if style == "neon" else "none"}">{grid}{body}</g>'
f'<g filter="{filter_value}">{"".join(grid_parts)}{body}</g>'
f'<text x="{size / 2:.1f}" y="{size - unit:.1f}" text-anchor="middle" font-family="monospace" font-size="{max(8, int(size / 13))}" fill="{dark}" opacity="0.78">{label}</text>'
"</svg>"
)
def _asset_body(asset_type: str, style: str, primary: str, accent: str, dark: str, stroke: int, px: Any, pattern: int) -> str:
def _asset_body(asset_type: str, style: str, primary: str, accent: str, dark: str, stroke: int, unit: float, pattern: int) -> str:
fill = "none" if style == "outline" else primary
common = f'stroke="{dark}" stroke-width="{stroke}" stroke-linejoin="round" stroke-linecap="round"'
p = lambda n: _scale(unit, n)
if asset_type == "character":
return (
f'<circle cx="{px(8)}" cy="{px(4.2)}" r="{px(2.1)}" fill="{fill}" {common}/>'
f'<path d="M {px(5)} {px(7)} H {px(11)} L {px(12)} {px(13)} H {px(4)} Z" fill="{fill}" {common}/>'
f'<circle cx="{px(7.25)}" cy="{px(4)}" r="{px(.35)}" fill="{accent}"/><circle cx="{px(8.75)}" cy="{px(4)}" r="{px(.35)}" fill="{accent}"/>'
f'<circle cx="{p(8)}" cy="{p(4.2)}" r="{p(2.1)}" fill="{fill}" {common}/>'
f'<path d="M {p(5)} {p(7)} H {p(11)} L {p(12)} {p(13)} H {p(4)} Z" fill="{fill}" {common}/>'
f'<circle cx="{p(7.25)}" cy="{p(4)}" r="{p(.35)}" fill="{accent}"/><circle cx="{p(8.75)}" cy="{p(4)}" r="{p(.35)}" fill="{accent}"/>'
)
if asset_type == "enemy":
spikes = " ".join(f'{px(2 + i * 2)},{px(7 + ((pattern + i) % 3))}' for i in range(7))
spike_points: list[str] = []
for i in range(7):
spike_points.append(f'{p(2 + i * 2)},{p(7 + ((pattern + i) % 3))}')
return (
f'<polygon points="{px(8)},{px(2)} {px(14)},{px(8)} {px(11)},{px(14)} {px(5)},{px(14)} {px(2)},{px(8)}" fill="{fill}" {common}/>'
f'<polyline points="{spikes}" fill="none" stroke="{accent}" stroke-width="{stroke}"/>'
f'<circle cx="{px(6)}" cy="{px(8)}" r="{px(.55)}" fill="{dark}"/><circle cx="{px(10)}" cy="{px(8)}" r="{px(.55)}" fill="{dark}"/>'
f'<polygon points="{p(8)},{p(2)} {p(14)},{p(8)} {p(11)},{p(14)} {p(5)},{p(14)} {p(2)},{p(8)}" fill="{fill}" {common}/>'
f'<polyline points="{" ".join(spike_points)}" fill="none" stroke="{accent}" stroke-width="{stroke}"/>'
f'<circle cx="{p(6)}" cy="{p(8)}" r="{p(.55)}" fill="{dark}"/><circle cx="{p(10)}" cy="{p(8)}" r="{p(.55)}" fill="{dark}"/>'
)
if asset_type == "item":
return (
f'<path d="M {px(8)} {px(2)} L {px(13)} {px(7)} L {px(8)} {px(14)} L {px(3)} {px(7)} Z" fill="{fill}" {common}/>'
f'<path d="M {px(8)} {px(3.7)} L {px(10.8)} {px(7)} L {px(8)} {px(12)} L {px(5.2)} {px(7)} Z" fill="{accent}" opacity="0.85"/>'
f'<path d="M {p(8)} {p(2)} L {p(13)} {p(7)} L {p(8)} {p(14)} L {p(3)} {p(7)} Z" fill="{fill}" {common}/>'
f'<path d="M {p(8)} {p(3.7)} L {p(10.8)} {p(7)} L {p(8)} {p(12)} L {p(5.2)} {p(7)} Z" fill="{accent}" opacity="0.85"/>'
)
if asset_type == "tile":
return "".join(
f'<rect x="{px(x)}" y="{px(y)}" width="{px(3)}" height="{px(3)}" fill="{primary if (x + y + pattern) % 2 else accent}" {common} opacity="0.9"/>'
for x in (2, 5, 8, 11)
for y in (2, 5, 8, 11)
)
tiles: list[str] = []
for x in (2, 5, 8, 11):
for y in (2, 5, 8, 11):
color = primary if (x + y + pattern) % 2 else accent
tiles.append(f'<rect x="{p(x)}" y="{p(y)}" width="{p(3)}" height="{p(3)}" fill="{color}" {common} opacity="0.9"/>')
return "".join(tiles)
if asset_type == "projectile":
return (
f'<path d="M {px(2)} {px(9)} C {px(6)} {px(3)}, {px(10)} {px(3)}, {px(14)} {px(7)} C {px(10)} {px(8)}, {px(7)} {px(10)}, {px(2)} {px(9)} Z" fill="{fill}" {common}/>'
f'<path d="M {px(3)} {px(9)} L {px(1)} {px(6)} M {px(3.4)} {px(9.5)} L {px(1)} {px(12)}" stroke="{accent}" stroke-width="{stroke}"/>'
f'<path d="M {p(2)} {p(9)} C {p(6)} {p(3)}, {p(10)} {p(3)}, {p(14)} {p(7)} C {p(10)} {p(8)}, {p(7)} {p(10)}, {p(2)} {p(9)} Z" fill="{fill}" {common}/>'
f'<path d="M {p(3)} {p(9)} L {p(1)} {p(6)} M {p(3.4)} {p(9.5)} L {p(1)} {p(12)}" stroke="{accent}" stroke-width="{stroke}"/>'
)
return (
f'<rect x="{px(4)}" y="{px(4)}" width="{px(8)}" height="{px(8)}" rx="{px(1.4)}" fill="{fill}" {common}/>'
f'<path d="M {px(8)} {px(3)} V {px(13)} M {px(3)} {px(8)} H {px(13)}" stroke="{accent}" stroke-width="{stroke}"/>'
f'<rect x="{p(4)}" y="{p(4)}" width="{p(8)}" height="{p(8)}" rx="{p(1.4)}" fill="{fill}" {common}/>'
f'<path d="M {p(8)} {p(3)} V {p(13)} M {p(3)} {p(8)} H {p(13)}" stroke="{accent}" stroke-width="{stroke}"/>'
)