@dyyz1993/create-agent 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/create.ts +31 -31
- package/src/commands/workspace.ts +112 -105
- package/src/lib/copy.ts +24 -1
- package/templates/agent/electron/main.js +46 -0
- package/templates/agent/electron/preload.js +5 -0
- package/templates/agent/electron-builder.json +40 -0
- package/templates/agent/eslint.config.mjs +17 -2
- package/templates/agent/package.json +69 -3
- package/templates/agent/src/__tests__/hybrid-mode.test.ts +126 -0
- package/templates/agent/src/__tests__/server-config-security.test.ts +55 -0
- package/templates/agent/src/bun/index.ts +96 -41
- package/templates/agent/src/bun/three.d.ts +1 -0
- package/templates/agent/src/gateway/__tests__/ws-handler-token.test.ts +118 -0
- package/templates/agent/src/gateway/http-routes.ts +1 -1
- package/templates/agent/src/gateway/ws-handler.ts +84 -56
- package/templates/agent/src/mainview/App.tsx +29 -26
- package/templates/agent/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
- package/templates/agent/src/mainview/__tests__/setup.ts +32 -29
- package/templates/agent/src/mainview/__tests__/theme-variables.test.ts +36 -0
- package/templates/agent/src/mainview/components/chat/ChatPanel.tsx +88 -88
- package/templates/agent/src/mainview/components/common/ErrorBoundary.tsx +1 -58
- package/templates/agent/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
- package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +1 -0
- package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +232 -210
- package/templates/agent/src/mainview/components/file-preview/VirtualizedCodeView.tsx +105 -81
- package/templates/agent/src/mainview/components/search/SearchPanel.tsx +427 -378
- package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +149 -122
- package/templates/agent/src/mainview/hooks/use-input-history.ts +70 -61
- package/templates/agent/src/mainview/lib/api-client.ts +179 -172
- package/templates/agent/src/mainview/main.tsx +4 -10
- package/templates/agent/src/mainview/stores/use-feed-store.ts +107 -107
- package/templates/agent/src/mainview/utils/drop-handler.ts +114 -115
- package/templates/agent/src/server-config.ts +34 -32
- package/templates/agent/src/server.ts +42 -54
- package/templates/agent/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
- package/templates/agent/src/shared/handlers/__tests__/handler-isolation.test.ts +244 -0
- package/templates/agent/src/shared/handlers/bash.ts +65 -65
- package/templates/agent/src/shared/handlers/chat.ts +189 -155
- package/templates/agent/src/shared/handlers/debug.ts +12 -0
- package/templates/agent/src/shared/handlers/feed.ts +31 -32
- package/templates/agent/src/shared/handlers/git.ts +286 -243
- package/templates/agent/src/shared/handlers/index.ts +1 -0
- package/templates/agent/src/shared/handlers/rules.ts +31 -31
- package/templates/agent/src/shared/handlers/todo.ts +31 -31
- package/templates/agent/src/shared/http-routes.ts +250 -0
- package/templates/agent/src/shared/lib/__tests__/web-server.test.ts +190 -0
- package/templates/agent/src/shared/lib/bash-security.ts +43 -43
- package/templates/agent/src/shared/lib/logger.ts +117 -95
- package/templates/agent/src/shared/lib/web-server.ts +128 -0
- package/templates/agent/src/shared/modules/debug.ts +8 -0
- package/templates/agent/src/shared/rpc-schema.ts +16 -3
- package/templates/agent/test-upload.txt +0 -0
- package/templates/agent/tsconfig.ipc.json +14 -0
- package/templates/agent/tsconfig.json +13 -3
- package/templates/chat/eslint.config.mjs +15 -2
- package/templates/chat/package.json +13 -2
- package/templates/chat/src/__tests__/hybrid-mode.test.ts +125 -0
- package/templates/chat/src/__tests__/server-config-security.test.ts +55 -0
- package/templates/chat/src/__tests__/server-config.test.ts +46 -44
- package/templates/chat/src/bun/index.ts +94 -44
- package/templates/chat/src/bun/three.d.ts +1 -0
- package/templates/chat/src/gateway/http-routes.ts +1 -1
- package/templates/chat/src/gateway/ws-handler.ts +84 -56
- package/templates/chat/src/mainview/App.tsx +6 -0
- package/templates/chat/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
- package/templates/chat/src/mainview/__tests__/theme-variables.test.ts +36 -0
- package/templates/chat/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
- package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +1 -0
- package/templates/chat/src/mainview/hooks/use-input-history.ts +70 -61
- package/templates/chat/src/mainview/lib/api-client.ts +180 -156
- package/templates/chat/src/mainview/lib/rpc-cache.ts +84 -0
- package/templates/chat/src/mainview/main.tsx +10 -6
- package/templates/chat/src/server-config.ts +33 -31
- package/templates/chat/src/server.ts +42 -60
- package/templates/chat/src/shared/handlers/chat.ts +196 -162
- package/templates/chat/src/shared/handlers/debug.ts +12 -0
- package/templates/chat/src/shared/handlers/index.ts +1 -0
- package/templates/chat/src/shared/http-routes.ts +250 -0
- package/templates/chat/src/shared/lib/__tests__/logger.test.ts +92 -0
- package/templates/chat/src/shared/lib/logger.ts +108 -95
- package/templates/chat/src/shared/lib/web-server.ts +128 -0
- package/templates/chat/src/shared/modules/debug.ts +8 -0
- package/templates/chat/src/shared/rpc-schema.ts +3 -2
- package/templates/chat/test-upload.txt +0 -0
- package/templates/chat/tsconfig.ipc.json +14 -0
- package/templates/chat/tsconfig.json +12 -2
- package/templates/general/eslint.config.mjs +15 -2
- package/templates/general/package.json +13 -2
- package/templates/general/src/__tests__/hybrid-mode.test.ts +125 -0
- package/templates/general/src/__tests__/server-config-security.test.ts +55 -0
- package/templates/general/src/__tests__/server-config.test.ts +46 -44
- package/templates/general/src/bun/index.ts +94 -44
- package/templates/general/src/bun/three.d.ts +1 -0
- package/templates/general/src/gateway/http-routes.ts +1 -1
- package/templates/general/src/gateway/ws-handler.ts +84 -56
- package/templates/general/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
- package/templates/general/src/mainview/__tests__/theme-variables.test.ts +36 -0
- package/templates/general/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
- package/templates/general/src/mainview/components/common/ThemeToggle.tsx +1 -0
- package/templates/general/src/mainview/components/feed/FeedPanel.tsx +241 -219
- package/templates/general/src/mainview/components/file-preview/VirtualizedCodeView.tsx +101 -81
- package/templates/general/src/mainview/components/layout/AppLayout.tsx +6 -0
- package/templates/general/src/mainview/components/search/SearchPanel.tsx +429 -378
- package/templates/general/src/mainview/hooks/use-input-history.ts +70 -61
- package/templates/general/src/mainview/lib/api-client.ts +180 -156
- package/templates/general/src/mainview/lib/rpc-cache.ts +84 -0
- package/templates/general/src/mainview/main.tsx +10 -6
- package/templates/general/src/mainview/stores/use-feed-store.ts +107 -107
- package/templates/general/src/mainview/utils/drop-handler.ts +114 -115
- package/templates/general/src/server-config.ts +33 -31
- package/templates/general/src/server.ts +42 -60
- package/templates/general/src/shared/handlers/__tests__/chat.test.ts +165 -160
- package/templates/general/src/shared/handlers/chat.ts +189 -155
- package/templates/general/src/shared/handlers/debug.ts +12 -0
- package/templates/general/src/shared/handlers/feed.ts +38 -40
- package/templates/general/src/shared/handlers/git.ts +286 -243
- package/templates/general/src/shared/handlers/index.ts +1 -0
- package/templates/general/src/shared/http-routes.ts +250 -0
- package/templates/general/src/shared/lib/__tests__/logger.test.ts +92 -0
- package/templates/general/src/shared/lib/logger.ts +117 -95
- package/templates/general/src/shared/lib/web-server.ts +128 -0
- package/templates/general/src/shared/modules/debug.ts +8 -0
- package/templates/general/src/shared/rpc-schema.ts +12 -2
- package/templates/general/test-upload.txt +0 -0
- package/templates/general/tsconfig.ipc.json +14 -0
- package/templates/general/tsconfig.json +12 -2
- package/templates/shared/__tests__/cors-security.test.ts +165 -0
- package/templates/shared/__tests__/file-path-security.test.ts +260 -0
- package/templates/shared/__tests__/http-routes.test.ts +174 -0
- package/templates/shared/__tests__/logger.test.ts +186 -0
- package/templates/shared/components/ErrorBoundary.tsx +59 -0
- package/templates/shared/env.d.ts +11 -0
- package/templates/shared/http-routes.ts +250 -0
- package/templates/shared/logger.ts +129 -0
- package/templates/shared/test-upload.txt +0 -0
- package/templates/shared/vite-base.config.ts +92 -0
- package/templates/shared/vitest-base.config.ts +42 -0
- package/templates/agent/eslint-plugin-rpc/index.js +0 -50
- package/templates/agent/eslint-plugin-rpc/package.json +0 -6
- package/templates/agent/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
- package/templates/agent/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
- package/templates/agent/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
- package/templates/agent/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
- package/templates/agent/eslint-plugin-rpc/rules/require-api-client.js +0 -58
- package/templates/agent/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
- package/templates/agent/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
- package/templates/chat/eslint-plugin-rpc/index.js +0 -50
- package/templates/chat/eslint-plugin-rpc/package.json +0 -6
- package/templates/chat/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
- package/templates/chat/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
- package/templates/chat/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
- package/templates/chat/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
- package/templates/chat/eslint-plugin-rpc/rules/require-api-client.js +0 -58
- package/templates/chat/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
- package/templates/chat/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
- package/templates/general/eslint-plugin-rpc/index.js +0 -50
- package/templates/general/eslint-plugin-rpc/package.json +0 -6
- package/templates/general/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
- package/templates/general/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
- package/templates/general/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
- package/templates/general/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
- package/templates/general/eslint-plugin-rpc/rules/require-api-client.js +0 -58
- package/templates/general/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
- package/templates/general/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createServer, type Server } from "http";
|
|
2
|
+
import { networkInterfaces } from "os";
|
|
3
|
+
import { createHttpHandler } from "../http-routes";
|
|
4
|
+
import { createWsHandler } from "../../gateway/ws-handler";
|
|
5
|
+
import { createLogger } from "./logger";
|
|
6
|
+
|
|
7
|
+
const log = createLogger("web-server");
|
|
8
|
+
|
|
9
|
+
export interface WebServerConfig {
|
|
10
|
+
port: number;
|
|
11
|
+
authToken: string;
|
|
12
|
+
maxUploadSize: number;
|
|
13
|
+
corsOrigin: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface WebServerResult {
|
|
17
|
+
httpServer: Server;
|
|
18
|
+
wss: ReturnType<typeof createWsHandler>;
|
|
19
|
+
port: number;
|
|
20
|
+
authToken: string;
|
|
21
|
+
close: () => Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getLocalIP(): string {
|
|
25
|
+
const nets = networkInterfaces();
|
|
26
|
+
for (const name of Object.keys(nets)) {
|
|
27
|
+
const interfaces = nets[name];
|
|
28
|
+
if (!interfaces) continue;
|
|
29
|
+
for (const net of interfaces) {
|
|
30
|
+
if (net.family === "IPv4" && !net.internal) {
|
|
31
|
+
return net.address;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return "127.0.0.1";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function checkPort(port: number): Promise<number> {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const testServer = createServer();
|
|
41
|
+
testServer.once("error", (err: NodeJS.ErrnoException) => {
|
|
42
|
+
testServer.close();
|
|
43
|
+
reject(err);
|
|
44
|
+
});
|
|
45
|
+
testServer.once("listening", () => {
|
|
46
|
+
testServer.close();
|
|
47
|
+
resolve(port);
|
|
48
|
+
});
|
|
49
|
+
testServer.listen(port);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function findAvailablePort(startPort: number, maxRetries = 10): Promise<number> {
|
|
54
|
+
for (let port = startPort; port < startPort + maxRetries; port++) {
|
|
55
|
+
try {
|
|
56
|
+
await checkPort(port);
|
|
57
|
+
return port;
|
|
58
|
+
} catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`No available port found after ${maxRetries} retries starting from ${startPort}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getFreePort(): Promise<number> {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const s = createServer();
|
|
68
|
+
s.listen(0, () => {
|
|
69
|
+
const addr = s.address();
|
|
70
|
+
s.close(() => {
|
|
71
|
+
if (typeof addr === "object" && addr) {
|
|
72
|
+
resolve(addr.port);
|
|
73
|
+
} else {
|
|
74
|
+
reject(new Error("Failed to allocate port"));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
s.on("error", reject);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function createWebServer(config: WebServerConfig): Promise<WebServerResult> {
|
|
83
|
+
const port = config.port === 0 ? await getFreePort() : await findAvailablePort(config.port);
|
|
84
|
+
|
|
85
|
+
const httpServer = createServer();
|
|
86
|
+
|
|
87
|
+
const wss = createWsHandler(httpServer, {
|
|
88
|
+
config: {
|
|
89
|
+
port,
|
|
90
|
+
authToken: config.authToken,
|
|
91
|
+
maxUploadSize: config.maxUploadSize,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
httpServer.on(
|
|
96
|
+
"request",
|
|
97
|
+
createHttpHandler({
|
|
98
|
+
config: {
|
|
99
|
+
port,
|
|
100
|
+
authToken: config.authToken,
|
|
101
|
+
maxUploadSize: config.maxUploadSize,
|
|
102
|
+
corsOrigin: config.corsOrigin,
|
|
103
|
+
},
|
|
104
|
+
getWebSocketClientCount: () => wss.clients.size,
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
httpServer.listen(port, () => {
|
|
110
|
+
log.info("Web server started", { port, corsOrigin: config.corsOrigin });
|
|
111
|
+
|
|
112
|
+
resolve({
|
|
113
|
+
httpServer,
|
|
114
|
+
wss,
|
|
115
|
+
port,
|
|
116
|
+
authToken: config.authToken,
|
|
117
|
+
close: () =>
|
|
118
|
+
new Promise<void>((res) => {
|
|
119
|
+
wss.clients.forEach((ws) => ws.close());
|
|
120
|
+
wss.close(() => {
|
|
121
|
+
httpServer.close(() => res());
|
|
122
|
+
});
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
httpServer.on("error", reject);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { AnyMethods } from "@dyyz1993/rpc-core";
|
|
2
2
|
import type { SystemMethods } from "./modules/system";
|
|
3
3
|
import type { ChatMethods, ChatEvents } from "./modules/chat";
|
|
4
|
+
import type { DebugMethods } from "./modules/debug";
|
|
4
5
|
|
|
5
|
-
export interface RPCMethods extends AnyMethods, SystemMethods, ChatMethods {}
|
|
6
|
+
export interface RPCMethods extends AnyMethods, SystemMethods, ChatMethods, DebugMethods {}
|
|
6
7
|
|
|
7
8
|
export interface RPCEvents extends ChatEvents {}
|
|
8
9
|
|
|
9
10
|
export interface HandlerOptions {
|
|
10
|
-
|
|
11
|
+
platform: "desktop" | "web";
|
|
11
12
|
}
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"noUnusedLocals": false,
|
|
5
|
+
"noUnusedParameters": false,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"noUncheckedIndexedAccess": true,
|
|
8
|
+
"noImplicitReturns": true,
|
|
9
|
+
"noFallthroughCasesInSwitch": true,
|
|
10
|
+
"strict": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/bun", "src/gateway/ipc-transport.ts", "src/shared"],
|
|
13
|
+
"exclude": ["node_modules", "dist", "build", "**/__tests__/**", "**/*.test.ts", "**/*.test.tsx"]
|
|
14
|
+
}
|
|
@@ -19,8 +19,18 @@
|
|
|
19
19
|
"paths": {
|
|
20
20
|
"@dyyz1993/rpc-core": ["../packages/rpc-core/src/index.ts"],
|
|
21
21
|
"@shared/*": ["../shared/*"]
|
|
22
|
-
}
|
|
22
|
+
},
|
|
23
|
+
"noUncheckedIndexedAccess": true,
|
|
24
|
+
"noImplicitReturns": true
|
|
23
25
|
},
|
|
24
26
|
"include": ["src", "../shared"],
|
|
25
|
-
"exclude": [
|
|
27
|
+
"exclude": [
|
|
28
|
+
"node_modules",
|
|
29
|
+
"dist",
|
|
30
|
+
"build",
|
|
31
|
+
"../../package/dist",
|
|
32
|
+
"**/__tests__/**",
|
|
33
|
+
"**/*.test.ts",
|
|
34
|
+
"**/*.test.tsx"
|
|
35
|
+
]
|
|
26
36
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import js from '@eslint/js';
|
|
2
2
|
import tseslint from 'typescript-eslint';
|
|
3
|
-
import rpcPlugin from '
|
|
3
|
+
import rpcPlugin from '@dyyz1993/eslint-plugin-rpc';
|
|
4
|
+
import reactHooks from 'eslint-plugin-react-hooks';
|
|
5
|
+
import reactRefresh from 'eslint-plugin-react-refresh';
|
|
6
|
+
import jsxA11y from 'eslint-plugin-jsx-a11y';
|
|
4
7
|
|
|
5
8
|
export default tseslint.config(
|
|
6
9
|
js.configs.recommended,
|
|
@@ -10,7 +13,6 @@ export default tseslint.config(
|
|
|
10
13
|
'node_modules/**',
|
|
11
14
|
'build/**',
|
|
12
15
|
'dist/**',
|
|
13
|
-
'eslint-plugin-rpc/**',
|
|
14
16
|
'postcss.config.js',
|
|
15
17
|
'tailwind.config.js',
|
|
16
18
|
],
|
|
@@ -19,8 +21,19 @@ export default tseslint.config(
|
|
|
19
21
|
files: ['src/**/*.{ts,tsx}'],
|
|
20
22
|
plugins: {
|
|
21
23
|
rpc: rpcPlugin,
|
|
24
|
+
'react-hooks': reactHooks,
|
|
25
|
+
'react-refresh': reactRefresh,
|
|
26
|
+
'jsx-a11y': jsxA11y,
|
|
22
27
|
},
|
|
23
28
|
rules: {
|
|
29
|
+
...reactHooks.configs.recommended.rules,
|
|
30
|
+
'react-hooks/rules-of-hooks': 'warn',
|
|
31
|
+
'react-hooks/exhaustive-deps': 'warn',
|
|
32
|
+
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
|
|
33
|
+
...jsxA11y.configs.recommended.rules,
|
|
34
|
+
'jsx-a11y/click-events-have-key-events': 'warn',
|
|
35
|
+
'jsx-a11y/no-static-element-interactions': 'warn',
|
|
36
|
+
'jsx-a11y/no-autofocus': 'warn',
|
|
24
37
|
// TS 严格规则
|
|
25
38
|
'@typescript-eslint/no-explicit-any': 'error',
|
|
26
39
|
'@typescript-eslint/no-empty-object-type': ['error', { allowObjectTypes: 'always', allowInterfaces: 'with-single-extends' }],
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
"dev:server": "bun src/server.ts",
|
|
10
10
|
"build": "vite build",
|
|
11
11
|
"build:canary": "vite build && electrobun build --env=canary",
|
|
12
|
+
"build:mac": "vite build && electrobun build --env=canary",
|
|
13
|
+
"build:win": "vite build && electrobun build --env=canary",
|
|
14
|
+
"build:linux": "vite build && electrobun build --env=canary",
|
|
12
15
|
"lint": "eslint .",
|
|
13
16
|
"lint:fix": "eslint . --fix",
|
|
14
17
|
"prepare": "husky",
|
|
@@ -19,7 +22,7 @@
|
|
|
19
22
|
"dependencies": {
|
|
20
23
|
"@dyyz1993/rpc-core": "^1.2.0",
|
|
21
24
|
"@tanstack/react-virtual": "^3.13.24",
|
|
22
|
-
"electrobun": "1.
|
|
25
|
+
"electrobun": "1.18.1",
|
|
23
26
|
"i18next": "^24.2.2",
|
|
24
27
|
"i18next-browser-languagedetector": "^8.0.4",
|
|
25
28
|
"lucide-react": "^1.9.0",
|
|
@@ -33,7 +36,8 @@
|
|
|
33
36
|
"zustand": "^5.0.12"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
|
|
39
|
+
"@dyyz1993/eslint-plugin-rpc": "workspace:*",
|
|
40
|
+
"@eslint/js": "^9.0.0",
|
|
37
41
|
"@testing-library/jest-dom": "^6.9.1",
|
|
38
42
|
"@testing-library/react": "^16.3.2",
|
|
39
43
|
"@testing-library/user-event": "^14.6.1",
|
|
@@ -41,10 +45,14 @@
|
|
|
41
45
|
"@types/react": "^18.3.12",
|
|
42
46
|
"@types/react-dom": "^18.3.1",
|
|
43
47
|
"@types/ws": "^8.18.1",
|
|
48
|
+
"ws": "^8.18.0",
|
|
44
49
|
"@vitejs/plugin-react": "^4.3.4",
|
|
45
50
|
"autoprefixer": "^10.4.20",
|
|
46
51
|
"concurrently": "^9.1.0",
|
|
47
52
|
"eslint": "^9.0.0",
|
|
53
|
+
"eslint-plugin-jsx-a11y": "^6.10.0",
|
|
54
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
55
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
48
56
|
"happy-dom": "^20.9.0",
|
|
49
57
|
"husky": "^9.1.0",
|
|
50
58
|
"jsdom": "^29.1.1",
|
|
@@ -54,5 +62,8 @@
|
|
|
54
62
|
"typescript-eslint": "^8.0.0",
|
|
55
63
|
"vite": "^6.0.3",
|
|
56
64
|
"vitest": "^4.1.5"
|
|
65
|
+
},
|
|
66
|
+
"overrides": {
|
|
67
|
+
"minimatch": "^10.0.1"
|
|
57
68
|
}
|
|
58
69
|
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("electrobun/bun", () => ({
|
|
4
|
+
BrowserWindow: {
|
|
5
|
+
defineRPC: vi.fn(() => ({})),
|
|
6
|
+
},
|
|
7
|
+
BrowserView: {
|
|
8
|
+
defineRPC: vi.fn(() => ({
|
|
9
|
+
requests: {},
|
|
10
|
+
messages: {},
|
|
11
|
+
})),
|
|
12
|
+
},
|
|
13
|
+
Updater: {
|
|
14
|
+
localInfo: vi.fn(() => ({ channel: vi.fn(() => "dev") })),
|
|
15
|
+
},
|
|
16
|
+
ApplicationMenu: {
|
|
17
|
+
setApplicationMenu: vi.fn(),
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock("electrobun", () => ({}));
|
|
22
|
+
|
|
23
|
+
describe("混合模式", () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.resetModules();
|
|
26
|
+
delete process.env.ENABLE_WEB_SERVICE;
|
|
27
|
+
delete process.env.AUTH_TOKEN;
|
|
28
|
+
process.env.NODE_ENV = "test";
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.restoreAllMocks();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("默认不启动 Web 服务(enableWebService 为 false)", async () => {
|
|
36
|
+
const { config } = await import("../server-config");
|
|
37
|
+
expect(config.enableWebService).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("enableWebService=true 时启动 HTTP + WS 服务", async () => {
|
|
41
|
+
process.env.ENABLE_WEB_SERVICE = "true";
|
|
42
|
+
const { config } = await import("../server-config");
|
|
43
|
+
expect(config.enableWebService).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("IPC 和 WS 服务同时工作,状态隔离", async () => {
|
|
47
|
+
const { ElectrobunTransport } = await import("../gateway/ipc-transport");
|
|
48
|
+
const ipcTransport = new ElectrobunTransport();
|
|
49
|
+
expect(ipcTransport.isConnected()).toBe(false);
|
|
50
|
+
|
|
51
|
+
ipcTransport.setBrowserView({ executeJavascript: vi.fn() });
|
|
52
|
+
expect(ipcTransport.isConnected()).toBe(true);
|
|
53
|
+
|
|
54
|
+
const { createWebServer } = await import("../shared/lib/web-server");
|
|
55
|
+
const webServer = await createWebServer({
|
|
56
|
+
port: 0,
|
|
57
|
+
authToken: "test-token",
|
|
58
|
+
maxUploadSize: 1024,
|
|
59
|
+
corsOrigin: "*",
|
|
60
|
+
});
|
|
61
|
+
expect(webServer.port).toBeGreaterThan(0);
|
|
62
|
+
expect(ipcTransport.isConnected()).toBe(true);
|
|
63
|
+
await webServer.close();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("WS 客户端需要 token 才能连接", async () => {
|
|
67
|
+
const { createWebServer } = await import("../shared/lib/web-server");
|
|
68
|
+
const webServer = await createWebServer({
|
|
69
|
+
port: 0,
|
|
70
|
+
authToken: "test-token",
|
|
71
|
+
maxUploadSize: 1024,
|
|
72
|
+
corsOrigin: "*",
|
|
73
|
+
});
|
|
74
|
+
expect(webServer.authToken).toBeTruthy();
|
|
75
|
+
expect(webServer.authToken.length).toBeGreaterThan(0);
|
|
76
|
+
await webServer.close();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("IPC 客户端不需要 token", async () => {
|
|
80
|
+
const { ElectrobunTransport } = await import("../gateway/ipc-transport");
|
|
81
|
+
const transport = new ElectrobunTransport();
|
|
82
|
+
transport.setBrowserView({ executeJavascript: vi.fn() });
|
|
83
|
+
expect(transport.isConnected()).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("动态生成的 token 每次启动不同", async () => {
|
|
87
|
+
const { createWebServer } = await import("../shared/lib/web-server");
|
|
88
|
+
const ws1 = await createWebServer({
|
|
89
|
+
port: 0,
|
|
90
|
+
authToken: `token-${Date.now()}-1`,
|
|
91
|
+
maxUploadSize: 1024,
|
|
92
|
+
corsOrigin: "*",
|
|
93
|
+
});
|
|
94
|
+
const ws2 = await createWebServer({
|
|
95
|
+
port: 0,
|
|
96
|
+
authToken: `token-${Date.now()}-2`,
|
|
97
|
+
maxUploadSize: 1024,
|
|
98
|
+
corsOrigin: "*",
|
|
99
|
+
});
|
|
100
|
+
expect(ws1.authToken).not.toBe(ws2.authToken);
|
|
101
|
+
await ws1.close();
|
|
102
|
+
await ws2.close();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("关闭桌面端时 Web 服务也关闭", async () => {
|
|
106
|
+
const { createWebServer } = await import("../shared/lib/web-server");
|
|
107
|
+
const webServer = await createWebServer({
|
|
108
|
+
port: 0,
|
|
109
|
+
authToken: "test-token",
|
|
110
|
+
maxUploadSize: 1024,
|
|
111
|
+
corsOrigin: "*",
|
|
112
|
+
});
|
|
113
|
+
expect(webServer.httpServer.listening).toBe(true);
|
|
114
|
+
await webServer.close();
|
|
115
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
116
|
+
expect(webServer.httpServer.listening).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("getLocalIP 返回有效 IP 地址", async () => {
|
|
120
|
+
const { getLocalIP } = await import("../shared/lib/web-server");
|
|
121
|
+
const ip = getLocalIP();
|
|
122
|
+
expect(ip).toBeTruthy();
|
|
123
|
+
expect(ip).toMatch(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
describe("Server Config 安全策略", () => {
|
|
4
|
+
const originalEnv = process.env;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
vi.resetModules();
|
|
8
|
+
process.env = { ...originalEnv };
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
process.env = originalEnv;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("生产环境未设 AUTH_TOKEN 应抛出致命错误", async () => {
|
|
16
|
+
process.env.NODE_ENV = "production";
|
|
17
|
+
delete process.env.AUTH_TOKEN;
|
|
18
|
+
await expect(import("../server-config")).rejects.toThrow(/AUTH_TOKEN/);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("开发环境未设 AUTH_TOKEN 应自动生成随机 token", async () => {
|
|
22
|
+
process.env.NODE_ENV = "development";
|
|
23
|
+
delete process.env.AUTH_TOKEN;
|
|
24
|
+
const mod = await import("../server-config");
|
|
25
|
+
expect(mod.config.authToken).toBeTruthy();
|
|
26
|
+
expect(mod.config.authToken).not.toBe("pi-agent-template-token");
|
|
27
|
+
expect(mod.config.authToken.length).toBeGreaterThan(20);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("显式设置 AUTH_TOKEN 应使用用户提供的值", async () => {
|
|
31
|
+
delete process.env.NODE_ENV;
|
|
32
|
+
process.env.AUTH_TOKEN = "my-custom-token-12345";
|
|
33
|
+
const mod = await import("../server-config");
|
|
34
|
+
expect(mod.config.authToken).toBe("my-custom-token-12345");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("自动生成的 token 每次不同", async () => {
|
|
38
|
+
process.env.NODE_ENV = "development";
|
|
39
|
+
delete process.env.AUTH_TOKEN;
|
|
40
|
+
vi.resetModules();
|
|
41
|
+
const mod1 = await import("../server-config");
|
|
42
|
+
const token1 = mod1.config.authToken;
|
|
43
|
+
vi.resetModules();
|
|
44
|
+
const mod2 = await import("../server-config");
|
|
45
|
+
const token2 = mod2.config.authToken;
|
|
46
|
+
expect(token1).not.toBe(token2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("生产环境设了 AUTH_TOKEN 不应抛错", async () => {
|
|
50
|
+
process.env.NODE_ENV = "production";
|
|
51
|
+
process.env.AUTH_TOKEN = "production-secure-token";
|
|
52
|
+
const mod = await import("../server-config");
|
|
53
|
+
expect(mod.config.authToken).toBe("production-secure-token");
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -2,58 +2,60 @@ import { describe, it, expect } from "vitest";
|
|
|
2
2
|
import { parseEnvInt } from "../server-config";
|
|
3
3
|
|
|
4
4
|
describe("parseEnvInt", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
it("should return default when value is undefined", () => {
|
|
6
|
+
expect(parseEnvInt("TEST_KEY", undefined, 3100, 1024, 65535)).toBe(3100);
|
|
7
|
+
});
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
it("should return default when value is empty string", () => {
|
|
10
|
+
expect(parseEnvInt("TEST_KEY", "", 3100, 1024, 65535)).toBe(3100);
|
|
11
|
+
});
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
it("should parse valid number", () => {
|
|
14
|
+
expect(parseEnvInt("TEST_KEY", "8080", 3100, 1024, 65535)).toBe(8080);
|
|
15
|
+
});
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
it("should return default for NaN input", () => {
|
|
18
|
+
expect(parseEnvInt("TEST_KEY", "abc", 3100, 1024, 65535)).toBe(3100);
|
|
19
|
+
});
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
it("should return default for out-of-range value (too low)", () => {
|
|
22
|
+
expect(parseEnvInt("TEST_KEY", "80", 3100, 1024, 65535)).toBe(3100);
|
|
23
|
+
});
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
it("should return default for out-of-range value (too high)", () => {
|
|
26
|
+
expect(parseEnvInt("TEST_KEY", "99999", 3100, 1024, 65535)).toBe(3100);
|
|
27
|
+
});
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
it("should accept boundary min value", () => {
|
|
30
|
+
expect(parseEnvInt("TEST_KEY", "1024", 3100, 1024, 65535)).toBe(1024);
|
|
31
|
+
});
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
it("should accept boundary max value", () => {
|
|
34
|
+
expect(parseEnvInt("TEST_KEY", "65535", 3100, 1024, 65535)).toBe(65535);
|
|
35
|
+
});
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
describe("server-config exports", () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
it("should export config with all required fields", async () => {
|
|
40
|
+
const { config } = await import("../server-config");
|
|
41
|
+
expect(config).toHaveProperty("port");
|
|
42
|
+
expect(config).toHaveProperty("authToken");
|
|
43
|
+
expect(config).toHaveProperty("maxUploadSize");
|
|
44
|
+
expect(config).toHaveProperty("logDir");
|
|
45
|
+
expect(config).toHaveProperty("enableWebService");
|
|
46
|
+
expect(config).toHaveProperty("corsOrigin");
|
|
47
|
+
expect(typeof config.port).toBe("number");
|
|
48
|
+
expect(typeof config.authToken).toBe("string");
|
|
49
|
+
expect(typeof config.maxUploadSize).toBe("number");
|
|
50
|
+
expect(typeof config.enableWebService).toBe("boolean");
|
|
51
|
+
expect(typeof config.corsOrigin).toBe("string");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should have sensible defaults", async () => {
|
|
55
|
+
const { config } = await import("../server-config");
|
|
56
|
+
expect(config.port).toBeGreaterThanOrEqual(1024);
|
|
57
|
+
expect(config.port).toBeLessThanOrEqual(65535);
|
|
58
|
+
expect(config.maxUploadSize).toBeGreaterThan(0);
|
|
59
|
+
expect(config.corsOrigin).toBeTruthy();
|
|
60
|
+
});
|
|
59
61
|
});
|