Golem add agent scala

From golemcloud/golem · View interactive version

Golem add agent scala is an AI agent skill from the GitHub repository golemcloud/golem. Adding a new Scala agent to a Golem component. Use when the user asks to create, add, or define a new agent type, implement an agent trait, or add agent methods in a Scala Golem project.

How to use this skill

Skill file

Adding a New Agent to a Scala Golem Component

Overview

An agent is a durable, stateful unit of computation in Golem. Each agent type is defined as a trait + implementation class pair using annotations from golem.runtime.annotations.

Steps

  1. Create the agent trait file — add src/main/scala/<package>/<AgentName>.scala
  2. Create the agent implementation file — add src/main/scala/<package>/<AgentName>Impl.scala
  3. Annotate the trait with @agentDefinition extending BaseAgent
  4. Annotate the implementation with @agentImplementation()
  5. Build — run golem build to verify

Agent Definition

The trait defines the agent's API:

import golem.runtime.annotations.agentDefinition
import golem.BaseAgent

import scala.concurrent.Future

@agentDefinition(mount = "/counters/{name}")
trait CounterAgent extends BaseAgent {

  class Id(val name: String)

  def increment(): Future[Int]

  def getCount(): Future[Int]
}

The implementation provides the behavior:

import golem.runtime.annotations.agentImplementation

import scala.concurrent.Future

@agentImplementation()
final class CounterAgentImpl(private val name: String) extends CounterAgent {
  private var count: Int = 0

  override def increment(): Future[Int] = Future.successful {
    count += 1
    count
  }

  override def getCount(): Future[Int] = Future.successful(count)
}

Agent Identity

The agent's constructor parameters are its key: upsert with the same parameters addresses the same keyed agent. Declare them as an inner class Id(...) in the trait:

@agentDefinition()
trait ShardAgent extends BaseAgent {
  class Id(val region: String, val partition: Int)
  // ...
}

The implementation class takes separate parameters with the same types and order:

@agentImplementation()
final class ShardAgentImpl(region: String, partition: Int) extends ShardAgent {
  // ...
}

Custom Types

Use case classes for structured data. The SDK requires a zio.blocks.schema.Schema for custom types used as method parameters or return values. For collections, use List[T] instead of Array[T] — Array does not have automatic Schema derivation support:

import zio.blocks.schema.Schema

final case class Coordinates(lat: Double, lon: Double) derives Schema
final case class WeatherReport(temperature: Double, description: String) derives Schema

@agentDefinition()
trait WeatherAgent extends BaseAgent {
  class Id(val location: String)
  def getWeather(coords: Coordinates): Future[WeatherReport]
}

Identity fields appear in the agent key and in telemetry. Never put API keys in Id.

If the weather provider needs a key, inject Config[WeatherConfig] into the

implementation with an apiKey: Secret[String] field and extend

AgentConfig[WeatherConfig] on the trait; see golem-add-secret-scala.

Returning Failures

Agent methods should distinguish between domain errors (expected failure outcomes) and uncaught errors:

import golem.runtime.annotations.{agentDefinition, agentImplementation}
import golem.BaseAgent
import zio.blocks.schema.Schema

import scala.concurrent.Future

enum WithdrawError derives Schema:
  case InsufficientFunds(available: Long)
  case AccountClosed

@agentDefinition(mount = "/wallets/{owner}")
trait Wallet extends BaseAgent {
  class Id(val owner: String)

  def withdraw(amount: Long): Future[Either[WithdrawError, Long]]
}

@agentImplementation()
final class WalletImpl(private val owner: String) extends Wallet {
  private var balance: Long = 0L
  private var closed: Boolean = false

  override def withdraw(amount: Long): Future[Either[WithdrawError, Long]] =
    Future.successful {
      if (closed) Left(WithdrawError.AccountClosed)
      else if (amount > balance) Left(WithdrawError.InsufficientFunds(balance))
      else {
        balance -= amount
        Right(balance)
      }
    }
}

Returning Left(WithdrawError.X) completes the invocation successfully — the caller receives the error as a value. Returning a failed Future (e.g. Future.failed(new RuntimeException(...))) or letting an exception escape instead fails the invocation without retry.

HTTP API Annotations

Agents can expose methods as HTTP endpoints using @endpoint and @header:

import golem.runtime.annotations.{endpoint, header}

@agentDefinition(mount = "/api/{id}")
trait ApiAgent extends BaseAgent {
  class Id(val id: String)

  @endpoint(method = "GET", path = "/data")
  def getData(@header("Authorization") auth: String): Future[String]

  @endpoint(method = "POST", path = "/update")
  def update(body: UpdateRequest): Future[UpdateResponse]
}

Related Skills

Key Constraints

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.