@timmeck/brain 1.0.0 → 1.1.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/BRAIN_PLAN.md +3324 -3324
- package/LICENSE +21 -21
- package/README.md +194 -188
- package/dist/brain.js +2 -0
- package/dist/brain.js.map +1 -1
- package/dist/cli/colors.d.ts +50 -0
- package/dist/cli/colors.js +106 -0
- package/dist/cli/colors.js.map +1 -0
- package/dist/cli/commands/config.d.ts +2 -0
- package/dist/cli/commands/config.js +165 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/dashboard.js +222 -8
- package/dist/cli/commands/dashboard.js.map +1 -1
- package/dist/cli/commands/export.js +3 -0
- package/dist/cli/commands/export.js.map +1 -1
- package/dist/cli/commands/import.js +24 -15
- package/dist/cli/commands/import.js.map +1 -1
- package/dist/cli/commands/insights.js +33 -6
- package/dist/cli/commands/insights.js.map +1 -1
- package/dist/cli/commands/learn.d.ts +2 -0
- package/dist/cli/commands/learn.js +22 -0
- package/dist/cli/commands/learn.js.map +1 -0
- package/dist/cli/commands/modules.js +25 -6
- package/dist/cli/commands/modules.js.map +1 -1
- package/dist/cli/commands/network.js +15 -9
- package/dist/cli/commands/network.js.map +1 -1
- package/dist/cli/commands/query.js +92 -25
- package/dist/cli/commands/query.js.map +1 -1
- package/dist/cli/commands/start.js +8 -5
- package/dist/cli/commands/start.js.map +1 -1
- package/dist/cli/commands/status.js +21 -16
- package/dist/cli/commands/status.js.map +1 -1
- package/dist/cli/commands/stop.js +5 -4
- package/dist/cli/commands/stop.js.map +1 -1
- package/dist/cli/ipc-helper.js +4 -3
- package/dist/cli/ipc-helper.js.map +1 -1
- package/dist/cli/update-check.d.ts +2 -0
- package/dist/cli/update-check.js +58 -0
- package/dist/cli/update-check.js.map +1 -0
- package/dist/db/migrations/001_core_schema.js +115 -115
- package/dist/db/migrations/002_learning_schema.js +33 -33
- package/dist/db/migrations/003_code_schema.js +48 -48
- package/dist/db/migrations/004_synapses_schema.js +52 -52
- package/dist/db/migrations/005_fts_indexes.js +73 -73
- package/dist/db/migrations/index.js +6 -6
- package/dist/db/repositories/antipattern.repository.js +3 -3
- package/dist/db/repositories/code-module.repository.d.ts +1 -0
- package/dist/db/repositories/code-module.repository.js +8 -0
- package/dist/db/repositories/code-module.repository.js.map +1 -1
- package/dist/db/repositories/error.repository.js +46 -46
- package/dist/db/repositories/insight.repository.js +3 -3
- package/dist/db/repositories/notification.repository.js +3 -3
- package/dist/db/repositories/project.repository.js +21 -21
- package/dist/db/repositories/rule.repository.js +24 -24
- package/dist/db/repositories/solution.repository.js +50 -50
- package/dist/db/repositories/synapse.repository.js +18 -18
- package/dist/db/repositories/terminal.repository.js +24 -24
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/ipc/router.d.ts +2 -0
- package/dist/ipc/router.js +7 -1
- package/dist/ipc/router.js.map +1 -1
- package/dist/services/code.service.d.ts +1 -1
- package/dist/services/code.service.js +5 -2
- package/dist/services/code.service.js.map +1 -1
- package/package.json +5 -4
- package/src/brain.ts +3 -0
- package/src/cli/colors.ts +116 -0
- package/src/cli/commands/config.ts +169 -0
- package/src/cli/commands/dashboard.ts +231 -8
- package/src/cli/commands/export.ts +4 -0
- package/src/cli/commands/import.ts +24 -15
- package/src/cli/commands/insights.ts +37 -5
- package/src/cli/commands/learn.ts +24 -0
- package/src/cli/commands/modules.ts +28 -5
- package/src/cli/commands/network.ts +15 -9
- package/src/cli/commands/query.ts +103 -26
- package/src/cli/commands/start.ts +8 -5
- package/src/cli/commands/status.ts +22 -16
- package/src/cli/commands/stop.ts +5 -4
- package/src/cli/ipc-helper.ts +4 -3
- package/src/cli/update-check.ts +63 -0
- package/src/code/analyzer.ts +77 -77
- package/src/code/fingerprint.ts +87 -87
- package/src/code/matcher.ts +64 -64
- package/src/code/parsers/generic.ts +29 -29
- package/src/code/parsers/python.ts +54 -54
- package/src/code/parsers/typescript.ts +65 -65
- package/src/code/registry.ts +60 -60
- package/src/code/scorer.ts +108 -108
- package/src/config.ts +111 -111
- package/src/db/connection.ts +22 -22
- package/src/db/migrations/001_core_schema.ts +120 -120
- package/src/db/migrations/002_learning_schema.ts +38 -38
- package/src/db/migrations/003_code_schema.ts +53 -53
- package/src/db/migrations/004_synapses_schema.ts +57 -57
- package/src/db/migrations/005_fts_indexes.ts +78 -78
- package/src/db/migrations/006_synapses_phase3.ts +17 -17
- package/src/db/migrations/index.ts +64 -64
- package/src/db/repositories/antipattern.repository.ts +66 -66
- package/src/db/repositories/code-module.repository.ts +9 -0
- package/src/db/repositories/error.repository.ts +149 -149
- package/src/db/repositories/insight.repository.ts +78 -78
- package/src/db/repositories/notification.repository.ts +66 -66
- package/src/db/repositories/project.repository.ts +93 -93
- package/src/db/repositories/rule.repository.ts +108 -108
- package/src/db/repositories/solution.repository.ts +154 -154
- package/src/db/repositories/synapse.repository.ts +153 -153
- package/src/db/repositories/terminal.repository.ts +101 -101
- package/src/hooks/post-tool-use.ts +90 -90
- package/src/hooks/post-write.ts +117 -117
- package/src/index.ts +4 -0
- package/src/ipc/client.ts +118 -118
- package/src/ipc/protocol.ts +35 -35
- package/src/ipc/router.ts +9 -1
- package/src/ipc/server.ts +110 -110
- package/src/learning/confidence-scorer.ts +47 -47
- package/src/learning/decay.ts +46 -46
- package/src/learning/learning-engine.ts +162 -162
- package/src/learning/pattern-extractor.ts +90 -90
- package/src/learning/rule-generator.ts +74 -74
- package/src/matching/error-matcher.ts +115 -115
- package/src/matching/fingerprint.ts +29 -29
- package/src/matching/similarity.ts +61 -61
- package/src/matching/tfidf.ts +74 -74
- package/src/matching/tokenizer.ts +41 -41
- package/src/mcp/auto-detect.ts +93 -93
- package/src/mcp/server.ts +73 -73
- package/src/mcp/tools.ts +290 -290
- package/src/parsing/error-parser.ts +28 -28
- package/src/parsing/parsers/compiler.ts +93 -93
- package/src/parsing/parsers/generic.ts +28 -28
- package/src/parsing/parsers/go.ts +97 -97
- package/src/parsing/parsers/node.ts +69 -69
- package/src/parsing/parsers/python.ts +62 -62
- package/src/parsing/parsers/rust.ts +50 -50
- package/src/parsing/parsers/shell.ts +42 -42
- package/src/parsing/types.ts +47 -47
- package/src/research/gap-analyzer.ts +135 -135
- package/src/research/insight-generator.ts +123 -123
- package/src/research/research-engine.ts +116 -116
- package/src/research/synergy-detector.ts +126 -126
- package/src/research/template-extractor.ts +130 -130
- package/src/research/trend-analyzer.ts +127 -127
- package/src/services/analytics.service.ts +87 -87
- package/src/services/code.service.ts +5 -2
- package/src/services/error.service.ts +164 -164
- package/src/services/notification.service.ts +41 -41
- package/src/services/prevention.service.ts +119 -119
- package/src/services/research.service.ts +93 -93
- package/src/services/solution.service.ts +116 -116
- package/src/services/synapse.service.ts +59 -59
- package/src/services/terminal.service.ts +81 -81
- package/src/synapses/activation.ts +80 -80
- package/src/synapses/decay.ts +38 -38
- package/src/synapses/hebbian.ts +69 -69
- package/src/synapses/pathfinder.ts +81 -81
- package/src/synapses/synapse-manager.ts +109 -109
- package/src/types/code.types.ts +52 -52
- package/src/types/config.types.ts +79 -79
- package/src/types/error.types.ts +67 -67
- package/src/types/ipc.types.ts +8 -8
- package/src/types/mcp.types.ts +53 -53
- package/src/types/research.types.ts +28 -28
- package/src/types/solution.types.ts +30 -30
- package/src/types/synapse.types.ts +49 -49
- package/src/utils/events.ts +45 -45
- package/src/utils/hash.ts +5 -5
- package/src/utils/logger.ts +48 -48
- package/src/utils/paths.ts +19 -19
- package/tests/fixtures/code-modules/modules.ts +83 -83
- package/tests/fixtures/errors/go.ts +9 -9
- package/tests/fixtures/errors/node.ts +24 -24
- package/tests/fixtures/errors/python.ts +21 -21
- package/tests/fixtures/errors/rust.ts +25 -25
- package/tests/fixtures/errors/shell.ts +15 -15
- package/tests/fixtures/solutions/solutions.ts +27 -27
- package/tests/helpers/setup-db.ts +52 -52
- package/tests/integration/code-flow.test.ts +86 -86
- package/tests/integration/error-flow.test.ts +83 -83
- package/tests/integration/ipc-flow.test.ts +166 -166
- package/tests/integration/learning-cycle.test.ts +82 -82
- package/tests/integration/synapse-flow.test.ts +117 -117
- package/tests/unit/code/analyzer.test.ts +58 -58
- package/tests/unit/code/fingerprint.test.ts +51 -51
- package/tests/unit/code/scorer.test.ts +55 -55
- package/tests/unit/learning/confidence-scorer.test.ts +60 -60
- package/tests/unit/learning/decay.test.ts +45 -45
- package/tests/unit/learning/pattern-extractor.test.ts +50 -50
- package/tests/unit/matching/error-matcher.test.ts +69 -69
- package/tests/unit/matching/fingerprint.test.ts +47 -47
- package/tests/unit/matching/similarity.test.ts +65 -65
- package/tests/unit/matching/tfidf.test.ts +71 -71
- package/tests/unit/matching/tokenizer.test.ts +83 -83
- package/tests/unit/parsing/parsers.test.ts +113 -113
- package/tests/unit/research/gap-analyzer.test.ts +45 -45
- package/tests/unit/research/trend-analyzer.test.ts +45 -45
- package/tests/unit/synapses/activation.test.ts +80 -80
- package/tests/unit/synapses/decay.test.ts +27 -27
- package/tests/unit/synapses/hebbian.test.ts +96 -96
- package/tests/unit/synapses/pathfinder.test.ts +72 -72
- package/tsconfig.json +18 -18
package/src/ipc/client.ts
CHANGED
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
import net from 'node:net';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import type { IpcMessage } from '../types/ipc.types.js';
|
|
4
|
-
import { encodeMessage, MessageDecoder } from './protocol.js';
|
|
5
|
-
import { getPipeName } from '../utils/paths.js';
|
|
6
|
-
|
|
7
|
-
interface PendingRequest {
|
|
8
|
-
resolve: (result: unknown) => void;
|
|
9
|
-
reject: (err: Error) => void;
|
|
10
|
-
timer: ReturnType<typeof setTimeout>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class IpcClient {
|
|
14
|
-
private socket: net.Socket | null = null;
|
|
15
|
-
private decoder = new MessageDecoder();
|
|
16
|
-
private pending = new Map<string, PendingRequest>();
|
|
17
|
-
private onNotification?: (msg: IpcMessage) => void;
|
|
18
|
-
|
|
19
|
-
constructor(
|
|
20
|
-
private pipeName: string = getPipeName(),
|
|
21
|
-
private timeout: number = 5000,
|
|
22
|
-
) {}
|
|
23
|
-
|
|
24
|
-
connect(): Promise<void> {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
this.socket = net.createConnection(this.pipeName, () => {
|
|
27
|
-
resolve();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
this.socket.on('data', (chunk) => {
|
|
31
|
-
const messages = this.decoder.feed(chunk);
|
|
32
|
-
for (const msg of messages) {
|
|
33
|
-
this.handleMessage(msg);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
this.socket.on('error', (err) => {
|
|
38
|
-
reject(err);
|
|
39
|
-
// Reject all pending requests
|
|
40
|
-
for (const [id, req] of this.pending) {
|
|
41
|
-
clearTimeout(req.timer);
|
|
42
|
-
req.reject(new Error(`Connection error: ${err.message}`));
|
|
43
|
-
this.pending.delete(id);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
this.socket.on('close', () => {
|
|
48
|
-
for (const [id, req] of this.pending) {
|
|
49
|
-
clearTimeout(req.timer);
|
|
50
|
-
req.reject(new Error('Connection closed'));
|
|
51
|
-
this.pending.delete(id);
|
|
52
|
-
}
|
|
53
|
-
this.socket = null;
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
request(method: string, params?: unknown): Promise<unknown> {
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
if (!this.socket || this.socket.destroyed) {
|
|
61
|
-
return reject(new Error('Not connected'));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const id = randomUUID();
|
|
65
|
-
const timer = setTimeout(() => {
|
|
66
|
-
this.pending.delete(id);
|
|
67
|
-
reject(new Error(`Request timeout: ${method} (${this.timeout}ms)`));
|
|
68
|
-
}, this.timeout);
|
|
69
|
-
|
|
70
|
-
this.pending.set(id, { resolve, reject, timer });
|
|
71
|
-
|
|
72
|
-
const msg: IpcMessage = {
|
|
73
|
-
id,
|
|
74
|
-
type: 'request',
|
|
75
|
-
method,
|
|
76
|
-
params,
|
|
77
|
-
};
|
|
78
|
-
this.socket.write(encodeMessage(msg));
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
setNotificationHandler(handler: (msg: IpcMessage) => void): void {
|
|
83
|
-
this.onNotification = handler;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
disconnect(): void {
|
|
87
|
-
for (const [id, req] of this.pending) {
|
|
88
|
-
clearTimeout(req.timer);
|
|
89
|
-
req.reject(new Error('Client disconnecting'));
|
|
90
|
-
this.pending.delete(id);
|
|
91
|
-
}
|
|
92
|
-
this.socket?.destroy();
|
|
93
|
-
this.socket = null;
|
|
94
|
-
this.decoder.reset();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
get connected(): boolean {
|
|
98
|
-
return this.socket !== null && !this.socket.destroyed;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private handleMessage(msg: IpcMessage): void {
|
|
102
|
-
if (msg.type === 'response') {
|
|
103
|
-
const req = this.pending.get(msg.id);
|
|
104
|
-
if (!req) return;
|
|
105
|
-
|
|
106
|
-
clearTimeout(req.timer);
|
|
107
|
-
this.pending.delete(msg.id);
|
|
108
|
-
|
|
109
|
-
if (msg.error) {
|
|
110
|
-
req.reject(new Error(msg.error.message));
|
|
111
|
-
} else {
|
|
112
|
-
req.resolve(msg.result);
|
|
113
|
-
}
|
|
114
|
-
} else if (msg.type === 'notification') {
|
|
115
|
-
this.onNotification?.(msg);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import type { IpcMessage } from '../types/ipc.types.js';
|
|
4
|
+
import { encodeMessage, MessageDecoder } from './protocol.js';
|
|
5
|
+
import { getPipeName } from '../utils/paths.js';
|
|
6
|
+
|
|
7
|
+
interface PendingRequest {
|
|
8
|
+
resolve: (result: unknown) => void;
|
|
9
|
+
reject: (err: Error) => void;
|
|
10
|
+
timer: ReturnType<typeof setTimeout>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class IpcClient {
|
|
14
|
+
private socket: net.Socket | null = null;
|
|
15
|
+
private decoder = new MessageDecoder();
|
|
16
|
+
private pending = new Map<string, PendingRequest>();
|
|
17
|
+
private onNotification?: (msg: IpcMessage) => void;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
private pipeName: string = getPipeName(),
|
|
21
|
+
private timeout: number = 5000,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
connect(): Promise<void> {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
this.socket = net.createConnection(this.pipeName, () => {
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
this.socket.on('data', (chunk) => {
|
|
31
|
+
const messages = this.decoder.feed(chunk);
|
|
32
|
+
for (const msg of messages) {
|
|
33
|
+
this.handleMessage(msg);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
this.socket.on('error', (err) => {
|
|
38
|
+
reject(err);
|
|
39
|
+
// Reject all pending requests
|
|
40
|
+
for (const [id, req] of this.pending) {
|
|
41
|
+
clearTimeout(req.timer);
|
|
42
|
+
req.reject(new Error(`Connection error: ${err.message}`));
|
|
43
|
+
this.pending.delete(id);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this.socket.on('close', () => {
|
|
48
|
+
for (const [id, req] of this.pending) {
|
|
49
|
+
clearTimeout(req.timer);
|
|
50
|
+
req.reject(new Error('Connection closed'));
|
|
51
|
+
this.pending.delete(id);
|
|
52
|
+
}
|
|
53
|
+
this.socket = null;
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
request(method: string, params?: unknown): Promise<unknown> {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
if (!this.socket || this.socket.destroyed) {
|
|
61
|
+
return reject(new Error('Not connected'));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const id = randomUUID();
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
this.pending.delete(id);
|
|
67
|
+
reject(new Error(`Request timeout: ${method} (${this.timeout}ms)`));
|
|
68
|
+
}, this.timeout);
|
|
69
|
+
|
|
70
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
71
|
+
|
|
72
|
+
const msg: IpcMessage = {
|
|
73
|
+
id,
|
|
74
|
+
type: 'request',
|
|
75
|
+
method,
|
|
76
|
+
params,
|
|
77
|
+
};
|
|
78
|
+
this.socket.write(encodeMessage(msg));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setNotificationHandler(handler: (msg: IpcMessage) => void): void {
|
|
83
|
+
this.onNotification = handler;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
disconnect(): void {
|
|
87
|
+
for (const [id, req] of this.pending) {
|
|
88
|
+
clearTimeout(req.timer);
|
|
89
|
+
req.reject(new Error('Client disconnecting'));
|
|
90
|
+
this.pending.delete(id);
|
|
91
|
+
}
|
|
92
|
+
this.socket?.destroy();
|
|
93
|
+
this.socket = null;
|
|
94
|
+
this.decoder.reset();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get connected(): boolean {
|
|
98
|
+
return this.socket !== null && !this.socket.destroyed;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private handleMessage(msg: IpcMessage): void {
|
|
102
|
+
if (msg.type === 'response') {
|
|
103
|
+
const req = this.pending.get(msg.id);
|
|
104
|
+
if (!req) return;
|
|
105
|
+
|
|
106
|
+
clearTimeout(req.timer);
|
|
107
|
+
this.pending.delete(msg.id);
|
|
108
|
+
|
|
109
|
+
if (msg.error) {
|
|
110
|
+
req.reject(new Error(msg.error.message));
|
|
111
|
+
} else {
|
|
112
|
+
req.resolve(msg.result);
|
|
113
|
+
}
|
|
114
|
+
} else if (msg.type === 'notification') {
|
|
115
|
+
this.onNotification?.(msg);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
package/src/ipc/protocol.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import { Buffer } from 'node:buffer';
|
|
2
|
-
import type { IpcMessage } from '../types/ipc.types.js';
|
|
3
|
-
|
|
4
|
-
export function encodeMessage(msg: IpcMessage): Buffer {
|
|
5
|
-
const json = JSON.stringify(msg);
|
|
6
|
-
const payload = Buffer.from(json, 'utf8');
|
|
7
|
-
const frame = Buffer.alloc(4 + payload.length);
|
|
8
|
-
frame.writeUInt32BE(payload.length, 0);
|
|
9
|
-
payload.copy(frame, 4);
|
|
10
|
-
return frame;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class MessageDecoder {
|
|
14
|
-
private buffer = Buffer.alloc(0);
|
|
15
|
-
|
|
16
|
-
feed(chunk: Buffer): IpcMessage[] {
|
|
17
|
-
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
18
|
-
const messages: IpcMessage[] = [];
|
|
19
|
-
|
|
20
|
-
while (this.buffer.length >= 4) {
|
|
21
|
-
const length = this.buffer.readUInt32BE(0);
|
|
22
|
-
if (this.buffer.length < 4 + length) break;
|
|
23
|
-
|
|
24
|
-
const json = this.buffer.subarray(4, 4 + length).toString('utf8');
|
|
25
|
-
this.buffer = this.buffer.subarray(4 + length);
|
|
26
|
-
messages.push(JSON.parse(json) as IpcMessage);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return messages;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
reset(): void {
|
|
33
|
-
this.buffer = Buffer.alloc(0);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { IpcMessage } from '../types/ipc.types.js';
|
|
3
|
+
|
|
4
|
+
export function encodeMessage(msg: IpcMessage): Buffer {
|
|
5
|
+
const json = JSON.stringify(msg);
|
|
6
|
+
const payload = Buffer.from(json, 'utf8');
|
|
7
|
+
const frame = Buffer.alloc(4 + payload.length);
|
|
8
|
+
frame.writeUInt32BE(payload.length, 0);
|
|
9
|
+
payload.copy(frame, 4);
|
|
10
|
+
return frame;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class MessageDecoder {
|
|
14
|
+
private buffer = Buffer.alloc(0);
|
|
15
|
+
|
|
16
|
+
feed(chunk: Buffer): IpcMessage[] {
|
|
17
|
+
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
18
|
+
const messages: IpcMessage[] = [];
|
|
19
|
+
|
|
20
|
+
while (this.buffer.length >= 4) {
|
|
21
|
+
const length = this.buffer.readUInt32BE(0);
|
|
22
|
+
if (this.buffer.length < 4 + length) break;
|
|
23
|
+
|
|
24
|
+
const json = this.buffer.subarray(4, 4 + length).toString('utf8');
|
|
25
|
+
this.buffer = this.buffer.subarray(4 + length);
|
|
26
|
+
messages.push(JSON.parse(json) as IpcMessage);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return messages;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
reset(): void {
|
|
33
|
+
this.buffer = Buffer.alloc(0);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/ipc/router.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { SynapseService } from '../services/synapse.service.js';
|
|
|
10
10
|
import type { ResearchService } from '../services/research.service.js';
|
|
11
11
|
import type { NotificationService } from '../services/notification.service.js';
|
|
12
12
|
import type { AnalyticsService } from '../services/analytics.service.js';
|
|
13
|
+
import type { LearningEngine, LearningCycleResult } from '../learning/learning-engine.js';
|
|
13
14
|
|
|
14
15
|
export interface Services {
|
|
15
16
|
error: ErrorService;
|
|
@@ -21,6 +22,7 @@ export interface Services {
|
|
|
21
22
|
research: ResearchService;
|
|
22
23
|
notification: NotificationService;
|
|
23
24
|
analytics: AnalyticsService;
|
|
25
|
+
learning?: LearningEngine;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
type MethodHandler = (params: unknown) => unknown;
|
|
@@ -76,7 +78,7 @@ export class IpcRouter {
|
|
|
76
78
|
['code.analyze', (params) => s.code.analyzeAndRegister(p(params))],
|
|
77
79
|
['code.find', (params) => s.code.findReusable(p(params))],
|
|
78
80
|
['code.similarity', (params) => s.code.checkSimilarity(p(params).source, p(params).language)],
|
|
79
|
-
['code.modules', (params) => s.code.listModules(p(params)?.projectId)],
|
|
81
|
+
['code.modules', (params) => s.code.listModules(p(params)?.projectId, p(params)?.language, p(params)?.limit)],
|
|
80
82
|
['code.get', (params) => s.code.getById(p(params).id)],
|
|
81
83
|
|
|
82
84
|
// Prevention
|
|
@@ -101,6 +103,12 @@ export class IpcRouter {
|
|
|
101
103
|
// Analytics
|
|
102
104
|
['analytics.summary', (params) => s.analytics.getSummary(p(params)?.projectId)],
|
|
103
105
|
['analytics.network', (params) => s.analytics.getNetworkOverview(p(params)?.limit)],
|
|
106
|
+
|
|
107
|
+
// Learning
|
|
108
|
+
['learning.run', () => {
|
|
109
|
+
if (!s.learning) throw new Error('Learning engine not available');
|
|
110
|
+
return s.learning.runCycle();
|
|
111
|
+
}],
|
|
104
112
|
]);
|
|
105
113
|
}
|
|
106
114
|
}
|
package/src/ipc/server.ts
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import net from 'node:net';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import { getLogger } from '../utils/logger.js';
|
|
4
|
-
|
|
5
|
-
const logger = getLogger();
|
|
6
|
-
import type { IpcMessage } from '../types/ipc.types.js';
|
|
7
|
-
import { encodeMessage, MessageDecoder } from './protocol.js';
|
|
8
|
-
import type { IpcRouter } from './router.js';
|
|
9
|
-
|
|
10
|
-
export class IpcServer {
|
|
11
|
-
private server: net.Server | null = null;
|
|
12
|
-
private clients = new Map<string, net.Socket>();
|
|
13
|
-
|
|
14
|
-
constructor(
|
|
15
|
-
private router: IpcRouter,
|
|
16
|
-
private pipeName: string,
|
|
17
|
-
) {}
|
|
18
|
-
|
|
19
|
-
start(): void {
|
|
20
|
-
this.server = net.createServer((socket) => {
|
|
21
|
-
const clientId = randomUUID();
|
|
22
|
-
this.clients.set(clientId, socket);
|
|
23
|
-
const decoder = new MessageDecoder();
|
|
24
|
-
|
|
25
|
-
logger.info(`IPC client connected: ${clientId}`);
|
|
26
|
-
|
|
27
|
-
socket.on('data', (chunk) => {
|
|
28
|
-
const messages = decoder.feed(chunk);
|
|
29
|
-
for (const msg of messages) {
|
|
30
|
-
this.handleMessage(clientId, msg, socket);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
socket.on('close', () => {
|
|
35
|
-
logger.info(`IPC client disconnected: ${clientId}`);
|
|
36
|
-
this.clients.delete(clientId);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
socket.on('error', (err) => {
|
|
40
|
-
logger.error(`IPC client ${clientId} error:`, err);
|
|
41
|
-
this.clients.delete(clientId);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
this.server.on('error', (err) => {
|
|
46
|
-
logger.error('IPC server error:', err);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
this.server.listen(this.pipeName, () => {
|
|
50
|
-
logger.info(`IPC server listening on ${this.pipeName}`);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private handleMessage(clientId: string, msg: IpcMessage, socket: net.Socket): void {
|
|
55
|
-
if (msg.type !== 'request' || !msg.method) return;
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
const result = this.router.handle(msg.method, msg.params);
|
|
59
|
-
const response: IpcMessage = {
|
|
60
|
-
id: msg.id,
|
|
61
|
-
type: 'response',
|
|
62
|
-
result,
|
|
63
|
-
};
|
|
64
|
-
socket.write(encodeMessage(response));
|
|
65
|
-
} catch (err) {
|
|
66
|
-
const response: IpcMessage = {
|
|
67
|
-
id: msg.id,
|
|
68
|
-
type: 'response',
|
|
69
|
-
error: { code: -1, message: err instanceof Error ? err.message : String(err) },
|
|
70
|
-
};
|
|
71
|
-
socket.write(encodeMessage(response));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
notify(clientId: string | null, notification: Omit<IpcMessage, 'id' | 'type'>): void {
|
|
76
|
-
const msg: IpcMessage = {
|
|
77
|
-
id: randomUUID(),
|
|
78
|
-
type: 'notification',
|
|
79
|
-
...notification,
|
|
80
|
-
};
|
|
81
|
-
const encoded = encodeMessage(msg);
|
|
82
|
-
|
|
83
|
-
if (clientId) {
|
|
84
|
-
const socket = this.clients.get(clientId);
|
|
85
|
-
if (socket && !socket.destroyed) {
|
|
86
|
-
socket.write(encoded);
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
for (const socket of this.clients.values()) {
|
|
90
|
-
if (!socket.destroyed) {
|
|
91
|
-
socket.write(encoded);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getClientCount(): number {
|
|
98
|
-
return this.clients.size;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
stop(): void {
|
|
102
|
-
for (const socket of this.clients.values()) {
|
|
103
|
-
socket.destroy();
|
|
104
|
-
}
|
|
105
|
-
this.clients.clear();
|
|
106
|
-
this.server?.close();
|
|
107
|
-
this.server = null;
|
|
108
|
-
logger.info('IPC server stopped');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { getLogger } from '../utils/logger.js';
|
|
4
|
+
|
|
5
|
+
const logger = getLogger();
|
|
6
|
+
import type { IpcMessage } from '../types/ipc.types.js';
|
|
7
|
+
import { encodeMessage, MessageDecoder } from './protocol.js';
|
|
8
|
+
import type { IpcRouter } from './router.js';
|
|
9
|
+
|
|
10
|
+
export class IpcServer {
|
|
11
|
+
private server: net.Server | null = null;
|
|
12
|
+
private clients = new Map<string, net.Socket>();
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private router: IpcRouter,
|
|
16
|
+
private pipeName: string,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
start(): void {
|
|
20
|
+
this.server = net.createServer((socket) => {
|
|
21
|
+
const clientId = randomUUID();
|
|
22
|
+
this.clients.set(clientId, socket);
|
|
23
|
+
const decoder = new MessageDecoder();
|
|
24
|
+
|
|
25
|
+
logger.info(`IPC client connected: ${clientId}`);
|
|
26
|
+
|
|
27
|
+
socket.on('data', (chunk) => {
|
|
28
|
+
const messages = decoder.feed(chunk);
|
|
29
|
+
for (const msg of messages) {
|
|
30
|
+
this.handleMessage(clientId, msg, socket);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
socket.on('close', () => {
|
|
35
|
+
logger.info(`IPC client disconnected: ${clientId}`);
|
|
36
|
+
this.clients.delete(clientId);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
socket.on('error', (err) => {
|
|
40
|
+
logger.error(`IPC client ${clientId} error:`, err);
|
|
41
|
+
this.clients.delete(clientId);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.server.on('error', (err) => {
|
|
46
|
+
logger.error('IPC server error:', err);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.server.listen(this.pipeName, () => {
|
|
50
|
+
logger.info(`IPC server listening on ${this.pipeName}`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private handleMessage(clientId: string, msg: IpcMessage, socket: net.Socket): void {
|
|
55
|
+
if (msg.type !== 'request' || !msg.method) return;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = this.router.handle(msg.method, msg.params);
|
|
59
|
+
const response: IpcMessage = {
|
|
60
|
+
id: msg.id,
|
|
61
|
+
type: 'response',
|
|
62
|
+
result,
|
|
63
|
+
};
|
|
64
|
+
socket.write(encodeMessage(response));
|
|
65
|
+
} catch (err) {
|
|
66
|
+
const response: IpcMessage = {
|
|
67
|
+
id: msg.id,
|
|
68
|
+
type: 'response',
|
|
69
|
+
error: { code: -1, message: err instanceof Error ? err.message : String(err) },
|
|
70
|
+
};
|
|
71
|
+
socket.write(encodeMessage(response));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
notify(clientId: string | null, notification: Omit<IpcMessage, 'id' | 'type'>): void {
|
|
76
|
+
const msg: IpcMessage = {
|
|
77
|
+
id: randomUUID(),
|
|
78
|
+
type: 'notification',
|
|
79
|
+
...notification,
|
|
80
|
+
};
|
|
81
|
+
const encoded = encodeMessage(msg);
|
|
82
|
+
|
|
83
|
+
if (clientId) {
|
|
84
|
+
const socket = this.clients.get(clientId);
|
|
85
|
+
if (socket && !socket.destroyed) {
|
|
86
|
+
socket.write(encoded);
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
for (const socket of this.clients.values()) {
|
|
90
|
+
if (!socket.destroyed) {
|
|
91
|
+
socket.write(encoded);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getClientCount(): number {
|
|
98
|
+
return this.clients.size;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
stop(): void {
|
|
102
|
+
for (const socket of this.clients.values()) {
|
|
103
|
+
socket.destroy();
|
|
104
|
+
}
|
|
105
|
+
this.clients.clear();
|
|
106
|
+
this.server?.close();
|
|
107
|
+
this.server = null;
|
|
108
|
+
logger.info('IPC server stopped');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wilson Score Interval for low-sample-size confidence.
|
|
3
|
-
* Prevents unrealistic 100% from single success/failure.
|
|
4
|
-
*/
|
|
5
|
-
export function wilsonScore(successes: number, total: number, z: number = 1.96): number {
|
|
6
|
-
if (total === 0) return 0;
|
|
7
|
-
|
|
8
|
-
const p = successes / total;
|
|
9
|
-
const z2 = z * z;
|
|
10
|
-
const n = total;
|
|
11
|
-
|
|
12
|
-
const numerator = p + z2 / (2 * n);
|
|
13
|
-
const denominator = 1 + z2 / n;
|
|
14
|
-
const margin = z * Math.sqrt((p * (1 - p) + z2 / (4 * n)) / n) / denominator;
|
|
15
|
-
|
|
16
|
-
// Lower bound of Wilson interval = conservative estimate
|
|
17
|
-
return Math.max(0, numerator / denominator - margin);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Time-decayed confidence: recent successes count more.
|
|
22
|
-
*/
|
|
23
|
-
export function timeDecayedConfidence(
|
|
24
|
-
successes: number,
|
|
25
|
-
total: number,
|
|
26
|
-
lastUsedAt: string,
|
|
27
|
-
halfLifeDays: number,
|
|
28
|
-
): number {
|
|
29
|
-
const base = wilsonScore(successes, total);
|
|
30
|
-
const ageDays = (Date.now() - new Date(lastUsedAt).getTime()) / (1000 * 60 * 60 * 24);
|
|
31
|
-
const decay = Math.pow(0.5, ageDays / halfLifeDays);
|
|
32
|
-
return base * decay;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Combined confidence from success rate + usage frequency + recency.
|
|
37
|
-
*/
|
|
38
|
-
export function computeConfidence(
|
|
39
|
-
successCount: number,
|
|
40
|
-
failCount: number,
|
|
41
|
-
lastUsedAt: string,
|
|
42
|
-
halfLifeDays: number = 30,
|
|
43
|
-
): number {
|
|
44
|
-
const total = successCount + failCount;
|
|
45
|
-
if (total === 0) return 0;
|
|
46
|
-
return timeDecayedConfidence(successCount, total, lastUsedAt, halfLifeDays);
|
|
47
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Wilson Score Interval for low-sample-size confidence.
|
|
3
|
+
* Prevents unrealistic 100% from single success/failure.
|
|
4
|
+
*/
|
|
5
|
+
export function wilsonScore(successes: number, total: number, z: number = 1.96): number {
|
|
6
|
+
if (total === 0) return 0;
|
|
7
|
+
|
|
8
|
+
const p = successes / total;
|
|
9
|
+
const z2 = z * z;
|
|
10
|
+
const n = total;
|
|
11
|
+
|
|
12
|
+
const numerator = p + z2 / (2 * n);
|
|
13
|
+
const denominator = 1 + z2 / n;
|
|
14
|
+
const margin = z * Math.sqrt((p * (1 - p) + z2 / (4 * n)) / n) / denominator;
|
|
15
|
+
|
|
16
|
+
// Lower bound of Wilson interval = conservative estimate
|
|
17
|
+
return Math.max(0, numerator / denominator - margin);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Time-decayed confidence: recent successes count more.
|
|
22
|
+
*/
|
|
23
|
+
export function timeDecayedConfidence(
|
|
24
|
+
successes: number,
|
|
25
|
+
total: number,
|
|
26
|
+
lastUsedAt: string,
|
|
27
|
+
halfLifeDays: number,
|
|
28
|
+
): number {
|
|
29
|
+
const base = wilsonScore(successes, total);
|
|
30
|
+
const ageDays = (Date.now() - new Date(lastUsedAt).getTime()) / (1000 * 60 * 60 * 24);
|
|
31
|
+
const decay = Math.pow(0.5, ageDays / halfLifeDays);
|
|
32
|
+
return base * decay;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Combined confidence from success rate + usage frequency + recency.
|
|
37
|
+
*/
|
|
38
|
+
export function computeConfidence(
|
|
39
|
+
successCount: number,
|
|
40
|
+
failCount: number,
|
|
41
|
+
lastUsedAt: string,
|
|
42
|
+
halfLifeDays: number = 30,
|
|
43
|
+
): number {
|
|
44
|
+
const total = successCount + failCount;
|
|
45
|
+
if (total === 0) return 0;
|
|
46
|
+
return timeDecayedConfidence(successCount, total, lastUsedAt, halfLifeDays);
|
|
47
|
+
}
|