forked from quantalogic/quantalogic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-prd-writer.py
executable file
·317 lines (244 loc) · 9.64 KB
/
08-prd-writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "quantalogic",
# ]
# ///
import os
import sys
from typing import Any
from rich.console import Console
from rich.live import Live
from rich.markdown import Markdown
from rich.panel import Panel
from rich.prompt import Confirm, Prompt
from rich.spinner import Spinner
from rich.text import Text
from quantalogic import Agent
from quantalogic.console_print_events import console_print_events
from quantalogic.console_print_token import console_print_token
from quantalogic.tools import (
JinjaTool,
ListDirectoryTool,
LLMTool,
ReadFileBlockTool,
ReadFileTool,
ReplaceInFileTool,
WriteFileTool,
)
MODEL_NAME = "deepseek/deepseek-chat"
# Ensuring API keys are configured
if not os.environ.get("DEEPSEEK_API_KEY"):
raise ValueError("DEEPSEEK_API_KEY environment variable is not set")
console = Console()
agent = Agent(
model_name=MODEL_NAME,
tools=[
WriteFileTool(),
ReadFileTool(),
ReplaceInFileTool(),
ReadFileBlockTool(),
ListDirectoryTool(),
JinjaTool(),
LLMTool(model_name=MODEL_NAME, on_token=console_print_token),
],
)
# Spinner Manager with external control
class SpinnerManager:
def __init__(self, console: Console, message: str = "Processing..."):
self.console = console
self.spinner = Spinner("dots", text=message, style="cyan")
self.live = Live(self.spinner, refresh_per_second=10, console=self.console, transient=True)
def start(self):
self.live.start()
def stop(self):
self.live.stop()
# Initialize SpinnerManager
spinner_manager = SpinnerManager(console, "Generating PRD...")
spinner_manager.spinner_active = False
# Hooking into events
agent.event_emitter.on(
[
"task_complete",
"task_think_start",
"task_think_end",
"tool_execution_start",
"tool_execution_end",
"error_max_iterations_reached",
],
console_print_events,
)
def on_task_solve_start(event: str, data: Any | None = None) -> None:
"""Start the spinner when task solving begins."""
spinner_manager.start()
spinner_manager.spinner_active = True
def on_task_solve_end(event: str, data: Any | None = None) -> None:
"""Stop the spinner when task solving ends."""
if spinner_manager.spinner_active:
spinner_manager.stop()
spinner_manager.spinner_active = False
def on_stream_chunk(event: str, data: Any | None = None) -> None:
"""Handle stream_chunk events by stopping the spinner and printing the token."""
if spinner_manager.spinner_active:
spinner_manager.stop()
spinner_manager.spinner_active = False
console_print_token(event, data) # Print the token chunk
agent.event_emitter.on(event=["task_solve_start"], listener=on_task_solve_start)
agent.event_emitter.on(event=["task_solve_end"], listener=on_task_solve_end)
agent.event_emitter.on(event=["stream_chunk"], listener=on_stream_chunk)
def get_multiline_input(
console: Console, prompt: str = "Paste your text below (Press Ctrl+D or type 'END' on a new line to finish):\n"
) -> str:
"""
Capture and process multi-line input from the user, including pasted text.
Args:
console: Rich Console instance for displaying prompts
prompt: Initial prompt message
Returns:
A single string containing all user input
"""
console.print(panel_prompt(prompt))
lines = []
try:
while True:
line = Prompt.ask("", default="", show_default=False, console=console)
if line.strip().upper() == "END":
break
lines.append(line)
except (EOFError, KeyboardInterrupt):
console.print("\n[yellow]Input cancelled.[/yellow]")
return ""
return "\n".join(lines)
def panel_prompt(message: str) -> Panel:
"""
Create a styled panel for prompts.
Args:
message: The message to display in the panel.
Returns:
A Rich Panel object.
"""
return Panel(Text(message, style="italic"), style="dim", expand=False)
def review_input(console: Console, requirements: str) -> str:
"""
Allow users to review their input.
Args:
console: Rich Console instance
requirements: Text input to review
Returns:
Reviewed input text
"""
markdown = Markdown(requirements)
console.print("\n[bold]Your current input is as follows:[/bold]")
console.print(markdown, style="cyan")
return requirements
def interactive_prd_prompt(default_project: str = "New Product", default_target_dir: str = "./docs/prd") -> dict:
"""
Interactive user prompts for collecting PRD-related information.
Args:
default_project: Default project name
default_target_dir: Default output directory
Returns:
A dictionary containing all input details for the PRD
"""
console.print(
Panel(
Text(
"[bold green]PRD Writer Assistant[/bold green]\nLet's create your PRD! Please follow the prompts below. 📝",
justify="center",
),
style="bright_blue",
)
)
project = Prompt.ask("Enter the name of your project", default=default_project, console=console)
console.print("\n[bold]Project Requirements[/bold]")
requirements = get_multiline_input(console)
if not requirements:
return {}
requirements = review_input(console, requirements)
target_dir = Prompt.ask("Where should the PRD be saved?", default=default_target_dir, console=console)
summary = (
"\n[bold]Summary of your PRD Details:[/bold]\n"
f"• Project Name: [cyan]{project}[/cyan]\n"
f"• Requirements:\n[cyan]{requirements}[/cyan]\n"
f"• Target Directory: [cyan]{target_dir}[/cyan]"
)
console.print(summary)
if not Confirm.ask("Are these details correct?", default=True, console=console):
console.print("[yellow]PRD creation cancelled. Restart and try again![/yellow]")
return {}
return {"project": project, "requirements": requirements, "target_dir": target_dir}
def prepare_prd_task(project: str = "New Product", target_dir: str = "./docs/prd") -> str:
"""
Prepares a Product Requirements Document (PRD) writing task based on user input.
Args:
project: The default project name.
target_dir: The default output directory for PRD files.
Returns:
A detailed task description for creating the PRD.
"""
details = interactive_prd_prompt(default_project=project, default_target_dir=target_dir)
if not details:
return ""
task_description = f"""
# Task: Create a Product Requirements Document (PRD)
You are a 10x Product Manager responsible for developing a Product Requirements Document (PRD) for the following project:
**Project Name:** {details['project']}
**Output Directory:** {details['target_dir']} (Format: Markdown)
## Workflow Steps:
### Step 1: Analyze Requirements
- Analyze these requirements: {details['requirements']} along with the last generated PRD.
- Conduct in-depth market research to identify the target users and their needs.
- Save findings in a separate file: `{details['target_dir']}/market_research.md`.
### Step 2: Feature Categorization
- Brainstorm a set of potential features and categorize them into **Core** and **Optional** using MoSCoW prioritization.
- Draw inspiration from the following books:
- *Hooked: How to Build Habit-Forming Products* by Nir Eyal
- *Inspired: How to Create Tech Products Customers Love* by Marty Cagan
- Document your findings in: `{details['target_dir']}/features.md`.
### Step 3: Develop PRD Sections
- Create individual files for each PRD section in the target directory: `{details['target_dir']}`:
- Overview
- Problem Statement
- Target Users
- Features (MVP & Future)
- Success Metrics
- Timeline
### Step 4: Review PRD Sections
- Review and critique the content of each section developed in Step 3, updating if necessary.
- Update each section as needed.
### Step 5: Draft the Final PRD
- Compile a cohesive first draft of the PRD encompassing all sections.
- Ensure the document is clear, readable, and navigable.
- Save as: `{details['target_dir']}/final_prd_draft.md`.
### Step 6: Draft Review
- Review the draft of the final PRD, providing constructive feedback and updates as needed.
- Write the feedback into a separate file: `{details['target_dir']}/final_feedback.md`.
### Step 7: Final PRD Creation
- Update the final PRD based on the feedback provided in Step 6.
- Produce the final updated PRD in a separate file: `{details['target_dir']}/final_prd.md`.
## Instructions:
Take inspiration from the following books to enrich your PRD:
- *Hooked: How to Build Habit-Forming Products* by Nir Eyal
- *Inspired: How to Create Tech Products Customers Love* by Marty Cagan
- *Escaping the Build Trap* by Melissa Perri
- *Good Strategy Bad Strategy* by Richard Rumelt
- *Crossing the Chasm* by Geoffrey A. Moore
## Goals:
- Generate a thorough PRD of at least 5000 words.
- Repeat the improvement process a minimum of 8 times for a solid final product.
- Ensure the product delivers an exceptional user experience and is financially viable.
"""
return task_description
def main():
task_description = prepare_prd_task()
if task_description:
try:
agent.solve_task(task_description, streaming=True,max_iterations=50)
except Exception as e:
console.print(f"[bold red]An unexpected error occurred: {e}[/bold red]")
sys.exit(1)
else:
console.print("[yellow]No task was prepared. Exiting.[/yellow]")
if __name__ == "__main__":
main()