Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp chatbot context to support multiple messages (cooccurrences) #377

Merged
merged 19 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/types/chatbotState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ export type ChatbotState =
| 'ASKING_ARTICLE_SUBMISSION_CONSENT'
| 'Error';

export type LegacyContext = {
data: {
/** Used to differientiate different search sessions (searched text or media) */
sessionId: number;
} & (
| {
/** Searched multi-media message that started this search session */
messageId: MessageEvent['message']['id'];
messageType: Extract<
MessageEvent['message']['type'],
'audio' | 'video' | 'image'
>;
}
| {
/** Searched text that started this search session */
searchedText: string;
}
);
};

export type Context = {
/** Used to differientiate different search sessions (searched text or media) */
sessionId: number;
} & (
| {
/** Searched multi-media message that started this search session */
messageId: MessageEvent['message']['id'];
messageType: MessageEvent['message']['type'];
}
| {
/** Searched text that started this search session */
searchedText: string;
}
);
msgs: ReadonlyArray<CooccurredMessage>;
};

/** A single messages in the same co-occurrence */
export type CooccurredMessage = {
Expand All @@ -41,8 +52,12 @@ export type CooccurredMessage = {
}
);

export type ChatbotStateHandlerReturnType = {
data: Context;
/** Result of handler or processors */
export type Result = {
/** The new context to set after processing the event */
context: Context;

/** The messages to send to the user as reply */
replies: Message[];
};

Expand All @@ -56,8 +71,8 @@ export type PostbackActionData<T> = {
};

export type ChatbotPostbackHandlerParams<T = unknown> = {
/** Data stored in Chatbot context */
data: Context;
/** Chatbot context */
context: Context;
/** Data in postback payload */
postbackData: PostbackActionData<T>;
userId: string;
Expand All @@ -68,4 +83,4 @@ export type ChatbotPostbackHandlerParams<T = unknown> = {
*/
export type ChatbotPostbackHandler<T = unknown> = (
params: ChatbotPostbackHandlerParams<T>
) => Promise<ChatbotStateHandlerReturnType>;
) => Promise<Result>;
9 changes: 0 additions & 9 deletions src/types/result.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ Array [

exports[`should submit article if user agrees to submit: has AI reply 1`] = `
Object {
"data": Object {
"searchedText": "Some text forwarded by the user",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "Some text forwarded by the user",
"type": "text",
},
],
"sessionId": 1577923200000,
},
"replies": Array [
Expand Down Expand Up @@ -325,8 +331,14 @@ Object {

exports[`should submit article if user agrees to submit: has no AI reply 1`] = `
Object {
"data": Object {
"searchedText": "Some text forwarded by the user",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "Some text forwarded by the user",
"type": "text",
},
],
"sessionId": 1577923200000,
},
"replies": Array [
Expand Down Expand Up @@ -475,10 +487,13 @@ Object {

exports[`should submit image article if user agrees to submit 1`] = `
Object {
"data": Object {
"messageId": "6530038889933",
"messageType": "image",
"searchedText": "",
"context": Object {
"msgs": Array [
Object {
"id": "6530038889933",
"type": "image",
},
],
"sessionId": 1577923200000,
},
"replies": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

exports[`should ask users if they want to submit article when user say not found 1`] = `
Object {
"data": Object {
"searchedText": "這一篇文章確實是一個轉傳文章,他夠長,看起來很轉傳,但是使用者覺得資料庫裡沒有。",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "這一篇文章確實是一個轉傳文章,他夠長,看起來很轉傳,但是使用者覺得資料庫裡沒有。",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -85,9 +91,13 @@ May I ask you a quick question?",

exports[`should ask users if they want to submit image article when user say not found 1`] = `
Object {
"data": Object {
"messageId": "6530038889933",
"messageType": "image",
"context": Object {
"msgs": Array [
Object {
"id": "6530038889933",
"type": "image",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -198,8 +208,14 @@ Array [

exports[`should select article and choose the only one reply for user 1`] = `
Object {
"data": Object {
"searchedText": "Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -325,8 +341,14 @@ https://dev.cofacts.tw/article/article-id",

exports[`should select article and have OPINIONATED and NOT_ARTICLE replies 1`] = `
Object {
"data": Object {
"searchedText": "老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人",
"type": "text",
},
],
"sessionId": 1497994017447,
},
"replies": Array [
Expand Down Expand Up @@ -671,8 +693,14 @@ Object {

exports[`should select article and slice replies when over 10 1`] = `
Object {
"data": Object {
"searchedText": "老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -1490,13 +1518,19 @@ Object {

exports[`should select article by articleId 1`] = `
Object {
"data": Object {
"searchedText": "《緊急通知》
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "《緊急通知》
台北馬偕醫院傳來訊息:
資深醫生(林清風)傳來:「請大家以後千萬不要再吃生魚片了!」
因為最近已經發現- 好多病人因為吃了生魚片,胃壁附著《海獸胃腺蟲》,大小隻不一定,有的病人甚至胃壁上滿滿都是無法夾出來,驅蟲藥也很難根治,罹患機率每個國家的人都一樣。
尤其;鮭魚的含蟲量最高、最可怕!
請傳給朋友,讓他們有所警惕!",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -1683,8 +1717,14 @@ Object {

exports[`should select article with no replies: has AI reply 1`] = `
Object {
"data": Object {
"searchedText": "老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -1862,8 +1902,14 @@ Don’t trust the message just yet!",

exports[`should select article with no replies: has no AI reply 1`] = `
Object {
"data": Object {
"searchedText": "老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,14 @@ Array [

exports[`should select reply by replyId should handle the case with just one reply 1`] = `
Object {
"data": Object {
"searchedText": "貼圖",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "貼圖",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down Expand Up @@ -370,8 +376,14 @@ https://dev.cofacts.tw/article/AWDZYXxAyCdS-nWhumlz",

exports[`should select reply by replyId should handle the case with multiple replies 1`] = `
Object {
"data": Object {
"searchedText": "貼圖",
"context": Object {
"msgs": Array [
Object {
"id": "foo",
"text": "貼圖",
"type": "text",
},
],
"sessionId": 0,
},
"replies": Array [
Expand Down
Loading
Loading