opencode-mailbox 0.0.0 → 0.0.1

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 (3) hide show
  1. package/README.md +7 -4
  2. package/dist/index.js +845 -44
  3. package/package.json +3 -1
package/README.md CHANGED
@@ -4,7 +4,9 @@ A simple mailbox system for sending and receiving messages between sessions.
4
4
 
5
5
  ## Description
6
6
 
7
- This OpenCode plugin provides a lightweight mailbox system that allows sessions to send messages to each other asynchronously. Messages are stored efficiently, indexed first by recipient and then by timestamp.
7
+ This OpenCode plugin provides a lightweight mailbox system that allows sessions to send messages to each other asynchronously. Messages are stored in a SQLite database with proper indexing for fast lookups.
8
+
9
+ **NOTE: Mail is stored in `~/.config/opencode/mailbox.db`**
8
10
 
9
11
  ## Installation
10
12
 
@@ -58,9 +60,10 @@ Stop all mail watching for the current session.
58
60
 
59
61
  ## Storage
60
62
 
61
- Mail data is persisted in the OpenCode config directory as `mailbox.json`. The storage is optimized with an index structure:
62
- - First index: recipient (who the mail is to)
63
- - Second index: timestamp (when the mail was received)
63
+ Mail data is persisted in a SQLite database at `~/.config/opencode/mailbox.db`. The database includes:
64
+ - Indexed `recipient` column for fast recipient lookups
65
+ - Indexed `read` status for efficient watch queries
66
+ - WAL (Write-Ahead Logging) mode for better concurrency
64
67
 
65
68
  ## Session Management
66
69
 
package/dist/index.js CHANGED
@@ -1,5 +1,21 @@
1
1
  // @bun
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
2
4
  var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
