|
| 1 | +package ai |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const phindAPIURL = "https://https.extension.phind.com/agent/" |
| 14 | +const defaultPhindModel = "Phind-70B" |
| 15 | + |
| 16 | +type PhindConfig struct { |
| 17 | + Model string |
| 18 | +} |
| 19 | + |
| 20 | +type PhindProvider struct { |
| 21 | + config PhindConfig |
| 22 | + client *http.Client |
| 23 | +} |
| 24 | + |
| 25 | +func NewPhindProvider(model string) *PhindProvider { |
| 26 | + if model == "" { |
| 27 | + model = defaultPhindModel |
| 28 | + } |
| 29 | + return &PhindProvider{ |
| 30 | + config: PhindConfig{Model: model}, |
| 31 | + client: &http.Client{Timeout: 30 * time.Second}, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (p *PhindProvider) GenerateCommitMessage(diff string, commitTypes string) (string, error) { |
| 36 | + // Construct the user prompt based on the Rust implementation's draft prompt |
| 37 | + userPrompt := fmt.Sprintf(`Generate a concise git commit message written in present tense for the following code diff with the given specifications below: |
| 38 | + The output response must be in format: |
| 39 | + <type>(<optional scope>): <commit message> |
| 40 | + Choose a type from the type-to-description JSON below that best describes the git diff: |
| 41 | + %s |
| 42 | + Focus on being accurate and concise. |
| 43 | + Commit message must be a maximum of 72 characters. |
| 44 | + Exclude anything unnecessary such as translation. Your entire response will be passed directly into git commit. |
| 45 | +
|
| 46 | + Code diff: |
| 47 | + 'diff |
| 48 | + %s |
| 49 | + ' |
| 50 | + `, commitTypes, diff) |
| 51 | + |
| 52 | + payload := map[string]interface{}{ |
| 53 | + "additional_extension_context": "", |
| 54 | + "allow_magic_buttons": true, |
| 55 | + "is_vscode_extension": true, |
| 56 | + "message_history": []map[string]string{ |
| 57 | + { |
| 58 | + "content": userPrompt, |
| 59 | + "role": "user", |
| 60 | + }, |
| 61 | + }, |
| 62 | + "requested_model": p.config.Model, |
| 63 | + "user_input": userPrompt, |
| 64 | + } |
| 65 | + |
| 66 | + jsonPayload, err := json.Marshal(payload) |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("failed to marshal payload: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + req, err := http.NewRequest("POST", phindAPIURL, bytes.NewBuffer(jsonPayload)) |
| 72 | + if err != nil { |
| 73 | + return "", fmt.Errorf("failed to create request: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + req.Header.Set("Content-Type", "application/json") |
| 77 | + req.Header.Set("User-Agent", "") |
| 78 | + req.Header.Set("Accept", "*/*") |
| 79 | + req.Header.Set("Accept-Encoding", "Identity") |
| 80 | + |
| 81 | + resp, err := p.client.Do(req) |
| 82 | + if err != nil { |
| 83 | + return "", fmt.Errorf("failed to send request to Phind: %w", err) |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + |
| 87 | + if resp.StatusCode != http.StatusOK { |
| 88 | + bodyBytes, _ := io.ReadAll(resp.Body) |
| 89 | + return "", fmt.Errorf("Phind API returned status %d: %s", resp.StatusCode, string(bodyBytes)) |
| 90 | + } |
| 91 | + |
| 92 | + bodyBytes, err := io.ReadAll(resp.Body) |
| 93 | + if err != nil { |
| 94 | + return "", fmt.Errorf("failed to read response body: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + var fullContent strings.Builder |
| 98 | + lines := strings.Split(string(bodyBytes), "\n") |
| 99 | + for _, line := range lines { |
| 100 | + if strings.HasPrefix(line, "data: ") { |
| 101 | + data := strings.TrimPrefix(line, "data: ") |
| 102 | + var responseJson map[string]interface{} |
| 103 | + if err := json.Unmarshal([]byte(data), &responseJson); err != nil { |
| 104 | + continue // Skip malformed lines |
| 105 | + } |
| 106 | + if choices, ok := responseJson["choices"].([]interface{}); ok && len(choices) > 0 { |
| 107 | + if choice, ok := choices[0].(map[string]interface{}); ok { |
| 108 | + if delta, ok := choice["delta"].(map[string]interface{}); ok { |
| 109 | + if content, ok := delta["content"].(string); ok { |
| 110 | + fullContent.WriteString(content) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + if fullContent.Len() == 0 { |
| 118 | + return "", fmt.Errorf("no content found in Phind response") |
| 119 | + } |
| 120 | + |
| 121 | + return fullContent.String(), nil |
| 122 | +} |
| 123 | +func (p *PhindProvider) GetModel() string { |
| 124 | + return p.config.Model |
| 125 | +} |
0 commit comments