| 283 | } |
| 284 | |
| 285 | func (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 | |
| 327 | func (s *Server) RecordToolUsage(ctx context.Context, in *proto.RecordToolUsageRequest) (*proto.RecordToolUsageResponse, error) { |
| 328 | //nolint:gocritic // AIBridged has specific authz rules. |