home / skills / benchflow-ai / skillsbench / dialogue_graph

This skill helps you build, validate, visualize, and export dialogue graphs for scripts, editors, or game logic with easy-to-use Python tools.

npx playbooks add skill benchflow-ai/skillsbench --skill dialogue_graph

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
2.3 KB
---
name: dialogue-graph
description: A library for building, validating, visualizing, and serializing dialogue graphs. Use this when parsing scripts or creating branching narrative structures.
---

# Dialogue Graph Skill

This skill provides a `dialogue_graph` module to easily build valid dialogue trees/graphs.

## When to use

*   **Script Parsers**: When converting text to data.
*   **Dialogue Editors**: When building tools to edit conversation flow.
*   **Game Logic**: When traversing a dialogue tree.
*   **Visualization**: When generating visual diagrams of dialogue flows.

## How to use

Import the module:
```python
from dialogue_graph import Graph, Node, Edge
```

### 1. The `Graph` Class
The main container.

```python
graph = Graph()
```

### 2. Adding Nodes
Define content nodes.

```python
# Regular line
graph.add_node(Node(id="Start", speaker="Guard", text="Halt!", type="line"))

# Choice hub
graph.add_node(Node(id="Choices", type="choice"))
```

### 3. Adding Edges
Connect nodes (transitions).

```python
# Simple transition
graph.add_edge(Edge(source="Start", target="Choices"))

# Choice transition (with text)
graph.add_edge(Edge(source="Choices", target="End", text="1. Run away"))
```

### 4. Export
Serialize to JSON format for the engine.

```python
data = graph.to_dict()
# returns {"nodes": [...], "edges": [...]}
json_str = graph.to_json()
```

### 5. Validation
Check for integrity.

```python
errors = graph.validate()
# Returns list of strings, e.g., ["Edge 'Start'->'Unk' points to missing node 'Unk'"]
```

### 6. Visualization
Generate a PNG/SVG graph diagram.

```python
# Requires: pip install graphviz
# Also requires Graphviz binary: https://graphviz.org/download/

graph.visualize('dialogue_graph')  # Creates dialogue_graph.png
graph.visualize('output', format='svg')  # Creates output.svg
```

The visualization includes:
- **Diamond shapes** for choice nodes (light blue)
- **Rounded boxes** for dialogue nodes (colored by speaker)
- **Bold blue edges** for skill-check choices like `[Lie]`, `[Attack]`
- **Gray edges** for regular choices
- **Black edges** for simple transitions

### 7. Loading from JSON
Load an existing dialogue graph.

```python
# From file
graph = Graph.from_file('dialogue.json')

# From dict
graph = Graph.from_dict({'nodes': [...], 'edges': [...]})

# From JSON string
graph = Graph.from_json(json_string)
```

Overview

This skill provides a compact library for building, validating, visualizing, and serializing dialogue graphs for branching narratives and scripts. It exposes simple Graph, Node, and Edge primitives to construct dialogue trees or general conversation graphs. The tool focuses on data integrity, easy export to JSON, and visual output suitable for editors and game engines.

How this skill works

Create a Graph object, add Node objects to represent lines, choices, or hubs, and connect them with Edge objects that represent transitions or choice options. The library validates graph integrity, reports missing node references or other issues, and can serialize the structure to a JSON-friendly dict or string. It also generates PNG or SVG visualizations using Graphviz, applying distinct shapes and colors for choice hubs, dialogue lines, speakers, and special edges.

When to use it

  • Parsing scripted text into a structured dialogue data model for runtime or tooling.
  • Designing or editing branching conversation flows in a dialogue editor.
  • Implementing game logic that must traverse or evaluate branching dialogues.
  • Generating visual diagrams for documentation or QA of narrative structure.
  • Serializing dialogue content for import into engines or localization pipelines.

Best practices

  • Assign stable, human-readable node IDs to make graph diffs and merges easier.
  • Use choice-type nodes for branching hubs and line-type nodes for spoken text.
  • Run graph.validate() after batch edits or imports to catch broken edges early.
  • Color or tag nodes by speaker to simplify visual reviews and localization checks.
  • Keep edges small and descriptive; include choice text on choice edges when needed.

Example use cases

  • Convert a screenplay or chat script into a structured dialogue JSON for an engine.
  • Build a visual dialogue editor that lets writers drag nodes and export valid graphs.
  • Automate QA that checks for unreachable nodes, cycles, or missing endpoints.
  • Render developer-facing SVG diagrams to review branching complexity before release.
  • Load and modify existing dialogue files programmatically for localization or tuning.

FAQ

How do I create a choice with labeled options?

Add a choice-style Node as the hub, then add Edge entries from that node to targets with the option text set on each Edge.

Can I load a graph from an existing JSON file?

Yes. Use Graph.from_file, Graph.from_dict, or Graph.from_json to recreate the Graph object from serialized data.

What do I need to generate visual diagrams?

Install the graphviz Python package and the Graphviz binary for your OS; then call graph.visualize(filename, format) to produce PNG or SVG outputs.