summaryrefslogtreecommitdiff
path: root/convertToPB.go
blob: 13faad57c7c0a636df10506dbc49c4adffb1bcb5 (plain)
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
package main

import (
	"fmt"

	"go.wit.com/lib/protobuf/chatpb"
	"google.golang.org/genai"
)

func convertToPB(resp *genai.GenerateContentResponse) *chatpb.ChatEntry {
	entry := &chatpb.ChatEntry{}

	if resp == nil || len(resp.Candidates) == 0 || resp.Candidates[0].Content == nil {
		return entry
	}

	content := resp.Candidates[0].Content
	for _, part := range content.Parts {
		if part.Text != "" {
			entry.Parts = append(entry.Parts, &chatpb.Part{
				PartType: &chatpb.Part_Text{Text: part.Text},
			})
		}
		if fc := part.FunctionCall; fc != nil {
			fmt.Printf("Gemini API requested to execute command: %s\n", fc.Name)
			entry.Parts = append(entry.Parts, &chatpb.Part{
				PartType: &chatpb.Part_FunctionCall{
					FunctionCall: &chatpb.FunctionCall{
						Name: fc.Name,
						Args: &chatpb.ArgsInfo{}, // TODO: Properly map args from fc.Args map[string]any
					},
				},
			})
		}
		if fr := part.FunctionResponse; fr != nil {
			// Convert the FunctionResponse to the protobuf equivalent
			entry.Parts = append(entry.Parts, &chatpb.Part{
				PartType: &chatpb.Part_FunctionResponse{
					FunctionResponse: &chatpb.FunctionResponse{
						Name: fr.Name,
						// TODO: Properly map the response content
					},
				},
			})
		}
	}
	return entry
}