parseToolOutput parses raw tool execution output from LLM agents and extracts structured ExecResponse fields. Handles formats like: Exit code: 0 Wall time: 1 seconds Output: codemonkey\john If no metadata is detected, the entire output is treated as stdout.
(raw string)
| 110 | // |
| 111 | // If no metadata is detected, the entire output is treated as stdout. |
| 112 | func parseToolOutput(raw string) *implantpb.ExecResponse { |
| 113 | resp := &implantpb.ExecResponse{} |
| 114 | |
| 115 | lines := strings.Split(raw, "\n") |
| 116 | |
| 117 | // Quick check: does this output contain tool metadata? |
| 118 | hasMetadata := false |
| 119 | for _, line := range lines { |
| 120 | trimmed := strings.TrimSpace(line) |
| 121 | if exitCodeRe.MatchString(trimmed) || |
| 122 | strings.HasPrefix(strings.ToLower(trimmed), "wall time:") || |
| 123 | trimmed == "Output:" { |
| 124 | hasMetadata = true |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if !hasMetadata { |
| 130 | // Plain output, no metadata wrapper. |
| 131 | resp.Stdout = []byte(raw) |
| 132 | return resp |
| 133 | } |
| 134 | |
| 135 | // Parse metadata lines and extract the real output. |
| 136 | var outputLines []string |
| 137 | inOutput := false |
| 138 | for _, line := range lines { |
| 139 | trimmed := strings.TrimSpace(line) |
| 140 | |
| 141 | if inOutput { |
| 142 | outputLines = append(outputLines, line) |
| 143 | continue |
| 144 | } |
| 145 | |
| 146 | if m := exitCodeRe.FindStringSubmatch(trimmed); m != nil { |
| 147 | code, _ := strconv.Atoi(m[1]) |
| 148 | resp.StatusCode = int32(code) |
| 149 | continue |
| 150 | } |
| 151 | if strings.HasPrefix(strings.ToLower(trimmed), "wall time:") { |
| 152 | continue |
| 153 | } |
| 154 | if trimmed == "Output:" { |
| 155 | inOutput = true |
| 156 | continue |
| 157 | } |
| 158 | if strings.HasPrefix(strings.ToLower(trimmed), "stderr:") { |
| 159 | stderr := strings.TrimSpace(strings.TrimPrefix(trimmed, "STDERR:")) |
| 160 | stderr = strings.TrimSpace(strings.TrimPrefix(stderr, "stderr:")) |
| 161 | resp.Stderr = []byte(stderr) |
| 162 | continue |
| 163 | } |
| 164 | // Unknown metadata line or blank, skip. |
| 165 | if trimmed == "" { |
| 166 | continue |
| 167 | } |
| 168 | // Not recognized as metadata, treat as output. |
| 169 | outputLines = append(outputLines, line) |
no outgoing calls
no test coverage detected