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

Function InsertSystem

coderd/x/chatd/chatprompt/chatprompt.go:273–299  ·  view source on GitHub ↗

InsertSystem inserts a system message after the existing system block and before the first non-system message.

(prompt []fantasy.Message, instruction string)

Source from the content-addressed store, hash-verified

271// InsertSystem inserts a system message after the existing system
272// block and before the first non-system message.
273func InsertSystem(prompt []fantasy.Message, instruction string) []fantasy.Message {
274 instruction = strings.TrimSpace(instruction)
275 if instruction == "" {
276 return prompt
277 }
278
279 systemMessage := fantasy.Message{
280 Role: fantasy.MessageRoleSystem,
281 Content: []fantasy.MessagePart{
282 fantasy.TextPart{Text: instruction},
283 },
284 }
285
286 out := make([]fantasy.Message, 0, len(prompt)+1)
287 inserted := false
288 for _, message := range prompt {
289 if !inserted && message.Role != fantasy.MessageRoleSystem {
290 out = append(out, systemMessage)
291 inserted = true
292 }
293 out = append(out, message)
294 }
295 if !inserted {
296 out = append(out, systemMessage)
297 }
298 return out
299}
300
301// AppendUser appends an instruction as a user message at the end of
302// the prompt.

Calls

no outgoing calls