-
Notifications
You must be signed in to change notification settings - Fork 0
Add research digest: POST /research sends Gemini-powered email on any topic #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4548e0
Add research digest: POST /research sends a Gemini-powered email on a…
claude 16d65bc
Address review: 400 on missing topic, guard empty response, UTF-8 MIME
claude 79528a0
Use configured model for research digests
krMaynard 4b93cf5
Make /research idempotent to prevent duplicate digests on Scheduler r…
claude 2657ba0
Make research digest reservation atomic
krMaynard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import logging | ||
| from datetime import date | ||
|
|
||
| from google import genai | ||
| from google.genai import types | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def research_topic(client: genai.Client, topic: str, model: str) -> str: | ||
| """Research a topic using Gemini with Google Search grounding. | ||
|
|
||
| Returns the research summary as plain text suitable for an email body. | ||
| """ | ||
| today = date.today().strftime("%B %d, %Y") | ||
| prompt = ( | ||
| f"Today is {today}. Research the following topic and write a clear, " | ||
| f"well-organized summary covering key recent developments, insights, " | ||
| f"and anything notable. Write only the email body — no greeting or sign-off.\n\n" | ||
| f"Topic: {topic}" | ||
| ) | ||
| response = client.models.generate_content( | ||
| model=model, | ||
| contents=prompt, | ||
| config=types.GenerateContentConfig( | ||
| tools=[types.Tool(google_search=types.GoogleSearch())], | ||
| max_output_tokens=2048, | ||
| ), | ||
| ) | ||
| if not response.text: | ||
| raise ValueError("Gemini returned an empty response or the response was blocked.") | ||
| return response.text | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Tests for atomic research-digest reservations.""" | ||
|
|
||
| from google.api_core import exceptions as google_exceptions | ||
|
|
||
| from assistant import state | ||
|
|
||
|
|
||
| class _Blob: | ||
| def __init__(self, name, objects): | ||
| self.name = name | ||
| self.objects = objects | ||
|
|
||
| def upload_from_string(self, value, **kwargs): | ||
| assert kwargs["if_generation_match"] == 0 | ||
| if self.name in self.objects: | ||
| raise google_exceptions.PreconditionFailed("already exists") | ||
| self.objects[self.name] = value | ||
|
|
||
| def delete(self): | ||
| if self.name not in self.objects: | ||
| raise google_exceptions.NotFound("missing") | ||
| del self.objects[self.name] | ||
|
|
||
|
|
||
| class _Bucket: | ||
| def __init__(self, objects): | ||
| self.objects = objects | ||
|
|
||
| def blob(self, name): | ||
| return _Blob(name, self.objects) | ||
|
|
||
|
|
||
| class _Client: | ||
| def __init__(self, objects): | ||
| self.objects = objects | ||
|
|
||
| def bucket(self, _name): | ||
| return _Bucket(self.objects) | ||
|
|
||
|
|
||
| def test_claim_is_atomic_and_topic_scoped(monkeypatch): | ||
| objects = {} | ||
| monkeypatch.setattr(state.storage, "Client", lambda: _Client(objects)) | ||
|
|
||
| marker = state.claim_research("bucket", "AI policy", "2026-07-17") | ||
| assert marker | ||
| assert state.claim_research("bucket", "AI policy", "2026-07-17") is None | ||
| assert state.claim_research("bucket", "different topic", "2026-07-17") | ||
|
|
||
|
|
||
| def test_release_allows_retry_after_definite_failure(monkeypatch): | ||
| objects = {} | ||
| monkeypatch.setattr(state.storage, "Client", lambda: _Client(objects)) | ||
|
|
||
| marker = state.claim_research("bucket", "AI policy", "2026-07-17") | ||
| state.release_research("bucket", marker) | ||
| assert state.claim_research("bucket", "AI policy", "2026-07-17") == marker |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from assistant.researcher import research_topic | ||
|
|
||
|
|
||
| class FakeModels: | ||
| def __init__(self, text): | ||
| self.text = text | ||
| self.calls = [] | ||
|
|
||
| def generate_content(self, **kwargs): | ||
| self.calls.append(kwargs) | ||
| return SimpleNamespace(text=self.text) | ||
|
|
||
|
|
||
| def test_research_uses_configured_model(): | ||
| models = FakeModels("Digest") | ||
| client = SimpleNamespace(models=models) | ||
|
|
||
| assert research_topic(client, "AI policy", "configured-model") == "Digest" | ||
| assert models.calls[0]["model"] == "configured-model" | ||
|
|
||
|
|
||
| def test_research_rejects_empty_response(): | ||
| client = SimpleNamespace(models=FakeModels("")) | ||
|
|
||
| with pytest.raises(ValueError, match="empty response"): | ||
| research_topic(client, "AI policy", "configured-model") |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.