Skip to content

Commit ab531ad

Browse files
feat(lessons): auto-discover lessons from plugins (#944)
* feat(lessons): auto-discover lessons from plugins When plugins are configured via [plugins].paths, automatically search each plugin's lessons/ directory for lesson files. This enables plugins to ship with their own lessons that get included automatically when the plugin is loaded. * fix(lessons): handle src/ layout for plugin lesson discovery When plugins use the standard Python src/ layout, plugin.path points to the module inside src/ (e.g., /project/src/module/), not the project root. This caused lessons/ directories at the project root to not be found. Fix: 1. First check plugin.path/lessons (flat layout) 2. If not found, walk up to find project root (where pyproject.toml is) 3. Check for lessons/ at project root for src/ layout plugins 4. Use is_dir() instead of exists() per code review suggestion Fixes issue identified by Greptile: plugins with src/ layout structure (pyproject.toml + src/ directory) would not have their lessons discovered. --------- Co-authored-by: Erik Bjäreholt <erik@bjareho.lt>
1 parent 684bbb6 commit ab531ad

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

gptme/lessons/index.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,36 @@ def _default_dirs() -> list[Path]:
155155
if lesson_dir.exists():
156156
dirs.append(lesson_dir)
157157

158+
# Plugin lesson directories (auto-discovered from [plugins].paths)
159+
if config.project and config.project.plugins.paths:
160+
from ..plugins import discover_plugins
161+
162+
plugin_paths = [
163+
Path(p).expanduser().resolve() for p in config.project.plugins.paths
164+
]
165+
plugins = discover_plugins(plugin_paths)
166+
for plugin in plugins:
167+
# Check for lessons/ directory in plugin
168+
# For flat layout: lessons/ is directly in plugin.path
169+
# For src/ layout: plugin.path is src/module/, so lessons/ is at project root
170+
plugin_lessons_dir = plugin.path / "lessons"
171+
if plugin_lessons_dir.is_dir():
172+
dirs.append(plugin_lessons_dir)
173+
logger.debug(
174+
f"Found plugin lessons: {plugin.name} -> {plugin_lessons_dir}"
175+
)
176+
else:
177+
# Check for src/ layout: walk up to find project root
178+
# plugin.path might be /project/src/module/, lessons at /project/lessons/
179+
project_root = plugin.path.parent.parent
180+
if (project_root / "pyproject.toml").exists():
181+
project_lessons_dir = project_root / "lessons"
182+
if project_lessons_dir.is_dir():
183+
dirs.append(project_lessons_dir)
184+
logger.debug(
185+
f"Found plugin lessons (src layout): {plugin.name} -> {project_lessons_dir}"
186+
)
187+
158188
return dirs
159189

160190
def _index_lessons(self) -> None:

0 commit comments

Comments
 (0)