Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: support devcontainer agents in ui and unify backend#18332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 29 commits intomainfrommafredri/feat-agent-devcontainer-injection-6
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
29 commits
Select commitHold shift + click to select a range
765c2cf
backend
mafredriJun 10, 2025
4019358
ui-1
mafredriJun 10, 2025
452dbc9
backend-2
mafredriJun 10, 2025
6a23998
ui-2
mafredriJun 10, 2025
ee3ed36
backend-3
mafredriJun 11, 2025
fc1a90a
ui-3
mafredriJun 11, 2025
88ce78f
ui-wip
mafredriJun 11, 2025
b1431a3
ui-wip2
mafredriJun 13, 2025
2c2bf28
backend fix test after rebase
mafredriJun 13, 2025
18e1593
ui-final?
mafredriJun 13, 2025
df28ff9
oh no I accidentally the coercion
mafredriJun 13, 2025
9f69f69
site: remove circ dependency
mafredriJun 13, 2025
ecfe483
site: add tests
mafredriJun 13, 2025
71c61b6
whelp
mafredriJun 13, 2025
2e1c31f
tweak app display
mafredriJun 13, 2025
7e41c15
add port forward test
mafredriJun 13, 2025
79e1844
cleanup
mafredriJun 13, 2025
8ce0aec
dont show content (due to margin) when there are no apps
mafredriJun 16, 2025
d3bda05
rewrite styles as Tailwind CSS
mafredriJun 16, 2025
d4f208b
review comments
mafredriJun 16, 2025
7e5ede0
review comments
mafredriJun 16, 2025
7a3674a
add comment about idempotency
mafredriJun 16, 2025
c0607b1
switch -> if
mafredriJun 16, 2025
e1ea4bf
rename apps to agentapps
mafredriJun 16, 2025
f150bad
refactor to use mutation
mafredriJun 16, 2025
2b22ee2
adjust refetch trigger
mafredriJun 17, 2025
4b9d218
fix linter
mafredriJun 17, 2025
e021c8d
Update agent/agentcontainers/api.go
mafredriJun 17, 2025
0c44de8
clarify todo
mafredriJun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
refactor to use mutation
  • Loading branch information
