@meshagent/meshagent 0.37.2 → 0.38.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 (77) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/browser/agent.js +74 -10
  3. package/dist/browser/developer-client.js +3 -0
  4. package/dist/browser/helpers.d.ts +2 -2
  5. package/dist/browser/helpers.js +1 -1
  6. package/dist/browser/meshagent-client.d.ts +25 -0
  7. package/dist/browser/meshagent-client.js +65 -0
  8. package/dist/browser/messaging-client.d.ts +29 -16
  9. package/dist/browser/messaging-client.js +256 -154
  10. package/dist/browser/participant.d.ts +7 -2
  11. package/dist/browser/participant.js +9 -9
  12. package/dist/browser/protocol.d.ts +85 -28
  13. package/dist/browser/protocol.js +356 -119
  14. package/dist/browser/room-client.d.ts +165 -29
  15. package/dist/browser/room-client.js +1114 -74
  16. package/dist/browser/room-event.d.ts +11 -0
  17. package/dist/browser/room-event.js +21 -1
  18. package/dist/browser/room-server-client.d.ts +2 -0
  19. package/dist/browser/room-server-client.js +6 -0
  20. package/dist/browser/runtime.d.ts +1 -1
  21. package/dist/browser/runtime.js +3 -1
  22. package/dist/browser/secrets-client.js +6 -2
  23. package/dist/browser/storage-client.d.ts +1 -0
  24. package/dist/browser/storage-client.js +9 -0
  25. package/dist/browser/sync-client.d.ts +16 -14
  26. package/dist/browser/sync-client.js +195 -116
  27. package/dist/esm/agent.js +74 -10
  28. package/dist/esm/developer-client.js +3 -0
  29. package/dist/esm/helpers.d.ts +2 -2
  30. package/dist/esm/helpers.js +1 -1
  31. package/dist/esm/meshagent-client.d.ts +25 -0
  32. package/dist/esm/meshagent-client.js +65 -0
  33. package/dist/esm/messaging-client.d.ts +29 -16
  34. package/dist/esm/messaging-client.js +256 -154
  35. package/dist/esm/participant.d.ts +7 -2
  36. package/dist/esm/participant.js +9 -9
  37. package/dist/esm/protocol.d.ts +85 -28
  38. package/dist/esm/protocol.js +352 -118
  39. package/dist/esm/room-client.d.ts +165 -29
  40. package/dist/esm/room-client.js +1112 -73
  41. package/dist/esm/room-event.d.ts +11 -0
  42. package/dist/esm/room-event.js +19 -0
  43. package/dist/esm/room-server-client.d.ts +2 -0
  44. package/dist/esm/room-server-client.js +7 -1
  45. package/dist/esm/runtime.d.ts +1 -1
  46. package/dist/esm/runtime.js +1 -1
  47. package/dist/esm/secrets-client.js +6 -2
  48. package/dist/esm/storage-client.d.ts +1 -0
  49. package/dist/esm/storage-client.js +9 -0
  50. package/dist/esm/sync-client.d.ts +16 -14
  51. package/dist/esm/sync-client.js +196 -117
  52. package/dist/node/agent.js +74 -10
  53. package/dist/node/developer-client.js +3 -0
  54. package/dist/node/helpers.d.ts +2 -2
  55. package/dist/node/helpers.js +1 -1
  56. package/dist/node/meshagent-client.d.ts +25 -0
  57. package/dist/node/meshagent-client.js +65 -0
  58. package/dist/node/messaging-client.d.ts +29 -16
  59. package/dist/node/messaging-client.js +256 -154
  60. package/dist/node/participant.d.ts +7 -2
  61. package/dist/node/participant.js +9 -9
  62. package/dist/node/protocol.d.ts +85 -28
  63. package/dist/node/protocol.js +356 -119
  64. package/dist/node/room-client.d.ts +165 -29
  65. package/dist/node/room-client.js +1114 -74
  66. package/dist/node/room-event.d.ts +11 -0
  67. package/dist/node/room-event.js +21 -1
  68. package/dist/node/room-server-client.d.ts +2 -0
  69. package/dist/node/room-server-client.js +6 -0
  70. package/dist/node/runtime.d.ts +1 -1
  71. package/dist/node/runtime.js +3 -1
  72. package/dist/node/secrets-client.js +6 -2
  73. package/dist/node/storage-client.d.ts +1 -0
  74. package/dist/node/storage-client.js +9 -0
  75. package/dist/node/sync-client.d.ts +16 -14
  76. package/dist/node/sync-client.js +195 -116
  77. package/package.json +6 -3
