4
4
"context"
5
5
"encoding/json"
6
6
"net/url"
7
+ "strconv"
8
+ "strings"
7
9
8
10
"github.com/rs/zerolog"
9
11
@@ -45,7 +47,7 @@ type SnykLLMBindings interface {
45
47
// output - a channel that can be used to stream the results
46
48
Explain (ctx context.Context , input AIRequest , format OutputFormat , output chan <- string ) error
47
49
}
48
- type ExplainResult map [string ]string
50
+ type ExplainResult map [int ]string
49
51
50
52
type DeepCodeLLMBinding interface {
51
53
SnykLLMBindings
@@ -64,17 +66,21 @@ type DeepCodeLLMBindingImpl struct {
64
66
func (d * DeepCodeLLMBindingImpl ) ExplainWithOptions (ctx context.Context , options ExplainOptions ) (ExplainResult , error ) {
65
67
s := d .instrumentor .StartSpan (ctx , "code.ExplainWithOptions" )
66
68
defer d .instrumentor .Finish (s )
69
+ logger := d .logger .With ().Str ("method" , "code.ExplainWithOptions" ).Logger ()
67
70
response , err := d .runExplain (s .Context (), options )
68
71
explainResult := ExplainResult {}
69
72
if err != nil {
70
73
return explainResult , err
71
74
}
72
- index := 0
73
- for _ , explanation := range response {
74
- if index < len (options .Diffs ) {
75
- explainResult [options.Diffs [index ]] = explanation
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
+ return explainResult , parseErr
76
81
}
77
- index ++
82
+ index := explanationNumber - 1
83
+ explainResult [index ] = v
78
84
}
79
85
80
86
return explainResult , nil
0 commit comments