-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1263 lines (1021 loc) · 52.2 KB
/
Copy pathserver.js
File metadata and controls
1263 lines (1021 loc) · 52.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
139
140
141
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require('dotenv').config();
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const cors = require('cors');
const { getHeaders } = require('./auth');
const cadMemory = require('./memory');
const OnshapeChangeDetector = require('./changeDetection');
console.log('🔄 Starting server...');
console.log('📁 Current directory:', process.cwd());
console.log('📝 Environment variables loaded:', !!process.env.ONSHAPE_ACCESS_KEY);
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
console.log('✅ Express and Socket.io initialized');
// Middleware
app.use(express.json()); // Parse JSON bodies for webhooks
console.log('✅ Middleware configured');
// Function to call Ollama LLM
async function getLLMResponse(messages) {
try {
console.log('🤖 Calling Ollama...');
const response = await fetch('http://localhost:11434/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'llama3.2',
messages: messages,
stream: false
})
});
if (!response.ok) {
throw new Error(`Ollama API error: ${response.status}`);
}
const data = await response.json();
console.log('✅ Ollama response received');
return data.message.content;
} catch (error) {
console.error('❌ Ollama error:', error);
throw error;
}
}
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
console.log('✅ Middleware configured');
// Onshape configuration from your .env
const ONSHAPE_CONFIG = {
accessKey: process.env.ONSHAPE_ACCESS_KEY,
secretKey: process.env.ONSHAPE_SECRET_KEY,
docId: process.env.ONSHAPE_DOC_ID,
elemId: process.env.ONSHAPE_ELEM_ID,
workspaceId: process.env.ONSHAPE_WORKSPACE_ID,
baseUrl: 'https://cad.onshape.com'
};
console.log('✅ Onshape config loaded');
// Simple request tracking for debugging
const requestTracker = {
logRequest(type) {
console.log(`📡 API Request: ${type} at ${new Date().toISOString()}`);
}
};
// Function to get complete feature specification (needed for parameter modification)
async function getFeatureSpecs(featureId) {
const path = `/api/v10/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/features`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
console.log(`📋 Getting all features to find ${featureId}...`);
const headers = getHeaders('GET', fullUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
const response = await fetch(fullUrl, { method: 'GET', headers });
if (!response.ok) {
throw new Error(`Onshape API error: ${response.status}`);
}
const data = await response.json();
const feature = data.features.find(f => f.featureId === featureId);
if (!feature) {
throw new Error(`Feature ${featureId} not found`);
}
console.log(`✅ Feature specs retrieved for ${featureId}`);
return feature;
} catch (error) {
console.error(`❌ Error getting feature specs for ${featureId}:`, error);
throw error;
}
}
// Function to fetch current features using user credentials
async function fetchOnshapeFeatures(userCredentials, selectedDocument) {
// Use user credentials and document info instead of server config
const credentials = userCredentials || ONSHAPE_CONFIG;
const docInfo = selectedDocument || ONSHAPE_CONFIG;
const path = `/api/v6/partstudios/d/${docInfo.docId || docInfo.id}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/features`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
requestTracker.logRequest('Features');
const headers = getHeaders('GET', fullUrl, credentials.accessKey, credentials.secretKey);
const response = await fetch(fullUrl, { method: 'GET', headers });
if (response.status === 429) {
console.warn('⚠️ Rate limited - try again later');
throw new Error('Rate limited - please try again in a moment');
}
if (response.status === 401) {
console.error('❌ Authentication error (401) - check API credentials');
throw new Error('Authentication failed - check API credentials');
}
if (!response.ok) {
throw new Error(`Onshape API error: ${response.status}`);
}
const data = await response.json();
console.log('✅ Onshape features fetched');
return data.features;
} catch (error) {
console.error('❌ Error fetching Onshape features:', error);
throw error; // Let caller handle the error
}
}
// Removed fetchSingleFeature - we use the existing features list instead
// Function to force a complete rebuild by creating a temporary commit
async function forceCompleteRebuild() {
try {
console.log('🔄 Attempting complete rebuild...');
// Try getting the current microversion first
const featuresPath = `/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/features`;
const featuresUrl = `${ONSHAPE_CONFIG.baseUrl}${featuresPath}`;
const headers = getHeaders('GET', featuresUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
console.log('📡 Getting current microversion...');
const response = await fetch(featuresUrl, { method: 'GET', headers });
if (response.ok) {
const data = await response.json();
console.log('Current microversion:', data.sourceMicroversion);
// Try to force evaluation with different endpoints
const evalEndpoints = [
`/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/massproperties`,
`/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/boundingboxes`,
`/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/tessellatedfaces`,
];
for (const evalPath of evalEndpoints) {
try {
const evalUrl = `${ONSHAPE_CONFIG.baseUrl}${evalPath}`;
const evalHeaders = getHeaders('GET', evalUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
console.log('🔄 Force rebuild via:', evalPath);
const evalResponse = await fetch(evalUrl, { method: 'GET', headers: evalHeaders });
if (evalResponse.ok) {
console.log('✅ Rebuild successful via', evalPath);
// Check if microversion changed
const newFeaturesResponse = await fetch(featuresUrl, { method: 'GET', headers });
if (newFeaturesResponse.ok) {
const newData = await newFeaturesResponse.json();
console.log('New microversion:', newData.sourceMicroversion);
if (newData.sourceMicroversion !== data.sourceMicroversion) {
console.log('🎉 Microversion changed! Geometry should update.');
return;
}
}
}
} catch (error) {
console.log('❌ Rebuild attempt failed:', error.message);
}
}
}
console.log('⚠️ All rebuild attempts completed, but geometry may not have updated');
} catch (error) {
console.error('❌ Error in complete rebuild:', error.message);
}
}
// Function to update sketch constraints using a simplified approach
async function updateSketchConstraint(featureId, constraintId, newValue) {
// Try using the simpler constraint update approach
const path = `/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/features/featureid/${featureId}`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
console.log(`🔄 Updating constraint ${constraintId} to ${newValue}...`);
// First get the current feature
const headers = getHeaders('GET', fullUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
const getResponse = await fetch(fullUrl, { method: 'GET', headers });
if (!getResponse.ok) {
throw new Error(`Failed to get feature: ${getResponse.status}`);
}
const featureData = await getResponse.json();
console.log('Current feature retrieved');
// Find and update the constraint
if (featureData.feature && featureData.feature.constraints) {
for (let constraint of featureData.feature.constraints) {
if (constraint.constraintType === 'LENGTH') {
const lengthParam = constraint.parameters?.find(p => p.parameterId === 'length');
if (lengthParam) {
console.log(`Found length parameter: ${lengthParam.expression} → ${newValue}`);
lengthParam.expression = newValue;
}
}
}
}
// Update the feature with PUT
const putHeaders = getHeaders('PUT', fullUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
const putResponse = await fetch(fullUrl, {
method: 'PUT',
headers: putHeaders,
body: JSON.stringify(featureData)
});
if (!putResponse.ok) {
const errorText = await putResponse.text();
console.error('PUT Error:', errorText);
throw new Error(`Failed to update feature: ${putResponse.status}`);
}
const result = await putResponse.json();
console.log('✅ Sketch constraint updated successfully');
return result;
} catch (error) {
console.error('❌ Error updating sketch constraint:', error);
throw error;
}
}
// FeatureScript approach to modify sketch constraints with proper solving
async function updateViaFeatureScript(featureId, parameterId, newValue, oldValue) {
const path = `/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/featurescript`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
console.log(`🎯 FeatureScript: Modifying ${parameterId} from ${oldValue} to ${newValue}...`);
const headers = getHeaders('POST', fullUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
// FeatureScript to modify constraint and trigger solver
const featureScriptCode = `
FeatureScript 2024;
import(path : "onshape/std/common.fs", version : "2024.1.0");
import(path : "onshape/std/sketch.fs", version : "2024.1.0");
export function myFeature(context is Context, id is Id, definition is map)
{
try {
debug(context, "🎯 Starting FeatureScript constraint modification");
debug(context, "Looking for sketch: ${featureId}");
debug(context, "Target: ${parameterId} = ${newValue}");
// Find all features and look for our sketch
var allFeatures = qAllNonMeshSolidBodies(context);
debug(context, "Found " ~ size(evaluateQuery(context, allFeatures)) ~ " total features");
// Try to find the sketch by feature ID
var sketchQuery = qFeatureId("${featureId}");
var sketchFeatures = evaluateQuery(context, sketchQuery);
if (size(sketchFeatures) > 0)
{
debug(context, "✅ Found sketch feature");
// Force a regeneration to ensure constraints are solved
debug(context, "🔄 Forcing regeneration to solve constraints");
// The key insight: we need to modify the feature definition
// and then let Onshape's constraint solver do its work
debug(context, "✅ FeatureScript constraint modification completed");
} else {
debug(context, "❌ Sketch feature not found with ID: ${featureId}");
// List all feature IDs for debugging
var allFeaturesQuery = qAllNonMeshSolidBodies(context);
debug(context, "Available features in context:");
// This will help us understand what's available
}
} catch (error) {
debug(context, "❌ FeatureScript error: " ~ toString(error));
}
}`;
const requestBody = {
"script": featureScriptCode
};
console.log('🚀 Executing FeatureScript...');
const response = await fetch(fullUrl, {
method: 'POST',
headers,
body: JSON.stringify(requestBody)
});
if (!response.ok) {
const errorText = await response.text();
console.log(`❌ FeatureScript failed: ${response.status}`);
console.log('Error details:', errorText);
return null;
}
const data = await response.json();
console.log(`⚠️ FeatureScript executed but didn't modify parameters`);
// Look for debug output
if (data.result && data.result.message) {
console.log('FeatureScript debug output:', data.result.message);
}
// Return null to force fallback to REST API approach since FeatureScript isn't actually modifying parameters
return null;
} catch (error) {
console.error(`❌ FeatureScript error:`, error.message);
return null;
}
}
// Update Feature using the correct approach from research
async function updateFeature(featureId, completeFeatureSpecs) {
const path = `/api/v6/partstudios/d/${ONSHAPE_CONFIG.docId}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/features/featureid/${featureId}`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
console.log(`🔄 Updating feature ${featureId} with complete feature definition...`);
const headers = getHeaders('POST', fullUrl, ONSHAPE_CONFIG.accessKey, ONSHAPE_CONFIG.secretKey);
// Wrap in BTFeatureDefinitionCall structure
const featureCall = {
btType: "BTFeatureDefinitionCall-1406",
feature: completeFeatureSpecs
};
console.log('🔍 Feature call being sent:', JSON.stringify(featureCall, null, 2));
const response = await fetch(fullUrl, {
method: 'POST',
headers,
body: JSON.stringify(featureCall)
});
if (!response.ok) {
const errorText = await response.text();
console.error('API Error Response:', errorText);
throw new Error(`Onshape API error: ${response.status} - ${errorText}`);
}
const data = await response.json();
console.log(`✅ Feature ${featureId} updated successfully`);
return data;
} catch (error) {
console.error(`❌ Error updating feature ${featureId}:`, error);
throw error;
}
}
// Function to modify parameter in feature using the CORRECT approach from research
async function modifyFeatureParameter(featureId, parameterId, newValue) {
try {
console.log(`📝 Getting complete feature specs for ${featureId}...`);
// Step 1: Get the complete feature specification (the research shows this is key!)
const featureSpecs = await getFeatureSpecs(featureId);
console.log(`🔍 Feature structure:`, JSON.stringify(featureSpecs, null, 2));
// Step 2: Look for parameters in feature (the correct location per research!)
if (!featureSpecs.parameters) {
throw new Error(`Feature ${featureId} doesn't have parameters`);
}
const message = featureSpecs;
console.log(`🔍 Feature message parameters:`, message.parameters ? message.parameters.map(p => p.parameterId) : 'No parameters found');
// Step 3: Find and modify the parameter in feature.message
let foundParam = false;
let oldValue = 'unknown';
let normalizedValue = newValue;
// Normalize the parameter format to match Onshape expectations
if (newValue.includes('mm') && !newValue.includes(' ')) {
normalizedValue = newValue.replace('mm', ' mm');
}
// Look for parameters in message.parameters (this is where they actually are!)
if (message.parameters) {
const param = message.parameters.find(p => p.parameterId === parameterId);
if (param) {
oldValue = param.expression || param.value || 'unknown';
if (param.expression !== undefined) {
param.expression = normalizedValue;
} else if (param.value !== undefined) {
param.value = normalizedValue;
}
foundParam = true;
console.log(`✅ Modified ${parameterId} in message.parameters: ${oldValue} → ${normalizedValue}`);
}
}
if (!foundParam) {
console.error('❌ Parameter not found in feature.message. Available parameters:',
message.parameters ? message.parameters.map(p => p.parameterId) : 'None');
console.error('Full message structure:', JSON.stringify(message, null, 2));
throw new Error(`Parameter ${parameterId} not found in feature ${featureId} message`);
}
// Step 4: Return the complete feature specs with modification
return {
completeFeature: featureSpecs,
normalizedValue,
oldValue
};
} catch (error) {
console.error(`❌ Error modifying feature parameter:`, error);
throw error;
}
}
// Helper function to add delay between API calls
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to analyze features and extract current parameters
function analyzeFeatures(features) {
const analysis = features.map(feature => {
const type = feature.featureType.toLowerCase();
if (type === 'sketch' || type === 'newsketch') {
const constraints = feature.constraints || [];
const dimensions = constraints
.filter(c => c.constraintType === 'LENGTH')
.map(c => {
const lengthParam = c.parameters.find(p => p.parameterId === 'length');
return lengthParam ? lengthParam.expression : null;
})
.filter(d => d);
return {
id: feature.featureId,
type: feature.featureType,
name: feature.name,
dimensions: dimensions
};
}
if (type === 'extrude') {
const params = feature.parameters || [];
const depth = params.find(p => p.parameterId === 'depth');
return {
id: feature.featureId,
type: feature.featureType,
name: feature.name,
depth: depth ? depth.expression : null
};
}
return {
id: feature.featureId,
type: feature.featureType,
name: feature.name
};
});
return analysis;
}
// Memory system to track design intent and changes
let designMemory = {
modifications: [],
intents: {},
parameters: {}
};
function recordModification(featureId, parameterId, oldValue, newValue, intent = '') {
const modification = {
timestamp: new Date().toISOString(),
featureId,
parameterId,
oldValue,
newValue,
intent,
id: Date.now().toString()
};
designMemory.modifications.push(modification);
if (intent) {
designMemory.intents[`${featureId}_${parameterId}`] = intent;
}
designMemory.parameters[`${featureId}_${parameterId}`] = newValue;
console.log('📝 Recorded modification:', modification);
return modification;
}
function getDesignHistory() {
return designMemory;
}
// Helper function to get updated model data after modifications (reduces API calls)
async function getUpdatedModelData(userCredentials, selectedDocument) {
try {
console.log('📡 Fetching updated model data after modification...');
// Fetch both in parallel for efficiency
const [features, geometry] = await Promise.all([
fetchOnshapeFeatures(userCredentials, selectedDocument),
getGeometryForPreview(userCredentials, selectedDocument)
]);
return {
features: analyzeFeatures(features),
geometry: geometry
};
} catch (error) {
console.error('❌ Error fetching updated model data:', error);
throw error;
}
}
console.log('✅ Functions and memory system defined');
// Function to get 3D geometry for preview
async function getGeometryForPreview(userCredentials, selectedDocument) {
// Use user credentials and document info instead of server config
const credentials = userCredentials || ONSHAPE_CONFIG;
const docInfo = selectedDocument || ONSHAPE_CONFIG;
const path = `/api/v6/partstudios/d/${docInfo.docId || docInfo.id}/w/${ONSHAPE_CONFIG.workspaceId}/e/${ONSHAPE_CONFIG.elemId}/tessellatedfaces`;
const fullUrl = `${ONSHAPE_CONFIG.baseUrl}${path}`;
try {
requestTracker.logRequest('Geometry');
const headers = getHeaders('GET', fullUrl, credentials.accessKey, credentials.secretKey);
const response = await fetch(fullUrl + '?angleTolerance=0.1&chordTolerance=0.01', {
method: 'GET',
headers
});
if (response.status === 429) {
console.warn('⚠️ Rate limited - try again later');
throw new Error('Rate limited - please try again in a moment');
}
if (response.status === 401) {
console.error('❌ Authentication error (401) - check API credentials');
throw new Error('Authentication failed - check API credentials');
}
if (!response.ok) {
throw new Error(`Onshape API error: ${response.status}`);
}
const data = await response.json();
console.log('✅ Geometry fetched for preview');
console.log('📊 Geometry data structure:', Object.keys(data));
if (data.faces && data.faces.length > 0) {
console.log('📐 Found', data.faces.length, 'faces');
console.log('📍 First face sample:', data.faces[0] ? Object.keys(data.faces[0]) : 'none');
}
return data;
} catch (error) {
console.error('❌ Error fetching geometry:', error);
throw error; // Let caller handle the error
}
}
// Initialize webhook-based change detector
const changeDetector = new OnshapeChangeDetector(ONSHAPE_CONFIG, app);
// Socket.io connection handling
io.on('connection', async (socket) => {
console.log('🔌 User connected:', socket.id);
// Set up change detection for this client
const changeHandler = async (event) => {
console.log('📡 Document changed - notifying client:', socket.id);
// Only notify about the change - don't automatically fetch data
socket.emit('onshape_changed', {
event,
message: '🔄 Model updated externally in Onshape',
timestamp: new Date().toISOString()
});
};
changeDetector.addListener(changeHandler);
// Start webhook-based change detection if not already running
if (!changeDetector.isRunning) {
await changeDetector.start();
}
// Store user session data
let userSession = {
userCredentials: null,
selectedDocument: null
};
socket.on('user_connected', (data) => {
console.log('👤 User session initialized:', {
email: data.userCredentials.email,
document: data.selectedDocument.name
});
userSession.userCredentials = data.userCredentials;
userSession.selectedDocument = data.selectedDocument;
// TODO: Update ONSHAPE_CONFIG with user's credentials for this session
// For now, we'll handle this in individual socket events
});
socket.on('chat_message', async (data) => {
const { message } = data;
console.log('💬 Received message:', message);
try {
// Emit thinking status
socket.emit('bot_thinking', true);
// Get current Onshape features
const features = await fetchOnshapeFeatures(userSession.userCredentials, userSession.selectedDocument);
const analysis = analyzeFeatures(features);
// Get design memory for context - NEW MEMORY SYSTEM
const documentId = ONSHAPE_CONFIG.docId; // Use current document
const memoryContext = cadMemory.getMemoryContext(message, documentId);
console.log('🧠 Memory context:', memoryContext);
// Legacy memory for backward compatibility
const memory = getDesignHistory();
// First, check if the user message contains a direct MODIFY command
console.log('🔍 Checking for MODIFY command in user message:', message);
const userModifyMatch = message.match(/MODIFY:\s*featureId=([^,]+),\s*parameterId=([^,]+),\s*newValue=([^,]+),\s*intent=(.+)/i);
if (userModifyMatch) {
console.log('🔧 User sent direct MODIFY command');
const [, featureId, parameterId, newValue, intent] = userModifyMatch;
// Execute modification directly
socket.emit('modification_progress', 'Processing your modification request...');
try {
// Find the feature in the already-fetched features list
console.log('📋 Available features:', features.map(f => f.featureId + ' (' + f.featureType + ')'));
const currentFeature = features.find(f => f.featureId === featureId);
if (!currentFeature) {
throw new Error(`Feature ${featureId} not found. Available features: ${features.map(f => f.featureId).join(', ')}`);
}
console.log(`📋 Found feature ${featureId}:`, currentFeature.featureType);
let oldValue = 'unknown';
if (currentFeature.parameters) {
const param = currentFeature.parameters.find(p => p.parameterId === parameterId);
if (param) oldValue = param.expression;
}
// APPLY CHANGES IMMEDIATELY FOR 3D PREVIEW
console.log('🎯 Applying changes to Onshape for 3D preview...');
// Normalize the value format if needed
if (parameterId === 'depth' || parameterId === 'length' || parameterId === 'width' || parameterId === 'height') {
// Ensure proper unit format
if (!newValue.includes('mm') && !newValue.includes('in') && !newValue.includes('m')) {
newValue = newValue + 'mm'; // Default to mm
}
}
// Store original state for potential revert
const originalState = {
featureId,
parameterId,
originalValue: oldValue,
timestamp: new Date().toISOString()
};
let updateResult = null;
// Apply the change to Onshape immediately
try {
const featureScriptResult = await updateViaFeatureScript(featureId, parameterId, newValue, oldValue);
if (featureScriptResult) {
console.log('✅ FeatureScript execution completed');
updateResult = featureScriptResult;
} else {
console.log('⚠️ FeatureScript failed, using REST API...');
const result = await modifyFeatureParameter(featureId, parameterId, newValue);
updateResult = await updateFeature(featureId, result.completeFeature);
newValue = result.normalizedValue;
}
console.log('✅ Changes applied to Onshape for preview');
} catch (updateError) {
console.error('❌ Failed to apply changes:', updateError);
throw updateError;
}
socket.emit('bot_thinking', false);
// Fetch current 3D geometry for preview
console.log('🔄 Fetching updated geometry after applying changes...');
const currentGeometry = await getGeometryForPreview(userSession.userCredentials, userSession.selectedDocument);
console.log('📐 Got geometry for preview:', currentGeometry ? 'YES' : 'NO');
// Send preview with applied changes
socket.emit('modification_preview', {
featureId,
parameterId,
oldValue,
newValue,
intent: intent.trim(),
message: `Applied: Changed ${parameterId} from ${oldValue} to ${newValue}`,
currentFeature: currentFeature,
currentGeometry: currentGeometry,
originalState: originalState,
applied: true
});
return; // Skip normal chat response
} catch (error) {
console.error('❌ Error executing MODIFY command:', error);
socket.emit('modification_error', {
message: `Failed to execute modification: ${error.message}`,
error: error.message
});
return;
}
}
// Get LLM response using Ollama
const response = await getLLMResponse([
{
role: "system",
content: `You are an AI assistant that helps modify CAD models in Onshape with design memory.
Current model features:
${analysis.map(f => `- ${f.name} (${f.type}) - ID: ${f.id}: ${JSON.stringify(f)}`).join('\n')}
Raw feature IDs available:
${features.map(f => `- ${f.featureId} (${f.featureType})`).join('\n')}
DESIGN MEMORY CONTEXT:
${memoryContext.hasContext
? `📚 ${memoryContext.message}:
${memoryContext.context.map(ctx =>
`- ${ctx.summary} (similarity: ${Math.round(ctx.similarity * 100)}%)
Previous reasoning: ${ctx.reasoning}
Keywords matched: [${ctx.matchedKeywords.join(', ')}]`
).join('\n')}`
: '📝 No previous similar design decisions found - this is new territory!'
}
Legacy memory - Recent modifications:
${memory.modifications.slice(-5).map(m => `- ${m.timestamp}: Changed ${m.parameterId} from ${m.oldValue} to ${m.newValue}${m.intent ? ` (${m.intent})` : ''}`).join('\n') || 'No recent modifications'}
IMPORTANT: When the user asks to modify a parameter (like "make it wider", "change width to 150mm", etc.):
You MUST respond with EXACTLY this format using the ACTUAL featureId from the list above:
MODIFY: featureId=[actual_feature_id], parameterId=[actual_parameter], newValue=[new_value], intent=[reason]
Examples based on current features:
- User: "Make the base width 200mm for stability" → You respond: "MODIFY: featureId=${features.find(f => f.featureType === 'newSketch')?.featureId || 'FxMafT1VqIpbvjL_0'}, parameterId=length, newValue=200mm, intent=improve stability"
- User: "Change extrude depth to 25mm" → You respond: "MODIFY: featureId=${features.find(f => f.featureType === 'extrude')?.featureId || 'FidJ1G54jew8fO4_0'}, parameterId=depth, newValue=25mm, intent=depth adjustment"
CRITICAL: Use the exact featureId from the "Raw feature IDs available" list above - NOT generic names like "sketch1" or "extrude1".
For other questions about the model, respond normally.`
},
{
role: "user",
content: message
}
]);
console.log('🤖 AI Response:', response);
// Check if AI response contains a MODIFY command
const aiModifyMatch = response.match(/MODIFY:\s*featureId=([^,]+),\s*parameterId=([^,]+),\s*newValue=([^,]+),\s*intent=(.+)/i);
if (aiModifyMatch) {
console.log('🤖 AI responded with MODIFY command, executing...');
const [, featureId, parameterId, initialNewValue, intent] = aiModifyMatch;
let newValue = initialNewValue;
// Send preview instead of executing modification
socket.emit('modification_progress', 'Preparing preview...');
try {
// Find the feature in the already-fetched features list
console.log('📋 Available features:', features.map(f => f.featureId + ' (' + f.featureType + ')'));
const currentFeature = features.find(f => f.featureId === featureId);
if (!currentFeature) {
throw new Error(`Feature ${featureId} not found. Available features: ${features.map(f => f.featureId).join(', ')}`);
}
console.log(`📋 Found feature ${featureId}:`, currentFeature.featureType);
let oldValue = 'unknown';
if (currentFeature.parameters) {
const param = currentFeature.parameters.find(p => p.parameterId === parameterId);
if (param) oldValue = param.expression;
}
// APPLY CHANGES IMMEDIATELY FOR 3D PREVIEW
console.log('🎯 Applying changes to Onshape for 3D preview...');
// Normalize the value format if needed
if (parameterId === 'depth' || parameterId === 'length' || parameterId === 'width' || parameterId === 'height') {
// Ensure proper unit format
if (!newValue.includes('mm') && !newValue.includes('in') && !newValue.includes('m')) {
newValue = newValue + 'mm'; // Default to mm
}
}
// Store original state for potential revert
const originalState = {
featureId,
parameterId,
originalValue: oldValue,
timestamp: new Date().toISOString()
};
let updateResult = null;
// Apply the change to Onshape immediately
try {
const featureScriptResult = await updateViaFeatureScript(featureId, parameterId, newValue, oldValue);
if (featureScriptResult) {
console.log('✅ FeatureScript execution completed');
updateResult = featureScriptResult;
} else {
console.log('⚠️ FeatureScript failed, using REST API...');
const result = await modifyFeatureParameter(featureId, parameterId, newValue);
updateResult = await updateFeature(featureId, result.completeFeature);
newValue = result.normalizedValue;
}
console.log('✅ Changes applied to Onshape for preview');
} catch (updateError) {
console.error('❌ Failed to apply changes:', updateError);
throw updateError;
}
socket.emit('bot_thinking', false);
// Fetch current 3D geometry for preview
console.log('🔄 Fetching updated geometry after applying changes...');
const currentGeometry = await getGeometryForPreview(userSession.userCredentials, userSession.selectedDocument);
console.log('📐 Got geometry for preview:', currentGeometry ? 'YES' : 'NO');
// Send preview with applied changes
socket.emit('modification_preview', {
featureId,
parameterId,
oldValue,
newValue,
intent: intent.trim(),
message: `Applied: Changed ${parameterId} from ${oldValue} to ${newValue}`,
currentFeature: currentFeature,
currentGeometry: currentGeometry,
originalState: originalState,
applied: true
});
return; // Skip normal response
} catch (error) {
console.error('❌ Error executing AI MODIFY command:', error);
socket.emit('modification_error', {
message: `Failed to execute modification: ${error.message}`,
error: error.message
});
return;
}
}
// Store this design decision in memory
const memoryEntry = cadMemory.storeDecision({
documentId: documentId,
userId: socket.id, // Use socket ID as user identifier for now
userIntent: message,
originalRequest: message,
aiReasoning: response,
proposedChanges: {
type: 'chat_response',
response: response,
features: analysis
},
memoryContext: memoryContext.context || []
});
socket.emit('bot_thinking', false);
socket.emit('bot_response', {
message: response,
timestamp: new Date().toISOString(),
features: analysis,
memoryEntry: memoryEntry.id, // Include memory ID for potential feedback
memoryContext: memoryContext.hasContext ? {
found: memoryContext.context.length,
summary: memoryContext.context.map(ctx => ctx.summary)
} : null
});
} catch (error) {
console.error('❌ Error processing message:', error);
socket.emit('bot_thinking', false);
socket.emit('bot_response', {
message: 'Sorry, I encountered an error processing your request. Please try again.',
error: true,
timestamp: new Date().toISOString()
});
}
});
// Combined endpoint - gets both features and geometry in one request (50% fewer API calls)
socket.on('get_model_data', async () => {
try {
console.log('📡 Fetching complete model data (features + geometry)...');
// Fetch both features and geometry in parallel to save time
const [features, geometry] = await Promise.all([
fetchOnshapeFeatures(userSession.userCredentials, userSession.selectedDocument),
getGeometryForPreview(userSession.userCredentials, userSession.selectedDocument)
]);
const analysis = analyzeFeatures(features);
socket.emit('model_data_update', {
features: analysis,
geometry: geometry,
timestamp: new Date().toISOString()
});
console.log('✅ Complete model data sent to client');
} catch (error) {
console.error('❌ Error getting model data:', error);
socket.emit('model_data_error', error.message);
}
});
// Legacy endpoint for backward compatibility
socket.on('get_current_features', async () => {
try {
const features = await fetchOnshapeFeatures(userSession.userCredentials, userSession.selectedDocument);
const analysis = analyzeFeatures(features);
socket.emit('current_features', analysis);
} catch (error) {
console.error('❌ Error getting features:', error);
socket.emit('features_error', error.message);
}
});