@kapishdima/ai-ledger 0.0.7 → 0.0.8
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 +20 -11
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// plugin.ts
|
|
3
|
+
import fs2 from "fs";
|
|
4
|
+
import os2 from "os";
|
|
5
|
+
import path2 from "path";
|
|
6
|
+
|
|
2
7
|
// lib/db.ts
|
|
3
8
|
import { Database as Database2 } from "bun:sqlite";
|
|
4
9
|
|
|
@@ -3739,16 +3744,20 @@ async function deductBalance(provider, cost) {
|
|
|
3739
3744
|
}
|
|
3740
3745
|
|
|
3741
3746
|
// plugin.ts
|
|
3747
|
+
var DEBUG_LOG = path2.join(os2.homedir(), ".config", "opencode", "ai-ledger", "debug.log");
|
|
3748
|
+
function dbg(msg) {
|
|
3749
|
+
const line = `[${new Date().toISOString()}] ${msg}
|
|
3750
|
+
`;
|
|
3751
|
+
fs2.appendFileSync(DEBUG_LOG, line);
|
|
3752
|
+
}
|
|
3742
3753
|
var sessions = new Map;
|
|
3743
3754
|
var plugin_default = async (input) => {
|
|
3744
3755
|
await initializeDatabase();
|
|
3745
|
-
|
|
3746
|
-
input.client.app.log({ body: { service: "ai-ledger", level: "info", message: msg } });
|
|
3747
|
-
}
|
|
3756
|
+
dbg("plugin initialized");
|
|
3748
3757
|
async function flushTurn(sessionID, state) {
|
|
3749
3758
|
sessions.delete(sessionID);
|
|
3750
3759
|
const text2 = `ai-ledger \xB7 ${state.providerID} \xB7 -$${state.totalCost.toFixed(4)} spent | $${state.lastBalance.toFixed(2)} remaining`;
|
|
3751
|
-
|
|
3760
|
+
dbg(`flushing turn for ${sessionID}: ${text2}`);
|
|
3752
3761
|
try {
|
|
3753
3762
|
const res = await input.client.session.prompt({
|
|
3754
3763
|
path: { id: sessionID },
|
|
@@ -3758,17 +3767,17 @@ var plugin_default = async (input) => {
|
|
|
3758
3767
|
parts: [{ type: "text", text: text2 }]
|
|
3759
3768
|
}
|
|
3760
3769
|
});
|
|
3761
|
-
|
|
3770
|
+
dbg(`session.prompt result: ${JSON.stringify(res)}`);
|
|
3762
3771
|
} catch (e) {
|
|
3763
|
-
|
|
3772
|
+
dbg(`session.prompt error: ${e?.message}`);
|
|
3764
3773
|
}
|
|
3765
3774
|
}
|
|
3766
3775
|
return {
|
|
3767
3776
|
event: async ({ event }) => {
|
|
3768
|
-
|
|
3777
|
+
dbg(`event: ${event.type}`);
|
|
3769
3778
|
if (event.type === "session.idle") {
|
|
3770
3779
|
const sessionID = event.properties?.sessionID;
|
|
3771
|
-
|
|
3780
|
+
dbg(`session.idle for ${sessionID}, accumulated: ${sessions.has(sessionID)}`);
|
|
3772
3781
|
const state = sessions.get(sessionID);
|
|
3773
3782
|
if (state)
|
|
3774
3783
|
await flushTurn(sessionID, state);
|
|
@@ -3781,11 +3790,11 @@ var plugin_default = async (input) => {
|
|
|
3781
3790
|
return;
|
|
3782
3791
|
if (msg.role !== "assistant")
|
|
3783
3792
|
return;
|
|
3784
|
-
|
|
3793
|
+
dbg(`assistant message: cost=${msg.cost} provider=${msg.providerID}`);
|
|
3785
3794
|
if (!msg.cost || msg.cost === 0)
|
|
3786
3795
|
return;
|
|
3787
3796
|
const wallet = await getWallet(msg.providerID);
|
|
3788
|
-
|
|
3797
|
+
dbg(`wallet for ${msg.providerID}: ${wallet ? "found" : "NOT FOUND"}`);
|
|
3789
3798
|
if (!wallet)
|
|
3790
3799
|
return;
|
|
3791
3800
|
const newBalance = await deductBalance(msg.providerID, msg.cost);
|
|
@@ -3803,7 +3812,7 @@ var plugin_default = async (input) => {
|
|
|
3803
3812
|
const existing = sessions.get(msg.sessionID);
|
|
3804
3813
|
const totalCost = (existing?.totalCost ?? 0) + msg.cost;
|
|
3805
3814
|
sessions.set(msg.sessionID, { totalCost, lastBalance: newBalance, providerID: msg.providerID });
|
|
3806
|
-
|
|
3815
|
+
dbg(`accumulated for session ${msg.sessionID}: totalCost=${totalCost}`);
|
|
3807
3816
|
}
|
|
3808
3817
|
};
|
|
3809
3818
|
};
|