@@ -33,6 +33,8 @@ import {
3333 assertWorkspaceFileFolderTarget ,
3434 buildWorkspaceFileFolderPathMap ,
3535 fileNameExistsInWorkspaceFolder ,
36+ findWorkspaceFileFolderIdByPath ,
37+ getWorkspaceFileFolderPath ,
3638 listWorkspaceFileFolders ,
3739 normalizeWorkspaceFileItemName ,
3840} from './workspace-file-folder-manager'
@@ -565,6 +567,24 @@ function mapWorkspaceFileRecord(
565567 }
566568}
567569
570+ async function mapSingleWorkspaceFileRecord (
571+ file : typeof workspaceFiles . $inferSelect ,
572+ workspaceId : string
573+ ) : Promise < WorkspaceFileRecord > {
574+ if ( ! file . folderId ) {
575+ return mapWorkspaceFileRecord ( file , workspaceId , new Map ( ) )
576+ }
577+
578+ const folderPath = await getWorkspaceFileFolderPath ( workspaceId , file . folderId , {
579+ includeDeleted : true ,
580+ } )
581+ return mapWorkspaceFileRecord (
582+ file ,
583+ workspaceId ,
584+ folderPath ? new Map ( [ [ file . folderId , folderPath ] ] ) : new Map ( )
585+ )
586+ }
587+
568588/**
569589 * Look up a single active workspace file by its original name.
570590 * Returns the record if found, or null if no matching file exists.
@@ -592,9 +612,7 @@ export async function getWorkspaceFileByName(
592612
593613 if ( files . length === 0 ) return null
594614
595- const folders = await listWorkspaceFileFolders ( workspaceId , { scope : 'all' } )
596- const folderPaths = buildWorkspaceFileFolderPathMap ( folders )
597- return mapWorkspaceFileRecord ( files [ 0 ] , workspaceId , folderPaths )
615+ return mapSingleWorkspaceFileRecord ( files [ 0 ] , workspaceId )
598616}
599617
600618/**
@@ -728,13 +746,43 @@ export function findWorkspaceFileRecord(
728746 return files . find ( ( file ) => normalizeVfsSegment ( file . name ) === segmentKey ) ?? null
729747}
730748
749+ async function getWorkspaceFileByExactReference (
750+ workspaceId : string ,
751+ fileReference : string
752+ ) : Promise < WorkspaceFileRecord | null > {
753+ const segments = fileReference
754+ . split ( '/' )
755+ . map ( ( segment ) => segment . trim ( ) )
756+ . filter ( Boolean )
757+
758+ if ( segments . length === 0 ) return null
759+ if ( segments . length === 1 ) {
760+ return getWorkspaceFileByName ( workspaceId , segments [ 0 ] , { folderId : null } )
761+ }
762+
763+ const folderId = await findWorkspaceFileFolderIdByPath ( workspaceId , segments . slice ( 0 , - 1 ) )
764+ return folderId ? getWorkspaceFileByName ( workspaceId , segments . at ( - 1 ) ?? '' , { folderId } ) : null
765+ }
766+
731767/**
732768 * Resolve a workspace file record from either its id or a VFS/name reference.
733769 */
734770export async function resolveWorkspaceFileReference (
735771 workspaceId : string ,
736772 fileReference : string
737773) : Promise < WorkspaceFileRecord | null > {
774+ const normalizedReference = normalizeWorkspaceFileReference ( fileReference )
775+ if ( normalizedReference . startsWith ( 'wf_' ) ) {
776+ const file = await getWorkspaceFile ( workspaceId , normalizedReference )
777+ if ( file ) return file
778+ }
779+
780+ const exactReferenceFile = await getWorkspaceFileByExactReference (
781+ workspaceId ,
782+ normalizedReference
783+ )
784+ if ( exactReferenceFile ) return exactReferenceFile
785+
738786 const files = await listWorkspaceFiles ( workspaceId )
739787 return findWorkspaceFileRecord ( files , fileReference )
740788}
@@ -770,9 +818,7 @@ export async function getWorkspaceFile(
770818
771819 if ( files . length === 0 ) return null
772820
773- const folders = await listWorkspaceFileFolders ( workspaceId , { scope : 'all' } )
774- const folderPaths = buildWorkspaceFileFolderPathMap ( folders )
775- return mapWorkspaceFileRecord ( files [ 0 ] , workspaceId , folderPaths )
821+ return mapSingleWorkspaceFileRecord ( files [ 0 ] , workspaceId )
776822 } catch ( error ) {
777823 logger . error ( `Failed to get workspace file ${ fileId } :` , error )
778824 return null
0 commit comments