@constructorfleet/sketchforge3d 0.0.0-stage → 1.2.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.
Files changed (61) hide show
  1. package/README.md +165 -2
  2. package/dist/cli.d.ts +20 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +107 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/client.d.ts +62 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/client.js +110 -0
  9. package/dist/client.js.map +1 -0
  10. package/dist/environment.d.ts +20 -0
  11. package/dist/environment.d.ts.map +1 -0
  12. package/dist/environment.js +31 -0
  13. package/dist/environment.js.map +1 -0
  14. package/dist/errors.d.ts +34 -0
  15. package/dist/errors.d.ts.map +1 -0
  16. package/dist/errors.js +46 -0
  17. package/dist/errors.js.map +1 -0
  18. package/dist/http.d.ts +52 -0
  19. package/dist/http.d.ts.map +1 -0
  20. package/dist/http.js +112 -0
  21. package/dist/http.js.map +1 -0
  22. package/dist/index.d.ts +12 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +12 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/layout.d.ts +38 -0
  27. package/dist/layout.d.ts.map +1 -0
  28. package/dist/layout.js +53 -0
  29. package/dist/layout.js.map +1 -0
  30. package/dist/macro.d.ts +117 -0
  31. package/dist/macro.d.ts.map +1 -0
  32. package/dist/macro.js +154 -0
  33. package/dist/macro.js.map +1 -0
  34. package/dist/resources/edges.d.ts +11 -0
  35. package/dist/resources/edges.d.ts.map +1 -0
  36. package/dist/resources/edges.js +21 -0
  37. package/dist/resources/edges.js.map +1 -0
  38. package/dist/resources/projects.d.ts +14 -0
  39. package/dist/resources/projects.d.ts.map +1 -0
  40. package/dist/resources/projects.js +45 -0
  41. package/dist/resources/projects.js.map +1 -0
  42. package/dist/resources/shapes.d.ts +32 -0
  43. package/dist/resources/shapes.d.ts.map +1 -0
  44. package/dist/resources/shapes.js +100 -0
  45. package/dist/resources/shapes.js.map +1 -0
  46. package/dist/resources/treatments.d.ts +20 -0
  47. package/dist/resources/treatments.d.ts.map +1 -0
  48. package/dist/resources/treatments.js +39 -0
  49. package/dist/resources/treatments.js.map +1 -0
  50. package/dist/testing.d.ts +23 -0
  51. package/dist/testing.d.ts.map +1 -0
  52. package/dist/testing.js +34 -0
  53. package/dist/testing.js.map +1 -0
  54. package/dist/types.d.ts +241 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/types.js +58 -0
  57. package/dist/types.js.map +1 -0
  58. package/examples/bolt-circle.macro.mjs +75 -0
  59. package/examples/bolt-plate.macro.mjs +60 -0
  60. package/examples/filleted-box.macro.mjs +45 -0
  61. package/package.json +46 -4
