Skip to content

armgit / chops

Public Imported from GitHub
0

Your AI agent skills, finally organized. A macOS app to browse, edit, and manage skills across Claude Code, Cursor, Codex, Windsurf, and Amp.

fix: Avoid GitHub API rate limits in Browse Registry (#80)

Josh Pigfordb94e725a95e7
diff --git a/Chops/Services/SkillRegistry.swift b/Chops/Services/SkillRegistry.swift
index eee001c..bcedf88 100644
--- a/Chops/Services/SkillRegistry.swift
+++ b/Chops/Services/SkillRegistry.swift
@@ -5,8 +5,8 @@ final class SkillRegistry {
var isSearching = false
var searchError: String?
- // Cache repo trees and default branches to avoid repeated API calls
- private var treeCache: [String: [String]] = [:] // source -> [SKILL.md paths]
+ // Cache repo metadata to avoid repeated GitHub API calls
+ private var treeCache: [String: [String]] = [:] // source@branch -> [SKILL.md paths]
private var branchCache: [String: String] = [:] // source -> default branch
// MARK: - Search
@@ -52,9 +52,45 @@ final class SkillRegistry {
func fetchContent(skill: RegistrySkill) async throws -> String {
let branch = try await getDefaultBranch(source: skill.source)
+
+ if let content = try await fetchContentAtConventionalPaths(skill: skill, branch: branch) {
+ return content
+ }
+
+ return try await fetchContentViaTreeAPI(skill: skill, branch: branch)
+ }
+
+ private func fetchContentAtConventionalPaths(skill: RegistrySkill, branch: String) async throws -> String? {
+ let pathPatterns = [
+ "skills/\(skill.skillId)/SKILL.md",
+ "skills/.curated/\(skill.skillId)/SKILL.md",
+ "skills/.experimental/\(skill.skillId)/SKILL.md",
+ "\(skill.skillId)/SKILL.md",
+ "SKILL.md",
+ ]
+
+ for path in pathPatterns {
+ let rawURL = URL(string: "https://raw.githubusercontent.com/\(skill.source)/\(branch)/\(path)")!
+ guard let (data, response) = try? await URLSession.shared.data(from: rawURL),
+ let http = response as? HTTPURLResponse, http.statusCode == 200,
+ let content = String(data: data, encoding: .utf8) else {
+ continue
+ }
+
+ if path == "SKILL.md" {
+ let name = parseFrontmatterName(from: content)
+ if name != skill.skillId && name != skill.name { continue }
+ }
+
+ return content
+ }
+
+ return nil
+ }
+
+ private func fetchContentViaTreeAPI(skill: RegistrySkill, branch: String) async throws -> String {
let paths = try await getSkillPaths(source: skill.source, branch: branch)
- // Try each SKILL.md path until we find one whose frontmatter name matches
for path in paths {
let rawURL = URL(string: "https://raw.githubusercontent.com/\(skill.source)/\(branch)/\(path)")!
guard let (data, response) = try? await URLSession.shared.data(from: rawURL),
@@ -63,7 +99,6 @@ final class SkillRegistry {
continue
}
- // Check if this SKILL.md's frontmatter name matches the skillId
let frontmatterName = parseFrontmatterName(from: content)
if frontmatterName == skill.skillId || frontmatterName == skill.name {
return content
@@ -80,9 +115,14 @@ final class SkillRegistry {
let url = URL(string: "https://api.github.com/repos/\(source)")!
let (data, response) = try await URLSession.shared.data(from: url)
- guard let http = response as? HTTPURLResponse, http.statusCode == 200 else {
- // Fall back to "main" if we can't determine default branch
- return "main"
+ guard let http = response as? HTTPURLResponse else {
+ throw RegistryError.treeFetchFailed
+ }
+ if http.statusCode == 403 {
+ throw RegistryError.rateLimited
+ }
+ guard http.statusCode == 200 else {
+ throw RegistryError.treeFetchFailed
}
struct RepoResponse: Codable {
@@ -95,7 +135,8 @@ final class SkillRegistry {
}
private func getSkillPaths(source: String, branch: String) async throws -> [String] {
- if let cached = treeCache[source] {
+ let cacheKey = "\(source)@\(branch)"
+ if let cached = treeCache[cacheKey] {
return cached
}
@@ -121,10 +162,10 @@ final class SkillRegistry {
let tree = try JSONDecoder().decode(TreeResponse.self, from: data)
let skillPaths = tree.tree
- .filter { $0.type == "blob" && $0.path.hasSuffix("/SKILL.md") }
+ .filter { $0.type == "blob" && ($0.path == "SKILL.md" || $0.path.hasSuffix("/SKILL.md")) }
.map(\.path)
- treeCache[source] = skillPaths
+ treeCache[cacheKey] = skillPaths
return skillPaths
}
diff --git a/Chops/Views/Shared/RegistrySheet.swift b/Chops/Views/Shared/RegistrySheet.swift
index f2ccc31..1f08f8d 100644
--- a/Chops/Views/Shared/RegistrySheet.swift
+++ b/Chops/Views/Shared/RegistrySheet.swift
@@ -101,14 +101,14 @@ struct RegistrySheet: View {
Divider()
// Results
- if results.isEmpty && !isSearching && searchText.isEmpty {
+ if results.isEmpty && (isSearching || searchText.count < 2) {
ContentUnavailableView {
Label("Search the Skills Registry", systemImage: "globe")
} description: {
Text("Find and install skills from the open agent skills ecosystem.")
}
.frame(maxHeight: .infinity)
- } else if results.isEmpty && !isSearching && !searchText.isEmpty {
+ } else if results.isEmpty && !isSearching && searchText.count >= 2 {
ContentUnavailableView.search(text: searchText)
.frame(maxHeight: .infinity)
} else {
@@ -286,15 +286,16 @@ struct RegistrySheet: View {
guard query.count >= 2 else {
results = []
+ isSearching = false
return
}
+ isSearching = true
+
searchTask = Task {
try? await Task.sleep(for: .milliseconds(300))
guard !Task.isCancelled else { return }
- await MainActor.run { isSearching = true }
-
do {
let skills = try await registry.search(query: query)
guard !Task.isCancelled else { return }
diff --git a/Chops/Views/Sidebar/SkillListView.swift b/Chops/Views/Sidebar/SkillListView.swift
index 9b97ebe..f47850e 100644
--- a/Chops/Views/Sidebar/SkillListView.swift
+++ b/Chops/Views/Sidebar/SkillListView.swift
@@ -230,7 +230,7 @@ struct SkillListView: View {
Label("Browse Registry", systemImage: "globe")
}
} label: {
- Image(systemName: "square.and.pencil")
+ Image(systemName: "plus")
}
.menuIndicator(.hidden)
}