-
Notifications
You must be signed in to change notification settings - Fork 5
feat(planning): add PRD template system for customizable output formats (#316) #321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fc6b7f8
feat(planning): add PRD template system for customizable output forma…
2df53b9
fix(planning): integrate template_id into PRD generation (#316)
bc7b165
fix(planning): persist imported templates and use workspace scope (#316)
a8932d1
fix(planning): use repo_path instead of path for Workspace attribute
a7e75dd
fix(planning): track resolved vs requested template ID in PRD metadata
367ad59
fix(planning): align docstring and use OS-agnostic test assertions
d757205
style(tests): remove unused Path import
e5daae9
fix(planning): improve template validation and override handling
07b0f94
security(planning): add input validation and XSS prevention
85609ca
test(planning): use resolve() for robust path comparison
8ed3e90
fix(planning): address PR review feedback
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -589,11 +589,16 @@ def resume_discovery(self, blocker_id: str) -> None: | |
|
|
||
| logger.info(f"Resumed session {session_id} from blocker {blocker_id}") | ||
|
|
||
| def generate_prd(self) -> prd.PrdRecord: | ||
| def generate_prd(self, template_id: Optional[str] = None) -> prd.PrdRecord: | ||
| """Generate PRD from discovery conversation. | ||
|
|
||
| Uses AI to synthesize the conversation into a structured PRD. | ||
|
|
||
| Args: | ||
| template_id: Optional PRD template ID to use for formatting. | ||
| If not provided or not found, uses the default built-in | ||
| prompt format (recorded as "default" in metadata). | ||
|
|
||
| Returns: | ||
| Created PrdRecord | ||
|
|
||
|
|
@@ -606,10 +611,10 @@ def generate_prd(self) -> prd.PrdRecord: | |
| f"Current coverage: {self._coverage}" | ||
| ) | ||
|
|
||
|
|
||
| qa_history = self._format_qa_history() | ||
|
|
||
| prompt = PRD_GENERATION_PROMPT.format(qa_history=qa_history) | ||
| # Build prompt based on template and get the resolved template ID | ||
| prompt, resolved_template_id = self._build_prd_prompt(qa_history, template_id) | ||
|
|
||
| response = self._llm_provider.complete( | ||
| messages=[{"role": "user", "content": prompt}], | ||
|
|
@@ -623,27 +628,93 @@ def generate_prd(self) -> prd.PrdRecord: | |
| # Extract title from PRD content | ||
| title = self._extract_title_from_prd(content) | ||
|
|
||
| # Store PRD | ||
| # Store PRD with both requested and resolved template IDs in metadata | ||
| metadata: dict[str, Any] = { | ||
| "source": "ai_discovery", | ||
| "session_id": self.session_id, | ||
| "questions_asked": len(self._qa_history), | ||
| "coverage": self._coverage, | ||
| "generated_at": _utc_now().isoformat(), | ||
| "template_id": resolved_template_id, | ||
| } | ||
| # Track if a different template was requested but not found | ||
| if template_id and template_id != resolved_template_id: | ||
| metadata["requested_template_id"] = template_id | ||
|
|
||
| record = prd.store( | ||
| self.workspace, | ||
| content=content, | ||
| title=title, | ||
| metadata={ | ||
| "source": "ai_discovery", | ||
| "session_id": self.session_id, | ||
| "questions_asked": len(self._qa_history), | ||
| "coverage": self._coverage, | ||
| "generated_at": _utc_now().isoformat(), | ||
| }, | ||
| metadata=metadata, | ||
| ) | ||
|
|
||
| # Update session state | ||
| self.state = SessionState.COMPLETED | ||
| self._save_session() | ||
|
|
||
| logger.info(f"Generated PRD {record.id} from session {self.session_id}") | ||
| logger.info(f"Generated PRD {record.id} from session {self.session_id} using template '{resolved_template_id}'") | ||
| return record | ||
|
|
||
| def _build_prd_prompt( | ||
| self, qa_history: str, template_id: Optional[str] = None | ||
| ) -> tuple[str, str]: | ||
| """Build PRD generation prompt based on template. | ||
|
|
||
| Args: | ||
| qa_history: Formatted Q&A history string | ||
| template_id: Template ID to use (defaults to None, which uses default prompt) | ||
|
|
||
| Returns: | ||
| Tuple of (prompt string, resolved template ID) | ||
| The resolved template ID is "default" if no template was used, | ||
| or the actual template ID that was successfully loaded. | ||
| """ | ||
| from codeframe.planning.prd_templates import PrdTemplateManager | ||
| from pathlib import Path | ||
|
|
||
| # Use default prompt if no template specified | ||
| if not template_id: | ||
| return (PRD_GENERATION_PROMPT.format(qa_history=qa_history), "default") | ||
|
|
||
| # Pass workspace path to include project templates | ||
| workspace_path = Path(self.workspace.repo_path) if self.workspace.repo_path else None | ||
| manager = PrdTemplateManager(workspace_path=workspace_path) | ||
| template = manager.get_template(template_id) | ||
|
|
||
|
frankbria marked this conversation as resolved.
|
||
| if template is None: | ||
| logger.warning(f"Template '{template_id}' not found, falling back to default prompt") | ||
| return (PRD_GENERATION_PROMPT.format(qa_history=qa_history), "default") | ||
|
|
||
| # Build dynamic prompt from template sections | ||
| sections_spec = [] | ||
| for section in template.sections: | ||
| required_note = " (required)" if section.required else " (optional)" | ||
| sections_spec.append(f"## {section.title}{required_note}\n{section.source} - related content") | ||
|
|
||
| sections_text = "\n\n".join(sections_spec) | ||
|
|
||
| prompt = f"""Generate a Product Requirements Document based on the discovery conversation. | ||
|
|
||
| ## Discovery Conversation | ||
| {qa_history} | ||
|
|
||
| ## Template: {template.name} | ||
| {template.description} | ||
|
|
||
| ## Sections | ||
| Generate a markdown PRD with these sections in order: | ||
|
|
||
| # [Project Title - infer from conversation] | ||
|
|
||
| {sections_text} | ||
|
|
||
| --- | ||
|
|
||
| Keep it concise but complete. Focus on actionable requirements. | ||
| Follow the template structure exactly. This PRD should be sufficient to generate development tasks.""" | ||
|
|
||
| return (prompt, template_id) | ||
|
Comment on lines
+658
to
+716
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the resolved template ID and clarify the section heading.
🔧 Suggested adjustment-## Required Sections
+## Sections
@@
- return (prompt, template_id)
+ return (prompt, template.id)🤖 Prompt for AI Agents |
||
|
|
||
| def _extract_title_from_prd(self, content: str) -> str: | ||
| """Extract project title from generated PRD content.""" | ||
| import re | ||
|
|
||
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
Oops, something went wrong.
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.