Repository navigation
refactor(codersdk): add chat client request helpers - #30961
Conversation
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review
|
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review
|
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Removes two unused exports from `codersdk/chats.go`: the `ChatGitChange` type and `IsChatGitWatchFallbackMessage`. Regenerating the TypeScript types drops the `ChatGitChange` interface from `site/src/api/typesGenerated.ts`. **Why removal is safe:** neither symbol has a caller anywhere in this repository, including `site/`, `cli/`, `enterprise/`, `scaletest/` and tests. A GitHub code search across the coder organization finds only forks of this file and a copy of `typesGenerated.ts`. `IsChatGitWatchFallbackMessage` lost its only caller when #25190 removed the agents TUI. The comment on the git watch messages loses the sentences about that helper and the "keep in sync" note: `coderd/exp_chats.go` uses these constants directly. **Kept on purpose:** - `NewDynamicTool`, `DynamicToolCall`, `DynamicToolResponse` and the `invopop/jsonschema` import: bots outside this repository build on them (#24036). - `ChatMessageSource`: also unused, but #30861 adds a function right above it, and deleting it here would make that PR conflict. It can go after #30861 merges. - `AllChatMessagePartTypes`, `ChatDebugTerminalStatuses` and `WithChatDiffStatusRef`: `scripts/apitypings`, a guard test and a coderd test use them. Validation: the pre-commit hook passed (`make gen`, formatting, golangci-lint over every package and test file, and the slim build). `make site/src/api/typesGenerated.ts` produces exactly the TypeScript change in this PR. ## Stack This stack removes two unused chat exports from `codersdk` and moves 72 chat client methods in `codersdk/chats.go` onto two shared request helpers. Client behavior does not change, except that #30961 closes an unexpected-status response body once instead of twice. 1. #30959 Remove unused chat exports from codersdk 2. #30960 Cover chat client request errors and options 3. #30961 Add chat client request helpers, for the project and model methods 4. #30962 Move the chat settings methods onto the request helpers 5. #30963 Move the chat methods onto the request helpers > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
c52a7bc to
57d0f8b
Compare
57d0f8b to
01ffb5f
Compare
|
@codex review
|
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
06d277b to
e1f45cb
Compare
Most chat client methods repeat the same request, status check, error read, and JSON decode. Add requestJSON and requestNoContent, and move the chat project and chat model methods onto them. Each converted method keeps its signature, HTTP method, path, body, request options, and expected status. Error results are unchanged. ListChatProjects, UpdateChatProject, DeleteChatProject, and ChatProjectInstructions stay as they are because open PRs edit them or the lines next to them.
ReadBodyAsError closes the response body, so the request helpers' deferred Close ran a second time for every unexpected status. Standard net/http bodies tolerate that, but a custom transport's body may not: one whose Close closes a channel panicked instead of returning the API error. Defer the close only after the status check, and make TestChatClientRequests require exactly one Close per response.
Adds `TestChatClientRequests`, a test of the two chat client request failures that no other test reaches. It is the test-first layer for the refactor above it, and it only adds test lines (+42). **Why it is needed:** the next layers replace the request handling in 70 chat client methods with two shared helpers. The coderd tests already run those methods against a real server, so they cover success paths, error statuses, and request options. A real server cannot produce two failures: - a transport error, which must come back as an error that `errors.Is` matches. If a method swallowed it, callers would get a zero value and no error. If it wrapped it with `%v`, callers that check `errors.Is(err, context.Canceled)` would stop matching. - a 200 response with a body that is not valid JSON, which must come back as an error, not a zero value and no error. `Test_ReadBodyAsJSON` covers the decoder itself, not that a method returns its error. The test calls `ChatModel` (JSON response) and `DeleteChatModel` (no content), which #30961 moves onto the two helpers, through a fake transport. **Proof:** the test passes on `main` and on this branch. With `go test -overlay`, it fails when `ChatModel` returns no error for a transport failure, wraps it with `%v`, or drops the decode error, and it passes when the transport error is wrapped with `%w`. Cases I dropped because existing tests already fail on the same mutants: removing the status check from `ChatModel` fails `TestChatModelACL` (expects 404), and dropping the request options from `ChatModelACLAvailable` fails `TestChatModelACLAvailable`. I also dropped the check that another 2xx status is an error, because coderd never returns one on these routes. Validation: `go test ./codersdk/... -count=1` passes, `golangci-lint run ./codersdk/` reports 0 issues, and the pre-commit hook passed. ## Stack This stack removes two unused chat exports from `codersdk` and moves 70 chat client methods in `codersdk/chats.go` onto two shared request helpers. Client behavior does not change, except that #30961 closes an unexpected-status response body once instead of twice. 1. #30959 Remove unused chat exports from codersdk 2. #30960 Cover chat client request errors 3. #30961 Add chat client request helpers, for the project and model methods 4. #30962 Move the chat settings methods onto the request helpers 5. #30963 Move the chat methods onto the request helpers > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
e1f45cb to
6c70d40
Compare
Adds two unexported helpers to
codersdk/chats.goand moves the 16 chat project and chat model client methods onto them.requestJSON[T]sends the request, checks for the one status the method expects, and decodes the JSON body.requestNoContentdoes the same for204 No Content. Each converted method drops its own copy of that block, so the file loses 116 lines (+46/-162). The test changes incodersdk/chats_test.goare +59/-0.Why it is safe: the helpers return the same errors as the code they replace: the
Client.Requesterror unchanged,ReadBodyAsErrorfor any other status, and theReadBodyAsJSONerror. A script did the conversion. It rewrites a method only when its body ends in exactly that block, with a zero value that matches the decoded type, and it keeps the HTTP method, path, body, request options, and expected status.Behavior change: the helpers close an unexpected-status response body once, in
ReadBodyAsError, where the old methods also deferred a secondClose. Standardnet/httpbodies tolerate the secondClose, but a custom transport's body may not (one whoseClosecloses a channel panics), and #30963 would otherwise have added it toCreateChatandCreateChatMessage, which closed once.TestChatClientRequestspasses here and fails on each of 7 helper mutants (go test -overlay): drop the transport error (requestNoContent), wrap it with%v(requestJSON), move the deferredCloseback above the status check or drop it (one each per helper), and drop the decode error.ClosesResponseBodyOncecase, added here, requires exactly oneCloseper call for each helper's success and unexpected status and for an invalid JSON body. Without it the test passed with theCloseabove the status check. It replaces theClosecounts this layer had added to cases that test(codersdk): cover chat client request errors #30960 dropped after review.mainand here. With-coverpkg=./codersdk, every converted method's success path runs and both helpers are fully covered.CreateChatProjectexpecting 200 (TestChatProjectsCRUDListAndDeleteDetaches,TestChatProjectLimit),DeleteChatProjectMemorysending GET (TestChatProjectMemoriesCRUD),ChatModelACLAvailablewithout its query option (TestChatModelACLAvailable), andUpdateChatModelwithout a body (TestUpdateChatModel).Review focus: the two helpers; the next two layers only add call sites.
ListChatProjects,UpdateChatProject,DeleteChatProject, andChatProjectInstructionsstay as they are, so #30439 and #30683 still apply cleanly.Validation:
go test ./codersdk -count=1passes,golangci-lint run ./codersdk/reports 0 issues, and the pre-commit hook passed.Stack
This stack removes two unused chat exports from
codersdkand moves 70 chat client methods incodersdk/chats.goonto two shared request helpers. Client behavior does not change, except that #30961 closes an unexpected-status response body once instead of twice.