Skip to content

Commit a5f5f6e

Browse files
authored
add cloud agents (#625)
* add cloud agents * update protocol * add client settings endpoint * latest protocol
1 parent 23b164d commit a5f5f6e

File tree

4 files changed

+193
-71
lines changed

4 files changed

+193
-71
lines changed

agent_client.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package lksdk
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
"regexp"
8+
9+
"github.com/twitchtv/twirp"
10+
11+
"github.com/livekit/protocol/livekit"
12+
)
13+
14+
type AgentClient struct {
15+
agentClient livekit.CloudAgent
16+
authBase
17+
}
18+
19+
func NewAgentClient(url string, apiKey string, apiSecret string, opts ...twirp.ClientOption) (*AgentClient, error) {
20+
serverUrl := os.Getenv("LK_AGENTS_URL")
21+
if serverUrl == "" {
22+
url = ToHttpURL(url)
23+
pattern := `^https?://[^.]+\.`
24+
re := regexp.MustCompile(pattern)
25+
serverUrl = re.ReplaceAllString(url, "https://agents.")
26+
}
27+
28+
client := livekit.NewCloudAgentProtobufClient(serverUrl, &http.Client{}, opts...)
29+
return &AgentClient{
30+
agentClient: client,
31+
authBase: authBase{apiKey, apiSecret},
32+
}, nil
33+
}
34+
35+
func (c *AgentClient) CreateAgent(ctx context.Context, req *livekit.CreateAgentRequest) (*livekit.CreateAgentResponse, error) {
36+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
37+
if err != nil {
38+
return nil, err
39+
}
40+
return c.agentClient.CreateAgent(ctx, req)
41+
}
42+
43+
func (c *AgentClient) ListAgents(ctx context.Context, req *livekit.ListAgentsRequest) (*livekit.ListAgentsResponse, error) {
44+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
45+
if err != nil {
46+
return nil, err
47+
}
48+
return c.agentClient.ListAgents(ctx, req)
49+
}
50+
51+
func (c *AgentClient) ListAgentVersions(ctx context.Context, req *livekit.ListAgentVersionsRequest) (*livekit.ListAgentVersionsResponse, error) {
52+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
53+
if err != nil {
54+
return nil, err
55+
}
56+
return c.agentClient.ListAgentVersions(ctx, req)
57+
}
58+
59+
func (c *AgentClient) DeleteAgent(ctx context.Context, req *livekit.DeleteAgentRequest) (*livekit.DeleteAgentResponse, error) {
60+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
61+
if err != nil {
62+
return nil, err
63+
}
64+
return c.agentClient.DeleteAgent(ctx, req)
65+
}
66+
67+
func (c *AgentClient) UpdateAgent(ctx context.Context, req *livekit.UpdateAgentRequest) (*livekit.UpdateAgentResponse, error) {
68+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
69+
if err != nil {
70+
return nil, err
71+
}
72+
return c.agentClient.UpdateAgent(ctx, req)
73+
}
74+
75+
func (c *AgentClient) RollbackAgent(ctx context.Context, req *livekit.RollbackAgentRequest) (*livekit.RollbackAgentResponse, error) {
76+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
77+
if err != nil {
78+
return nil, err
79+
}
80+
return c.agentClient.RollbackAgent(ctx, req)
81+
}
82+
83+
func (c *AgentClient) ListAgentSecrets(ctx context.Context, req *livekit.ListAgentSecretsRequest) (*livekit.ListAgentSecretsResponse, error) {
84+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
85+
if err != nil {
86+
return nil, err
87+
}
88+
return c.agentClient.ListAgentSecrets(ctx, req)
89+
}
90+
91+
func (c *AgentClient) UpdateAgentSecrets(ctx context.Context, req *livekit.UpdateAgentSecretsRequest) (*livekit.UpdateAgentSecretsResponse, error) {
92+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
93+
if err != nil {
94+
return nil, err
95+
}
96+
return c.agentClient.UpdateAgentSecrets(ctx, req)
97+
}
98+
99+
func (c *AgentClient) DeployAgent(ctx context.Context, req *livekit.DeployAgentRequest) (*livekit.DeployAgentResponse, error) {
100+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
101+
if err != nil {
102+
return nil, err
103+
}
104+
return c.agentClient.DeployAgent(ctx, req)
105+
}
106+
107+
func (c *AgentClient) GetClientSettings(ctx context.Context, req *livekit.ClientSettingsRequest) (*livekit.ClientSettingsResponse, error) {
108+
ctx, err := c.withAuth(ctx, withAgentGrant{Admin: true})
109+
if err != nil {
110+
return nil, err
111+
}
112+
return c.agentClient.GetClientSettings(ctx, req)
113+
}

auth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
"context"
1919
"net/http"
2020

21-
"github.com/livekit/protocol/auth"
2221
"github.com/twitchtv/twirp"
22+
23+
"github.com/livekit/protocol/auth"
2324
)
2425

