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

Function marshalForProto

coderd/aibridged/translator.go:129–159  ·  view source on GitHub ↗

marshalForProto will attempt to convert from aibridge.Metadata into a proto-friendly map[string]*anypb.Any. If any marshaling fails, rather return a map with the error details since we don't want to fail Record* funcs if metadata can't encode, since it's, well, metadata.

(in aibridge.Metadata)

Source from the content-addressed store, hash-verified

127// If any marshaling fails, rather return a map with the error details since we don't want to fail Record* funcs if metadata can't encode,
128// since it's, well, metadata.
129func marshalForProto(in aibridge.Metadata) map[string]*anypb.Any {
130 out := make(map[string]*anypb.Any, len(in))
131 if len(in) == 0 {
132 return out
133 }
134
135 // Instead of returning error, just encode error into metadata.
136 encodeErr := func(err error) map[string]*anypb.Any {
137 errVal, _ := anypb.New(structpb.NewStringValue(err.Error()))
138 mdVal, _ := anypb.New(structpb.NewStringValue(fmt.Sprintf("%+v", in)))
139 return map[string]*anypb.Any{
140 "error": errVal,
141 "metadata": mdVal,
142 }
143 }
144
145 for k, v := range in {
146 sv, err := structpb.NewValue(v)
147 if err != nil {
148 return encodeErr(err)
149 }
150
151 av, err := anypb.New(sv)
152 if err != nil {
153 return encodeErr(err)
154 }
155
156 out[k] = av
157 }
158 return out
159}

Callers 5

RecordInterceptionMethod · 0.85
RecordPromptUsageMethod · 0.85
RecordTokenUsageMethod · 0.85
RecordToolUsageMethod · 0.85
RecordModelThoughtMethod · 0.85

Calls 2

NewMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected