| 927 | } |
| 928 | |
| 929 | func (p *Server) createChildSubagentChatWithOptions( |
| 930 | ctx context.Context, |
| 931 | parent database.Chat, |
| 932 | prompt string, |
| 933 | title string, |
| 934 | opts childSubagentChatOptions, |
| 935 | ) (database.Chat, error) { |
| 936 | if parent.ParentChatID.Valid { |
| 937 | return database.Chat{}, xerrors.New("delegated chats cannot create child subagents") |
| 938 | } |
| 939 | |
| 940 | prompt = strings.TrimSpace(prompt) |
| 941 | if prompt == "" { |
| 942 | return database.Chat{}, xerrors.New("prompt is required") |
| 943 | } |
| 944 | |
| 945 | title = strings.TrimSpace(title) |
| 946 | if title == "" { |
| 947 | title = subagentFallbackChatTitle(prompt) |
| 948 | } |
| 949 | |
| 950 | rootChatID := parent.ID |
| 951 | if parent.RootChatID.Valid { |
| 952 | rootChatID = parent.RootChatID.UUID |
| 953 | } |
| 954 | |
| 955 | modelConfigID := parent.LastModelConfigID |
| 956 | if opts.modelConfigIDOverride != nil { |
| 957 | modelConfigID = *opts.modelConfigIDOverride |
| 958 | } |
| 959 | if modelConfigID == uuid.Nil { |
| 960 | return database.Chat{}, xerrors.New("model config is required") |
| 961 | } |
| 962 | childAPIKeyID, err := p.delegatedAPIKeyIDForSubagent(ctx) |
| 963 | if err != nil { |
| 964 | return database.Chat{}, err |
| 965 | } |
| 966 | |
| 967 | childPlanMode := parent.PlanMode |
| 968 | if opts.planModeOverride != nil { |
| 969 | childPlanMode = *opts.planModeOverride |
| 970 | } |
| 971 | |
| 972 | mcpServerIDs := parent.MCPServerIDs |
| 973 | if isExploreSubagentMode(opts.chatMode) { |
| 974 | mcpServerIDs = slices.Clone(opts.inheritedMCPServerIDs) |
| 975 | } |
| 976 | if mcpServerIDs == nil { |
| 977 | mcpServerIDs = []uuid.UUID{} |
| 978 | } |
| 979 | |
| 980 | labelsJSON, err := json.Marshal(database.StringMap{}) |
| 981 | if err != nil { |
| 982 | return database.Chat{}, xerrors.Errorf("marshal labels: %w", err) |
| 983 | } |
| 984 | childSystemPrompt := SanitizePromptText(opts.systemPrompt) |
| 985 | // Resolve the deployment prompt before opening the transaction so |
| 986 | // child chat creation does not hold one DB connection while waiting |