package/README.md CHANGED
@@ -1,3 +1,166 @@
1
- # Temporary Holding Version
1
+ # @constructorfleet/sketchforge3d
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A typed client for the [SketchForge REST API](../../docs/REST_API.md), and a runner for **macros**: short parameterised scripts that build a part.
4
+
5
+ ```bash
6
+ npm install @constructorfleet/sketchforge3d
7
+ ```
8
+
9
+ The API is headless -- it reads and writes projects in server-side storage with no browser open -- so a macro is a build script for a design, the way a Makefile is for a binary.
10
+
11
+ ## Connecting
12
+
13
+ ```ts
14
+ import { SketchForgeClient } from "@constructorfleet/sketchforge3d";
15
+
16
+ const client = new SketchForgeClient({
17
+ baseUrl: "http://localhost:3000", // with or without /api/v1
18
+ token: process.env.SKETCHFORGE_API_TOKEN!,
19
+ });
20
+
21
+ const health = await client.check();
22
+ if (!health.ok) throw new Error(health.reason);
23
+ ```
24
+
25
+ `clientFromEnvironment()` does the same from `SKETCHFORGE_API_TOKEN` and `SKETCHFORGE_URL` (default `http://127.0.0.1:3000`). Prefer it: the token is a password for every project on the server and does not belong in a script or in shell history.
26
+
27
+ The server needs both `SKETCHFORGE_API_TOKEN` and `SKETCHFORGE_PROJECTS_DIR` set, or the API answers 404 and 503 respectively. `check()` tells you which.
28
+
29
+ ## The client
30
+
31
+ ```ts
32
+ new SketchForgeClient({ baseUrl, token, fetch?, timeoutMs?, retryOnBusy?, headers? })
33
+
34
+ client.apiRoot: string
35
+ client.check(): Promise<{ ok: true; projects: number } | { ok: false; reason: string }>
36
+ client.project(projectId): ProjectHandle
37
+
38
+ client.projects.list(): Promise<ProjectSummary[]>
39
+ client.projects.get(projectId): Promise<ProjectDetail>
40
+ client.projects.create(name): Promise<ProjectSummary>
41
+ client.projects.delete(projectId): Promise<DeleteResult>
42
+ client.projects.findByName(name): Promise<ProjectSummary | null>
43
+
44
+ client.shapes.list(projectId): Promise<ShapeSummary[]>
45
+ client.shapes.create(projectId, shape): Promise<ShapeSummary>
46
+ client.shapes.createMany(projectId, shapes): Promise<ShapeWriteResult>
47
+ client.shapes.update(projectId, shapeId, changes): Promise<ShapePatchResult>
48
+ client.shapes.delete(projectId, shapeId): Promise<DeleteResult>
49
+ client.shapes.deleteAll(projectId): Promise<number>
50
+
51
+ client.edges.list(projectId, shapeId): Promise<EdgeSummary[]>
52
+ client.edges.filletableIndexes(projectId, shapeId): Promise<number[]>
53
+
54
+ client.treatments.apply(projectId, shapeId, { kind, amount, angle?, edges? }): Promise<TreatmentResult>
55
+ client.treatments.fillet(projectId, shapeId, radius, edges?): Promise<TreatmentResult>
56
+ client.treatments.chamfer(projectId, shapeId, distance, { angle?, edges? }): Promise<TreatmentResult>
57
+ ```
58
+
59
+ A `ProjectHandle` is the same surface with the project id bound, which is what a macro works through:
60
+
61
+ ```ts
62
+ project.get() | delete() | listShapes() | add(shape) | addMany(shapes) | update(id, changes)
63
+ project.remove(id) | clear() | edges(id) | filletableEdges(id)
64
+ project.treat(id, treatment) | fillet(id, radius, edges?) | chamfer(id, distance, options?)
65
+ ```
66
+
67
+ Shapes are described as parameters, with a union per kind so a field that belongs to another kind is a compile error:
68
+
69
+ ```ts
70
+ await project.addMany([
71
+ { kind: "box", name: "Plate", width: 120, depth: 40, height: 6 },
72
+ { kind: "cylinder", width: 8, height: 12, sides: 6, x: -40, elevation: -1, hole: true },
73
+ { kind: "thread", size: "M8", length: 10, hole: true, x: 40 },
74
+ { kind: "revolve", profile: [[8, 0], [10, 0], [10, 2], [8, 2]], pitch: 6, turns: 6 },
75
+ ]);
76
+ ```
77
+
78
+ `addMany` is one write of the project package per request and splits lists longer than the server's 200-shape limit -- which also means a longer list is no longer atomic.
79
+
80
+ Failures are `SketchForgeApiError` (`status`, `detail`, plus `looksDisabled`, `needsStorage`, `isProjectBusy`) or `SketchForgeTransportError`. A write that lost the project's lock is retried; a *locked shape*, which answers 409 for an unrelated reason, is not.
81
+
82
+ ## Macros
83
+
84
+ A macro is a file that exports a definition and builds something when it is handed a connected project:
85
+
86
+ ```js
87
+ // macros/bolt-plate.macro.mjs
88
+ export default {
89
+ name: "Bolt plate",
90
+ project: { mode: "replace", name: "Bolt plate" },
91
+ params: { holes: 5, spacing: 25, size: "M8", thickness: 8 },
92
+
93
+ async run({ project, params, layout, log }) {
94
+ log(`${params.holes} ${params.size} holes`);
95
+ await project.addMany([
96
+ { kind: "box", name: "Plate", width: 200, depth: 40, height: params.thickness },
97
+ ...layout.row(params.holes, params.spacing).map((at, index) => ({
98
+ kind: "thread",
99
+ name: `hole ${index + 1}`,
100
+ size: params.size,
101
+ length: params.thickness + 2,
102
+ hole: true,
103
+ x: at.x,
104
+ z: at.z,
105
+ elevation: -1,
106
+ })),
107
+ ]);
108
+ },
109
+ };
110
+ ```
111
+
112
+ ```bash
113
+ SKETCHFORGE_API_TOKEN=... npx sketchforge-macro macros/bolt-plate.macro.mjs --holes=7 --spacing=30
114
+ ```
115
+
116
+ The context carries everything the macro needs -- `client`, `project`, `params`, `layout`, `log` -- so a macro file imports nothing and can live in any folder.
117
+
118
+ ### Project modes, and why there is no default
119
+
120
+ The second run is what makes a macro useful or dangerous. A script that appends to a project it found doubles the part every time, and nobody notices until they open the project. So the mode is declared and the runner, not the macro body, acts on it:
121
+
122
+ | `project` | What the runner does first | Use it for |
123
+ | --- | --- | --- |
124
+ | `"fresh"` | Creates a new project, name stamped with the time | Something you generate several of to compare |
125
+ | `{ mode: "replace", name }` | Finds that project and **empties** it, or creates it | A part you iterate on; re-running is idempotent |
126
+ | `{ mode: "reuse", name }` | Finds or creates it, and leaves the contents alone | Adding a feature to whatever is there |
127
+ | `"target"` | Requires `SKETCHFORGE_PROJECT_ID` or `--project`, touches nothing else | Operating on a project someone else named |
128
+
129
+ A module that exports a bare function instead of a definition runs in a `fresh` project, because that is the only mode that is safe to assume.
130
+
131
+ ### Parameters
132
+
133
+ `params` is both the defaults and the schema. A command-line override is coerced to the type of its default, and a name the macro does not declare is refused -- so `--hoels=7` is an error rather than a silently ignored typo. Parameters are resolved before the project is created, so a bad one costs nothing.
134
+
135
+ ## Examples
136
+
137
+ `examples/` holds the three worked macros, which double as the integration tests:
138
+
139
+ | Macro | Builds |
140
+ | --- | --- |
141
+ | `bolt-plate.macro.mjs` | A plate sized around a computed row of tapped holes |
142
+ | `bolt-circle.macro.mjs` | A flange with N tapped holes on a ring of radius R |
143
+ | `filleted-box.macro.mjs` | A box from three numbers, with every roundable edge filleted on the server |
144
+
145
+ Tapped holes are `kind: "thread"` with `hole: true`: the form is cut wider by a clearance and the shape is marked as a hole, so grouping it with the plate in the editor taps the plate. The API has no grouping endpoint yet, so the macros stop at marked holes.
146
+
147
+ ## Development
148
+
149
+ ```bash
150
+ npm run client:build # compile to dist/, from the repository root
151
+ npm run client:typecheck # including the tests
152
+ npm test # unit tests, in the repository's vitest run
153
+ npm run test:integration # the examples, against a live server
154
+ ```
155
+
156
+ The integration tests need a real server and are not part of `npm test`:
157
+
158
+ ```bash
159
+ SKETCHFORGE_PROJECTS_DIR=$(mktemp -d) SKETCHFORGE_API_TOKEN=test-token \
160
+ npx next dev apps/web -p 3100 &
161
+ SKETCHFORGE_API_TOKEN=test-token SKETCHFORGE_URL=http://127.0.0.1:3100 npm run test:integration
162
+ ```
163
+
164
+ ## Publishing
165
+
166
+ See [docs/PUBLISHING_CLIENTS.md](../../docs/PUBLISHING_CLIENTS.md) for what has to be configured on npm before the release workflow can publish this package.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `sketchforge-macro <file> [--name=value ...]`
4
+ *
5
+ * The macro file is imported, not evaluated from a string, and only ever from a
6
+ * path the person running the command typed. Nothing here accepts code over a
7
+ * socket.
8
+ */
9
+ import { type MacroRunReport } from "./macro.js";
10
+ export type ParsedArguments = {
11
+ file?: string;
12
+ params: Record<string, string>;
13
+ projectId?: string;
14
+ json: boolean;
15
+ help: boolean;
16
+ };
17
+ export declare function parseArguments(argv: readonly string[]): ParsedArguments;
18
+ export declare function describeReport(report: MacroRunReport): string;
19
+ export declare function main(argv: readonly string[]): Promise<1 | 0>;
20
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;;;;GAMG;AAMH,OAAO,EAA6B,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAS5E,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,OAAO,CAAC;CACf,CAAC;AAEF,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,eAAe,CAoBvE;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,UAYpD;AAED,wBAAsB,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,kBAoBjD"}
package/dist/cli.js ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `sketchforge-macro <file> [--name=value ...]`
4
+ *
5
+ * The macro file is imported, not evaluated from a string, and only ever from a
6
+ * path the person running the command typed. Nothing here accepts code over a
7
+ * socket.
8
+ */
9
+ import { pathToFileURL } from "node:url";
10
+ import path from "node:path";
11
+ import { clientFromEnvironment, targetProjectFromEnvironment, MissingConfigurationError } from "./environment.js";
12
+ import { SketchForgeApiError, SketchForgeTransportError } from "./errors.js";
13
+ import { macroFromModule, runMacro } from "./macro.js";
14
+ const USAGE = `sketchforge-macro <macro-file> [--param=value ...] [--project=<id>] [--json]
15
+
16
+ Environment:
17
+ SKETCHFORGE_API_TOKEN required; the server's API token
18
+ SKETCHFORGE_URL default http://127.0.0.1:3000
19
+ SKETCHFORGE_PROJECT_ID the project for a macro declaring project: "target"`;
20
+ export function parseArguments(argv) {
21
+ const parsed = { params: {}, json: false, help: false };
22
+ for (const argument of argv) {
23
+ if (argument === "--help" || argument === "-h") {
24
+ parsed.help = true;
25
+ }
26
+ else if (argument === "--json") {
27
+ parsed.json = true;
28
+ }
29
+ else if (argument.startsWith("--")) {
30
+ const [name, ...rest] = argument.slice(2).split("=");
31
+ if (!name || rest.length === 0)
32
+ throw new Error(`Expected --name=value, got "${argument}".`);
33
+ const value = rest.join("=");
34
+ if (name === "project")
35
+ parsed.projectId = value;
36
+ else
37
+ parsed.params[name] = value;
38
+ }
39
+ else if (!parsed.file) {
40
+ parsed.file = argument;
41
+ }
42
+ else {
43
+ throw new Error(`Unexpected argument "${argument}"; one macro file at a time.`);
44
+ }
45
+ }
46
+ return parsed;
47
+ }
48
+ export function describeReport(report) {
49
+ const lines = [
50
+ `macro ${report.macro}`,
51
+ `project ${report.projectId}${report.projectCreated ? " (created)" : ""}`,
52
+ `shapes ${report.shapeCount}`,
53
+ `took ${report.durationMs} ms`,
54
+ ];
55
+ if (report.shapesCleared)
56
+ lines.splice(2, 0, `cleared ${report.shapesCleared}`);
57
+ if (report.result !== null && report.result !== undefined) {
58
+ lines.push(`result ${JSON.stringify(report.result)}`);
59
+ }
60
+ return lines.join("\n");
61
+ }
62
+ export async function main(argv) {
63
+ const options = parseArguments(argv);
64
+ if (options.help || !options.file) {
65
+ process.stdout.write(`${USAGE}\n`);
66
+ return options.file ? 0 : 1;
67
+ }
68
+ const absolute = path.resolve(process.cwd(), options.file);
69
+ const module = (await import(pathToFileURL(absolute).href));
70
+ const definition = macroFromModule(module, path.basename(absolute).replace(/\.[cm]?[jt]s$/, ""));
71
+ const client = clientFromEnvironment();
72
+ const report = await runMacro(client, definition, {
73
+ params: options.params,
74
+ targetProjectId: options.projectId ?? targetProjectFromEnvironment(),
75
+ log: (message) => process.stderr.write(`${message}\n`),
76
+ });
77
+ process.stdout.write(options.json ? `${JSON.stringify(report, null, 2)}\n` : `${describeReport(report)}\n`);
78
+ return 0;
79
+ }
80
+ /** A failure a macro author can act on, rather than a stack trace through fetch. */
81
+ function explain(error) {
82
+ if (error instanceof MissingConfigurationError)
83
+ return error.message;
84
+ if (error instanceof SketchForgeTransportError) {
85
+ return `${error.message}\nIs the server running, and is SKETCHFORGE_URL pointing at it?`;
86
+ }
87
+ if (error instanceof SketchForgeApiError) {
88
+ if (error.looksDisabled)
89
+ return "The server has no SKETCHFORGE_API_TOKEN set, so its REST API is switched off.";
90
+ if (error.needsStorage)
91
+ return "The server has no SKETCHFORGE_PROJECTS_DIR set, so it has nowhere to store projects.";
92
+ return error.message;
93
+ }
94
+ return error instanceof Error ? (error.stack ?? error.message) : String(error);
95
+ }
96
+ const invokedDirectly = process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url;
97
+ if (invokedDirectly) {
98
+ main(process.argv.slice(2))
99
+ .then((code) => {
100
+ process.exitCode = code;
101
+ })
102
+ .catch((error) => {
103
+ process.stderr.write(`${explain(error)}\n`);
104
+ process.exitCode = 1;
105
+ });
106
+ }
107
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;;;;GAMG;A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O,iEAAiE,CAAC;IAC3F,CAAC;IACD,IAAI,KAAK,YAAY,mBAAmB,EAAE,CAAC;QACzC,IAAI,KAAK,CAAC,aAAa;YAAE,OAAO,+EAA+E,CAAC;QAChH,IAAI,KAAK,CAAC,YAAY;YAAE,OAAO,sFAAsF,CAAC;QACtH,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;IACD,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACjF,CAAC;AAED,MAAM,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;AACnG,IAAI,eAAe,EAAE,CAAC;IACpB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;SACxB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;QACb,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAC1B,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QACxB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5C,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The client, and the project handle a macro actually works through.
3
+ *
4
+ * Resources are plain objects hanging off the client, each owning one route.
5
+ * A route added to the API later is a new file here and one more field; nothing
6
+ * in the transport or the handle needs to know about it.
7
+ */
8
+ import { type TransportOptions } from "./http.js";
9
+ import { Edges } from "./resources/edges.js";
10
+ import { Projects } from "./resources/projects.js";
11
+ import { Shapes } from "./resources/shapes.js";
12
+ import { Treatments } from "./resources/treatments.js";
13
+ import type { EdgeSummary, EdgeTreatment, ProjectDetail, ShapePatch, ShapeRequest, ShapeSummary, ShapeWriteResult, TreatmentResult } from "./types.js";
14
+ export type SketchForgeClientOptions = TransportOptions;
15
+ export declare class SketchForgeClient {
16
+ readonly projects: Projects;
17
+ readonly shapes: Shapes;
18
+ readonly edges: Edges;
19
+ readonly treatments: Treatments;
20
+ private readonly transport;
21
+ constructor(options: SketchForgeClientOptions);
22
+ get apiRoot(): string;
23
+ /** A view of one project, so a macro does not thread its id through every call. */
24
+ project(projectId: string): ProjectHandle;
25
+ /**
26
+ * Whether the API is reachable, enabled, and has storage.
27
+ *
28
+ * Worth calling first in anything interactive: the three ways this fails --
29
+ * no token on the server, no project directory, wrong port -- are answered by
30
+ * a 404, a 503 and a connection error, none of which reads as a
31
+ * configuration mistake on its own.
32
+ */
33
+ check(): Promise<{
34
+ ok: true;
35
+ projects: number;
36
+ } | {
37
+ ok: false;
38
+ reason: string;
39
+ }>;
40
+ }
41
+ export declare class ProjectHandle {
42
+ private readonly client;
43
+ readonly id: string;
44
+ constructor(client: SketchForgeClient, id: string);
45
+ get(): Promise<ProjectDetail>;
46
+ delete(): Promise<import("./types.js").DeleteResult>;
47
+ listShapes(): Promise<ShapeSummary[]>;
48
+ add(shape: ShapeRequest): Promise<ShapeSummary>;
49
+ addMany(shapes: readonly ShapeRequest[]): Promise<ShapeWriteResult>;
50
+ update(shapeId: string, changes: ShapePatch): Promise<import("./types.js").ShapePatchResult>;
51
+ remove(shapeId: string): Promise<import("./types.js").DeleteResult>;
52
+ clear(): Promise<number>;
53
+ edges(shapeId: string): Promise<EdgeSummary[]>;
54
+ filletableEdges(shapeId: string): Promise<number[]>;
55
+ treat(shapeId: string, treatment: EdgeTreatment): Promise<TreatmentResult>;
56
+ fillet(shapeId: string, radius: number, edges?: readonly number[]): Promise<TreatmentResult>;
57
+ chamfer(shapeId: string, distance: number, options?: {
58
+ angle?: number;
59
+ edges?: readonly number[];
60
+ }): Promise<TreatmentResult>;
61
+ }
62
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,KAAK,EACV,WAAW,EACX,aAAa,EACb,aAAa,EACb,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,gBAAgB,EAChB,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,wBAAwB,GAAG,gBAAgB,CAAC;AAExD,qBAAa,iBAAiB;IAC5B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;gBAE1B,OAAO,EAAE,wBAAwB;IAQ7C,IAAI,OAAO,WAEV;IAED,mFAAmF;IACnF,OAAO,CAAC,SAAS,EAAE,MAAM;IAIzB;;;;;;;OAOG;IACG,KAAK,IAAI,OAAO,CAAC;QAAE,EAAE,EAAE,IAAI,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,EAAE,EAAE,KAAK,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;CAevF;AAED,qBAAa,aAAa;IAEtB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM;gBADF,MAAM,EAAE,iBAAiB,EACjC,EAAE,EAAE,MAAM;IAGrB,GAAG,IAAI,OAAO,CAAC,aAAa,CAAC;IAI7B,MAAM;IAIN,UAAU,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;IAIrC,GAAG,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAI/C,OAAO,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAInE,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU;IAI3C,MAAM,CAAC,OAAO,EAAE,MAAM;IAItB,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAIxB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAI9C,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAInD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC;IAI1E,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE;IAIjE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE;CAGnG"}
package/dist/client.js ADDED
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The client, and the project handle a macro actually works through.
3
+ *
4
+ * Resources are plain objects hanging off the client, each owning one route.
5
+ * A route added to the API later is a new file here and one more field; nothing
6
+ * in the transport or the handle needs to know about it.
7
+ */
8
+ import { SketchForgeApiError, SketchForgeTransportError } from "./errors.js";
9
+ import { Transport } from "./http.js";
10
+ import { Edges } from "./resources/edges.js";
11
+ import { Projects } from "./resources/projects.js";
12
+ import { Shapes } from "./resources/shapes.js";
13
+ import { Treatments } from "./resources/treatments.js";
14
+ export class SketchForgeClient {
15
+ projects;
16
+ shapes;
17
+ edges;
18
+ treatments;
19
+ transport;
20
+ constructor(options) {
21
+ this.transport = new Transport(options);
22
+ this.projects = new Projects(this.transport);
23
+ this.shapes = new Shapes(this.transport);
24
+ this.edges = new Edges(this.transport);
25
+ this.treatments = new Treatments(this.transport);
26
+ }
27
+ get apiRoot() {
28
+ return this.transport.apiRoot;
29
+ }
30
+ /** A view of one project, so a macro does not thread its id through every call. */
31
+ project(projectId) {
32
+ return new ProjectHandle(this, projectId);
33
+ }
34
+ /**
35
+ * Whether the API is reachable, enabled, and has storage.
36
+ *
37
+ * Worth calling first in anything interactive: the three ways this fails --
38
+ * no token on the server, no project directory, wrong port -- are answered by
39
+ * a 404, a 503 and a connection error, none of which reads as a
40
+ * configuration mistake on its own.
41
+ */
42
+ async check() {
43
+ try {
44
+ const projects = await this.projects.list();
45
+ return { ok: true, projects: projects.length };
46
+ }
47
+ catch (error) {
48
+ if (error instanceof SketchForgeApiError) {
49
+ if (error.looksDisabled)
50
+ return { ok: false, reason: "The server has no SKETCHFORGE_API_TOKEN set, so the API is off." };
51
+ if (error.needsStorage)
52
+ return { ok: false, reason: "The server has no SKETCHFORGE_PROJECTS_DIR set, so it can store nothing." };
53
+ if (error.status === 401)
54
+ return { ok: false, reason: "The token was refused." };
55
+ return { ok: false, reason: error.message };
56
+ }
57
+ if (error instanceof SketchForgeTransportError)
58
+ return { ok: false, reason: error.message };
59
+ throw error;
60
+ }
61
+ }
62
+ }
63
+ export class ProjectHandle {
64
+ client;
65
+ id;
66
+ constructor(client, id) {
67
+ this.client = client;
68
+ this.id = id;
69
+ }
70
+ get() {
71
+ return this.client.projects.get(this.id);
72
+ }
73
+ delete() {
74
+ return this.client.projects.delete(this.id);
75
+ }
76
+ listShapes() {
77
+ return this.client.shapes.list(this.id);
78
+ }
79
+ add(shape) {
80
+ return this.client.shapes.create(this.id, shape);
81
+ }
82
+ addMany(shapes) {
83
+ return this.client.shapes.createMany(this.id, shapes);
84
+ }
85
+ update(shapeId, changes) {
86
+ return this.client.shapes.update(this.id, shapeId, changes);
87
+ }
88
+ remove(shapeId) {
89
+ return this.client.shapes.delete(this.id, shapeId);
90
+ }
91
+ clear() {
92
+ return this.client.shapes.deleteAll(this.id);
93
+ }
94
+ edges(shapeId) {
95
+ return this.client.edges.list(this.id, shapeId);
96
+ }
97
+ filletableEdges(shapeId) {
98
+ return this.client.edges.filletableIndexes(this.id, shapeId);
99
+ }
100
+ treat(shapeId, treatment) {
101
+ return this.client.treatments.apply(this.id, shapeId, treatment);
102
+ }
103
+ fillet(shapeId, radius, edges) {
104
+ return this.client.treatments.fillet(this.id, shapeId, radius, edges);
105
+ }
106
+ chamfer(shapeId, distance, options) {
107
+ return this.client.treatments.chamfer(this.id, shapeId, distance, options);
108
+ }
109
+ }
110
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAyB,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAcvD,MAAM,OAAO,iBAAiB;IACnB,QAAQ,CAAW;IACnB,MAAM,CAAS;IACf,KAAK,CAAQ;IACb,UAAU,CAAa;IACf,SAAS,CAAY;IAEtC,YAAY,OAAiC;QAC3C,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MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,KAAK,CAAC,OAAe;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,eAAe,CAAC,OAAe;QAC7B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAC/D,CAAC;IAED,KAAK,CAAC,OAAe,EAAE,SAAwB;QAC7C,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,CAAC,OAAe,EAAE,MAAc,EAAE,KAAyB;QAC/D,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,CAAC,OAAe,EAAE,QAAgB,EAAE,OAAuD;QAChG,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC7E,CAAC;CACF"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Connecting from the environment, which is how a macro is meant to be run.
3
+ *
4
+ * The token is a password for every project on the server, so it belongs in the
5
+ * environment rather than in a macro's source or on a command line where it
6
+ * lands in shell history and in `ps`.
7
+ */
8
+ import { SketchForgeClient, type SketchForgeClientOptions } from "./client.js";
9
+ export declare const BASE_URL_ENV = "SKETCHFORGE_URL";
10
+ export declare const TOKEN_ENV = "SKETCHFORGE_API_TOKEN";
11
+ export declare const PROJECT_ENV = "SKETCHFORGE_PROJECT_ID";
12
+ export declare const DEFAULT_BASE_URL = "http://127.0.0.1:3000";
13
+ export type Environment = Record<string, string | undefined>;
14
+ export declare class MissingConfigurationError extends Error {
15
+ constructor(message: string);
16
+ }
17
+ export declare function clientFromEnvironment(environment?: Environment, overrides?: Partial<SketchForgeClientOptions>): SketchForgeClient;
18
+ /** The project an operator named for a `target` macro, if any. */
19
+ export declare function targetProjectFromEnvironment(environment?: Environment): string | undefined;
20
+ //# sourceMappingURL=environment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAE,KAAK,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAE/E,eAAO,MAAM,YAAY,oBAAoB,CAAC;AAC9C,eAAO,MAAM,SAAS,0BAA0B,CAAC;AACjD,eAAO,MAAM,WAAW,2BAA2B,CAAC;AAEpD,eAAO,MAAM,gBAAgB,0BAA0B,CAAC;AAExD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAE7D,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,EAAE,MAAM;CAI5B;AAED,wBAAgB,qBAAqB,CACnC,WAAW,GAAE,WAAyB,EACtC,SAAS,GAAE,OAAO,CAAC,wBAAwB,CAAM,GAChD,iBAAiB,CASnB;AAED,kEAAkE;AAClE,wBAAgB,4BAA4B,CAAC,WAAW,GAAE,WAAyB,sBAElF"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Connecting from the environment, which is how a macro is meant to be run.
3
+ *
4
+ * The token is a password for every project on the server, so it belongs in the
5
+ * environment rather than in a macro's source or on a command line where it
6
+ * lands in shell history and in `ps`.
7
+ */
8
+ import { SketchForgeClient } from "./client.js";
9
+ export const BASE_URL_ENV = "SKETCHFORGE_URL";
10
+ export const TOKEN_ENV = "SKETCHFORGE_API_TOKEN";
11
+ export const PROJECT_ENV = "SKETCHFORGE_PROJECT_ID";
12
+ export const DEFAULT_BASE_URL = "http://127.0.0.1:3000";
13
+ export class MissingConfigurationError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = "MissingConfigurationError";
17
+ }
18
+ }
19
+ export function clientFromEnvironment(environment = process.env, overrides = {}) {
20
+ const token = overrides.token ?? environment[TOKEN_ENV];
21
+ if (!token) {
22
+ throw new MissingConfigurationError(`${TOKEN_ENV} is not set. It is the same token the server was started with; without it every route answers 404.`);
23
+ }
24
+ const baseUrl = overrides.baseUrl ?? environment[BASE_URL_ENV] ?? DEFAULT_BASE_URL;
25
+ return new SketchForgeClient({ ...overrides, baseUrl, token });
26
+ }
27
+ /** The project an operator named for a `target` macro, if any. */
28
+ export function targetProjectFromEnvironment(environment = process.env) {
29
+ return environment[PROJECT_ENV] || undefined;
30
+ }
31
+ //# sourceMappingURL=environment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment.js","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAiC,MAAM,aAAa,CAAC;AAE/E,MAAM,CAAC,MAAM,YAAY,GAAG,iBAAiB,CAAC;AAC9C,MAAM,CAAC,MAAM,SAAS,GAAG,uBAAuB,CAAC;AACjD,MAAM,CAAC,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAEpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AAIxD,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF;AAED,MAAM,UAAU,qBAAqB,CACnC,cAA2B,OAAO,CAAC,GAAG,EACtC,YAA+C,EAAE;IAEjD,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;IACxD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,yBAAyB,CACjC,GAAG,SAAS,oGAAoG,CACjH,CAAC;IACJ,CAAC;IACD,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,IAAI,WAAW,CAAC,YAAY,CAAC,IAAI,gBAAgB,CAAC;IACnF,OAAO,IAAI,iBAAiB,CAAC,EAAE,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;AACjE,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,4BAA4B,CAAC,cAA2B,OAAO,CAAC,GAAG;IACjF,OAAO,WAAW,CAAC,WAAW,CAAC,IAAI,SAAS,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,34 @@
1
+ /** Errors the API answers with, kept distinguishable so a macro can react to them. */
2
+ export type ApiErrorContext = {
3
+ method: string;
4
+ url: string;
5
+ status: number;
6
+ /** The server's `error` string, when it sent one. */
7
+ detail?: string;
8
+ body?: unknown;
9
+ };
10
+ export declare class SketchForgeApiError extends Error {
11
+ readonly method: string;
12
+ readonly url: string;
13
+ readonly status: number;
14
+ readonly detail: string | undefined;
15
+ readonly body: unknown;
16
+ constructor(context: ApiErrorContext);
17
+ /**
18
+ * The API answers 404 for every route when `SKETCHFORGE_API_TOKEN` is unset,
19
+ * which is indistinguishable from a missing project by status alone -- so a
20
+ * 404 on the project list is the one that means "the API is switched off".
21
+ */
22
+ get looksDisabled(): boolean;
23
+ /** The server has a token but no project directory, so it can store nothing. */
24
+ get needsStorage(): boolean;
25
+ /** Another write holds the project's lock. Retrying is the right response. */
26
+ get isProjectBusy(): boolean;
27
+ }
28
+ /** The request never reached the server, or never came back. */
29
+ export declare class SketchForgeTransportError extends Error {
30
+ readonly method: string;
31
+ readonly url: string;
32
+ constructor(method: string, url: string, cause: unknown);
33
+ }
34
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,sFAAsF;AAEtF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;gBAEX,OAAO,EAAE,eAAe;IAUpC;;;;OAIG;IACH,IAAI,aAAa,YAEhB;IAED,gFAAgF;IAChF,IAAI,YAAY,YAEf;IAED,8EAA8E;IAC9E,IAAI,aAAa,YAEhB;CACF;AAED,gEAAgE;AAChE,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;gBAET,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAOxD"}
package/dist/errors.js ADDED
@@ -0,0 +1,46 @@
1
+ /** Errors the API answers with, kept distinguishable so a macro can react to them. */
2
+ export class SketchForgeApiError extends Error {
3
+ method;
4
+ url;
5
+ status;
6
+ detail;
7
+ body;
8
+ constructor(context) {
9
+ super(`${context.method} ${context.url} failed with ${context.status}: ${context.detail ?? "no detail"}`);
10
+ this.name = "SketchForgeApiError";
11
+ this.method = context.method;
12
+ this.url = context.url;
13
+ this.status = context.status;
14
+ this.detail = context.detail;
15
+ this.body = context.body;
16
+ }
17
+ /**
18
+ * The API answers 404 for every route when `SKETCHFORGE_API_TOKEN` is unset,
19
+ * which is indistinguishable from a missing project by status alone -- so a
20
+ * 404 on the project list is the one that means "the API is switched off".
21
+ */
22
+ get looksDisabled() {
23
+ return this.status === 404 && /disabled/i.test(this.detail ?? "");
24
+ }
25
+ /** The server has a token but no project directory, so it can store nothing. */
26
+ get needsStorage() {
27
+ return this.status === 503;
28
+ }
29
+ /** Another write holds the project's lock. Retrying is the right response. */
30
+ get isProjectBusy() {
31
+ return this.status === 409 && /being changed/i.test(this.detail ?? "");
32
+ }
33
+ }
34
+ /** The request never reached the server, or never came back. */
35
+ export class SketchForgeTransportError extends Error {
36
+ method;
37
+ url;
38
+ constructor(method, url, cause) {
39
+ super(`${method} ${url} could not be completed: ${cause instanceof Error ? cause.message : String(cause)}`);
40
+ this.name = "SketchForgeTransportError";
41
+ this.method = method;
42
+ this.url = url;
43
+ this.cause = cause;
44
+ }
45
+ }
46
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,sFAAsF;AAWtF,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,MAAM,CAAS;IACf,GAAG,CAAS;IACZ,MAAM,CAAS;IACf,MAAM,CAAqB;IAC3B,IAAI,CAAU;IAEvB,YAAY,OAAwB;QAClC,KAAK,CAAC,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,gBAAgB,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM,IAAI,WAAW,EAAE,CAAC,CAAC;QAC1G,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,I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