Add knowledge-mapper run CLI subcommand (#10)#47
Merged
Conversation
Adds a typer-based CLI entry point so users can start a Python-defined
KnowledgeBase without writing asyncio.run boilerplate:
knowledge-mapper run path/to/file.py:kb
The command:
- loads the named KnowledgeBase attribute from the given Python file
(the file's parent directory is added to sys.path so sibling imports
work just like `python file.py`)
- runs the full async lifecycle: connect, register, start_handling_loop
- on SIGINT/SIGTERM cancels the handling loop and unregisters + closes
the KB before exiting cleanly
- still unregisters + closes if the handling loop raises
Includes an example (examples/10-cli.py) that defines a KB without any
asyncio.run or __main__ block, README/CONTEXT updates, and unit + typer
CliRunner integration tests.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #10.
What
Adds a typer-based
knowledge-mapperCLI with one subcommand,run, that starts a Python-definedKnowledgeBaseand replaces the usualasyncio.run(...)boilerplate:The
PATH:ATTRspec points at a Python file and the name of theKnowledgeBasevariable inside it.Behaviour
importlib.utiland retrieves the named attribute. The file's parent directory is added tosys.pathso the module can import siblings just likepython file.py.connect → register → start_handling_loop.SIGINT/SIGTERM: cancels the handling task, thenunregister+closerun infinally, and the process exits cleanly.start_handling_loopraises,unregister+closestill run.The issue's spec was written before the SDK switched to async; this implementation adapts the lifecycle accordingly (and adds
close()to match the README lifecycle, as discussed).Layout
src/knowledge_mapper/cli.py—load_kb,run_kb, and the typerappwith therunsubcommand. A@app.callback()is added so the app keeps a subcommand structure (room for a futuresparqlsubcommand, per the issue).pyproject.toml— addstyper>=0.15dep and registers theknowledge-mapper = knowledge_mapper.cli:appconsole script.tests/test_cli.py— 9 behavior-focused tests written TDD-style covering load happy/sad paths, sibling imports, full lifecycle order, cleanup on error, graceful SIGINT shutdown, and an end-to-end typerCliRunnertest.examples/10-cli.py— minimal KB definition with noasyncio.run/__main__block, runnable viaknowledge-mapper run 10-cli.py:kb.README.md,examples/README.md,CONTEXT.mdupdated.Verification
uv run pytest— 122 tests pass (113 existing + 9 new)uv run ruff check .— cleanuv run knowledge-mapper --helpandknowledge-mapper run --helprender correctlyexamples/10-cli.pyvia the loader — KI registered as expected