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

Function BuildLastInjectedContext

coderd/x/chatd/contextparts.go:125–153  ·  view source on GitHub ↗

BuildLastInjectedContext filters parts down to non-empty context-file and skill parts, strips their internal fields, and marshals the result for LastInjectedContext. A nil or fully filtered input returns an invalid NullRawMessage.

(
	parts []codersdk.ChatMessagePart,
)

Source from the content-addressed store, hash-verified

123// result for LastInjectedContext. A nil or fully filtered input returns
124// an invalid NullRawMessage.
125func BuildLastInjectedContext(
126 parts []codersdk.ChatMessagePart,
127) (pqtype.NullRawMessage, error) {
128 if parts == nil {
129 return pqtype.NullRawMessage{Valid: false}, nil
130 }
131
132 filtered := FilterContextParts(parts, false)
133 if len(filtered) == 0 {
134 return pqtype.NullRawMessage{Valid: false}, nil
135 }
136
137 stripped := make([]codersdk.ChatMessagePart, 0, len(filtered))
138 for _, part := range filtered {
139 cp := part
140 cp.StripInternal()
141 stripped = append(stripped, cp)
142 }
143
144 raw, err := json.Marshal(stripped)
145 if err != nil {
146 return pqtype.NullRawMessage{}, xerrors.Errorf(
147 "marshal injected context: %w",
148 err,
149 )
150 }
151
152 return pqtype.NullRawMessage{RawMessage: raw, Valid: true}, nil
153}

Calls 4

FilterContextPartsFunction · 0.85
StripInternalMethod · 0.80
MarshalMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected