Skip to content

Commit 1405587

Browse files
authored
fix: upload file error (#248)
* add log * fix: upload file * fix: upload file error
1 parent f8d0018 commit 1405587

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

modules/tool/utils/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export const replaceSensitiveText = (text: string) => {
99
return text;
1010
};
1111

12-
export const getNanoid = (length: number) => {
12+
export const getNanoid = (length: number = 12) => {
1313
return customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', length)();
1414
};

modules/tool/utils/uploadFile.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { FileMetadata } from '@/s3/config';
22
import type { FileInput } from '@/s3/type';
33
import { parentPort } from 'worker_threads';
4+
import { getNanoid } from './string';
45

56
export const uploadFile = async (data: FileInput) => {
67
// 判断是否在 worker 线程中
@@ -12,8 +13,13 @@ export const uploadFile = async (data: FileInput) => {
1213
const timer = setTimeout(() => {
1314
reject('Upload file timeout');
1415
}, 120000);
15-
global.uploadFileResponseFn = ({ data, error }) => {
16+
17+
if (!global.uploadFileResponseFnMap) {
18+
global.uploadFileResponseFnMap = new Map();
19+
}
20+
const fn = ({ data, error }: { data?: FileMetadata; error?: string }) => {
1621
clearTimeout(timer);
22+
global.uploadFileResponseFnMap.delete(id);
1723
if (error) {
1824
reject(error);
1925
} else if (data) {
@@ -22,6 +28,8 @@ export const uploadFile = async (data: FileInput) => {
2228
reject('Unknow error');
2329
}
2430
};
31+
const id = getNanoid();
32+
global.uploadFileResponseFnMap.set(id, fn);
2533

2634
// Serialize buffer data to avoid transferList issues
2735
// Convert Buffer/Uint8Array to a plain object that can be safely cloned
@@ -41,7 +49,10 @@ export const uploadFile = async (data: FileInput) => {
4149

4250
parentPort?.postMessage({
4351
type: 'uploadFile',
44-
data: serializedData
52+
data: {
53+
id,
54+
file: serializedData
55+
}
4556
});
4657
});
4758
} else {

src/worker/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,13 @@ export async function dispatchWithNewWorker(data: {
201201
} else if (type === 'uploadFile') {
202202
try {
203203
const result = await fileUploadS3Server.uploadFileAdvanced({
204-
...data,
205-
...(data.buffer ? { buffer: Buffer.from(data.buffer) } : {})
204+
...data.file,
205+
...(data.file.buffer ? { buffer: Buffer.from(data.file.buffer) } : {})
206206
});
207207
worker.postMessage({
208208
type: 'uploadFileResponse',
209209
data: {
210+
id: data.id,
210211
data: result
211212
}
212213
});
@@ -215,6 +216,7 @@ export async function dispatchWithNewWorker(data: {
215216
worker.postMessage({
216217
type: 'uploadFileResponse',
217218
data: {
219+
id: data.id,
218220
error: `Tool upload file error: ${getErrText(error)}`
219221
}
220222
});

src/worker/type.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ToolCallbackReturnSchema } from '@tool/type/tool';
55
import { FileInputSchema } from '@/s3/type';
66

77
declare global {
8-
var uploadFileResponseFn: (data: { data?: FileMetadata; error?: string }) => void | undefined;
8+
var uploadFileResponseFnMap: Map<string, (data: { data?: FileMetadata; error?: string }) => void>;
99
}
1010

1111
/**
@@ -14,7 +14,7 @@ declare global {
1414
export const Worker2MainMessageSchema = z.discriminatedUnion('type', [
1515
z.object({
1616
type: z.literal('uploadFile'),
17-
data: FileInputSchema
17+
data: z.object({ id: z.string(), file: FileInputSchema })
1818
}),
1919
z.object({
2020
type: z.literal('log'),
@@ -46,6 +46,7 @@ export const Main2WorkerMessageSchema = z.discriminatedUnion('type', [
4646
z.object({
4747
type: z.literal('uploadFileResponse'),
4848
data: z.object({
49+
id: z.string(),
4950
data: FileMetadataSchema.optional(),
5051
error: z.string().optional()
5152
})

src/worker/worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ parentPort?.on('message', async (params: Main2WorkerMessageType) => {
7272
break;
7373
}
7474
case 'uploadFileResponse': {
75-
global.uploadFileResponseFn?.(data);
75+
const fn = global.uploadFileResponseFnMap.get(data.id);
76+
if (fn) {
77+
fn(data);
78+
}
7679
break;
7780
}
7881
}

0 commit comments

Comments
 (0)