bots-garden/sidekickspublic Fork
⑂ v1.0.0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/sidekicks.git
git clone ssh://git@rickub.com/bots-garden/sidekicks.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
stream.go · 91 lines · 2.1 KBGo Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE

package main

import (
	"strings"
	"time"
	"unicode/utf8"

	"github.com/wailsapp/wails/v3/pkg/application"
)

const eventChatChunk = "chat:chunk"

// StreamChunk is one fragment of a simulated agent answer, like an ACP
// agent_message_chunk. Done marks the end of the turn.
type StreamChunk struct {
	Text string `json:"text"`
	Done bool   `json:"done"`
}

// StreamService replays a Markdown answer in small fragments to measure how
// the frontend copes with streaming (rendering, highlighting, scrolling).
type StreamService struct{}

// NewStreamService returns the service.
func NewStreamService() *StreamService {
	return &StreamService{}
}

// Simulate emits sampleAnswer as fragments of chunkSize runes, one every
// interval milliseconds (20 ms ≈ 50 events/s, the target of issue #13).
func (s *StreamService) Simulate(chunkSize, intervalMs int) {
	if chunkSize < 1 {
		chunkSize = 1
	}
	go func() {
		events := application.Get().Event
		text := sampleAnswer
		for len(text) > 0 {
			cut := 0
			for i := 0; i < chunkSize && cut < len(text); i++ {
				_, size := utf8.DecodeRuneInString(text[cut:])
				cut += size
			}
			events.Emit(eventChatChunk, StreamChunk{Text: text[:cut]})
			text = text[cut:]
			time.Sleep(time.Duration(intervalMs) * time.Millisecond)
		}
		events.Emit(eventChatChunk, StreamChunk{Done: true})
	}()
}

var sampleAnswer = strings.TrimSpace(`
Voici un **serveur HTTP** minimal en Go, avec un *handler* et un test :

` + "```go" + `
package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Bonjour %s 👋", r.URL.Query().Get("name"))
}

func main() {
	http.HandleFunc("/hello", hello)
	_ = http.ListenAndServe(":8080", nil)
}
` + "```" + `

Pour le lancer :

` + "```bash" + `
go run . &
curl "http://localhost:8080/hello?name=Alice"
` + "```" + `

| Méthode | Route | Rôle |
| --- | --- | --- |
| GET | /hello | salue l'utilisateur |

> Astuce : un contenu HTML comme <img src=x onerror="alert(1)"> n'est jamais exécuté.

1. Créer le fichier
2. Lancer le serveur
3. Tester avec curl
`)