-
Notifications
You must be signed in to change notification settings - Fork 1
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
df29cdd
commit fdabf7e
Showing
4 changed files
with
216 additions
and
144 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
apps/nextjs/src/components/AppComponents/Chat/chat-message/ChatMessagePart.stories.tsx
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,63 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { ChatMessagePart } from "./ChatMessagePart"; | ||
|
||
const meta: Meta<typeof ChatMessagePart> = { | ||
title: "Components/Chat/ChatMessagePart", | ||
component: ChatMessagePart, | ||
tags: ["autodocs"], | ||
args: { | ||
inspect: false, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof ChatMessagePart>; | ||
|
||
export const PromptMessagePart: Story = { | ||
args: { | ||
part: { | ||
document: { | ||
type: "prompt", | ||
message: | ||
"Are the learning outcome and learning cycles appropriate for your pupils? If not, suggest an edit. Otherwise, tap **Continue** to move on to the next step.", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const ErrorMessagePart: Story = { | ||
args: { | ||
part: { | ||
document: { | ||
type: "error", | ||
message: | ||
"**Unfortunately you’ve exceeded your fair usage limit for today.** Please come back in 11 hours. If you require a higher limit, please [make a request](https://forms.gle/tHsYMZJR367zydsG8).", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const TextMessagePart: Story = { | ||
args: { | ||
part: { | ||
document: { | ||
type: "text", | ||
value: | ||
"Are the learning outcome and learning cycles appropriate for your pupils? If not, suggest an edit. Otherwise, tap **Continue** to move on to the next step.", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const WithInspector: Story = { | ||
args: { | ||
inspect: true, | ||
part: { | ||
document: { | ||
type: "prompt", | ||
message: "This is a prompt", | ||
}, | ||
}, | ||
}, | ||
}; |
94 changes: 94 additions & 0 deletions
94
apps/nextjs/src/components/AppComponents/Chat/chat-message/ChatMessagePart.tsx
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,94 @@ | ||
import type { | ||
ErrorDocument, | ||
MessagePart, | ||
PromptDocument, | ||
TextDocument, | ||
} from "@oakai/aila/src/protocol/jsonPatchProtocol"; | ||
import { aiLogger } from "@oakai/logger"; | ||
|
||
import { MemoizedReactMarkdownWithStyles } from "@/components/AppComponents/Chat/markdown"; | ||
|
||
import type { ModerationModalHelpers } from "../../FeedbackForms/ModerationFeedbackModal"; | ||
|
||
const log = aiLogger("chat"); | ||
|
||
const components = { | ||
comment: NonRenderedPart, | ||
prompt: PromptMessagePart, | ||
error: ErrorMessagePart, | ||
bad: NonRenderedPart, | ||
patch: NonRenderedPart, | ||
/** | ||
* Patches do not get rendered, they get applied to the lesson plan | ||
* state, which is then rendered in the right hand side. | ||
*/ | ||
experimentalPatch: NonRenderedPart, | ||
state: NonRenderedPart, | ||
text: TextMessagePart, | ||
action: NonRenderedPart, | ||
moderation: NonRenderedPart, | ||
id: NonRenderedPart, | ||
unknown: NonRenderedPart, | ||
}; | ||
|
||
export interface ChatMessagePartProps { | ||
part: MessagePart; | ||
inspect: boolean; | ||
moderationModalHelpers: ModerationModalHelpers; | ||
} | ||
|
||
export function ChatMessagePart({ | ||
part, | ||
inspect, | ||
}: Readonly<ChatMessagePartProps>) { | ||
const PartComponent = components[part.document.type] as React.ComponentType<{ | ||
part: typeof part.document; | ||
}>; | ||
|
||
if (!PartComponent) { | ||
log.info("Unknown part type", part.document.type, JSON.stringify(part)); | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="w-full"> | ||
<PartComponent part={part.document} /> | ||
|
||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
inspect && <PartInspector part={part} /> | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
function NonRenderedPart() { | ||
return null; | ||
} | ||
|
||
function PromptMessagePart({ part }: Readonly<{ part: PromptDocument }>) { | ||
return <MemoizedReactMarkdownWithStyles markdown={part.message} />; | ||
} | ||
|
||
function ErrorMessagePart({ | ||
part, | ||
}: Readonly<{ | ||
part: ErrorDocument; | ||
}>) { | ||
const markdown = part.message ?? "Sorry, an error has occurred"; | ||
return <MemoizedReactMarkdownWithStyles markdown={markdown} />; | ||
} | ||
|
||
function TextMessagePart({ part }: Readonly<{ part: TextDocument }>) { | ||
return <MemoizedReactMarkdownWithStyles markdown={part.value} />; | ||
} | ||
|
||
function PartInspector({ part }: Readonly<{ part: MessagePart }>) { | ||
return ( | ||
<div className="w-full bg-gray-200 px-8 py-16"> | ||
<pre className="w-full text-wrap text-xs"> | ||
{JSON.stringify(part, null, 2)} | ||
</pre> | ||
</div> | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
apps/nextjs/src/components/AppComponents/Chat/chat-message/index.stories.tsx
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,57 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { ChatModerationProvider } from "@/components/ContextProviders/ChatModerationContext"; | ||
|
||
import { ChatMessage } from "./"; | ||
|
||
const meta: Meta<typeof ChatMessage> = { | ||
title: "Components/Chat/ChatMessage", | ||
component: ChatMessage, | ||
tags: ["autodocs"], | ||
decorators: [ | ||
(Story) => ( | ||
<ChatModerationProvider chatId="test-chat-id"> | ||
<Story /> | ||
</ChatModerationProvider> | ||
), | ||
], | ||
args: { | ||
persistedModerations: [], | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof ChatMessage>; | ||
|
||
export const UserMessage: Story = { | ||
args: { | ||
message: { | ||
id: "test-chat-id", | ||
content: | ||
"Create a lesson plan about the end of Roman Britain for key stage 3 history", | ||
role: "user", | ||
}, | ||
}, | ||
}; | ||
|
||
export const LlmMessage: Story = { | ||
args: { | ||
message: { | ||
id: "test-chat-id", | ||
content: | ||
'{"type":"llmMessage","sectionsToEdit":["learningOutcome","learningCycles"],"patches":[{"type":"patch","reasoning":"Since there are no existing Oak lessons for this topic, I have created a new lesson plan from scratch focusing on the end of Roman Britain.","value":{"type":"string","op":"add","path":"/learningOutcome","value":"I can explain the reasons behind the decline of Roman Britain and its impact on society."},"status":"complete"},{"type":"patch","reasoning":"I have outlined the learning cycles to break down the lesson structure for teaching about the end of Roman Britain.","value":{"type":"string-array","op":"add","path":"/learningCycles","value":["Identify the key events leading to the end of Roman Britain.","Describe the societal changes that occurred post-Roman withdrawal.","Analyse the archaeological evidence of Roman Britain\'s legacy."]},"status":"complete"}],"sectionsEdited":["learningOutcome","learningCycles"],"prompt":{"type":"text","value":"Are the learning outcome and learning cycles appropriate for your pupils? If not, suggest an edit. Otherwise, tap **Continue** to move on to the next step."},"status":"complete"}', | ||
role: "assistant", | ||
}, | ||
}, | ||
}; | ||
|
||
export const ErrorMessage: Story = { | ||
args: { | ||
message: { | ||
id: "test-chat-id", | ||
role: "assistant", | ||
content: | ||
'{"type":"error","value":"Rate limit exceeded","message":"**Unfortunately you’ve exceeded your fair usage limit for today.** Please come back in 11 hours. If you require a higher limit, please [make a request](https://forms.gle/tHsYMZJR367zydsG8)."}', | ||
}, | ||
}, | ||
}; |
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