-
Notifications
You must be signed in to change notification settings - Fork 228
feat: chat stream helper #1545
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
Open
srtaalej
wants to merge
14
commits into
main
Choose a base branch
from
ale-chat-stream-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: chat stream helper #1545
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b32772b
feat: chat stream helper
srtaalej a4f2b06
Update slack-api-client/src/main/java/com/slack/api/methods/ChatStrea…
srtaalej b81ae19
build(deps): bump actions/checkout from 6.0.0 to 6.0.1 (#1549)
dependabot[bot] b540c95
build(deps): bump actions/setup-java from 5.0.0 to 5.1.0 (#1547)
dependabot[bot] 90b560a
build(deps): bump codecov/codecov-action from 5.5.1 to 5.5.2 (#1548)
dependabot[bot] 32a9a7f
build(deps): bump actions/stale from 10.1.0 to 10.1.1 (#1546)
dependabot[bot] a919689
feat: wrap helper in request config
srtaalej dc13df5
adjust buffer size
srtaalej beb1b92
fix: set test channel/ team to null
srtaalej 443d380
Update slack-api-client/src/main/java/com/slack/api/methods/ChatStrea…
srtaalej 18da58c
Update slack-api-client/src/main/java/com/slack/api/methods/AsyncMeth…
srtaalej 1d9a51d
Update slack-api-client/src/main/java/com/slack/api/methods/MethodsCl…
srtaalej 873abee
fix: match name to py/node sdk helpers
srtaalej e9f3639
bug: privatize StreamState
srtaalej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
slack-api-client/src/main/java/com/slack/api/methods/AsyncChatStreamHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package com.slack.api.methods; | ||
|
|
||
| import com.slack.api.methods.request.chat.ChatAppendStreamRequest; | ||
| import com.slack.api.methods.request.chat.ChatStartStreamRequest; | ||
| import com.slack.api.methods.request.chat.ChatStopStreamRequest; | ||
| import com.slack.api.methods.response.chat.ChatAppendStreamResponse; | ||
| import com.slack.api.methods.response.chat.ChatStartStreamResponse; | ||
| import com.slack.api.methods.response.chat.ChatStopStreamResponse; | ||
| import com.slack.api.model.Message; | ||
| import com.slack.api.model.block.LayoutBlock; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * Async variant of {@link ChatStreamHelper} for {@link AsyncMethodsClient}. | ||
| * <p> | ||
| * This helper buffers markdown text and flushes via chat.startStream / chat.appendStream, then finalizes via | ||
| * chat.stopStream. | ||
| * <p> | ||
| * | ||
| */ | ||
| @Data | ||
| @Slf4j | ||
| @Builder | ||
| public class AsyncChatStreamHelper { | ||
|
|
||
| public enum State { | ||
| STARTING, | ||
| IN_PROGRESS, | ||
| COMPLETED | ||
| } | ||
|
|
||
| private final AsyncMethodsClient client; | ||
| private final String channel; | ||
| private final String threadTs; | ||
| private final String recipientTeamId; | ||
| private final String recipientUserId; | ||
|
|
||
| @Builder.Default | ||
| private final int bufferSize = 256; | ||
|
|
||
| @Builder.Default | ||
| private StringBuilder buffer = new StringBuilder(); | ||
| @Builder.Default | ||
| private State state = State.STARTING; | ||
| private String streamTs; | ||
|
|
||
| /** | ||
| * Append text to the stream. | ||
| * | ||
| * @param markdownText markdown text to append | ||
| * @return a future that completes with a response if the buffer was flushed; completes with null if buffering | ||
| */ | ||
| public CompletableFuture<ChatAppendStreamResponse> append(String markdownText) { | ||
| if (state == State.COMPLETED) { | ||
| CompletableFuture<ChatAppendStreamResponse> f = new CompletableFuture<>(); | ||
| f.completeExceptionally(new SlackChatStreamException("Cannot append to stream: stream state is " + state)); | ||
| return f; | ||
| } | ||
|
|
||
| buffer.append(markdownText); | ||
|
|
||
| if (buffer.length() >= bufferSize) { | ||
| return flushBuffer(); | ||
| } | ||
|
|
||
| if (log.isDebugEnabled()) { | ||
| log.debug("AsyncChatStream appended to buffer: bufferLength={}, bufferSize={}, channel={}, " + | ||
| "recipientTeamId={}, recipientUserId={}, threadTs={}", | ||
| buffer.length(), bufferSize, channel, recipientTeamId, recipientUserId, threadTs); | ||
| } | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| public CompletableFuture<ChatStopStreamResponse> stop() { | ||
| return stop(null, null, null); | ||
| } | ||
|
|
||
| public CompletableFuture<ChatStopStreamResponse> stop(String markdownText) { | ||
| return stop(markdownText, null, null); | ||
| } | ||
|
|
||
| public CompletableFuture<ChatStopStreamResponse> stop( | ||
| String markdownText, | ||
| List<LayoutBlock> blocks, | ||
| Message.Metadata metadata | ||
| ) { | ||
| if (state == State.COMPLETED) { | ||
| CompletableFuture<ChatStopStreamResponse> f = new CompletableFuture<>(); | ||
| f.completeExceptionally(new SlackChatStreamException("Cannot stop stream: stream state is " + state)); | ||
| return f; | ||
| } | ||
|
|
||
| if (markdownText != null) { | ||
| buffer.append(markdownText); | ||
| } | ||
|
|
||
| CompletableFuture<Void> ensureStarted; | ||
| if (streamTs == null) { | ||
| ensureStarted = client.chatStartStream(ChatStartStreamRequest.builder() | ||
| .channel(channel) | ||
| .threadTs(threadTs) | ||
| .recipientTeamId(recipientTeamId) | ||
| .recipientUserId(recipientUserId) | ||
| .build()) | ||
| .thenApply(startResponse -> { | ||
| if (!startResponse.isOk() || startResponse.getTs() == null) { | ||
| SlackChatStreamException ex = new SlackChatStreamException( | ||
| "Failed to stop stream: stream not started - " + startResponse.getError()); | ||
| ex.setStartResponse(startResponse); | ||
| throw ex; | ||
| } | ||
| streamTs = startResponse.getTs(); | ||
| state = State.IN_PROGRESS; | ||
| return null; | ||
| }); | ||
| } else { | ||
| ensureStarted = CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| return ensureStarted.thenCompose(ignored -> client.chatStopStream(ChatStopStreamRequest.builder() | ||
| .channel(channel) | ||
| .ts(streamTs) | ||
| .markdownText(buffer.toString()) | ||
| .blocks(blocks) | ||
| .metadata(metadata) | ||
| .build()) | ||
| .thenApply(resp -> { | ||
| state = State.COMPLETED; | ||
| return resp; | ||
| })); | ||
| } | ||
|
|
||
| private CompletableFuture<ChatAppendStreamResponse> flushBuffer() { | ||
| if (streamTs == null) { | ||
| return client.chatStartStream(ChatStartStreamRequest.builder() | ||
| .channel(channel) | ||
| .threadTs(threadTs) | ||
| .recipientTeamId(recipientTeamId) | ||
| .recipientUserId(recipientUserId) | ||
| .markdownText(buffer.toString()) | ||
| .build()) | ||
| .thenApply(startResponse -> { | ||
| if (!startResponse.isOk()) { | ||
| SlackChatStreamException ex = new SlackChatStreamException( | ||
| "Failed to start stream: " + startResponse.getError()); | ||
| ex.setStartResponse(startResponse); | ||
| throw ex; | ||
| } | ||
| streamTs = startResponse.getTs(); | ||
| state = State.IN_PROGRESS; | ||
| ChatAppendStreamResponse synth = new ChatAppendStreamResponse(); | ||
| synth.setOk(startResponse.isOk()); | ||
| synth.setChannel(startResponse.getChannel()); | ||
| synth.setTs(startResponse.getTs()); | ||
| synth.setWarning(startResponse.getWarning()); | ||
| synth.setError(startResponse.getError()); | ||
| buffer.setLength(0); | ||
| return synth; | ||
| }); | ||
| } else { | ||
| return client.chatAppendStream(ChatAppendStreamRequest.builder() | ||
| .channel(channel) | ||
| .ts(streamTs) | ||
| .markdownText(buffer.toString()) | ||
| .build()) | ||
| .thenApply(resp -> { | ||
| if (!resp.isOk()) { | ||
| SlackChatStreamException ex = new SlackChatStreamException( | ||
| "Failed to append to stream: " + resp.getError()); | ||
| ex.getAppendResponses().add(resp); | ||
| throw ex; | ||
| } | ||
| buffer.setLength(0); | ||
| return resp; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.