mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-09 11:48:47 +00:00
Introduces a new AI Bot persona called 'GitHub Helper' which is specialized in assisting with GitHub-related tasks and questions. It includes the following key changes: - Implements the GitHub Helper persona class with its system prompt and available tools - Adds three new AI Bot tools for GitHub interactions: - github_file_content: Retrieves content of files from a GitHub repository - github_pull_request_diff: Retrieves the diff for a GitHub pull request - github_search_code: Searches for code in a GitHub repository - Updates the AI Bot dialects to support the new GitHub tools - Implements multiple function calls for standard tool dialect
73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module AiBot
|
|
module Tools
|
|
class GithubSearchCode < Tool
|
|
def self.signature
|
|
{
|
|
name: name,
|
|
description: "Searches for code in a GitHub repository",
|
|
parameters: [
|
|
{
|
|
name: "repo",
|
|
description: "The repository name in the format 'owner/repo'",
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "query",
|
|
description: "The search query (e.g., a function name, variable, or code snippet)",
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
],
|
|
}
|
|
end
|
|
|
|
def self.name
|
|
"github_search_code"
|
|
end
|
|
|
|
def repo
|
|
parameters[:repo]
|
|
end
|
|
|
|
def query
|
|
parameters[:query]
|
|
end
|
|
|
|
def description_args
|
|
{ repo: repo, query: query }
|
|
end
|
|
|
|
def invoke(_bot_user, llm)
|
|
api_url = "https://api.github.com/search/code?q=#{query}+repo:#{repo}"
|
|
|
|
response =
|
|
send_http_request(
|
|
api_url,
|
|
headers: {
|
|
"Accept" => "application/vnd.github.v3.text-match+json",
|
|
},
|
|
authenticate_github: true,
|
|
)
|
|
|
|
if response.code == "200"
|
|
search_data = JSON.parse(response.body)
|
|
results =
|
|
search_data["items"]
|
|
.map { |item| "#{item["path"]}:\n#{item["text_matches"][0]["fragment"]}" }
|
|
.join("\n---\n")
|
|
|
|
results = truncate(results, max_length: 20_000, percent_length: 0.3, llm: llm)
|
|
{ search_results: results }
|
|
else
|
|
{ error: "Failed to perform code search. Status code: #{response.code}" }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|