@kapishdima/ai-ledger 0.0.5 → 0.0.7

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.
Files changed (2) hide show
  1. package/dist/plugin.js +37 -20
  2. package/package.json +1 -1
package/dist/plugin.js CHANGED
@@ -3739,24 +3739,53 @@ async function deductBalance(provider, cost) {
3739
3739
  }
3740
3740
 
3741
3741
  // plugin.ts
3742
- var pending = new Map;
3743
- var DEBOUNCE_MS = 1500;
3742
+ var sessions = new Map;
3744
3743
  var plugin_default = async (input) => {
3745
3744
  await initializeDatabase();
3745
+ function log(msg) {
3746
+ input.client.app.log({ body: { service: "ai-ledger", level: "info", message: msg } });
3747
+ }
3748
+ async function flushTurn(sessionID, state) {
3749
+ sessions.delete(sessionID);
3750
+ const text2 = `ai-ledger \xB7 ${state.providerID} \xB7 -$${state.totalCost.toFixed(4)} spent | $${state.lastBalance.toFixed(2)} remaining`;
3751
+ log(`flushing turn for ${sessionID}: ${text2}`);
3752
+ try {
3753
+ const res = await input.client.session.prompt({
3754
+ path: { id: sessionID },
3755
+ query: { directory: input.directory },
3756
+ body: {
3757
+ noReply: true,
3758
+ parts: [{ type: "text", text: text2 }]
3759
+ }
3760
+ });
3761
+ log(`session.prompt result: ${JSON.stringify(res)}`);
3762
+ } catch (e) {
3763
+ log(`session.prompt error: ${e?.message}`);
3764
+ }
3765
+ }
3746
3766
  return {
3747
3767
  event: async ({ event }) => {
3768
+ log(`event: ${event.type}`);
3769
+ if (event.type === "session.idle") {
3770
+ const sessionID = event.properties?.sessionID;
3771
+ log(`session.idle for ${sessionID}, accumulated: ${sessions.has(sessionID)}`);
3772
+ const state = sessions.get(sessionID);
3773
+ if (state)
3774
+ await flushTurn(sessionID, state);
3775
+ return;
3776
+ }
3748
3777
  if (event.type !== "message.updated")
3749
3778
  return;
3750
3779
  const msg = event.properties?.info;
3751
- if (!msg)
3780
+ if (!msg?.finish)
3752
3781
  return;
3753
3782
  if (msg.role !== "assistant")
3754
3783
  return;
3755
- if (!msg.finish)
3756
- return;
3784
+ log(`assistant message: cost=${msg.cost} provider=${msg.providerID}`);
3757
3785
  if (!msg.cost || msg.cost === 0)
3758
3786
  return;
3759
3787
  const wallet = await getWallet(msg.providerID);
3788
+ log(`wallet for ${msg.providerID}: ${wallet ? "found" : "NOT FOUND"}`);
3760
3789
  if (!wallet)
3761
3790
  return;
3762
3791
  const newBalance = await deductBalance(msg.providerID, msg.cost);
@@ -3771,22 +3800,10 @@ var plugin_default = async (input) => {
3771
3800
  created_at: new Date().toISOString(),
3772
3801
  synced: 0
3773
3802
  });
3774
- const existing = pending.get(msg.providerID);
3775
- if (existing)
3776
- clearTimeout(existing.timer);
3803
+ const existing = sessions.get(msg.sessionID);
3777
3804
  const totalCost = (existing?.totalCost ?? 0) + msg.cost;
3778
- const timer = setTimeout(() => {
3779
- pending.delete(msg.providerID);
3780
- input.client.tui.showToast({
3781
- body: {
3782
- title: `ai-ledger \xB7 ${msg.providerID}`,
3783
- message: `-$${totalCost.toFixed(4)} | balance: $${newBalance.toFixed(2)}`,
3784
- variant: newBalance < 0 ? "warning" : "info",
3785
- duration: 4000
3786
- }
3787
- });
3788
- }, DEBOUNCE_MS);
3789
- pending.set(msg.providerID, { totalCost, lastBalance: newBalance, timer });
3805
+ sessions.set(msg.sessionID, { totalCost, lastBalance: newBalance, providerID: msg.providerID });
3806
+ log(`accumulated for session ${msg.sessionID}: totalCost=${totalCost}`);
3790
3807
  }
3791
3808
  };
3792
3809
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapishdima/ai-ledger",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "AI provider balance and spending tracker for OpenCode",
5
5
  "type": "module",
6
6
  "main": "dist/plugin.js",