@inferhub/opencode-usage 0.1.1 → 0.1.3

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 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
- export declare function InferHubUsagePlugin(ctx: PluginCtx, options?: Opts): Promise<PluginHooks>;
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
- ? { apiKey: opts.apiKey, baseUrl: normalizeBaseUrl(opts.baseUrl), source: "options" }
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
- export async function InferHubUsagePlugin(ctx, options) {
22
- const opts = options ?? {};
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: "Fetch InferHub account balance, day/all-time spend, and top used model for the current user",
37
+ inferhub_usage: tool({
38
+ description: "Show InferHub balance and session/today/all-time usage for the current account",
29
39
  args: {},
30
40
  async execute() {
31
- const usage = await loadUsage(opts);
32
- return formatReport(usage) + "\n\n" + JSON.stringify(usage, null, 2);
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?.app?.log?.({
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/dist/tui.js CHANGED
@@ -1,25 +1,36 @@
1
1
  /**
2
- * OpenCode TUI plugin — registers sidebar/home footer slots with InferHub usage.
2
+ * OpenCode TUI plugin — same InferHub status line as Claude Code.
3
3
  *
4
4
  * Install:
5
- * opencode plugin opencode-inferhub-usage --global
6
- * # or in opencode.json:
7
- * { "plugin": ["opencode-inferhub-usage", "opencode-inferhub-usage/tui"] }
8
- *
9
- * Note: TUI plugins require OpenCode's TUI module export (`./tui`). If the
10
- * runtime version only supports server plugins, the server tool still works.
5
+ * { "plugin": ["@inferhub/opencode-usage", "@inferhub/opencode-usage/tui"] }
11
6
  */
12
- import { fetchAccountUsageAuto, formatStatusLine, money, shortModel, } from "@inferhub/usage";
7
+ import { fetchAccountUsageAuto, formatStatusLine, money, } from "@inferhub/usage";
13
8
  let cached = null;
14
- let sessionCost = null;
15
- let sessionModel = null;
9
+ let openCodeSessionId = null;
10
+ let model = null;
16
11
  async function refresh() {
17
12
  try {
18
- const { usage } = await fetchAccountUsageAuto({ window: "day" });
19
- cached = usage;
20
- if (Number(usage.balance.amount_usdc) < 0.5) {
21
- // toast if available
13
+ const { usage } = await fetchAccountUsageAuto({
14
+ window: "day",
15
+ // Prefer OpenCode's session id when known so sess matches Claude semantics
16
+ sessionId: openCodeSessionId || undefined,
17
+ });
18
+ // If we asked for a specific session and API returned something else / empty,
19
+ // strip mismatched session (same rule as Claude statusline).
20
+ if (openCodeSessionId) {
21
+ const got = usage.session?.id ? String(usage.session.id) : "";
22
+ if (got !== openCodeSessionId && !got.includes(openCodeSessionId)) {
23
+ usage.session = null;
24
+ }
25
+ else if (usage.session) {
26
+ usage.session.id = openCodeSessionId;
27
+ }
22
28
  }
29
+ else {
30
+ // No OpenCode session id → don't show account-latest sess (avoids jumping)
31
+ usage.session = null;
32
+ }
33
+ cached = usage;
23
34
  return usage;
24
35
  }
25
36
  catch {
@@ -28,55 +39,51 @@ async function refresh() {
28
39
  }
29
40
  function line() {
30
41
  if (!cached)
31
- return "IH · loading…";
32
- return formatStatusLine(cached, {
33
- sessionCostUsd: sessionCost,
34
- model: sessionModel,
35
- });
42
+ return "InferHub · loading…";
43
+ return formatStatusLine(cached, { model });
36
44
  }
37
45
  export async function tui(api) {
38
46
  void refresh();
39
- const timer = setInterval(() => void refresh(), 60_000);
47
+ const timer = setInterval(() => void refresh(), 30_000);
40
48
  const offIdle = api.event.on("session.idle", () => void refresh());
49
+ const offCreated = api.event.on("session.created", (ev) => {
50
+ const info = ev?.properties?.info ?? ev?.info ?? ev?.properties ?? {};
51
+ const id = info.id || info.sessionID || info.sessionId || ev?.properties?.sessionID;
52
+ if (typeof id === "string" && id)
53
+ openCodeSessionId = id;
54
+ void refresh();
55
+ });
41
56
  const offUpd = api.event.on("session.updated", (ev) => {
42
57
  const info = ev?.properties?.info ?? ev?.info ?? {};
43
- if (typeof info.cost === "number")
44
- sessionCost = info.cost;
58
+ const id = info.id || info.sessionID || info.sessionId;
59
+ if (typeof id === "string" && id)
60
+ openCodeSessionId = id;
45
61
  if (typeof info.model === "string")
46
- sessionModel = info.model;
62
+ model = info.model;
47
63
  if (info.model?.id)
48
- sessionModel = info.model.id;
64
+ model = info.model.id;
65
+ void refresh();
49
66
  });
50
67
  api.lifecycle.onDispose(() => {
51
68
  clearInterval(timer);
52
69
  offIdle();
70
+ offCreated();
53
71
  offUpd();
54
72
  });
55
- // Prefer slots when the host supports Solid slot plugins.
56
73
  try {
57
74
  api.slots.register({
58
75
  name: "inferhub-usage",
59
- // Hosts that accept render functions will display these; others ignore.
60
76
  slots: {
77
+ // Same string Claude shows, everywhere we can place it.
61
78
  home_footer: () => line(),
62
- sidebar_footer: () => {
63
- if (!cached)
64
- return "IH …";
65
- return `bal ${money(cached.balance.amount_usdc)} · all ${money(cached.all_time.spend_usdc)} · top ${shortModel(cached.top_model?.model)}`;
66
- },
67
- sidebar_content: () => {
68
- if (!cached)
69
- return "InferHub usage loading…";
70
- const sess = sessionCost != null ? money(sessionCost) : "—";
71
- return `IH session ${sess} · day ${money(cached.window.spend_usdc)} · ${cached.window.requests} req`;
72
- },
79
+ sidebar_footer: () => line(),
80
+ sidebar_content: () => line(),
73
81
  },
74
82
  });
75
83
  }
76
84
  catch {
77
85
  // Slot API unavailable — server tool still works.
78
86
  }
79
- // Toast on first successful load when balance is low.
80
87
  void refresh().then((u) => {
81
88
  if (u && Number(u.balance.amount_usdc) < 0.5) {
82
89
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferhub/opencode-usage",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
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.0"
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.11",
28
+ "@opencode-ai/plugin": "^1.17.18"
36
29
  },
37
30
  "devDependencies": {
38
31
  "@types/node": "^22.15.0",