Skip to content

Commit

Permalink
feat(SHAPE-8085): apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
demetriusfeijoo committed Jan 17, 2025
1 parent f8fd00b commit 3f85b24
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 63 deletions.
18 changes: 10 additions & 8 deletions packages/demo/src/components/PromptAI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Button,
Checkbox,
FormControl,
FormLabel,
FormControlLabel,
MenuItem,
Stack,
TextField,
Expand Down Expand Up @@ -84,13 +84,15 @@ export const PromptAI: PluginComponent = (props) => {
onChange={(e) => setPromptTone(e.target.value)}
/>
<FormControl>
<FormLabel htmlFor="based-on-current-story-checkbox">
Based on the current story:
</FormLabel>
<Checkbox
id="based-on-current-story-checkbox"
value={promptBasedOnCurrentStory}
onChange={(e) => setPromptBasedOnCurrentStory(e.target.checked)}
<FormControlLabel
label="Based on the current story"
control={
<Checkbox
value={promptBasedOnCurrentStory}
onChange={(e) => setPromptBasedOnCurrentStory(e.target.checked)}
/>
}
sx={{ ml: '-9px' }}
/>
</FormControl>
<Typography>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('createPluginActions', () => {
expect(postToContainer).toHaveBeenLastCalledWith(
expect.objectContaining({
event: 'promptAI',
payload: promptAIPayload,
promptAIPayload,
} satisfies Partial<PluginPromptAIMessage>),
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {
getUserContextMessage,
heightChangeMessage,
modalChangeMessage,
OnAssetSelectMessage,
OnContextRequestMessage,
OnUserContextRequestMessage,
OnLoadedMessage,
OnStateChangeMessage,
OnUnknownPluginMessage,
pluginLoadedMessage,
getPluginPromptAIMessage,
valueChangeMessage,
OnPromptAIMessage,
type OnAssetSelectMessage,
type OnContextRequestMessage,
type OnUserContextRequestMessage,
type OnLoadedMessage,
type OnStateChangeMessage,
type OnUnknownPluginMessage,
type OnPromptAIMessage,
type PromptAIPayload,
} from '../../messaging'
import { FieldPluginActions, Initialize } from '../FieldPluginActions'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ describe('handlePluginMessage', () => {
expect(callbacks.onLoaded).not.toHaveBeenCalled()
expect(callbacks.onStateChange).not.toHaveBeenCalled()
expect(callbacks.onContextRequest).not.toHaveBeenCalled()
expect(callbacks.onAssetSelect).not.toHaveBeenCalledWith(data)
expect(callbacks.onPromptAI).toHaveBeenCalled()
expect(callbacks.onAssetSelect).not.toHaveBeenCalled()
expect(callbacks.onPromptAI).toHaveBeenCalledWith(data)
expect(callbacks.onUnknownMessage).not.toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const stub: PluginPromptAIMessage = {
action: 'prompt-ai',
event: 'promptAI',
uid,
payload: {
promptAIPayload: {
action: 'prompt',
text: 'Some text to prompt',
},
Expand All @@ -36,7 +36,7 @@ describe('PromptAIMessage', () => {
}),
).toEqual(false)
})
it('has a payload', () => {
it('has a promptAIPayload', () => {
expect(
isPluginPromptAIMessage({
...stub,
Expand All @@ -45,15 +45,15 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: undefined,
promptAIPayload: undefined,
}),
).toEqual(false)
})
it('has the payload all required fields', () => {
it('has the promptAIPayload all required fields', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
action: '',
},
}),
Expand All @@ -62,7 +62,7 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
text: '',
},
}),
Expand All @@ -71,7 +71,7 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
action: 'prompt',
text: 'random text',
},
Expand All @@ -81,7 +81,7 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
action: 'prompt',
text: 123,
},
Expand All @@ -91,7 +91,7 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
action: null,
text: 123,
},
Expand All @@ -101,19 +101,19 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
promptAIPayload: {
action: null,
text: null,
},
}),
).toEqual(false)
})
it('has the payload a language in case of translation', () => {
it('has the promptAIPayload a language in case of translation', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
...stub.payload,
promptAIPayload: {
...stub.promptAIPayload,
action: 'translate',
},
}),
Expand All @@ -122,20 +122,20 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
...stub.payload,
promptAIPayload: {
...stub.promptAIPayload,
action: 'translate',
language: 'en',
},
}),
).toEqual(true)
})
it('has the payload a tone in case of tone adjustment', () => {
it('has the promptAIPayload a tone in case of tone adjustment', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
...stub.payload,
promptAIPayload: {
...stub.promptAIPayload,
action: 'adjust-tone',
},
}),
Expand All @@ -144,8 +144,8 @@ describe('PromptAIMessage', () => {
expect(
isPluginPromptAIMessage({
...stub,
payload: {
...stub.payload,
promptAIPayload: {
...stub.promptAIPayload,
action: 'adjust-tone',
tone: 'academic',
},
Expand All @@ -156,13 +156,13 @@ describe('PromptAIMessage', () => {
describe('constructor', () => {
it('includes the uid', () => {
expect(
getPluginPromptAIMessage(stub.payload, { uid, callbackId }),
getPluginPromptAIMessage(stub.promptAIPayload, { uid, callbackId }),
).toHaveProperty('uid', uid)
})
it('includes the payload was added to the message', () => {
it('includes the promptAIPayload', () => {
expect(
getPluginPromptAIMessage(stub.payload, { uid, callbackId }),
).toHaveProperty('payload')
getPluginPromptAIMessage(stub.promptAIPayload, { uid, callbackId }),
).toHaveProperty('promptAIPayload')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@ 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[] = [
export const promptAIActionsList = [
'prompt',
'complete',
'shorten',
Expand All @@ -31,7 +17,9 @@ export const promptAIActionsList: PromptAIAction[] = [
'adjust-tone',
'emojify',
'fix_spelling_and_grammar',
]
] as const

export type PromptAIAction = (typeof promptAIActionsList)[number]

export type PromptAIPayload = {
action: PromptAIAction
Expand All @@ -48,16 +36,16 @@ export type PluginPromptAIMessage = Omit<
'action'
> & {
action: 'prompt-ai'
payload: PromptAIPayload
promptAIPayload: PromptAIPayload
}

export const isPluginPromptAIMessage = (
obj: unknown,
): obj is PluginPromptAIMessage =>
isMessageToContainer(obj) &&
obj.event === 'promptAI' &&
hasKey(obj, 'payload') &&
isPromptAIPayloadValid(obj.payload as PromptAIPayload)
hasKey(obj, 'promptAIPayload') &&
isPromptAIPayloadValid(obj.promptAIPayload as PromptAIPayload)

export const isPromptAIPayloadValid = (promptAIPayload: PromptAIPayload) =>
promptAIPayload !== null &&
Expand All @@ -83,5 +71,5 @@ export const getPluginPromptAIMessage = (
action: 'prompt-ai',
event: 'promptAI',
...options,
payload: { ...message },
promptAIPayload: { ...message },
})
3 changes: 2 additions & 1 deletion packages/lib-helpers/test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const getContainer = (sendToFieldPlugin: (data: unknown) => void) => {
callbackId: data.callbackId,
aiResponse: {
ok: true,
answer: 'Fake AI answer for the prompt: ' + data.payload.text,
answer:
'Fake AI answer for the prompt: ' + data.promptAIPayload.text,
},
})
} else if (isGetUserContextMessage(data)) {
Expand Down

0 comments on commit 3f85b24

Please sign in to comment.