@nahisaho/musubix-neural-search 2.0.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/README.md +55 -0
- package/dist/errors.d.ts +48 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +70 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/learning/ModelUpdater.d.ts +49 -0
- package/dist/learning/ModelUpdater.d.ts.map +1 -0
- package/dist/learning/ModelUpdater.js +98 -0
- package/dist/learning/ModelUpdater.js.map +1 -0
- package/dist/learning/OnlineLearner.d.ts +59 -0
- package/dist/learning/OnlineLearner.d.ts.map +1 -0
- package/dist/learning/OnlineLearner.js +155 -0
- package/dist/learning/OnlineLearner.js.map +1 -0
- package/dist/learning/TrajectoryLog.d.ts +52 -0
- package/dist/learning/TrajectoryLog.d.ts.map +1 -0
- package/dist/learning/TrajectoryLog.js +96 -0
- package/dist/learning/TrajectoryLog.js.map +1 -0
- package/dist/learning/index.d.ts +10 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +8 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/scorer/BranchScorer.d.ts +70 -0
- package/dist/scorer/BranchScorer.d.ts.map +1 -0
- package/dist/scorer/BranchScorer.js +161 -0
- package/dist/scorer/BranchScorer.js.map +1 -0
- package/dist/scorer/ContextEncoder.d.ts +59 -0
- package/dist/scorer/ContextEncoder.d.ts.map +1 -0
- package/dist/scorer/ContextEncoder.js +149 -0
- package/dist/scorer/ContextEncoder.js.map +1 -0
- package/dist/scorer/EmbeddingModel.d.ts +39 -0
- package/dist/scorer/EmbeddingModel.d.ts.map +1 -0
- package/dist/scorer/EmbeddingModel.js +104 -0
- package/dist/scorer/EmbeddingModel.js.map +1 -0
- package/dist/scorer/index.d.ts +9 -0
- package/dist/scorer/index.d.ts.map +1 -0
- package/dist/scorer/index.js +8 -0
- package/dist/scorer/index.js.map +1 -0
- package/dist/search/BeamSearch.d.ts +32 -0
- package/dist/search/BeamSearch.d.ts.map +1 -0
- package/dist/search/BeamSearch.js +139 -0
- package/dist/search/BeamSearch.js.map +1 -0
- package/dist/search/BestFirstSearch.d.ts +23 -0
- package/dist/search/BestFirstSearch.d.ts.map +1 -0
- package/dist/search/BestFirstSearch.js +110 -0
- package/dist/search/BestFirstSearch.js.map +1 -0
- package/dist/search/PriorityQueue.d.ts +54 -0
- package/dist/search/PriorityQueue.d.ts.map +1 -0
- package/dist/search/PriorityQueue.js +114 -0
- package/dist/search/PriorityQueue.js.map +1 -0
- package/dist/search/PruningManager.d.ts +62 -0
- package/dist/search/PruningManager.d.ts.map +1 -0
- package/dist/search/PruningManager.js +188 -0
- package/dist/search/PruningManager.js.map +1 -0
- package/dist/search/index.d.ts +10 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +9 -0
- package/dist/search/index.js.map +1 -0
- package/dist/types.d.ts +416 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trajectory Log
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Logs and queries search trajectories
|
|
5
|
+
* Traces to: REQ-NS-004 (探索履歴学習)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Trajectory log implementation
|
|
9
|
+
*/
|
|
10
|
+
export class TrajectoryLog {
|
|
11
|
+
trajectories;
|
|
12
|
+
specIndex; // spec hash -> trajectory ids
|
|
13
|
+
constructor() {
|
|
14
|
+
this.trajectories = new Map();
|
|
15
|
+
this.specIndex = new Map();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Log a completed trajectory
|
|
19
|
+
*/
|
|
20
|
+
log(trajectory) {
|
|
21
|
+
this.trajectories.set(trajectory.id, trajectory);
|
|
22
|
+
// Index by specification
|
|
23
|
+
const specHash = this.hashSpec(trajectory.specification);
|
|
24
|
+
const existing = this.specIndex.get(specHash) ?? [];
|
|
25
|
+
existing.push(trajectory.id);
|
|
26
|
+
this.specIndex.set(specHash, existing);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Query trajectories by specification
|
|
30
|
+
*/
|
|
31
|
+
queryBySpec(spec, limit = 10) {
|
|
32
|
+
const specHash = this.hashSpec(spec);
|
|
33
|
+
const ids = this.specIndex.get(specHash) ?? [];
|
|
34
|
+
return ids
|
|
35
|
+
.slice(0, limit)
|
|
36
|
+
.map((id) => this.trajectories.get(id))
|
|
37
|
+
.filter((t) => t !== undefined);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get successful trajectories
|
|
41
|
+
*/
|
|
42
|
+
getSuccessful(limit = 10) {
|
|
43
|
+
return Array.from(this.trajectories.values())
|
|
44
|
+
.filter((t) => t.outcome.success)
|
|
45
|
+
.sort((a, b) => b.outcome.finalScore - a.outcome.finalScore)
|
|
46
|
+
.slice(0, limit);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get all trajectories
|
|
50
|
+
*/
|
|
51
|
+
getAll() {
|
|
52
|
+
return Array.from(this.trajectories.values());
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get trajectory by ID
|
|
56
|
+
*/
|
|
57
|
+
get(id) {
|
|
58
|
+
return this.trajectories.get(id);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get statistics
|
|
62
|
+
*/
|
|
63
|
+
getStatistics() {
|
|
64
|
+
const all = Array.from(this.trajectories.values());
|
|
65
|
+
const successful = all.filter((t) => t.outcome.success);
|
|
66
|
+
const totalLength = all.reduce((sum, t) => sum + t.path.length, 0);
|
|
67
|
+
const totalDuration = all.reduce((sum, t) => sum + t.path.reduce((s, step) => s + step.duration, 0), 0);
|
|
68
|
+
return {
|
|
69
|
+
totalTrajectories: all.length,
|
|
70
|
+
successRate: all.length > 0 ? successful.length / all.length : 0,
|
|
71
|
+
averageLength: all.length > 0 ? totalLength / all.length : 0,
|
|
72
|
+
averageDuration: all.length > 0 ? totalDuration / all.length : 0,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Clear all trajectories
|
|
77
|
+
*/
|
|
78
|
+
clear() {
|
|
79
|
+
this.trajectories.clear();
|
|
80
|
+
this.specIndex.clear();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get size
|
|
84
|
+
*/
|
|
85
|
+
size() {
|
|
86
|
+
return this.trajectories.size;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Hash specification for indexing
|
|
90
|
+
*/
|
|
91
|
+
hashSpec(spec) {
|
|
92
|
+
// Simple hash: normalized lowercase
|
|
93
|
+
return spec.toLowerCase().replace(/\s+/g, ' ').trim().substring(0, 100);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=TrajectoryLog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TrajectoryLog.js","sourceRoot":"","sources":["../../src/learning/TrajectoryLog.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH;;GAEG;AACH,MAAM,OAAO,aAAa;IAChB,YAAY,CAAgC;IAC5C,SAAS,CAAwB,CAAC,8BAA8B;IAExE;QACE,IAAI,CAAC,YAAY,GAAG,IAAI,GAAG,EAAE,CAAC;QAC9B,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAE,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,UAA4B;QAC9B,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;QAEjD,yBAAyB;QACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,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KAAK;QACH,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;QAC1B,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;IAChC,CAAC;IAED;;OAEG;IACK,QAAQ,CAAC,IAAY;QAC3B,oCAAoC;QACpC,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC1E,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Learning Module
|
|
3
|
+
* @module @nahisaho/musubix-neural-search/learning
|
|
4
|
+
*/
|
|
5
|
+
export { TrajectoryLog } from './TrajectoryLog.js';
|
|
6
|
+
export { OnlineLearner } from './OnlineLearner.js';
|
|
7
|
+
export type { OnlineLearnerConfig } from './OnlineLearner.js';
|
|
8
|
+
export { ModelUpdater } from './ModelUpdater.js';
|
|
9
|
+
export type { ModelUpdaterConfig } from './ModelUpdater.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/learning/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,YAAY,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Learning Module
|
|
3
|
+
* @module @nahisaho/musubix-neural-search/learning
|
|
4
|
+
*/
|
|
5
|
+
export { TrajectoryLog } from './TrajectoryLog.js';
|
|
6
|
+
export { OnlineLearner } from './OnlineLearner.js';
|
|
7
|
+
export { ModelUpdater } from './ModelUpdater.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/learning/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch Scorer
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Neural scoring for search branches
|
|
5
|
+
* Traces to: REQ-NS-001 (分岐スコアリング)
|
|
6
|
+
*/
|
|
7
|
+
import type { Branch, BranchScore, IBranchScorer, IEmbeddingModel, ScoreFeedback, SearchContext } from '../types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Weight configuration for score components
|
|
10
|
+
*/
|
|
11
|
+
export interface ScoreWeights {
|
|
12
|
+
specAlignment: number;
|
|
13
|
+
codeQuality: number;
|
|
14
|
+
novelty: number;
|
|
15
|
+
feasibility: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Branch scorer implementation
|
|
19
|
+
*/
|
|
20
|
+
export declare class BranchScorer implements IBranchScorer {
|
|
21
|
+
private readonly embeddingModel;
|
|
22
|
+
private readonly weights;
|
|
23
|
+
private readonly feedbackHistory;
|
|
24
|
+
constructor(embeddingModel?: IEmbeddingModel, weights?: Partial<ScoreWeights>);
|
|
25
|
+
/**
|
|
26
|
+
* Score a single branch
|
|
27
|
+
*/
|
|
28
|
+
score(branch: Branch, context: SearchContext): Promise<BranchScore>;
|
|
29
|
+
/**
|
|
30
|
+
* Score multiple branches in batch
|
|
31
|
+
*/
|
|
32
|
+
scoreBatch(branches: Branch[], context: SearchContext): Promise<BranchScore[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Update scorer with feedback
|
|
35
|
+
*/
|
|
36
|
+
update(feedback: ScoreFeedback): void;
|
|
37
|
+
/**
|
|
38
|
+
* Get current weights
|
|
39
|
+
*/
|
|
40
|
+
getWeights(): ScoreWeights;
|
|
41
|
+
/**
|
|
42
|
+
* Get feedback history for a branch
|
|
43
|
+
*/
|
|
44
|
+
getFeedbackHistory(branchId: string): ScoreFeedback[];
|
|
45
|
+
/**
|
|
46
|
+
* Compute individual score components
|
|
47
|
+
*/
|
|
48
|
+
private computeComponents;
|
|
49
|
+
/**
|
|
50
|
+
* Compute code quality score
|
|
51
|
+
*/
|
|
52
|
+
private computeCodeQuality;
|
|
53
|
+
/**
|
|
54
|
+
* Compute novelty score
|
|
55
|
+
*/
|
|
56
|
+
private computeNovelty;
|
|
57
|
+
/**
|
|
58
|
+
* Compute feasibility score
|
|
59
|
+
*/
|
|
60
|
+
private computeFeasibility;
|
|
61
|
+
/**
|
|
62
|
+
* Combine component scores with weights
|
|
63
|
+
*/
|
|
64
|
+
private combineScores;
|
|
65
|
+
/**
|
|
66
|
+
* Compute confidence in the score
|
|
67
|
+
*/
|
|
68
|
+
private computeConfidence;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=BranchScorer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BranchScorer.d.ts","sourceRoot":"","sources":["../../src/scorer/BranchScorer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,MAAM,EACN,WAAW,EACX,aAAa,EACb,eAAe,EAEf,aAAa,EACb,aAAa,EACd,MAAM,aAAa,CAAC;AAGrB;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB;AAYD;;GAEG;AACH,qBAAa,YAAa,YAAW,aAAa;IAChD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAkB;IACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IACvC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA+B;gBAG7D,cAAc,CAAC,EAAE,eAAe,EAChC,OAAO,GAAE,OAAO,CAAC,YAAY,CAAM;IAOrC;;OAEG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC;IAazE;;OAEG;IACG,UAAU,CACd,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,WAAW,EAAE,CAAC;IAIzB;;OAEG;IACH,MAAM,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI;IAMrC;;OAEG;IACH,UAAU,IAAI,YAAY;IAI1B;;OAEG;IACH,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,EAAE;IAIrD;;OAEG;YACW,iBAAiB;IA2B/B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAkB1B;;OAEG;IACH,OAAO,CAAC,cAAc;IAatB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAiB1B;;OAEG;IACH,OAAO,CAAC,aAAa;IASrB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAoB1B"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch Scorer
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Neural scoring for search branches
|
|
5
|
+
* Traces to: REQ-NS-001 (分岐スコアリング)
|
|
6
|
+
*/
|
|
7
|
+
import { EmbeddingModel } from './EmbeddingModel.js';
|
|
8
|
+
/**
|
|
9
|
+
* Default score weights
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_WEIGHTS = {
|
|
12
|
+
specAlignment: 0.4,
|
|
13
|
+
codeQuality: 0.3,
|
|
14
|
+
novelty: 0.15,
|
|
15
|
+
feasibility: 0.15,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Branch scorer implementation
|
|
19
|
+
*/
|
|
20
|
+
export class BranchScorer {
|
|
21
|
+
embeddingModel;
|
|
22
|
+
weights;
|
|
23
|
+
feedbackHistory;
|
|
24
|
+
constructor(embeddingModel, weights = {}) {
|
|
25
|
+
this.embeddingModel = embeddingModel ?? new EmbeddingModel();
|
|
26
|
+
this.weights = { ...DEFAULT_WEIGHTS, ...weights };
|
|
27
|
+
this.feedbackHistory = new Map();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Score a single branch
|
|
31
|
+
*/
|
|
32
|
+
async score(branch, context) {
|
|
33
|
+
const components = await this.computeComponents(branch, context);
|
|
34
|
+
const score = this.combineScores(components);
|
|
35
|
+
const confidence = this.computeConfidence(branch, context);
|
|
36
|
+
return {
|
|
37
|
+
branch,
|
|
38
|
+
score,
|
|
39
|
+
confidence,
|
|
40
|
+
components,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Score multiple branches in batch
|
|
45
|
+
*/
|
|
46
|
+
async scoreBatch(branches, context) {
|
|
47
|
+
return Promise.all(branches.map((branch) => this.score(branch, context)));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Update scorer with feedback
|
|
51
|
+
*/
|
|
52
|
+
update(feedback) {
|
|
53
|
+
const history = this.feedbackHistory.get(feedback.branchId) ?? [];
|
|
54
|
+
history.push(feedback);
|
|
55
|
+
this.feedbackHistory.set(feedback.branchId, history);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get current weights
|
|
59
|
+
*/
|
|
60
|
+
getWeights() {
|
|
61
|
+
return { ...this.weights };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get feedback history for a branch
|
|
65
|
+
*/
|
|
66
|
+
getFeedbackHistory(branchId) {
|
|
67
|
+
return this.feedbackHistory.get(branchId) ?? [];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Compute individual score components
|
|
71
|
+
*/
|
|
72
|
+
async computeComponents(branch, context) {
|
|
73
|
+
// Spec alignment: similarity between code embedding and spec embedding
|
|
74
|
+
const specAlignment = this.embeddingModel.similarity(branch.features.codeEmbedding, context.specEmbedding);
|
|
75
|
+
// Code quality: based on syntax and type checks
|
|
76
|
+
const codeQuality = this.computeCodeQuality(branch);
|
|
77
|
+
// Novelty: inverse of depth normalized
|
|
78
|
+
const novelty = this.computeNovelty(branch, context);
|
|
79
|
+
// Feasibility: based on complexity and constraints
|
|
80
|
+
const feasibility = this.computeFeasibility(branch, context);
|
|
81
|
+
return {
|
|
82
|
+
specAlignment,
|
|
83
|
+
codeQuality,
|
|
84
|
+
novelty,
|
|
85
|
+
feasibility,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Compute code quality score
|
|
90
|
+
*/
|
|
91
|
+
computeCodeQuality(branch) {
|
|
92
|
+
let quality = 0.5; // Base score
|
|
93
|
+
if (branch.features.syntaxValid) {
|
|
94
|
+
quality += 0.25;
|
|
95
|
+
}
|
|
96
|
+
if (branch.features.typeChecks) {
|
|
97
|
+
quality += 0.25;
|
|
98
|
+
}
|
|
99
|
+
// Penalize high complexity
|
|
100
|
+
const complexityPenalty = Math.min(0.3, branch.features.complexity / 100);
|
|
101
|
+
quality -= complexityPenalty;
|
|
102
|
+
return Math.max(0, Math.min(1, quality));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Compute novelty score
|
|
106
|
+
*/
|
|
107
|
+
computeNovelty(branch, context) {
|
|
108
|
+
// Base novelty from features
|
|
109
|
+
const baseNovelty = branch.features.novelty;
|
|
110
|
+
// Penalize deep exploration
|
|
111
|
+
const depthPenalty = Math.min(0.5, branch.to.depth / 20);
|
|
112
|
+
// Bonus for different path than history
|
|
113
|
+
const historyBonus = context.history.length > 0 ? 0.1 : 0;
|
|
114
|
+
return Math.max(0, Math.min(1, baseNovelty - depthPenalty + historyBonus));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Compute feasibility score
|
|
118
|
+
*/
|
|
119
|
+
computeFeasibility(branch, _context) {
|
|
120
|
+
// Start with spec similarity
|
|
121
|
+
let feasibility = branch.features.specSimilarity;
|
|
122
|
+
// Boost if syntax valid
|
|
123
|
+
if (branch.features.syntaxValid) {
|
|
124
|
+
feasibility += 0.2;
|
|
125
|
+
}
|
|
126
|
+
// Boost if types check
|
|
127
|
+
if (branch.features.typeChecks) {
|
|
128
|
+
feasibility += 0.2;
|
|
129
|
+
}
|
|
130
|
+
return Math.max(0, Math.min(1, feasibility));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Combine component scores with weights
|
|
134
|
+
*/
|
|
135
|
+
combineScores(components) {
|
|
136
|
+
return (components.specAlignment * this.weights.specAlignment +
|
|
137
|
+
components.codeQuality * this.weights.codeQuality +
|
|
138
|
+
components.novelty * this.weights.novelty +
|
|
139
|
+
components.feasibility * this.weights.feasibility);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Compute confidence in the score
|
|
143
|
+
*/
|
|
144
|
+
computeConfidence(branch, context) {
|
|
145
|
+
let confidence = 0.5; // Base confidence
|
|
146
|
+
// Higher confidence for valid code
|
|
147
|
+
if (branch.features.syntaxValid && branch.features.typeChecks) {
|
|
148
|
+
confidence += 0.2;
|
|
149
|
+
}
|
|
150
|
+
// Higher confidence with more history
|
|
151
|
+
if (context.history.length > 0) {
|
|
152
|
+
confidence += Math.min(0.2, context.history.length / 10);
|
|
153
|
+
}
|
|
154
|
+
// Lower confidence for high complexity
|
|
155
|
+
if (branch.features.complexity > 50) {
|
|
156
|
+
confidence -= 0.1;
|
|
157
|
+
}
|
|
158
|
+
return Math.max(0, Math.min(1, confidence));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=BranchScorer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BranchScorer.js","sourceRoot":"","sources":["../../src/scorer/BranchScorer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAYrD;;GAEG;AACH,MAAM,eAAe,GAAiB;IACpC,aAAa,EAAE,GAAG;IAClB,WAAW,EAAE,GAAG;IAChB,OAAO,EAAE,IAAI;IACb,WAAW,EAAE,IAAI;CAClB,CAAC;AAEF;;GAEG;AACH,MAAM,OAAO,YAAY;IACN,cAAc,CAAkB;IAChC,OAAO,CAAe;IACtB,eAAe,CAA+B;IAE/D,YACE,cAAgC,EAChC,UAAiC,EAAE;QAEnC,IAAI,CAAC,cAAc,GAAG,cAAc,IAAI,IAAI,cAAc,EAAE,CAAC;QAC7D,IAAI,CAAC,OAAO,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;QAClD,IAAI,CAAC,eAAe,GAAG,IAAI,GAAG,EAAE,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK,CAAC,MAAc,EAAE,OAAsB;QAChD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACjE,MAAM,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;QAC7C,MAAM,UAAU,GAAG,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE3D,OAAO;YACL,MAAM;YACN,KAAK;YACL,UAAU;YACV,UAAU;SACX,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CACd,QAAkB,EAClB,OAAsB;QAEtB,OAAO,OAAO,CAAC,GAAG,CAAC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AAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC;IAC9C,CAAC;CACF"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Encoder
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Encodes search context into embeddings
|
|
5
|
+
*/
|
|
6
|
+
import type { EncodedContext, IContextEncoder, IEmbeddingModel, SearchContext } from '../types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Context encoder implementation
|
|
9
|
+
*/
|
|
10
|
+
export declare class ContextEncoder implements IContextEncoder {
|
|
11
|
+
private readonly embeddingModel;
|
|
12
|
+
private patterns;
|
|
13
|
+
constructor(embeddingModel?: IEmbeddingModel);
|
|
14
|
+
/**
|
|
15
|
+
* Encode full search context
|
|
16
|
+
*/
|
|
17
|
+
encode(context: SearchContext): Promise<EncodedContext>;
|
|
18
|
+
/**
|
|
19
|
+
* Encode specification only
|
|
20
|
+
*/
|
|
21
|
+
encodeSpec(spec: string): Promise<EncodedContext>;
|
|
22
|
+
/**
|
|
23
|
+
* Update encoder with learned patterns
|
|
24
|
+
*/
|
|
25
|
+
updatePatterns(patterns: string[]): void;
|
|
26
|
+
/**
|
|
27
|
+
* Get learned patterns
|
|
28
|
+
*/
|
|
29
|
+
getPatterns(): string[];
|
|
30
|
+
/**
|
|
31
|
+
* Combine context elements into a single string
|
|
32
|
+
*/
|
|
33
|
+
private combineContextElements;
|
|
34
|
+
/**
|
|
35
|
+
* Extract features from full context
|
|
36
|
+
*/
|
|
37
|
+
private extractFeatures;
|
|
38
|
+
/**
|
|
39
|
+
* Extract features from specification only
|
|
40
|
+
*/
|
|
41
|
+
private extractSpecFeatures;
|
|
42
|
+
/**
|
|
43
|
+
* Estimate complexity of the context
|
|
44
|
+
*/
|
|
45
|
+
private estimateComplexity;
|
|
46
|
+
/**
|
|
47
|
+
* Estimate complexity of specification
|
|
48
|
+
*/
|
|
49
|
+
private estimateSpecComplexity;
|
|
50
|
+
/**
|
|
51
|
+
* Extract domain signals from text
|
|
52
|
+
*/
|
|
53
|
+
private extractDomainSignals;
|
|
54
|
+
/**
|
|
55
|
+
* Estimate token count
|
|
56
|
+
*/
|
|
57
|
+
private estimateTokens;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=ContextEncoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextEncoder.d.ts","sourceRoot":"","sources":["../../src/scorer/ContextEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAEV,cAAc,EACd,eAAe,EACf,eAAe,EACf,aAAa,EACd,MAAM,aAAa,CAAC;AAGrB;;GAEG;AACH,qBAAa,cAAe,YAAW,eAAe;IACpD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAkB;IACjD,OAAO,CAAC,QAAQ,CAAc;gBAElB,cAAc,CAAC,EAAE,eAAe;IAK5C;;OAEG;IACG,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC;IAa7D;;OAEG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAWvD;;OAEG;IACH,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI;IAIxC;;OAEG;IACH,WAAW,IAAI,MAAM,EAAE;IAIvB;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAe9B;;OAEG;IACH,OAAO,CAAC,eAAe;IASvB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAS3B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAO1B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAM9B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAkC5B;;OAEG;IACH,OAAO,CAAC,cAAc;CAIvB"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Encoder
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Encodes search context into embeddings
|
|
5
|
+
*/
|
|
6
|
+
import { EmbeddingModel } from './EmbeddingModel.js';
|
|
7
|
+
/**
|
|
8
|
+
* Context encoder implementation
|
|
9
|
+
*/
|
|
10
|
+
export class ContextEncoder {
|
|
11
|
+
embeddingModel;
|
|
12
|
+
patterns;
|
|
13
|
+
constructor(embeddingModel) {
|
|
14
|
+
this.embeddingModel = embeddingModel ?? new EmbeddingModel();
|
|
15
|
+
this.patterns = new Set();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Encode full search context
|
|
19
|
+
*/
|
|
20
|
+
async encode(context) {
|
|
21
|
+
// Combine specification with constraints
|
|
22
|
+
const combined = this.combineContextElements(context);
|
|
23
|
+
const embedding = await this.embeddingModel.embedSpec(combined);
|
|
24
|
+
const features = this.extractFeatures(context);
|
|
25
|
+
return {
|
|
26
|
+
embedding,
|
|
27
|
+
features,
|
|
28
|
+
tokens: this.estimateTokens(combined),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Encode specification only
|
|
33
|
+
*/
|
|
34
|
+
async encodeSpec(spec) {
|
|
35
|
+
const embedding = await this.embeddingModel.embedSpec(spec);
|
|
36
|
+
const features = this.extractSpecFeatures(spec);
|
|
37
|
+
return {
|
|
38
|
+
embedding,
|
|
39
|
+
features,
|
|
40
|
+
tokens: this.estimateTokens(spec),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Update encoder with learned patterns
|
|
45
|
+
*/
|
|
46
|
+
updatePatterns(patterns) {
|
|
47
|
+
patterns.forEach((p) => this.patterns.add(p));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get learned patterns
|
|
51
|
+
*/
|
|
52
|
+
getPatterns() {
|
|
53
|
+
return Array.from(this.patterns);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Combine context elements into a single string
|
|
57
|
+
*/
|
|
58
|
+
combineContextElements(context) {
|
|
59
|
+
const parts = [context.specification];
|
|
60
|
+
if (context.constraints.length > 0) {
|
|
61
|
+
parts.push(`Constraints: ${context.constraints.join(', ')}`);
|
|
62
|
+
}
|
|
63
|
+
if (context.history.length > 0) {
|
|
64
|
+
const lastState = context.history[context.history.length - 1];
|
|
65
|
+
parts.push(`Current state: ${lastState.code.substring(0, 100)}`);
|
|
66
|
+
}
|
|
67
|
+
return parts.join('\n');
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Extract features from full context
|
|
71
|
+
*/
|
|
72
|
+
extractFeatures(context) {
|
|
73
|
+
return {
|
|
74
|
+
specLength: context.specification.length,
|
|
75
|
+
constraintCount: context.constraints.length,
|
|
76
|
+
complexityEstimate: this.estimateComplexity(context),
|
|
77
|
+
domainSignals: this.extractDomainSignals(context.specification),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Extract features from specification only
|
|
82
|
+
*/
|
|
83
|
+
extractSpecFeatures(spec) {
|
|
84
|
+
return {
|
|
85
|
+
specLength: spec.length,
|
|
86
|
+
constraintCount: 0,
|
|
87
|
+
complexityEstimate: this.estimateSpecComplexity(spec),
|
|
88
|
+
domainSignals: this.extractDomainSignals(spec),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Estimate complexity of the context
|
|
93
|
+
*/
|
|
94
|
+
estimateComplexity(context) {
|
|
95
|
+
let complexity = context.specification.length / 100;
|
|
96
|
+
complexity += context.constraints.length * 0.5;
|
|
97
|
+
complexity += context.history.length * 0.2;
|
|
98
|
+
return Math.min(10, complexity);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Estimate complexity of specification
|
|
102
|
+
*/
|
|
103
|
+
estimateSpecComplexity(spec) {
|
|
104
|
+
const words = spec.split(/\s+/).length;
|
|
105
|
+
const sentences = spec.split(/[.!?]+/).length;
|
|
106
|
+
return Math.min(10, words / 50 + sentences / 5);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Extract domain signals from text
|
|
110
|
+
*/
|
|
111
|
+
extractDomainSignals(text) {
|
|
112
|
+
const signals = [];
|
|
113
|
+
const lowerText = text.toLowerCase();
|
|
114
|
+
// Programming domain signals
|
|
115
|
+
if (lowerText.includes('function') || lowerText.includes('method')) {
|
|
116
|
+
signals.push('function');
|
|
117
|
+
}
|
|
118
|
+
if (lowerText.includes('class') || lowerText.includes('interface')) {
|
|
119
|
+
signals.push('oop');
|
|
120
|
+
}
|
|
121
|
+
if (lowerText.includes('async') || lowerText.includes('await')) {
|
|
122
|
+
signals.push('async');
|
|
123
|
+
}
|
|
124
|
+
if (lowerText.includes('test') || lowerText.includes('spec')) {
|
|
125
|
+
signals.push('testing');
|
|
126
|
+
}
|
|
127
|
+
if (lowerText.includes('api') || lowerText.includes('endpoint')) {
|
|
128
|
+
signals.push('api');
|
|
129
|
+
}
|
|
130
|
+
if (lowerText.includes('database') || lowerText.includes('query')) {
|
|
131
|
+
signals.push('database');
|
|
132
|
+
}
|
|
133
|
+
// Check for learned patterns
|
|
134
|
+
this.patterns.forEach((pattern) => {
|
|
135
|
+
if (lowerText.includes(pattern.toLowerCase())) {
|
|
136
|
+
signals.push(`pattern:${pattern}`);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return signals;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Estimate token count
|
|
143
|
+
*/
|
|
144
|
+
estimateTokens(text) {
|
|
145
|
+
// Rough estimate: ~4 characters per token
|
|
146
|
+
return Math.ceil(text.length / 4);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=ContextEncoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextEncoder.js","sourceRoot":"","sources":["../../src/scorer/ContextEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO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AC,OAAO,CAAC,CAAC;QACxB,CAAC;QACD,IAAI,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YAC7D,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC1B,CAAC;QACD,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YAChE,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;QACD,IAAI,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAClE,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC3B,CAAC;QAED,6BAA6B;QAC7B,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAChC,IAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;gBAC9C,OAAO,CAAC,IAAI,CAAC,WAAW,OAAO,EAAE,CAAC,CAAC;YACrC,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;OAEG;IACK,cAAc,CAAC,IAAY;QACjC,0CAA0C;QAC1C,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Embedding Model
|
|
3
|
+
* @module @nahisaho/musubix-neural-search
|
|
4
|
+
* @description Neural embedding model for code and specifications
|
|
5
|
+
* Traces to: REQ-NS-001 (分岐スコアリング)
|
|
6
|
+
*/
|
|
7
|
+
import type { Embedding, EmbeddingConfig, IEmbeddingModel } from '../types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Simple hash-based embedding model (for testing without ML dependencies)
|
|
10
|
+
*/
|
|
11
|
+
export declare class EmbeddingModel implements IEmbeddingModel {
|
|
12
|
+
private readonly config;
|
|
13
|
+
constructor(config?: Partial<EmbeddingConfig>);
|
|
14
|
+
/**
|
|
15
|
+
* Embed source code
|
|
16
|
+
*/
|
|
17
|
+
embedCode(code: string): Promise<Embedding>;
|
|
18
|
+
/**
|
|
19
|
+
* Embed specification
|
|
20
|
+
*/
|
|
21
|
+
embedSpec(spec: string): Promise<Embedding>;
|
|
22
|
+
/**
|
|
23
|
+
* Calculate cosine similarity between embeddings
|
|
24
|
+
*/
|
|
25
|
+
similarity(a: Embedding, b: Embedding): number;
|
|
26
|
+
/**
|
|
27
|
+
* Batch embed multiple texts
|
|
28
|
+
*/
|
|
29
|
+
embedBatch(texts: string[]): Promise<Embedding[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Get configuration
|
|
32
|
+
*/
|
|
33
|
+
getConfig(): EmbeddingConfig;
|
|
34
|
+
/**
|
|
35
|
+
* Simple hash-based embedding (deterministic, for testing)
|
|
36
|
+
*/
|
|
37
|
+
private hashEmbed;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=EmbeddingModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmbeddingModel.d.ts","sourceRoot":"","sources":["../../src/scorer/EmbeddingModel.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAa/E;;GAEG;AACH,qBAAa,cAAe,YAAW,eAAe;IACpD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;gBAE7B,MAAM,GAAE,OAAO,CAAC,eAAe,CAAM;IAIjD;;OAEG;IACG,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAOjD;;OAEG;IACG,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAOjD;;OAEG;IACH,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM;IAyB9C;;OAEG;IACG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAIvD;;OAEG;IACH,SAAS,IAAI,eAAe;IAI5B;;OAEG;IACH,OAAO,CAAC,SAAS;CA0BlB"}
|