Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165//! The usage part of the footer: the session's tokens and cost, and how full//! the context is.
const std = @import("std");const vaxis = @import("vaxis");
const Usage = @import("../protocol.zig").core.Usage;const UsageReport = @import("../protocol.zig").core.UsageReport;
const warning: vaxis.Style = .{ .fg = .{ .index = 3 } };const danger: vaxis.Style = .{ .fg = .{ .index = 1 } };
/// Writes a token count in at most four characters up to 999M, such as/// `999`, `1.2k`, `12k` or `1M`. Rounds down.pub fn writeTokens(writer: *std.Io.Writer, count: u64) std.Io.Writer.Error!void { if (count < 1000) { return writer.print("{d}", .{count}); } const unit: u64, const suffix: u8 = if (count < 1_000_000) .{ 1000, 'k' } else .{ 1_000_000, 'M' }; const whole = count / unit; const tenth = count % unit * 10 / unit; if (whole < 10 and tenth != 0) { return writer.print("{d}.{d}{c}", .{ whole, tenth, suffix }); } return writer.print("{d}{c}", .{ whole, suffix });}
/// The session totals, such as `↑12k ↓1.2k R8.3k $0.012`. Leaves out a count/// of zero and an unknown cost. Empty when nothing is left.pub fn totals(arena: std.mem.Allocator, report: UsageReport) std.mem.Allocator.Error![]const u8 { var out: std.Io.Writer.Allocating = .init(arena); write(&out.writer, report) catch return error.OutOfMemory; return out.written();}
fn write(writer: *std.Io.Writer, report: UsageReport) std.Io.Writer.Error!void { const fields = [_]struct { []const u8, u64 }{ .{ "↑", report.session.input }, .{ "↓", report.session.output }, .{ "R", report.session.cache_read }, .{ "W", report.session.cache_write }, }; var first = true; for (fields) |field| { if (field[1] == 0) { continue; } if (!first) { try writer.writeByte(' '); } first = false; try writer.writeAll(field[0]); try writeTokens(writer, field[1]); } if (report.session_cost) |cost| { if (!first) { try writer.writeByte(' '); } try writer.print("${d:.3}", .{cost}); }}
pub const Context = struct { text: []const u8, style: vaxis.Style,};
/// How full the context is, such as `4.3%/1M`, in a color that warns when it/// is nearly full. Null when the size of the context is unknown.pub fn context(arena: std.mem.Allocator, report: UsageReport) std.mem.Allocator.Error!?Context { if (report.context_window == 0) { return null; } const percent = @as(f64, @floatFromInt(report.context_tokens)) * 100 / @as(f64, @floatFromInt(report.context_window)); var out: std.Io.Writer.Allocating = .init(arena); out.writer.print("{d:.1}%/", .{percent}) catch return error.OutOfMemory; writeTokens(&out.writer, report.context_window) catch return error.OutOfMemory; return .{ .text = out.written(), .style = if (percent > 90) danger else if (percent > 70) warning else .{}, };}
const testing = std.testing;
fn expectTokens(expected: []const u8, count: u64) !void { var buffer: [32]u8 = undefined; var writer: std.Io.Writer = .fixed(&buffer); try writeTokens(&writer, count); try testing.expectEqualStrings(expected, writer.buffered());}
test "a token count is short and rounds down" { try expectTokens("0", 0); try expectTokens("999", 999); try expectTokens("1k", 1000); try expectTokens("1.2k", 1200); try expectTokens("9.9k", 9999); try expectTokens("10k", 10000); try expectTokens("12k", 12_999); try expectTokens("999k", 999_999); try expectTokens("1M", 1_000_000); try expectTokens("1M", 1_048_576); try expectTokens("1.2M", 1_250_000); try expectTokens("12M", 12_345_678);}
fn sample(session: Usage, cost: ?f64, context_tokens: u64, window: u32) UsageReport { return .{ .request = .{}, .request_cost = cost, .session = session, .session_cost = cost, .context_tokens = context_tokens, .context_window = window, };}
test "the totals show each count that is not zero, then the cost" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator();
try testing.expectEqualStrings( "↑12k ↓1.2k R8.3k $0.012", try totals(arena, sample(.{ .input = 12_000, .output = 1_200, .cache_read = 8_300 }, 0.0123, 0, 0)), ); try testing.expectEqualStrings( "↑5 ↓7 W2k $1.500", try totals(arena, sample(.{ .input = 5, .output = 7, .cache_write = 2000 }, 1.5, 0, 0)), );}
test "the totals of a model without a price show no cost" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator();
try testing.expectEqualStrings("↑12k ↓1.2k", try totals(arena, sample(.{ .input = 12_000, .output = 1_200 }, null, 0, 0))); try testing.expectEqualStrings("", try totals(arena, sample(.{}, null, 0, 0)));}
test "the context shows the percent of the window, and nothing for an unknown window" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator();
const shown = (try context(arena, sample(.{}, null, 45_000, 1_048_576))).?; try testing.expectEqualStrings("4.3%/1M", shown.text); try testing.expectEqual(vaxis.Style{}, shown.style); try testing.expectEqual(null, try context(arena, sample(.{}, null, 45_000, 0)));}
test "the context warns above 70 percent and more above 90 percent" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator();
try testing.expectEqual(vaxis.Style{}, (try context(arena, sample(.{}, null, 700, 1000))).?.style); try testing.expectEqual(warning, (try context(arena, sample(.{}, null, 701, 1000))).?.style); try testing.expectEqual(warning, (try context(arena, sample(.{}, null, 900, 1000))).?.style); try testing.expectEqual(danger, (try context(arena, sample(.{}, null, 901, 1000))).?.style); try testing.expectEqualStrings("90.1%/1k", (try context(arena, sample(.{}, null, 901, 1000))).?.text);}