| 1237 | } |
| 1238 | |
| 1239 | func (p *Server) sendSubagentMessage( |
| 1240 | ctx context.Context, |
| 1241 | parentChatID uuid.UUID, |
| 1242 | targetChatID uuid.UUID, |
| 1243 | message string, |
| 1244 | busyBehavior SendMessageBusyBehavior, |
| 1245 | ) (database.Chat, error) { |
| 1246 | message = strings.TrimSpace(message) |
| 1247 | if message == "" { |
| 1248 | return database.Chat{}, xerrors.New("message is required") |
| 1249 | } |
| 1250 | |
| 1251 | isDescendant, err := isSubagentDescendant(ctx, p.db, parentChatID, targetChatID) |
| 1252 | if err != nil { |
| 1253 | return database.Chat{}, err |
| 1254 | } |
| 1255 | if !isDescendant { |
| 1256 | return database.Chat{}, ErrSubagentNotDescendant |
| 1257 | } |
| 1258 | |
| 1259 | // Look up the target chat to get the owner for CreatedBy. |
| 1260 | targetChat, err := p.db.GetChatByID(ctx, targetChatID) |
| 1261 | if err != nil { |
| 1262 | return database.Chat{}, xerrors.Errorf("get target chat: %w", err) |
| 1263 | } |
| 1264 | |
| 1265 | apiKeyID, err := p.delegatedAPIKeyIDForSubagent(ctx) |
| 1266 | if err != nil { |
| 1267 | return database.Chat{}, err |
| 1268 | } |
| 1269 | |
| 1270 | sendResult, err := p.SendMessage(ctx, SendMessageOptions{ |
| 1271 | ChatID: targetChatID, |
| 1272 | CreatedBy: targetChat.OwnerID, |
| 1273 | Content: []codersdk.ChatMessagePart{codersdk.ChatMessageText(message)}, |
| 1274 | APIKeyID: apiKeyID, |
| 1275 | BusyBehavior: busyBehavior, |
| 1276 | }) |
| 1277 | if err != nil { |
| 1278 | return database.Chat{}, err |
| 1279 | } |
| 1280 | |
| 1281 | return sendResult.Chat, nil |
| 1282 | } |
| 1283 | |
| 1284 | func (p *Server) awaitSubagentCompletion( |
| 1285 | ctx context.Context, |