Skip to content

Commit f8fd00b

Browse files
feat(SHAPE-8085): improve prompt ai response message
1 parent 11297f8 commit f8fd00b

File tree

6 files changed

+185
-24
lines changed

6 files changed

+185
-24
lines changed

packages/demo/src/components/PromptAI.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useState } from 'react'
1212
import {
1313
isPromptAIPayloadValid,
1414
promptAIActionsList,
15+
PromptAIResponse,
1516
type PromptAIAction,
1617
} from '@storyblok/field-plugin'
1718
import type { PluginComponent } from './FieldPluginDemo'
@@ -23,7 +24,7 @@ export const PromptAI: PluginComponent = (props) => {
2324
const [promptAction, setPromptAction] = useState<PromptAIAction>('prompt')
2425
const [promptLanguage, setPromptLanguage] = useState<string>()
2526
const [promptTone, setPromptTone] = useState<string>()
26-
const [promptAIGeneratedText, setPromptAIGeneratedText] = useState<string>()
27+
const [promptAIResponse, setPromptAIResponse] = useState<PromptAIResponse>()
2728
const [promptBasedOnCurrentStory, setPromptBasedOnCurrentStory] =
2829
useState<boolean>(false)
2930

@@ -41,9 +42,9 @@ export const PromptAI: PluginComponent = (props) => {
4142
return
4243
}
4344

44-
const promptAIGeneratedText = await actions.promptAI(payload)
45+
const promptAIResponse = await actions.promptAI(payload)
4546

46-
setPromptAIGeneratedText(promptAIGeneratedText)
47+
setPromptAIResponse(promptAIResponse)
4748
}
4849

