instructionFromContextFiles reconstructs the formatted instruction string from persisted context-file parts. This is used on non-first turns so the instruction can be re-injected after compaction without re-dialing the workspace agent.
( messages []database.ChatMessage, )
| 92 | // turns so the instruction can be re-injected after compaction |
| 93 | // without re-dialing the workspace agent. |
| 94 | func instructionFromContextFiles( |
| 95 | messages []database.ChatMessage, |
| 96 | ) string { |
| 97 | filterAgentID, filterByAgent := latestContextAgentID(messages) |
| 98 | var contextParts []codersdk.ChatMessagePart |
| 99 | var os, dir string |
| 100 | for _, msg := range messages { |
| 101 | if !msg.Content.Valid || |
| 102 | !bytes.Contains(msg.Content.RawMessage, []byte(`"context-file"`)) { |
| 103 | continue |
| 104 | } |
| 105 | var parts []codersdk.ChatMessagePart |
| 106 | if err := json.Unmarshal(msg.Content.RawMessage, &parts); err != nil { |
| 107 | continue |
| 108 | } |
| 109 | for _, part := range parts { |
| 110 | if part.Type != codersdk.ChatMessagePartTypeContextFile { |
| 111 | continue |
| 112 | } |
| 113 | if filterByAgent && part.ContextFileAgentID.Valid && |
| 114 | part.ContextFileAgentID.UUID != filterAgentID { |
| 115 | continue |
| 116 | } |
| 117 | if part.ContextFileOS != "" { |
| 118 | os = part.ContextFileOS |
| 119 | } |
| 120 | if part.ContextFileDirectory != "" { |
| 121 | dir = part.ContextFileDirectory |
| 122 | } |
| 123 | if part.ContextFileContent != "" { |
| 124 | contextParts = append(contextParts, part) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return formatSystemInstructions(os, dir, contextParts) |
| 129 | } |
| 130 | |
| 131 | // hasPersistedInstructionFiles reports whether messages include a |
| 132 | // persisted context-file part that should suppress another baseline |