Golem add agent rust

From golemcloud/golem · View interactive version

Golem add agent rust is an AI agent skill from the GitHub repository golemcloud/golem. Adding a new Rust 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 Rust Golem project.

How to use this skill

Skill file

Adding a New Agent to a Rust Golem Component

Overview

An agent is a durable, stateful unit of computation in Golem. Each agent type is defined as a trait annotated with #[agent_definition] and implemented on a struct annotated with #[agent_implementation].

Steps

  1. Create the agent module — add a new file src/<agent_name>.rs
  2. Define the agent trait — annotate with #[agent_definition]
  3. Implement the agent — annotate with #[agent_implementation]
  4. Re-export from lib.rs — add mod <agent_name>; and pub use <agent_name>::*;
  5. Build — run golem build to verify

Agent Definition

use golem_rust::{agent_definition, agent_implementation};

#[agent_definition]
pub trait MyAgent {
    // Constructor parameters form the agent's key.
    // Addressed by its key, the same parameters reach the same agent.
    fn new(name: String) -> Self;

    // Agent methods — can be sync or async
    fn get_count(&self) -> u32;
    fn increment(&mut self) -> u32;
    async fn fetch_data(&self, url: String) -> String;
}

struct MyAgentImpl {
    name: String,
    count: u32,
}

#[agent_implementation]
impl MyAgent for MyAgentImpl {
    fn new(name: String) -> Self {
        Self { name, count: 0 }
    }

    fn get_count(&self) -> u32 {
        self.count
    }

    fn increment(&mut self) -> u32 {
        self.count += 1;
        self.count
    }

    async fn fetch_data(&self, url: String) -> String {
        // Use golem_rust::http for WASI HTTP p3 requests
        todo!()
    }
}

Custom Types

All parameter and return types must support the schema and wire traits. For custom types, derive Schema, which supplies IntoSchema, FromSchema, IntoWire, FromWire, and WireSchema:

use golem_rust::Schema;
use serde::{Serialize, Deserialize};

#[derive(Clone, Schema, Serialize, Deserialize)]
pub struct MyData {
    pub field1: String,
    pub field2: u32,
}

Returning Failures

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

use golem_rust::{agent_definition, agent_implementation, Schema};
use serde::{Serialize, Deserialize};

#[derive(Clone, Debug, Schema, Serialize, Deserialize)]
pub enum WithdrawError {
    InsufficientFunds { available: u64 },
    AccountClosed,
}

#[agent_definition]
pub trait Wallet {
    fn new(owner: String) -> Self;

    fn withdraw(&mut self, amount: u64) -> Result<u64, WithdrawError>;
}

struct WalletImpl {
    owner: String,
    balance: u64,
    closed: bool,
}

#[agent_implementation]
impl Wallet for WalletImpl {
    fn new(owner: String) -> Self {
        Self { owner, balance: 0, closed: false }
    }

    fn withdraw(&mut self, amount: u64) -> Result<u64, WithdrawError> {
        if self.closed {
            return Err(WithdrawError::AccountClosed);
        }
        if amount > self.balance {
            return Err(WithdrawError::InsufficientFunds { available: self.balance });
        }
        self.balance -= amount;
        Ok(self.balance)
    }
}

Returning Err(WithdrawError::...) completes the invocation successfully — the caller receives the error as a value. The ? operator propagates an error through the method's return type; it does not panic. Panicking with unwrap(), expect(), or panic!() 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.