@mastra/hono 1.3.5-alpha.0 → 1.4.0-alpha.2
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/CHANGELOG.md +23 -0
- package/dist/browser-stream/index.d.ts +35 -0
- package/dist/browser-stream/index.d.ts.map +1 -0
- package/dist/index.cjs +79 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +79 -1
- package/dist/index.js.map +1 -1
- package/package.json +10 -7
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { MastraServer as MastraServer$1, normalizeQueryParams, redactStreamChunk
|
|
|
2
2
|
import { toReqRes, toFetchResponse } from 'fetch-to-node';
|
|
3
3
|
import { RequestContext } from '@mastra/core/request-context';
|
|
4
4
|
import { coreAuthMiddleware } from '@mastra/server/auth';
|
|
5
|
+
import { ViewerRegistry, handleInputMessage } from '@mastra/server/browser-stream';
|
|
5
6
|
|
|
6
7
|
// src/index.ts
|
|
7
8
|
|
|
@@ -458,6 +459,83 @@ function createAuthMiddleware({ mastra, requiresAuth = true }) {
|
|
|
458
459
|
return c.json(result.body, result.status);
|
|
459
460
|
};
|
|
460
461
|
}
|
|
462
|
+
async function setupBrowserStream(app, config2) {
|
|
463
|
+
let createNodeWebSocket;
|
|
464
|
+
try {
|
|
465
|
+
const mod = "@hono/node-ws";
|
|
466
|
+
const honoNodeWs = await import(
|
|
467
|
+
/* @vite-ignore */
|
|
468
|
+
/* webpackIgnore: true */
|
|
469
|
+
mod
|
|
470
|
+
);
|
|
471
|
+
createNodeWebSocket = honoNodeWs.createNodeWebSocket;
|
|
472
|
+
} catch {
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app });
|
|
476
|
+
const registry2 = new ViewerRegistry();
|
|
477
|
+
app.get(
|
|
478
|
+
"/browser/:agentId/stream",
|
|
479
|
+
upgradeWebSocket((c) => {
|
|
480
|
+
const agentId = c.req.param("agentId");
|
|
481
|
+
const threadId = c.req.query("threadId");
|
|
482
|
+
const viewerKey = threadId ? `${agentId}:${threadId}` : agentId;
|
|
483
|
+
return {
|
|
484
|
+
onOpen(_event, ws) {
|
|
485
|
+
ws.send(JSON.stringify({ status: "connected" }));
|
|
486
|
+
void registry2.addViewer(viewerKey, ws, config2.getToolset, agentId, threadId);
|
|
487
|
+
},
|
|
488
|
+
onMessage(event, _ws) {
|
|
489
|
+
const data = typeof event.data === "string" ? event.data : null;
|
|
490
|
+
if (data) {
|
|
491
|
+
handleInputMessage(data, config2.getToolset, agentId, threadId);
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
onClose(_event, ws) {
|
|
495
|
+
void registry2.removeViewer(viewerKey, ws);
|
|
496
|
+
},
|
|
497
|
+
onError(event, ws) {
|
|
498
|
+
console.error("[BrowserStream] WebSocket error:", event);
|
|
499
|
+
void registry2.removeViewer(viewerKey, ws);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
})
|
|
503
|
+
);
|
|
504
|
+
app.post("/api/agents/:agentId/browser/close", async (c) => {
|
|
505
|
+
const agentId = c.req.param("agentId");
|
|
506
|
+
if (!agentId) {
|
|
507
|
+
return c.json({ error: "Agent ID is required" }, 400);
|
|
508
|
+
}
|
|
509
|
+
const toolset = config2.getToolset(agentId);
|
|
510
|
+
if (!toolset) {
|
|
511
|
+
return c.json({ error: "No browser session for this agent" }, 404);
|
|
512
|
+
}
|
|
513
|
+
try {
|
|
514
|
+
let threadId;
|
|
515
|
+
try {
|
|
516
|
+
const body = await c.req.json();
|
|
517
|
+
threadId = body?.threadId;
|
|
518
|
+
} catch {
|
|
519
|
+
}
|
|
520
|
+
const scope = toolset.getScope();
|
|
521
|
+
const viewerKey = threadId ? `${agentId}:${threadId}` : agentId;
|
|
522
|
+
if (scope === "thread" && threadId) {
|
|
523
|
+
await registry2.closeBrowserSession(viewerKey);
|
|
524
|
+
if ("closeThreadSession" in toolset && typeof toolset.closeThreadSession === "function") {
|
|
525
|
+
await toolset.closeThreadSession(threadId);
|
|
526
|
+
}
|
|
527
|
+
} else {
|
|
528
|
+
await registry2.closeBrowserSession(viewerKey);
|
|
529
|
+
await toolset.close();
|
|
530
|
+
}
|
|
531
|
+
return c.json({ success: true });
|
|
532
|
+
} catch (error) {
|
|
533
|
+
console.error(`[BrowserStream] Error closing browser for ${agentId}:`, error);
|
|
534
|
+
return c.json({ error: "Failed to close browser" }, 500);
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
return { injectWebSocket, registry: registry2 };
|
|
538
|
+
}
|
|
461
539
|
|
|
462
540
|
// src/index.ts
|
|
463
541
|
var _hasPermissionPromise;
|
|
@@ -961,6 +1039,6 @@ var MastraServer = class extends MastraServer$1 {
|
|
|
961
1039
|
}
|
|
962
1040
|
};
|
|
963
1041
|
|
|
964
|
-
export { MastraServer, createAuthMiddleware };
|
|
1042
|
+
export { MastraServer, createAuthMiddleware, setupBrowserStream };
|
|
965
1043
|
//# sourceMappingURL=index.js.map
|
|
966
1044
|
//# sourceMappingURL=index.js.map
|