Skip to content

Commit d297e17

Browse files
committed
refactor: centralize control ui avatar helpers
1 parent 6a25e23 commit d297e17

8 files changed

Lines changed: 92 additions & 51 deletions

File tree

‎src/commands/onboard-helpers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { ClawdbotConfig } from "../config/config.js";
1010
import { CONFIG_PATH_CLAWDBOT } from "../config/config.js";
1111
import { resolveSessionTranscriptsDirForAgent } from "../config/sessions.js";
1212
import { callGateway } from "../gateway/call.js";
13-
import { normalizeControlUiBasePath } from "../gateway/control-ui.js";
13+
import { normalizeControlUiBasePath } from "../gateway/control-ui-shared.js";
1414
import { isSafeExecutableValue } from "../infra/exec-safety.js";
1515
import { pickPrimaryTailnetIPv4 } from "../infra/tailnet.js";
1616
import { isWSL } from "../infra/wsl.js";

‎src/commands/status-all.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { GatewayService } from "../daemon/service.js";
77
import { resolveGatewayService } from "../daemon/service.js";
88
import { resolveNodeService } from "../daemon/node-service.js";
99
import { buildGatewayConnectionDetails, callGateway } from "../gateway/call.js";
10-
import { normalizeControlUiBasePath } from "../gateway/control-ui.js";
10+
import { normalizeControlUiBasePath } from "../gateway/control-ui-shared.js";
1111
import { probeGateway } from "../gateway/probe.js";
1212
import { collectChannelStatusIssues } from "../infra/channels-status-issues.js";
1313
import { resolveClawdbotPackageRoot } from "../infra/clawdbot-root.js";

‎src/commands/status.scan.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { withProgress } from "../cli/progress.js";
22
import { loadConfig } from "../config/config.js";
33
import { buildGatewayConnectionDetails, callGateway } from "../gateway/call.js";
4-
import { normalizeControlUiBasePath } from "../gateway/control-ui.js";
4+
import { normalizeControlUiBasePath } from "../gateway/control-ui-shared.js";
55
import { probeGateway } from "../gateway/probe.js";
66
import { collectChannelStatusIssues } from "../infra/channels-status-issues.js";
77
import { resolveOsSummary } from "../infra/os-summary.js";

