Skip to content

refactor(codersdk): move chat client methods onto request helpers - #30963

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/codersdk-chat-request-helpers-chats
Oct 30, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/codersdk-chat-request-helpers-chats

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Moves 23 more chat client methods in codersdk/chats.go onto the requestJSON and requestNoContent helpers from #30961. They cover chats, messages, prompts, ACLs, cost, debug runs, diffs, file uploads and download URLs, and tool results. Each method drops its own request, status check, and decode block, so the file loses 204 lines.

Why it is safe: the script from #30961 converted 21 of the methods, and that part of the diff is exactly its output. UploadChatFile and UploadChatWorkspaceFile are converted by hand, because the script skips request options written as closures. They pass their io.Reader body and header option to requestJSON unchanged. #30961 shows that the helpers return the same errors as the code they replace.

  • The 776 tests in test files that call a chat client method (12 packages) pass here. With -coverpkg=./codersdk, every converted method's success path runs.
  • A throwaway differential test called all 84 chat client methods against 9 canned responses (1476 calls) and recorded identical requests and results on main and here.
  • Call-site mutants (go test -overlay) fail existing tests: CreateChat expecting 200 (TestPostChats), UpdateChat sending PUT (TestPatchChat), GetChatMessages without its pagination options (TestGetChatMessages_Pagination), SubmitToolResults or UploadChatWorkspaceFile without a body (TestSubmitToolResults, TestPostChatWorkspaceFile), UploadChatWorkspaceFile without its header option (TestUploadChatWorkspaceFileContentType), and UploadChatFile expecting 200 (TestPostChatFile).

Review focus: the methods that stay as they are. EditChatMessage and InterruptChat stay so that #29215 still applies cleanly. StreamChat and WatchChats use WebSockets. GetChatFile returns raw bytes and a response header. The three user AI provider key methods wrap the request error, which the helpers do not.

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. refactor: remove unused chat exports from codersdk #30959 Remove unused chat exports from codersdk
  2. test(codersdk): cover chat client request errors #30960 Cover chat client request errors
  3. refactor(codersdk): add chat client request helpers #30961 Add chat client request helpers, for the project and model methods
  4. refactor(codersdk): move chat settings client methods onto request helpers #30962 Move the chat settings methods onto the request helpers
  5. refactor(codersdk): move chat client methods onto request helpers #30963 Move the chat methods onto the request helpers

Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T02:36:58.820350Z fd25e9d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: e6667e1838

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum, an AI agent, requested this review for @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: eefd502563

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@ibetitsmike
ibetitsmike marked this pull request as ready for review October 19, 2026 12:15
@linear-code

linear-code Bot commented Oct 21, 2026

Copy link
Copy Markdown

CODAGT-1445

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Xum, an AI agent, requested this review for @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: fd25e9de44

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@pr-queue-manager
pr-queue-manager Bot requested a review from f0ssel October 28, 2026 10:32
ibetitsmike added a commit that referenced this pull request Oct 28, 2026
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.
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from fd25e9d to cd82528 Compare October 28, 2026 22:28
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from cd82528 to 1c38e84 Compare October 29, 2026 22:57
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from d2cb08a to 00be951 Compare October 30, 2026 15:51
ibetitsmike added a commit that referenced this pull request Oct 30, 2026
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.
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from 00be951 to 4fc01f9 Compare October 30, 2026 19:08
ibetitsmike added a commit that referenced this pull request Oct 30, 2026
Adds two unexported helpers to `codersdk/chats.go` and 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. `requestNoContent` does the same for `204 No Content`.
Each converted method drops its own copy of that block, so the file
loses 116 lines (+46/-162). The test changes in `codersdk/chats_test.go`
are +59/-0.

**Why it is safe:** the helpers return the same errors as the code they
replace: the `Client.Request` error unchanged, `ReadBodyAsError` for any
other status, and the `ReadBodyAsJSON` error. 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
second `Close`. Standard `net/http` bodies tolerate the second `Close`,
but a custom transport's body may not (one whose `Close` closes a
channel panics), and #30963 would otherwise have added it to
`CreateChat` and `CreateChatMessage`, which closed once.

