-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rename a few things * remove a few unused flex properties * simplify conditional * remove unneessary alignItems * only affect direct children * remove rawAnchor * drawer chat first pass * connect chat to api * show at md width * tweak spacing, update smoot * fix tests * add two simple tests * tweak title, prompts * bump smoot * bump smoot * bump smoot * bump smoot * rename css class * tweak demo height * bump smoot * use default collection_name * restore feature flag * fix a width issue * fix comment * bump smoot * bump smoot last time (??) * keep CallToAction border when chat open * equalize spacing left/right of chat * kebab-case-the-flag-like-others
- Loading branch information
1 parent
46a3898
commit 471e5c1
Showing
23 changed files
with
531 additions
and
175 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
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,19 @@ | ||
/** | ||
* For some reason Jest's JSDOM environment does not include these, though they | ||
* have been available in NodeJS and web browsers for a while now. | ||
*/ | ||
import { TestEnvironment } from "jest-environment-jsdom" | ||
import { EnvironmentContext, JestEnvironmentConfig } from "@jest/environment" | ||
|
||
class JSDOMEnvironmentExtended extends TestEnvironment { | ||
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) { | ||
super(config, context) | ||
|
||
this.global.TransformStream = TransformStream | ||
this.global.ReadableStream = ReadableStream | ||
this.global.Response = Response | ||
this.global.TextDecoderStream = TextDecoderStream | ||
} | ||
} | ||
|
||
export default JSDOMEnvironmentExtended |
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
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
43 changes: 43 additions & 0 deletions
43
frontends/main/src/page-components/LearningResourceDrawer/AiChatSyllabus.test.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,43 @@ | ||
import React from "react" | ||
import AiChatSyllabus from "./AiChatSyllabus" | ||
import { renderWithProviders, screen } from "@/test-utils" | ||
import { factories, setMockResponse, urls } from "api/test-utils" | ||
|
||
/** | ||
* Note: This component is primarily tested in @mitodl/smoot-design. | ||
* | ||
* Here we just check a few config settings. | ||
*/ | ||
describe("AiChatSyllabus", () => { | ||
test("Greets authenticated user by name", async () => { | ||
const resource = factories.learningResources.course() | ||
const user = factories.user.user() | ||
|
||
// Sanity | ||
expect(user.profile.name).toBeTruthy() | ||
|
||
setMockResponse.get(urls.userMe.get(), user) | ||
renderWithProviders( | ||
<AiChatSyllabus onClose={jest.fn()} resource={resource} />, | ||
) | ||
|
||
// byAll because there are two instances, one is SR-only in an aria-live area | ||
// check for username and resource title | ||
await screen.findAllByText( | ||
new RegExp(`Hello ${user.profile.name}.*${resource.title}.*`), | ||
) | ||
}) | ||
|
||
test("Greets anonymous user generically", async () => { | ||
const resource = factories.learningResources.course() | ||
|
||
setMockResponse.get(urls.userMe.get(), {}, { code: 403 }) | ||
renderWithProviders( | ||
<AiChatSyllabus onClose={jest.fn()} resource={resource} />, | ||
) | ||
|
||
// byAll because there are two instances, one is SR-only in an aria-live area | ||
// check for username and resource title | ||
await screen.findAllByText(new RegExp(`Hello and.*${resource.title}.*`)) | ||
}) | ||
}) |
79 changes: 79 additions & 0 deletions
79
frontends/main/src/page-components/LearningResourceDrawer/AiChatSyllabus.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,79 @@ | ||
import * as React from "react" | ||
import type { AiChatProps } from "@mitodl/smoot-design/ai" | ||
import { getCsrfToken } from "@/common/utils" | ||
import { LearningResource } from "api" | ||
import { useUserMe } from "api/hooks/user" | ||
import type { User } from "api/hooks/user" | ||
import dynamic from "next/dynamic" | ||
import type { SyllabusChatRequestRequest } from "api/v0" | ||
|
||
const AiChat = dynamic( | ||
() => import("@mitodl/smoot-design/ai").then((mod) => mod.AiChat), | ||
{ ssr: false }, | ||
) | ||
|
||
const STARTERS: AiChatProps["conversationStarters"] = [ | ||
{ content: "What is this course about?" }, | ||
{ content: "What are the prerequisites for this course?" }, | ||
{ content: "How will this course be graded?" }, | ||
] | ||
|
||
const getInitialMessage = ( | ||
resource: LearningResource, | ||
user?: User, | ||
): AiChatProps["initialMessages"] => { | ||
const grettings = user?.profile?.name | ||
? `Hello ${user.profile.name}, ` | ||
: "Hello and " | ||
return [ | ||
{ | ||
content: `${grettings} welcome to **${resource.title}**. How can I assist you today?`, | ||
role: "assistant", | ||
}, | ||
] | ||
} | ||
|
||
type AiChatSyllabusProps = { | ||
onClose: () => void | ||
resource?: LearningResource | ||
className?: string | ||
} | ||
|
||
const AiChatSyllabus: React.FC<AiChatSyllabusProps> = ({ | ||
onClose, | ||
resource, | ||
...props | ||
}) => { | ||
const user = useUserMe() | ||
if (!resource) return null | ||
|
||
return ( | ||
<AiChat | ||
data-testid="ai-chat-syllabus" | ||
conversationStarters={STARTERS} | ||
initialMessages={getInitialMessage(resource, user.data)} | ||
chatId={`chat-${resource?.readable_id}`} | ||
title="Ask Tim about this course" | ||
onClose={onClose} | ||
requestOpts={{ | ||
apiUrl: `${process.env.NEXT_PUBLIC_MITOL_API_BASE_URL}/api/v0/syllabus_agent/`, | ||
fetchOpts: { | ||
headers: { | ||
"X-CSRFToken": getCsrfToken(), | ||
}, | ||
}, | ||
transformBody: (messages) => { | ||
const body: SyllabusChatRequestRequest = { | ||
collection_name: "content_files", | ||
message: messages[messages.length - 1].content, | ||
readable_id: resource?.readable_id, | ||
} | ||
return body | ||
}, | ||
}} | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export default AiChatSyllabus |
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
Oops, something went wrong.