@markupai/sidebar-adapter 2.2.0 → 2.3.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 CHANGED
@@ -29,7 +29,7 @@ You do **not** need prior knowledge of how the sidebar is built internally. As a
29
29
  - **createSidebarHost** — Optional one-shot setup: create/mount iframe, optional load-failed overlay, and `SidebarAdapter` (or iframe-only when your UI and document logic run in separate contexts)
30
30
  - **Host shell helpers** — `ensureSidebarHostShell` / `buildSidebarHostHtmlDocument` so you don’t duplicate container markup and layout CSS
31
31
  - **MIME helpers** — `MimeType` enum plus arbitrary MIME strings for `ContentInfo`
32
- - **Errors** — `TextLookupError` and guards for editor lookup failures
32
+ - **Errors** — `TextLookupError` / `ReplaceBatchError` and guards for editor lookup failures
33
33
  - **Auth** — `SidebarConfig.auth` for declaring the Auth0 login strategy (`"mediation" | "popup"`) and optional `PluginInterface.openAuthUrl` for routing the authorize URL through a host-owned external-browser primitive (see [Authentication](#authentication-optional))
34
34
  - **Cleanup** — `destroy()` / teardown functions on adapters and hosts
35
35
 
@@ -539,7 +539,29 @@ try {
539
539
  }
540
540
  ```
541
541
 
542
- Note: We document thrown errors via JSDoc `@throws` on the plugin interface and export union types `SelectContentError` and `ReplaceContentError` for control-flow typing when catching.
542
+ For `replaceMultipleContents`, throw `ReplaceBatchError` when the batch could not be applied in full and you know which ranges landed. It extends `TextLookupError` and carries `applied` (`ContentRange[]`) and `failed` (`ReplaceBatchFailedRange[]`), the ranges as they were sent; a range in neither list was left untouched. The sidebar resolves the applied occurrences, invalidates the failed ones, and leaves the rest active. The error's `message` describes the batch, so it is shown only on the card the user applied from. Give a failed range its own `message` to explain that one occurrence on its card; without one, the sidebar shows its own "text could not be found" message. A host that verifies every range before editing throws it with an empty `applied`; a plain `TextLookupError` is still accepted and treated as a failure of the whole batch.
543
+
544
+ ```typescript
545
+ import { ReplaceBatchError } from "@markupai/sidebar-adapter";
546
+
547
+ // In replaceMultipleContents, after verifying every range against the document:
548
+ if (unverifiable.length > 0) {
549
+ throw new ReplaceBatchError(
550
+ `None of the ${replacements.length} occurrences were replaced because one of them could not be verified in the document.`,
551
+ {
552
+ applied: [],
553
+ failed: unverifiable.map((r) => ({
554
+ ...r.range,
555
+ message: "This text was edited since the last check.",
556
+ })),
557
+ },
558
+ );
559
+ }
560
+ ```
561
+
562
+ Non-JS ports serialize it as `{ name: "ReplaceBatchError", message, applied, failed }` — see `ReplaceBatchErrorPayload` in `asyncapi.json`.
563
+
564
+ Note: We document thrown errors via JSDoc `@throws` on the plugin interface and export union types `SelectContentError`, `ReplaceContentError` and `ReplaceMultipleContentsError` for control-flow typing when catching.
543
565
 
544
566
  ### Message Format
545
567
 
package/asyncapi.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "asyncapi": "3.0.0",
3
3
  "info": {
4
4
  "title": "Markup AI Sidebar IPC contract",
5
- "version": "2.2.0",
5
+ "version": "2.3.0",
6
6
  "description": "Bidirectional RPC-over-postMessage contract between the Markup AI sidebar and its host. Generated from the @markupai/sidebar-adapter TypeScript types — do not hand-edit."
7
7
  },
8
8
  "channels": {
@@ -454,7 +454,7 @@
454
454
  "name": { "type": "string" },
455
455
  "errorJSON": {
456
456
  "type": "string",
457
- "description": "JSON-stringified error: {message, name?}."
457
+ "description": "JSON-stringified error: {message, name?, ...own properties}. The receiver rebuilds a typed error from `name`; the payloads it recognises are TextLookupErrorPayload and ReplaceBatchErrorPayload (see components/schemas). Any other name arrives as a plain Error."
458
458
  }
459
459
  }
460
460
  }
@@ -2047,6 +2047,62 @@
2047
2047
  }
2048
2048
  },
2049
2049
  "title": "isIntegrationSettingField"
2050
+ },
2051
+ "TextLookupErrorPayload": {
2052
+ "type": "object",
2053
+ "properties": {
2054
+ "name": { "type": "string", "const": "TextLookupError" },
2055
+ "message": { "type": "string" }
2056
+ },
2057
+ "required": ["name", "message"],
2058
+ "additionalProperties": false,
2059
+ "description": "Wire shape of the `errorJSON` payload for a `TextLookupError` thrown by a host's `selectContent` / `replaceContent` / `replaceMultipleContents`.\n\nThe receiving side rebuilds the typed error from `name` through the error registry (`ipcErrorReconstructors`), so a non-JS port must send exactly this `name` for the sidebar to recognise the failure.",
2060
+ "title": "TextLookupErrorPayload"
2061
+ },
2062
+ "ReplaceBatchFailedRange": {
2063
+ "type": "object",
2064
+ "properties": {
2065
+ "start": {
2066
+ "type": "number",
2067
+ "description": "The zero-based character offset from the beginning of the document where the content to replace starts. Must be a non-negative integer."
2068
+ },
2069
+ "end": {
2070
+ "type": "number",
2071
+ "description": "The zero-based exclusive end offset; the span replaced or selected is `[start, end)` in the document."
2072
+ },
2073
+ "message": {
2074
+ "type": "string",
2075
+ "description": "Why this occurrence failed, written for the author (e.g. \"This text was edited since the last check.\"). The sidebar shows it on that occurrence's card; when omitted it shows its own \"text could not be found\" message. The error's top-level `message` describes the batch and is shown only on the card the user applied from."
2076
+ }
2077
+ },
2078
+ "additionalProperties": false,
2079
+ "required": ["end", "start"],
2080
+ "description": "A range (as sent in the call) that the host could not apply, optionally with the reason for that one occurrence.",
2081
+ "title": "ReplaceBatchFailedRange"
2082
+ },
2083
+ "ReplaceBatchErrorPayload": {
2084
+ "type": "object",
2085
+ "properties": {
2086
+ "name": { "type": "string", "const": "ReplaceBatchError" },
2087
+ "message": { "type": "string" },
2088
+ "applied": { "type": "array", "items": { "$ref": "#/components/schemas/ContentRange" } },
2089
+ "failed": {
2090
+ "type": "array",
2091
+ "items": { "$ref": "#/components/schemas/ReplaceBatchFailedRange" }
2092
+ }
2093
+ },
2094
+ "required": ["name", "message", "applied", "failed"],
2095
+ "additionalProperties": false,
2096
+ "description": "Wire shape of the `errorJSON` payload for a `ReplaceBatchError`: a `replaceMultipleContents` batch that the host could not apply in full.\n\n`applied` lists the ranges (as sent in the call) whose replacement landed in the document; `failed` lists the ranges the host could not locate or verify, each with an optional per-occurrence `message`. A range in neither list was left untouched. A host that verifies the whole batch before editing sends every unverifiable range in `failed` and an empty `applied`.",
2097
+ "title": "ReplaceBatchErrorPayload"
2098
+ },
2099
+ "KnownIpcErrorPayload": {
2100
+ "anyOf": [
2101
+ { "$ref": "#/components/schemas/TextLookupErrorPayload" },
2102
+ { "$ref": "#/components/schemas/ReplaceBatchErrorPayload" }
2103
+ ],
2104
+ "description": "Every `errorJSON` payload the sidebar reconstructs into a typed error.",
2105
+ "title": "KnownIpcErrorPayload"
2050
2106
  }
2051
2107
  }
2052
2108
  }
package/dist/errors.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { ContentRange } from "./types/common";
2
+ import type { ReplaceBatchFailedRange } from "./types/errors";
1
3
  /**
2
4
  * Error thrown by integration/plugin code when text lookup (e.g. selection or
3
5
  * positioning) fails. The sidebar treats this as a selection failure and can
@@ -7,9 +9,38 @@
7
9
  * replaceContent) when the requested text cannot be found or located.
8
10
  */
9
11
  export declare class TextLookupError extends Error {
10
- readonly name = "TextLookupError";
12
+ readonly name: string;
11
13
  constructor(message: string);
12
14
  }
15
+ /** Per-range outcome of a `replaceMultipleContents` batch the host could not apply in full. */
16
+ export interface ReplaceBatchOutcome {
17
+ /** Ranges (as sent in the call) whose replacement landed in the document. */
18
+ applied: ContentRange[];
19
+ /** Ranges the host could not locate or verify, each with an optional reason for that occurrence. */
20
+ failed: ReplaceBatchFailedRange[];
21
+ }
22
+ /**
23
+ * Error thrown by `replaceMultipleContents` when the batch could not be applied
24
+ * in full, carrying which ranges were `applied` and which `failed`. A range in
25
+ * neither list was left untouched.
26
+ *
27
+ * A `TextLookupError`, so a sidebar that predates this type still handles it
28
+ * as a batch failure; a sidebar that knows it resolves the applied
29
+ * occurrences, invalidates the failed ones and leaves the rest active. Throw
30
+ * it with an empty `applied` when the batch is verified before any edit.
31
+ */
32
+ export declare class ReplaceBatchError extends TextLookupError {
33
+ readonly name: string;
34
+ readonly applied: ContentRange[];
35
+ readonly failed: ReplaceBatchFailedRange[];
36
+ constructor(message: string, outcome: ReplaceBatchOutcome);
37
+ }
38
+ /**
39
+ * Type guard for ReplaceBatchError. Check it before `isTextLookupError` when
40
+ * catching from `replaceMultipleContents`: every ReplaceBatchError is also a
41
+ * TextLookupError.
42
+ */
43
+ export declare function isReplaceBatchError(error: unknown): error is ReplaceBatchError;
13
44
  /**
14
45
  * Type guard for TextLookupError. Use when catching errors from adapter methods
15
46
  * (e.g. selectContent, replaceContent) to narrow the type and handle text
@@ -26,6 +57,12 @@ export type SelectContentError = TextLookupError;
26
57
  * catching. Extend when adding new known errors for replaceContent.
27
58
  */
28
59
  export type ReplaceContentError = TextLookupError;
60
+ /**
61
+ * Union of errors that replaceMultipleContents may throw. A host that can say
62
+ * which ranges landed throws `ReplaceBatchError`; older hosts throw a plain
63
+ * `TextLookupError` for the whole batch.
64
+ */
65
+ export type ReplaceMultipleContentsError = TextLookupError | ReplaceBatchError;
29
66
  /**
30
67
  * Serialized shape of an error sent over IPC (name + message; extend for custom payloads).
31
68
  */
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,SAAkB,IAAI,qBAAqB;gBAE/B,OAAO,EAAE,MAAM;CAI5B;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAE1E;AAED;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,eAAe,CAAC;AAEjD;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,KAAK,KAAK,CAAC;AAErF;;;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