Golem add agent moonbit

From golemcloud/golem · View interactive version

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

How to use this skill

Skill file

Adding a New Agent to a MoonBit Golem Component

Overview

An agent is a durable, stateful unit of computation in Golem. Each agent type is defined as a struct annotated with #derive.agent and has a constructor fn AgentName::new(...) plus public methods pub fn AgentName::method(self: Self).

Steps

  1. Create the agent file — add a new file src/<agent_name>.mbt
  2. Define the agent struct — annotate with #derive.agent
  3. Add constructor — fn AgentName::new(param: Type) -> AgentName
  4. Add methods — pub fn AgentName::method(self: Self) -> ReturnType
  5. Build — run golem build to verify

Agent Definition

#derive.agent
struct MyAgent {
  name: String
  mut count: UInt
}

fn MyAgent::new(name: String) -> MyAgent {
  { name, count: 0 }
}

pub fn MyAgent::get_count(self: Self) -> UInt {
  self.count
}

pub fn MyAgent::increment(self: Self) -> UInt {
  self.count = self.count + 1
  self.count
}

Custom Types

All parameter and return types must have schema support. For custom structs and enums, annotate with #derive.golem_schema:

#derive.golem_schema
struct MyData {
  field1: String
  field2: UInt
}

#derive.golem_schema
enum Status {
  Active
  Inactive(String)
}

Returning Failures

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

#derive.golem_schema
pub(all) enum WithdrawError {
  InsufficientFunds(UInt64)
  AccountClosed
}

#derive.agent
struct Wallet {
  owner : String
  mut balance : UInt64
  mut closed : Bool
}

fn Wallet::new(owner : String) -> Wallet {
  { owner, balance: 0, closed: false }
}

pub fn Wallet::withdraw(self : Self, amount : UInt64) -> Result[UInt64, WithdrawError] {
  if self.closed {
    Err(AccountClosed)
  } else if amount > self.balance {
    Err(InsufficientFunds(self.balance))
  } else {
    self.balance = self.balance - amount
    Ok(self.balance)
  }
}

Returning Err(...) completes the invocation successfully — the caller receives the error as a value. Letting an exception propagate out of the method (e.g. an unhandled raise, or abort(...)) instead fails the invocation without retry.

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.