Skip to content

Commit

Permalink
add doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tanq16 committed Dec 13, 2024
1 parent b3b2b23 commit d80e858
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions aicontext/ignores.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"strings"
)

// IgnorePatterns holds the default and custom ignore patterns
type IgnorePatterns struct {
defaultPatterns []string
customPatterns []string
}

// newIgnorePatterns creates a new IgnorePatterns instance
func newIgnorePatterns(additionalPatterns []string) *IgnorePatterns {
customPatterns := make([]string, len(additionalPatterns))
for i, pattern := range additionalPatterns {
Expand All @@ -21,6 +23,7 @@ func newIgnorePatterns(additionalPatterns []string) *IgnorePatterns {
}
}

// shouldIgnore checks if the path should be ignored
func (ip *IgnorePatterns) shouldIgnore(path string) bool {
// Check default patterns
for _, pattern := range ip.defaultPatterns {
Expand All @@ -41,8 +44,8 @@ func (ip *IgnorePatterns) shouldIgnore(path string) bool {
return false
}

// Check if the content is binary
// funny heuristic, but it works against a very limited sample set
// isBinary checks if the content is binary
func isBinary(content []byte) bool {
nullCount := 0
nonPrintable := 0
Expand All @@ -57,7 +60,7 @@ func isBinary(content []byte) bool {
return nullCount > 0 || float64(nonPrintable)/float64(checkSize) > 0.3
}

// Default ignored patterns
// defaultIgnores holds the default ignore patterns
var defaultIgnores = []string{
".git",
".gitignore",
Expand Down
12 changes: 12 additions & 0 deletions aicontext/source-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
log "github.com/rs/zerolog/log"
)

// FileEntry holds the file path, content, and language
type FileEntry struct {
Path string
Content string
Language string
}

// Output holds the output data
type Output struct {
GenerationDate string
FileCount int
Expand All @@ -26,11 +28,13 @@ type Output struct {
Files []FileEntry
}

// ProcessorConfig holds the configuration for the processor
type ProcessorConfig struct {
OutputPath string
AdditionalIgnores []string
}

// Processor processes the source code context
type Processor struct {
config ProcessorConfig
ignorePatterns *IgnorePatterns
Expand Down Expand Up @@ -64,13 +68,15 @@ Generated on: {{.GenerationDate}}
{{end}}`

// NewProcessor creates a new Processor instance
func NewProcessor(config ProcessorConfig) *Processor {
return &Processor{
config: config,
ignorePatterns: newIgnorePatterns(config.AdditionalIgnores),
}
}

// ProcessDirectory processes the directory
func (p *Processor) ProcessDirectory(path string) error {
output, err := p.processDirectory(path)
if err != nil {
Expand All @@ -79,6 +85,7 @@ func (p *Processor) ProcessDirectory(path string) error {
return p.writeOutput(output)
}

// ProcessGitHubURL processes the GitHub URL
func (p *Processor) ProcessGitHubURL(url string) error {
tempDir, err := os.MkdirTemp("", "aicontext-clone-")
if err != nil {
Expand All @@ -97,6 +104,7 @@ func (p *Processor) ProcessGitHubURL(url string) error {
return p.ProcessDirectory(tempDir)
}

// processGitRepository processes the Git repository
func (p *Processor) processDirectory(root string) (*Output, error) {
output := &Output{
GenerationDate: time.Now().Format(time.RFC3339),
Expand Down Expand Up @@ -146,6 +154,7 @@ func (p *Processor) processDirectory(root string) (*Output, error) {
return output, nil
}

// writeOutput writes the output to the file
func (p *Processor) writeOutput(output *Output) error {
tmpl, err := template.New("markdown").Parse(markdownTemplate)
if err != nil {
Expand All @@ -163,6 +172,7 @@ func (p *Processor) writeOutput(output *Output) error {
return nil
}

// detectLanguage detects the language based on the file extension
func detectLanguage(path string) string {
ext := strings.ToLower(filepath.Ext(path))
switch ext {
Expand Down Expand Up @@ -211,6 +221,7 @@ func detectLanguage(path string) string {
}
}

// generateDirectoryTree generates the directory tree
func (p *Processor) generateDirectoryTree(root string) string {
var tree strings.Builder
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -245,6 +256,7 @@ func (p *Processor) generateDirectoryTree(root string) string {
return tree.String()
}

// helper function to find the minimum of two integers
func min(a, b int) int {
if a < b {
return a
Expand Down
6 changes: 6 additions & 0 deletions aicontext/video-context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package aicontext provides functions to extract context from GitHub repositories, local directories, and YouTube videos
package aicontext

import (
Expand All @@ -13,15 +14,18 @@ import (
log "github.com/rs/zerolog/log"
)

// TranscriptParams holds the parameters for the transcript request
type TranscriptParams struct {
Params string `json:"params"`
}

// TranscriptSegment holds the transcript segment data
type TranscriptSegment struct {
StartTime string `json:"startTime"`
Text string `json:"text"`
}

// InnerTubeRequest holds the request data for the InnerTube API
type InnerTubeRequest struct {
Context struct {
Client struct {
Expand Down Expand Up @@ -175,6 +179,7 @@ func DownloadTranscript(videoURL string) ([]TranscriptSegment, error) {
return segments, nil
}

// makeInnerTubeRequest makes a request to the InnerTube API
func makeInnerTubeRequest(endpoint string, reqBody interface{}, apiKey string) (map[string]interface{}, error) {
baseURL := "https://www.youtube.com/youtubei/v1"
jsonBody, err := json.Marshal(reqBody)
Expand All @@ -201,6 +206,7 @@ func makeInnerTubeRequest(endpoint string, reqBody interface{}, apiKey string) (
return result, nil
}

// extractInnertubeKey extracts the INNERTUBE_API_KEY from the video page
func extractInnertubeKey(videoID string) (string, error) {
resp, err := http.Get("https://www.youtube.com/watch?v=" + videoID)
if err != nil {
Expand Down

0 comments on commit d80e858

Please sign in to comment.