From 65b3591739f160b0679c52cdf0b0702a248b498d Mon Sep 17 00:00:00 2001 From: Maxime GUERIN - Netsyst Date: Tue, 19 Mar 2024 10:53:18 +0100 Subject: [PATCH 1/2] added TYPE_IN_MAIN env variable to add the typing effect in the main thread --- src/botservice.ts | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/botservice.ts b/src/botservice.ts index 9054503..f7dbfc4 100644 --- a/src/botservice.ts +++ b/src/botservice.ts @@ -1,23 +1,23 @@ -import {continueThread, registerChatPlugin} from "./openai-wrapper"; -import {mmClient, wsClient} from "./mm-client"; +import { continueThread, registerChatPlugin } from "./openai-wrapper"; +import { mmClient, wsClient } from "./mm-client"; import 'babel-polyfill' import 'isomorphic-fetch' -import {WebSocketMessage} from "@mattermost/client"; -import {ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum} from "openai"; -import {GraphPlugin} from "./plugins/GraphPlugin"; -import {ImagePlugin} from "./plugins/ImagePlugin"; -import {Post} from "@mattermost/types/lib/posts"; -import {PluginBase} from "./plugins/PluginBase"; -import {JSONMessageData, MessageData} from "./types"; -import {ExitPlugin} from "./plugins/ExitPlugin"; -import {MessageCollectPlugin} from "./plugins/MessageCollectPlugin"; - -import {botLog, matterMostLog} from "./logging"; +import { WebSocketMessage } from "@mattermost/client"; +import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from "openai"; +import { GraphPlugin } from "./plugins/GraphPlugin"; +import { ImagePlugin } from "./plugins/ImagePlugin"; +import { Post } from "@mattermost/types/lib/posts"; +import { PluginBase } from "./plugins/PluginBase"; +import { JSONMessageData, MessageData } from "./types"; +import { ExitPlugin } from "./plugins/ExitPlugin"; +import { MessageCollectPlugin } from "./plugins/MessageCollectPlugin"; + +import { botLog, matterMostLog } from "./logging"; if (!global.FormData) { global.FormData = require('form-data') } - +const typingInMainThread = process.env['TYPE_IN_MAIN'] == 'true' || false const name = process.env['MATTERMOST_BOTNAME'] || '@chatgpt' const contextMsgCount = Number(process.env['BOT_CONTEXT_MSG'] ?? 100) const additionalBotInstructions = process.env['BOT_INSTRUCTION'] || "You are a helpful assistant. Whenever users asks you for help you will " + @@ -34,16 +34,16 @@ const plugins: PluginBase[] = [ /* The main system instruction for GPT */ const botInstructions = "Your name is " + name + ". " + additionalBotInstructions -botLog.debug({botInstructions: botInstructions}) +botLog.debug({ botInstructions: botInstructions }) async function onClientMessage(msg: WebSocketMessage, meId: string) { if (msg.event !== 'posted' || !meId) { - matterMostLog.debug({msg: msg}) + matterMostLog.debug({ msg: msg }) return } const msgData = parseMessageData(msg.data) - const posts = await getOlderPosts(msgData.post, {lookBackTime: 1000 * 60 * 60 * 24}) + const posts = await getOlderPosts(msgData.post, { lookBackTime: 1000 * 60 * 60 * 24 }) if (isMessageIgnored(msgData, meId, posts)) { return @@ -58,7 +58,7 @@ async function onClientMessage(msg: WebSocketMessage, meId: str // create the context for (const threadPost of posts.slice(-contextMsgCount)) { - matterMostLog.trace({msg: threadPost}) + matterMostLog.trace({ msg: threadPost }) if (threadPost.user_id === meId) { chatmessages.push({ role: ChatCompletionRequestMessageRoleEnum.Assistant, @@ -78,9 +78,18 @@ async function onClientMessage(msg: WebSocketMessage, meId: str typing() const typingInterval = setInterval(typing, 2000) + // start typing in global channel + const typingInMain = () => wsClient.userTyping(msgData.post.channel_id, "") + var typingIntervalInMain = setInterval(typingInMain, 2000) + if (typingInMainThread) { + typingInMain() + } else { + clearInterval(typingIntervalInMain) + } + try { - const {message, fileId, props} = await continueThread(chatmessages, msgData) - botLog.trace({message}) + const { message, fileId, props } = await continueThread(chatmessages, msgData) + botLog.trace({ message }) // create answer response const newPost = await mmClient.createPost({ @@ -90,7 +99,7 @@ async function onClientMessage(msg: WebSocketMessage, meId: str root_id: msgData.post.root_id || msgData.post.id, file_ids: fileId ? [fileId] : undefined }) - botLog.trace({msg: newPost}) + botLog.trace({ msg: newPost }) } catch (e) { botLog.error(e) await mmClient.createPost({ @@ -101,6 +110,9 @@ async function onClientMessage(msg: WebSocketMessage, meId: str } finally { // stop typing clearInterval(typingInterval) + if (typingInMainThread) { + clearInterval(typingIntervalInMain) + } } } From 45c5710c633c191ec4c8786a10cb8d6e9d78a90f Mon Sep 17 00:00:00 2001 From: Maxime GUERIN - Netsyst Date: Tue, 19 Mar 2024 10:58:40 +0100 Subject: [PATCH 2/2] change env name --- src/botservice.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/botservice.ts b/src/botservice.ts index f7dbfc4..4fed4f1 100644 --- a/src/botservice.ts +++ b/src/botservice.ts @@ -17,7 +17,7 @@ import { botLog, matterMostLog } from "./logging"; if (!global.FormData) { global.FormData = require('form-data') } -const typingInMainThread = process.env['TYPE_IN_MAIN'] == 'true' || false +const typingInMainThread = process.env['MATTERMOST_TYPE_IN_MAIN_THREAD'] == 'true' || false const name = process.env['MATTERMOST_BOTNAME'] || '@chatgpt' const contextMsgCount = Number(process.env['BOT_CONTEXT_MSG'] ?? 100) const additionalBotInstructions = process.env['BOT_INSTRUCTION'] || "You are a helpful assistant. Whenever users asks you for help you will " +