seratch/ChatGPT-in-Slack

[Feature Request] Close open tags while streaming response

BigBerny opened this issue ยท 5 comments

First, awesome app that you built! ๐Ÿ‘

I've one feature request only so far: While the response is returned (message is updated) code blocks don't look nice because they are only opened but not closed. It would be awesome if code blocks (and potentially other formatting) already look good while they are not fully complete. Do you think you can add this? Happy to donate something ๐Ÿ˜Š

Hi @BigBerny, thanks for your message!

Could you clarify whether you're suggesting that we wait to update the message until a code block is complete? While I understand the concern, I'm not entirely convinced that this change is necessary. As long as the message eventually displays complete code blocks, it seems like everything will be okay. So, I'm hesitant to make the code more complex for this purpose.

Hi @seratch
No, not wait. But to close ``` so the formatting already looks right. So instead of adding "... โœ๏ธ" it would add "```... โœ๏ธ" at the end of the message if there's an open code block. Actually, this is also the case in ChatGPT's UI, you'll already see the code block, even if it's not yet fully predicted. Especially when waiting for huge code blocks this would make reading much easier while it's still completing the code block. At the moment you almost have to wait for the code block to be completed to actually read it.
I hope it's clear, otherwise I can also create a screen recording.

def close_open_tags(slack_msg):
    # Slack formatting tags
    tags = {
        '*': 'bold',
        '_': 'italic',
        '~': 'strikethrough',
        '```': 'codeblock'
    }

    # Stack to keep track of open tags
    stack = []

    # Splitting the message into parts by backticks
    parts = slack_msg.split('`')

    # Variable to keep track of whether we are inside a code block
    inside_code_block = False

    # Iterate over each part in the parts list
    for part in parts:
        # If the part is empty, it means we encountered backticks
        if part == '':
            # If we are inside a code block, we just ended it
            if inside_code_block:
                inside_code_block = False
                if stack and stack[-1] == '```':
                    stack.pop()
            # If we are not inside a code block, we just started it
            else:
                inside_code_block = True
                stack.append('```')
        # If the part is not empty, it means we encountered regular text
        else:
            # If we are not inside a code block, check for other tags
            if not inside_code_block:
                for char in part:
                    if char in tags:
                        if stack and stack[-1] == char:
                            stack.pop()
                        else:
                            stack.append(char)

    # For any remaining open tags, append the corresponding closing tags
    for tag in reversed(stack):
        slack_msg += tag

    return slack_msg

This close_open_tags could be run while the answer is not complete.

Thanks for clarifying this. Probably, I got the point but I am still not so convinced to have such a change in this repo (this issue does not sound critical to me). I may change my mind in the future but for now please consider forking this repo for your use case.