Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OpenUsage lives in your menu bar and shows you how much of your AI coding subscr
- [**Amp**](docs/providers/amp.md) / free tier, bonus, credits
- [**Antigravity**](docs/providers/antigravity.md) / all models
- [**Claude**](docs/providers/claude.md) / session, weekly, extra usage, local token usage (ccusage)
- [**Crof.AI**](docs/providers/crofai.md) / API key, requests, credits, per-model tokens
- [**Codex**](docs/providers/codex.md) / session, weekly, reviews, credits
- [**Copilot**](docs/providers/copilot.md) / premium, chat, completions
- [**Cursor**](docs/providers/cursor.md) / credits, total usage, auto usage, API usage, on-demand, CLI auth
Expand Down
56 changes: 56 additions & 0 deletions docs/providers/crofai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Crof.AI

> Uses the Crof.AI API to display usage, credits, plan info, and per-model token breakdown.

## Overview

- **Source of truth:** `https://crof.ai/usage_api/`, `https://crof.ai/pricing_api`, `https://crof.ai/user-api/usage`
- **Auth:** API key (required) + session key (optional, for plan + model breakdown)
- **Provider ID:** `crofai`
- **Usage scope:** requests, credits, plan limits, per-model token usage

## Setup

### Required: API key

Open [crof.ai/usage_api/](https://crof.ai/usage_api/) while logged in. Copy your API key, then add to your shell config (`~/.zshrc`):

```sh
export CROF_AI_API_KEY="your-api-key-here"
```

Then `source ~/.zshrc` and restart OpenUsage.

### Optional: Session key

For progress bar (max requests from pricing) and per-model token breakdown, also set:

```sh
export CROF_AI_SESSION_KEY="your-session-cookie-value"
```

Get it from [crof.ai](https://crof.ai) > DevTools > Application > Cookies > copy the `session` value.

Or save it to `{appDataDir}/plugins_data/crofai/session-key` (env var takes priority).

## Data Sources

| Endpoint | Auth | Purpose |
|---|---|---|
| `GET /usage_api/` | `Authorization: Bearer <API_KEY>` | Requests + credits (required) |
| `GET /pricing_api` | `Cookie: session=<SESSION_KEY>` | Plan name + max requests (optional) |
| `GET /user-api/usage` | `Cookie: session=<SESSION_KEY>` | Per-model token breakdown (optional) |

## Display

- **Status badge:** Connected (green) when API responds
- **Usage progress bar:** Used requests / plan max (shown when both API key + session key available)
- **Credits:** Dollar-formatted credit balance
- **Total tokens:** Sum of all models' tokens (shown only with session key)
- **Top models:** Top 5 models sorted by token usage (shown only with session key)

## Failure Behavior

- **Missing API key:** Error asking to set `CROF_AI_API_KEY`
- **401/403 on usage_api:** API key invalid or expired
- **Bad session key:** Only session-backed features drop out, API key data still shows
4 changes: 4 additions & 0 deletions plugins/crofai/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
276 changes: 276 additions & 0 deletions plugins/crofai/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
(function () {
var PROVIDER_ID = "crofai";
var USAGE_API_URL = "https://crof.ai/usage_api/";
var USER_USAGE_URL = "https://crof.ai/user-api/usage";
var PRICING_URL = "https://crof.ai/pricing_api";
var TIMEOUT_MS = 10000;
var MAX_DISPLAY_MODELS = 5;

var PLAN_NAMES = {
hobby: "Hobby",
pro: "Pro",
int: "Intermediate",
scale: "Scale",
max: "Max",
};
var FALLBACK_MAX_REQUESTS = 15000;

function readApiKey(ctx) {
var raw = ctx.host.env.get("CROF_AI_API_KEY");
if (typeof raw === "string" && raw.trim()) {
return raw.trim();
}
throw (
"Crof.AI not configured. Set CROF_AI_API_KEY env var.\n" +
"Get your API key from https://crof.ai/usage_api/"
);
}

function readSessionKey(ctx) {
var raw = ctx.host.env.get("CROF_AI_SESSION_KEY");
if (typeof raw === "string" && raw.trim()) {
return raw.trim();
}

var keyPath = ctx.app.pluginDataDir + "/session-key";
if (!ctx.host.fs.exists(keyPath)) {
return null;
}
try {
var key = ctx.host.fs.readText(keyPath).trim();
return key || null;
} catch (e) {
return null;
}
}

function requestJson(ctx, opts) {
var resp;
try {
resp = ctx.host.http.request({
method: "GET",
url: opts.url,
headers: opts.headers || {},
Accept: "application/json",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Move Accept header into the request headers map

The Accept value is set at the top level of the request object, not inside headers, so it is not forwarded by the host HTTP wrapper (which serializes only req.headers). If Crof.AI enforces content negotiation, this can return non-JSON content and trigger the plugin's "Invalid response" path even with valid credentials.

Useful? React with 👍 / 👎.

timeoutMs: TIMEOUT_MS,
});
} catch (e) {
throw "Crof.AI network error. Check your connection.";
}

if (resp.status === 401 || resp.status === 403) {
throw "Crof.AI auth expired. Check your API key or session key.";
}

if (resp.status !== 200) {
throw "Crof.AI API error (HTTP " + resp.status + "). Try again later.";
}

var parsed = ctx.util.tryParseJson(resp.bodyText);
if (parsed === null) {
throw "Invalid response from Crof.AI. Try again later.";
}
return parsed;
}

function planFullName(raw) {
if (!raw) return null;
var lower = String(raw).toLowerCase().trim();
return PLAN_NAMES[lower] || null;
}

function formatTokens(count) {
if (count >= 1e9) {
var b = count / 1e9;
return (Math.round(b * 10) / 10).toFixed(1) + "B";
}
if (count >= 1e6) {
var m = count / 1e6;
return (Math.round(m * 10) / 10).toFixed(1) + "M";
}
if (count >= 1e3) {
var k = count / 1e3;
return (Math.round(k * 10) / 10).toFixed(1) + "K";
}
return String(count);
}

function formatCredits(raw) {
var num = Number(raw);
if (!Number.isFinite(num)) return "$0.00";
var abs = Math.abs(num);
if (abs < 0.01) return "$0.00";
var sign = num < 0 ? "-" : "";
return sign + "$" + abs.toFixed(2);
}

function buildLines(ctx, usageData, requestsCount, creditsValue, planInfo, usagePlanRequests) {
var lines = [];

var maxRequests = FALLBACK_MAX_REQUESTS;
if (
planInfo &&
typeof planInfo.requests === "number" &&
planInfo.requests > 0
) {
maxRequests = planInfo.requests;
} else if (
typeof usagePlanRequests === "number" &&
Number.isFinite(usagePlanRequests) &&
usagePlanRequests > 0
) {
maxRequests = usagePlanRequests;
}

var used = 0;
if (requestsCount !== null) {
used = maxRequests - requestsCount;
if (used < 0) used = 0;
}

lines.push(
ctx.line.progress({
label: "Requests",
used: used,
limit: maxRequests,
format: { kind: "count", suffix: "requests" },
})
);

if (creditsValue >= 0) {
lines.push(
ctx.line.text({
label: "Credits",
value: formatCredits(creditsValue),
})
);
}

var models = [];
var totalTokens = 0;

if (usageData && typeof usageData === "object") {
for (var key in usageData) {
if (Object.prototype.hasOwnProperty.call(usageData, key)) {
var model = usageData[key];
var tt = model.total_tokens || model.totalTokens || 0;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
if (tt > 0) {
totalTokens += tt;
models.push({ name: key, tokens: tt });
}
}
}
}

lines.push(
ctx.line.text({
label: "Total tokens",
value: formatTokens(totalTokens),
subtitle: models.length > 0 ? models.length + " models" : undefined,
})
);

models.sort(function (a, b) {
return b.tokens - a.tokens;
});

var topModels = models.slice(0, MAX_DISPLAY_MODELS);
for (var i = 0; i < topModels.length; i++) {
var m = topModels[i];
lines.push(
ctx.line.text({
label: m.name,
value: formatTokens(m.tokens) + " tokens",
})
);
}

return lines;
}

function probe(ctx) {
var apiKey = readApiKey(ctx);

var usageResp = requestJson(ctx, {
url: USAGE_API_URL,
headers: {
Authorization: "Bearer " + apiKey,
},
});

if (!usageResp || typeof usageResp !== "object" || Array.isArray(usageResp)) {
throw "Invalid response from Crof.AI. Try again later.";
}

var hasCredits = "credits" in usageResp;
var creditsValue = 0;
if (hasCredits) {
if (typeof usageResp.credits !== "number" || !Number.isFinite(usageResp.credits)) {
throw "Invalid response from Crof.AI. Try again later.";
}
creditsValue = usageResp.credits;
}

var requestsCount = null;
var hasRequests = "usable_requests" in usageResp;
if (hasRequests) {
if (usageResp.usable_requests !== null) {
if (typeof usageResp.usable_requests !== "number" || !Number.isFinite(usageResp.usable_requests)) {
throw "Invalid response from Crof.AI. Try again later.";
}
requestsCount = usageResp.usable_requests;
}
}

var usagePlanRequests = null;
if ("requests_plan" in usageResp) {
if (
typeof usageResp.requests_plan === "number" &&
Number.isFinite(usageResp.requests_plan) &&
usageResp.requests_plan > 0
) {
usagePlanRequests = usageResp.requests_plan;
}
}

var sessionKey = readSessionKey(ctx);

var userUsageData = null;
var planInfo = null;
var planName = null;

if (sessionKey) {
try {
userUsageData = requestJson(ctx, {
url: USER_USAGE_URL,
headers: {
Cookie: "session=" + sessionKey,
},
});
} catch (e) {
ctx.host.log.warn("Crof.AI user usage fetch failed: " + String(e));
}

try {
planInfo = requestJson(ctx, {
url: PRICING_URL,
headers: {
Cookie: "session=" + sessionKey,
},
});
if (planInfo && planInfo.name) {
planName = planFullName(planInfo.name) || ctx.fmt.planLabel(planInfo.name);
}
} catch (e) {
ctx.host.log.warn("Crof.AI pricing fetch failed: " + String(e));
}
}

return {
plan: planName,
lines: buildLines(ctx, userUsageData, requestsCount, creditsValue, planInfo, usagePlanRequests),
};
}

globalThis.__openusage_plugin = { id: PROVIDER_ID, probe };
})();
22 changes: 22 additions & 0 deletions plugins/crofai/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"schemaVersion": 1,
"id": "crofai",
"name": "Crof.AI",
"version": "0.0.1",
"entry": "plugin.js",
"icon": "icon.svg",
"brandColor": "#6B52F2",
"links": [
{
"label": "Dashboard",
"url": "https://crof.ai"
}
],
"lines": [
{ "type": "badge", "label": "Status", "scope": "overview" },
{ "type": "progress", "label": "Requests", "scope": "overview", "primaryOrder": 1 },
{ "type": "text", "label": "Credits", "scope": "overview" },
{ "type": "text", "label": "Total tokens", "scope": "overview" },
{ "type": "text", "label": "Models", "scope": "detail" }
]
}
Loading
Loading