Agent
Agents are the core building blocks of Spice Framework.
What is an Agent?β
An Agent is an autonomous unit that:
- Processes incoming communications
- Executes tools and actions
- Maintains internal state
- Interacts with other agents
Agent Interfaceβ
interface Agent : Identifiable {
val id: String
val name: String
val description: String
val capabilities: List<String>
suspend fun processComm(comm: Comm): SpiceResult<Comm>
fun canHandle(comm: Comm): Boolean
fun getTools(): List<Tool>
fun isReady(): Boolean
}
Creating Agentsβ
Using DSLβ
val agent = buildAgent {
id = "my-agent"
name = "My Agent"
description = "Does amazing things"
handle { comm ->
// Process communication - returns SpiceResult<Comm>
SpiceResult.success(
comm.reply("Processed!", id)
)
}
}
With Toolsβ
val agent = buildAgent {
id = "tool-agent"
name = "Tool Agent"
// Add inline tool
tool("analyze") {
description = "Analyze data"
parameter("data", "string", "Data to analyze")
execute { params ->
ToolResult.success("Analysis: ${params["data"]}")
}
}
handle { comm ->
val result = run("analyze", mapOf("data" to comm.content))
comm.reply(result.result, id)
}
}
BaseAgentβ
Extend BaseAgent for custom agents:
class CustomAgent(
id: String,
name: String
) : BaseAgent(id, name, "Custom agent") {
override suspend fun processComm(comm: Comm): SpiceResult<Comm> {
// Custom processing logic - returns SpiceResult
return SpiceResult.success(
comm.reply("Custom response", id)
)
}
}
Agent Lifecycleβ
- Initialization:
initialize(runtime: AgentRuntime) - Processing:
processComm(comm: Comm) - Cleanup:
cleanup()
Agent Metricsβ
Track agent performance:
val metrics = agent.getMetrics()
println("Total requests: ${metrics.totalRequests}")
println("Success rate: ${metrics.getSuccessRate()}")
println("Avg response time: ${metrics.getAverageResponseTimeMs()}ms")