File size: 11,336 Bytes
b5a3c55
3ee7577
 
b5a3c55
 
 
0792d54
 
 
 
 
 
7e46b08
 
0792d54
 
 
 
 
7e46b08
 
2ace325
0792d54
2ace325
b5a3c55
7e46b08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ace325
7e46b08
2ace325
7e46b08
 
b5a3c55
3ee7577
b5a3c55
 
3ee7577
b5a3c55
7e46b08
b5a3c55
 
 
 
3ee7577
 
 
b5a3c55
3ee7577
b5a3c55
7e46b08
 
 
b5a3c55
3ee7577
 
b5a3c55
3ee7577
 
 
 
7e46b08
3ee7577
 
b5a3c55
3ee7577
b5a3c55
 
3ee7577
 
b5a3c55
3ee7577
 
 
 
 
 
b5a3c55
3ee7577
b5a3c55
 
 
 
 
7e46b08
b5a3c55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d26e2d
 
 
 
 
 
 
 
b5a3c55
 
 
 
 
7e46b08
 
b5a3c55
 
 
7d26e2d
 
b5a3c55
7d26e2d
 
 
 
 
 
 
 
7e46b08
 
 
 
 
 
 
 
 
 
7d26e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a3c55
7e46b08
7d26e2d
7e46b08
 
7d26e2d
 
7e46b08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a3c55
 
 
 
7e46b08
b5a3c55
 
 
 
 
7e46b08
 
b5a3c55
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
import os
from typing import Optional

# System prompt for reformatting rough drafts into polished system prompts
SYSTEM_PROMPT = """You are a system prompt specialist that converts rough drafts into polished, clear specifications for AI code generation agents and subagents.

Your task:
1. Take the user's rough draft describing an AI code generation agent's desired functionalities
2. Transform it into a well-organized, clear system prompt written in second person ("you")
3. Ensure all desired functionalities from the original draft are preserved and clearly reflected
4. Include context that the agent will operate as part of an AI agent crew collaborating within a repository/codebase
5. Inform the agent about its multi-agent environment without prescribing specific collaborative behaviors

Requirements:
- Write the system prompt directing the agent in second person
- Organize the content logically with clear structure
- Maintain all functional requirements from the original draft
- Include information about the agent's multi-agent context in the system prompt
- Let the agent determine how to handle collaboration based on its role
- Return only the polished system prompt with no additional commentary

Output format: Return the improved system prompt as plain text with no markdown formatting or code fences."""

# Alternative system prompt for Claude Code with frontmatter
CLAUDE_CODE_SYSTEM_PROMPT = """You are a system prompt specialist that converts rough drafts into polished, clear specifications for AI code generation agents designed for use in Claude Code.

Your task:
1. Take the user's rough draft describing an AI code generation agent's desired functionalities
2. Create appropriate frontmatter following the Claude Code format with name, description, examples, tools, model, and color
3. Transform the draft into a well-organized system prompt written in second person ("you")
4. Include context that the agent will operate as part of an AI agent crew collaborating within a repository/codebase
5. Ensure the frontmatter description and examples clearly show when to use this agent

Requirements:
- Start with properly formatted frontmatter (name, description with examples, tools, model, color)
- Write the system prompt directing the agent in second person
- Organize the content logically with clear structure
- Maintain all functional requirements from the original draft
- Include information about the agent's multi-agent context
- Provide realistic usage examples in the frontmatter description
- Return only the complete prompt with frontmatter as plain text

Output format: Return the complete prompt with frontmatter and system prompt as plain text with no markdown formatting or code fences."""

def reformat_prompt(api_key: str, rough_draft: str, prompt_type: str = "standard") -> str:
    """
    Reformat a rough draft into a polished system prompt using OpenAI API.
    
    Args:
        api_key: OpenAI API key
        rough_draft: The rough draft text to be reformatted
        prompt_type: Type of prompt to generate ("standard" or "claude_code")
        
    Returns:
        Reformatted system prompt or error message
    """
    if not api_key.strip():
        return "Error: Please provide your OpenAI API key."
    
    if not rough_draft.strip():
        return "Error: Please provide a rough draft to reformat."
    
    # Select the appropriate system prompt based on type
    selected_system_prompt = CLAUDE_CODE_SYSTEM_PROMPT if prompt_type == "claude_code" else SYSTEM_PROMPT
    
    try:
        # Initialize OpenAI client with the provided API key
        client = openai.OpenAI(api_key=api_key)
        
        # Make API call to reformat the prompt
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": selected_system_prompt},
                {"role": "user", "content": rough_draft}
            ],
            temperature=0.1,
            max_tokens=2000
        )
        
        reformatted_text = response.choices[0].message.content
        return reformatted_text
        
    except openai.AuthenticationError:
        return "Error: Invalid API key. Please check your OpenAI API key."
    except openai.RateLimitError:
        return "Error: Rate limit exceeded. Please try again later."
    except openai.APIError as e:
        return f"Error: OpenAI API error - {str(e)}"
    except Exception as e:
        return f"Error: {str(e)}"

def create_interface():
    """Create and configure the Gradio interface."""
    
    with gr.Blocks(
        title="Code Gen Agent Prompt Assistant",
        theme=gr.themes.Soft(),
        css="""
        .container {
            max-width: 1200px;
            margin: auto;
        }
        .header {
            text-align: center;
            margin-bottom: 2rem;
        }
        .api-key-box {
            background-color: #f8f9fa;
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1rem;
        }
        .system-prompt-display {
            background-color: #f8f9fa;
            padding: 1rem;
            border-radius: 8px;
            font-family: monospace;
            white-space: pre-wrap;
            border: 1px solid #dee2e6;
        }
        """
    ) as interface:
        
        gr.HTML("""
        <div class="header">
            <h1>Code Gen Agent Prompt Assistant</h1>
            <p>Transform rough drafts into polished system prompts for AI code generation agents operating within multi-agent crews.</p>
        </div>
        """)
        
        with gr.Tabs():
            with gr.TabItem("Prompt Reformatter"):
                with gr.Row():
                    with gr.Column():
                        api_key_input = gr.Textbox(
                            label="OpenAI API Key",
                            placeholder="sk-...",
                            type="password",
                            info="Your API key is not stored and only used for this session"
                        )
                        
                        prompt_type_radio = gr.Radio(
                            choices=[
                                ("Standard Multi-Agent Prompt", "standard"),
                                ("Claude Code with Frontmatter", "claude_code")
                            ],
                            value="standard",
                            label="Prompt Type",
                            info="Choose the format for your system prompt"
                        )
                        
                        rough_draft_input = gr.Textbox(
                            label="Rough Draft",
                            placeholder="Enter your rough draft describing the desired functionalities of the AI code generation agent...",
                            lines=10,
                            max_lines=20
                        )
                        
                        with gr.Row():
                            clear_btn = gr.Button("Clear", variant="secondary")
                            submit_btn = gr.Button("Reformat Prompt", variant="primary", scale=2)
                    
                    with gr.Column():
                        output = gr.Textbox(
                            label="Reformatted System Prompt",
                            lines=15,
                            max_lines=25,
                            show_copy_button=True,
                            interactive=False
                        )
            
            with gr.TabItem("System Prompts"):
                gr.HTML("""
                <h3>System Prompts Used in Workflow</h3>
                <p>These are the system prompts that guide the AI in reformatting your rough drafts:</p>
                """)
                
                with gr.Tabs():
                    with gr.TabItem("Standard Multi-Agent"):
                        gr.Textbox(
                            value=SYSTEM_PROMPT,
                            label="Standard Multi-Agent System Prompt",
                            lines=15,
                            max_lines=25,
                            show_copy_button=True,
                            interactive=False,
                            elem_classes=["system-prompt-display"]
                        )
                        
                        gr.HTML("""
                        <div style="margin-top: 1rem; padding: 1rem; background-color: #e3f2fd; border-radius: 8px;">
                            <h4>About This System Prompt</h4>
                            <p>This system prompt creates standard prompts that:</p>
                            <ul>
                                <li>Inform agents about their multi-agent environment context</li>
                                <li>Convert rough notes into polished system prompts for code generation agents</li>
                                <li>Focus on agents within multi-agent crews working in codebases</li>
                                <li>Let agents determine their own collaborative behaviors</li>
                            </ul>
                        </div>
                        """)
                    
                    with gr.TabItem("Claude Code with Frontmatter"):
                        gr.Textbox(
                            value=CLAUDE_CODE_SYSTEM_PROMPT,
                            label="Claude Code System Prompt",
                            lines=15,
                            max_lines=25,
                            show_copy_button=True,
                            interactive=False,
                            elem_classes=["system-prompt-display"]
                        )
                        
                        gr.HTML("""
                        <div style="margin-top: 1rem; padding: 1rem; background-color: #fff3e0; border-radius: 8px;">
                            <h4>About This System Prompt</h4>
                            <p>This system prompt creates Claude Code compatible prompts that:</p>
                            <ul>
                                <li>Include proper frontmatter with name, description, examples, tools, model, and color</li>
                                <li>Provide realistic usage examples in the description</li>
                                <li>Create agents designed for Claude Code's multi-agent environment</li>
                                <li>Format everything according to Claude Code specifications</li>
                            </ul>
                        </div>
                        """)
        
        # Event handlers
        submit_btn.click(
            fn=reformat_prompt,
            inputs=[api_key_input, rough_draft_input, prompt_type_radio],
            outputs=output,
            show_progress=True
        )
        
        clear_btn.click(
            fn=lambda: ("", "standard", "", ""),
            outputs=[api_key_input, prompt_type_radio, rough_draft_input, output]
        )
        
    
    return interface

# Create and launch the interface
if __name__ == "__main__":
    app = create_interface()
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )