11defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
22 @ moduledoc false
33
4- @ type kind :: :json_array | :diff | :html | :search | :build | :source_code | :text
5- @ type strategy :: :json_array_lossless | :diff | :search_results | :log_output
4+ @ type kind ::
5+ :json_array | :json_document | :diff | :html | :search | :build | :source_code | :text
6+ @ type strategy ::
7+ :json_array_lossless | :json_document_lossless | :diff | :search_results | :log_output
68 @ type decision :: % {
79 required ( :kind ) => kind ( ) ,
810 required ( :confidence ) => float ( ) ,
@@ -12,6 +14,7 @@ defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
1214
1315 @ strategies % {
1416 json_array: :json_array_lossless ,
17+ json_document: :json_document_lossless ,
1518 diff: :diff ,
1619 search: :search_results ,
1720 build: :log_output
@@ -39,6 +42,9 @@ defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
3942 @ build_term_regex ~r/ \b (?:build|building|compile|compiling|compiled|test|tests|failed|failure|running|finished|warning|error|npm|yarn|pnpm|mix|make|cargo|gradle|maven|pytest|rspec|exunit)\b / i
4043 @ build_prefix_regex ~r/ ^\s *(?:\$ |==>|-->|Compiling|Running|Finished|warning:|error:|\[ \w +\] )/ mi
4144 @ stack_line_regex ~r/ (?:^\s +at\s +\S +|^\s *File\s +"[^"]+",\s +line\s +\d +|^\s *\S +:\d +:\d +:)/ m
45+ @ diagnostic_path_regex ~r/ ^\s *\S +\. (?:c|cc|cpp|cs|css|ex|exs|go|h|hpp|html|java|js|jsx|json|kt|m|md|mjs|mm|php|py|rb|rs|scss|svelte|swift|toml|ts|tsx|vue|xml|ya?ml)(?:\( \d +,\d +\) |:\d +(?::\d +)?)[:\s ].+/ im
46+ @ diagnostic_summary_regex ~r/ ^\s *(?:Build\s +(?:FAILED|succeeded)|BUILD\s +(?:FAILED|SUCCESSFUL)|\* \* \s +BUILD\s +(?:FAILED|SUCCEEDED)\s +\* \* |Failed!\s +-\s +Failed:|Passed!\s +-\s +Failed:|Test summary:|Found\s +\d +\s +(?:errors?|warnings?)|Executed\s +\d +\s +tests?,\s +with\s +\d +|\d +\s +(?:Warning|Error)\( s\) |\d +\s +(?:tests?|examples?)\s +(?:completed|run|passed|failed)|\d +\s +actionable tasks?:)/ im
47+ @ lint_rule_regex ~r/ \b (?:lint|style|correctness|suspicious|complexity|nursery|performance|security)\/ [a-z0-9_\/ -]+\b / i
4248
4349 @ source_keyword_regex ~r/ ^\s *(?:defmodule|defp?\s +\w +|class\s +\w +|function\s +\w +|import\s +|export\s +|const\s +\w +|let\s +\w +|var\s +\w +|pub\s +fn\s +\w +|fn\s +\w +|impl\s +\w +|module\s +\w +|alias\s +)/ m
4450 @ source_punctuation_regex ~r/ [{}();]/
@@ -58,6 +64,9 @@ defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
5864 json_array? ( trimmed ) ->
5965 decision ( :json_array , 100 )
6066
67+ json_document? ( trimmed ) ->
68+ decision ( :json_document , 100 )
69+
6170 ( points = diff_points ( content ) ) >= 70 ->
6271 decision ( :diff , points )
6372
@@ -100,6 +109,13 @@ defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
100109 end
101110 end
102111
112+ defp json_document? ( content ) do
113+ case Jason . decode ( content ) do
114+ { :ok , value } when is_map ( value ) -> true
115+ _other -> false
116+ end
117+ end
118+
103119 defp diff_points ( content ) do
104120 [
105121 score ( regex_match? ( @ diff_git_regex , content ) , 25 ) ,
@@ -151,9 +167,14 @@ defmodule CodexPooler.Gateway.RequestCompression.ContentDetector do
151167 defp build_points ( content ) do
152168 lines = lines ( content )
153169 severities = scan_count ( @ severity_regex , content )
170+ diagnostic_paths = scan_count ( @ diagnostic_path_regex , content )
171+ diagnostic_summaries = scan_count ( @ diagnostic_summary_regex , content )
154172
155173 [
156174 cond_score ( severities , [ { 2 , 30 } , { 1 , 20 } ] ) ,
175+ cond_score ( diagnostic_paths , [ { 2 , 35 } , { 1 , 25 } ] ) ,
176+ cond_score ( diagnostic_summaries , [ { 2 , 25 } , { 1 , 15 } ] ) ,
177+ score ( regex_match? ( @ lint_rule_regex , content ) , 15 ) ,
157178 score ( regex_match? ( @ build_term_regex , content ) , 20 ) ,
158179 score ( regex_match? ( @ build_prefix_regex , content ) , 20 ) ,
159180 score ( regex_match? ( @ stack_line_regex , content ) , 25 ) ,
0 commit comments