Skip to content

Commit

Permalink
Merge branch 'bot_ui' into 'master'
Browse files Browse the repository at this point in the history
Bot UI

See merge request postgres-ai/database-lab!856
  • Loading branch information
Bogdan Tsechoev committed May 15, 2024
2 parents 648d066 + 0ed58ce commit 5773279
Show file tree
Hide file tree
Showing 49 changed files with 3,324 additions and 43 deletions.
1 change: 1 addition & 0 deletions ui/packages/platform/.env_example_dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
REACT_APP_API_URL_PREFIX=https://v2.postgres.ai/api/general
REACT_APP_TOKEN_DEBUG=__YOUR_JWT_TOKEN_FROM_STAGING__
REACT_APP_WS_URL=ws://127.0.0.1:9100/
3 changes: 3 additions & 0 deletions ui/packages/platform/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ ENV REACT_APP_STRIPE_API_KEY=$ARG_REACT_APP_STRIPE_API_KEY
ARG ARG_REACT_APP_SENTRY_DSN
ENV REACT_APP_SENTRY_DSN=$ARG_REACT_APP_SENTRY_DSN

ARG ARG_REACT_APP_WS_URL
ENV REACT_APP_WS_URL=$ARG_REACT_APP_WS_URL

RUN apk add --no-cache --update git && \
npm i -g [email protected]; \
pnpm config set store-dir /app/.pnpm-store; \
Expand Down
1 change: 1 addition & 0 deletions ui/packages/platform/ci_docker_build_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ DOCKER_BUILDKIT=1 docker build \
--build-arg ARG_REACT_APP_AUTH_URL="${REACT_APP_AUTH_URL}" \
--build-arg ARG_REACT_APP_ROOT_URL="${REACT_APP_ROOT_URL}" \
--build-arg ARG_REACT_APP_SENTRY_DSN="${REACT_APP_SENTRY_DSN}" \
--build-arg ARG_REACT_APP_WS_URL="${REACT_APP_WS_URL}" \
$tags_build --file ./ui/packages/platform/Dockerfile .
set +x

Expand Down
6 changes: 5 additions & 1 deletion ui/packages/platform/deploy/configs/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ export REACT_APP_ROOT_URL="https://v2.postgres.ai"
export PUBLIC_URL=""

# Public Stripe key, it is ok to keep it here.
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
export REACT_APP_STRIPE_API_KEY="xxx"
export REACT_APP_SENTRY_DSN=""

# AI Bot
# don't forget trailing slash if GET path is used!
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # we use staging for now, since dev env is not fullfledged yet
2 changes: 1 addition & 1 deletion ui/packages/platform/deploy/configs/dev1.imgdata.ru.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
export PUBLIC_URL=""

# Public Stripe key, it is ok to keep it here.
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
export REACT_APP_STRIPE_API_KEY="xxx"
export REACT_APP_SENTRY_DSN=""
5 changes: 4 additions & 1 deletion ui/packages/platform/deploy/configs/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
export PUBLIC_URL=""

# Public Stripe key, it is ok to keep it here.
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
export REACT_APP_STRIPE_API_KEY="xxx"
export REACT_APP_SENTRY_DSN=""

# AI Bot
export REACT_APP_WS_URL="ws://localhost:9100/" # don't forget trailing slash if GET path is used!
5 changes: 4 additions & 1 deletion ui/packages/platform/deploy/configs/production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export REACT_APP_ROOT_URL="https://postgres.ai"
export PUBLIC_URL=""

# Public Stripe key, it is ok to keep it here.
export REACT_APP_STRIPE_API_KEY="pk_live_X5yTnxXNtoVDl4Rl5oxY6X1400fwxOS1x3"
export REACT_APP_STRIPE_API_KEY="xxx"

# Sentry.
export REACT_APP_SENTRY_DSN="https://[email protected]/2"

# AI Bot
export REACT_APP_WS_URL="wss://postgres.ai/ai-bot-wss/" # don't forget trailing slash!
5 changes: 4 additions & 1 deletion ui/packages/platform/deploy/configs/staging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ export REACT_APP_ROOT_URL="https://v2.postgres.ai"
export PUBLIC_URL=""

# Public Stripe key, it is ok to keep it here.
export REACT_APP_STRIPE_API_KEY="pk_test_f8joWMK94CaOPVnc0QzqEGOX00PwnHa3BG"
export REACT_APP_STRIPE_API_KEY="xxx"
export REACT_APP_SENTRY_DSN=""

