Skip to content

Commit 1aec1d1

Browse files
author
Bogdan Tsechoev
committed
Merge branch 'bot_ui_guest_access' into 'master'
Bot UI: Convert thread and redirect to bot page See merge request postgres-ai/database-lab!887
2 parents fb16d2c + 11818e2 commit 1aec1d1

File tree

10 files changed

+67
-6
lines changed

10 files changed

+67
-6
lines changed

ui/packages/platform/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ ENV REACT_APP_SENTRY_DSN=$ARG_REACT_APP_SENTRY_DSN
4747
ARG ARG_REACT_APP_WS_URL
4848
ENV REACT_APP_WS_URL=$ARG_REACT_APP_WS_URL
4949

50+
ARG ARG_REACT_APP_BOT_API_URL
51+
ENV REACT_APP_BOT_API_URL=$ARG_REACT_APP_BOT_API_URL
52+
5053
RUN apk add --no-cache --update git && \
5154
npm i -g [email protected]; \
5255
pnpm config set store-dir /app/.pnpm-store; \

ui/packages/platform/ci_docker_build_push.sh

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ DOCKER_BUILDKIT=1 docker build \
3838
--build-arg ARG_REACT_APP_ROOT_URL="${REACT_APP_ROOT_URL}" \
3939
--build-arg ARG_REACT_APP_SENTRY_DSN="${REACT_APP_SENTRY_DSN}" \
4040
--build-arg ARG_REACT_APP_WS_URL="${REACT_APP_WS_URL}" \
41+
--build-arg ARG_REACT_APP_BOT_API_URL="${REACT_APP_BOT_API_URL}" \
4142
$tags_build --file ./ui/packages/platform/Dockerfile .
4243
set +x
4344

ui/packages/platform/deploy/configs/dev.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ export REACT_APP_SENTRY_DSN=""
2222

2323
# AI Bot
2424
# don't forget trailing slash if GET path is used!
25-
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # we use staging for now, since dev env is not fullfledged yet
25+
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # we use staging for now, since dev env is not fullfledged yet
26+
export REACT_APP_BOT_API_URL="https://v2.postgres.ai/ai-bot-api/bot"

ui/packages/platform/deploy/configs/local.sh

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export REACT_APP_SENTRY_DSN=""
2222

2323
# AI Bot
2424
export REACT_APP_WS_URL="ws://localhost:9100/" # don't forget trailing slash if GET path is used!
25+
export REACT_APP_BOT_API_URL="https://v2.postgres.ai/ai-bot-api/bot"

ui/packages/platform/deploy/configs/production.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export REACT_APP_STRIPE_API_KEY="xxx"
2323
export REACT_APP_SENTRY_DSN="https://[email protected]/2"
2424

2525
# AI Bot
26-
export REACT_APP_WS_URL="wss://postgres.ai/ai-bot-ws/" # don't forget trailing slash!
26+
export REACT_APP_WS_URL="wss://postgres.ai/ai-bot-ws/" # don't forget trailing slash!
27+
export REACT_APP_BOT_API_URL="https://postgres.ai/ai-bot-api/bot"

ui/packages/platform/deploy/configs/staging.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export REACT_APP_STRIPE_API_KEY="xxx"
2121
export REACT_APP_SENTRY_DSN=""
2222

2323
# AI Bot
24-
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # don't forget trailing slash!
24+
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # don't forget trailing slash!
25+
export REACT_APP_BOT_API_URL="https://v2.postgres.ai/ai-bot-api/bot"

ui/packages/platform/deploy/platform-console.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ spec:
4949
value: "$REACT_APP_STRIPE_API_KEY"
5050
- name: REACT_APP_WS_URL
5151
value: "$REACT_APP_WS_URL"
52+
- name: REACT_APP_BOT_API_URL
53+
value: "$REACT_APP_BOT_API_URL"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {request} from "../../helpers/request";
2+
3+
export const convertThread = async (thread_id: string): Promise<{ response: { final_thread_id: string, msg: string } | null; error: Response | null }> => {
4+
const apiServer = process.env.REACT_APP_BOT_API_URL || '';
5+
6+
try {
7+
const response = await request(
8+
`/convert_thread`,
9+
{
10+
method: 'POST',
11+
body: JSON.stringify({
12+
thread_id
13+
})
14+
},
15+
apiServer
16+
);
17+
18+
if (!response.ok) {
19+
return { response: null, error: response };
20+
}
21+
22+
const responseData = await response.json();
23+
24+
return { response: responseData, error: null };
25+
26+
} catch (error) {
27+
return { response: null, error: error as Response };
28+
}
29+
}

ui/packages/platform/src/components/Dashboard/Dashboard.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { ProductCardWrapper } from 'components/ProductCard/ProductCardWrapper'
4646
import { DashboardProps } from 'components/Dashboard/DashboardWrapper'
4747
import { FilteredTableMessage } from 'components/AccessTokens/FilteredTableMessage/FilteredTableMessage'
4848
import { CreatedDbLabCards } from 'components/CreateDbLabCards/CreateDbLabCards'
49+
import { convertThread } from "../../api/bot/convertThread";
4950

5051
interface DashboardWithStylesProps extends DashboardProps {
5152
classes: ClassesType
@@ -163,6 +164,8 @@ class Dashboard extends Component<DashboardWithStylesProps, DashboardState> {
163164
})
164165

165166
Actions.refresh()
167+
168+
this.convertThreadAndRedirectToBot()
166169
}
167170

168171
componentWillUnmount() {
@@ -212,6 +215,24 @@ class Dashboard extends Component<DashboardWithStylesProps, DashboardState> {
212215
this.setState({ filterValue: event.target.value })
213216
}
214217

218+
convertThreadAndRedirectToBot = async () => {
219+
const cookieName = "pgai_tmp_thread_id=";
220+
const cookies = document.cookie.split(';').map(cookie => cookie.trim());
221+
const pgaiTmpThreadId = cookies.find(cookie => cookie.startsWith(cookieName))?.substring(cookieName.length) || null;
222+
223+
if (pgaiTmpThreadId) {
224+
try {
225+
const data = await convertThread(pgaiTmpThreadId);
226+
if (data?.response?.final_thread_id) {
227+
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=.${window.location.hostname.split('.').slice(-2).join('.')}`;
228+
this.props.history.push(`demo/bot/${data.response.final_thread_id}`);
229+
}
230+
} catch (error) {
231+
console.error('Error converting thread:', error);
232+
}
233+
}
234+
}
235+
215236
render() {
216237
const renderProjects = this.props.onlyProjects
217238

ui/packages/platform/src/components/IndexPage/IndexPage.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { Component } from 'react'
9-
import { Switch, Route, NavLink, Redirect } from 'react-router-dom'
9+
import { Switch, Route, NavLink, Redirect, useRouteMatch } from 'react-router-dom'
1010
import {
1111
AppBar,
1212
Toolbar,
@@ -291,6 +291,7 @@ function ProjectWrapper(parentProps: Omit<ProjectWrapperProps, 'classes'>) {
291291
}
292292

293293
function OrganizationMenu(parentProps: OrganizationMenuProps) {
294+
const isDemoOrg = useRouteMatch(`/${settings.demoOrgAlias}`)
294295
if (
295296
parentProps.env &&
296297
parentProps.env.data &&
@@ -352,7 +353,7 @@ function OrganizationMenu(parentProps: OrganizationMenuProps) {
352353
</NavLink>
353354
</ListItem>
354355

355-
{/*<ListItem
356+
{Boolean(isDemoOrg) && <ListItem
356357
button
357358
className={parentProps.classes.menuSectionHeader}
358359
disabled={isBlocked}
@@ -368,7 +369,7 @@ function OrganizationMenu(parentProps: OrganizationMenuProps) {
368369
</span>
369370
AI Bot<span className={cn(parentProps.classes.menuItemLabel, parentProps.classes.headerLinkMenuItemLabel)}>NEW</span>
370371
</NavLink>
371-
</ListItem>*/}
372+
</ListItem>}
372373
<ListItem
373374
button
374375
className={parentProps.classes.menuSectionHeader}

0 commit comments

Comments
 (0)