feat: add CLI commands for managing chat context from workspaces (#24105)

Adds `coder exp chat context add` and `coder exp chat context clear`
commands that run inside a workspace to manage chat context files via
the agent token.

`add` reads instruction and skill files from a directory (defaulting to
cwd) and inserts them as context-file messages into an active chat.
Multiple calls are additive — `instructionFromContextFiles` already
accumulates all context-file parts across messages.

`clear` soft-deletes all context-file messages, causing
`contextFileAgentID()` to return `!found` on the next turn, which
triggers `needsInstructionPersist=true` and re-fetches defaults from the
agent.

Both commands auto-detect the target chat via `CODER_CHAT_ID` (already
set by `agentproc` on chat-spawned processes), or fall back to
single-active-chat resolution for the agent. The `--chat` flag overrides
both.

Also adds sub-agent context inheritance: `createChildSubagentChat` now
copies parent context-file messages to child chats at spawn time, so
delegated sub-agents share the same instruction context without
independently re-fetching from the workspace agent.

<details><summary>Implementation details</summary>

**New files:**
- `cli/exp_chat.go` — CLI command tree under `coder exp chat context`

**Modified files:**
- `agent/agentcontextconfig/api.go` — `ConfigFromDir()` reads context
from an arbitrary directory without env vars
- `codersdk/agentsdk/agentsdk.go` — `AddChatContext`/`ClearChatContext`
SDK methods
- `coderd/workspaceagents.go` — POST/DELETE handlers on
`/workspaceagents/me/chat-context`
- `coderd/coderd.go` — Route registration
- `coderd/database/queries/chats.sql` — `GetActiveChatsByAgentID`,
`SoftDeleteContextFileMessages`
- `coderd/database/dbauthz/dbauthz.go` — RBAC implementations for new
queries
- `coderd/x/chatd/subagent.go` — `copyParentContextFiles` for sub-agent
inheritance
- `cli/root.go` — Register `chatCommand()` in `AGPLExperimental()`

**Auth pattern:** Uses `AgentAuth` (same as `coder external-auth`) —
agent token via `CODER_AGENT_TOKEN` + `CODER_AGENT_URL` env vars.

</details>

> 🤖 Generated by Coder Agents

---------

Co-authored-by: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com>
This commit is contained in:
Kyle Carberry
2026-04-09 16:33:00 +02:00
committed by GitHub
co-authored by Michael Suchacz
parent f8e8f979a2
commit 391b22aef7
29 changed files with 4354 additions and 258 deletions
+63
View File
@@ -892,3 +892,66 @@ func (s *SSEAgentReinitReceiver) Receive(ctx context.Context) (*Reinitialization
return &reinitEvent, nil
}
}
// AddChatContextRequest is the request body for adding chat context.
type AddChatContextRequest struct {
// ChatID optionally identifies the chat to add context to.
// If empty, auto-detection is used (CODER_CHAT_ID env, the
// only active chat, or the only top-level active chat for this
// agent).
ChatID uuid.UUID `json:"chat_id,omitempty"`
// Parts are the context-file and skill parts to add.
Parts []codersdk.ChatMessagePart `json:"parts"`
}
// AddChatContextResponse is the response for adding chat context.
type AddChatContextResponse struct {
ChatID uuid.UUID `json:"chat_id"`
Count int `json:"count"`
}
// ClearChatContextRequest is the request body for clearing chat context.
type ClearChatContextRequest struct {
// ChatID optionally identifies the chat to clear context from.
// If empty, auto-detection is used (CODER_CHAT_ID env, the
// only active chat, or the only top-level active chat for this
// agent).
ChatID uuid.UUID `json:"chat_id,omitempty"`
}
// ClearChatContextResponse is the response for clearing chat context.
type ClearChatContextResponse struct {
ChatID uuid.UUID `json:"chat_id"`
}
// AddChatContext adds context-file and skill parts to an active chat.
func (c *Client) AddChatContext(ctx context.Context, req AddChatContextRequest) (AddChatContextResponse, error) {
res, err := c.SDK.Request(ctx, http.MethodPost, "/api/v2/workspaceagents/me/experimental/chat-context", req)
if err != nil {
return AddChatContextResponse{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return AddChatContextResponse{}, codersdk.ReadBodyAsError(res)
}
var resp AddChatContextResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}
// ClearChatContext soft-deletes context-file and skill messages from an active chat.
func (c *Client) ClearChatContext(ctx context.Context, req ClearChatContextRequest) (ClearChatContextResponse, error) {
res, err := c.SDK.Request(ctx, http.MethodDelete, "/api/v2/workspaceagents/me/experimental/chat-context", req)
if err != nil {
return ClearChatContextResponse{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ClearChatContextResponse{}, codersdk.ReadBodyAsError(res)
}
var resp ClearChatContextResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}