-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
280f6fc
commit c5baba3
Showing
12 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
Button, | ||
MenuItem, | ||
Select, | ||
Stack, | ||
TextField, | ||
Typography, | ||
} from '@mui/material' | ||
import { useState } from 'react' | ||
import { | ||
promptAIActionsList, | ||
type PromptAIAction, | ||
} from '@storyblok/field-plugin' | ||
import type { PluginComponent } from './FieldPluginDemo' | ||
|
||
export const PromptAI: PluginComponent = (props) => { | ||
const { actions } = props | ||
|
||
const [promptQuestion, setPromptQuestion] = useState<string>('') | ||
const [promptType, setPromptType] = useState<PromptAIAction>('prompt') | ||
const [promptOutput, setPromptOutput] = useState<string>('') | ||
|
||
return ( | ||
<Stack | ||
gap={2} | ||
direction={'column'} | ||
> | ||
<Typography variant="subtitle1">Prompt AI</Typography> | ||
<Stack gap={2}> | ||
<TextField | ||
label="Ask a question" | ||
onChange={(e) => setPromptQuestion(e.target.value)} | ||
required | ||
/> | ||
<Select | ||
value={promptType} | ||
onChange={(e) => setPromptType(e.target.value as PromptAIAction)} | ||
> | ||
{promptAIActionsList.map((promptAIAction) => ( | ||
<MenuItem | ||
key={promptAIAction} | ||
value={promptAIAction} | ||
> | ||
{promptAIAction} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
<Typography>Output: {promptOutput}</Typography> | ||
<Button | ||
variant="outlined" | ||
color="secondary" | ||
onClick={async () => | ||
setPromptOutput( | ||
await actions.promptAI({ | ||
action: promptType, | ||
text: promptQuestion, | ||
}), | ||
) | ||
} | ||
> | ||
Prompt | ||
</Button> | ||
</Stack> | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ld-plugin/src/messaging/pluginMessage/containerToPluginMessage/PromptAIResponseMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { hasKey } from '../../../utils' | ||
import { isMessageToPlugin, type MessageToPlugin } from './MessageToPlugin' | ||
|
||
/** | ||
* The object returned when calling the "prompt-ai" action. | ||
*/ | ||
export type PromptAIResponseMessage = MessageToPlugin<'prompt-ai'> & { | ||
output: string | ||
} | ||
|
||
export const isPromptAIMessage = ( | ||
data: unknown, | ||
): data is PromptAIResponseMessage => | ||
isMessageToPlugin(data) && | ||
hasKey(data, 'output') && | ||
typeof data.output === 'string' | ||
|
||
export const getResponseFromPromptAIMessage = ( | ||
message: PromptAIResponseMessage, | ||
): string => { | ||
const { output } = message | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ages/field-plugin/src/messaging/pluginMessage/pluginToContainerMessage/PromptAIMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { MessageToContainer } from './MessageToContainer' | ||
|
||
export type PromptAIAction = | ||
| 'prompt' | ||
| 'complete' | ||
| 'shorten' | ||
| 'extend' | ||
| 'rephrase' | ||
| 'summarize' | ||
| 'simplify' | ||
| 'translate' | ||
| 'tldr' | ||
| 'adjust-tone' | ||
| 'emojify' | ||
| 'fix_spelling_and_grammar' | ||
|
||
export const promptAIActionsList: PromptAIAction[] = [ | ||
'prompt', | ||
'complete', | ||
'shorten', | ||
'extend', | ||
'rephrase', | ||
'summarize', | ||
'simplify', | ||
'translate', | ||
'tldr', | ||
'adjust-tone', | ||
'emojify', | ||
'fix_spelling_and_grammar', | ||
] | ||
|
||
export type PromptAIPayload = { | ||
action: PromptAIAction | ||
text: string | ||
language?: string | ||
textLength?: string | ||
tone?: string | ||
textLengthUnit?: string | ||
} | ||
|
||
export type PromptAIMessage = Omit<MessageToContainer<'promptAI'>, 'action'> & { | ||
action: 'prompt-ai' | ||
promptAI: PromptAIPayload | ||
} | ||
|
||
export const getPromptAIMessage = ( | ||
message: PromptAIPayload, | ||
options: Pick<PromptAIMessage, 'uid' | 'callbackId'>, | ||
): PromptAIMessage => ({ | ||
action: 'prompt-ai', | ||
event: 'promptAI', | ||
...options, | ||
promptAI: { ...message }, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters