@meshagent/meshagent 0.31.2 → 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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.31.3]
2
+ - Stability
3
+
1
4
  ## [0.31.2]
2
5
  - Stability
3
6
 
@@ -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;
@@ -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
  }
@@ -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;
@@ -14,6 +14,8 @@ const database_client_1 = require("./database-client");
14
14
  const agent_client_1 = require("./agent-client");
15
15
  const secrets_client_1 = require("./secrets-client");
16
16
  const containers_client_1 = require("./containers-client");
17
+ const memory_client_1 = require("./memory-client");
18
+ const services_client_1 = require("./services-client");
17
19
  const room_server_client_1 = require("./room-server-client");
18
20
  const response_1 = require("./response");
19
21
  class RoomClient {
@@ -37,6 +39,8 @@ class RoomClient {
37
39
  this.agents = new agent_client_1.AgentsClient({ room: this });
38
40
  this.secrets = new secrets_client_1.SecretsClient({ room: this });
39
41
  this.containers = new containers_client_1.ContainersClient({ room: this });
42
+ this.memory = new memory_client_1.MemoryClient({ room: this });
43
+ this.services = new services_client_1.ServicesClient({ room: this });
40
44
  }
41
45
  get localParticipant() {
42
46
  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,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServicesClient = void 0;
4
+ const response_1 = require("./response");
5
+ const room_server_client_1 = require("./room-server-client");
6
+ function isRecord(value) {
7
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8
+ }
9
+ function toOptionalNumber(value) {
10
+ return typeof value === "number" ? value : undefined;
11
+ }
12
+ function toOptionalInteger(value) {
13
+ return typeof value === "number" && Number.isInteger(value) ? value : undefined;
14
+ }
15
+ function parseServiceRuntimeState(value) {
16
+ if (!isRecord(value) || typeof value["service_id"] !== "string") {
17
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
18
+ }
19
+ const state = value["state"];
20
+ const containerId = value["container_id"];
21
+ return {
22
+ serviceId: value["service_id"],
23
+ state: typeof state === "string" ? state : "unknown",
24
+ containerId: typeof containerId === "string" ? containerId : undefined,
25
+ restartScheduledAt: toOptionalNumber(value["restart_scheduled_at"]),
26
+ startedAt: toOptionalNumber(value["started_at"]),
27
+ restartCount: toOptionalInteger(value["restart_count"]) ?? 0,
28
+ lastExitCode: toOptionalInteger(value["last_exit_code"]),
29
+ lastExitAt: toOptionalNumber(value["last_exit_at"]),
30
+ };
31
+ }
32
+ function parseServiceSpecJson(value) {
33
+ if (typeof value === "string") {
34
+ const parsed = JSON.parse(value);
35
+ if (!isRecord(parsed)) {
36
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
37
+ }
38
+ return parsed;
39
+ }
40
+ if (!isRecord(value)) {
41
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
42
+ }
43
+ return value;
44
+ }
45
+ class ServicesClient {
46
+ constructor({ room }) {
47
+ this.room = room;
48
+ }
49
+ unexpectedResponse(operation) {
50
+ return new room_server_client_1.RoomServerException(`unexpected return type from services.${operation}`);
51
+ }
52
+ async list() {
53
+ return (await this.listWithState()).services;
54
+ }
55
+ async listWithState() {
56
+ const response = await this.room.invoke({
57
+ toolkit: "services",
58
+ tool: "list",
59
+ input: {},
60
+ });
61
+ if (!(response instanceof response_1.JsonContent)) {
62
+ throw this.unexpectedResponse("list");
63
+ }
64
+ const servicesRaw = response.json["services_json"];
65
+ const serviceStatesRaw = response.json["service_states"];
66
+ if (!Array.isArray(servicesRaw) || !Array.isArray(serviceStatesRaw)) {
67
+ throw this.unexpectedResponse("list");
68
+ }
69
+ const serviceStates = {};
70
+ for (const item of serviceStatesRaw) {
71
+ const state = parseServiceRuntimeState(item);
72
+ serviceStates[state.serviceId] = state;
73
+ }
74
+ return {
75
+ services: servicesRaw.map((item) => parseServiceSpecJson(item)),
76
+ serviceStates,
77
+ };
78
+ }
79
+ async restart(params) {
80
+ const response = await this.room.invoke({
81
+ toolkit: "services",
82
+ tool: "restart",
83
+ input: {
84
+ service_id: params.serviceId,
85
+ },
86
+ });
87
+ if (response instanceof response_1.EmptyContent || response instanceof response_1.JsonContent) {
88
+ return;
89
+ }
90
+ throw this.unexpectedResponse("restart");
91
+ }
92
+ }
93
+ exports.ServicesClient = ServicesClient;
@@ -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';
package/dist/esm/index.js CHANGED
@@ -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';
@@ -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,52 @@
1
+ import { EmptyContent, JsonContent } from "./response";
2
+ import { RoomServerException } from "./room-server-client";
3
+ export class MemoryClient {
4
+ constructor({ room }) {
5
+ this.room = room;
6
+ }
7
+ unexpectedResponse(operation) {
8
+ return new RoomServerException(`unexpected return type from memory.${operation}`);
9
+ }
10
+ async invoke(operation, input) {
11
+ const response = await this.room.invoke({
12
+ toolkit: "memory",
13
+ tool: operation,
14
+ input,
15
+ });
16
+ if (response instanceof JsonContent) {
17
+ return response;
18
+ }
19
+ if (response instanceof EmptyContent) {
20
+ return null;
21
+ }
22
+ throw this.unexpectedResponse(operation);
23
+ }
24
+ async list(params) {
25
+ const response = await this.invoke("list", {
26
+ namespace: params?.namespace ?? null,
27
+ });
28
+ if (!(response instanceof JsonContent)) {
29
+ throw this.unexpectedResponse("list");
30
+ }
31
+ const memories = response.json["memories"];
32
+ if (!Array.isArray(memories)) {
33
+ return [];
34
+ }
35
+ return memories.filter((value) => typeof value === "string");
36
+ }
37
+ async create(params) {
38
+ await this.invoke("create", {
39
+ name: params.name,
40
+ namespace: params.namespace ?? null,
41
+ overwrite: params.overwrite ?? false,
42
+ ignore_exists: params.ignoreExists ?? false,
43
+ });
44
+ }
45
+ async drop(params) {
46
+ await this.invoke("drop", {
47
+ name: params.name,
48
+ namespace: params.namespace ?? null,
49
+ ignore_missing: params.ignoreMissing ?? false,
50
+ });
51
+ }
52
+ }
@@ -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
  }
@@ -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;
@@ -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
  }
