@snaptrude/plugin-core 0.5.0 → 0.7.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 (134) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/api-manifest.full.json +2644 -542
  3. package/api-manifest.json +2606 -424
  4. package/dist/api/analysis/heatmaps.d.ts +234 -0
  5. package/dist/api/analysis/heatmaps.d.ts.map +1 -0
  6. package/dist/api/analysis/illuminance.d.ts +145 -0
  7. package/dist/api/analysis/illuminance.d.ts.map +1 -0
  8. package/dist/api/analysis/index.d.ts +44 -0
  9. package/dist/api/analysis/index.d.ts.map +1 -0
  10. package/dist/api/analysis/shadows.d.ts +165 -0
  11. package/dist/api/analysis/shadows.d.ts.map +1 -0
  12. package/dist/api/analysis/sunlightHours.d.ts +208 -0
  13. package/dist/api/analysis/sunlightHours.d.ts.map +1 -0
  14. package/dist/api/analysis/sunpath.d.ts +80 -0
  15. package/dist/api/analysis/sunpath.d.ts.map +1 -0
  16. package/dist/api/core/index.d.ts +10 -0
  17. package/dist/api/core/index.d.ts.map +1 -1
  18. package/dist/api/core/io/import/index.d.ts +392 -0
  19. package/dist/api/core/io/import/index.d.ts.map +1 -0
  20. package/dist/api/core/io/index.d.ts +35 -0
  21. package/dist/api/core/io/index.d.ts.map +1 -0
  22. package/dist/api/core/io/job/index.d.ts +139 -0
  23. package/dist/api/core/io/job/index.d.ts.map +1 -0
  24. package/dist/api/core/io/query/index.d.ts +74 -0
  25. package/dist/api/core/io/query/index.d.ts.map +1 -0
  26. package/dist/api/core/io/terrain/index.d.ts +206 -0
  27. package/dist/api/core/io/terrain/index.d.ts.map +1 -0
  28. package/dist/api/core/io/underlay/index.d.ts +286 -0
  29. package/dist/api/core/io/underlay/index.d.ts.map +1 -0
  30. package/dist/api/core/layers.d.ts +7 -7
  31. package/dist/api/core/proposals/index.d.ts +475 -0
  32. package/dist/api/core/proposals/index.d.ts.map +1 -0
  33. package/dist/api/design/create/index.d.ts +218 -0
  34. package/dist/api/design/create/index.d.ts.map +1 -1
  35. package/dist/api/design/doors/index.d.ts +159 -0
  36. package/dist/api/design/doors/index.d.ts.map +1 -1
  37. package/dist/api/design/furniture/index.d.ts +34 -5
  38. package/dist/api/design/furniture/index.d.ts.map +1 -1
  39. package/dist/api/design/index.d.ts +83 -0
  40. package/dist/api/design/index.d.ts.map +1 -1
  41. package/dist/api/design/lock.d.ts +26 -0
  42. package/dist/api/design/lock.d.ts.map +1 -1
  43. package/dist/api/design/materials/index.d.ts +159 -2
  44. package/dist/api/design/materials/index.d.ts.map +1 -1
  45. package/dist/api/design/query/index.d.ts +115 -5
  46. package/dist/api/design/query/index.d.ts.map +1 -1
  47. package/dist/api/design/query/spaces.d.ts +3 -3
  48. package/dist/api/design/selection/index.d.ts +144 -0
  49. package/dist/api/design/selection/index.d.ts.map +1 -1
  50. package/dist/api/design/transform/index.d.ts +83 -2
  51. package/dist/api/design/transform/index.d.ts.map +1 -1
  52. package/dist/api/design/update/index.d.ts +168 -0
  53. package/dist/api/design/update/index.d.ts.map +1 -1
  54. package/dist/api/design/windows/index.d.ts +113 -2
  55. package/dist/api/design/windows/index.d.ts.map +1 -1
  56. package/dist/api/entity/space.d.ts +2 -2
  57. package/dist/api/entity/story.d.ts +101 -7
  58. package/dist/api/entity/story.d.ts.map +1 -1
  59. package/dist/api/index.d.ts +5 -0
  60. package/dist/api/index.d.ts.map +1 -1
  61. package/dist/api/presentation/import.d.ts +49 -4
  62. package/dist/api/presentation/import.d.ts.map +1 -1
  63. package/dist/api/presentation/views.d.ts +210 -5
  64. package/dist/api/presentation/views.d.ts.map +1 -1
  65. package/dist/api/program/areas.d.ts +42 -12
  66. package/dist/api/program/areas.d.ts.map +1 -1
  67. package/dist/api/program/index.d.ts +3 -11
  68. package/dist/api/program/index.d.ts.map +1 -1
  69. package/dist/api/program/layout.d.ts +186 -11
  70. package/dist/api/program/layout.d.ts.map +1 -1
  71. package/dist/api/program/site.d.ts +84 -0
  72. package/dist/api/program/site.d.ts.map +1 -1
  73. package/dist/errors/codes.d.ts +34 -0
  74. package/dist/errors/codes.d.ts.map +1 -0
  75. package/dist/errors/envelope.d.ts +56 -0
  76. package/dist/errors/envelope.d.ts.map +1 -0
  77. package/dist/errors/index.d.ts +6 -0
  78. package/dist/errors/index.d.ts.map +1 -0
  79. package/dist/errors/plugin-error.d.ts +69 -0
  80. package/dist/errors/plugin-error.d.ts.map +1 -0
  81. package/dist/handles.d.ts +33 -0
  82. package/dist/handles.d.ts.map +1 -1
  83. package/dist/host-utils.d.ts +4 -0
  84. package/dist/host-utils.d.ts.map +1 -1
  85. package/dist/index.cjs +1942 -998
  86. package/dist/index.cjs.map +1 -1
  87. package/dist/index.d.ts +1 -0
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.js +1810 -994
  90. package/dist/index.js.map +1 -1
  91. package/package.json +2 -1
  92. package/scripts/generate-manifest.test.mjs +103 -4
  93. package/src/api/analysis/heatmaps.ts +256 -0
  94. package/src/api/analysis/illuminance.ts +155 -0
  95. package/src/api/analysis/index.ts +46 -0
  96. package/src/api/analysis/shadows.ts +183 -0
  97. package/src/api/analysis/sunlightHours.ts +211 -0
  98. package/src/api/analysis/sunpath.ts +83 -0
  99. package/src/api/core/index.ts +10 -0
  100. package/src/api/core/io/import/index.ts +432 -0
  101. package/src/api/core/io/index.ts +37 -0
  102. package/src/api/core/io/job/index.ts +140 -0
  103. package/src/api/core/io/query/index.ts +71 -0
  104. package/src/api/core/io/terrain/index.ts +214 -0
  105. package/src/api/core/io/underlay/index.ts +295 -0
  106. package/src/api/core/proposals/index.ts +509 -0
  107. package/src/api/design/create/index.ts +258 -0
  108. package/src/api/design/doors/index.ts +168 -0
  109. package/src/api/design/erase/index.ts +1 -1
  110. package/src/api/design/furniture/index.ts +36 -5
  111. package/src/api/design/index.ts +87 -0
  112. package/src/api/design/lock.ts +27 -0
  113. package/src/api/design/materials/index.ts +182 -2
  114. package/src/api/design/query/index.ts +81 -0
  115. package/src/api/design/selection/index.ts +129 -0
  116. package/src/api/design/transform/index.ts +74 -2
  117. package/src/api/design/update/index.ts +167 -0
  118. package/src/api/design/windows/index.ts +128 -2
  119. package/src/api/entity/story.ts +105 -7
  120. package/src/api/index.ts +5 -0
  121. package/src/api/presentation/import.ts +45 -4
  122. package/src/api/presentation/views.ts +221 -5
  123. package/src/api/program/areas.ts +34 -12
  124. package/src/api/program/index.ts +3 -11
  125. package/src/api/program/layout.ts +195 -11
  126. package/src/api/program/site.ts +93 -0
  127. package/src/errors/codes.ts +136 -0
  128. package/src/errors/envelope.ts +75 -0
  129. package/src/errors/index.ts +21 -0
  130. package/src/errors/plugin-error.ts +134 -0
  131. package/src/handles.ts +46 -0
  132. package/src/host-utils.ts +4 -0
  133. package/src/index.ts +1 -0
  134. package/test/errors.test.mjs +184 -0
