Skip to content

Commit cbaa058

Browse files
fix(cloud): return workspace-scoped connect URL with tokens (anomalyco#641)
1 parent fe9894f commit cbaa058

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎packages/web/components/cloud-control.tsx‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type WorkerSummary = {
3535
type WorkerTokens = {
3636
clientToken: string | null;
3737
hostToken: string | null;
38+
openworkUrl: string | null;
39+
workspaceId: string | null;
3840
};
3941

4042
type WorkerListItem = {
@@ -185,14 +187,17 @@ function getWorkerTokens(payload: unknown): WorkerTokens | null {
185187
}
186188

187189
const tokens = payload.tokens;
190+
const connect = isRecord(payload.connect) ? payload.connect : null;
188191
const clientToken = typeof tokens.client === "string" ? tokens.client : null;
189192
const hostToken = typeof tokens.host === "string" ? tokens.host : null;
193+
const openworkUrl = connect && typeof connect.openworkUrl === "string" ? connect.openworkUrl : null;
194+
const workspaceId = connect && typeof connect.workspaceId === "string" ? connect.workspaceId : null;
190195

191196
if (!clientToken && !hostToken) {
192197
return null;
193198
}
194199

195-
return { clientToken, hostToken };
200+
return { clientToken, hostToken, openworkUrl, workspaceId };
196201
}
197202

198203
function parseWorkerListItem(value: unknown): WorkerListItem | null {
@@ -525,6 +530,16 @@ export function CloudControlPanel() {
525530
}
526531

527532
async function withResolvedOpenworkCredentials(candidate: WorkerLaunch, options: { quiet?: boolean } = {}) {
533+
const existingConnectUrl = candidate.openworkUrl?.trim() ?? "";
534+
const existingWorkspaceId = candidate.workspaceId?.trim() ?? "";
535+
if (existingConnectUrl && existingWorkspaceId) {
536+
return {
537+
...candidate,
538+
openworkUrl: existingConnectUrl,
539+
workspaceId: existingWorkspaceId
540+
};
541+
}
542+
528543
const instanceUrl = candidate.instanceUrl?.trim() ?? "";
529544
if (!instanceUrl) {
530545
return {
@@ -1018,6 +1033,8 @@ export function CloudControlPanel() {
10181033
worker && worker.workerId === id
10191034
? {
10201035
...worker,
1036+
openworkUrl: tokens.openworkUrl ?? worker.openworkUrl,
1037+
workspaceId: tokens.workspaceId ?? worker.workspaceId,
10211038
clientToken: tokens.clientToken,
10221039
hostToken: tokens.hostToken
10231040
}
@@ -1027,8 +1044,8 @@ export function CloudControlPanel() {
10271044
status: "unknown",
10281045
provider: null,
10291046
instanceUrl: null,
1030-
openworkUrl: null,
1031-
workspaceId: null,
1047+
openworkUrl: tokens.openworkUrl,
1048+
workspaceId: tokens.workspaceId,
10321049
clientToken: tokens.clientToken,
10331050
hostToken: tokens.hostToken
10341051
};

‎services/den/src/http/workers.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,72 @@ const token = () => randomBytes(32).toString("hex")
2929
type WorkerRow = typeof WorkerTable.$inferSelect
3030
type WorkerInstanceRow = typeof WorkerInstanceTable.$inferSelect
3131

32+
function isRecord(value: unknown): value is Record<string, unknown> {
33+
return typeof value === "object" && value !== null
34+
}
35+
36+
function normalizeUrl(value: string): string {
37+
return value.trim().replace(/\/+$/, "")
38+
}
39+
40+
function parseWorkspaceSelection(payload: unknown): { workspaceId: string; openworkUrl: string } | null {
41+
if (!isRecord(payload) || !Array.isArray(payload.items)) {
42+
return null
43+
}
44+
45+
const activeId = typeof payload.activeId === "string" ? payload.activeId : null
46+
let workspaceId = activeId
47+
48+
if (!workspaceId) {
49+
for (const item of payload.items) {
50+
if (isRecord(item) && typeof item.id === "string" && item.id.trim()) {
51+
workspaceId = item.id
52+
break
53+
}
54+
}
55+
}
56+
57+
const baseUrl = typeof payload.baseUrl === "string" ? normalizeUrl(payload.baseUrl) : ""
58+
if (!workspaceId || !baseUrl) {
59+
return null
60+
}
61+
62+
return {
63+
workspaceId,
64+
openworkUrl: `${baseUrl}/w/${encodeURIComponent(workspaceId)}`,
65+
}
66+
}
67+
68+
async function resolveConnectUrlFromWorker(instanceUrl: string, clientToken: string) {
69+
const baseUrl = normalizeUrl(instanceUrl)
70+
if (!baseUrl || !clientToken.trim()) {
71+
return null
72+
}
73+
74+
try {
75+
const response = await fetch(`${baseUrl}/workspaces`, {
76+
method: "GET",
77+
headers: {
78+
Accept: "application/json",
79+
Authorization: `Bearer ${clientToken.trim()}`,
80+
},
81+
})
82+
83+
if (!response.ok) {
84+
return null
85+
}
86+
87+
const payload = (await response.json()) as unknown
88+
const selected = parseWorkspaceSelection({
89+
...(isRecord(payload) ? payload : {}),
90+
baseUrl,
91+
})
92+
return selected
93+
} catch {
94+
return null
95+
}
96+
}
97+
3298
async function requireSession(req: express.Request, res: express.Response) {
3399
const session = await auth.api.getSession({
34100
headers: fromNodeHeaders(req.headers),
@@ -335,10 +401,14 @@ workersRouter.post("/:id/tokens", async (req, res) => {
335401
return
336402
}
337403

404+
const instance = await getLatestWorkerInstance(rows[0].id)
405+
const connect = instance?.url ? await resolveConnectUrlFromWorker(instance.url, clientToken) : null
406+
338407
res.json({
339408
tokens: {
340409
host: hostToken,
341410
client: clientToken,
342411
},
412+
connect: connect ?? (instance?.url ? { openworkUrl: instance.url, workspaceId: null } : null),
343413
})
344414
})

0 commit comments

Comments
 (0)