package main import "./skills" import "./provider" import "./session" import "./ui/theme" import "base:runtime" import "core:encoding/json" import "core:fmt" import "core:strings" import "core:unicode" import "core:unicode/utf8" MAX_RESPONSE_BYTES :: 4 * runtime.Megabyte MAX_DISPLAY_TEXT_BYTES :: 64 * runtime.Kilobyte STREAM_BUFFER_RETAIN_CAPACITY :: 512 * runtime.Kilobyte DISPLAY_UPDATE_BEGIN :: "\x1b[?25l\x1b[?2026h" DISPLAY_UPDATE_END :: "\x1b[?2026l\x1b[?25h" Stream_Event_Kind :: enum { Text_Delta, Reasoning_Start, Reasoning_Delta, Reasoning_End, Tool_Calls, Usage, Continuation, Finished, Failed, Cancelled, } Stream_Event :: struct { kind: Stream_Event_Kind, data: string, tool_calls: [dynamic]session.Tool_Call, prompt_tokens: int, completion_tokens: int, total_tokens: int, reasoning_id: string, reasoning_kind: provider.Reasoning_Kind, provider_id: string, model_id: string, } Terminal_Escape_State :: enum { Text, Escape, Escape_Intermediate, Control_Sequence, String, String_Escape, } stream_event_destroy :: proc(event: ^Stream_Event) { delete(event.data) delete(event.reasoning_id) delete(event.provider_id) delete(event.model_id) for index in 0 ..< len(event.tool_calls) { session.destroy_tool_call(&event.tool_calls[index]) } delete(event.tool_calls) event^ = {} } Reasoning_Header_Rewrite :: struct { rows: int, suffix_start: int, suffix_end: int, } Response_Accumulator :: struct { content: [dynamic]u8, // Sanitized assistant text only. } Transcript_Render_State :: struct { bytes: [dynamic]u8, // Display transcript; may contain ANSI and UI-only text. escape_state: Terminal_Escape_State, utf8_pending: [4]u8, utf8_pending_len: int, rendered_bytes: int, response_drawn: bool, column: int, rows_below: int, markdown: Markdown_State, reasoning: [dynamic]u8, reasoning_kind: provider.Reasoning_Kind, reasoning_body: int, reasoning_plain_text: bool, reasoning_plain_text_started: bool, reasoning_title: string, reasoning_header_drawn: bool, reasoning_header_start: int, reasoning_header_suffix_start: int, reasoning_header_suffix_end: int, reasoning_header_rows: int, reasoning_rewrite_pending: bool, reasoning_rewrites: [dynamic]Reasoning_Header_Rewrite, } Stream :: struct { active: bool, response: Response_Accumulator, transcript: Transcript_Render_State, prompt: [dynamic]byte, prompt_len: int, cancel_pending: bool, terminal: bool, overflowed: bool, } response_accumulator_clear :: proc(response: ^Response_Accumulator) { clear(&response.content) } stream_destroy :: proc(stream: ^Stream) { markdown_destroy(&stream.transcript.markdown) delete(stream.response.content) delete(stream.transcript.bytes) delete(stream.prompt) delete(stream.transcript.reasoning) delete(stream.transcript.reasoning_rewrites) delete(stream.transcript.reasoning_title) stream^ = {} } stream_release_large_buffer :: proc(buffer: ^[dynamic]u8) -> bool { if cap(buffer^) <= STREAM_BUFFER_RETAIN_CAPACITY do return false delete(buffer^) buffer^ = {} return true } // Call after rendering the terminal transcript and committing response text // to session history. stream_reclaim_large_buffers_after_commit :: proc(stream: ^Stream) -> bool { if !stream.terminal do return false reclaimed := stream_release_large_buffer(&stream.response.content) if stream_release_large_buffer(&stream.transcript.bytes) { stream.transcript.rendered_bytes = 0 reclaimed = true } reclaimed = stream_release_large_buffer(&stream.transcript.reasoning) || reclaimed if stream_release_large_buffer(&stream.prompt) { stream.prompt_len = 0 reclaimed = true } return reclaimed } stream_start :: proc(stream: ^Stream, prompt: []byte) { clear(&stream.transcript.bytes) response_accumulator_clear(&stream.response) stream.active = true stream.transcript.rendered_bytes = 0 stream.transcript.response_drawn = false stream.transcript.column = 0 stream.prompt_len = len(prompt) stream.cancel_pending = false stream.terminal = false stream.overflowed = false stream.transcript.utf8_pending_len = 0 stream.transcript.escape_state = .Text clear(&stream.transcript.reasoning) delete(stream.transcript.reasoning_title) stream.transcript.reasoning_title = "" stream.transcript.reasoning_body = 0 stream.transcript.reasoning_plain_text = false stream.transcript.reasoning_plain_text_started = false stream.transcript.reasoning_header_drawn = false stream.transcript.reasoning_header_start = -1 stream.transcript.reasoning_header_suffix_start = -1 stream.transcript.reasoning_header_suffix_end = -1 stream.transcript.reasoning_header_rows = 0 stream.transcript.reasoning_rewrite_pending = false clear(&stream.transcript.reasoning_rewrites) markdown_reset(&stream.transcript.markdown) resize(&stream.prompt, len(prompt)) for index in 0 ..< len(prompt) { stream.prompt[index] = prompt[index] } } stream_append_safe_text_limit :: proc(stream: ^Stream, text: []u8, limit: int) -> bool { index := 0 if stream.transcript.utf8_pending_len > 0 { expected := stream_expected_rune_size(stream.transcript.utf8_pending[0]) available := min(expected - stream.transcript.utf8_pending_len, len(text)) copy(stream.transcript.utf8_pending[stream.transcript.utf8_pending_len:], text[:available]) stream.transcript.utf8_pending_len += available index += available if stream.transcript.utf8_pending_len < expected { return true } rune, size := utf8.decode_rune(stream.transcript.utf8_pending[:stream.transcript.utf8_pending_len]) stream.transcript.utf8_pending_len = 0 if rune != utf8.RUNE_ERROR || size > 1 { if limit >= 0 && len(stream.transcript.bytes) + size > limit { return false } append(&stream.transcript.bytes, ..stream.transcript.utf8_pending[:size]) } } for index < len(text) { character := text[index] switch stream.transcript.escape_state { case .Text: if character == 0x1b { stream.transcript.escape_state = .Escape index += 1 continue } expected := stream_expected_rune_size(character) if expected > len(text) - index { copy(stream.transcript.utf8_pending[:], text[index:]) stream.transcript.utf8_pending_len = len(text) - index return true } rune, size := utf8.decode_rune(text[index:]) if rune == utf8.RUNE_ERROR && size == 1 { // Provider text should be UTF-8. Dropping malformed bytes keeps the // stored response valid and prevents splitting a sequence at the cap. index += max(size, 1) continue } if limit >= 0 && len(stream.transcript.bytes) + size > limit { return false } append(&stream.transcript.bytes, ..text[index:index + size]) index += size continue case .Escape: switch character { case '[': stream.transcript.escape_state = .Control_Sequence case ']', 'P', 'X', '^', '_': stream.transcript.escape_state = .String case 0x20 ..= 0x2f: stream.transcript.escape_state = .Escape_Intermediate case: stream.transcript.escape_state = .Text } case .Escape_Intermediate: if character >= 0x30 && character <= 0x7e { stream.transcript.escape_state = .Text } case .Control_Sequence: if character >= 0x40 && character <= 0x7e { stream.transcript.escape_state = .Text } case .String: if character == 0x07 { stream.transcript.escape_state = .Text } else if character == 0x1b { stream.transcript.escape_state = .String_Escape } case .String_Escape: if character == '\\' { stream.transcript.escape_state = .Text } else if character != 0x1b { stream.transcript.escape_state = .String } } index += 1 } return true } stream_append_safe_text :: proc(stream: ^Stream, text: []u8) -> bool { return stream_append_safe_text_limit(stream, text, MAX_RESPONSE_BYTES) } stream_append_safe_assistant_text :: proc(stream: ^Stream, text: []u8) -> bool { start := len(stream.transcript.bytes) remaining := max(0, MAX_RESPONSE_BYTES - len(stream.response.content)) transcript_limit := start + remaining ok := stream_append_safe_text_limit(stream, text, transcript_limit) // Decoration writers use the display-only path and never reach this copy. append(&stream.response.content, ..stream.transcript.bytes[start:]) return ok } // Borrowed canonical assistant text; transcript-only rendering never enters it. stream_response_content :: proc(stream: ^Stream) -> string { return string(stream.response.content[:]) } stream_append_safe_display_text :: proc(stream: ^Stream, text: []u8) { _ = stream_append_safe_text_limit(stream, text, len(stream.transcript.bytes) + MAX_DISPLAY_TEXT_BYTES) } stream_end_safe_text :: proc(stream: ^Stream) { stream.transcript.escape_state = .Text stream.transcript.utf8_pending_len = 0 } stream_fail_overflow :: proc(stream: ^Stream) { stream_end_safe_text(stream) if stream.transcript.reasoning_plain_text_started { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) stream.transcript.reasoning_plain_text_started = false } if len(stream.transcript.bytes) > 0 { append(&stream.transcript.bytes, '\n') } message := fmt.aprintf("Error: response exceeded the %d byte limit", MAX_RESPONSE_BYTES) append(&stream.transcript.bytes, ..transmute([]u8)message) delete(message) stream.active = false stream.cancel_pending = false stream.terminal = true stream.overflowed = true } reasoning_summary_parts :: proc( text: string, ) -> ( title: string, body_offset: int, complete: bool, ) { trimmed := strings.trim_left(text, " \t\r\n") prefix := len(text) - len(trimmed) if !strings.has_prefix(trimmed, "**") { return "", prefix, len(trimmed) >= 2 } closing := strings.index(trimmed[2:], "**") if closing < 0 { return "", 0, false } closing += 2 title = strings.trim_space(trimmed[2:closing]) body_offset = prefix + closing + 2 for body_offset < len(text) && (text[body_offset] == '\r' || text[body_offset] == '\n' || text[body_offset] == ' ' || text[body_offset] == '\t') { body_offset += 1 } return title, body_offset, true } reasoning_display_parts :: proc( text: string, final: bool = false, ) -> ( title: string, body_offset: int, complete: bool, ) { title, body_offset, complete = reasoning_summary_parts(text) if title != "" || (strings.has_prefix(strings.trim_left(text, " \t\r\n"), "**") && !complete) { return } trimmed := strings.trim_left(text, " \t\r\n") prefix := len(text) - len(trimmed) line_end := strings.index(trimmed, "\n") if line_end < 0 { if !final || len(trimmed) == 0 || len(trimmed) > 120 { return "", prefix, false } return strings.trim_space(trimmed), len(text), true } line := strings.trim_space(trimmed[:line_end]) if line == "" || len(line) > 120 { return "", prefix, true } body_offset = prefix + line_end + 1 for body_offset < len(text) && (text[body_offset] == '\r' || text[body_offset] == '\n' || text[body_offset] == ' ' || text[body_offset] == '\t') { body_offset += 1 } return line, body_offset, true } reasoning_uses_plain_text_style :: proc(provider_id: string) -> bool { return provider_id == "opencode" || provider_id == "opencode-go" } stream_draw_reasoning_header :: proc(stream: ^Stream) { stream.transcript.reasoning_header_start = len(stream.transcript.bytes) stream.transcript.reasoning_header_suffix_start = -1 stream.transcript.reasoning_header_suffix_end = -1 append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.muted + "~ Thinking")) if stream.transcript.reasoning_title != "" { stream.transcript.reasoning_header_suffix_start = len(stream.transcript.bytes) append(&stream.transcript.bytes, ..transmute([]u8)string(": ")) stream_append_safe_display_text(stream, transmute([]u8)stream.transcript.reasoning_title) stream.transcript.reasoning_header_suffix_end = len(stream.transcript.bytes) } if stream.transcript.reasoning_body < len(stream.transcript.reasoning) { append( &stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset + "\n" + theme.DEFAULT.text), ) } else { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) } stream.transcript.reasoning_header_drawn = true } stream_reasoning_start :: proc( stream: ^Stream, kind: provider.Reasoning_Kind, provider_id: string, ) { stream_end_safe_text(stream) if len(stream.transcript.bytes) > 0 && stream.transcript.bytes[len(stream.transcript.bytes) - 1] != '\n' { append(&stream.transcript.bytes, '\n') } clear(&stream.transcript.reasoning) delete(stream.transcript.reasoning_title) stream.transcript.reasoning_title = "" stream.transcript.reasoning_body = 0 stream.transcript.reasoning_header_drawn = false stream.transcript.reasoning_header_start = -1 stream.transcript.reasoning_header_suffix_start = -1 stream.transcript.reasoning_header_suffix_end = -1 stream.transcript.reasoning_header_rows = 0 stream.transcript.reasoning_kind = kind stream.transcript.reasoning_plain_text = reasoning_uses_plain_text_style(provider_id) stream.transcript.reasoning_plain_text_started = false } stream_reasoning_delta :: proc(stream: ^Stream, text: string) { if len(stream.transcript.reasoning) + len(text) > MAX_RESPONSE_BYTES { stream_fail_overflow(stream) return } append(&stream.transcript.reasoning, ..transmute([]u8)text) if stream.transcript.reasoning_plain_text { if stream.transcript.reasoning_body < len(stream.transcript.reasoning) { if !stream.transcript.reasoning_plain_text_started { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.muted)) stream.transcript.reasoning_plain_text_started = true } if !stream_append_safe_text(stream, stream.transcript.reasoning[stream.transcript.reasoning_body:]) { stream_fail_overflow(stream) return } stream.transcript.reasoning_body = len(stream.transcript.reasoning) } return } title, body, complete := reasoning_display_parts(string(stream.transcript.reasoning[:])) if !complete { if stream.transcript.reasoning_kind == .Raw && !stream.transcript.reasoning_header_drawn { stream.transcript.reasoning_body = 0 stream_draw_reasoning_header(stream) } if !stream.transcript.reasoning_header_drawn { return } } if stream.transcript.reasoning_title == "" && title != "" && !stream.transcript.reasoning_header_drawn { stream.transcript.reasoning_title = strings.clone(title) } if !stream.transcript.reasoning_header_drawn { stream.transcript.reasoning_body = body stream_draw_reasoning_header(stream) if stream.transcript.reasoning_kind == .Raw && stream.transcript.reasoning_title == "" { stream.transcript.reasoning_body = 0 } } if stream.transcript.reasoning_body < len(stream.transcript.reasoning) { if !stream_append_safe_text(stream, stream.transcript.reasoning[stream.transcript.reasoning_body:]) { stream_fail_overflow(stream) return } stream.transcript.reasoning_body = len(stream.transcript.reasoning) } } stream_reasoning_end :: proc(stream: ^Stream) { if stream.transcript.reasoning_plain_text { if stream.transcript.reasoning_body < len(stream.transcript.reasoning) { if !stream.transcript.reasoning_plain_text_started { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.muted)) stream.transcript.reasoning_plain_text_started = true } if !stream_append_safe_text(stream, stream.transcript.reasoning[stream.transcript.reasoning_body:]) { stream_fail_overflow(stream) return } stream.transcript.reasoning_body = len(stream.transcript.reasoning) } if stream.transcript.reasoning_plain_text_started { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) stream.transcript.reasoning_plain_text_started = false } } else { if !stream.transcript.reasoning_header_drawn { title, body, _ := reasoning_display_parts(string(stream.transcript.reasoning[:]), true) if title != "" { stream.transcript.reasoning_title = strings.clone(title) } stream.transcript.reasoning_body = body if stream.transcript.reasoning_kind == .Raw && stream.transcript.reasoning_title == "" { stream.transcript.reasoning_body = 0 } stream_draw_reasoning_header(stream) } if stream.transcript.reasoning_body < len(stream.transcript.reasoning) { if !stream_append_safe_text(stream, stream.transcript.reasoning[stream.transcript.reasoning_body:]) { stream_fail_overflow(stream) return } stream.transcript.reasoning_body = len(stream.transcript.reasoning) } } stream_end_safe_text(stream) if len(stream.transcript.bytes) > 0 && stream.transcript.bytes[len(stream.transcript.bytes) - 1] != '\n' { append(&stream.transcript.bytes, '\n') } if stream.transcript.reasoning_plain_text { stream.transcript.reasoning_header_start = -1 } else if stream.transcript.rendered_bytes <= stream.transcript.reasoning_header_start { label_start := stream.transcript.reasoning_header_start + len(theme.DEFAULT.muted) + len("~ ") if label_start + len("Thinking") <= len(stream.transcript.bytes) { copy( stream.transcript.bytes[label_start + len("Thought"):], stream.transcript.bytes[label_start + len("Thinking"):], ) copy( stream.transcript.bytes[label_start:label_start + len("Thought")], transmute([]u8)string("Thought"), ) resize(&stream.transcript.bytes, len(stream.transcript.bytes) - 1) stream.transcript.reasoning_header_start = -1 } } else { append(&stream.transcript.reasoning_rewrites, Reasoning_Header_Rewrite{ rows = stream.transcript.reasoning_header_rows, suffix_start = stream.transcript.reasoning_header_suffix_start, suffix_end = stream.transcript.reasoning_header_suffix_end, }) } stream.transcript.reasoning_rewrite_pending = len(stream.transcript.reasoning_rewrites) > 0 } stream_rewrite_reasoning_header :: proc(stream: ^Stream, sb: ^strings.Builder) { if len(stream.transcript.reasoning_rewrites) == 0 || !stream.transcript.response_drawn { return } for rewrite in stream.transcript.reasoning_rewrites { fmt.sbprint(sb, "\x1b[s") if rewrite.rows > 0 { fmt.sbprintf(sb, "\x1b[%dA", rewrite.rows) } fmt.sbprint(sb, "\x1b[1G\x1b[2K" + theme.DEFAULT.muted + "~ Thought") if rewrite.suffix_start >= 0 { fmt.sbprint(sb, string(stream.transcript.bytes[rewrite.suffix_start:rewrite.suffix_end])) } fmt.sbprint(sb, theme.DEFAULT.reset) fmt.sbprint(sb, "\x1b[u") } clear(&stream.transcript.reasoning_rewrites) stream.transcript.reasoning_rewrite_pending = false stream.transcript.reasoning_header_start = -1 stream.transcript.reasoning_header_rows = 0 } stream_apply_event :: proc(stream: ^Stream, event: Stream_Event) { if stream.terminal { return } switch event.kind { case .Text_Delta: if !stream_append_safe_assistant_text(stream, transmute([]u8)event.data) { stream_fail_overflow(stream) } case .Reasoning_Start: stream_reasoning_start(stream, event.reasoning_kind, event.provider_id) case .Reasoning_Delta: stream_reasoning_delta(stream, event.data) case .Reasoning_End: stream_reasoning_end(stream) case .Tool_Calls: stream_end_safe_text(stream) for call in event.tool_calls { if len(stream.transcript.bytes) > 0 && stream.transcript.bytes[len(stream.transcript.bytes) - 1] != '\n' { append(&stream.transcript.bytes, '\n') } append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.tool_name)) append(&stream.transcript.bytes, '[') stream_append_safe_display_text(stream, transmute([]u8)call.function_call.name) append(&stream.transcript.bytes, ']') append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) parameter: string is_skill := call.function_call.name == "activate_skill" if is_skill { parameter = skills.call_name(call.function_call.arguments) } path_arguments: struct { path: string, } bash_arguments: struct { command: string, } if call.function_call.name == "bash" { if json.unmarshal_string( call.function_call.arguments, &bash_arguments, spec = .JSON, ) == nil { parameter = bash_arguments.command } } else if !is_skill && json.unmarshal_string( call.function_call.arguments, &path_arguments, spec = .JSON, ) == nil { parameter = path_arguments.path } if len(parameter) > 0 { append(&stream.transcript.bytes, ' ') append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.tool_parameter)) if !is_skill { append(&stream.transcript.bytes, '"') } if !is_skill && parameter == "." { append(&stream.transcript.bytes, '.', '/') } else { stream_append_safe_display_text(stream, transmute([]u8)parameter) } if !is_skill { append(&stream.transcript.bytes, '"') } append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) } if is_skill do delete(parameter) delete(path_arguments.path) delete(bash_arguments.command) append(&stream.transcript.bytes, '\n') if call.function_call.name == "task" { append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.muted)) append(&stream.transcript.bytes, ..transmute([]u8)string("│\n└")) append(&stream.transcript.bytes, ..transmute([]u8)string(theme.DEFAULT.reset)) append(&stream.transcript.bytes, '\n') } } case .Usage: // Session metadata does not alter the transcript stream. case .Continuation: // Opaque encrypted reasoning is retained by the agent, not rendered. // Tool calls keep the logical stream active while a follow-up request runs. case .Finished: stream_end_safe_text(stream) stream.active = false stream.cancel_pending = false stream.terminal = true case .Failed: stream_end_safe_text(stream) if len(stream.transcript.bytes) > 0 { append(&stream.transcript.bytes, '\n') } append(&stream.transcript.bytes, "Error: ") stream_append_safe_display_text(stream, transmute([]u8)event.data) stream_end_safe_text(stream) stream.active = false stream.cancel_pending = false stream.terminal = true case .Cancelled: stream_end_safe_text(stream) stream.active = false stream.cancel_pending = false stream.terminal = true } } stream_cancel :: proc(stream: ^Stream) { stream.active = false stream.cancel_pending = false } stream_new_line :: proc(stream: ^Stream, sb: ^strings.Builder) { // Create space below the entire prompt first. At the terminal bottom, the // newline scrolls normally instead of letting IL discard part of the prompt. fmt.sbprintf(sb, "\x1b[%dB\r\n\x1b[%dA\r\x1b[L", stream.transcript.rows_below, stream.transcript.rows_below) stream.transcript.column = 0 if stream.transcript.reasoning_header_start >= 0 { stream.transcript.reasoning_header_rows += 1 } } stream_expected_rune_size :: proc(first: u8) -> int { if first < 0x80 { return 1 } if first >= 0xc2 && first <= 0xdf { return 2 } if first >= 0xe0 && first <= 0xef { return 3 } if first >= 0xf0 && first <= 0xf4 { return 4 } return 1 } stream_write_text :: proc( stream: ^Stream, sb: ^strings.Builder, text: []u8, columns: int, final := true, ) -> int { index := 0 for index < len(text) { character := text[index] if character == '\n' { stream_new_line(stream, sb) index += 1 continue } if character == '\t' || character == ' ' { if stream.transcript.column >= columns - 1 { stream_new_line(stream, sb) } else { fmt.sbprint(sb, " ") stream.transcript.column += 1 } index += 1 continue } expected := stream_expected_rune_size(character) if expected > len(text) - index && !final { break } decoded, size := utf8.decode_rune(text[index:]) if decoded == utf8.RUNE_ERROR { if expected > len(text) - index && !final { break } if stream.transcript.column >= columns - 1 { stream_new_line(stream, sb) } fmt.sbprint(sb, "\xef\xbf\xbd") stream.transcript.column += 1 index += max(size, 1) continue } if unicode.is_control(decoded) { index += size continue } width := unicode.normalized_east_asian_width(decoded) if width > 0 && stream.transcript.column + width > columns - 1 { stream_new_line(stream, sb) } fmt.sbprint(sb, string(text[index:index + size])) stream.transcript.column += width index += size } return index } begin_stream :: proc( stream: ^Stream, sb: ^strings.Builder, cursor_row, columns, input_rows, below_input_rows: int, ) { stream.transcript.rows_below = input_rows + 1 + below_input_rows // Reserve a terminal row before inserting above the input. This keeps the // status row intact when the prompt already touches the screen bottom. to_bottom := input_rows - cursor_row + below_input_rows fmt.sbprintf(sb, "\x1b[%dB\r\n\x1b[%dA\r\x1b[L\x1b[2K\x1b[1G", to_bottom, stream.transcript.rows_below) fmt.sbprint(sb, theme.DEFAULT.user + "❯ ") stream.transcript.column = 2 stream_write_text(stream, sb, stream.prompt[:stream.prompt_len], columns) fmt.sbprint(sb, theme.DEFAULT.reset) // Return from the inserted user row to the input cursor. The response row is // inserted only when its first bytes arrive, so an empty anchor cannot be // consumed by a prompt redraw. fmt.sbprintf(sb, "\x1b[%dB\r", cursor_row + 1) } append_stream :: proc( stream: ^Stream, sb: ^strings.Builder, columns, input_cursor_row, input_cursor_column, input_rows, below_input_rows: int, ) { if len(stream.transcript.bytes) == stream.transcript.rendered_bytes && (stream.active || stream.transcript.markdown.finalized) { if !stream.transcript.reasoning_rewrite_pending { return } } stream.transcript.rows_below = input_rows + 1 + below_input_rows if !stream.transcript.response_drawn { // Reserve and insert the first response row directly above the prompt. to_bottom := input_rows - input_cursor_row + below_input_rows fmt.sbprintf(sb, "\x1b[%dB\r\n\x1b[%dA\r\x1b[L", to_bottom, stream.transcript.rows_below) stream.transcript.response_drawn = true stream.transcript.column = 0 } else { // The prompt immediately follows the newest response row. fmt.sbprintf(sb, "\x1b[%dA\r", input_cursor_row + 1) if stream.transcript.column > 0 { fmt.sbprintf(sb, "\x1b[%dC", stream.transcript.column) } } // Finalize previously rendered reasoning headers before appending new rows; // their saved row offsets are relative to the current response bottom. stream_rewrite_reasoning_header(stream, sb) new_text := stream.transcript.bytes[stream.transcript.rendered_bytes:] // Input and status redraws reset terminal attributes between deltas. // Restore the Markdown state before continuing an open span. markdown_emit_style(&stream.transcript.markdown, sb) markdown_feed(&stream.transcript.markdown, stream, sb, new_text, columns, !stream.active) stream.transcript.rendered_bytes = len(stream.transcript.bytes) // Return to the prompt, which may have moved down as rows were inserted. fmt.sbprintf(sb, "\x1b[%dB\x1b[%dG", input_cursor_row + 1, input_cursor_column) }