streaming

package
v2.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// InterruptedReasonShutdown: the plugin was deactivated (upgrade, disable,
	// server shutdown) while the response was streaming and the stream was
	// finalized gracefully.
	InterruptedReasonShutdown = "shutdown"
	// InterruptedReasonLost: the node generating the response disappeared
	// without finalizing it; a later recovery sweep marked it.
	InterruptedReasonLost = "lost"
)
View Source
const ConversationIDProp = "conversation_id"
View Source
const DefaultHeartbeatInterval = 5 * time.Second

DefaultHeartbeatInterval is how often an in-progress stream persists its partial content and bumps its turn's UpdatedAt.

View Source
const DefaultRecoveryBatchSize = 200

DefaultRecoveryBatchSize bounds the turns processed per sweep.

View Source
const GenerationStoppedProp = "generation_stopped"

GenerationStoppedProp marks a response the requester stopped before it finished, so every viewer can tell a cut-off reply from a complete one.

View Source
const InterruptedAtProp = "llm_interrupted_at"

InterruptedAtProp is the time (ms since epoch) the interruption was recorded.

View Source
const InterruptedProp = "llm_interrupted"

InterruptedProp marks a bot post whose generation was cut short by the plugin or server going away rather than finishing or being stopped by the user. Its value is one of the InterruptedReason* constants. The webapp renders the interruption notice from this prop (only on agent-bot posts); the post message itself only carries the partial response.

View Source
const NoRegen = "no_regen"
View Source
const PostStreamingControlCancel = "cancel"
View Source
const PostStreamingControlContinue = "continue"

PostStreamingControlContinue signals a tool-approval resume stream onto a post that already has content. The webapp clears the visible message but keeps the resolved tool cards.

View Source
const PostStreamingControlEnd = "end"
View Source
const PostStreamingControlHeartbeat = "heartbeat"

PostStreamingControlHeartbeat is published every heartbeat tick while a stream is alive, even when no tokens arrive (slow first token, long tool runs), so clients can tell a live stream from a dead one.

View Source
const PostStreamingControlStart = "start"
View Source
const RespondingToProp = "responding_to"
View Source
const StaleStreamThreshold = 60 * time.Second

StaleStreamThreshold is how long a streaming turn may go without a heartbeat before a recovery sweep treats it as abandoned.

View Source
const UnsafeLinksPostProp = "unsafe_links"
View Source
const WebSearchContextProp = "web_search_context"

WebSearchContextProp is still read by conversations/web_search_context.go when extracting web search state from legacy thread posts.

Variables

View Source
var ErrAlreadyStreamingToPost = fmt.Errorf("already streaming to post")
View Source
var ErrPostNotFound = errors.New("post not found")

ErrPostNotFound is returned by RecoveryClient.GetPost when the post no longer exists (e.g. it was deleted mid-stream).

View Source
var ErrRunOwnershipLost = errors.New("run lost its post reservation")

ErrRunOwnershipLost cancels a run whose post reservation was lost (see conversations' post run reservation): another run may own the post now, so the stream must write nothing more, as with a claimed turn.

Functions

func ClearInterrupted added in v2.13.0

func ClearInterrupted(post *model.Post) bool

ClearInterrupted removes the interruption props. Returns true if any were present.

func MarkInterrupted added in v2.13.0

func MarkInterrupted(post *model.Post, reason string, at int64)

MarkInterrupted sets the interruption props on a post.

func ModifyPostForBot

func ModifyPostForBot(botid string, post *model.Post, respondingToPostID string)

ModifyPostForBot modifies a post to add bot-specific properties

func PartialTextFromTurn added in v2.13.0

func PartialTextFromTurn(turn store.Turn) string

PartialTextFromTurn concatenates the text blocks of a turn's content.

Types

type Client

type Client interface {
	PublishWebSocketEvent(event string, payload map[string]any, broadcast *model.WebsocketBroadcast)
	UpdatePost(post *model.Post) error
	CreatePost(post *model.Post) error
	DM(senderID, receiverID string, post *model.Post) error
	GetUser(userID string) (*model.User, error)
	GetChannel(channelID string) (*model.Channel, error)
	GetConfig() *model.Config
	KVSet(key string, value any) error
	LogError(msg string, keyValuePairs ...any)
	LogWarn(msg string, keyValuePairs ...any)
	LogDebug(msg string, keyValuePairs ...any)
}

Client defines the minimal client interface needed for streaming operations.

type MMPostStreamService

type MMPostStreamService struct {
	// contains filtered or unexported fields
}

func NewMMPostStreamService

func NewMMPostStreamService(mmClient Client, i18n *i18n.Bundle) *MMPostStreamService

func (*MMPostStreamService) ActiveStreams added in v2.13.0

func (p *MMPostStreamService) ActiveStreams() int64

ActiveStreams returns the number of streams currently being written on this node.

func (*MMPostStreamService) FinishStreaming

func (p *MMPostStreamService) FinishStreaming(postID string)

FinishStreaming should be called when a post streaming operation is finished on success or failure. It is safe to call multiple times, must be called at least once.

func (*MMPostStreamService) GetStreamingContext

func (p *MMPostStreamService) GetStreamingContext(inCtx context.Context, postID string) (context.Context, error)

func (*MMPostStreamService) SetHeartbeatInterval added in v2.13.0

func (p *MMPostStreamService) SetHeartbeatInterval(d time.Duration)

SetHeartbeatInterval overrides DefaultHeartbeatInterval.

func (*MMPostStreamService) SetRootContext added in v2.13.0

func (p *MMPostStreamService) SetRootContext(ctx context.Context)

SetRootContext overrides the plugin root context streams are tied to.

func (*MMPostStreamService) SetTurnStore

func (p *MMPostStreamService) SetTurnStore(ts TurnStore)

SetTurnStore sets the store used for persisting assistant turns. When nil (the default), turn persistence is silently skipped.

func (*MMPostStreamService) StopStreaming

func (p *MMPostStreamService) StopStreaming(postID string)

StopStreaming cancels any in-flight stream to the given post on behalf of the requester; the post is marked as stopped. It waits (bounded) for the stream to finish so a new stream can take the post. The run stays registered until its owner calls FinishStreaming, so a new run on the post cannot start while the stopped one is still finishing and be canceled by its cleanup.

func (*MMPostStreamService) StreamContinuationToPost

func (p *MMPostStreamService) StreamContinuationToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, userLocale string, requesterUserID string)

func (*MMPostStreamService) StreamToNewDM

func (p *MMPostStreamService) StreamToNewDM(ctx context.Context, botID string, stream *llm.TextStreamResult, userID string, post *model.Post, respondingToPostID string) error

func (*MMPostStreamService) StreamToNewPost

func (p *MMPostStreamService) StreamToNewPost(ctx context.Context, botID string, requesterUserID string, stream *llm.TextStreamResult, post *model.Post, respondingToPostID string) error

func (*MMPostStreamService) StreamToPost

func (p *MMPostStreamService) StreamToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, userLocale string, requesterUserID string)

StreamToPost streams a fresh response onto a post (first stream or regen, where the caller has already scrubbed prior turns). For tool-approval resume use StreamContinuationToPost.

func (*MMPostStreamService) WaitForActiveStreams added in v2.13.0

func (p *MMPostStreamService) WaitForActiveStreams(timeout time.Duration) bool

WaitForActiveStreams blocks until no stream is active on this node or the timeout elapses. Returns true if all streams finished. A post reserved with GetStreamingContext counts too: its stream goroutine may not have started writing yet, and must still get to mark itself interrupted.

type RecoveredStream added in v2.13.0

type RecoveredStream struct {
	PostID    string
	ChannelID string
	TurnID    string
}

RecoveredStream identifies a post repaired by the sweep (identifiers only).

func RecoverAbandonedStreams added in v2.13.0

func RecoverAbandonedStreams(ctx context.Context, rs RecoveryStore, client RecoveryClient, limit uint64) ([]RecoveredStream, error)

RecoverAbandonedStreams finds assistant turns left in the streaming state whose heartbeat is older than StaleStreamThreshold (the generating node crashed, or was killed before finalizing), claims each atomically, and marks its post as interrupted with the last flushed partial content. Concurrent sweeps on several nodes are safe: only the node whose claim update wins repairs a given post.

type RecoveryClient added in v2.13.0

type RecoveryClient interface {
	GetPost(postID string) (*model.Post, error)
	UpdatePost(post *model.Post) error
	LogWarn(msg string, keyValuePairs ...any)
	LogInfo(msg string, keyValuePairs ...any)
}

RecoveryClient is the Mattermost API subset used by the sweep.

type RecoveryStore added in v2.13.0

type RecoveryStore interface {
	GetStaleStreamingTurns(staleAfterMillis int64, limit uint64) ([]store.Turn, error)
	ClaimStaleStreamingTurn(id string, staleAfterMillis int64) (bool, error)
	ReleaseStreamingTurnClaim(id string) error
}

RecoveryStore is the store subset used by the abandoned-stream sweep.

type Service

type Service interface {
	StreamToNewPost(ctx context.Context, botID string, requesterUserID string, stream *llm.TextStreamResult, post *model.Post, respondingToPostID string) error
	StreamToNewDM(ctx context.Context, botID string, stream *llm.TextStreamResult, userID string, post *model.Post, respondingToPostID string) error
	StreamToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, userLocale string, requesterUserID string)

	// StreamContinuationToPost streams a follow-up round onto a post that
	// already has an assistant turn (tool-approval resume). Finalize demotes
	// the prior anchor so both rounds render. Do not use for regeneration.
	StreamContinuationToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, userLocale string, requesterUserID string)

	StopStreaming(postID string)
	GetStreamingContext(inCtx context.Context, postID string) (context.Context, error)
	FinishStreaming(postID string)
}

type TurnStore

type TurnStore interface {
	CreateTurnAutoSequence(turn *store.Turn) error
	GetTurnByPostID(postID string) (*store.Turn, error)
	UpdateTurnPostID(id string, postID *string) error
	HeartbeatStreamingTurn(id string, content json.RawMessage) (bool, error)
	FinalizeStreamingTurn(id string, content json.RawMessage, tokensIn, tokensOut int64, status string) (bool, error)
}

TurnStore is the subset of store operations needed by the streaming layer. Finalize either creates a fresh anchor (first stream / regen, with the caller having scrubbed any prior turns) or demotes the existing anchor and creates a new one (continuation).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL