Queue Plugin
Lightweight in-memory queue management system for batching and deferred execution of AI agent tasks.
Overview
The @tokenring-ai/queue
package provides a lightweight, in-memory queue management system for the Token Ring AI agent framework. It enables the queuing of work items, such as chat prompts or tasks, to be processed sequentially while preserving and restoring agent state via checkpoints.
Key Features
- FIFO (First-In-First-Out) queue structure
- Optional size limits
- State preservation via checkpoints
- Chat commands for interactive management
- Tool for programmatic task enqueueing
- Sequential processing with state restoration
Core Components
WorkQueueService
Primary service for queue operations.
Key Methods:
constructor({ maxSize? })
: Initializes with optional queue size limitattach(agent)
: Initializes the state slice on the agentstartWork(agent)
: Setsstarted = true
stopWork(agent)
: Setsstarted = false
and clears current itemstarted(agent)
: Checks if processing is activeenqueue(item, agent)
: Adds to end of queue (returns false if full)dequeue(agent)
: Removes and returns front itemget(idx, agent)
: Retrieves item by indexsplice(start, deleteCount, agent, ...items)
: Modifies queuesize(agent)
: Current queue lengthisEmpty(agent)
: Checks if queue is emptyclear(agent)
: Empties the queuegetAll(agent)
: Returns a copy of all items
Chat Commands
/queue: Interactive command for managing the queue
add <prompt>
: Enqueues a new itemremove <index>
: Removes item at indexdetails <index>
: Displays JSON detailsclear
: Empties the queuelist
: Lists all items with indicesstart
: Initializes queue sessionnext
/done
: Advances to next item or ends sessionskip
: Moves current item to endrun
: Executes the current item's input
Tools
addTaskToQueue: Programmatic tool for enqueuing tasks
- Input:
{ description, content }
- Captures current agent checkpoint
- Returns:
{ status, message }
Usage Examples
Programmatic Queue Management
import { Agent } from '@tokenring-ai/agent';
import { WorkQueueService } from '@tokenring-ai/queue';
const agent = new Agent(/* config */);
const queueService = new WorkQueueService({ maxSize: 10 });
await queueService.attach(agent);
// Enqueue a task
const item = {
checkpoint: agent.generateCheckpoint(),
name: 'Analyze data',
input: [{ role: 'user', content: 'Analyze the sales data and report trends.' }]
};
const added = queueService.enqueue(item, agent);
console.log(`Added: ${added}, Size: ${queueService.size(agent)}`);
// Process front item
const nextItem = queueService.dequeue(agent);
if (nextItem) {
agent.restoreCheckpoint(nextItem.checkpoint);
// Execute nextItem.input
}
Using Chat Commands
/queue add Generate report on user metrics
/queue add Fix bug in authentication
/queue list // Shows: [0] Generate report... [1] Fix bug...
/queue start // Begins session
/queue next // Loads first item
/queue run // Executes it
/queue next // Moves to second
/queue done // Ends and restores initial state
Enqueuing via Tool
import { tools } from '@tokenring-ai/queue';
await tools.addTaskToQueue.execute(
{
description: 'Optimize query performance',
content: 'Review database queries, identify bottlenecks, and suggest indexes.'
},
agent
);
Configuration Options
maxSize
(number, optional): Limits queue length (defaults to unlimited)- No environment variables required
- All configuration via constructor or agent state
Dependencies
@tokenring-ai/ai-client@0.1.0
: For ChatInputMessage, runChat@tokenring-ai/agent@0.1.0
: Core agent framework@tokenring-ai/history@0.1.0
: Checkpoint operationstypescript@^5.9.2
: Developmentzod
: Schema validation