@snaptrude/plugin-core 0.6.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 (83) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/api-manifest.full.json +511 -10
  3. package/api-manifest.json +529 -4
  4. package/dist/api/core/index.d.ts +5 -0
  5. package/dist/api/core/index.d.ts.map +1 -1
  6. package/dist/api/core/proposals/index.d.ts +475 -0
  7. package/dist/api/core/proposals/index.d.ts.map +1 -0
  8. package/dist/api/design/create/index.d.ts +209 -0
  9. package/dist/api/design/create/index.d.ts.map +1 -1
  10. package/dist/api/design/doors/index.d.ts +159 -0
  11. package/dist/api/design/doors/index.d.ts.map +1 -1
  12. package/dist/api/design/furniture/index.d.ts +34 -5
  13. package/dist/api/design/furniture/index.d.ts.map +1 -1
  14. package/dist/api/design/index.d.ts +83 -0
  15. package/dist/api/design/index.d.ts.map +1 -1
  16. package/dist/api/design/lock.d.ts +26 -0
  17. package/dist/api/design/lock.d.ts.map +1 -1
  18. package/dist/api/design/materials/index.d.ts +159 -2
  19. package/dist/api/design/materials/index.d.ts.map +1 -1
  20. package/dist/api/design/query/index.d.ts +115 -5
  21. package/dist/api/design/query/index.d.ts.map +1 -1
  22. package/dist/api/design/selection/index.d.ts +2 -2
  23. package/dist/api/design/transform/index.d.ts +83 -2
  24. package/dist/api/design/transform/index.d.ts.map +1 -1
  25. package/dist/api/design/update/index.d.ts +168 -0
  26. package/dist/api/design/update/index.d.ts.map +1 -1
  27. package/dist/api/design/windows/index.d.ts +113 -2
  28. package/dist/api/design/windows/index.d.ts.map +1 -1
  29. package/dist/api/entity/story.d.ts +101 -7
  30. package/dist/api/entity/story.d.ts.map +1 -1
  31. package/dist/api/presentation/import.d.ts +49 -4
  32. package/dist/api/presentation/import.d.ts.map +1 -1
  33. package/dist/api/presentation/views.d.ts +210 -5
  34. package/dist/api/presentation/views.d.ts.map +1 -1
  35. package/dist/api/program/areas.d.ts +42 -12
  36. package/dist/api/program/areas.d.ts.map +1 -1
  37. package/dist/api/program/index.d.ts +3 -11
  38. package/dist/api/program/index.d.ts.map +1 -1
  39. package/dist/api/program/layout.d.ts +186 -11
  40. package/dist/api/program/layout.d.ts.map +1 -1
  41. package/dist/errors/codes.d.ts +34 -0
  42. package/dist/errors/codes.d.ts.map +1 -0
  43. package/dist/errors/envelope.d.ts +56 -0
  44. package/dist/errors/envelope.d.ts.map +1 -0
  45. package/dist/errors/index.d.ts +6 -0
  46. package/dist/errors/index.d.ts.map +1 -0
  47. package/dist/errors/plugin-error.d.ts +69 -0
  48. package/dist/errors/plugin-error.d.ts.map +1 -0
  49. package/dist/host-utils.d.ts +4 -0
  50. package/dist/host-utils.d.ts.map +1 -1
  51. package/dist/index.cjs +1624 -1028
  52. package/dist/index.cjs.map +1 -1
  53. package/dist/index.d.ts +1 -0
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +1546 -1025
  56. package/dist/index.js.map +1 -1
  57. package/package.json +2 -1
  58. package/scripts/generate-manifest.test.mjs +77 -0
  59. package/src/api/core/index.ts +5 -0
  60. package/src/api/core/proposals/index.ts +509 -0
  61. package/src/api/design/create/index.ts +249 -0
  62. package/src/api/design/doors/index.ts +168 -0
  63. package/src/api/design/furniture/index.ts +36 -5
  64. package/src/api/design/index.ts +87 -0
  65. package/src/api/design/lock.ts +27 -0
  66. package/src/api/design/materials/index.ts +182 -2
  67. package/src/api/design/query/index.ts +81 -0
  68. package/src/api/design/transform/index.ts +74 -2
  69. package/src/api/design/update/index.ts +167 -0
  70. package/src/api/design/windows/index.ts +128 -2
  71. package/src/api/entity/story.ts +105 -7
  72. package/src/api/presentation/import.ts +45 -4
  73. package/src/api/presentation/views.ts +221 -5
  74. package/src/api/program/areas.ts +34 -12
  75. package/src/api/program/index.ts +3 -11
  76. package/src/api/program/layout.ts +195 -11
  77. package/src/errors/codes.ts +136 -0
  78. package/src/errors/envelope.ts +75 -0
  79. package/src/errors/index.ts +21 -0
  80. package/src/errors/plugin-error.ts +134 -0
  81. package/src/host-utils.ts +4 -0
  82. package/src/index.ts +1 -0
  83. package/test/errors.test.mjs +184 -0
