diff --git a/src/components/ModelingMachineProvider.tsx b/src/components/ModelingMachineProvider.tsx index 89e28022a..b26ae4541 100644 --- a/src/components/ModelingMachineProvider.tsx +++ b/src/components/ModelingMachineProvider.tsx @@ -75,6 +75,7 @@ import { MAKE_TOAST_MESSAGES, EXECUTION_TYPE_MOCK, FILE_EXT, + PROJECT_ENTRYPOINT, } from '@src/lib/constants' import { exportMake } from '@src/lib/exportMake' import { exportSave } from '@src/lib/exportSave' @@ -1759,7 +1760,8 @@ export const ModelingMachineProvider = ({ ) let basePath = '' if (isDesktop() && context?.project?.children) { - basePath = context?.selectedDirectory?.path + // Use the entire project directory as the basePath for prompt to edit, do not use relative subdir paths + basePath = context?.project?.path const filePromises: Promise[] = [] let uploadSize = 0 const recursivelyPushFilePromises = (files: FileEntry[]) => { @@ -1826,6 +1828,13 @@ export const ModelingMachineProvider = ({ ) } } + let filePath = file?.path + // When prompt to edit finishes, try to route to the file they were in otherwise go to main.kcl + if (filePath) { + filePath = window.electron.path.relative(basePath, filePath) + } else { + filePath = PROJECT_ENTRYPOINT + } return await promptToEditFlow({ projectFiles, prompt: input.prompt, @@ -1833,6 +1842,7 @@ export const ModelingMachineProvider = ({ token, artifactGraph: kclManager.artifactGraph, projectName: context.project.name, + filePath, }) }), }, diff --git a/src/components/ToastTextToCad.tsx b/src/components/ToastTextToCad.tsx index 6e971faa8..6dbc1c9c9 100644 --- a/src/components/ToastTextToCad.tsx +++ b/src/components/ToastTextToCad.tsx @@ -35,6 +35,7 @@ import { } from '@src/machines/systemIO/utils' import { useProjectDirectoryPath, + useRequestedFileName, useRequestedProjectName, } from '@src/machines/systemIO/hooks' import { commandBarActor } from '@src/lib/singletons' @@ -498,7 +499,14 @@ export function ToastPromptToEditCadSuccess({ token?: string }) { const modelId = data.id - const requestedProjectName = useRequestedProjectName() + const possibleRequestedProjectName = useRequestedProjectName() + const possibleRequestedFileName = useRequestedFileName() + + // Depends on navigation method + const requestedProjectName = { + name: + possibleRequestedProjectName.name || possibleRequestedFileName.project, + } return (
@@ -548,6 +556,7 @@ export function ToastPromptToEditCadSuccess({ await writeOverFilesAndExecute({ requestedFiles: requestedFiles, projectName: requestedProjectName.name, + filePath: possibleRequestedFileName.file, }) } else { codeManager.updateCodeEditor(oldCode) @@ -588,18 +597,32 @@ export function ToastPromptToEditCadSuccess({ export const writeOverFilesAndExecute = async ({ requestedFiles, projectName, + filePath, }: { requestedFiles: RequestedKCLFile[] projectName: string + filePath?: string | undefined }) => { - systemIOActor.send({ - type: SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToProject, - data: { - files: requestedFiles, - requestedProjectName: projectName, - override: true, - }, - }) + if (filePath) { + systemIOActor.send({ + type: SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToFile, + data: { + files: requestedFiles, + requestedProjectName: projectName, + requestedFileNameWithExtension: filePath, + override: true, + }, + }) + } else { + systemIOActor.send({ + type: SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToProject, + data: { + files: requestedFiles, + requestedProjectName: projectName, + override: true, + }, + }) + } // to await the result of the send event above await waitForIdleState({ systemIOActor }) diff --git a/src/lib/promptToEdit.tsx b/src/lib/promptToEdit.tsx index 339492091..a4f602fec 100644 --- a/src/lib/promptToEdit.tsx +++ b/src/lib/promptToEdit.tsx @@ -415,6 +415,7 @@ export async function promptToEditFlow({ token, artifactGraph, projectName, + filePath, }: { prompt: string selections: Selections @@ -422,6 +423,7 @@ export async function promptToEditFlow({ token?: string artifactGraph: ArtifactGraph projectName: string + filePath: string | undefined }) { const result = await doPromptEdit({ prompt, @@ -498,6 +500,7 @@ export async function promptToEditFlow({ await writeOverFilesAndExecute({ requestedFiles, projectName, + filePath, }) } else { const newCode = result.outputs['main.kcl'] diff --git a/src/machines/systemIO/systemIOMachine.ts b/src/machines/systemIO/systemIOMachine.ts index ae15b5ce0..9f1fdfab4 100644 --- a/src/machines/systemIO/systemIOMachine.ts +++ b/src/machines/systemIO/systemIOMachine.ts @@ -102,6 +102,16 @@ export const systemIOMachine = setup({ requestedSubRoute?: string } } + | { + type: SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToFile + data: { + files: RequestedKCLFile[] + requestedProjectName: string + requestedFileNameWithExtension: string + override?: boolean + requestedSubRoute?: string + } + } | { type: SystemIOMachineEvents.importFileFromURL data: { @@ -339,6 +349,27 @@ export const systemIOMachine = setup({ return { message: '', fileName: '', projectName: '', subRoute: '' } } ), + [SystemIOMachineActors.bulkCreateKCLFilesAndNavigateToFile]: fromPromise( + async ({ + input, + }: { + input: { + context: SystemIOContext + files: RequestedKCLFile[] + rootContext: AppMachineContext + requestedProjectName: string + requestedFileNameWithExtension: string + requestedSubRoute?: string + } + }): Promise<{ + message: string + fileName: string + projectName: string + subRoute: string + }> => { + return { message: '', fileName: '', projectName: '', subRoute: '' } + } + ), }, }).createMachine({ initial: SystemIOMachineStates.idle, @@ -414,6 +445,9 @@ export const systemIOMachine = setup({ target: SystemIOMachineStates.bulkCreatingKCLFilesAndNavigateToProject, }, + [SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToFile]: { + target: SystemIOMachineStates.bulkCreatingKCLFilesAndNavigateToFile, + }, }, }, [SystemIOMachineStates.readingFolders]: { @@ -683,5 +717,53 @@ export const systemIOMachine = setup({ }, }, }, + [SystemIOMachineStates.bulkCreatingKCLFilesAndNavigateToFile]: { + invoke: { + id: SystemIOMachineActors.bulkCreateKCLFilesAndNavigateToFile, + src: SystemIOMachineActors.bulkCreateKCLFilesAndNavigateToFile, + input: ({ context, event, self }) => { + assertEvent( + event, + SystemIOMachineEvents.bulkCreateKCLFilesAndNavigateToFile + ) + return { + context, + files: event.data.files, + rootContext: self.system.get('root').getSnapshot().context, + requestedProjectName: event.data.requestedProjectName, + override: event.data.override, + requestedFileNameWithExtension: + event.data.requestedFileNameWithExtension, + requestedSubRoute: event.data.requestedSubRoute, + } + }, + onDone: { + target: SystemIOMachineStates.readingFolders, + actions: [ + assign({ + requestedFileName: ({ event }) => { + assertEvent( + event, + SystemIOMachineEvents.done_bulkCreateKCLFilesAndNavigateToFile + ) + // Gotcha: file could have an ending of .kcl... + const file = event.output.fileName.endsWith('.kcl') + ? event.output.fileName + : event.output.fileName + '.kcl' + return { + project: event.output.projectName, + file, + } + }, + }), + SystemIOMachineActions.toastSuccess, + ], + }, + onError: { + target: SystemIOMachineStates.idle, + actions: [SystemIOMachineActions.toastError], + }, + }, + }, }, }) diff --git a/src/machines/systemIO/systemIOMachineDesktop.ts b/src/machines/systemIO/systemIOMachineDesktop.ts index 0f0ea5836..0a5681dae 100644 --- a/src/machines/systemIO/systemIOMachineDesktop.ts +++ b/src/machines/systemIO/systemIOMachineDesktop.ts @@ -73,6 +73,10 @@ const sharedBulkCreateWorkflow = async ({ baseDir, }).name + console.log('why?') + console.log('override', input.override) + console.log('override', newProjectName) + console.log('override', fileName) // Create the project around the file if newProject await createNewProjectDirectory( newProjectName, @@ -370,5 +374,33 @@ export const systemIOMachineDesktop = systemIOMachine.provide({ } } ), + [SystemIOMachineActors.bulkCreateKCLFilesAndNavigateToFile]: fromPromise( + async ({ + input, + }: { + input: { + context: SystemIOContext + files: RequestedKCLFile[] + rootContext: AppMachineContext + requestedProjectName: string + override?: boolean + requestedFileNameWithExtension: string + requestedSubRoute?: string + } + }) => { + const message = await sharedBulkCreateWorkflow({ + input: { + ...input, + override: input.override, + }, + }) + return { + ...message, + projectName: input.requestedProjectName, + fileName: input.requestedFileNameWithExtension || '', + subRoute: input.requestedSubRoute || '', + } + } + ), }, }) diff --git a/src/machines/systemIO/utils.ts b/src/machines/systemIO/utils.ts index 2db9b583d..013fcd69e 100644 --- a/src/machines/systemIO/utils.ts +++ b/src/machines/systemIO/utils.ts @@ -15,6 +15,7 @@ export enum SystemIOMachineActors { deleteKCLFile = 'delete kcl delete', bulkCreateKCLFiles = 'bulk create kcl files', bulkCreateKCLFilesAndNavigateToProject = 'bulk create kcl files and navigate to project', + bulkCreateKCLFilesAndNavigateToFile = 'bulk create kcl files and navigate to file', } export enum SystemIOMachineStates { @@ -31,6 +32,7 @@ export enum SystemIOMachineStates { deletingKCLFile = 'deletingKCLFile', bulkCreatingKCLFiles = 'bulkCreatingKCLFiles', bulkCreatingKCLFilesAndNavigateToProject = 'bulkCreatingKCLFilesAndNavigateToProject', + bulkCreatingKCLFilesAndNavigateToFile = 'bulkCreatingKCLFilesAndNavigateToFile', } const donePrefix = 'xstate.done.actor.' @@ -56,6 +58,9 @@ export enum SystemIOMachineEvents { deleteKCLFile = 'delete kcl file', bulkCreateKCLFiles = 'bulk create kcl files', bulkCreateKCLFilesAndNavigateToProject = 'bulk create kcl files and navigate to project', + bulkCreateKCLFilesAndNavigateToFile = 'bulk create kcl files and navigate to file', + done_bulkCreateKCLFilesAndNavigateToFile = donePrefix + + 'bulk create kcl files and navigate to file', } export enum SystemIOMachineActions {