@dyyz1993/create-agent 2.0.0 → 2.0.1
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/lib/copy.ts +23 -1
- package/templates/agent/eslint.config.mjs +15 -2
- package/templates/agent/package.json +10 -2
- 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/__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/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/todo/TodoPanel.tsx +149 -122
- package/templates/agent/src/mainview/lib/api-client.ts +182 -172
- package/templates/agent/src/server-config.ts +34 -32
- package/templates/agent/src/server.ts +43 -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 +8 -0
- package/templates/agent/src/shared/handlers/feed.ts +31 -32
- 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/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 +10 -0
- package/templates/agent/tsconfig.json +10 -2
- package/templates/chat/eslint.config.mjs +15 -2
- package/templates/chat/package.json +10 -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/lib/api-client.ts +183 -156
- package/templates/chat/src/mainview/lib/rpc-cache.ts +84 -0
- package/templates/chat/src/mainview/main.tsx +4 -3
- package/templates/chat/src/server-config.ts +33 -31
- package/templates/chat/src/server.ts +43 -60
- package/templates/chat/src/shared/handlers/chat.ts +196 -162
- package/templates/chat/src/shared/handlers/debug.ts +8 -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 +10 -0
- package/templates/general/eslint.config.mjs +15 -2
- package/templates/general/package.json +10 -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/layout/AppLayout.tsx +6 -0
- package/templates/general/src/mainview/lib/api-client.ts +183 -156
- package/templates/general/src/mainview/lib/rpc-cache.ts +84 -0
- package/templates/general/src/mainview/main.tsx +4 -3
- package/templates/general/src/server-config.ts +33 -31
- package/templates/general/src/server.ts +43 -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 +8 -0
- package/templates/general/src/shared/handlers/feed.ts +38 -40
- 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 +10 -0
- 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 +58 -0
- package/templates/shared/env.d.ts +11 -0
- package/templates/shared/http-routes.ts +230 -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
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web server entry point — HTTP file endpoints + WebSocket RPC gateway.
|
|
3
|
-
* Port auto-negotiation: tries config.port first, increments on EADDRINUSE.
|
|
4
|
-
* Writes actual port to .server-port for dev orchestration.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { createServer } from "http";
|
|
8
1
|
import { writeFileSync, unlinkSync, existsSync } from "fs";
|
|
9
2
|
import { join, resolve, basename } from "path";
|
|
10
3
|
import { config } from "./server-config";
|
|
11
|
-
import {
|
|
12
|
-
import { createWsHandler } from "./gateway/ws-handler";
|
|
4
|
+
import { createWebServer, getLocalIP } from "./shared/lib/web-server";
|
|
13
5
|
import { createLogger, configureLogDir } from "./shared/lib/logger";
|
|
14
6
|
import { registerPort, unregisterPort, formatRegistryForOutput } from "./shared/lib/port-registry";
|
|
15
7
|
import { discoverMethodNames } from "./shared/register-all-handlers";
|
|
@@ -22,68 +14,59 @@ const PROJECT_ROOT = resolve(import.meta.dir, "..");
|
|
|
22
14
|
const PROJECT_NAME = basename(PROJECT_ROOT);
|
|
23
15
|
|
|
24
16
|
function cleanupPortFile() {
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
try {
|
|
18
|
+
if (existsSync(PORT_FILE)) unlinkSync(PORT_FILE);
|
|
19
|
+
} catch {}
|
|
20
|
+
unregisterPort(PROJECT_ROOT);
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
function writePortFile(port: number) {
|
|
30
|
-
|
|
24
|
+
writeFileSync(PORT_FILE, String(port), "utf-8");
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
process.on("exit", cleanupPortFile);
|
|
34
|
-
process.on("SIGINT", () => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
process.on("SIGINT", () => {
|
|
29
|
+
cleanupPortFile();
|
|
30
|
+
process.exit(0);
|
|
31
|
+
});
|
|
32
|
+
process.on("SIGTERM", () => {
|
|
33
|
+
cleanupPortFile();
|
|
34
|
+
process.exit(0);
|
|
35
|
+
});
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
async function start() {
|
|
38
|
+
const { port, close } = await createWebServer({
|
|
39
|
+
port: config.port,
|
|
40
|
+
authToken: config.authToken,
|
|
41
|
+
maxUploadSize: config.maxUploadSize,
|
|
42
|
+
corsOrigin: config.corsOrigin,
|
|
43
|
+
});
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
});
|
|
56
|
-
testServer.listen(port);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
45
|
+
process.on("SIGINT", () => {
|
|
46
|
+
close();
|
|
47
|
+
cleanupPortFile();
|
|
48
|
+
process.exit(0);
|
|
49
|
+
});
|
|
50
|
+
process.on("SIGTERM", () => {
|
|
51
|
+
close();
|
|
52
|
+
cleanupPortFile();
|
|
53
|
+
process.exit(0);
|
|
54
|
+
});
|
|
59
55
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
await checkPort(port);
|
|
64
|
-
return port;
|
|
65
|
-
} catch {
|
|
66
|
-
log.info(`Port ${port} in use, trying ${port + 1}...`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
throw new Error(`No available port found after ${maxRetries} retries`);
|
|
70
|
-
}
|
|
56
|
+
writePortFile(port);
|
|
57
|
+
registerPort(PROJECT_ROOT, port, PROJECT_NAME);
|
|
71
58
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
log.info("File endpoints: GET /file/{path}, GET /info/{path}");
|
|
81
|
-
// eslint-disable-next-line no-console
|
|
82
|
-
console.log("\n" + formatRegistryForOutput() + "\n");
|
|
83
|
-
});
|
|
59
|
+
const localIp = getLocalIP();
|
|
60
|
+
log.info(`HTTP + WebSocket server running on http://localhost:${port}`);
|
|
61
|
+
log.info(`Local network access: http://${localIp}:${port}`);
|
|
62
|
+
log.info(`WebSocket: ws://localhost:${port}/ws (auth required)`);
|
|
63
|
+
log.info(`Available RPC methods: ${discoverMethodNames().join(", ")}`);
|
|
64
|
+
log.info("File endpoints: GET /file/{path}, GET /info/{path}");
|
|
65
|
+
// eslint-disable-next-line no-console
|
|
66
|
+
console.log("\n" + formatRegistryForOutput() + "\n");
|
|
84
67
|
}
|
|
85
68
|
|
|
86
69
|
start().catch((err) => {
|
|
87
|
-
|
|
88
|
-
|
|
70
|
+
log.error("Server failed to start", { error: err.message });
|
|
71
|
+
process.exit(1);
|
|
89
72
|
});
|
|
@@ -9,178 +9,212 @@ import { createLogger } from "../lib/logger";
|
|
|
9
9
|
|
|
10
10
|
const log = createLogger("chat");
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
type Platform = "desktop" | "web";
|
|
13
|
+
|
|
14
|
+
export function getStoragePathFor(platform: Platform): string {
|
|
15
|
+
const dir = join(homedir(), ".pi-agent");
|
|
16
|
+
if (platform === "desktop") {
|
|
17
|
+
return join(dir, "chat-history-desktop.json");
|
|
18
|
+
}
|
|
19
|
+
const sessionId = Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 8);
|
|
20
|
+
return join(dir, `chat-history-web-${sessionId}.json`);
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
type ChatMessage = { id: string; role: "user" | "assistant"; content: string; timestamp: number };
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
25
|
+
function createMessageStore(filePath: string) {
|
|
26
|
+
async function load(): Promise<ChatMessage[]> {
|
|
27
|
+
try {
|
|
28
|
+
if (!existsSync(filePath)) {
|
|
29
|
+
log.info(`No history file at ${filePath}`);
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
const raw = await readFile(filePath, "utf-8");
|
|
33
|
+
const msgs = JSON.parse(raw) as ChatMessage[];
|
|
34
|
+
log.info(`Loaded ${msgs.length} messages from ${filePath}`);
|
|
35
|
+
return msgs;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
log.error("Failed to load history", { error: err });
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function save(messages: ChatMessage[]): Promise<void> {
|
|
43
|
+
const dir = dirname(filePath);
|
|
44
|
+
try {
|
|
45
|
+
if (!existsSync(dir)) {
|
|
46
|
+
await mkdir(dir, { recursive: true });
|
|
47
|
+
}
|
|
48
|
+
await writeFile(filePath, JSON.stringify(messages, null, 2), "utf-8");
|
|
49
|
+
log.info(`Saved ${messages.length} messages to ${filePath}`);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
log.error("Failed to save history", { error: err });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { load, save };
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
type RegisterFn = <K extends keyof RPCMethods & string>(
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
method: K,
|
|
60
|
+
handler: (params: MethodParams<RPCMethods, K>) => Promise<MethodResult<RPCMethods, K>>
|
|
53
61
|
) => void;
|
|
54
62
|
|
|
55
63
|
export function generateReply(input: string): string {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
64
|
+
const lower = input.toLowerCase().trim();
|
|
65
|
+
|
|
66
|
+
if (/^(hi|hello|hey|howdy|hola|yo|sup)\b/i.test(lower)) {
|
|
67
|
+
const greetings = [
|
|
68
|
+
"Hey there! How can I help you today?",
|
|
69
|
+
"Hello! Great to see you. What would you like to know?",
|
|
70
|
+
"Hi! I'm your desktop assistant. Ask me anything!",
|
|
71
|
+
];
|
|
72
|
+
return greetings[Math.floor(Math.random() * greetings.length)];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (
|
|
76
|
+
/what('?s| is) the (time|date|day)|current (time|date)|what time|today'?s date/i.test(lower)
|
|
77
|
+
) {
|
|
78
|
+
const now = new Date();
|
|
79
|
+
const date = now.toLocaleDateString("en-US", {
|
|
80
|
+
weekday: "long",
|
|
81
|
+
year: "numeric",
|
|
82
|
+
month: "long",
|
|
83
|
+
day: "numeric",
|
|
84
|
+
});
|
|
85
|
+
const time = now.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit" });
|
|
86
|
+
return `It's currently **${time}** on **${date}**.`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const mathMatch = lower.match(
|
|
90
|
+
/(?:what(?:'s| is)\s+)?(\d+(?:\.\d+)?)\s*([+\-*/x×÷^])\s*(\d+(?:\.\d+)?)/
|
|
91
|
+
);
|
|
92
|
+
if (mathMatch) {
|
|
93
|
+
const a = parseFloat(mathMatch[1]);
|
|
94
|
+
const op = mathMatch[2];
|
|
95
|
+
const b = parseFloat(mathMatch[3]);
|
|
96
|
+
let result: number;
|
|
97
|
+
switch (op) {
|
|
98
|
+
case "+":
|
|
99
|
+
result = a + b;
|
|
100
|
+
break;
|
|
101
|
+
case "-":
|
|
102
|
+
result = a - b;
|
|
103
|
+
break;
|
|
104
|
+
case "*":
|
|
105
|
+
case "x":
|
|
106
|
+
case "×":
|
|
107
|
+
result = a * b;
|
|
108
|
+
break;
|
|
109
|
+
case "/":
|
|
110
|
+
case "÷":
|
|
111
|
+
result = b !== 0 ? a / b : NaN;
|
|
112
|
+
break;
|
|
113
|
+
case "^":
|
|
114
|
+
result = Math.pow(a, b);
|
|
115
|
+
break;
|
|
116
|
+
default:
|
|
117
|
+
result = NaN;
|
|
118
|
+
}
|
|
119
|
+
if (isNaN(result)) {
|
|
120
|
+
return "Hmm, I couldn't calculate that. Did you try dividing by zero?";
|
|
121
|
+
}
|
|
122
|
+
const niceResult = Number.isInteger(result)
|
|
123
|
+
? result.toString()
|
|
124
|
+
: result.toFixed(4).replace(/0+$/, "").replace(/\.$/, "");
|
|
125
|
+
return `That would be **${niceResult}**! Need help with anything else?`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (/file|files|browse|explorer|directory|folder|open file/i.test(lower)) {
|
|
129
|
+
return (
|
|
130
|
+
"I can help you explore files! While I can't directly browse files in this demo, " +
|
|
131
|
+
"here's what you can do:\n\n" +
|
|
132
|
+
"- Use the **Explorer** panel to browse your project files\n" +
|
|
133
|
+
"- Click any file to preview its contents\n" +
|
|
134
|
+
"- Use the search feature to find specific files\n\n" +
|
|
135
|
+
"Try asking about **time** or **math** for a live demo!"
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (/git|commit|branch|status|diff|push|pull/i.test(lower)) {
|
|
140
|
+
return (
|
|
141
|
+
"Git is a powerful version control system! Here's what I know:\n\n" +
|
|
142
|
+
"- **git status** - Check your current changes\n" +
|
|
143
|
+
"- **git branch** - See or switch branches\n" +
|
|
144
|
+
"- **git log** - View commit history\n" +
|
|
145
|
+
"- **git diff** - See what changed\n\n" +
|
|
146
|
+
"Check out the **Source Control** panel for a visual Git interface! " +
|
|
147
|
+
"Or try asking me about **time** or **math**."
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (/^(help|commands|what can you|what do you|capabilities|features)/i.test(lower)) {
|
|
152
|
+
return (
|
|
153
|
+
"Here's what I can help with:\n\n" +
|
|
154
|
+
"- **Greetings** - Say hi and I'll say hi back!\n" +
|
|
155
|
+
'- **Time & Date** - Ask "what time is it?" or "what\'s today\'s date?"\n' +
|
|
156
|
+
'- **Math** - Give me an expression like "12 * 8" or "what is 100 / 4"\n' +
|
|
157
|
+
"- **Files** - Ask about file browsing and the explorer\n" +
|
|
158
|
+
"- **Git** - Ask about version control commands\n" +
|
|
159
|
+
"- **Help** - Show this message anytime!\n\n" +
|
|
160
|
+
"This is a demo assistant - try different things to see what sticks!"
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const defaults = [
|
|
165
|
+
"That's an interesting question! I'm a demo assistant, so my knowledge is limited - but try asking about **time**, **math**, **files**, or **git**.",
|
|
166
|
+
"I wish I could help with that! For now I can answer questions about time, math, files, and git. Type **help** to see what I can do.",
|
|
167
|
+
"Hmm, I'm not sure about that one. But I *can* do math, tell you the time, and talk about files and git. Give it a shot!",
|
|
168
|
+
];
|
|
169
|
+
return defaults[Math.floor(Math.random() * defaults.length)];
|
|
139
170
|
}
|
|
140
171
|
|
|
141
|
-
export function register(server: RPCServer,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
172
|
+
export function register(server: RPCServer, options: HandlerOptions): void {
|
|
173
|
+
const storagePath = getStoragePathFor(options.platform);
|
|
174
|
+
const store = createMessageStore(storagePath);
|
|
175
|
+
|
|
176
|
+
const r: RegisterFn = (method, handler) => {
|
|
177
|
+
server.register(method, handler as (params: unknown) => Promise<unknown>);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
r("chat.list", async (params) => {
|
|
181
|
+
const all = await store.load();
|
|
182
|
+
const limit = params.limit ?? 50;
|
|
183
|
+
const messages = all.slice(-limit);
|
|
184
|
+
return {
|
|
185
|
+
messages,
|
|
186
|
+
hasMore: all.length > limit,
|
|
187
|
+
};
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
r("chat.send", async (params) => {
|
|
191
|
+
const all = await store.load();
|
|
192
|
+
|
|
193
|
+
const userMsg: ChatMessage = {
|
|
194
|
+
id: `msg-${Date.now()}-user`,
|
|
195
|
+
role: "user",
|
|
196
|
+
content: params.content,
|
|
197
|
+
timestamp: Date.now(),
|
|
198
|
+
};
|
|
199
|
+
all.push(userMsg);
|
|
200
|
+
|
|
201
|
+
server.emitEvent("chat.message", userMsg, { role: userMsg.role });
|
|
202
|
+
|
|
203
|
+
const thinkDelay = 200 + Math.floor(Math.random() * 300);
|
|
204
|
+
await new Promise((r) => setTimeout(r, thinkDelay));
|
|
205
|
+
|
|
206
|
+
const reply: ChatMessage = {
|
|
207
|
+
id: `msg-${Date.now()}-assistant`,
|
|
208
|
+
role: "assistant",
|
|
209
|
+
content: generateReply(params.content),
|
|
210
|
+
timestamp: Date.now(),
|
|
211
|
+
};
|
|
212
|
+
all.push(reply);
|
|
213
|
+
|
|
214
|
+
server.emitEvent("chat.message", reply, { role: reply.role });
|
|
215
|
+
|
|
216
|
+
await store.save(all);
|
|
217
|
+
|
|
218
|
+
return { ok: true };
|
|
219
|
+
});
|
|
186
220
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RPCServer } from "@dyyz1993/rpc-core";
|
|
2
|
+
import type { HandlerOptions } from "../rpc-schema";
|
|
3
|
+
|
|
4
|
+
export function register(server: RPCServer, _options: HandlerOptions): void {
|
|
5
|
+
server.register("debug.subscriptions", async () => {
|
|
6
|
+
return { subscriptions: server.getActiveSubscriptions() };
|
|
7
|
+
});
|
|
8
|
+
}
|