Skip to content

Commit

Permalink
move functions
Browse files Browse the repository at this point in the history
  • Loading branch information
khlevin committed Apr 8, 2024
1 parent 84cfc65 commit 7a3bd21
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
13 changes: 1 addition & 12 deletions src/chatdbg/chatdbg_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from typing import List, Optional, Union
import lldb

import llm_utils

from chatdbg.native_util import clangd_lsp_integration
from chatdbg.native_util.stacks import _ArgumentEntry, _FrameSummaryEntry, _SkippedFramesEntry
from chatdbg.native_util.code import code
from chatdbg.util.config import chatdbg_config

from chatdbg.native_util.dbg_dialog import DBGDialog
Expand All @@ -25,16 +24,6 @@ def __lldb_init_module(debugger: lldb.SBDebugger, internal_dict: dict) -> None:
# chatdbg_config.format = "text"


def code(command):
parts = command.split(":")
if len(parts) != 2:
return ("usage: code <filename>:<lineno>")
filename, lineno = parts[0], int(parts[1])
try:
lines, first = llm_utils.read_lines(filename, lineno - 7, lineno + 3)
except FileNotFoundError:
return (f"file '{filename}' not found.")
return llm_utils.number_group_of_lines(lines, first)

@lldb.command("code")
def _function_code(
Expand Down
12 changes: 12 additions & 0 deletions src/chatdbg/native_util/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import llm_utils

def code(command):
parts = command.split(":")
if len(parts) != 2:
return ("usage: code <filename>:<lineno>")
filename, lineno = parts[0], int(parts[1])
try:
lines, first = llm_utils.read_lines(filename, lineno - 7, lineno + 3)
except FileNotFoundError:
return (f"file '{filename}' not found.")
return llm_utils.number_group_of_lines(lines, first)
47 changes: 1 addition & 46 deletions src/chatdbg/native_util/dbg_dialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import sys
import textwrap

import llm_utils

from . import clangd_lsp_integration
from ..util.prompts import (build_followup_prompt, build_initial_prompt,
Expand All @@ -11,7 +8,7 @@
from ..util.config import chatdbg_config
from ..util.history import CommandHistory
from ..util.log import ChatDBGLog
from .stacks import _FrameSummaryEntry, _SkippedFramesEntry
from .stacks import build_enriched_stacktrace


class DBGError(Exception):
Expand All @@ -20,48 +17,6 @@ def __init__(self, message):
self.message = message
super().__init__(self.message)

def build_enriched_stacktrace(summaries):
parts = []
if not summaries:
print("could not generate any frame summary.")
else:
frame_summary = "\n".join([str(s) for s in summaries])
parts.append(frame_summary)

total_frames = sum(
[s.count() if isinstance(s, _SkippedFramesEntry) else 1 for s in summaries]
)

if total_frames > 1000:
parts.append(
"Note that there are over 1000 frames in the stack trace, hinting at a possible stack overflow error."
)

max_initial_locations_to_send = 3
source_code_entries = []
for summary in summaries:
if isinstance(summary, _FrameSummaryEntry):
file_path, lineno = summary.file_path(), summary.lineno()
lines, first = llm_utils.read_lines(file_path, lineno - 10, lineno + 9)
block = llm_utils.number_group_of_lines(lines, first)
block = textwrap.indent(block, ' ')
source_code_entries.append(
f"Frame #{summary.index()} at {file_path}:{lineno}:\n{block}\n"
)

if len(source_code_entries) == max_initial_locations_to_send:
break

if source_code_entries:
parts.append(
f"Here is the source code for the first {len(source_code_entries)} frames:\n\n"
+ "\n\n".join(source_code_entries)
)
else:
print("could not retrieve source code for any frames.")

return "\n\n".join(parts)


class DBGDialog:
# The log file used by the listener on the Assistant
Expand Down
45 changes: 45 additions & 0 deletions src/chatdbg/native_util/stacks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import textwrap
from typing import List

import llm_utils


class _ArgumentEntry:
def __init__(self, type: str, name: str, value: str):
Expand Down Expand Up @@ -59,3 +62,45 @@ def __str__(self):
def __repr__(self):
return f"_SkippedFramesEntry({self._count})"


def build_enriched_stacktrace(summaries):
parts = []
if not summaries:
print("could not generate any frame summary.")
else:
frame_summary = "\n".join([str(s) for s in summaries])
parts.append(frame_summary)

total_frames = sum(
[s.count() if isinstance(s, _SkippedFramesEntry) else 1 for s in summaries]
)

if total_frames > 1000:
parts.append(
"Note that there are over 1000 frames in the stack trace, hinting at a possible stack overflow error."
)

max_initial_locations_to_send = 3
source_code_entries = []
for summary in summaries:
if isinstance(summary, _FrameSummaryEntry):
file_path, lineno = summary.file_path(), summary.lineno()
lines, first = llm_utils.read_lines(file_path, lineno - 10, lineno + 9)
block = llm_utils.number_group_of_lines(lines, first)
block = textwrap.indent(block, ' ')
source_code_entries.append(
f"Frame #{summary.index()} at {file_path}:{lineno}:\n{block}\n"
)

if len(source_code_entries) == max_initial_locations_to_send:
break

if source_code_entries:
parts.append(
f"Here is the source code for the first {len(source_code_entries)} frames:\n\n"
+ "\n\n".join(source_code_entries)
)
else:
print("could not retrieve source code for any frames.")

return "\n\n".join(parts)

0 comments on commit 7a3bd21

Please sign in to comment.