-
Notifications
You must be signed in to change notification settings - Fork 0
feat: AiChatContext / useAiChat #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
export { AiChat } from "./components/AiChat/AiChat" | ||
export type { AiChatProps } from "./components/AiChat/AiChat" | ||
export type { AiChatMessage } from "./components/AiChat/types" | ||
export { AiChat, AiChatDisplay } from "./components/AiChat/AiChat" | ||
export { AiChatProvider, useAiChat } from "./components/AiChat/AiChatContext" | ||
export type { | ||
AiChatMessage, | ||
AiChatContextProps, | ||
AiChatDisplayProps, | ||
AiChatProps, | ||
} from "./components/AiChat/types" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
import * as React from "react" | ||
import type { Meta, StoryObj } from "@storybook/react" | ||
import { AiChatDisplay } from "./AiChat" | ||
import { AiChatProvider, useAiChat } from "./AiChatContext" | ||
import type { AiChatProps } from "./types" | ||
import styled from "@emotion/styled" | ||
import { handlers } from "./test-utils/api" | ||
import Typography from "@mui/material/Typography" | ||
|
||
const TEST_API_STREAMING = "http://localhost:4567/streaming" | ||
|
||
const INITIAL_MESSAGES: AiChatProps["initialMessages"] = [ | ||
{ | ||
content: "Hi! What are you interested in learning about?", | ||
role: "assistant", | ||
}, | ||
] | ||
|
||
const STARTERS = [ | ||
{ content: "I'm interested in quantum computing" }, | ||
{ content: "I want to understand global warming. " }, | ||
{ content: "I am curious about AI applications for business" }, | ||
] | ||
|
||
const Container = styled.div({ | ||
width: "100%", | ||
height: "400px", | ||
position: "relative", | ||
}) | ||
|
||
const MessageCounter = () => { | ||
const { messages } = useAiChat() | ||
|
||
return ( | ||
<Typography variant="subtitle1"> | ||
Message count: {messages.length} (Provided by <code>AiChatContext</code>) | ||
</Typography> | ||
) | ||
} | ||
|
||
/** | ||
* AiChatProvider provides state and functions for managing chat. The higher-level | ||
* `AiChat` component is a wrapper around this provider and the `AiChatDisplay`, | ||
* roughly. | ||
* | ||
* If you need to access chat state outside of the chat display, you can use | ||
* `AiChatProvider` directly. | ||
*/ | ||
const meta: Meta<typeof AiChatProvider> = { | ||
title: "smoot-design/AI/AiChatContext", | ||
component: AiChatProvider, | ||
parameters: { | ||
msw: { handlers }, | ||
}, | ||
render: (args) => { | ||
return ( | ||
<AiChatProvider {...args}> | ||
<MessageCounter /> | ||
<Container> | ||
<AiChatDisplay | ||
entryScreenEnabled={false} | ||
conversationStarters={STARTERS} | ||
placeholder="Type your message here" | ||
askTimTitle="Ask TIM" | ||
/> | ||
</Container> | ||
</AiChatProvider> | ||
) | ||
}, | ||
decorators: (Story) => { | ||
return ( | ||
<Container> | ||
<Story /> | ||
</Container> | ||
) | ||
}, | ||
args: { | ||
requestOpts: { apiUrl: TEST_API_STREAMING }, | ||
initialMessages: INITIAL_MESSAGES, | ||
}, | ||
argTypes: { | ||
initialMessages: { | ||
control: { type: "object", disable: true }, | ||
}, | ||
requestOpts: { | ||
control: { type: "object", disable: true }, | ||
table: { readonly: true }, // See above | ||
}, | ||
}, | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof AiChatProvider> | ||
|
||
export const StreamingResponses: Story = {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.