From 106c40466382daaa403e7f472647248e14d939d7 Mon Sep 17 00:00:00 2001 From: Young-Jin Park Date: Sun, 4 Aug 2024 23:11:43 -0400 Subject: [PATCH] chore: add back custom code This reverts commit ad38619dc8095e4022a958f74c7f3285d5d199e1. --- README.md | 84 ++++++-- bedrock/bedrock.go | 210 ++++++++++++++++++++ examples/tools-streaming-jsonschema/main.go | 2 +- go.mod | 43 +++- go.sum | 178 +++++++++++++++++ internal/requestconfig/requestconfig.go | 8 +- internal/version.go | 2 +- message.go | 200 +++++++++++++++---- message_test.go | 40 +--- vertex/vertex.go | 99 +++++++++ 10 files changed, 765 insertions(+), 101 deletions(-) create mode 100644 bedrock/bedrock.go create mode 100644 vertex/vertex.go diff --git a/README.md b/README.md index 6bef330..883643a 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,9 @@ func main() { ) message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -181,10 +180,9 @@ To handle errors, we recommend that you use the `errors.As` pattern: ```go _, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }) if err != nil { @@ -215,10 +213,9 @@ client.Messages.New( ctx, anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, // This sets the per-retry timeout @@ -258,10 +255,9 @@ client.Messages.New( context.TODO(), anthropic.MessageNewParams{ MaxTokens: anthropic.F(int64(1024)), - Messages: anthropic.F([]anthropic.MessageParam{{ - Role: anthropic.F(anthropic.MessageParamRoleUser), - Content: anthropic.F([]anthropic.MessageParamContentUnion{anthropic.TextBlockParam{Type: anthropic.F(anthropic.TextBlockParamTypeText), Text: anthropic.F("What is a quaternion?")}}), - }}), + Messages: anthropic.F([]anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock("What is the weather in SF?")), + }), Model: anthropic.F(anthropic.ModelClaude_3_5_Sonnet_20240620), }, option.WithMaxRetries(5), @@ -354,6 +350,62 @@ You may also replace the default `http.Client` with accepted (this overwrites any previous client) and receives requests after any middleware has been applied. +## Amazon Bedrock + +To use this library with [Amazon Bedrock](https://aws.amazon.com/bedrock/claude/), +use the bedrock request option `bedrock.WithLoadDefaultConfig(…)` which reads the +[default config](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). + +Importing the `bedrock` library also globally registers a decoder for `application/vnd.amazon.eventstream` for +streaming. + +```go +package main + +import ( + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/bedrock" +) + +func main() { + client := anthropic.NewClient( + bedrock.WithLoadDefaultConfig(context.Background()), + ) +} +``` + +If you already have an `aws.Config`, you can also use it directly with `bedrock.WithConfig(cfg)`. + +Read more about Anthropic and Amazon Bedrock [here](https://docs.anthropic.com/en/api/claude-on-amazon-bedrock). + +## Google Vertex AI + +To use this library with [Google Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), +use the request option `vertex.WithGoogleAuth(…)` which reads the +[Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials). + +```go +package main + +import ( + "context" + + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/vertex" +) + +func main() { + client := anthropic.NewClient( + vertex.WithGoogleAuth(context.Background(), "us-central1", "stainless-399616"), + ) +} +``` + +If you already have `*google.Credentials`, you can also use it directly with +`vertex.WithCredentials(ctx, region, projectId, creds)`. + +Read more about Anthropic and Google Vertex [here](https://docs.anthropic.com/en/api/claude-on-vertex-ai). + ## Semantic versioning This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: diff --git a/bedrock/bedrock.go b/bedrock/bedrock.go new file mode 100644 index 0000000..a4729e5 --- /dev/null +++ b/bedrock/bedrock.go @@ -0,0 +1,210 @@ +package bedrock + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream" + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream/eventstreamapi" + v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" + + "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" + "github.com/anthropics/anthropic-sdk-go/option" + "github.com/anthropics/anthropic-sdk-go/packages/ssestream" +) + +const DefaultVersion = "bedrock-2023-05-31" + +var DefaultEndpoints = map[string]bool{ + "/v1/complete": true, + "/v1/messages": true, +} + +type eventstreamChunk struct { + Bytes string `json:"bytes"` + P string `json:"p"` +} + +type eventstreamDecoder struct { + eventstream.Decoder + + rc io.ReadCloser + evt ssestream.Event + err error +} + +func (e *eventstreamDecoder) Close() error { + return nil +} + +func (e *eventstreamDecoder) Err() error { + return nil +} + +func (e *eventstreamDecoder) Next() bool { + if e.err != nil { + return false + } + + msg, err := e.Decoder.Decode(e.rc, nil) + if err != nil { + e.err = err + return false + } + + messageType := msg.Headers.Get(eventstreamapi.MessageTypeHeader) + if messageType == nil { + e.err = fmt.Errorf("%s event header not present", eventstreamapi.MessageTypeHeader) + return false + } + + switch messageType.String() { + case eventstreamapi.EventMessageType: + eventType := msg.Headers.Get(eventstreamapi.EventTypeHeader) + if eventType == nil { + e.err = fmt.Errorf("%s event header not present", eventstreamapi.EventTypeHeader) + return false + } + + if eventType.String() == "chunk" { + chunk := eventstreamChunk{} + err = json.Unmarshal(msg.Payload, &chunk) + if err != nil { + e.err = err + return false + } + decoded, err := base64.StdEncoding.DecodeString(chunk.Bytes) + if err != nil { + e.err = err + return false + } + e.evt = ssestream.Event{ + Type: gjson.GetBytes(decoded, "type").String(), + Data: decoded, + } + } + + case eventstreamapi.ExceptionMessageType: + case eventstreamapi.ErrorMessageType: + errorCode := "UnknownError" + errorMessage := errorCode + if header := msg.Headers.Get(eventstreamapi.ErrorCodeHeader); header != nil { + errorCode = header.String() + } + if header := msg.Headers.Get(eventstreamapi.ErrorMessageHeader); header != nil { + errorMessage = header.String() + } + e.err = fmt.Errorf("received error or exception %s: %s", errorCode, errorMessage) + return false + } + + return true +} + +func (e *eventstreamDecoder) Event() ssestream.Event { + return e.evt +} + +var ( + _ ssestream.Decoder = &eventstreamDecoder{} +) + +func init() { + ssestream.RegisterDecoder("application/vnd.amazon.eventstream", func(rc io.ReadCloser) ssestream.Decoder { + return &eventstreamDecoder{rc: rc} + }) +} + +// WithLoadDefaultConfig returns a request option which loads the default config for Amazon and registers +// middleware that intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. +// +// If you already have an [aws.Config], it is recommended that you instead call [WithConfig] directly. +func WithLoadDefaultConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) option.RequestOption { + cfg, err := config.LoadDefaultConfig(ctx, optFns...) + if err != nil { + panic(err) + } + return WithConfig(cfg) +} + +// WithConfig returns a request option which uses the provided config and registers middleware that +// intercepts request to the Messages API so that this SDK can be used with Amazon Bedrock. +func WithConfig(cfg aws.Config) option.RequestOption { + signer := v4.NewSigner() + middleware := bedrockMiddleware(signer, cfg) + + return func(rc *requestconfig.RequestConfig) error { + return rc.Apply( + option.WithBaseURL(fmt.Sprintf("https://bedrock-runtime.%s.amazonaws.com", cfg.Region)), + option.WithMiddleware(middleware), + ) + } +} + +func bedrockMiddleware(signer *v4.Signer, cfg aws.Config) option.Middleware { + return func(r *http.Request, next option.MiddlewareNext) (res *http.Response, err error) { + var body []byte + if r.Body != nil { + body, err = io.ReadAll(r.Body) + if err != nil { + return nil, err + } + r.Body.Close() + + if !gjson.GetBytes(body, "anthropic_version").Exists() { + body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) + } + + if r.Method == http.MethodPost && DefaultEndpoints[r.URL.Path] { + model := gjson.GetBytes(body, "model").String() + stream := gjson.GetBytes(body, "stream").Bool() + + body, _ = sjson.DeleteBytes(body, "model") + body, _ = sjson.DeleteBytes(body, "stream") + + var path string + if stream { + path = fmt.Sprintf("/model/%s/invoke-with-response-stream", model) + } else { + path = fmt.Sprintf("/model/%s/invoke", model) + } + + r.URL.Path = path + } + + reader := bytes.NewReader(body) + r.Body = io.NopCloser(reader) + r.GetBody = func() (io.ReadCloser, error) { + _, err := reader.Seek(0, 0) + return io.NopCloser(reader), err + } + r.ContentLength = int64(len(body)) + } + + ctx := r.Context() + credentials, err := cfg.Credentials.Retrieve(ctx) + if err != nil { + return nil, err + } + + hash := sha256.Sum256(body) + err = signer.SignHTTP(ctx, credentials, r, hex.EncodeToString(hash[:]), "bedrock", cfg.Region, time.Now()) + if err != nil { + return nil, err + } + + return next(r) + } +} diff --git a/examples/tools-streaming-jsonschema/main.go b/examples/tools-streaming-jsonschema/main.go index 7569e8c..ba745d6 100644 --- a/examples/tools-streaming-jsonschema/main.go +++ b/examples/tools-streaming-jsonschema/main.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" - "github.com/anthropics/anthropic-sdk-go" "github.com/invopop/jsonschema" + "github.com/anthropics/anthropic-sdk-go" ) func main() { diff --git a/go.mod b/go.mod index 5b49773..c3f3578 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,50 @@ module github.com/anthropics/anthropic-sdk-go go 1.19 require ( - github.com/google/uuid v1.3.0 // indirect + cloud.google.com/go/auth v0.7.2 // indirect + cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect + cloud.google.com/go/compute/metadata v0.5.0 // indirect + github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect + github.com/aws/aws-sdk-go-v2/config v1.27.27 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.17.27 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect + github.com/aws/smithy-go v1.20.3 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/s2a-go v0.1.7 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/tidwall/gjson v1.14.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect + go.opencensus.io v0.24.0 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect + golang.org/x/time v0.5.0 // indirect + google.golang.org/api v0.189.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect + google.golang.org/grpc v1.64.1 // indirect + google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 569e555..5d70b97 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,96 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.115.0 h1:CnFSK6Xo3lDYRoBKEcAtia6VSC837/ZkJuRduSFnr14= +cloud.google.com/go/auth v0.7.2 h1:uiha352VrCDMXg+yoBtaD0tUF4Kv9vrtrWPYXwutnDE= +cloud.google.com/go/auth v0.7.2/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJJQAbs= +cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI= +cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I= +cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= +cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= +cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= +cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY= +github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 h1:tW1/Rkad38LA15X4UQtjXZXNKsCgkshC3EbmcUmghTg= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3/go.mod h1:UbnqO+zjqk3uIt9yCACHJ9IVNhyhOCnYk8yA19SAWrM= +github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90= +github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ= +github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE= +github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= +github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -10,3 +101,90 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.189.0 h1:equMo30LypAkdkLMBqfeIqtyAnlyig1JSZArl4XPwdI= +google.golang.org/api v0.189.0/go.mod h1:FLWGJKb0hb+pU2j+rJqwbnsF+ym+fQs73rbJ+KAUgy8= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20240722135656-d784300faade h1:lKFsS7wpngDgSCeFn7MoLy+wBDQZ1UQIJD4UNM1Qvkg= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= +google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go index b48ff1e..bd78b51 100644 --- a/internal/requestconfig/requestconfig.go +++ b/internal/requestconfig/requestconfig.go @@ -332,6 +332,7 @@ func (cfg *RequestConfig) Execute() (err error) { handler = applyMiddleware(cfg.Middlewares[i], handler) } + var req *http.Request var res *http.Response for retryCount := 0; retryCount <= cfg.MaxRetries; retryCount += 1 { ctx := cfg.Request.Context() @@ -341,7 +342,8 @@ func (cfg *RequestConfig) Execute() (err error) { defer cancel() } - res, err = handler(cfg.Request.Clone(ctx)) + req = cfg.Request.Clone(ctx) + res, err = handler(req) if ctx != nil && ctx.Err() != nil { return ctx.Err() } @@ -393,7 +395,7 @@ func (cfg *RequestConfig) Execute() (err error) { res.Body = io.NopCloser(bytes.NewBuffer(contents)) // Load the contents into the error format if it is provided. - aerr := apierror.Error{Request: cfg.Request, Response: res, StatusCode: res.StatusCode} + aerr := apierror.Error{Request: req, Response: res, StatusCode: res.StatusCode} err = aerr.UnmarshalJSON(contents) if err != nil { return err @@ -439,7 +441,7 @@ func (cfg *RequestConfig) Execute() (err error) { err = json.NewDecoder(bytes.NewReader(contents)).Decode(cfg.ResponseBodyInto) if err != nil { - return fmt.Errorf("error parsing response json: %w", err) + err = fmt.Errorf("error parsing response json: %w", err) } return nil diff --git a/internal/version.go b/internal/version.go index 4ff68e4..1e49ee4 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,4 +2,4 @@ package internal -const PackageVersion = "0.0.1-alpha.0" // x-release-please-version +const PackageVersion = "0.0.1-alpha.0" diff --git a/message.go b/message.go index c0be88e..4863f83 100644 --- a/message.go +++ b/message.go @@ -4,6 +4,8 @@ package anthropic import ( "context" + "encoding/json" + "fmt" "net/http" "reflect" @@ -72,12 +74,11 @@ func (r *MessageService) NewStreaming(ctx context.Context, body MessageNewParams } type ContentBlock struct { - Type ContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - // This field can have the runtime type of [interface{}]. - Input interface{} `json:"input,required"` + Type ContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input,required"` JSON contentBlockJSON `json:"-"` union ContentBlockUnion } @@ -156,6 +157,17 @@ type ImageBlockParam struct { Type param.Field[ImageBlockParamType] `json:"type,required"` } +func NewImageBlockBase64(mediaType string, encodedData string) ImageBlockParam { + return ImageBlockParam{ + Type: F(ImageBlockParamTypeImage), + Source: F(ImageBlockParamSource{ + Type: F(ImageBlockParamSourceTypeBase64), + Data: F(encodedData), + MediaType: F(ImageBlockParamSourceMediaType(mediaType)), + }), + } +} + func (r ImageBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -257,6 +269,65 @@ func (r InputJSONDeltaType) IsKnown() bool { return false } +// Accumulate builds up the Message incrementally from a MessageStreamEvent. The Message then can be used as +// any other Message, except with the caveat that the Message.JSON field which normally can be used to inspect +// the JSON sent over the network may not be populated fully. +// +// message := anthropic.Message{} +// for stream.Next() { +// event := stream.Current() +// message.Accumulate(event) +// } +func (a *Message) Accumulate(event MessageStreamEvent) error { + if a == nil { + *a = Message{} + } + + switch event := event.AsUnion().(type) { + case MessageStartEvent: + *a = event.Message + + case MessageDeltaEvent: + a.StopReason = MessageStopReason(event.Delta.StopReason) + a.JSON.StopReason = event.Delta.JSON.StopReason + a.StopSequence = event.Delta.StopSequence + a.JSON.StopSequence = event.Delta.JSON.StopSequence + a.Usage.OutputTokens = event.Usage.OutputTokens + a.Usage.JSON.OutputTokens = event.Usage.JSON.OutputTokens + + case MessageStopEvent: + + case ContentBlockStartEvent: + a.Content = append(a.Content, ContentBlock{}) + err := a.Content[len(a.Content)-1].UnmarshalJSON([]byte(event.ContentBlock.JSON.RawJSON())) + if err != nil { + return err + } + + case ContentBlockDeltaEvent: + if len(a.Content) == 0 { + return fmt.Errorf("received event of type %s but there was no content block", event.Type) + } + switch delta := event.Delta.AsUnion().(type) { + case TextDelta: + a.Content[len(a.Content)-1].Text += delta.Text + case InputJSONDelta: + cb := &a.Content[len(a.Content)-1] + if string(cb.Input) == "{}" { + cb.Input = json.RawMessage{} + } + cb.Input = append(cb.Input, []byte(delta.PartialJSON)...) + } + + case ContentBlockStopEvent: + if len(a.Content) == 0 { + return fmt.Errorf("received event of type %s but there was no content block", event.Type) + } + } + + return nil +} + type Message struct { // Unique object identifier. // @@ -356,6 +427,39 @@ type messageJSON struct { ExtraFields map[string]apijson.Field } +func (r *Message) ToParam() MessageParam { + content := []MessageParamContentUnion{} + + for _, block := range r.Content { + content = append(content, MessageParamContent{ + Type: F(MessageParamContentType(block.Type)), + ID: param.Field[string]{ + Value: block.ID, + Present: !block.JSON.ID.IsNull(), + }, + Text: param.Field[string]{ + Value: block.Text, + Present: !block.JSON.Text.IsNull(), + }, + Name: param.Field[string]{ + Value: block.Name, + Present: !block.JSON.Name.IsNull(), + }, + Input: param.Field[interface{}]{ + Value: block.Input, + Present: len(block.Input) > 0 && !block.JSON.Input.IsNull(), + }, + }) + } + + message := MessageParam{ + Role: F(MessageParamRole(r.Role)), + Content: F(content), + } + + return message +} + func (r *Message) UnmarshalJSON(data []byte) (err error) { return apijson.UnmarshalRoot(data, r) } @@ -453,6 +557,20 @@ type MessageParam struct { Role param.Field[MessageParamRole] `json:"role,required"` } +func NewUserMessage(blocks ...MessageParamContentUnion) MessageParam { + return MessageParam{ + Role: F(MessageParamRoleUser), + Content: F(blocks), + } +} + +func NewAssistantMessage(blocks ...MessageParamContentUnion) MessageParam { + return MessageParam{ + Role: F(MessageParamRoleAssistant), + Content: F(blocks), + } +} + func (r MessageParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -674,12 +792,11 @@ func (r contentBlockStartEventJSON) RawJSON() string { func (r ContentBlockStartEvent) implementsMessageStreamEvent() {} type ContentBlockStartEventContentBlock struct { - Type ContentBlockStartEventContentBlockType `json:"type,required"` - Text string `json:"text"` - ID string `json:"id"` - Name string `json:"name"` - // This field can have the runtime type of [interface{}]. - Input interface{} `json:"input,required"` + Type ContentBlockStartEventContentBlockType `json:"type,required"` + Text string `json:"text"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input,required"` JSON contentBlockStartEventContentBlockJSON `json:"-"` union ContentBlockStartEventContentBlockUnion } @@ -1145,6 +1262,13 @@ type TextBlockParam struct { Type param.Field[TextBlockParamType] `json:"type,required"` } +func NewTextBlock(text string) TextBlockParam { + return TextBlockParam{ + Text: F(text), + Type: F(TextBlockParamTypeText), + } +} + func (r TextBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1210,8 +1334,8 @@ type ToolParam struct { // // This defines the shape of the `input` that your tool accepts and that the model // will produce. - InputSchema param.Field[ToolInputSchemaParam] `json:"input_schema,required"` - Name param.Field[string] `json:"name,required"` + InputSchema param.Field[interface{}] `json:"input_schema,required"` + Name param.Field[string] `json:"name,required"` // Description of what this tool does. // // Tool descriptions should be as detailed as possible. The more information that @@ -1225,34 +1349,6 @@ func (r ToolParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } -// [JSON schema](https://json-schema.org/) for this tool's input. -// -// This defines the shape of the `input` that your tool accepts and that the model -// will produce. -type ToolInputSchemaParam struct { - Type param.Field[ToolInputSchemaType] `json:"type,required"` - Properties param.Field[interface{}] `json:"properties"` - ExtraFields map[string]interface{} `json:"-,extras"` -} - -func (r ToolInputSchemaParam) MarshalJSON() (data []byte, err error) { - return apijson.MarshalRoot(r) -} - -type ToolInputSchemaType string - -const ( - ToolInputSchemaTypeObject ToolInputSchemaType = "object" -) - -func (r ToolInputSchemaType) IsKnown() bool { - switch r { - case ToolInputSchemaTypeObject: - return true - } - return false -} - type ToolResultBlockParam struct { ToolUseID param.Field[string] `json:"tool_use_id,required"` Type param.Field[ToolResultBlockParamType] `json:"type,required"` @@ -1260,6 +1356,15 @@ type ToolResultBlockParam struct { IsError param.Field[bool] `json:"is_error"` } +func NewToolResultBlock(toolUseID string, content string, isError bool) ToolResultBlockParam { + return ToolResultBlockParam{ + Type: F(ToolResultBlockParamTypeToolResult), + ToolUseID: F(toolUseID), + Content: F([]ToolResultBlockParamContentUnion{NewTextBlock(content)}), + IsError: F(isError), + } +} + func (r ToolResultBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } @@ -1314,7 +1419,7 @@ func (r ToolResultBlockParamContentType) IsKnown() bool { type ToolUseBlock struct { ID string `json:"id,required"` - Input interface{} `json:"input,required"` + Input json.RawMessage `json:"input,required"` Name string `json:"name,required"` Type ToolUseBlockType `json:"type,required"` JSON toolUseBlockJSON `json:"-"` @@ -1363,6 +1468,15 @@ type ToolUseBlockParam struct { Type param.Field[ToolUseBlockParamType] `json:"type,required"` } +func NewToolUseBlockParam(id string, name string, input interface{}) ToolUseBlockParam { + return ToolUseBlockParam{ + ID: F(id), + Input: F(input), + Name: F(name), + Type: F(ToolUseBlockParamTypeToolUse), + } +} + func (r ToolUseBlockParam) MarshalJSON() (data []byte, err error) { return apijson.MarshalRoot(r) } diff --git a/message_test.go b/message_test.go index 0cb7067..72b2d63 100644 --- a/message_test.go +++ b/message_test.go @@ -47,9 +47,9 @@ func TestMessageNewWithOptionalParams(t *testing.T) { Tools: anthropic.F([]anthropic.ToolParam{{ Description: anthropic.F("Get the current weather in a given location"), Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ + InputSchema: anthropic.F[interface{}](map[string]interface{}{ + "type": "object", + "properties": map[string]interface{}{ "location": map[string]interface{}{ "description": "The city and state, e.g. San Francisco, CA", "type": "string", @@ -58,39 +58,7 @@ func TestMessageNewWithOptionalParams(t *testing.T) { "description": "Unit for the output - one of (celsius, fahrenheit)", "type": "string", }, - }), - }), - }, { - Description: anthropic.F("Get the current weather in a given location"), - Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ - "location": map[string]interface{}{ - "description": "The city and state, e.g. San Francisco, CA", - "type": "string", - }, - "unit": map[string]interface{}{ - "description": "Unit for the output - one of (celsius, fahrenheit)", - "type": "string", - }, - }), - }), - }, { - Description: anthropic.F("Get the current weather in a given location"), - Name: anthropic.F("x"), - InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{ - Type: anthropic.F(anthropic.ToolInputSchemaTypeObject), - Properties: anthropic.F[any](map[string]interface{}{ - "location": map[string]interface{}{ - "description": "The city and state, e.g. San Francisco, CA", - "type": "string", - }, - "unit": map[string]interface{}{ - "description": "Unit for the output - one of (celsius, fahrenheit)", - "type": "string", - }, - }), + }, }), }}), TopK: anthropic.F(int64(5)), diff --git a/vertex/vertex.go b/vertex/vertex.go new file mode 100644 index 0000000..baeb7c3 --- /dev/null +++ b/vertex/vertex.go @@ -0,0 +1,99 @@ +package vertex + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + + "golang.org/x/oauth2/google" + "google.golang.org/api/option" + "google.golang.org/api/transport" + + "github.com/anthropics/anthropic-sdk-go/internal/requestconfig" + sdkoption "github.com/anthropics/anthropic-sdk-go/option" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +const DefaultVersion = "vertex-2023-10-16" + +// WithGoogleAuth returns a request option which loads the [Application Default Credentials] for Google Vertex AI and registers +// middleware that intercepts requests to the Messages API. +// +// If you already have a [*google.Credentials], it is recommended that you instead call [WithCredentials] directly. +// +// [Application Default Credentials]: https://cloud.google.com/docs/authentication/application-default-credentials +func WithGoogleAuth(ctx context.Context, region string, projectID string, scopes ...string) sdkoption.RequestOption { + if region == "" { + panic("region must be provided") + } + creds, err := google.FindDefaultCredentials(ctx, scopes...) + if err != nil { + panic(fmt.Errorf("failed to find default credentials: %v", err)) + } + return WithCredentials(ctx, region, projectID, creds) +} + +// WithCredentials returns a request option which uses the provided credentials for Google Vertex AI and registers middleware that +// intercepts request to the Messages API. +func WithCredentials(ctx context.Context, region string, projectID string, creds *google.Credentials) sdkoption.RequestOption { + client, _, err := transport.NewHTTPClient(ctx, option.WithTokenSource(creds.TokenSource)) + if err != nil { + panic(fmt.Errorf("failed to create HTTP client: %v", err)) + } + middleware := vertexMiddleware(region, projectID) + + return func(rc *requestconfig.RequestConfig) error { + return rc.Apply( + sdkoption.WithBaseURL(fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1", region)), + sdkoption.WithMiddleware(middleware), + sdkoption.WithHTTPClient(client), + ) + } +} + +func vertexMiddleware(region, projectID string) sdkoption.Middleware { + return func(r *http.Request, next sdkoption.MiddlewareNext) (*http.Response, error) { + if r.Body != nil { + body, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + r.Body.Close() + + if !gjson.GetBytes(body, "anthropic_version").Exists() { + body, _ = sjson.SetBytes(body, "anthropic_version", DefaultVersion) + } + + if r.URL.Path == "/v1/messages" && r.Method == http.MethodPost { + if projectID == "" { + return nil, fmt.Errorf("no projectId was given and it could not be resolved from credentials") + } + + model := gjson.GetBytes(body, "model").String() + stream := gjson.GetBytes(body, "stream").Bool() + + body, _ = sjson.DeleteBytes(body, "model") + + specifier := "rawPredict" + if stream { + specifier = "streamRawPredict" + } + + r.URL.Path = fmt.Sprintf("/v1/projects/%s/locations/%s/publishers/anthropic/models/%s:%s", projectID, region, model, specifier) + } + + reader := bytes.NewReader(body) + r.Body = io.NopCloser(reader) + r.GetBody = func() (io.ReadCloser, error) { + _, err := reader.Seek(0, 0) + return io.NopCloser(reader), err + } + r.ContentLength = int64(len(body)) + } + + return next(r) + } +}