@constructorfleet/sketchforge3d 1.2.1 → 1.4.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 +35 -0
- package/dist/client.d.ts +11 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +22 -0
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +34 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +67 -4
- package/dist/errors.js.map +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +4 -1
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/resources/faces.d.ts +17 -0
- package/dist/resources/faces.d.ts.map +1 -0
- package/dist/resources/faces.js +27 -0
- package/dist/resources/faces.js.map +1 -0
- package/dist/resources/shapes.d.ts +8 -0
- package/dist/resources/shapes.d.ts.map +1 -1
- package/dist/resources/shapes.js +25 -0
- package/dist/resources/shapes.js.map +1 -1
- package/dist/resources/shell.d.ts +20 -0
- package/dist/resources/shell.d.ts.map +1 -0
- package/dist/resources/shell.js +35 -0
- package/dist/resources/shell.js.map +1 -0
- package/dist/types.d.ts +131 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -1
- package/mcp/macroTools.mjs +239 -0
- package/mcp/server.mjs +605 -0
- package/package.json +4 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class Shell {
|
|
2
|
+
transport;
|
|
3
|
+
constructor(transport) {
|
|
4
|
+
this.transport = transport;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Hollows a solid out on the server, with OCCT.
|
|
8
|
+
*
|
|
9
|
+
* The result is exact, so it still exports to STEP and can still be filleted.
|
|
10
|
+
* Not every shape can be hollowed: a sphere, a torus, a cone and a tube's
|
|
11
|
+
* round wall are each one continuous curved surface, and OCCT refuses to
|
|
12
|
+
* offset them inwards at any thickness.
|
|
13
|
+
*/
|
|
14
|
+
async apply(projectId, shapeId, request) {
|
|
15
|
+
return this.transport.send({
|
|
16
|
+
method: "POST",
|
|
17
|
+
path: "/shell",
|
|
18
|
+
query: { projectId, shapeId },
|
|
19
|
+
body: {
|
|
20
|
+
thickness: request.thickness,
|
|
21
|
+
...(request.openFaces === undefined ? {} : { openFaces: [...request.openFaces] }),
|
|
22
|
+
},
|
|
23
|
+
retryable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** A sealed hollow: a wall all the way round, with the void closed in. */
|
|
27
|
+
hollow(projectId, shapeId, thickness) {
|
|
28
|
+
return this.apply(projectId, shapeId, { thickness });
|
|
29
|
+
}
|
|
30
|
+
/** The usual print: a box with its lid off, which is a tray. */
|
|
31
|
+
tray(projectId, shapeId, thickness, openFaces = ["top"]) {
|
|
32
|
+
return this.apply(projectId, shapeId, { thickness, openFaces });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=shell.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.js","sourceRoot":"","sources":["../../src/resources/shell.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,KAAK;IACa;IAA7B,YAA6B,SAAoB;QAApB,cAAS,GAAT,SAAS,CAAW;IAAG,CAAC;IAErD;;;;;;;OAOG;IACH,KAAK,CAAC,KAAK,CAAC,SAAiB,EAAE,OAAe,EAAE,OAAqB;QACnE,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAc;YACtC,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE;YAC7B,IAAI,EAAE;gBACJ,SAAS,EAAE,OAAO,CAAC,SAAS;gBAC5B,GAAG,CAAC,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;aAClF;YACD,SAAS,EAAE,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;IAED,0EAA0E;IAC1E,MAAM,CAAC,SAAiB,EAAE,OAAe,EAAE,SAAiB;QAC1D,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,gEAAgE;IAChE,IAAI,CAAC,SAAiB,EAAE,OAAe,EAAE,SAAiB,EAAE,YAAuC,CAAC,KAAK,CAAC;QACxG,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IAClE,CAAC;CACF"}
|
package/dist/types.d.ts
CHANGED
|
@@ -40,7 +40,18 @@ export type ShapeDimensions = {
|
|
|
40
40
|
/** Stands in for all three, which is what `cube` is for. */
|
|
41
41
|
size?: number;
|
|
42
42
|
};
|
|
43
|
-
export type LoftProfileShape = "
|
|
43
|
+
export type LoftProfileShape = "Oval" | "Rectangle" | "Triangle" | "Pentagon" | "Hexagon";
|
|
44
|
+
/**
|
|
45
|
+
* The taper every kind but `gear` carries: the footprint at each end of the
|
|
46
|
+
* extrusion, which need not match the shape's own width and depth.
|
|
47
|
+
*/
|
|
48
|
+
export type TaperFields = {
|
|
49
|
+
taperTopWidth?: number;
|
|
50
|
+
taperTopDepth?: number;
|
|
51
|
+
taperBottomWidth?: number;
|
|
52
|
+
taperBottomDepth?: number;
|
|
53
|
+
};
|
|
54
|
+
export type GearKindType = "spur" | "helical" | "bevel";
|
|
44
55
|
type KindFields = {
|
|
45
56
|
box: unknown;
|
|
46
57
|
cube: unknown;
|
|
@@ -51,6 +62,7 @@ type KindFields = {
|
|
|
51
62
|
cone: {
|
|
52
63
|
sides?: number;
|
|
53
64
|
topRadius?: number;
|
|
65
|
+
baseRadius?: number;
|
|
54
66
|
};
|
|
55
67
|
sphere: {
|
|
56
68
|
steps?: number;
|
|
@@ -95,13 +107,19 @@ type KindFields = {
|
|
|
95
107
|
toothSize?: number;
|
|
96
108
|
toothWidth?: number;
|
|
97
109
|
centerHoleSize?: number;
|
|
110
|
+
gearType?: GearKindType;
|
|
98
111
|
helixAngle?: number;
|
|
99
112
|
};
|
|
100
113
|
};
|
|
114
|
+
/** Every per-kind parameter, for a context that does not know the kind yet. */
|
|
115
|
+
export type AnyKindFields = {
|
|
116
|
+
[Kind in ParametricShapeKind]: KindFields[Kind] extends object ? KindFields[Kind] : Record<never, never>;
|
|
117
|
+
}[ParametricShapeKind] extends infer Union ? Partial<UnionToIntersection<Union>> : never;
|
|
118
|
+
type UnionToIntersection<Union> = (Union extends unknown ? (value: Union) => void : never) extends (value: infer Intersection) => void ? Intersection : never;
|
|
101
119
|
export type ParametricShapeRequest = {
|
|
102
120
|
[Kind in ParametricShapeKind]: {
|
|
103
121
|
kind: Kind;
|
|
104
|
-
} & ShapePlacement & ShapeDimensions & (KindFields[Kind] extends object ? KindFields[Kind] : Record<never, never>);
|
|
122
|
+
} & ShapePlacement & ShapeDimensions & (KindFields[Kind] extends object ? KindFields[Kind] : Record<never, never>) & (Kind extends "gear" ? Record<never, never> : TaperFields);
|
|
105
123
|
}[ParametricShapeKind];
|
|
106
124
|
/** A point of a revolve outline: `[distance from the axis, position along it]`. */
|
|
107
125
|
export type ProfilePoint = readonly [number, number];
|
|
@@ -139,22 +157,57 @@ export type ThreadShapeRequest = {
|
|
|
139
157
|
profile?: readonly ProfilePoint[];
|
|
140
158
|
} & ShapePlacement;
|
|
141
159
|
export type ShapeRequest = ParametricShapeRequest | RevolveShapeRequest | ThreadShapeRequest;
|
|
142
|
-
/**
|
|
143
|
-
|
|
160
|
+
/**
|
|
161
|
+
* The fields a PATCH may change.
|
|
162
|
+
*
|
|
163
|
+
* Everything a summary reports is here, which is the point: a shape can be read,
|
|
164
|
+
* one number changed, and the whole thing sent back. The kind is not in the type
|
|
165
|
+
* -- a summary reports it as a plain string, so there is nothing to narrow on --
|
|
166
|
+
* which means this is wider than any one shape accepts. The server refuses what
|
|
167
|
+
* the shape's kind has no use for, naming the fields in `fields` on a
|
|
168
|
+
* `422 unapplicable_fields`, rather than ignoring them as it used to.
|
|
169
|
+
*/
|
|
170
|
+
export type ShapePatch = ShapePlacement & ShapeDimensions & AnyKindFields & TaperFields & RevolveSettings & {
|
|
144
171
|
locked?: boolean;
|
|
145
172
|
hidden?: boolean;
|
|
173
|
+
/** Replaces a revolve's outline; the sweep is rebuilt on the server. */
|
|
174
|
+
profile?: readonly ProfilePoint[];
|
|
175
|
+
/** The sweep's settings as one object, for a caller that keeps them together. */
|
|
176
|
+
revolve?: RevolveSettings;
|
|
146
177
|
};
|
|
178
|
+
/** How far round a sweep goes, and how fast it rises, as the server reports it. */
|
|
179
|
+
export type RevolveSummary = {
|
|
180
|
+
startAngle: number;
|
|
181
|
+
sweepAngle: number;
|
|
182
|
+
/** Facets per full turn of the sweep, not a prism's side count. */
|
|
183
|
+
sides: number;
|
|
184
|
+
/** How finely a curved profile is sampled, 1 to 10. */
|
|
185
|
+
quality: number;
|
|
186
|
+
pitch: number;
|
|
187
|
+
turns: number;
|
|
188
|
+
coreRadius: number;
|
|
189
|
+
};
|
|
190
|
+
/** The operation a shape's geometry came out of, for the ones that have one. */
|
|
191
|
+
export type SketchOperation = "extrude" | "revolve" | "loft" | "sweep";
|
|
147
192
|
/**
|
|
148
193
|
* A shape as the server reports it.
|
|
149
194
|
*
|
|
150
195
|
* `kind` is the stored kind rather than the requested one: a `cube` comes back
|
|
151
196
|
* as a `box`, and a `revolve` or `thread` as a `mesh`, because that is what the
|
|
152
|
-
* project holds.
|
|
197
|
+
* project holds. `requestKind` is the other half of that -- the kind a `POST`
|
|
198
|
+
* would need to build the shape again, which is what tells a thread apart from
|
|
199
|
+
* an imported STL when both store as `mesh`. It is absent for a shape no request
|
|
200
|
+
* can reproduce, such as a boolean cut.
|
|
201
|
+
*
|
|
202
|
+
* Every per-kind parameter the server accepts also comes back, so a read,
|
|
203
|
+
* change and write loses nothing; which ones are present depends on the kind.
|
|
153
204
|
*/
|
|
154
205
|
export type ShapeSummary = {
|
|
155
206
|
id: string;
|
|
156
207
|
name: string;
|
|
157
208
|
kind: string;
|
|
209
|
+
requestKind?: ShapeKind;
|
|
210
|
+
sketchOperation?: SketchOperation;
|
|
158
211
|
color: string;
|
|
159
212
|
hole: boolean;
|
|
160
213
|
locked: boolean;
|
|
@@ -168,10 +221,39 @@ export type ShapeSummary = {
|
|
|
168
221
|
rotation: number;
|
|
169
222
|
rotationX: number;
|
|
170
223
|
rotationZ: number;
|
|
224
|
+
steps?: number;
|
|
171
225
|
sides?: number;
|
|
226
|
+
bevel?: number;
|
|
227
|
+
segments?: number;
|
|
228
|
+
topRadius?: number;
|
|
229
|
+
baseRadius?: number;
|
|
230
|
+
teeth?: number;
|
|
231
|
+
toothSize?: number;
|
|
232
|
+
toothWidth?: number;
|
|
233
|
+
centerHoleSize?: number;
|
|
234
|
+
gearType?: GearKindType;
|
|
235
|
+
helixAngle?: number;
|
|
236
|
+
loftBottomShape?: LoftProfileShape;
|
|
237
|
+
loftTopShape?: LoftProfileShape;
|
|
238
|
+
loftTopWidth?: number;
|
|
239
|
+
loftTopDepth?: number;
|
|
240
|
+
loftBottomRotation?: number;
|
|
241
|
+
loftTopRotation?: number;
|
|
242
|
+
loftSegments?: number;
|
|
243
|
+
loftLayers?: number;
|
|
172
244
|
text?: string;
|
|
245
|
+
font?: string;
|
|
246
|
+
/** Present on a revolve or a thread: the sweep that built it. */
|
|
247
|
+
revolve?: RevolveSummary;
|
|
248
|
+
/**
|
|
249
|
+
* The outline the sweep ran round, when it was described rather than drawn.
|
|
250
|
+
*
|
|
251
|
+
* A profile drawn in the editor can be several paths with curve handles, which
|
|
252
|
+
* no list of `[x, z]` pairs describes, so it is omitted rather than flattened.
|
|
253
|
+
*/
|
|
254
|
+
profile?: ProfilePoint[];
|
|
173
255
|
groupedCount?: number;
|
|
174
|
-
};
|
|
256
|
+
} & TaperFields;
|
|
175
257
|
export type ProjectSummary = {
|
|
176
258
|
id: string;
|
|
177
259
|
name: string;
|
|
@@ -219,6 +301,47 @@ export type EdgeTreatment = {
|
|
|
219
301
|
/** Edge indexes from the edge listing, or omitted for every edge that can be treated. */
|
|
220
302
|
edges?: readonly number[];
|
|
221
303
|
};
|
|
304
|
+
/**
|
|
305
|
+
* The directions a shell request can name a face by, in the shape's own frame:
|
|
306
|
+
* centred in X and Z with its base at the bottom, `front` towards the viewer.
|
|
307
|
+
*/
|
|
308
|
+
export declare const FACE_DIRECTIONS: readonly ["top", "bottom", "left", "right", "front", "back"];
|
|
309
|
+
export type FaceDirection = (typeof FACE_DIRECTIONS)[number];
|
|
310
|
+
export type FaceSummary = {
|
|
311
|
+
index: number;
|
|
312
|
+
/** As OCCT names the surface: `plane`, `cylinder`, `bspline`, or `unknown`. */
|
|
313
|
+
surfaceType: string;
|
|
314
|
+
area: number;
|
|
315
|
+
/** Area-weighted centre, which is a real point on the face even when curved. */
|
|
316
|
+
centroid: {
|
|
317
|
+
x: number;
|
|
318
|
+
y: number;
|
|
319
|
+
z: number;
|
|
320
|
+
};
|
|
321
|
+
/** The outward normal, or null for a surface that cannot report one. */
|
|
322
|
+
normal: {
|
|
323
|
+
x: number;
|
|
324
|
+
y: number;
|
|
325
|
+
z: number;
|
|
326
|
+
} | null;
|
|
327
|
+
/**
|
|
328
|
+
* The direction name this face answers to in a shell request.
|
|
329
|
+
*
|
|
330
|
+
* Only the outermost flat face pointing a given way takes the name: the floor
|
|
331
|
+
* of a hollow part's cavity points up as surely as its lid does, and opening
|
|
332
|
+
* the wrong one of those looks like nothing happened.
|
|
333
|
+
*/
|
|
334
|
+
facing: FaceDirection | null;
|
|
335
|
+
};
|
|
336
|
+
export type ShellRequest = {
|
|
337
|
+
/** Wall thickness in mm, measured inwards from the surface. */
|
|
338
|
+
thickness: number;
|
|
339
|
+
/**
|
|
340
|
+
* Faces to leave open, as direction names or as indexes from the face
|
|
341
|
+
* listing. Omitted or empty seals the hollow in.
|
|
342
|
+
*/
|
|
343
|
+
openFaces?: readonly (FaceDirection | number)[];
|
|
344
|
+
};
|
|
222
345
|
export type ShapeWriteResult = {
|
|
223
346
|
shapes: ShapeSummary[];
|
|
224
347
|
revision: number;
|
|
@@ -232,6 +355,8 @@ export type TreatmentResult = {
|
|
|
232
355
|
triangles: number;
|
|
233
356
|
revision: number;
|
|
234
357
|
};
|
|
358
|
+
/** The same three fields a treatment answers with; hollowing is one more of them. */
|
|
359
|
+
export type ShellResult = TreatmentResult;
|
|
235
360
|
export type DeleteResult = {
|
|
236
361
|
deleted: true;
|
|
237
362
|
id: string;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,qFAAqF;AACrF,eAAO,MAAM,sBAAsB,0LAmBzB,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE1E,yEAAyE;AACzE,MAAM,MAAM,SAAS,GAAG,mBAAmB,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEnE,oEAAoE;AACpE,eAAO,MAAM,mBAAmB,iKAoBtB,CAAC;AAEX,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAErE,mFAAmF;AACnF,eAAO,MAAM,aAAa,OAAO,CAAC;AAClC,eAAO,MAAM,aAAa,QAAS,CAAC;AAEpC,mDAAmD;AACnD,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAE1C,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iDAAiD;IACjD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,CAAC,CAAC,EAAE,MA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CAAC,EAAE,CAAC;CACjD,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAAE,MAAM,EAAE,YAAY,EAAE,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAC5E,MAAM,MAAM,gBAAgB,GAAG;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AACzE,MAAM,MAAM,eAAe,GAAG;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAC3F,qFAAqF;AACrF,MAAM,MAAM,WAAW,GAAG,eAAe,CAAC;AAC1C,MAAM,MAAM,YAAY,GAAG;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/dist/types.js
CHANGED
|
@@ -55,4 +55,9 @@ export const MIN_DIMENSION = 0.01;
|
|
|
55
55
|
export const MAX_DIMENSION = 10_000;
|
|
56
56
|
/** One POST may carry at most this many shapes. */
|
|
57
57
|
export const MAX_SHAPES_PER_REQUEST = 200;
|
|
58
|
+
/**
|
|
59
|
+
* The directions a shell request can name a face by, in the shape's own frame:
|
|
60
|
+
* centred in X and Z with its base at the bottom, `front` towards the viewer.
|
|
61
|
+
*/
|
|
62
|
+
export const FACE_DIRECTIONS = ["top", "bottom", "left", "right", "front", "back"];
|
|
58
63
|
//# sourceMappingURL=types.js.map
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,qFAAqF;AACrF,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,KAAK;IACL,MAAM;IACN,UAAU;IACV,MAAM;IACN,QAAQ;IACR,YAAY;IACZ,OAAO;IACP,MAAM;IACN,MAAM;IACN,SAAS;IACT,SAAS;IACT,MAAM;IACN,WAAW;IACX,OAAO;IACP,aAAa;IACb,MAAM;IACN,MAAM;IACN,MAAM;CACE,CAAC;AAOX,oEAAoE;AACpE,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,IAAI;IACJ,MAAM;IACN,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,OAAO;IACP,OAAO;IACP,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,QAAQ;CACA,CAAC;AAIX,mFAAmF;AACnF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC;AAClC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,mDAAmD;AACnD,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,qFAAqF;AACrF,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,KAAK;IACL,MAAM;IACN,UAAU;IACV,MAAM;IACN,QAAQ;IACR,YAAY;IACZ,OAAO;IACP,MAAM;IACN,MAAM;IACN,SAAS;IACT,SAAS;IACT,MAAM;IACN,WAAW;IACX,OAAO;IACP,aAAa;IACb,MAAM;IACN,MAAM;IACN,MAAM;CACE,CAAC;AAOX,oEAAoE;AACpE,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,IAAI;IACJ,MAAM;IACN,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,KAAK;IACL,KAAK;IACL,KAAK;IACL,KAAK;IACL,OAAO;IACP,OAAO;IACP,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,QAAQ;CACA,CAAC;AAIX,mFAAmF;AACnF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC;AAClC,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,mDAAmD;AACnD,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AA6R1C;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAU,CAAC"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running macros from the MCP bridge, by name only.
|
|
3
|
+
*
|
|
4
|
+
* `freecad-mcp` exposes an `execute_code` tool, which is remote code execution
|
|
5
|
+
* by design: whatever the model writes, the CAD process runs. This does not.
|
|
6
|
+
* The only thing an MCP client can ask for here is one of the files an operator
|
|
7
|
+
* put in `SKETCHFORGE_MACRO_DIR`, chosen from the listing this module produces,
|
|
8
|
+
* with parameters that reach the macro as strings. There is no inline-code tool
|
|
9
|
+
* and no opt-in flag that adds one: a macro is reviewed once, by the person who
|
|
10
|
+
* installed it, rather than on every call.
|
|
11
|
+
*
|
|
12
|
+
* With no macro directory configured, both tools report that and do nothing, so
|
|
13
|
+
* a default install exposes no way to run a local file at all.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { spawn } from "node:child_process";
|
|
17
|
+
import { existsSync } from "node:fs";
|
|
18
|
+
import { readdir, stat } from "node:fs/promises";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
|
|
22
|
+
export const MACRO_DIR_ENV = "SKETCHFORGE_MACRO_DIR";
|
|
23
|
+
export const MACRO_TIMEOUT_ENV = "SKETCHFORGE_MACRO_TIMEOUT_MS";
|
|
24
|
+
|
|
25
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
26
|
+
const MAX_OUTPUT_BYTES = 64 * 1024;
|
|
27
|
+
|
|
28
|
+
/** Macros are scripts for the two runners this repo ships; nothing else is executable. */
|
|
29
|
+
const RUNNABLE_EXTENSIONS = new Set([".mjs", ".js", ".py"]);
|
|
30
|
+
|
|
31
|
+
// Installed from npm, this module sits at <package>/mcp, so the JavaScript
|
|
32
|
+
// macro runner it spawns is its own package's `dist/cli.js` -- the same path it
|
|
33
|
+
// has in a checkout. The Python package only exists in a checkout; without it,
|
|
34
|
+
// a Python macro runs against a pip-installed `sketchforge3d`, which is what
|
|
35
|
+
// anyone running this from npx will have.
|
|
36
|
+
const moduleDirectory = path.dirname(fileURLToPath(import.meta.url));
|
|
37
|
+
const clientPackageRoot = path.resolve(moduleDirectory, "..");
|
|
38
|
+
const bundledPythonSource = path.resolve(clientPackageRoot, "..", "sketchforge-py", "src");
|
|
39
|
+
|
|
40
|
+
export class MacroToolError extends Error {}
|
|
41
|
+
|
|
42
|
+
export function macroDirectory(environment = process.env) {
|
|
43
|
+
const configured = environment[MACRO_DIR_ENV];
|
|
44
|
+
if (!configured) {
|
|
45
|
+
throw new MacroToolError(
|
|
46
|
+
`No macro directory is configured. Set ${MACRO_DIR_ENV} to a folder of reviewed macro files to enable this tool; ` +
|
|
47
|
+
"it runs only files that are already in that folder, never code sent to it.",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return path.resolve(configured);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The macros an operator has installed.
|
|
55
|
+
*
|
|
56
|
+
* The listing is also the allow-list: a run request is matched against these
|
|
57
|
+
* names, so a name is never joined onto a path and `../` cannot escape.
|
|
58
|
+
*/
|
|
59
|
+
export async function listMacros(environment = process.env) {
|
|
60
|
+
const directory = macroDirectory(environment);
|
|
61
|
+
let entries;
|
|
62
|
+
try {
|
|
63
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new MacroToolError(`${directory} could not be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const macros = [];
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (!entry.isFile() && !entry.isSymbolicLink()) continue;
|
|
71
|
+
const extension = path.extname(entry.name).toLowerCase();
|
|
72
|
+
if (!RUNNABLE_EXTENSIONS.has(extension)) continue;
|
|
73
|
+
const full = path.join(directory, entry.name);
|
|
74
|
+
const info = await stat(full).catch(() => null);
|
|
75
|
+
// A symlink out of the directory is still a file the operator put there on
|
|
76
|
+
// purpose; what matters is that the name came from this listing.
|
|
77
|
+
if (!info?.isFile()) continue;
|
|
78
|
+
macros.push({
|
|
79
|
+
name: entry.name,
|
|
80
|
+
language: extension === ".py" ? "python" : "javascript",
|
|
81
|
+
bytes: info.size,
|
|
82
|
+
modifiedAt: new Date(info.mtimeMs).toISOString(),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return { directory, macros: macros.sort((left, right) => left.name.localeCompare(right.name)) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function javascriptRunner(environment) {
|
|
89
|
+
const configured = environment.SKETCHFORGE_MACRO_RUNNER_JS;
|
|
90
|
+
if (configured) return configured;
|
|
91
|
+
const bundled = path.join(clientPackageRoot, "dist", "cli.js");
|
|
92
|
+
if (existsSync(bundled)) return bundled;
|
|
93
|
+
throw new MacroToolError(
|
|
94
|
+
"No JavaScript macro runner was found. Build it with `npm run build --prefix packages/sketchforge-client`, " +
|
|
95
|
+
"or point SKETCHFORGE_MACRO_RUNNER_JS at an installed sketchforge-macro.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function runnerFor(macroName, environment) {
|
|
100
|
+
if (path.extname(macroName).toLowerCase() === ".py") {
|
|
101
|
+
const python = environment.SKETCHFORGE_MACRO_PYTHON || "python3";
|
|
102
|
+
const pythonPath = existsSync(bundledPythonSource)
|
|
103
|
+
? [bundledPythonSource, environment.PYTHONPATH].filter(Boolean).join(path.delimiter)
|
|
104
|
+
: environment.PYTHONPATH;
|
|
105
|
+
return {
|
|
106
|
+
command: python,
|
|
107
|
+
leadingArguments: ["-m", "sketchforge3d.macro"],
|
|
108
|
+
extraEnvironment: pythonPath ? { PYTHONPATH: pythonPath } : {},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return { command: process.execPath, leadingArguments: [javascriptRunner(environment)], extraEnvironment: {} };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function parameterArguments(params) {
|
|
115
|
+
if (params === undefined || params === null) return [];
|
|
116
|
+
if (typeof params !== "object" || Array.isArray(params)) {
|
|
117
|
+
throw new MacroToolError("params must be an object of name/value pairs.");
|
|
118
|
+
}
|
|
119
|
+
return Object.entries(params).map(([name, value]) => {
|
|
120
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
121
|
+
throw new MacroToolError(`"${name}" is not a parameter name a macro can declare.`);
|
|
122
|
+
}
|
|
123
|
+
if (value !== null && typeof value === "object") {
|
|
124
|
+
throw new MacroToolError(`Parameter "${name}" must be a number, string or boolean.`);
|
|
125
|
+
}
|
|
126
|
+
return `--${name}=${String(value)}`;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function truncate(text) {
|
|
131
|
+
return text.length > MAX_OUTPUT_BYTES ? `${text.slice(0, MAX_OUTPUT_BYTES)}\n...output truncated...` : text;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Runs one installed macro and reports what the runner printed.
|
|
136
|
+
*
|
|
137
|
+
* `spawn` with an argument list rather than a shell: a parameter value is one
|
|
138
|
+
* argument whatever it contains, and no part of it is ever parsed as a command.
|
|
139
|
+
*/
|
|
140
|
+
export async function runMacroByName(request, options = {}) {
|
|
141
|
+
const environment = options.environment ?? process.env;
|
|
142
|
+
const spawnImpl = options.spawn ?? spawn;
|
|
143
|
+
const name = typeof request?.macro === "string" ? request.macro.trim() : "";
|
|
144
|
+
if (!name) throw new MacroToolError("Name the macro to run; sketchforge_list_macros shows what is installed.");
|
|
145
|
+
|
|
146
|
+
const { directory, macros } = await listMacros(environment);
|
|
147
|
+
const match = macros.find((macro) => macro.name === name);
|
|
148
|
+
if (!match) {
|
|
149
|
+
throw new MacroToolError(
|
|
150
|
+
`No macro named "${name}" is installed in ${directory}. Installed: ${macros.map((macro) => macro.name).join(", ") || "none"}.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const { command, leadingArguments, extraEnvironment } = runnerFor(match.name, environment);
|
|
155
|
+
const args = [...leadingArguments, path.join(directory, match.name), "--json", ...parameterArguments(request.params)];
|
|
156
|
+
if (typeof request.projectId === "string" && request.projectId) args.push(`--project=${request.projectId}`);
|
|
157
|
+
|
|
158
|
+
const timeoutMs = Number(request.timeoutMs ?? environment[MACRO_TIMEOUT_ENV] ?? DEFAULT_TIMEOUT_MS);
|
|
159
|
+
const started = Date.now();
|
|
160
|
+
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
const child = spawnImpl(command, args, {
|
|
163
|
+
cwd: directory,
|
|
164
|
+
env: { ...environment, ...extraEnvironment },
|
|
165
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
let stdout = "";
|
|
169
|
+
let stderr = "";
|
|
170
|
+
let timedOut = false;
|
|
171
|
+
const timer = setTimeout(() => {
|
|
172
|
+
timedOut = true;
|
|
173
|
+
child.kill("SIGKILL");
|
|
174
|
+
}, Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_TIMEOUT_MS);
|
|
175
|
+
|
|
176
|
+
child.stdout?.on("data", (chunk) => {
|
|
177
|
+
stdout += chunk;
|
|
178
|
+
});
|
|
179
|
+
child.stderr?.on("data", (chunk) => {
|
|
180
|
+
stderr += chunk;
|
|
181
|
+
});
|
|
182
|
+
child.on("error", (error) => {
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
reject(new MacroToolError(`${command} could not be run: ${error.message}`));
|
|
185
|
+
});
|
|
186
|
+
child.on("close", (code) => {
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
if (timedOut) {
|
|
189
|
+
reject(new MacroToolError(`${match.name} was still running after ${timeoutMs} ms and was stopped.`));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (code !== 0) {
|
|
193
|
+
reject(new MacroToolError(`${match.name} failed (exit ${code}): ${truncate(stderr || stdout).trim()}`));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
let report = null;
|
|
197
|
+
try {
|
|
198
|
+
report = JSON.parse(stdout);
|
|
199
|
+
} catch {
|
|
200
|
+
// A macro that printed something other than the runner's report is not
|
|
201
|
+
// an error; the text is what the caller gets.
|
|
202
|
+
}
|
|
203
|
+
resolve({
|
|
204
|
+
macro: match.name,
|
|
205
|
+
durationMs: Date.now() - started,
|
|
206
|
+
report,
|
|
207
|
+
...(report ? {} : { output: truncate(stdout).trim() }),
|
|
208
|
+
log: truncate(stderr).trim(),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const macroTools = [
|
|
215
|
+
{
|
|
216
|
+
name: "sketchforge_list_macros",
|
|
217
|
+
description:
|
|
218
|
+
"List the macro scripts an operator has installed in SKETCHFORGE_MACRO_DIR. These are the only macros sketchforge_run_macro will run.",
|
|
219
|
+
inputSchema: { type: "object", properties: {} },
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
name: "sketchforge_run_macro",
|
|
223
|
+
description:
|
|
224
|
+
"Run one installed macro by name, against the headless REST API. Parameters are the macro's own declared parameters. Only files already present in SKETCHFORGE_MACRO_DIR can be run; code cannot be passed in.",
|
|
225
|
+
inputSchema: {
|
|
226
|
+
type: "object",
|
|
227
|
+
required: ["macro"],
|
|
228
|
+
properties: {
|
|
229
|
+
macro: { type: "string", description: "A file name from sketchforge_list_macros." },
|
|
230
|
+
params: {
|
|
231
|
+
type: "object",
|
|
232
|
+
description: "The macro's parameters, as name/value pairs of numbers, strings or booleans.",
|
|
233
|
+
},
|
|
234
|
+
projectId: { type: "string", description: "The project to build in, for a macro declaring project: target." },
|
|
235
|
+
timeoutMs: { type: "number", description: "How long to let the macro run. Defaults to 120000." },
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
];
|