Skip to content

Commit a93d32c

Browse files
amirai21asafgardin
authored andcommitted
feat: reorganized
1 parent b71e598 commit a93d32c

File tree

6 files changed

+91
-97
lines changed

6 files changed

+91
-97
lines changed

examples/studio/conversational-rag/rag-engine.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AI21 } from 'ai21';
22
import { FileResponse, UploadFileResponse } from '../../../src/types/rag';
33

44
function sleep(ms) {
5-
return new Promise(resolve => setTimeout(resolve, ms));
5+
return new Promise((resolve) => setTimeout(resolve, ms));
66
}
77

88
async function uploadGetUpdateDelete(fileInput, label) {
@@ -41,7 +41,9 @@ const filePath = '/Users/amirkoblyansky/Documents/ukraine.txt';
4141
uploadGetUpdateDelete(filePath, Date.now().toString()).catch(console.error);
4242

4343
/* Simulate a file upload passing File instance */
44-
const fileContent = Buffer.from('Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.');
44+
const fileContent = Buffer.from(
45+
'Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.',
46+
);
4547
const dummyFile = new File([fileContent], 'example.txt', { type: 'text/plain' });
4648
uploadGetUpdateDelete(dummyFile, Date.now().toString()).catch(console.error);
4749

src/fetch/BaseFetch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { AI21Error } from '../errors';
33
import { FinalRequestOptions, CrossPlatformResponse } from '../types';
44
import { APIResponseProps } from '../types/API';
55

6-
76
export abstract class BaseFetch {
87
abstract call(url: string, options: FinalRequestOptions): Promise<CrossPlatformResponse>;
98
async handleResponse<T>({ response, options }: APIResponseProps) {

src/files/BaseFilesHandler.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import { UnifiedFormData } from "types";
2-
import { FilePathOrFileObject } from "types/rag";
1+
import { UnifiedFormData } from 'types';
2+
import { FilePathOrFileObject } from 'types/rag';
33

4-
export abstract class BaseFilesHandler {
5-
abstract createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData>;
4+
export abstract class BaseFilesHandler {
5+
abstract createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData>;
66

7-
abstract getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null;
7+
abstract getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null;
88

9-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10-
appendBodyToFormData = (formData: UnifiedFormData, body: Record<string, any>): void => {
11-
for (const [key, value] of Object.entries(body)) {
12-
if (Array.isArray(value)) {
13-
value.forEach((item) => formData.append(key, item));
14-
} else {
15-
formData.append(key, value);
16-
}
17-
}
18-
};
19-
20-
21-
}
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
appendBodyToFormData = (formData: UnifiedFormData, body: Record<string, any>): void => {
11+
for (const [key, value] of Object.entries(body)) {
12+
if (Array.isArray(value)) {
13+
value.forEach((item) => formData.append(key, item));
14+
} else {
15+
formData.append(key, value);
16+
}
17+
}
18+
};
19+
}

src/files/BrowserFilesHandler.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { UnifiedFormData } from "types";
2-
import { FilePathOrFileObject } from "types/rag";
3-
import { BaseFilesHandler } from "./BaseFilesHandler";
1+
import { UnifiedFormData } from 'types';
2+
import { FilePathOrFileObject } from 'types/rag';
3+
import { BaseFilesHandler } from './BaseFilesHandler';
44

5+
export class BrowserFilesHandler extends BaseFilesHandler {
6+
async createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData> {
7+
const formData = new FormData();
58

6-
export class BrowserFilesHandler extends BaseFilesHandler {
7-
8-
async createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData> {
9-
const formData = new FormData();
10-
11-
if (file instanceof window.File) {
12-
formData.append('file', file);
13-
} else {
14-
throw new Error('Unsupported file type in browser');
15-
}
16-
17-
return formData;
9+
if (file instanceof window.File) {
10+
formData.append('file', file);
11+
} else {
12+
throw new Error('Unsupported file type in browser');
1813
}
1914

20-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21-
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
22-
return {};
23-
}
15+
return formData;
16+
}
17+
18+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
19+
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
20+
return {};
2421
}
22+
}

src/files/NodeFilesHandler.ts

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,62 @@
1-
import { UnifiedFormData } from "types";
2-
import { FilePathOrFileObject } from "types/rag";
3-
import { BaseFilesHandler } from "./BaseFilesHandler";
1+
import { UnifiedFormData } from 'types';
2+
import { FilePathOrFileObject } from 'types/rag';
3+
import { BaseFilesHandler } from './BaseFilesHandler';
44

5-
export class NodeFilesHandler extends BaseFilesHandler {
5+
export class NodeFilesHandler extends BaseFilesHandler {
6+
async convertReadableStream(whatwgStream: ReadableStream): Promise<NodeJS.ReadableStream> {
7+
const { Readable } = await import('stream');
8+
const reader = whatwgStream.getReader();
69

7-
async convertReadableStream(whatwgStream: ReadableStream): Promise<NodeJS.ReadableStream> {
8-
const { Readable } = await import('stream');
9-
const reader = whatwgStream.getReader();
10-
11-
return new Readable({
12-
async read() {
13-
const { done, value } = await reader.read();
14-
if (done) {
15-
this.push(null);
16-
} else {
17-
this.push(value);
18-
}
19-
},
20-
});
21-
}
22-
23-
async getBoundary(formData: UnifiedFormData): Promise<string | undefined> {
24-
const { default: FormDataNode } = await import('form-data');
25-
if (formData instanceof FormDataNode) {
26-
return formData.getBoundary();
27-
}
28-
else {
29-
throw new Error('getBoundary invoked with native browser FormData instance instead of NodeJS form-data');
10+
return new Readable({
11+
async read() {
12+
const { done, value } = await reader.read();
13+
if (done) {
14+
this.push(null);
15+
} else {
16+
this.push(value);
3017
}
18+
},
19+
});
20+
}
21+
22+
async getBoundary(formData: UnifiedFormData): Promise<string | undefined> {
23+
const { default: FormDataNode } = await import('form-data');
24+
if (formData instanceof FormDataNode) {
25+
return formData.getBoundary();
26+
} else {
27+
throw new Error(
28+
'getBoundary invoked with native browser FormData instance instead of NodeJS form-data',
29+
);
3130
}
32-
31+
}
3332

34-
async createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData> {
35-
const { default: FormDataNode } = await import('form-data');
36-
const formData = new FormDataNode();
37-
38-
if (typeof file === 'string') {
39-
const fs = (await import('fs')).default;
40-
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
41-
} else if (Buffer.isBuffer(file)) {
42-
formData.append('file', file, { filename: 'TODO - add filename to buffer flow' });
43-
} else if (file instanceof File) {
44-
const nodeStream = await this.convertReadableStream(file.stream());
45-
formData.append('file', nodeStream, file.name);
46-
} else {
47-
throw new Error(`Unsupported file type for Node.js file upload flow: ${file}`);
48-
}
49-
50-
return formData;
33+
async createFormData(file: FilePathOrFileObject): Promise<UnifiedFormData> {
34+
const { default: FormDataNode } = await import('form-data');
35+
const formData = new FormDataNode();
36+
37+
if (typeof file === 'string') {
38+
const fs = (await import('fs')).default;
39+
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
40+
} else if (Buffer.isBuffer(file)) {
41+
formData.append('file', file, { filename: 'TODO - add filename to buffer flow' });
42+
} else if (file instanceof File) {
43+
const nodeStream = await this.convertReadableStream(file.stream());
44+
formData.append('file', nodeStream, file.name);
45+
} else {
46+
throw new Error(`Unsupported file type for Node.js file upload flow: ${file}`);
5147
}
5248

53-
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
54-
if (formData instanceof FormData) {
55-
return null;
56-
}
49+
return formData;
50+
}
5751

58-
const boundary = formData.getBoundary();
59-
return {
60-
'Content-Type': `multipart/form-data; boundary=${boundary}`,
61-
};
62-
}
63-
52+
getMultipartFormDataHeaders(formData: UnifiedFormData): Record<string, string> | null {
53+
if (formData instanceof FormData) {
54+
return null;
6455
}
65-
56+
57+
const boundary = formData.getBoundary();
58+
return {
59+
'Content-Type': `multipart/form-data; boundary=${boundary}`,
60+
};
61+
}
62+
}

src/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function createFetchInstance(): Fetch {
3232

3333
export function createFilesHandlerInstance(): BaseFilesHandler {
3434
if (isBrowser || isWebWorker) {
35-
return new BrowserFilesHandler();
35+
return new BrowserFilesHandler();
3636
}
3737
return new NodeFilesHandler();
3838
}

0 commit comments

Comments
 (0)