@@ -127,6 +127,63 @@ export abstract class PluginProgramSiteApi {
127
127
  * ```
128
128
  */
129
129
  public abstract getContext(): PluginApiReturn<PluginProgramSiteContextResult>
130
+
131
+ /**
132
+ * Get the project's geographic location.
133
+ *
134
+ * The latitude/longitude the site is geo-located at — the location the
135
+ * `analysis.*` sun and daylight studies compute against. Returns `null`
136
+ * when the project is not geo-located on terrain (there is no fallback
137
+ * location).
138
+ *
139
+ * Note the spelling: this result uses the full `{ latitude, longitude }`
140
+ * keys, while the older site reads
141
+ * ({@linkcode PluginProgramSiteApi.getContext},
142
+ * {@linkcode PluginProgramSiteApi.listGeoPolygons}) use the abbreviated
143
+ * `{ lat, lng }`.
144
+ *
145
+ * @returns A {@linkcode PluginProgramSiteLocationResult} —
146
+ * `{ latitude, longitude }`, or `null` when the project has no
147
+ * geo-located site.
148
+ *
149
+ * @examplePrompt What's the project's latitude and longitude?
150
+ * @examplePrompt Where is this site located?
151
+ * @examplePrompt Get the geographic coordinates of the project
152
+ *
153
+ * # Example
154
+ * ```ts
155
+ * const location = await snaptrude.program.site.getLocation()
156
+ * if (location) console.log(location.latitude, location.longitude)
157
+ * ```
158
+ */
159
+ public abstract getLocation(): PluginApiReturn<PluginProgramSiteLocationResult>
160
+
161
+ /**
162
+ * Get the site's true-north angle.
163
+ *
164
+ * The angle in degrees the model's north (the scene's +Z axis) is rotated
165
+ * from true north, derived from the terrain's rotation. `0` means the
166
+ * model is aligned to true north. Reported in `[0, 360)`,
167
+ * **clockwise-positive** viewed from above (Babylon's +Y rotation sign) —
168
+ * note this is the opposite sign convention to `design.transform.rotate`,
169
+ * where positive = CCW. Returns `null` when the project is not
170
+ * geo-located on terrain.
171
+ *
172
+ * @returns A {@linkcode PluginProgramSiteNorthAngleResult} — the angle in
173
+ * degrees (`[0, 360)`, clockwise-positive), or `null` when the project
174
+ * has no geo-located site.
175
+ *
176
+ * @examplePrompt What's the north angle of the site?
177
+ * @examplePrompt How far is the model rotated from true north?
178
+ * @examplePrompt Get the project's true north direction
179
+ *
180
+ * # Example
181
+ * ```ts
182
+ * const northAngle = await snaptrude.program.site.getNorthAngle()
183
+ * if (northAngle !== null) console.log(`${northAngle}° from true north`)
184
+ * ```
185
+ */
186
+ public abstract getNorthAngle(): PluginApiReturn<PluginProgramSiteNorthAngleResult>
130
187
  }
131
188
 
132
189
  /**
@@ -324,3 +381,39 @@ export const PluginProgramSiteContextResult =
324
381
  export type PluginProgramSiteContextResult = z.infer<
325
382
  typeof PluginProgramSiteContextResult
326
383
  >
384
+
385
+ /**
386
+ * The project's geographic location (WGS84).
387
+ *
388
+ * | Property | Type | Description |
389
+ * |---|---|---|
390
+ * | `latitude` | `number` | Latitude (degrees) |
391
+ * | `longitude` | `number` | Longitude (degrees) |
392
+ */
393
+ export const PluginProgramSiteLocation = z.object({
394
+ latitude: z.number(),
395
+ longitude: z.number(),
396
+ })
397
+ export type PluginProgramSiteLocation = z.infer<
398
+ typeof PluginProgramSiteLocation
399
+ >
400
+
401
+ /**
402
+ * Result of {@linkcode PluginProgramSiteApi.getLocation} — the location, or
403
+ * `null` when the project is not geo-located.
404
+ */
405
+ export const PluginProgramSiteLocationResult =
406
+ PluginProgramSiteLocation.nullable()
407
+ export type PluginProgramSiteLocationResult = z.infer<
408
+ typeof PluginProgramSiteLocationResult
409
+ >
410
+
411
+ /**
412
+ * Result of {@linkcode PluginProgramSiteApi.getNorthAngle} — the true-north
413
+ * angle in degrees (`[0, 360)`, clockwise-positive), or `null` when the
414
+ * project is not geo-located.
415
+ */
416
+ export const PluginProgramSiteNorthAngleResult = z.number().nullable()
417
+ export type PluginProgramSiteNorthAngleResult = z.infer<
418
+ typeof PluginProgramSiteNorthAngleResult
419
+ >
@@ -0,0 +1,136 @@
1
+ /**
2
+ * The closed error-code registry — the single source of truth for every error
3
+ * that crosses the plugin wire (host → worker) or is minted client-side.
4
+ *
5
+ * APPEND-ONLY: codes are a published contract; never rename or remove one.
6
+ * Handle-resolution failures deliberately collapse to ONE code
7
+ * (`HANDLE_INVALID`) so error responses cannot be used as an existence /
8
+ * ownership oracle (PLUGIN_HANDLE_MIGRATION_HLD §6). `HANDLE_KIND_MISMATCH`
9
+ * stays distinct because the kind is information the caller already holds.
10
+ */
11
+ export const PLUGIN_ERROR_CODES = [
12
+ "VALIDATION",
13
+ "INVALID_RPC_FORMAT",
14
+ "METHOD_NOT_FOUND",
15
+ "METHOD_NOT_PERMITTED",
16
+ "HANDLE_INVALID",
17
+ "HANDLE_KIND_MISMATCH",
18
+ "RESOURCE_QUOTA_EXCEEDED",
19
+ "RATE_LIMITED",
20
+ "PRECONDITION_FAILED",
21
+ "OPERATION_FAILED",
22
+ "TIMEOUT",
23
+ "CALL_TIMEOUT",
24
+ "TRANSPORT_LOST",
25
+ "PLUGIN_NOT_READY",
26
+ "PLUGIN_TERMINATED",
27
+ "RESULT_NOT_SERIALIZABLE",
28
+ "RESULT_TOO_LARGE",
29
+ "BATCH_PARTIAL",
30
+ "STORY_UPDATE_FAILED",
31
+ "STORY_HEIGHT_REJECTED",
32
+ "NO_ACTIVE_STRUCTURE",
33
+ "NO_ELIGIBLE_ELEMENTS",
34
+ "PROPOSAL_SWITCH_BLOCKED",
35
+ "INTERNAL",
36
+ "UNKNOWN",
37
+ ] as const
38
+
39
+ export type PluginErrorCode = (typeof PLUGIN_ERROR_CODES)[number]
40
+
41
+ /**
42
+ * A known code, or a string outside the union — a plugin built against an
43
+ * older SDK may receive codes a newer host appended. Keeps autocomplete for
44
+ * known codes while staying open on the wire (forward compatibility).
45
+ */
46
+ export type WirePluginErrorCode = PluginErrorCode | (string & {})
47
+
48
+ export type PluginErrorCategory =
49
+ | "validation"
50
+ | "not_found"
51
+ | "permission"
52
+ | "handle"
53
+ | "quota"
54
+ | "timeout"
55
+ | "transport"
56
+ | "lifecycle"
57
+ | "execution"
58
+ | "internal"
59
+
60
+ /**
61
+ * Client-side metadata for a code. NOT carried on the wire — every field is a
62
+ * pure function of the code, and this table ships inside plugin-core, so both
63
+ * endpoints derive it locally.
64
+ */
65
+ export interface CodeMeta {
66
+ /** Family of the code; picks the `PluginError` sugar subclass. */
67
+ category: PluginErrorCategory
68
+ /** Fallback `hint` when the throw site provides none. */
69
+ hintTemplate?: string
70
+ }
71
+
72
+ export const CODE_META: Record<PluginErrorCode, CodeMeta> = {
73
+ VALIDATION: {
74
+ category: "validation",
75
+ hintTemplate: "Check details.issues for the failing argument.",
76
+ },
77
+ INVALID_RPC_FORMAT: { category: "validation" },
78
+ METHOD_NOT_FOUND: {
79
+ category: "not_found",
80
+ hintTemplate: "Check details.didYouMean for a close match.",
81
+ },
82
+ METHOD_NOT_PERMITTED: { category: "permission" },
83
+ HANDLE_INVALID: {
84
+ category: "handle",
85
+ hintTemplate: "Re-query the entity; do not cache handles across undo/reload.",
86
+ },
87
+ HANDLE_KIND_MISMATCH: { category: "handle" },
88
+ RESOURCE_QUOTA_EXCEEDED: {
89
+ category: "quota",
90
+ hintTemplate: "Release unused handles (core.handles.release) or use scopes.",
91
+ },
92
+ RATE_LIMITED: {
93
+ category: "quota",
94
+ hintTemplate: "Wait details.retryAfterMs before retrying.",
95
+ },
96
+ PRECONDITION_FAILED: { category: "execution" },
97
+ OPERATION_FAILED: { category: "execution" },
98
+ TIMEOUT: { category: "timeout" },
99
+ CALL_TIMEOUT: { category: "timeout" },
100
+ TRANSPORT_LOST: { category: "transport" },
101
+ PLUGIN_NOT_READY: { category: "lifecycle" },
102
+ PLUGIN_TERMINATED: { category: "lifecycle" },
103
+ RESULT_NOT_SERIALIZABLE: { category: "execution" },
104
+ RESULT_TOO_LARGE: { category: "execution" },
105
+ BATCH_PARTIAL: { category: "execution" },
106
+ STORY_UPDATE_FAILED: {
107
+ category: "execution",
108
+ hintTemplate: "The storey update could not be applied; re-read the storey and retry.",
109
+ },
110
+ STORY_HEIGHT_REJECTED: {
111
+ category: "execution",
112
+ hintTemplate: "The requested height was out of range; pick a value within the storey's limits.",
113
+ },
114
+ NO_ACTIVE_STRUCTURE: {
115
+ category: "execution",
116
+ hintTemplate: "Activate a structure before running this operation.",
117
+ },
118
+ NO_ELIGIBLE_ELEMENTS: {
119
+ category: "execution",
120
+ hintTemplate: "The storey has no elements eligible for this operation.",
121
+ },
122
+ PROPOSAL_SWITCH_BLOCKED: {
123
+ category: "execution",
124
+ hintTemplate: "Wait for the in-canvas agent run to finish, then retry.",
125
+ },
126
+ INTERNAL: {
127
+ category: "internal",
128
+ hintTemplate: "Host-side fault. Report with errorId.",
129
+ },
130
+ UNKNOWN: { category: "internal" },
131
+ }
132
+
133
+ /** Narrow an arbitrary string to a member of the closed code union. */
134
+ export function isPluginErrorCode(value: string): value is PluginErrorCode {
135
+ return (PLUGIN_ERROR_CODES as readonly string[]).includes(value)
136
+ }
@@ -0,0 +1,75 @@
1
+ import type { WirePluginErrorCode } from "./codes"
2
+
3
+ /**
4
+ * Structured error payload carried on the `{success: false}` branch of the RPC
5
+ * wire result, next to the legacy `error: string` field (additive — old
6
+ * clients ignore it, old hosts never set it).
7
+ *
8
+ * Errors cross the worker boundary as THIS plain object, never as a thrown
9
+ * class: Comlink's throw transfer strips subclasses and custom fields and
10
+ * leaks host stacks. plugin-client reconstructs a typed `PluginError` from it
11
+ * (`fromEnvelope`).
12
+ */
13
+ export const ERROR_ENVELOPE_VERSION = 1 as const
14
+
15
+ export interface ErrorEnvelope {
16
+ envelopeVersion: typeof ERROR_ENVELOPE_VERSION
17
+ code: WirePluginErrorCode
18
+ /**
19
+ * Human-readable failure description. For INTERNAL/UNKNOWN this is a GENERIC
20
+ * message — the real message and stack stay host-side, logged under
21
+ * `errorId` (the worker is untrusted; internals must not leak).
22
+ */
23
+ message: string
24
+ /**
25
+ * Per-occurrence id. For INTERNAL the host logs the real message and stack
26
+ * under this id (the sanitized message would otherwise be an undebuggable
27
+ * dead end). Other codes carry the id only to identify the occurrence in
28
+ * bug reports — the host does not log them. Client-minted ids carry a `c-`
29
+ * prefix.
30
+ */
31
+ errorId: string
32
+ /** Structured, JSON-safe, code-specific data (issues / retryAfterMs / didYouMean / handles…). */
33
+ details?: Record<string, unknown>
34
+ /**
35
+ * Wire id of the single primary offending handle, when the failure is
36
+ * handle-scoped. Multiple offenders go in `details.handles: string[]`.
37
+ * May echo ONLY ids the caller itself supplied (anti-oracle rule).
38
+ */
39
+ handle?: string
40
+ /** Dotted method path of the failed call (e.g. "design.boolean.union"). */
41
+ methodPath?: string
42
+ /** Machine-actionable fix suggestion (written for LLM plugin authors). */
43
+ hint?: string
44
+ }
45
+
46
+ /**
47
+ * Structural guard for a wire value claiming to be an `ErrorEnvelope`.
48
+ *
49
+ * Checks shape only — deliberately does NOT reject codes outside the known
50
+ * union, so envelopes from a newer host still pass (forward compatibility).
51
+ */
52
+ export function isErrorEnvelope(value: unknown): value is ErrorEnvelope {
53
+ if (typeof value !== "object" || value === null) return false
54
+ const record = value as Record<string, unknown>
55
+ return (
56
+ record.envelopeVersion === ERROR_ENVELOPE_VERSION &&
57
+ typeof record.code === "string" &&
58
+ typeof record.message === "string" &&
59
+ typeof record.errorId === "string"
60
+ )
61
+ }
62
+
63
+ let idCounter = 0
64
+
65
+ /**
66
+ * Mint a per-occurrence error id. Uses `crypto.randomUUID` when available
67
+ * (browser main thread and workers both have it); falls back to a
68
+ * counter+random suffix — uniqueness within a session is all that is needed.
69
+ */
70
+ export function newErrorId(prefix = ""): string {
71
+ const cryptoApi = (globalThis as { crypto?: { randomUUID?: () => string } }).crypto
72
+ if (cryptoApi?.randomUUID) return `${prefix}${cryptoApi.randomUUID()}`
73
+ idCounter += 1
74
+ return `${prefix}${Date.now().toString(36)}-${idCounter}-${Math.random().toString(36).slice(2, 10)}`
75
+ }
@@ -0,0 +1,21 @@
1
+ export { PLUGIN_ERROR_CODES, CODE_META, isPluginErrorCode } from "./codes"
2
+ export type { PluginErrorCode, WirePluginErrorCode, PluginErrorCategory, CodeMeta } from "./codes"
3
+
4
+ export { ERROR_ENVELOPE_VERSION, isErrorEnvelope, newErrorId } from "./envelope"
5
+ export type { ErrorEnvelope } from "./envelope"
6
+
7
+ export {
8
+ PluginError,
9
+ PluginValidationError,
10
+ PluginNotFoundError,
11
+ PluginPermissionError,
12
+ PluginHandleError,
13
+ PluginQuotaError,
14
+ PluginTimeoutError,
15
+ PluginTransportError,
16
+ PluginLifecycleError,
17
+ PluginExecutionError,
18
+ PluginInternalError,
19
+ fromEnvelope,
20
+ makeClientEnvelope,
21
+ } from "./plugin-error"
@@ -0,0 +1,134 @@
1
+ import { CODE_META, isPluginErrorCode } from "./codes"
2
+ import type { PluginErrorCategory, PluginErrorCode, WirePluginErrorCode } from "./codes"
3
+ import { ERROR_ENVELOPE_VERSION, newErrorId } from "./envelope"
4
+ import type { ErrorEnvelope } from "./envelope"
5
+
6
+ /**
7
+ * Cross-copy brand: a plugin bundle may contain its OWN copy of this module,
8
+ * in which case `instanceof` fails across the two class identities.
9
+ * `Symbol.for` resolves to the same symbol in every copy within a realm, so
10
+ * `PluginError.is()` keeps working. Canonical check = `.is()` + `code`.
11
+ */
12
+ const BRAND = Symbol.for("snaptrude.pluginError")
13
+
14
+ interface Branded {
15
+ [BRAND]?: boolean
16
+ }
17
+
18
+ /**
19
+ * Typed error every failed plugin API call rejects with.
20
+ *
21
+ * Rehydrated client-side from the wire `ErrorEnvelope` (`fromEnvelope`) — the
22
+ * class itself never crosses the worker boundary. Branch on the stable string
23
+ * `code`; use `PluginError.is()` rather than `instanceof` (a bundled duplicate
24
+ * SDK copy breaks `instanceof`, never the brand).
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * try {
29
+ * await snaptrude.design.boolean.union(handles)
30
+ * } catch (e) {
31
+ * if (PluginError.is(e) && e.code === "RATE_LIMITED") {
32
+ * await sleep(e.details?.retryAfterMs as number)
33
+ * } else {
34
+ * throw e
35
+ * }
36
+ * }
37
+ * ```
38
+ */
39
+ export class PluginError extends Error {
40
+ readonly code: WirePluginErrorCode
41
+ readonly errorId: string
42
+ readonly details?: Record<string, unknown>
43
+ readonly handle?: string
44
+ readonly methodPath?: string
45
+ readonly hint?: string
46
+
47
+ constructor(envelope: ErrorEnvelope) {
48
+ super(envelope.message)
49
+ this.name = new.target.name
50
+ this.code = envelope.code
51
+ this.errorId = envelope.errorId
52
+ this.details = envelope.details
53
+ this.handle = envelope.handle
54
+ this.methodPath = envelope.methodPath
55
+ this.hint = envelope.hint ?? hintFor(envelope.code)
56
+ ;(this as Branded)[BRAND] = true
57
+ }
58
+
59
+ /** Canonical check — survives bundled duplicate SDK copies where `instanceof` fails. */
60
+ static is(value: unknown): value is PluginError {
61
+ return (
62
+ typeof value === "object" &&
63
+ value !== null &&
64
+ (value as Branded)[BRAND] === true
65
+ )
66
+ }
67
+
68
+ /** Derived (not wire-carried) family of the code; unknown codes map to "internal". */
69
+ get category(): PluginErrorCategory {
70
+ return isPluginErrorCode(this.code) ? CODE_META[this.code].category : "internal"
71
+ }
72
+ }
73
+
74
+ // Category sugar — same brand, so `PluginError.is()` matches all of them.
75
+ // Convenience for single-SDK-copy plugins; docs teach `.is()` + `code`.
76
+ export class PluginValidationError extends PluginError {}
77
+ export class PluginNotFoundError extends PluginError {}
78
+ export class PluginPermissionError extends PluginError {}
79
+ export class PluginHandleError extends PluginError {}
80
+ export class PluginQuotaError extends PluginError {}
81
+ export class PluginTimeoutError extends PluginError {}
82
+ export class PluginTransportError extends PluginError {}
83
+ export class PluginLifecycleError extends PluginError {}
84
+ export class PluginExecutionError extends PluginError {}
85
+ export class PluginInternalError extends PluginError {}
86
+
87
+ type PluginErrorConstructor = new (envelope: ErrorEnvelope) => PluginError
88
+
89
+ const CATEGORY_CLASS: Record<PluginErrorCategory, PluginErrorConstructor> = {
90
+ validation: PluginValidationError,
91
+ not_found: PluginNotFoundError,
92
+ permission: PluginPermissionError,
93
+ handle: PluginHandleError,
94
+ quota: PluginQuotaError,
95
+ timeout: PluginTimeoutError,
96
+ transport: PluginTransportError,
97
+ lifecycle: PluginLifecycleError,
98
+ execution: PluginExecutionError,
99
+ internal: PluginInternalError,
100
+ }
101
+
102
+ function hintFor(code: WirePluginErrorCode): string | undefined {
103
+ return isPluginErrorCode(code) ? CODE_META[code].hintTemplate : undefined
104
+ }
105
+
106
+ /**
107
+ * Rehydrate a wire envelope into the typed error the plugin catches.
108
+ * A code outside the known union (newer host) constructs the base
109
+ * `PluginError` with the code preserved verbatim.
110
+ */
111
+ export function fromEnvelope(envelope: ErrorEnvelope): PluginError {
112
+ if (!isPluginErrorCode(envelope.code)) return new PluginError(envelope)
113
+ const ErrorClass = CATEGORY_CLASS[CODE_META[envelope.code].category]
114
+ return new ErrorClass(envelope)
115
+ }
116
+
117
+ /**
118
+ * Build an envelope for a CLIENT-detected failure (transport rejection,
119
+ * legacy host without `errorInfo`). Host-originated envelopes are built by
120
+ * the host's `toErrorEnvelope` mapper, never by this.
121
+ */
122
+ export function makeClientEnvelope(
123
+ code: PluginErrorCode,
124
+ message: string,
125
+ extra?: Partial<Pick<ErrorEnvelope, "details" | "handle" | "methodPath" | "hint">>
126
+ ): ErrorEnvelope {
127
+ return {
128
+ envelopeVersion: ERROR_ENVELOPE_VERSION,
129
+ code,
130
+ message,
131
+ errorId: newErrorId("c-"),
132
+ ...extra,
133
+ }
134
+ }
package/src/handles.ts CHANGED
@@ -46,6 +46,34 @@ export type ComponentHandle = EntityId<"component">
46
46
  */
47
47
  export type MaterialHandle = EntityId<"material">
48
48
 
49
+ /**
50
+ * A placed **scene underlay** — an imported reference plane the user traces over:
51
+ * an image `FloorPlan`, a `PDF` overlay, or a `CAD` sketch. Entity-style: the token
52
+ * IS the raw `Component.id` (`fp_…` / a pdf id / `cad_…`). Resolved live host-side
53
+ * via the import-order layer walk (`collectStoreyImportItems`) — NOT via
54
+ * `ComponentUtility.FindComponentById`, which does not index underlays. No arena, no
55
+ * quota, stable across undo/redo. Returned by `core.io.import.{image,pdf,cadJson}`
56
+ * and consumed by every `core.io.underlay.*` method.
57
+ */
58
+ export type UnderlayHandle = EntityId<"underlay">
59
+
60
+ /**
61
+ * The project's **site terrain** — a Mapbox-derived topography mesh. Entity-style
62
+ * and a project **singleton** (one terrain per project): the token is the terrain
63
+ * `Component.id` (`terrain-…`), resolved live via `TerrainOperations.getTerrain()`
64
+ * (absent from the level component collections, so NOT `FindComponentById`).
65
+ * Returned by `core.io.import.terrain`.
66
+ */
67
+ export type TerrainHandle = EntityId<"terrain">
68
+
69
+ /**
70
+ * A handle to an **asynchronous import job** (today: a DWG → Forge conversion, which
71
+ * can take minutes). Returned immediately by `core.io.import.dwg`; poll it via
72
+ * `core.io.job.*` (`getStatus` / `isComplete` / `getResult` / `getError`). The token
73
+ * is the engine's import request id; job state lives in a host-side registry.
74
+ */
75
+ export type ImportJobHandle = EntityId<"importJob">
76
+
49
77
  // ── Value-kind handles (all-handle model, §11) ──────────────────────────────
50
78
  // Under the all-handle decision every structured type is a handle. The only
51
79
  // non-handles crossing the boundary are JS primitives (number/string/boolean)
@@ -123,6 +151,24 @@ export const MaterialHandle = z
123
151
  .min(1)
124
152
  .transform((s) => s as MaterialHandle)
125
153
 
154
+ /**
155
+ * {@linkcode UnderlayHandle} / {@linkcode TerrainHandle} / {@linkcode ImportJobHandle}
156
+ * are entity-style handles — raw host ids, validated only as a non-empty string
157
+ * (no `"<kind>_"` prefix), resolved live host-side (existence enforced there).
158
+ */
159
+ export const UnderlayHandle = z
160
+ .string()
161
+ .min(1)
162
+ .transform((s) => s as UnderlayHandle)
163
+ export const TerrainHandle = z
164
+ .string()
165
+ .min(1)
166
+ .transform((s) => s as TerrainHandle)
167
+ export const ImportJobHandle = z
168
+ .string()
169
+ .min(1)
170
+ .transform((s) => s as ImportJobHandle)
171
+
126
172
  // Value-kind handle schemas (all-handle model, §11).
127
173
  export const Vec3Handle = handleSchema("vec3")
128
174
  export const QuatHandle = handleSchema("quat")
package/src/host-utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { PluginApi } from "./api"
2
+ import type { ErrorEnvelope } from "./errors"
2
3
 
3
4
  // -- Type Utilities for inferring method paths from the class hierarchy ------
4
5
 
@@ -60,5 +61,8 @@ export type PluginApiCallResult<M extends PluginApiMethod> =
60
61
  export type PluginApiCallWrappedResult<M extends PluginApiMethod> = {
61
62
  success: boolean;
62
63
  data?: PluginApiCallResult<M>;
64
+ /** Legacy failure message — kept verbatim for already-published plugins. */
63
65
  error?: string;
66
+ /** Structured failure (hosts ≥ error-envelope rollout). Present iff `success` is false. */
67
+ errorInfo?: ErrorEnvelope;
64
68
  }
package/src/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./api"
2
2
  export * from "./types"
3
3
  export * from "./host-utils"
4
4
  export * from "./handles"
5
+ export * from "./errors"