Skip to content

Branch ​

Conversation forks.


Purpose ​

Show alternative responses, create conversation branches, enable A/B testing.

Use <branch> when multiple valid paths should stay visible inside one interaction instead of being flattened into a single narrative.


Try it Live ​

editor.g
12345678910111213141516171819

Edit the Grain document and inspect the rendered preview beside it.

preview

Interactive preview initializes after hydration.

Interactive preview initializes after hydration. Open the docs preview at /grain/; the site is mounted under the project base path.

G-Lang ​

grain
<branch id="alt-1" label="Alternative: Recursive Solution" active="true">
  <message role="assistant">
    <artifact type="code" language="javascript">
      function factorial(n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
      }
    </artifact>
  </message>
</branch>

<branch id="alt-2" label="Alternative: Iterative Solution">
  <message role="assistant">
    <artifact type="code" language="javascript">
      function factorial(n) {
        let result = 1;
        for (let i = 2; i <= n; i++) {
          result *= i;
        }
        return result;
      }
    </artifact>
  </message>
</branch>

Attributes ​

AttributeTypeDescription
idstringUnique branch ID
labelstringBranch label
activetrue | falseIs active branch
mergeabletrue | falseCan merge
parentstringParent branch ID

States ​

CREATED → EXPANDED → ACTIVE
   ↓        ↓         ↓
MERGED  COLLAPSED  MERGING → ACTIVE

Events ​

EventDescription
branch.createBranch created
branch.expandUser expanded
branch.activateUser switched to branch
branch.mergeBranch merged

Examples ​

Two alternatives ​

grain
<message role="assistant">
  <stream>I found two possible solutions:</stream>
</message>

<branch id="opt-1" label="Option A: Quick Fix" active="true">
  <message role="assistant">
    <stream>Quick but temporary solution...</stream>
  </message>
</branch>

<branch id="opt-2" label="Option B: Permanent Fix">
  <message role="assistant">
    <stream>Comprehensive solution...</stream>
  </message>
</branch>

Nested branches ​

grain
<branch id="main" label="Main Discussion" active="true">
  <message role="assistant">
    <stream>Main response...</stream>
  </message>
  
  <branch id="sub-1" label="Detail A">
    <message role="assistant">
      <stream>More details on A...</stream>
    </message>
  </branch>
</branch>

Mergeable branch ​

grain
<branch id="draft" label="Draft Response" mergeable="true">
  <message role="assistant">
    <stream>This is a draft that can be merged...</stream>
  </message>
</branch>

Usage Notes ​

  • Keep branch labels user-readable so the decision surface is understandable without opening every branch.
  • Use active to indicate the current path instead of inferring it from order.
  • Prefer branches when alternatives should remain inspectable; prefer <stream> plus final selection when the alternates are only internal.

Released under the MIT License.