Skip to content

Commit 9927e77

Browse files
authored
Merge branch 'main' into fix_cli_config
2 parents fdcf52e + a236b96 commit 9927e77

16 files changed

+724
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class GetContextAction < Sublayer::Actions::Base
2+
def initialize(path:)
3+
@path = path
4+
end
5+
6+
def call
7+
ignored_patterns = load_contextignore
8+
files = get_files(ignored_patterns)
9+
concatenate_files(files)
10+
end
11+
12+
private
13+
14+
def load_contextignore
15+
contextignore_path = File.join(@path, '.contextignore')
16+
return [] unless File.exist?(contextignore_path)
17+
18+
File.readlines(contextignore_path).map(&:strip).reject do |line|
19+
line.empty? || line.start_with?('#')
20+
end
21+
end
22+
23+
def get_files(ignored_patterns)
24+
Dir.chdir(@path) do
25+
all_files = `git ls-files`.split("\n")
26+
all_files.reject do |file|
27+
ignored_patterns.any? do |pattern|
28+
File.fnmatch?(pattern, file) ||
29+
file.start_with?(pattern.chomp('/'))
30+
end
31+
end
32+
end
33+
end
34+
35+
def concatenate_files(files)
36+
files.map do |file|
37+
content = File.read(File.join(@path, file))
38+
"File: #{file}\n#{content}\n\n"
39+
end.join
40+
end
41+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class GithubAddOrModifyFileAction < Sublayer::Actions::Base
2+
def initialize(repo:, branch:, file_path:, file_content:)
3+
@client = Octokit::Client.new(access_token: ENV['ACCESS_TOKEN'])
4+
@repo = repo
5+
@branch = branch
6+
@file_path = file_path
7+
@file_content = file_content
8+
end
9+
10+
def call
11+
content = @client.contents(@repo, path: @file_path, ref: @branch)
12+
@client.update_contents(
13+
@repo,
14+
@file_path,
15+
"Updating #{@file_path}",
16+
content.sha,
17+
@file_content,
18+
branch: @branch
19+
)
20+
end
21+
22+
def call
23+
begin
24+
# Try to fetch the file contents to get its SHA
25+
content = @client.contents(@repo, path: @file_path, ref: @branch)
26+
27+
# If the file exists, update it
28+
@client.update_contents(
29+
@repo,
30+
@file_path,
31+
"Updating #{@file_path}",
32+
content.sha,
33+
@file_content,
34+
branch: @branch
35+
)
36+
puts "File updated: #{@file_path}"
37+
rescue Octokit::NotFound
38+
# If the file does not exist, create it instead
39+
@client.create_contents(
40+
@repo,
41+
@file_path,
42+
"Creating #{@file_path}",
43+
@file_content,
44+
branch: @branch
45+
)
46+
puts "File created: #{@file_path}"
47+
end
48+
end
49+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class GithubCreateBranchAction < Sublayer::Actions::Base
2+
def initialize(repo:, base_branch:, new_branch:)
3+
@client = Octokit::Client.new(access_token: ENV['ACCESS_TOKEN'])
4+
@repo = repo
5+
@base_branch = base_branch
6+
@new_branch = new_branch
7+
end
8+
9+
def call
10+
ref = @client.ref(@repo, "heads/#{@base_branch}")
11+
@client.create_ref(@repo, "refs/heads/#{@new_branch}", ref.object.sha)
12+
end
13+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class GithubCreatePullRequestAction < Sublayer::Actions::Base
2+
def initialize(repo:, base:, head:, title:, body:)
3+
@client = Octokit::Client.new(access_token: ENV["ACCESS_TOKEN"])
4+
@repo = repo
5+
@base = base
6+
@head = head
7+
@title = title
8+
@body = body
9+
end
10+
11+
def call
12+
@client.create_pull_request(@repo, @base, @head, @title, @body)
13+
end
14+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class GithubGetDiffAction < Sublayer::Actions::Base
2+
def initialize(repo:, pr_number:)
3+
@client = Octokit::Client.new(access_token: ENV['ACCESS_TOKEN'])
4+
@repo = repo
5+
@pr_number = pr_number
6+
end
7+
8+
def call
9+
pr_files = @client.pull_request_files(@repo, @pr_number)
10+
pr_files.map do |file|
11+
{
12+
filename: file.filename,
13+
status: file.status,
14+
patch: file.patch
15+
}
16+
end
17+
end
18+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class DocUpdateGenerator < Sublayer::Generators::Base
2+
TEMPLATE_CONTENT = File.read(File.join(__dir__, 'just_the_docs_template.md'))
3+
4+
llm_output_adapter type: :list_of_named_strings,
5+
name: "files_and_contents",
6+
description: "A list of files to update along with their corresponding updated contents",
7+
item_name: "file_update",
8+
attributes: [
9+
{ name: "explanation", description: "Brief explanation for how a change to a specified file makes progress towards the suggested update." },
10+
{ name: "file_path", description: "The path of the file to update" },
11+
{ name: "file_content", description: "The updated content for the file" }
12+
]
13+
14+
def initialize(suggestions:, doc_context:, code_context:, context_ignore_list:)
15+
@suggestions = suggestions
16+
@doc_context = doc_context
17+
@code_context = code_context
18+
@context_ignore_list = context_ignore_list
19+
end
20+
21+
def generate
22+
super
23+
end
24+
25+
def prompt
26+
<<~PROMPT
27+
You are tasked to make changes in the documentation repository based on suggestions.
28+
29+
Use the following information to guide both tasks:
30+
31+
1. Code repository structure:
32+
#{@code_context}
33+
34+
2. Documentation repository structure:
35+
#{@doc_context}
36+
37+
3. Documentation update suggestions:
38+
#{@suggestions}
39+
40+
4. Files excluded from updates (do not modify these files):
41+
#{@context_ignore_list}
42+
43+
5. Example of doc format:
44+
#{TEMPLATE_CONTENT}
45+
46+
Guidelines:
47+
1. Do not make updates to any files excluded from updates
48+
2. Follow the format given in the example as a template for the structure of your file
49+
3. If a new page is added make sure to add them to the navigation as well
50+
3. If a link is added make sure it leads to an existing page, or create the new page being referenced
51+
52+
Your task:
53+
Generate the full updated content for each file in the documentation repository that should be changed according to the suggestions.
54+
PROMPT
55+
end
56+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class DocUpdateNecessityGenerator < Sublayer::Generators::Base
2+
llm_output_adapter type: :named_strings,
3+
name: "doc_update_score",
4+
description: "boolean and confidence score indicating if documentation changes are needed",
5+
attributes: [
6+
{ name: "reasoning", description: "brief explanation for whether or not doc changes are needed" },
7+
{ name: "confidence", description: "number from 0 to 1 indicating confidence in the decision" },
8+
{ name: "needs_update", description: "boolean indicating if updates are needed" }
9+
]
10+
11+
def initialize(doc_context:, code_context:, diff:)
12+
@doc_context = doc_context
13+
@code_context = code_context
14+
@diff = diff
15+
end
16+
17+
def generate
18+
super
19+
end
20+
21+
def prompt
22+
<<~PROMPT
23+
Code diff:
24+
#{@diff}
25+
26+
Code context:
27+
#{@code_context}
28+
29+
Documentation context:
30+
#{@doc_context}
31+
32+
Given the above code diff and context, determine if documentation updates are necessary.
33+
Consider the following factors:
34+
1. The significance of the changes
35+
2. Whether the changes affect public APIs or user-facing features
36+
3. If the changes introduce new concepts or modify existing ones
37+
4. Whether the current documentation accurately reflects the changes
38+
39+
Based on this information, are documentation updates necessary?
40+
PROMPT
41+
end
42+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class DocUpdateSuggestionGenerator < Sublayer::Generators::Base
2+
llm_output_adapter type: :list_of_named_strings,
3+
name: "doc_update_suggestions",
4+
description: "List of doc update suggestions with usefulness scores",
5+
item_name: "suggestion",
6+
attributes: [
7+
{ name: "suggestion", description: "description of the doc update suggestion and its reasoning" },
8+
{ name: "file_changes", description: "description of the files and their respective changes" },
9+
{ name: "usefulness_score", description: "A score from 1-10 indicating the usefulness of the suggestion" }, #unused
10+
{ name: "title", description: "doc update suggestion title" } #unused
11+
]
12+
13+
def initialize(code_context:, doc_context:, context_ignore_list:, diff:)
14+
@code_context = code_context
15+
@doc_context = doc_context
16+
@context_ignore_list = context_ignore_list
17+
@diff = diff
18+
end
19+
20+
def generate
21+
super
22+
end
23+
24+
def prompt
25+
<<~PROMPT
26+
As an expert in documentation with a focus on concise and hierarchical organization. Consider the following:
27+
28+
1. Newest changes:
29+
#{@diff}
30+
31+
1. Documentation repository context:
32+
#{@doc_context}
33+
34+
2. Code repository context:
35+
#{@code_context}
36+
37+
3. Files excluded from updates (do not modify these files):
38+
#{@context_ignore_list}
39+
40+
You are tasked with generating detailed and specific suggestions for updating a documentation repository based on the newest changes to the code repository.
41+
42+
Generate documentation update suggestions, considering:
43+
1. The appropriate level in the documentation hierarchy for each change (high-level concepts vs. specific details)
44+
2. The impact of the changes on existing documentation (updates, additions, or removals)
45+
3. The importance of each change for user understanding and API use
46+
4. The need for examples or clarifications of new or modified functionality
47+
48+
For each suggestion
49+
- Describe the suggestion and the reasoning behind it. Be specific.
50+
- Meticulously describe the files and the changes that should be made in them.
51+
- Indicate its usefulness, 10 being most useful and 1 being least, as a way of prioritizing which suggestion should be done first
52+
- A succinct title that encapsulates the spirit of the suggestion
53+
54+
Guidelines:
55+
1. Do not suggest changes to any files excluded from updates
56+
2. Make the fewest number of suggestions possible to achieve the desired outcome
57+
PROMPT
58+
end
59+
end

0 commit comments

Comments
 (0)