@@ -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/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"
@@ -0,0 +1,184 @@
1
+ // Node built-in test runner suite for the error module (codes/envelope/PluginError).
2
+ // Run: node --test test/
3
+ // Imports the BUILT package (dist) — run `pnpm build` first.
4
+ import test from "node:test"
5
+ import assert from "node:assert/strict"
6
+ import {
7
+ PLUGIN_ERROR_CODES,
8
+ CODE_META,
9
+ isPluginErrorCode,
10
+ ERROR_ENVELOPE_VERSION,
11
+ isErrorEnvelope,
12
+ newErrorId,
13
+ PluginError,
14
+ PluginValidationError,
15
+ PluginQuotaError,
16
+ PluginTimeoutError,
17
+ PluginTransportError,
18
+ PluginInternalError,
19
+ fromEnvelope,
20
+ makeClientEnvelope,
21
+ } from "../dist/index.js"
22
+
23
+ const CATEGORIES = [
24
+ "validation", "not_found", "permission", "handle", "quota",
25
+ "timeout", "transport", "lifecycle", "execution", "internal",
26
+ ]
27
+
28
+ function envelope(overrides = {}) {
29
+ return {
30
+ envelopeVersion: ERROR_ENVELOPE_VERSION,
31
+ code: "OPERATION_FAILED",
32
+ message: "it failed",
33
+ errorId: "test-id-1",
34
+ ...overrides,
35
+ }
36
+ }
37
+
38
+ // --- codes registry ---------------------------------------------------------
39
+
40
+ test("every code has CODE_META with a valid category (union↔meta completeness)", () => {
41
+ for (const code of PLUGIN_ERROR_CODES) {
42
+ const meta = CODE_META[code]
43
+ assert.ok(meta, `missing CODE_META for ${code}`)
44
+ assert.ok(CATEGORIES.includes(meta.category), `bad category for ${code}: ${meta.category}`)
45
+ }
46
+ assert.equal(Object.keys(CODE_META).length, PLUGIN_ERROR_CODES.length)
47
+ })
48
+
49
+ test("codes are unique", () => {
50
+ assert.equal(new Set(PLUGIN_ERROR_CODES).size, PLUGIN_ERROR_CODES.length)
51
+ })
52
+
53
+ test("isPluginErrorCode narrows union members and rejects strangers", () => {
54
+ assert.equal(isPluginErrorCode("HANDLE_INVALID"), true)
55
+ assert.equal(isPluginErrorCode("TOTALLY_MADE_UP"), false)
56
+ assert.equal(isPluginErrorCode(""), false)
57
+ })
58
+
59
+ // --- envelope ----------------------------------------------------------------
60
+
61
+ test("isErrorEnvelope accepts a minimal valid envelope", () => {
62
+ assert.equal(isErrorEnvelope(envelope()), true)
63
+ })
64
+
65
+ test("isErrorEnvelope accepts codes OUTSIDE the union (forward compat)", () => {
66
+ assert.equal(isErrorEnvelope(envelope({ code: "FUTURE_CODE" })), true)
67
+ })
68
+
69
+ test("isErrorEnvelope rejects malformed values", () => {
70
+ assert.equal(isErrorEnvelope(null), false)
71
+ assert.equal(isErrorEnvelope("boom"), false)
72
+ assert.equal(isErrorEnvelope({}), false)
73
+ assert.equal(isErrorEnvelope(envelope({ envelopeVersion: 2 })), false)
74
+ assert.equal(isErrorEnvelope(envelope({ errorId: undefined })), false)
75
+ })
76
+
77
+ test("newErrorId returns unique prefixed ids", () => {
78
+ const a = newErrorId("c-")
79
+ const b = newErrorId("c-")
80
+ assert.notEqual(a, b)
81
+ assert.ok(a.startsWith("c-"))
82
+ })
83
+
84
+ // --- PluginError rehydration --------------------------------------------------
85
+
86
+ test("fromEnvelope picks the category sugar subclass", () => {
87
+ assert.ok(fromEnvelope(envelope({ code: "VALIDATION" })) instanceof PluginValidationError)
88
+ assert.ok(fromEnvelope(envelope({ code: "RATE_LIMITED" })) instanceof PluginQuotaError)
89
+ assert.ok(fromEnvelope(envelope({ code: "TIMEOUT" })) instanceof PluginTimeoutError)
90
+ assert.ok(fromEnvelope(envelope({ code: "TRANSPORT_LOST" })) instanceof PluginTransportError)
91
+ assert.ok(fromEnvelope(envelope({ code: "INTERNAL" })) instanceof PluginInternalError)
92
+ })
93
+
94
+ test("fromEnvelope preserves envelope fields on the error", () => {
95
+ const err = fromEnvelope(
96
+ envelope({
97
+ code: "OPERATION_FAILED",
98
+ details: { op: "union", handles: ["mass_1", "mass_2"] },
99
+ handle: "mass_1",
100
+ methodPath: "design.boolean.union",
101
+ hint: "check overlap",
102
+ })
103
+ )
104
+ assert.equal(err.code, "OPERATION_FAILED")
105
+ assert.equal(err.message, "it failed")
106
+ assert.equal(err.errorId, "test-id-1")
107
+ assert.deepEqual(err.details, { op: "union", handles: ["mass_1", "mass_2"] })
108
+ assert.equal(err.handle, "mass_1")
109
+ assert.equal(err.methodPath, "design.boolean.union")
110
+ assert.equal(err.hint, "check overlap")
111
+ assert.equal(err.category, "execution")
112
+ })
113
+
114
+ test("unknown code constructs base PluginError with code preserved verbatim", () => {
115
+ const err = fromEnvelope(envelope({ code: "FUTURE_CODE" }))
116
+ assert.equal(err.constructor, PluginError)
117
+ assert.equal(err.code, "FUTURE_CODE")
118
+ assert.equal(err.category, "internal")
119
+ assert.equal(PluginError.is(err), true)
120
+ })
121
+
122
+ test("hint falls back to the code's template when the envelope has none", () => {
123
+ const err = fromEnvelope(envelope({ code: "RATE_LIMITED" }))
124
+ assert.equal(err.hint, CODE_META.RATE_LIMITED.hintTemplate)
125
+ })
126
+
127
+ test("PluginError.is matches the brand across class identities (dual SDK copy)", () => {
128
+ // Simulate a second bundled copy: a plain object carrying the Symbol.for
129
+ // brand but NOT part of this module's prototype chain.
130
+ const impostor = Object.assign(new Error("from other copy"), {
131
+ [Symbol.for("snaptrude.pluginError")]: true,
132
+ code: "TIMEOUT",
133
+ })
134
+ assert.equal(impostor instanceof PluginError, false)
135
+ assert.equal(PluginError.is(impostor), true)
136
+ assert.equal(PluginError.is(new Error("plain")), false)
137
+ assert.equal(PluginError.is(null), false)
138
+ })
139
+
140
+ // --- makeClientEnvelope --------------------------------------------------------
141
+
142
+ test("makeClientEnvelope mints a c- prefixed id and passes extras through", () => {
143
+ const env = makeClientEnvelope("TRANSPORT_LOST", "port closed", {
144
+ methodPath: "design.boolean.union",
145
+ })
146
+ assert.equal(isErrorEnvelope(env), true)
147
+ assert.equal(env.code, "TRANSPORT_LOST")
148
+ assert.ok(env.errorId.startsWith("c-"))
149
+ assert.equal(env.methodPath, "design.boolean.union")
150
+ })
151
+
152
+ // --- Registry snapshot (append-only guard) -----------------------------------
153
+ // Deliberate: adding a code REQUIRES updating this list in the same change so
154
+ // the addition is reviewed; removing or renaming one must fail loudly forever.
155
+
156
+ test("PLUGIN_ERROR_CODES matches the reviewed snapshot (append-only contract)", () => {
157
+ assert.deepEqual([...PLUGIN_ERROR_CODES], [
158
+ "VALIDATION",
159
+ "INVALID_RPC_FORMAT",
160
+ "METHOD_NOT_FOUND",
161
+ "METHOD_NOT_PERMITTED",
162
+ "HANDLE_INVALID",
163
+ "HANDLE_KIND_MISMATCH",
164
+ "RESOURCE_QUOTA_EXCEEDED",
165
+ "RATE_LIMITED",
166
+ "PRECONDITION_FAILED",
167
+ "OPERATION_FAILED",
168
+ "TIMEOUT",
169
+ "CALL_TIMEOUT",
170
+ "TRANSPORT_LOST",
171
+ "PLUGIN_NOT_READY",
172
+ "PLUGIN_TERMINATED",
173
+ "RESULT_NOT_SERIALIZABLE",
174
+ "RESULT_TOO_LARGE",
175
+ "BATCH_PARTIAL",
176
+ "STORY_UPDATE_FAILED",
177
+ "STORY_HEIGHT_REJECTED",
178
+ "NO_ACTIVE_STRUCTURE",
179
+ "NO_ELIGIBLE_ELEMENTS",
180
+ "PROPOSAL_SWITCH_BLOCKED",
181
+ "INTERNAL",
182
+ "UNKNOWN",
183
+ ])
184
+ })