-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathcreate_file.ts
More file actions
196 lines (182 loc) · 5.59 KB
/
Copy pathcreate_file.ts
File metadata and controls
196 lines (182 loc) · 5.59 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
import type { CreateFileParams, FileOperationResponse } from '@/tools/github/types'
import type { ToolConfig } from '@/tools/types'
export const createFileTool: ToolConfig<CreateFileParams, FileOperationResponse> = {
id: 'github_create_file',
name: 'GitHub Create File',
description:
'Create a new file in a GitHub repository. The file content will be automatically Base64 encoded. Supports files up to 1MB.',
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 where the file will be created (e.g., "src/newfile.ts")',
},
message: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Commit message for this file creation',
},
content: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'File content (plain text, will be Base64 encoded automatically)',
},
branch: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Branch to create the file in (defaults to repository default branch)',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub Personal Access Token',
},
},
request: {
url: (params) =>
`https://api.github.com/repos/${params.owner}/${params.repo}/contents/${params.path}`,
method: 'PUT',
headers: (params) => ({
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
}),
body: (params) => {
const base64Content = Buffer.from(params.content).toString('base64')
const body: Record<string, any> = {
message: params.message,
content: base64Content,
}
if (params.branch) {
body.branch = params.branch
}
return body
},
},
transformResponse: async (response) => {
const data = await response.json()
const content = `File created successfully!
Path: ${data.content.path}
Name: ${data.content.name}
Size: ${data.content.size} bytes
SHA: ${data.content.sha}
Commit:
- SHA: ${data.commit.sha}
- Message: ${data.commit.message}
- Author: ${data.commit.author.name}
- Date: ${data.commit.author.date}
View file: ${data.content.html_url}`
return {
success: true,
output: {
content,
metadata: {
file: {
name: data.content.name,
path: data.content.path,
sha: data.content.sha,
size: data.content.size,
type: data.content.type,
download_url: data.content.download_url,
html_url: data.content.html_url,
},
commit: {
sha: data.commit.sha,
message: data.commit.message,
author: {
name: data.commit.author.name,
email: data.commit.author.email,
date: data.commit.author.date,
},
committer: {
name: data.commit.committer.name,
email: data.commit.committer.email,
date: data.commit.committer.date,
},
html_url: data.commit.html_url,
},
},
},
}
},
outputs: {
content: { type: 'string', description: 'Human-readable file creation confirmation' },
metadata: {
type: 'object',
description: 'File and commit metadata',
properties: {
file: {
type: 'object',
description: 'Created file information',
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' },
download_url: { type: 'string', description: 'Direct download URL' },
html_url: { type: 'string', description: 'GitHub web UI URL' },
},
},
commit: {
type: 'object',
description: 'Commit information',
properties: {
sha: { type: 'string', description: 'Commit SHA' },
message: { type: 'string', description: 'Commit message' },
author: {
type: 'object',
description: 'Author information',
},
committer: {
type: 'object',
description: 'Committer information',
},
html_url: { type: 'string', description: 'Commit URL' },
},
},
},
},
},
}
export const createFileV2Tool: ToolConfig = {
id: 'github_create_file_v2',
name: createFileTool.name,
description: createFileTool.description,
version: '2.0.0',
params: createFileTool.params,
request: createFileTool.request,
oauth: createFileTool.oauth,
transformResponse: async (response: Response) => {
const data = await response.json()
return {
success: true,
output: {
content: data.content,
commit: data.commit,
},
}
},
outputs: {
content: { type: 'json', description: 'Created file content info' },
commit: { type: 'json', description: 'Commit information' },
},
}