chat: strip <context>...</context> blocks in filtered output

chat.raw includes everything; chat strips:
- [[[...]]] markers
- fence delimiters
- <context>...</context> blocks (user prompts)

Text clients get clean output; GUI clients use chat.raw for full control.
This commit is contained in:
Levi Neely 2026-08-20 12:01:45 +02:00
parent c602238835
commit 7f9ff18132
1 changed files with 21 additions and 4 deletions

View File

@ -43,21 +43,38 @@ func dispatch(handlers map[string]func([]string) ([]byte, error), data []byte) (
return h(args)
}
// stripMarkers removes [[[...]]] marker lines and source fence delimiter lines
// from raw chat data. Content inside fences is preserved — only the ``` lines
// themselves are stripped.
// stripMarkers removes [[[...]]] marker lines, source fence delimiter lines,
// and <context>...</context> blocks from raw chat data. Content inside fences
// is preserved — only the ``` lines themselves are stripped.
func stripMarkers(data []byte) []byte {
var out []byte
inContext := false
for len(data) > 0 {
nl := bytes.IndexByte(data, '\n')
if nl < 0 {
out = append(out, data...)
if !inContext {
out = append(out, data...)
}
break
}
line := data[:nl+1]
data = data[nl+1:]
trimmed := bytes.TrimRight(line, "\n")
// Handle <context> block start/end
if bytes.Equal(bytes.TrimSpace(trimmed), []byte("<context>")) {
inContext = true
continue
}
if bytes.Equal(bytes.TrimSpace(trimmed), []byte("</context>")) {
inContext = false
continue
}
if inContext {
continue
}
if len(trimmed) >= 7 && bytes.HasPrefix(trimmed, []byte("[[[")) && bytes.HasSuffix(trimmed, []byte("]]]")) {
continue
}