mirror of
https://github.com/coder/coder.git
synced 2026-09-23 22:20:22 +08:00
Implements the chatd stabilization RFC. Combines: - https://github.com/coder/coder/pull/25908 - https://github.com/coder/coder/pull/25923 - https://github.com/coder/coder/pull/26109 - https://github.com/coder/coder/pull/26110 - https://github.com/coder/coder/pull/26111 - https://github.com/coder/coder/pull/26112
33 lines
979 B
Go
33 lines
979 B
Go
package chatloop
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type messagePartPublisherKey struct{}
|
|
|
|
// WithMessagePartPublisher returns a context carrying the streaming
|
|
// message-part publisher so tools can stream intermediate output (e.g.
|
|
// advisor advice deltas) while they execute. ExecuteLocalTools injects
|
|
// the publisher before running tools.
|
|
func WithMessagePartPublisher(
|
|
ctx context.Context,
|
|
publish func(codersdk.ChatMessageRole, codersdk.ChatMessagePart),
|
|
) context.Context {
|
|
if publish == nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, messagePartPublisherKey{}, publish)
|
|
}
|
|
|
|
// MessagePartPublisherFromContext returns the publisher injected by
|
|
// ExecuteLocalTools, or nil when absent.
|
|
func MessagePartPublisherFromContext(
|
|
ctx context.Context,
|
|
) func(codersdk.ChatMessageRole, codersdk.ChatMessagePart) {
|
|
publish, _ := ctx.Value(messagePartPublisherKey{}).(func(codersdk.ChatMessageRole, codersdk.ChatMessagePart))
|
|
return publish
|
|
}
|