Skip to content

Commit

Permalink
AI
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Oct 2, 2024
1 parent 54c85f0 commit d43e600
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 19 deletions.
Binary file added apps/www/public/ai-selection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export function PlaygroundTurnIntoDropdownMenu(props: DropdownMenuProps) {
<DropdownMenuContent
className="min-w-0"
onCloseAutoFocus={onCloseAutoFocus}
data-plate-prevent-overlay
align="start"
>
<DropdownMenuLabel>Turn into</DropdownMenuLabel>
Expand Down
3 changes: 1 addition & 2 deletions apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ export const usePlaygroundEditor = (id: any = '', scrollSelector?: string) => {
AIPlugin.configure({
options: {
createAIEditor: createAIEditor,
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
fetchSuggestion: async ({ abortSignal, prompt, system }) => {
fetchStream: async ({ abortSignal, prompt, system }) => {
const response = await fetch(
'https://pro.platejs.org/api/ai/command',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const cursorCommandsHandler = async (
{ group, value }: ActionHandlerOptions
) => {
if (group === GROUP_LANGUAGES) {
const content = serializeMd(editor as any);
const content = serializeMd(editor);
await streamInsertText(editor, {
prompt: `Keep the original paragraph format. Translate the following article to ${value?.slice(7)}: ${content}`,
});
Expand All @@ -28,7 +28,7 @@ export const cursorCommandsHandler = async (

switch (value) {
case ACTION_CONTINUE_WRITE: {
const content = serializeMd(editor as any);
const content = serializeMd(editor);

await streamInsertText(editor, {
prompt: `Continue writing the following article in 3-5 sentences: ${content}`,
Expand All @@ -37,7 +37,7 @@ export const cursorCommandsHandler = async (
break;
}
case ACTION_SUMMARIZE: {
const content = serializeMd(editor as any);
const content = serializeMd(editor);

await streamInsertText(editor, {
prompt: `Summarize the following article in 3-5 sentences: ${content}`,
Expand All @@ -46,7 +46,7 @@ export const cursorCommandsHandler = async (
break;
}
case ACTION_EXPLAIN: {
const content = serializeMd(editor as any);
const content = serializeMd(editor);

await streamInsertText(editor, {
prompt: `Explain the following article in 3-5 sentences: ${content}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/src/react/ai/AIPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface FetchAISuggestionProps {
interface ExposeOptions {
createAIEditor: () => PlateEditor;
scrollContainerSelector: string;
fetchSuggestion?: (props: FetchAISuggestionProps) => Promise<ReadableStream>;
fetchStream?: (props: FetchAISuggestionProps) => Promise<ReadableStream>;
trigger?: RegExp | string[] | string;

triggerPreviousCharPattern?: RegExp;
Expand Down
6 changes: 1 addition & 5 deletions packages/ai/src/react/ai/stream/getSystemMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const getSelectionMenuSystem = () => `\
export const getAISystem = () => `\
You are a text-based conversational robot that helps users with tasks such as continuation and refinement.
Users will provide you with some content, and you will help them with their needs.
CRITICAL RULE:If you want to start a new line, output a '\n'. If you want to start a new paragraph, output two '\n'.
Do not respond to the user,generate the content directly.`;

export const getAISystem = () => `\
'Unless the user explicitly requests otherwise, the output will be two to three sentences.'
`;
7 changes: 2 additions & 5 deletions packages/ai/src/react/ai/stream/streamInsertTextSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { deserializeMd } from '@udecode/plate-markdown';

import { AIPlugin } from '../AIPlugin';
import { getNextPathByNumber } from '../utils/getNextPathByNumber';
import { getSelectionMenuSystem } from './getSystemMessage';
import { getAISystem } from './getSystemMessage';
import { streamTraversal } from './streamTraversal';

interface StreamInsertTextSelectionOptions {
Expand All @@ -23,10 +23,7 @@ interface StreamInsertTextSelectionOptions {
export const streamInsertTextSelection = async (
editor: PlateEditor,
aiEditor: PlateEditor,
{
prompt,
system = getSelectionMenuSystem(),
}: StreamInsertTextSelectionOptions
{ prompt, system = getAISystem() }: StreamInsertTextSelectionOptions
) => {
editor.setOptions(AIPlugin, {
aiState: 'requesting',
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/src/react/ai/stream/streamTraversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const streamTraversal = async (
const abortController = new AbortController();
editor.setOptions(AIPlugin, { abortController });

const fetchSuggestion = editor.getOptions(AIPlugin).fetchSuggestion!;
const fetchStream = editor.getOptions(AIPlugin).fetchStream!;

const response = await fetchSuggestion({
const response = await fetchStream({
abortSignal: abortController,
prompt,
system,
Expand Down

0 comments on commit d43e600

Please sign in to comment.