@platejs/ai 54.0.0-beta.0 → 54.0.0-beta.1

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.
@@ -2,7 +2,7 @@ import { c as AIMode, d as BaseAIPluginConfig, i as EditorPrompt, l as AIToolNam
2
2
  import * as platejs0 from "platejs";
3
3
  import { Descendant, EditorNodesOptions, ExtendConfig, NodeEntry, OmitFirst, Path, PluginConfig, Range, RemoveNodesOptions, SlateEditor, TElement, TIdElement, TRange, TTableElement, TText } from "platejs";
4
4
  import { DeserializeMdOptions, SerializeMdOptions } from "@platejs/markdown";
5
- import * as platejs_react0 from "platejs/react";
5
+ import * as platejs_react1 from "platejs/react";
6
6
  import { OverrideEditor, PlateEditor, RenderNodeWrapperProps } from "platejs/react";
7
7
  import React from "react";
8
8
  import { ChatRequestOptions, UIMessage } from "ai";
@@ -11,7 +11,7 @@ import { TriggerComboboxPluginOptions } from "@platejs/combobox";
11
11
 
12
12
  //#region src/react/ai/AIPlugin.d.ts
13
13
  type AIPluginConfig = ExtendConfig<BaseAIPluginConfig>;
14
- declare const AIPlugin: platejs_react0.PlatePlugin<platejs0.PluginConfig<"ai", {}, {}, {
14
+ declare const AIPlugin: platejs_react1.PlatePlugin<platejs0.PluginConfig<"ai", {}, {}, {
15
15
  ai: {
16
16
  acceptPreview: ((_value?: platejs0.Value | undefined) => boolean) & ((_value?: platejs0.Value | undefined) => boolean);
17
17
  beginPreview: ((args_0?: {
@@ -168,7 +168,7 @@ type AIChatPluginConfig = PluginConfig<'aiChat', {
168
168
  removeAnchor: (options?: EditorNodesOptions) => void;
169
169
  };
170
170
  }>;
171
- declare const AIChatPlugin: platejs_react0.PlatePlugin<PluginConfig<"aiChat", {
171
+ declare const AIChatPlugin: platejs_react1.PlatePlugin<PluginConfig<"aiChat", {
172
172
  _blockChunks: string;
173
173
  _blockPath: Path | null;
174
174
  /** @private Using For streamInsertChunk */
@@ -510,7 +510,7 @@ type CompletionState = {
510
510
  error?: Error | null;
511
511
  isLoading?: boolean;
512
512
  };
513
- declare const CopilotPlugin: platejs_react0.PlatePlugin<PluginConfig<"copilot", CompletionState & {
513
+ declare const CopilotPlugin: platejs_react1.PlatePlugin<PluginConfig<"copilot", CompletionState & {
514
514
  /**
515
515
  * AI completion options. See:
516
516
  * {@link https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion#parameters | AI SDK UI useCompletion Parameters}
@@ -947,6 +947,7 @@ const withoutDeserializeInMdx = (editor, input) => {
947
947
 
948
948
  //#endregion
949
949
  //#region src/react/ai-chat/streaming/streamSerializeMd.ts
950
+ const STREAM_LINE_BREAK_PLACEHOLDER = "platejs-stream-line-break";
950
951
  const trimEndHeading = (editor, value) => {
951
952
  const headingKeys = new Set([
952
953
  KEYS.h1,
@@ -977,9 +978,40 @@ const trimEndHeading = (editor, value) => {
977
978
  value
978
979
  };
979
980
  };
981
+ const escapeEmbeddedTextLineBreaks = (nodes) => {
982
+ let hasLineBreaks = false;
983
+ const value = nodes.map((node) => {
984
+ if (TextApi.isText(node)) {
985
+ if (node.text !== "\n" && node.text.includes("\n")) {
986
+ hasLineBreaks = true;
987
+ return {
988
+ ...node,
989
+ text: node.text.replaceAll("\n", STREAM_LINE_BREAK_PLACEHOLDER)
990
+ };
991
+ }
992
+ return node;
993
+ }
994
+ if (ElementApi.isElement(node)) {
995
+ const escapedChildren = escapeEmbeddedTextLineBreaks(node.children);
996
+ if (escapedChildren.hasLineBreaks) {
997
+ hasLineBreaks = true;
998
+ return {
999
+ ...node,
1000
+ children: escapedChildren.value
1001
+ };
1002
+ }
1003
+ }
1004
+ return node;
1005
+ });
1006
+ return {
1007
+ hasLineBreaks,
1008
+ value
1009
+ };
1010
+ };
980
1011
  const streamSerializeMd = (editor, options, chunk) => {
981
1012
  const { value: optionsValue, ...restOptions } = options;
982
- const { value } = trimEndHeading(editor, optionsValue ?? editor.children);
1013
+ const { value: trimmedValue } = trimEndHeading(editor, optionsValue ?? editor.children);
1014
+ const { hasLineBreaks, value } = escapeEmbeddedTextLineBreaks(trimmedValue);
983
1015
  let result = "";
984
1016
  result = editor.getApi(MarkdownPlugin).markdown.serialize({
985
1017
  value,
@@ -991,6 +1023,15 @@ const streamSerializeMd = (editor, options, chunk) => {
991
1023
  result = result.replace(/&#x20;/g, " ");
992
1024
  result = result.replace(/&#x200B;/g, " ");
993
1025
  result = result.replace(/\u200B/g, "");
1026
+ if (hasLineBreaks) result = result.replaceAll(STREAM_LINE_BREAK_PLACEHOLDER, "\n");
1027
+ if (trimmedChunk.includes("\n") && trimmedChunk !== "\n\n") {
1028
+ const trimmedResult = result.trimEnd();
1029
+ if (trimmedResult.endsWith("\n<br />")) result = trimmedResult.slice(0, -6);
1030
+ else {
1031
+ const trailingWhitespaceBreakSuffix = `${trimmedChunk}\n`;
1032
+ if (result.endsWith(trailingWhitespaceBreakSuffix)) result = result.slice(0, -trailingWhitespaceBreakSuffix.length);
1033
+ }
1034
+ }
994
1035
  if (trimmedChunk !== "\n\n") result = result.trimEnd() + trimmedChunk;
995
1036
  if (chunk.endsWith("\n\n")) {
996
1037
  if (result === "\n") result = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platejs/ai",
3
- "version": "54.0.0-beta.0",
3
+ "version": "54.0.0-beta.1",
4
4
  "description": "Text AI plugin for Plate",
5
5
  "keywords": [
6
6
  "plate",
@@ -32,7 +32,7 @@
32
32
  "fastest-levenshtein": "1.0.16",
33
33
  "lodash": "^4.17.21",
34
34
  "react-compiler-runtime": "^1.0.0",
35
- "@platejs/markdown": "^54.0.0-beta.0",
35
+ "@platejs/markdown": "^54.0.0-beta.1",
36
36
  "@platejs/selection": "^54.0.0-beta.0",
37
37
  "@platejs/suggestion": "^54.0.0-beta.0",
38
38
  "@platejs/table": "^54.0.0-beta.0"
@@ -41,10 +41,10 @@
41
41
  "@ai-sdk/react": "^3.0.0",
42
42
  "ai": "^6.0.0",
43
43
  "@plate/scripts": "1.0.0",
44
- "platejs": "^54.0.0-beta.0"
44
+ "platejs": "^54.0.0-beta.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "platejs": ">=54.0.0-beta.0",
47
+ "platejs": ">=54.0.0-beta.1",
48
48
  "react": ">=18.0.0",
49
49
  "react-dom": ">=18.0.0"
50
50
  },