a2a-source-edit: write agent.py

This commit is contained in:
a2a-cloud
2026-07-18 16:58:51 +00:00
parent 403d6582d4
commit 5081077e6a

View File

@@ -30,6 +30,7 @@ from a2a_pack import (
ASSET_TYPES = ("character", "enemy", "item", "tile", "icon", "projectile") ASSET_TYPES = ("character", "enemy", "item", "tile", "icon", "projectile")
STYLES = ("pixel", "flat", "outline", "neon") STYLES = ("pixel", "flat", "outline", "neon")
DB_OPTIONS = "-c statement_timeout=5000 -c lock_timeout=2000 -c idle_in_transaction_session_timeout=10000" DB_OPTIONS = "-c statement_timeout=5000 -c lock_timeout=2000 -c idle_in_transaction_session_timeout=10000"
DB_ENV_NAME = "DATABASE_URL"
class GeneratesSvgVideoGame9jg9vdF0f510Config(BaseModel): class GeneratesSvgVideoGame9jg9vdF0f510Config(BaseModel):
@@ -90,7 +91,7 @@ class GeneratesSvgVideoGame9jg9vdF0f510(
name="generates-svg-video-game-9jg9vd-f0f510-data", name="generates-svg-video-game-9jg9vd-f0f510-data",
scope="user", scope="user",
access_mode="read_write", access_mode="read_write",
env=AgentDatabaseEnv(url="DATABASE_URL"), env=AgentDatabaseEnv(url=DB_ENV_NAME),
migrations=AgentDatabaseMigrations(path="db/migrations"), migrations=AgentDatabaseMigrations(path="db/migrations"),
), ),
) )
@@ -170,7 +171,7 @@ class GeneratesSvgVideoGame9jg9vdF0f510(
warnings: list[str] = [] warnings: list[str] = []
persisted = False persisted = False
if os.environ.get("DATABASE_URL"): if os.environ.get(DB_ENV_NAME):
try: try:
_persist_asset_and_receipt( _persist_asset_and_receipt(
tenant_key=tenant, tenant_key=tenant,
@@ -191,7 +192,7 @@ class GeneratesSvgVideoGame9jg9vdF0f510(
except Exception as exc: # noqa: BLE001 except Exception as exc: # noqa: BLE001
warnings.append(f"receipt persistence unavailable: {type(exc).__name__}") warnings.append(f"receipt persistence unavailable: {type(exc).__name__}")
else: else:
warnings.append("DATABASE_URL not configured; receipt persistence skipped in this environment") warnings.append("managed receipt store is not configured in this environment")
await ctx.emit_progress("svg asset generation complete") await ctx.emit_progress("svg asset generation complete")
return SvgAssetResult( return SvgAssetResult(
@@ -220,11 +221,11 @@ class GeneratesSvgVideoGame9jg9vdF0f510(
limit: Annotated[int, Field(ge=1, le=25)] = 10, limit: Annotated[int, Field(ge=1, le=25)] = 10,
) -> ReceiptListResult: ) -> ReceiptListResult:
tenant = _tenant_key(ctx) tenant = _tenant_key(ctx)
if not os.environ.get("DATABASE_URL"): if not os.environ.get(DB_ENV_NAME):
return ReceiptListResult( return ReceiptListResult(
status="setup_required", status="setup_required",
receipts=[], receipts=[],
warning="DATABASE_URL is not configured for this environment.", warning="Managed receipt storage is not configured for this environment.",
) )
rows = _load_recent_receipts(tenant, limit) rows = _load_recent_receipts(tenant, limit)
return ReceiptListResult(status="ok", receipts=[ReceiptSummary(**row) for row in rows]) return ReceiptListResult(status="ok", receipts=[ReceiptSummary(**row) for row in rows])
@@ -242,7 +243,7 @@ def _tenant_key(ctx: RunContext[PlatformUserAuth]) -> str:
def _db_connection() -> Iterator[Any]: def _db_connection() -> Iterator[Any]:
import psycopg import psycopg
database_url = os.environ["DATABASE_URL"] database_url = os.environ[DB_ENV_NAME]
with psycopg.connect(database_url, options=DB_OPTIONS) as conn: with psycopg.connect(database_url, options=DB_OPTIONS) as conn:
yield conn yield conn