Skip to content

Commit

Permalink
[CAI-259] Add history to queries (#1246)
Browse files Browse the repository at this point in the history
* Add history to queries

* Add changeset

* add chatMaxHistoryMessages

* add env variable

* Update .changeset/hungry-eels-stare.md

Co-authored-by: marcobottaro <[email protected]>

---------

Co-authored-by: marcobottaro <[email protected]>
  • Loading branch information
MarcoPonchia and marcobottaro authored Nov 18, 2024
1 parent 8a810ce commit fe61351
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-eels-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": minor
---

Add history to send query endpoint
1 change: 1 addition & 0 deletions .github/actions/build-nextjs-website/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ runs:
NEXT_PUBLIC_COGNITO_USER_POOL_ID: ${{ inputs.cognito_user_pool_id }}
NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID: ${{ inputs.cognito_identity_pool_id }}
NEXT_PUBLIC_CHATBOT_HOST: ${{ inputs.chatbot_host }}
NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES: ${{ inputs.chat_max_history_messages }}
NEXT_PUBLIC_WEBSITE_NAME: ${{ inputs.website_name }}
NEXT_PUBLIC_ORGANIZATION_NAME: ${{ inputs.organization_name }}
NEXT_PUBLIC_ORGANIZATION_LOGO: ${{ inputs.organization_logo }}
Expand Down
1 change: 1 addition & 0 deletions .github/actions/deploy/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ runs:
NEXT_PUBLIC_COGNITO_USER_POOL_ID: ${{ inputs.cognito_user_pool_id }}
NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID: ${{ inputs.cognito_identity_pool_id }}
NEXT_PUBLIC_CHATBOT_HOST: ${{ inputs.chatbot_host }}
NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES: ${{ inputs.chat_max_history_messages }}
NEXT_PUBLIC_WEBSITE_NAME: ${{ inputs.website_name }}
NEXT_PUBLIC_ORGANIZATION_NAME: ${{ inputs.organization_name }}
NEXT_PUBLIC_ORGANIZATION_LOGO: ${{ inputs.organization_logo }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/code_review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ jobs:
cognito_identity_pool_id: ${{ secrets.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID }}
cognito_app_client_id: ${{ secrets.NEXT_PUBLIC_COGNITO_USER_POOL_WEB_CLIENT_ID }}
chatbot_host: ${{ vars.NEXT_PUBLIC_CHATBOT_HOST }}
chat_max_history_messages: ${{ vars.NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES }}
website_name: ${{ vars.NEXT_PUBLIC_WEBSITE_NAME }}
organization_name: ${{ vars.NEXT_PUBLIC_ORGANIZATION_NAME }}
organization_logo: ${{ vars.NEXT_PUBLIC_ORGANIZATION_LOGO }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy_website.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
cognito_identity_pool_id: ${{ secrets.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID }}
cognito_app_client_id: ${{ secrets.NEXT_PUBLIC_COGNITO_USER_POOL_WEB_CLIENT_ID }}
chatbot_host: ${{ vars.NEXT_PUBLIC_CHATBOT_HOST }}
chat_max_history_messages: ${{ vars.NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES }}
website_name: ${{ vars.NEXT_PUBLIC_WEBSITE_NAME }}
organization_name: ${{ vars.NEXT_PUBLIC_ORGANIZATION_NAME }}
organization_logo: ${{ vars.NEXT_PUBLIC_ORGANIZATION_LOGO }}
Expand Down Expand Up @@ -106,6 +107,7 @@ jobs:
cognito_identity_pool_id: ${{ secrets.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID }}
cognito_app_client_id: ${{ secrets.NEXT_PUBLIC_COGNITO_USER_POOL_WEB_CLIENT_ID }}
chatbot_host: ${{ vars.NEXT_PUBLIC_CHATBOT_HOST }}
chat_max_history_messages: ${{ vars.NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES }}
website_name: ${{ vars.NEXT_PUBLIC_WEBSITE_NAME }}
organization_name: ${{ vars.NEXT_PUBLIC_ORGANIZATION_NAME }}
organization_logo: ${{ vars.NEXT_PUBLIC_ORGANIZATION_LOGO }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy_website_content.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
cognito_identity_pool_id: ${{ secrets.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID }}
cognito_app_client_id: ${{ secrets.NEXT_PUBLIC_COGNITO_USER_POOL_WEB_CLIENT_ID }}
chatbot_host: ${{ vars.NEXT_PUBLIC_CHATBOT_HOST }}
chat_max_history_messages: ${{ vars.NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES }}
website_name: ${{ vars.NEXT_PUBLIC_WEBSITE_NAME }}
organization_name: ${{ vars.NEXT_PUBLIC_ORGANIZATION_NAME }}
organization_logo: ${{ vars.NEXT_PUBLIC_ORGANIZATION_LOGO }}
Expand Down
1 change: 1 addition & 0 deletions apps/nextjs-website/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NEXT_PUBLIC_CHATBOT_HOST=""
NEXT_PUBLIC_CHATBOT_ACTIVE=""
NEXT_PUBLIC_COOKIE_DOMAIN_SCRIPT="a8c87faf-1769-4c95-a2e5-a6fff26eadab-test"
NEXT_PUBLIC_CHATBOT_HOST="http://localhost:8000"
NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES=10
NEXT_PUBLIC_CHATBOT_ACTIVE="false"
STRAPI_ENDPOINT="http://127.0.0.1:1337"
STRAPI_API_TOKEN="aStrapiApiToken"
Expand Down
2 changes: 2 additions & 0 deletions apps/nextjs-website/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const allowCrawler = process.env.ALLOW_CRAWLER === 'true';
export const isProduction = process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod';
export const isChatbotActive =
process.env.NEXT_PUBLIC_CHATBOT_ACTIVE === 'true';
export const chatMaxHistoryMessages =
parseInt(`${process.env.NEXT_PUBLIC_CHAT_MAX_HISTORY_MESSAGES}`) || 10;

export const amplifyConfig = {
Auth: {
Expand Down
2 changes: 2 additions & 0 deletions apps/nextjs-website/src/helpers/chatbot.helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@/lib/chatbot/queries';
import { pipe } from 'fp-ts/lib/function';
import * as E from 'fp-ts/lib/Either';
import { chatMaxHistoryMessages } from '@/config';

const HISTORY_PAGE_SIZE = 10;
const EXPIRE_CHAT_DATE_LOCAL_STORAGE_KEY = 'expireChatDate';
Expand Down Expand Up @@ -133,6 +134,7 @@ export const useChatbot = (isUserAuthenticated: boolean) => {
sendChatbotQuery({
question: queryMessage,
queriedAt: queriedAt,
history: previousQueries.slice(-chatMaxHistoryMessages),
})
.then((response) => {
setIsAwaitingResponse(false);
Expand Down
12 changes: 12 additions & 0 deletions apps/nextjs-website/src/lib/chatbot/__tests__/chatbotApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const postQueryResponses = {
queriedAt: '2024-02-08T11:12:02.142Z',
question: 'question',
answer: 'answer',
history: [
{
id: '1',
question: 'question',
answer: 'answer',
},
{
id: '2',
question: 'question',
answer: 'answer',
},
],
createdAt: '2024-02-08T11:12:02.438Z',
badAnswer: false,
},
Expand Down
21 changes: 19 additions & 2 deletions apps/nextjs-website/src/lib/chatbot/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from 'io-ts/lib';
import { NullToUndefinedCodec } from '../strapi/codecs/NullToUndefinedCodec';

export const QueryCodec = t.strict({
id: t.string,
Expand All @@ -10,14 +11,21 @@ export const QueryCodec = t.strict({
createdAt: t.string,
});

const ChatbotHistoryCodec = t.array(QueryCodec);

export const QueryWithHistoryCodec = t.intersection([
QueryCodec,
t.strict({
history: t.union([NullToUndefinedCodec, ChatbotHistoryCodec]),
}),
]);

export type RemoteQuery = t.TypeOf<typeof QueryCodec>;

export const ChatbotQueriesCodec = t.array(QueryCodec);

export type ChatbotQueries = t.TypeOf<typeof ChatbotQueriesCodec>;

export type QueryInput = Pick<RemoteQuery, 'question' | 'queriedAt'>;

export type Answer = Pick<RemoteQuery, 'answer' | 'createdAt'>;

export type Query = {
Expand All @@ -30,6 +38,15 @@ export type Query = {
readonly createdAt: string | null;
};

export type QueryWithHistory = Query & {
readonly history?: readonly Pick<Query, 'question' | 'id' | 'answer'>[];
};

export type QueryInput = Pick<
QueryWithHistory,
'question' | 'queriedAt' | 'history'
>;

export const SessionCodec = t.strict({
id: t.string,
title: t.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ describe('UrlReplaceMapCodec', () => {
const result = pipe(
validation,
Either.fold(
(left) => {
console.error('Error:', left);
() => {
return null; // or handle the error as needed
},
(right) => {
console.log('Success:', right);
return right;
}
)
Expand Down

0 comments on commit fe61351

Please sign in to comment.