---
A minimal stateful agent in the bundle target: instantiation lowers through the
generated factory, the method call is a direct call, and state persists per key
across calls within a session.
---
context demo.counter

exports transparent { CounterError }

type CounterId = String where Matches("[a-z0-9]{1,16}")

type CounterError = enum {
	Overflow,
}

agent Counter {
	key id: CounterId

	store count: Cell[Int]

	on call increment() -> Effect[Result[Int, CounterError]] {
		let next = count + 1
		count := next
		Ok(next)
	}

	on call current() -> Effect[Result[Int, CounterError]] {
		Ok(count)
	}
}

service bump {
	on call(id: CounterId) -> Effect[Result[Int, CounterError]] {
		let counter = Counter(id)
		let total <- counter.increment()
		total
	}
}

service read {
	on call(id: CounterId) -> Effect[Result[Int, CounterError]] {
		let counter = Counter(id)
		let total <- counter.current()
		total
	}
}
