async

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine wraps an engine.Session with queue-based async operation. Submissions are processed in order; events are broadcast to all subscribers.

func New

func New(sess *engine.Session) *Engine

New creates an async engine wrapping the given session.

func (*Engine) Cancel

func (e *Engine) Cancel()

Cancel enqueues a cancellation for the current turn.

func (*Engine) Events

func (e *Engine) Events() <-chan *Event

Events returns a channel for consuming events.

func (*Engine) Replay

func (e *Engine) Replay() []*Event

Replay returns all events since the engine started.

func (*Engine) Session

func (e *Engine) Session() *engine.Session

Session returns the underlying engine session.

func (*Engine) Start

func (e *Engine) Start(ctx context.Context)

Start begins processing submissions in the background.

func (*Engine) Stop

func (e *Engine) Stop()

Stop gracefully stops the engine.

func (*Engine) Submit

func (e *Engine) Submit(message string) *Submission

Submit enqueues a user turn for processing.

type Event

type Event struct {
	ID        string
	Type      EventType
	Content   string
	ToolName  string
	ToolID    string
	Usage     *Usage
	Timestamp time.Time
	SessionID string
}

Event is a single event from the async engine.

type EventQueue

type EventQueue struct {
	// contains filtered or unexported fields
}

EventQueue is a thread-safe, replayable queue of events.

func NewEventQueue

func NewEventQueue(maxSize int) *EventQueue

NewEventQueue creates an event queue with the given max capacity.

func (*EventQueue) Push

func (eq *EventQueue) Push(evt *Event)

Push adds an event and broadcasts it to all subscribers.

func (*EventQueue) Replay

func (eq *EventQueue) Replay() []*Event

Replay returns all stored events for replay.

func (*EventQueue) Subscribe

func (eq *EventQueue) Subscribe() <-chan *Event

Subscribe returns a channel that receives new events.

type EventType

type EventType string

EventType identifies the kind of event emitted by the engine.

const (
	EventToken      EventType = "token"
	EventToolCall   EventType = "tool_call"
	EventToolResult EventType = "tool_result"
	EventDone       EventType = "done"
	EventError      EventType = "error"
	EventThinking   EventType = "thinking"
	EventUsage      EventType = "usage"
)

type OpType

type OpType string

OpType identifies the kind of operation submitted to the engine.

const (
	OpUserTurn  OpType = "user_turn"
	OpCancel    OpType = "cancel"
	OpResume    OpType = "resume"
	OpInterrupt OpType = "interrupt"
)

type Submission

type Submission struct {
	ID        string
	Op        OpType
	Payload   string
	Timestamp time.Time
	ReplyTo   chan<- *Event // optional: caller gets a direct reply
}

Submission represents a user request sent to the async engine.

type SubmissionQueue

type SubmissionQueue struct {
	// contains filtered or unexported fields
}

SubmissionQueue is a thread-safe queue for user submissions.

func NewSubmissionQueue

func NewSubmissionQueue() *SubmissionQueue

NewSubmissionQueue creates a new submission queue.

func (*SubmissionQueue) Len

func (sq *SubmissionQueue) Len() int

Len returns the number of pending submissions.

func (*SubmissionQueue) Next

func (sq *SubmissionQueue) Next() *Submission

Next blocks until a submission is available and returns it.

func (*SubmissionQueue) Submit

func (sq *SubmissionQueue) Submit(s *Submission)

Submit adds a submission and notifies the engine.

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

Usage tracks token usage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL