Turn strava.py chat into a tool-using agent with runlog search#3
Open
sahilds1 wants to merge 5 commits into
Open
Turn strava.py chat into a tool-using agent with runlog search#3sahilds1 wants to merge 5 commits into
sahilds1 wants to merge 5 commits into
Conversation
Replace the passive single-shot chat with an agent loop: the conversation now runs llm's tool-calling loop (model.conversation(tools=[...]) + conversation.chain()), so the model can decide mid-conversation to call a tool and act on the result, instead of only answering from the seeded activity JSON. The agent's first tool is search_runlog, a keyword search over the athlete's running log (RUNLOG.txt, override with --runlog). It lives in a new self-contained strava_tools.py built from pure parse_runlog / search_entries / format_entries helpers, which are the test targets in test_strava_tools.py. Adding a second tool is a one-line change to the tools=[...] list. format_entries returns self-contained titles + bodies on purpose: with the default OpenAI (Chat Completions) plugin, reasoning state is not preserved across turns or across a tool hop, so the tool's returned text is the only durable state the next turn sees. strava.py carries a comment on this and on the OpenAI Agents SDK as a future extension path. Also updates SYSTEM_PROMPT to tell the model about the tool and to ground history claims in what it returns, and documents the agent in CLAUDE.md.
Replace search_entries' hand-rolled token-match scorer with scikit-learn's TfidfVectorizer (fit corpus, cosine-score the query, stable-sort, keep score > 0, slice to limit). TF-IDF weights terms by rarity, so a match on a distinctive word (e.g. "fartlek") outranks one on a ubiquitous word like "run" -- the thing plain counting got wrong. Drops the now-unused _tokenize. Adds scikit-learn to strava.py's script block and pyproject.toml (standalone run and pytest are separate environments). Comments record the decisions (scikit-learn for the simplest code despite the numpy/scipy weight; title text folded into the document but not up-weighted) and point to BM25 as the next step, via rank-bm25 or SQLite FTS5. Tests now cover only the logic we wrote around the vectorizer -- the empty-corpus guard, the score > 0 filter, and the limit slice -- not scikit-learn's ranking; drops the ranking and end-to-end glue tests.
The `llm`-vs-agent-SDK rationale and the reasoning-state caveat now live in CLAUDE.md and WORKLOG.md; keep the inline comment to what the code does. Note at the import why strava_tools is imported as a module — --runlog rebinds strava_tools.RUNLOG_PATH, which a from-import misses.
RUNLOG.txt delimits entries with "# " headings now, so a title is a heading line rather than "any non-bullet line" -- which lets a body hold prose and not just bullets. Split the text on the heading pattern instead of walking it line by line: re.split's capturing group hands back each title and body whole, so they go straight into a RunlogEntry with no sentinel, no buffer, and no flush step. Make RunlogEntry a frozen dataclass. Nothing depended on its tuple-ness, and value semantics without structural equality mean an entry won't compare equal to a bare 2-tuple or shift positional access if a field is added later. Move search_runlog to the top of the module, ahead of the helpers it composes, so the tool itself is what a reader meets first. Tests: fixtures converted to headings, plus a case for text before the first heading. Both TODO'd as unrun.
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.
Description
Replace the passive single-shot chat with an agent loop: the conversation now runs llm's tool-calling loop (model.conversation(tools=[...]) + conversation.chain()), so the model can decide mid-conversation to call a tool and act on the result, instead of only answering from the seeded activity JSON.
The agent's first tool is search_runlog, a keyword search over the athlete's running log (RUNLOG.txt, override with --runlog). It lives in a new self-contained strava_tools.py built from pure parse_runlog / search_entries / format_entries helpers, which are the test targets in test_strava_tools.py. Adding a second tool is a one-line change to the tools=[...] list.
format_entries returns self-contained titles + bodies on purpose: with the default OpenAI (Chat Completions) plugin, reasoning state is not preserved across turns or across a tool hop, so the tool's returned text is the only durable state the next turn sees. strava.py carries a comment on this and on the OpenAI Agents SDK as a future extension path.
Also updates SYSTEM_PROMPT to tell the model about the tool and to ground history claims in what it returns, and documents the agent in CLAUDE.md.
Related Issue
Manual Tests
Automated Tests
Documentation
Reviewers
Notes