Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Image Generation Agent

A payment-ready agent that:

  • Requests payment via the uAgents payment protocol
  • Verifies and charges 0.1 FET via direct FET on Dorado / Fetch network (seller flow)
  • After successful payment, generates an image via ASI1 One LLM API
  • Uploads to tmpfiles.org (HTTPS) and replies with markdown-formatted image in chat (image URL)

Protocols

This repo uses two uAgents protocols:

  • AgentChatProtocol: Handles chat messages and acknowledgement.
  • Payment protocol (uagents_core.contrib.protocols.payment): Seller-side payment.

Payment protocol models (from uagents-core):

  • Funds(amount, currency, payment_method) – here: amount="0.1", currency="FET", payment_method="fet_direct".
  • RequestPayment(accepted_funds, recipient, deadline_seconds, reference?, description?, metadata?).
  • CommitPayment(funds, recipient, transaction_id, metadata?).
  • RejectPayment, CancelPayment, CompletePayment.

Rules (seller role):

  • MUST implement handlers for CommitPayment and RejectPayment.
  • Verification is on-chain: agent checks the Fetch/Dorado tx for transfer to its wallet.

Payment Flow

  1. User sends a chat message (the image prompt).
  2. Agent immediately sends RequestPayment with:
    • accepted_funds=[Funds(amount="0.1", currency="FET", payment_method="fet_direct")]
    • recipient=<agent wallet address>
    • metadata.provider_agent_wallet=<agent wallet address>
    • metadata.fet_network=stable-testnet or mainnet
    • metadata.content="Please complete the payment to generate this image."
  3. UI renders a FET payment card. User pays 0.1 FET on Dorado to the agent wallet.
  4. User sends CommitPayment with transaction_id and metadata.buyer_fet_wallet.
  5. Agent verifies the on-chain transfer via LedgerClient and sends CompletePayment on success.
  6. Agent calls ASI1 One LLM API image generation with the stored prompt, uploads base64 images to tmpfiles.org (HTTPS) if needed, and replies with ChatMessage containing markdown-formatted image ![Generated image]({image_url}).

Workflow (Mermaid)

flowchart TB
    subgraph User
        A[User sends ChatMessage with prompt]
        B[User sees RequestPayment 0.1 FET]
        C[User pays FET on Dorado]
        D[User sends CommitPayment with tx_id]
        E[User receives image URL in chat]
    end

    subgraph Agent
        F[chat_proto: handle_message]
        G[request_payment_from_user]
        H[payment_proto: handle_commit_payment]
        I[verify_fet_payment_to_agent]
        J[CompletePayment]
        K[generate_response_after_payment]
        L[call_asi_one_api]
        M[upload_to_tmpfiles if base64]
        N[Send ChatMessage with markdown image]
    end

    A --> F
    F --> G
    G --> B
    B --> C
    C --> D
    D --> H
    H --> I
    I -->|ok| J
    I -->|fail| CancelPayment
    J --> K
    K --> L
    L -->|image bytes| M
    M -->|HTTPS URL| N
    N --> E
Loading
sequenceDiagram
    participant U as User
    participant C as Chat UI
    participant A as Agent
    participant L as Fetch Ledger
    participant G as ASI1 One LLM API
    participant T as tmpfiles.org

    U->>C: ChatMessage(prompt)
    C->>A: ChatMessage
    A->>C: ChatAcknowledgement
    A->>C: RequestPayment(0.1 FET, metadata)
    C->>U: Show payment card

    U->>C: Pay 0.1 FET on Dorado
    U->>C: CommitPayment(tx_id, buyer_fet_wallet)
    C->>A: CommitPayment

    A->>L: query_tx(transaction_id)
    L->>A: tx events (transfer, recipient, amount)
    A->>A: verify_fet_payment_to_agent

    alt verified
        A->>C: CompletePayment
        A->>G: generate_image(prompt)
        G->>A: image URL or base64
        alt base64 response
            A->>T: upload base64 image
            T->>A: HTTPS image URL
        end
        A->>C: ChatMessage(TextContent with markdown: ![image](URL))
        C->>U: Show "Image generated successfully" + rendered image
    else not verified
        A->>C: CancelPayment(reason)
    end
Loading

Environment

Create a .env (you can add an env.example with the same keys and empty values):

# Agent
AGENT_NAME=Fet Example Agent
AGENT_SEED_PHRASE=asi1-llm-agent
AGENT_PORT=8000

# ASI1 One LLM API (required for image generation)
ASI_ONE_API_KEY=
ASI_ONE_MODEL=asi1

# Fetch network
FET_USE_TESTNET=true

Important:

  • Load .env before importing modules that read env vars. agent.py does:
    from dotenv import load_dotenv
    load_dotenv()
  • The UI uses metadata.provider_agent_wallet and metadata.fet_network to show the FET payment card and network.

Run locally

python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python agent.py

Agent runs on the port set in AGENT_PORT (default 8000) with mailbox enabled.

Project structure

fet-example/
  agent.py          # Agent setup, loads env early, includes chat + payment protocols
  chat_proto.py     # Chat protocol and message handling (prompt → request payment / post-payment → generate)
  payment.py        # Seller-side payment (request, verify FET on-chain, charge flow)
  client.py         # ASI1 One LLM API client + tmpfiles.org upload (handles URL and base64 responses)
  shared.py         # Shared helpers (e.g. create_text_chat)
  requirements.txt
  README.md

Notes

  • If you change payment model shapes or protocol name/version, the UI may not render the payment card. Keep the same Funds and RequestPayment / CommitPayment semantics.
  • accepted_funds must include FET with payment_method="fet_direct".
  • Always send metadata.provider_agent_wallet (and optionally metadata.fet_network) so the UI can show where to send FET.
  • Image URLs are forced to HTTPS (including tmpfiles.org) so HTTPS pages (e.g. staging.asi1.ai) do not hit mixed-content blocks.
  • ASI1 One LLM API returns either image URLs directly or base64-encoded images. The agent automatically handles both formats and uploads base64 images to tmpfiles.org.
  • Images are sent as markdown format ![Generated image]({image_url}) in TextContent, which renders properly in ASI1 Chat UI.
  • You can replace the payment method with another (e.g. Skyfire/USDC) by changing payment_method, verification logic, and metadata expected by the UI.

Enabling FET payments on ASI1

  • In ASI1, open My account (e.g. from settings in the bottom left).
  • Use Labs and enable Developer mode if you use it.
  • Go to Manage payments (e.g. under your AI / top right) and connect a wallet that supports FET on Dorado so you can pay the agent’s 0.1 FET requests.