Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions wren/src/wren/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,68 @@
_TARGET_DIR = "target"
_TARGET_FILE = "mdl.json"

_AGENTS_MD_TEMPLATE = """\
# AGENTS.md

This project uses [Wren Engine](https://github.com/Canner/wren-engine) as the semantic layer for data querying. Queries are written against MDL model names, not raw database tables.

## Answering data questions

When the user asks about data, metrics, reports, or business questions, follow this workflow:

1. `wren memory fetch -q "<question>"` — get relevant schema context
2. `wren memory recall -q "<question>" --limit 3` — find similar past queries
3. Write SQL using model names from the MDL (not raw table names)
4. `wren --sql "<sql>"` — execute through the semantic layer
5. `wren memory store --nl "<question>" --sql "<sql>"` — store confirmed results

If this is the first query in the session, also run `wren context instructions` to load business rules.

## Modifying the data model

When the user wants to add models, change schema, or onboard a new table:

1. Edit YAML files in `models/`, `views/`, or `relationships.yml`
2. `wren context validate` — check structure
3. `wren context build` — compile to `target/mdl.json`
4. `wren memory index` — re-index schema for search

## Prerequisites

This project requires the `wren` CLI. If not installed:

```bash
pip install "wren-engine[memory]"
```

Each data source needs its own extra — install the one matching your `data_source` in `wren_project.yml`:

```bash
pip install "wren-engine[postgres]" # or mysql, bigquery, snowflake, clickhouse,
# trino, mssql, databricks, redshift, spark,
# athena, oracle
```

Or install everything at once:

```bash
pip install "wren-engine[all]"
```

See https://docs.getwren.ai/oss/engine/get_started/installation for full setup.

## Quick reference

| Task | Command |
|------|---------|
| Run a query | `wren --sql "SELECT ..."` |
| Preview planned SQL | `wren dry-plan --sql "SELECT ..."` |
| Show available models | `wren context show` |
| Check connection | `wren profile debug` |
| Check memory index | `wren memory status` |
| Rebuild after changes | `wren context build && wren memory index` |
"""


# ── Case conversion ───────────────────────────────────────────────────────

Expand Down Expand Up @@ -195,6 +257,14 @@ def convert_mdl_to_project(mdl_json: dict) -> list[ProjectFile]:
)
)

# ── AGENTS.md ──────────────────────────────────────────────
files.append(
ProjectFile(
relative_path="AGENTS.md",
content=_AGENTS_MD_TEMPLATE,
)
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return files


Expand Down Expand Up @@ -223,6 +293,7 @@ def write_project_files(
"relationships.yml",
"instructions.md",
"wren_project.yml",
"AGENTS.md",
):
target = output_dir / managed
if target.is_dir():
Expand Down
13 changes: 11 additions & 2 deletions wren/src/wren/context_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ def init(

# ── Scaffold empty project (existing behavior) ────────────
project_file = project_path / "wren_project.yml"
if project_file.exists() and not force:
agents_file = project_path / "AGENTS.md"
conflicts = [f for f in (project_file, agents_file) if f.exists()]
if conflicts and not force:
names = ", ".join(f"'{c.name}'" for c in conflicts)
typer.echo(
f"Error: '{project_file}' already exists. This is already a Wren project.",
f"Error: {names} already exists. This is already a Wren project.",
err=True,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
)
raise typer.Exit(1)
Expand Down Expand Up @@ -167,12 +170,18 @@ def init(
"Add custom rules or guidelines for LLM-based query generation here.\n"
)

# ── AGENTS.md ──
from wren.context import _AGENTS_MD_TEMPLATE # noqa: PLC0415

(project_path / "AGENTS.md").write_text(_AGENTS_MD_TEMPLATE)

typer.echo(f"Wren project initialized: {project_path}")
typer.echo(" wren_project.yml — project metadata (edit data_source)")
typer.echo(" models/example/ — example model (metadata.yml)")
typer.echo(" views/example_view/ — example view (metadata.yml + sql.yml)")
typer.echo(" relationships.yml — define joins between models")
typer.echo(" instructions.md — LLM instructions")
typer.echo(" AGENTS.md — AI agent workflow guidance")
typer.echo("\nNext: edit your models, then run `wren context build`.")


Expand Down
Loading