Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ToolCallbackReturnSchema } from '../../packages/tool/type/tool';
import { z } from 'zod';
import { addLog } from '@/utils/log';
import { isProd } from '@/constants';
import type { Worker2MainMessageType } from './type';

type WorkerQueueItem = {
id: string;
Expand Down Expand Up @@ -183,24 +184,27 @@ export async function dispatchWithNewWorker(data: {

const resolvePromise = new Promise<z.infer<typeof ToolCallbackReturnSchema>>(
(resolve, reject) => {
worker.on(
'message',
({ type, data }: WorkerResponse<z.infer<typeof ToolCallbackReturnSchema>>) => {
if (type === 'success') {
resolve(data);
worker.terminate();
} else if (type === 'error') {
reject(data);
worker.terminate();
} else if (type === 'log') {
const msg = data as {
type: 'info' | 'error' | 'warn';
args: any[];
};
addLog[msg.type](`Tool run: `, msg.args);
}
worker.on('message', ({ type, data }: Worker2MainMessageType) => {
if (type === 'success') {
resolve(data);
worker.terminate();
} else if (type === 'error') {
reject(data);
worker.terminate();
} else if (type === 'log') {
const msg = data as {
type: 'info' | 'error' | 'warn';
args: any[];
};
addLog[msg.type](`Tool run: `, msg.args);
} else if (type === 'uploadFile') {
// TODO upload
worker.postMessage({
type: 'uploadFileResponse',
data: null // TODO: response
});
}
);
});

worker.on('error', (err) => {
addLog.error(`Run tool error`, err);
Expand All @@ -214,8 +218,11 @@ export async function dispatchWithNewWorker(data: {
});

worker.postMessage({
toolDirName: tool.toolDirName,
...data
type: 'runTool',
data: {
toolDirName: tool.toolDirName,
...data
}
});
}
);
Expand Down
48 changes: 48 additions & 0 deletions src/worker/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import z from 'zod';

/**
* Worker --> Main Thread
*/
export const Worker2MainMessageSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('uploadFile'),
data: z.any() // minio upload file params
}),
z.object({
type: z.literal('log'),
data: z.object({
type: z.enum(['info', 'error', 'warn']),
args: z.array(z.any())
})
}),
z.object({
type: z.literal('success'),
data: z.any()
}),
z.object({
type: z.literal('error'),
data: z.any()
})
]);

/**
* Main Thread --> Worker
*/
export const Main2WorkerMessageSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('runTool'),
data: z.object({
toolId: z.string(),
inputs: z.any(),
systemVar: z.any(),
toolDirName: z.string()
})
}),
z.object({
type: z.literal('uploadFileResponse'),
data: z.any() // minio upload file response
})
]);

export type Worker2MainMessageType = z.infer<typeof Worker2MainMessageSchema>;
export type Main2WorkerMessageType = z.infer<typeof Main2WorkerMessageSchema>;
20 changes: 20 additions & 0 deletions src/worker/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { parentPort } from 'worker_threads';

export const uploadFile = async (data: any) => {
return new Promise((resolve, reject) => {
global.uploadFileResponseFn = (res: any) => {
resolve(res);
};
parentPort?.postMessage({
type: 'uploadFile',
data
});
});
};

declare global {
// eslint-disable-next-line no-var
var uploadFileResponseFn: (data: any) => void | undefined;
}

export {};
21 changes: 21 additions & 0 deletions src/worker/worker.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// declare global {
// // eslint-disable-next-line no-var
// var uploadFileResponseFn: (
// data: Record<string, any>
// ) => ((data: Record<string, any>) => void) | undefined;
// }

//eslint-disable-next-line no-var
declare var uploadFileResponseFn: (
data: Record<string, any>
) => ((data: Record<string, any>) => void) | undefined;

// declare module NodeJS {
// interface Global {
// uploadFileResponseFn: (
// data: Record<string, any>
// ) => ((data: Record<string, any>) => void) | undefined;
// }
// }

// export {};
104 changes: 70 additions & 34 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { parentPort } from 'worker_threads';
import path from 'path';
import { LoadToolsByFilename } from '@tool/init';
import { isProd } from '@/constants';
import type { SystemVarType } from '@tool/type';
import { getErrText } from '@tool/utils/err';
import type { Main2WorkerMessageType } from './type';

// rewrite console.debug to send to parent
console.debug = (...args: any[]) => {
Expand Down Expand Up @@ -52,40 +52,76 @@ const basePath = isProd
? process.env.TOOLS_DIR || path.join(process.cwd(), 'dist', 'tools')
: path.join(process.cwd(), 'packages', 'tool', 'packages');

parentPort?.on(
'message',
async ({
toolId,
inputs,
systemVar,
toolDirName
}: {
toolId: string;
inputs: Record<string, any>;
systemVar: SystemVarType;
toolDirName: string;
}) => {
const tools = await LoadToolsByFilename(basePath, toolDirName);
const tool = tools.find((tool) => tool.toolId === toolId);
parentPort?.on('message', async (params: Main2WorkerMessageType) => {
const { type, data } = params;
switch (type) {
case 'runTool': {
const tools = await LoadToolsByFilename(basePath, data.toolDirName);
const tool = tools.find((tool) => tool.toolId === data.toolId);

if (!tool || !tool.cb) {
parentPort?.postMessage({
type: 'error',
data: `Tool with ID ${toolId} not found or does not have a callback.`
});
}
try {
const result = await tool?.cb(inputs, systemVar);
if (!tool || !tool.cb) {
parentPort?.postMessage({
type: 'error',
data: `Tool with ID ${data.toolId} not found or does not have a callback.`
});
}
try {
const result = tool?.cb(data.inputs, data.systemVar);

parentPort?.postMessage({
type: 'success',
data: result
});
} catch (error) {
parentPort?.postMessage({
type: 'error',
data: getErrText(error)
});
parentPort?.postMessage({
type: 'success',
data: result
});
} catch (error) {
// TODO: 处理错误
parentPort?.postMessage({
type: 'error',
data: getErrText(error)
});
}
break;
}
case 'uploadFileResponse': {
global.uploadFileResponseFn?.(data);
break;
}
}
);
});

// parentPort?.on(
// 'message',
// async ({
// toolId,
// inputs,
// systemVar,
// toolDirName
// }: {
// toolId: string;
// inputs: Record<string, any>;
// systemVar: SystemVarType;
// toolDirName: string;
// }) => {
// const tools = await LoadToolsByFilename(basePath, toolDirName);
// const tool = tools.find((tool) => tool.toolId === toolId);

// if (!tool || !tool.cb) {
// parentPort?.postMessage({
// type: 'error',
// data: `Tool with ID ${toolId} not found or does not have a callback.`
// });
// }
// try {
// const result = await tool?.cb(inputs, systemVar);

// parentPort?.postMessage({
// type: 'success',
// data: result
// });
// } catch (error) {
// parentPort?.postMessage({
// type: 'error',
// data: getErrText(error)
// });
// }
// }
// );
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"@/*": ["./src/*"],
"@tool/*": ["./packages/tool/*"]
}
}
},
"include": ["**/*.ts", "**/*.d.ts"]
}
Loading