livekit-server-sdk 2.8.1 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +1 -1
  2. package/dist/AccessToken.cjs +169 -0
  3. package/dist/AccessToken.cjs.map +1 -0
  4. package/dist/AccessToken.d.ts.map +1 -1
  5. package/dist/AccessToken.js +130 -137
  6. package/dist/AccessToken.js.map +1 -1
  7. package/dist/AccessToken.test.cjs +147 -0
  8. package/dist/AccessToken.test.cjs.map +1 -0
  9. package/dist/AccessToken.test.js +129 -0
  10. package/dist/AccessToken.test.js.map +1 -0
  11. package/dist/AgentDispatchClient.cjs +133 -0
  12. package/dist/AgentDispatchClient.cjs.map +1 -0
  13. package/dist/AgentDispatchClient.js +102 -79
  14. package/dist/AgentDispatchClient.js.map +1 -1
  15. package/dist/EgressClient.cjs +381 -0
  16. package/dist/EgressClient.cjs.map +1 -0
  17. package/dist/EgressClient.js +349 -288
  18. package/dist/EgressClient.js.map +1 -1
  19. package/dist/IngressClient.cjs +164 -0
  20. package/dist/IngressClient.cjs.map +1 -0
  21. package/dist/IngressClient.js +131 -106
  22. package/dist/IngressClient.js.map +1 -1
  23. package/dist/RoomServiceClient.cjs +240 -0
  24. package/dist/RoomServiceClient.cjs.map +1 -0
  25. package/dist/RoomServiceClient.js +219 -151
  26. package/dist/RoomServiceClient.js.map +1 -1
  27. package/dist/ServiceBase.cjs +49 -0
  28. package/dist/ServiceBase.cjs.map +1 -0
  29. package/dist/ServiceBase.js +25 -28
  30. package/dist/ServiceBase.js.map +1 -1
  31. package/dist/SipClient.cjs +379 -0
  32. package/dist/SipClient.cjs.map +1 -0
  33. package/dist/SipClient.d.ts +15 -0
  34. package/dist/SipClient.d.ts.map +1 -1
  35. package/dist/SipClient.js +360 -260
  36. package/dist/SipClient.js.map +1 -1
  37. package/dist/TwirpRPC.cjs +71 -0
  38. package/dist/TwirpRPC.cjs.map +1 -0
  39. package/dist/TwirpRPC.d.ts.map +1 -1
  40. package/dist/TwirpRPC.js +32 -29
  41. package/dist/TwirpRPC.js.map +1 -1
  42. package/dist/WebhookReceiver.cjs +78 -0
  43. package/dist/WebhookReceiver.cjs.map +1 -0
  44. package/dist/WebhookReceiver.js +48 -45
  45. package/dist/WebhookReceiver.js.map +1 -1
  46. package/dist/WebhookReceiver.test.cjs +38 -0
  47. package/dist/WebhookReceiver.test.cjs.map +1 -0
  48. package/dist/WebhookReceiver.test.js +37 -0
  49. package/dist/WebhookReceiver.test.js.map +1 -0
  50. package/dist/grants.cjs +53 -0
  51. package/dist/grants.cjs.map +1 -0
  52. package/dist/grants.js +25 -23
  53. package/dist/grants.js.map +1 -1
  54. package/dist/grants.test.cjs +27 -0
  55. package/dist/grants.test.cjs.map +1 -0
  56. package/dist/grants.test.js +26 -0
  57. package/dist/grants.test.js.map +1 -0
  58. package/dist/index.cjs +136 -0
  59. package/dist/index.cjs.map +1 -0
  60. package/dist/index.js +104 -12
  61. package/dist/index.js.map +1 -1
  62. package/package.json +12 -3
  63. package/src/AccessToken.ts +3 -1
  64. package/src/SipClient.ts +33 -0
  65. package/src/TwirpRPC.ts +2 -2
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var import_protocol = require("@livekit/protocol");
25
+ var jose = __toESM(require("jose"), 1);
26
+ var import_vitest = require("vitest");
27
+ var import_AccessToken = require("./AccessToken");
28
+ const testApiKey = "abcdefg";
29
+ const testSecret = "abababa";
30
+ (0, import_vitest.describe)("encoded tokens are valid", () => {
31
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret, {
32
+ identity: "me",
33
+ name: "myname"
34
+ });
35
+ t.addGrant({ room: "myroom" });
36
+ const EncodedTestSecret = new TextEncoder().encode(testSecret);
37
+ (0, import_vitest.it)("can be decoded", async () => {
38
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
39
+ issuer: testApiKey
40
+ });
41
+ (0, import_vitest.expect)(payload).not.toBe(void 0);
42
+ });
43
+ (0, import_vitest.it)("has name set", async () => {
44
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
45
+ issuer: testApiKey
46
+ });
47
+ (0, import_vitest.expect)(payload.name).toBe("myname");
48
+ });
49
+ (0, import_vitest.it)("has video grants set", async () => {
50
+ var _a;
51
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
52
+ issuer: testApiKey
53
+ });
54
+ (0, import_vitest.expect)(payload.video).toBeTruthy();
55
+ (0, import_vitest.expect)((_a = payload.video) == null ? void 0 : _a.room).toEqual("myroom");
56
+ });
57
+ });
58
+ (0, import_vitest.describe)("identity is required for only join grants", () => {
59
+ (0, import_vitest.it)("allows empty identity for create", async () => {
60
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret);
61
+ t.addGrant({ roomCreate: true });
62
+ (0, import_vitest.expect)(await t.toJwt()).toBeTruthy();
63
+ });
64
+ (0, import_vitest.it)("throws error when identity is not provided for join", async () => {
65
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret);
66
+ t.addGrant({ roomJoin: true });
67
+ await (0, import_vitest.expect)(async () => {
68
+ await t.toJwt();
69
+ }).rejects.toThrow();
70
+ });
71
+ });
72
+ (0, import_vitest.describe)("verify token is valid", () => {
73
+ (0, import_vitest.it)("can decode encoded token", async () => {
74
+ var _a;
75
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret);
76
+ t.sha256 = "abcdefg";
77
+ t.kind = "agent";
78
+ t.addGrant({ roomCreate: true });
79
+ t.attributes = { foo: "bar", live: "kit" };
80
+ const v = new import_AccessToken.TokenVerifier(testApiKey, testSecret);
81
+ const decoded = await v.verify(await t.toJwt());
82
+ (0, import_vitest.expect)(decoded).not.toBe(void 0);
83
+ (0, import_vitest.expect)(decoded.sha256).toEqual("abcdefg");
84
+ (0, import_vitest.expect)((_a = decoded.video) == null ? void 0 : _a.roomCreate).toBeTruthy();
85
+ (0, import_vitest.expect)(decoded.kind).toEqual("agent");
86
+ (0, import_vitest.expect)(decoded.attributes).toEqual(t.attributes);
87
+ });
88
+ });
89
+ (0, import_vitest.describe)("adding grants should not overwrite existing grants", () => {
90
+ const EncodedTestSecret = new TextEncoder().encode(testSecret);
91
+ (0, import_vitest.it)("should not overwrite existing grants", async () => {
92
+ var _a, _b;
93
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret, {
94
+ identity: "me",
95
+ name: "myname"
96
+ });
97
+ t.addGrant({ roomCreate: true });
98
+ t.addGrant({ roomJoin: true });
99
+ const { payload } = await jose.jwtVerify(
100
+ await t.toJwt(),
101
+ EncodedTestSecret,
102
+ { issuer: testApiKey }
103
+ );
104
+ (0, import_vitest.expect)((_a = payload.video) == null ? void 0 : _a.roomCreate).toBeTruthy();
105
+ (0, import_vitest.expect)((_b = payload.video) == null ? void 0 : _b.roomJoin).toBeTruthy();
106
+ });
107
+ });
108
+ (0, import_vitest.describe)("room configuration with agents and egress", () => {
109
+ (0, import_vitest.it)("should set agents and egress in room configuration", async () => {
110
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
111
+ const t = new import_AccessToken.AccessToken(testApiKey, testSecret, {
112
+ identity: "test-identity"
113
+ });
114
+ const roomConfig = new import_protocol.RoomConfiguration({
115
+ name: "test-room",
116
+ maxParticipants: 10
117
+ });
118
+ const agents = [
119
+ new import_protocol.RoomAgentDispatch({
120
+ agentName: "agent1",
121
+ metadata: "metadata-1"
122
+ }),
123
+ new import_protocol.RoomAgentDispatch({
124
+ agentName: "agent2",
125
+ metadata: "metadata-2"
126
+ })
127
+ ];
128
+ const egress = new import_protocol.RoomEgress({
129
+ room: new import_protocol.RoomCompositeEgressRequest({ roomName: "test-room" })
130
+ });
131
+ roomConfig.agents = agents;
132
+ roomConfig.egress = egress;
133
+ t.roomConfig = roomConfig;
134
+ const v = new import_AccessToken.TokenVerifier(testApiKey, testSecret);
135
+ const decoded = await v.verify(await t.toJwt());
136
+ (0, import_vitest.expect)(decoded.roomConfig).toBeDefined();
137
+ (0, import_vitest.expect)((_a = decoded.roomConfig) == null ? void 0 : _a.name).toEqual("test-room");
138
+ (0, import_vitest.expect)((_b = decoded.roomConfig) == null ? void 0 : _b.maxParticipants).toEqual(10);
139
+ (0, import_vitest.expect)((_c = decoded.roomConfig) == null ? void 0 : _c.agents).toHaveLength(2);
140
+ (0, import_vitest.expect)((_e = (_d = decoded.roomConfig) == null ? void 0 : _d.agents) == null ? void 0 : _e[0].agentName).toEqual("agent1");
141
+ (0, import_vitest.expect)((_g = (_f = decoded.roomConfig) == null ? void 0 : _f.agents) == null ? void 0 : _g[0].metadata).toEqual("metadata-1");
142
+ (0, import_vitest.expect)((_i = (_h = decoded.roomConfig) == null ? void 0 : _h.agents) == null ? void 0 : _i[1].agentName).toEqual("agent2");
143
+ (0, import_vitest.expect)((_k = (_j = decoded.roomConfig) == null ? void 0 : _j.agents) == null ? void 0 : _k[1].metadata).toEqual("metadata-2");
144
+ (0, import_vitest.expect)((_n = (_m = (_l = decoded.roomConfig) == null ? void 0 : _l.egress) == null ? void 0 : _m.room) == null ? void 0 : _n.roomName).toEqual("test-room");
145
+ });
146
+ });
147
+ //# sourceMappingURL=AccessToken.test.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AccessToken.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n RoomAgentDispatch,\n RoomCompositeEgressRequest,\n RoomConfiguration,\n RoomEgress,\n} from '@livekit/protocol';\nimport * as jose from 'jose';\nimport { describe, expect, it } from 'vitest';\nimport { AccessToken, TokenVerifier } from './AccessToken';\nimport type { ClaimGrants } from './grants';\n\nconst testApiKey = 'abcdefg';\nconst testSecret = 'abababa';\n\ndescribe('encoded tokens are valid', () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'me',\n name: 'myname',\n });\n t.addGrant({ room: 'myroom' });\n const EncodedTestSecret = new TextEncoder().encode(testSecret);\n it('can be decoded', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload).not.toBe(undefined);\n });\n\n it('has name set', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload.name).toBe('myname');\n });\n\n it('has video grants set', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload.video).toBeTruthy();\n expect((payload as ClaimGrants).video?.room).toEqual('myroom');\n });\n});\n\ndescribe('identity is required for only join grants', () => {\n it('allows empty identity for create', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.addGrant({ roomCreate: true });\n\n expect(await t.toJwt()).toBeTruthy();\n });\n it('throws error when identity is not provided for join', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.addGrant({ roomJoin: true });\n\n await expect(async () => {\n await t.toJwt();\n }).rejects.toThrow();\n });\n});\n\ndescribe('verify token is valid', () => {\n it('can decode encoded token', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.sha256 = 'abcdefg';\n t.kind = 'agent';\n t.addGrant({ roomCreate: true });\n t.attributes = { foo: 'bar', live: 'kit' };\n\n const v = new TokenVerifier(testApiKey, testSecret);\n const decoded = await v.verify(await t.toJwt());\n\n expect(decoded).not.toBe(undefined);\n expect(decoded.sha256).toEqual('abcdefg');\n expect(decoded.video?.roomCreate).toBeTruthy();\n expect(decoded.kind).toEqual('agent');\n expect(decoded.attributes).toEqual(t.attributes);\n });\n});\n\ndescribe('adding grants should not overwrite existing grants', () => {\n const EncodedTestSecret = new TextEncoder().encode(testSecret);\n\n it('should not overwrite existing grants', async () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'me',\n name: 'myname',\n });\n t.addGrant({ roomCreate: true });\n t.addGrant({ roomJoin: true });\n\n const { payload }: jose.JWTVerifyResult<ClaimGrants> = await jose.jwtVerify(\n await t.toJwt(),\n EncodedTestSecret,\n { issuer: testApiKey },\n );\n expect(payload.video?.roomCreate).toBeTruthy();\n expect(payload.video?.roomJoin).toBeTruthy();\n });\n});\n\ndescribe('room configuration with agents and egress', () => {\n it('should set agents and egress in room configuration', async () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'test-identity',\n });\n\n const roomConfig = new RoomConfiguration({\n name: 'test-room',\n maxParticipants: 10,\n });\n\n const agents: RoomAgentDispatch[] = [\n new RoomAgentDispatch({\n agentName: 'agent1',\n metadata: 'metadata-1',\n }),\n new RoomAgentDispatch({\n agentName: 'agent2',\n metadata: 'metadata-2',\n }),\n ];\n\n const egress = new RoomEgress({\n room: new RoomCompositeEgressRequest({ roomName: 'test-room' }),\n });\n\n roomConfig.agents = agents;\n roomConfig.egress = egress;\n\n t.roomConfig = roomConfig;\n\n const v = new TokenVerifier(testApiKey, testSecret);\n const decoded = await v.verify(await t.toJwt());\n\n expect(decoded.roomConfig).toBeDefined();\n expect(decoded.roomConfig?.name).toEqual('test-room');\n expect(decoded.roomConfig?.maxParticipants).toEqual(10);\n expect(decoded.roomConfig?.agents).toHaveLength(2);\n expect(decoded.roomConfig?.agents?.[0].agentName).toEqual('agent1');\n expect(decoded.roomConfig?.agents?.[0].metadata).toEqual('metadata-1');\n expect(decoded.roomConfig?.agents?.[1].agentName).toEqual('agent2');\n expect(decoded.roomConfig?.agents?.[1].metadata).toEqual('metadata-2');\n expect(decoded.roomConfig?.egress?.room?.roomName).toEqual('test-room');\n });\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA,sBAKO;AACP,WAAsB;AACtB,oBAAqC;AACrC,yBAA2C;AAG3C,MAAM,aAAa;AACnB,MAAM,aAAa;AAAA,IAEnB,wBAAS,4BAA4B,MAAM;AACzC,QAAM,IAAI,IAAI,+BAAY,YAAY,YAAY;AAAA,IAChD,UAAU;AAAA,IACV,MAAM;AAAA,EACR,CAAC;AACD,IAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC7B,QAAM,oBAAoB,IAAI,YAAY,EAAE,OAAO,UAAU;AAC7D,wBAAG,kBAAkB,YAAY;AAC/B,UAAM,EAAE,QAAQ,IAAI,MAAM,KAAK,UAAU,MAAM,EAAE,MAAM,GAAG,mBAAmB;AAAA,MAC3E,QAAQ;AAAA,IACV,CAAC;AAED,8BAAO,OAAO,EAAE,IAAI,KAAK,MAAS;AAAA,EACpC,CAAC;AAED,wBAAG,gBAAgB,YAAY;AAC7B,UAAM,EAAE,QAAQ,IAAI,MAAM,KAAK,UAAU,MAAM,EAAE,MAAM,GAAG,mBAAmB;AAAA,MAC3E,QAAQ;AAAA,IACV,CAAC;AAED,8BAAO,QAAQ,IAAI,EAAE,KAAK,QAAQ;AAAA,EACpC,CAAC;AAED,wBAAG,wBAAwB,YAAY;AAxCzC;AAyCI,UAAM,EAAE,QAAQ,IAAI,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AU;AAClD,UAAM,UAAU,MAAM,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC;AAE9C,8BAAO,QAAQ,UAAU,EAAE,YAAY;AACvC,+BAAO,aAAQ,eAAR,mBAAoB,IAAI,EAAE,QAAQ,WAAW;AACpD,+BAAO,aAAQ,eAAR,mBAAoB,eAAe,EAAE,QAAQ,EAAE;AACtD,+BAAO,aAAQ,eAAR,mBAAoB,MAAM,EAAE,aAAa,CAAC;AACjD,+BAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,SAAS,EAAE,QAAQ,QAAQ;AAClE,+BAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,QAAQ,EAAE,QAAQ,YAAY;AACrE,+BAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,SAAS,EAAE,QAAQ,QAAQ;AAClE,+BAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,QAAQ,EAAE,QAAQ,YAAY;AACrE,+BAAO,yBAAQ,eAAR,mBAAoB,WAApB,mBAA4B,SAA5B,mBAAkC,QAAQ,EAAE,QAAQ,WAAW;AAAA,EACxE,CAAC;AACH,CAAC;","names":[]}
@@ -0,0 +1,129 @@
1
+ import {
2
+ RoomAgentDispatch,
3
+ RoomCompositeEgressRequest,
4
+ RoomConfiguration,
5
+ RoomEgress
6
+ } from "@livekit/protocol";
7
+ import * as jose from "jose";
8
+ import { describe, expect, it } from "vitest";
9
+ import { AccessToken, TokenVerifier } from "./AccessToken";
10
+ const testApiKey = "abcdefg";
11
+ const testSecret = "abababa";
12
+ describe("encoded tokens are valid", () => {
13
+ const t = new AccessToken(testApiKey, testSecret, {
14
+ identity: "me",
15
+ name: "myname"
16
+ });
17
+ t.addGrant({ room: "myroom" });
18
+ const EncodedTestSecret = new TextEncoder().encode(testSecret);
19
+ it("can be decoded", async () => {
20
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
21
+ issuer: testApiKey
22
+ });
23
+ expect(payload).not.toBe(void 0);
24
+ });
25
+ it("has name set", async () => {
26
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
27
+ issuer: testApiKey
28
+ });
29
+ expect(payload.name).toBe("myname");
30
+ });
31
+ it("has video grants set", async () => {
32
+ var _a;
33
+ const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {
34
+ issuer: testApiKey
35
+ });
36
+ expect(payload.video).toBeTruthy();
37
+ expect((_a = payload.video) == null ? void 0 : _a.room).toEqual("myroom");
38
+ });
39
+ });
40
+ describe("identity is required for only join grants", () => {
41
+ it("allows empty identity for create", async () => {
42
+ const t = new AccessToken(testApiKey, testSecret);
43
+ t.addGrant({ roomCreate: true });
44
+ expect(await t.toJwt()).toBeTruthy();
45
+ });
46
+ it("throws error when identity is not provided for join", async () => {
47
+ const t = new AccessToken(testApiKey, testSecret);
48
+ t.addGrant({ roomJoin: true });
49
+ await expect(async () => {
50
+ await t.toJwt();
51
+ }).rejects.toThrow();
52
+ });
53
+ });
54
+ describe("verify token is valid", () => {
55
+ it("can decode encoded token", async () => {
56
+ var _a;
57
+ const t = new AccessToken(testApiKey, testSecret);
58
+ t.sha256 = "abcdefg";
59
+ t.kind = "agent";
60
+ t.addGrant({ roomCreate: true });
61
+ t.attributes = { foo: "bar", live: "kit" };
62
+ const v = new TokenVerifier(testApiKey, testSecret);
63
+ const decoded = await v.verify(await t.toJwt());
64
+ expect(decoded).not.toBe(void 0);
65
+ expect(decoded.sha256).toEqual("abcdefg");
66
+ expect((_a = decoded.video) == null ? void 0 : _a.roomCreate).toBeTruthy();
67
+ expect(decoded.kind).toEqual("agent");
68
+ expect(decoded.attributes).toEqual(t.attributes);
69
+ });
70
+ });
71
+ describe("adding grants should not overwrite existing grants", () => {
72
+ const EncodedTestSecret = new TextEncoder().encode(testSecret);
73
+ it("should not overwrite existing grants", async () => {
74
+ var _a, _b;
75
+ const t = new AccessToken(testApiKey, testSecret, {
76
+ identity: "me",
77
+ name: "myname"
78
+ });
79
+ t.addGrant({ roomCreate: true });
80
+ t.addGrant({ roomJoin: true });
81
+ const { payload } = await jose.jwtVerify(
82
+ await t.toJwt(),
83
+ EncodedTestSecret,
84
+ { issuer: testApiKey }
85
+ );
86
+ expect((_a = payload.video) == null ? void 0 : _a.roomCreate).toBeTruthy();
87
+ expect((_b = payload.video) == null ? void 0 : _b.roomJoin).toBeTruthy();
88
+ });
89
+ });
90
+ describe("room configuration with agents and egress", () => {
91
+ it("should set agents and egress in room configuration", async () => {
92
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
93
+ const t = new AccessToken(testApiKey, testSecret, {
94
+ identity: "test-identity"
95
+ });
96
+ const roomConfig = new RoomConfiguration({
97
+ name: "test-room",
98
+ maxParticipants: 10
99
+ });
100
+ const agents = [
101
+ new RoomAgentDispatch({
102
+ agentName: "agent1",
103
+ metadata: "metadata-1"
104
+ }),
105
+ new RoomAgentDispatch({
106
+ agentName: "agent2",
107
+ metadata: "metadata-2"
108
+ })
109
+ ];
110
+ const egress = new RoomEgress({
111
+ room: new RoomCompositeEgressRequest({ roomName: "test-room" })
112
+ });
113
+ roomConfig.agents = agents;
114
+ roomConfig.egress = egress;
115
+ t.roomConfig = roomConfig;
116
+ const v = new TokenVerifier(testApiKey, testSecret);
117
+ const decoded = await v.verify(await t.toJwt());
118
+ expect(decoded.roomConfig).toBeDefined();
119
+ expect((_a = decoded.roomConfig) == null ? void 0 : _a.name).toEqual("test-room");
120
+ expect((_b = decoded.roomConfig) == null ? void 0 : _b.maxParticipants).toEqual(10);
121
+ expect((_c = decoded.roomConfig) == null ? void 0 : _c.agents).toHaveLength(2);
122
+ expect((_e = (_d = decoded.roomConfig) == null ? void 0 : _d.agents) == null ? void 0 : _e[0].agentName).toEqual("agent1");
123
+ expect((_g = (_f = decoded.roomConfig) == null ? void 0 : _f.agents) == null ? void 0 : _g[0].metadata).toEqual("metadata-1");
124
+ expect((_i = (_h = decoded.roomConfig) == null ? void 0 : _h.agents) == null ? void 0 : _i[1].agentName).toEqual("agent2");
125
+ expect((_k = (_j = decoded.roomConfig) == null ? void 0 : _j.agents) == null ? void 0 : _k[1].metadata).toEqual("metadata-2");
126
+ expect((_n = (_m = (_l = decoded.roomConfig) == null ? void 0 : _l.egress) == null ? void 0 : _m.room) == null ? void 0 : _n.roomName).toEqual("test-room");
127
+ });
128
+ });
129
+ //# sourceMappingURL=AccessToken.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AccessToken.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n RoomAgentDispatch,\n RoomCompositeEgressRequest,\n RoomConfiguration,\n RoomEgress,\n} from '@livekit/protocol';\nimport * as jose from 'jose';\nimport { describe, expect, it } from 'vitest';\nimport { AccessToken, TokenVerifier } from './AccessToken';\nimport type { ClaimGrants } from './grants';\n\nconst testApiKey = 'abcdefg';\nconst testSecret = 'abababa';\n\ndescribe('encoded tokens are valid', () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'me',\n name: 'myname',\n });\n t.addGrant({ room: 'myroom' });\n const EncodedTestSecret = new TextEncoder().encode(testSecret);\n it('can be decoded', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload).not.toBe(undefined);\n });\n\n it('has name set', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload.name).toBe('myname');\n });\n\n it('has video grants set', async () => {\n const { payload } = await jose.jwtVerify(await t.toJwt(), EncodedTestSecret, {\n issuer: testApiKey,\n });\n\n expect(payload.video).toBeTruthy();\n expect((payload as ClaimGrants).video?.room).toEqual('myroom');\n });\n});\n\ndescribe('identity is required for only join grants', () => {\n it('allows empty identity for create', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.addGrant({ roomCreate: true });\n\n expect(await t.toJwt()).toBeTruthy();\n });\n it('throws error when identity is not provided for join', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.addGrant({ roomJoin: true });\n\n await expect(async () => {\n await t.toJwt();\n }).rejects.toThrow();\n });\n});\n\ndescribe('verify token is valid', () => {\n it('can decode encoded token', async () => {\n const t = new AccessToken(testApiKey, testSecret);\n t.sha256 = 'abcdefg';\n t.kind = 'agent';\n t.addGrant({ roomCreate: true });\n t.attributes = { foo: 'bar', live: 'kit' };\n\n const v = new TokenVerifier(testApiKey, testSecret);\n const decoded = await v.verify(await t.toJwt());\n\n expect(decoded).not.toBe(undefined);\n expect(decoded.sha256).toEqual('abcdefg');\n expect(decoded.video?.roomCreate).toBeTruthy();\n expect(decoded.kind).toEqual('agent');\n expect(decoded.attributes).toEqual(t.attributes);\n });\n});\n\ndescribe('adding grants should not overwrite existing grants', () => {\n const EncodedTestSecret = new TextEncoder().encode(testSecret);\n\n it('should not overwrite existing grants', async () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'me',\n name: 'myname',\n });\n t.addGrant({ roomCreate: true });\n t.addGrant({ roomJoin: true });\n\n const { payload }: jose.JWTVerifyResult<ClaimGrants> = await jose.jwtVerify(\n await t.toJwt(),\n EncodedTestSecret,\n { issuer: testApiKey },\n );\n expect(payload.video?.roomCreate).toBeTruthy();\n expect(payload.video?.roomJoin).toBeTruthy();\n });\n});\n\ndescribe('room configuration with agents and egress', () => {\n it('should set agents and egress in room configuration', async () => {\n const t = new AccessToken(testApiKey, testSecret, {\n identity: 'test-identity',\n });\n\n const roomConfig = new RoomConfiguration({\n name: 'test-room',\n maxParticipants: 10,\n });\n\n const agents: RoomAgentDispatch[] = [\n new RoomAgentDispatch({\n agentName: 'agent1',\n metadata: 'metadata-1',\n }),\n new RoomAgentDispatch({\n agentName: 'agent2',\n metadata: 'metadata-2',\n }),\n ];\n\n const egress = new RoomEgress({\n room: new RoomCompositeEgressRequest({ roomName: 'test-room' }),\n });\n\n roomConfig.agents = agents;\n roomConfig.egress = egress;\n\n t.roomConfig = roomConfig;\n\n const v = new TokenVerifier(testApiKey, testSecret);\n const decoded = await v.verify(await t.toJwt());\n\n expect(decoded.roomConfig).toBeDefined();\n expect(decoded.roomConfig?.name).toEqual('test-room');\n expect(decoded.roomConfig?.maxParticipants).toEqual(10);\n expect(decoded.roomConfig?.agents).toHaveLength(2);\n expect(decoded.roomConfig?.agents?.[0].agentName).toEqual('agent1');\n expect(decoded.roomConfig?.agents?.[0].metadata).toEqual('metadata-1');\n expect(decoded.roomConfig?.agents?.[1].agentName).toEqual('agent2');\n expect(decoded.roomConfig?.agents?.[1].metadata).toEqual('metadata-2');\n expect(decoded.roomConfig?.egress?.room?.roomName).toEqual('test-room');\n });\n});\n"],"mappings":"AAGA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,YAAY,UAAU;AACtB,SAAS,UAAU,QAAQ,UAAU;AACrC,SAAS,aAAa,qBAAqB;AAG3C,MAAM,aAAa;AACnB,MAAM,aAAa;AAEnB,SAAS,4BAA4B,MAAM;AACzC,QAAM,IAAI,IAAI,YAAY,YAAY,YAAY;AAAA,IAChD,UAAU;AAAA,IACV,MAAM;AAAA,EACR,CAAC;AACD,IAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC7B,QAAM,oBAAoB,IAAI,YAAY,EAAE,OAAO,UAAU;AAC7D,KAAG,kBAAkB,YAAY;AAC/B,UAAM,EAAE,QAAQ,IAAI,MAAM,KAAK,UAAU,MAAM,EAAE,MAAM,GAAG,mBAAmB;AAAA,MAC3E,QAAQ;AAAA,IACV,CAAC;AAED,WAAO,OAAO,EAAE,IAAI,KAAK,MAAS;AAAA,EACpC,CAAC;AAED,KAAG,gBAAgB,YAAY;AAC7B,UAAM,EAAE,QAAQ,IAAI,MAAM,KAAK,UAAU,MAAM,EAAE,MAAM,GAAG,mBAAmB;AAAA,MAC3E,QAAQ;AAAA,IACV,CAAC;A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AAW,SAAS;AAEpB,MAAE,aAAa;AAEf,UAAM,IAAI,IAAI,cAAc,YAAY,UAAU;AAClD,UAAM,UAAU,MAAM,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC;AAE9C,WAAO,QAAQ,UAAU,EAAE,YAAY;AACvC,YAAO,aAAQ,eAAR,mBAAoB,IAAI,EAAE,QAAQ,WAAW;AACpD,YAAO,aAAQ,eAAR,mBAAoB,eAAe,EAAE,QAAQ,EAAE;AACtD,YAAO,aAAQ,eAAR,mBAAoB,MAAM,EAAE,aAAa,CAAC;AACjD,YAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,SAAS,EAAE,QAAQ,QAAQ;AAClE,YAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,QAAQ,EAAE,QAAQ,YAAY;AACrE,YAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,SAAS,EAAE,QAAQ,QAAQ;AAClE,YAAO,mBAAQ,eAAR,mBAAoB,WAApB,mBAA6B,GAAG,QAAQ,EAAE,QAAQ,YAAY;AACrE,YAAO,yBAAQ,eAAR,mBAAoB,WAApB,mBAA4B,SAA5B,mBAAkC,QAAQ,EAAE,QAAQ,WAAW;AAAA,EACxE,CAAC;AACH,CAAC;","names":[]}
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var AgentDispatchClient_exports = {};
30
+ __export(AgentDispatchClient_exports, {
31
+ AgentDispatchClient: () => AgentDispatchClient
32
+ });
33
+ module.exports = __toCommonJS(AgentDispatchClient_exports);
34
+ var import_protocol = require("@livekit/protocol");
35
+ var import_ServiceBase = __toESM(require("./ServiceBase.cjs"), 1);
36
+ var import_TwirpRPC = require("./TwirpRPC.cjs");
37
+ const svc = "AgentDispatchService";
38
+ class AgentDispatchClient extends import_ServiceBase.default {
39
+ /**
40
+ * @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
41
+ * @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
42
+ * @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
43
+ */
44
+ constructor(host, apiKey, secret) {
45
+ super(apiKey, secret);
46
+ this.rpc = new import_TwirpRPC.TwirpRpc(host, import_TwirpRPC.livekitPackage);
47
+ }
48
+ /**
49
+ * Create an explicit dispatch for an agent to join a room. To use explicit
50
+ * dispatch, your agent must be registered with an `agentName`.
51
+ * @param roomName - name of the room to dispatch to
52
+ * @param agentName - name of the agent to dispatch
53
+ * @param options - optional metadata to send along with the dispatch
54
+ * @returns the dispatch that was created
55
+ */
56
+ async createDispatch(roomName, agentName, options) {
57
+ const req = new import_protocol.CreateAgentDispatchRequest({
58
+ room: roomName,
59
+ agentName,
60
+ metadata: options == null ? void 0 : options.metadata
61
+ }).toJson();
62
+ const data = await this.rpc.request(
63
+ svc,
64
+ "CreateDispatch",
65
+ req,
66
+ await this.authHeader({ roomAdmin: true, room: roomName })
67
+ );
68
+ return import_protocol.AgentDispatch.fromJson(data, { ignoreUnknownFields: true });
69
+ }
70
+ /**
71
+ * Delete an explicit dispatch for an agent in a room.
72
+ * @param dispatchId - id of the dispatch to delete
73
+ * @param roomName - name of the room the dispatch is for
74
+ */
75
+ async deleteDispatch(dispatchId, roomName) {
76
+ const req = new import_protocol.DeleteAgentDispatchRequest({
77
+ dispatchId,
78
+ room: roomName
79
+ }).toJson();
80
+ await this.rpc.request(
81
+ svc,
82
+ "DeleteDispatch",
83
+ req,
84
+ await this.authHeader({ roomAdmin: true, room: roomName })
85
+ );
86
+ }
87
+ /**
88
+ * Get an Agent dispatch by ID
89
+ * @param dispatchId - id of the dispatch to get
90
+ * @param roomName - name of the room the dispatch is for
91
+ * @returns the dispatch that was found, or undefined if not found
92
+ */
93
+ async getDispatch(dispatchId, roomName) {
94
+ const req = new import_protocol.ListAgentDispatchRequest({
95
+ dispatchId,
96
+ room: roomName
97
+ }).toJson();
98
+ const data = await this.rpc.request(
99
+ svc,
100
+ "ListDispatch",
101
+ req,
102
+ await this.authHeader({ roomAdmin: true, room: roomName })
103
+ );
104
+ const res = import_protocol.ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
105
+ if (res.agentDispatches.length === 0) {
106
+ return void 0;
107
+ }
108
+ return res.agentDispatches[0];
109
+ }
110
+ /**
111
+ * List all agent dispatches for a room
112
+ * @param roomName - name of the room to list dispatches for
113
+ * @returns the list of dispatches
114
+ */
115
+ async listDispatch(roomName) {
116
+ const req = new import_protocol.ListAgentDispatchRequest({
117
+ room: roomName
118
+ }).toJson();
119
+ const data = await this.rpc.request(
120
+ svc,
121
+ "ListDispatch",
122
+ req,
123
+ await this.authHeader({ roomAdmin: true, room: roomName })
124
+ );
125
+ const res = import_protocol.ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
126
+ return res.agentDispatches;
127
+ }
128
+ }
129
+ // Annotate the CommonJS export names for ESM import in node:
130
+ 0 && (module.exports = {
131
+ AgentDispatchClient
132
+ });
133
+ //# sourceMappingURL=AgentDispatchClient.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AgentDispatchClient.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n AgentDispatch,\n CreateAgentDispatchRequest,\n DeleteAgentDispatchRequest,\n ListAgentDispatchRequest,\n ListAgentDispatchResponse,\n} from '@livekit/protocol';\nimport ServiceBase from './ServiceBase.js';\nimport { type Rpc, TwirpRpc, livekitPackage } from './TwirpRPC.js';\n\ninterface CreateDispatchOptions {\n // any custom data to send along with the job.\n // note: this is different from room and participant metadata\n metadata?: string;\n}\n\nconst svc = 'AgentDispatchService';\n\n/**\n * Client to access Agent APIs\n */\nexport class AgentDispatchClient extends ServiceBase {\n private readonly rpc: Rpc;\n\n /**\n * @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'\n * @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY\n * @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET\n */\n constructor(host: string, apiKey?: string, secret?: string) {\n super(apiKey, secret);\n this.rpc = new TwirpRpc(host, livekitPackage);\n }\n\n /**\n * Create an explicit dispatch for an agent to join a room. To use explicit\n * dispatch, your agent must be registered with an `agentName`.\n * @param roomName - name of the room to dispatch to\n * @param agentName - name of the agent to dispatch\n * @param options - optional metadata to send along with the dispatch\n * @returns the dispatch that was created\n */\n async createDispatch(\n roomName: string,\n agentName: string,\n options?: CreateDispatchOptions,\n ): Promise<AgentDispatch> {\n const req = new CreateAgentDispatchRequest({\n room: roomName,\n agentName,\n metadata: options?.metadata,\n }).toJson();\n const data = await this.rpc.request(\n svc,\n 'CreateDispatch',\n req,\n await this.authHeader({ roomAdmin: true, room: roomName }),\n );\n return AgentDispatch.fromJson(data, { ignoreUnknownFields: true });\n }\n\n /**\n * Delete an explicit dispatch for an agent in a room.\n * @param dispatchId - id of the dispatch to delete\n * @param roomName - name of the room the dispatch is for\n */\n async deleteDispatch(dispatchId: string, roomName: string): Promise<void> {\n const req = new DeleteAgentDispatchRequest({\n dispatchId,\n room: roomName,\n }).toJson();\n await this.rpc.request(\n svc,\n 'DeleteDispatch',\n req,\n await this.authHeader({ roomAdmin: true, room: roomName }),\n );\n }\n\n /**\n * Get an Agent dispatch by ID\n * @param dispatchId - id of the dispatch to get\n * @param roomName - name of the room the dispatch is for\n * @returns the dispatch that was found, or undefined if not found\n */\n async getDispatch(dispatchId: string, roomName: string): Promise<AgentDispatch | undefined> {\n const req = new ListAgentDispatchRequest({\n dispatchId,\n room: roomName,\n }).toJson();\n const data = await this.rpc.request(\n svc,\n 'ListDispatch',\n req,\n await this.authHeader({ roomAdmin: true, room: roomName }),\n );\n const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });\n if (res.agentDispatches.length === 0) {\n return undefined;\n }\n return res.agentDispatches[0];\n }\n\n /**\n * List all agent dispatches for a room\n * @param roomName - name of the room to list dispatches for\n * @returns the list of dispatches\n */\n async listDispatch(roomName: string): Promise<AgentDispatch[]> {\n const req = new ListAgentDispatchRequest({\n room: roomName,\n }).toJson();\n const data = await this.rpc.request(\n svc,\n 'ListDispatch',\n req,\n await this.authHeader({ roomAdmin: true, room: roomName }),\n );\n const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });\n return res.agentDispatches;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,sBAMO;AACP,yBAAwB;AACxB,sBAAmD;AAQnD,MAAM,MAAM;AAKL,MAAM,4BAA4B,mBAAAA,QAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQnD,YAAY,MAAc,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GAAG;AACpC,aAAO;AAAA,IACT;AACA,WAAO,IAAI,gBAAgB,CAAC;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,UAA4C;AAC7D,UAAM,MAAM,IAAI,yCAAyB;AAAA,MACvC,MAAM;AAAA,IACR,CAAC,EAAE,OAAO;AACV,UAAM,OAAO,MAAM,KAAK,IAAI;AAAA,MAC1B;AAAA,MACA;AAAA,MACA;AAAA,MACA,MAAM,KAAK,WAAW,EAAE,WAAW,MAAM,MAAM,SAAS,CAAC;AAAA,IAC3D;AACA,UAAM,MAAM,0CAA0B,SAAS,MAAM,EAAE,qBAAqB,KAAK,CAAC;AAClF,WAAO,IAAI;AAAA,EACb;AACF;","names":["ServiceBase"]}
@@ -1,82 +1,105 @@
1
- // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
- //
3
- // SPDX-License-Identifier: Apache-2.0
4
- import { AgentDispatch, CreateAgentDispatchRequest, DeleteAgentDispatchRequest, ListAgentDispatchRequest, ListAgentDispatchResponse, } from '@livekit/protocol';
5
- import ServiceBase from './ServiceBase.js';
6
- import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
7
- const svc = 'AgentDispatchService';
8
- /**
9
- * Client to access Agent APIs
10
- */
11
- export class AgentDispatchClient extends ServiceBase {
12
- /**
13
- * @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
14
- * @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
15
- * @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
16
- */
17
- constructor(host, apiKey, secret) {
18
- super(apiKey, secret);
19
- this.rpc = new TwirpRpc(host, livekitPackage);
20
- }
21
- /**
22
- * Create an explicit dispatch for an agent to join a room. To use explicit
23
- * dispatch, your agent must be registered with an `agentName`.
24
- * @param roomName - name of the room to dispatch to
25
- * @param agentName - name of the agent to dispatch
26
- * @param options - optional metadata to send along with the dispatch
27
- * @returns the dispatch that was created
28
- */
29
- async createDispatch(roomName, agentName, options) {
30
- const req = new CreateAgentDispatchRequest({
31
- room: roomName,
32
- agentName,
33
- metadata: options === null || options === void 0 ? void 0 : options.metadata,
34
- }).toJson();
35
- const data = await this.rpc.request(svc, 'CreateDispatch', req, await this.authHeader({ roomAdmin: true, room: roomName }));
36
- return AgentDispatch.fromJson(data, { ignoreUnknownFields: true });
37
- }
38
- /**
39
- * Delete an explicit dispatch for an agent in a room.
40
- * @param dispatchId - id of the dispatch to delete
41
- * @param roomName - name of the room the dispatch is for
42
- */
43
- async deleteDispatch(dispatchId, roomName) {
44
- const req = new DeleteAgentDispatchRequest({
45
- dispatchId,
46
- room: roomName,
47
- }).toJson();
48
- await this.rpc.request(svc, 'DeleteDispatch', req, await this.authHeader({ roomAdmin: true, room: roomName }));
49
- }
50
- /**
51
- * Get an Agent dispatch by ID
52
- * @param dispatchId - id of the dispatch to get
53
- * @param roomName - name of the room the dispatch is for
54
- * @returns the dispatch that was found, or undefined if not found
55
- */
56
- async getDispatch(dispatchId, roomName) {
57
- const req = new ListAgentDispatchRequest({
58
- dispatchId,
59
- room: roomName,
60
- }).toJson();
61
- const data = await this.rpc.request(svc, 'ListDispatch', req, await this.authHeader({ roomAdmin: true, room: roomName }));
62
- const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
63
- if (res.agentDispatches.length === 0) {
64
- return undefined;
65
- }
66
- return res.agentDispatches[0];
67
- }
68
- /**
69
- * List all agent dispatches for a room
70
- * @param roomName - name of the room to list dispatches for
71
- * @returns the list of dispatches
72
- */
73
- async listDispatch(roomName) {
74
- const req = new ListAgentDispatchRequest({
75
- room: roomName,
76
- }).toJson();
77
- const data = await this.rpc.request(svc, 'ListDispatch', req, await this.authHeader({ roomAdmin: true, room: roomName }));
78
- const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
79
- return res.agentDispatches;
1
+ import {
2
+ AgentDispatch,
3
+ CreateAgentDispatchRequest,
4
+ DeleteAgentDispatchRequest,
5
+ ListAgentDispatchRequest,
6
+ ListAgentDispatchResponse
7
+ } from "@livekit/protocol";
8
+ import ServiceBase from "./ServiceBase.js";
9
+ import { TwirpRpc, livekitPackage } from "./TwirpRPC.js";
10
+ const svc = "AgentDispatchService";
11
+ class AgentDispatchClient extends ServiceBase {
12
+ /**
13
+ * @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
14
+ * @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
15
+ * @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
16
+ */
17
+ constructor(host, apiKey, secret) {
18
+ super(apiKey, secret);
19
+ this.rpc = new TwirpRpc(host, livekitPackage);
20
+ }
21
+ /**
22
+ * Create an explicit dispatch for an agent to join a room. To use explicit
23
+ * dispatch, your agent must be registered with an `agentName`.
24
+ * @param roomName - name of the room to dispatch to
25
+ * @param agentName - name of the agent to dispatch
26
+ * @param options - optional metadata to send along with the dispatch
27
+ * @returns the dispatch that was created
28
+ */
29
+ async createDispatch(roomName, agentName, options) {
30
+ const req = new CreateAgentDispatchRequest({
31
+ room: roomName,
32
+ agentName,
33
+ metadata: options == null ? void 0 : options.metadata
34
+ }).toJson();
35
+ const data = await this.rpc.request(
36
+ svc,
37
+ "CreateDispatch",
38
+ req,
39
+ await this.authHeader({ roomAdmin: true, room: roomName })
40
+ );
41
+ return AgentDispatch.fromJson(data, { ignoreUnknownFields: true });
42
+ }
43
+ /**
44
+ * Delete an explicit dispatch for an agent in a room.
45
+ * @param dispatchId - id of the dispatch to delete
46
+ * @param roomName - name of the room the dispatch is for
47
+ */
48
+ async deleteDispatch(dispatchId, roomName) {
49
+ const req = new DeleteAgentDispatchRequest({
50
+ dispatchId,
51
+ room: roomName
52
+ }).toJson();
53
+ await this.rpc.request(
54
+ svc,
55
+ "DeleteDispatch",
56
+ req,
57
+ await this.authHeader({ roomAdmin: true, room: roomName })
58
+ );
59
+ }
60
+ /**
61
+ * Get an Agent dispatch by ID
62
+ * @param dispatchId - id of the dispatch to get
63
+ * @param roomName - name of the room the dispatch is for
64
+ * @returns the dispatch that was found, or undefined if not found
65
+ */
66
+ async getDispatch(dispatchId, roomName) {
67
+ const req = new ListAgentDispatchRequest({
68
+ dispatchId,
69
+ room: roomName
70
+ }).toJson();
71
+ const data = await this.rpc.request(
72
+ svc,
73
+ "ListDispatch",
74
+ req,
75
+ await this.authHeader({ roomAdmin: true, room: roomName })
76
+ );
77
+ const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
78
+ if (res.agentDispatches.length === 0) {
79
+ return void 0;
80
80
  }
81
+ return res.agentDispatches[0];
82
+ }
83
+ /**
84
+ * List all agent dispatches for a room
85
+ * @param roomName - name of the room to list dispatches for
86
+ * @returns the list of dispatches
87
+ */
88
+ async listDispatch(roomName) {
89
+ const req = new ListAgentDispatchRequest({
90
+ room: roomName
91
+ }).toJson();
92
+ const data = await this.rpc.request(
93
+ svc,
94
+ "ListDispatch",
95
+ req,
96
+ await this.authHeader({ roomAdmin: true, room: roomName })
97
+ );
98
+ const res = ListAgentDispatchResponse.fromJson(data, { ignoreUnknownFields: true });
99
+ return res.agentDispatches;
100
+ }
81
101
  }
102
+ export {
103
+ AgentDispatchClient
104
+ };
82
105
  //# sourceMappingURL=AgentDispatchClient.js.map