3
19
  var __export = (target, all) => {
4
20
  for (var name in all)
5
21
  __defProp(target, name, {
@@ -10,6 +26,772 @@ var __export = (target, all) => {
10
26
  });
11
27
  };
12
28
  var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
29
+ var __require = import.meta.require;
30
+
31
+ // node_modules/better-sqlite3/lib/util.js
32
+ var require_util = __commonJS((exports) => {
33
+ exports.getBooleanOption = (options, key) => {
34
+ let value = false;
35
+ if (key in options && typeof (value = options[key]) !== "boolean") {
36
+ throw new TypeError(`Expected the "${key}" option to be a boolean`);
37
+ }
38
+ return value;
39
+ };
40
+ exports.cppdb = Symbol();
41
+ exports.inspect = Symbol.for("nodejs.util.inspect.custom");
42
+ });
43
+
44
+ // node_modules/better-sqlite3/lib/sqlite-error.js
45
+ var require_sqlite_error = __commonJS((exports, module) => {
46
+ var descriptor = { value: "SqliteError", writable: true, enumerable: false, configurable: true };
47
+ function SqliteError(message, code) {
48
+ if (new.target !== SqliteError) {
49
+ return new SqliteError(message, code);
50
+ }
51
+ if (typeof code !== "string") {
52
+ throw new TypeError("Expected second argument to be a string");
53
+ }
54
+ Error.call(this, message);
55
+ descriptor.value = "" + message;
56
+ Object.defineProperty(this, "message", descriptor);
57
+ Error.captureStackTrace(this, SqliteError);
58
+ this.code = code;
59
+ }
60
+ Object.setPrototypeOf(SqliteError, Error);
61
+ Object.setPrototypeOf(SqliteError.prototype, Error.prototype);
62
+ Object.defineProperty(SqliteError.prototype, "name", descriptor);
63
+ module.exports = SqliteError;
64
+ });
65
+
66
+ // node_modules/file-uri-to-path/index.js
67
+ var require_file_uri_to_path = __commonJS((exports, module) => {
68
+ var sep = __require("path").sep || "/";
69
+ module.exports = fileUriToPath;
70
+ function fileUriToPath(uri) {
71
+ if (typeof uri != "string" || uri.length <= 7 || uri.substring(0, 7) != "file://") {
72
+ throw new TypeError("must pass in a file:// URI to convert to a file path");
73
+ }
74
+ var rest = decodeURI(uri.substring(7));
75
+ var firstSlash = rest.indexOf("/");
76
+ var host = rest.substring(0, firstSlash);
77
+ var path = rest.substring(firstSlash + 1);
78
+ if (host == "localhost")
79
+ host = "";
80
+ if (host) {
81
+ host = sep + sep + host;
82
+ }
83
+ path = path.replace(/^(.+)\|/, "$1:");
84
+ if (sep == "\\") {
85
+ path = path.replace(/\//g, "\\");
86
+ }
87
+ if (/^.+\:/.test(path)) {} else {
88
+ path = sep + path;
89
+ }
90
+ return host + path;
91
+ }
92
+ });
93
+
94
+ // node_modules/bindings/bindings.js
95
+ var require_bindings = __commonJS((exports, module) => {
96
+ var __filename = "/var/home/wizard/opencode-mailbox/node_modules/bindings/bindings.js";
97
+ var fs = __require("fs");
98
+ var path = __require("path");
99
+ var fileURLToPath = require_file_uri_to_path();
100
+ var join = path.join;
101
+ var dirname = path.dirname;
102
+ var exists = fs.accessSync && function(path2) {
103
+ try {
104
+ fs.accessSync(path2);
105
+ } catch (e) {
106
+ return false;
107
+ }
108
+ return true;
109
+ } || fs.existsSync || path.existsSync;
110
+ var defaults = {
111
+ arrow: process.env.NODE_BINDINGS_ARROW || " \u2192 ",
112
+ compiled: process.env.NODE_BINDINGS_COMPILED_DIR || "compiled",
113
+ platform: process.platform,
114
+ arch: process.arch,
115
+ nodePreGyp: "node-v" + process.versions.modules + "-" + process.platform + "-" + process.arch,
116
+ version: process.versions.node,
117
+ bindings: "bindings.node",
118
+ try: [
119
+ ["module_root", "build", "bindings"],
120
+ ["module_root", "build", "Debug", "bindings"],
121
+ ["module_root", "build", "Release", "bindings"],
122
+ ["module_root", "out", "Debug", "bindings"],
123
+ ["module_root", "Debug", "bindings"],
124
+ ["module_root", "out", "Release", "bindings"],
125
+ ["module_root", "Release", "bindings"],
126
+ ["module_root", "build", "default", "bindings"],
127
+ ["module_root", "compiled", "version", "platform", "arch", "bindings"],
128
+ ["module_root", "addon-build", "release", "install-root", "bindings"],
129
+ ["module_root", "addon-build", "debug", "install-root", "bindings"],
130
+ ["module_root", "addon-build", "default", "install-root", "bindings"],
131
+ ["module_root", "lib", "binding", "nodePreGyp", "bindings"]
132
+ ]
133
+ };
134
+ function bindings(opts) {
135
+ if (typeof opts == "string") {
136
+ opts = { bindings: opts };
137
+ } else if (!opts) {
138
+ opts = {};
139
+ }
140
+ Object.keys(defaults).map(function(i2) {
141
+ if (!(i2 in opts))
142
+ opts[i2] = defaults[i2];
143
+ });
144
+ if (!opts.module_root) {
145
+ opts.module_root = exports.getRoot(exports.getFileName());
146
+ }
147
+ if (path.extname(opts.bindings) != ".node") {
148
+ opts.bindings += ".node";
149
+ }
150
+ var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
151
+ var tries = [], i = 0, l = opts.try.length, n, b, err;
152
+ for (;i < l; i++) {
153
+ n = join.apply(null, opts.try[i].map(function(p) {
154
+ return opts[p] || p;
155
+ }));
156
+ tries.push(n);
157
+ try {
158
+ b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
159
+ if (!opts.path) {
160
+ b.path = n;
161
+ }
162
+ return b;
163
+ } catch (e) {
164
+ if (e.code !== "MODULE_NOT_FOUND" && e.code !== "QUALIFIED_PATH_RESOLUTION_FAILED" && !/not find/i.test(e.message)) {
165
+ throw e;
166
+ }
167
+ }
168
+ }
169
+ err = new Error(`Could not locate the bindings file. Tried:
170
+ ` + tries.map(function(a) {
171
+ return opts.arrow + a;
172
+ }).join(`
173
+ `));
174
+ err.tries = tries;
175
+ throw err;
176
+ }
177
+ module.exports = exports = bindings;
178
+ exports.getFileName = function getFileName(calling_file) {
179
+ var { prepareStackTrace: origPST, stackTraceLimit: origSTL } = Error, dummy = {}, fileName;
180
+ Error.stackTraceLimit = 10;
181
+ Error.prepareStackTrace = function(e, st) {
182
+ for (var i = 0, l = st.length;i < l; i++) {
183
+ fileName = st[i].getFileName();
184
+ if (fileName !== __filename) {
185
+ if (calling_file) {
186
+ if (fileName !== calling_file) {
187
+ return;
188
+ }
189
+ } else {
190
+ return;
191
+ }
192
+ }
193
+ }
194
+ };
195
+ Error.captureStackTrace(dummy);
196
+ dummy.stack;
197
+ Error.prepareStackTrace = origPST;
198
+ Error.stackTraceLimit = origSTL;
199
+ var fileSchema = "file://";
200
+ if (fileName.indexOf(fileSchema) === 0) {
201
+ fileName = fileURLToPath(fileName);
202
+ }
203
+ return fileName;
204
+ };
205
+ exports.getRoot = function getRoot(file) {
206
+ var dir = dirname(file), prev;
207
+ while (true) {
208
+ if (dir === ".") {
209
+ dir = process.cwd();
210
+ }
211
+ if (exists(join(dir, "package.json")) || exists(join(dir, "node_modules"))) {
212
+ return dir;
213
+ }
214
+ if (prev === dir) {
215
+ throw new Error('Could not find module root given file: "' + file + '". Do you have a `package.json` file? ');
216
+ }
217
+ prev = dir;
218
+ dir = join(dir, "..");
219
+ }
220
+ };
221
+ });
222
+
223
+ // node_modules/better-sqlite3/lib/methods/wrappers.js
224
+ var require_wrappers = __commonJS((exports) => {
225
+ var { cppdb } = require_util();
226
+ exports.prepare = function prepare(sql) {
227
+ return this[cppdb].prepare(sql, this, false);
228
+ };
229
+ exports.exec = function exec(sql) {
230
+ this[cppdb].exec(sql);
231
+ return this;
232
+ };
233
+ exports.close = function close() {
234
+ this[cppdb].close();
235
+ return this;
236
+ };
237
+ exports.loadExtension = function loadExtension(...args) {
238
+ this[cppdb].loadExtension(...args);
239
+ return this;
240
+ };
241
+ exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
242
+ this[cppdb].defaultSafeIntegers(...args);
243
+ return this;
244
+ };
245
+ exports.unsafeMode = function unsafeMode(...args) {
246
+ this[cppdb].unsafeMode(...args);
247
+ return this;
248
+ };
249
+ exports.getters = {
250
+ name: {
251
+ get: function name() {
252
+ return this[cppdb].name;
253
+ },
254
+ enumerable: true
255
+ },
256
+ open: {
257
+ get: function open() {
258
+ return this[cppdb].open;
259
+ },
260
+ enumerable: true
261
+ },
262
+ inTransaction: {
263
+ get: function inTransaction() {
264
+ return this[cppdb].inTransaction;
265
+ },
266
+ enumerable: true
267
+ },
268
+ readonly: {
269
+ get: function readonly() {
270
+ return this[cppdb].readonly;
271
+ },
272
+ enumerable: true
273
+ },
274
+ memory: {
275
+ get: function memory() {
276
+ return this[cppdb].memory;
277
+ },
278
+ enumerable: true
279
+ }
280
+ };
281
+ });
282
+
283
+ // node_modules/better-sqlite3/lib/methods/transaction.js
284
+ var require_transaction = __commonJS((exports, module) => {
285
+ var { cppdb } = require_util();
286
+ var controllers = new WeakMap;
287
+ module.exports = function transaction(fn) {
288
+ if (typeof fn !== "function")
289
+ throw new TypeError("Expected first argument to be a function");
290
+ const db = this[cppdb];
291
+ const controller = getController(db, this);
292
+ const { apply } = Function.prototype;
293
+ const properties = {
294
+ default: { value: wrapTransaction(apply, fn, db, controller.default) },
295
+ deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
296
+ immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
297
+ exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
298
+ database: { value: this, enumerable: true }
299
+ };
300
+ Object.defineProperties(properties.default.value, properties);
301
+ Object.defineProperties(properties.deferred.value, properties);
302
+ Object.defineProperties(properties.immediate.value, properties);
303
+ Object.defineProperties(properties.exclusive.value, properties);
304
+ return properties.default.value;
305
+ };
306
+ var getController = (db, self) => {
307
+ let controller = controllers.get(db);
308
+ if (!controller) {
309
+ const shared = {
310
+ commit: db.prepare("COMMIT", self, false),
311
+ rollback: db.prepare("ROLLBACK", self, false),
312
+ savepoint: db.prepare("SAVEPOINT `\t_bs3.\t`", self, false),
313
+ release: db.prepare("RELEASE `\t_bs3.\t`", self, false),
314
+ rollbackTo: db.prepare("ROLLBACK TO `\t_bs3.\t`", self, false)
315
+ };
316
+ controllers.set(db, controller = {
317
+ default: Object.assign({ begin: db.prepare("BEGIN", self, false) }, shared),
318
+ deferred: Object.assign({ begin: db.prepare("BEGIN DEFERRED", self, false) }, shared),
319
+ immediate: Object.assign({ begin: db.prepare("BEGIN IMMEDIATE", self, false) }, shared),
320
+ exclusive: Object.assign({ begin: db.prepare("BEGIN EXCLUSIVE", self, false) }, shared)
321
+ });
322
+ }
323
+ return controller;
324
+ };
325
+ var wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() {
326
+ let before, after, undo;
327
+ if (db.inTransaction) {
328
+ before = savepoint;
329
+ after = release;
330
+ undo = rollbackTo;
331
+ } else {
332
+ before = begin;
333
+ after = commit;
334
+ undo = rollback;
335
+ }
336
+ before.run();
337
+ try {
338
+ const result = apply.call(fn, this, arguments);
339
+ if (result && typeof result.then === "function") {
340
+ throw new TypeError("Transaction function cannot return a promise");
341
+ }
342
+ after.run();
343
+ return result;
344
+ } catch (ex) {
345
+ if (db.inTransaction) {
346
+ undo.run();
347
+ if (undo !== rollback)
348
+ after.run();
349
+ }
350
+ throw ex;
351
+ }
352
+ };
353
+ });
354
+
355
+ // node_modules/better-sqlite3/lib/methods/pragma.js
356
+ var require_pragma = __commonJS((exports, module) => {
357
+ var { getBooleanOption, cppdb } = require_util();
358
+ module.exports = function pragma(source, options) {
359
+ if (options == null)
360
+ options = {};
361
+ if (typeof source !== "string")
362
+ throw new TypeError("Expected first argument to be a string");
363
+ if (typeof options !== "object")
364
+ throw new TypeError("Expected second argument to be an options object");
365
+ const simple = getBooleanOption(options, "simple");
366
+ const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true);
367
+ return simple ? stmt.pluck().get() : stmt.all();
368
+ };
369
+ });
370
+
371
+ // node_modules/better-sqlite3/lib/methods/backup.js
372
+ var require_backup = __commonJS((exports, module) => {
373
+ var fs = __require("fs");
374
+ var path = __require("path");
375
+ var { promisify } = __require("util");
376
+ var { cppdb } = require_util();
377
+ var fsAccess = promisify(fs.access);
378
+ module.exports = async function backup(filename, options) {
379
+ if (options == null)
380
+ options = {};
381
+ if (typeof filename !== "string")
382
+ throw new TypeError("Expected first argument to be a string");
383
+ if (typeof options !== "object")
384
+ throw new TypeError("Expected second argument to be an options object");
385
+ filename = filename.trim();
386
+ const attachedName = "attached" in options ? options.attached : "main";
387
+ const handler = "progress" in options ? options.progress : null;
388
+ if (!filename)
389
+ throw new TypeError("Backup filename cannot be an empty string");
390
+ if (filename === ":memory:")
391
+ throw new TypeError('Invalid backup filename ":memory:"');
392
+ if (typeof attachedName !== "string")
393
+ throw new TypeError('Expected the "attached" option to be a string');
394
+ if (!attachedName)
395
+ throw new TypeError('The "attached" option cannot be an empty string');
396
+ if (handler != null && typeof handler !== "function")
397
+ throw new TypeError('Expected the "progress" option to be a function');
398
+ await fsAccess(path.dirname(filename)).catch(() => {
399
+ throw new TypeError("Cannot save backup because the directory does not exist");
400
+ });
401
+ const isNewFile = await fsAccess(filename).then(() => false, () => true);
402
+ return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
403
+ };
404
+ var runBackup = (backup, handler) => {
405
+ let rate = 0;
406
+ let useDefault = true;
407
+ return new Promise((resolve, reject) => {
408
+ setImmediate(function step() {
409
+ try {
410
+ const progress = backup.transfer(rate);
411
+ if (!progress.remainingPages) {
412
+ backup.close();
413
+ resolve(progress);
414
+ return;
415
+ }
416
+ if (useDefault) {
417
+ useDefault = false;
418
+ rate = 100;
419
+ }
420
+ if (handler) {
421
+ const ret = handler(progress);
422
+ if (ret !== undefined) {
423
+ if (typeof ret === "number" && ret === ret)
424
+ rate = Math.max(0, Math.min(2147483647, Math.round(ret)));
425
+ else
426
+ throw new TypeError("Expected progress callback to return a number or undefined");
427
+ }
428
+ }
429
+ setImmediate(step);
430
+ } catch (err) {
431
+ backup.close();
432
+ reject(err);
433
+ }
434
+ });
435
+ });
436
+ };
437
+ });
438
+
439
+ // node_modules/better-sqlite3/lib/methods/serialize.js
440
+ var require_serialize = __commonJS((exports, module) => {
441
+ var { cppdb } = require_util();
442
+ module.exports = function serialize(options) {
443
+ if (options == null)
444
+ options = {};
445
+ if (typeof options !== "object")
446
+ throw new TypeError("Expected first argument to be an options object");
447
+ const attachedName = "attached" in options ? options.attached : "main";
448
+ if (typeof attachedName !== "string")
449
+ throw new TypeError('Expected the "attached" option to be a string');
450
+ if (!attachedName)
451
+ throw new TypeError('The "attached" option cannot be an empty string');
452
+ return this[cppdb].serialize(attachedName);
453
+ };
454
+ });
455
+
456
+ // node_modules/better-sqlite3/lib/methods/function.js
457
+ var require_function = __commonJS((exports, module) => {
458
+ var { getBooleanOption, cppdb } = require_util();
459
+ module.exports = function defineFunction(name, options, fn) {
460
+ if (options == null)
461
+ options = {};
462
+ if (typeof options === "function") {
463
+ fn = options;
464
+ options = {};
465
+ }
466
+ if (typeof name !== "string")
467
+ throw new TypeError("Expected first argument to be a string");
468
+ if (typeof fn !== "function")
469
+ throw new TypeError("Expected last argument to be a function");
470
+ if (typeof options !== "object")
471
+ throw new TypeError("Expected second argument to be an options object");
472
+ if (!name)
473
+ throw new TypeError("User-defined function name cannot be an empty string");
474
+ const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
475
+ const deterministic = getBooleanOption(options, "deterministic");
476
+ const directOnly = getBooleanOption(options, "directOnly");
477
+ const varargs = getBooleanOption(options, "varargs");
478
+ let argCount = -1;
479
+ if (!varargs) {
480
+ argCount = fn.length;
481
+ if (!Number.isInteger(argCount) || argCount < 0)
482
+ throw new TypeError("Expected function.length to be a positive integer");
483
+ if (argCount > 100)
484
+ throw new RangeError("User-defined functions cannot have more than 100 arguments");
485
+ }
486
+ this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
487
+ return this;
488
+ };
489
+ });
490
+
491
+ // node_modules/better-sqlite3/lib/methods/aggregate.js
492
+ var require_aggregate = __commonJS((exports, module) => {
493
+ var { getBooleanOption, cppdb } = require_util();
494
+ module.exports = function defineAggregate(name, options) {
495
+ if (typeof name !== "string")
496
+ throw new TypeError("Expected first argument to be a string");
497
+ if (typeof options !== "object" || options === null)
498
+ throw new TypeError("Expected second argument to be an options object");
499
+ if (!name)
500
+ throw new TypeError("User-defined function name cannot be an empty string");
501
+ const start = "start" in options ? options.start : null;
502
+ const step = getFunctionOption(options, "step", true);
503
+ const inverse = getFunctionOption(options, "inverse", false);
504
+ const result = getFunctionOption(options, "result", false);
505
+ const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
506
+ const deterministic = getBooleanOption(options, "deterministic");
507
+ const directOnly = getBooleanOption(options, "directOnly");
508
+ const varargs = getBooleanOption(options, "varargs");
509
+ let argCount = -1;
510
+ if (!varargs) {
511
+ argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
512
+ if (argCount > 0)
513
+ argCount -= 1;
514
+ if (argCount > 100)
515
+ throw new RangeError("User-defined functions cannot have more than 100 arguments");
516
+ }
517
+ this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
518
+ return this;
519
+ };
520
+ var getFunctionOption = (options, key, required) => {
521
+ const value = key in options ? options[key] : null;
522
+ if (typeof value === "function")
523
+ return value;
524
+ if (value != null)
525
+ throw new TypeError(`Expected the "${key}" option to be a function`);
526
+ if (required)
527
+ throw new TypeError(`Missing required option "${key}"`);
528
+ return null;
529
+ };
530
+ var getLength = ({ length }) => {
531
+ if (Number.isInteger(length) && length >= 0)
532
+ return length;
533
+ throw new TypeError("Expected function.length to be a positive integer");
534
+ };
535
+ });
536
+
537
+ // node_modules/better-sqlite3/lib/methods/table.js
538
+ var require_table = __commonJS((exports, module) => {
539
+ var { cppdb } = require_util();
540
+ module.exports = function defineTable(name, factory) {
541
+ if (typeof name !== "string")
542
+ throw new TypeError("Expected first argument to be a string");
543
+ if (!name)
544
+ throw new TypeError("Virtual table module name cannot be an empty string");
545
+ let eponymous = false;
546
+ if (typeof factory === "object" && factory !== null) {
547
+ eponymous = true;
548
+ factory = defer(parseTableDefinition(factory, "used", name));
549
+ } else {
550
+ if (typeof factory !== "function")
551
+ throw new TypeError("Expected second argument to be a function or a table definition object");
552
+ factory = wrapFactory(factory);
553
+ }
554
+ this[cppdb].table(factory, name, eponymous);
555
+ return this;
556
+ };
557
+ function wrapFactory(factory) {
558
+ return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
559
+ const thisObject = {
560
+ module: moduleName,
561
+ database: databaseName,
562
+ table: tableName
563
+ };
564
+ const def = apply.call(factory, thisObject, args);
565
+ if (typeof def !== "object" || def === null) {
566
+ throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
567
+ }
568
+ return parseTableDefinition(def, "returned", moduleName);
569
+ };
570
+ }
571
+ function parseTableDefinition(def, verb, moduleName) {
572
+ if (!hasOwnProperty.call(def, "rows")) {
573
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
574
+ }
575
+ if (!hasOwnProperty.call(def, "columns")) {
576
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
577
+ }
578
+ const rows = def.rows;
579
+ if (typeof rows !== "function" || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
580
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
581
+ }
582
+ let columns = def.columns;
583
+ if (!Array.isArray(columns) || !(columns = [...columns]).every((x) => typeof x === "string")) {
584
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
585
+ }
586
+ if (columns.length !== new Set(columns).size) {
587
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
588
+ }
589
+ if (!columns.length) {
590
+ throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
591
+ }
592
+ let parameters;
593
+ if (hasOwnProperty.call(def, "parameters")) {
594
+ parameters = def.parameters;
595
+ if (!Array.isArray(parameters) || !(parameters = [...parameters]).every((x) => typeof x === "string")) {
596
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
597
+ }
598
+ } else {
599
+ parameters = inferParameters(rows);
600
+ }
601
+ if (parameters.length !== new Set(parameters).size) {
602
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
603
+ }
604
+ if (parameters.length > 32) {
605
+ throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
606
+ }
607
+ for (const parameter of parameters) {
608
+ if (columns.includes(parameter)) {
609
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
610
+ }
611
+ }
612
+ let safeIntegers = 2;
613
+ if (hasOwnProperty.call(def, "safeIntegers")) {
614
+ const bool = def.safeIntegers;
615
+ if (typeof bool !== "boolean") {
616
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
617
+ }
618
+ safeIntegers = +bool;
619
+ }
620
+ let directOnly = false;
621
+ if (hasOwnProperty.call(def, "directOnly")) {
622
+ directOnly = def.directOnly;
623
+ if (typeof directOnly !== "boolean") {
624
+ throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
625
+ }
626
+ }
627
+ const columnDefinitions = [
628
+ ...parameters.map(identifier).map((str) => `${str} HIDDEN`),
629
+ ...columns.map(identifier)
630
+ ];
631
+ return [
632
+ `CREATE TABLE x(${columnDefinitions.join(", ")});`,
633
+ wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
634
+ parameters,
635
+ safeIntegers,
636
+ directOnly
637
+ ];
638
+ }
639
+ function wrapGenerator(generator, columnMap, moduleName) {
640
+ return function* virtualTable(...args) {
641
+ const output = args.map((x) => Buffer.isBuffer(x) ? Buffer.from(x) : x);
642
+ for (let i = 0;i < columnMap.size; ++i) {
643
+ output.push(null);
644
+ }
645
+ for (const row of generator(...args)) {
646
+ if (Array.isArray(row)) {
647
+ extractRowArray(row, output, columnMap.size, moduleName);
648
+ yield output;
649
+ } else if (typeof row === "object" && row !== null) {
650
+ extractRowObject(row, output, columnMap, moduleName);
651
+ yield output;
652
+ } else {
653
+ throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
654
+ }
655
+ }
656
+ };
657
+ }
658
+ function extractRowArray(row, output, columnCount, moduleName) {
659
+ if (row.length !== columnCount) {
660
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
661
+ }
662
+ const offset = output.length - columnCount;
663
+ for (let i = 0;i < columnCount; ++i) {
664
+ output[i + offset] = row[i];
665
+ }
666
+ }
667
+ function extractRowObject(row, output, columnMap, moduleName) {
668
+ let count = 0;
669
+ for (const key of Object.keys(row)) {
670
+ const index = columnMap.get(key);
671
+ if (index === undefined) {
672
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
673
+ }
674
+ output[index] = row[key];
675
+ count += 1;
676
+ }
677
+ if (count !== columnMap.size) {
678
+ throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
679
+ }
680
+ }
681
+ function inferParameters({ length }) {
682
+ if (!Number.isInteger(length) || length < 0) {
683
+ throw new TypeError("Expected function.length to be a positive integer");
684
+ }
685
+ const params = [];
686
+ for (let i = 0;i < length; ++i) {
687
+ params.push(`$${i + 1}`);
688
+ }
689
+ return params;
690
+ }
691
+ var { hasOwnProperty } = Object.prototype;
692
+ var { apply } = Function.prototype;
693
+ var GeneratorFunctionPrototype = Object.getPrototypeOf(function* () {});
694
+ var identifier = (str) => `"${str.replace(/"/g, '""')}"`;
695
+ var defer = (x) => () => x;
696
+ });
697
+
698
+ // node_modules/better-sqlite3/lib/methods/inspect.js
699
+ var require_inspect = __commonJS((exports, module) => {
700
+ var DatabaseInspection = function Database() {};
701
+ module.exports = function inspect(depth, opts) {
702
+ return Object.assign(new DatabaseInspection, this);
703
+ };
704
+ });
705
+
706
+ // node_modules/better-sqlite3/lib/database.js
707
+ var require_database = __commonJS((exports, module) => {
708
+ var fs = __require("fs");
709
+ var path = __require("path");
710
+ var util = require_util();
711
+ var SqliteError = require_sqlite_error();
712
+ var DEFAULT_ADDON;
713
+ function Database(filenameGiven, options) {
714
+ if (new.target == null) {
715
+ return new Database(filenameGiven, options);
716
+ }
717
+ let buffer;
718
+ if (Buffer.isBuffer(filenameGiven)) {
719
+ buffer = filenameGiven;
720
+ filenameGiven = ":memory:";
721
+ }
722
+ if (filenameGiven == null)
723
+ filenameGiven = "";
724
+ if (options == null)
725
+ options = {};
726
+ if (typeof filenameGiven !== "string")
727
+ throw new TypeError("Expected first argument to be a string");
728
+ if (typeof options !== "object")
729
+ throw new TypeError("Expected second argument to be an options object");
730
+ if ("readOnly" in options)
731
+ throw new TypeError('Misspelled option "readOnly" should be "readonly"');
732
+ if ("memory" in options)
733
+ throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
734
+ const filename = filenameGiven.trim();
735
+ const anonymous = filename === "" || filename === ":memory:";
736
+ const readonly = util.getBooleanOption(options, "readonly");
737
+ const fileMustExist = util.getBooleanOption(options, "fileMustExist");
738
+ const timeout = "timeout" in options ? options.timeout : 5000;
739
+ const verbose = "verbose" in options ? options.verbose : null;
740
+ const nativeBinding = "nativeBinding" in options ? options.nativeBinding : null;
741
+ if (readonly && anonymous && !buffer)
742
+ throw new TypeError("In-memory/temporary databases cannot be readonly");
743
+ if (!Number.isInteger(timeout) || timeout < 0)
744
+ throw new TypeError('Expected the "timeout" option to be a positive integer');
745
+ if (timeout > 2147483647)
746
+ throw new RangeError('Option "timeout" cannot be greater than 2147483647');
747
+ if (verbose != null && typeof verbose !== "function")
748
+ throw new TypeError('Expected the "verbose" option to be a function');
749
+ if (nativeBinding != null && typeof nativeBinding !== "string" && typeof nativeBinding !== "object")
750
+ throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
751
+ let addon;
752
+ if (nativeBinding == null) {
753
+ addon = DEFAULT_ADDON || (DEFAULT_ADDON = require_bindings()("better_sqlite3.node"));
754
+ } else if (typeof nativeBinding === "string") {
755
+ const requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : __require;
756
+ addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, ".node"));
757
+ } else {
758
+ addon = nativeBinding;
759
+ }
760
+ if (!addon.isInitialized) {
761
+ addon.setErrorConstructor(SqliteError);
762
+ addon.isInitialized = true;
763
+ }
764
+ if (!anonymous && !filename.startsWith("file:") && !fs.existsSync(path.dirname(filename))) {
765
+ throw new TypeError("Cannot open database because the directory does not exist");
766
+ }
767
+ Object.defineProperties(this, {
768
+ [util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
769
+ ...wrappers.getters
770
+ });
771
+ }
772
+ var wrappers = require_wrappers();
773
+ Database.prototype.prepare = wrappers.prepare;
774
+ Database.prototype.transaction = require_transaction();
775
+ Database.prototype.pragma = require_pragma();
776
+ Database.prototype.backup = require_backup();
777
+ Database.prototype.serialize = require_serialize();
778
+ Database.prototype.function = require_function();
779
+ Database.prototype.aggregate = require_aggregate();
780
+ Database.prototype.table = require_table();
781
+ Database.prototype.loadExtension = wrappers.loadExtension;
782
+ Database.prototype.exec = wrappers.exec;
783
+ Database.prototype.close = wrappers.close;
784
+ Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
785
+ Database.prototype.unsafeMode = wrappers.unsafeMode;
786
+ Database.prototype[util.inspect] = require_inspect();
787
+ module.exports = Database;
788
+ });
789
+
790
+ // node_modules/better-sqlite3/lib/index.js
791
+ var require_lib = __commonJS((exports, module) => {
792
+ module.exports = require_database();
793
+ module.exports.SqliteError = require_sqlite_error();
794
+ });
13
795
 
