Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nicovank committed Feb 7, 2024
1 parent 7ca9e49 commit 277842e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/chatdbg/chatdbg_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,5 +429,4 @@ def converse(
print("Set the environment variable OPENAI_API_KEY to your key value.")
sys.exit(1)

the_prompt = buildPrompt(debugger)
print(conversation.converse(client, args, the_prompt[1]))
print(conversation.converse(client, args))
47 changes: 15 additions & 32 deletions src/chatdbg/conversation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
import argparse
import json
import textwrap

import lldb
import llm_utils

from .functions_lldb import LldbFunctions


def get_truncated_error_message(args, diagnostic) -> str:
"""
Alternate taking front and back lines until the maximum number of tokens.
"""
front: list[str] = []
back: list[str] = []
diagnostic_lines = diagnostic.splitlines()
n = len(diagnostic_lines)

def build_diagnostic_string():
return "\n".join(front) + "\n\n[...]\n\n" + "\n".join(reversed(back)) + "\n"

for i in range(n):
if i % 2 == 0:
line = diagnostic_lines[i // 2]
list = front
else:
line = diagnostic_lines[n - i // 2 - 1]
list = back
list.append(line)
count = llm_utils.count_tokens(args.llm, build_diagnostic_string())
if count > args.max_error_tokens:
list.pop()
break

if len(front) + len(back) == n:
return diagnostic
return build_diagnostic_string()


def converse(client, args, diagnostic):
# This is LLDB-only (for now?).
def converse(client, args: argparse.Namespace):
fns = LldbFunctions(args)
available_functions_names = [fn["function"]["name"] for fn in fns.as_tools()]
system_message = textwrap.dedent(
Expand All @@ -47,7 +20,17 @@ def converse(client, args, diagnostic):
Once you have identified the problem, explain the diagnostic and provide a way to fix the issue if you can.
"""
).strip()
user_message = f"Here is my error message:\n\n```\n{get_truncated_error_message(args, diagnostic)}\n```\n\nWhat's the problem?"
user_message = f"""Here is the reason the program stopped execution:
```
{fns.get_error_message()}
```
Here is a summary of the stack frames:
```
{fns.get_frame_summary()}
```
What's the problem?"""
conversation = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_message},
Expand Down
38 changes: 38 additions & 0 deletions src/chatdbg/conversation/functions_lldb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

import lldb

from .functions_interface import BaseFunctions


Expand All @@ -14,3 +16,39 @@ def as_tools(self):

def dispatch(self, name, arguments) -> Optional[str]:
return super().dispatch(name, arguments)

def get_frame_summary(self) -> str:
target = lldb.debugger.GetSelectedTarget()
if not target:
return None

for thread in target.process:
reason = thread.GetStopReason()
if reason not in [lldb.eStopReasonNone, lldb.eStopReasonInvalid]:
break

summaries = []
for i, frame in enumerate(thread):
name = frame.GetDisplayFunctionName().split("(")[0]
arguments = []
for j in range(
frame.GetFunction().GetType().GetFunctionArgumentTypes().GetSize()
):
arg = frame.FindVariable(frame.GetFunction().GetArgumentName(j))
if not arg:
continue
arguments.append(f"{arg.GetName()}={arg.GetValue()}")
summaries.append(f"{i}: {name}({', '.join(arguments)})")
return "\n".join(summaries)

def get_error_message(self) -> Optional[str]:
target = lldb.debugger.GetSelectedTarget()
if not target:
return None

for thread in target.process:
reason = thread.GetStopReason()
if reason not in [lldb.eStopReasonNone, lldb.eStopReasonInvalid]:
break

return thread.GetStopDescription(1024)

0 comments on commit 277842e

Please sign in to comment.