| 1 | // Package chatstream extracts display text, never arbitrary tool arguments. |
| 2 | package chatstream |
| 3 | |
| 4 | import ( |
| 5 | "encoding/json" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "unicode/utf8" |
| 9 | ) |
| 10 | |
| 11 | const Limit = 32 << 10 |
| 12 | |
| 13 | type Stream struct { |
| 14 | text, args, reply string |
| 15 | toolIndex int |
| 16 | post bool |
| 17 | } |
| 18 | |
| 19 | func (s *Stream) Add(raw []byte) { |
| 20 | var v struct { |
| 21 | Event struct { |
| 22 | Type string |
| 23 | Index int |
| 24 | Content struct{ Type, Name string } `json:"content_block"` |
| 25 | Delta struct { |
| 26 | Type, Text string |
| 27 | JSON string `json:"partial_json"` |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | if json.Unmarshal(raw, &v) != nil { |
| 32 | return |
| 33 | } |
| 34 | e := v.Event |
| 35 | switch e.Type { |
| 36 | case "content_block_start": |
| 37 | s.post = e.Content.Type == "tool_use" && (e.Content.Name == "mcp__equipo__post_message" || e.Content.Name == "post_message") |
| 38 | if s.post { |
| 39 | s.args = "" |
| 40 | s.toolIndex = e.Index |
| 41 | } |
| 42 | case "content_block_delta": |
| 43 | if e.Delta.Type == "text_delta" && len(s.text)+len(e.Delta.Text) <= Limit { |
| 44 | s.text += e.Delta.Text |
| 45 | } |
| 46 | if e.Delta.Type == "input_json_delta" && s.post && e.Index == s.toolIndex && len(s.args)+len(e.Delta.JSON) <= Limit*6 { |
| 47 | s.args += e.Delta.JSON |
| 48 | if body, ok := partialBody(s.args); ok && len(body) <= Limit { |
| 49 | s.reply = body |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | func (s *Stream) Text() string { |
| 55 | if s.reply != "" { |
| 56 | return s.reply |
| 57 | } |
| 58 | return s.text |
| 59 | } |
| 60 | func partialBody(raw string) (string, bool) { |
| 61 | // Tokenize complete preceding fields; only the top-level body string is visible. |
| 62 | d := json.NewDecoder(strings.NewReader(raw)) |
| 63 | token, err := d.Token() |
| 64 | if err != nil || token != json.Delim('{') { |
| 65 | return "", false |
| 66 | } |
| 67 | for d.More() { |
| 68 | k, err := d.Token() |
| 69 | if err != nil { |
| 70 | return "", false |
| 71 | } |
| 72 | if k != "body" { |
| 73 | var discard json.RawMessage |
| 74 | if d.Decode(&discard) != nil { |
| 75 | return "", false |
| 76 | } |
| 77 | continue |
| 78 | } |
| 79 | rest := strings.TrimSpace(raw[d.InputOffset():]) |
| 80 | rest = strings.TrimSpace(strings.TrimPrefix(rest, ":")) |
| 81 | if !strings.HasPrefix(rest, `"`) { |
| 82 | return "", false |
| 83 | } |
| 84 | // Decode only complete characters, leaving a split escape for the next chunk. |
| 85 | var out strings.Builder |
| 86 | for i := 1; i < len(rest); { |
| 87 | if rest[i] == '"' { |
| 88 | return out.String(), true |
| 89 | } |
| 90 | if rest[i] == '\\' { |
| 91 | n := 2 |
| 92 | if i+1 >= len(rest) { |
| 93 | break |
| 94 | } |
| 95 | if rest[i+1] == 'u' { |
| 96 | n = 6 |
| 97 | } |
| 98 | if i+n > len(rest) { |
| 99 | break |
| 100 | } |
| 101 | part := rest[i : i+n] |
| 102 | if n == 6 { |
| 103 | code, _ := strconv.ParseUint(part[2:], 16, 16) |
| 104 | if code >= 0xd800 && code <= 0xdbff { |
| 105 | if i+12 > len(rest) { |
| 106 | break |
| 107 | } |
| 108 | part = rest[i : i+12] |
| 109 | n = 12 |
| 110 | } |
| 111 | } |
| 112 | var decoded string |
| 113 | if json.Unmarshal([]byte(`"`+part+`"`), &decoded) != nil { |
| 114 | return "", false |
| 115 | } |
| 116 | out.WriteString(decoded) |
| 117 | i += n |
| 118 | } else { |
| 119 | r, n := utf8.DecodeRuneInString(rest[i:]) |
| 120 | if r == utf8.RuneError && n == 1 { |
| 121 | break |
| 122 | } |
| 123 | out.WriteRune(r) |
| 124 | i += n |
| 125 | } |
| 126 | } |
| 127 | return out.String(), true |
| 128 | } |
| 129 | return "", false |
| 130 | } |
| 131 | |