8
8
import json
9
9
10
10
import llm_utils
11
- import openai
12
11
13
12
from assistant .lite_assistant import LiteAssistant
14
13
import chatdbg_utils
@@ -234,7 +233,7 @@ def why(
234
233
sys .exit (1 )
235
234
236
235
the_prompt = buildPrompt (debugger )
237
- args , _ = chatdbg_utils .parse_known_args (command )
236
+ args , _ = chatdbg_utils .parse_known_args (command . split () )
238
237
chatdbg_utils .explain (the_prompt [0 ], the_prompt [1 ], the_prompt [2 ], args )
239
238
240
239
@@ -389,7 +388,12 @@ def _instructions():
389
388
You are an assistant debugger.
390
389
The user is having an issue with their code, and you are trying to help them find the root cause.
391
390
They will provide a short summary of the issue and a question to be answered.
391
+
392
392
Call the `lldb` function to run lldb debugger commands on the stopped program.
393
+ Call the `get_code_surrounding` function to retrieve user code and give more context back to the user on their problem.
394
+ Call the `find_definition` function to retrieve the definition of a particular symbol.
395
+ You should call `find_definition` on every symbol that could be linked to the issue.
396
+
393
397
Don't hesitate to use as many function calls as needed to give the best possible answer.
394
398
Once you have identified the root cause of the problem, explain it and provide a way to fix the issue if you can.
395
399
"""
@@ -440,52 +444,6 @@ def get_code_surrounding(filename: str, lineno: int) -> str:
440
444
(lines , first ) = llm_utils .read_lines (filename , lineno - 7 , lineno + 3 )
441
445
return llm_utils .number_group_of_lines (lines , first )
442
446
443
- clangd = clangd_lsp_integration .clangd ()
444
-
445
- def find_definition (filename : str , lineno : int , character : int ) -> str :
446
- """
447
- {
448
- "name": "find_definition",
449
- "description": "Returns the definition for the symbol at the given source location.",
450
- "parameters": {
451
- "type": "object",
452
- "properties": {
453
- "filename": {
454
- "type": "string",
455
- "description": "The filename the code location is from."
456
- },
457
- "lineno": {
458
- "type": "integer",
459
- "description": "The line number where the symbol is present."
460
- },
461
- "character": {
462
- "type": "integer",
463
- "description": "The column number where the symbol is present."
464
- }
465
- },
466
- "required": [ "filename", "lineno", "character" ]
467
- }
468
- }
469
- """
470
- clangd .didOpen (filename , "c" if filename .endswith (".c" ) else "cpp" )
471
- definition = clangd .definition (filename , lineno , character )
472
- clangd .didClose (filename )
473
-
474
- if "result" not in definition or not definition ["result" ]:
475
- return "No definition found."
476
-
477
- path = clangd_lsp_integration .uri_to_path (definition ["result" ][0 ]["uri" ])
478
- start_lineno = definition ["result" ][0 ]["range" ]["start" ]["line" ] + 1
479
- end_lineno = definition ["result" ][0 ]["range" ]["end" ]["line" ] + 1
480
- (lines , first ) = llm_utils .read_lines (path , start_lineno - 5 , end_lineno + 5 )
481
- content = llm_utils .number_group_of_lines (lines , first )
482
- line_string = (
483
- f"line { start_lineno } "
484
- if start_lineno == end_lineno
485
- else f"lines { start_lineno } -{ end_lineno } "
486
- )
487
- return f"""File '{ path } ' at { line_string } :\n ```\n { content } \n ```"""
488
-
489
447
assistant = LiteAssistant (
490
448
_instructions (),
491
449
model = args .llm ,
@@ -500,6 +458,62 @@ def find_definition(filename: str, lineno: int, character: int) -> str:
500
458
print ("[WARNING] clangd is not available." )
501
459
print ("[WARNING] The `find_definition` function will not be made available." )
502
460
else :
461
+ clangd = clangd_lsp_integration .clangd ()
462
+
463
+ def find_definition (filename : str , lineno : int , symbol : str ) -> str :
464
+ """
465
+ {
466
+ "name": "find_definition",
467
+ "description": "Returns the definition for the given symbol at the given source line number.",
468
+ "parameters": {
469
+ "type": "object",
470
+ "properties": {
471
+ "filename": {
472
+ "type": "string",
473
+ "description": "The filename the symbol is from."
474
+ },
475
+ "lineno": {
476
+ "type": "integer",
477
+ "description": "The line number where the symbol is present."
478
+ },
479
+ "symbol": {
480
+ "type": "string",
481
+ "description": "The symbol to lookup."
482
+ }
483
+ },
484
+ "required": [ "filename", "lineno", "symbol" ]
485
+ }
486
+ }
487
+ """
488
+ # We just return the first match here. Maybe we should find all definitions.
489
+ with open (filename , "r" ) as file :
490
+ lines = file .readlines ()
491
+ if lineno - 1 >= len (lines ):
492
+ return "Symbol not found at that location!"
493
+ character = lines [lineno - 1 ].find (symbol )
494
+ if character == - 1 :
495
+ return "Symbol not found at that location!"
496
+ clangd .didOpen (filename , "c" if filename .endswith (".c" ) else "cpp" )
497
+ definition = clangd .definition (filename , lineno , character + 1 )
498
+ clangd .didClose (filename )
499
+
500
+ if "result" not in definition or not definition ["result" ]:
501
+ return "No definition found."
502
+
503
+ path = clangd_lsp_integration .uri_to_path (definition ["result" ][0 ]["uri" ])
504
+ start_lineno = definition ["result" ][0 ]["range" ]["start" ]["line" ] + 1
505
+ end_lineno = definition ["result" ][0 ]["range" ]["end" ]["line" ] + 1
506
+ (lines , first ) = llm_utils .read_lines (
507
+ path , start_lineno - 5 , end_lineno + 5
508
+ )
509
+ content = llm_utils .number_group_of_lines (lines , first )
510
+ line_string = (
511
+ f"line { start_lineno } "
512
+ if start_lineno == end_lineno
513
+ else f"lines { start_lineno } -{ end_lineno } "
514
+ )
515
+ return f"""File '{ path } ' at { line_string } :\n ```\n { content } \n ```"""
516
+
503
517
assistant .add_function (find_definition )
504
518
505
519
return assistant
@@ -517,6 +531,8 @@ def get_frame_summary() -> str:
517
531
518
532
summaries = []
519
533
for i , frame in enumerate (thread ):
534
+ if not frame .GetDisplayFunctionName ():
535
+ continue
520
536
name = frame .GetDisplayFunctionName ().split ("(" )[0 ]
521
537
arguments = []
522
538
for j in range (
0 commit comments