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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"prepare": "husky install",
"build": "bun ./scripts/build.ts",
"build:main": "bun build --env=disable --outfile=dist/index.js --target=node --minify ./src/index.ts",
"build:worker": "bun build --outfile=dist/worker.js --target=node --minify ./src/worker/worker.ts",
"build:worker": "bun build --env=disable --outfile=dist/worker.js --target=node --minify ./src/worker/worker.ts",
"start": "NODE_ENV=production node --env-file-if-exists=.env.local dist/index.js",
"dev": "bun run ./scripts/dev.ts && bun run --watch src/index.ts",
"dev": "bun run ./scripts/dev.ts",
"test": "vitest",
"lint": "bun eslint --fix",
"prettier": "prettier --write \"./**/*.{ts,js,json}\"",
Expand Down
7 changes: 4 additions & 3 deletions packages/tool/api/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export const runToolHandler = s.route(contract.tool.run, async (args) => {
}

try {
const result = isProd
? await dispatchWithNewWorker({ toolId, inputs, systemVar })
: await tool.cb(inputs, systemVar);
// const result = isProd
// ? await dispatchWithNewWorker({ toolId, inputs, systemVar })
// : await tool.cb(inputs, systemVar);
const result = await dispatchWithNewWorker({ toolId, inputs, systemVar });

if (result?.error) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/tool/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function initTool() {

addLog.info(`
=================
Load tools in prod mode
Load tools in ${isProd ? 'production' : 'development'} env
amount: ${tools.length}
=================
`);
Expand Down
20 changes: 16 additions & 4 deletions packages/tool/packages/bing/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ export default defineTool({
description: 'Default version',
inputs: [
{
key: 'key',
label: 'Bing API Key',
valueType: WorkflowIOValueTypeEnum.string,
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference]
key: 'system_input_config',
label: '',
inputList: [
{
key: 'key',
label: 'Bing API Key',
description: '可以在 https://www.bing.com/business/create 获取',
required: true,
inputType: 'secret'
}
],
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.object,
defaultValue: {
type: 'system'
}
},
{
key: 'query',
Expand Down
17 changes: 17 additions & 0 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isProd } from '@/constants';
import { copyToolIcons } from '@tool/utils/icon';
import path from 'path';
import { watch } from 'fs/promises';
import { $ } from 'bun';
import { addLog } from '@/utils/log';

async function copyDevIcons() {
if (isProd) return;
Expand All @@ -15,3 +18,17 @@ async function copyDevIcons() {
});
}
await copyDevIcons();

// watch the worker.ts change and build it
const workerPath = path.join(__dirname, '..', 'src', 'worker', 'worker.ts');
const watcher = watch(workerPath);

(async () => {
for await (const _ of watcher) {
addLog.debug(`Worker file changed, rebuilding...`);
await $`bun run build:worker`;
}
})();

// run the main server
await $`bun run --watch src/index.ts`;
13 changes: 10 additions & 3 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export async function dispatchWithNewWorker(data: {
const isBun = typeof Bun !== 'undefined';
const workerPath = isProd ? './dist/worker.js' : `${process.cwd()}/dist/worker.js`;
const worker = new Worker(workerPath, {
env: {},
env: {
NODE_ENV: process.env.NODE_ENV
},
...(isBun
? {}
: {
Expand All @@ -186,12 +188,17 @@ export async function dispatchWithNewWorker(data: {
({ 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') {
addLog.info(`Tool run`, data);
const msg = data as {
type: 'info' | 'error' | 'warn';
args: any[];
};
addLog[msg.type](`Tool run: `, msg.args);
}
worker.terminate();
}
);

Expand Down
37 changes: 36 additions & 1 deletion src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,46 @@ import { isProd } from '@/constants';
import type { SystemVarType } from '@tool/type';
import { getErrText } from '@tool/utils/err';

// rewrite console.debug to send to parent
console.debug = (...args: any[]) => {
parentPort?.postMessage({
type: 'log',
data: {
type: 'debug',
args: args
}
});
};

// rewrite console.log to send to parent
console.log = (...args: any[]) => {
parentPort?.postMessage({
type: 'log',
data: args
data: {
type: 'info',
args: args
}
});
};

console.warn = (...args: any[]) => {
parentPort?.postMessage({
type: 'log',
data: {
type: 'warn',
args: args
}
});
};

// rewrite console.error to send to parent
console.error = (...args: any[]) => {
parentPort?.postMessage({
type: 'log',
data: {
type: 'error',
args: args
}
});
};

Expand Down