Skip to content

Commit 9dcc546

Browse files
author
Clawdbot
committed
fix: support multi-file uploads
1 parent b15b446 commit 9dcc546

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/bin/chrome-devtools-cli-options.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,15 @@ export const commands: Commands = {
710710
filePath: {
711711
name: 'filePath',
712712
type: 'string',
713-
description: 'The local path of the file to upload',
714-
required: true,
713+
description:
714+
'The local path of a file to upload. Use filePaths for multiple files.',
715+
required: false,
716+
},
717+
filePaths: {
718+
name: 'filePaths',
719+
type: 'array',
720+
description: 'One or more local file paths to upload in a single call.',
721+
required: false,
715722
},
716723
includeSnapshot: {
717724
name: 'includeSnapshot',

src/bin/cliDefinitions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,15 @@ export const commands: Commands = {
691691
filePath: {
692692
name: 'filePath',
693693
type: 'string',
694-
description: 'The local path of the file to upload',
695-
required: true,
694+
description:
695+
'The local path of a file to upload. Use filePaths for multiple files.',
696+
required: false,
697+
},
698+
filePaths: {
699+
name: 'filePaths',
700+
type: 'array',
701+
description: 'One or more local file paths to upload in a single call.',
702+
required: false,
696703
},
697704
includeSnapshot: {
698705
name: 'includeSnapshot',

src/tools/input.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,30 @@ export const uploadFile = definePageTool({
361361
.describe(
362362
'The uid of the file input element or an element that will open file chooser on the page from the page content snapshot',
363363
),
364-
filePath: zod.string().describe('The local path of the file to upload'),
364+
filePath: zod
365+
.string()
366+
.describe('The local path of a file to upload. Use filePaths for multiple files.')
367+
.optional(),
368+
filePaths: zod
369+
.array(zod.string())
370+
.describe('One or more local file paths to upload in a single operation.')
371+
.optional(),
365372
includeSnapshot: includeSnapshotSchema,
366373
},
367374
handler: async (request, response) => {
368-
const {uid, filePath} = request.params;
375+
const {uid} = request.params;
376+
const filePaths =
377+
request.params.filePaths ??
378+
(request.params.filePath ? [request.params.filePath] : []);
379+
if (!filePaths.length) {
380+
throw new Error('Provide filePath or filePaths to upload.');
381+
}
369382
const handle = (await request.page.getElementByUid(
370383
uid,
371384
)) as ElementHandle<HTMLInputElement>;
372385
try {
373386
try {
374-
await handle.uploadFile(filePath);
387+
await handle.uploadFile(...filePaths);
375388
} catch {
376389
// Some sites use a proxy element to trigger file upload instead of
377390
// a type=file element. In this case, we want to default to
@@ -381,7 +394,7 @@ export const uploadFile = definePageTool({
381394
request.page.pptrPage.waitForFileChooser({timeout: 3000}),
382395
handle.asLocator().click(),
383396
]);
384-
await fileChooser.accept([filePath]);
397+
await fileChooser.accept(filePaths);
385398
} catch {
386399
throw new Error(
387400
`Failed to upload file. The element could not accept the file directly, and clicking it did not trigger a file chooser.`,
@@ -391,7 +404,11 @@ export const uploadFile = definePageTool({
391404
if (request.params.includeSnapshot) {
392405
response.includeSnapshot();
393406
}
394-
response.appendResponseLine(`File uploaded from ${filePath}.`);
407+
response.appendResponseLine(
408+
filePaths.length === 1
409+
? `File uploaded from ${filePaths[0]}.`
410+
: `Files uploaded from ${filePaths.join(', ')}.`,
411+
);
395412
} finally {
396413
void handle.dispose();
397414
}

0 commit comments

Comments
 (0)