opencode-mailbox 0.0.7 → 0.0.9
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 +45 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12683,6 +12683,20 @@ var dbFile = null;
|
|
|
12683
12683
|
var db = null;
|
|
12684
12684
|
var activeWatches = new Map;
|
|
12685
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
|
+
}
|
|
12686
12700
|
async function getDbFile(client) {
|
|
12687
12701
|
if (!dbFile) {
|
|
12688
12702
|
const result = await client.path.get();
|
|
@@ -12762,9 +12776,14 @@ function startMailWatch(client, recipient, sessionId, instructions) {
|
|
|
12762
12776
|
}
|
|
12763
12777
|
for (const message of unreadMessages) {
|
|
12764
12778
|
await markMessageAsRead(client, recipient, message.timestamp);
|
|
12765
|
-
await injectMailMessage(client, sessionId, recipient, message, instructions);
|
|
12766
12779
|
}
|
|
12780
|
+
await injectMailMessages(client, sessionId, recipient, unreadMessages, instructions);
|
|
12767
12781
|
} catch (error45) {
|
|
12782
|
+
if (isDatabaseFileError(error45)) {
|
|
12783
|
+
console.error(`[Mailbox] Database file error for ${recipient}, resetting connection...`);
|
|
12784
|
+
resetDatabaseConnection();
|
|
12785
|
+
return;
|
|
12786
|
+
}
|
|
12768
12787
|
console.error(`[Mailbox] Error watching mail for ${recipient}:`, error45);
|
|
12769
12788
|
}
|
|
12770
12789
|
}, 5000);
|
|
@@ -12780,15 +12799,25 @@ function stopMailWatch(recipient) {
|
|
|
12780
12799
|
activeWatches.delete(recipient);
|
|
12781
12800
|
}
|
|
12782
12801
|
}
|
|
12783
|
-
async function
|
|
12784
|
-
|
|
12785
|
-
|
|
12802
|
+
async function injectMailMessages(client, sessionId, recipient, messages, instructions) {
|
|
12803
|
+
let injectedText = `[MAIL BATCH] You have ${messages.length} new message(s) for ${recipient}
|
|
12804
|
+
|
|
12805
|
+
`;
|
|
12806
|
+
for (const message of messages) {
|
|
12807
|
+
const timestamp = new Date(message.timestamp).toISOString();
|
|
12808
|
+
injectedText += `---
|
|
12809
|
+
From: ${message.from}
|
|
12786
12810
|
To: ${recipient}
|
|
12787
12811
|
Time: ${timestamp}
|
|
12788
12812
|
|
|
12789
12813
|
${message.message}
|
|
12790
12814
|
|
|
12791
|
-
|
|
12815
|
+
`;
|
|
12816
|
+
}
|
|
12817
|
+
injectedText += `---
|
|
12818
|
+
[Instructions: ${instructions}]
|
|
12819
|
+
|
|
12820
|
+
IMPORTANT: remember in order for a sender to see your response, you must send them a mail back`;
|
|
12792
12821
|
try {
|
|
12793
12822
|
await client.session.prompt({
|
|
12794
12823
|
path: { id: sessionId },
|
|
@@ -12839,7 +12868,17 @@ var mailboxPlugin = async (ctx) => {
|
|
|
12839
12868
|
const to = args.to.toLowerCase();
|
|
12840
12869
|
const from = args.from.toLowerCase();
|
|
12841
12870
|
const timestamp = Date.now();
|
|
12842
|
-
|
|
12871
|
+
try {
|
|
12872
|
+
await addMessage(client, to, from, args.message, timestamp);
|
|
12873
|
+
} catch (error45) {
|
|
12874
|
+
if (isDatabaseFileError(error45)) {
|
|
12875
|
+
console.error(`[Mailbox] Database file error while sending mail, resetting connection...`);
|
|
12876
|
+
resetDatabaseConnection();
|
|
12877
|
+
await addMessage(client, to, from, args.message, timestamp);
|
|
12878
|
+
} else {
|
|
12879
|
+
throw error45;
|
|
12880
|
+
}
|
|
12881
|
+
}
|
|
12843
12882
|
return `Mail sent to "${args.to}" from "${args.from}" at ${new Date(timestamp).toISOString()}`;
|
|
12844
12883
|
}
|
|
12845
12884
|
});
|