package connectors import ( "context" "google.golang.org/protobuf/proto" "github.com/samcharles93/archie-core/internal/platform/auditjson" ) // Call is one call core made to a connector or MCP server: the request it sent // and the response it received, rendered as JSON. Credentials are removed and // an oversized body is capped, so the record is safe to keep. type Call struct { Connector string `json:"connector"` Method string `json:"method"` Request auditjson.Payload `json:"request"` Response auditjson.Payload `json:"response,omitzero"` Error string `json:"error,omitzero"` } // Recorder keeps each call core makes to a connector. The module that owns the // record supplies one on the context with WithRecorder; without one nothing is // recorded. type Recorder interface { RecordConnectorCall(ctx context.Context, call Call) } type recorderKey struct{} // WithRecorder makes every connector call made with ctx report to r. func WithRecorder(ctx context.Context, r Recorder) context.Context { return context.WithValue(ctx, recorderKey{}, r) } // record hands c to the recorder on ctx, if there is one. func record(ctx context.Context, c Call) { if r, ok := ctx.Value(recorderKey{}).(Recorder); ok && r != nil { r.RecordConnectorCall(ctx, c) } } // recordProto records one protobuf wire call. The response is rendered only // when the call returned one; a transport error is recorded instead. func recordProto(ctx context.Context, connector, method string, req, resp proto.Message, err error) { c := Call{Connector: connector, Method: method, Request: auditjson.FromProto(req)} switch { case err != nil: c.Error = err.Error() case resp != nil: c.Response = auditjson.FromProto(resp) } record(ctx, c) }