@@ -12,6 +12,7 @@ import { readFile } from 'fs-extra'
1212import { ensureCyPromptBundle } from './ensure_cy_prompt_bundle'
1313import chokidar from 'chokidar'
1414import { getCloudMetadata } from '../get_cloud_metadata'
15+ import type { CyPromptAuthenticatedUserShape } from '@packages/types'
1516
1617const debug = Debug ( 'cypress:server:cy-prompt-lifecycle-manager' )
1718
@@ -33,22 +34,34 @@ export class CyPromptLifecycleManager {
3334 * @param ctx Data context to register this instance with
3435 */
3536 initializeCyPromptManager ( {
36- projectId,
3737 cloudDataSource,
3838 ctx,
39+ record,
40+ key,
3941 } : {
40- projectId ?: string
4142 cloudDataSource : CloudDataSource
4243 ctx : DataContext
44+ record ?: boolean
45+ key ?: string
4346 } ) : void {
4447 // Register this instance in the data context
4548 ctx . update ( ( data ) => {
4649 data . cyPromptLifecycleManager = this
4750 } )
4851
52+ const getProjectOptions = async ( ) => {
53+ return {
54+ user : await ctx . actions . auth . authApi . getUser ( ) ,
55+ projectSlug : ( await ctx . project . getConfig ( ) ) . projectId || undefined ,
56+ record,
57+ key,
58+ isOpenMode : ctx . isOpenMode ,
59+ }
60+ }
61+
4962 const cyPromptManagerPromise = this . createCyPromptManager ( {
50- projectId,
5163 cloudDataSource,
64+ getProjectOptions,
5265 } ) . catch ( async ( error ) => {
5366 debug ( 'Error during cy prompt manager setup: %o' , error )
5467
@@ -81,8 +94,8 @@ export class CyPromptLifecycleManager {
8194 this . cyPromptManagerPromise = cyPromptManagerPromise
8295
8396 this . setupWatcher ( {
84- projectId,
8597 cloudDataSource,
98+ getProjectOptions,
8699 } )
87100 }
88101
@@ -97,17 +110,25 @@ export class CyPromptLifecycleManager {
97110 }
98111
99112 private async createCyPromptManager ( {
100- projectId,
101113 cloudDataSource,
114+ getProjectOptions,
102115 } : {
103116 projectId ?: string
104117 cloudDataSource : CloudDataSource
118+ getProjectOptions : ( ) => Promise < {
119+ user ?: CyPromptAuthenticatedUserShape
120+ projectSlug ?: string
121+ record ?: boolean
122+ key ?: string
123+ } >
105124 } ) : Promise < { cyPromptManager ?: CyPromptManager , error ?: Error } > {
106125 let cyPromptHash : string
107126 let cyPromptPath : string
108127
128+ const currentProjectOptions = await getProjectOptions ( )
129+ const projectId = currentProjectOptions . projectSlug
109130 const cyPromptSession = await postCyPromptSession ( {
110- projectId,
131+ projectId : currentProjectOptions . projectSlug ,
111132 } )
112133
113134 if ( ! process . env . CYPRESS_LOCAL_CY_PROMPT_PATH ) {
@@ -138,20 +159,19 @@ export class CyPromptLifecycleManager {
138159 const script = await readFile ( serverFilePath , 'utf8' )
139160 const cyPromptManager = new CyPromptManager ( )
140161
141- const { cloudUrl, cloudHeaders } = await getCloudMetadata ( cloudDataSource )
162+ const { cloudUrl } = await getCloudMetadata ( cloudDataSource )
142163
143164 await cyPromptManager . setup ( {
144165 script,
145166 cyPromptPath,
146167 cyPromptHash,
147- projectSlug : projectId ,
148168 cloudApi : {
149169 cloudUrl,
150- cloudHeaders,
151170 CloudRequest,
152171 isRetryableError,
153172 asyncRetry,
154173 } ,
174+ getProjectOptions,
155175 } )
156176
157177 debug ( 'cy prompt is ready' )
@@ -181,9 +201,16 @@ export class CyPromptLifecycleManager {
181201 private setupWatcher ( {
182202 projectId,
183203 cloudDataSource,
204+ getProjectOptions,
184205 } : {
185206 projectId ?: string
186207 cloudDataSource : CloudDataSource
208+ getProjectOptions : ( ) => Promise < {
209+ user ?: CyPromptAuthenticatedUserShape
210+ projectSlug ?: string
211+ record ?: boolean
212+ key ?: string
213+ } >
187214 } ) {
188215 // Don't setup a watcher if the cy prompt bundle is NOT local
189216 if ( ! process . env . CYPRESS_LOCAL_CY_PROMPT_PATH ) {
@@ -204,6 +231,7 @@ export class CyPromptLifecycleManager {
204231 this . cyPromptManagerPromise = this . createCyPromptManager ( {
205232 projectId,
206233 cloudDataSource,
234+ getProjectOptions,
207235 } ) . catch ( ( error ) => {
208236 debug ( 'Error during reload of cy prompt manager: %o' , error )
209237
0 commit comments