oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chatstream
2
3import (
4 "amadan.net/oficina/equipo/internal/runnerproto"
5 "context"
6 "encoding/json"
7 "time"
8 "unicode/utf8"
9)
10
11type Forwarder struct {
12 Send func(context.Context, string, int64) error
13 stream Stream
14 at time.Time
15 sequence int64
16 sent string
17}
18
19func (f *Forwarder) Add(ctx context.Context, raw []byte) error {
20 f.stream.Add(raw)
21 text := f.stream.Text()
22 if text == "" || text == f.sent || time.Since(f.at) < 200*time.Millisecond || f.sequence >= runnerproto.MaxCoordinatorTextEvents {
23 return nil
24 }
25 f.sequence++
26 if err := f.Send(ctx, text, f.sequence); err != nil {
27 return err
28 }
29 f.sent, f.at = text, time.Now()
30 return nil
31}
32func TextEvent(e runnerproto.Event, text string, sequence int64) runnerproto.Event {
33 e.Kind = "coordinator.text"
34 e.Epoch = "coordinator.text.v1"
35 e.Sequence = &sequence
36 // Escaping can make a JSON string larger than its UTF-8 source.
37 for {
38 e.Payload, _ = json.Marshal(runnerproto.CoordinatorText{Text: text})
39 if len(e.Payload) <= runnerproto.MaxEventBytes {
40 break
41 }
42 text = text[:len(text)/2]
43 for !utf8.ValidString(text) {
44 text = text[:len(text)-1]
45 }
46 }
47 e.ID = runnerproto.EventHash([]any{e.AssignmentID, e.Kind, e.SessionID, sequence})
48 e.RequestID = e.ID
49 return e
50}
51