ollama-cloud-usage 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -7
- package/dist/cli.js +8 -22
- package/dist/key.js +67 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,18 @@ state from `/api/balance` (session and weekly remaining percentages plus
|
|
|
5
5
|
authoritative reset times) and request counts from `/api/usage`, shows a live
|
|
6
6
|
dashboard, and logs history to a JSONL file.
|
|
7
7
|
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Live quota dashboard** — session and weekly usage with bars, burn rate, and
|
|
11
|
+
reset countdowns.
|
|
12
|
+
- **Authoritative reset times** — read from `/api/balance`, not inferred.
|
|
13
|
+
- **Configurable polling interval** — 5 minutes by default.
|
|
14
|
+
- **Keyboard shortcuts** — refresh on demand without restarting.
|
|
15
|
+
- **History log** — append-only JSONL you can analyse later.
|
|
16
|
+
- **Credit-plan aware** — shows remaining credit when session/weekly limits are
|
|
17
|
+
not used.
|
|
18
|
+
- **Zero runtime dependencies** — Node built-ins only.
|
|
19
|
+
|
|
8
20
|
## Install
|
|
9
21
|
|
|
10
22
|
```sh
|
|
@@ -14,14 +26,10 @@ npm install -g ollama-cloud-usage
|
|
|
14
26
|
## Usage
|
|
15
27
|
|
|
16
28
|
```sh
|
|
17
|
-
ollama-cloud-usage [--
|
|
29
|
+
ollama-cloud-usage [--interval <sec>]
|
|
18
30
|
```
|
|
19
31
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
- `--baseline <sec>` — idle polling interval (default `300` = 5 min)
|
|
23
|
-
- `--fine <sec>` — near-boundary polling interval (default `30`)
|
|
24
|
-
- `--window <min>` — half-window around each reset boundary (default `5`)
|
|
32
|
+
- `--interval <sec>` — polling interval (default `300` = 5 min)
|
|
25
33
|
|
|
26
34
|
### Interactive commands
|
|
27
35
|
|
|
@@ -36,7 +44,10 @@ All flags are named and can be given in any order.
|
|
|
36
44
|
The key is resolved by precedence:
|
|
37
45
|
|
|
38
46
|
1. `OLLAMA_API_KEY` env var
|
|
39
|
-
2. `~/.local/share/opencode/auth.json` → `ollama-cloud.key` (
|
|
47
|
+
2. `~/.local/share/opencode/auth.json` → `ollama-cloud.key` (OpenCode 1)
|
|
48
|
+
3. `~/.local/share/opencode/opencode.db` → `credential` row with
|
|
49
|
+
`integration_id = 'ollama-cloud'` (OpenCode 2, read via Node's built-in
|
|
50
|
+
SQLite, falling back to the `sqlite3` CLI if needed)
|
|
40
51
|
|
|
41
52
|
The key is never logged or written to disk by this tool.
|
|
42
53
|
|
package/dist/cli.js
CHANGED
|
@@ -10,27 +10,15 @@ const readline_1 = __importDefault(require("readline"));
|
|
|
10
10
|
const key_1 = require("./key");
|
|
11
11
|
const api_1 = require("./api");
|
|
12
12
|
const display_1 = require("./display");
|
|
13
|
-
// CLI: ollama-cloud-usage [--
|
|
14
|
-
// --
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
// All flags are named so they can be given in any order.
|
|
18
|
-
let baselineSec = 300;
|
|
19
|
-
let fineSec = 30;
|
|
20
|
-
let windowMin = 5;
|
|
13
|
+
// CLI: ollama-cloud-usage [--interval <sec>]
|
|
14
|
+
// --interval <sec> = polling interval (default 300 = 5 min)
|
|
15
|
+
// Reset times come from the API, so there is no boundary-aware polling.
|
|
16
|
+
let intervalSec = 300;
|
|
21
17
|
for (let i = 2; i < process.argv.length; i++) {
|
|
22
18
|
let arg = process.argv[i];
|
|
23
19
|
let value = process.argv[i + 1];
|
|
24
|
-
if (arg === "--
|
|
25
|
-
|
|
26
|
-
i++;
|
|
27
|
-
}
|
|
28
|
-
else if (arg === "--fine" && value) {
|
|
29
|
-
fineSec = parseInt(value, 10);
|
|
30
|
-
i++;
|
|
31
|
-
}
|
|
32
|
-
else if (arg === "--window" && value) {
|
|
33
|
-
windowMin = parseInt(value, 10);
|
|
20
|
+
if (arg === "--interval" && value) {
|
|
21
|
+
intervalSec = parseInt(value, 10);
|
|
34
22
|
i++;
|
|
35
23
|
}
|
|
36
24
|
}
|
|
@@ -97,9 +85,7 @@ async function main() {
|
|
|
97
85
|
weeklyReset,
|
|
98
86
|
now,
|
|
99
87
|
});
|
|
100
|
-
|
|
101
|
-
let interval = nearest <= windowMin * 60000 ? fineSec : baselineSec;
|
|
102
|
-
await waitForIntervalOrKeypress(interval);
|
|
88
|
+
await waitForIntervalOrKeypress(intervalSec);
|
|
103
89
|
continue;
|
|
104
90
|
}
|
|
105
91
|
// Credit plans (or unexpected shape): show remaining allowance.
|
|
@@ -116,7 +102,7 @@ async function main() {
|
|
|
116
102
|
}
|
|
117
103
|
}
|
|
118
104
|
console.log(` requests ${usage.totals.request_count.toLocaleString()} over ${usage.range}`);
|
|
119
|
-
await waitForIntervalOrKeypress(
|
|
105
|
+
await waitForIntervalOrKeypress(intervalSec);
|
|
120
106
|
}
|
|
121
107
|
}
|
|
122
108
|
// Resolve when the interval elapses, or when the user types a command + enter.
|
package/dist/key.js
CHANGED
|
@@ -9,22 +9,84 @@ const os_1 = __importDefault(require("os"));
|
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
// Resolve the ollama cloud API key by precedence:
|
|
11
11
|
// 1. OLLAMA_API_KEY env var (works for anyone, CI, etc.)
|
|
12
|
-
// 2. ~/.local/share/opencode/auth.json -> ollama-cloud.key
|
|
12
|
+
// 2. ~/.local/share/opencode/auth.json -> ollama-cloud.key (OpenCode 1)
|
|
13
|
+
// 3. ~/.local/share/opencode/opencode.db -> credential row (OpenCode 2)
|
|
14
|
+
// 4. anything else: clear error
|
|
13
15
|
// The key is never logged or written to disk by this tool.
|
|
14
16
|
function loadKey() {
|
|
15
17
|
let env = process.env.OLLAMA_API_KEY;
|
|
16
18
|
if (env)
|
|
17
19
|
return env;
|
|
20
|
+
let fromAuthJson = keyFromAuthJson();
|
|
21
|
+
if (fromAuthJson)
|
|
22
|
+
return fromAuthJson;
|
|
23
|
+
let fromDb = keyFromOpenCodeDb();
|
|
24
|
+
if (fromDb)
|
|
25
|
+
return fromDb;
|
|
26
|
+
throw new Error("No ollama cloud API key found. Set OLLAMA_API_KEY, or store it in " +
|
|
27
|
+
"~/.local/share/opencode/auth.json ('ollama-cloud'.key) or the " +
|
|
28
|
+
"OpenCode database (credential where integration_id = 'ollama-cloud').");
|
|
29
|
+
}
|
|
30
|
+
function keyFromAuthJson() {
|
|
18
31
|
let authPath = path_1.default.join(os_1.default.homedir(), ".local", "share", "opencode", "auth.json");
|
|
19
32
|
try {
|
|
20
33
|
let auth = JSON.parse(fs_1.default.readFileSync(authPath, "utf8"));
|
|
21
34
|
let key = auth["ollama-cloud"] && auth["ollama-cloud"].key;
|
|
22
|
-
if (key)
|
|
35
|
+
if (typeof key === "string" && key)
|
|
23
36
|
return key;
|
|
24
37
|
}
|
|
25
38
|
catch {
|
|
26
|
-
// fall through
|
|
39
|
+
// fall through
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
// OpenCode 2 stores credentials in SQLite. Prefer the built-in node:sqlite
|
|
44
|
+
// (Node >= 22, no external binary); fall back to the sqlite3 CLI if present.
|
|
45
|
+
function keyFromOpenCodeDb() {
|
|
46
|
+
let dbPath = path_1.default.join(os_1.default.homedir(), ".local", "share", "opencode", "opencode.db");
|
|
47
|
+
if (!fs_1.default.existsSync(dbPath))
|
|
48
|
+
return null;
|
|
49
|
+
let query = "select value from credential where integration_id = 'ollama-cloud' " +
|
|
50
|
+
"order by time_updated desc limit 1";
|
|
51
|
+
try {
|
|
52
|
+
let { DatabaseSync } = require("node:sqlite");
|
|
53
|
+
let db = new DatabaseSync(dbPath, { readOnly: true });
|
|
54
|
+
try {
|
|
55
|
+
let row = db.prepare(query).get();
|
|
56
|
+
if (row && row.value)
|
|
57
|
+
return extractKey(row.value);
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
db.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// node:sqlite unavailable; try the CLI below
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
let { execFileSync } = require("child_process");
|
|
68
|
+
let out = execFileSync("sqlite3", ["-readonly", "-json", dbPath, query], {
|
|
69
|
+
encoding: "utf8",
|
|
70
|
+
});
|
|
71
|
+
let rows = JSON.parse(out || "[]");
|
|
72
|
+
if (rows.length > 0 && rows[0].value)
|
|
73
|
+
return extractKey(rows[0].value);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// no sqlite3 binary either
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
// The stored value is JSON like {"type":"api_key","key":"..."}; older entries
|
|
81
|
+
// may be the raw key.
|
|
82
|
+
function extractKey(value) {
|
|
83
|
+
try {
|
|
84
|
+
let parsed = JSON.parse(value);
|
|
85
|
+
if (parsed && typeof parsed.key === "string" && parsed.key)
|
|
86
|
+
return parsed.key;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// not JSON: treat as a raw key
|
|
27
90
|
}
|
|
28
|
-
|
|
29
|
-
"~/.local/share/opencode/auth.json under 'ollama-cloud'.key");
|
|
91
|
+
return value || null;
|
|
30
92
|
}
|