@meshagent/meshagent 0.46.4 → 0.47.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.
@@ -189,7 +189,8 @@ export declare class RoomClient {
189
189
  }): void;
190
190
  _sendLocalAttributesNowait(attributes: Record<string, unknown>): void;
191
191
  _resendLocalAttributesNowait(): void;
192
- sendRequest(type: string, request: RequestHeader, data?: Uint8Array): Promise<Content>;
192
+ sendRequest(type: string, request: RequestHeader, data?: Uint8Array, afterSend?: () => void): Promise<Content>;
193
+ private _dispatchRequest;
193
194
  call(params: {
194
195
  name: string;
195
196
  url: string;
@@ -216,6 +217,7 @@ export declare class RoomClient {
216
217
  input?: InvokeInput;
217
218
  participantId?: string;
218
219
  onBehalfOfId?: string;
220
+ afterSend?: () => void;
219
221
  }): Promise<Content>;
220
222
  invokeToolCall(params: {
221
223
  toolkit: string;
@@ -224,6 +226,7 @@ export declare class RoomClient {
224
226
  streamInput?: boolean;
225
227
  participantId?: string;
226
228
  onBehalfOfId?: string;
229
+ afterSend?: () => void;
227
230
  }): Promise<{
228
231
  kind: "content";
229
232
  content: Content;
@@ -247,7 +250,7 @@ export declare class RoomClient {
247
250
  participantId?: string;
248
251
  onBehalfOfId?: string;
249
252
  }): Promise<AsyncIterable<Content>>;
250
- private _sendToolCallRequestChunk;
253
+ private _dispatchToolCallRequestChunk;
251
254
  private _streamInvokeToolRequestChunks;
252
255
  private _decodeToolCallContent;
253
256
  private _handleToolCallResponseChunk;
@@ -1142,7 +1142,12 @@ export class RoomClient {
1142
1142
  }
1143
1143
  this._sendLocalAttributesNowait(attributes);
1144
1144
  }
1145
- async sendRequest(type, request, data) {
1145
+ async sendRequest(type, request, data, afterSend) {
1146
+ const response = this._dispatchRequest(type, request, data);
1147
+ afterSend?.();
1148
+ return await response;
1149
+ }
1150
+ _dispatchRequest(type, request, data) {
1146
1151
  this._raiseIfTerminal();
1147
1152
  if (this._entered && !this._connected && !this._allowDisconnectedRequests) {
1148
1153
  throw this._disconnectedError({ baseMessage: "room connection is disconnected" });
@@ -1151,8 +1156,8 @@ export class RoomClient {
1151
1156
  const completer = new Completer();
1152
1157
  this._pendingRequests.set(requestId, completer);
1153
1158
  try {
1154
- await this._protocolInstance.send(type, packMessage(request, data), requestId);
1155
- return await completer.fut;
1159
+ this._protocolInstance.sendNowait(type, packMessage(request, data), { id: requestId });
1160
+ return completer.fut;
1156
1161
  }
1157
1162
  catch (error) {
1158
1163
  this._pendingRequests.delete(requestId);
@@ -1235,6 +1240,7 @@ export class RoomClient {
1235
1240
  input: this._normalizeInvokeInput(params.input ?? params.arguments),
1236
1241
  participantId: params.participantId,
1237
1242
  onBehalfOfId: params.onBehalfOfId,
1243
+ afterSend: params.afterSend,
1238
1244
  });
1239
1245
  if (output.kind !== "content") {
1240
1246
  throw new RoomServerException("unexpected streamed output from " + params.toolkit + "." + params.tool);
@@ -1257,7 +1263,6 @@ export class RoomClient {
1257
1263
  let requestTask;
1258
1264
  if (params.streamInput === true) {
1259
1265
  request["arguments"] = { type: "control", method: "open" };
1260
- requestTask = this._streamInvokeToolRequestChunks(toolCallId, params.input);
1261
1266
  }
1262
1267
  else {
1263
1268
  const input = params.input;
@@ -1274,9 +1279,14 @@ export class RoomClient {
1274
1279
  if (params.onBehalfOfId != null) {
1275
1280
  request["on_behalf_of_id"] = params.onBehalfOfId;
1276
1281
  }
1282
+ const responseRequest = this._dispatchRequest("room.invoke_tool", request, requestData);
1283
+ params.afterSend?.();
1284
+ if (params.streamInput === true) {
1285
+ requestTask = this._streamInvokeToolRequestChunks(toolCallId, params.input);
1286
+ }
1277
1287
  try {
1278
1288
  const response = await Promise.race([
1279
- this.sendRequest("room.invoke_tool", request, requestData),
1289
+ responseRequest,
1280
1290
  preOpenError.fut,
1281
1291
  ]);
1282
1292
  this._toolCallPreOpenErrors.delete(toolCallId);
@@ -1331,9 +1341,10 @@ export class RoomClient {
1331
1341
  if (params.onBehalfOfId != null) {
1332
1342
  request["on_behalf_of_id"] = params.onBehalfOfId;
1333
1343
  }
1344
+ const responseRequest = this._dispatchRequest("room.invoke_tool", request);
1334
1345
  const requestTask = this._streamInvokeToolRequestChunks(toolCallId, params.input);
1335
1346
  try {
1336
- const response = await this.sendRequest("room.invoke_tool", request);
1347
+ const response = await responseRequest;
1337
1348
  await requestTask;
1338
1349
  if (response instanceof ControlContent && response.method === "open") {
1339
1350
  throw new RoomServerException(`unexpected return type from ${params.toolkit}.${params.tool}`);
@@ -1364,6 +1375,7 @@ export class RoomClient {
1364
1375
  if (params.onBehalfOfId != null) {
1365
1376
  request["on_behalf_of_id"] = params.onBehalfOfId;
1366
1377
  }
1378
+ const responseRequest = this._dispatchRequest("room.invoke_tool", request);
1367
1379
  const requestTask = this._streamInvokeToolRequestChunks(toolCallId, params.input);
1368
1380
  void requestTask.catch((error) => {
1369
1381
  const stream = this._toolCallStreams.get(toolCallId);
@@ -1377,7 +1389,7 @@ export class RoomClient {
1377
1389
  let response;
1378
1390
  try {
1379
1391
  response = await Promise.race([
1380
- this.sendRequest("room.invoke_tool", request),
1392
+ responseRequest,
1381
1393
  preOpenError.fut,
1382
1394
  ]);
1383
1395
  }
@@ -1399,24 +1411,27 @@ export class RoomClient {
1399
1411
  },
1400
1412
  };
1401
1413
  }
1402
- async _sendToolCallRequestChunk(toolCallId, chunk) {
1414
+ _dispatchToolCallRequestChunk(toolCallId, chunk) {
1415
+ this._raiseIfTerminal();
1416
+ if (this._entered && !this._connected && !this._allowDisconnectedRequests) {
1417
+ throw this._disconnectedError({ baseMessage: "room connection is disconnected" });
1418
+ }
1403
1419
  const packed = chunk.pack();
1404
1420
  const request = {
1405
1421
  tool_call_id: toolCallId,
1406
1422
  chunk: JSON.parse(splitMessageHeader(packed)),
1407
1423
  };
1408
1424
  const payload = splitMessagePayload(packed);
1409
- await this.sendRequest("room.tool_call_request_chunk", request, payload.length > 0 ? payload : undefined);
1425
+ this._protocolInstance.sendNowait("room.tool_call_request_chunk", packMessage(request, payload.length > 0 ? payload : undefined));
1410
1426
  }
1411
1427
  async _streamInvokeToolRequestChunks(toolCallId, input) {
1412
- await Promise.resolve();
1413
1428
  try {
1414
1429
  for await (const item of input) {
1415
- await this._sendToolCallRequestChunk(toolCallId, item);
1430
+ this._dispatchToolCallRequestChunk(toolCallId, item);
1416
1431
  }
1417
1432
  }
1418
1433
  finally {
1419
- await this._sendToolCallRequestChunk(toolCallId, new ControlContent({ method: "close" }));
1434
+ this._dispatchToolCallRequestChunk(toolCallId, new ControlContent({ method: "close" }));
1420
1435
  }
1421
1436
  }
1422
1437
  _decodeToolCallContent(params) {
@@ -530,10 +530,10 @@ class _RemoteToolkitWrapper {
530
530
  return false;
531
531
  }
532
532
  }
533
- async _sendToolCallResponseChunk(messageId, toolCallId, chunk) {
533
+ _sendToolCallResponseChunk(messageId, toolCallId, chunk) {
534
534
  const [header, payload] = (0, utils_js_1.unpackMessage)(chunk.pack());
535
535
  try {
536
- await this.client.protocol.send("room.tool_call_response_chunk", (0, utils_js_1.packMessage)({ tool_call_id: toolCallId, chunk: header }, payload.length > 0 ? payload : undefined), { id: messageId });
536
+ this.client.protocol.sendNowait("room.tool_call_response_chunk", (0, utils_js_1.packMessage)({ tool_call_id: toolCallId, chunk: header }, payload.length > 0 ? payload : undefined), { id: messageId });
537
537
  return true;
538
538
  }
539
539
  catch (error) {
@@ -634,10 +634,10 @@ class _RemoteToolkitWrapper {
634
634
  return;
635
635
  for await (const chunk of output.stream) {
636
636
  this.toolkit.validateOutputContent(tool, chunk);
637
- if (!await this._sendToolCallResponseChunk(messageId, toolCallId, chunk))
637
+ if (!this._sendToolCallResponseChunk(messageId, toolCallId, chunk))
638
638
  return;
639
639
  }
640
- await this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ControlContent({ method: "close" }));
640
+ this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ControlContent({ method: "close" }));
641
641
  return;
642
642
  }
643
643
  throw new Error(`tool ${toolName} returned unsupported output`);
@@ -648,9 +648,9 @@ class _RemoteToolkitWrapper {
648
648
  return;
649
649
  }
650
650
  if (!(error instanceof InvalidToolDataException)) {
651
- await this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ErrorContent({ text: String(error) }));
651
+ this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ErrorContent({ text: String(error) }));
652
652
  }
653
- await this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ControlContent({
653
+ this._sendToolCallResponseChunk(messageId, toolCallId, new response_js_1.ControlContent({
654
654
  method: "close",
655
655
  statusCode: error instanceof InvalidToolDataException ? response_js_1.ControlCloseStatus.INVALID_DATA : undefined,
656
656
  message: error instanceof InvalidToolDataException ? error.message : undefined,
@@ -1,9 +1,10 @@
1
1
  import { ApiScope } from "./participant-token.js";
2
2
  export type ProjectRole = "member" | "admin" | "developer" | "room_creator" | "room_inventory" | "room_manager" | "session_inventory" | "agent_creator" | "agent_inventory" | "agent_manager" | "repository_creator" | "repository_inventory" | "repository_manager" | "feed_creator" | "feed_inventory" | "feed_manager" | "oauth_client_creator" | "oauth_client_inventory" | "oauth_client_manager" | "api_key_creator" | "api_key_inventory" | "api_key_manager" | "service_creator" | "service_inventory" | "service_manager" | "service_account_creator" | "service_account_inventory" | "service_account_manager" | "participant_token_creator" | "mailbox_creator" | "mailbox_inventory" | "mailbox_manager" | "route_creator" | "route_inventory" | "route_manager" | "scheduled_task_creator" | "scheduled_task_inventory" | "scheduled_task_manager" | "feed_subscription_creator" | "feed_subscription_inventory" | "feed_subscription_manager" | "llm_logger_creator" | "llm_logger_inventory" | "llm_logger_manager" | "llm_proxy_user" | "usage_reporter" | "billing_manager" | "group_manager";
3
3
  export type ResourceRole = "viewer" | "operator" | "developer" | "admin";
4
+ export type RoomRole = "site_user" | ResourceRole;
4
5
  export type FeedRole = "reader" | "subscriber" | "publisher" | "manager";
5
6
  export type SecretRole = "use_proxy";
6
- export type AccessRole = ProjectRole | ResourceRole | FeedRole | SecretRole | "list";
7
+ export type AccessRole = ProjectRole | RoomRole | FeedRole | SecretRole | "list";
7
8
  export type AccessSubjectType = "user" | "group" | "agent" | "service_account" | "userset";
8
9
  export type AccessResourceType = "project" | "room" | "agent" | "group" | "repository" | "feed" | "secret";
9
10
  export interface AccessSubject {
@@ -29,6 +30,12 @@ export interface RoomConnectionInfo {
29
30
  projectId: string;
30
31
  roomUrl: string;
31
32
  }
33
+ export interface AgentConnectionInfo {
34
+ jwt: string;
35
+ agentName: string;
36
+ projectId: string;
37
+ agentUrl: string;
38
+ }
32
39
  export declare class RoomSession {
33
40
  readonly id: string;
34
41
  readonly roomId?: string | null;
@@ -559,6 +566,7 @@ export declare class Meshagent {
559
566
  private parseTransaction;
560
567
  private parseOAuthClient;
561
568
  private parseRoomConnectionInfo;
569
+ private parseAgentConnectionInfo;
562
570
  private encodePathComponent;
563
571
  upload({ projectId, path, data }: {
564
572
  projectId: string;
@@ -908,6 +916,7 @@ export declare class Meshagent {
908
916
  subjectId: string;
909
917
  }): Promise<void>;
910
918
  connectRoom(projectId: string, room: string): Promise<RoomConnectionInfo>;
919
+ connectAgent(projectId: string, agentName: string): Promise<AgentConnectionInfo>;
911
920
  listRooms(projectId: string, options?: {
912
921
  pageSize?: number;
913
922
  continuationToken?: string;
@@ -659,6 +659,7 @@ class Meshagent {
659
659
  const parsedSubject = this.parseAccessSubject(data.subject);
660
660
  const parsedDirectRoles = Array.isArray(data.direct_roles)
661
661
  ? data.direct_roles.filter((item) => item === "member" ||
662
+ item === "site_user" ||
662
663
  item === "viewer" ||
663
664
  item === "operator" ||
664
665
  item === "developer" ||
@@ -822,6 +823,25 @@ class Meshagent {
822
823
  }
823
824
  return { jwt, roomName: roomNameValue, projectId: projectIdValue, roomUrl: roomUrlValue };
824
825
  }
826
+ parseAgentConnectionInfo(data) {
827
+ if (!data || typeof data !== "object") {
828
+ throw new requirement_js_1.RoomException("Invalid agent connection payload");
829
+ }
830
+ const { jwt, agent_name: agentNameRaw, agentName, project_id: projectIdRaw, projectId, agent_url: agentUrlRaw, agentUrl } = data;
831
+ const agentNameValue = typeof agentName === "string" ? agentName : agentNameRaw;
832
+ const projectIdValue = typeof projectId === "string" ? projectId : projectIdRaw;
833
+ const agentUrlValue = typeof agentUrl === "string" ? agentUrl : agentUrlRaw;
834
+ if (typeof jwt !== "string" || typeof agentNameValue !== "string" || typeof projectIdValue !== "string" || typeof agentUrlValue !== "string") {
835
+ throw new requirement_js_1.RoomException("Invalid agent connection payload: missing fields");
836
+ }
837
+ const parsedUrl = new URL(agentUrlValue);
838
+ const legacySuffix = `/accounts/projects/${encodeURIComponent(projectIdValue)}/agents/${encodeURIComponent(agentNameValue)}/messages`;
839
+ if (parsedUrl.pathname.endsWith(legacySuffix)) {
840
+ const prefix = parsedUrl.pathname.slice(0, -legacySuffix.length);
841
+ parsedUrl.pathname = `${prefix}/agents/${encodeURIComponent(projectIdValue)}/${encodeURIComponent(agentNameValue)}/messages`;
842
+ }
843
+ return { jwt, agentName: agentNameValue, projectId: projectIdValue, agentUrl: parsedUrl.toString() };
844
+ }
825
845
  encodePathComponent(value) {
826
846
  return encodeURIComponent(value);
827
847
  }
@@ -1847,6 +1867,14 @@ class Meshagent {
1847
1867
  });
1848
1868
  return this.parseRoomConnectionInfo(data);
1849
1869
  }
1870
+ async connectAgent(projectId, agentName) {
1871
+ const data = await this.request(`/accounts/projects/${projectId}/agents/${encodeURIComponent(agentName)}/connect`, {
1872
+ method: "POST",
1873
+ json: {},
1874
+ action: "connect agent",
1875
+ });
1876
+ return this.parseAgentConnectionInfo(data);
1877
+ }
1850
1878
  async listRooms(projectId, options = {}) {
1851
1879
  const { pageSize = 50, continuationToken, filter, view } = options;
1852
1880
  const data = await this.request(`/accounts/projects/${projectId}/rooms`, {
@@ -1,8 +1,72 @@
1
1
  import { EventEmitter } from "./event-emitter.js";
2
2
  import { Participant, RemoteParticipant } from "./participant.js";
3
+ import { Content } from "./response.js";
3
4
  import { RoomClient } from "./room-client.js";
4
- import { RoomMessageEvent } from "./room-event.js";
5
+ import { RoomMessage, RoomMessageEvent } from "./room-event.js";
5
6
  type MessagePayload = Record<string, unknown>;
7
+ export type MessagingStreamEvent = {
8
+ kind: "message";
9
+ message: RoomMessage;
10
+ } | {
11
+ kind: "client_disconnected";
12
+ streamId: string;
13
+ participantId: string;
14
+ } | {
15
+ kind: "closed";
16
+ streamId: string;
17
+ reason: string;
18
+ message?: string;
19
+ };
20
+ declare class MessagingStreamInput {
21
+ private readonly items;
22
+ private signal;
23
+ private active;
24
+ private closed;
25
+ private error;
26
+ enqueue(content: Content, { wait }?: {
27
+ wait?: boolean;
28
+ }): Promise<void>;
29
+ close(error?: unknown): void;
30
+ fail(error: unknown): void;
31
+ stream(): AsyncIterable<Content>;
32
+ }
33
+ export declare class MessagingStream implements AsyncIterable<MessagingStreamEvent> {
34
+ readonly streamId: string;
35
+ readonly remoteParticipantId: string;
36
+ private readonly input;
37
+ private readonly output;
38
+ private readonly inputDone;
39
+ private readonly onClosed;
40
+ private readonly events;
41
+ private eventSignal;
42
+ private state;
43
+ constructor({ streamId, remoteParticipantId, input, output, inputClosed, onClosed, }: {
44
+ streamId: string;
45
+ remoteParticipantId: string;
46
+ input: MessagingStreamInput;
47
+ output: AsyncIterator<Content>;
48
+ inputClosed?: Promise<void>;
49
+ onClosed: () => void;
50
+ });
51
+ get closed(): boolean;
52
+ private push;
53
+ private finish;
54
+ private consumeOutput;
55
+ private content;
56
+ sendMessageNowait(params: {
57
+ type: string;
58
+ message: MessagePayload;
59
+ attachment?: Uint8Array;
60
+ }): void;
61
+ sendMessage(params: {
62
+ type: string;
63
+ message: MessagePayload;
64
+ attachment?: Uint8Array;
65
+ }): Promise<void>;
66
+ close(): Promise<void>;
67
+ clientDisconnected(participantId: string): void;
68
+ [Symbol.asyncIterator](): AsyncIterator<MessagingStreamEvent>;
69
+ }
6
70
  export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
7
71
  private readonly client;
8
72
  private readonly _messageHandler;
@@ -10,6 +74,8 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
10
74
  private readonly _messageQueue;
11
75
  private _messageQueued;
12
76
  private _sendTask;
77
+ private readonly _sendOperations;
78
+ private readonly _streams;
13
79
  private _messageQueueClosed;
14
80
  private _desiredEnabled;
15
81
  private _online;
@@ -42,6 +108,7 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
42
108
  private _dropQueuedMessage;
43
109
  private _drainQueuedMessages;
44
110
  private _sendMessages;
111
+ private _sendQueuedMessage;
45
112
  sendMessage({ to, type, message, attachment, ignoreOffline, }: {
46
113
  to: Participant;
47
114
  type: string;
@@ -62,6 +129,12 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
62
129
  message: MessagePayload;
63
130
  attachment?: Uint8Array;
64
131
  }): Promise<void>;
132
+ stream({ to, type, message, attachment, }: {
133
+ to: Participant;
134
+ type: string;
135
+ message: MessagePayload;
136
+ attachment?: Uint8Array;
137
+ }): Promise<MessagingStream>;
65
138
  getParticipants(): RemoteParticipant[];
66
139
  getParticipant(id: string): RemoteParticipant | null;
67
140
  getParticipantByName(name: string): RemoteParticipant | null;