2
0
Files
gitcaddy-ai/examples/dotnet/Program.cs
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

164 lines
4.8 KiB
C#

// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: BSL-1.1
// Example: Using GitCaddy AI Client in .NET
using GitCaddy.AI.Client;
using GitCaddy.AI.Proto;
Console.WriteLine("GitCaddy AI Client Example");
Console.WriteLine("==========================\n");
// Create client
using var client = new GitCaddyAIClient("http://localhost:5051");
// Check health
Console.WriteLine("Checking service health...");
var health = await client.CheckHealthAsync();
Console.WriteLine($"Service healthy: {health.Healthy}");
Console.WriteLine($"Version: {health.Version}");
if (health.License != null)
{
Console.WriteLine($"License: {health.License.Tier} tier for {health.License.Customer}");
}
Console.WriteLine();
// Example 1: Code Review
Console.WriteLine("Example 1: Reviewing a Pull Request");
Console.WriteLine("------------------------------------");
var reviewResponse = await client.ReviewPullRequestAsync(new ReviewPullRequestRequest
{
RepoId = 1,
PullRequestId = 42,
PrTitle = "Add user authentication",
PrDescription = "Implements JWT-based authentication for the API",
BaseBranch = "main",
HeadBranch = "feature/auth",
Files =
{
new 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 = new ReviewOptions
{
CheckSecurity = true,
CheckPerformance = true,
SuggestImprovements = true
}
});
Console.WriteLine($"Verdict: {reviewResponse.Verdict}");
Console.WriteLine($"Summary: {reviewResponse.Summary[..Math.Min(500, reviewResponse.Summary.Length)]}...\n");
// Example 2: Explain Code
Console.WriteLine("Example 2: Explaining Code");
Console.WriteLine("---------------------------");
var explainResponse = await client.ExplainCodeAsync(new ExplainCodeRequest
{
RepoId = 1,
FilePath = "src/utils/retry.go",
Code = @"
func Retry(attempts int, sleep time.Duration, f func() error) error {
var err error
for i := 0; i < attempts; i++ {
if err = f(); err == nil {
return nil
}
time.Sleep(sleep)
sleep *= 2
}
return fmt.Errorf(""after %d attempts: %w"", attempts, err)
}
",
Question = "What pattern is this implementing?"
});
Console.WriteLine($"Explanation: {explainResponse.Explanation[..Math.Min(500, explainResponse.Explanation.Length)]}...\n");
// Example 3: Generate Commit Message
Console.WriteLine("Example 3: Generating Commit Message");
Console.WriteLine("-------------------------------------");
var commitResponse = await client.GenerateCommitMessageAsync(new GenerateCommitMessageRequest
{
RepoId = 1,
Style = "conventional",
Files =
{
new 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)
+}
"
}
}
});
Console.WriteLine($"Suggested message: {commitResponse.Message}");
if (commitResponse.Alternatives.Count > 0)
{
Console.WriteLine("Alternatives:");
foreach (var alt in commitResponse.Alternatives)
{
Console.WriteLine($" - {alt}");
}
}
Console.WriteLine();
// Example 4: Triage Issue
Console.WriteLine("Example 4: Triaging an Issue");
Console.WriteLine("-----------------------------");
var triageResponse = await client.TriageIssueAsync(new TriageIssueRequest
{
RepoId = 1,
IssueId = 123,
Title = "App crashes when uploading large files",
Body = @"
When I try to upload a file larger than 100MB, the application crashes with an out of memory error.
Steps to reproduce:
1. Go to upload page
2. Select a file > 100MB
3. Click upload
4. App crashes
Expected: Should show an error or handle gracefully
Actual: Application crashes
Environment: Windows 11, Chrome 120
",
AvailableLabels = { "bug", "enhancement", "question", "documentation", "performance", "security", "crash" }
});
Console.WriteLine($"Priority: {triageResponse.Priority}");
Console.WriteLine($"Category: {triageResponse.Category}");
Console.WriteLine($"Suggested labels: {string.Join(", ", triageResponse.SuggestedLabels)}");
Console.WriteLine($"Summary: {triageResponse.Summary[..Math.Min(300, triageResponse.Summary.Length)]}...\n");
Console.WriteLine("Done!");