SystemPromptBuilder

Struct SystemPromptBuilder 

Source
pub struct SystemPromptBuilder { /* private fields */ }
Expand description

Builder that tracks tools and generates formatted system prompts.

The environment section is always included and appears before tool listings.

§Example

use llm_coding_tools_core::context::{ToolContext, READ_ABSOLUTE};
use llm_coding_tools_core::SystemPromptBuilder;

struct ReadTool;

impl ToolContext for ReadTool {
    const NAME: &'static str = "read";

    fn context(&self) -> &'static str {
        READ_ABSOLUTE
    }
}

let mut pb = SystemPromptBuilder::new()
    .working_directory(std::env::current_dir().unwrap().display().to_string());

pb.track(ReadTool);

let _prompt = pb.build();

§Output

The generated system prompt is Markdown. For example, with two tools:

# Environment

Working directory: /home/user/project

# Tool Usage Guidelines

## `Read` Tool
Reads files from disk.
## `Bash` Tool
Executes shell commands.

Implementations§

Source§

impl SystemPromptBuilder

Source

pub fn new() -> SystemPromptBuilder

Creates a new system prompt builder.

Source

pub fn track<T>(&mut self, tool: T) -> T
where T: ToolContext,

Records context and returns tool unchanged.

Use this to wrap tools before registering them with your tool collection:

use llm_coding_tools_core::context::{ToolContext, READ_ABSOLUTE};
use llm_coding_tools_core::SystemPromptBuilder;

struct MyTool;

impl ToolContext for MyTool {
    const NAME: &'static str = "read";

    fn context(&self) -> &'static str {
        READ_ABSOLUTE
    }
}

let mut pb = SystemPromptBuilder::new();
let _my_tool = pb.track(MyTool);
// register _my_tool with your tool collection

For example, if working with rig’s agent builder:

let mut pb = SystemPromptBuilder::new();
let agent = client
    .agent("gpt-4o")
    .tool(pb.track(ReadTool::new()))
    .system prompt(&pb.build())
    .build();
Source

pub fn add_context( self, name: &'static str, context: &'static str, ) -> SystemPromptBuilder

Adds supplemental context to the system prompt.

Supplemental context appears in a separate “Supplemental Context” section after tool usage guidelines. Use this for guidance that isn’t inherent to a specific tool, such as git workflows or GitHub CLI patterns.

§Arguments
  • name - Section header (e.g., “Git Workflow”, “GitHub CLI”)
  • context - Context string content (e.g., GIT_WORKFLOW)
§Examples

Adding both git and GitHub CLI context:

use llm_coding_tools_core::{SystemPromptBuilder, context};

let pb = SystemPromptBuilder::new()
    .add_context("Git Workflow", context::GIT_WORKFLOW)
    .add_context("GitHub CLI", context::GITHUB_CLI);

let prompt = pb.build();
assert!(prompt.contains("# Supplemental Context"));
assert!(prompt.contains("## Git Workflow"));

Selective inclusion - adding only Git Workflow when not using GitHub features:

use llm_coding_tools_core::{SystemPromptBuilder, context};

// Only include git workflow for agents that use git but not GitHub
let pb = SystemPromptBuilder::new()
    .add_context("Git Workflow", context::GIT_WORKFLOW);

let prompt = pb.build();
assert!(prompt.contains("## Git Workflow"));
assert!(!prompt.contains("## GitHub CLI"));
Source

pub fn system_prompt(self, prompt: impl Into<String>) -> SystemPromptBuilder

Sets a custom system prompt that appears first in the generated system prompt.

The provided prompt is prepended before all other sections (environment, tools, supplemental context). User provides exactly what they want, including any markdown headers - no auto-modification is applied.

§Example
use llm_coding_tools_core::SystemPromptBuilder;

let pb = SystemPromptBuilder::new()
    .system_prompt("# System Instructions\n\nYou are a helpful assistant.");

let prompt = pb.build();
assert!(prompt.starts_with("# System Instructions"));
Source§

impl SystemPromptBuilder

Source

pub fn working_directory(self, path: impl Into<String>) -> SystemPromptBuilder

Sets the working directory to display in the environment section.

Accepts any type that can be converted to String, including:

  • &str
  • String
  • PathBuf or &Path (via .display().to_string())
§Example
use llm_coding_tools_core::SystemPromptBuilder;

let _pb = SystemPromptBuilder::new()
    .working_directory("/home/user/project");

// With runtime-computed path
let _pb = SystemPromptBuilder::new()
    .working_directory(std::env::current_dir().unwrap().display().to_string());
Source

pub fn allowed_paths( self, resolver: &AllowedPathResolver, ) -> SystemPromptBuilder

Sets the allowed directories to display in the environment section.

Takes an AllowedPathResolver reference and extracts its allowed paths for display. Paths are already canonicalized (absolute, symlinks resolved) by the resolver during construction.

§Example
use llm_coding_tools_core::{AllowedPathResolver, SystemPromptBuilder};

let resolver = AllowedPathResolver::new(vec!["/home/user/project", "/tmp"]).unwrap();
let _pb = SystemPromptBuilder::new()
    .working_directory("/home/user/project")
    .allowed_paths(&resolver);
Source§

impl SystemPromptBuilder

Source

pub fn build(self) -> String

Generates the system prompt string with environment section.

Trait Implementations§

Source§

impl Default for SystemPromptBuilder

Source§

fn default() -> SystemPromptBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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
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
Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,