Skip to content

Commit 9cf269d

Browse files
authored
chore: watch worker auto build in dev. (#13)
* fix: middleware * chore: add log; worker ts * feat: auto build worker
1 parent 02c0af8 commit 9cf269d

File tree

7 files changed

+86
-14
lines changed

7 files changed

+86
-14
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"prepare": "husky install",
77
"build": "bun ./scripts/build.ts",
88
"build:main": "bun build --env=disable --outfile=dist/index.js --target=node --minify ./src/index.ts",
9-
"build:worker": "bun build --outfile=dist/worker.js --target=node --minify ./src/worker/worker.ts",
9+
"build:worker": "bun build --env=disable --outfile=dist/worker.js --target=node --minify ./src/worker/worker.ts",
1010
"start": "NODE_ENV=production node --env-file-if-exists=.env.local dist/index.js",
11-
"dev": "bun run ./scripts/dev.ts && bun run --watch src/index.ts",
11+
"dev": "bun run ./scripts/dev.ts",
1212
"test": "vitest",
1313
"lint": "bun eslint --fix",
1414
"prettier": "prettier --write \"./**/*.{ts,js,json}\"",

packages/tool/api/run.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export const runToolHandler = s.route(contract.tool.run, async (args) => {
1717
}
1818

1919
try {
20-
const result = isProd
21-
? await dispatchWithNewWorker({ toolId, inputs, systemVar })
22-
: await tool.cb(inputs, systemVar);
20+
// const result = isProd
21+
// ? await dispatchWithNewWorker({ toolId, inputs, systemVar })
22+
// : await tool.cb(inputs, systemVar);
23+
const result = await dispatchWithNewWorker({ toolId, inputs, systemVar });
2324

2425
if (result?.error) {
2526
return {

packages/tool/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function initTool() {
9090

9191
addLog.info(`
9292
=================
93-
Load tools in prod mode
93+
Load tools in ${isProd ? 'production' : 'development'} env
9494
amount: ${tools.length}
9595
=================
9696
`);

packages/tool/packages/bing/config.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,22 @@ export default defineTool({
2626
description: 'Default version',
2727
inputs: [
2828
{
29-
key: 'key',
30-
label: 'Bing API Key',
31-
valueType: WorkflowIOValueTypeEnum.string,
32-
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference]
29+
key: 'system_input_config',
30+
label: '',
31+
inputList: [
32+
{
33+
key: 'key',
34+
label: 'Bing API Key',
35+
description: '可以在 https://www.bing.com/business/create 获取',
36+
required: true,
37+
inputType: 'secret'
38+
}
39+
],
40+
renderTypeList: [FlowNodeInputTypeEnum.hidden],
41+
valueType: WorkflowIOValueTypeEnum.object,
42+
defaultValue: {
43+
type: 'system'
44+
}
3345
},
3446
{
3547
key: 'query',

scripts/dev.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { isProd } from '@/constants';
22
import { copyToolIcons } from '@tool/utils/icon';
33
import path from 'path';
4+
import { watch } from 'fs/promises';
5+
import { $ } from 'bun';
6+
import { addLog } from '@/utils/log';
47

58
async function copyDevIcons() {
69
if (isProd) return;
@@ -15,3 +18,17 @@ async function copyDevIcons() {
1518
});
1619
}
1720
await copyDevIcons();
21+
22+
// watch the worker.ts change and build it
23+
const workerPath = path.join(__dirname, '..', 'src', 'worker', 'worker.ts');
24+
const watcher = watch(workerPath);
25+
26+
(async () => {
27+
for await (const _ of watcher) {
28+
addLog.debug(`Worker file changed, rebuilding...`);
29+
await $`bun run build:worker`;
30+
}
31+
})();
32+
33+
// run the main server
34+
await $`bun run --watch src/index.ts`;

src/worker/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ export async function dispatchWithNewWorker(data: {
169169
const isBun = typeof Bun !== 'undefined';
170170
const workerPath = isProd ? './dist/worker.js' : `${process.cwd()}/dist/worker.js`;
171171
const worker = new Worker(workerPath, {
172-
env: {},
172+
env: {
173+
NODE_ENV: process.env.NODE_ENV
174+
},
173175
...(isBun
174176
? {}
175177
: {
@@ -186,12 +188,17 @@ export async function dispatchWithNewWorker(data: {
186188
({ type, data }: WorkerResponse<z.infer<typeof ToolCallbackReturnSchema>>) => {
187189
if (type === 'success') {
188190
resolve(data);
191+
worker.terminate();
189192
} else if (type === 'error') {
190193
reject(data);
194+
worker.terminate();
191195
} else if (type === 'log') {
192-
addLog.info(`Tool run`, data);
196+
const msg = data as {
197+
type: 'info' | 'error' | 'warn';
198+
args: any[];
199+
};
200+
addLog[msg.type](`Tool run: `, msg.args);
193201
}
194-
worker.terminate();
195202
}
196203
);
197204

src/worker/worker.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,46 @@ import { isProd } from '@/constants';
55
import type { SystemVarType } from '@tool/type';
66
import { getErrText } from '@tool/utils/err';
77

8+
// rewrite console.debug to send to parent
9+
console.debug = (...args: any[]) => {
10+
parentPort?.postMessage({
11+
type: 'log',
12+
data: {
13+
type: 'debug',
14+
args: args
15+
}
16+
});
17+
};
18+
819
// rewrite console.log to send to parent
920
console.log = (...args: any[]) => {
1021
parentPort?.postMessage({
1122
type: 'log',
12-
data: args
23+
data: {
24+
type: 'info',
25+
args: args
26+
}
27+
});
28+
};
29+
30+
console.warn = (...args: any[]) => {
31+
parentPort?.postMessage({
32+
type: 'log',
33+
data: {
34+
type: 'warn',
35+
args: args
36+
}
37+
});
38+
};
39+
40+
// rewrite console.error to send to parent
41+
console.error = (...args: any[]) => {
42+
parentPort?.postMessage({
43+
type: 'log',
44+
data: {
45+
type: 'error',
46+
args: args
47+
}
1348
});
1449
};
1550

0 commit comments

Comments
 (0)