Skip to content

Commit 76cec1c

Browse files
committed
Preserve reference links across streamed Markdown blocks
1 parent 1aba68b commit 76cec1c

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎cmd/micro/chat/display.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func (d *chatDisplay) token(text string) {
3535
defer d.mu.Unlock()
3636
d.markdown.WriteString(terminalText(text))
3737
text = d.markdown.String()
38-
if end := completeMarkdown(text); end > 0 {
38+
// Reference links and their definitions may occur in different blocks.
39+
// Conservatively retain the rest of the document from the first bracket.
40+
if end := completeMarkdown(text); end > 0 && !strings.Contains(text[:end], "[") {
3941
width := d.width()
4042
fmt.Fprintln(d.terminal.Stdout(), renderMarkdown(text[:end], width))
4143
d.markdown.Reset()

‎cmd/micro/chat/display_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,41 @@ func TestCompletedBlocksAppearBeforeFlush(t *testing.T) {
6464
}
6565
}
6666
}
67+
68+
func TestStreamedReferenceLinks(t *testing.T) {
69+
for _, parts := range [][]string{
70+
{"[Docs][docs]\n\n", "[docs]: https://example.com\n"},
71+
{"[docs]: https://example.com\n\n", "[Docs][docs]\n\n"},
72+
{"[docs]\n\n", "[docs]: https://example.com\n"},
73+
{"[docs][]\n\n", "[docs]: https://example.com\n"},
74+
} {
75+
t.Run(strings.Join(parts, ""), func(t *testing.T) {
76+
var output bytes.Buffer
77+
terminal, err := readline.NewEx(&readline.Config{Prompt: "micro > ", Stdin: io.NopCloser(strings.NewReader("")), Stdout: &output, Stderr: io.Discard})
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
defer terminal.Close()
82+
d := &chatDisplay{terminal: terminal}
83+
d.token("Introduction.\n\n")
84+
if strings.Count(output.String(), "Introduction.") != 1 {
85+
t.Fatal("ordinary paragraph did not stream")
86+
}
87+
before := output.String()
88+
for _, part := range parts {
89+
d.token(part)
90+
}
91+
if output.String() != before {
92+
t.Fatalf("reference context rendered early: %q", output.String())
93+
}
94+
d.flush()
95+
d.flush()
96+
if strings.Count(output.String(), "https://example.com") != 1 {
97+
t.Fatalf("link lost or repeated: %q", output.String())
98+
}
99+
if strings.Count(output.String(), "Introduction.") != 1 {
100+
t.Fatal("earlier paragraph repeated")
101+
}
102+
})
103+
}
104+
}

‎internal/website/content/en/docs/interactive-agent.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ options without using the CLI. No product-specific assistant runtime is required
205205

206206
The terminal prints completed Markdown paragraphs and fenced code blocks as they arrive,
207207
and prints the unfinished block when the reply ends. Each block appears once.
208+
Content containing brackets is held until completion to preserve Markdown reference links.
208209
Command output stays plain text; incomplete lines wait for a newline or completion.
209210
It uses the terminal’s foreground and background and respects `NO_COLOR`; piped
210211
output retains Markdown source. No separate graphical UI is included. Live

0 commit comments

Comments
 (0)