# How the Two-Level Routing Mechanism Works in patent-disclosure-skill

> Understand the two-level routing mechanism in patent-disclosure-skill. This layered architecture separates intent and step routing for efficient patent document generation.

- Repository: [handsomestWei/patent-disclosure-skill](https://github.com/handsomestWei/patent-disclosure-skill)
- Tags: internals
- Published: 2026-09-05

---

**The `patent-disclosure-skill` uses a layered routing architecture that separates intent routing from step-specific view routing to modularize patent document generation.**

This open-source skill framework, maintained at `handsomestWei/patent-disclosure-skill`, implements a clean separation between high-level intent classification and low-level view rendering. The two-level routing mechanism enables flexible handling of complex patent disclosure workflows—from drafting claims to rendering technical figures—by decoupling *what* the user wants from *how* each output is produced.

---

## Overview of the Two-Level Routing Architecture

The routing system operates at distinct layers:

| Level | Purpose | Implementation Location |
|-------|---------|------------------------|
| **Intent Router (Level 1)** | Maps user requests to skill modules | `skills/patent-disclosure/` manifest configuration |
| **Step-to-View Router (Level 2)** | Maps processing steps to view-rendering functions | [`skills/patent-disclosure/tools/step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/step_to_views.py) |

This separation allows the system to handle diverse patent-related tasks through a unified execution pipeline without hardcoding view logic into intent handlers.

---

## Level 1: Intent Router

The **intent router** serves as the entry point for all user requests. It analyzes the user's utterance and selects the appropriate skill module to handle the task.

According to the source code, this router is configured through a YAML-style manifest that defines intent-to-module mappings:

```yaml

# skill manifest (simplified structure from the repo)

intents:
  claim_draft: patent_disclosure.claims
  figure_gen:  patent_disclosure.figures

```

When a request arrives, the intent router performs the following:

1. Parses the user utterance to identify the high-level intent
2. Looks up the corresponding module in the manifest
3. Dispatches to that module's entry point

This top-level routing ensures that requests like *"draft a patent claim"* and *"search CNIPA"* are handled by specialized sub-modules rather than a monolithic handler.

---

## Level 2: Step-to-View Router

Once the intent router has selected the `patent-disclosure` skill, the **step-to-view router** takes over. This second-level mechanism breaks patent generation into discrete, composable steps and maps each to a concrete view-rendering function.

### The STEP_TO_VIEW Mapping

The core of this router lives in [`skills/patent-disclosure/tools/step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/step_to_views.py):

```python

# step_to_views.py (excerpt from source)

STEP_TO_VIEW = {
    "generate_title": render_title,
    "render_figure": render_figure,
    "compose_claim": render_claim,
    # … additional step → view mappings

}

```

Each key is a **step identifier** representing a discrete task in the patent workflow. Each value is a **callable** that produces a specific output format—Markdown for titles, SVG or PNG for figures, structured text for claims.

### Execution via run_step_to_views.py

The orchestration logic resides in [`skills/patent-disclosure/tools/run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/run_step_to_views.py):

```python

# run_step_to_views.py (excerpt)

def run_steps(step_names, context):
    outputs = []
    for name in step_names:
        view_func = STEP_TO_VIEW[name]          # ← second-level routing lookup

        outputs.append(view_func(context))
    return "\n".join(outputs)

```

This function iterates through a workflow-defined list of steps, queries `STEP_TO_VIEW` for each, and executes the mapped function with the provided context. The rendered outputs are then assembled into the final patent disclosure document.

---

## Complete Execution Flow

The two-level routing mechanism executes in four stages:

1. **Intent classification** — The top-level router receives the user request and selects the `patent-disclosure` skill module
2. **Workflow decomposition** — The skill parses the request into a sequential list of processing steps
3. **Step dispatch** — The step-to-view router looks up each step in `STEP_TO_VIEW` and invokes the corresponding view function
4. **Output assembly** — Rendered components are combined and returned to the user

View functions may leverage additional tools from the `tools/` directory, such as:

- [`svg_screenshot.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/svg_screenshot.py) — Captures SVG-based technical drawings
- [`math_render.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/math_render.py) — Renders mathematical expressions for patent formulas

---

## Key Source Files

| File | Role in Two-Level Routing |
|------|--------------------------|
| [`skills/patent-disclosure/tools/step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/step_to_views.py) | Defines `STEP_TO_VIEW` dictionary mapping step names to view functions |
| [`skills/patent-disclosure/tools/run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/run_step_to_views.py) | Orchestrates step execution using the Level 2 router |
| [`skills/patent-disclosure/tools/svg_screenshot.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/svg_screenshot.py) | View implementation for figure generation |
| [`skills/patent-disclosure/tools/math_render.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/math_render.py) | View implementation for mathematical content |
| [`skills/patent-disclosure/README.md`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/README.md) | Documents overall architecture and routing concepts |

---

## Practical Example: Invoking the Routing Stack

```python

# Entry point usage (based on source structure)

from skills.patent_disclosure import router as intent_router

def handle_user_request(text: str):
    # Level 1: Intent routing

    skill_module = intent_router.route_intent(text)
    
    # Level 2: Handled internally by skill_module.run()

    # which calls run_steps() with appropriate step sequence

    return skill_module.run(text)

```

The `skill_module.run()` method internally constructs the step sequence (e.g., `["generate_title", "compose_claim", "render_figure"]`) and delegates to [`run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/run_step_to_views.py) for execution.

---

## Summary

- **Two-level routing** separates **what** to do (intent) from **how** to render it (views)
- **Level 1** uses a manifest-based **intent router** to select skill modules
- **Level 2** uses `STEP_TO_VIEW` in [`step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/step_to_views.py) to dispatch steps to view functions
- **Execution** is orchestrated by [`run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/run_step_to_views.py), which iterates steps and assembles outputs
- **Modularity** enables easy extension: add new steps to `STEP_TO_VIEW` and new view implementations without modifying core routing logic

---

## Frequently Asked Questions

### What problem does two-level routing solve in patent-disclosure-skill?

The two-level routing mechanism solves the **separation of concerns** problem in complex document generation. Without this architecture, intent handling and view rendering would be tightly coupled, making it difficult to reuse view logic across different patent tasks or to add new output formats. By splitting routing into intent selection and step dispatch, the codebase remains modular and extensible.

### How do I add a new step type to the routing system?

To add a new step, modify [`skills/patent-disclosure/tools/step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skills/patent-disclosure/tools/step_to_views.py) to include your step name and view function in `STEP_TO_VIEW`:

```python
STEP_TO_VIEW = {
    # existing mappings...

    "validate_claim": validate_claim_syntax,  # your new step

}

```

Then implement `validate_claim_syntax(context)` in your tools directory. The [`run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/run_step_to_views.py) executor will automatically pick up the new step when included in a workflow.

### Can the step-to-view router handle conditional or branching workflows?

The current implementation in [`run_step_to_views.py`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/run_step_to_views.py) processes steps sequentially. For conditional logic, you would either: (a) implement branching *within* a view function that modifies context for subsequent steps, or (b) extend `run_steps()` to evaluate conditions before dispatch. The source structure in `handsomestWei/patent-disclosure-skill` provides the foundation for such extensions.

### Where is the intent router manifest actually defined?

The intent router configuration resides in the skill's package metadata under `skills/patent-disclosure/`, typically in a [`skill.yaml`](https://github.com/handsomestWei/patent-disclosure-skill/blob/main/skill.yaml) or similar manifest file as referenced in the repository documentation. This manifest maps natural language intents to Python module paths, enabling the top-level router to dynamically load the appropriate handler.