Skip to content

Commit 8f8773f

Browse files
fix(app): scope identities by workspace URL and ID (anomalyco#539)
1 parent b820867 commit 8f8773f

6 files changed

Lines changed: 100 additions & 15 deletions

File tree

114 KB
Loading
113 KB
Loading

‎packages/app/src/app/app.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ import {
132132
type OwpenbotInfo,
133133
} from "./lib/tauri";
134134
import {
135+
parseOpenworkWorkspaceIdFromUrl,
135136
createOpenworkServerClient,
136137
hydrateOpenworkServerSettingsFromEnv,
137138
normalizeOpenworkServerUrl,
@@ -1820,14 +1821,19 @@ export default function App() {
18201821
createEffect(() => {
18211822
const active = workspaceStore.activeWorkspaceDisplay();
18221823
const client = openworkServerClient();
1824+
const openworkUrl = openworkServerUrl().trim();
18231825

18241826
if (!client || openworkServerStatus() !== "connected") {
18251827
setOpenworkServerWorkspaceId(null);
18261828
return;
18271829
}
18281830

18291831
if (active.workspaceType === "remote" && active.remoteType === "openwork") {
1830-
const storedId = active.openworkWorkspaceId ?? null;
1832+
const inferredWorkspaceId =
1833+
parseOpenworkWorkspaceIdFromUrl(active.openworkHostUrl ?? "") ??
1834+
parseOpenworkWorkspaceIdFromUrl(active.baseUrl ?? "") ??
1835+
parseOpenworkWorkspaceIdFromUrl(openworkUrl);
1836+
const storedId = active.openworkWorkspaceId?.trim() || inferredWorkspaceId || null;
18311837
if (storedId) {
18321838
setOpenworkServerWorkspaceId(storedId);
18331839
return;
@@ -1838,8 +1844,15 @@ export default function App() {
18381844
try {
18391845
const response = await client.listWorkspaces();
18401846
if (cancelled) return;
1841-
const match = response.items?.[0];
1842-
setOpenworkServerWorkspaceId(match?.id ?? null);
1847+
const items = Array.isArray(response.items) ? response.items : [];
1848+
const directoryHint = normalizeDirectoryPath(active.directory?.trim() ?? active.path?.trim() ?? "");
1849+
const match = directoryHint
1850+
? items.find((entry) => {
1851+
const entryPath = normalizeDirectoryPath((entry.opencode?.directory ?? entry.directory ?? entry.path ?? "").trim());
1852+
return Boolean(entryPath && entryPath === directoryHint);
1853+
})
1854+
: (response.activeId ? items.find((entry) => entry.id === response.activeId) : null) ?? items[0];
1855+
setOpenworkServerWorkspaceId(match?.id ?? response.activeId ?? null);
18431856
} catch {
18441857
if (!cancelled) setOpenworkServerWorkspaceId(null);
18451858
}

‎packages/app/src/app/lib/openwork-server.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,28 @@ export function normalizeOpenworkServerUrl(input: string) {
357357
return withProtocol.replace(/\/+$/, "");
358358
}
359359

360+
export function parseOpenworkWorkspaceIdFromUrl(input: string) {
361+
const normalized = normalizeOpenworkServerUrl(input) ?? "";
362+
if (!normalized) return null;
363+
364+
try {
365+
const url = new URL(normalized);
366+
const segments = url.pathname.split("/").filter(Boolean);
367+
const last = segments[segments.length - 1] ?? "";
368+
const prev = segments[segments.length - 2] ?? "";
369+
if (prev !== "w" || !last) return null;
370+
return decodeURIComponent(last);
371+
} catch {
372+
const match = normalized.match(/\/w\/([^/?#]+)/);
373+
if (!match?.[1]) return null;
374+
try {
375+
return decodeURIComponent(match[1]);
376+
} catch {
377+
return match[1];
378+
}
379+
}
380+
}
381+
360382
export function buildOpenworkWorkspaceBaseUrl(hostUrl: string, workspaceId?: string | null) {
361383
const normalized = normalizeOpenworkServerUrl(hostUrl) ?? "";
362384
if (!normalized) return null;

‎packages/app/src/app/pages/config.tsx‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TextInput from "../components/text-input";
77

88
import { RefreshCcw } from "lucide-solid";
99

10+
import { buildOpenworkWorkspaceBaseUrl, parseOpenworkWorkspaceIdFromUrl } from "../lib/openwork-server";
1011
import type { OpenworkServerSettings, OpenworkServerStatus } from "../lib/openwork-server";
1112
import type { OpenworkServerInfo } from "../lib/tauri";
1213

@@ -108,6 +109,18 @@ export default function ConfigView(props: ConfigViewProps) {
108109
return openworkUrl().trim() !== currentUrl || openworkToken().trim() !== currentToken;
109110
});
110111

112+
const resolvedWorkspaceId = createMemo(() => {
113+
const explicitId = props.openworkServerWorkspaceId?.trim() ?? "";
114+
if (explicitId) return explicitId;
115+
return parseOpenworkWorkspaceIdFromUrl(openworkUrl()) ?? "";
116+
});
117+
118+
const resolvedWorkspaceUrl = createMemo(() => {
119+
const baseUrl = openworkUrl().trim();
120+
if (!baseUrl) return "";
121+
return buildOpenworkWorkspaceBaseUrl(baseUrl, resolvedWorkspaceId()) ?? baseUrl;
122+
});
123+
111124
const hostInfo = createMemo(() => props.openworkServerHostInfo);
112125
const hostStatusLabel = createMemo(() => {
113126
if (!hostInfo()?.running) return "Offline";
@@ -454,7 +467,10 @@ export default function ConfigView(props: ConfigViewProps) {
454467
</label>
455468
</div>
456469

457-
<div class="text-[11px] text-gray-7 font-mono truncate">Resolved server: {openworkUrl().trim() || "Not set"}</div>
470+
<div class="space-y-1">
471+
<div class="text-[11px] text-gray-7 font-mono truncate">Resolved workspace URL: {resolvedWorkspaceUrl() || "Not set"}</div>
472+
<div class="text-[11px] text-gray-8 font-mono truncate">Workspace ID: {resolvedWorkspaceId() || "Unavailable"}</div>
473+
</div>
458474

459475
<div class="flex flex-wrap gap-2">
460476
<Button

‎packages/app/src/app/pages/identities.tsx‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
} from "lucide-solid";
1010

1111
import Button from "../components/button";
12-
import { createOpenworkServerClient, OpenworkServerError } from "../lib/openwork-server";
12+
import {
13+
buildOpenworkWorkspaceBaseUrl,
14+
createOpenworkServerClient,
15+
OpenworkServerError,
16+
parseOpenworkWorkspaceIdFromUrl,
17+
} from "../lib/openwork-server";
1318
import type {
1419
OpenworkOwpenbotHealthSnapshot,
1520
OpenworkOwpenbotIdentityItem,
@@ -122,21 +127,34 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
122127

123128
const [expandedChannel, setExpandedChannel] = createSignal<string | null>(null);
124129

125-
const openworkServerClient = createMemo(() => {
130+
const workspaceId = createMemo(() => {
131+
const explicitId = props.openworkServerWorkspaceId?.trim() ?? "";
132+
if (explicitId) return explicitId;
133+
return parseOpenworkWorkspaceIdFromUrl(props.openworkServerUrl) ?? "";
134+
});
135+
136+
const scopedOpenworkBaseUrl = createMemo(() => {
126137
const baseUrl = props.openworkServerUrl.trim();
138+
if (!baseUrl) return "";
139+
return buildOpenworkWorkspaceBaseUrl(baseUrl, workspaceId()) ?? baseUrl;
140+
});
141+
142+
const openworkServerClient = createMemo(() => {
143+
const baseUrl = scopedOpenworkBaseUrl().trim();
127144
const localBaseUrl = props.openworkServerHostInfo?.baseUrl?.trim() ?? "";
145+
const localScopedBaseUrl = buildOpenworkWorkspaceBaseUrl(localBaseUrl, workspaceId()) ?? localBaseUrl;
128146
const hostToken = props.openworkServerHostInfo?.hostToken?.trim() ?? "";
129147
const clientToken = props.openworkServerHostInfo?.clientToken?.trim() ?? "";
130148
const settingsToken = props.openworkServerSettings.token?.trim() ?? "";
131149

132-
const isLocalServer = localBaseUrl && baseUrl === localBaseUrl;
150+
const isLocalServer = Boolean(localBaseUrl) && (baseUrl === localBaseUrl || baseUrl === localScopedBaseUrl);
133151
const token = isLocalServer ? (clientToken || settingsToken) : (settingsToken || clientToken);
134152
if (!baseUrl || !token) return null;
135153
return createOpenworkServerClient({ baseUrl, token, hostToken: isLocalServer ? hostToken : undefined });
136154
});
137155

138156
const serverReady = createMemo(() => props.openworkServerStatus === "connected" && Boolean(openworkServerClient()));
139-
const workspaceId = createMemo(() => props.openworkServerWorkspaceId?.trim() || "");
157+
const scopedWorkspaceReady = createMemo(() => Boolean(workspaceId()));
140158

141159
let lastResetKey = "";
142160

@@ -167,21 +185,29 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
167185
if (!serverReady()) return;
168186
const client = openworkServerClient();
169187
if (!client) return;
188+
const id = workspaceId();
170189

171190
setRefreshing(true);
172191
try {
173192
setHealthError(null);
174193
setTelegramIdentitiesError(null);
175194
setSlackIdentitiesError(null);
176195

196+
if (!id) {
197+
setHealth(null);
198+
setTelegramIdentities([]);
199+
setSlackIdentities([]);
200+
setHealthError("Workspace scope unavailable. Reconnect using a workspace URL or switch to a known workspace.");
201+
setTelegramIdentitiesError("Workspace scope unavailable.");
202+
setSlackIdentitiesError("Workspace scope unavailable.");
203+
setLastUpdatedAt(null);
204+
return;
205+
}
206+
177207
const [healthRes, tgRes, slackRes] = await Promise.all([
178208
client.owpenbotHealth(),
179-
workspaceId()
180-
? client.getOwpenbotTelegramIdentities(workspaceId())
181-
: client.owpenbotTelegramIdentities().then((raw) => raw.json as unknown),
182-
workspaceId()
183-
? client.getOwpenbotSlackIdentities(workspaceId())
184-
: client.owpenbotSlackIdentities().then((raw) => raw.json as unknown),
209+
client.getOwpenbotTelegramIdentities(id),
210+
client.getOwpenbotSlackIdentities(id),
185211
]);
186212

187213
if (isOwpenbotSnapshot(healthRes.json)) {
@@ -359,7 +385,7 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
359385
};
360386

361387
createEffect(() => {
362-
const baseUrl = props.openworkServerUrl.trim();
388+
const baseUrl = scopedOpenworkBaseUrl().trim();
363389
const id = workspaceId();
364390
const nextKey = `${baseUrl}|${id}`;
365391
if (nextKey === lastResetKey) return;
@@ -405,6 +431,9 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
405431
Let people reach your worker through messaging apps. Connect a channel and
406432
your worker will automatically read and respond to messages.
407433
</p>
434+
<div class="mt-1.5 text-[11px] text-gray-8 font-mono truncate">
435+
Workspace scope: {scopedOpenworkBaseUrl().trim() || props.openworkServerUrl.trim() || "Not set"}
436+
</div>
408437
</div>
409438

410439
{/* ---- Not connected to server ---- */}
@@ -418,6 +447,11 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
418447
</Show>
419448

420449
<Show when={serverReady()}>
450+
<Show when={!scopedWorkspaceReady()}>
451+
<div class="rounded-xl border border-amber-7/20 bg-amber-1/30 px-3 py-2 text-xs text-amber-12">
452+
Workspace ID is required to manage identities. Reconnect with a workspace URL (for example: <code class="text-[11px]">/w/&lt;workspace-id&gt;</code>) or select a workspace mapped on this host.
453+
</div>
454+
</Show>
421455

422456
{/* ---- Worker status card ---- */}
423457
<div class="rounded-xl border border-gray-4 bg-gray-1 p-4 space-y-3.5">

0 commit comments

Comments
 (0)