-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathsearch_code.ts
More file actions
270 lines (257 loc) · 8.61 KB
/
search_code.ts
File metadata and controls
270 lines (257 loc) · 8.61 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import type { ToolConfig } from '@/tools/types'
interface SearchCodeParams {
q: string
sort?: 'indexed'
order?: 'asc' | 'desc'
per_page?: number
page?: number
apiKey: string
}
interface SearchCodeResponse {
success: boolean
output: {
content: string
metadata: {
total_count: number
incomplete_results: boolean
items: Array<{
name: string
path: string
sha: string
html_url: string
repository: {
full_name: string
html_url: string
}
}>
}
}
}
export const searchCodeTool: ToolConfig<SearchCodeParams, SearchCodeResponse> = {
id: 'github_search_code',
name: 'GitHub Search Code',
description:
'Search for code across GitHub repositories. Use qualifiers like repo:owner/name, language:js, path:src, extension:py',
version: '1.0.0',
params: {
q: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description:
'Search query with optional qualifiers (repo:, language:, path:, extension:, user:, org:)',
},
sort: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Sort by indexed date (default: best match)',
},
order: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Sort order: asc or desc (default: desc)',
},
per_page: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Results per page (max 100, default: 30)',
default: 30,
},
page: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Page number (default: 1)',
default: 1,
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub API token',
},
},
request: {
url: (params) => {
const url = new URL('https://api.github.com/search/code')
url.searchParams.append('q', params.q)
if (params.sort) url.searchParams.append('sort', params.sort)
if (params.order) url.searchParams.append('order', params.order)
if (params.per_page) url.searchParams.append('per_page', String(params.per_page))
if (params.page) url.searchParams.append('page', String(params.page))
return url.toString()
},
method: 'GET',
headers: (params) => ({
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
}),
},
transformResponse: async (response) => {
const data = await response.json()
const items = data.items.map((item: any) => ({
name: item.name,
path: item.path,
sha: item.sha,
html_url: item.html_url,
repository: {
full_name: item.repository.full_name,
html_url: item.repository.html_url,
},
}))
const content = `Found ${data.total_count} code result(s)${data.incomplete_results ? ' (incomplete)' : ''}:
${items
.map(
(item: any) =>
`- ${item.repository.full_name}/${item.path}
${item.html_url}`
)
.join('\n')}`
return {
success: true,
output: {
content,
metadata: {
total_count: data.total_count,
incomplete_results: data.incomplete_results,
items,
},
},
}
},
outputs: {
content: { type: 'string', description: 'Human-readable search results' },
metadata: {
type: 'object',
description: 'Search results metadata',
properties: {
total_count: { type: 'number', description: 'Total matching results' },
incomplete_results: { type: 'boolean', description: 'Whether results are incomplete' },
items: {
type: 'array',
description: 'Array of code matches',
items: {
type: 'object',
properties: {
name: { type: 'string', description: 'File name' },
path: { type: 'string', description: 'File path' },
sha: { type: 'string', description: 'Blob SHA' },
html_url: { type: 'string', description: 'GitHub web URL' },
repository: {
type: 'object',
description: 'Repository info',
properties: {
full_name: { type: 'string', description: 'Repository full name' },
html_url: { type: 'string', description: 'Repository URL' },
},
},
},
},
},
},
},
},
}
export const searchCodeV2Tool: ToolConfig<SearchCodeParams, any> = {
id: 'github_search_code_v2',
name: searchCodeTool.name,
description: searchCodeTool.description,
version: '2.0.0',
params: searchCodeTool.params,
request: searchCodeTool.request,
transformResponse: async (response: Response) => {
const data = await response.json()
return {
success: true,
output: {
total_count: data.total_count,
incomplete_results: data.incomplete_results,
items: data.items.map((item: any) => ({
...item,
text_matches: item.text_matches ?? [],
})),
},
}
},
outputs: {
total_count: { type: 'number', description: 'Total matching results' },
incomplete_results: { type: 'boolean', description: 'Whether results are incomplete' },
items: {
type: 'array',
description: 'Array of code matches from GitHub API',
items: {
type: 'object',
properties: {
name: { type: 'string', description: 'File name' },
path: { type: 'string', description: 'File path' },
sha: { type: 'string', description: 'Blob SHA' },
url: { type: 'string', description: 'API URL' },
git_url: { type: 'string', description: 'Git blob URL' },
html_url: { type: 'string', description: 'GitHub web URL' },
score: { type: 'number', description: 'Search relevance score' },
repository: {
type: 'object',
description: 'Repository containing the code',
properties: {
id: { type: 'number', description: 'Repository ID' },
node_id: { type: 'string', description: 'GraphQL node ID' },
name: { type: 'string', description: 'Repository name' },
full_name: { type: 'string', description: 'Full name (owner/repo)' },
private: { type: 'boolean', description: 'Whether repository is private' },
html_url: { type: 'string', description: 'GitHub web URL' },
description: {
type: 'string',
description: 'Repository description',
optional: true,
},
fork: { type: 'boolean', description: 'Whether this is a fork' },
url: { type: 'string', description: 'API URL' },
owner: {
type: 'object',
description: 'Repository owner',
properties: {
login: { type: 'string', description: 'Username' },
id: { type: 'number', description: 'User ID' },
node_id: { type: 'string', description: 'GraphQL node ID' },
avatar_url: { type: 'string', description: 'Avatar image URL' },
url: { type: 'string', description: 'API URL' },
html_url: { type: 'string', description: 'Profile page URL' },
type: { type: 'string', description: 'User or Organization' },
site_admin: { type: 'boolean', description: 'GitHub staff indicator' },
},
},
},
},
text_matches: {
type: 'array',
description: 'Text matches showing context',
items: {
type: 'object',
properties: {
object_url: { type: 'string', description: 'Object URL' },
object_type: { type: 'string', description: 'Object type', optional: true },
property: { type: 'string', description: 'Property matched' },
fragment: { type: 'string', description: 'Text fragment with match' },
matches: {
type: 'array',
description: 'Match indices',
items: {
type: 'object',
properties: {
text: { type: 'string', description: 'Matched text' },
indices: { type: 'array', description: 'Start and end indices' },
},
},
},
},
},
},
},
},
},
},
}