Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show the section that is streaming in during testing #300

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
import { useMemo, useEffect } from "react";

import type { LessonPlanKeys } from "@oakai/aila/src/protocol/schema";
import { LessonPlanKeysSchema } from "@oakai/aila/src/protocol/schema";
import { aiLogger } from "@oakai/logger";
import type { Message } from "ai";

const log = aiLogger("chat");

function findStreamingSections(message: Message | undefined): {
streamingSections: LessonPlanKeys[];
streamingSection: LessonPlanKeys | undefined;
content: string | undefined;
} {
if (!message?.content) {
return {
streamingSections: [],
streamingSection: undefined,
content: undefined,
};
}
const { content } = message;
const pathMatches: RegExpExecArray[] = [];
let match: RegExpExecArray | null;
const regex = /"path":"\/([^/"]*)(?:\/|")"/g;
let startIndex = 0;
while ((match = regex.exec(content.slice(startIndex))) !== null) {
pathMatches.push(match);
startIndex += match.index + match[0].length;
if (pathMatches.length > 100) {
log.warn("Too many path matches found, stopping search");
break;
}
}

const streamingSections: LessonPlanKeys[] = pathMatches
.map((match) => match[1])
.filter((i): i is string => typeof i === "string")
.map((section) => {
const result = LessonPlanKeysSchema.safeParse(section);
return result.success ? result.data : undefined;
})
.filter((section): section is LessonPlanKeys => section !== undefined);
const streamingSection: LessonPlanKeys | undefined =
streamingSections[streamingSections.length - 1];

return { streamingSections, streamingSection, content };
}

export type AilaStreamingStatus =
| "Loading"
| "RequestMade"
Expand All @@ -18,36 +60,49 @@ export const useAilaStreamingStatus = ({
}: {
isLoading: boolean;
messages: Message[];
}): AilaStreamingStatus => {
const ailaStreamingStatus = useMemo<AilaStreamingStatus>(() => {
const moderationStart = "MODERATION_START";
const chatStart = "CHAT_START";
if (messages.length === 0) return "Idle";
}): {
status: AilaStreamingStatus;
streamingSection: LessonPlanKeys | undefined;
streamingSections: LessonPlanKeys[] | undefined;
} => {
const { status, streamingSection, streamingSections } = useMemo(() => {
const moderationStart = `MODERATION_START`;
const chatStart = `CHAT_START`;
if (messages.length === 0)
return {
status: "Idle" as AilaStreamingStatus,
streamingSection: undefined,
};
const lastMessage = messages[messages.length - 1];

let status: AilaStreamingStatus = "Idle";
const { streamingSections, streamingSection, content } =
findStreamingSections(lastMessage);

if (isLoading) {
if (!lastMessage) return "Loading";
const { content } = lastMessage;
if (lastMessage.role === "user") {
return "RequestMade";
} else if (content.includes(moderationStart)) {
return "Moderating";
} else if (
content.includes('"type":"prompt"') ||
content.includes('\\"type\\":\\"prompt\\"')
) {
return "StreamingChatResponse";
} else if (content.includes(chatStart)) {
return "StreamingLessonPlan";
if (!lastMessage || !content) {
status = "Loading";
} else {
if (lastMessage.role === "user") {
status = "RequestMade";
} else if (content.includes(moderationStart)) {
status = "Moderating";
} else if (content.includes(`"type":"text"`)) {
status = "StreamingChatResponse";
} else if (content.includes(chatStart)) {
status = "StreamingLessonPlan";
} else {
status = "Loading";
}
}
return "Loading";
}
return "Idle";

return { status, streamingSections, streamingSection };
}, [isLoading, messages]);

useEffect(() => {
log.info("ailaStreamingStatus set:", ailaStreamingStatus);
}, [ailaStreamingStatus]);
log.info("ailaStreamingStatus set:", status);
}, [status]);

return ailaStreamingStatus;
return { status, streamingSection, streamingSections };
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const ChatLhsHeader = ({
<div data-testid="chat-aila-streaming-status">
{chat.ailaStreamingStatus}
</div>
<div data-testid="chat-aila-streaming-section">
{chat.streamingSection}
</div>
</div>
)}
<ChatButton
Expand Down
13 changes: 9 additions & 4 deletions apps/nextjs/src/components/AppComponents/Chat/chat-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Dispatch, SetStateAction } from "react";
import { useCallback, useEffect, useRef, useState } from "react";

