opencode-mailbox 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/index.js +50 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -12682,6 +12682,7 @@ 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;
12685
12686
  async function getDbFile(client) {
12686
12687
  if (!dbFile) {
12687
12688
  const result = await client.path.get();
@@ -12741,7 +12742,12 @@ async function markMessageAsRead(client, recipient, timestamp) {
12741
12742
  stmt.run(recipient.toLowerCase(), timestamp);
12742
12743
  }
12743
12744
  function startMailWatch(client, recipient, sessionId, instructions) {
12744
- if (activeWatches.has(recipient)) {
12745
+ const existingWatch = activeWatches.get(recipient);
12746
+ if (existingWatch) {
12747
+ existingWatch.refCount++;
12748
+ const sessionWatches2 = watchesBySession.get(sessionId) ?? new Set;
12749
+ sessionWatches2.add(recipient);
12750
+ watchesBySession.set(sessionId, sessionWatches2);
12745
12751
  return;
12746
12752
  }
12747
12753
  const interval = setInterval(async () => {
@@ -12762,7 +12768,10 @@ function startMailWatch(client, recipient, sessionId, instructions) {
12762
12768
  console.error(`[Mailbox] Error watching mail for ${recipient}:`, error45);
12763
12769
  }
12764
12770
  }, 5000);
12765
- activeWatches.set(recipient, { interval, instructions });
12771
+ activeWatches.set(recipient, { interval, instructions, refCount: 1 });
12772
+ const sessionWatches = watchesBySession.get(sessionId) ?? new Set;
12773
+ sessionWatches.add(recipient);
12774
+ watchesBySession.set(sessionId, sessionWatches);
12766
12775
  }
12767
12776
  function stopMailWatch(recipient) {
12768
12777
  const watch = activeWatches.get(recipient);
@@ -12799,7 +12808,12 @@ ${message.message}
12799
12808
  await client.session.prompt({
12800
12809
  path: { id: sessionId },
12801
12810
  body: {
12802
- parts: [{ type: "text", text: "You have new mail. Please review the injected message above and respond accordingly." }]
12811
+ parts: [
12812
+ {
12813
+ type: "text",
12814
+ text: "You have new mail. Please review the injected message above and respond accordingly."
12815
+ }
12816
+ ]
12803
12817
  }
12804
12818
  });
12805
12819
  }
@@ -12815,7 +12829,7 @@ var mailboxPlugin = async (ctx) => {
12815
12829
  const { tool: tool3 } = await Promise.resolve().then(() => (init_dist(), exports_dist));
12816
12830
  const z = tool3.schema;
12817
12831
  const sendMailTool = tool3({
12818
- description: "Send a message to a recipient's mailbox",
12832
+ description: "Send a message to a recipient's mailbox. Note: The parameters 'to' and 'from' do NOT have to be an email. It can just be a name that the recipient watches for (e.g. 'samus').",
12819
12833
  args: {
12820
12834
  to: z.string().describe("Recipient name. Note, this does NOT have to be an email. It can just be a name that the recipient watches for it (e.g. 'samus')."),
12821
12835
  from: z.string().describe("Sender name. Note, this does NOT have to be an email. It can just be a name that the sender wants to appear as (e.g. 'link')."),
@@ -12830,7 +12844,7 @@ var mailboxPlugin = async (ctx) => {
12830
12844
  }
12831
12845
  });
12832
12846
  const watchUnreadMailTool = tool3({
12833
- description: "Create a hook that auto-injects messages when they are received for a specific name and can specify what should be done with the messages",
12847
+ description: "Create a hook that auto-injects messages when they are received for a specific name and can specify what should be done with the messages. Note: The parameters 'name' does NOT have to be an email. It can just be a name that the recipient watches for (e.g. 'samus').",
12834
12848
  args: {
12835
12849
  name: z.string().describe("Name of the recipient to watch. Note: this does NOT have to be an email. It can just be a name that the sender uses (e.g. 'samus')."),
12836
12850
  "what-to-do-with-it": z.string().describe("Instructions on how to process received messages")
@@ -12843,16 +12857,27 @@ var mailboxPlugin = async (ctx) => {
12843
12857
  }
12844
12858
  });
12845
12859
  const stopWatchingMailTool = tool3({
12846
- description: "Stop all mail watching",
12860
+ description: "Stop all mail watching for this session",
12847
12861
  args: {},
12848
- async execute() {
12862
+ async execute(_args, toolCtx) {
12863
+ const sessionId = toolCtx.sessionID;
12849
12864
  const stoppedWatches = [];
12850
- for (const recipient of activeWatches.keys()) {
12851
- stopMailWatch(recipient);
12852
- stoppedWatches.push(recipient);
12865
+ const sessionWatches = watchesBySession.get(sessionId);
12866
+ if (sessionWatches) {
12867
+ for (const recipient of sessionWatches) {
12868
+ const watch = activeWatches.get(recipient);
12869
+ if (watch) {
12870
+ watch.refCount--;
12871
+ if (watch.refCount <= 0) {
12872
+ stopMailWatch(recipient);
12873
+ }
12874
+ stoppedWatches.push(recipient);
12875
+ }
12876
+ }
12877
+ watchesBySession.delete(sessionId);
12853
12878
  }
12854
12879
  if (stoppedWatches.length === 0) {
12855
- return "No active mail watches found.";
12880
+ return "No active mail watches found for this session.";
12856
12881
  }
12857
12882
  return `Stopped watching mail for: ${stoppedWatches.join(", ")}`;
12858
12883
  }
@@ -12869,10 +12894,21 @@ var mailboxPlugin = async (ctx) => {
12869
12894
  input.experimental.primary_tools.push("send_mail", "watch_unread_mail", "stop_watching_mail");
12870
12895
  },
12871
12896
  hooks: {
12872
- "session.end": async () => {
12873
- for (const recipient of activeWatches.keys()) {
12874
- stopMailWatch(recipient);
12897
+ "session.end": async (input) => {
12898
+ const sessionId = input.sessionID;
12899
+ const sessionWatches = watchesBySession.get(sessionId);
12900
+ if (!sessionWatches)
12901
+ return;
12902
+ for (const recipient of sessionWatches) {
12903
+ const watch = activeWatches.get(recipient);
12904
+ if (watch) {
12905
+ watch.refCount--;
12906
+ if (watch.refCount <= 0) {
12907
+ stopMailWatch(recipient);
12908
+ }
12909
+ }
12875
12910
  }
12911
+ watchesBySession.delete(sessionId);
12876
12912
  }
12877
12913
  }
12878
12914
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-mailbox",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A simple mailbox system for sending and receiving messages between sessions",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",