c6b13f7cc8
Session interface decouples UI from engine via channels: - Send(input) starts agentic turn in background goroutine - Events() returns channel for streaming events - TurnResult() returns completed Turn after drain - Cancel() propagates context cancellation - Status() reports state, provider, model, token usage, turn count Local implementation: engine runs on dedicated goroutine per turn, events pushed to buffered channel (64), context cancellation propagated. 5 tests.
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package session
|
|
|
|
import (
|
|
"somegit.dev/Owlibou/gnoma/internal/engine"
|
|
"somegit.dev/Owlibou/gnoma/internal/stream"
|
|
)
|
|
|
|
// SessionState tracks the current state of a session.
|
|
type SessionState int
|
|
|
|
const (
|
|
StateIdle SessionState = iota
|
|
StateStreaming
|
|
StateToolExec
|
|
StateCancelled
|
|
StateError
|
|
StateClosed
|
|
)
|
|
|
|
func (s SessionState) String() string {
|
|
switch s {
|
|
case StateIdle:
|
|
return "idle"
|
|
case StateStreaming:
|
|
return "streaming"
|
|
case StateToolExec:
|
|
return "tool_exec"
|
|
case StateCancelled:
|
|
return "cancelled"
|
|
case StateError:
|
|
return "error"
|
|
case StateClosed:
|
|
return "closed"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
// Status holds observable session state.
|
|
type Status struct {
|
|
State SessionState
|
|
Provider string
|
|
Model string
|
|
TokensUsed int64
|
|
TurnCount int
|
|
}
|
|
|
|
// Session is the boundary between UI and engine.
|
|
// All communication is via channels. No shared mutable state.
|
|
type Session interface {
|
|
// Send submits user input and begins an agentic turn.
|
|
Send(input string) error
|
|
// Events returns the channel that receives streaming events.
|
|
// A new channel is created per Send(). Closed when the turn completes.
|
|
Events() <-chan stream.Event
|
|
// TurnResult returns the completed Turn after Events() is drained.
|
|
TurnResult() (*engine.Turn, error)
|
|
// Cancel aborts the current turn.
|
|
Cancel()
|
|
// Close shuts down the session.
|
|
Close() error
|
|
// Status returns current session state.
|
|
Status() Status
|
|
}
|