PranitInitial commit439f83c3 months agoHistory
4966 lines · 206.1 KB · Zig
RawDownload
const std = @import("std");const question_prompt = @import("../../core/agent/question_prompt.zig");const approval_decision = @import("../../core/permissions/approval_decision.zig");const command_specs = @import("../../core/slash_commands/command_specs.zig");const input_appearance = @import("../../core/config/input_appearance.zig");const settings_catalog = @import("../../core/config/settings_catalog.zig");const usage_menu = @import("../../core/session/usage_menu.zig");const paste_blocks = @import("../../core/input/pasted_blocks.zig");const native_clear_probe_runtime = @import("native_clear_probe.zig");const image_attachments = @import("../../core/images/image_attachments.zig");const debug_trace = @import("../../core/shared/debug_trace.zig");const io_mod = @import("../../core/shared/io.zig");const types = @import("../../core/shared/types.zig");const cursor_probe = @import("../terminal/cursor_probe.zig");const theme_monitor = @import("../terminal/theme_monitor.zig");const editor_state = @import("../../core/input/editor_state.zig");const edit_history = @import("../../core/input/edit_history.zig");const gesture_state = @import("../../core/input/gesture_state.zig");const horizontal_navigation = @import("../../core/input/horizontal_navigation.zig");const input_limit_rejection = @import("../../core/input/input_limit_rejection.zig");const input_reset = @import("../../core/input/input_reset.zig");const picker_state = @import("../../core/input/picker_state.zig");const paste_framing = @import("../../core/input/paste_framing.zig");const registered_entities = @import("../../core/input/registered_entities.zig");const text_scalar = @import("../../core/input/text_scalar.zig");const composer_history = @import("../../core/input/composer_history.zig");const composer_deletion = @import("../../core/input/composer_deletion.zig");const composer_insertion = @import("../../core/input/composer_insertion.zig");const composer_kill_ring = @import("../../core/input/composer_kill_ring.zig");const composer_line_continuation = @import("../../core/input/composer_line_continuation.zig");const composer_replacement = @import("../../core/input/composer_replacement.zig");const composer_selection = @import("../../core/input/composer_selection.zig");const composer_skill_binding = @import("../../core/input/composer_skill_binding.zig");const composer_text_replacement = @import("../../core/input/composer_text_replacement.zig");const composer_undo = @import("../../core/input/composer_undo.zig");const kill_ring = @import("../../core/input/kill_ring.zig");const workspace_menu = @import("../../core/workspace/workspace_menu.zig");const entity_spans = @import("../../core/shared/entity_spans.zig");const input_action = @import("../../core/input/input_action.zig");const vertical_navigation = @import("../../core/input/vertical_navigation.zig");const visual_layout = @import("visual_layout.zig");const escape_parser = @import("escape_parser.zig");const shortcuts = @import("shortcuts.zig");const terminal_action_decoder = @import("terminal_action_decoder.zig"); pub const ImageBlocks = kill_ring.ImageBlocks; const Allocator = std.mem.Allocator;const InsertResult = composer_insertion.InsertResult; pub const DeferredTerminalInputSource = enum {    theme_monitor,    cursor_probe,}; pub const TerminalInputOwner = enum {    theme_monitor,    paste,    takeover,    fx_input,}; pub fn terminalInputOwner(    monitor: *const theme_monitor.Monitor,    paste_active: bool,    takeover_active: bool,) TerminalInputOwner {    if (monitor.ownsInput()) return .theme_monitor;    if (paste_active) return .paste;    if (takeover_active) return .takeover;    if (monitor.enabled) return .theme_monitor;    return .fx_input;} const InputEscapeAction = input_action.Action;const MouseInput = escape_parser.MouseInput; pub const shortcutFromControlByte = shortcuts.fromControlByte;pub const shortcutFromFocusedEditorControlByte = shortcuts.fromFocusedEditorControlByte;pub const shortcutFromEscapeAction = shortcuts.fromEscapeAction; pub fn approvalActionFromByte(byte: u8) ?approval_decision.Action {    return switch (byte) {        3 => .deny,        '\r', '\n' => .submit,        '\t' => .tab,        0x7F, 0x08 => .backspace,        '1'...'3' => .{ .number = byte - '1' },        else => if (byte >= 0x20 and byte <= 0x7E)            .{ .insert_ascii = byte }        else            null,    };} fn approvalDraftActionFromShortcut(    action: input_action.ShortcutAction,) ?approval_decision.DraftAction {    return switch (action) {        .move => |intent| switch (intent.kind) {            .character_left => .cursor_left,            .character_right => .cursor_right,            .line_start, .draft_start => .cursor_home,            .line_end, .draft_end => .cursor_end,            .word_left => .cursor_word_left,            .word_right => .cursor_word_right,            else => null,        },        .delete_forward => .delete_next,        .delete_word_left => .delete_word_left,        .delete_whitespace_word_left => .delete_whitespace_word_left,        .delete_word_right => .delete_word_right,        .delete_to_line_start => .delete_to_line_start,        .delete_to_line_end => .delete_to_line_end,        else => null,    };} fn approvalDraftActionFromControlByte(byte: u8) ?approval_decision.DraftAction {    const action = shortcuts.fromFocusedEditorControlByte(byte) orelse return null;    return approvalDraftActionFromShortcut(action);} fn approvalActionFromRawByte(byte: u8) ?approval_decision.Action {    if (approvalDraftActionFromControlByte(byte)) |edit| {        return .{ .edit_draft = edit };    }    return approvalActionFromByte(byte);} fn withApprovalInput(    ingress: input_action.TerminalInputIngress,) input_action.TerminalInputIngress {    var typed = ingress;    const event = typed.event orelse return typed;    typed.event = switch (event) {        .raw => |raw| input_action.TerminalInputEvent{ .raw = .{            .byte = raw.byte,            .composer_shortcut = raw.composer_shortcut,            .approval_action = approvalActionFromRawByte(raw.byte),        } },        .action => |decoded| input_action.TerminalInputEvent{ .action = .{            .action = decoded.action,            .composer_shortcut = decoded.composer_shortcut,            .approval_focused_edit = if (decoded.composer_shortcut) |shortcut|                approvalDraftActionFromShortcut(shortcut)            else                null,            .cancel_pending = decoded.cancel_pending,        } },        .paste_byte, .unrecognized_escape => event,    };    return typed;} fn questionActionFromByte(    byte: u8,    freeform_selected: bool,) ?question_prompt.Action {    return switch (byte) {        3 => .cancel,        '\r', '\n' => .submit,        '\t' => .next_entry,        0x7F, 0x08 => if (freeform_selected) .backspace else null,        '1'...'9' => if (freeform_selected)            .{ .insert_ascii = byte }        else            .{ .select_ordinal = byte - '1' },        else => if (freeform_selected and byte >= 0x20 and byte <= 0x7E)            .{ .insert_ascii = byte }        else            null,    };} fn questionFreeformActionFromShortcut(    action: input_action.ShortcutAction,) ?question_prompt.FreeformAction {    return switch (action) {        .move => |intent| switch (intent.kind) {            .character_left => .cursor_left,            .character_right => .cursor_right,            .line_start, .draft_start => .cursor_home,            .line_end, .draft_end => .cursor_end,            .word_left => .cursor_word_left,            .word_right => .cursor_word_right,            else => null,        },        .delete_forward => .delete_next,        .delete_word_left => .delete_word_left,        .delete_whitespace_word_left => .delete_whitespace_word_left,        .delete_word_right => .delete_word_right,        .delete_to_line_start => .delete_to_line_start,        .delete_to_line_end => .delete_to_line_end,        else => null,    };} fn questionActionFromShortcut(    action: input_action.ShortcutAction,    freeform_selected: bool,) ?question_prompt.Action {    switch (action) {        .move => |intent| {            if (intent.extend_selection) {                switch (intent.kind) {                    .visual_up => return .{ .move_choice = .previous },                    .visual_down => return .{ .move_choice = .next },                    else => {},                }            }        },        else => {},    }    if (!freeform_selected) return null;    const edit = questionFreeformActionFromShortcut(action) orelse return null;    return .{ .edit_freeform = edit };} fn questionActionFromRawByte(    byte: u8,    freeform_selected: bool,) ?question_prompt.Action {    if (freeform_selected) {        if (shortcuts.fromFocusedEditorControlByte(byte)) |shortcut| {            return questionActionFromShortcut(shortcut, true);        }    }    return questionActionFromByte(byte, freeform_selected);} fn questionActionFromDecoded(    action: input_action.Action,    shortcut: ?input_action.ShortcutAction,    freeform_selected: bool,) ?question_prompt.Action {    switch (action) {        .remapped_byte => |byte| return questionActionFromRawByte(            byte,            freeform_selected,        ),        else => {},    }    return questionActionFromShortcut(        shortcut orelse return null,        freeform_selected,    );} fn withQuestionInput(    ingress: input_action.TerminalInputIngress,    freeform_selected: bool,) input_action.TerminalInputIngress {    var typed = ingress;    const event = typed.event orelse return typed;    typed.event = switch (event) {        .raw => |raw| input_action.TerminalInputEvent{ .raw = .{            .byte = raw.byte,            .composer_shortcut = raw.composer_shortcut,            .approval_action = raw.approval_action,            .question_action = questionActionFromRawByte(                raw.byte,                freeform_selected,            ),        } },        .action => |decoded| input_action.TerminalInputEvent{ .action = .{            .action = decoded.action,            .composer_shortcut = decoded.composer_shortcut,            .approval_focused_edit = decoded.approval_focused_edit,            .question_action = questionActionFromDecoded(                decoded.action,                decoded.composer_shortcut,                freeform_selected,            ),            .cancel_pending = decoded.cancel_pending,        } },        .paste_byte, .unrecognized_escape => event,    };    return typed;} test "question bytes translate to typed prompt actions" {    try std.testing.expectEqual(        question_prompt.Action.cancel,        questionActionFromByte(3, false).?,    );    try std.testing.expectEqual(        question_prompt.Action.submit,        questionActionFromByte('\r', false).?,    );    try std.testing.expectEqual(        question_prompt.Action.next_entry,        questionActionFromByte('\t', false).?,    );    try std.testing.expectEqual(        question_prompt.Action{ .select_ordinal = 2 },        questionActionFromByte('3', false).?,    );    try std.testing.expectEqual(        question_prompt.Action{ .insert_ascii = '3' },        questionActionFromByte('3', true).?,    );    try std.testing.expectEqual(        question_prompt.Action.backspace,        questionActionFromByte(0x7F, true).?,    );    try std.testing.expect(questionActionFromByte('x', false) == null);    try std.testing.expect(questionActionFromByte(0x7F, false) == null);    try std.testing.expect(questionActionFromByte(0x80, true) == null);} test "terminal input carries typed question decisions and focused edits" {    var runtime = InputRuntime{};    defer runtime.deinit(std.testing.allocator);     const choice_context: input_action.TerminalDecodeContext = .{        .now_ms = 1,        .paste_active = false,        .cancel_pending = false,        .child_route_active = false,        .question_freeform_selected = false,    };    const choice = runtime.decodeTerminalByte('3', choice_context);    try std.testing.expectEqual(        question_prompt.Action{ .select_ordinal = 2 },        choice.event.?.raw.question_action.?,    );     const freeform_context: input_action.TerminalDecodeContext = .{        .now_ms = 2,        .paste_active = false,        .cancel_pending = false,        .child_route_active = false,        .question_freeform_selected = true,    };    const digit = runtime.decodeTerminalByte('3', freeform_context);    try std.testing.expectEqual(        question_prompt.Action{ .insert_ascii = '3' },        digit.event.?.raw.question_action.?,    );    const focused_control = runtime.decodeTerminalByte(1, freeform_context);    try std.testing.expectEqual(        question_prompt.Action{ .edit_freeform = .cursor_home },        focused_control.event.?.raw.question_action.?,    );     var focused_escape = input_action.TerminalInputIngress{};    for ("\x1bd") |byte| {        focused_escape = runtime.decodeTerminalByte(byte, freeform_context);    }    try std.testing.expectEqual(        question_prompt.Action{ .edit_freeform = .delete_word_right },        focused_escape.event.?.action.question_action.?,    );     var shifted_option = input_action.TerminalInputIngress{};    for ("\x1b[1;2A") |byte| {        shifted_option = runtime.decodeTerminalByte(byte, freeform_context);    }    try std.testing.expectEqual(        question_prompt.Action{ .move_choice = .previous },        shifted_option.event.?.action.question_action.?,    );     var remapped_cancel = input_action.TerminalInputIngress{};    for ("\x1b[99;5u") |byte| {        remapped_cancel = runtime.decodeTerminalByte(byte, choice_context);    }    try std.testing.expectEqual(        question_prompt.Action.cancel,        remapped_cancel.event.?.action.question_action.?,    );} test "Fx terminal reply ownership survives takeover transition" {    const alloc = std.testing.allocator;    var monitor = theme_monitor.Monitor{};    monitor.start();     try std.testing.expect(monitor.takeQueryRequest(0) == null);    try std.testing.expectEqual(        theme_monitor.QueryRequest.response_fence,        monitor.takeQueryRequest(1000).?,    );     var child: std.ArrayList(u8) = .empty;    defer child.deinit(alloc);    var composer: std.ArrayList(u8) = .empty;    defer composer.deinit(alloc);     const Driver = struct {        fn forwarded(            takeover_active: bool,            bytes: []const u8,            child_bytes: *std.ArrayList(u8),            composer_bytes: *std.ArrayList(u8),        ) !void {            if (takeover_active) {                try child_bytes.appendSlice(alloc, bytes);            } else {                try composer_bytes.appendSlice(alloc, bytes);            }        }         fn byte(            theme: *theme_monitor.Monitor,            takeover_active: bool,            input_byte: u8,            now_ms: i64,            child_bytes: *std.ArrayList(u8),            composer_bytes: *std.ArrayList(u8),        ) !void {            switch (terminalInputOwner(theme, false, takeover_active)) {                .paste => unreachable,                .takeover => try child_bytes.append(alloc, input_byte),                .fx_input => try composer_bytes.append(alloc, input_byte),                .theme_monitor => switch (theme.feed(input_byte, now_ms)) {                    .pending, .consumed => {},                    .forward => |bytes| try forwarded(                        takeover_active,                        bytes.slice(),                        child_bytes,                        composer_bytes,                    ),                },            }        }    };     const response = "\x1b[?1;2;4c";    for (response[0..3]) |byte| {        try Driver.byte(&monitor, true, byte, 1001, &child, &composer);    }    for (response[3..]) |byte| {        try Driver.byte(&monitor, true, byte, 1002, &child, &composer);    }    try std.testing.expectEqual(@as(usize, 0), child.items.len);    try std.testing.expectEqual(@as(usize, 0), composer.items.len);     for (response) |byte| {        try Driver.byte(&monitor, true, byte, 1003, &child, &composer);    }    try std.testing.expectEqualStrings(response, child.items);     const raw_input = "\x1b[Akey";    for (raw_input) |byte| {        try Driver.byte(&monitor, true, byte, 1004, &child, &composer);    }    try std.testing.expectEqualStrings(response ++ raw_input, child.items);    try std.testing.expectEqual(@as(usize, 0), composer.items.len);     try Driver.byte(&monitor, false, 0x1b, 1005, &child, &composer);    try Driver.byte(&monitor, true, '[', 1005, &child, &composer);    monitor.poll(1005 + theme_monitor.response_idle_timeout_ms);    while (monitor.takeDeferredByte()) |byte| {        try Driver.forwarded(true, &.{byte}, &child, &composer);        try std.testing.expect(monitor.consumeDeferredInputDispatch());    }    try std.testing.expectEqualStrings(response ++ raw_input ++ "\x1b[", child.items);    try std.testing.expectEqual(@as(usize, 0), composer.items.len);} test "terminal reply ownership precedes active paste transport" {    var monitor = theme_monitor.Monitor{};    monitor.start();     try std.testing.expect(monitor.takeQueryRequest(0) == null);    try std.testing.expectEqual(        theme_monitor.QueryRequest.response_fence,        monitor.takeQueryRequest(1000).?,    );    try std.testing.expectEqual(        TerminalInputOwner.theme_monitor,        terminalInputOwner(&monitor, true, false),    );     for ("\x1b[?1;2;4c") |byte| {        try std.testing.expect(monitor.feed(byte, 1001) != .forward);    }    try std.testing.expectEqual(        TerminalInputOwner.paste,        terminalInputOwner(&monitor, true, false),    );} test "approval bytes translate to typed decision actions" {    try std.testing.expectEqual(approval_decision.Action.deny, approvalActionFromByte(3).?);    try std.testing.expectEqual(approval_decision.Action.submit, approvalActionFromByte('\r').?);    try std.testing.expectEqual(approval_decision.Action.tab, approvalActionFromByte('\t').?);    try std.testing.expectEqual(approval_decision.Action.backspace, approvalActionFromByte(0x7F).?);    try std.testing.expectEqual(        approval_decision.Action{ .number = 2 },        approvalActionFromByte('3').?,    );    try std.testing.expectEqual(        approval_decision.Action{ .insert_ascii = 'x' },        approvalActionFromByte('x').?,    );    try std.testing.expect(approvalActionFromByte(0x80) == null);}

Lines 1–500 of 4966.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t