- `TestChatClientRequests` passes here and fails on each of 7 helper
mutants (`go test -overlay`): drop the transport error
(`requestNoContent`), wrap it with `%v` (`requestJSON`), move the
deferred `Close` back above the status check or drop it (one each per
helper), and drop the decode error.
- Its `ClosesResponseBodyOnce` case, added here, requires exactly one
`Close` per call for each helper's success and unexpected status and for
an invalid JSON body. Without it the test passed with the `Close` above
the status check. It replaces the `Close` counts this layer had added to
cases that #30960 dropped after review.
- The 776 tests in test files that call a chat client method (12
packages) pass on `main` and here. With `-coverpkg=./codersdk`, every
converted method's success path runs and both helpers are fully covered.
- Call-site mutants fail existing coderd tests: `CreateChatProject`
expecting 200 (`TestChatProjectsCRUDListAndDeleteDetaches`,
`TestChatProjectLimit`), `DeleteChatProjectMemory` sending GET
(`TestChatProjectMemoriesCRUD`), `ChatModelACLAvailable` without its
query option (`TestChatModelACLAvailable`), and `UpdateChatModel`
without a body (`TestUpdateChatModel`).

**Review focus:** the two helpers; the next two layers only add call
sites. `ListChatProjects`, `UpdateChatProject`, `DeleteChatProject`, and
`ChatProjectInstructions` stay as they are, so #30439 and #30683 still
apply cleanly.

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.
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from 4fc01f9 to 62d7b7d Compare October 30, 2026 20:16
Base automatically changed from mike/codersdk-chat-request-helpers-settings to main October 30, 2026 20:31
ibetitsmike added a commit that referenced this pull request Oct 30, 2026
…lpers (#30962)

Moves the 31 chat settings client methods in `codersdk/chats.go` onto
the `requestJSON` and `requestNoContent` helpers from #30961: the
deployment, organization, and user settings under `/chats/config`, and
the organization model overrides. Each method drops its own request,
status check, and decode block, so the file loses 266 lines.

**Why it is safe:** the script from #30961 did the conversion, and this
diff is exactly its output. It rewrites a method only when its body ends
in the standard block, with a zero value that matches the decoded type,
and it keeps the HTTP method, path, body, and expected status. #30961
shows that the helpers return the same errors as the code they replace.

- The 776 tests in test files that call a chat client method (12
packages) pass here. With `-coverpkg=./codersdk`, every converted
method's success path runs.
- Call-site mutants (`go test -overlay`) fail existing coderd tests:
`UpdateOrganizationChatModelOverride` without a body
(`TestChatModelOverrides`), `DeleteUserChatCompactionThreshold` sending
GET (`TestUserChatCompactionThresholds`), `GetChatAdvisorConfig`
expecting 201 (`TestChatAdvisorConfig_GetDefault`), and
`GetChatRetentionDays` reading the debug retention path
(`TestChatRetentionDays`).

**Review focus:** each hunk swaps one block for one call, so check the
status argument against the deleted check.
`UpdateOrganizationChatModelOverride`, `UpdateUserChatCustomPrompt`, and
`UpdateUserChatCompactionThreshold` return a body with 200. The other
updates expect 204.

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.
Move 21 more chat client methods (chats, messages, prompts, ACLs, cost,
debug runs, diffs, file download URLs, and tool results) onto
requestJSON and requestNoContent. Each converted method keeps its
signature, HTTP method, path, body, request options, and expected
status. Error results are unchanged.

EditChatMessage and InterruptChat stay as they are because an open PR
edits them. The streaming, upload, raw download, and user AI provider
key methods do not fit the helpers.
UploadChatFile and UploadChatWorkspaceFile end in the standard request
block, and requestJSON passes their io.Reader body and header option
straight to Client.Request, so move both onto it. GetChatFile stays as
it is because it returns raw bytes and a response header.
@ibetitsmike
ibetitsmike force-pushed the mike/codersdk-chat-request-helpers-chats branch from 62d7b7d to f7b72ca Compare October 30, 2026 20:31
@ibetitsmike
ibetitsmike merged commit c80aafe into main Oct 30, 2026
28 of 29 checks passed
@ibetitsmike
ibetitsmike deleted the mike/codersdk-chat-request-helpers-chats branch October 30, 2026 20:46
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants