Skip to content

Commit

Permalink
Merge branch 'feature/gra-866-grai-experiment' of github.com:grai-io/…
Browse files Browse the repository at this point in the history
…grai-core into feature/gra-866-grai-experiment
  • Loading branch information
ieaves committed Oct 26, 2023
2 parents 896704d + a9b14dd commit 7f2ceda
Show file tree
Hide file tree
Showing 14 changed files with 568 additions and 301 deletions.
501 changes: 264 additions & 237 deletions grai-frontend/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion grai-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"react-helmet": "^6.1.0",
"react-helmet-async": "^1.3.0",
"react-instantsearch-hooks-web": "^6.41.0",
"react-json-view-lite": "^0.9.5",
"react-json-view-lite": "^1.1.0",
"react-qr-code": "^2.0.12",
"react-router-dom": "^6.0.0",
"react-shepherd": "^4.2.0",
Expand Down
35 changes: 35 additions & 0 deletions grai-frontend/src/components/chat/ChatWrapper.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react"
import { render, screen, waitFor } from "testing"
import ChatWrapper, { FETCH_CHATS } from "./ChatWrapper"

const workspace = {
id: "1",
}

test("renders", async () => {
render(<ChatWrapper workspace={workspace} />)

await waitFor(() => {
expect(screen.getByRole("textbox")).toBeInTheDocument()
})
})

test("error", async () => {
const mocks = [
{
request: {
query: FETCH_CHATS,
variables: {
workspaceId: "1",
},
},
error: new Error("An error occurred"),
},
]

render(<ChatWrapper workspace={workspace} />, { mocks })

await waitFor(() => {
expect(screen.getByText("An error occurred")).toBeInTheDocument()
})
})
75 changes: 75 additions & 0 deletions grai-frontend/src/components/chat/ChatWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { useEffect } from "react"
import { gql, useMutation } from "@apollo/client"
import Loading from "components/layout/Loading"
import GraphError from "components/utils/GraphError"
import {
FetchChats,
FetchChatsVariables,
FetchChats_fetchOrCreateChats_data_messages_data,
} from "./__generated__/FetchChats"
import { Chat } from "./ChatWindow"
import WebsocketChat from "./WebsocketChat"

export const FETCH_CHATS = gql`
mutation FetchChats($workspaceId: ID!) {
fetchOrCreateChats(workspaceId: $workspaceId) {
data {
id
messages {
data {
id
message
role
created_at
}
}
}
}
}
`

interface Workspace {
id: string
}

type ChatWrapperProps = {
workspace: Workspace
}

const ChatWrapper: React.FC<ChatWrapperProps> = ({ workspace }) => {
const [fetchChats, { data, loading, error }] = useMutation<
FetchChats,
FetchChatsVariables
>(FETCH_CHATS, {
variables: {
workspaceId: workspace.id,
},
})

useEffect(() => {
fetchChats().catch(() => {})
}, [fetchChats])

if (error) return <GraphError error={error} />
if (loading || !data) return <Loading />

const chats: Chat[] = data.fetchOrCreateChats.data
.reduce<FetchChats_fetchOrCreateChats_data_messages_data[]>((acc, chat) => {
return acc.concat(chat.messages.data)
}, [])
.map(message => ({
message: message.message,
sender: message.role === "USER",
}))
const chatId = data.fetchOrCreateChats.data.at(-1)?.id

return (
<WebsocketChat
workspace={workspace}
initialChats={chats}
initialChatId={chatId}
/>
)
}

export default ChatWrapper
40 changes: 40 additions & 0 deletions grai-frontend/src/components/chat/__generated__/FetchChats.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grai-frontend/src/components/edges/EdgeProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const EdgeProfile: React.FC<EdgeProfileProps> = ({ edge }) => {
{edge.metadata && (
<JsonView
data={edge.metadata}
shouldInitiallyExpand={level => level < 1}
shouldExpandNode={level => level < 1}
style={{ ...defaultStyles, container: "" }}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion grai-frontend/src/components/nodes/TableDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const TableDetail: React.FC<TableDetailProps> = ({ table }) => (
{table.metadata && (
<JsonView
data={table.metadata}
shouldInitiallyExpand={level => level < 1}
shouldExpandNode={level => level < 1}
style={{ ...defaultStyles, container: "" }}
/>
)}
Expand Down
36 changes: 2 additions & 34 deletions grai-frontend/src/pages/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@ import React from "react"
import { gql, useQuery } from "@apollo/client"
import { Box, Grid, Typography } from "@mui/material"
import useWorkspace from "helpers/useWorkspace"
import { Chat as ChatType } from "components/chat/ChatWindow"
import WebsocketChat from "components/chat/WebsocketChat"
import ChatWrapper from "components/chat/ChatWrapper"
import Loading from "components/layout/Loading"
import GraphError from "components/utils/GraphError"
import {
GetWorkspaceChat,
GetWorkspaceChatVariables,
GetWorkspaceChat_workspace_chats_data_messages_data,
} from "./__generated__/GetWorkspaceChat"
import NotFound from "./NotFound"

export const GET_WORKSPACE = gql`
query GetWorkspaceChat($organisationName: String!, $workspaceName: String!) {
workspace(organisationName: $organisationName, name: $workspaceName) {
id
chats {
data {
id
messages {
data {
id
message
role
created_at
}
}
}
}
}
}
`
Expand All @@ -54,29 +39,12 @@ const Chat: React.FC = () => {

if (!workspace) return <NotFound />

const chats: ChatType[] = workspace.chats.data
.reduce<GetWorkspaceChat_workspace_chats_data_messages_data[]>(
(acc, chat) => {
return acc.concat(chat.messages.data)
},
[],
)
.map(message => ({
message: message.message,
sender: message.role === "USER",
}))
const chatId = workspace.chats.data.at(-1)?.id

return (
<Box sx={{ p: 3 }}>
<Typography variant="h6">GrAI Workspace Chat</Typography>
<Grid container spacing={3}>
<Grid item xs={12} md={6}>
<WebsocketChat
workspace={workspace}
initialChats={chats}
initialChatId={chatId}
/>
<ChatWrapper workspace={workspace} />
</Grid>
</Grid>
</Box>
Expand Down
25 changes: 0 additions & 25 deletions grai-frontend/src/pages/__generated__/GetWorkspaceChat.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions grai-frontend/src/testing/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11069,6 +11069,37 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "fetchOrCreateChats",
"description": null,
"args": [
{
"name": "workspaceId",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "ChatDataWrapper",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down Expand Up @@ -11236,6 +11267,41 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "ChatDataWrapper",
"description": null,
"fields": [
{
"name": "data",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Chat",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "__Schema",
Expand Down
4 changes: 2 additions & 2 deletions grai-server/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ RUN apt update \
libpq-dev \
unixodbc-dev \
gnupg2 \
&& curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg \
&& curl https://packages.microsoft.com/config/debian/12/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \
&& ACCEPT_EULA=Y apt-get install -y msodbcsql18 \
&& rm -rf /var/lib/apt/lists/*
Expand Down
2 changes: 2 additions & 0 deletions grai-server/app/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from api.queries import Query
from auth.mutations import Mutation as AuthMutation
from connections.mutations import Mutation as ConnectionMutation
from grAI.mutations import Mutation as grAIMutation
from installations.mutations import Mutation as InstallationMutation
from lineage.mutations import Mutation as LineageMutation
from workspaces.mutations import Mutation as WorkspaceMutation
Expand All @@ -18,6 +19,7 @@
ConnectionMutation,
NotificationMutation,
LineageMutation,
grAIMutation,
)
Mutation = merge_types("Mutation", mutations)

Expand Down
Loading

0 comments on commit 7f2ceda

Please sign in to comment.