State¶
Manages conversation data, results, and memory.
Class: RobotLab::State¶
Attributes¶
thread_id¶
Conversation thread identifier for persistence.
memory¶
Shared key-value store.
Methods¶
data¶
Access workflow data as a proxy object.
state.data[:user_id] # Hash access
state.data.user_id # Method access
state.data[:status] = "active"
results¶
All robot execution results.
messages¶
Formatted conversation messages for LLM.
append_result¶
Add a robot result to history.
set_results¶
Replace all results.
results_from¶
Get results starting at index.
thread_id=¶
Set the thread identifier.
format_history¶
Format results as conversation history.
clone¶
Create a deep copy.
to_h¶
Hash representation.
to_json¶
JSON representation.
from_hash (class method)¶
Restore from hash.
StateProxy¶
The data attribute is a StateProxy:
proxy = state.data
# Hash-style access
proxy[:key]
proxy[:key] = value
# Method-style access
proxy.key
proxy.key = value
# Hash operations
proxy.key?(:key)
proxy.keys
proxy.values
proxy.each { |k, v| ... }
proxy.merge!(other_hash)
proxy.delete(:key)
proxy.to_h
proxy.empty?
proxy.size
Creating State¶
Basic¶
With Data¶
state = RobotLab.create_state(
message: "Process order",
data: {
user_id: "user_123",
order_id: "ord_456"
}
)
With Thread ID¶
# Via UserMessage
message = UserMessage.new("Continue", thread_id: "thread_123")
state = RobotLab.create_state(message: message)
# Direct assignment
state = RobotLab.create_state(message: "Continue")
state.thread_id = "thread_123"
With Existing Results¶
UserMessage¶
Enhanced message with metadata:
message = UserMessage.new(
"What's my order status?",
thread_id: "thread_123",
system_prompt: "Respond in Spanish",
metadata: { source: "web" }
)
message.content # => "What's my order status?"
message.thread_id # => "thread_123"
message.system_prompt # => "Respond in Spanish"
message.metadata # => { source: "web" }
message.id # => UUID
message.created_at # => Time
Examples¶
Accessing Data¶
state = RobotLab.create_state(
message: "Help",
data: { user: { name: "Alice", plan: "pro" } }
)
state.data[:user][:name] # => "Alice"
state.data.to_h # => { user: { name: "Alice", plan: "pro" } }
Working with Results¶
# After running network
state.results.size # Number of results
state.results.last # Most recent
state.results.map(&:robot_name) # ["classifier", "support"]
Using Memory¶
state.memory.remember("intent", "billing")
intent = state.memory.recall("intent")
scoped = state.memory.scoped("user:123")
scoped.remember("preference", "dark_mode")
Serialization¶
# Save state
json = state.to_json
File.write("state.json", json)
# Restore state
data = JSON.parse(File.read("state.json"))
state = State.from_hash(data)