14
796
  // node_modules/zod/v4/core/core.js
15
797
  function $constructor(name, initializer, params) {
@@ -12677,30 +13459,68 @@ var init_dist = __esm(() => {
12677
13459
  });
12678
13460
 
12679
13461
  // index.ts
12680
- import * as fs from "fs";
13462
+ var import_better_sqlite3 = __toESM(require_lib(), 1);
12681
13463
  import * as path from "path";
12682
- var mailboxFile = null;
13464
+ var dbFile = null;
13465
+ var db = null;
12683
13466
  var activeWatches = new Map;
12684
- async function getMailboxFile(client) {
12685
- if (!mailboxFile) {
13467
+ async function getDbFile(client) {
13468
+ if (!dbFile) {
12686
13469
  const result = await client.path.get();
12687
- mailboxFile = path.join(result.data.config, "mailbox.json");
12688
- }
12689
- return mailboxFile;
12690
- }
12691
- async function loadMailbox(client) {
12692
- const file2 = await getMailboxFile(client);
12693
- try {
12694
- if (fs.existsSync(file2)) {
12695
- const data = JSON.parse(fs.readFileSync(file2, "utf-8"));
12696
- return data;
12697
- }
12698
- } catch {}
12699
- return {};
12700
- }
12701
- async function saveMailbox(client, mailbox) {
12702
- const file2 = await getMailboxFile(client);
12703
- fs.writeFileSync(file2, JSON.stringify(mailbox, null, 2));
13470
+ dbFile = path.join(result.data.config, "mailbox.db");
13471
+ }
13472
+ return dbFile;
13473
+ }
13474
+ async function getDatabase(client) {
13475
+ if (!db) {
13476
+ const file2 = await getDbFile(client);
13477
+ db = new import_better_sqlite3.default(file2);
13478
+ db.pragma("journal_mode = WAL");
13479
+ db.exec(`
13480
+ CREATE TABLE IF NOT EXISTS messages (
13481
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
13482
+ recipient TEXT NOT NULL,
13483
+ sender TEXT NOT NULL,
13484
+ message TEXT NOT NULL,
13485
+ timestamp INTEGER NOT NULL,
13486
+ read INTEGER NOT NULL DEFAULT 0
13487
+ )
13488
+ `);
13489
+ db.exec(`
13490
+ CREATE INDEX IF NOT EXISTS idx_messages_recipient ON messages(recipient)
13491
+ `);
13492
+ db.exec(`
13493
+ CREATE INDEX IF NOT EXISTS idx_messages_read ON messages(recipient, read)
13494
+ `);
13495
+ }
13496
+ return db;
13497
+ }
13498
+ async function addMessage(client, recipient, sender, message, timestamp) {
13499
+ const database = await getDatabase(client);
13500
+ const stmt = database.prepare(`
13501
+ INSERT INTO messages (recipient, sender, message, timestamp, read)
13502
+ VALUES (?, ?, ?, ?, 0)
13503
+ `);
13504
+ stmt.run(recipient.toLowerCase(), sender.toLowerCase(), message, timestamp);
13505
+ }
13506
+ async function getUnreadMessages(client, recipient) {
13507
+ const database = await getDatabase(client);
13508
+ const stmt = database.prepare(`
13509
+ SELECT sender as from, message, timestamp, read
13510
+ FROM messages
13511
+ WHERE recipient = ? AND read = 0
13512
+ ORDER BY timestamp ASC
13513
+ `);
13514
+ return stmt.all(recipient.toLowerCase());
13515
+ }
13516
+ async function markMessageAsRead(client, recipient, timestamp) {
13517
+ const database = await getDatabase(client);
13518
+ const stmt = database.prepare(`
13519
+ UPDATE messages
13520
+ SET read = 1
13521
+ WHERE recipient = ? AND timestamp = ?
13522
+ `);
13523
+ stmt.run(recipient.toLowerCase(), timestamp);
12704
13524
  }
12705
13525
  function startMailWatch(client, recipient, sessionId, instructions) {
12706
13526
  if (activeWatches.has(recipient)) {
@@ -12708,25 +13528,16 @@ function startMailWatch(client, recipient, sessionId, instructions) {
12708
13528
  }
12709
13529
  const interval = setInterval(async () => {
12710
13530
  try {
12711
- const mailbox = await loadMailbox(client);
12712
13531
  const watch = activeWatches.get(recipient);
12713
13532
  if (!watch) {
12714
13533
  return;
12715
13534
  }
12716
- const recipientMailbox = mailbox[recipient];
12717
- if (!recipientMailbox) {
13535
+ const unreadMessages = await getUnreadMessages(client, recipient);
13536
+ if (unreadMessages.length === 0) {
12718
13537
  return;
12719
13538
  }
12720
- const unreadMessages = [];
12721
- for (const [timestamp, message] of Object.entries(recipientMailbox)) {
12722
- if (!message.read) {
12723
- unreadMessages.push(message);
12724
- }
12725
- }
12726
- unreadMessages.sort((a, b) => a.timestamp - b.timestamp);
12727
13539
  for (const message of unreadMessages) {
12728
- recipientMailbox[message.timestamp].read = true;
12729
- await saveMailbox(client, mailbox);
13540
+ await markMessageAsRead(client, recipient, message.timestamp);
12730
13541
  await injectMailMessage(client, sessionId, recipient, message, instructions);
12731
13542
  }
12732
13543
  } catch (error45) {
@@ -12793,20 +13604,10 @@ var mailboxPlugin = async (ctx) => {
12793
13604
  message: z.string().describe("Message content to send")
12794
13605
  },
12795
13606
  async execute(args) {
12796
- const mailbox = await loadMailbox(client);
12797
13607
  const to = args.to.toLowerCase();
12798
13608
  const from = args.from.toLowerCase();
12799
13609
  const timestamp = Date.now();
12800
- if (!mailbox[to]) {
12801
- mailbox[to] = {};
12802
- }
12803
- mailbox[to][timestamp] = {
12804
- from,
12805
- message: args.message,
12806
- timestamp,
12807
- read: false
12808
- };
12809
- await saveMailbox(client, mailbox);
13610
+ await addMessage(client, to, from, args.message, timestamp);
12810
13611
  return `Mail sent to "${args.to}" from "${args.from}" at ${new Date(timestamp).toISOString()}`;
12811
13612
  }
12812
13613
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-mailbox",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "A simple mailbox system for sending and receiving messages between sessions",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,6 +22,7 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "devDependencies": {
25
+ "@types/better-sqlite3": "^7.6.12",
25
26
  "@types/node": "^25.0.10",
26
27
  "electron": "^28.0.0",
27
28
  "typescript": "^5.9.3"
@@ -30,6 +31,7 @@
30
31
  "@opencode-ai/plugin": "^1.1.25"
31
32
  },
32
33
  "dependencies": {
34
+ "better-sqlite3": "^12.1.0",
33
35
  "dotenv": "^17.2.3"
34
36
  },
35
37
  "keywords": [