muthuk1 commited on
Commit
3b60e51
·
verified ·
1 Parent(s): 227eb8e

Upload integration/alwas-ml-client.js

Browse files
Files changed (1) hide show
  1. integration/alwas-ml-client.js +184 -0
integration/alwas-ml-client.js ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * ALWAS ML Client — Drop-in integration for Express.js backend
3
+ * Replaces Groq API with local ML models for complexity estimation,
4
+ * bottleneck detection, and completion time prediction.
5
+ *
6
+ * Usage:
7
+ * const ml = require('./alwas-ml-client');
8
+ * const estimate = await ml.estimateBlock({ block_type: 'PLL', tech_node: '7nm', ... });
9
+ */
10
+
11
+ const ML_API_URL = process.env.ML_API_URL || 'http://localhost:7860';
12
+
13
+ class ALWASMLClient {
14
+ constructor(baseUrl = ML_API_URL) {
15
+ this.baseUrl = baseUrl;
16
+ }
17
+
18
+ async _post(endpoint, data) {
19
+ const response = await fetch(`${this.baseUrl}${endpoint}`, {
20
+ method: 'POST',
21
+ headers: { 'Content-Type': 'application/json' },
22
+ body: JSON.stringify(data),
23
+ });
24
+ if (!response.ok) {
25
+ throw new Error(`ML API error: ${response.status} ${await response.text()}`);
26
+ }
27
+ return response.json();
28
+ }
29
+
30
+ async _get(endpoint) {
31
+ const response = await fetch(`${this.baseUrl}${endpoint}`);
32
+ if (!response.ok) {
33
+ throw new Error(`ML API error: ${response.status}`);
34
+ }
35
+ return response.json();
36
+ }
37
+
38
+ /**
39
+ * Estimate complexity and hours for a new block.
40
+ * DIRECT REPLACEMENT for Groq AI estimation endpoint.
41
+ *
42
+ * @param {Object} block - Block metadata
43
+ * @param {string} block.block_type - e.g., 'ADC', 'PLL', 'LDO'
44
+ * @param {string} block.tech_node - e.g., '7nm', '28nm'
45
+ * @param {string} [block.priority='P3-Medium'] - Priority level
46
+ * @param {number} [block.transistor_count] - Estimated transistor count
47
+ * @param {boolean} [block.has_dependencies=false]
48
+ * @param {number} [block.num_dependencies=0]
49
+ * @param {number} [block.constraint_complexity=1.0] - 0-3 scale
50
+ * @param {number} [block.drc_iterations=2]
51
+ * @param {number} [block.engineer_skill_factor=1.0] - 0.5-1.5
52
+ * @returns {Object} { complexity, estimated_hours, confidence, reasoning, ... }
53
+ */
54
+ async estimateBlock(block) {
55
+ return this._post('/predict/estimate', block);
56
+ }
57
+
58
+ /**
59
+ * Predict bottleneck risk for an in-progress block.
60
+ * Use in the hourly cron bottleneck scanner.
61
+ *
62
+ * @param {Object} block - Current block state
63
+ * @returns {Object} { risk_level, confidence, recommendations, should_alert }
64
+ */
65
+ async predictBottleneck(block) {
66
+ return this._post('/predict/bottleneck', block);
67
+ }
68
+
69
+ /**
70
+ * Predict remaining time to completion.
71
+ * Use in Block Detail page for "Est. Completion" row.
72
+ *
73
+ * @param {Object} block - Block with progress info
74
+ * @returns {Object} { remaining_hours, remaining_days, estimated_completion_date, progress_percent }
75
+ */
76
+ async predictCompletion(block) {
77
+ return this._post('/predict/completion', block);
78
+ }
79
+
80
+ /**
81
+ * Bulk estimate multiple blocks at once.
82
+ * Use with CSV bulk import feature.
83
+ *
84
+ * @param {Array<Object>} blocks - Array of block metadata
85
+ * @returns {Object} { count, estimates, total_estimated_hours }
86
+ */
87
+ async bulkEstimate(blocks) {
88
+ return this._post('/predict/bulk-estimate', { blocks });
89
+ }
90
+
91
+ /**
92
+ * Get model performance metrics.
93
+ * @returns {Object} metrics for all 4 models
94
+ */
95
+ async getMetrics() {
96
+ return this._get('/model/metrics');
97
+ }
98
+
99
+ /**
100
+ * Get supported block types, tech nodes, priorities.
101
+ * @returns {Object} { tech_nodes, block_types, priorities, stages }
102
+ */
103
+ async getSupportedValues() {
104
+ return this._get('/model/supported-values');
105
+ }
106
+
107
+ /**
108
+ * Health check
109
+ * @returns {Object} { status, models_loaded, timestamp }
110
+ */
111
+ async healthCheck() {
112
+ return this._get('/health');
113
+ }
114
+
115
+ /**
116
+ * Convert a Mongoose block document to ML API format for estimation.
117
+ * @param {Object} mongoBlock - Mongoose Block document
118
+ * @returns {Object} formatted for /predict/estimate
119
+ */
120
+ static formatForEstimate(mongoBlock) {
121
+ return {
122
+ block_type: mongoBlock.type || mongoBlock.blockType,
123
+ tech_node: mongoBlock.techNode || mongoBlock.tech_node,
124
+ priority: mongoBlock.priority || 'P3-Medium',
125
+ transistor_count: mongoBlock.transistorCount || mongoBlock.transistor_count,
126
+ has_dependencies: (mongoBlock.dependencies?.length || 0) > 0,
127
+ num_dependencies: mongoBlock.dependencies?.length || 0,
128
+ constraint_complexity: mongoBlock.constraintComplexity || 1.0,
129
+ drc_iterations: mongoBlock.drcIterations || 2,
130
+ engineer_skill_factor: 1.0, // default; update from engineer profile
131
+ };
132
+ }
133
+
134
+ /**
135
+ * Convert a Mongoose block document to ML API format for bottleneck prediction.
136
+ * @param {Object} mongoBlock - Mongoose Block document
137
+ * @param {number} daysSinceLastTransition - Days in current stage
138
+ * @returns {Object} formatted for /predict/bottleneck
139
+ */
140
+ static formatForBottleneck(mongoBlock, daysSinceLastTransition) {
141
+ return {
142
+ block_type: mongoBlock.type || mongoBlock.blockType,
143
+ tech_node: mongoBlock.techNode || mongoBlock.tech_node,
144
+ priority: mongoBlock.priority || 'P3-Medium',
145
+ estimated_hours: mongoBlock.estimatedHours || 20,
146
+ hours_logged: mongoBlock.hoursLogged || 0,
147
+ drc_iterations: mongoBlock.drcIterations || 2,
148
+ drc_violations_total: mongoBlock.drcViolations || 0,
149
+ lvs_mismatches_total: mongoBlock.lvsMismatches || 0,
150
+ current_stage: mongoBlock.status,
151
+ days_in_current_stage: daysSinceLastTransition,
152
+ engineer_skill_factor: 1.0,
153
+ is_overdue: mongoBlock.dueDate ? new Date() > new Date(mongoBlock.dueDate) : false,
154
+ };
155
+ }
156
+
157
+ /**
158
+ * Convert a Mongoose block document to ML API format for completion prediction.
159
+ * @param {Object} mongoBlock - Mongoose Block document
160
+ * @returns {Object} formatted for /predict/completion
161
+ */
162
+ static formatForCompletion(mongoBlock) {
163
+ const startDate = new Date(mongoBlock.createdAt || mongoBlock.startDate);
164
+ const now = new Date();
165
+ const daysSinceStart = (now - startDate) / (1000 * 60 * 60 * 24);
166
+
167
+ return {
168
+ block_type: mongoBlock.type || mongoBlock.blockType,
169
+ tech_node: mongoBlock.techNode || mongoBlock.tech_node,
170
+ priority: mongoBlock.priority || 'P3-Medium',
171
+ estimated_hours: mongoBlock.estimatedHours || 20,
172
+ engineer_skill_factor: 1.0,
173
+ drc_iterations: mongoBlock.drcIterations || 2,
174
+ current_stage: mongoBlock.status,
175
+ cumulative_hours: mongoBlock.hoursLogged || 0,
176
+ cumulative_days: daysSinceStart,
177
+ cumulative_drc_violations: mongoBlock.drcViolations || 0,
178
+ cumulative_lvs_mismatches: mongoBlock.lvsMismatches || 0,
179
+ };
180
+ }
181
+ }
182
+
183
+ module.exports = new ALWASMLClient();
184
+ module.exports.ALWASMLClient = ALWASMLClient;