MCPcopy Index your code
hub / github.com/coder/coder / WatchChats

Method WatchChats

codersdk/chats.go:2910–2956  ·  view source on GitHub ↗

WatchChats streams lifecycle events for all of the authenticated user's chats in real time. The returned channel emits ChatWatchEvent values for status changes, title changes, creation, deletion, diff-status changes, and action-required notifications. Callers must close the returned io.Closer to rel

(ctx context.Context)

Source from the content-addressed store, hash-verified

2908// Callers must close the returned io.Closer to release the websocket
2909// connection when done.
2910func (c *ExperimentalClient) WatchChats(ctx context.Context) (<-chan ChatWatchEvent, io.Closer, error) {
2911 conn, err := c.Dial(
2912 ctx,
2913 "/api/experimental/chats/watch",
2914 &websocket.DialOptions{CompressionMode: websocket.CompressionDisabled},
2915 )
2916 if err != nil {
2917 return nil, nil, err
2918 }
2919 conn.SetReadLimit(1 << 22) // 4MiB
2920
2921 streamCtx, streamCancel := context.WithCancel(ctx)
2922 events := make(chan ChatWatchEvent, 128)
2923
2924 go func() {
2925 defer close(events)
2926 defer streamCancel()
2927 defer func() {
2928 _ = conn.Close(websocket.StatusNormalClosure, "")
2929 }()
2930
2931 for {
2932 var event ChatWatchEvent
2933 if err := wsjson.Read(streamCtx, conn, &event); err != nil {
2934 if streamCtx.Err() != nil {
2935 return
2936 }
2937 switch websocket.CloseStatus(err) {
2938 case websocket.StatusNormalClosure, websocket.StatusGoingAway:
2939 return
2940 }
2941 return
2942 }
2943
2944 select {
2945 case <-streamCtx.Done():
2946 return
2947 case events <- event:
2948 }
2949 }
2950 }()
2951
2952 return events, closeFunc(func() error {
2953 streamCancel()
2954 return nil
2955 }), nil
2956}
2957
2958// GetChatDebugLogging returns the runtime admin setting that allows
2959// users to opt into chat debug logging.

Callers

nothing calls this directly

Calls 6

ErrMethod · 0.80
closeFuncFuncType · 0.70
DialMethod · 0.65
CloseMethod · 0.65
ReadMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected