@@ -8,10 +8,10 @@ import { credentialAccessClause, credentialMembershipJoin } from '@/lib/credenti
88import { getWorkspaceEnvironmentVariableNames } from '@/lib/environment/utils'
99import {
1010 lookupActiveWorkspaceFiles ,
11+ type QueryWorkspaceFilesOptions ,
1112 queryWorkspaceFiles ,
1213 type WorkspaceFileRecord ,
1314} from '@/lib/uploads/contexts/workspace/file-manager'
14- import { encodeVfsPathSegments , VfsPathError } from '@/lib/vfs/path'
1515import { isBuiltinSkillId } from '@/lib/workflows/skills/builtin-skills'
1616import { workspaceFileSourcePath } from '@/lib/workspace-files/folder-display-path'
1717import { checkWorkspaceAccess , type WorkspaceAccess } from '@/lib/workspaces/permissions/utils'
@@ -138,20 +138,6 @@ async function loadSkillNames(workspaceId: string, ids: string[]): Promise<Map<s
138138const credentialIdsOf = ( refs : ResourceIdRef [ ] ) =>
139139 refs . flatMap ( ( ref ) => ( ref . kind === 'credential' ? [ ref . id ] : [ ] ) )
140140
141- /**
142- * `path` in the VFS form a file reference is read in (`files/` prefix stripped, segments
143- * percent-decoded), so its folder and file names resolve exactly as written; `null` for a path
144- * with an empty or dot segment, which names no file.
145- */
146- function vfsReference ( path : string ) : string | null {
147- try {
148- return `files/${ encodeVfsPathSegments ( path . split ( '/' ) ) } `
149- } catch ( error ) {
150- if ( error instanceof VfsPathError ) return null
151- throw error
152- }
153- }
154-
155141/**
156142 * The live workspace files `references` name: by the id or storage key a stored file-upload
157143 * object carries (`stored`) and by source path (`paths`), each file under its source path.
@@ -163,13 +149,13 @@ async function loadReferencedFiles(
163149) : Promise < Map < string , WorkspaceFileRecord > > {
164150 const byPath = new Map < string , WorkspaceFileRecord > ( )
165151 if ( stored . length === 0 && paths . length === 0 ) return byPath
152+ // A path is matched as written, never read as a VFS path: `docs/content` names a file
153+ // called `content`, not the folder `docs`.
166154 const { files } = await lookupActiveWorkspaceFiles ( workspaceId , {
167155 ids : stored ,
168156 keys : stored ,
169- references : paths . flatMap ( ( path ) => {
170- const reference = vfsReference ( path )
171- return reference === null ? [ ] : [ reference ]
172- } ) ,
157+ names : paths . flatMap ( ( path ) => path . split ( '/' ) . at ( - 1 ) || [ ] ) ,
158+ references : [ ] ,
173159 } )
174160 for ( const file of files ) {
175161 const path = workspaceFileSourcePath ( file )
@@ -289,7 +275,8 @@ export async function loadAvailableResourceKeys(
289275 const entries = await Promise . all (
290276 [ ...new Set ( kinds ) ] . map ( async ( kind ) => {
291277 if ( kind === 'file' ) {
292- return [ kind , await loadWorkspaceFilePaths ( workspaceId , AVAILABLE_KEYS_LIMIT ) ] as const
278+ const { paths } = await loadWorkspaceFilePaths ( workspaceId , AVAILABLE_KEYS_LIMIT )
279+ return [ kind , paths ] as const
293280 }
294281 const rows = await listLiveKeys ( workspaceId , kind , { limit : AVAILABLE_KEYS_LIMIT } )
295282 if ( kind !== 'credential' ) return [ kind , rows . map ( ( row ) => row . key ) ] as const
@@ -306,22 +293,32 @@ export async function loadAvailableResourceKeys(
306293
307294/**
308295 * The source paths (`file('<path>')`) of up to `limit` of the workspace's live files, oldest upload
309- * first; a file no slash-separated path names is left out.
296+ * first, and whether more follow; a file no slash-separated path names is left out, so pages are
297+ * read until `limit` paths are found or none remain.
310298 */
311299export async function loadWorkspaceFilePaths (
312300 workspaceId : string ,
313301 limit : number
314- ) : Promise < string [ ] > {
315- const { files } = await queryWorkspaceFiles ( workspaceId , {
316- scope : 'active' ,
317- sortBy : 'uploadedAt' ,
318- sortOrder : 'asc' ,
319- limit,
320- } )
321- return files . flatMap ( ( file ) => {
322- const path = workspaceFileSourcePath ( file )
323- return path === null ? [ ] : [ path ]
324- } )
302+ ) : Promise < { paths : string [ ] ; truncated : boolean } > {
303+ const paths : string [ ] = [ ]
304+ let after : QueryWorkspaceFilesOptions [ 'after' ]
305+ for ( ; ; ) {
306+ const page = await queryWorkspaceFiles ( workspaceId , {
307+ scope : 'active' ,
308+ sortBy : 'uploadedAt' ,
309+ sortOrder : 'asc' ,
310+ limit : limit + 1 - paths . length ,
311+ after,
312+ } )
313+ for ( const file of page . files ) {
314+ const path = workspaceFileSourcePath ( file )
315+ if ( path !== null ) paths . push ( path )
316+ }
317+ if ( paths . length > limit || page . nextKeys === null ) {
318+ return { paths : paths . slice ( 0 , limit ) , truncated : paths . length > limit }
319+ }
320+ after = page . nextKeys
321+ }
325322}
326323
327324/** `idsByKey` without the credential keys bound to a credential `userId` cannot see. */
0 commit comments