opencode-mailbox 0.0.6 → 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/index.js +78 -13
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12682,6 +12682,21 @@ import { Database } from "bun:sqlite";
|
|
|
12682
12682
|
var dbFile = null;
|
|
12683
12683
|
var db = null;
|
|
12684
12684
|
var activeWatches = new Map;
|
|
12685
|
+
var watchesBySession = new Map;
|
|
12686
|
+
function resetDatabaseConnection() {
|
|
12687
|
+
if (db) {
|
|
12688
|
+
try {
|
|
12689
|
+
db.close();
|
|
12690
|
+
} catch {}
|
|
12691
|
+
db = null;
|
|
12692
|
+
}
|
|
12693
|
+
}
|
|
12694
|
+
function isDatabaseFileError(error45) {
|
|
12695
|
+
if (!(error45 instanceof Error))
|
|
12696
|
+
return false;
|
|
12697
|
+
const message = error45.message.toLowerCase();
|
|
12698
|
+
return message.includes("database disk image is malformed") || message.includes("no such table") || message.includes("unable to open database file") || message.includes("database is locked") || message.includes("disk i/o error") || message.includes("no more rows available") || message.includes("unable to close") || message.includes("bad parameter or other api misuse");
|
|
12699
|
+
}
|
|
12685
12700
|
async function getDbFile(client) {
|
|
12686
12701
|
if (!dbFile) {
|
|
12687
12702
|
const result = await client.path.get();
|
|
@@ -12741,7 +12756,12 @@ async function markMessageAsRead(client, recipient, timestamp) {
|
|
|
12741
12756
|
stmt.run(recipient.toLowerCase(), timestamp);
|
|
12742
12757
|
}
|
|
12743
12758
|
function startMailWatch(client, recipient, sessionId, instructions) {
|
|
12744
|
-
|
|
12759
|
+
const existingWatch = activeWatches.get(recipient);
|
|
12760
|
+
if (existingWatch) {
|
|
12761
|
+
existingWatch.refCount++;
|
|
12762
|
+
const sessionWatches2 = watchesBySession.get(sessionId) ?? new Set;
|
|
12763
|
+
sessionWatches2.add(recipient);
|
|
12764
|
+
watchesBySession.set(sessionId, sessionWatches2);
|
|
12745
12765
|
return;
|
|
12746
12766
|
}
|
|
12747
12767
|
const interval = setInterval(async () => {
|
|
@@ -12759,10 +12779,18 @@ function startMailWatch(client, recipient, sessionId, instructions) {
|
|
|
12759
12779
|
await injectMailMessage(client, sessionId, recipient, message, instructions);
|
|
12760
12780
|
}
|
|
12761
12781
|
} catch (error45) {
|
|
12782
|
+
if (isDatabaseFileError(error45)) {
|
|
12783
|
+
console.error(`[Mailbox] Database file error for ${recipient}, resetting connection...`);
|
|
12784
|
+
resetDatabaseConnection();
|
|
12785
|
+
return;
|
|
12786
|
+
}
|
|
12762
12787
|
console.error(`[Mailbox] Error watching mail for ${recipient}:`, error45);
|
|
12763
12788
|
}
|
|
12764
12789
|
}, 5000);
|
|
12765
|
-
activeWatches.set(recipient, { interval, instructions });
|
|
12790
|
+
activeWatches.set(recipient, { interval, instructions, refCount: 1 });
|
|
12791
|
+
const sessionWatches = watchesBySession.get(sessionId) ?? new Set;
|
|
12792
|
+
sessionWatches.add(recipient);
|
|
12793
|
+
watchesBySession.set(sessionId, sessionWatches);
|
|
12766
12794
|
}
|
|
12767
12795
|
function stopMailWatch(recipient) {
|
|
12768
12796
|
const watch = activeWatches.get(recipient);
|
|
@@ -12799,7 +12827,12 @@ ${message.message}
|
|
|
12799
12827
|
await client.session.prompt({
|
|
12800
12828
|
path: { id: sessionId },
|
|
12801
12829
|
body: {
|
|
12802
|
-
parts: [
|
|
12830
|
+
parts: [
|
|
12831
|
+
{
|
|
12832
|
+
type: "text",
|
|
12833
|
+
text: "You have new mail. Please review the injected message above and respond accordingly."
|
|
12834
|
+
}
|
|
12835
|
+
]
|
|
12803
12836
|
}
|
|
12804
12837
|
});
|
|
12805
12838
|
}
|
|
@@ -12825,7 +12858,17 @@ var mailboxPlugin = async (ctx) => {
|
|
|
12825
12858
|
const to = args.to.toLowerCase();
|
|
12826
12859
|
const from = args.from.toLowerCase();
|
|
12827
12860
|
const timestamp = Date.now();
|
|
12828
|
-
|
|
12861
|
+
try {
|
|
12862
|
+
await addMessage(client, to, from, args.message, timestamp);
|
|
12863
|
+
} catch (error45) {
|
|
12864
|
+
if (isDatabaseFileError(error45)) {
|
|
12865
|
+
console.error(`[Mailbox] Database file error while sending mail, resetting connection...`);
|
|
12866
|
+
resetDatabaseConnection();
|
|
12867
|
+
await addMessage(client, to, from, args.message, timestamp);
|
|
12868
|
+
} else {
|
|
12869
|
+
throw error45;
|
|
12870
|
+
}
|
|
12871
|
+
}
|
|
12829
12872
|
return `Mail sent to "${args.to}" from "${args.from}" at ${new Date(timestamp).toISOString()}`;
|
|
12830
12873
|
}
|
|
12831
12874
|
});
|
|
@@ -12843,16 +12886,27 @@ var mailboxPlugin = async (ctx) => {
|
|
|
12843
12886
|
}
|
|
12844
12887
|
});
|
|
12845
12888
|
const stopWatchingMailTool = tool3({
|
|
12846
|
-
description: "Stop all mail watching",
|
|
12889
|
+
description: "Stop all mail watching for this session",
|
|
12847
12890
|
args: {},
|
|
12848
|
-
async execute() {
|
|
12891
|
+
async execute(_args, toolCtx) {
|
|
12892
|
+
const sessionId = toolCtx.sessionID;
|
|
12849
12893
|
const stoppedWatches = [];
|
|
12850
|
-
|
|
12851
|
-
|
|
12852
|
-
|
|
12894
|
+
const sessionWatches = watchesBySession.get(sessionId);
|
|
12895
|
+
if (sessionWatches) {
|
|
12896
|
+
for (const recipient of sessionWatches) {
|
|
12897
|
+
const watch = activeWatches.get(recipient);
|
|
12898
|
+
if (watch) {
|
|
12899
|
+
watch.refCount--;
|
|
12900
|
+
if (watch.refCount <= 0) {
|
|
12901
|
+
stopMailWatch(recipient);
|
|
12902
|
+
}
|
|
12903
|
+
stoppedWatches.push(recipient);
|
|
12904
|
+
}
|
|
12905
|
+
}
|
|
12906
|
+
watchesBySession.delete(sessionId);
|
|
12853
12907
|
}
|
|
12854
12908
|
if (stoppedWatches.length === 0) {
|
|
12855
|
-
return "No active mail watches found.";
|
|
12909
|
+
return "No active mail watches found for this session.";
|
|
12856
12910
|
}
|
|
12857
12911
|
return `Stopped watching mail for: ${stoppedWatches.join(", ")}`;
|
|
12858
12912
|
}
|
|
@@ -12869,10 +12923,21 @@ var mailboxPlugin = async (ctx) => {
|
|
|
12869
12923
|
input.experimental.primary_tools.push("send_mail", "watch_unread_mail", "stop_watching_mail");
|
|
12870
12924
|
},
|
|
12871
12925
|
hooks: {
|
|
12872
|
-
"session.end": async () => {
|
|
12873
|
-
|
|
12874
|
-
|
|
12926
|
+
"session.end": async (input) => {
|
|
12927
|
+
const sessionId = input.sessionID;
|
|
12928
|
+
const sessionWatches = watchesBySession.get(sessionId);
|
|
12929
|
+
if (!sessionWatches)
|
|
12930
|
+
return;
|
|
12931
|
+
for (const recipient of sessionWatches) {
|
|
12932
|
+
const watch = activeWatches.get(recipient);
|
|
12933
|
+
if (watch) {
|
|
12934
|
+
watch.refCount--;
|
|
12935
|
+
if (watch.refCount <= 0) {
|
|
12936
|
+
stopMailWatch(recipient);
|
|
12937
|
+
}
|
|
12938
|
+
}
|
|
12875
12939
|
}
|
|
12940
|
+
watchesBySession.delete(sessionId);
|
|
12876
12941
|
}
|
|
12877
12942
|
}
|
|
12878
12943
|
};
|