-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClaudeSwAddin.cs
More file actions
149 lines (135 loc) · 4.96 KB
/
Copy pathClaudeSwAddin.cs
File metadata and controls
149 lines (135 loc) · 4.96 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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using ClaudeSW.Api;
using ClaudeSW.Core;
using ClaudeSW.Security;
using ClaudeSW.Tools;
using ClaudeSW.UI;
using Microsoft.Win32;
using SolidWorks.Interop.swpublished;
namespace ClaudeSW
{
[ComVisible(true)]
[Guid("8A2B3C4D-5E6F-7A8B-9C0D-E1F2A3B4C5D6")]
[DisplayName("SolidWorks AI Assistant")]
[Description("Provider-agnostic AI assistant for SolidWorks")]
public class ClaudeSwAddin : ISwAddin
{
private const string AddinKeyTemplate = @"SOFTWARE\SolidWorks\Addins\{{{0}}}";
private const string StartupKeyTemplate = @"Software\SolidWorks\AddInsStartup\{{{0}}}";
private const string TitleRegKey = "Title";
private const string DescRegKey = "Description";
private dynamic _swApp;
private SwThreadMarshaller _marshaller;
private SwToolExecutor _toolExecutor;
private ModelProviderRegistry _providers;
private AgentSessionOrchestrator _orchestrator;
private ChatTaskPane _taskPane;
[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
try
{
string guid = t.GUID.ToString();
using (var key = Registry.LocalMachine.CreateSubKey(string.Format(AddinKeyTemplate, guid)))
{
key.SetValue(TitleRegKey, GetDisplayName(t));
key.SetValue(DescRegKey, GetDescription(t));
}
using (var key = Registry.CurrentUser.CreateSubKey(string.Format(StartupKeyTemplate, guid)))
{
key.SetValue(null, 1);
}
}
catch (Exception ex)
{
Debug.WriteLine("ClaudeSW COM registration failed: " + ex.Message);
}
}
[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
try
{
string guid = t.GUID.ToString();
Registry.LocalMachine.DeleteSubKey(string.Format(AddinKeyTemplate, guid), false);
Registry.CurrentUser.DeleteSubKey(string.Format(StartupKeyTemplate, guid), false);
}
catch (Exception ex)
{
Debug.WriteLine("ClaudeSW COM unregistration failed: " + ex.Message);
}
}
public bool ConnectToSW(object thisSW, int cookie)
{
try
{
_swApp = thisSW;
_marshaller = new SwThreadMarshaller();
_toolExecutor = new SwToolExecutor(_swApp, _marshaller.InvokeAsync);
_providers = new ModelProviderRegistry(new IModelProvider[]
{
new OpenAIModelProvider(),
new AnthropicModelProvider()
}, "openai");
foreach (var provider in _providers.GetProviderDescriptors())
{
if (!CredentialStore.HasApiKey(provider.Key))
continue;
try
{
_providers.SetApiKey(provider.Key, CredentialStore.LoadApiKey(provider.Key));
}
catch (Exception ex)
{
Debug.WriteLine("Failed to load credential for " + provider.Key + ": " + ex.Message);
}
}
_orchestrator = new AgentSessionOrchestrator(_providers);
_taskPane = new ChatTaskPane(_swApp, _orchestrator, _toolExecutor);
_taskPane.CreatePane();
Debug.WriteLine("SolidWorks AI Assistant loaded successfully.");
return true;
}
catch (Exception ex)
{
Debug.WriteLine("ClaudeSW ConnectToSW failed: " + ex);
return false;
}
}
public bool DisconnectFromSW()
{
try
{
_taskPane?.Dispose();
_providers?.Dispose();
_marshaller?.Dispose();
_swApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
return true;
}
catch
{
return false;
}
}
private static string GetDisplayName(Type t)
{
var attr = t.GetCustomAttributes(typeof(DisplayNameAttribute), false)
.OfType<DisplayNameAttribute>()
.FirstOrDefault();
return attr != null ? attr.DisplayName : t.Name;
}
private static string GetDescription(Type t)
{
var attr = t.GetCustomAttributes(typeof(DescriptionAttribute), false)
.OfType<DescriptionAttribute>()
.FirstOrDefault();
return attr != null ? attr.Description : "";
}
}
}