|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +package com.microsoft.semantickernel.samples.plugins.github; |
| 3 | + |
| 4 | +import reactor.core.publisher.Mono; |
| 5 | +import reactor.netty.http.client.HttpClient; |
| 6 | +import com.microsoft.semantickernel.semanticfunctions.annotations.DefineKernelFunction; |
| 7 | +import com.microsoft.semantickernel.semanticfunctions.annotations.KernelFunctionParameter; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class GitHubPlugin { |
| 13 | + public static final String baseUrl = "https://api.github.com"; |
| 14 | + private final String token; |
| 15 | + |
| 16 | + public GitHubPlugin(String token) { |
| 17 | + this.token = token; |
| 18 | + } |
| 19 | + |
| 20 | + @DefineKernelFunction(name = "get_user_info", description = "Get user information from GitHub", returnType = "com.microsoft.semantickernel.samples.plugins.github.GitHubModel$User") |
| 21 | + public Mono<GitHubModel.User> getUserProfileAsync() { |
| 22 | + HttpClient client = createClient(); |
| 23 | + |
| 24 | + return makeRequestAsync(client, "/user") |
| 25 | + .map(json -> { |
| 26 | + try { |
| 27 | + return GitHubModel.objectMapper.readValue(json, GitHubModel.User.class); |
| 28 | + } catch (IOException e) { |
| 29 | + throw new IllegalStateException("Failed to deserialize GitHubUser", e); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @DefineKernelFunction(name = "get_repo_info", description = "Get repository information from GitHub", returnType = "com.microsoft.semantickernel.samples.plugins.github.GitHubModel$Repository") |
| 35 | + public Mono<GitHubModel.Repository> getRepositoryAsync( |
| 36 | + @KernelFunctionParameter(name = "organization", description = "The name of the repository to retrieve information for") String organization, |
| 37 | + @KernelFunctionParameter(name = "repo_name", description = "The name of the repository to retrieve information for") String repoName) { |
| 38 | + HttpClient client = createClient(); |
| 39 | + |
| 40 | + return makeRequestAsync(client, String.format("/repos/%s/%s", organization, repoName)) |
| 41 | + .map(json -> { |
| 42 | + try { |
| 43 | + return GitHubModel.objectMapper.readValue(json, GitHubModel.Repository.class); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new IllegalStateException("Failed to deserialize GitHubRepository", e); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @DefineKernelFunction(name = "get_issues", description = "Get issues from GitHub", returnType = "java.util.List") |
| 51 | + public Mono<List<GitHubModel.Issue>> getIssuesAsync( |
| 52 | + @KernelFunctionParameter(name = "organization", description = "The name of the organization to retrieve issues for") String organization, |
| 53 | + @KernelFunctionParameter(name = "repo_name", description = "The name of the repository to retrieve issues for") String repoName, |
| 54 | + @KernelFunctionParameter(name = "max_results", description = "The maximum number of issues to retrieve", required = false, defaultValue = "10", type = int.class) int maxResults, |
| 55 | + @KernelFunctionParameter(name = "state", description = "The state of the issues to retrieve", required = false, defaultValue = "open") String state, |
| 56 | + @KernelFunctionParameter(name = "assignee", description = "The assignee of the issues to retrieve", required = false) String assignee) { |
| 57 | + HttpClient client = createClient(); |
| 58 | + |
| 59 | + String query = String.format("/repos/%s/%s/issues", organization, repoName); |
| 60 | + query = buildQueryString(query, "state", state); |
| 61 | + query = buildQueryString(query, "assignee", assignee); |
| 62 | + query = buildQueryString(query, "per_page", String.valueOf(maxResults)); |
| 63 | + |
| 64 | + return makeRequestAsync(client, query) |
| 65 | + .flatMap(json -> { |
| 66 | + try { |
| 67 | + GitHubModel.Issue[] issues = GitHubModel.objectMapper.readValue(json, |
| 68 | + GitHubModel.Issue[].class); |
| 69 | + return Mono.just(List.of(issues)); |
| 70 | + } catch (IOException e) { |
| 71 | + throw new IllegalStateException("Failed to deserialize GitHubIssues", e); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @DefineKernelFunction(name = "get_issue_detail_info", description = "Get detail information of a single issue from GitHub", returnType = "com.microsoft.semantickernel.samples.plugins.github.GitHubModel$IssueDetail") |
| 77 | + public GitHubModel.IssueDetail getIssueDetailAsync( |
| 78 | + @KernelFunctionParameter(name = "organization", description = "The name of the repository to retrieve information for") String organization, |
| 79 | + @KernelFunctionParameter(name = "repo_name", description = "The name of the repository to retrieve information for") String repoName, |
| 80 | + @KernelFunctionParameter(name = "issue_number", description = "The issue number to retrieve information for", type = int.class) int issueNumber) { |
| 81 | + HttpClient client = createClient(); |
| 82 | + |
| 83 | + return makeRequestAsync(client, |
| 84 | + String.format("/repos/%s/%s/issues/%d", organization, repoName, issueNumber)) |
| 85 | + .map(json -> { |
| 86 | + try { |
| 87 | + return GitHubModel.objectMapper.readValue(json, GitHubModel.IssueDetail.class); |
| 88 | + } catch (IOException e) { |
| 89 | + throw new IllegalStateException("Failed to deserialize GitHubIssue", e); |
| 90 | + } |
| 91 | + }).block(); |
| 92 | + } |
| 93 | + |
| 94 | + private HttpClient createClient() { |
| 95 | + return HttpClient.create() |
| 96 | + .baseUrl(baseUrl) |
| 97 | + .headers(headers -> { |
| 98 | + headers.add("User-Agent", "request"); |
| 99 | + headers.add("Accept", "application/vnd.github+json"); |
| 100 | + headers.add("Authorization", "Bearer " + token); |
| 101 | + headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private static String buildQueryString(String path, String param, String value) { |
| 106 | + if (value == null || value.isEmpty() |
| 107 | + || value.equals(KernelFunctionParameter.NO_DEFAULT_VALUE)) { |
| 108 | + return path; |
| 109 | + } |
| 110 | + |
| 111 | + return path + (path.contains("?") ? "&" : "?") + param + "=" + value; |
| 112 | + } |
| 113 | + |
| 114 | + private Mono<String> makeRequestAsync(HttpClient client, String path) { |
| 115 | + return client |
| 116 | + .get() |
| 117 | + .uri(path) |
| 118 | + .responseSingle((res, content) -> { |
| 119 | + if (res.status().code() != 200) { |
| 120 | + return Mono.error(new IllegalStateException("Request failed: " + res.status())); |
| 121 | + } |
| 122 | + return content.asString(); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
0 commit comments