xonecas.io
79113
Ma

mini-coding-agent / commit 52b3907

fix: Openrouter identification and small fixes

xonecas2026-10-27 52b3907deb49962701781b5208cefb55265f2e5e parent 23b0399

10 files changed

README.md+2−2
@@ -167,7 +167,7 @@ Keys:
167167
168168 | Key | Default | Meaning |
169169 |---|---|---|
170-| `provider` | unset | provider id; built-ins are `opencode` and `opencode-go`, or a `customProviders` id |
170+| `provider` | unset | provider id; built-ins are `opencode`, `opencode-go` and `openrouter`, or a `customProviders` id |
171171 | `model` | unset | model id |
172172 | `thinkingEffort` | unset | one of `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, `max`; unset sends no reasoning parameter; a level the model does not accept clamps up to the next accepted one, else down |
173173 | `showImages` | `false` | draw the images the `read` tool returns inline using the kitty graphics protocol; requires kitty 0.28+ |
@@ -226,7 +226,7 @@ so one provider may mix APIs and base URLs:
226226 The built-in providers are named, keyed and described by the embedded models.dev
227227 document. `opencode` and `opencode-go` read the first present variable in their
228228 `env` list (`OPENCODE_API_KEY`), and send the session id as
229-`x-opencode-session`.
229+`x-opencode-session`. `openrouter` reads `OPENROUTER_API_KEY`.
230230
231231 Each wire carries its own key header:
232232
src/http.zig+17−0
@@ -5,6 +5,20 @@ const build_options = @import("build_options");
55
66 const user_agent = "mini-coding-agent/" ++ build_options.version;
77
8+/// OpenRouter only creates an app page from a `HTTP-Referer`; the title and
9+/// user agent alone leave usage unattributed.
10+const openrouter_headers = [_]std.http.Header{
11+ .{ .name = "HTTP-Referer", .value = "https://xonecas.io/mini-coding-agent/" },
12+ .{ .name = "X-OpenRouter-Title", .value = "mini-coding-agent" },
13+};
14+
15+fn isOpenRouter(uri: std.Uri) bool {
16+ const host = switch (uri.host orelse return false) {
17+ inline else => |h| h,
18+ };
19+ return std.mem.eql(u8, host, "openrouter.ai") or std.mem.endsWith(u8, host, ".openrouter.ai");
20+}
21+
822 pub const Header = struct { name: []const u8, value: []const u8 };
923
1024 pub const SseHandler = struct {
@@ -99,6 +113,9 @@ const Exchange = struct {
99113 self.client = .{ .allocator = a, .io = platform.io };
100114 errdefer self.client.deinit();
101115 for (headers) |h| self.hdrs.append(a, .{ .name = h.name, .value = h.value }) catch return error.OutOfMemory;
116+ if (isOpenRouter(uri)) {
117+ for (openrouter_headers) |h| self.hdrs.append(a, h) catch return error.OutOfMemory;
118+ }
102119 errdefer self.hdrs.deinit(a);
103120 self.req = self.client.request(method, uri, .{
104121 .redirect_behavior = .unhandled,
src/models.zig+1−0
@@ -35,6 +35,7 @@ const Builtin = struct {
3535 const builtins = [_]Builtin{
3636 .{ .id = "opencode", .session_header = "x-opencode-session" },
3737 .{ .id = "opencode-go", .session_header = "x-opencode-session" },
38+ .{ .id = "openrouter" },
3839 };
3940
4041 const ladder = [_][]const u8{ "off", "minimal", "low", "medium", "high", "xhigh", "max" };
src/models_dev.json+1−1
@@ -1 +1 @@
1-{"opencode":{"id":"opencode","env":["OPENCODE_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://opencode.ai/inference/openai/v1","name":"OpenCode","doc":"https://opencode.ai/v2/docs/console/models/","models":{"claude-fable-5":{"id":"claude-fable-5","name":"Claude Fable 5","description":"Claude model for creative writing, analysis, and controlled agent workflows","family":"claude-fable","attachment":true,"reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","hig …
1+{"opencode":{"id":"opencode","env":["OPENCODE_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://opencode.ai/inference/openai/v1","name":"OpenCode","doc":"https://opencode.ai/v2/docs/console/models/","models":{"gpt-6-sol":{"id":"gpt-6-sol","name":"GPT-6 Sol","description":"OpenAI model for complex coding and agentic workflows","family":"gpt-sol","attachment":true,"reasoning":true,"reasoning_options":[{"type":"effort","values":["none","low","medium","high","xhigh","max"]}],"tool_call":tru …
src/models_dev.zig+1−0
@@ -26,6 +26,7 @@ const wires = [_][2][]const u8{
2626 .{ "@ai-sdk/openai", "openai-responses" },
2727 .{ "@ai-sdk/anthropic", "anthropic-messages" },
2828 .{ "@ai-sdk/google", "google-generative-ai" },
29+ .{ "@openrouter/ai-sdk-provider", "openai-completions" },
2930 };
3031
3132 fn wire(npm: []const u8) ?[]const u8 {
src/time.zig+10−0
@@ -38,6 +38,16 @@ fn isoFromMs(buf: []u8, ms: i64) []const u8 {
3838 }) catch unreachable;
3939 }
4040
41+pub fn humanize(buf: []u8, ms: i64) []const u8 {
42+ const total = @max(0, @divFloor(ms, 1000));
43+ const s = @mod(total, 60);
44+ const m = @mod(@divFloor(total, 60), 60);
45+ const h = @divFloor(total, 3600);
46+ if (h > 0) return std.fmt.bufPrint(buf, "{d}h {d}m {d}s", .{ h, m, s }) catch "";
47+ if (m > 0) return std.fmt.bufPrint(buf, "{d}m {d}s", .{ m, s }) catch "";
48+ return std.fmt.bufPrint(buf, "{d}s", .{s}) catch "";
49+}
50+
4151 pub fn isoAlloc(a: std.mem.Allocator) []const u8 {
4252 const buf = a.alloc(u8, 32) catch unreachable;
4353 return isoFromMs(buf, nowMs());
src/tools/bash.zig+2−1
@@ -15,7 +15,8 @@ const description_fmt =
1515 "The command runs with `bash -c` starting in {s}, your working directory. Use it for " ++
1616 "builds, tests, git, package managers, and other file work. The output ends " ++
1717 "with an `exit code:` line, or `killed: timed out after Ns` if it hits the time limit. " ++
18- "Pass `timeout` (seconds) to change the 120-second limit.";
18+ "Pass `timeout` (seconds) to change the 120-second limit." ++
19+ "Do not repeatedly `cd` into the working directory, it's wasteful.";
1920
2021 const params = [_]tools.Param{
2122 .{ .name = "command", .kind = .string, .description = "Bash command line to execute." },
src/tui/tui.zig+22−3
@@ -106,6 +106,7 @@ const Tui = struct {
106106 pending_calls: std.ArrayList(PendingCall) = .empty,
107107 streamed: std.ArrayList(u8) = .empty,
108108 turn_start: i64 = 0,
109+ turn_msgs: usize = 0,
109110 ctx_msgs: usize = std.math.maxInt(usize),
110111 ctx_used: u64 = 0,
111112 ctx_cache: message_mod.Usage = .{},
@@ -252,12 +253,13 @@ const Tui = struct {
252253 return std.fmt.allocPrint(self.s, "{s}{s}{s}", .{ styles.err(self.s, "failed - Enter to retry"), sep, info }) catch info;
253254 }
254255 if (!self.active or self.phase == .idle) return info;
255- const elapsed = @max(0, @divFloor(time.nowMs() - self.turn_start, 1000));
256+ var elapsed_buf: [32]u8 = undefined;
257+ const elapsed = time.humanize(&elapsed_buf, time.nowMs() - self.turn_start);
256258 return std.fmt.allocPrint(self.s, "{s} {s}{s}{s}{s}{s}", .{
257259 styles.accent(self.s, spinnerChar(self.frame)),
258260 self.stateLabel(),
259261 sep,
260- styles.comment(self.s, std.fmt.allocPrint(self.s, "{d}s", .{elapsed}) catch ""),
262+ styles.comment(self.s, elapsed),
261263 sep,
262264 info,
263265 }) catch info;
@@ -468,12 +470,26 @@ const Tui = struct {
468470 .complete => {
469471 status.report("done", null, null);
470472 status.notify("mini: turn complete");
471- self.endTurn(styles.comment(self.s, std.fmt.allocPrint(self.s, "[complete · {d}s]", .{@max(0, @divFloor(time.nowMs() - self.turn_start, 1000))}) catch "[complete]"));
473+ self.endTurn(styles.comment(self.s, self.doneLine()));
472474 },
473475 }
474476 self.dirty = true;
475477 }
476478
479+ fn doneLine(self: *Tui) []const u8 {
480+ var elapsed_buf: [32]u8 = undefined;
481+ const secs = time.humanize(&elapsed_buf, time.nowMs() - self.turn_start);
482+ self.messages_mutex.lockUncancelable(platform.io);
483+ const u = usage_mod.turnUsage(self.messages.items, self.turn_msgs);
484+ self.messages_mutex.unlock(platform.io);
485+ const prompt = u.input + u.cache_read + u.cache_write;
486+ return std.fmt.allocPrint(self.s, "done · {s} · {s} in · {s} out", .{
487+ secs,
488+ usage_mod.formatTokens(self.s, prompt),
489+ usage_mod.formatTokens(self.s, u.output),
490+ }) catch "done";
491+ }
492+
477493 fn endTurn(self: *Tui, line: []const u8) void {
478494 self.phase = .idle;
479495 self.keepName(&self.detail, null);
@@ -746,6 +762,9 @@ const Tui = struct {
746762 self.keepName(&self.detail, null);
747763 self.keepName(&self.retrying, null);
748764 self.turn_start = time.nowMs();
765+ self.messages_mutex.lockUncancelable(platform.io);
766+ self.turn_msgs = self.messages.items.len;
767+ self.messages_mutex.unlock(platform.io);
749768 self.frame = 0;
750769 self.event_mutex.lockUncancelable(platform.io);
751770 self.events.clearRetainingCapacity();
src/tui/usage.zig+14−0
@@ -77,6 +77,20 @@ pub fn lastUsage(messages: []const message_mod.Message) message_mod.Usage {
7777 return out;
7878 }
7979
80+pub fn turnUsage(messages: []const message_mod.Message, from: usize) message_mod.Usage {
81+ var out: message_mod.Usage = .{};
82+ for (messages[@min(from, messages.len)..]) |m| {
83+ if (m != .assistant) continue;
84+ const am = m.assistant;
85+ if (am.stop_reason == .aborted or am.stop_reason == .err) continue;
86+ out.input += am.usage.input;
87+ out.output += am.usage.output;
88+ out.cache_read += am.usage.cache_read;
89+ out.cache_write += am.usage.cache_write;
90+ }
91+ return out;
92+}
93+
8094 pub fn cacheLine(a: std.mem.Allocator, usage: message_mod.Usage) []const u8 {
8195 const prompt = usage.input + usage.cache_read + usage.cache_write;
8296 const pct: u64 = if (prompt == 0)
tools/gen_models_dev.py+1−1
@@ -13,7 +13,7 @@ import urllib.request
1313
1414 MODELS_DEV = "https://models.dev/api.json"
1515
16-PROVIDERS = ["opencode", "opencode-go"]
16+PROVIDERS = ["opencode", "opencode-go", "openrouter"]
1717
1818
1919 def main():