fix(gateway): resolve local avatars to URL in HTML injection and RPC
The frontend fix alone wasn't enough because:
1. serveIndexHtml() was injecting the raw avatar filename into HTML
2. agent.identity.get RPC was returning raw filename, overwriting the
HTML-injected value
Now both paths resolve local file avatars (*.png, *.jpg, etc.) to the
/avatar/{agentId} endpoint URL.
This commit is contained in:
@@ -206,11 +206,22 @@ interface ServeIndexHtmlOpts {
|
|||||||
agentId?: string;
|
agentId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function looksLikeLocalAvatarPath(value: string | undefined): boolean {
|
||||||
|
if (!value) return false;
|
||||||
|
if (/^https?:\/\//i.test(value) || /^data:image\//i.test(value)) return false;
|
||||||
|
return /\.(png|jpe?g|gif|webp|svg|ico)$/i.test(value);
|
||||||
|
}
|
||||||
|
|
||||||
function serveIndexHtml(res: ServerResponse, indexPath: string, opts: ServeIndexHtmlOpts) {
|
function serveIndexHtml(res: ServerResponse, indexPath: string, opts: ServeIndexHtmlOpts) {
|
||||||
const { basePath, config, agentId } = opts;
|
const { basePath, config, agentId } = opts;
|
||||||
const identity = config
|
const identity = config
|
||||||
? resolveAssistantIdentity({ cfg: config, agentId })
|
? resolveAssistantIdentity({ cfg: config, agentId })
|
||||||
: DEFAULT_ASSISTANT_IDENTITY;
|
: DEFAULT_ASSISTANT_IDENTITY;
|
||||||
|
// Resolve local file avatars to /avatar/{agentId} URL
|
||||||
|
let avatarValue = identity.avatar;
|
||||||
|
if (looksLikeLocalAvatarPath(avatarValue) && identity.agentId) {
|
||||||
|
avatarValue = buildAvatarUrl(basePath, identity.agentId);
|
||||||
|
}
|
||||||
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
||||||
res.setHeader("Cache-Control", "no-cache");
|
res.setHeader("Cache-Control", "no-cache");
|
||||||
const raw = fs.readFileSync(indexPath, "utf8");
|
const raw = fs.readFileSync(indexPath, "utf8");
|
||||||
@@ -218,7 +229,7 @@ function serveIndexHtml(res: ServerResponse, indexPath: string, opts: ServeIndex
|
|||||||
injectControlUiConfig(raw, {
|
injectControlUiConfig(raw, {
|
||||||
basePath,
|
basePath,
|
||||||
assistantName: identity.name,
|
assistantName: identity.name,
|
||||||
assistantAvatar: identity.avatar,
|
assistantAvatar: avatarValue,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -407,7 +407,18 @@ export const agentHandlers: GatewayRequestHandlers = {
|
|||||||
}
|
}
|
||||||
const cfg = loadConfig();
|
const cfg = loadConfig();
|
||||||
const identity = resolveAssistantIdentity({ cfg, agentId });
|
const identity = resolveAssistantIdentity({ cfg, agentId });
|
||||||
respond(true, identity, undefined);
|
// Resolve local file avatars to /avatar/{agentId} URL
|
||||||
|
let avatarValue = identity.avatar;
|
||||||
|
if (
|
||||||
|
avatarValue &&
|
||||||
|
!/^https?:\/\//i.test(avatarValue) &&
|
||||||
|
!/^data:image\//i.test(avatarValue) &&
|
||||||
|
/\.(png|jpe?g|gif|webp|svg|ico)$/i.test(avatarValue) &&
|
||||||
|
identity.agentId
|
||||||
|
) {
|
||||||
|
avatarValue = `/avatar/${identity.agentId}`;
|
||||||
|
}
|
||||||
|
respond(true, { ...identity, avatar: avatarValue }, undefined);
|
||||||
},
|
},
|
||||||
"agent.wait": async ({ params, respond }) => {
|
"agent.wait": async ({ params, respond }) => {
|
||||||
if (!validateAgentWaitParams(params)) {
|
if (!validateAgentWaitParams(params)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user