-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
426 lines (342 loc) · 12.4 KB
/
Copy pathmain.py
File metadata and controls
426 lines (342 loc) · 12.4 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
main.py — DevMind's Entry Point
Ties everything together and starts the terminal REPL.
Features:
- Real-time streaming display
- Smart conversation history management
- Slash commands: /help /cost /metrics /tasks /history /save /load
/sessions /delete /cd /clear /exit
- Graceful error handling
- Autosave every N turns (configurable)
"""
from __future__ import annotations
import os
import sys
try:
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
except ImportError:
print("Please install the Rich library: pip install rich")
sys.exit(1)
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
print("Please install python-dotenv: pip install python-dotenv")
sys.exit(1)
from config import config
from logger import get_logger
log = get_logger("main")
console = Console()
def show_banner():
"""Display the welcome banner in the terminal."""
banner = Text()
banner.append(" DevMind ", style="bold white on blue")
banner.append(" AI Coding Assistant", style="dim")
console.print()
console.print(
Panel(
banner,
subtitle=(
"[dim]Inspired by Claude Code . Python + LangGraph . " "Streaming[/dim]"
),
border_style="blue",
padding=(0, 2),
)
)
console.print(
"[dim]Commands: [bold]/help[/bold] . [bold]/cost[/bold] . "
"[bold]/metrics[/bold] . [bold]/save[/bold] . [bold]/load[/bold] . "
"[bold]/exit[/bold][/dim]"
)
console.print()
def show_help():
"""Display the help panel."""
help_text = """
[bold]DevMind - How to Use:[/bold]
[cyan]Normal use:[/cyan]
Just type any coding question!
"what files are in this folder?"
"are there any bugs in main.py?"
"create a file called hello_world.py"
[cyan]Slash commands:[/cyan]
[bold]/help[/bold] - Show this help message
[bold]/cost[/bold] - Show API cost so far (tokens + USD)
[bold]/metrics[/bold] - Show latency / success / retry metrics
[bold]/tasks[/bold] - Show all tasks in this session
[bold]/history[/bold] - Print current conversation history
[bold]/save <name>[/bold] - Save conversation under <name>
[bold]/load <name>[/bold] - Load a saved conversation
[bold]/sessions[/bold] - List saved conversations
[bold]/delete <name>[/bold] - Delete a saved conversation
[bold]/clear[/bold] - Clear chat history
[bold]/cd <path>[/bold] - Change working directory
[bold]/exit[/bold] - Exit DevMind
[cyan]Tips:[/cyan]
. DevMind will read, edit, and run files on its own
. Be specific: "find the Y function in file X"
. Drop Python plugins into ~/.devmind/plugins/ to add new tools
"""
console.print(Panel(help_text.strip(), border_style="dim", title="[dim]Help[/dim]"))
def smart_truncate_history(history, max_messages=None, summary_threshold=None):
"""Keep conversation history manageable by summarizing old messages."""
max_msgs = max_messages or config.history.max_messages
threshold = summary_threshold or config.history.summary_threshold
if len(history) <= threshold:
return history
old_messages = history[: len(history) - max_msgs + 2]
recent_messages = history[len(history) - max_msgs + 2 :]
summary_parts = []
for msg in old_messages:
role = "User" if msg["role"] == "user" else "DevMind"
content_preview = msg["content"][:100]
summary_parts.append(f"- {role}: {content_preview}")
summary_text = (
"[Previous conversation summary]\n"
+ "\n".join(summary_parts[-6:])
+ "\n[End of summary]"
)
summarized = [
{"role": "user", "content": summary_text},
{"role": "assistant", "content": "Understood, I have the previous context."},
]
log.debug(
"History truncated: "
f"{len(history)} -> {len(summarized) + len(recent_messages)} messages"
)
return summarized + recent_messages
def _cmd_cost():
from cost_tracker import format_cost_summary
console.print(
Panel(
format_cost_summary(),
title="[dim]Session Cost[/dim]",
border_style="dim",
)
)
def _cmd_metrics():
from metrics import format_metrics_summary
console.print(
Panel(
format_metrics_summary(),
title="[dim]Performance Metrics[/dim]",
border_style="dim",
)
)
def _cmd_tasks(agent):
console.print(
Panel(
agent.get_task_summary(),
title="[dim]Task History[/dim]",
border_style="dim",
)
)
def _cmd_history(history):
if not history:
console.print("[dim]Conversation history is empty.[/dim]")
return
for i, msg in enumerate(history, 1):
role = msg["role"].capitalize()
preview = msg["content"][:200].replace("\n", " ")
style = "cyan" if msg["role"] == "user" else "green"
console.print(f"[{style}]{i:2d}. {role}:[/{style}] {preview}")
def _cmd_save(args, history, agent):
if not args:
console.print("[red]Usage: /save <name>[/red]")
return
try:
from persistence import save_session
path = save_session(args, history, agent.model_name)
console.print(f"[green]Saved:[/green] {path}")
except Exception as e:
console.print(f"[red]Save failed:[/red] {e}")
def _cmd_load(args, history):
if not args:
console.print("[red]Usage: /load <name>[/red]")
return history
try:
from persistence import load_session
session = load_session(args)
console.print(
f"[green]Loaded[/green] '{session.session_id}' "
f"- {len(session.history)} messages (model {session.model})"
)
return list(session.history)
except Exception as e:
console.print(f"[red]Load failed:[/red] {e}")
return history
def _cmd_sessions():
try:
from persistence import list_sessions
sessions = list_sessions()
except Exception as e:
console.print(f"[red]Failed to list sessions:[/red] {e}")
return
if not sessions:
console.print("[dim]No saved sessions.[/dim]")
return
table = Table(title="Saved sessions", show_header=True, header_style="bold")
table.add_column("Name")
table.add_column("Messages", justify="right")
table.add_column("Model")
table.add_column("Updated")
for s in sessions:
table.add_row(s["name"], str(s["messages"]), s["model"], s["updated_at"])
console.print(table)
def _cmd_delete(args):
if not args:
console.print("[red]Usage: /delete <name>[/red]")
return
try:
from persistence import delete_session
if delete_session(args):
console.print(f"[green]Deleted session '{args}'[/green]")
else:
console.print(f"[yellow]Session '{args}' not found[/yellow]")
except Exception as e:
console.print(f"[red]Delete failed:[/red] {e}")
def handle_slash_command(cmd, history, agent):
"""Handle a slash command. Returns (should_continue, updated_history)."""
cmd = cmd.strip()
lower = cmd.lower()
parts = cmd.split(maxsplit=1)
verb = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
if verb in ("/exit", "/quit"):
console.print("\n[dim]Shutting down DevMind... Goodbye![/dim]\n")
return False, history
if verb == "/help":
show_help()
elif verb == "/cost":
_cmd_cost()
elif verb == "/metrics":
_cmd_metrics()
elif verb == "/tasks":
_cmd_tasks(agent)
elif verb == "/history":
_cmd_history(history)
elif verb == "/save":
_cmd_save(args, history, agent)
elif verb == "/load":
history = _cmd_load(args, history)
elif verb == "/sessions":
_cmd_sessions()
elif verb == "/delete":
_cmd_delete(args)
elif verb == "/clear":
history.clear()
console.clear()
show_banner()
console.print("[dim]Chat history cleared.[/dim]\n")
elif verb == "/cd":
new_dir = args.strip()
if not new_dir:
console.print("[red]Usage: /cd <path>[/red]")
else:
try:
os.chdir(new_dir)
from context import build_system_prompt
agent.system_prompt = build_system_prompt()
console.print(f"[dim]Working directory: {os.getcwd()}[/dim]")
log.info(f"Working directory changed: {os.getcwd()}")
except FileNotFoundError:
console.print(f"[red]Directory not found: {new_dir}[/red]")
else:
console.print(f"[red]Unknown command: {lower}[/red] - [dim]type /help[/dim]")
return True, history
def display_streaming_response(agent, user_input, lc_history):
"""Stream Claude's response token by token to the terminal."""
console.print("[bold green]DevMind:[/bold green]")
full_response = ""
buffer = ""
try:
for chunk in agent.chat_stream(user_input, lc_history):
full_response += chunk
buffer += chunk
if "\n" in buffer or len(buffer) > 80:
console.print(buffer, end="", highlight=False)
buffer = ""
if buffer:
console.print(buffer, end="", highlight=False)
console.print()
except Exception as e:
log.error(f"Streaming display error: {e}")
if not full_response:
try:
full_response = agent.chat(user_input, lc_history)
try:
console.print(Markdown(full_response))
except Exception:
console.print(full_response)
except Exception as inner:
console.print(f"[red]Error: {inner}[/red]")
console.print()
return full_response
def _maybe_autosave(history, agent, turn):
if not config.persistence.enabled:
return
every = config.persistence.autosave_every
if every <= 0 or turn % every != 0:
return
try:
from persistence import save_session
save_session("autosave", history, agent.model_name, metadata={"auto": True})
log.debug(f"Autosaved at turn {turn}")
except Exception as e:
log.debug(f"Autosave skipped: {e}")
def run_repl():
"""Run the interactive REPL loop."""
console.print("[dim]Starting DevMind...[/dim]", end="")
try:
from agent import DevMindAgent
agent = DevMindAgent()
console.print(" [green]Ready![/green]\n")
except Exception as e:
console.print(f"\n[red]Error: {e}[/red]")
if "API_KEY" in str(e):
console.print(
"\n[dim]Fix: Add your Anthropic API key to a .env file:\n"
"ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxx[/dim]"
)
sys.exit(1)
conversation_history = []
turn = 0
while True:
try:
console.print(f"[dim]{os.getcwd()}[/dim]")
user_input = console.input("[bold cyan]You:[/bold cyan] ").strip()
if not user_input:
continue
if user_input.startswith("/"):
should_continue, conversation_history = handle_slash_command(
user_input, conversation_history, agent
)
if not should_continue:
break
continue
console.print()
lc_history = agent.get_history_messages(conversation_history)
response = display_streaming_response(agent, user_input, lc_history)
conversation_history.append({"role": "user", "content": user_input})
conversation_history.append({"role": "assistant", "content": response})
turn += 1
conversation_history = smart_truncate_history(conversation_history)
_maybe_autosave(conversation_history, agent, turn)
except KeyboardInterrupt:
console.print("\n\n[dim]Ctrl+C detected - shutting down...[/dim]")
break
except EOFError:
break
except Exception as e:
log.error(f"REPL error: {e}")
console.print(f"\n[red]Error: {e}[/red]\n")
continue
def main():
"""Start DevMind."""
show_banner()
run_repl()
if __name__ == "__main__":
main()