-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
109 lines (94 loc) · 2.93 KB
/
Copy pathhandler.js
File metadata and controls
109 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* AnythingLLM GitHub Repository Reader Skill
* Reads content from public GitHub repositories using the GitHub API
*/
module.exports.runtime = {
handler: async function ({ repository, path = "", branch = "main" }) {
try {
// Validate repository format
if (!repository || !repository.includes('/')) {
return {
success: false,
error: "Repository must be in format 'owner/repo'"
};
}
const [owner, repo] = repository.split('/');
// Get GitHub token from setup args if available
const githubToken = this.config.GITHUB_TOKEN || process.env.GITHUB_TOKEN;
// Build headers
const headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'AnythingLLM-GitHub-Skill'
};
if (githubToken) {
headers['Authorization'] = `token ${githubToken}`;
}
// GitHub API endpoint for repository contents
const apiUrl = path
? `https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${branch}`
: `https://api.github.com/repos/${owner}/${repo}/contents?ref=${branch}`;
console.log(`Fetching from GitHub: ${apiUrl}`);
const response = await fetch(apiUrl, { headers });
if (!response.ok) {
if (response.status === 404) {
return {
success: false,
error: `Repository, path, or branch not found: ${repository}/${path} (${branch})`
};
}
if (response.status === 403) {
return {
success: false,
error: "GitHub API rate limit exceeded. Please provide a GitHub token in settings."
};
}
return {
success: false,
error: `GitHub API error: ${response.status} ${response.statusText}`
};
}
const data = await response.json();
// Handle single file
if (data.type === 'file') {
const content = Buffer.from(data.content, 'base64').toString('utf-8');
return {
success: true,
repository,
path: data.path,
type: 'file',
size: data.size,
content: content,
url: data.html_url
};
}
// Handle directory
if (Array.isArray(data)) {
const items = data.map(item => ({
name: item.name,
path: item.path,
type: item.type,
size: item.size,
url: item.html_url
}));
return {
success: true,
repository,
path: path || 'root',
type: 'directory',
items: items,
count: items.length
};
}
return {
success: false,
error: "Unexpected response format from GitHub API"
};
} catch (error) {
console.error("GitHub Skill Error:", error);
return {
success: false,
error: error.message || "Unknown error occurred"
};
}
}
};