langgenius / webapp-conversation

MIT License
388 stars 710 forks source link

Cannot remove workflow and node information to be sent to the frontend. #80

Closed racerxdl closed 1 month ago

racerxdl commented 1 month ago

Currently there is no option to eat the node/workflow events from getting to the frontend.

racerxdl commented 1 month ago

@iamjoel that does not actually fix it, since events are still sent to the frontend with SSE. The events should not be sent, as it can leak some private data, prompts, overview of the flow.

The way I solved locally was to edit route.ts and instead of:

return new Response(res.data as any)

do

  const reader = res.data;
  const decoder = new TextDecoder('utf-8')
  let buffer = ''

  const out = new ReadableStream({
    start(controller) {
      reader.on('data', (data: any) => {
        buffer += decoder.decode(data, { stream: true });
        const lines = buffer.split('\n')
        for (const message of lines) {
          if (lines.indexOf(message) == lines.length - 1) // Skip last, since it can be broken
            break;

          try {
            let send = true;
            if (message.startsWith('data: ')) {
              const pack = JSON.parse(message.substring(6));
              if (pack.event.startsWith('node_') || pack.event.startsWith('workflow_')) {
                send = false;
                //console.log(`Skipping send ${message}`)
              }
            } else if (!message.startsWith('event: '))
              send = false;

            if (message.trim().length && send) {
              controller.enqueue("\n")
              controller.enqueue(message + "\n");
            }
            buffer = buffer.substring(message.length); // Move buffer
          } catch (e) {
            break;
          }
        }
      });
      reader.on('end', () => {
        controller.close();
      })
    },
  });

  return new Response(out as any)

This filters out, but seens to eat some tokens randomly (and I'm not sure why)