-
Notifications
You must be signed in to change notification settings - Fork 46
Download agent file from GitHub on demand with version-aware URL patching #13612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
83bc461
1d1f613
cb8822e
233a119
7c83c1d
98c479d
24b002a
6d583a8
a040410
64c51b5
9095c45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/github/gh-aw/pkg/console" | ||
| "github.com/github/gh-aw/pkg/constants" | ||
|
|
@@ -28,39 +30,91 @@ func init() { | |
| workflow.SetDefaultVersion(version) | ||
| } | ||
|
|
||
| //go:embed templates/github-agentic-workflows.md | ||
| var copilotInstructionsTemplate string | ||
| // SetVersionInfo sets the version information for the CLI and workflow package | ||
| func SetVersionInfo(v string) { | ||
| version = v | ||
| workflow.SetDefaultVersion(v) // Keep workflow package in sync | ||
| } | ||
|
|
||
| //go:embed templates/agentic-workflows.agent.md | ||
| var agenticWorkflowsDispatcherTemplate string | ||
| // GetVersion returns the current version | ||
| func GetVersion() string { | ||
| return version | ||
| } | ||
|
|
||
| //go:embed templates/create-agentic-workflow.md | ||
| var createWorkflowPromptTemplate string | ||
| // downloadAgentFileFromGitHub downloads the agentic-workflows.agent.md file from GitHub | ||
| func downloadAgentFileFromGitHub(verbose bool) (string, error) { | ||
| commandsLog.Print("Downloading agentic-workflows.agent.md from GitHub") | ||
|
|
||
| // Determine the ref to use (tag for releases, main for dev builds) | ||
| ref := "main" | ||
| currentVersion := GetVersion() | ||
|
|
||
| // If version looks like a release tag (starts with v and contains dots), use it | ||
| isRelease := strings.HasPrefix(currentVersion, "v") && strings.Contains(currentVersion, ".") | ||
| if isRelease { | ||
| ref = currentVersion | ||
| commandsLog.Printf("Using release tag: %s", ref) | ||
| if verbose { | ||
| fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Using release version: %s", ref))) | ||
| } | ||
| } else { | ||
| commandsLog.Print("Using main branch for dev build") | ||
| if verbose { | ||
| fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Using main branch (dev build)")) | ||
| } | ||
| } | ||
|
|
||
| //go:embed templates/update-agentic-workflow.md | ||
| var updateWorkflowPromptTemplate string | ||
| // Construct the raw GitHub URL | ||
| url := fmt.Sprintf("https://raw.githubusercontent.com/github/gh-aw/%s/.github/agents/agentic-workflows.agent.md", ref) | ||
| commandsLog.Printf("Downloading from URL: %s", url) | ||
|
|
||
| //go:embed templates/create-shared-agentic-workflow.md | ||
| var createSharedAgenticWorkflowPromptTemplate string | ||
| // Create HTTP client with timeout | ||
| client := &http.Client{ | ||
| Timeout: 30 * time.Second, | ||
| } | ||
|
|
||
| //go:embed templates/debug-agentic-workflow.md | ||
| var debugWorkflowPromptTemplate string | ||
| // Download the file | ||
| resp, err := client.Get(url) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to download agent file: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| //go:embed templates/upgrade-agentic-workflows.md | ||
| var upgradeAgenticWorkflowsPromptTemplate string | ||
| if resp.StatusCode != http.StatusOK { | ||
| return "", fmt.Errorf("failed to download agent file: HTTP %d", resp.StatusCode) | ||
| } | ||
|
|
||
| //go:embed templates/serena-tool.md | ||
| var serenaToolTemplate string | ||
| // Read the content | ||
| content, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read agent file content: %w", err) | ||
| } | ||
|
|
||
| // SetVersionInfo sets the version information for the CLI and workflow package | ||
| func SetVersionInfo(v string) { | ||
| version = v | ||
| workflow.SetDefaultVersion(v) // Keep workflow package in sync | ||
| contentStr := string(content) | ||
|
|
||
| // Patch URLs to match the current version/ref | ||
| patchedContent := patchAgentFileURLs(contentStr, ref) | ||
| if patchedContent != contentStr && verbose { | ||
| fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Patched URLs to use ref: %s", ref))) | ||
| } | ||
|
|
||
| commandsLog.Printf("Successfully downloaded agent file (%d bytes)", len(patchedContent)) | ||
| return patchedContent, nil | ||
| } | ||
|
Comment on lines
+45
to
103
|
||
|
|
||
| // GetVersion returns the current version | ||
| func GetVersion() string { | ||
| return version | ||
| // patchAgentFileURLs patches URLs in the agent file to use the correct ref | ||
| func patchAgentFileURLs(content, ref string) string { | ||
| // Pattern 1: Convert local paths to GitHub URLs | ||
| // `.github/aw/file.md` -> `https://github.com/github/gh-aw/blob/{ref}/.github/aw/file.md` | ||
| content = strings.ReplaceAll(content, "`.github/aw/", fmt.Sprintf("`https://github.com/github/gh-aw/blob/%s/.github/aw/", ref)) | ||
|
|
||
| // Pattern 2: Update existing GitHub URLs to use the correct ref | ||
| // https://github.com/github/gh-aw/blob/main/ -> https://github.com/github/gh-aw/blob/{ref}/ | ||
| if ref != "main" { | ||
| content = strings.ReplaceAll(content, "/blob/main/", fmt.Sprintf("/blob/%s/", ref)) | ||
| } | ||
|
|
||
| return content | ||
| } | ||
|
Comment on lines
+106
to
118
|
||
|
|
||
| func isGHCLIAvailable() bool { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version detection logic assumes that release versions always start with
vand contain dots (e.g.,v1.2.3). However, this could fail for valid version formats such as:v1(single number version)v1.2.3-rc1(pre-release with suffix)1.2.3(withoutvprefix)The current logic would incorrectly treat
v1as a dev build (missing dot) and usemainbranch instead of thev1tag.Consider using a more robust version detection approach:
vfollowed by at least one digitExample improvement:
This is a minor edge case that may not affect most users, but it could cause confusion if uncommon versioning schemes are used.