Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f634027
feat(mobile): browse & install community AI providers
ddutchie Aug 1, 2026
abfa8ee
feat(ai): provider credit balances and per-call cost visibility
ddutchie Aug 2, 2026
424d3ce
fix(mobile): address PR review — registry fetch guards, install dedup…
ddutchie Aug 2, 2026
8edd856
fix(ci): unused test imports + live-check skip when sibling repo absent
ddutchie Aug 2, 2026
b741548
fix(ai): PR review — costUsd optional/validated, subagent cost, tiny-…
ddutchie Aug 2, 2026
359f805
feat(ai): provider balance in the chat usage ring + portal chat-setti…
ddutchie Aug 2, 2026
3d4010e
feat(ai): default model is now gpt-5.6-luna (desktop + mobile)
ddutchie Aug 2, 2026
7113cb1
fix(ai): address PR review — ring balance cache keys, shared formatte…
ddutchie Aug 2, 2026
4139f6c
feat(ai): per-model cost, provider logos & image gating from models.dev
ddutchie Aug 2, 2026
e856132
feat(ai): show model input-capability chips (T/I/PDF/V/A) in pickers
ddutchie Aug 2, 2026
4e4b428
fix(ai): crash on legacy catalog cache + icon capability chips
ddutchie Aug 2, 2026
b8ee5cb
feat(ai): attach PDFs to chat messages (document parts + rasterized f…
ddutchie Aug 2, 2026
cee5385
fix(ai): PR review — negative-cost formatting + catalog-aware image g…
ddutchie Aug 2, 2026
61a9704
feat(ai, mobile): model dropdown + auto-fetch model list like desktop
ddutchie Aug 2, 2026
3cb6ef1
feat(ai, mobile): favorite models in the model dropdown like desktop
ddutchie Aug 2, 2026
90f0fa8
fix(ai): resolve model logos from models.dev canonical owner
ddutchie Aug 2, 2026
e9a68f5
feat(settings): brand marks on saved providers + URL fallback match
ddutchie Aug 2, 2026
5024ddd
feat(settings): fall back to models.dev logos for direct-vendor endpo…
ddutchie Aug 2, 2026
17ff3fb
fix(mobile+desktop): render models.dev logos through ConnectorLogo chip
ddutchie Aug 2, 2026
2f0bef1
feat(mobile): edit provider on demand + hide Browse in Apple/Rork mode
ddutchie Aug 2, 2026
57fc792
fix: address PR #108 review comments
ddutchie Aug 2, 2026
59a66db
fix(lint): use const for logoSvgSubscribers (never reassigned)
ddutchie Aug 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelogs/v2.5.20.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Features

- **Remaining-credit badges now work beyond OpenRouter.** The settings credit lookup previously probed a single OpenRouter-style `GET {base}/v1/key` endpoint, so only OpenRouter showed a balance. Community providers can now advertise a `credits` descriptor (a dedicated HTTPS endpoint + response shape) in the `providers.json` catalog, and the main process looks that up for the active endpoint and parses the reply accordingly. DeepSeek (via `GET /user/balance`, with its USD/CNY balance shown in the right currency) and OpenAI (via the legacy `credit_grants` endpoint) ship with descriptors; any provider that doesn't expose a balance API still gracefully hides the display.
- **Neuralwatt now shows a live balance too.** Its catalog entry gained a `credits` descriptor pointing at `GET /v1/quota`, and the parser reads the energy account's `credits_remaining_usd` / `total_credits_usd` / `credits_used_usd`. The credit-parsing logic moved into a small shared module, and the `cairn-community` repo gained a live endpoint test (`npm run test:credits`) that exercises every provider's balance endpoint against real keys (set them in `.env.test`).
- **Per-turn cost in the context ring.** Providers that report the USD cost of a call in their usage (`usage.cost` — Neuralwatt's energy accounting does) now surface that figure in the context-ring popover, alongside the token breakdown, so you can see what each message cost.
- **Account balance in the context ring.** The ring's popover now also shows a **Balance** row with your remaining credits for the active provider (`$5.42 of $45.5`, or "Free tier" for no-cost providers), refreshed at most once a minute so repeated ring instances share a single probe. It hides when the provider exposes no balance or you're offline, and is skipped on per-subagent rings (it's account-level, not per-turn).

## Fixes

- **Chat settings no longer clip in narrow windows.** The in-chat quick-settings dropdown used inline positioning inside the chat panel's overflow container, so in a narrow window (or a docked panel) it could get cut off at the panel edge. It's now a portaled popover, so it always floats above the window edge.

55 changes: 18 additions & 37 deletions electron/ipc/ai-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { generatePrd } from "../lib/prd";
import { callLLM, isLocalEndpoint, normaliseBaseUrl, buildApiUrl, type LLMConfig } from "../lib/llm";
import { getCachedConfig, cacheLlmConnection } from "../lib/config-cache";
import { resolveLlmApiKey } from "../lib/secure-store";
import { fetchProvidersManifest } from "../lib/community-registry";
import { resolveCreditSpec, probeCredits } from "../lib/provider-credits";

function resolveConfig(
config: { baseUrl?: string; model?: string; apiKey?: string } | undefined,
Expand Down Expand Up @@ -131,54 +133,33 @@ export function registerAiHandlers(ctx: DbContext): void {

// ── Provider credits / key info ──────────────────
// Best-effort lookup of remaining credits for providers that expose it.
// OpenRouter returns account/key balance at GET {base}/v1/key as
// Default probe: OpenRouter-style GET {base}/v1/key returning
// { data: { limit, limit_remaining, usage, is_free_tier } } (credits are USD).
// Returns null when the provider doesn't implement it (any non-2xx / parse
// failure) so the caller can simply hide the display — never throws.
// Providers that expose balance off the chat base (DeepSeek, OpenAI's legacy
// credit_grants endpoint) advertise a `credits` descriptor in the community
// providers.json manifest — the handler looks that up by endpoint, then parses
// the response per its `shape`. Returns null when the provider doesn't expose
// credits (any non-2xx / parse failure) so the caller can hide the display —
// never throws.
registerIpcHandle(
"ai:fetchKeyInfo",
async (_e, args: { baseUrl?: string; apiKey?: string }) =>
handle(async () => {
const url = normaliseBaseUrl(args.baseUrl || "https://api.openai.com");
const baseUrl = normaliseBaseUrl(args.baseUrl || "https://api.openai.com");
const realKey = resolveLlmApiKey(args.apiKey);
// No key → nothing to query (credit endpoints are all authed).
if (!realKey) return null;
const ac = new AbortController();
const timer = setTimeout(() => ac.abort(), 12_000);
try {
const res = await fetch(buildApiUrl(url, "key"), {
headers: { Authorization: `Bearer ${realKey}` },
signal: ac.signal,
});
if (!res.ok) return null;
const json = (await res.json()) as {
data?: {
limit?: number | null;
limit_remaining?: number | null;
usage?: number | null;
is_free_tier?: boolean;
};
};
const d = json?.data;
if (!d || typeof d !== "object") return null;
const usage = typeof d.usage === "number" ? d.usage : null;
const limit = typeof d.limit === "number" ? d.limit : null;
// Prefer the explicit remaining figure; else derive from limit - usage.
const remaining =
typeof d.limit_remaining === "number"
? d.limit_remaining
: limit != null && usage != null
? limit - usage
: null;
// Nothing usable to show.
if (remaining == null && usage == null && limit == null) return null;
return {
remaining,
usage,
limit,
isFreeTier: d.is_free_tier ?? null,
currency: "USD" as const,
};
const { manifest } = await fetchProvidersManifest();
const credits = resolveCreditSpec(baseUrl, manifest.providers);
// Explicit descriptor wins; otherwise fall back to the OpenRouter-style
// /v1/key probe (harmless for providers without one — returns null).
const url = credits?.url ?? buildApiUrl(baseUrl, "key");
const shape = credits?.shape ?? "openrouter";
const probe = await probeCredits(url, realKey, shape, ac.signal);
return probe.info;
} catch {
// Endpoint absent / offline / aborted — treat as "no credits info".
return null;
Expand Down
31 changes: 26 additions & 5 deletions electron/ipc/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function registerChatHandler(db: Database.Database, workspacePath: string
let promptTokens = 0;
let completionTokens = 0;
let reasoningTokens = 0;
let costUsd: number | undefined = undefined;
let lastBreakdown: TokenBreakdown | undefined = undefined;

// Assemble external tool defs (MCP servers + custom services) in scope for
Expand All @@ -186,19 +187,39 @@ export function registerChatHandler(db: Database.Database, workspacePath: string
}
const allTools = externalDefs.length > 0 ? [...TOOLS, ...externalDefs] : TOOLS;

const addUsage = (pt: number, ct: number, rt?: number) => {
const addUsage = (pt: number, ct: number, rt?: number, cost?: number) => {
promptTokens = pt;
completionTokens += ct;
if (typeof rt === "number") reasoningTokens += rt;
// Provider-reported USD cost of this call (e.g. Neuralwatt usage.cost);
// accumulated across tool-loop rounds like completion tokens. Only
// non-negative finite values are accepted; an explicitly reported 0 is
// preserved, and nothing is set when the provider reports no cost.
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) {
costUsd = (costUsd ?? 0) + cost;
}
try {
const rawBreakdown = calculatePromptBreakdown(buildSystemPrompt(req), messages, allTools);
lastBreakdown = scaleBreakdown(rawBreakdown, promptTokens);
} catch (err) {
console.error("[chat] failed to calculate breakdown:", err);
}
send("chat:usage", { promptTokens, completionTokens, reasoningTokens, breakdown: lastBreakdown });
send("chat:usage", { promptTokens, completionTokens, reasoningTokens, breakdown: lastBreakdown, costUsd });
};

// Final usage object attached to chat:done (or undefined when nothing ran —
// a cost-only report is still retained, not dropped for having no tokens).
const finalUsage = () =>
promptTokens > 0 || costUsd != null
? {
promptTokens,
completionTokens,
reasoningTokens,
breakdown: lastBreakdown,
costUsd: costUsd != null ? costUsd : undefined,
}
: undefined;

// ── Subagent mode ─────────────────────────────────────────────────────────
// Per-thread toggle routes the turn through the dispatch → research/write
// loop. It streams a live, expandable subagent trace to the renderer via
Expand Down Expand Up @@ -243,7 +264,7 @@ export function registerChatHandler(db: Database.Database, workspacePath: string
content: abortCtrl.signal.aborted ? "" : dispatchResult.content,
reasoning: dispatchResult.reasoning,
contextRefs: [],
usage: promptTokens > 0 ? { promptTokens, completionTokens, reasoningTokens, breakdown: lastBreakdown } : undefined,
usage: finalUsage(),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});
} catch (err) {
// Cancellation or an unexpected failure must never leave the input
Expand Down Expand Up @@ -284,10 +305,10 @@ export function registerChatHandler(db: Database.Database, workspacePath: string
}

if (abortCtrl.signal.aborted) {
send("chat:done", { content: "", reasoning: loopResult.reasoning, contextRefs: [], usage: promptTokens > 0 ? { promptTokens, completionTokens, reasoningTokens, breakdown: lastBreakdown } : undefined });
send("chat:done", { content: "", reasoning: loopResult.reasoning, contextRefs: [], usage: finalUsage() });
return;
}

send("chat:done", { content: loopResult.content, reasoning: loopResult.reasoning, contextRefs: [], usage: promptTokens > 0 ? { promptTokens, completionTokens, reasoningTokens, breakdown: lastBreakdown } : undefined });
send("chat:done", { content: loopResult.content, reasoning: loopResult.reasoning, contextRefs: [], usage: finalUsage() });
});
}
5 changes: 4 additions & 1 deletion electron/lib/chat-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function runToolLoop(
signal?: AbortSignal,
getWin?: () => BrowserWindow | null,
provider?: string,
onUsage?: (pt: number, ct: number, rt?: number) => void,
onUsage?: (pt: number, ct: number, rt?: number, costUsd?: number) => void,
emitToolCallDone?: (e: { tool: string; cairnRef?: { type: "note" | "task"; id: string; title: string }; externalRef?: { url: string; title?: string; snippet?: string }; output?: string; callId?: string; ok?: boolean; error?: string }) => void,
onToken?: (delta: string) => void,
onThought?: (delta: string) => void,
Expand Down Expand Up @@ -81,10 +81,12 @@ export async function runToolLoop(
const choice = res.choices?.[0];
if (!choice) return { exhausted: true, content: "No response from local Llama on-device model.", reasoning: "" };
if (res.usage && onUsage) {
const usageCost = (res.usage as { cost?: unknown }).cost;
onUsage(
res.usage.prompt_tokens ?? 0,
res.usage.completion_tokens ?? 0,
res.usage.completion_tokens_details?.reasoning_tokens ?? 0,
typeof usageCost === "number" ? usageCost : undefined,
);
}
const rawMsg = choice.message as OpenAIMessage & { reasoning?: string; reasoning_content?: string };
Expand Down Expand Up @@ -178,6 +180,7 @@ export async function runToolLoop(
chunk.usage.prompt_tokens ?? 0,
chunk.usage.completion_tokens ?? 0,
chunk.usage.completion_tokens_details?.reasoning_tokens ?? 0,
typeof chunk.usage.cost === "number" ? chunk.usage.cost : undefined,
);
}
const delta = chunk.choices?.[0]?.delta;
Expand Down
23 changes: 18 additions & 5 deletions electron/lib/chat-subagent-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface SubagentEvents {
/** A tool call finished inside a subagent. */
onSubagentToolCallDone?: (e: { childId: string; tool: string; cairnRef?: { type: "note" | "task"; id: string; title: string }; externalRef?: { url: string; title?: string; snippet?: string }; output?: string; callId?: string; ok?: boolean; error?: string }) => void;
/** Latest token usage for a subagent (its OWN context window) — drives its ring. */
onSubagentUsage?: (e: { childId: string; promptTokens: number; completionTokens: number; reasoningTokens?: number }) => void;
onSubagentUsage?: (e: { childId: string; promptTokens: number; completionTokens: number; reasoningTokens?: number; costUsd?: number }) => void;
}

const DISPATCH_SYSTEM_PROMPT = (date: string) =>
Expand Down Expand Up @@ -256,6 +256,14 @@ async function runSubagent(
{ role: "user", content: instruction },
];

// Accumulated USD cost for THIS subagent's turns, surfaced on its own ring.
const costAcc: { usd: number | undefined } = { usd: undefined };
const addCost = (cost: unknown) => {
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) {
costAcc.usd = (costAcc.usd ?? 0) + cost;
}
};

const result = await runToolLoop(
db,
req,
Expand All @@ -271,13 +279,14 @@ async function runSubagent(
signal,
getWin,
cfg.provider,
(pt, ct, rt) => {
(pt, ct, rt, cost) => {
// Accumulate into the total-cost figures…
metrics.promptTokens += pt; metrics.completionTokens += ct; if (rt) metrics.reasoningTokens += rt;
// …and report THIS subagent's own context window (its latest prompt size)
// so the renderer can give it a dedicated ring. prompt_tokens is the size
// of the context this turn, not a running sum — take the latest value.
if (childId) events?.onSubagentUsage?.({ childId, promptTokens: pt, completionTokens: ct, reasoningTokens: rt });
addCost(cost);
if (childId) events?.onSubagentUsage?.({ childId, promptTokens: pt, completionTokens: ct, reasoningTokens: rt, costUsd: costAcc.usd });
},
(e) => {
// Count tool errors by sniffing the JSON output for an error field.
Expand All @@ -302,7 +311,7 @@ async function runSubagent(
// explicit nudge to write the findings. This is the standard "force final
// answer" pattern and rescues most empty-output cases on small models.
if (metrics.toolCalls > 0 || messages.some((m) => m.role === "tool")) {
const forced = await forceFinalAnswer(cfg, messages, metrics, signal);
const forced = await forceFinalAnswer(cfg, messages, metrics, addCost, signal);
if (forced.trim()) {
if (childId) events?.onSubagentToken?.({ childId, delta: forced.trim() });
return forced.trim();
Expand All @@ -321,6 +330,7 @@ async function forceFinalAnswer(
cfg: DispatchConfig,
messages: OpenAIMessage[],
metrics: SubagentMetrics,
addCost?: (cost: unknown) => void,
signal?: AbortSignal,
): Promise<string> {
const nudged: OpenAIMessage[] = [
Expand Down Expand Up @@ -358,6 +368,7 @@ async function forceFinalAnswer(
metrics.promptTokens += data.usage.prompt_tokens ?? 0;
metrics.completionTokens += data.usage.completion_tokens ?? 0;
metrics.reasoningTokens += data.usage.completion_tokens_details?.reasoning_tokens ?? 0;
addCost?.(data.usage.cost);
}
return (data.choices?.[0]?.message?.content as string) ?? "";
} catch {
Expand Down Expand Up @@ -508,7 +519,9 @@ export async function runDispatchLoop(
// subagents did real work. Force a final synthesis from the gathered
// subagent results rather than returning nothing.
if (!finalContent && metrics.subagentRuns > 0) {
finalContent = (await forceFinalAnswer(cfg, messages, metrics, signal)).trim();
// Dispatcher-side forced answer: its cost isn't surfaced (subagent
// rings carry their own), so no cost accumulator is passed.
finalContent = (await forceFinalAnswer(cfg, messages, metrics, undefined, signal)).trim();
}
return { content: finalContent, reasoning: finalReasoning, metrics };
}
Expand Down
66 changes: 66 additions & 0 deletions electron/lib/provider-credits.live-check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fs from "fs";
import path from "path";
import { describe, expect, it } from "vitest";
import { resolveCreditSpec, probeCredits } from "./provider-credits";

// Integration check: the app's OWN resolver + parser + probe against the real
// cairn-community providers.json + real keys (from the sibling repo's .env.test).
// Self-skips when the sibling repo / manifest is absent (CI, fresh clones), and
// per-provider when no key is set. Run with keys to prove credits will render.

const COMMUNITY = path.resolve(__dirname, "../../../cairn-community");

function loadEnv(): Record<string, string> {
const out: Record<string, string> = {};
const p = path.join(COMMUNITY, ".env.test");
if (!fs.existsSync(p)) return out;
for (const line of fs.readFileSync(p, "utf8").split("\n")) {
const t = line.trim();
if (!t || t.startsWith("#")) continue;
const eq = t.indexOf("=");
if (eq === -1) continue;
out[t.slice(0, eq).trim()] = t.slice(eq + 1).trim();
}
return out;
}

const manifestPath = path.join(COMMUNITY, "providers.json");
// Path checks + env parsing are safe at collection time (existsSync-guarded);
// the manifest read itself is deferred into the test body so an absent sibling
// repo (CI, fresh clones) skips cleanly instead of crashing the suite.
const hasManifest = fs.existsSync(manifestPath);
const hasAnyKey = Object.keys(loadEnv()).some((k) => k.endsWith("_API_KEY"));

describe("app-side credit display against the real community manifest", () => {
it.skipIf(!hasManifest || !hasAnyKey)(
"resolves a spec for every descriptor provider and parses live",
async () => {
const env = loadEnv();
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
const results: string[] = [];
for (const p of manifest.providers) {
const spec = p.definition?.credits;
if (!spec) continue;
const resolved = resolveCreditSpec(p.definition.baseUrl, manifest.providers);
if (!resolved) {
results.push(`✗ ${p.id}: resolveCreditSpec returned null for its OWN baseUrl`);
continue;
}
const envKey = `${p.id.toUpperCase()}_API_KEY`;
const key = env[envKey] || env[`PROVIDER_${envKey}`];
if (!key) {
results.push(`· ${p.id}: no key (set ${envKey}), skipped`);
continue;
}
const probe = await probeCredits(resolved.url, key, resolved.shape);
results.push(
probe.info
? `✓ ${p.id}: HTTP ${probe.status} → ${JSON.stringify({ remaining: probe.info.remaining, usage: probe.info.usage, limit: probe.info.limit, currency: probe.info.currency })}`
: `✗ ${p.id}: HTTP ${probe.status}${probe.error ? ` (${probe.error})` : ""}`,
);
}
for (const r of results) console.log(r);
expect(results.some((r) => r.startsWith("✗"))).toBe(false);
},
);
});
Loading