Skip to content

Commit 0ed58ce

Browse files
author
Bogdan Tsechoev
committed
Bot UI
1 parent 648d066 commit 0ed58ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3324
-43
lines changed

ui/packages/platform/.env_example_dev

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
REACT_APP_API_URL_PREFIX=https://v2.postgres.ai/api/general
22
REACT_APP_TOKEN_DEBUG=__YOUR_JWT_TOKEN_FROM_STAGING__
3+
REACT_APP_WS_URL=ws://127.0.0.1:9100/

ui/packages/platform/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ENV REACT_APP_STRIPE_API_KEY=$ARG_REACT_APP_STRIPE_API_KEY
4444
ARG ARG_REACT_APP_SENTRY_DSN
4545
ENV REACT_APP_SENTRY_DSN=$ARG_REACT_APP_SENTRY_DSN
4646

47+
ARG ARG_REACT_APP_WS_URL
48+
ENV REACT_APP_WS_URL=$ARG_REACT_APP_WS_URL
49+
4750
RUN apk add --no-cache --update git && \
4851
npm i -g [email protected]; \
4952
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
@@ -37,6 +37,7 @@ DOCKER_BUILDKIT=1 docker build \
3737
--build-arg ARG_REACT_APP_AUTH_URL="${REACT_APP_AUTH_URL}" \
3838
--build-arg ARG_REACT_APP_ROOT_URL="${REACT_APP_ROOT_URL}" \
3939
--build-arg ARG_REACT_APP_SENTRY_DSN="${REACT_APP_SENTRY_DSN}" \
40+
--build-arg ARG_REACT_APP_WS_URL="${REACT_APP_WS_URL}" \
4041
$tags_build --file ./ui/packages/platform/Dockerfile .
4142
set +x
4243

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ export REACT_APP_ROOT_URL="https://v2.postgres.ai"
1717
export PUBLIC_URL=""
1818

1919
# Public Stripe key, it is ok to keep it here.
20-
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
20+
export REACT_APP_STRIPE_API_KEY="xxx"
2121
export REACT_APP_SENTRY_DSN=""
22+
23+
# AI Bot
24+
# 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

ui/packages/platform/deploy/configs/dev1.imgdata.ru.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
1717
export PUBLIC_URL=""
1818

1919
# Public Stripe key, it is ok to keep it here.
20-
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
20+
export REACT_APP_STRIPE_API_KEY="xxx"
2121
export REACT_APP_SENTRY_DSN=""

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
1717
export PUBLIC_URL=""
1818

1919
# Public Stripe key, it is ok to keep it here.
20-
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
20+
export REACT_APP_STRIPE_API_KEY="xxx"
2121
export REACT_APP_SENTRY_DSN=""
22+
23+
# AI Bot
24+
export REACT_APP_WS_URL="ws://localhost:9100/" # don't forget trailing slash if GET path is used!

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
1717
export PUBLIC_URL=""
1818

1919
# Public Stripe key, it is ok to keep it here.
20-
export REACT_APP_STRIPE_API_KEY="pk_live_X5yTnxXNtoVDl4Rl5oxY6X1400fwxOS1x3"
20+
export REACT_APP_STRIPE_API_KEY="xxx"
2121

2222
# Sentry.
2323
export REACT_APP_SENTRY_DSN="https://[email protected]/2"
24+
25+
# AI Bot
26+
export REACT_APP_WS_URL="wss://postgres.ai/ai-bot-wss/" # don't forget trailing slash!

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ export REACT_APP_ROOT_URL="https://v2.postgres.ai"
1717
export PUBLIC_URL=""
1818

1919
# Public Stripe key, it is ok to keep it here.
20-
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
20+
export REACT_APP_STRIPE_API_KEY="xxx"
2121
export REACT_APP_SENTRY_DSN=""
22+
23+
# AI Bot
24+
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # don't forget trailing slash!

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

+2
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ spec:
4747
value: "$REACT_APP_EXPLAIN_DEPESZ_SERVER"
4848
- name: REACT_APP_STRIPE_API_KEY
4949
value: "$REACT_APP_STRIPE_API_KEY"
50+
- name: REACT_APP_WS_URL
51+
value: "$REACT_APP_WS_URL"

ui/packages/platform/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"react-router-hash-link": "^1.2.2",
6868
"react-scripts": "^5.0.0",
6969
"react-syntax-highlighter": "^15.5.0",
70+
"react-use-websocket": "3.0.0",
7071
"reflux": "^6.4.1",
7172
"rehype-raw": "^6.1.1",
7273
"remark-gfm": "^3.0.1",
Loading

ui/packages/platform/src/actions/actions.js

+37
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const Actions = Reflux.createActions([{
5353
getOrgUsers: ASYNC_ACTION,
5454
updateOrg: ASYNC_ACTION,
5555
createOrg: ASYNC_ACTION,
56+
updateAiBotSettings: ASYNC_ACTION,
5657
inviteUser: ASYNC_ACTION,
5758
useDemoData: ASYNC_ACTION,
5859
setReportsProject: {},
@@ -617,6 +618,42 @@ Actions.updateOrg.listen(function (token, orgId, orgData) {
617618
});
618619
});
619620

621+
Actions.updateAiBotSettings.listen(function (token, orgId, orgData) {
622+
let action = this;
623+
624+
if (!api) {
625+
settings.init(function () {
626+
api = new Api(settings);
627+
});
628+
}
629+
630+
action.progressed({ orgId } + orgData);
631+
timeoutPromise(REQUEST_TIMEOUT, api.updateAiBotSettings(token, orgId, orgData))
632+
633+
.then(result => {
634+
result.json()
635+
.then(json => {
636+
if (json) {
637+
action.completed(json);
638+
} else {
639+
action.failed(new Error('wrong_reply'));
640+
}
641+
})
642+
.catch(err => {
643+
console.error(err);
644+
action.failed(new Error('wrong_reply'));
645+
});
646+
})
647+
.catch(err => {
648+
console.error(err);
649+
if (err && err.message && err.message === 'timeout') {
650+
action.failed(new Error('failed_fetch'));
651+
} else {
652+
action.failed(new Error('wrong_reply'));
653+
}
654+
});
655+
});
656+
620657
Actions.createOrg.listen(function (token, orgData) {
621658
let action = this;
622659

ui/packages/platform/src/api/api.js

+22
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,28 @@ class Api {
451451
});
452452
}
453453

454+
updateAiBotSettings(token, orgId, orgData) {
455+
let params = {};
456+
let headers = {
457+
Authorization: 'Bearer ' + token,
458+
prefer: 'return=representation'
459+
};
460+
461+
if (typeof orgData.custom_prompt !== 'undefined') {
462+
params.custom_prompt = orgData.custom_prompt;
463+
}
464+
465+
if (typeof orgData.model !== 'undefined') {
466+
params.model = orgData.model;
467+
}
468+
469+
console.log('params', params)
470+
471+
return this.patch(`${this.apiServer}/orgs?id=eq.` + orgId, params, {
472+
headers: headers
473+
});
474+
}
475+
454476
inviteUser(token, orgId, email) {
455477
let headers = {
456478
Authorization: 'Bearer ' + token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {request} from "../../helpers/request";
2+
import {BotMessage} from "../../types/api/entities/bot";
3+
4+
type Req = {
5+
query?: string
6+
}
7+
8+
export const getChats = async (req: Req): Promise<{ response: BotMessage[] | null; error: Response | null }> => {
9+
const { query } = req;
10+
11+
const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';
12+
13+
try {
14+
const response = await request(`${apiServer}/chats${query ? query : ''}`, {
15+
method: 'GET',
16+
});
17+
18+
if (!response.ok) {
19+
return { response: null, error: response };
20+
}
21+
22+
const responseData: BotMessage[] = await response.json();
23+
24+
return { response: responseData, error: null };
25+
26+
} catch (error) {
27+
return { response: null, error: error as Response };
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {request} from "../../helpers/request";
2+
import {BotMessage} from "../../types/api/entities/bot";
3+
4+
type Req = {
5+
id: string
6+
}
7+
8+
export const getChatsWithWholeThreads = async (req: Req): Promise<{ response: BotMessage[] | null; error: Response | null }> => {
9+
const { id } = req;
10+
11+
const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';
12+
13+
try {
14+
const response = await request(`${apiServer}/rpc/chats_ancestors_and_descendants`, {
15+
method: 'POST',
16+
body: JSON.stringify({ id }),
17+
});
18+
19+
if (!response.ok) {
20+
return { response: null, error: response };
21+
}
22+
23+
const responseData: BotMessage[] = await response.json();
24+
25+
return { response: responseData, error: null };
26+
27+
} catch (error) {
28+
return { response: null, error: error as Response };
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {request} from "../../helpers/request";
2+
import {BotMessage} from "../../types/api/entities/bot";
3+
4+
type Req = {
5+
thread_id: string,
6+
is_public: boolean
7+
}
8+
9+
export const makeChatPublic = async (req: Req): Promise<{ response: BotMessage | null; error: Response | null }> => {
10+
const { thread_id, is_public } = req;
11+
12+
const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';
13+
14+
try {
15+
const response = await request(`${apiServer}/chats_internal?thread_id=eq.${thread_id}`, {
16+
method: 'PATCH',
17+
headers: {
18+
Prefer: 'return=representation'
19+
},
20+
body: JSON.stringify({
21+
is_public
22+
})
23+
});
24+
25+
if (!response.ok) {
26+
return { response: null, error: response };
27+
}
28+
29+
const responseData: BotMessage = await response.json();
30+
31+
return { response: responseData, error: null };
32+
33+
} catch (error) {
34+
return { response: null, error: error as Response };
35+
}
36+
}

0 commit comments

Comments
 (0)