@meshagent/meshagent 0.0.29 → 0.0.31

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 CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.0.31]
2
+ - Stability
3
+
4
+ ## [0.0.31]
5
+ - Stability
6
+
1
7
  ## [0.0.29]
2
8
  - Stability
3
9
 
@@ -148,8 +148,7 @@ class RemoteToolkit extends Toolkit {
148
148
  }
149
149
  async _toolCall(protocol, messageId, type, data) {
150
150
  try {
151
- const raw = utils_1.decoder.decode(data);
152
- const message = JSON.parse(raw);
151
+ const [message, _] = (0, utils_1.unpackMessage)(data);
153
152
  const toolName = message["name"];
154
153
  const args = message["arguments"];
155
154
  const response = await this.execute(toolName, args);
@@ -203,8 +202,7 @@ class RemoteTaskRunner {
203
202
  async _ask(protocol, messageId, msgType, data) {
204
203
  console.info("_ask handler invoked with data", data);
205
204
  try {
206
- const raw = utils_1.decoder.decode(data);
207
- const message = JSON.parse(raw);
205
+ const [message, _] = (0, utils_1.unpackMessage)(data);
208
206
  console.info("got message", message);
209
207
  const jwt = message["jwt"];
210
208
  const args = message["arguments"];
@@ -214,15 +212,15 @@ class RemoteTaskRunner {
214
212
  const chat = AgentChatContext.fromJson(context_json);
215
213
  const callContext = new AgentCallContext({ chat, jwt, api_url });
216
214
  const answer = await this.ask(callContext, args);
217
- const encoded = utils_1.encoder.encode(JSON.stringify({ task_id, answer }));
215
+ const encoded = (0, utils_1.packMessage)({ task_id, answer });
218
216
  await protocol.send("agent.ask_response", encoded);
219
217
  }
220
218
  catch (e) {
221
- const rawError = JSON.stringify({
219
+ const rawError = {
222
220
  task_id: "",
223
221
  error: String(e),
224
- });
225
- await protocol.send("agent.ask_response", utils_1.encoder.encode(rawError));
222
+ };
223
+ await protocol.send("agent.ask_response", (0, utils_1.packMessage)(rawError));
226
224
  }
227
225
  }
228
226
  }
@@ -11,7 +11,7 @@ class DeveloperClient extends event_emitter_1.EventEmitter {
11
11
  this.client.protocol.addHandler("developer.log", this._handleDeveloperLog.bind(this));
12
12
  }
13
13
  async _handleDeveloperLog(protocol, messageId, type, bytes) {
14
- const rawJson = JSON.parse(utils_1.decoder.decode(bytes || new Uint8Array()));
14
+ const [rawJson, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
15
15
  const event = new room_event_1.RoomLogEvent({
16
16
  type: rawJson["type"],
17
17
  data: rawJson["data"],
@@ -31,7 +31,7 @@ class LocalParticipant extends Participant {
31
31
  async setAttribute(name, value) {
32
32
  this._attributes[name] = value;
33
33
  try {
34
- const payload = utils_1.encoder.encode(JSON.stringify({ [name]: value }));
34
+ const payload = (0, utils_1.packMessage)({ [name]: value });
35
35
  await this.client.protocol.send("set_attributes", payload);
36
36
  }
37
37
  catch (err) {
@@ -85,7 +85,8 @@ class RoomClient {
85
85
  }
86
86
  }
87
87
  async _handleRoomReady(protocol, messageId, type, data) {
88
- this._ready.complete(JSON.parse(utils_1.decoder.decode(data))["room_name"]);
88
+ const [message, _] = (0, utils_1.unpackMessage)(data);
89
+ this._ready.complete(message["room_name"]);
89
90
  }
90
91
  _onParticipantInit(participantId, attributes) {
91
92
  this._localParticipant = new participant_1.LocalParticipant(this, participantId);
@@ -94,7 +95,7 @@ class RoomClient {
94
95
  }
95
96
  }
96
97
  async _handleParticipant(protocol, messageId, type, data) {
97
- const message = JSON.parse(utils_1.decoder.decode(data));
98
+ const [message, _] = (0, utils_1.unpackMessage)(data);
98
99
  switch (message["type"]) {
99
100
  case "init": this._onParticipantInit(message["participantId"], message["attributes"]);
100
101
  }
@@ -32,15 +32,13 @@ class StorageClient extends event_emitter_1.EventEmitter {
32
32
  this.client.protocol.addHandler("storage.file.updated", this._handleFileUpdated.bind(this));
33
33
  }
34
34
  async _handleFileUpdated(protocol, messageId, type, bytes) {
35
- const raw = utils_1.decoder.decode(bytes || new Uint8Array());
36
- const data = JSON.parse(raw);
35
+ const [data, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
37
36
  const event = new room_event_1.FileUpdatedEvent({ path: data["path"] });
38
37
  this.client.emit(event);
39
38
  this.emit('file.updated', event);
40
39
  }
41
40
  async _handleFileDeleted(protocol, messageId, type, bytes) {
42
- const raw = utils_1.decoder.decode(bytes || new Uint8Array());
43
- const data = JSON.parse(raw);
41
+ const [data, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
44
42
  const event = new room_event_1.FileDeletedEvent({ path: data["path"] });
45
43
  this.client.emit(event);
46
44
  this.emit('file.deleted', event);
@@ -4,6 +4,7 @@ declare const decoder: TextDecoder;
4
4
  export { encoder, decoder };
5
5
  export declare function splitMessagePayload(packet: Uint8Array): Uint8Array;
6
6
  export declare function splitMessageHeader(packet: Uint8Array): string;
7
+ export declare function unpackMessage(packet: Uint8Array): [Record<string, any>, Uint8Array];
7
8
  export declare function packMessage(request: Record<string, any>, data?: Uint8Array): Uint8Array;
8
9
  export declare function mergeUint8Arrays(...arrays: Uint8Array[]): Uint8Array;
9
10
  export declare class RefCount<T> {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RefCount = exports.decoder = exports.encoder = void 0;
4
4
  exports.splitMessagePayload = splitMessagePayload;
5
5
  exports.splitMessageHeader = splitMessageHeader;
6
+ exports.unpackMessage = unpackMessage;
6
7
  exports.packMessage = packMessage;
7
8
  exports.mergeUint8Arrays = mergeUint8Arrays;
8
9
  const text_encoding_1 = require("@kayahr/text-encoding");
@@ -21,6 +22,9 @@ function splitMessageHeader(packet) {
21
22
  const payload = packet.subarray(8, 8 + headerSize);
22
23
  return decoder.decode(payload);
23
24
  }
25
+ function unpackMessage(packet) {
26
+ return [JSON.parse(splitMessageHeader(packet)), splitMessagePayload(packet)];
27
+ }
24
28
  function packMessage(request, data) {
25
29
  const jsonMessage = encoder.encode(JSON.stringify(request));
26
30
  const size = jsonMessage.length;
@@ -148,8 +148,7 @@ class RemoteToolkit extends Toolkit {
148
148
  }
149
149
  async _toolCall(protocol, messageId, type, data) {
150
150
  try {
151
- const raw = utils_1.decoder.decode(data);
152
- const message = JSON.parse(raw);
151
+ const [message, _] = (0, utils_1.unpackMessage)(data);
153
152
  const toolName = message["name"];
154
153
  const args = message["arguments"];
155
154
  const response = await this.execute(toolName, args);
@@ -203,8 +202,7 @@ class RemoteTaskRunner {
203
202
  async _ask(protocol, messageId, msgType, data) {
204
203
  console.info("_ask handler invoked with data", data);
205
204
  try {
206
- const raw = utils_1.decoder.decode(data);
207
- const message = JSON.parse(raw);
205
+ const [message, _] = (0, utils_1.unpackMessage)(data);
208
206
  console.info("got message", message);
209
207
  const jwt = message["jwt"];
210
208
  const args = message["arguments"];
@@ -214,15 +212,15 @@ class RemoteTaskRunner {
214
212
  const chat = AgentChatContext.fromJson(context_json);
215
213
  const callContext = new AgentCallContext({ chat, jwt, api_url });
216
214
  const answer = await this.ask(callContext, args);
217
- const encoded = utils_1.encoder.encode(JSON.stringify({ task_id, answer }));
215
+ const encoded = (0, utils_1.packMessage)({ task_id, answer });
218
216
  await protocol.send("agent.ask_response", encoded);
219
217
  }
220
218
  catch (e) {
221
- const rawError = JSON.stringify({
219
+ const rawError = {
222
220
  task_id: "",
223
221
  error: String(e),
224
- });
225
- await protocol.send("agent.ask_response", utils_1.encoder.encode(rawError));
222
+ };
223
+ await protocol.send("agent.ask_response", (0, utils_1.packMessage)(rawError));
226
224
  }
227
225
  }
228
226
  }
@@ -11,7 +11,7 @@ class DeveloperClient extends event_emitter_1.EventEmitter {
11
11
  this.client.protocol.addHandler("developer.log", this._handleDeveloperLog.bind(this));
12
12
  }
13
13
  async _handleDeveloperLog(protocol, messageId, type, bytes) {
14
- const rawJson = JSON.parse(utils_1.decoder.decode(bytes || new Uint8Array()));
14
+ const [rawJson, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
15
15
  const event = new room_event_1.RoomLogEvent({
16
16
  type: rawJson["type"],
17
17
  data: rawJson["data"],
@@ -31,7 +31,7 @@ class LocalParticipant extends Participant {
31
31
  async setAttribute(name, value) {
32
32
  this._attributes[name] = value;
33
33
  try {
34
- const payload = utils_1.encoder.encode(JSON.stringify({ [name]: value }));
34
+ const payload = (0, utils_1.packMessage)({ [name]: value });
35
35
  await this.client.protocol.send("set_attributes", payload);
36
36
  }
37
37
  catch (err) {
@@ -85,7 +85,8 @@ class RoomClient {
85
85
  }
86
86
  }
87
87
  async _handleRoomReady(protocol, messageId, type, data) {
88
- this._ready.complete(JSON.parse(utils_1.decoder.decode(data))["room_name"]);
88
+ const [message, _] = (0, utils_1.unpackMessage)(data);
89
+ this._ready.complete(message["room_name"]);
89
90
  }
90
91
  _onParticipantInit(participantId, attributes) {
91
92
  this._localParticipant = new participant_1.LocalParticipant(this, participantId);
@@ -94,7 +95,7 @@ class RoomClient {
94
95
  }
95
96
  }
96
97
  async _handleParticipant(protocol, messageId, type, data) {
97
- const message = JSON.parse(utils_1.decoder.decode(data));
98
+ const [message, _] = (0, utils_1.unpackMessage)(data);
98
99
  switch (message["type"]) {
99
100
  case "init": this._onParticipantInit(message["participantId"], message["attributes"]);
100
101
  }
@@ -32,15 +32,13 @@ class StorageClient extends event_emitter_1.EventEmitter {
32
32
  this.client.protocol.addHandler("storage.file.updated", this._handleFileUpdated.bind(this));
33
33
  }
34
34
  async _handleFileUpdated(protocol, messageId, type, bytes) {
35
- const raw = utils_1.decoder.decode(bytes || new Uint8Array());
36
- const data = JSON.parse(raw);
35
+ const [data, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
37
36
  const event = new room_event_1.FileUpdatedEvent({ path: data["path"] });
38
37
  this.client.emit(event);
39
38
  this.emit('file.updated', event);
40
39
  }
41
40
  async _handleFileDeleted(protocol, messageId, type, bytes) {
42
- const raw = utils_1.decoder.decode(bytes || new Uint8Array());
43
- const data = JSON.parse(raw);
41
+ const [data, _] = (0, utils_1.unpackMessage)(bytes || new Uint8Array());
44
42
  const event = new room_event_1.FileDeletedEvent({ path: data["path"] });
45
43
  this.client.emit(event);
46
44
  this.emit('file.deleted', event);
@@ -4,6 +4,7 @@ declare const decoder: TextDecoder;
4
4
  export { encoder, decoder };
5
5
  export declare function splitMessagePayload(packet: Uint8Array): Uint8Array;
6
6
  export declare function splitMessageHeader(packet: Uint8Array): string;
7
+ export declare function unpackMessage(packet: Uint8Array): [Record<string, any>, Uint8Array];
7
8
  export declare function packMessage(request: Record<string, any>, data?: Uint8Array): Uint8Array;
8
9
  export declare function mergeUint8Arrays(...arrays: Uint8Array[]): Uint8Array;
9
10
  export declare class RefCount<T> {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RefCount = exports.decoder = exports.encoder = void 0;
4
4
  exports.splitMessagePayload = splitMessagePayload;
5
5
  exports.splitMessageHeader = splitMessageHeader;
6
+ exports.unpackMessage = unpackMessage;
6
7
  exports.packMessage = packMessage;
7
8
  exports.mergeUint8Arrays = mergeUint8Arrays;
8
9
  const text_encoding_1 = require("@kayahr/text-encoding");
@@ -21,6 +22,9 @@ function splitMessageHeader(packet) {
21
22
  const payload = packet.subarray(8, 8 + headerSize);
22
23
  return decoder.decode(payload);
23
24
  }
25
+ function unpackMessage(packet) {
26
+ return [JSON.parse(splitMessageHeader(packet)), splitMessagePayload(packet)];
27
+ }
24
28
  function packMessage(request, data) {
25
29
  const jsonMessage = encoder.encode(JSON.stringify(request));
26
30
  const size = jsonMessage.length;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "Meshagent Client",
5
5
  "homepage": "https://www.meshagent.com",
6
6
  "scripts": {