package/dist/esm/agent.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { ErrorContent } from "./response";
2
+ import { RoomServerException } from "./room-server-client";
2
3
  import { ToolContentSpec } from "./tool-content-type";
3
4
  import { unpackMessage } from "./utils";
5
+ import { RoomStatusEvent } from "./room-event";
4
6
  export class Tool {
5
7
  constructor({ name, description, title, inputSchema, inputSpec, outputSpec, outputSchema, thumbnailUrl }) {
6
8
  this.name = name;
@@ -93,17 +95,46 @@ export class HostedToolkit {
93
95
  }
94
96
  class _RemoteToolkitWrapper {
95
97
  constructor({ toolkit, room }) {
98
+ this._toolCallHandler = this._toolCall.bind(this);
99
+ this._roomEventHandler = this._onRoomEvent.bind(this);
100
+ this._started = false;
101
+ this._public = false;
102
+ this._registerTask = null;
96
103
  this.toolkit = toolkit;
97
104
  this.client = room;
98
105
  }
99
106
  async start({ public_: isPublic = false } = {}) {
100
- const handler = this._toolCall.bind(this);
101
- this.client.protocol.addHandler(`room.tool_call.${this.toolkit.name}`, handler);
102
- await this._register(isPublic);
107
+ if (this._started) {
108
+ throw new RoomServerException(`toolkit '${this.toolkit.name}' is already started`);
109
+ }
110
+ this._public = isPublic;
111
+ this.client.protocol.addHandler(`room.tool_call.${this.toolkit.name}`, this._toolCallHandler);
112
+ this.client.on("disconnected", this._roomEventHandler);
113
+ this.client.on("reconnected", this._roomEventHandler);
114
+ try {
115
+ await this._register(isPublic);
116
+ this._started = true;
117
+ }
118
+ catch (error) {
119
+ this.client.off("disconnected", this._roomEventHandler);
120
+ this.client.off("reconnected", this._roomEventHandler);
121
+ this.client.protocol.removeHandler(`room.tool_call.${this.toolkit.name}`, this._toolCallHandler);
122
+ throw error;
123
+ }
103
124
  }
104
125
  async stop() {
105
- await this._unregister();
106
- this.client.protocol.removeHandler(`room.tool_call.${this.toolkit.name}`);
126
+ if (!this._started) {
127
+ return;
128
+ }
129
+ this._started = false;
130
+ this.client.off("disconnected", this._roomEventHandler);
131
+ this.client.off("reconnected", this._roomEventHandler);
132
+ try {
133
+ await this._unregister();
134
+ }
135
+ finally {
136
+ this.client.protocol.removeHandler(`room.tool_call.${this.toolkit.name}`, this._toolCallHandler);
137
+ }
107
138
  }
108
139
  async _register(public_) {
109
140
  const response = await this.client.sendRequest("room.register_toolkit", {
@@ -118,13 +149,43 @@ class _RemoteToolkitWrapper {
118
149
  this._registrationId = json["id"];
119
150
  }
120
151
  async _unregister() {
121
- if (!this._registrationId)
152
+ const registrationId = this._registrationId;
153
+ this._registrationId = undefined;
154
+ if (registrationId == null || this.client.isClosed) {
122
155
  return;
156
+ }
123
157
  await this.client.sendRequest("room.unregister_toolkit", {
124
- id: this._registrationId,
158
+ id: registrationId,
125
159
  });
126
160
  }
161
+ _scheduleRegisterIfNeeded() {
162
+ if (!this._started || this._registrationId != null || this._registerTask != null || this.client.isClosed) {
163
+ return;
164
+ }
165
+ this._registerTask = this._register(this._public)
166
+ .catch((error) => {
167
+ console.warn(`unable to reregister hosted toolkit ${this.toolkit.name}`, error);
168
+ })
169
+ .finally(() => {
170
+ this._registerTask = null;
171
+ });
172
+ }
173
+ _onRoomEvent(event) {
174
+ if (!this._started || !(event instanceof RoomStatusEvent)) {
175
+ return;
176
+ }
177
+ if (event.status === "disconnected") {
178
+ this._registrationId = undefined;
179
+ return;
180
+ }
181
+ if (event.status === "reconnected") {
182
+ this._scheduleRegisterIfNeeded();
183
+ }
184
+ }
127
185
  async _toolCall(protocol, messageId, type, data) {
186
+ if (!this.client.isActiveProtocol(protocol)) {
187
+ return;
188
+ }
128
189
  try {
129
190
  const [message, _] = unpackMessage(data);
130
191
  const toolName = message["name"];
@@ -150,11 +211,11 @@ class _RemoteToolkitWrapper {
150
211
  args = rawArguments ?? {};
151
212
  }
152
213
  const response = await this.toolkit.execute(toolName, args);
153
- await this.client.protocol.send("room.tool_call_response", response.pack(), messageId);
214
+ await this.client.protocol.send("room.tool_call_response", response.pack(), { id: messageId });
154
215
  }
155
216
  catch (e) {
156
217
  const err = new ErrorContent({ text: String(e) });
157
- await this.client.protocol.send("room.tool_call_response", err.pack(), messageId);
218
+ await this.client.protocol.send("room.tool_call_response", err.pack(), { id: messageId });
158
219
  }
159
220
  }
160
221
  }
@@ -179,6 +240,9 @@ export class RemoteTaskRunner {
179
240
  async start() {
180
241
  }
181
242
  async stop() {
182
- this.client.protocol.removeHandler("agent.ask");
243
+ const handler = this.client.protocol.getHandler("agent.ask");
244
+ if (handler != null) {
245
+ this.client.protocol.removeHandler("agent.ask", handler);
246
+ }
183
247
  }
184
248
  }
@@ -14,6 +14,9 @@ export class DeveloperClient extends EventEmitter {
14
14
  this.emit("log", event);
15
15
  }
16
16
  async _handleDeveloperLog(protocol, messageId, type, bytes) {
17
+ if (!this.client.isActiveProtocol(protocol)) {
18
+ return;
19
+ }
17
20
  const [rawJson, _] = unpackMessage(bytes || new Uint8Array());
18
21
  this._emitDeveloperLog(rawJson["type"], rawJson["data"]);
19
22
  }
@@ -1,7 +1,7 @@
1
1
  import { MeshSchema } from './schema';
2
2
  import { RoomClient } from './room-client';
3
3
  import { ParticipantToken } from './participant-token';
4
- import { WebSocketClientProtocol } from './protocol';
4
+ import { ProtocolFactory } from './protocol';
5
5
  export declare function validateSchemaName(name: string): void;
6
6
  export declare function deploySchema({ room, schema, name, overwrite }: {
7
7
  room: RoomClient;
@@ -31,4 +31,4 @@ export declare function websocketProtocol({ participantName, roomName, role, pro
31
31
  secret?: string;
32
32
  apiUrl?: string;
33
33
  apiKey?: string;
34
- }): Promise<WebSocketClientProtocol>;
34
+ }): Promise<ProtocolFactory>;
@@ -56,5 +56,5 @@ export async function websocketProtocol({ participantName, roomName, role, proje
56
56
  const url = websocketRoomUrl({ roomName, apiUrl });
57
57
  const token = participantToken({ participantName, roomName, role, projectId, apiKeyId, apiKey });
58
58
  const jwt = await token.toJwt({ token: secret, apiKey });
59
- return new WebSocketClientProtocol({ url, token: jwt });
59
+ return WebSocketClientProtocol.createFactory({ url, token: jwt });
60
60
  }
@@ -202,6 +202,14 @@ export interface Mailbox {
202
202
  roomId?: string;
203
203
  queue: string;
204
204
  }
205
+ export interface ProjectRepository {
206
+ id: string;
207
+ projectId: string;
208
+ name: string;
209
+ description: string;
210
+ annotations: Record<string, string>;
211
+ createdAt: Date;
212
+ }
205
213
  export interface Balance {
206
214
  balance: number;
207
215
  autoRechargeThreshold?: number | null;
@@ -278,6 +286,7 @@ export declare class Meshagent {
278
286
  private parseRoomShare;
279
287
  private parseRoomSession;
280
288
  private parseRoom;
289
+ private parseProjectRepository;
281
290
  private parseProjectRoomGrant;
282
291
  private parseProjectRoomGrantCount;
283
292
  private parseProjectUserGrantCount;
@@ -393,6 +402,22 @@ export declare class Meshagent {
393
402
  }): Promise<void>;
394
403
  listMailboxes(projectId: string): Promise<Mailbox[]>;
395
404
  deleteMailbox(projectId: string, address: string): Promise<void>;
405
+ createRepository(params: {
406
+ projectId: string;
407
+ name: string;
408
+ description?: string;
409
+ annotations?: Record<string, string>;
410
+ }): Promise<ProjectRepository>;
411
+ updateRepository(params: {
412
+ projectId: string;
413
+ repositoryId: string;
414
+ name: string;
415
+ description?: string;
416
+ annotations?: Record<string, string>;
417
+ }): Promise<ProjectRepository>;
418
+ getRepository(projectId: string, repositoryId: string): Promise<ProjectRepository>;
419
+ listRepositories(projectId: string): Promise<ProjectRepository[]>;
420
+ deleteRepository(projectId: string, repositoryId: string): Promise<void>;
396
421
  createService(projectId: string, service: ServiceSpec): Promise<string>;
397
422
  createRoomService(projectId: string, roomName: string, service: ServiceSpec): Promise<string>;
398
423
  updateRoomService(projectId: string, roomName: string, serviceId: string, service: ServiceSpec): Promise<void>;
@@ -223,6 +223,33 @@ export class Meshagent {
223
223
  annotations: annotations && typeof annotations === "object" ? annotations : {},
224
224
  };
225
225
  }
226
+ parseProjectRepository(data) {
227
+ if (!data || typeof data !== "object") {
228
+ throw new RoomException("Invalid repository payload");
229
+ }
230
+ const { id, project_id: projectIdRaw, projectId, name, description, annotations, created_at: createdAtRaw, createdAt, } = data;
231
+ const projectIdValue = typeof projectId === "string"
232
+ ? projectId
233
+ : typeof projectIdRaw === "string"
234
+ ? projectIdRaw
235
+ : undefined;
236
+ const createdAtValue = typeof createdAt === "string"
237
+ ? createdAt
238
+ : typeof createdAtRaw === "string"
239
+ ? createdAtRaw
240
+ : undefined;
241
+ if (typeof id !== "string" || typeof projectIdValue !== "string" || typeof name !== "string" || typeof createdAtValue !== "string") {
242
+ throw new RoomException("Invalid repository payload: missing required fields");
243
+ }
244
+ return {
245
+ id,
246
+ projectId: projectIdValue,
247
+ name,
248
+ description: typeof description === "string" ? description : "",
249
+ annotations: annotations && typeof annotations === "object" ? annotations : {},
250
+ createdAt: new Date(createdAtValue),
251
+ };
252
+ }
226
253
  parseProjectRoomGrant(data) {
227
254
  if (!data || typeof data !== "object") {
228
255
  throw new RoomException("Invalid room grant payload");
@@ -889,6 +916,44 @@ export class Meshagent {
889
916
  responseType: "void",
890
917
  });
891
918
  }
919
+ async createRepository(params) {
920
+ const { projectId, name, description = "", annotations = {} } = params;
921
+ const data = await this.request(`/accounts/projects/${projectId}/repositories`, {
922
+ method: "POST",
923
+ json: { name, description, annotations },
924
+ action: "create repository",
925
+ });
926
+ return this.parseProjectRepository(data);
927
+ }
928
+ async updateRepository(params) {
929
+ const { projectId, repositoryId, name, description = "", annotations = {} } = params;
930
+ const data = await this.request(`/accounts/projects/${projectId}/repositories/${repositoryId}`, {
931
+ method: "PUT",
932
+ json: { name, description, annotations },
933
+ action: "update repository",
934
+ });
935
+ return this.parseProjectRepository(data);
936
+ }
937
+ async getRepository(projectId, repositoryId) {
938
+ const data = await this.request(`/accounts/projects/${projectId}/repositories/${repositoryId}`, {
939
+ action: "fetch repository",
940
+ });
941
+ return this.parseProjectRepository(data);
942
+ }
943
+ async listRepositories(projectId) {
944
+ const data = await this.request(`/accounts/projects/${projectId}/repositories`, {
945
+ action: "list repositories",
946
+ });
947
+ const repositories = Array.isArray(data?.repositories) ? data.repositories : [];
948
+ return repositories.map((item) => this.parseProjectRepository(item));
949
+ }
950
+ async deleteRepository(projectId, repositoryId) {
951
+ await this.request(`/accounts/projects/${projectId}/repositories/${repositoryId}`, {
952
+ method: "DELETE",
953
+ action: "delete repository",
954
+ responseType: "void",
955
+ });
956
+ }
892
957
  async createService(projectId, service) {
893
958
  const data = await this.request(`/accounts/projects/${projectId}/services`, {
894
959
  method: "POST",
@@ -1,6 +1,6 @@
1
1
  import { EventEmitter } from "./event-emitter";
2
- import { RoomClient } from "./room-client";
3
2
  import { Participant, RemoteParticipant } from "./participant";
3
+ import { RoomClient } from "./room-client";
4
4
  import { RoomMessageEvent } from "./room-event";
5
5
  type MessagePayload = Record<string, unknown>;
6
6
  export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
@@ -11,44 +11,57 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
11
11
  private _messageQueued;
12
12
  private _sendTask;
13
13
  private _messageQueueClosed;
14
- private _enabled;
14
+ private _desiredEnabled;
15
+ private _online;
16
+ private _enableInFlight;
15
17
  constructor({ room }: {
16
18
  room: RoomClient;
17
19
  });
20
+ get isEnabled(): boolean;
21
+ get online(): boolean;
22
+ get remoteParticipants(): RemoteParticipant[];
18
23
  private _messageInput;
19
- private _syntheticMessageEvent;
24
+ private _invoke;
25
+ private _invokeNowait;
26
+ start(): void;
27
+ stop(): Promise<void>;
28
+ private _nextQueuedMessage;
29
+ private _wakeMessageQueue;
30
+ private _queueMessage;
31
+ private _setOnline;
32
+ private _waitUntilOnline;
33
+ private _enableCurrentConnectionNowait;
34
+ private _clearCurrentConnectionState;
35
+ _onRoomDisconnect({ reason: _reason }: {
36
+ reason: string | null;
37
+ }): void;
38
+ _onRoomReconnect(): void;
20
39
  private _removeParticipant;
21
40
  private _markParticipantOffline;
22
41
  private _resolveMessageRecipient;
23
- private _queueMessage;
24
- private _rejectQueuedMessages;
25
- private _isParticipantNotFound;
26
- private _nextQueuedMessage;
42
+ private _dropQueuedMessage;
43
+ private _drainQueuedMessages;
27
44
  private _sendMessages;
28
- start(): Promise<void>;
29
- stop(): Promise<void>;
30
- sendMessage({ to, type, message, attachment, ignoreOffline }: {
45
+ sendMessage({ to, type, message, attachment, ignoreOffline, }: {
31
46
  to: Participant;
32
47
  type: string;
33
48
  message: MessagePayload;
34
49
  attachment?: Uint8Array;
35
50
  ignoreOffline?: boolean;
36
51
  }): Promise<void>;
37
- sendMessageNowait({ to, type, message, attachment }: {
52
+ sendMessageNowait({ to, type, message, attachment, }: {
38
53
  to: Participant;
39
54
  type: string;
40
55
  message: MessagePayload;
41
56
  attachment?: Uint8Array;
42
57
  }): void;
43
- enable(): Promise<void>;
44
- disable(): Promise<void>;
45
- broadcastMessage({ type, message, attachment }: {
58
+ enable(): void;
59
+ disable(): void;
60
+ broadcastMessage({ type, message, attachment, }: {
46
61
  type: string;
47
62
  message: MessagePayload;
48
63
  attachment?: Uint8Array;
49
64
  }): Promise<void>;
50
- get remoteParticipants(): RemoteParticipant[];
51
- get isEnabled(): boolean;
52
65
  getParticipants(): RemoteParticipant[];
53
66
  getParticipant(id: string): RemoteParticipant | null;
54
67
  getParticipantByName(name: string): RemoteParticipant | null;