MCPcopy Index your code
hub / github.com/coder/coder / RecordPromptUsage

Method RecordPromptUsage

coderd/aibridgedserver/aibridgedserver.go:285–325  ·  view source on GitHub ↗
(ctx context.Context, in *proto.RecordPromptUsageRequest)

Source from the content-addressed store, hash-verified

283}
284
285func (s *Server) RecordPromptUsage(ctx context.Context, in *proto.RecordPromptUsageRequest) (*proto.RecordPromptUsageResponse, error) {
286 //nolint:gocritic // AIBridged has specific authz rules.
287 ctx = dbauthz.AsAIBridged(ctx)
288
289 intcID, err := uuid.Parse(in.GetInterceptionId())
290 if err != nil {
291 return nil, xerrors.Errorf("failed to parse interception_id %q: %w", in.GetInterceptionId(), err)
292 }
293
294 metadata := metadataToMap(in.GetMetadata())
295
296 if s.structuredLogging {
297 s.logger.Info(ctx, InterceptionLogMarker,
298 slog.F("record_type", "prompt_usage"),
299 slog.F("interception_id", intcID.String()),
300 slog.F("msg_id", in.GetMsgId()),
301 slog.F("prompt", in.GetPrompt()),
302 slog.F("created_at", in.GetCreatedAt().AsTime()),
303 slog.F("metadata", metadata),
304 )
305 }
306
307 out, err := json.Marshal(metadata)
308 if err != nil {
309 s.logger.Warn(ctx, "failed to marshal aibridge metadata from proto to JSON", slog.F("metadata", in), slog.Error(err))
310 }
311
312 _, err = s.store.InsertAIBridgeUserPrompt(ctx, database.InsertAIBridgeUserPromptParams{
313 ID: uuid.New(),
314 InterceptionID: intcID,
315 ProviderResponseID: in.GetMsgId(),
316 Prompt: in.GetPrompt(),
317 Metadata: out,
318 CreatedAt: in.GetCreatedAt().AsTime(),
319 })
320 if err != nil {
321 return nil, xerrors.Errorf("insert user prompt: %w", err)
322 }
323
324 return &proto.RecordPromptUsageResponse{}, nil
325}
326
327func (s *Server) RecordToolUsage(ctx context.Context, in *proto.RecordToolUsageRequest) (*proto.RecordToolUsageResponse, error) {
328 //nolint:gocritic // AIBridged has specific authz rules.

Callers 1

TestStructuredLoggingFunction · 0.95

Calls 15

AsAIBridgedFunction · 0.92
metadataToMapFunction · 0.85
GetPromptMethod · 0.80
ParseMethod · 0.65
NewMethod · 0.65
GetInterceptionIdMethod · 0.45
ErrorfMethod · 0.45
GetMetadataMethod · 0.45
InfoMethod · 0.45
StringMethod · 0.45
GetMsgIdMethod · 0.45

Tested by 1

TestStructuredLoggingFunction · 0.76