Skip to content

Commit 6067b11

Browse files
authored
Merge pull request #5590 from thatsKevinJain/kevin/form-data
Allow FormData to be passed as input to AI bindings
2 parents 7b106e2 + db025d8 commit 6067b11

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

src/cloudflare/internal/ai-api.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type AiOptions = {
4242
};
4343

4444
export 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
9190
function 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
}

src/cloudflare/internal/test/ai/ai-api-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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(

src/cloudflare/internal/test/ai/ai-mock.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
{

types/defines/ai.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

types/generated-snapshot/experimental/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

types/generated-snapshot/experimental/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

types/generated-snapshot/latest/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

types/generated-snapshot/latest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)