@meshagent/meshagent 0.31.1 → 0.31.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 (41) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/browser/index.d.ts +2 -0
  3. package/dist/browser/index.js +2 -0
  4. package/dist/browser/memory-client.d.ts +23 -0
  5. package/dist/browser/memory-client.js +56 -0
  6. package/dist/browser/messaging-client.d.ts +5 -1
  7. package/dist/browser/messaging-client.js +49 -8
  8. package/dist/browser/participant-token.js +8 -2
  9. package/dist/browser/participant.d.ts +3 -1
  10. package/dist/browser/participant.js +5 -1
  11. package/dist/browser/room-client.d.ts +4 -0
  12. package/dist/browser/room-client.js +4 -0
  13. package/dist/browser/services-client.d.ts +28 -0
  14. package/dist/browser/services-client.js +93 -0
  15. package/dist/esm/index.d.ts +2 -0
  16. package/dist/esm/index.js +2 -0
  17. package/dist/esm/memory-client.d.ts +23 -0
  18. package/dist/esm/memory-client.js +52 -0
  19. package/dist/esm/messaging-client.d.ts +5 -1
  20. package/dist/esm/messaging-client.js +49 -8
  21. package/dist/esm/participant-token.js +8 -2
  22. package/dist/esm/participant.d.ts +3 -1
  23. package/dist/esm/participant.js +5 -1
  24. package/dist/esm/room-client.d.ts +4 -0
  25. package/dist/esm/room-client.js +4 -0
  26. package/dist/esm/services-client.d.ts +28 -0
  27. package/dist/esm/services-client.js +89 -0
  28. package/dist/node/index.d.ts +2 -0
  29. package/dist/node/index.js +2 -0
  30. package/dist/node/memory-client.d.ts +23 -0
  31. package/dist/node/memory-client.js +56 -0
  32. package/dist/node/messaging-client.d.ts +5 -1
  33. package/dist/node/messaging-client.js +49 -8
  34. package/dist/node/participant-token.js +8 -2
  35. package/dist/node/participant.d.ts +3 -1
  36. package/dist/node/participant.js +5 -1
  37. package/dist/node/room-client.d.ts +4 -0
  38. package/dist/node/room-client.js +4 -0
  39. package/dist/node/services-client.d.ts +28 -0
  40. package/dist/node/services-client.js +93 -0
  41. package/package.json +1 -1
@@ -2,6 +2,7 @@ import { v4 as uuidV4 } from "uuid";
2
2
  import { EventEmitter } from "./event-emitter";
3
3
  import { RemoteParticipant } from "./participant";
4
4
  import { RoomMessage, RoomMessageEvent } from "./room-event";
5
+ import { RoomServerException } from "./room-server-client";
5
6
  import { splitMessageHeader, splitMessagePayload } from "./utils";
6
7
  import { StreamController } from "./stream-controller";
7
8
  import { Completer } from "./completer";
@@ -47,6 +48,37 @@ export class MessagingClient extends EventEmitter {
47
48
  }
48
49
  return input;
49
50
  }
51
+ _removeParticipant(participantId) {
52
+ const participant = this._participants[participantId];
53
+ if (participant === undefined) {
54
+ return undefined;
55
+ }
56
+ participant._setOnline(false);
57
+ delete this._participants[participantId];
58
+ for (const [streamId, reader] of Object.entries(this._streamReaders)) {
59
+ if (reader._to.id !== participant.id) {
60
+ continue;
61
+ }
62
+ reader._controller.close();
63
+ delete this._streamReaders[streamId];
64
+ }
65
+ return participant;
66
+ }
67
+ _resolveMessageRecipient(to) {
68
+ if (!(to instanceof RemoteParticipant)) {
69
+ return to;
70
+ }
71
+ if (to.online === false) {
72
+ return null;
73
+ }
74
+ return this._participants[to.id] ?? null;
75
+ }
76
+ _participantNotFound(ignoreOffline) {
77
+ if (ignoreOffline) {
78
+ return null;
79
+ }
80
+ throw new RoomServerException("the participant was not found");
81
+ }
50
82
  async createStream({ to, header }) {
51
83
  const streamId = uuidV4();
52
84
  const completer = new Completer();
@@ -58,12 +90,16 @@ export class MessagingClient extends EventEmitter {
58
90
  });
59
91
  return completer.fut;
60
92
  }
61
- async sendMessage({ to, type, message, attachment }) {
93
+ async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
94
+ const resolvedTo = this._resolveMessageRecipient(to) ?? this._participantNotFound(ignoreOffline);
95
+ if (resolvedTo === null) {
96
+ return;
97
+ }
62
98
  await this.client.invoke({
63
99
  toolkit: "messaging",
64
100
  tool: "send",
65
101
  input: this._messageInput({
66
- toParticipantId: to.id,
102
+ toParticipantId: resolvedTo.id,
67
103
  type,
68
104
  message,
69
105
  attachment,
@@ -140,7 +176,7 @@ export class MessagingClient extends EventEmitter {
140
176
  }
141
177
  _onParticipantEnabled(message) {
142
178
  const data = message.message;
143
- const p = new RemoteParticipant(this.client, data["id"], data["role"]);
179
+ const p = new RemoteParticipant(this.client, data["id"], data["role"], true);
144
180
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
145
181
  p._attributes[k] = v;
146
182
  }
@@ -158,16 +194,15 @@ export class MessagingClient extends EventEmitter {
158
194
  this.emit("participant_attributes_updated", { message });
159
195
  }
160
196
  _onParticipantDisabled(message) {
161
- const part = this._participants[message.message["id"]];
197
+ const part = this._removeParticipant(message.message["id"]);
162
198
  if (part) {
163
- delete this._participants[message.message["id"]];
164
199
  this.emit("participant_removed", { message });
165
200
  }
166
201
  }
167
202
  _onMessagingEnabled(message) {
168
203
  const participants = message.message["participants"];
169
204
  for (const data of participants) {
170
- const rp = new RemoteParticipant(this.client, data["id"], data["role"]);
205
+ const rp = new RemoteParticipant(this.client, data["id"], data["role"], true);
171
206
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
172
207
  rp._attributes[k] = v;
173
208
  }
@@ -193,17 +228,23 @@ export class MessagingClient extends EventEmitter {
193
228
  throw new Error("streams are not allowed by this client");
194
229
  }
195
230
  this._onStreamAcceptCallback(reader);
196
- this.sendMessage({
231
+ void this.sendMessage({
197
232
  to: from,
198
233
  type: "stream.accept",
199
234
  message: { stream_id: streamId },
235
+ ignoreOffline: true,
236
+ }).catch((error) => {
237
+ console.warn("unable to send stream response", error);
200
238
  });
201
239
  }
202
240
  catch (e) {
203
- this.sendMessage({
241
+ void this.sendMessage({
204
242
  to: from,
205
243
  type: "stream.reject",
206
244
  message: { stream_id: streamId, error: String(e) },
245
+ ignoreOffline: true,
246
+ }).catch((error) => {
247
+ console.warn("unable to send stream response", error);
207
248
  });
208
249
  }
209
250
  this._streamReaders[streamId] = reader;
@@ -1,5 +1,11 @@
1
1
  import { decodeJwt, jwtVerify, SignJWT } from "jose";
2
2
  import { parseApiKey } from "./api_keys";
3
+ function getEnvValue(name) {
4
+ if (typeof process === "undefined") {
5
+ return undefined;
6
+ }
7
+ return process.env?.[name];
8
+ }
3
9
  export class AgentsGrant {
4
10
  constructor({ registerAgent, registerPublicToolkit, registerPrivateToolkit, call, useAgents, useTools, } = {}) {
5
11
  this.registerAgent = registerAgent ?? true;
@@ -352,7 +358,7 @@ export class ParticipantToken {
352
358
  let resolvedSecret = token;
353
359
  let resolvedKid = this.apiKeyId;
354
360
  let resolvedSub = this.projectId;
355
- const apiKeyValue = apiKey ?? process.env.MESHAGENT_API_KEY;
361
+ const apiKeyValue = apiKey ?? getEnvValue("MESHAGENT_API_KEY");
356
362
  if (apiKeyValue) {
357
363
  const parsed = parseApiKey(apiKeyValue);
358
364
  resolvedSecret ?? (resolvedSecret = parsed.secret);
@@ -361,7 +367,7 @@ export class ParticipantToken {
361
367
  }
362
368
  let usingDefaultSecret = false;
363
369
  if (!resolvedSecret) {
364
- const envSecret = process.env.MESHAGENT_SECRET;
370
+ const envSecret = getEnvValue("MESHAGENT_SECRET");
365
371
  if (!envSecret) {
366
372
  throw new Error("ParticipantToken.toJwt: No secret provided. Pass `token`, `apiKey`, or set MESHAGENT_SECRET / MESHAGENT_API_KEY.");
367
373
  }
@@ -10,7 +10,9 @@ export declare abstract class Participant {
10
10
  }
11
11
  export declare class RemoteParticipant extends Participant {
12
12
  readonly role: string;
13
- constructor(client: RoomClient, id: string, role: string);
13
+ online?: boolean;
14
+ constructor(client: RoomClient, id: string, role: string, online?: boolean);
15
+ _setOnline(online: boolean): void;
14
16
  }
15
17
  export declare class LocalParticipant extends Participant {
16
18
  constructor(client: RoomClient, id: string);
@@ -14,9 +14,13 @@ export class Participant {
14
14
  }
15
15
  }
16
16
  export class RemoteParticipant extends Participant {
17
- constructor(client, id, role) {
17
+ constructor(client, id, role, online) {
18
18
  super(client, id);
19
19
  this.role = role;
20
+ this.online = online;
21
+ }
22
+ _setOnline(online) {
23
+ this.online = online;
20
24
  }
21
25
  }
22
26
  export class LocalParticipant extends Participant {
@@ -9,6 +9,8 @@ import { DatabaseClient } from "./database-client";
9
9
  import { AgentsClient, ToolkitDescription } from "./agent-client";
10
10
  import { SecretsClient } from "./secrets-client";
11
11
  import { ContainersClient } from "./containers-client";
12
+ import { MemoryClient } from "./memory-client";
13
+ import { ServicesClient } from "./services-client";
12
14
  import { RoomEvent } from "./room-event";
13
15
  import { Content } from "./response";
14
16
  interface RequestHeader {
@@ -25,6 +27,8 @@ export declare class RoomClient {
25
27
  readonly agents: AgentsClient;
26
28
  readonly secrets: SecretsClient;
27
29
  readonly containers: ContainersClient;
30
+ readonly memory: MemoryClient;
31
+ readonly services: ServicesClient;
28
32
  private _pendingRequests;
29
33
  private _ready;
30
34
  private _localParticipant;
@@ -11,6 +11,8 @@ import { DatabaseClient } from "./database-client";
11
11
  import { AgentsClient, ToolkitDescription } from "./agent-client";
12
12
  import { SecretsClient } from "./secrets-client";
13
13
  import { ContainersClient } from "./containers-client";
14
+ import { MemoryClient } from "./memory-client";
15
+ import { ServicesClient } from "./services-client";
14
16
  import { RoomServerException } from "./room-server-client";
15
17
  import { BinaryContent, ControlContent, EmptyContent, ErrorContent, FileContent, JsonContent, LinkContent, TextContent, unpackContent } from "./response";
16
18
  export class RoomClient {
@@ -34,6 +36,8 @@ export class RoomClient {
34
36
  this.agents = new AgentsClient({ room: this });
35
37
  this.secrets = new SecretsClient({ room: this });
36
38
  this.containers = new ContainersClient({ room: this });
39
+ this.memory = new MemoryClient({ room: this });
40
+ this.services = new ServicesClient({ room: this });
37
41
  }
38
42
  get localParticipant() {
39
43
  return this._localParticipant;
@@ -0,0 +1,28 @@
1
+ import { ServiceSpec } from "./meshagent-client";
2
+ import { RoomClient } from "./room-client";
3
+ export interface ServiceRuntimeState {
4
+ serviceId: string;
5
+ state: string;
6
+ containerId?: string;
7
+ restartScheduledAt?: number;
8
+ startedAt?: number;
9
+ restartCount: number;
10
+ lastExitCode?: number;
11
+ lastExitAt?: number;
12
+ }
13
+ export interface ListServicesResult {
14
+ services: ServiceSpec[];
15
+ serviceStates: Record<string, ServiceRuntimeState>;
16
+ }
17
+ export declare class ServicesClient {
18
+ private readonly room;
19
+ constructor({ room }: {
20
+ room: RoomClient;
21
+ });
22
+ private unexpectedResponse;
23
+ list(): Promise<ServiceSpec[]>;
24
+ listWithState(): Promise<ListServicesResult>;
25
+ restart(params: {
26
+ serviceId: string;
27
+ }): Promise<void>;
28
+ }
@@ -0,0 +1,89 @@
1
+ import { JsonContent, EmptyContent } from "./response";
2
+ import { RoomServerException } from "./room-server-client";
3
+ function isRecord(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ }
6
+ function toOptionalNumber(value) {
7
+ return typeof value === "number" ? value : undefined;
8
+ }
9
+ function toOptionalInteger(value) {
10
+ return typeof value === "number" && Number.isInteger(value) ? value : undefined;
11
+ }
12
+ function parseServiceRuntimeState(value) {
13
+ if (!isRecord(value) || typeof value["service_id"] !== "string") {
14
+ throw new RoomServerException("unexpected return type from services.list");
15
+ }
16
+ const state = value["state"];
17
+ const containerId = value["container_id"];
18
+ return {
19
+ serviceId: value["service_id"],
20
+ state: typeof state === "string" ? state : "unknown",
21
+ containerId: typeof containerId === "string" ? containerId : undefined,
22
+ restartScheduledAt: toOptionalNumber(value["restart_scheduled_at"]),
23
+ startedAt: toOptionalNumber(value["started_at"]),
24
+ restartCount: toOptionalInteger(value["restart_count"]) ?? 0,
25
+ lastExitCode: toOptionalInteger(value["last_exit_code"]),
26
+ lastExitAt: toOptionalNumber(value["last_exit_at"]),
27
+ };
28
+ }
29
+ function parseServiceSpecJson(value) {
30
+ if (typeof value === "string") {
31
+ const parsed = JSON.parse(value);
32
+ if (!isRecord(parsed)) {
33
+ throw new RoomServerException("unexpected return type from services.list");
34
+ }
35
+ return parsed;
36
+ }
37
+ if (!isRecord(value)) {
38
+ throw new RoomServerException("unexpected return type from services.list");
39
+ }
40
+ return value;
41
+ }
42
+ export class ServicesClient {
43
+ constructor({ room }) {
44
+ this.room = room;
45
+ }
46
+ unexpectedResponse(operation) {
47
+ return new RoomServerException(`unexpected return type from services.${operation}`);
48
+ }
49
+ async list() {
50
+ return (await this.listWithState()).services;
51
+ }
52
+ async listWithState() {
53
+ const response = await this.room.invoke({
54
+ toolkit: "services",
55
+ tool: "list",
56
+ input: {},
57
+ });
58
+ if (!(response instanceof JsonContent)) {
59
+ throw this.unexpectedResponse("list");
60
+ }
61
+ const servicesRaw = response.json["services_json"];
62
+ const serviceStatesRaw = response.json["service_states"];
63
+ if (!Array.isArray(servicesRaw) || !Array.isArray(serviceStatesRaw)) {
64
+ throw this.unexpectedResponse("list");
65
+ }
66
+ const serviceStates = {};
67
+ for (const item of serviceStatesRaw) {
68
+ const state = parseServiceRuntimeState(item);
69
+ serviceStates[state.serviceId] = state;
70
+ }
71
+ return {
72
+ services: servicesRaw.map((item) => parseServiceSpecJson(item)),
73
+ serviceStates,
74
+ };
75
+ }
76
+ async restart(params) {
77
+ const response = await this.room.invoke({
78
+ toolkit: "services",
79
+ tool: "restart",
80
+ input: {
81
+ service_id: params.serviceId,
82
+ },
83
+ });
84
+ if (response instanceof EmptyContent || response instanceof JsonContent) {
85
+ return;
86
+ }
87
+ throw this.unexpectedResponse("restart");
88
+ }
89
+ }
@@ -10,6 +10,7 @@ export * from './developer-client';
10
10
  export * from './document';
11
11
  export * from './meshagent-client';
12
12
  export * from './messaging-client';
13
+ export * from './memory-client';
13
14
  export * from './participant-token';
14
15
  export * from './participant';
15
16
  export * from './protocol';
@@ -22,6 +23,7 @@ export * from './room-server-client';
22
23
  export * from './runtime';
23
24
  export * from './schema';
24
25
  export * from './storage-client';
26
+ export * from './services-client';
25
27
  export * from './secrets-client';
26
28
  export * from './stream-controller';
27
29
  export * from './sync-client';
@@ -26,6 +26,7 @@ __exportStar(require("./developer-client"), exports);
26
26
  __exportStar(require("./document"), exports);
27
27
  __exportStar(require("./meshagent-client"), exports);
28
28
  __exportStar(require("./messaging-client"), exports);
29
+ __exportStar(require("./memory-client"), exports);
29
30
  __exportStar(require("./participant-token"), exports);
30
31
  __exportStar(require("./participant"), exports);
31
32
  __exportStar(require("./protocol"), exports);
@@ -38,6 +39,7 @@ __exportStar(require("./room-server-client"), exports);
38
39
  __exportStar(require("./runtime"), exports);
39
40
  __exportStar(require("./schema"), exports);
40
41
  __exportStar(require("./storage-client"), exports);
42
+ __exportStar(require("./services-client"), exports);
41
43
  __exportStar(require("./secrets-client"), exports);
42
44
  __exportStar(require("./stream-controller"), exports);
43
45
  __exportStar(require("./sync-client"), exports);
@@ -0,0 +1,23 @@
1
+ import { RoomClient } from "./room-client";
2
+ export declare class MemoryClient {
3
+ private readonly room;
4
+ constructor({ room }: {
5
+ room: RoomClient;
6
+ });
7
+ private unexpectedResponse;
8
+ private invoke;
9
+ list(params?: {
10
+ namespace?: string[] | null;
11
+ }): Promise<string[]>;
12
+ create(params: {
13
+ name: string;
14
+ namespace?: string[] | null;
15
+ overwrite?: boolean;
16
+ ignoreExists?: boolean;
17
+ }): Promise<void>;
18
+ drop(params: {
19
+ name: string;
20
+ namespace?: string[] | null;
21
+ ignoreMissing?: boolean;
22
+ }): Promise<void>;
23
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryClient = void 0;
4
+ const response_1 = require("./response");
5
+ const room_server_client_1 = require("./room-server-client");
6
+ class MemoryClient {
7
+ constructor({ room }) {
8
+ this.room = room;
9
+ }
10
+ unexpectedResponse(operation) {
11
+ return new room_server_client_1.RoomServerException(`unexpected return type from memory.${operation}`);
12
+ }
13
+ async invoke(operation, input) {
14
+ const response = await this.room.invoke({
15
+ toolkit: "memory",
16
+ tool: operation,
17
+ input,
18
+ });
19
+ if (response instanceof response_1.JsonContent) {
20
+ return response;
21
+ }
22
+ if (response instanceof response_1.EmptyContent) {
23
+ return null;
24
+ }
25
+ throw this.unexpectedResponse(operation);
26
+ }
27
+ async list(params) {
28
+ const response = await this.invoke("list", {
29
+ namespace: params?.namespace ?? null,
30
+ });
31
+ if (!(response instanceof response_1.JsonContent)) {
32
+ throw this.unexpectedResponse("list");
33
+ }
34
+ const memories = response.json["memories"];
35
+ if (!Array.isArray(memories)) {
36
+ return [];
37
+ }
38
+ return memories.filter((value) => typeof value === "string");
39
+ }
40
+ async create(params) {
41
+ await this.invoke("create", {
42
+ name: params.name,
43
+ namespace: params.namespace ?? null,
44
+ overwrite: params.overwrite ?? false,
45
+ ignore_exists: params.ignoreExists ?? false,
46
+ });
47
+ }
48
+ async drop(params) {
49
+ await this.invoke("drop", {
50
+ name: params.name,
51
+ namespace: params.namespace ?? null,
52
+ ignore_missing: params.ignoreMissing ?? false,
53
+ });
54
+ }
55
+ }
56
+ exports.MemoryClient = MemoryClient;
@@ -21,15 +21,19 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
21
21
  room: RoomClient;
22
22
  });
23
23
  private _messageInput;
24
+ private _removeParticipant;
25
+ private _resolveMessageRecipient;
26
+ private _participantNotFound;
24
27
  createStream({ to, header }: {
25
28
  to: Participant;
26
29
  header: Record<string, any>;
27
30
  }): Promise<MessageStreamWriter>;
28
- sendMessage({ to, type, message, attachment }: {
31
+ sendMessage({ to, type, message, attachment, ignoreOffline }: {
29
32
  to: Participant;
30
33
  type: string;
31
34
  message: Record<string, any>;
32
35
  attachment?: Uint8Array;
36
+ ignoreOffline?: boolean;
33
37
  }): Promise<void>;
34
38
  enable(onStreamAccept?: (reader: MessageStreamReader) => void): Promise<void>;
35
39
  disable(): Promise<void>;
@@ -5,6 +5,7 @@ const uuid_1 = require("uuid");
5
5
  const event_emitter_1 = require("./event-emitter");
6
6
  const participant_1 = require("./participant");
7
7
  const room_event_1 = require("./room-event");
8
+ const room_server_client_1 = require("./room-server-client");
8
9
  const utils_1 = require("./utils");
9
10
  const stream_controller_1 = require("./stream-controller");
10
11
  const completer_1 = require("./completer");
@@ -51,6 +52,37 @@ class MessagingClient extends event_emitter_1.EventEmitter {
51
52
  }
52
53
  return input;
53
54
  }
55
+ _removeParticipant(participantId) {
56
+ const participant = this._participants[participantId];
57
+ if (participant === undefined) {
58
+ return undefined;
59
+ }
60
+ participant._setOnline(false);
61
+ delete this._participants[participantId];
62
+ for (const [streamId, reader] of Object.entries(this._streamReaders)) {
63
+ if (reader._to.id !== participant.id) {
64
+ continue;
65
+ }
66
+ reader._controller.close();
67
+ delete this._streamReaders[streamId];
68
+ }
69
+ return participant;
70
+ }
71
+ _resolveMessageRecipient(to) {
72
+ if (!(to instanceof participant_1.RemoteParticipant)) {
73
+ return to;
74
+ }
75
+ if (to.online === false) {
76
+ return null;
77
+ }
78
+ return this._participants[to.id] ?? null;
79
+ }
80
+ _participantNotFound(ignoreOffline) {
81
+ if (ignoreOffline) {
82
+ return null;
83
+ }
84
+ throw new room_server_client_1.RoomServerException("the participant was not found");
85
+ }
54
86
  async createStream({ to, header }) {
55
87
  const streamId = (0, uuid_1.v4)();
56
88
  const completer = new completer_1.Completer();
@@ -62,12 +94,16 @@ class MessagingClient extends event_emitter_1.EventEmitter {
62
94
  });
63
95
  return completer.fut;
64
96
  }
65
- async sendMessage({ to, type, message, attachment }) {
97
+ async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
98
+ const resolvedTo = this._resolveMessageRecipient(to) ?? this._participantNotFound(ignoreOffline);
99
+ if (resolvedTo === null) {
100
+ return;
101
+ }
66
102
  await this.client.invoke({
67
103
  toolkit: "messaging",
68
104
  tool: "send",
69
105
  input: this._messageInput({
70
- toParticipantId: to.id,
106
+ toParticipantId: resolvedTo.id,
71
107
  type,
72
108
  message,
73
109
  attachment,
@@ -144,7 +180,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
144
180
  }
145
181
  _onParticipantEnabled(message) {
146
182
  const data = message.message;
147
- const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
183
+ const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
148
184
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
149
185
  p._attributes[k] = v;
150
186
  }
@@ -162,16 +198,15 @@ class MessagingClient extends event_emitter_1.EventEmitter {
162
198
  this.emit("participant_attributes_updated", { message });
163
199
  }
164
200
  _onParticipantDisabled(message) {
165
- const part = this._participants[message.message["id"]];
201
+ const part = this._removeParticipant(message.message["id"]);
166
202
  if (part) {
167
- delete this._participants[message.message["id"]];
168
203
  this.emit("participant_removed", { message });
169
204
  }
170
205
  }
171
206
  _onMessagingEnabled(message) {
172
207
  const participants = message.message["participants"];
173
208
  for (const data of participants) {
174
- const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
209
+ const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
175
210
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
176
211
  rp._attributes[k] = v;
177
212
  }
@@ -197,17 +232,23 @@ class MessagingClient extends event_emitter_1.EventEmitter {
197
232
  throw new Error("streams are not allowed by this client");
198
233
  }
199
234
  this._onStreamAcceptCallback(reader);
200
- this.sendMessage({
235
+ void this.sendMessage({
201
236
  to: from,
202
237
  type: "stream.accept",
203
238
  message: { stream_id: streamId },
239
+ ignoreOffline: true,
240
+ }).catch((error) => {
241
+ console.warn("unable to send stream response", error);
204
242
  });
205
243
  }
206
244
  catch (e) {
207
- this.sendMessage({
245
+ void this.sendMessage({
208
246
  to: from,
209
247
  type: "stream.reject",
210
248
  message: { stream_id: streamId, error: String(e) },
249
+ ignoreOffline: true,
250
+ }).catch((error) => {
251
+ console.warn("unable to send stream response", error);
211
252
  });
212
253
  }
213
254
  this._streamReaders[streamId] = reader;
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParticipantToken = exports.ParticipantGrant = exports.ApiScope = exports.SecretsGrant = exports.AdminGrant = exports.DeveloperGrant = exports.ContainersGrant = exports.StorageGrant = exports.StoragePathGrant = exports.SyncGrant = exports.SyncPathGrant = exports.DatabaseGrant = exports.TableGrant = exports.MessagingGrant = exports.QueuesGrant = exports.LivekitGrant = exports.AgentsGrant = void 0;
4
4
  const jose_1 = require("jose");
5
5
  const api_keys_1 = require("./api_keys");
6
+ function getEnvValue(name) {
7
+ if (typeof process === "undefined") {
8
+ return undefined;
9
+ }
10
+ return process.env?.[name];
11
+ }
6
12
  class AgentsGrant {
7
13
  constructor({ registerAgent, registerPublicToolkit, registerPrivateToolkit, call, useAgents, useTools, } = {}) {
8
14
  this.registerAgent = registerAgent ?? true;
@@ -371,7 +377,7 @@ class ParticipantToken {
371
377
  let resolvedSecret = token;
372
378
  let resolvedKid = this.apiKeyId;
373
379
  let resolvedSub = this.projectId;
374
- const apiKeyValue = apiKey ?? process.env.MESHAGENT_API_KEY;
380
+ const apiKeyValue = apiKey ?? getEnvValue("MESHAGENT_API_KEY");
375
381
  if (apiKeyValue) {
376
382
  const parsed = (0, api_keys_1.parseApiKey)(apiKeyValue);
377
383
  resolvedSecret ?? (resolvedSecret = parsed.secret);
@@ -380,7 +386,7 @@ class ParticipantToken {
380
386
  }
381
387
  let usingDefaultSecret = false;
382
388
  if (!resolvedSecret) {
383
- const envSecret = process.env.MESHAGENT_SECRET;
389
+ const envSecret = getEnvValue("MESHAGENT_SECRET");
384
390
  if (!envSecret) {
385
391
  throw new Error("ParticipantToken.toJwt: No secret provided. Pass `token`, `apiKey`, or set MESHAGENT_SECRET / MESHAGENT_API_KEY.");
386
392
  }
@@ -10,7 +10,9 @@ export declare abstract class Participant {
10
10
  }
11
11
  export declare class RemoteParticipant extends Participant {
12
12
  readonly role: string;
13
- constructor(client: RoomClient, id: string, role: string);
13
+ online?: boolean;
14
+ constructor(client: RoomClient, id: string, role: string, online?: boolean);
15
+ _setOnline(online: boolean): void;
14
16
  }
15
17
  export declare class LocalParticipant extends Participant {
16
18
  constructor(client: RoomClient, id: string);
@@ -18,9 +18,13 @@ class Participant {
18
18
  }
19
19
  exports.Participant = Participant;
20
20
  class RemoteParticipant extends Participant {
21
- constructor(client, id, role) {
21
+ constructor(client, id, role, online) {
22
22
  super(client, id);
23
23
  this.role = role;
24
+ this.online = online;
25
+ }
26
+ _setOnline(online) {
27
+ this.online = online;
24
28
  }
25
29
  }
26
30
  exports.RemoteParticipant = RemoteParticipant;
@@ -9,6 +9,8 @@ import { DatabaseClient } from "./database-client";
9
9
  import { AgentsClient, ToolkitDescription } from "./agent-client";
10
10
  import { SecretsClient } from "./secrets-client";
11
11
  import { ContainersClient } from "./containers-client";
12
+ import { MemoryClient } from "./memory-client";
13
+ import { ServicesClient } from "./services-client";
12
14
  import { RoomEvent } from "./room-event";
13
15
  import { Content } from "./response";
14
16
  interface RequestHeader {
@@ -25,6 +27,8 @@ export declare class RoomClient {
25
27
  readonly agents: AgentsClient;
26
28
  readonly secrets: SecretsClient;
27
29
  readonly containers: ContainersClient;
30
+ readonly memory: MemoryClient;
31
+ readonly services: ServicesClient;
28
32
  private _pendingRequests;
29
33
  private _ready;
30
34
  private _localParticipant;