4950
return (
@@ -92,7 +93,12 @@ export const PromptAI: PluginComponent = (props) => {
9293
onChange={(e) => setPromptBasedOnCurrentStory(e.target.checked)}
9394
/>
9495
</FormControl>
95-
<Typography>AI Generated Text: {promptAIGeneratedText}</Typography>
96+
<Typography>
97+
{promptAIResponse?.ok === true &&
98+
`AI Generated Text: ${promptAIResponse.answer}`}
99+
{promptAIResponse?.ok === false &&
100+
`AI Error: ${promptAIResponse.error}`}
101+
</Typography>
96102
<Button
97103
variant="outlined"
98104
color="secondary"

packages/field-plugin/src/createFieldPlugin/FieldPluginActions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
UserData,
55
ModalSize,
66
PromptAIPayload,
7+
PromptAIResponse,
78
} from '../messaging'
89
import type { FieldPluginData } from './FieldPluginData'
910

@@ -15,7 +16,7 @@ export type SetModalOpen<Content> = (
1516
modalSize?: ModalSize,
1617
) => Promise<FieldPluginData<Content>>
1718
export type RequestContext = () => Promise<StoryData>
18-
export type PromptAI = (payload: PromptAIPayload) => Promise<string>
19+
export type PromptAI = (payload: PromptAIPayload) => Promise<PromptAIResponse>
1920
export type RequestUserContext = () => Promise<UserData>
2021
export type SelectAsset = () => Promise<Asset>
2122
export type Initialize<Content> = () => Promise<FieldPluginData<Content>>

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginMessageListener/handlePluginMessage.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ describe('handlePluginMessage', () => {
148148
action: 'prompt-ai',
149149
uid,
150150
callbackId: 'test-callback-id',
151-
aiGeneratedText: 'Some AI generated text',
151+
aiResponse: {
152+
ok: true,
153+
answer: 'Some AI generated text',
154+
},
152155
}
153156

154157
const callbacks = mockCallbacks()

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/PromptAIResponseMessage.test.ts

Lines changed: 147 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const stub: PromptAIResponseMessage = {
77
action: 'prompt-ai',
88
uid: '-preview',
99
callbackId: 'test-callback-id',
10-
aiGeneratedText: 'test-ai-generated-text',
10+
aiResponse: {
11+
ok: true,
12+
answer: 'test-ai-answer',
13+
},
1114
}
1215

1316
describe('PromptAIResponseMessage', function () {
@@ -30,50 +33,184 @@ describe('PromptAIResponseMessage', function () {
3033
).toEqual(false)
3134
})
3235
})
33-
describe('the aiGeneratedText property', () => {
36+
describe('the aiResponse property', () => {
3437
it('cannot be undefined', () => {
3538
expect(
3639
isPromptAIMessage({
3740
...stub,
38-
aiGeneratedText: undefined,
41+
aiResponse: undefined,
3942
}),
4043
).toEqual(false)
4144
})
42-
it('is a string', () => {
45+
46+
it('cannot be null', () => {
47+
expect(
48+
isPromptAIMessage({
49+
...stub,
50+
aiResponse: null,
51+
}),
52+
).toEqual(false)
53+
})
54+
55+
it('should be an object', () => {
56+
expect(
57+
isPromptAIMessage({
58+
...stub,
59+
aiResponse: 'any-string',
60+
}),
61+
).toEqual(false)
62+
})
63+
64+
it('should contain an ok property', () => {
65+
expect(
66+
isPromptAIMessage({
67+
...stub,
68+
aiResponse: {
69+
answer: 'any AI answer',
70+
},
71+
}),
72+
).toEqual(false)
73+
})
74+
75+
it('should contain an error property in case of failure', () => {
76+
expect(
77+
isPromptAIMessage({
78+
...stub,
79+
aiResponse: {
80+
ok: false,
81+
error: 'any error message',
82+
},
83+
}),
84+
).toEqual(true)
85+
})
86+
87+
it('should contain an answer property in case of success', () => {
88+
expect(
89+
isPromptAIMessage({
90+
...stub,
91+
aiResponse: {
92+
ok: true,
93+
answer: 'any AI answer',
94+
},
95+
}),
96+
).toEqual(true)
97+
})
98+
99+
it('error is a string', () => {
100+
expect(
101+
isPromptAIMessage({
102+
...stub,
103+
aiResponse: {
104+
ok: false,
105+
error: 'any AI answer',
106+
},
107+
}),
108+
).toEqual(true)
109+
110+
expect(
111+
isPromptAIMessage({
112+
...stub,
113+
aiResponse: {
114+
ok: false,
115+
error: 123,
116+
},
117+
}),
118+
).toEqual(false)
119+
120+
expect(
121+
isPromptAIMessage({
122+
...stub,
123+
aiResponse: {
124+
ok: false,
125+
error: null,
126+
},
127+
}),
128+
).toEqual(false)
129+
130+
expect(
131+
isPromptAIMessage({
132+
...stub,
133+
aiResponse: {
134+
ok: false,
135+
error: [],
136+
},
137+
}),
138+
).toEqual(false)
139+
140+
expect(
141+
isPromptAIMessage({
142+
...stub,
143+
aiResponse: {
144+
ok: false,
145+
error: {},
146+
},
147+
}),
148+
).toEqual(false)
149+
150+
expect(
151+
isPromptAIMessage({
152+
...stub,
153+
aiResponse: {
154+
ok: false,
155+
error: false,
156+
},
157+
}),
158+
).toEqual(false)
159+
})
160+
161+
it('answer is a string', () => {
43162
expect(
44163
isPromptAIMessage({
45164
...stub,
46-
aiGeneratedText: 'any-string',
165+
aiResponse: {
166+
ok: true,
167+
answer: 'any AI answer',
168+
},
47169
}),
48170
).toEqual(true)
49171
expect(
50172
isPromptAIMessage({
51173
...stub,
52-
aiGeneratedText: 123,
174+
aiResponse: {
175+
ok: true,
176+
answer: 123,
177+
},
53178
}),
54179
).toEqual(false)
55180
expect(
56181
isPromptAIMessage({
57182
...stub,
58-
aiGeneratedText: null,
183+
aiResponse: {
184+
ok: true,
185+
answer: null,
186+
},
59187
}),
60188
).toEqual(false)
61189
expect(
62190
isPromptAIMessage({
63191
...stub,
64-
aiGeneratedText: [],
192+
aiResponse: {
193+
ok: true,
194+
answer: [],
195+
},
65196
}),
66197
).toEqual(false)
67198
expect(
68199
isPromptAIMessage({
69200
...stub,
70-
aiGeneratedText: {},
201+
aiResponse: {
202+
ok: true,
203+
answer: {},
204+
},
71205
}),
72206
).toEqual(false)
73207
expect(
74208
isPromptAIMessage({
75209
...stub,
76-
aiGeneratedText: false,
210+
aiResponse: {
211+
ok: true,
212+
answer: false,
213+
},
77214
}),
78215
).toEqual(false)
79216
})

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/PromptAIResponseMessage.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@ import { isMessageToPlugin, type MessageToPlugin } from './MessageToPlugin'
44
/**
55
* The object returned when calling the "prompt-ai" action.
66
*/
7+
export type PromptAIResponse =
8+
| { ok: true; answer: string }
9+
| { ok: false; error: string }
10+
711
export type PromptAIResponseMessage = MessageToPlugin<'prompt-ai'> & {
8-
aiGeneratedText: string
12+
aiResponse: PromptAIResponse
913
}
1014

1115
export const isPromptAIMessage = (
1216
data: unknown,
1317
): data is PromptAIResponseMessage =>
1418
isMessageToPlugin(data) &&
1519
data.action === 'prompt-ai' &&
16-
hasKey(data, 'aiGeneratedText') &&
17-
typeof data.aiGeneratedText === 'string'
20+
hasKey(data, 'aiResponse') &&
21+
typeof data.aiResponse === 'object' &&
22+
data.aiResponse !== null &&
23+
hasKey(data.aiResponse, 'ok') &&
24+
typeof data.aiResponse.ok === 'boolean' &&
25+
(data.aiResponse.ok
26+
? hasKey(data.aiResponse, 'answer') &&
27+
typeof data.aiResponse.answer === 'string'
28+
: hasKey(data.aiResponse, 'error') &&
29+
typeof data.aiResponse.error === 'string')
1830

1931
export const getResponseFromPromptAIMessage = (
2032
message: PromptAIResponseMessage,
21-
): string => {
22-
const { aiGeneratedText } = message
23-
return aiGeneratedText
33+
) => {
34+
const { aiResponse } = message
35+
return aiResponse
2436
}

packages/lib-helpers/test/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ const getContainer = (sendToFieldPlugin: (data: unknown) => void) => {
115115
action: 'prompt-ai',
116116
uid,
117117
callbackId: data.callbackId,
118-
aiGeneratedText:
119-
'Fake AI generated text for the prompt: ' + data.payload.text,
118+
aiResponse: {
119+
ok: true,
120+
answer: 'Fake AI answer for the prompt: ' + data.payload.text,
121+
},
120122
})
121123
} else if (isGetUserContextMessage(data)) {
122124
sendToFieldPlugin({

0 commit comments

Comments
 (0)