Conjure.Executor behaviour (Conjure v0.1.1-alpha)

View Source

Behaviour for tool execution backends.

Executors are responsible for running tool operations (bash commands, file reads/writes) in an execution environment. Different executors provide different levels of isolation:

Implementing a Custom Executor

To create a custom executor, implement all required callbacks:

defmodule MyApp.FirecrackerExecutor do
  @behaviour Conjure.Executor

  @impl true
  def init(context) do
    # Start Firecracker microVM
    {:ok, %{context | vm_id: vm_id}}
  end

  @impl true
  def bash(command, context) do
    # Execute in microVM
    {:ok, output}
  end

  @impl true
  def view(path, context, opts) do
    # Read from microVM
    {:ok, content}
  end

  @impl true
  def create_file(path, content, context) do
    {:ok, "File created"}
  end

  @impl true
  def str_replace(path, old_str, new_str, context) do
    {:ok, "File updated"}
  end

  @impl true
  def cleanup(context) do
    # Shutdown microVM
    :ok
  end
end

Result Types

All execution callbacks return:

  • {:ok, output} - Successful execution with string output
  • {:ok, output, files} - Execution with output and generated files
  • {:error, reason} - Execution failed

Summary

Callbacks

Execute a bash command.

Cleanup the execution environment.

Create a new file with content.

Initialize the execution environment.

Replace a string in a file.

Read a file or directory listing.

Functions

Executes a tool call using the specified executor.

Types

file_output()

@type file_output() :: %{path: Path.t(), content: binary()}

result()

@type result() ::
  {:ok, String.t()} | {:ok, String.t(), [file_output()]} | {:error, term()}

Callbacks

bash(command, context)

@callback bash(command :: String.t(), context :: Conjure.ExecutionContext.t()) :: result()

Execute a bash command.

cleanup(context)

(optional)
@callback cleanup(context :: Conjure.ExecutionContext.t()) :: :ok

Cleanup the execution environment.

Called when the session ends to clean up resources.

create_file(path, content, context)

@callback create_file(
  path :: Path.t(),
  content :: String.t(),
  context :: Conjure.ExecutionContext.t()
) :: result()

Create a new file with content.

init(context)

(optional)
@callback init(context :: Conjure.ExecutionContext.t()) ::
  {:ok, Conjure.ExecutionContext.t()} | {:error, term()}

Initialize the execution environment.

Called once per session to set up the execution environment. For stateless executors, this can be a no-op that returns the context unchanged. For container-based executors, this starts the container.

str_replace(path, old_str, new_str, context)

@callback str_replace(
  path :: Path.t(),
  old_str :: String.t(),
  new_str :: String.t(),
  context :: Conjure.ExecutionContext.t()
) :: result()

Replace a string in a file.

view(path, context, opts)

@callback view(
  path :: Path.t(),
  context :: Conjure.ExecutionContext.t(),
  opts :: keyword()
) :: result()

Read a file or directory listing.

Options

  • :view_range - [start_line, end_line] for partial file reads

Functions

execute(tool_call, context, executor)

Executes a tool call using the specified executor.

This is a convenience function that dispatches to the appropriate executor callback based on the tool name.