Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: record user agent in interception metadata#20595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
pawbana wants to merge1 commit intopb/aibridge-intc-api-key-graphite
base:pb/aibridge-intc-api-key-graphite
Choose a base branch
Loading
from10-30-pb-aibridge-intc-user-agent-metadata
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletionsenterprise/aibridged/aibridged_integration_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ package aibridged_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand DownExpand Up@@ -208,10 +209,13 @@ func TestIntegration(t *testing.T) {
}
]
}`))
userAgent:="userAgent123"
require.NoError(t,err,"make request to test server")
req.Header.Add("Authorization","Bearer "+apiKey.Key)
req.Header.Add("Accept","application/json")
req.Header.Add("User-Agent",userAgent)

require.Equal(t,userAgent,req.UserAgent())
// When: aibridged handles the request.
rec:=httptest.NewRecorder()
srv.ServeHTTP(rec,req)
Expand All@@ -234,6 +238,13 @@ func TestIntegration(t *testing.T) {
require.True(t,intc0.StartedAt.Before(intc0.EndedAt.Time))
require.Less(t,intc0.EndedAt.Time.Sub(intc0.StartedAt),5*time.Second)

require.True(t,intc0.Metadata.Valid)
meta:=map[string]any{}
err=json.Unmarshal(intc0.Metadata.RawMessage,&meta)
require.NoError(t,err)
require.Contains(t,meta,"user-agent")
require.Equal(t,userAgent,meta["user-agent"])

prompts,err:=db.GetAIBridgeUserPromptsByInterceptionID(ctx,interceptions[0].ID)
require.NoError(t,err)
require.Len(t,prompts,1)
Expand Down
3 changes: 2 additions & 1 deletionenterprise/aibridged/http.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,9 +67,10 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
}

handler,err:=s.GetRequestHandler(ctx,Request{
SessionKey:key,
APIKeyID:resp.ApiKeyId,
InitiatorID:id,
SessionKey:key,
UserAgent:r.UserAgent(),
})
iferr!=nil {
logger.Warn(ctx,"failed to acquire request handler",slog.Error(err))
Expand Down
6 changes: 5 additions & 1 deletionenterprise/aibridged/pool.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -129,7 +129,11 @@ func (p *CachedBridgePool) Acquire(ctx context.Context, req Request, clientFn Cl
returnnil,xerrors.Errorf("acquire client: %w",err)
}

return&recorderTranslation{apiKeyID:req.APIKeyID,client:client},nil
return&recorderTranslation{
apiKeyID:req.APIKeyID,
client:client,
userAgent:req.UserAgent,
},nil
})

// Slow path.
Expand Down
3 changes: 2 additions & 1 deletionenterprise/aibridged/pool_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,9 +56,10 @@ func TestPool(t *testing.T) {

// ...and it will return it when acquired again.
instB,err:=pool.Acquire(t.Context(), aibridged.Request{
SessionKey:"key",
SessionKey:"differentkey",
InitiatorID:id,
APIKeyID:apiKeyID1.String(),
UserAgent:"some user-agent",
},clientFn,newMockMCPFactory(mcpProxy))
require.NoError(t,err,"acquire pool instance")
require.Same(t,inst,instB)
Expand Down
3 changes: 2 additions & 1 deletionenterprise/aibridged/request.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,8 @@ package aibridged
import"github.com/google/uuid"

typeRequeststruct {
SessionKeystring
APIKeyIDstring
InitiatorID uuid.UUID
SessionKeystring
UserAgentstring
}
16 changes: 14 additions & 2 deletionsenterprise/aibridged/translator.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,15 +16,27 @@ import (
"github.com/coder/aibridge"
)

const (
MetaKeyUserAgent="user-agent"
)

var_ aibridge.Recorder=&recorderTranslation{}

// recorderTranslation satisfies the aibridge.Recorder interface and translates calls into dRPC calls to aibridgedserver.
typerecorderTranslationstruct {
apiKeyIDstring
client proto.DRPCRecorderClient
apiKeyIDstring
client proto.DRPCRecorderClient
userAgentstring
}

func (t*recorderTranslation)RecordInterception(ctx context.Context,req*aibridge.InterceptionRecord)error {
ift.userAgent!="" {
ifreq.Metadata==nil {
req.Metadata=map[string]any{}
}
req.Metadata[MetaKeyUserAgent]=t.userAgent
}

_,err:=t.client.RecordInterception(ctx,&proto.RecordInterceptionRequest{
Id:req.ID,
ApiKeyId:t.apiKeyID,
Expand Down
158 changes: 158 additions & 0 deletionsenterprise/aibridged/translator_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
package aibridged//nolint:testpackage

import (
"context"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/coder/aibridge"
abpb"github.com/coder/coder/v2/enterprise/aibridged/proto"
"github.com/coder/coder/v2/testutil"
)

typemockClientstruct {
abpb.DRPCRecorderClient
got*abpb.RecordInterceptionRequest
}

func (mc*mockClient)RecordInterception(ctx context.Context,in*abpb.RecordInterceptionRequest) (*abpb.RecordInterceptionResponse,error) {
mc.got=in
return&abpb.RecordInterceptionResponse{},nil
}

funcmustAnypbNew(t*testing.T,src proto.Message)*anypb.Any {
ret,err:=anypb.New(src)
require.NoError(t,err)
returnret
}

funcTestRecordInterception(t*testing.T) {
t.Parallel()

tests:= []struct {
namestring
apiKeyIDstring
userAgentstring
in aibridge.InterceptionRecord
expect*abpb.RecordInterceptionRequest
}{
{
name:"simple",
apiKeyID:"key",
userAgent:"user-agent",
in: aibridge.InterceptionRecord{
ID: uuid.UUID{1}.String(),
InitiatorID: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]any{"some":"data"},
StartedAt:time.UnixMicro(123),
},
expect:&abpb.RecordInterceptionRequest{
Id: uuid.UUID{1}.String(),
ApiKeyId:"key",
InitiatorId: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]*anypb.Any{
"some":mustAnypbNew(t,structpb.NewStringValue("data")),
MetaKeyUserAgent:mustAnypbNew(t,structpb.NewStringValue("user-agent")),
},
StartedAt:timestamppb.New(time.UnixMicro(123)),
},
},
{
name:"empty-user-agent",
apiKeyID:"key",
in: aibridge.InterceptionRecord{
ID: uuid.UUID{1}.String(),
InitiatorID: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]any{"some":"data"},
StartedAt:time.UnixMicro(123),
},
expect:&abpb.RecordInterceptionRequest{
Id: uuid.UUID{1}.String(),
ApiKeyId:"key",
InitiatorId: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]*anypb.Any{
"some":mustAnypbNew(t,structpb.NewStringValue("data")),
},
StartedAt:timestamppb.New(time.UnixMicro(123)),
},
},
{
name:"overrides-user-agent",
apiKeyID:"key",
userAgent:"user-agent",
in: aibridge.InterceptionRecord{
ID: uuid.UUID{1}.String(),
InitiatorID: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]any{MetaKeyUserAgent:"key-already-set"},
StartedAt:time.UnixMicro(123),
},
expect:&abpb.RecordInterceptionRequest{
Id: uuid.UUID{1}.String(),
ApiKeyId:"key",
InitiatorId: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]*anypb.Any{
MetaKeyUserAgent:mustAnypbNew(t,structpb.NewStringValue("user-agent")),
},
StartedAt:timestamppb.New(time.UnixMicro(123)),
},
},
{
name:"user-agent-empty-metadata",
apiKeyID:"key",
userAgent:"user-agent",
in: aibridge.InterceptionRecord{
ID: uuid.UUID{1}.String(),
InitiatorID: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
StartedAt:time.UnixMicro(123),
},
expect:&abpb.RecordInterceptionRequest{
Id: uuid.UUID{1}.String(),
ApiKeyId:"key",
InitiatorId: uuid.UUID{2}.String(),
Provider:"prov",
Model:"model",
Metadata:map[string]*anypb.Any{
MetaKeyUserAgent:mustAnypbNew(t,structpb.NewStringValue("user-agent")),
},
StartedAt:timestamppb.New(time.UnixMicro(123)),
},
},
}

for_,tc:=rangetests {
t.Run(tc.name,func(t*testing.T) {
t.Parallel()
ctx:=testutil.Context(t,testutil.WaitShort)

mc:=&mockClient{}
rt:=&recorderTranslation{
apiKeyID:tc.apiKeyID,
client:mc,
userAgent:tc.userAgent,
}
rt.RecordInterception(ctx,&tc.in)
require.Equal(t,tc.expect,mc.got)
})
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp