Skip to content

Commit d47c872

Browse files
committed
implement debug mode flag
1 parent 4f22f75 commit d47c872

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,6 @@ dmypy.json
129129

130130
# Yarn cache
131131
.yarn/
132+
133+
# Ignore secrets in '.env' (API keys)
134+
.env

jupyter_ai_tutor/app.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22

33
from jupyter_server.extension.application import ExtensionApp
4-
from traitlets import Unicode
4+
from traitlets import Bool, Unicode
55

66
from .handlers import ExplainHandler
77

@@ -20,12 +20,26 @@ class JupyterAITutorApp(ExtensionApp):
2020
),
2121
).tag(config=True)
2222

23+
debug = Bool(
24+
default_value=False,
25+
help="Whether to log prompts and replies to /tmp for debugging.",
26+
).tag(config=True)
27+
28+
flags = {
29+
"tutor-debug": (
30+
{"JupyterAITutorApp": {"debug": True}},
31+
"Log prompts and replies to /tmp for debugging.",
32+
)
33+
}
34+
2335
def initialize_settings(self):
2436
path = Path(self.agent_md) if self.agent_md else _DEFAULT_AGENT_MD
2537
self.settings["jupyter_ai_tutor.system_prompt"] = path.read_text(
2638
encoding="utf-8"
2739
).strip()
28-
self.log.info("jupyter_ai_tutor: loaded system prompt from %s", path)
40+
self.settings["jupyter_ai_tutor.debug"] = self.debug
41+
self.log.info("jupyter_ai_tutor: loaded system prompt from %s (debug=%s)", path, self.debug)
42+
2943

3044
def initialize_handlers(self):
3145
self.handlers = [

jupyter_ai_tutor/handlers.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime
23

34
import tornado
45
from jupyter_server.base.handlers import APIHandler
@@ -36,6 +37,31 @@ async def post(self):
3637
self.set_header("X-Accel-Buffering", "no")
3738

3839
system_prompt = self.settings.get("jupyter_ai_tutor.system_prompt", "")
40+
debug_mode = self.settings.get("jupyter_ai_tutor.debug", False)
41+
prompt_file = None
42+
answer_file = None
43+
accumulated_response = ""
44+
45+
if debug_mode:
46+
import os
47+
debug_dir = "/tmp/jupyter-ai-tutor"
48+
try:
49+
os.makedirs(debug_dir, exist_ok=True)
50+
except Exception as e:
51+
self.log.error(f"Failed to create debug directory {debug_dir}: {e}")
52+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
53+
prompt_file = f"{debug_dir}/{timestamp}_jupyter_tutor_prompt.txt"
54+
answer_file = f"{debug_dir}/{timestamp}_jupyter_tutor_answer.txt"
55+
try:
56+
with open(prompt_file, "w", encoding="utf-8") as f:
57+
f.write("=== SYSTEM PROMPT ===\n")
58+
f.write(system_prompt)
59+
f.write("\n=====================\n\n")
60+
f.write("=== USER MESSAGE ===\n")
61+
f.write(message_body)
62+
f.write("\n====================\n")
63+
except Exception as e:
64+
self.log.error(f"Failed to write tutor debug prompt: {e}")
3965

4066
try:
4167
from jupyter_ai_jupyternaut.jupyternaut.chat_models import ChatLiteLLM
@@ -63,10 +89,25 @@ async def post(self):
6389
)
6490
)
6591
if text:
92+
if debug_mode:
93+
accumulated_response += text
6694
self.write(f"data: {json.dumps({'text': text})}\n\n")
6795
self.flush()
6896

97+
if debug_mode and answer_file:
98+
try:
99+
with open(answer_file, "w", encoding="utf-8") as f:
100+
f.write(accumulated_response)
101+
except Exception as e:
102+
self.log.error(f"Failed to write tutor debug answer: {e}")
103+
69104
except StreamClosedError:
105+
if debug_mode and answer_file and accumulated_response:
106+
try:
107+
with open(answer_file, "w", encoding="utf-8") as f:
108+
f.write(accumulated_response)
109+
except Exception as e:
110+
pass
70111
return # Client disconnected; stop streaming
71112
except Exception as e:
72113
self.log.exception("Error during tutor LLM call")

0 commit comments

Comments
 (0)