MCPcopy Index your code
hub / github.com/Sollimann/bonsai

github.com/Sollimann/bonsai @py-v0.13.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release py-v0.13.0 ↗ · + Follow
544 symbols 1,298 edges 63 files 168 documented · 31%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Bonsai 盆栽

<em>Rust implementation of Behavior Trees</em>

Build Status Bonsai crate PyPI minimum rustc 1.72 Docs codecov Maintenance GitHub pull-requests GitHub pull-requests closed ViewCount License: MIT

Contents

Using Bonsai

Rust

Bonsai is available on crates.io. The recommended way to use it is to add a line into your Cargo.toml such as:

[dependencies]
bonsai-bt = "*"

Python

Bonsai is available on PyPI as bonsai-bt:

pip install bonsai-bt

See bonsai-py/ for examples.

Example of use

See Examples folder.

What is a Behavior Tree?

A Behavior Tree (BT) is a data structure in which we can set the rules of how certain behavior's can occur, and the order in which they would execute. BTs are a very efficient way of creating complex systems that are both modular and reactive. These properties are crucial in many applications, which has led to the spread of BT from computer game programming to many branches of AI and Robotics.

How to use a Behavior tree?

A Behavior Tree forms a tree structure where each node represents a process. When the process terminates, it signals Success or Failure. This can then be used by the parent node to select the next process. A signal Running is used to tell the process is not done yet.

For example, if you have a state A and a state B:

  • Move from state A to state B if A succeeds: Sequence([A, B])
  • Try A first and then try B if A fails: Select([A, B])
  • If condition succeedes do A, else do B : If(condition, A, B)
  • If A succeeds, return failure (and vice-versa): Invert(A)
  • Do A, B repeatedly while LoopCondition runs: While(LoopCondition, [A, B]). Checks condition node between nodes A, B.
  • Do A, B forever: While(WaitForever, [A, B])
  • Do A, B repeatedly while LoopCondition runs: WhileAll(LoopCondition, [A, B]). After All nodes A, B are completed successfully, check the condition node.
  • Run A and B in parallell and wait for both to succeed: WhenAll([A, B])
  • Run A and B in parallell and wait for any to succeed: WhenAny([A, B])
  • Run A and B in parallell and wait for any to complete regardless of success or failure: Race([A, B])
  • Run A and B in parallell, but A has to succeed before B: After([A, B])

See the Behavior enum for more information.

Calling long-running tasks in behavior tree

To make sure that the behavior tree is always responsive, it is important that the actions that are created executes instantly so that they do not block the tree traversal. If you have long-running tasks/functions that can take seconds or minutes to execute - either async or sync - then we can dispatch those jobs into background threads, and get status of the task through a channel.

see async drone example in the /examples folder for more details.

Extension points exported contracts — how you extend this code

Tracer (Interface)
Tick-time recording sink, monomorphized into `State::tick`. `IS_RECORDING` is a const switch — when `false`, the optimi [2 …
bonsai/src/tracer.rs
UpdateEvent (Interface)
When the application state should be updated. [1 implementers]
bonsai/src/event.rs

Core symbols most depended-on inside this repo

tick
called by 69
bonsai/src/bt.rs
clone
called by 33
bonsai-py/src/behavior.rs
reset_bt
called by 29
bonsai/src/bt.rs
memory
called by 26
bonsai/src/behavior.rs
record
called by 20
bonsai/src/tracer.rs
get_graphviz_with_graph_instance
called by 18
bonsai/src/bt_telemetry.rs
tick_recording
called by 17
bonsai/src/bt_telemetry.rs
is_finished
called by 11
bonsai/src/bt.rs

Shape

Function 240
Method 195
Class 82
Enum 25
Interface 2

Languages

Rust56%
Python44%

Modules by API surface

bonsai/tests/behavior_tests.rs50 symbols
bonsai-py/tests/test_bt.py41 symbols
bonsai-py/tests/test_behavior.py41 symbols
bonsai-py/tests/test_memory.py22 symbols
bonsai/src/visualizer.rs21 symbols
bonsai-py/src/behavior.rs21 symbols
bonsai/src/telemetry.rs18 symbols
bonsai/tests/visualizer_server_tests.rs17 symbols
bonsai/src/event.rs17 symbols
bonsai/tests/telemetry_tracer_tests.rs14 symbols
bonsai-py/tests/test_action_args.py14 symbols
bonsai-py/examples/simple_npc_ai.py13 symbols

For agents

$ claude mcp add bonsai \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page