@mafredri
mafredri committedJun 16, 2025
commitf150badda1b892e9d1fcec22f1a13fac4668800e
7 changes: 2 additions & 5 deletionsagent/agentcontainers/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -663,11 +663,7 @@ func (api *API) getContainers() (codersdk.WorkspaceAgentListContainersResponse,
for _, dc := range api.knownDevcontainers {
// Include the agent if it's been created (we're iterating over
// copies, so mutating is fine).
//
// NOTE(mafredri): We could filter on "proc.containerID == dc.Container.ID"
// here but not doing so allows us to do some tricks in the UI to
// make the experience more responsive for now.
if proc := api.injectedSubAgentProcs[dc.WorkspaceFolder]; proc.agent.ID != uuid.Nil {
if proc := api.injectedSubAgentProcs[dc.WorkspaceFolder]; proc.agent.ID != uuid.Nil && dc.Container != nil && proc.containerID == dc.Container.ID {
dc.Agent = &codersdk.WorkspaceAgentDevcontainerAgent{
ID: proc.agent.ID,
Name: proc.agent.Name,
Expand DownExpand Up@@ -762,6 +758,7 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
// Update the status so that we don't try to recreate the
// devcontainer multiple times in parallel.
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
dc.Container = nil
api.knownDevcontainers[dc.WorkspaceFolder] = dc
api.asyncWg.Add(1)
go api.recreateDevcontainer(dc, configPath)
Expand Down
148 changes: 93 additions & 55 deletionssite/src/modules/resources/AgentDevcontainerCard.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@ import type {
Workspace,
WorkspaceAgent,
WorkspaceAgentDevcontainer,
WorkspaceAgentListContainersResponse,
} from "api/typesGenerated";
import { Button } from "components/Button/Button";
import { displayError } from "components/GlobalSnackbar/utils";
Expand All@@ -20,7 +21,8 @@ import { Container, ExternalLinkIcon } from "lucide-react";
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
import { AppStatuses } from "pages/WorkspacePage/AppStatuses";
import type { FC } from "react";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import { useMutation, useQueryClient } from "react-query";
import { portForwardURL } from "utils/portForward";
import { AgentApps, organizeAgentApps } from "./AgentApps/AgentApps";
import { AgentButton } from "./AgentButton";
Expand DownExpand Up@@ -51,12 +53,7 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
}) => {
const { browser_only } = useFeatureVisibility();
const { proxy } = useProxy();

const [isRebuilding, setIsRebuilding] = useState(false);

// Track sub agent removal state to improve UX. This will not be needed once
// the devcontainer and agent responses are aligned.
const [subAgentRemoved, setSubAgentRemoved] = useState(false);
const queryClient = useQueryClient();

// The sub agent comes from the workspace response whereas the devcontainer
// comes from the agent containers endpoint. We need alignment between the
Expand All@@ -80,64 +77,105 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
showVSCode ||
appSections.some((it) => it.apps.length > 0);

const showDevcontainerControls =
!subAgentRemoved && subAgent && devcontainer.container;
const showSubAgentApps =
!subAgentRemoved && subAgent?.status === "connected" && hasAppsToDisplay;
const showSubAgentAppsPlaceholders =
subAgentRemoved || subAgent?.status === "connecting";

const handleRebuildDevcontainer = async () => {
setIsRebuilding(true);
setSubAgentRemoved(true);
let rebuildSucceeded = false;
try {
const rebuildDevcontainerMutation = useMutation({
mutationFn: async () => {
const response = await fetch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In the FE, we handle requests in two ways: queries or mutations. In this case, you should wrap this request into a mutation.Here is the docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When using a mutation, you can invalidate other queries or update their data to reflect the most recent status, instead of doing it manually, so you wouldn't need the isRebuilding or subAgentRemoved statuses.Here is how you can do it. If you need more examples, you can search in the codebase for "invalidateQueries".

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks for the tip! This was superb, actually solved a couple of issues I wanted to address but didn't know how!

f150bad (#18332)

BrunoQuaresma reacted with heart emoji
`/api/v2/workspaceagents/${parentAgent.id}/containers/devcontainers/container/${devcontainer.container?.id}/recreate`,
{
method: "POST",
},
{ method: "POST" },
);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(
errorData.message || `Failed torecreate: ${response.statusText}`,
errorData.message || `Failed torebuild: ${response.statusText}`,
);
}
// If the request was accepted (e.g. 202), we mark it as succeeded.
// Once complete, the component will unmount, so the spinner will
// disappear with it.
if (response.status === 202) {
rebuildSucceeded = true;
return response;
},
onMutate: async () => {
await queryClient.cancelQueries({
queryKey: ["agents", parentAgent.id, "containers"],
});

// Snapshot the previous data for rollback in case of error.
const previousData = queryClient.getQueryData([
"agents",
parentAgent.id,
"containers",
]);

// Optimistically update the devcontainer status to
// "starting" and zero the agent and container to mimic what
// the API does.
queryClient.setQueryData(
["agents", parentAgent.id, "containers"],
(oldData?: WorkspaceAgentListContainersResponse) => {
if (!oldData?.devcontainers) return oldData;
return {
...oldData,
devcontainers: oldData.devcontainers.map((dc) => {
if (dc.id === devcontainer.id) {
return {
...dc,
agent: null,
container: null,
status: "starting",
};
}
return dc;
}),
};
},
);

return { previousData };
},
Comment on lines +94 to +131
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@BrunoQuaresma should I keep or remove this? It essentially just leads to a slightly faster UI response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It is up to you! If you think this faster UI experience, worth the amount of code/complexity, go for it.

Copy link
MemberAuthor

@mafredrimafredriJun 16, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ok, I think it's a bit better and without it's dependent on ping between user <-> coderd <-> agent, so let's keep for now. Pretty easy to eliminate if it becomes a burden. 👍🏻

onSuccess: async () => {
// Invalidate the containers query to refetch updated data.
await queryClient.invalidateQueries({
queryKey: ["agents", parentAgent.id, "containers"],
});
},
onError: (error, _, context) => {
// If the mutation fails, use the context returned from
// onMutate to roll back.
if (context?.previousData) {
queryClient.setQueryData(
["agents", parentAgent.id, "containers"],
context.previousData,
);
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "An unknown error occurred.";
displayError(`Failed to recreate devcontainer: ${errorMessage}`);
console.error("Failed to recreate devcontainer:", error);
} finally {
if (!rebuildSucceeded) {
setIsRebuilding(false);
}
}
};
displayError(`Failed to rebuild devcontainer: ${errorMessage}`);
console.error("Failed to rebuild devcontainer:", error);
},
});

// Re-fetch containers when the subAgent changes to ensure data is
// in sync.
const latestSubAgentByName = subAgents.find(
(agent) => agent.name === devcontainer.name,
);
useEffect(() => {
if (subAgent?.id) {
setSubAgentRemoved(false);
} else {
setSubAgentRemoved(true);
if (!latestSubAgentByName) {
return;
}
}, [subAgent?.id]);
queryClient.invalidateQueries({
queryKey: ["agents", parentAgent.id, "containers"],
});
}, [latestSubAgentByName, queryClient, parentAgent.id]);

// If the devcontainer is starting, reflect this in the recreate button.
useEffect(() => {
if (devcontainer.status === "starting") {
setIsRebuilding(true);
} else {
setIsRebuilding(false);
}
}, [devcontainer]);
const showDevcontainerControls = subAgent && devcontainer.container;
const showSubAgentApps =
devcontainer.status !== "starting" &&
subAgent?.status === "connected" &&
hasAppsToDisplay;
const showSubAgentAppsPlaceholders =
devcontainer.status === "starting" || subAgent?.status === "connecting";

const handleRebuildDevcontainer = () => {
rebuildDevcontainerMutation.mutate();
};

const appsClasses = "flex flex-wrap gap-4 empty:hidden md:justify-start";

Expand DownExpand Up@@ -172,15 +210,15 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
md:overflow-visible"
>
{subAgent?.name ?? devcontainer.name}
{!isRebuilding &&devcontainer.container && (
{devcontainer.container && (
<span className="text-content-tertiary">
{" "}
({devcontainer.container.name})
</span>
)}
</span>
</div>
{!subAgentRemoved &&subAgent?.status === "connected" && (
{subAgent?.status === "connected" && (
<>
<SubAgentOutdatedTooltip
devcontainer={devcontainer}
Expand All@@ -190,7 +228,7 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
<AgentLatency agent={subAgent} />
</>
)}
{!subAgentRemoved &&subAgent?.status === "connecting" && (
{subAgent?.status === "connecting" && (
<>
<Skeleton width={160} variant="text" />
<Skeleton width={36} variant="text" />
Expand All@@ -203,9 +241,9 @@ export const AgentDevcontainerCard: FC<AgentDevcontainerCardProps> = ({
variant="outline"
size="sm"
onClick={handleRebuildDevcontainer}
disabled={isRebuilding}
disabled={devcontainer.status === "starting"}
>
<Spinner loading={isRebuilding} />
<Spinner loading={devcontainer.status === "starting"} />
Rebuild
</Button>

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp