-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_builder_renderer.zig
More file actions
331 lines (272 loc) · 9.89 KB
/
agent_builder_renderer.zig
File metadata and controls
331 lines (272 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Agent Builder Renderer - Draws the agent builder UI to the terminal
const std = @import("std");
const agent_builder_state = @import("agent_builder_state");
const ui = @import("ui");
const AgentBuilderState = agent_builder_state.AgentBuilderState;
const AgentField = agent_builder_state.AgentField;
const FieldType = agent_builder_state.FieldType;
/// Render the agent builder screen
pub fn render(
state: *AgentBuilderState,
writer: anytype,
terminal_width: u16,
terminal_height: u16,
) !void {
// Clear screen
try writer.writeAll("\x1b[2J\x1b[H");
// Calculate layout dimensions
const box_width = @min(terminal_width - 4, 80); // Wider box for text areas
const box_start_x = (terminal_width - box_width) / 2;
var current_y: usize = 2;
// Draw title
try drawCentered(writer, "🤖 Agent Builder", terminal_width, current_y);
current_y += 1;
try drawCentered(writer, "Create a new agent with custom tools and behavior", terminal_width, current_y);
current_y += 1;
try drawCentered(writer, "Tab/Shift+Tab: navigate | Enter: edit | Space: toggle | Ctrl+S: save | Esc: cancel", terminal_width, current_y);
current_y += 2;
// Draw form box
try writer.print("\x1b[{d};{d}H┌", .{ current_y, box_start_x });
for (0..box_width - 2) |_| try writer.writeAll("─");
try writer.writeAll("┐");
current_y += 1;
// Draw each field
for (state.fields, 0..) |*field, idx| {
const is_focused = idx == state.focused_field_index;
switch (field.field_type) {
.text_input => {
try drawTextInputField(
writer,
field,
box_start_x,
¤t_y,
box_width,
is_focused,
);
},
.text_area => {
try drawTextAreaField(
writer,
field,
box_start_x,
¤t_y,
box_width,
terminal_height,
is_focused,
);
},
.tool_checkboxes => {
try drawToolCheckboxes(
writer,
field,
box_start_x,
¤t_y,
box_width,
terminal_height,
is_focused,
);
},
}
}
// Draw form box bottom
try writer.print("\x1b[{d};{d}H└", .{ current_y, box_start_x });
for (0..box_width - 2) |_| try writer.writeAll("─");
try writer.writeAll("┘");
current_y += 2;
// Show validation error if present
if (state.validation_error) |err_msg| {
try writer.print("\x1b[{d};{d}H", .{ current_y, box_start_x });
try writer.print("\x1b[31m⚠ {s}\x1b[0m", .{err_msg});
current_y += 1;
}
// Draw action buttons at bottom
current_y = terminal_height - 3;
try drawCentered(writer, "[Ctrl+S] Save Agent [Esc] Cancel", terminal_width, current_y);
// Show change indicator
if (state.has_changes) {
try writer.print("\x1b[{d};{d}H\x1b[33m● Unsaved changes\x1b[0m", .{ terminal_height - 1, box_start_x });
}
}
/// Draw centered text
fn drawCentered(writer: anytype, text: []const u8, terminal_width: u16, y: usize) !void {
const visible_len = text.len; // TODO: Strip ANSI for accurate centering
const start_x = if (terminal_width > visible_len)
(terminal_width - @as(u16, @intCast(visible_len))) / 2
else
0;
try writer.print("\x1b[{d};{d}H{s}", .{ y, start_x, text });
}
/// Draw text input field (single line)
fn drawTextInputField(
writer: anytype,
field: *const AgentField,
box_x: u16,
current_y: *usize,
box_width: u16,
is_focused: bool,
) !void {
// Label line
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
if (is_focused) {
try writer.writeAll("\x1b[7m"); // Reverse video
}
try writer.print("{s}: ", .{field.label});
// Value
if (field.edit_buffer) |*buffer| {
if (buffer.items.len > 0) {
const max_display = box_width - field.label.len - 8;
const display_text = if (buffer.items.len > max_display)
buffer.items[0..max_display]
else
buffer.items;
try writer.print("{s}", .{display_text});
// Show cursor if editing
if (is_focused and field.is_editing) {
try writer.writeAll("_");
}
} else if (is_focused and field.is_editing) {
try writer.writeAll("_");
}
}
if (is_focused) {
try writer.writeAll("\x1b[0m");
}
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
// Help text
if (field.help_text) |help| {
try writer.print("\x1b[{d};{d}H│ \x1b[2m{s}\x1b[0m", .{ current_y.*, box_x, help });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
}
current_y.* += 1; // Extra spacing
}
/// Draw text area field (multi-line)
fn drawTextAreaField(
writer: anytype,
field: *const AgentField,
box_x: u16,
current_y: *usize,
box_width: u16,
terminal_height: u16,
is_focused: bool,
) !void {
_ = terminal_height; // Reserved for scrolling
// Label line
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
if (is_focused) {
try writer.writeAll("\x1b[1;36m"); // Cyan bold
}
try writer.print("{s}:", .{field.label});
if (is_focused) {
try writer.writeAll("\x1b[0m");
}
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
// Content area (show first few lines)
const max_lines = 5;
var line_count: usize = 0;
if (field.edit_buffer) |*buffer| {
var line_iter = std.mem.splitScalar(u8, buffer.items, '\n');
while (line_iter.next()) |line| {
if (line_count >= max_lines) break;
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
if (is_focused) {
try writer.writeAll("\x1b[7m");
}
const max_display = box_width - 6;
const display_line = if (line.len > max_display)
line[0..max_display]
else
line;
try writer.print(" {s}", .{display_line});
if (is_focused) {
try writer.writeAll("\x1b[0m");
}
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
line_count += 1;
}
}
// Show placeholder if empty
if (line_count == 0) {
try writer.print("\x1b[{d};{d}H│ \x1b[2m (Enter to edit...)\x1b[0m", .{ current_y.*, box_x });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
line_count = 1;
}
// Pad remaining lines
while (line_count < max_lines) : (line_count += 1) {
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
}
// Help text
if (field.help_text) |help| {
try writer.print("\x1b[{d};{d}H│ \x1b[2m{s}\x1b[0m", .{ current_y.*, box_x, help });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
}
current_y.* += 1;
}
/// Draw tool checkboxes
fn drawToolCheckboxes(
writer: anytype,
field: *const AgentField,
box_x: u16,
current_y: *usize,
box_width: u16,
terminal_height: u16,
is_focused: bool,
) !void {
_ = terminal_height; // Reserved for scrolling
// Label line
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
if (is_focused) {
try writer.writeAll("\x1b[1;36m"); // Cyan bold
}
try writer.print("{s}:", .{field.label});
if (is_focused) {
try writer.writeAll("\x1b[0m");
}
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
// Help text
if (field.help_text) |help| {
try writer.print("\x1b[{d};{d}H│ \x1b[2m{s}\x1b[0m", .{ current_y.*, box_x, help });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
}
// Draw checkboxes (show max 15 at a time - covers typical tool count)
const max_visible = 15;
var shown: usize = 0;
if (field.tool_options) |tools| {
if (field.tool_selected) |selected| {
for (tools, selected, 0..) |tool_name, is_selected, idx| {
if (shown >= max_visible) break;
try writer.print("\x1b[{d};{d}H│ ", .{ current_y.*, box_x });
// Highlight current checkbox if focused and this is the selected one
const is_this_focused = is_focused and idx == field.checkbox_focus_index;
if (is_this_focused) {
try writer.writeAll("\x1b[7m"); // Reverse video
}
// Draw checkbox
const checkbox = if (is_selected) "[✓]" else "[ ]";
try writer.print(" {s} {s}", .{ checkbox, tool_name });
if (is_this_focused) {
try writer.writeAll("\x1b[0m");
}
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
shown += 1;
}
// Show "more tools" indicator if needed
if (tools.len > max_visible) {
try writer.print("\x1b[{d};{d}H│ \x1b[2m ... and {d} more tools (scroll down to see all)\x1b[0m", .{ current_y.*, box_x, tools.len - max_visible });
try writer.print("\x1b[{d}G│", .{box_x + box_width - 1});
current_y.* += 1;
}
}
}
current_y.* += 1;
}