2022-02-21 14:36:29 -06:00
package codersdk
import (
"context"
"encoding/json"
"fmt"
2023-03-23 14:09:13 -05:00
"io"
2022-02-21 14:36:29 -06:00
"net/http"
2023-06-28 11:54:13 +03:00
"strings"
2022-08-31 20:09:44 -05:00
"time"
2022-02-21 14:36:29 -06:00
2022-04-11 16:06:15 -05:00
"github.com/google/uuid"
2022-02-21 14:36:29 -06:00
"golang.org/x/xerrors"
2022-04-11 16:06:15 -05:00
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/tracing"
2024-12-03 10:12:30 +04:00
"github.com/coder/coder/v2/codersdk/wsjson"
2024-12-19 00:51:30 +04:00
"github.com/coder/websocket"
2022-02-21 14:36:29 -06:00
)
2022-10-03 17:01:13 -04:00
type WorkspaceAgentStatus string
2023-03-31 00:24:51 +11:00
// This is also in database/modelmethods.go and should be kept in sync.
2022-10-03 17:01:13 -04:00
const (
WorkspaceAgentConnecting WorkspaceAgentStatus = "connecting"
WorkspaceAgentConnected WorkspaceAgentStatus = "connected"
WorkspaceAgentDisconnected WorkspaceAgentStatus = "disconnected"
2022-11-09 17:27:05 +02:00
WorkspaceAgentTimeout WorkspaceAgentStatus = "timeout"
2022-10-03 17:01:13 -04:00
)
2023-01-24 14:24:27 +02:00
// WorkspaceAgentLifecycle represents the lifecycle state of a workspace agent.
//
// The agent lifecycle starts in the "created" state, and transitions to
// "starting" when the agent reports it has begun preparing (e.g. started
// executing the startup script).
type WorkspaceAgentLifecycle string
// WorkspaceAgentLifecycle enums.
const (
2023-03-06 21:34:00 +02:00
WorkspaceAgentLifecycleCreated WorkspaceAgentLifecycle = "created"
WorkspaceAgentLifecycleStarting WorkspaceAgentLifecycle = "starting"
WorkspaceAgentLifecycleStartTimeout WorkspaceAgentLifecycle = "start_timeout"
WorkspaceAgentLifecycleStartError WorkspaceAgentLifecycle = "start_error"
WorkspaceAgentLifecycleReady WorkspaceAgentLifecycle = "ready"
WorkspaceAgentLifecycleShuttingDown WorkspaceAgentLifecycle = "shutting_down"
WorkspaceAgentLifecycleShutdownTimeout WorkspaceAgentLifecycle = "shutdown_timeout"
WorkspaceAgentLifecycleShutdownError WorkspaceAgentLifecycle = "shutdown_error"
WorkspaceAgentLifecycleOff WorkspaceAgentLifecycle = "off"
2023-01-24 14:24:27 +02:00
)
2023-06-16 17:14:22 +03:00
// Starting returns true if the agent is in the process of starting.
func ( l WorkspaceAgentLifecycle ) Starting () bool {
switch l {
2023-09-25 16:47:17 -05:00
case WorkspaceAgentLifecycleCreated , WorkspaceAgentLifecycleStarting :
2023-06-16 17:14:22 +03:00
return true
default :
return false
}
}
2023-06-28 11:54:13 +03:00
// ShuttingDown returns true if the agent is in the process of shutting
// down or has shut down.
func ( l WorkspaceAgentLifecycle ) ShuttingDown () bool {
switch l {
case WorkspaceAgentLifecycleShuttingDown , WorkspaceAgentLifecycleShutdownTimeout , WorkspaceAgentLifecycleShutdownError , WorkspaceAgentLifecycleOff :
return true
default :
return false
}
}
2023-03-06 21:34:00 +02:00
// WorkspaceAgentLifecycleOrder is the order in which workspace agent
// lifecycle states are expected to be reported during the lifetime of
// the agent process. For instance, the agent can go from starting to
// ready without reporting timeout or error, but it should not go from
// ready to starting. This is merely a hint for the agent process, and
// is not enforced by the server.
var WorkspaceAgentLifecycleOrder = [] WorkspaceAgentLifecycle {
WorkspaceAgentLifecycleCreated ,
WorkspaceAgentLifecycleStarting ,
WorkspaceAgentLifecycleStartTimeout ,
WorkspaceAgentLifecycleStartError ,
WorkspaceAgentLifecycleReady ,
WorkspaceAgentLifecycleShuttingDown ,
WorkspaceAgentLifecycleShutdownTimeout ,
WorkspaceAgentLifecycleShutdownError ,
WorkspaceAgentLifecycleOff ,
}
2023-06-06 11:58:07 +03:00
// WorkspaceAgentStartupScriptBehavior defines whether or not the startup script
// should be considered blocking or non-blocking. The blocking behavior means
// that the agent will not be considered ready until the startup script has
// completed and, for example, SSH connections will wait for the agent to be
// ready (can be overridden).
//
// Presently, non-blocking is the default, but this may change in the future.
2023-09-25 16:47:17 -05:00
// Deprecated: `coder_script` allows configuration on a per-script basis.
2023-06-06 11:58:07 +03:00
type WorkspaceAgentStartupScriptBehavior string
const (
WorkspaceAgentStartupScriptBehaviorBlocking WorkspaceAgentStartupScriptBehavior = "blocking"
WorkspaceAgentStartupScriptBehaviorNonBlocking WorkspaceAgentStartupScriptBehavior = "non-blocking"
)
2023-03-31 15:26:19 -05:00
type WorkspaceAgentMetadataResult struct {
CollectedAt time . Time `json:"collected_at" format:"date-time"`
// Age is the number of seconds since the metadata was collected.
// It is provided in addition to CollectedAt to protect against clock skew.
Age int64 `json:"age"`
Value string `json:"value"`
Error string `json:"error"`
}
// WorkspaceAgentMetadataDescription is a description of dynamic metadata the agent should report
// back to coderd. It is provided via the `metadata` list in the `coder_agent`
// block.
type WorkspaceAgentMetadataDescription struct {
DisplayName string `json:"display_name"`
Key string `json:"key"`
Script string `json:"script"`
Interval int64 `json:"interval"`
Timeout int64 `json:"timeout"`
}
type WorkspaceAgentMetadata struct {
Result WorkspaceAgentMetadataResult `json:"result"`
Description WorkspaceAgentMetadataDescription `json:"description"`
}
2023-08-30 14:53:42 -05:00
type DisplayApp string
const (
DisplayAppVSCodeDesktop DisplayApp = "vscode"
DisplayAppVSCodeInsiders DisplayApp = "vscode_insiders"
DisplayAppWebTerminal DisplayApp = "web_terminal"
DisplayAppPortForward DisplayApp = "port_forwarding_helper"
DisplayAppSSH DisplayApp = "ssh_helper"
)
2022-10-03 17:01:13 -04:00
type WorkspaceAgent struct {
2023-09-25 16:47:17 -05:00
ID uuid . UUID `json:"id" format:"uuid"`
2025-05-13 00:01:31 +01:00
ParentID uuid . NullUUID `json:"parent_id" format:"uuid"`
2023-09-25 16:47:17 -05:00
CreatedAt time . Time `json:"created_at" format:"date-time"`
UpdatedAt time . Time `json:"updated_at" format:"date-time"`
FirstConnectedAt * time . Time `json:"first_connected_at,omitempty" format:"date-time"`
LastConnectedAt * time . Time `json:"last_connected_at,omitempty" format:"date-time"`
DisconnectedAt * time . Time `json:"disconnected_at,omitempty" format:"date-time"`
StartedAt * time . Time `json:"started_at,omitempty" format:"date-time"`
ReadyAt * time . Time `json:"ready_at,omitempty" format:"date-time"`
Status WorkspaceAgentStatus `json:"status"`
LifecycleState WorkspaceAgentLifecycle `json:"lifecycle_state"`
Name string `json:"name"`
ResourceID uuid . UUID `json:"resource_id" format:"uuid"`
InstanceID string `json:"instance_id,omitempty"`
Architecture string `json:"architecture"`
EnvironmentVariables map [ string ] string `json:"environment_variables"`
OperatingSystem string `json:"operating_system"`
LogsLength int32 `json:"logs_length"`
LogsOverflowed bool `json:"logs_overflowed"`
Directory string `json:"directory,omitempty"`
ExpandedDirectory string `json:"expanded_directory,omitempty"`
Version string `json:"version"`
2023-10-31 10:08:43 +04:00
APIVersion string `json:"api_version"`
2023-09-25 16:47:17 -05:00
Apps [] WorkspaceApp `json:"apps"`
2022-10-03 17:01:13 -04:00
// DERPLatency is mapped by region name (e.g. "New York City", "Seattle").
2023-09-25 16:47:17 -05:00
DERPLatency map [ string ] DERPRegion `json:"latency,omitempty"`
ConnectionTimeoutSeconds int32 `json:"connection_timeout_seconds"`
TroubleshootingURL string `json:"troubleshooting_url"`
Subsystems [] AgentSubsystem `json:"subsystems"`
Health WorkspaceAgentHealth `json:"health"` // Health reports the health of the agent.
DisplayApps [] DisplayApp `json:"display_apps"`
LogSources [] WorkspaceAgentLogSource `json:"log_sources"`
Scripts [] WorkspaceAgentScript `json:"scripts"`
2026-08-10 08:13:32 -05:00
// Metadata is only populated on the workspaces list endpoint when the
// request opts in with the include_agent_metadata search key, and it
// only carries the requested keys. The description's script is always
// empty here: it can be long, and list consumers want values.
Metadata [] WorkspaceAgentMetadata `json:"metadata,omitempty"`
2023-09-25 16:47:17 -05:00
// StartupScriptBehavior is a legacy field that is deprecated in favor
// of the `coder_script` resource. It's only referenced by old clients.
// Deprecated: Remove in the future!
StartupScriptBehavior WorkspaceAgentStartupScriptBehavior `json:"startup_script_behavior"`
}
type WorkspaceAgentLogSource struct {
WorkspaceAgentID uuid . UUID `json:"workspace_agent_id" format:"uuid"`
ID uuid . UUID `json:"id" format:"uuid"`
CreatedAt time . Time `json:"created_at" format:"date-time"`
DisplayName string `json:"display_name"`
Icon string `json:"icon"`
}
2026-04-29 12:24:26 -08:00
type WorkspaceAgentScriptStatus string
// This is also in database/models.go and should be kept in sync.
const (
WorkspaceAgentScriptStatusOK WorkspaceAgentScriptStatus = "ok"
WorkspaceAgentScriptStatusExitFailure WorkspaceAgentScriptStatus = "exit_failure"
WorkspaceAgentScriptStatusTimedOut WorkspaceAgentScriptStatus = "timed_out"
WorkspaceAgentScriptStatusPipesLeftOpen WorkspaceAgentScriptStatus = "pipes_left_open"
)
2023-09-25 16:47:17 -05:00
type WorkspaceAgentScript struct {
2026-04-29 12:24:26 -08:00
ID uuid . UUID `json:"id" format:"uuid"`
LogSourceID uuid . UUID `json:"log_source_id" format:"uuid"`
LogPath string `json:"log_path"`
Script string `json:"script"`
Cron string `json:"cron"`
RunOnStart bool `json:"run_on_start"`
RunOnStop bool `json:"run_on_stop"`
StartBlocksLogin bool `json:"start_blocks_login"`
Timeout time . Duration `json:"timeout"`
DisplayName string `json:"display_name"`
ExitCode * int32 `json:"exit_code,omitempty"`
Status * WorkspaceAgentScriptStatus `json:"status,omitempty"`
2023-07-10 12:40:11 +03:00
}
type WorkspaceAgentHealth struct {
Healthy bool `json:"healthy" example:"false"` // Healthy is true if the agent is healthy.
Reason string `json:"reason,omitempty" example:"agent has lost connection"` // Reason is a human-readable explanation of the agent's health. It is empty if Healthy is true.
2022-10-03 17:01:13 -04:00
}
type DERPRegion struct {
Preferred bool `json:"preferred"`
LatencyMilliseconds float64 `json:"latency_ms"`
}
2024-03-26 12:44:31 -05:00
type WorkspaceAgentLog struct {
ID int64 `json:"id"`
CreatedAt time . Time `json:"created_at" format:"date-time"`
Output string `json:"output"`
Level LogLevel `json:"level"`
SourceID uuid . UUID `json:"source_id" format:"uuid"`
2022-08-31 20:09:44 -05:00
}
2026-02-03 09:45:23 +00:00
// Text formats the log entry as human-readable text.
func ( l WorkspaceAgentLog ) Text ( agentName , sourceName string ) string {
var sb strings . Builder
_ , _ = sb . WriteString ( l . CreatedAt . Format ( time . RFC3339 ))
_ , _ = sb . WriteString ( " [" )
_ , _ = sb . WriteString ( string ( l . Level ))
_ , _ = sb . WriteString ( "] [agent" )
if agentName != "" {
_ , _ = sb . WriteString ( "." )
_ , _ = sb . WriteString ( agentName )
}
if sourceName != "" {
_ , _ = sb . WriteString ( "|" )
_ , _ = sb . WriteString ( sourceName )
}
_ , _ = sb . WriteString ( "] " )
_ , _ = sb . WriteString ( l . Output )
return sb . String ()
}
2024-03-26 12:44:31 -05:00
type AgentSubsystem string
2023-06-21 14:33:19 -05:00
2024-03-26 12:44:31 -05:00
const (
AgentSubsystemEnvbox AgentSubsystem = "envbox"
AgentSubsystemEnvbuilder AgentSubsystem = "envbuilder"
AgentSubsystemExectrace AgentSubsystem = "exectrace"
)
2023-07-26 09:21:04 -07:00
2024-03-26 12:44:31 -05:00
func ( s AgentSubsystem ) Valid () bool {
switch s {
case AgentSubsystemEnvbox , AgentSubsystemEnvbuilder , AgentSubsystemExectrace :
return true
default :
return false
2024-01-29 11:26:50 +04:00
}
}
2023-03-31 15:26:19 -05:00
// WatchWorkspaceAgentMetadata watches the metadata of a workspace agent.
// The returned channel will be closed when the context is canceled. Exactly
// one error will be sent on the error channel. The metadata channel is never closed.
func ( c * Client ) WatchWorkspaceAgentMetadata ( ctx context . Context , id uuid . UUID ) ( <- chan [] WorkspaceAgentMetadata , <- chan error ) {
ctx , span := tracing . StartSpan ( ctx )
defer span . End ()
metadataChan := make ( chan [] WorkspaceAgentMetadata , 256 )
2023-06-13 15:21:06 +03:00
ready := make ( chan struct {})
2023-03-31 15:26:19 -05:00
watch := func () error {
res , err := c . Request ( ctx , http . MethodGet , fmt . Sprintf ( "/api/v2/workspaceagents/%s/watch-metadata" , id ), nil )
if err != nil {
return err
}
if res . StatusCode != http . StatusOK {
return ReadBodyAsError ( res )
}
nextEvent := ServerSentEventReader ( ctx , res . Body )
defer res . Body . Close ()
2023-06-13 15:21:06 +03:00
firstEvent := true
2023-03-31 15:26:19 -05:00
for {
select {
case <- ctx . Done ():
return ctx . Err ()
default :
}
sse , err := nextEvent ()
if err != nil {
return err
}
2023-06-13 15:21:06 +03:00
if firstEvent {
close ( ready ) // Only close ready after the first event is received.
firstEvent = false
}
2023-11-16 17:03:53 +02:00
// Ignore pings.
if sse . Type == ServerSentEventTypePing {
continue
}
2023-03-31 15:26:19 -05:00
b , ok := sse . Data .([] byte )
if ! ok {
return xerrors . Errorf ( "unexpected data type: %T" , sse . Data )
}
switch sse . Type {
case ServerSentEventTypeData :
var met [] WorkspaceAgentMetadata
err = json . Unmarshal ( b , & met )
if err != nil {
return xerrors . Errorf ( "unmarshal metadata: %w" , err )
}
metadataChan <- met
case ServerSentEventTypeError :
var r Response
err = json . Unmarshal ( b , & r )
if err != nil {
return xerrors . Errorf ( "unmarshal error: %w" , err )
}
return xerrors . Errorf ( "%+v" , r )
default :
return xerrors . Errorf ( "unexpected event type: %s" , sse . Type )
}
}
}
errorChan := make ( chan error , 1 )
go func () {
defer close ( errorChan )
2023-06-13 15:21:06 +03:00
err := watch ()
select {
case <- ready :
default :
close ( ready ) // Error before first event.
}
errorChan <- err
2023-03-31 15:26:19 -05:00
}()
2023-06-13 15:21:06 +03:00
// Wait until first event is received and the subscription is registered.
<- ready
2023-03-31 15:26:19 -05:00
return metadataChan , errorChan
}
2022-04-11 16:06:15 -05:00
// WorkspaceAgent returns an agent by ID.
func ( c * Client ) WorkspaceAgent ( ctx context . Context , id uuid . UUID ) ( WorkspaceAgent , error ) {
2022-05-17 13:43:19 -05:00
res , err := c . Request ( ctx , http . MethodGet , fmt . Sprintf ( "/api/v2/workspaceagents/%s" , id ), nil )
2022-04-11 16:06:15 -05:00
if err != nil {
return WorkspaceAgent {}, err
}
defer res . Body . Close ()
if res . StatusCode != http . StatusOK {
2023-01-29 15:47:24 -06:00
return WorkspaceAgent {}, ReadBodyAsError ( res )
2022-04-11 16:06:15 -05:00
}
var workspaceAgent WorkspaceAgent
2026-08-05 16:22:23 -04:00
err = ReadBodyAsJSON ( res , & workspaceAgent )
2023-06-09 16:01:56 +03:00
if err != nil {
return WorkspaceAgent {}, err
}
return workspaceAgent , nil
2022-04-11 16:06:15 -05:00
}
2022-04-18 17:40:25 -05:00
2023-04-20 16:59:45 -07:00
type IssueReconnectingPTYSignedTokenRequest struct {
// URL is the URL of the reconnecting-pty endpoint you are connecting to.
URL string `json:"url" validate:"required"`
AgentID uuid . UUID `json:"agentID" format:"uuid" validate:"required"`
}
type IssueReconnectingPTYSignedTokenResponse struct {
SignedToken string `json:"signed_token"`
}
func ( c * Client ) IssueReconnectingPTYSignedToken ( ctx context . Context , req IssueReconnectingPTYSignedTokenRequest ) ( IssueReconnectingPTYSignedTokenResponse , error ) {
res , err := c . Request ( ctx , http . MethodPost , "/api/v2/applications/reconnecting-pty-signed-token" , req )
if err != nil {
return IssueReconnectingPTYSignedTokenResponse {}, err
}
defer res . Body . Close ()
if res . StatusCode != http . StatusOK {
return IssueReconnectingPTYSignedTokenResponse {}, ReadBodyAsError ( res )
}
var resp IssueReconnectingPTYSignedTokenResponse
2026-08-05 16:22:23 -04:00
return resp , ReadBodyAsJSON ( res , & resp )
2023-04-20 16:59:45 -07:00
}
2024-03-26 12:44:31 -05:00
type WorkspaceAgentListeningPortsResponse struct {
// If there are no ports in the list, nothing should be displayed in the UI.
// There must not be a "no ports available" message or anything similar, as
// there will always be no ports displayed on platforms where our port
// detection logic is unsupported.
Ports [] WorkspaceAgentListeningPort `json:"ports"`
2023-04-25 17:31:41 -07:00
}
2024-03-26 12:44:31 -05:00
type WorkspaceAgentListeningPort struct {
ProcessName string `json:"process_name"` // may be empty
Network string `json:"network"` // only "tcp" at the moment
Port uint16 `json:"port"`
2022-04-29 17:30:10 -05:00
}
2022-10-06 22:38:22 +10:00
// WorkspaceAgentListeningPorts returns a list of ports that are currently being
// listened on inside the workspace agent's network namespace.
2023-01-29 15:47:24 -06:00
func ( c * Client ) WorkspaceAgentListeningPorts ( ctx context . Context , agentID uuid . UUID ) ( WorkspaceAgentListeningPortsResponse , error ) {
2022-10-06 22:38:22 +10:00
res , err := c . Request ( ctx , http . MethodGet , fmt . Sprintf ( "/api/v2/workspaceagents/%s/listening-ports" , agentID ), nil )
if err != nil {
2023-01-29 15:47:24 -06:00
return WorkspaceAgentListeningPortsResponse {}, err
2022-10-06 22:38:22 +10:00
}
defer res . Body . Close ()
if res . StatusCode != http . StatusOK {
2023-01-29 15:47:24 -06:00
return WorkspaceAgentListeningPortsResponse {}, ReadBodyAsError ( res )
2022-10-06 22:38:22 +10:00
}
2023-01-29 15:47:24 -06:00
var listeningPorts WorkspaceAgentListeningPortsResponse
2026-08-05 16:22:23 -04:00
return listeningPorts , ReadBodyAsJSON ( res , & listeningPorts )
2022-10-06 22:38:22 +10:00
}
2025-05-26 18:30:52 +03:00
// WorkspaceAgentDevcontainerStatus is the status of a devcontainer.
type WorkspaceAgentDevcontainerStatus string
// WorkspaceAgentDevcontainerStatus enums.
const (
WorkspaceAgentDevcontainerStatusRunning WorkspaceAgentDevcontainerStatus = "running"
WorkspaceAgentDevcontainerStatusStopped WorkspaceAgentDevcontainerStatus = "stopped"
WorkspaceAgentDevcontainerStatusStarting WorkspaceAgentDevcontainerStatus = "starting"
2025-12-22 11:28:31 +00:00
WorkspaceAgentDevcontainerStatusStopping WorkspaceAgentDevcontainerStatus = "stopping"
WorkspaceAgentDevcontainerStatusDeleting WorkspaceAgentDevcontainerStatus = "deleting"
2025-05-26 18:30:52 +03:00
WorkspaceAgentDevcontainerStatusError WorkspaceAgentDevcontainerStatus = "error"
)
2025-12-22 11:28:31 +00:00
func ( s WorkspaceAgentDevcontainerStatus ) Transitioning () bool {
switch s {
case WorkspaceAgentDevcontainerStatusStarting ,
WorkspaceAgentDevcontainerStatusStopping ,
WorkspaceAgentDevcontainerStatusDeleting :
return true
default :
return false
}
}
2025-03-20 19:09:39 +02:00
// WorkspaceAgentDevcontainer defines the location of a devcontainer
// configuration in a workspace that is visible to the workspace agent.
type WorkspaceAgentDevcontainer struct {
2026-02-06 15:52:54 +00:00
ID uuid . UUID `json:"id" format:"uuid"`
Name string `json:"name"`
WorkspaceFolder string `json:"workspace_folder"`
ConfigPath string `json:"config_path,omitempty"`
SubagentID uuid . NullUUID `json:"subagent_id,omitempty" format:"uuid"`
2025-04-15 17:53:37 +03:00
// Additional runtime fields.
2025-05-26 18:30:52 +03:00
Status WorkspaceAgentDevcontainerStatus `json:"status"`
Dirty bool `json:"dirty"`
Container * WorkspaceAgentContainer `json:"container,omitempty"`
2025-06-17 16:06:47 +03:00
Agent * WorkspaceAgentDevcontainerAgent `json:"agent,omitempty"`
2025-06-30 21:34:29 +01:00
Error string `json:"error,omitempty"`
2025-06-17 16:06:47 +03:00
}
2025-07-14 21:35:35 +01:00
func ( d WorkspaceAgentDevcontainer ) Equals ( other WorkspaceAgentDevcontainer ) bool {
return d . ID == other . ID &&
d . Name == other . Name &&
d . WorkspaceFolder == other . WorkspaceFolder &&
2026-02-06 15:52:54 +00:00
d . SubagentID == other . SubagentID &&
2025-07-14 21:35:35 +01:00
d . Status == other . Status &&
d . Dirty == other . Dirty &&
( d . Container == nil && other . Container == nil ||
( d . Container != nil && other . Container != nil && d . Container . ID == other . Container . ID )) &&
( d . Agent == nil && other . Agent == nil ||
( d . Agent != nil && other . Agent != nil && * d . Agent == * other . Agent )) &&
d . Error == other . Error
}
2026-02-06 15:52:54 +00:00
// IsTerraformDefined returns true if this devcontainer has resources defined
// in Terraform.
func ( d WorkspaceAgentDevcontainer ) IsTerraformDefined () bool {
return d . SubagentID . Valid
}
2025-06-17 16:06:47 +03:00
// WorkspaceAgentDevcontainerAgent represents the sub agent for a
// devcontainer.
type WorkspaceAgentDevcontainerAgent struct {
ID uuid . UUID `json:"id" format:"uuid"`
Name string `json:"name"`
Directory string `json:"directory"`
2025-03-20 19:09:39 +02:00
}
2025-03-19 12:16:14 +02:00
// WorkspaceAgentContainer describes a devcontainer of some sort
2025-02-10 11:29:30 +00:00
// that is visible to the workspace agent. This struct is an abstraction
// of potentially multiple implementations, and the fields will be
// somewhat implementation-dependent.
2025-03-19 12:16:14 +02:00
type WorkspaceAgentContainer struct {
2025-02-10 11:29:30 +00:00
// CreatedAt is the time the container was created.
CreatedAt time . Time `json:"created_at" format:"date-time"`
// ID is the unique identifier of the container.
ID string `json:"id"`
// FriendlyName is the human-readable name of the container.
FriendlyName string `json:"name"`
// Image is the name of the container image.
Image string `json:"image"`
// Labels is a map of key-value pairs of container labels.
Labels map [ string ] string `json:"labels"`
// Running is true if the container is currently running.
Running bool `json:"running"`
// Ports includes ports exposed by the container.
2025-03-19 12:16:14 +02:00
Ports [] WorkspaceAgentContainerPort `json:"ports"`
2025-02-10 11:29:30 +00:00
// Status is the current status of the container. This is somewhat
// implementation-dependent, but should generally be a human-readable
// string.
Status string `json:"status"`
// Volumes is a map of "things" mounted into the container. Again, this
// is somewhat implementation-dependent.
Volumes map [ string ] string `json:"volumes"`
}
2025-04-10 16:16:16 +03:00
func ( c * WorkspaceAgentContainer ) Match ( idOrName string ) bool {
if c . ID == idOrName {
return true
}
if c . FriendlyName == idOrName {
return true
}
return false
}
2025-03-19 12:16:14 +02:00
// WorkspaceAgentContainerPort describes a port as exposed by a container.
type WorkspaceAgentContainerPort struct {
2025-03-18 14:37:45 +00:00
// Port is the port number *inside* the container.
Port uint16 `json:"port"`
// Network is the network protocol used by the port (tcp, udp, etc).
Network string `json:"network"`
// HostIP is the IP address of the host interface to which the port is
// bound. Note that this can be an IPv4 or IPv6 address.
HostIP string `json:"host_ip,omitempty"`
// HostPort is the port number *outside* the container.
HostPort uint16 `json:"host_port,omitempty"`
}
2025-02-10 11:29:30 +00:00
// WorkspaceAgentListContainersResponse is the response to the list containers
// request.
type WorkspaceAgentListContainersResponse struct {
2025-06-17 16:06:47 +03:00
// Devcontainers is a list of devcontainers visible to the workspace agent.
Devcontainers [] WorkspaceAgentDevcontainer `json:"devcontainers"`
2025-02-10 11:29:30 +00:00
// Containers is a list of containers visible to the workspace agent.
2025-03-19 12:16:14 +02:00
Containers [] WorkspaceAgentContainer `json:"containers"`
2025-02-10 11:29:30 +00:00
// Warnings is a list of warnings that may have occurred during the
// process of listing containers. This should not include fatal errors.
Warnings [] string `json:"warnings,omitempty"`
}
func workspaceAgentContainersLabelFilter ( kvs map [ string ] string ) RequestOption {
return func ( r * http . Request ) {
q := r . URL . Query ()
for k , v := range kvs {
kv := fmt . Sprintf ( "%s=%s" , k , v )
q . Add ( "label" , kv )
}
r . URL . RawQuery = q . Encode ()
}
}
// WorkspaceAgentListContainers returns a list of containers that are currently
// running on a Docker daemon accessible to the workspace agent.
func ( c * Client ) WorkspaceAgentListContainers ( ctx context . Context , agentID uuid . UUID , labels map [ string ] string ) ( WorkspaceAgentListContainersResponse , error ) {
lf := workspaceAgentContainersLabelFilter ( labels )
res , err := c . Request ( ctx , http . MethodGet , fmt . Sprintf ( "/api/v2/workspaceagents/%s/containers" , agentID ), nil , lf )
if err != nil {
return WorkspaceAgentListContainersResponse {}, err
}
defer res . Body . Close ()
if res . StatusCode != http . StatusOK {
return WorkspaceAgentListContainersResponse {}, ReadBodyAsError ( res )
}
var cr WorkspaceAgentListContainersResponse
2026-08-05 16:22:23 -04:00
return cr , ReadBodyAsJSON ( res , & cr )
2025-02-10 11:29:30 +00:00
}
2025-07-14 21:35:35 +01:00
func ( c * Client ) WatchWorkspaceAgentContainers ( ctx context . Context , agentID uuid . UUID ) ( <- chan WorkspaceAgentListContainersResponse , io . Closer , error ) {
reqURL , err := c . URL . Parse ( fmt . Sprintf ( "/api/v2/workspaceagents/%s/containers/watch" , agentID ))
if err != nil {
return nil , nil , err
}
conn , res , err := websocket . Dial ( ctx , reqURL . String (), & websocket . DialOptions {
2025-08-15 12:32:33 +01:00
// We want `NoContextTakeover` compression to balance improving
// bandwidth cost/latency with minimal memory usage overhead.
CompressionMode : websocket . CompressionNoContextTakeover ,
2025-07-14 21:35:35 +01:00
HTTPClient : & http . Client {
Transport : c . HTTPClient . Transport ,
},
2026-03-02 19:32:36 +11:00
HTTPHeader : http . Header {
SessionTokenHeader : [] string { c . SessionToken ()},
},
2025-07-14 21:35:35 +01:00
})
if err != nil {
if res == nil {
return nil , nil , err
}
return nil , nil , ReadBodyAsError ( res )
}
2025-08-15 12:32:33 +01:00
// When a workspace has a few devcontainers running, or a single devcontainer
// has a large amount of apps, then each payload can easily exceed 32KiB.
// We up the limit to 4MiB to give us plenty of headroom for workspaces that
// have lots of dev containers with lots of apps.
conn . SetReadLimit ( 1 << 22 ) // 4MiB
2025-07-14 21:35:35 +01:00
d := wsjson . NewDecoder [ WorkspaceAgentListContainersResponse ]( conn , websocket . MessageText , c . logger )
return d . Chan (), d , nil
}
2025-12-24 12:34:39 +00:00
// WorkspaceAgentDeleteDevcontainer deletes the devcontainer with the given ID.
func ( c * Client ) WorkspaceAgentDeleteDevcontainer ( ctx context . Context , agentID uuid . UUID , devcontainerID string ) error {
res , err := c . Request ( ctx , http . MethodDelete , fmt . Sprintf ( "/api/v2/workspaceagents/%s/containers/devcontainers/%s" , agentID , devcontainerID ), nil )
if err != nil {
return err
}
defer res . Body . Close ()
if res . StatusCode != http . StatusNoContent {
return ReadBodyAsError ( res )
}
return nil
}
2025-05-19 12:56:10 +03:00
// WorkspaceAgentRecreateDevcontainer recreates the devcontainer with the given ID.
2025-06-26 11:41:57 +01:00
func ( c * Client ) WorkspaceAgentRecreateDevcontainer ( ctx context . Context , agentID uuid . UUID , devcontainerID string ) ( Response , error ) {
res , err := c . Request ( ctx , http . MethodPost , fmt . Sprintf ( "/api/v2/workspaceagents/%s/containers/devcontainers/%s/recreate" , agentID , devcontainerID ), nil )
2025-05-19 12:56:10 +03:00
if err != nil {
2025-05-27 19:42:24 +03:00
return Response {}, err
2025-05-19 12:56:10 +03:00
}
defer res . Body . Close ()
2025-05-27 19:42:24 +03:00
if res . StatusCode != http . StatusAccepted {
return Response {}, ReadBodyAsError ( res )
2025-05-19 12:56:10 +03:00
}
2025-05-27 19:42:24 +03:00
var m Response
2026-08-05 16:22:23 -04:00
if err := ReadBodyAsJSON ( res , & m ); err != nil {
2025-05-27 19:42:24 +03:00
return Response {}, xerrors . Errorf ( "decode response body: %w" , err )
}
return m , nil
2025-05-19 12:56:10 +03:00
}
2023-06-28 11:54:13 +03:00
//nolint:revive // Follow is a control flag on the server as well.
2023-07-28 09:57:23 -06:00
func ( c * Client ) WorkspaceAgentLogsAfter ( ctx context . Context , agentID uuid . UUID , after int64 , follow bool ) ( <- chan [] WorkspaceAgentLog , io . Closer , error ) {
2023-06-28 11:54:13 +03:00
var queryParams [] string
2023-03-23 14:09:13 -05:00
if after != 0 {
2023-06-28 11:54:13 +03:00
queryParams = append ( queryParams , fmt . Sprintf ( "after=%d" , after ))
2023-03-23 14:09:13 -05:00
}
2023-06-28 11:54:13 +03:00
if follow {
queryParams = append ( queryParams , "follow" )
}
var query string
if len ( queryParams ) > 0 {
query = "?" + strings . Join ( queryParams , "&" )
}
2023-07-28 09:57:23 -06:00
reqURL , err := c . URL . Parse ( fmt . Sprintf ( "/api/v2/workspaceagents/%s/logs%s" , agentID , query ))
2023-03-23 14:09:13 -05:00
if err != nil {
return nil , nil , err
}
2023-06-28 11:54:13 +03:00
if ! follow {
resp , err := c . Request ( ctx , http . MethodGet , reqURL . String (), nil )
if err != nil {
return nil , nil , xerrors . Errorf ( "execute request: %w" , err )
}
defer resp . Body . Close ()
if resp . StatusCode != http . StatusOK {
return nil , nil , ReadBodyAsError ( resp )
}
2023-07-28 09:57:23 -06:00
var logs [] WorkspaceAgentLog
2026-08-05 16:22:23 -04:00
err = ReadBodyAsJSON ( resp , & logs )
2023-06-28 11:54:13 +03:00
if err != nil {
return nil , nil , xerrors . Errorf ( "decode startup logs: %w" , err )
}
2023-07-28 09:57:23 -06:00
ch := make ( chan [] WorkspaceAgentLog , 1 )
2023-06-28 11:54:13 +03:00
ch <- logs
close ( ch )
return ch , closeFunc ( func () error { return nil }), nil
}
2023-03-23 14:09:13 -05:00
httpClient := & http . Client {
Transport : c . HTTPClient . Transport ,
}
2023-06-28 11:54:13 +03:00
conn , res , err := websocket . Dial ( ctx , reqURL . String (), & websocket . DialOptions {
2026-03-02 19:32:36 +11:00
HTTPClient : httpClient ,
HTTPHeader : http . Header {
SessionTokenHeader : [] string { c . SessionToken ()},
},
2023-03-23 14:09:13 -05:00
CompressionMode : websocket . CompressionDisabled ,
})
if err != nil {
if res == nil {
return nil , nil , err
}
return nil , nil , ReadBodyAsError ( res )
}
2024-12-03 10:12:30 +04:00
d := wsjson . NewDecoder [[] WorkspaceAgentLog ]( conn , websocket . MessageText , c . logger )
return d . Chan (), d , nil
2023-03-23 14:09:13 -05:00
}
2026-03-06 10:47:55 +01:00
// WorkspaceAgentGitClientMessageType represents the type of a client
// message sent to the git watch WebSocket.
type WorkspaceAgentGitClientMessageType string
const (
// WorkspaceAgentGitClientMessageTypeRefresh requests an immediate
// re-scan of all subscribed repositories.
WorkspaceAgentGitClientMessageTypeRefresh WorkspaceAgentGitClientMessageType = "refresh"
)
// WorkspaceAgentGitClientMessage is a message sent from the client to
// the agent over the git watch WebSocket.
type WorkspaceAgentGitClientMessage struct {
Type WorkspaceAgentGitClientMessageType `json:"type"`
}
// WorkspaceAgentGitServerMessageType represents the type of a server
// message sent from the git watch WebSocket.
type WorkspaceAgentGitServerMessageType string
const (
// WorkspaceAgentGitServerMessageTypeChanges contains a delta of
// repository changes since the last emitted update.
WorkspaceAgentGitServerMessageTypeChanges WorkspaceAgentGitServerMessageType = "changes"
// WorkspaceAgentGitServerMessageTypeError signals a server-side
// error.
WorkspaceAgentGitServerMessageTypeError WorkspaceAgentGitServerMessageType = "error"
)
// WorkspaceAgentGitServerMessage is a message sent from the agent to
// the client over the git watch WebSocket.
type WorkspaceAgentGitServerMessage struct {
Type WorkspaceAgentGitServerMessageType `json:"type"`
ScannedAt * time . Time `json:"scanned_at,omitempty" format:"date-time"`
Repositories [] WorkspaceAgentRepoChanges `json:"repositories,omitempty"`
Message string `json:"message,omitempty"`
}
// WorkspaceAgentRepoChanges describes the current state of a single
// git repository's working tree. When Removed is true the repo root
// directory or its .git subdirectory no longer exists; all other
// fields (Branch, RemoteOrigin, UnifiedDiff) are empty/zero.
type WorkspaceAgentRepoChanges struct {
RepoRoot string `json:"repo_root"`
Branch string `json:"branch"`
RemoteOrigin string `json:"remote_origin,omitempty"`
UnifiedDiff string `json:"unified_diff,omitempty"`
Removed bool `json:"removed,omitempty"`
}