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

[Feature Request] Close open tags while streaming response #38

Open
BigBerny opened this issue May 14, 2023 · 5 comments
Open

[Feature Request] Close open tags while streaming response #38

BigBerny opened this issue May 14, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@BigBerny
Copy link

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 😊

@seratch seratch added the enhancement New feature or request label May 14, 2023
@seratch
Copy link
Owner

seratch commented May 14, 2023

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.

@BigBerny
Copy link
Author

BigBerny commented May 14, 2023

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.

@BigBerny
Copy link
Author

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

@BigBerny
Copy link
Author

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

@seratch
Copy link
Owner

seratch commented May 14, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants