File tree Expand file tree Collapse file tree 8 files changed +45
-15
lines changed
Expand file tree Collapse file tree 8 files changed +45
-15
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ export type AiOptions = {
4242} ;
4343
4444export type AiInputReadableStream = {
45- body : ReadableStream ;
45+ body : ReadableStream | FormData ;
4646 contentType : string ;
4747} ;
4848
@@ -87,14 +87,12 @@ export class AiInternalError extends Error {
8787 }
8888}
8989
90- // TODO: merge this function with the one with images-api.ts
9190function isReadableStream ( obj : unknown ) : obj is ReadableStream {
92- return ! ! (
93- obj &&
94- typeof obj === 'object' &&
95- 'getReader' in obj &&
96- typeof obj . getReader === 'function'
97- ) ;
91+ return obj instanceof ReadableStream ;
92+ }
93+
94+ function isFormData ( obj : unknown ) : obj is FormData {
95+ return obj instanceof FormData ;
9896}
9997
10098/**
@@ -111,9 +109,9 @@ function findReadableStreamKeys(
111109 value &&
112110 typeof value === 'object' &&
113111 'body' in value &&
114- isReadableStream ( value . body ) ;
112+ ( isReadableStream ( value . body ) || isFormData ( value . body ) ) ;
115113
116- if ( hasReadableStreamBody || isReadableStream ( value ) ) {
114+ if ( hasReadableStreamBody || isReadableStream ( value ) || isFormData ( value ) ) {
117115 readableStreamKeys . push ( key ) ;
118116 }
119117 }
Original file line number Diff line number Diff line change @@ -214,6 +214,25 @@ export const tests = {
214214 ) ;
215215 }
216216
217+ {
218+ // Test form data input
219+ const form = new FormData ( ) ;
220+ form . append ( 'prompt' , 'cat' ) ;
221+ const resp = await env . ai . run ( 'formDataInputs' , {
222+ audio : {
223+ body : form ,
224+ contentType : 'multipart/form-data' ,
225+ } ,
226+ } ) ;
227+
228+ assert . deepStrictEqual ( resp , {
229+ inputs : { } ,
230+ options : { userInputs : '{}' , version : '3' } ,
231+ requestUrl :
232+ 'https://workers-binding.ai/run?version=3&userInputs=%7B%7D' ,
233+ } ) ;
234+ }
235+
217236 {
218237 // Test gateway option
219238 const resp = await env . ai . run (
Original file line number Diff line number Diff line change @@ -97,6 +97,19 @@ export default {
9797 ) ;
9898 }
9999
100+ if ( modelName === 'formDataInputs' ) {
101+ return Response . json (
102+ {
103+ inputs : { } ,
104+ options : { ...data . options } ,
105+ requestUrl : request . url ,
106+ } ,
107+ {
108+ headers : respHeaders ,
109+ }
110+ ) ;
111+ }
112+
100113 if ( modelName === 'inputErrorModel' ) {
101114 return Response . json (
102115 {
Original file line number Diff line number Diff line change @@ -5415,7 +5415,7 @@ export type AiOptions = {
54155415 * Maximum 5 tags are allowed each request.
54165416 * Duplicate tags will removed.
54175417 */
5418- tags : string [ ] ;
5418+ tags ? : string [ ] ;
54195419 gateway ?: GatewayOptions ;
54205420 returnRawResponse ?: boolean ;
54215421 prefix ?: string ;
Original file line number Diff line number Diff line change @@ -9932,7 +9932,7 @@ type AiOptions = {
99329932 * Maximum 5 tags are allowed each request.
99339933 * Duplicate tags will removed.
99349934 */
9935- tags : string [ ] ;
9935+ tags ? : string [ ] ;
99369936 gateway ?: GatewayOptions ;
99379937 returnRawResponse ?: boolean ;
99389938 prefix ?: string ;
Original file line number Diff line number Diff line change @@ -9946,7 +9946,7 @@ export type AiOptions = {
99469946 * Maximum 5 tags are allowed each request.
99479947 * Duplicate tags will removed.
99489948 */
9949- tags : string [ ] ;
9949+ tags ? : string [ ] ;
99509950 gateway ?: GatewayOptions ;
99519951 returnRawResponse ?: boolean ;
99529952 prefix ?: string ;
Original file line number Diff line number Diff line change @@ -9328,7 +9328,7 @@ type AiOptions = {
93289328 * Maximum 5 tags are allowed each request.
93299329 * Duplicate tags will removed.
93309330 */
9331- tags : string [ ] ;
9331+ tags ? : string [ ] ;
93329332 gateway ?: GatewayOptions ;
93339333 returnRawResponse ?: boolean ;
93349334 prefix ?: string ;
Original file line number Diff line number Diff line change @@ -9340,7 +9340,7 @@ export type AiOptions = {
93409340 * Maximum 5 tags are allowed each request.
93419341 * Duplicate tags will removed.
93429342 */
9343- tags : string [ ] ;
9343+ tags ? : string [ ] ;
93449344 gateway ?: GatewayOptions ;
93459345 returnRawResponse ?: boolean ;
93469346 prefix ?: string ;
You can’t perform that action at this time.
0 commit comments