-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathconnectors.go
More file actions
84 lines (72 loc) · 2.38 KB
/
connectors.go
File metadata and controls
84 lines (72 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package setup
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/HexmosTech/git-lrc/network"
)
func redactConnectorErrorBody(body []byte, secrets ...string) string {
msg := string(body)
for _, secret := range secrets {
if strings.TrimSpace(secret) == "" {
continue
}
msg = strings.ReplaceAll(msg, secret, "[REDACTED]")
}
msg = strings.TrimSpace(msg)
if msg == "" {
return "<empty response body>"
}
return msg
}
// ValidateGeminiKey checks the key against LiveReview's validate-key endpoint.
func ValidateGeminiKey(result *SetupResult, geminiKey string, apiURL string) (bool, string, error) {
apiURL = strings.TrimSpace(apiURL)
if apiURL == "" {
apiURL = CloudAPIURL
}
reqBody := ValidateKeyRequest{
Provider: "gemini",
APIKey: geminiKey,
Model: DefaultGeminiModel,
}
client := network.NewSetupClient(30 * time.Second)
resp, err := network.SetupValidateConnectorKey(client, apiURL, result.OrgID, reqBody, result.AccessToken)
if err != nil {
return false, "", fmt.Errorf("failed to validate key: %w", err)
}
if resp.StatusCode != http.StatusOK {
// Redact submitted provider key from surfaced errors to avoid secret leakage.
return false, "", fmt.Errorf("validate-key returned %d: %s", resp.StatusCode, redactConnectorErrorBody(resp.Body, geminiKey))
}
var valResp ValidateKeyResponse
if err := json.Unmarshal(resp.Body, &valResp); err != nil {
return false, "", fmt.Errorf("failed to parse validation response: %w", err)
}
return valResp.Valid, valResp.Message, nil
}
// CreateGeminiConnector creates a Gemini AI connector in LiveReview.
func CreateGeminiConnector(result *SetupResult, geminiKey string, apiURL string) error {
apiURL = strings.TrimSpace(apiURL)
if apiURL == "" {
apiURL = CloudAPIURL
}
reqBody := CreateConnectorRequest{
ProviderName: "gemini",
APIKey: geminiKey,
ConnectorName: "Gemini Flash",
SelectedModel: DefaultGeminiModel,
DisplayOrder: 0,
}
client := network.NewSetupClient(30 * time.Second)
resp, err := network.SetupCreateConnector(client, apiURL, result.OrgID, reqBody, result.AccessToken)
if err != nil {
return fmt.Errorf("failed to create connector: %w", err)
}
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return fmt.Errorf("create connector returned %d: %s", resp.StatusCode, redactConnectorErrorBody(resp.Body, geminiKey))
}
return nil
}