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
impl SystemPromptBuilder
Sourcepub fn new() -> SystemPromptBuilder
pub fn new() -> SystemPromptBuilder
Creates a new system prompt builder.
Sourcepub fn track<T>(&mut self, tool: T) -> Twhere
T: ToolContext,
pub fn track<T>(&mut self, tool: T) -> Twhere
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 collectionFor 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();Sourcepub fn add_context(
self,
name: &'static str,
context: &'static str,
) -> SystemPromptBuilder
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"));Sourcepub fn system_prompt(self, prompt: impl Into<String>) -> SystemPromptBuilder
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
impl SystemPromptBuilder
Sourcepub fn working_directory(self, path: impl Into<String>) -> SystemPromptBuilder
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:
&strStringPathBufor&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());Sourcepub fn allowed_paths(
self,
resolver: &AllowedPathResolver,
) -> SystemPromptBuilder
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);