Skip to content

Commit

Permalink
Add custom errors to predefined prompt
Browse files Browse the repository at this point in the history
Summary: Adds custom errors to the predefined prompt.

Reviewed By: ianmah

Differential Revision: D70554314

fbshipit-source-id: 748ffd50c9ff70046ee36bdde0b2e87460cc34ee
  • Loading branch information
Michael Thomas authored and facebook-github-bot committed Mar 5, 2025
1 parent 1efcd2f commit 76d0dec
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,44 @@ open Hh_prelude

let max_file_content_lines = 400

let extract_prompt_context pos =
let extract_prompt_context buf pos =
let path = Pos.(filename pos) in
let start_ln = Pos.line pos - 1 and end_ln = Pos.end_line pos - 1 in
let line_num_width = 1 + (String.length @@ string_of_int end_ln) in
List.iteri (Errors.read_lines path) ~f:(fun ln str ->
if ln >= start_ln && ln <= end_ln then (
let ln_num =
String.pad_left (Int.to_string (ln + 1)) ~char:' ' ~len:line_num_width
in
Buffer.add_string buf ln_num;
Buffer.add_string buf " | ";
Buffer.add_string buf str;
Buffer.add_string buf "\n"
))

let create_user_prompt selection user_error =
let buf = Buffer.create 500 in
let () =
List.iteri (Errors.read_lines path) ~f:(fun ln str ->
if ln >= start_ln && ln <= end_ln then (
let ln_num =
String.pad_left
(Int.to_string (ln + 1))
~char:' '
~len:line_num_width
in
Buffer.add_string buf ln_num;
Buffer.add_string buf " | ";
Buffer.add_string buf str;
Buffer.add_string buf "\n"
))
Buffer.add_string
buf
"Given the following snippet of Hack code that is part of the file:\n<SNIPPET>";
extract_prompt_context buf selection;
Buffer.add_string buf "\n</SNIPPET>\n<DIAGNOSTIC>\n";
Buffer.add_string buf (Extended_error_formatter.to_string user_error);
Buffer.add_string buf "\n</DIAGNOSTIC>\n";
(match User_error.custom_errors user_error with
| [] -> ()
| msgs ->
List.iter msgs ~f:(fun str ->
Buffer.add_string buf (Format.sprintf {|<HINT>%s<\HINT>\n|} str)));
Buffer.add_string
buf
{|Edit <SNIPPET> in a way that would fix that lint.
If there are multiple ways to fix this issue, please return in the code section the most strightforward one that is part of <SNIPPET>,
any further suggestions can be added in the explanation section.|}
in
Buffer.contents buf

let create_user_prompt selection user_error =
Format.sprintf
{|
Given the following snippet of Hack code that is part of the file:
%s
<DIAGNOSTIC>
%s
</DIAGNOSTIC>
Edit <selection_to_edit> in a way that would fix that lint.
If there are multiple ways to fix this issue, please return in the code section the most strightforward one that is part of <selection_to_edit>,
any further suggestions can be added in the explanation section.
|}
(extract_prompt_context selection)
(Extended_error_formatter.to_string user_error)

let error_to_show_inline_chat_command user_error =
let claim = User_error.claim_message user_error in
let override_selection =
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/src/errors/user_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,5 @@ let to_json ~(human_formatter : (_ -> string) option) ~filename_to_string error
let claim_message { claim; _ } = claim

let function_pos { function_pos; _ } = function_pos

let custom_errors { custom_msgs; _ } = custom_msgs
2 changes: 2 additions & 0 deletions hphp/hack/src/errors/user_error.mli
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ val to_json :
val claim_message : ('a, _) t -> 'a Message.t

val function_pos : ('a, _) t -> 'a option

val custom_errors : (_, _) t -> string list
Loading

0 comments on commit 76d0dec

Please sign in to comment.