2
0
Files
gitcaddy-ai/examples/go/main.go
logikonline 17581918dd feat: add client libraries, examples, and project documentation
Add Go and .NET client libraries for GitCaddy AI Service with usage examples. Include Business Source License 1.1, Makefile for build automation, and comprehensive README. Update service configuration and all service classes to support new client integration.
2026-01-19 10:44:24 -05:00

148 lines
4.0 KiB
Go

// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: BSL-1.1
// Example: Using GitCaddy AI Client in Go
package main
import (
"context"
"fmt"
"log"
"time"
ai "git.marketally.com/gitcaddy/gitcaddy-ai/go/gitcaddy-ai-client"
pb "git.marketally.com/gitcaddy/gitcaddy-ai/go/gitcaddy-ai-client/proto"
)
func main() {
fmt.Println("GitCaddy AI Client Example (Go)")
fmt.Println("================================\n")
// Create client
client, err := ai.NewClient("localhost:5051")
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Check health
fmt.Println("Checking service health...")
health, err := client.CheckHealth(ctx)
if err != nil {
log.Fatalf("Health check failed: %v", err)
}
fmt.Printf("Service healthy: %v\n", health.Healthy)
fmt.Printf("Version: %s\n", health.Version)
if health.License != nil {
fmt.Printf("License: %s tier for %s\n", health.License.Tier, health.License.Customer)
}
fmt.Println()
// Example 1: Code Review
fmt.Println("Example 1: Reviewing a Pull Request")
fmt.Println("------------------------------------")
reviewResp, err := client.ReviewPullRequest(ctx, &pb.ReviewPullRequestRequest{
RepoId: 1,
PullRequestId: 42,
PrTitle: "Add user authentication",
PrDescription: "Implements JWT-based authentication for the API",
BaseBranch: "main",
HeadBranch: "feature/auth",
Files: []*pb.FileDiff{
{
Path: "src/auth/jwt.go",
Status: "added",
Language: "go",
Patch: `
+package auth
+
+import "github.com/golang-jwt/jwt/v5"
+
+func GenerateToken(userID string) (string, error) {
+ token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
+ "user_id": userID,
+ })
+ return token.SignedString([]byte("secret"))
+}
`,
},
},
Options: &pb.ReviewOptions{
CheckSecurity: true,
CheckPerformance: true,
SuggestImprovements: true,
},
})
if err != nil {
log.Printf("Review failed: %v", err)
} else {
fmt.Printf("Verdict: %v\n", reviewResp.Verdict)
summary := reviewResp.Summary
if len(summary) > 500 {
summary = summary[:500] + "..."
}
fmt.Printf("Summary: %s\n\n", summary)
}
// Example 2: Generate Commit Message
fmt.Println("Example 2: Generating Commit Message")
fmt.Println("-------------------------------------")
commitResp, err := client.GenerateCommitMessage(ctx, &pb.GenerateCommitMessageRequest{
RepoId: 1,
Style: "conventional",
Files: []*pb.FileDiff{
{
Path: "src/api/handlers.go",
Status: "modified",
Patch: `
@@ -45,6 +45,15 @@ func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request) {
+func (h *Handler) DeleteUser(w http.ResponseWriter, r *http.Request) {
+ id := chi.URLParam(r, "id")
+ if err := h.userService.Delete(r.Context(), id); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
`,
},
},
})
if err != nil {
log.Printf("Commit message generation failed: %v", err)
} else {
fmt.Printf("Suggested message: %s\n", commitResp.Message)
if len(commitResp.Alternatives) > 0 {
fmt.Println("Alternatives:")
for _, alt := range commitResp.Alternatives {
fmt.Printf(" - %s\n", alt)
}
}
fmt.Println()
}
// Example 3: Start a Workflow
fmt.Println("Example 3: Starting a Code Review Workflow")
fmt.Println("-------------------------------------------")
err = client.StartWorkflow(ctx, &pb.StartWorkflowRequest{
RepoId: 1,
WorkflowType: "code_review",
Goal: "Review all changes in the auth module for security issues",
Parameters: map[string]string{
"path": "src/auth/",
},
}, func(event *pb.WorkflowEvent) error {
fmt.Printf("[%s] %s: %s\n", event.Type, event.AgentId, event.Message)
return nil
})
if err != nil {
log.Printf("Workflow failed: %v", err)
}
fmt.Println()
fmt.Println("Done!")
}