swarm-mail 0.1.0 → 0.1.3

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 (75) hide show
  1. package/README.md +28 -0
  2. package/dist/adapter.d.ts +36 -0
  3. package/dist/adapter.d.ts.map +1 -0
  4. package/dist/index.d.ts +22 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +16710 -0
  7. package/{src/pglite.ts → dist/pglite.d.ts} +7 -93
  8. package/dist/pglite.d.ts.map +1 -0
  9. package/dist/streams/agent-mail.d.ts +139 -0
  10. package/dist/streams/agent-mail.d.ts.map +1 -0
  11. package/dist/streams/debug.d.ts +173 -0
  12. package/dist/streams/debug.d.ts.map +1 -0
  13. package/dist/streams/effect/ask.d.ts +124 -0
  14. package/dist/streams/effect/ask.d.ts.map +1 -0
  15. package/dist/streams/effect/cursor.d.ts +87 -0
  16. package/dist/streams/effect/cursor.d.ts.map +1 -0
  17. package/dist/streams/effect/deferred.d.ts +108 -0
  18. package/dist/streams/effect/deferred.d.ts.map +1 -0
  19. package/{src/streams/effect/index.ts → dist/streams/effect/index.d.ts} +1 -0
  20. package/dist/streams/effect/index.d.ts.map +1 -0
  21. package/{src/streams/effect/layers.ts → dist/streams/effect/layers.d.ts} +8 -33
  22. package/dist/streams/effect/layers.d.ts.map +1 -0
  23. package/dist/streams/effect/lock.d.ts +137 -0
  24. package/dist/streams/effect/lock.d.ts.map +1 -0
  25. package/dist/streams/effect/mailbox.d.ts +98 -0
  26. package/dist/streams/effect/mailbox.d.ts.map +1 -0
  27. package/dist/streams/events.d.ts +487 -0
  28. package/dist/streams/events.d.ts.map +1 -0
  29. package/dist/streams/index.d.ts +106 -0
  30. package/dist/streams/index.d.ts.map +1 -0
  31. package/dist/streams/migrations.d.ts +102 -0
  32. package/dist/streams/migrations.d.ts.map +1 -0
  33. package/dist/streams/projections.d.ts +173 -0
  34. package/dist/streams/projections.d.ts.map +1 -0
  35. package/dist/streams/store.d.ts +171 -0
  36. package/dist/streams/store.d.ts.map +1 -0
  37. package/dist/streams/swarm-mail.d.ts +153 -0
  38. package/dist/streams/swarm-mail.d.ts.map +1 -0
  39. package/dist/types/adapter.d.ts +267 -0
  40. package/dist/types/adapter.d.ts.map +1 -0
  41. package/dist/types/database.d.ts +117 -0
  42. package/dist/types/database.d.ts.map +1 -0
  43. package/{src/types/index.ts → dist/types/index.d.ts} +2 -15
  44. package/dist/types/index.d.ts.map +1 -0
  45. package/package.json +21 -5
  46. package/src/adapter.ts +0 -306
  47. package/src/index.ts +0 -57
  48. package/src/streams/agent-mail.test.ts +0 -777
  49. package/src/streams/agent-mail.ts +0 -535
  50. package/src/streams/debug.test.ts +0 -500
  51. package/src/streams/debug.ts +0 -727
  52. package/src/streams/effect/ask.integration.test.ts +0 -314
  53. package/src/streams/effect/ask.ts +0 -202
  54. package/src/streams/effect/cursor.integration.test.ts +0 -418
  55. package/src/streams/effect/cursor.ts +0 -288
  56. package/src/streams/effect/deferred.test.ts +0 -357
  57. package/src/streams/effect/deferred.ts +0 -445
  58. package/src/streams/effect/lock.test.ts +0 -385
  59. package/src/streams/effect/lock.ts +0 -399
  60. package/src/streams/effect/mailbox.test.ts +0 -260
  61. package/src/streams/effect/mailbox.ts +0 -318
  62. package/src/streams/events.test.ts +0 -924
  63. package/src/streams/events.ts +0 -329
  64. package/src/streams/index.test.ts +0 -229
  65. package/src/streams/index.ts +0 -578
  66. package/src/streams/migrations.test.ts +0 -359
  67. package/src/streams/migrations.ts +0 -362
  68. package/src/streams/projections.test.ts +0 -611
  69. package/src/streams/projections.ts +0 -564
  70. package/src/streams/store.integration.test.ts +0 -658
  71. package/src/streams/store.ts +0 -1129
  72. package/src/streams/swarm-mail.ts +0 -552
  73. package/src/types/adapter.ts +0 -392
  74. package/src/types/database.ts +0 -127
  75. package/tsconfig.json +0 -22
package/src/adapter.ts DELETED
@@ -1,306 +0,0 @@
1
- /**
2
- * SwarmMail Adapter - Factory for creating SwarmMailAdapter instances
3
- *
4
- * This file implements the adapter pattern for swarm-mail, enabling
5
- * dependency injection of the database instead of singleton access.
6
- *
7
- * ## Design Pattern
8
- * - Accept DatabaseAdapter via factory parameter
9
- * - Return SwarmMailAdapter interface
10
- * - Delegate to internal implementation functions
11
- * - No direct database access (all via adapter)
12
- *
13
- * ## Usage
14
- * ```typescript
15
- * import { createPGLiteAdapter } from '@opencode/swarm-mail/adapters/pglite';
16
- * import { createSwarmMailAdapter } from '@opencode/swarm-mail';
17
- *
18
- * const dbAdapter = createPGLiteAdapter({ path: './streams.db' });
19
- * const swarmMail = createSwarmMailAdapter(dbAdapter, '/path/to/project');
20
- *
21
- * // Use the adapter
22
- * await swarmMail.appendEvent(event);
23
- * const messages = await swarmMail.getInbox('agent-name', { limit: 5 });
24
- * ```
25
- */
26
-
27
- import type { DatabaseAdapter } from "./types/database";
28
- import type { SwarmMailAdapter } from "./types/adapter";
29
- import type {
30
- AgentRegisteredEvent,
31
- MessageSentEvent,
32
- FileReservedEvent,
33
- } from "./streams/events";
34
-
35
- // Import all implementation functions (now refactored to accept dbOverride)
36
- import {
37
- appendEvent,
38
- appendEvents,
39
- readEvents,
40
- getLatestSequence,
41
- replayEvents,
42
- registerAgent,
43
- sendMessage,
44
- reserveFiles,
45
- } from "./streams/store";
46
-
47
- import {
48
- getAgents,
49
- getAgent,
50
- getInbox,
51
- getMessage,
52
- getThreadMessages,
53
- getActiveReservations,
54
- checkConflicts,
55
- } from "./streams/projections";
56
-
57
- import { appendEvent as appendEventUtil } from "./streams/store";
58
- import { createEvent } from "./streams/events";
59
-
60
- /**
61
- * Create a SwarmMailAdapter instance
62
- *
63
- * @param db - DatabaseAdapter instance (PGLite, SQLite, PostgreSQL, etc.)
64
- * @param projectKey - Project identifier (typically the project path)
65
- * @returns SwarmMailAdapter interface
66
- */
67
- export function createSwarmMailAdapter(
68
- db: DatabaseAdapter,
69
- projectKey: string,
70
- ): SwarmMailAdapter {
71
- return {
72
- // ============================================================================
73
- // Event Store Operations
74
- // ============================================================================
75
-
76
- async appendEvent(event, projectPath?) {
77
- return appendEvent(event, projectPath, db);
78
- },
79
-
80
- async appendEvents(events, projectPath?) {
81
- return appendEvents(events, projectPath, db);
82
- },
83
-
84
- async readEvents(options?, projectPath?) {
85
- return readEvents(options, projectPath, db);
86
- },
87
-
88
- async getLatestSequence(projectKeyParam?, projectPath?) {
89
- return getLatestSequence(projectKeyParam, projectPath, db);
90
- },
91
-
92
- async replayEvents(options?, projectPath?) {
93
- return replayEvents(options, projectPath, db);
94
- },
95
-
96
- // ============================================================================
97
- // Agent Operations
98
- // ============================================================================
99
-
100
- async registerAgent(
101
- projectKeyParam,
102
- agentName,
103
- options?,
104
- projectPath?,
105
- ): Promise<AgentRegisteredEvent & { id: number; sequence: number }> {
106
- return registerAgent(
107
- projectKeyParam,
108
- agentName,
109
- options,
110
- projectPath,
111
- db,
112
- );
113
- },
114
-
115
- async getAgents(projectKeyParam, projectPath?) {
116
- return getAgents(projectKeyParam, projectPath, db);
117
- },
118
-
119
- async getAgent(projectKeyParam, agentName, projectPath?) {
120
- return getAgent(projectKeyParam, agentName, projectPath, db);
121
- },
122
-
123
- // ============================================================================
124
- // Messaging Operations
125
- // ============================================================================
126
-
127
- async sendMessage(
128
- projectKeyParam,
129
- fromAgent,
130
- toAgents,
131
- subject,
132
- body,
133
- options?,
134
- projectPath?,
135
- ): Promise<MessageSentEvent & { id: number; sequence: number }> {
136
- return sendMessage(
137
- projectKeyParam,
138
- fromAgent,
139
- toAgents,
140
- subject,
141
- body,
142
- options,
143
- projectPath,
144
- db,
145
- );
146
- },
147
-
148
- async getInbox(projectKeyParam, agentName, options?, projectPath?) {
149
- return getInbox(projectKeyParam, agentName, options, projectPath, db);
150
- },
151
-
152
- async getMessage(projectKeyParam, messageId, projectPath?) {
153
- return getMessage(projectKeyParam, messageId, projectPath, db);
154
- },
155
-
156
- async getThreadMessages(projectKeyParam, threadId, projectPath?) {
157
- return getThreadMessages(projectKeyParam, threadId, projectPath, db);
158
- },
159
-
160
- async markMessageAsRead(
161
- projectKeyParam,
162
- messageId,
163
- agentName,
164
- projectPath?,
165
- ) {
166
- // Create message_read event
167
- const event = createEvent("message_read", {
168
- project_key: projectKeyParam,
169
- message_id: messageId,
170
- agent_name: agentName,
171
- });
172
-
173
- await appendEventUtil(event, projectPath, db);
174
- },
175
-
176
- async acknowledgeMessage(
177
- projectKeyParam,
178
- messageId,
179
- agentName,
180
- projectPath?,
181
- ) {
182
- // Create message_acked event
183
- const event = createEvent("message_acked", {
184
- project_key: projectKeyParam,
185
- message_id: messageId,
186
- agent_name: agentName,
187
- });
188
-
189
- await appendEventUtil(event, projectPath, db);
190
- },
191
-
192
- // ============================================================================
193
- // Reservation Operations
194
- // ============================================================================
195
-
196
- async reserveFiles(
197
- projectKeyParam,
198
- agentName,
199
- paths,
200
- options?,
201
- projectPath?,
202
- ): Promise<FileReservedEvent & { id: number; sequence: number }> {
203
- return reserveFiles(
204
- projectKeyParam,
205
- agentName,
206
- paths,
207
- options,
208
- projectPath,
209
- db,
210
- );
211
- },
212
-
213
- async releaseFiles(projectKeyParam, agentName, options?, projectPath?) {
214
- // Create file_released event
215
- const event = createEvent("file_released", {
216
- project_key: projectKeyParam,
217
- agent_name: agentName,
218
- paths: options?.paths,
219
- reservation_ids: options?.reservationIds,
220
- });
221
-
222
- await appendEventUtil(event, projectPath, db);
223
- },
224
-
225
- async getActiveReservations(projectKeyParam, projectPath?, agentName?) {
226
- return getActiveReservations(projectKeyParam, projectPath, agentName, db);
227
- },
228
-
229
- async checkConflicts(projectKeyParam, agentName, paths, projectPath?) {
230
- return checkConflicts(projectKeyParam, agentName, paths, projectPath, db);
231
- },
232
-
233
- // ============================================================================
234
- // Schema and Health Operations
235
- // ============================================================================
236
-
237
- async runMigrations(projectPath?) {
238
- // Import migrations module and pass db
239
- // Note: migrations expects PGlite but DatabaseAdapter is compatible
240
- const { runMigrations: runMigrationsImpl } =
241
- await import("./streams/migrations");
242
- await runMigrationsImpl(db as any);
243
- },
244
-
245
- async healthCheck(projectPath?) {
246
- // Simple query to check if db is working
247
- try {
248
- const result = await db.query("SELECT 1 as ok");
249
- return result.rows.length > 0;
250
- } catch {
251
- return false;
252
- }
253
- },
254
-
255
- async getDatabaseStats(projectPath?) {
256
- const [events, agents, messages, reservations] = await Promise.all([
257
- db.query<{ count: string }>("SELECT COUNT(*) as count FROM events"),
258
- db.query<{ count: string }>("SELECT COUNT(*) as count FROM agents"),
259
- db.query<{ count: string }>("SELECT COUNT(*) as count FROM messages"),
260
- db.query<{ count: string }>(
261
- "SELECT COUNT(*) as count FROM reservations WHERE released_at IS NULL",
262
- ),
263
- ]);
264
-
265
- return {
266
- events: parseInt(events.rows[0]?.count || "0"),
267
- agents: parseInt(agents.rows[0]?.count || "0"),
268
- messages: parseInt(messages.rows[0]?.count || "0"),
269
- reservations: parseInt(reservations.rows[0]?.count || "0"),
270
- };
271
- },
272
-
273
- async resetDatabase(projectPath?) {
274
- await db.exec(`
275
- DELETE FROM message_recipients;
276
- DELETE FROM messages;
277
- DELETE FROM reservations;
278
- DELETE FROM agents;
279
- DELETE FROM events;
280
- DELETE FROM locks;
281
- DELETE FROM cursors;
282
- `);
283
- },
284
-
285
- // ============================================================================
286
- // Database Connection Management
287
- // ============================================================================
288
-
289
- async getDatabase(projectPath?) {
290
- return db;
291
- },
292
-
293
- async close(projectPath?) {
294
- if (db.close) {
295
- await db.close();
296
- }
297
- },
298
-
299
- async closeAll() {
300
- // For single-instance adapter, same as close()
301
- if (db.close) {
302
- await db.close();
303
- }
304
- },
305
- };
306
- }
package/src/index.ts DELETED
@@ -1,57 +0,0 @@
1
- /**
2
- * Swarm Mail - Actor-model primitives for multi-agent coordination
3
- *
4
- * ## Simple API (PGLite convenience layer)
5
- * ```typescript
6
- * import { getSwarmMail } from '@opencode/swarm-mail';
7
- * const swarmMail = await getSwarmMail('/path/to/project');
8
- * ```
9
- *
10
- * ## Advanced API (database-agnostic adapter)
11
- * ```typescript
12
- * import { createSwarmMailAdapter } from '@opencode/swarm-mail';
13
- * const db = createCustomDbAdapter({ path: './custom.db' });
14
- * const swarmMail = createSwarmMailAdapter(db, '/path/to/project');
15
- * ```
16
- */
17
-
18
- export const SWARM_MAIL_VERSION = "0.1.0";
19
-
20
- // ============================================================================
21
- // Core (database-agnostic)
22
- // ============================================================================
23
-
24
- export { createSwarmMailAdapter } from "./adapter";
25
- export type {
26
- DatabaseAdapter,
27
- SwarmMailAdapter,
28
- EventStoreAdapter,
29
- AgentAdapter,
30
- MessagingAdapter,
31
- ReservationAdapter,
32
- SchemaAdapter,
33
- ReadEventsOptions,
34
- InboxOptions,
35
- Message,
36
- Reservation,
37
- Conflict,
38
- } from "./types";
39
-
40
- // ============================================================================
41
- // PGLite Convenience Layer
42
- // ============================================================================
43
-
44
- export {
45
- getSwarmMail,
46
- createInMemorySwarmMail,
47
- closeSwarmMail,
48
- closeAllSwarmMail,
49
- getDatabasePath,
50
- PGlite,
51
- } from "./pglite";
52
-
53
- // ============================================================================
54
- // Re-export everything from streams for backward compatibility
55
- // ============================================================================
56
-
57
- export * from "./streams";