2526
type authBase struct {
@@ -43,6 +44,12 @@ func (g withSIPGrant) Apply(t *auth.AccessToken) {
4344
t.SetSIPGrant((*auth.SIPGrant)(&g))
4445
}
4546

47+
type withAgentGrant auth.AgentGrant
48+
49+
func (g withAgentGrant) Apply(t *auth.AccessToken) {
50+
t.SetAgentGrant((*auth.AgentGrant)(&g))
51+
}
52+
4653
func (b authBase) withAuth(ctx context.Context, opt authOption, options ...authOption) (context.Context, error) {
4754
at := auth.NewAccessToken(b.apiKey, b.apiSecret)
4855
opt.Apply(at)

go.mod

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/livekit/server-sdk-go/v2
22

3-
go 1.22.7
3+
go 1.23.0
44

55
toolchain go1.23.6
66

@@ -12,7 +12,7 @@ require (
1212
github.com/gorilla/websocket v1.5.3
1313
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
1414
github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564
15-
github.com/livekit/protocol v1.32.2-0.20250206110518-331f97dbf4f3
15+
github.com/livekit/protocol v1.35.1-0.20250319165056-fdacb1a293e5
1616
github.com/magefile/mage v1.15.0
1717
github.com/pion/dtls/v3 v3.0.4
1818
github.com/pion/interceptor v0.1.37
@@ -23,34 +23,35 @@ require (
2323
github.com/stretchr/testify v1.10.0
2424
github.com/twitchtv/twirp v8.1.3+incompatible
2525
go.uber.org/atomic v1.11.0
26-
golang.org/x/crypto v0.33.0
27-
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3
28-
google.golang.org/grpc v1.70.0
26+
golang.org/x/crypto v0.36.0
27+
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
28+
google.golang.org/grpc v1.71.0
2929
google.golang.org/protobuf v1.36.5
3030
)
3131

3232
require (
33-
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240717164558-a6c49f84cc0f.2 // indirect
34-
buf.build/go/protoyaml v0.2.0 // indirect
33+
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.0-20241127180247-a33202765966.1 // indirect
34+
buf.build/go/protoyaml v0.3.1 // indirect
35+
cel.dev/expr v0.19.1 // indirect
3536
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
3637
github.com/benbjohnson/clock v1.3.5 // indirect
37-
github.com/bufbuild/protovalidate-go v0.6.3 // indirect
38+
github.com/bufbuild/protovalidate-go v0.8.0 // indirect
3839
github.com/cespare/xxhash/v2 v2.3.0 // indirect
3940
github.com/davecgh/go-spew v1.1.1 // indirect
4041
github.com/dennwc/iters v1.0.1 // indirect
4142
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
42-
github.com/frostbyte73/core v0.1.0 // indirect
43+
github.com/frostbyte73/core v0.1.1 // indirect
4344
github.com/fsnotify/fsnotify v1.8.0 // indirect
4445
github.com/gammazero/deque v1.0.0 // indirect
45-
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
46-
github.com/google/cel-go v0.21.0 // indirect
46+
github.com/go-jose/go-jose/v3 v3.0.4 // indirect
47+
github.com/google/cel-go v0.22.1 // indirect
4748
github.com/jxskiss/base62 v1.1.0 // indirect
48-
github.com/klauspost/compress v1.17.11 // indirect
49+
github.com/klauspost/compress v1.18.0 // indirect
4950
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
50-
github.com/lithammer/shortuuid/v4 v4.0.0 // indirect
51-
github.com/livekit/psrpc v0.6.1-0.20250204212339-6de8b05bfcff // indirect
52-
github.com/nats-io/nats.go v1.38.0 // indirect
53-
github.com/nats-io/nkeys v0.4.9 // indirect
51+
github.com/lithammer/shortuuid/v4 v4.2.0 // indirect
52+
github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 // indirect
53+
github.com/nats-io/nats.go v1.39.1 // indirect
54+
github.com/nats-io/nkeys v0.4.10 // indirect
5455
github.com/nats-io/nuid v1.0.1 // indirect
5556
github.com/pion/datachannel v1.5.10 // indirect
5657
github.com/pion/ice/v4 v4.0.7 // indirect
@@ -63,19 +64,19 @@ require (
6364
github.com/pion/transport/v3 v3.0.7 // indirect
6465
github.com/pion/turn/v4 v4.0.0 // indirect
6566
github.com/pmezard/go-difflib v1.0.0 // indirect
66-
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
67-
github.com/redis/go-redis/v9 v9.7.0 // indirect
67+
github.com/puzpuzpuz/xsync/v3 v3.5.0 // indirect
68+
github.com/redis/go-redis/v9 v9.7.1 // indirect
6869
github.com/stoewer/go-strcase v1.3.0 // indirect
6970
github.com/wlynxg/anet v0.0.5 // indirect
7071
github.com/zeebo/xxh3 v1.0.2 // indirect
7172
go.uber.org/multierr v1.11.0 // indirect
7273
go.uber.org/zap v1.27.0 // indirect
7374
go.uber.org/zap/exp v0.3.0 // indirect
7475
golang.org/x/net v0.35.0 // indirect
75-
golang.org/x/sync v0.11.0 // indirect
76-
golang.org/x/sys v0.30.0 // indirect
77-
golang.org/x/text v0.22.0 // indirect
78-
google.golang.org/genproto/googleapis/api v0.0.0-20241202173237-19429a94021a // indirect
79-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250124145028-65684f501c47 // indirect
76+
golang.org/x/sync v0.12.0 // indirect
77+
golang.org/x/sys v0.31.0 // indirect
78+
golang.org/x/text v0.23.0 // indirect
79+
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
80+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
8081
gopkg.in/yaml.v3 v3.0.1 // indirect
8182
)

0 commit comments

Comments
 (0)