-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathget_file_content.ts
More file actions
243 lines (227 loc) · 7.1 KB
/
Copy pathget_file_content.ts
File metadata and controls
243 lines (227 loc) · 7.1 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { getFileExtension, getMimeTypeFromExtension } from '@/lib/uploads/utils/file-utils'
import type { FileContentResponse, GetFileContentParams } from '@/tools/github/types'
import type { ToolConfig } from '@/tools/types'
export const getFileContentTool: ToolConfig<GetFileContentParams, FileContentResponse> = {
id: 'github_get_file_content',
name: 'GitHub Get File Content',
description:
'Get the content of a file from a GitHub repository. Supports files up to 1MB. Content is returned decoded and human-readable.',
version: '1.0.0',
params: {
owner: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository owner (user or organization)',
},
repo: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository name',
},
path: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Path to the file in the repository (e.g., "src/index.ts")',
},
ref: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Branch name, tag, or commit SHA (defaults to repository default branch)',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub Personal Access Token',
},
},
request: {
url: (params) => {
const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${params.path}`
return params.ref ? `${baseUrl}?ref=${params.ref}` : baseUrl
},
method: 'GET',
headers: (params) => ({
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
}),
},
transformResponse: async (response) => {
const data = await response.json()
if (Array.isArray(data)) {
return {
success: false,
error: 'Path points to a directory. Use github_get_tree to list directory contents.',
output: {
content: '',
metadata: {
name: '',
path: '',
sha: '',
size: 0,
type: 'dir',
download_url: '',
html_url: '',
},
},
}
}
let decodedContent = ''
let file:
| {
name: string
mimeType: string
data: string
size: number
}
| undefined
if (data.content) {
try {
decodedContent = Buffer.from(data.content, 'base64').toString('utf-8')
} catch (error) {
decodedContent = '[Binary file - content cannot be displayed as text]'
}
}
if (data.content && data.encoding === 'base64' && data.name) {
const base64Data = String(data.content).replace(/\n/g, '')
const extension = getFileExtension(data.name)
const mimeType = getMimeTypeFromExtension(extension)
file = {
name: data.name,
mimeType,
data: base64Data,
size: data.size || 0,
}
}
const contentPreview =
decodedContent.length > 500
? `${decodedContent.substring(0, 500)}...\n\n[Content truncated. Full content available in metadata]`
: decodedContent
const content = `File: ${data.name}
Path: ${data.path}
Size: ${data.size} bytes
Type: ${data.type}
SHA: ${data.sha}
Content Preview:
${contentPreview}`
return {
success: true,
output: {
content,
file,
metadata: {
name: data.name,
path: data.path,
sha: data.sha,
size: data.size,
type: data.type,
download_url: data.download_url,
html_url: data.html_url,
},
},
}
},
outputs: {
content: {
type: 'string',
description: 'Human-readable file information with content preview',
},
file: {
type: 'file',
description: 'Downloaded file stored in execution files',
optional: true,
},
metadata: {
type: 'object',
description: 'File metadata including name, path, SHA, size, and URLs',
properties: {
name: { type: 'string', description: 'File name' },
path: { type: 'string', description: 'Full path in repository' },
sha: { type: 'string', description: 'Git blob SHA' },
size: { type: 'number', description: 'File size in bytes' },
type: { type: 'string', description: 'Content type (file or dir)' },
download_url: { type: 'string', description: 'Direct download URL', optional: true },
html_url: { type: 'string', description: 'GitHub web UI URL', optional: true },
},
},
},
}
export const getFileContentV2Tool: ToolConfig<GetFileContentParams, any> = {
id: 'github_get_file_content_v2',
name: getFileContentTool.name,
description: getFileContentTool.description,
version: '2.0.0',
params: getFileContentTool.params,
request: getFileContentTool.request,
transformResponse: async (response: Response) => {
const data = await response.json()
// Decode base64 content if present
let decodedContent = ''
let file:
| {
name: string
mimeType: string
data: string
size: number
}
| undefined
if (data.content && data.encoding === 'base64') {
try {
decodedContent = Buffer.from(data.content, 'base64').toString('utf-8')
} catch {
decodedContent = data.content
}
}
if (data.content && data.encoding === 'base64' && data.name) {
const base64Data = String(data.content).replace(/\n/g, '')
const extension = getFileExtension(data.name)
const mimeType = getMimeTypeFromExtension(extension)
file = {
name: data.name,
mimeType,
data: base64Data,
size: data.size || 0,
}
}
return {
success: true,
output: {
name: data.name,
path: data.path,
sha: data.sha,
size: data.size,
type: data.type,
content: (decodedContent || data.content) ?? null,
encoding: data.encoding,
html_url: data.html_url,
download_url: data.download_url ?? null,
git_url: data.git_url,
_links: data._links,
file,
},
}
},
outputs: {
name: { type: 'string', description: 'File name' },
path: { type: 'string', description: 'Full path in repository' },
sha: { type: 'string', description: 'Git blob SHA' },
size: { type: 'number', description: 'File size in bytes' },
type: { type: 'string', description: 'Content type (file/dir/symlink/submodule)' },
content: { type: 'string', description: 'Decoded file content', optional: true },
encoding: { type: 'string', description: 'Content encoding' },
html_url: { type: 'string', description: 'GitHub web URL' },
download_url: { type: 'string', description: 'Direct download URL', optional: true },
git_url: { type: 'string', description: 'Git blob API URL' },
_links: { type: 'json', description: 'Related links' },
file: {
type: 'file',
description: 'Downloaded file stored in execution files',
optional: true,
},
},
}