@kapishdima/ai-ledger 0.0.5 → 0.0.6
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/plugin.js +23 -20
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -3739,21 +3739,37 @@ async function deductBalance(provider, cost) {
|
|
|
3739
3739
|
}
|
|
3740
3740
|
|
|
3741
3741
|
// plugin.ts
|
|
3742
|
-
var
|
|
3743
|
-
var DEBOUNCE_MS = 1500;
|
|
3742
|
+
var sessions = new Map;
|
|
3744
3743
|
var plugin_default = async (input) => {
|
|
3745
3744
|
await initializeDatabase();
|
|
3745
|
+
async function flushTurn(sessionID, state) {
|
|
3746
|
+
sessions.delete(sessionID);
|
|
3747
|
+
const balanceSign = state.lastBalance < 0 ? "\u26A0\uFE0F " : "";
|
|
3748
|
+
const text2 = `\`ai-ledger\` \xB7 ${state.providerID} \u2014 **-$${state.totalCost.toFixed(4)}** spent | **$${state.lastBalance.toFixed(2)}** remaining`;
|
|
3749
|
+
await input.client.session.prompt({
|
|
3750
|
+
path: { id: sessionID },
|
|
3751
|
+
body: {
|
|
3752
|
+
noReply: true,
|
|
3753
|
+
parts: [{ type: "text", text: balanceSign + text2, synthetic: true }]
|
|
3754
|
+
}
|
|
3755
|
+
});
|
|
3756
|
+
}
|
|
3746
3757
|
return {
|
|
3747
3758
|
event: async ({ event }) => {
|
|
3759
|
+
if (event.type === "session.idle") {
|
|
3760
|
+
const sessionID = event.properties?.sessionID;
|
|
3761
|
+
const state = sessions.get(sessionID);
|
|
3762
|
+
if (state)
|
|
3763
|
+
await flushTurn(sessionID, state);
|
|
3764
|
+
return;
|
|
3765
|
+
}
|
|
3748
3766
|
if (event.type !== "message.updated")
|
|
3749
3767
|
return;
|
|
3750
3768
|
const msg = event.properties?.info;
|
|
3751
|
-
if (!msg)
|
|
3769
|
+
if (!msg?.finish)
|
|
3752
3770
|
return;
|
|
3753
3771
|
if (msg.role !== "assistant")
|
|
3754
3772
|
return;
|
|
3755
|
-
if (!msg.finish)
|
|
3756
|
-
return;
|
|
3757
3773
|
if (!msg.cost || msg.cost === 0)
|
|
3758
3774
|
return;
|
|
3759
3775
|
const wallet = await getWallet(msg.providerID);
|
|
@@ -3771,22 +3787,9 @@ var plugin_default = async (input) => {
|
|
|
3771
3787
|
created_at: new Date().toISOString(),
|
|
3772
3788
|
synced: 0
|
|
3773
3789
|
});
|
|
3774
|
-
const existing =
|
|
3775
|
-
if (existing)
|
|
3776
|
-
clearTimeout(existing.timer);
|
|
3790
|
+
const existing = sessions.get(msg.sessionID);
|
|
3777
3791
|
const totalCost = (existing?.totalCost ?? 0) + msg.cost;
|
|
3778
|
-
|
|
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 });
|
|
3792
|
+
sessions.set(msg.sessionID, { totalCost, lastBalance: newBalance, providerID: msg.providerID });
|
|
3790
3793
|
}
|
|
3791
3794
|
};
|
|
3792
3795
|
};
|