Repository navigation
Expand file tree
/
Copy pathpr.ts
More file actions
412 lines (374 loc) · 14 KB
/
Copy pathpr.ts
File metadata and controls
412 lines (374 loc) · 14 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { isRecordLike } from '@sim/utils/object'
import {
nullableBoolean,
nullableString,
optionalString,
readGitHubErrorMessage,
requiredBoolean,
requiredNumber,
requiredString,
} from '@/tools/github/response-parsers'
import type {
GitHubPullRequestBranch,
GitHubPullRequestFile,
GitHubPullRequestUser,
GitHubPullRequestV2Output,
PROperationParams,
PRV2OperationParams,
PullRequestResponse,
PullRequestV2Response,
} from '@/tools/github/types'
import { PR_BRANCH_REF_OUTPUT, PR_FILE_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types'
import type { ToolConfig } from '@/tools/types'
type GitHubPullRequest = Omit<GitHubPullRequestV2Output, 'files'>
type PullRequestFilesResult =
| { success: true; files: GitHubPullRequestFile[] }
| { success: false; error: string }
const PULL_REQUEST_FILES_PER_PAGE = 100
const MAX_PULL_REQUEST_FILES = 3_000
function parsePullRequestUser(value: unknown, context: string): GitHubPullRequestUser {
if (!isRecordLike(value)) throw new Error(`${context} must be an object`)
return {
login: requiredString(value, 'login', context),
id: requiredNumber(value, 'id', context),
avatar_url: requiredString(value, 'avatar_url', context),
html_url: requiredString(value, 'html_url', context),
type: requiredString(value, 'type', context),
}
}
function parseNullablePullRequestUser(
value: unknown,
context: string
): GitHubPullRequestUser | null {
if (value === null) return null
return parsePullRequestUser(value, context)
}
/**
* A branch's repository is absent on some payloads and explicitly null when the
* repository is gone (a deleted fork), so both read as "unknown" rather than as
* a malformed response.
*/
function parseBranchRepoFullName(repo: unknown, context: string): string | null {
if (repo === null || repo === undefined) return null
if (!isRecordLike(repo)) throw new Error(`${context} must be an object or null`)
return requiredString(repo, 'full_name', context)
}
function parsePullRequestBranch(value: unknown, context: string): GitHubPullRequestBranch {
if (!isRecordLike(value)) throw new Error(`${context} must be an object`)
return {
label: requiredString(value, 'label', context),
ref: requiredString(value, 'ref', context),
sha: requiredString(value, 'sha', context),
repo_full_name: parseBranchRepoFullName(value.repo, `${context}.repo`),
}
}
function parsePullRequest(value: unknown): GitHubPullRequest {
if (!isRecordLike(value)) throw new Error('GitHub pull request response must be an object')
return {
id: requiredNumber(value, 'id', 'pull_request'),
number: requiredNumber(value, 'number', 'pull_request'),
title: requiredString(value, 'title', 'pull_request'),
state: requiredString(value, 'state', 'pull_request'),
html_url: requiredString(value, 'html_url', 'pull_request'),
diff_url: requiredString(value, 'diff_url', 'pull_request'),
body: nullableString(value, 'body', 'pull_request'),
user: parsePullRequestUser(value.user, 'pull_request.user'),
head: parsePullRequestBranch(value.head, 'pull_request.head'),
base: parsePullRequestBranch(value.base, 'pull_request.base'),
merged: requiredBoolean(value, 'merged', 'pull_request'),
mergeable: nullableBoolean(value, 'mergeable', 'pull_request'),
merged_by: parseNullablePullRequestUser(value.merged_by, 'pull_request.merged_by'),
comments: requiredNumber(value, 'comments', 'pull_request'),
review_comments: requiredNumber(value, 'review_comments', 'pull_request'),
commits: requiredNumber(value, 'commits', 'pull_request'),
additions: requiredNumber(value, 'additions', 'pull_request'),
deletions: requiredNumber(value, 'deletions', 'pull_request'),
changed_files: requiredNumber(value, 'changed_files', 'pull_request'),
created_at: requiredString(value, 'created_at', 'pull_request'),
updated_at: requiredString(value, 'updated_at', 'pull_request'),
closed_at: nullableString(value, 'closed_at', 'pull_request'),
merged_at: nullableString(value, 'merged_at', 'pull_request'),
}
}
function parsePullRequestFile(value: unknown, index: number): GitHubPullRequestFile {
const context = `pull_request_files[${index}]`
if (!isRecordLike(value)) throw new Error(`${context} must be an object`)
const patch = optionalString(value, 'patch', context)
const previousFilename = optionalString(value, 'previous_filename', context)
return {
sha: requiredString(value, 'sha', context),
filename: requiredString(value, 'filename', context),
status: requiredString(value, 'status', context),
additions: requiredNumber(value, 'additions', context),
deletions: requiredNumber(value, 'deletions', context),
changes: requiredNumber(value, 'changes', context),
blob_url: requiredString(value, 'blob_url', context),
raw_url: requiredString(value, 'raw_url', context),
contents_url: requiredString(value, 'contents_url', context),
...(patch === undefined ? {} : { patch }),
...(previousFilename === undefined ? {} : { previous_filename: previousFilename }),
}
}
function parsePullRequestFiles(value: unknown): GitHubPullRequestFile[] {
if (!Array.isArray(value)) throw new Error('GitHub pull request files response must be an array')
return value.map(parsePullRequestFile)
}
async function parsePullRequestResponse(response: Response): Promise<GitHubPullRequest> {
if (!response.ok) {
throw new Error(
(await readGitHubErrorMessage(response)) ??
`Failed to fetch pull request (HTTP ${response.status})`
)
}
const value: unknown = await response.json()
return parsePullRequest(value)
}
async function fetchPullRequestFiles(
params: PROperationParams,
pullNumber: number
): Promise<PullRequestFilesResult> {
const files: GitHubPullRequestFile[] = []
const maxPages = MAX_PULL_REQUEST_FILES / PULL_REQUEST_FILES_PER_PAGE
for (let page = 1; page <= maxPages; page += 1) {
const response = await fetch(
`https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${pullNumber}/files?per_page=${PULL_REQUEST_FILES_PER_PAGE}&page=${page}`,
{
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
},
}
)
if (!response.ok) {
return {
success: false,
error:
(await readGitHubErrorMessage(response)) ??
`Failed to fetch PR files (HTTP ${response.status})`,
}
}
const value: unknown = await response.json()
const pageFiles = parsePullRequestFiles(value)
if (pageFiles.length > PULL_REQUEST_FILES_PER_PAGE) {
throw new Error(
`GitHub returned more than ${PULL_REQUEST_FILES_PER_PAGE} pull request files in one page`
)
}
files.push(...pageFiles)
if (pageFiles.length < PULL_REQUEST_FILES_PER_PAGE) break
}
return { success: true, files }
}
function requireParams<T extends PROperationParams>(params: T | undefined): T {
if (!params) throw new Error('GitHub PR reader parameters are required')
return params
}
const PR_PARAMS = {
owner: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository owner',
},
repo: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository name',
},
pullNumber: {
type: 'number',
required: true,
visibility: 'user-or-llm',
description: 'Pull request number',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub API token',
},
} satisfies ToolConfig<PROperationParams, PullRequestResponse>['params']
export const prTool: ToolConfig<PROperationParams, PullRequestResponse> = {
id: 'github_pr',
name: 'GitHub PR Reader',
description: 'Fetch PR details including diff and files changed',
version: '1.0.0',
params: PR_PARAMS,
request: {
url: (params) =>
`https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`,
method: 'GET',
headers: (params) => ({
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${params.apiKey}`,
}),
},
transformResponse: async (response, params) => {
const requestParams = requireParams(params)
const pr = await parsePullRequestResponse(response)
const filesResult = await fetchPullRequestFiles(requestParams, pr.number)
if (!filesResult.success) {
return {
success: false,
error: filesResult.error,
output: {
content: '',
metadata: {
number: pr.number,
title: pr.title,
state: pr.state,
html_url: pr.html_url,
diff_url: pr.diff_url,
created_at: pr.created_at,
updated_at: pr.updated_at,
files: [],
},
},
}
}
const content = `PR #${pr.number}: "${pr.title}" (${pr.state}) - Created: ${pr.created_at}, Updated: ${pr.updated_at}
Description: ${pr.body || 'No description'}
Files changed: ${filesResult.files.length}
URL: ${pr.html_url}`
return {
success: true,
output: {
content,
metadata: {
number: pr.number,
title: pr.title,
state: pr.state,
html_url: pr.html_url,
diff_url: pr.diff_url,
created_at: pr.created_at,
updated_at: pr.updated_at,
files: filesResult.files.map((file) => ({
filename: file.filename,
additions: file.additions,
deletions: file.deletions,
changes: file.changes,
patch: file.patch,
blob_url: file.blob_url,
raw_url: file.raw_url,
status: file.status,
})),
},
},
}
},
outputs: {
content: { type: 'string', description: 'Human-readable PR summary' },
metadata: {
type: 'object',
description: 'Detailed PR metadata including file changes',
properties: {
number: { type: 'number', description: 'Pull request number' },
title: { type: 'string', description: 'PR title' },
state: { type: 'string', description: 'PR state (open/closed/merged)' },
html_url: { type: 'string', description: 'GitHub web URL' },
diff_url: { type: 'string', description: 'Raw diff URL' },
created_at: { type: 'string', description: 'Creation timestamp' },
updated_at: { type: 'string', description: 'Last update timestamp' },
files: {
type: 'array',
description: 'Files changed in the PR',
items: {
type: 'object',
properties: {
filename: { type: 'string', description: 'File path' },
additions: { type: 'number', description: 'Lines added' },
deletions: { type: 'number', description: 'Lines deleted' },
changes: { type: 'number', description: 'Total changes' },
patch: { type: 'string', description: 'File diff patch', optional: true },
blob_url: { type: 'string', description: 'GitHub blob URL' },
raw_url: { type: 'string', description: 'Raw file URL' },
status: { type: 'string', description: 'Change type (added/modified/deleted)' },
},
},
},
},
},
},
}
export const prV2Tool: ToolConfig<PRV2OperationParams, PullRequestV2Response> = {
id: 'github_pr_v2',
name: prTool.name,
description: prTool.description,
version: '2.0.0',
params: {
...PR_PARAMS,
includeFiles: {
type: 'boolean',
required: false,
default: true,
visibility: 'user-or-llm',
description: 'Whether to fetch changed-file details from the separate files endpoint',
},
},
request: prTool.request,
transformResponse: async (response: Response, params) => {
const requestParams = requireParams(params)
const pr = await parsePullRequestResponse(response)
if (requestParams.includeFiles !== false) {
const filesResult = await fetchPullRequestFiles(requestParams, pr.number)
if (!filesResult.success) {
return {
success: false,
error: filesResult.error,
output: {
...pr,
},
}
}
return {
success: true,
output: {
...pr,
files: filesResult.files,
},
}
}
return {
success: true,
output: {
...pr,
},
}
},
outputs: {
id: { type: 'number', description: 'Pull request ID' },
number: { type: 'number', description: 'Pull request number' },
title: { type: 'string', description: 'PR title' },
state: { type: 'string', description: 'PR state (open/closed)' },
html_url: { type: 'string', description: 'GitHub web URL' },
diff_url: { type: 'string', description: 'Raw diff URL' },
body: { type: 'string', description: 'PR description', nullable: true },
user: USER_OUTPUT,
head: PR_BRANCH_REF_OUTPUT,
base: PR_BRANCH_REF_OUTPUT,
merged: { type: 'boolean', description: 'Whether PR is merged' },
mergeable: { type: 'boolean', description: 'Whether PR is mergeable', nullable: true },
merged_by: { ...USER_OUTPUT, nullable: true },
comments: { type: 'number', description: 'Number of comments' },
review_comments: { type: 'number', description: 'Number of review comments' },
commits: { type: 'number', description: 'Number of commits' },
additions: { type: 'number', description: 'Lines added' },
deletions: { type: 'number', description: 'Lines deleted' },
changed_files: { type: 'number', description: 'Number of changed files' },
created_at: { type: 'string', description: 'Creation timestamp' },
updated_at: { type: 'string', description: 'Last update timestamp' },
closed_at: { type: 'string', description: 'Close timestamp', nullable: true },
merged_at: { type: 'string', description: 'Merge timestamp', nullable: true },
files: {
type: 'array',
description: 'Array of changed file objects',
optional: true,
items: {
type: 'object',
properties: PR_FILE_OUTPUT_PROPERTIES,
},
},
},
}