import type { PersistedModerationBase } from "@oakai/core/src/utils/ailaModeration/moderationSchema";
import { camelCaseToTitleCase } from "@oakai/core/src/utils/camelCaseConversion";
import { OakBox, OakFlex, OakIcon, OakSpan } from "@oaknational/oak-components";
import type { Message } from "ai";
import Link from "next/link";
Expand Down Expand Up @@ -126,13 +127,17 @@ export const ChatMessagesDisplay = ({
ailaStreamingStatus: AilaStreamingStatus;
demo: DemoContextProps;
}) => {
const { lessonPlan, isStreaming } = useLessonChat();
const { lessonPlan, isStreaming, streamingSection } = useLessonChat();
const { setDialogWindow } = useDialog();
const { totalSections, totalSectionsComplete } = useProgressForDownloads({
lessonPlan,
isStreaming,
});

const workingOnItMessage = streamingSection
? `Editing ${camelCaseToTitleCase(streamingSection)}…`
: "Working on it…";

return (
<>
{messages.map((message) => {
Expand All @@ -159,7 +164,7 @@ export const ChatMessagesDisplay = ({
message={{
id: "working-on-it-initial",
role: "assistant",
content: "Working on it…",
content: workingOnItMessage,
}}
lastModeration={lastModeration}
persistedModerations={[]}
Expand All @@ -186,7 +191,7 @@ export const ChatMessagesDisplay = ({
? {
id: "working-on-it-initial",
role: "assistant",
content: "Working on it…",
content: workingOnItMessage,
}
: message
}
Expand All @@ -209,7 +214,7 @@ export const ChatMessagesDisplay = ({
message={{
id: "working-on-it-initial",
role: "assistant",
content: "Working on it…",
content: workingOnItMessage,
}}
lastModeration={lastModeration}
persistedModerations={[]}
Expand Down
13 changes: 12 additions & 1 deletion apps/nextjs/src/components/ContextProviders/ChatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { generateMessageId } from "@oakai/aila/src/helpers/chat/generateMessageI
import { parseMessageParts } from "@oakai/aila/src/protocol/jsonPatchProtocol";
import type {
AilaPersistedChat,
LessonPlanKeys,
LooseLessonPlan,
} from "@oakai/aila/src/protocol/schema";
import { isToxic } from "@oakai/core/src/utils/ailaModeration/helpers";
Expand Down Expand Up @@ -64,6 +65,8 @@ export type ChatContextProps = {
queuedUserAction: string | null;
queueUserAction: (action: string) => void;
executeQueuedAction: () => Promise<void>;
streamingSection: LessonPlanKeys | undefined;
streamingSections: LessonPlanKeys[] | undefined;
};

const ChatContext = createContext<ChatContextProps | null>(null);
Expand Down Expand Up @@ -382,7 +385,11 @@ export function ChatProvider({ id, children }: Readonly<ChatProviderProps>) {
? lastModeration
: toxicInitialModeration;

const ailaStreamingStatus = useAilaStreamingStatus({ isLoading, messages });
const {
status: ailaStreamingStatus,
streamingSection,
streamingSections,
} = useAilaStreamingStatus({ isLoading, messages });

useEffect(() => {
if (toxicModeration) {
Expand Down Expand Up @@ -416,6 +423,8 @@ export function ChatProvider({ id, children }: Readonly<ChatProviderProps>) {
queuedUserAction,
queueUserAction,
executeQueuedAction,
streamingSection,
streamingSections,
}),
[
id,
Expand All @@ -441,6 +450,8 @@ export function ChatProvider({ id, children }: Readonly<ChatProviderProps>) {
queuedUserAction,
queueUserAction,
executeQueuedAction,
streamingSection,
streamingSections,
],
);

Expand Down
Loading