DEV: chat streaming (#25736)

This commit introduces the possibility to stream messages. To allow plugins to use streaming this commit also ships a `ChatSDK` library to allow to interact with few parts of discourse chat.

```ruby
ChatSDK::Message.create_with_stream(raw: "test") do |helper|
  5.times do |i|
    is_streaming = helper.stream(raw: "more #{i}")
    next if !is_streaming
    sleep 2
  end
end
```

This commit also introduces all the frontend parts:
- messages can now be marked as streaming
- when streaming their content will be updated when a new content is appended
- a special UI will be showing (a blinking indicator)
- a cancel button allows the user to stop the streaming, when cancelled `helper.stream(...)` will return `false`, and the plugin can decide exit early
This commit is contained in:
Joffrey JAFFEUX
2024-02-20 09:49:19 +01:00
committed by GitHub
parent b057f1b2b4
commit d8d756cd2f
30 changed files with 815 additions and 24 deletions

View File

@ -0,0 +1,43 @@
# frozen_string_literal: true
require "rails_helper"
describe ChatSDK::Channel do
describe ".messages" do
fab!(:channel_1) { Fabricate(:chat_channel) }
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
fab!(:message_2) { Fabricate(:chat_message, chat_channel: channel_1) }
let(:params) { { channel_id: channel_1.id, guardian: Discourse.system_user.guardian } }
it "loads the messages" do
messages = described_class.messages(**params)
expect(messages).to eq([message_1, message_2])
end
it "accepts page_size" do
messages = described_class.messages(**params, page_size: 1)
expect(messages).to eq([message_1])
end
context "when guardian can't see the channel" do
fab!(:channel_1) { Fabricate(:private_category_channel) }
it "fails" do
params[:guardian] = Fabricate(:user).guardian
expect { described_class.messages(**params) }.to raise_error("Guardian can't view channel")
end
end
context "when target_message doesn’t exist" do
it "fails" do
expect { described_class.messages(**params, target_message_id: -999) }.to raise_error(
"Target message doesn't exist",
)
end
end
end
end