forked from Doriandarko/deepseek-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
405 lines (348 loc) · 15.7 KB
/
main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
import os
import sys
import json
from pathlib import Path
from textwrap import dedent
from typing import List, Dict, Any, Optional
from openai import OpenAI
from pydantic import BaseModel
from dotenv import load_dotenv
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.style import Style
# Initialize Rich console
console = Console()
# --------------------------------------------------------------------------------
# 1. Configure OpenAI client and load environment variables
# --------------------------------------------------------------------------------
load_dotenv() # Load environment variables from .env file
client = OpenAI(
api_key=os.getenv("DEEPSEEK_API_KEY"),
base_url="https://api.deepseek.com"
) # Configure for DeepSeek API
# --------------------------------------------------------------------------------
# 2. Define our schema using Pydantic for type safety
# --------------------------------------------------------------------------------
class FileToCreate(BaseModel):
path: str
content: str
# NEW: Diff editing structure
class FileToEdit(BaseModel):
path: str
original_snippet: str
new_snippet: str
class AssistantResponse(BaseModel):
assistant_reply: str
files_to_create: Optional[List[FileToCreate]] = None
# NEW: optionally hold diff edits
files_to_edit: Optional[List[FileToEdit]] = None
# --------------------------------------------------------------------------------
# 3. system prompt
# --------------------------------------------------------------------------------
system_PROMPT = dedent("""\
You are an elite software engineer called DeepSeek Engineer with decades of experience across all programming domains.
Your expertise spans system design, algorithms, testing, and best practices.
You provide thoughtful, well-structured solutions while explaining your reasoning.
Core capabilities:
1. Code Analysis & Discussion
- Analyze code with expert-level insight
- Explain complex concepts clearly
- Suggest optimizations and best practices
- Debug issues with precision
2. File Operations:
a) Read existing files
- Access user-provided file contents for context
- Analyze multiple files to understand project structure
b) Create new files
- Generate complete new files with proper structure
- Create complementary files (tests, configs, etc.)
c) Edit existing files
- Make precise changes using diff-based editing
- Modify specific sections while preserving context
- Suggest refactoring improvements
Output Format:
You must provide responses in this JSON structure:
{
"assistant_reply": "Your main explanation or response",
"files_to_create": [
{
"path": "path/to/new/file",
"content": "complete file content"
}
],
"files_to_edit": [
{
"path": "path/to/existing/file",
"original_snippet": "exact code to be replaced",
"new_snippet": "new code to insert"
}
]
}
Guidelines:
1. For normal responses, use 'assistant_reply'
2. When creating files, include full content in 'files_to_create'
3. For editing files:
- Use 'files_to_edit' for precise changes
- Include enough context in original_snippet to locate the change
- Ensure new_snippet maintains proper indentation
- Prefer targeted edits over full file replacements
4. Always explain your changes and reasoning
5. Consider edge cases and potential impacts
6. Follow language-specific best practices
7. Suggest tests or validation steps when appropriate
Remember: You're a senior engineer - be thorough, precise, and thoughtful in your solutions.
""")
# --------------------------------------------------------------------------------
# 4. Helper functions
# --------------------------------------------------------------------------------
def read_local_file(file_path: str) -> str:
"""Return the text content of a local file."""
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
def create_file(path: str, content: str):
"""Create (or overwrite) a file at 'path' with the given 'content'."""
file_path = Path(path)
file_path.parent.mkdir(parents=True, exist_ok=True) # ensures any dirs exist
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)
console.print(f"[green]✓[/green] Created/updated file at '[cyan]{file_path}[/cyan]'")
# Record the action
conversation_history.append({
"role": "assistant",
"content": f"✓ Created/updated file at '{file_path}'"
})
# NEW: Add the actual content to conversation context
normalized_path = normalize_path(str(file_path))
conversation_history.append({
"role": "system",
"content": f"Content of file '{normalized_path}':\n\n{content}"
})
# NEW: Show the user a table of proposed edits and confirm
def show_diff_table(files_to_edit: List[FileToEdit]) -> None:
if not files_to_edit:
return
# Enable multi-line rows by setting show_lines=True
table = Table(title="Proposed Edits", show_header=True, header_style="bold magenta", show_lines=True)
table.add_column("File Path", style="cyan")
table.add_column("Original", style="red")
table.add_column("New", style="green")
for edit in files_to_edit:
table.add_row(edit.path, edit.original_snippet, edit.new_snippet)
console.print(table)
# NEW: Apply diff edits
def apply_diff_edit(path: str, original_snippet: str, new_snippet: str):
"""Reads the file at 'path', replaces the first occurrence of 'original_snippet' with 'new_snippet', then overwrites."""
try:
content = read_local_file(path)
if original_snippet in content:
updated_content = content.replace(original_snippet, new_snippet, 1)
create_file(path, updated_content) # This will now also update conversation context
console.print(f"[green]✓[/green] Applied diff edit to '[cyan]{path}[/cyan]'")
conversation_history.append({
"role": "assistant",
"content": f"✓ Applied diff edit to '{path}'"
})
else:
# NEW: Add debug info about the mismatch
console.print(f"[yellow]⚠[/yellow] Original snippet not found in '[cyan]{path}[/cyan]'. No changes made.", style="yellow")
console.print("\nExpected snippet:", style="yellow")
console.print(Panel(original_snippet, title="Expected", border_style="yellow"))
console.print("\nActual file content:", style="yellow")
console.print(Panel(content, title="Actual", border_style="yellow"))
except FileNotFoundError:
console.print(f"[red]✗[/red] File not found for diff editing: '[cyan]{path}[/cyan]'", style="red")
def try_handle_add_command(user_input: str) -> bool:
"""
If user_input starts with '/add ', read that file and insert its content
into conversation as a system message. Returns True if handled; else False.
"""
prefix = "/add "
if user_input.strip().lower().startswith(prefix):
file_path = user_input[len(prefix):].strip()
try:
content = read_local_file(file_path)
conversation_history.append({
"role": "system",
"content": f"Content of file '{file_path}':\n\n{content}"
})
console.print(f"[green]✓[/green] Added file '[cyan]{file_path}[/cyan]' to conversation.\n")
except OSError as e:
console.print(f"[red]✗[/red] Could not add file '[cyan]{file_path}[/cyan]': {e}\n", style="red")
return True
return False
def ensure_file_in_context(file_path: str) -> bool:
"""
Ensures the file content is in the conversation context.
Returns True if successful, False if file not found.
"""
try:
normalized_path = normalize_path(file_path)
content = read_local_file(normalized_path)
file_marker = f"Content of file '{normalized_path}'"
if not any(file_marker in msg["content"] for msg in conversation_history):
conversation_history.append({
"role": "system",
"content": f"{file_marker}:\n\n{content}"
})
return True
except OSError:
console.print(f"[red]✗[/red] Could not read file '[cyan]{file_path}[/cyan]' for editing context", style="red")
return False
def normalize_path(path_str: str) -> str:
"""Return a canonical, absolute version of the path."""
return str(Path(path_str).resolve())
# --------------------------------------------------------------------------------
# 5. Conversation state
# --------------------------------------------------------------------------------
conversation_history = [
{"role": "system", "content": system_PROMPT}
]
# --------------------------------------------------------------------------------
# 6. OpenAI API interaction with streaming
# --------------------------------------------------------------------------------
def guess_files_in_message(user_message: str) -> List[str]:
"""
Attempt to guess which files the user might be referencing.
Returns normalized absolute paths.
"""
recognized_extensions = [".css", ".html", ".js", ".py", ".json", ".md"]
potential_paths = []
for word in user_message.split():
if any(ext in word for ext in recognized_extensions) or "/" in word:
path = word.strip("',\"")
try:
normalized_path = normalize_path(path)
potential_paths.append(normalized_path)
except (OSError, ValueError):
continue
return potential_paths
def stream_openai_response(user_message: str):
"""
Streams the DeepSeek chat completion response and handles structured output.
Returns the final AssistantResponse.
"""
# Attempt to guess which file(s) user references
potential_paths = guess_files_in_message(user_message)
valid_files = {}
# Try to read all potential files before the API call
for path in potential_paths:
try:
content = read_local_file(path)
valid_files[path] = content # path is already normalized
file_marker = f"Content of file '{path}'"
# Add to conversation if we haven't already
if not any(file_marker in msg["content"] for msg in conversation_history):
conversation_history.append({
"role": "system",
"content": f"{file_marker}:\n\n{content}"
})
except OSError:
error_msg = f"Cannot proceed: File '{path}' does not exist or is not accessible"
console.print(f"[red]✗[/red] {error_msg}", style="red")
continue
# Now proceed with the API call
conversation_history.append({"role": "user", "content": user_message})
try:
stream = client.chat.completions.create(
model="deepseek-chat",
messages=conversation_history,
response_format={"type": "json_object"},
max_completion_tokens=8000,
stream=True
)
console.print("\nAssistant> ", style="bold blue", end="")
full_content = ""
for chunk in stream:
if chunk.choices[0].delta.content:
content_chunk = chunk.choices[0].delta.content
full_content += content_chunk
console.print(content_chunk, end="")
console.print()
try:
parsed_response = json.loads(full_content)
# [NEW] Ensure assistant_reply is present
if "assistant_reply" not in parsed_response:
parsed_response["assistant_reply"] = ""
# If assistant tries to edit files not in valid_files, remove them
if "files_to_edit" in parsed_response and parsed_response["files_to_edit"]:
new_files_to_edit = []
for edit in parsed_response["files_to_edit"]:
try:
edit_abs_path = normalize_path(edit["path"])
# If we have the file in context or can read it now
if edit_abs_path in valid_files or ensure_file_in_context(edit_abs_path):
edit["path"] = edit_abs_path # Use normalized path
new_files_to_edit.append(edit)
except (OSError, ValueError):
console.print(f"[yellow]⚠[/yellow] Skipping invalid path: '{edit['path']}'", style="yellow")
continue
parsed_response["files_to_edit"] = new_files_to_edit
response_obj = AssistantResponse(**parsed_response)
# Save the assistant's textual reply to conversation
conversation_history.append({
"role": "assistant",
"content": response_obj.assistant_reply
})
return response_obj
except json.JSONDecodeError:
error_msg = "Failed to parse JSON response from assistant"
console.print(f"[red]✗[/red] {error_msg}", style="red")
return AssistantResponse(
assistant_reply=error_msg,
files_to_create=[]
)
except Exception as e:
error_msg = f"DeepSeek API error: {str(e)}"
console.print(f"\n[red]✗[/red] {error_msg}", style="red")
return AssistantResponse(
assistant_reply=error_msg,
files_to_create=[]
)
# --------------------------------------------------------------------------------
# 7. Main interactive loop
# --------------------------------------------------------------------------------
def main():
console.print(Panel.fit(
"[bold blue]Welcome to Deep Seek Engineer with Structured Output[/bold blue] [green](and streaming)[/green]!🐋",
border_style="blue"
))
console.print(
"To include a file in the conversation, use '[bold magenta]/add path/to/file[/bold magenta]'.\n"
"Type '[bold red]exit[/bold red]' or '[bold red]quit[/bold red]' to end.\n"
)
while True:
try:
user_input = console.input("[bold green]You>[/bold green] ").strip()
except (EOFError, KeyboardInterrupt):
console.print("\n[yellow]Exiting.[/yellow]")
break
if not user_input:
continue
if user_input.lower() in ["exit", "quit"]:
console.print("[yellow]Goodbye![/yellow]")
break
# If user is reading a file
if try_handle_add_command(user_input):
continue
# Get streaming response from OpenAI (DeepSeek)
response_data = stream_openai_response(user_input)
# Create any files if requested
if response_data.files_to_create:
for file_info in response_data.files_to_create:
create_file(file_info.path, file_info.content)
# Show and confirm diff edits if requested
if response_data.files_to_edit:
show_diff_table(response_data.files_to_edit)
confirm = console.input(
"\nDo you want to apply these changes? ([green]y[/green]/[red]n[/red]): "
).strip().lower()
if confirm == 'y':
for edit_info in response_data.files_to_edit:
apply_diff_edit(edit_info.path, edit_info.original_snippet, edit_info.new_snippet)
else:
console.print("[yellow]ℹ[/yellow] Skipped applying diff edits.", style="yellow")
console.print("[blue]Session finished.[/blue]")
if __name__ == "__main__":
main()