Agent + conversation pickers, prefs persistence

- tray submenus: Agent (from letta agents list, radio-style) and
  Conversation (from local backend storage, CLI has no listing surface)
- conversation entries show preview: summary or last-message text
- conversation menu refreshes on aboutToShow (KDE DBus menu syncs a
  step behind eager mutation)
- CLI rule honored: --agent only with --conversation default; named
  conversations imply the agent
- plain-text fallback parsing for non-stream-json output
- empty-reply debug: raw stream tail dump
- prefs: ~/.local/inferon/prefs.json, saved on pick, loaded at startup,
  HOME-resolved paths (std.c.getenv), owned buffers (no alias double-free)
- leak fixes: agent name fallback, quit-path drains for menu entries

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta Code <noreply@letta.com>
This commit is contained in:
pierreandLetta Code committed 2026-09-01 23:15:58 +03:00
1 parent bcd48a3443
commit 2fdc812a13
2 files changed
+416 -9

No files matched your search

+208 -8
View File
@@ -1,4 +1,5 @@
const std = @import("std");
const Io = std.Io;
const LETTA_PATH = "/home/pierre/.bun/bin/letta";
@@ -61,13 +62,39 @@ pub fn streamInfer(
ctx: ?*anyopaque,
onEvent: *const fn (ctx: ?*anyopaque, ev: Event) void,
) ![]u8 {
return streamInferConv(io, allocator, agent, "default", prompt, ctx, onEvent);
}
pub fn streamInferConv(
io: std.Io,
allocator: std.mem.Allocator,
agent: []const u8,
conversation: []const u8,
prompt: []const u8,
ctx: ?*anyopaque,
onEvent: *const fn (ctx: ?*anyopaque, ev: Event) void,
) ![]u8 {
// CLI rule: --agent only pairs with --conversation default; for named
// conversations the agent is implied, so we omit it.
const is_default = std.mem.eql(u8, conversation, "default");
var argv: [11][]const u8 = undefined;
var ai: usize = 0;
argv[ai] = LETTA_PATH; ai += 1;
if (is_default) {
argv[ai] = "--agent"; ai += 1;
argv[ai] = agent; ai += 1;
}
argv[ai] = "-p"; ai += 1;
argv[ai] = prompt; ai += 1;
argv[ai] = "--conversation"; ai += 1;
argv[ai] = conversation; ai += 1;
argv[ai] = "--toolset"; ai += 1;
argv[ai] = "default"; ai += 1;
argv[ai] = "--output-format"; ai += 1;
argv[ai] = "stream-json"; ai += 1;
var child = try std.process.spawn(io, .{
.argv = &.{
LETTA_PATH, "--agent", agent,
"-p", prompt, "--conversation",
"default", "--toolset", "default",
"--output-format", "stream-json",
},
.argv = argv[0..ai],
.stdout = .pipe,
.stderr = .inherit,
});
@@ -75,6 +102,8 @@ pub fn streamInfer(
var reply: std.ArrayList(u8) = .empty;
errdefer reply.deinit(allocator);
var raw_tail: std.ArrayList(u8) = .empty; // last bytes, debug on empty reply
defer raw_tail.deinit(allocator);
var line_buf: [16 * 1024]u8 = undefined;
var line_len: usize = 0;
@@ -83,6 +112,12 @@ pub fn streamInfer(
while (true) {
const n = stdout_file.readStreaming(io, &.{&buf}) catch break;
if (n == 0) break;
raw_tail.appendSlice(allocator, buf[0..n]) catch {};
if (raw_tail.items.len > 4096) {
const excess = raw_tail.items.len - 4096;
std.mem.copyForwards(u8, raw_tail.items[0..4096], raw_tail.items[excess..]);
raw_tail.shrinkRetainingCapacity(4096);
}
for (buf[0..n]) |ch| {
if (ch == '\n') {
if (line_len > 0) {
@@ -97,6 +132,9 @@ pub fn streamInfer(
}
_ = child.wait(io) catch {};
if (reply.items.len == 0 and raw_tail.items.len > 0) {
std.debug.print("inferon/letta: EMPTY reply, raw stream tail:\n{s}\n", .{raw_tail.items});
}
return reply.toOwnedSlice(allocator);
}
@@ -109,12 +147,21 @@ fn handleLine(
reply: *std.ArrayList(u8),
) void {
const mt = dupeStr(allocator, line, "message_type") orelse {
// Non-message line; the "result" line holds the full text as backup.
// The "result" line holds the full text as backup.
if (std.mem.indexOf(u8, line, "\"type\":\"result\"") != null and reply.items.len == 0) {
if (dupeStr(allocator, line, "result")) |r| {
defer allocator.free(r);
reply.appendSlice(allocator, r) catch {};
}
return;
}
// Conversation-mode (no --agent) ignores --output-format and emits
// PLAIN TEXT — treat non-JSON lines as the reply itself.
if (line.len > 0 and line[0] != '{') {
reply.appendSlice(allocator, line) catch {};
reply.append(allocator, ' ') catch {};
const dup = allocator.dupe(u8, line) catch return;
onEvent(ctx, .{ .chunk = dup });
}
return;
};
@@ -144,7 +191,7 @@ fn handleLine(
}
/// Extract "key":"value" (with escape handling), allocated with `allocator`.
fn dupeStr(allocator: std.mem.Allocator, line: []const u8, key: []const u8) ?[]u8 {
pub fn dupeStr(allocator: std.mem.Allocator, line: []const u8, key: []const u8) ?[]u8 {
var pat_buf: [64]u8 = undefined;
const pat = std.fmt.bufPrint(&pat_buf, "\"{s}\":\"", .{key}) catch return null;
const start = std.mem.indexOf(u8, line, pat) orelse return null;
@@ -168,3 +215,156 @@ fn dupeStr(allocator: std.mem.Allocator, line: []const u8, key: []const u8) ?[]u
}
return out.toOwnedSlice(allocator) catch null;
}
// -------------------------------------------------------------- listings ---
pub const Agent = struct { id: []u8, name: []u8 }; // caller frees both
fn readAllStdout(io: std.Io, alloc: std.mem.Allocator, argv: []const []const u8) ![]u8 {
var child = try std.process.spawn(io, .{
.argv = argv,
.stdin = .ignore,
.stdout = .pipe,
.stderr = .ignore,
});
const out = child.stdout.?;
var buf: std.ArrayList(u8) = .empty;
errdefer buf.deinit(alloc);
var tmp: [16 * 1024]u8 = undefined;
while (true) {
const n = out.readStreaming(io, &.{&tmp}) catch break;
if (n == 0) break;
try buf.appendSlice(alloc, tmp[0..n]);
}
_ = child.wait(io) catch {};
return buf.toOwnedSlice(alloc);
}
/// Parse `letta agents list` JSON into id/name pairs.
pub fn listAgents(io: std.Io, alloc: std.mem.Allocator) ![]Agent {
const json = try readAllStdout(io, alloc, &.{ LETTA_PATH, "agents", "list" });
defer alloc.free(json);
var out: std.ArrayList(Agent) = .empty;
errdefer out.deinit(alloc);
var i: usize = 0;
while (jsonStr(alloc, json, i, "id")) |r| {
defer alloc.free(r.val);
if (!std.mem.startsWith(u8, r.val, "agent-")) {
i = r.end;
continue;
}
const id = try alloc.dupe(u8, r.val);
errdefer alloc.free(id);
// name is the next field after id in each item — no fallback leak.
const name: []u8 = if (jsonStr(alloc, json, r.end, "name")) |nr| nr.val else try alloc.dupe(u8, "?");
try out.append(alloc, .{ .id = id, .name = name });
i = r.end;
}
return out.toOwnedSlice(alloc);
}
pub const Convo = struct { id: []u8, preview: []u8 }; // caller frees both
/// Conversations for an agent, straight from the local backend's storage
/// (the CLI has no list-conversations surface; messages list only exports
/// "default"). Reads <backend>/conversations/*/conversation.json.
var conv_dir_buf: [512]u8 = undefined;
pub var CONVERSATIONS_DIR: []const u8 = "";
pub fn initConversationsDir() void {
const home = std.c.getenv("HOME") orelse return;
CONVERSATIONS_DIR = std.fmt.bufPrint(&conv_dir_buf, "{s}/.letta/lc-local-backend/conversations", .{std.mem.span(home)}) catch return;
}
pub fn listConversations(io: std.Io, alloc: std.mem.Allocator, agent: []const u8) ![]Convo {
var out: std.ArrayList(Convo) = .empty;
errdefer out.deinit(alloc);
try out.append(alloc, .{ .id = try alloc.dupe(u8, "default"), .preview = try alloc.dupe(u8, "") });
var dir = try Io.Dir.openDirAbsolute(io, CONVERSATIONS_DIR, .{ .iterate = true });
defer dir.close(io);
var it = dir.iterate();
while (try it.next(io)) |entry| {
if (entry.kind != .directory) continue;
var buf: [4096]u8 = undefined;
const sub = try std.fmt.bufPrint(&buf, "{s}/conversation.json", .{entry.name});
const json = dir.readFileAlloc(io, sub, alloc, .limited(64 * 1024)) catch continue;
defer alloc.free(json);
const aid = jsonStr(alloc, json, 0, "agent_id") orelse continue;
defer alloc.free(aid.val);
if (!std.mem.eql(u8, aid.val, agent)) continue;
// skip archived
if (std.mem.indexOf(u8, json, "\"archived\": true") != null) continue;
const cid = jsonStr(alloc, json, 0, "id") orelse continue;
var dup = false;
for (out.items) |existing| {
if (std.mem.eql(u8, existing.id, cid.val)) dup = true;
}
if (dup) {
alloc.free(cid.val);
continue;
}
// Preview: summary if set, else the last message text.
var preview: []u8 = try alloc.dupe(u8, "");
if (jsonStr(alloc, json, 0, "summary")) |s| {
if (s.val.len > 0) {
alloc.free(preview);
preview = s.val;
} else {
alloc.free(s.val);
}
}
if (preview.len == 0) {
var mbuf: [4096]u8 = undefined;
const msub = try std.fmt.bufPrint(&mbuf, "{s}/messages.jsonl", .{entry.name});
const msgs = dir.readFileAlloc(io, msub, alloc, .limited(16 * 1024 * 1024)) catch null;
if (msgs) |m| {
defer alloc.free(m);
if (std.mem.lastIndexOfScalar(u8, m, '\n')) |nl| {
const last = m[nl + 1 ..];
if (jsonStr(alloc, last, 0, "text")) |t| {
alloc.free(preview);
preview = t.val;
}
}
}
}
try out.append(alloc, .{ .id = cid.val, .preview = preview });
}
return out.toOwnedSlice(alloc);
}
/// Whitespace-tolerant JSON string-field extractor: finds "key" (with or
/// without space after the colon), returns the unescaped value.
pub fn jsonStr(alloc: std.mem.Allocator, json: []const u8, from: usize, key: []const u8) ?struct { val: []u8, end: usize } {
var pat_buf: [64]u8 = undefined;
const pat = std.fmt.bufPrint(&pat_buf, "\"{s}\"", .{key}) catch return null;
const ks = std.mem.indexOfPos(u8, json, from, pat) orelse return null;
var i = ks + pat.len;
while (i < json.len and (json[i] == ' ' or json[i] == ':')) i += 1;
if (i >= json.len or json[i] != '"') return null;
i += 1;
var out: std.ArrayList(u8) = .empty;
while (i < json.len and json[i] != '"') {
if (json[i] == '\\' and i + 1 < json.len) {
const esc: u8 = switch (json[i + 1]) {
'n' => '\n',
't' => '\t',
else => json[i + 1],
};
out.append(alloc, esc) catch return null;
i += 2;
} else {
out.append(alloc, json[i]) catch return null;
i += 1;
}
}
if (i >= json.len) return null;
return .{ .val = out.toOwnedSlice(alloc) catch return null, .end = i + 1 };
}