@abraca/resend 2.90.0 → 2.92.0
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/package.json +6 -12
- package/src/bootstrap.ts +0 -153
- package/src/inbound-server.ts +0 -413
- package/src/index.ts +0 -128
- package/src/outbox-watcher.ts +0 -367
- package/src/render.ts +0 -112
- package/src/resend-client.ts +0 -50
- package/src/server.ts +0 -324
- package/src/utils.ts +0 -6
package/src/server.ts
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AbracadabraResendServer — connection lifecycle for the Resend bridge.
|
|
3
|
-
*
|
|
4
|
-
* Trimmed-down equivalent of @abraca/mcp's server: Ed25519 identity, login/
|
|
5
|
-
* register, single-space binding, child-provider cache with idle eviction,
|
|
6
|
-
* `ensureConnected` heal on dropped sockets / expired JWTs. No awareness,
|
|
7
|
-
* no chat, no tool wiring — this is a daemon, not an agent.
|
|
8
|
-
*/
|
|
9
|
-
import type { DocumentMeta, ServerInfo } from "@abraca/dabra";
|
|
10
|
-
import {
|
|
11
|
-
AbracadabraClient,
|
|
12
|
-
AbracadabraProvider,
|
|
13
|
-
Kind,
|
|
14
|
-
loadOrCreateKeyFile,
|
|
15
|
-
loginOrRegisterWithKey,
|
|
16
|
-
makeChallengeSigner,
|
|
17
|
-
openSyncedProvider,
|
|
18
|
-
SERVER_ROOT_ID,
|
|
19
|
-
WebSocketStatus,
|
|
20
|
-
} from "@abraca/dabra";
|
|
21
|
-
import * as Y from "yjs";
|
|
22
|
-
import { waitForSync } from "./utils.ts";
|
|
23
|
-
|
|
24
|
-
export interface ResendServerConfig {
|
|
25
|
-
url: string;
|
|
26
|
-
agentName?: string;
|
|
27
|
-
keyFile?: string;
|
|
28
|
-
inviteCode?: string;
|
|
29
|
-
/** Bind to this space id. When omitted, the first visible space is used. */
|
|
30
|
-
spaceId?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface SpaceConnection {
|
|
34
|
-
doc: Y.Doc;
|
|
35
|
-
provider: AbracadabraProvider;
|
|
36
|
-
docId: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface CachedProvider {
|
|
40
|
-
provider: AbracadabraProvider;
|
|
41
|
-
lastAccessed: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const IDLE_TIMEOUT_MS = 5 * 60 * 1000;
|
|
45
|
-
|
|
46
|
-
export class AbracadabraResendServer {
|
|
47
|
-
readonly config: ResendServerConfig;
|
|
48
|
-
readonly client: AbracadabraClient;
|
|
49
|
-
private _serverInfo: ServerInfo | null = null;
|
|
50
|
-
private _spaces: DocumentMeta[] = [];
|
|
51
|
-
private _connection: SpaceConnection | null = null;
|
|
52
|
-
private childCache = new Map<string, CachedProvider>();
|
|
53
|
-
private evictionTimer: ReturnType<typeof setInterval> | null = null;
|
|
54
|
-
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
55
|
-
private _userId: string | null = null;
|
|
56
|
-
private _signFn: ((challenge: string) => Promise<string>) | null = null;
|
|
57
|
-
private _reconnecting: Promise<void> | null = null;
|
|
58
|
-
|
|
59
|
-
constructor(config: ResendServerConfig) {
|
|
60
|
-
this.config = config;
|
|
61
|
-
this.client = new AbracadabraClient({
|
|
62
|
-
url: config.url,
|
|
63
|
-
persistAuth: false,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
get agentName(): string {
|
|
68
|
-
return this.config.agentName || "Resend Bridge";
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
get serverInfo(): ServerInfo | null {
|
|
72
|
-
return this._serverInfo;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get spaceDocId(): string | null {
|
|
76
|
-
return this._connection?.docId ?? null;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
get rootDocument(): Y.Doc | null {
|
|
80
|
-
return this._connection?.doc ?? null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
get rootProvider(): AbracadabraProvider | null {
|
|
84
|
-
return this._connection?.provider ?? null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
get userId(): string | null {
|
|
88
|
-
return this._userId;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
get spaces(): DocumentMeta[] {
|
|
92
|
-
return this._spaces;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Authenticate, discover spaces, connect to the configured (or first) space. */
|
|
96
|
-
async connect(): Promise<void> {
|
|
97
|
-
const keypair = await loadOrCreateKeyFile(this.config.keyFile, {
|
|
98
|
-
defaultFileName: "resend.key",
|
|
99
|
-
onCreate: ({ path, publicKeyB64 }) => {
|
|
100
|
-
console.error(
|
|
101
|
-
`[abracadabra-resend] Generated new agent keypair at ${path}`,
|
|
102
|
-
);
|
|
103
|
-
console.error(`[abracadabra-resend] Public key: ${publicKeyB64}`);
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
this._userId = keypair.publicKeyB64;
|
|
107
|
-
this._signFn = makeChallengeSigner(keypair);
|
|
108
|
-
|
|
109
|
-
await loginOrRegisterWithKey(this.client, keypair, {
|
|
110
|
-
displayName: this.agentName,
|
|
111
|
-
deviceName: "Resend Bridge",
|
|
112
|
-
inviteCode: this.config.inviteCode,
|
|
113
|
-
onRegister: () =>
|
|
114
|
-
console.error(
|
|
115
|
-
"[abracadabra-resend] Key not registered, creating new account...",
|
|
116
|
-
),
|
|
117
|
-
});
|
|
118
|
-
console.error(
|
|
119
|
-
`[abracadabra-resend] Authenticated as ${this.agentName} (pubkey=${keypair.publicKeyB64})`,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
this._serverInfo = await this.client.serverInfo();
|
|
123
|
-
|
|
124
|
-
const roots = await this.client.listChildren();
|
|
125
|
-
this._spaces = roots.filter((d) => d.kind === Kind.Space);
|
|
126
|
-
|
|
127
|
-
let targetId = this.config.spaceId;
|
|
128
|
-
if (targetId) {
|
|
129
|
-
const found =
|
|
130
|
-
this._spaces.find((s) => s.id === targetId) ??
|
|
131
|
-
roots.find((d) => d.id === targetId);
|
|
132
|
-
if (!found) {
|
|
133
|
-
throw new Error(
|
|
134
|
-
`Configured ABRA_SPACE_ID=${targetId} not found among server roots`,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
} else {
|
|
138
|
-
targetId = this._spaces[0]?.id ?? roots[0]?.id;
|
|
139
|
-
if (!targetId) {
|
|
140
|
-
throw new Error(
|
|
141
|
-
`No entry point found: server has no top-level documents under ${SERVER_ROOT_ID}.`,
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
console.error(`[abracadabra-resend] Binding to space ${targetId}`);
|
|
147
|
-
await this._connectToSpace(targetId);
|
|
148
|
-
console.error("[abracadabra-resend] Space doc synced");
|
|
149
|
-
|
|
150
|
-
this.evictionTimer = setInterval(() => this.evictIdle(), 60_000);
|
|
151
|
-
|
|
152
|
-
// Connection heartbeat. The bridge is a long-running daemon that can sit
|
|
153
|
-
// idle for hours; its JWT will expire and the socket can drop with no
|
|
154
|
-
// dispatch to trigger a heal. `ensureConnected` is a no-op while healthy
|
|
155
|
-
// (just refreshes the token if it's near expiry), and revives the socket
|
|
156
|
-
// on the SAME doc — keeping the outbox watcher's observers alive — if it
|
|
157
|
-
// died. This is what makes "once connected, STAY connected" true when idle.
|
|
158
|
-
this.heartbeatTimer = setInterval(() => {
|
|
159
|
-
void this.ensureConnected();
|
|
160
|
-
}, 30_000);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private async _connectToSpace(docId: string): Promise<SpaceConnection> {
|
|
164
|
-
if (!this.client.isTokenValid() && this._signFn && this._userId) {
|
|
165
|
-
console.error("[abracadabra-resend] JWT expired, re-authenticating...");
|
|
166
|
-
await this.client.loginWithKey(this._userId, this._signFn);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const { doc, provider } = await openSyncedProvider({ client: this.client, docId });
|
|
170
|
-
|
|
171
|
-
provider.awareness?.setLocalStateField("user", {
|
|
172
|
-
name: this.agentName,
|
|
173
|
-
color: "hsl(170, 70%, 45%)",
|
|
174
|
-
publicKey: this._userId,
|
|
175
|
-
isAgent: true,
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
const conn: SpaceConnection = { doc, provider, docId };
|
|
179
|
-
this._connection = conn;
|
|
180
|
-
return conn;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
private _wsConnected(provider: AbracadabraProvider): boolean {
|
|
184
|
-
return provider.connectionStatus === WebSocketStatus.Connected;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Heal a dropped socket / expired JWT before tool ops. De-duped across
|
|
189
|
-
* concurrent callers; best-effort (never throws — a failed heal falls
|
|
190
|
-
* through to the caller's normal error handling).
|
|
191
|
-
*/
|
|
192
|
-
async ensureConnected(): Promise<void> {
|
|
193
|
-
if (this._reconnecting) return this._reconnecting;
|
|
194
|
-
this._reconnecting = (async () => {
|
|
195
|
-
try {
|
|
196
|
-
if (!this.client.isTokenValid() && this._signFn && this._userId) {
|
|
197
|
-
try {
|
|
198
|
-
await this.client.loginWithKey(this._userId, this._signFn);
|
|
199
|
-
} catch (e) {
|
|
200
|
-
console.error("[abracadabra-resend] Re-auth during heal failed:", e);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const conn = this._connection;
|
|
204
|
-
if (!conn) return;
|
|
205
|
-
if (this._wsConnected(conn.provider)) return;
|
|
206
|
-
|
|
207
|
-
// Give the SDK's own auto-reconnect a bounded window before forcing
|
|
208
|
-
// one ourselves.
|
|
209
|
-
try {
|
|
210
|
-
await waitForSync(conn.provider, 6000);
|
|
211
|
-
} catch {
|
|
212
|
-
/* fall through to forced reconnect */
|
|
213
|
-
}
|
|
214
|
-
if (this._wsConnected(conn.provider)) return;
|
|
215
|
-
|
|
216
|
-
// Still dead → force a reconnect on the SAME Y.Doc. Critically we do
|
|
217
|
-
// NOT destroy + recreate the provider: the outbox watcher's
|
|
218
|
-
// observeDeep / afterTransaction listeners are attached to THIS doc,
|
|
219
|
-
// and swapping the doc out from under them is exactly what made the
|
|
220
|
-
// bridge go silent ("works until it doesn't") after a drop. The doc
|
|
221
|
-
// survives, observers keep firing, and step 1's token refresh lets
|
|
222
|
-
// the recycled socket re-authenticate cleanly. Cached child providers
|
|
223
|
-
// multiplex this same socket and re-sync on reopen, so we keep them.
|
|
224
|
-
console.error(
|
|
225
|
-
"[abracadabra-resend] Socket dead — forcing reconnect on the same doc…",
|
|
226
|
-
);
|
|
227
|
-
conn.provider.reconnect();
|
|
228
|
-
try {
|
|
229
|
-
await waitForSync(conn.provider, 10000);
|
|
230
|
-
console.error("[abracadabra-resend] Reconnected + re-synced");
|
|
231
|
-
} catch (e) {
|
|
232
|
-
console.error(
|
|
233
|
-
"[abracadabra-resend] Reconnect did not re-sync in time:",
|
|
234
|
-
e,
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
} finally {
|
|
238
|
-
this._reconnecting = null;
|
|
239
|
-
}
|
|
240
|
-
})();
|
|
241
|
-
return this._reconnecting;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
getTreeMap(): Y.Map<any> | null {
|
|
245
|
-
return this._connection?.doc.getMap("doc-tree") ?? null;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
async getChildProvider(docId: string): Promise<AbracadabraProvider> {
|
|
249
|
-
await this.ensureConnected();
|
|
250
|
-
|
|
251
|
-
const cached = this.childCache.get(docId);
|
|
252
|
-
if (
|
|
253
|
-
cached &&
|
|
254
|
-
cached.provider.connectionStatus !== WebSocketStatus.Disconnected
|
|
255
|
-
) {
|
|
256
|
-
cached.lastAccessed = Date.now();
|
|
257
|
-
return cached.provider;
|
|
258
|
-
}
|
|
259
|
-
if (cached) {
|
|
260
|
-
try {
|
|
261
|
-
cached.provider.destroy();
|
|
262
|
-
} catch {
|
|
263
|
-
/* already gone */
|
|
264
|
-
}
|
|
265
|
-
this.childCache.delete(docId);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const root = this._connection?.provider;
|
|
269
|
-
if (!root) throw new Error("Not connected. Call connect() first.");
|
|
270
|
-
|
|
271
|
-
if (!this.client.isTokenValid() && this._signFn && this._userId) {
|
|
272
|
-
await this.client.loginWithKey(this._userId, this._signFn);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const childProvider = await root.loadChild(docId);
|
|
276
|
-
await waitForSync(childProvider);
|
|
277
|
-
|
|
278
|
-
this.childCache.set(docId, {
|
|
279
|
-
provider: childProvider,
|
|
280
|
-
lastAccessed: Date.now(),
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
return childProvider;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private evictIdle(): void {
|
|
287
|
-
const now = Date.now();
|
|
288
|
-
for (const [docId, cached] of this.childCache) {
|
|
289
|
-
if (now - cached.lastAccessed > IDLE_TIMEOUT_MS) {
|
|
290
|
-
cached.provider.destroy();
|
|
291
|
-
this.childCache.delete(docId);
|
|
292
|
-
console.error(`[abracadabra-resend] Evicted idle provider: ${docId}`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
async destroy(): Promise<void> {
|
|
298
|
-
if (this.evictionTimer) {
|
|
299
|
-
clearInterval(this.evictionTimer);
|
|
300
|
-
this.evictionTimer = null;
|
|
301
|
-
}
|
|
302
|
-
if (this.heartbeatTimer) {
|
|
303
|
-
clearInterval(this.heartbeatTimer);
|
|
304
|
-
this.heartbeatTimer = null;
|
|
305
|
-
}
|
|
306
|
-
for (const [, cached] of this.childCache) {
|
|
307
|
-
try {
|
|
308
|
-
cached.provider.destroy();
|
|
309
|
-
} catch {
|
|
310
|
-
/* already gone */
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
this.childCache.clear();
|
|
314
|
-
if (this._connection) {
|
|
315
|
-
try {
|
|
316
|
-
this._connection.provider.destroy();
|
|
317
|
-
} catch {
|
|
318
|
-
/* already gone */
|
|
319
|
-
}
|
|
320
|
-
this._connection = null;
|
|
321
|
-
}
|
|
322
|
-
console.error("[abracadabra-resend] Shutdown complete");
|
|
323
|
-
}
|
|
324
|
-
}
|
package/src/utils.ts
DELETED