@@ -250,6 +250,9 @@ export function createVersionedToolSelector<TParams extends Record<string, any>>
250250 }
251251}
252252
253+ const DEFAULT_MULTIPLE_FILES_ERROR =
254+ 'File reference must be a single file, not an array. Use <block.files[0]> to select one file.'
255+
253256/**
254257 * Normalizes file input from block params to a consistent format.
255258 * Handles the case where template resolution JSON.stringify's arrays/objects
@@ -260,20 +263,21 @@ export function createVersionedToolSelector<TParams extends Record<string, any>>
260263 * - An array of file objects (basic mode or properly resolved)
261264 * - A single file object
262265 * - A JSON string of file(s) (from advanced mode template resolution)
263- * @param options.single - If true, returns only the first file object instead of an array
266+ * @param options.single - If true, returns single file object and throws if multiple provided
267+ * @param options.errorMessage - Custom error message when single is true and multiple files provided
264268 * @returns Normalized file(s), or undefined if no files
265269 */
266270export function normalizeFileInput (
267271 fileParam : unknown ,
268- options : { single : true }
272+ options : { single : true ; errorMessage ?: string }
269273) : object | undefined
270274export function normalizeFileInput (
271275 fileParam : unknown ,
272276 options ?: { single ?: false }
273277) : object [ ] | undefined
274278export function normalizeFileInput (
275279 fileParam : unknown ,
276- options ?: { single ?: boolean }
280+ options ?: { single ?: boolean ; errorMessage ?: string }
277281) : object | object [ ] | undefined {
278282 if ( ! fileParam ) return undefined
279283
@@ -295,5 +299,12 @@ export function normalizeFileInput(
295299
296300 if ( ! files ) return undefined
297301
298- return options ?. single ? files [ 0 ] : files
302+ if ( options ?. single ) {
303+ if ( files . length > 1 ) {
304+ throw new Error ( options . errorMessage ?? DEFAULT_MULTIPLE_FILES_ERROR )
305+ }
306+ return files [ 0 ]
307+ }
308+
309+ return files
299310}
0 commit comments