‎src/gateway/control-ui-shared.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const CONTROL_UI_AVATAR_PREFIX = "/avatar";
2+
3+
export function normalizeControlUiBasePath(basePath?: string): string {
4+
if (!basePath) return "";
5+
let normalized = basePath.trim();
6+
if (!normalized) return "";
7+
if (!normalized.startsWith("/")) normalized = `/${normalized}`;
8+
if (normalized === "/") return "";
9+
if (normalized.endsWith("/")) normalized = normalized.slice(0, -1);
10+
return normalized;
11+
}
12+
13+
export function buildControlUiAvatarUrl(basePath: string, agentId: string): string {
14+
return basePath
15+
? `${basePath}${CONTROL_UI_AVATAR_PREFIX}/${agentId}`
16+
: `${CONTROL_UI_AVATAR_PREFIX}/${agentId}`;
17+
}
18+
19+
function looksLikeLocalAvatarPath(value: string): boolean {
20+
if (/[\\/]/.test(value)) return true;
21+
return /\.(png|jpe?g|gif|webp|svg|ico)$/i.test(value);
22+
}
23+
24+
export function resolveAssistantAvatarUrl(params: {
25+
avatar?: string | null;
26+
agentId?: string | null;
27+
basePath?: string;
28+
}): string | undefined {
29+
const avatar = params.avatar?.trim();
30+
if (!avatar) return undefined;
31+
if (/^https?:\/\//i.test(avatar) || /^data:image\//i.test(avatar)) return avatar;
32+
33+
const basePath = normalizeControlUiBasePath(params.basePath);
34+
const baseAvatarPrefix = basePath
35+
? `${basePath}${CONTROL_UI_AVATAR_PREFIX}/`
36+
: `${CONTROL_UI_AVATAR_PREFIX}/`;
37+
if (basePath && avatar.startsWith(`${CONTROL_UI_AVATAR_PREFIX}/`)) {
38+
return `${basePath}${avatar}`;
39+
}
40+
if (avatar.startsWith(baseAvatarPrefix)) return avatar;
41+
42+
if (!params.agentId) return avatar;
43+
if (looksLikeLocalAvatarPath(avatar)) {
44+
return buildControlUiAvatarUrl(basePath, params.agentId);
45+
}
46+
return avatar;
47+
}
48+
49+
export { CONTROL_UI_AVATAR_PREFIX };

‎src/gateway/control-ui.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { resolveAssistantAvatarUrl } from "./control-ui.js";
3+
import {
4+
buildControlUiAvatarUrl,
5+
normalizeControlUiBasePath,
6+
resolveAssistantAvatarUrl,
7+
} from "./control-ui-shared.js";
48

59
describe("resolveAssistantAvatarUrl", () => {
10+
it("normalizes base paths", () => {
11+
expect(normalizeControlUiBasePath()).toBe("");
12+
expect(normalizeControlUiBasePath("")).toBe("");
13+
expect(normalizeControlUiBasePath(" ")).toBe("");
14+
expect(normalizeControlUiBasePath("/")).toBe("");
15+
expect(normalizeControlUiBasePath("ui")).toBe("/ui");
16+
expect(normalizeControlUiBasePath("/ui/")).toBe("/ui");
17+
});
18+
19+
it("builds avatar URLs", () => {
20+
expect(buildControlUiAvatarUrl("", "main")).toBe("/avatar/main");
21+
expect(buildControlUiAvatarUrl("/ui", "main")).toBe("/ui/avatar/main");
22+
});
23+
624
it("keeps remote and data URLs", () => {
725
expect(
826
resolveAssistantAvatarUrl({
@@ -54,6 +72,15 @@ describe("resolveAssistantAvatarUrl", () => {
5472
).toBe("/ui/avatar/main");
5573
});
5674

75+
it("leaves local paths untouched when agentId is missing", () => {
76+
expect(
77+
resolveAssistantAvatarUrl({
78+
avatar: "avatars/me.png",
79+
basePath: "/ui",
80+
}),
81+
).toBe("avatars/me.png");
82+
});
83+
5784
it("keeps short text avatars", () => {
5885
expect(
5986
resolveAssistantAvatarUrl({

‎src/gateway/control-ui.ts‎

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@ import { fileURLToPath } from "node:url";
55

66
import type { ClawdbotConfig } from "../config/config.js";
77
import { DEFAULT_ASSISTANT_IDENTITY, resolveAssistantIdentity } from "./assistant-identity.js";
8+
import {
9+
buildControlUiAvatarUrl,
10+
CONTROL_UI_AVATAR_PREFIX,
11+
normalizeControlUiBasePath,
12+
resolveAssistantAvatarUrl,
13+
} from "./control-ui-shared.js";
814

915
const ROOT_PREFIX = "/";
10-
const AVATAR_PREFIX = "/avatar";
1116

1217
export type ControlUiRequestOptions = {
1318
basePath?: string;
1419
config?: ClawdbotConfig;
1520
agentId?: string;
1621
};
1722

18-
export function normalizeControlUiBasePath(basePath?: string): string {
19-
if (!basePath) return "";
20-
let normalized = basePath.trim();
21-
if (!normalized) return "";
22-
if (!normalized.startsWith("/")) normalized = `/${normalized}`;
23-
if (normalized === "/") return "";
24-
if (normalized.endsWith("/")) normalized = normalized.slice(0, -1);
25-
return normalized;
26-
}
27-
2823
function resolveControlUiRoot(): string | null {
2924
const here = path.dirname(fileURLToPath(import.meta.url));
3025
const execDir = (() => {
@@ -98,10 +93,6 @@ function sendJson(res: ServerResponse, status: number, body: unknown) {
9893
res.end(JSON.stringify(body));
9994
}
10095

101-
export function buildAvatarUrl(basePath: string, agentId: string): string {
102-
return basePath ? `${basePath}${AVATAR_PREFIX}/${agentId}` : `${AVATAR_PREFIX}/${agentId}`;
103-
}
104-
10596
function isValidAgentId(agentId: string): boolean {
10697
return /^[a-z0-9][a-z0-9_-]{0,63}$/i.test(agentId);
10798
}
@@ -118,7 +109,9 @@ export function handleControlUiAvatarRequest(
118109
const url = new URL(urlRaw, "http://localhost");
119110
const basePath = normalizeControlUiBasePath(opts.basePath);
120111
const pathname = url.pathname;
121-
const pathWithBase = basePath ? `${basePath}${AVATAR_PREFIX}/` : `${AVATAR_PREFIX}/`;
112+
const pathWithBase = basePath
113+
? `${basePath}${CONTROL_UI_AVATAR_PREFIX}/`
114+
: `${CONTROL_UI_AVATAR_PREFIX}/`;
122115
if (!pathname.startsWith(pathWithBase)) return false;
123116

124117
const agentIdParts = pathname.slice(pathWithBase.length).split("/").filter(Boolean);
@@ -132,7 +125,7 @@ export function handleControlUiAvatarRequest(
132125
const resolved = opts.resolveAvatar(agentId);
133126
const avatarUrl =
134127
resolved.kind === "local"
135-
? buildAvatarUrl(basePath, agentId)
128+
? buildControlUiAvatarUrl(basePath, agentId)
136129
: resolved.kind === "remote" || resolved.kind === "data"
137130
? resolved.url
138131
: null;
@@ -206,34 +199,6 @@ interface ServeIndexHtmlOpts {
206199
agentId?: string;
207200
}
208201

209-
function looksLikeLocalAvatarPath(value: string): boolean {
210-
if (/[\\/]/.test(value)) return true;
211-
return /\.(png|jpe?g|gif|webp|svg|ico)$/i.test(value);
212-
}
213-
214-
export function resolveAssistantAvatarUrl(params: {
215-
avatar?: string | null;
216-
agentId?: string | null;
217-
basePath?: string;
218-
}): string | undefined {
219-
const avatar = params.avatar?.trim();
220-
if (!avatar) return undefined;
221-
if (/^https?:\/\//i.test(avatar) || /^data:image\//i.test(avatar)) return avatar;
222-
223-
const basePath = normalizeControlUiBasePath(params.basePath);
224-
const baseAvatarPrefix = basePath ? `${basePath}${AVATAR_PREFIX}/` : `${AVATAR_PREFIX}/`;
225-
if (basePath && avatar.startsWith(`${AVATAR_PREFIX}/`)) {
226-
return `${basePath}${avatar}`;
227-
}
228-
if (avatar.startsWith(baseAvatarPrefix)) return avatar;
229-
230-
if (!params.agentId) return avatar;
231-
if (looksLikeLocalAvatarPath(avatar)) {
232-
return buildAvatarUrl(basePath, params.agentId);
233-
}
234-
return avatar;
235-
}
236-
237202
function serveIndexHtml(res: ServerResponse, indexPath: string, opts: ServeIndexHtmlOpts) {
238203
const { basePath, config, agentId } = opts;
239204
const identity = config

‎src/gateway/server-methods/agent.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import { loadSessionEntry } from "../session-utils.js";
3939
import { formatForLog } from "../ws-log.js";
4040
import { resolveAssistantIdentity } from "../assistant-identity.js";
41-
import { resolveAssistantAvatarUrl } from "../control-ui.js";
41+
import { resolveAssistantAvatarUrl } from "../control-ui-shared.js";
4242
import { waitForAgentJob } from "./agent-job.js";
4343
import type { GatewayRequestHandlers } from "./types.js";
4444

‎src/gateway/server-runtime-config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type ResolvedGatewayAuth,
1010
resolveGatewayAuth,
1111
} from "./auth.js";
12-
import { normalizeControlUiBasePath } from "./control-ui.js";
12+
import { normalizeControlUiBasePath } from "./control-ui-shared.js";
1313
import { resolveHooksConfig } from "./hooks.js";
1414
import { isLoopbackHost, resolveGatewayBindHost } from "./net.js";
1515

0 commit comments

Comments
 (0)