Skip to main content

AgentsMd

Struct AgentsMd 

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

Discovers and loads AGENTS.md files by walking ancestor directories.

AgentsMd is the primary way to inject project-level instructions into an agent session. It walks upward from a given starting directory, collecting AGENTS.md files according to the configured AgentsMdMode. Explicit paths and extra search directories can be added for cases that fall outside simple ancestor discovery.

Loaded items carry metadata under the agentkit.context.* namespace:

KeyValue
agentkit.context.source"agents_md"
agentkit.context.pathFilesystem path of the file

§Example

use agentkit_context::AgentsMd;
use agentkit_context::ContextSource; // for `.load()`

// Find the nearest AGENTS.md starting from the current directory.
let items = AgentsMd::discover(".").load().await?;

// Or collect all ancestor AGENTS.md files, with an extra search dir.
let items = AgentsMd::discover_all(".")
    .with_search_dir("./.agent")
    .load()
    .await?;

Implementations§

Source§

impl AgentsMd

Source

pub fn discover(start_dir: impl Into<PathBuf>) -> Self

Create a new AgentsMd that searches for the nearest AGENTS.md starting from start_dir and walking upward.

This uses AgentsMdMode::Nearest by default. Call with_mode or use discover_all to collect every ancestor match instead.

Source

pub fn discover_all(start_dir: impl Into<PathBuf>) -> Self

Shorthand for AgentsMd::discover(start_dir).with_mode(AgentsMdMode::All).

Collects every AGENTS.md from the filesystem root down to start_dir, ordered outermost-first so that more specific instructions appear last.

Source

pub fn with_mode(self, mode: AgentsMdMode) -> Self

Set the discovery mode.

See AgentsMdMode for the available options.

Source

pub fn with_file_name(self, file_name: impl Into<String>) -> Self

Override the file name to look for (default: AGENTS.md).

Useful when a project uses a different convention such as CLAUDE.md.

Source

pub fn with_path(self, path: impl Into<PathBuf>) -> Self

Add an explicit file path to include.

The path is checked for existence at load time; if it does not exist it is silently skipped. Explicit paths are loaded before ancestor discovery results.

Source

pub fn with_search_dir(self, dir: impl Into<PathBuf>) -> Self

Add a directory to search for the configured file name.

Unlike ancestor discovery, this checks only the given directory (not its ancestors). This is useful for well-known sidecar locations like .agent/ or .config/.

Source

pub async fn resolve(&self) -> Result<Option<PathBuf>, ContextError>

Resolve the first matching path without reading its contents.

Returns None when no AGENTS.md file is found. This is a convenience wrapper around resolve_all.

§Errors

Returns ContextError if a filesystem metadata check fails.

Source

pub async fn resolve_all(&self) -> Result<Vec<PathBuf>, ContextError>

Resolve all matching paths without reading their contents.

The returned paths are deduplicated and ordered from outermost to innermost. When the mode is AgentsMdMode::Nearest, at most one path is returned.

§Errors

Returns ContextError if a filesystem metadata check fails.

Trait Implementations§

Source§

impl Clone for AgentsMd

Source§

fn clone(&self) -> AgentsMd

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ContextSource for AgentsMd

Source§

fn load<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Item>, ContextError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Load context items from this source. Read more
Source§

impl Debug for AgentsMd

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.