Skip to content

Commit 363d225

Browse files
committed
improvement[github]: add file contents
1 parent cc33b4a commit 363d225

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

sim/app/tools/github/commit.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const latestCommitTool: ToolConfig<LatestCommitParams, LatestCommitRespon
4646
}),
4747
},
4848

49-
transformResponse: async (response) => {
49+
transformResponse: async (response, params) => {
5050
if (!response.ok) {
5151
throw new Error(`GitHub API error: ${response.statusText}`)
5252
}
@@ -56,6 +56,48 @@ export const latestCommitTool: ToolConfig<LatestCommitParams, LatestCommitRespon
5656
// Create a human-readable content string
5757
const content = `Latest commit: "${data.commit.message}" by ${data.commit.author.name} on ${data.commit.author.date}. SHA: ${data.sha}`
5858

59+
// Initialize files array and add file information
60+
const files = data.files || []
61+
const fileDetailsWithContent = []
62+
63+
// Fetch raw content for each file if includeFileContent is true
64+
if (files.length > 0) {
65+
for (const file of files) {
66+
const fileDetail = {
67+
filename: file.filename,
68+
additions: file.additions,
69+
deletions: file.deletions,
70+
changes: file.changes,
71+
status: file.status,
72+
raw_url: file.raw_url,
73+
blob_url: file.blob_url,
74+
patch: file.patch,
75+
content: undefined as string | undefined,
76+
}
77+
78+
// Only try to fetch content for files that are not too large and not deleted
79+
if (file.status !== 'removed' && file.raw_url) {
80+
try {
81+
// Fetch the raw file content
82+
const contentResponse = await fetch(file.raw_url, {
83+
headers: {
84+
Authorization: `Bearer ${params?.apiKey}`,
85+
'X-GitHub-Api-Version': '2022-11-28',
86+
},
87+
})
88+
89+
if (contentResponse.ok) {
90+
fileDetail.content = await contentResponse.text()
91+
}
92+
} catch (error) {
93+
console.error(`Failed to fetch content for ${file.filename}:`, error)
94+
}
95+
}
96+
97+
fileDetailsWithContent.push(fileDetail)
98+
}
99+
}
100+
59101
return {
60102
success: true,
61103
output: {
@@ -81,6 +123,7 @@ export const latestCommitTool: ToolConfig<LatestCommitParams, LatestCommitRespon
81123
deletions: data.stats.deletions,
82124
total: data.stats.total,
83125
} : undefined,
126+
files: fileDetailsWithContent.length > 0 ? fileDetailsWithContent : undefined,
84127
},
85128
},
86129
}

sim/app/tools/github/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ interface CommitMetadata {
9797
deletions: number
9898
total: number
9999
}
100+
files?: Array<{
101+
filename: string
102+
additions: number
103+
deletions: number
104+
changes: number
105+
status: string
106+
raw_url: string
107+
blob_url: string
108+
patch?: string
109+
content?: string
110+
}>
100111
}
101112

102113
interface RepoMetadata {

0 commit comments

Comments
 (0)