@@ -5,26 +5,21 @@ import { fileURLToPath } from "node:url";
55
66import type { ClawdbotConfig } from "../config/config.js" ;
77import { 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
915const ROOT_PREFIX = "/" ;
10- const AVATAR_PREFIX = "/avatar" ;
1116
1217export 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-
2823function 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-
10596function isValidAgentId ( agentId : string ) : boolean {
10697 return / ^ [ a - z 0 - 9 ] [ a - z 0 - 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 / \. ( p n g | j p e ? g | g i f | w e b p | s v g | i c o ) $ / 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 ( / ^ h t t p s ? : \/ \/ / i. test ( avatar ) || / ^ d a t a : i m a g e \/ / 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-
237202function serveIndexHtml ( res : ServerResponse , indexPath : string , opts : ServeIndexHtmlOpts ) {
238203 const { basePath, config, agentId } = opts ;
239204 const identity = config
0 commit comments