letagents 0.6.0 → 0.7.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.
@@ -1,45 +1,71 @@
1
+ import { encodeRoomIdPath } from "./room-id.js";
1
2
  export class SseClient {
2
3
  apiUrl;
4
+ getAccessToken;
3
5
  subscriptions = new Map();
4
- constructor(apiUrl) {
6
+ constructor(apiUrl, getAccessToken) {
5
7
  this.apiUrl = apiUrl.replace(/\/$/, "");
8
+ this.getAccessToken = getAccessToken;
6
9
  }
7
- subscribe(projectId, onMessage) {
8
- if (this.subscriptions.has(projectId)) {
10
+ subscribe(target, onMessage) {
11
+ const subscriptionKey = target.roomId;
12
+ if (this.subscriptions.has(subscriptionKey)) {
9
13
  return;
10
14
  }
11
15
  const controller = new AbortController();
12
- const promise = this.consumeStream(projectId, controller.signal, onMessage)
16
+ const promise = this.consumeStream(target, controller.signal, onMessage)
13
17
  .catch((error) => {
14
18
  if (controller.signal.aborted) {
15
19
  return;
16
20
  }
17
- console.error(`SSE subscription failed for project ${projectId}:`, error);
21
+ console.error(`SSE subscription failed for room ${target.roomId}:`, error);
18
22
  })
19
23
  .finally(() => {
20
- const current = this.subscriptions.get(projectId);
24
+ const current = this.subscriptions.get(subscriptionKey);
21
25
  if (current?.controller === controller) {
22
- this.subscriptions.delete(projectId);
26
+ this.subscriptions.delete(subscriptionKey);
23
27
  }
24
28
  });
25
- this.subscriptions.set(projectId, { controller, promise });
29
+ this.subscriptions.set(subscriptionKey, { controller, promise });
26
30
  }
27
- unsubscribe(projectId) {
28
- const subscription = this.subscriptions.get(projectId);
31
+ unsubscribe(roomId) {
32
+ const subscription = this.subscriptions.get(roomId);
29
33
  if (!subscription) {
30
34
  return;
31
35
  }
32
36
  subscription.controller.abort();
33
- this.subscriptions.delete(projectId);
37
+ this.subscriptions.delete(roomId);
34
38
  }
35
39
  unsubscribeAll() {
36
- for (const projectId of this.subscriptions.keys()) {
37
- this.unsubscribe(projectId);
40
+ for (const roomId of this.subscriptions.keys()) {
41
+ this.unsubscribe(roomId);
38
42
  }
39
43
  }
40
- async consumeStream(projectId, signal, onMessage) {
41
- const response = await fetch(`${this.apiUrl}/projects/${encodeURIComponent(projectId)}/messages/stream`, {
42
- headers: { Accept: "text/event-stream" },
44
+ getHeaders() {
45
+ const token = this.getAccessToken?.();
46
+ if (!token) {
47
+ return { Accept: "text/event-stream" };
48
+ }
49
+ return {
50
+ Accept: "text/event-stream",
51
+ Authorization: `Bearer ${token}`,
52
+ };
53
+ }
54
+ async consumeStream(target, signal, onMessage) {
55
+ try {
56
+ await this.openStream(`${this.apiUrl}/rooms/${encodeRoomIdPath(target.roomId)}/messages/stream`, signal, onMessage);
57
+ return;
58
+ }
59
+ catch (error) {
60
+ if (!target.projectId || !this.isMissingRouteError(error)) {
61
+ throw error;
62
+ }
63
+ }
64
+ await this.openStream(`${this.apiUrl}/projects/${encodeURIComponent(target.projectId)}/messages/stream`, signal, onMessage);
65
+ }
66
+ async openStream(url, signal, onMessage) {
67
+ const response = await fetch(url, {
68
+ headers: this.getHeaders(),
43
69
  signal,
44
70
  });
45
71
  if (!response.ok) {
@@ -81,4 +107,8 @@ export class SseClient {
81
107
  }
82
108
  onMessage(JSON.parse(dataLines.join("\n")));
83
109
  }
110
+ isMissingRouteError(error) {
111
+ return (error instanceof Error &&
112
+ /status 404|status 405|Cannot (GET|POST|PATCH)/.test(error.message));
113
+ }
84
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "letagents",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Let Agents Chat — MCP server for AI agent communication",
5
5
  "type": "module",
6
6
  "main": "dist/mcp/server.js",
@@ -13,6 +13,9 @@
13
13
  ],
14
14
  "scripts": {
15
15
  "build": "tsc",
16
+ "db:generate": "drizzle-kit generate",
17
+ "db:migrate": "tsx src/api/migrate.ts",
18
+ "db:studio": "drizzle-kit studio",
16
19
  "prepublishOnly": "npm run build",
17
20
  "dev:api": "tsx src/api/server.ts",
18
21
  "dev:mcp": "tsx src/mcp/server.ts"
@@ -31,14 +34,16 @@
31
34
  "license": "MIT",
32
35
  "dependencies": {
33
36
  "@modelcontextprotocol/sdk": "^1.12.1",
34
- "better-sqlite3": "^12.8.0",
37
+ "drizzle-orm": "^0.45.1",
35
38
  "express": "^5.1.0",
39
+ "pg": "^8.20.0",
36
40
  "yaml": "^2.8.2"
37
41
  },
38
42
  "devDependencies": {
39
- "@types/better-sqlite3": "^7.6.13",
40
43
  "@types/express": "^5.0.2",
41
44
  "@types/node": "^22.15.3",
45
+ "@types/pg": "^8.18.0",
46
+ "drizzle-kit": "^0.31.10",
42
47
  "tsx": "^4.19.4",
43
48
  "typescript": "^5.8.3"
44
49
  }