4
4
"context"
5
5
"encoding/json"
6
6
"net/url"
7
- "strconv"
8
- "strings"
7
+ "slices"
9
8
10
9
"github.com/rs/zerolog"
11
10
@@ -47,7 +46,7 @@ type SnykLLMBindings interface {
47
46
// output - a channel that can be used to stream the results
48
47
Explain (ctx context.Context , input AIRequest , format OutputFormat , output chan <- string ) error
49
48
}
50
- type ExplainResult map [ int ]string
49
+ type ExplainResult [ ]string
51
50
52
51
type DeepCodeLLMBinding interface {
53
52
SnykLLMBindings
@@ -66,24 +65,29 @@ type DeepCodeLLMBindingImpl struct {
66
65
func (d * DeepCodeLLMBindingImpl ) ExplainWithOptions (ctx context.Context , options ExplainOptions ) (ExplainResult , error ) {
67
66
s := d .instrumentor .StartSpan (ctx , "code.ExplainWithOptions" )
68
67
defer d .instrumentor .Finish (s )
69
- logger := d .logger .With ().Str ("method" , "code.ExplainWithOptions" ).Logger ()
70
68
response , err := d .runExplain (s .Context (), options )
71
69
explainResult := ExplainResult {}
72
70
if err != nil {
73
71
return explainResult , err
74
72
}
75
- for k , v := range response {
76
- // response key is "explanation1", "explanation2", etc
77
- explanationNumber , parseErr := strconv .Atoi (strings .ReplaceAll (strings .ToLower (k ), "explanation" , "" ))
78
- if parseErr != nil || (explanationNumber - 1 ) < 0 {
79
- logger .Err (parseErr ).Msgf ("error parsing explanationNumber%s" , k )
80
- continue
81
- }
82
- index := explanationNumber - 1
83
- explainResult [index ] = v
73
+
74
+ orderedExplainResults := getOrderedResponse (response )
75
+
76
+ return orderedExplainResults , nil
77
+ }
78
+
79
+ func getOrderedResponse (explainResponse Explanations ) []string {
80
+ explainMapKeys := make ([]string , 0 , len (explainResponse ))
81
+ for k := range explainResponse {
82
+ explainMapKeys = append (explainMapKeys , k )
84
83
}
84
+ slices .Sort (explainMapKeys )
85
85
86
- return explainResult , nil
86
+ orderedValues := make ([]string , 0 , len (explainResponse ))
87
+ for _ , key := range explainMapKeys {
88
+ orderedValues = append (orderedValues , explainResponse [key ])
89
+ }
90
+ return orderedValues
87
91
}
88
92
89
93
func (d * DeepCodeLLMBindingImpl ) PublishIssues (_ context.Context , _ []map [string ]string ) error {
0 commit comments