Skip to content

Commit 99ed197

Browse files
committed
optimize context usage
1 parent 543a1fa commit 99ed197

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

pkg/github/minimal_types.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,29 @@ type MinimalIssueComment struct {
185185
UpdatedAt string `json:"updated_at,omitempty"`
186186
}
187187

188+
// MinimalFileContentResponse is the trimmed output type for create/update/delete file responses.
189+
type MinimalFileContentResponse struct {
190+
Content *MinimalFileContent `json:"content,omitempty"`
191+
Commit *MinimalFileCommit `json:"commit,omitempty"`
192+
}
193+
194+
// MinimalFileContent is the trimmed content portion of a file operation response.
195+
type MinimalFileContent struct {
196+
Name string `json:"name"`
197+
Path string `json:"path"`
198+
SHA string `json:"sha"`
199+
Size int `json:"size,omitempty"`
200+
HTMLURL string `json:"html_url"`
201+
}
202+
203+
// MinimalFileCommit is the trimmed commit portion of a file operation response.
204+
type MinimalFileCommit struct {
205+
SHA string `json:"sha"`
206+
Message string `json:"message,omitempty"`
207+
HTMLURL string `json:"html_url,omitempty"`
208+
Author *MinimalCommitAuthor `json:"author,omitempty"`
209+
}
210+
188211
// MinimalPullRequest is the trimmed output type for pull request objects to reduce verbosity.
189212
type MinimalPullRequest struct {
190213
Number int `json:"number"`
@@ -328,6 +351,38 @@ func convertToMinimalIssueComment(comment *github.IssueComment) MinimalIssueComm
328351
return m
329352
}
330353

354+
func convertToMinimalFileContentResponse(resp *github.RepositoryContentResponse) MinimalFileContentResponse {
355+
m := MinimalFileContentResponse{}
356+
357+
if c := resp.Content; c != nil {
358+
m.Content = &MinimalFileContent{
359+
Name: c.GetName(),
360+
Path: c.GetPath(),
361+
SHA: c.GetSHA(),
362+
Size: c.GetSize(),
363+
HTMLURL: c.GetHTMLURL(),
364+
}
365+
}
366+
367+
m.Commit = &MinimalFileCommit{
368+
SHA: resp.Commit.GetSHA(),
369+
Message: resp.Commit.GetMessage(),
370+
HTMLURL: resp.Commit.GetHTMLURL(),
371+
}
372+
373+
if author := resp.Commit.Author; author != nil {
374+
m.Commit.Author = &MinimalCommitAuthor{
375+
Name: author.GetName(),
376+
Email: author.GetEmail(),
377+
}
378+
if author.Date != nil {
379+
m.Commit.Author.Date = author.Date.Format(time.RFC3339)
380+
}
381+
}
382+
383+
return m
384+
}
385+
331386
func convertToMinimalPullRequest(pr *github.PullRequest) MinimalPullRequest {
332387
m := MinimalPullRequest{
333388
Number: pr.GetNumber(),

pkg/github/repositories.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,14 @@ If the SHA is not provided, the tool will attempt to acquire it by fetching the
489489
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to create/update file", resp, body), nil, nil
490490
}
491491

492-
r, err := json.Marshal(fileContent)
493-
if err != nil {
494-
return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
495-
}
492+
minimalResponse := convertToMinimalFileContentResponse(fileContent)
496493

497494
// Warn if file was updated without SHA validation (blind update)
498495
if sha == "" && previousSHA != "" {
496+
warning, err := json.Marshal(minimalResponse)
497+
if err != nil {
498+
return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
499+
}
499500
return utils.NewToolResultText(fmt.Sprintf(
500501
"Warning: File updated without SHA validation. Previous file SHA was %s. "+
501502
`Verify no unintended changes were overwritten:
@@ -504,10 +505,10 @@ If the SHA is not provided, the tool will attempt to acquire it by fetching the
504505
3. Revert changes if shas do not match.
505506
506507
%s`,
507-
previousSHA, path, string(r))), nil, nil
508+
previousSHA, path, string(warning))), nil, nil
508509
}
509510

510-
return utils.NewToolResultText(string(r)), nil, nil
511+
return MarshalledTextResult(minimalResponse), nil, nil
511512
},
512513
)
513514
}

pkg/github/repositories_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,18 +1434,18 @@ func Test_CreateOrUpdateFile(t *testing.T) {
14341434
}
14351435

14361436
// Unmarshal and verify the result
1437-
var returnedContent github.RepositoryContentResponse
1437+
var returnedContent MinimalFileContentResponse
14381438
err = json.Unmarshal([]byte(textContent.Text), &returnedContent)
14391439
require.NoError(t, err)
14401440

14411441
// Verify content
1442-
assert.Equal(t, *tc.expectedContent.Content.Name, *returnedContent.Content.Name)
1443-
assert.Equal(t, *tc.expectedContent.Content.Path, *returnedContent.Content.Path)
1444-
assert.Equal(t, *tc.expectedContent.Content.SHA, *returnedContent.Content.SHA)
1442+
assert.Equal(t, tc.expectedContent.Content.GetName(), returnedContent.Content.Name)
1443+
assert.Equal(t, tc.expectedContent.Content.GetPath(), returnedContent.Content.Path)
1444+
assert.Equal(t, tc.expectedContent.Content.GetSHA(), returnedContent.Content.SHA)
14451445

14461446
// Verify commit
1447-
assert.Equal(t, *tc.expectedContent.Commit.SHA, *returnedContent.Commit.SHA)
1448-
assert.Equal(t, *tc.expectedContent.Commit.Message, *returnedContent.Commit.Message)
1447+
assert.Equal(t, tc.expectedContent.Commit.GetSHA(), returnedContent.Commit.SHA)
1448+
assert.Equal(t, tc.expectedContent.Commit.GetMessage(), returnedContent.Commit.Message)
14491449
})
14501450
}
14511451
}

0 commit comments

Comments
 (0)