package share import ( "fmt" "html" "strings" ) // kandevRepoURL is the public GitHub repo for the project โ€” used in the // share page's "Try kandev" CTA and brand link. Pointing at the repo // instead of a marketing site signals "this is open source, here's the // code" which is the actual value proposition for the audience that // receives a shared task. const kandevRepoURL = "https://github.com/kdlbs/kandev" // BuildShareHTML produces a self-contained styled HTML page that renders // the snapshot as a real chat conversation: user bubbles right-aligned, // assistant content left-aligned, consecutive same-role messages fused // into one block, and tool calls collapsed inline rather than rendered // as their own messages. func BuildShareHTML(snap *Snapshot) string { if snap == nil { return "kandev share" } var b strings.Builder b.WriteString("\n\n") writeHTMLHead(&b, snap) b.WriteString("\n") // Duplicate the stylesheet at the top of as defense-in-depth: some // gist-rendering proxies copy only the body content into their own page, // dropping and any \n") writeHTMLHero(&b, snap) b.WriteString("
\n") writeHTMLConversation(&b, snap.Messages) b.WriteString("
\n") writeHTMLFooter(&b, snap) b.WriteString("\n\n") return b.String() } func writeHTMLHead(b *strings.Builder, snap *Snapshot) { title := html.EscapeString(nonEmpty(snap.Task.Title, "Untitled task")) fmt.Fprintf(b, "\n\n") fmt.Fprintf(b, "\n") fmt.Fprintf(b, "%s โ€” kandev share\n", title) fmt.Fprintf(b, "\n", title) b.WriteString("\n\n") } func writeHTMLHero(b *strings.Builder, snap *Snapshot) { title := html.EscapeString(nonEmpty(snap.Task.Title, "Untitled task")) b.WriteString("
\n") b.WriteString("
kandev") b.WriteString("ยทshared task
\n") fmt.Fprintf(b, "

%s

\n", title) b.WriteString("
") writeHTMLBadge(b, snap.Session.AgentType) writeHTMLBadge(b, snap.Session.Model) writeHTMLBadge(b, snap.Session.ExecutorType) writeHTMLBadge(b, fmt.Sprintf("%d messages", len(snap.Messages))) if completed := formatPtrTime(snap.Session.CompletedAt); completed != "" { writeHTMLBadge(b, completed) } b.WriteString("
\n") if len(snap.Redaction.AppliedRules) > 0 { fmt.Fprintf(b, "

๐Ÿ›ก๏ธ Redacted: %s

\n", html.EscapeString(strings.Join(snap.Redaction.AppliedRules, ", "))) } b.WriteString("
\n") } func writeHTMLBadge(b *strings.Builder, text string) { if text == "" { return } fmt.Fprintf(b, "%s", html.EscapeString(text)) } // messageGroup is a run of consecutive messages sharing the same role. // Blocks from every member are flattened in order so the whole run renders // inside a single bubble. type messageGroup struct { role string blocks []Block } // groupMessages collapses consecutive same-role messages so the renderer // can draw one bubble per group. Empty messages (no blocks) are dropped // rather than producing empty groups. func groupMessages(messages []Message) []messageGroup { var groups []messageGroup for _, m := range messages { if len(m.Blocks) == 0 { continue } if n := len(groups); n > 0 && groups[n-1].role == m.Role { groups[n-1].blocks = append(groups[n-1].blocks, m.Blocks...) continue } groups = append(groups, messageGroup{ role: m.Role, blocks: append([]Block(nil), m.Blocks...), }) } return groups } func writeHTMLConversation(b *strings.Builder, messages []Message) { groups := groupMessages(messages) if len(groups) == 0 { b.WriteString("

No messages.

\n") return } for _, g := range groups { writeHTMLGroup(b, g) } } func writeHTMLGroup(b *strings.Builder, g messageGroup) { cls, label, icon := messageRoleAttrs(g.role) fmt.Fprintf(b, "
\n", cls) fmt.Fprintf(b, "
%s
\n", icon) b.WriteString("
\n") fmt.Fprintf(b, "
%s
\n", html.EscapeString(label)) for _, block := range g.blocks { writeHTMLBlock(b, block) } b.WriteString("
\n
\n") } func messageRoleAttrs(role string) (cls, label, icon string) { switch role { case roleUser: return "user", "You", "๐Ÿง‘" case roleAssistant: return "assistant", "Assistant", "๐Ÿค–" case roleSystem: return "system", "System", "โš™๏ธ" } return "other", role, "โ€ข" } func writeHTMLBlock(b *strings.Builder, block Block) { switch block.Kind { case blockKindText: writeHTMLText(b, block.Text) case blockKindToolCall: writeHTMLToolCall(b, block) case blockKindToolResult: writeHTMLToolResult(b, block) case blockKindDiff: writeHTMLDiff(b, block) } } func writeHTMLToolCall(b *strings.Builder, block Block) { name := html.EscapeString(nonEmpty(block.ToolName, "tool")) summary := html.EscapeString(strings.TrimSpace(block.Text)) // Closed by default โ€” tool calls are noise unless the reader cares. b.WriteString("
\n") b.WriteString("๐Ÿ”ง") fmt.Fprintf(b, "%s", name) if summary != "" { fmt.Fprintf(b, "%s", summary) } b.WriteString("โ–ธ") b.WriteString("\n") if len(block.Args) > 0 { fmt.Fprintf(b, "
%s
\n", html.EscapeString(prettyJSON(block.Args))) } b.WriteString("
\n") } func writeHTMLToolResult(b *strings.Builder, block Block) { out := strings.TrimRight(block.Output, "\n") if strings.TrimSpace(out) == "" { return } label := "Tool output" if block.Truncated { label += " (truncated)" } b.WriteString("
\n") b.WriteString("๐Ÿ“ค") fmt.Fprintf(b, "%s", html.EscapeString(label)) b.WriteString("โ–ธ") b.WriteString("\n") fmt.Fprintf(b, "
%s
\n", html.EscapeString(out)) b.WriteString("
\n") } func writeHTMLDiff(b *strings.Builder, block Block) { if strings.TrimSpace(block.UnifiedDiff) == "" { return } path := html.EscapeString(nonEmpty(block.Path, "diff")) fmt.Fprintf(b, "
\n
๐Ÿ“") fmt.Fprintf(b, "%s
\n
", path)
	for _, line := range strings.Split(strings.TrimRight(block.UnifiedDiff, "\n"), "\n") {
		writeHTMLDiffLine(b, line)
	}
	b.WriteString("
\n
\n") } func writeHTMLDiffLine(b *strings.Builder, line string) { cls := "diff-ctx" switch { case strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---"): cls = "diff-file" case strings.HasPrefix(line, "@@"): cls = "diff-hunk" case strings.HasPrefix(line, "+"): cls = "diff-add" case strings.HasPrefix(line, "-"): cls = "diff-del" } fmt.Fprintf(b, "%s\n", cls, html.EscapeString(line)) } func writeHTMLFooter(b *strings.Builder, snap *Snapshot) { b.WriteString("\n") }