@pantheon-systems/p1-next-sdk 0.21.0 → 0.22.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
@@ -64,8 +64,10 @@ Next.js router.
64
64
 
65
65
  The chatbot is in limited release, and whether it is available to a given site is decided by
66
66
  Pantheon rather than configured by the application. Return `useP1Chatbot`'s result from the
67
- editor's `useExtensions` slot; there is nothing to branch on, because an unavailable chatbot
68
- contributes no plugins, no handler and an unchanged canvas key.
67
+ editor's `useExtensions` slot; there is nothing to branch on. The chat plugin is always in the
68
+ list and shows itself only when the chatbot is available, so the editor canvas does not reload
69
+ when availability resolves. `pluginOptions` follows availability: while the chatbot is
70
+ unavailable it carries no handler and hides the panel toggle.
69
71
 
70
72
  ```tsx
71
73
  function useEditorExtensions({ openDocument }: P1EditorContext): P1EditorExtensions {
@@ -74,7 +76,6 @@ function useEditorExtensions({ openDocument }: P1EditorContext): P1EditorExtensi
74
76
  return {
75
77
  plugins: chatbot.plugins,
76
78
  pluginOptions: chatbot.pluginOptions,
77
- editorKeySuffix: chatbot.editorKeySuffix,
78
79
  };
79
80
  }
80
81
 
@@ -88,6 +89,7 @@ There is nothing to configure. Availability is resolved alongside the editor's o
88
89
  experimental features, in the same single check, so a project adds no environment variables
89
90
  for the chatbot and changes nothing when the rollout ends. `P1ChatbotProvider` is deprecated
90
91
  and renders its children unchanged; an existing `wrapEditor` that uses it can be dropped.
92
+ `editorKeySuffix` is deprecated and always an empty string; remove it from your extensions.
91
93
 
92
94
  ## Entry points
93
95
 
@@ -0,0 +1,7 @@
1
+ import { type DraftRequestChannel } from "@pantheon-systems/p1-ai-chat";
2
+ import type { Plugin } from "@puckeditor/core";
3
+ export declare function useChatbotEnabled(): boolean;
4
+ export declare function createGatedChatPlugin(options: {
5
+ draftRequests: DraftRequestChannel;
6
+ onPageCreated: (path: string) => void;
7
+ }): Plugin;
@@ -0,0 +1,32 @@
1
+ "use client";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
+ import { useP1Auth } from "@pantheon-systems/puck-css";
4
+ import { createAIChatPlugin } from "@pantheon-systems/p1-ai-chat";
5
+ import { useP1ExperimentalFeatures } from "../experimental-features";
6
+ import { p1UserFlagKey } from "../launchdarkly/user-key";
7
+ export function useChatbotEnabled() {
8
+ const { user } = useP1Auth();
9
+ const features = useP1ExperimentalFeatures({
10
+ userId: p1UserFlagKey(user),
11
+ siteId: process.env.NEXT_PUBLIC_CSS_SITE_ID || undefined,
12
+ });
13
+ return features.isEnabled("chatbot");
14
+ }
15
+ export function createGatedChatPlugin(options) {
16
+ const inner = createAIChatPlugin({
17
+ agentUrl: process.env.NEXT_PUBLIC_AGENT_URL,
18
+ draftRequests: options.draftRequests,
19
+ onPageCreated: options.onPageCreated,
20
+ });
21
+ const InnerFields = inner.overrides?.fields;
22
+ function ChatbotGate({ children }) {
23
+ const enabled = useChatbotEnabled();
24
+ if (!enabled || !InnerFields)
25
+ return _jsx(_Fragment, { children: children });
26
+ return _jsx(InnerFields, { children: children });
27
+ }
28
+ return {
29
+ ...inner,
30
+ overrides: { ...inner.overrides, fields: ChatbotGate },
31
+ };
32
+ }
@@ -8,7 +8,10 @@ export interface P1ChatbotOptions {
8
8
  onPageCreated: (path: string) => void;
9
9
  }
10
10
  export interface P1Chatbot {
11
- /** Append to `useP1Editor`'s `additionalPlugins`. Empty when the chatbot is off. */
11
+ /**
12
+ * Append to `useP1Editor`'s `additionalPlugins`. The list is the same whether or not
13
+ * the chatbot is available, so the editor does not reload when availability changes.
14
+ */
12
15
  plugins: Plugin[];
13
16
  /** Spread into `useP1Editor`'s `pluginOptions`. */
14
17
  pluginOptions: {
@@ -19,9 +22,8 @@ export interface P1Chatbot {
19
22
  showAIPanelToggle: boolean;
20
23
  };
21
24
  /**
22
- * Append to the `key` given to `<Puck>`. Puck reads its plugin list once per mount, so
23
- * a chatbot that becomes available without the key changing would not appear until the
24
- * next navigation.
25
+ * @deprecated No longer needed; always an empty string. Remove it from your editor's
26
+ * extensions.
25
27
  */
26
28
  editorKeySuffix: string;
27
29
  }
@@ -30,7 +32,7 @@ export interface P1Chatbot {
30
32
  * mount it. Call it inside `<P1App>`, where the signed-in editor is known.
31
33
  *
32
34
  * Availability is Pantheon's to decide, so an application wires the result through
33
- * without branching on it: an empty plugin list, an absent handler and an unchanged key
34
- * suffix are what "off" looks like.
35
+ * without branching on it: an absent handler and a hidden toggle are what "off" looks
36
+ * like.
35
37
  */
36
38
  export declare function useP1Chatbot(options: P1ChatbotOptions): P1Chatbot;
@@ -1,35 +1,24 @@
1
1
  "use client";
2
- import { useMemo } from "react";
3
- import { useP1Auth } from "@pantheon-systems/puck-css";
4
- import { createAIChatPlugin } from "@pantheon-systems/p1-ai-chat";
5
- import { useP1ExperimentalFeatures } from "../experimental-features";
6
- import { p1UserFlagKey } from "../launchdarkly/user-key";
2
+ import { useMemo, useRef } from "react";
7
3
  import { createGenerateWithAIHandler } from "./ai-generate";
4
+ import { createGatedChatPlugin, useChatbotEnabled } from "./chatbot-plugin";
8
5
  import { getDraftRequestChannel } from "./draft-request-channel";
9
6
  export function useP1Chatbot(options) {
10
- const { onPageCreated } = options;
11
- const { user } = useP1Auth();
12
- const features = useP1ExperimentalFeatures({
13
- userId: p1UserFlagKey(user),
14
- siteId: process.env.NEXT_PUBLIC_CSS_SITE_ID || undefined,
15
- });
16
- const enabled = features.isEnabled("chatbot");
7
+ const enabled = useChatbotEnabled();
17
8
  const draftRequests = getDraftRequestChannel();
18
- const plugins = useMemo(() => enabled
19
- ? [
20
- createAIChatPlugin({
21
- agentUrl: process.env.NEXT_PUBLIC_AGENT_URL,
22
- draftRequests,
23
- onPageCreated,
24
- }),
25
- ]
26
- : [], [enabled, draftRequests, onPageCreated]);
9
+ const onPageCreated = useRef(options.onPageCreated);
10
+ onPageCreated.current = options.onPageCreated;
11
+ const plugin = useMemo(() => createGatedChatPlugin({
12
+ draftRequests,
13
+ onPageCreated: (path) => onPageCreated.current(path),
14
+ }), [draftRequests]);
15
+ const plugins = useMemo(() => [plugin], [plugin]);
27
16
  return useMemo(() => ({
28
17
  plugins,
29
18
  pluginOptions: {
30
19
  onGenerateWithAI: createGenerateWithAIHandler(draftRequests, enabled),
31
20
  showAIPanelToggle: enabled,
32
21
  },
33
- editorKeySuffix: enabled ? "-ai" : "-no-ai",
22
+ editorKeySuffix: "",
34
23
  }), [plugins, draftRequests, enabled]);
35
24
  }
@@ -18,8 +18,15 @@ export async function verifyEditorAccess(opts) {
18
18
  let branchId = configuredBranchId(opts.branchId) ??
19
19
  configuredBranchId(getSharedBranchId() ?? undefined);
20
20
  if (!branchId) {
21
- const branches = await client.branches.list(siteId);
22
- branchId = branches.find((b) => b.isMain)?.id;
21
+ try {
22
+ const branches = await client.branches.list(siteId);
23
+ branchId = branches.find((b) => b.isMain)?.id;
24
+ }
25
+ catch (err) {
26
+ if (await isApiStatus(err, 404))
27
+ return { status: "forbidden" };
28
+ throw err;
29
+ }
23
30
  }
24
31
  if (!branchId)
25
32
  return { status: "forbidden" };
@@ -29,12 +36,12 @@ export async function verifyEditorAccess(opts) {
29
36
  : { status: "forbidden" };
30
37
  }
31
38
  catch (err) {
32
- if (await isForbidden(err))
39
+ if (await isApiStatus(err, 403))
33
40
  return { status: "forbidden" };
34
41
  return { status: "unauthenticated" };
35
42
  }
36
43
  }
37
- async function isForbidden(err) {
44
+ async function isApiStatus(err, status) {
38
45
  const mod = await import("@pantheon-systems/css-client");
39
- return err instanceof mod.P1ApiError && err.status === 403;
46
+ return err instanceof mod.P1ApiError && err.status === status;
40
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pantheon-systems/p1-next-sdk",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "license": "UNLICENSED",
5
5
  "description": "Next.js SDK for P1 editor integration",
6
6
  "publishConfig": {
@@ -42,9 +42,9 @@
42
42
  "!dist/**/*.map"
43
43
  ],
44
44
  "dependencies": {
45
- "@pantheon-systems/css-client": "0.21.0",
45
+ "@pantheon-systems/css-client": "0.22.0",
46
46
  "@pantheon-systems/p1-ai-chat": "0.10.0",
47
- "@pantheon-systems/puck-css": "0.21.0",
47
+ "@pantheon-systems/puck-css": "0.22.0",
48
48
  "launchdarkly-js-client-sdk": "^3.9.3",
49
49
  "server-only": "^0.0.1"
50
50
  },