Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion aibridge/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
_ Recorder = &AsyncRecorder{}
)

// WrappedRecorder is a convenience struct which implements RecorderClient and resolves a client before calling each method.
// WrappedRecorder is a convenience struct which implements Recorder and resolves a client before calling each method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the interface used to be called RecorderClient and that the name drifted, leaving this comment out of date. This caused confusion for me, so I brought it back in line with the interface name.

// It also sets the start/creation time of each record.
type WrappedRecorder struct {
logger slog.Logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
"github.com/coder/coder/v2/coderd/aibridged/proto"
)

var _ aibridge.Recorder = &recorderTranslation{}
var _ aibridge.Recorder = &DRPCRecorder{}

// recorderTranslation satisfies the aibridge.Recorder interface and translates calls into dRPC calls to aibridgedserver.
type recorderTranslation struct {
// DRPCRecorder satisfies the aibridge.Recorder interface and translates calls into dRPC calls to aibridgedserver.
type DRPCRecorder struct {
apiKeyID string
client proto.DRPCRecorderClient
}

func (t *recorderTranslation) RecordInterception(ctx context.Context, req *aibridge.InterceptionRecord) error {
func (t *DRPCRecorder) RecordInterception(ctx context.Context, req *aibridge.InterceptionRecord) error {
_, err := t.client.RecordInterception(ctx, &proto.RecordInterceptionRequest{
Id: req.ID,
ApiKeyId: t.apiKeyID,
Expand All @@ -44,7 +44,7 @@ func (t *recorderTranslation) RecordInterception(ctx context.Context, req *aibri
return err
}

func (t *recorderTranslation) RecordInterceptionEnded(ctx context.Context, req *aibridge.InterceptionRecordEnded) error {
func (t *DRPCRecorder) RecordInterceptionEnded(ctx context.Context, req *aibridge.InterceptionRecordEnded) error {
endedReq := &proto.RecordInterceptionEndedRequest{
Id: req.ID,
EndedAt: timestamppb.New(req.EndedAt),
Expand All @@ -61,7 +61,7 @@ func (t *recorderTranslation) RecordInterceptionEnded(ctx context.Context, req *
return err
}

func (t *recorderTranslation) RecordPromptUsage(ctx context.Context, req *aibridge.PromptUsageRecord) error {
func (t *DRPCRecorder) RecordPromptUsage(ctx context.Context, req *aibridge.PromptUsageRecord) error {
_, err := t.client.RecordPromptUsage(ctx, &proto.RecordPromptUsageRequest{
InterceptionId: req.InterceptionID,
MsgId: req.MsgID,
Expand All @@ -72,7 +72,7 @@ func (t *recorderTranslation) RecordPromptUsage(ctx context.Context, req *aibrid
return err
}

func (t *recorderTranslation) RecordTokenUsage(ctx context.Context, req *aibridge.TokenUsageRecord) error {
func (t *DRPCRecorder) RecordTokenUsage(ctx context.Context, req *aibridge.TokenUsageRecord) error {
merged := req.Metadata
if merged == nil {
merged = aibridge.Metadata{}
Expand All @@ -96,7 +96,7 @@ func (t *recorderTranslation) RecordTokenUsage(ctx context.Context, req *aibridg
return err
}

func (t *recorderTranslation) RecordToolUsage(ctx context.Context, req *aibridge.ToolUsageRecord) error {
func (t *DRPCRecorder) RecordToolUsage(ctx context.Context, req *aibridge.ToolUsageRecord) error {
serialized, err := json.Marshal(req.Args)
if err != nil {
return xerrors.Errorf("serialize tool %q args: %w", req.Tool, err)
Expand All @@ -123,7 +123,7 @@ func (t *recorderTranslation) RecordToolUsage(ctx context.Context, req *aibridge
return err
}

func (t *recorderTranslation) RecordModelThought(ctx context.Context, req *aibridge.ModelThoughtRecord) error {
func (t *DRPCRecorder) RecordModelThought(ctx context.Context, req *aibridge.ModelThoughtRecord) error {
_, err := t.client.RecordModelThought(ctx, &proto.RecordModelThoughtRequest{
InterceptionId: req.InterceptionID,
Content: req.Content,
Expand Down
22 changes: 13 additions & 9 deletions coderd/aibridged/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,20 @@ func (p *CachedBridgePool) Acquire(ctx context.Context, req Request, clientFn Cl

span.AddEvent("cache_miss")
providerVersion := p.providerVersion.Load()
recorder := aibridge.NewRecorder(p.logger.Named("recorder"), p.tracer, func(clientCtx context.Context) (aibridge.Recorder, error) {
// The recorder outlives this Acquire call, so the client is acquired
// against the context of the record call being served.
client, err := clientFn(clientCtx)
if err != nil {
return nil, xerrors.Errorf("acquire client: %w", err)
}
recorder := aibridge.NewRecorder(
p.logger.Named("recorder"),
p.tracer,
func(clientCtx context.Context) (aibridge.Recorder, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(since parameters where cleaned up) very nit: I'd extract func to variable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was considering it. will do something like it in a follow up. I still have questions about why we fetch a new client for every call. Once I understand that, I will make the necessary change here.

// The recorder outlives this Acquire call, so the client is acquired
// against the context of the record call being served.
client, err := clientFn(clientCtx)
if err != nil {
return nil, xerrors.Errorf("acquire client: %w", err)
}

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

// Slow path.
// Creating an *aibridge.RequestBridge may take some time, so gate all subsequent callers behind the initial request and return the resulting value.
Expand Down
Loading