Documentation
¶
Index ¶
- Constants
- Variables
- func ClearInterrupted(post *model.Post) bool
- func MarkInterrupted(post *model.Post, reason string, at int64)
- func ModifyPostForBot(botid string, post *model.Post, respondingToPostID string)
- func PartialTextFromTurn(turn store.Turn) string
- type Client
- type MMPostStreamService
- func (p *MMPostStreamService) ActiveStreams() int64
- func (p *MMPostStreamService) FinishStreaming(postID string)
- func (p *MMPostStreamService) GetStreamingContext(inCtx context.Context, postID string) (context.Context, error)
- func (p *MMPostStreamService) SetHeartbeatInterval(d time.Duration)
- func (p *MMPostStreamService) SetRootContext(ctx context.Context)
- func (p *MMPostStreamService) SetTurnStore(ts TurnStore)
- func (p *MMPostStreamService) StopStreaming(postID string)
- func (p *MMPostStreamService) StreamContinuationToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, ...)
- func (p *MMPostStreamService) StreamToNewDM(ctx context.Context, botID string, stream *llm.TextStreamResult, userID string, ...) error
- func (p *MMPostStreamService) StreamToNewPost(ctx context.Context, botID string, requesterUserID string, ...) error
- func (p *MMPostStreamService) StreamToPost(ctx context.Context, stream *llm.TextStreamResult, post *model.Post, ...)
- func (p *MMPostStreamService) WaitForActiveStreams(timeout time.Duration) bool
- type RecoveredStream
- type RecoveryClient
- type RecoveryStore
- type Service
- type TurnStore
Constants ¶
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" )
const ConversationIDProp = "conversation_id"
const DefaultHeartbeatInterval = 5 * time.Second
DefaultHeartbeatInterval is how often an in-progress stream persists its partial content and bumps its turn's UpdatedAt.
const DefaultRecoveryBatchSize = 200
DefaultRecoveryBatchSize bounds the turns processed per sweep.
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.
const InterruptedAtProp = "llm_interrupted_at"
InterruptedAtProp is the time (ms since epoch) the interruption was recorded.
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.
const NoRegen = "no_regen"
const PostStreamingControlCancel = "cancel"
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.
const PostStreamingControlEnd = "end"
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.
const PostStreamingControlStart = "start"
const RespondingToProp = "responding_to"
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.
const UnsafeLinksPostProp = "unsafe_links"
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 ¶
var ErrAlreadyStreamingToPost = fmt.Errorf("already streaming to post")
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).
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
ClearInterrupted removes the interruption props. Returns true if any were present.
func MarkInterrupted ¶ added in v2.13.0
MarkInterrupted sets the interruption props on a post.
func ModifyPostForBot ¶
ModifyPostForBot modifies a post to add bot-specific properties
func PartialTextFromTurn ¶ added in v2.13.0
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 (*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 (*MMPostStreamService) StreamToNewPost ¶
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
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).