Skip to main content

Engine

Struct Engine 

Source
pub struct Engine<'a> { /* private fields */ }
Expand description

Core execution engine for GBA.

The engine orchestrates AI-assisted workflows by:

  1. Loading task configurations from the tasks/ directory
  2. Rendering system and user prompts using templates
  3. Configuring and invoking the Claude agent
  4. Processing responses and extracting results

§Example

use gba_core::{Engine, EngineConfig, Task, TaskKind};
use gba_pm::PromptManager;
use serde_json::json;

// Create and configure the prompt manager
let mut prompts = PromptManager::new();
prompts.load_dir("./tasks")?;

// Create the engine
let config = EngineConfig::builder()
    .workdir(".")
    .prompts(prompts)
    .build();
let engine = Engine::new(config)?;

// Run a task
let task = Task::new(TaskKind::Init, json!({"repo_path": "."}));
let result = engine.run(task).await?;
println!("Success: {}", result.success);

Implementations§

Source§

impl<'a> Engine<'a>

Source

pub fn new(config: EngineConfig<'a>) -> Result<Self>

Create a new engine with the given configuration.

§Arguments
  • config - Engine configuration including workdir and prompts
§Errors

Returns an error if the configuration is invalid.

Source

pub async fn run(&self, task: Task) -> Result<TaskResult>

Run a task and return the result.

This method:

  1. Loads the task configuration from tasks/<kind>/config.yml
  2. Renders the system and user prompts from templates
  3. Configures the Claude agent based on the task config
  4. Executes the query and processes the response
§Arguments
  • task - The task to execute
§Errors

Returns an error if:

  • The task configuration cannot be loaded
  • The prompt templates cannot be rendered
  • The Claude agent query fails
Source

pub async fn run_stream( &self, task: Task, handler: &mut impl EventHandler, ) -> Result<TaskResult>

Run a task with streaming events.

This method is similar to run() but streams events to the provided handler during execution, enabling real-time feedback and progress tracking.

§Arguments
  • task - The task to execute
  • handler - Event handler for streaming events
§Errors

Returns an error if:

  • The task configuration cannot be loaded
  • The prompt templates cannot be rendered
  • The Claude agent query fails
§Example
use gba_core::{Engine, EngineConfig, Task, TaskKind};
use gba_core::event::PrintEventHandler;
use gba_pm::PromptManager;
use serde_json::json;

let mut prompts = PromptManager::new();
prompts.load_dir("./tasks")?;

let config = EngineConfig::builder()
    .workdir(".")
    .prompts(prompts)
    .build();
let engine = Engine::new(config)?;

let task = Task::new(TaskKind::Init, json!({"repo_path": "."}));
let mut handler = PrintEventHandler::new().with_auto_flush();
let result = engine.run_stream(task, &mut handler).await?;
Source

pub fn session(&self, session_id: Option<String>) -> Result<Session>

Create an interactive session for multi-turn conversations.

Sessions maintain a persistent connection to Claude and track conversation history and statistics across multiple turns.

§Arguments
  • session_id - Optional session ID; if None, a UUID is generated
§Errors

Returns an error if the session cannot be created.

§Example
use gba_core::{Engine, EngineConfig};
use gba_pm::PromptManager;

let mut prompts = PromptManager::new();
let config = EngineConfig::builder()
    .workdir(".")
    .prompts(prompts)
    .build();
let engine = Engine::new(config)?;

let mut session = engine.session(None)?;
let response = session.send("Hello Claude!").await?;
println!("Claude: {}", response);

// Follow-up in same session
let response = session.send("Tell me more").await?;

// Check accumulated stats
let stats = session.stats();
println!("Total turns: {}", stats.turns);

session.disconnect().await?;
Source

pub fn session_with_task( &self, task_kind: &TaskKind, context: &Value, session_id: Option<String>, ) -> Result<Session>

Create a session with a specific task configuration.

This method creates a session that uses the configuration from a specific task type, including its system prompt and tool settings.

§Arguments
  • task_kind - The task kind to use for configuration
  • context - Context for rendering the system prompt template
  • session_id - Optional session ID
§Errors

Returns an error if the task configuration cannot be loaded or the session cannot be created.

Trait Implementations§

Source§

impl Debug for Engine<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for Engine<'a>

§

impl<'a> !RefUnwindSafe for Engine<'a>

§

impl<'a> Send for Engine<'a>

§

impl<'a> Sync for Engine<'a>

§

impl<'a> Unpin for Engine<'a>

§

impl<'a> !UnwindSafe for Engine<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more