Skip to content

Commit c09970f

Browse files
committed
Add dev connection message
1 parent bbf2597 commit c09970f

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { prisma } from "~/db.server";
4+
import { devPresence } from "~/presenters/v3/DevPresence.server";
5+
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
6+
import { getEnvironmentFromEnv } from "./api.v1.projects.$projectRef.$env";
7+
8+
const ParamsSchema = z.object({
9+
projectRef: z.string(),
10+
});
11+
12+
type ParamsSchema = z.infer<typeof ParamsSchema>;
13+
14+
export async function loader({ request, params }: LoaderFunctionArgs) {
15+
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
16+
17+
if (!authenticationResult) {
18+
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
19+
}
20+
21+
const parsedParams = ParamsSchema.safeParse(params);
22+
23+
if (!parsedParams.success) {
24+
return json({ error: "Invalid Params" }, { status: 400 });
25+
}
26+
27+
const { projectRef } = parsedParams.data;
28+
29+
const project = await prisma.project.findFirst({
30+
where: {
31+
externalRef: projectRef,
32+
organization: {
33+
members: {
34+
some: {
35+
userId: authenticationResult.userId,
36+
},
37+
},
38+
},
39+
},
40+
});
41+
42+
if (!project) {
43+
return json({ error: "Project not found" }, { status: 404 });
44+
}
45+
46+
const envResult = await getEnvironmentFromEnv({
47+
projectId: project.id,
48+
userId: authenticationResult.userId,
49+
env: "dev",
50+
});
51+
52+
if (!envResult.success) {
53+
return json({ error: envResult.error }, { status: 404 });
54+
}
55+
56+
const runtimeEnv = envResult.environment;
57+
58+
const isConnected = await devPresence.isConnected(runtimeEnv.id);
59+
60+
return json({ isConnected });
61+
}

packages/cli-v3/src/apiClient.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ export class CliApiClient {
196196
);
197197
}
198198

199+
async getDevStatus(projectRef: string) {
200+
if (!this.accessToken) {
201+
throw new Error("getJWT: No access token");
202+
}
203+
204+
return wrapZodFetch(
205+
z.object({ isConnected: z.boolean() }),
206+
`${this.apiURL}/api/v1/projects/${projectRef}/dev-status`,
207+
{
208+
headers: this.getHeaders(),
209+
}
210+
);
211+
}
212+
199213
async createBackgroundWorker(projectRef: string, body: CreateBackgroundWorkerRequestBody) {
200214
if (!this.accessToken) {
201215
throw new Error("createBackgroundWorker: No access token");

packages/cli-v3/src/mcp/tools.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,32 @@ export function registerTriggerTaskTool(context: McpContext) {
478478

479479
const taskRunUrl = `${auth.dashboardUrl}/projects/v3/${$projectRef}/runs/${result.id}`;
480480

481-
return {
482-
content: [{ type: "text", text: JSON.stringify({ ...result, taskRunUrl }, null, 2) }],
483-
};
481+
if (environment === "dev") {
482+
const cliApiClient = new CliApiClient(auth.auth.apiUrl, auth.auth.accessToken);
483+
484+
const devStatus = await cliApiClient.getDevStatus($projectRef);
485+
const isConnected = devStatus.success ? devStatus.data.isConnected : false;
486+
const connectionMessage = isConnected
487+
? undefined
488+
: "The dev CLI is not connected to this project, because it is not currently running. Make sure to run the dev command to execute triggered tasks.";
489+
490+
if (connectionMessage) {
491+
return {
492+
content: [
493+
{ type: "text", text: JSON.stringify({ ...result, taskRunUrl }, null, 2) },
494+
{ type: "text", text: connectionMessage },
495+
],
496+
};
497+
} else {
498+
return {
499+
content: [{ type: "text", text: JSON.stringify({ ...result, taskRunUrl }, null, 2) }],
500+
};
501+
}
502+
} else {
503+
return {
504+
content: [{ type: "text", text: JSON.stringify({ ...result, taskRunUrl }, null, 2) }],
505+
};
506+
}
484507
}
485508
);
486509
}

0 commit comments

Comments
 (0)