# AI Bot
export REACT_APP_WS_URL="wss://v2.postgres.ai/ai-bot-ws/" # don't forget trailing slash!
2 changes: 2 additions & 0 deletions ui/packages/platform/deploy/platform-console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ spec:
value: "$REACT_APP_EXPLAIN_DEPESZ_SERVER"
- name: REACT_APP_STRIPE_API_KEY
value: "$REACT_APP_STRIPE_API_KEY"
- name: REACT_APP_WS_URL
value: "$REACT_APP_WS_URL"
1 change: 1 addition & 0 deletions ui/packages/platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"react-router-hash-link": "^1.2.2",
"react-scripts": "^5.0.0",
"react-syntax-highlighter": "^15.5.0",
"react-use-websocket": "3.0.0",
"reflux": "^6.4.1",
"rehype-raw": "^6.1.1",
"remark-gfm": "^3.0.1",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions ui/packages/platform/src/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const Actions = Reflux.createActions([{
getOrgUsers: ASYNC_ACTION,
updateOrg: ASYNC_ACTION,
createOrg: ASYNC_ACTION,
updateAiBotSettings: ASYNC_ACTION,
inviteUser: ASYNC_ACTION,
useDemoData: ASYNC_ACTION,
setReportsProject: {},
Expand Down Expand Up @@ -617,6 +618,42 @@ Actions.updateOrg.listen(function (token, orgId, orgData) {
});
});

Actions.updateAiBotSettings.listen(function (token, orgId, orgData) {
let action = this;

if (!api) {
settings.init(function () {
api = new Api(settings);
});
}

action.progressed({ orgId } + orgData);
timeoutPromise(REQUEST_TIMEOUT, api.updateAiBotSettings(token, orgId, orgData))

.then(result => {
result.json()
.then(json => {
if (json) {
action.completed(json);
} else {
action.failed(new Error('wrong_reply'));
}
})
.catch(err => {
console.error(err);
action.failed(new Error('wrong_reply'));
});
})
.catch(err => {
console.error(err);
if (err && err.message && err.message === 'timeout') {
action.failed(new Error('failed_fetch'));
} else {
action.failed(new Error('wrong_reply'));
}
});
});

Actions.createOrg.listen(function (token, orgData) {
let action = this;

Expand Down
22 changes: 22 additions & 0 deletions ui/packages/platform/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,28 @@ class Api {
});
}

updateAiBotSettings(token, orgId, orgData) {
let params = {};
let headers = {
Authorization: 'Bearer ' + token,
prefer: 'return=representation'
};

if (typeof orgData.custom_prompt !== 'undefined') {
params.custom_prompt = orgData.custom_prompt;
}

if (typeof orgData.model !== 'undefined') {
params.model = orgData.model;
}

console.log('params', params)

return this.patch(`${this.apiServer}/orgs?id=eq.` + orgId, params, {
headers: headers
});
}

inviteUser(token, orgId, email) {
let headers = {
Authorization: 'Bearer ' + token
Expand Down
29 changes: 29 additions & 0 deletions ui/packages/platform/src/api/bot/getChats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {request} from "../../helpers/request";
import {BotMessage} from "../../types/api/entities/bot";

type Req = {
query?: string
}

export const getChats = async (req: Req): Promise<{ response: BotMessage[] | null; error: Response | null }> => {
const { query } = req;

const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';

try {
const response = await request(`${apiServer}/chats${query ? query : ''}`, {
method: 'GET',
});

if (!response.ok) {
return { response: null, error: response };
}

const responseData: BotMessage[] = await response.json();

return { response: responseData, error: null };

} catch (error) {
return { response: null, error: error as Response };
}
}
30 changes: 30 additions & 0 deletions ui/packages/platform/src/api/bot/getChatsWithWholeThreads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {request} from "../../helpers/request";
import {BotMessage} from "../../types/api/entities/bot";

type Req = {
id: string
}

export const getChatsWithWholeThreads = async (req: Req): Promise<{ response: BotMessage[] | null; error: Response | null }> => {
const { id } = req;

const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';

try {
const response = await request(`${apiServer}/rpc/chats_ancestors_and_descendants`, {
method: 'POST',
body: JSON.stringify({ id }),
});

if (!response.ok) {
return { response: null, error: response };
}

const responseData: BotMessage[] = await response.json();

return { response: responseData, error: null };

} catch (error) {
return { response: null, error: error as Response };
}
}
36 changes: 36 additions & 0 deletions ui/packages/platform/src/api/bot/makeChatPublic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {request} from "../../helpers/request";
import {BotMessage} from "../../types/api/entities/bot";

type Req = {
thread_id: string,
is_public: boolean
}

export const makeChatPublic = async (req: Req): Promise<{ response: BotMessage | null; error: Response | null }> => {
const { thread_id, is_public } = req;

const apiServer = process.env.REACT_APP_API_URL_PREFIX || '';

try {
const response = await request(`${apiServer}/chats_internal?thread_id=eq.${thread_id}`, {
method: 'PATCH',
headers: {
Prefer: 'return=representation'
},
body: JSON.stringify({
is_public
})
});

if (!response.ok) {
return { response: null, error: response };
}

const responseData: BotMessage = await response.json();

return { response: responseData, error: null };

} catch (error) {
return { response: null, error: error as Response };
}
}
Loading

0 comments on commit 5773279

Please sign in to comment.