@@ -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;
@@ -14,6 +14,8 @@ const database_client_1 = require("./database-client");
14
14
  const agent_client_1 = require("./agent-client");
15
15
  const secrets_client_1 = require("./secrets-client");
16
16
  const containers_client_1 = require("./containers-client");
17
+ const memory_client_1 = require("./memory-client");
18
+ const services_client_1 = require("./services-client");
17
19
  const room_server_client_1 = require("./room-server-client");
18
20
  const response_1 = require("./response");
19
21
  class RoomClient {
@@ -37,6 +39,8 @@ class RoomClient {
37
39
  this.agents = new agent_client_1.AgentsClient({ room: this });
38
40
  this.secrets = new secrets_client_1.SecretsClient({ room: this });
39
41
  this.containers = new containers_client_1.ContainersClient({ room: this });
42
+ this.memory = new memory_client_1.MemoryClient({ room: this });
43
+ this.services = new services_client_1.ServicesClient({ room: this });
40
44
  }
41
45
  get localParticipant() {
42
46
  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,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServicesClient = void 0;
4
+ const response_1 = require("./response");
5
+ const room_server_client_1 = require("./room-server-client");
6
+ function isRecord(value) {
7
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8
+ }
9
+ function toOptionalNumber(value) {
10
+ return typeof value === "number" ? value : undefined;
11
+ }
12
+ function toOptionalInteger(value) {
13
+ return typeof value === "number" && Number.isInteger(value) ? value : undefined;
14
+ }
15
+ function parseServiceRuntimeState(value) {
16
+ if (!isRecord(value) || typeof value["service_id"] !== "string") {
17
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
18
+ }
19
+ const state = value["state"];
20
+ const containerId = value["container_id"];
21
+ return {
22
+ serviceId: value["service_id"],
23
+ state: typeof state === "string" ? state : "unknown",
24
+ containerId: typeof containerId === "string" ? containerId : undefined,
25
+ restartScheduledAt: toOptionalNumber(value["restart_scheduled_at"]),
26
+ startedAt: toOptionalNumber(value["started_at"]),
27
+ restartCount: toOptionalInteger(value["restart_count"]) ?? 0,
28
+ lastExitCode: toOptionalInteger(value["last_exit_code"]),
29
+ lastExitAt: toOptionalNumber(value["last_exit_at"]),
30
+ };
31
+ }
32
+ function parseServiceSpecJson(value) {
33
+ if (typeof value === "string") {
34
+ const parsed = JSON.parse(value);
35
+ if (!isRecord(parsed)) {
36
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
37
+ }
38
+ return parsed;
39
+ }
40
+ if (!isRecord(value)) {
41
+ throw new room_server_client_1.RoomServerException("unexpected return type from services.list");
42
+ }
43
+ return value;
44
+ }
45
+ class ServicesClient {
46
+ constructor({ room }) {
47
+ this.room = room;
48
+ }
49
+ unexpectedResponse(operation) {
50
+ return new room_server_client_1.RoomServerException(`unexpected return type from services.${operation}`);
51
+ }
52
+ async list() {
53
+ return (await this.listWithState()).services;
54
+ }
55
+ async listWithState() {
56
+ const response = await this.room.invoke({
57
+ toolkit: "services",
58
+ tool: "list",
59
+ input: {},
60
+ });
61
+ if (!(response instanceof response_1.JsonContent)) {
62
+ throw this.unexpectedResponse("list");
63
+ }
64
+ const servicesRaw = response.json["services_json"];
65
+ const serviceStatesRaw = response.json["service_states"];
66
+ if (!Array.isArray(servicesRaw) || !Array.isArray(serviceStatesRaw)) {
67
+ throw this.unexpectedResponse("list");
68
+ }
69
+ const serviceStates = {};
70
+ for (const item of serviceStatesRaw) {
71
+ const state = parseServiceRuntimeState(item);
72
+ serviceStates[state.serviceId] = state;
73
+ }
74
+ return {
75
+ services: servicesRaw.map((item) => parseServiceSpecJson(item)),
76
+ serviceStates,
77
+ };
78
+ }
79
+ async restart(params) {
80
+ const response = await this.room.invoke({
81
+ toolkit: "services",
82
+ tool: "restart",
83
+ input: {
84
+ service_id: params.serviceId,
85
+ },
86
+ });
87
+ if (response instanceof response_1.EmptyContent || response instanceof response_1.JsonContent) {
88
+ return;
89
+ }
90
+ throw this.unexpectedResponse("restart");
91
+ }
92
+ }
93
+ exports.ServicesClient = ServicesClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent",
3
- "version": "0.31.2",
3
+ "version": "0.31.3",
4
4
  "description": "Meshagent Client",
5
5
  "homepage": "https://github.com/meshagent/meshagent-ts",
6
6
  "scripts": {