Skip to content

Commit 730e766

Browse files
committed
added snippets integration
1 parent 0c30a6e commit 730e766

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

handlers/gemini.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,59 @@
11
package handlers
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
8+
"io"
79
"log"
810
"net/http"
911
"os"
12+
"time"
1013

1114
"github.com/gofor-little/env"
1215
"github.com/google/generative-ai-go/genai"
1316
"google.golang.org/api/option"
1417
)
1518

19+
type Snippet struct {
20+
Title string `json:"title"`
21+
Category string `json:"category"`
22+
Content string `json:"content"`
23+
DateTime time.Time `json:"dateTime"`
24+
Notes string `json:"notes"`
25+
}
26+
1627
func GeminiHandler(w http.ResponseWriter, r *http.Request) {
1728

29+
if r.Method != http.MethodPost {
30+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
31+
return
32+
}
33+
34+
var bodyBytes bytes.Buffer
35+
if _, err := io.Copy(&bodyBytes, r.Body); err != nil {
36+
http.Error(w, "Error reading request body", http.StatusBadRequest)
37+
return
38+
}
39+
r.Body.Close()
40+
41+
requestBody := bodyBytes.String()
42+
43+
var snippets []Snippet
44+
err := json.Unmarshal([]byte(requestBody), &snippets)
45+
if err != nil {
46+
fmt.Println("Error unmarshalling JSON:", err)
47+
return
48+
}
49+
50+
var mergedContent string
51+
for _, snippet := range snippets {
52+
mergedContent += snippet.Content
53+
}
54+
1855
// Assuming getResponseFromGemini returns the generated text as a string
19-
generatedText, err := getResponseFromGemini(w)
56+
generatedText, err := getResponseFromGemini(w, mergedContent)
2057

2158
if err != nil {
2259
// Handle error appropriately (e.g., log the error and return a generic error message)
@@ -42,7 +79,7 @@ func GeminiHandler(w http.ResponseWriter, r *http.Request) {
4279
}
4380
}
4481

45-
func getResponseFromGemini(w http.ResponseWriter) (string, error) {
82+
func getResponseFromGemini(w http.ResponseWriter, mergedContent string) (string, error) {
4683
ctx := context.Background()
4784

4885
// Replace with your actual API key
@@ -53,13 +90,13 @@ func getResponseFromGemini(w http.ResponseWriter) (string, error) {
5390
defer client.Close()
5491

5592
model := client.GenerativeModel("gemini-1.5-flash")
56-
resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
93+
resp, err := model.GenerateContent(ctx, genai.Text("create a summary from the following content: " + mergedContent))
5794
if err != nil {
5895
return "", err
5996
}
6097
encodeJSON(w, resp)
6198

62-
return "hellooo from gljgldg", nil
99+
return "", nil
63100
}
64101

65102
func goDotEnvVariable(key string) string {

models/snippet.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package models
2+
3+
type Snippet struct {
4+
Title string `json:"title"`
5+
Category string `json:"category"`
6+
Content string `json:"content"`
7+
Date string `json:"dateTime"`
8+
Notes string `json:"notes"`
9+
}

0 commit comments

Comments
 (0)