@inferhub/opencode-usage 0.1.1 → 0.1.2
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/dist/index.d.ts +6 -28
- package/dist/index.js +25 -10
- package/package.json +3 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,9 @@
|
|
|
1
|
+
import { type Plugin } from "@opencode-ai/plugin";
|
|
1
2
|
import { type AccountUsage } from "@inferhub/usage";
|
|
2
|
-
type Opts = {
|
|
3
|
-
apiKey?: string;
|
|
4
|
-
baseUrl?: string;
|
|
5
|
-
window?: "day" | "24h" | "7d" | "30d";
|
|
6
|
-
lowBalanceUsd?: number;
|
|
7
|
-
};
|
|
8
|
-
type PluginCtx = {
|
|
9
|
-
client?: {
|
|
10
|
-
app?: {
|
|
11
|
-
log?: (input: {
|
|
12
|
-
body: Record<string, unknown>;
|
|
13
|
-
}) => Promise<void> | void;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
type PluginHooks = {
|
|
18
|
-
tool?: Record<string, {
|
|
19
|
-
description: string;
|
|
20
|
-
args?: Record<string, unknown>;
|
|
21
|
-
execute: (...args: unknown[]) => Promise<string> | string;
|
|
22
|
-
}>;
|
|
23
|
-
event?: (input: {
|
|
24
|
-
event: {
|
|
25
|
-
type: string;
|
|
26
|
-
};
|
|
27
|
-
}) => Promise<void> | void;
|
|
28
|
-
};
|
|
29
3
|
export declare function getLastUsage(): AccountUsage | null;
|
|
30
|
-
|
|
4
|
+
/**
|
|
5
|
+
* OpenCode server plugin — registers `inferhub_usage` tool + idle refresh.
|
|
6
|
+
* Export both default and named Plugin so either load path works.
|
|
7
|
+
*/
|
|
8
|
+
export declare const InferHubUsagePlugin: Plugin;
|
|
31
9
|
export default InferHubUsagePlugin;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { tool } from "@opencode-ai/plugin";
|
|
1
2
|
import { fetchAccountUsage, formatReport, money, normalizeBaseUrl, resolveAuth, } from "@inferhub/usage";
|
|
2
3
|
let lastUsage = null;
|
|
3
4
|
async function loadUsage(opts = {}) {
|
|
4
5
|
const auth = opts.apiKey
|
|
5
|
-
? {
|
|
6
|
+
? {
|
|
7
|
+
apiKey: opts.apiKey,
|
|
8
|
+
baseUrl: normalizeBaseUrl(opts.baseUrl),
|
|
9
|
+
source: "options",
|
|
10
|
+
}
|
|
6
11
|
: resolveAuth({ apiKey: opts.apiKey, baseUrl: opts.baseUrl });
|
|
7
12
|
if (!auth) {
|
|
8
13
|
throw new Error("No InferHub API key. Configure provider.inferhub in opencode.json or set INFERHUB_API_KEY.");
|
|
@@ -18,27 +23,37 @@ async function loadUsage(opts = {}) {
|
|
|
18
23
|
export function getLastUsage() {
|
|
19
24
|
return lastUsage;
|
|
20
25
|
}
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
/**
|
|
27
|
+
* OpenCode server plugin — registers `inferhub_usage` tool + idle refresh.
|
|
28
|
+
* Export both default and named Plugin so either load path works.
|
|
29
|
+
*/
|
|
30
|
+
export const InferHubUsagePlugin = async (ctx, options) => {
|
|
31
|
+
const opts = (options ?? {});
|
|
23
32
|
const low = opts.lowBalanceUsd ?? 0.5;
|
|
33
|
+
// Warm cache; never throw during plugin init.
|
|
24
34
|
void loadUsage(opts).catch(() => { });
|
|
25
35
|
return {
|
|
26
36
|
tool: {
|
|
27
|
-
inferhub_usage: {
|
|
28
|
-
description: "
|
|
37
|
+
inferhub_usage: tool({
|
|
38
|
+
description: "Show InferHub balance, session/today/all-time usage, and top model for the current account",
|
|
29
39
|
args: {},
|
|
30
40
|
async execute() {
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
try {
|
|
42
|
+
const usage = await loadUsage({ ...opts });
|
|
43
|
+
return formatReport(usage);
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return `InferHub usage unavailable: ${e instanceof Error ? e.message : e}`;
|
|
47
|
+
}
|
|
33
48
|
},
|
|
34
|
-
},
|
|
49
|
+
}),
|
|
35
50
|
},
|
|
36
51
|
event: async ({ event }) => {
|
|
37
52
|
if (event.type === "session.idle" || event.type === "session.created") {
|
|
38
53
|
try {
|
|
39
54
|
const usage = await loadUsage(opts);
|
|
40
55
|
if (Number(usage.balance.amount_usdc) < low) {
|
|
41
|
-
await ctx.client
|
|
56
|
+
await ctx.client.app.log({
|
|
42
57
|
body: {
|
|
43
58
|
service: "inferhub-usage",
|
|
44
59
|
level: "warn",
|
|
@@ -53,5 +68,5 @@ export async function InferHubUsagePlugin(ctx, options) {
|
|
|
53
68
|
}
|
|
54
69
|
},
|
|
55
70
|
};
|
|
56
|
-
}
|
|
71
|
+
};
|
|
57
72
|
export default InferHubUsagePlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inferhub/opencode-usage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "OpenCode plugin: InferHub balance, session/day/all-time usage, top model",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,15 +24,8 @@
|
|
|
24
24
|
"prepublishOnly": "npm run build"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@inferhub/usage": "^0.1.
|
|
28
|
-
|
|
29
|
-
"peerDependencies": {
|
|
30
|
-
"@opencode-ai/plugin": ">=1.0.0"
|
|
31
|
-
},
|
|
32
|
-
"peerDependenciesMeta": {
|
|
33
|
-
"@opencode-ai/plugin": {
|
|
34
|
-
"optional": true
|
|
35
|
-
}
|
|
27
|
+
"@inferhub/usage": "^0.1.2",
|
|
28
|
+
"@opencode-ai/plugin": "^1.17.18"
|
|
36
29
|
},
|
|
37
30
|
"devDependencies": {
|
|
38
31
|
"@types/node": "^22.15.0",
|