Skip to content

Commit 5c1a417

Browse files
authored
Don't include the current file prompt if there is no current file (#8135)
* Don't include the current file prompt if there is no current file And use more correct utility to get the current file. It was failing in the browser version of the app because the way I got the relative path to the current file was incorrect. * Fix lint
1 parent 49d40f2 commit 5c1a417

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

src/components/ModelingSidebar/ModelingPanes/MlEphantConversationPane.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ export const MlEphantConversationPane = (props: {
6161
console.warn('theProject is `undefined` - should not be possible')
6262
return
6363
}
64+
if (props.loaderFile === undefined) {
65+
console.warn('loaderFile is `undefined` - should not be possible')
66+
return
67+
}
6468

6569
const projectFiles = await collectProjectFiles({
6670
selectedFileContents: props.codeManager.code,
6771
fileNames: props.kclManager.execState.filenames,
6872
projectContext: props.theProject,
69-
targetFile: props.loaderFile,
7073
})
7174

7275
// Only on initial project creation do we call the create endpoint, which
@@ -76,6 +79,7 @@ export const MlEphantConversationPane = (props: {
7679
type: MlEphantManagerTransitions.PromptEditModel,
7780
prompt: requestedPrompt,
7881
projectForPromptOutput: props.theProject,
82+
applicationProjectDirectory: props.settings.app.projectDirectory.current,
7983
fileSelectedDuringPrompting: {
8084
entry: props.loaderFile,
8185
content: props.codeManager.code,

src/lib/promptToEdit.tsx

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
PromptToEditRequest,
1313
TextToCadErrorResponse,
1414
} from '@src/lib/promptToEditTypes'
15+
import { parentPathRelativeToProject } from '@src/lib/paths'
1516

1617
function sourceIndexToLineColumn(
1718
code: string,
@@ -57,7 +58,10 @@ export async function submitTextToCadMultiFileIterationRequest(
5758
)
5859

5960
if (!response.ok) {
60-
return new Error(`HTTP error! status: ${response.status}`)
61+
const errorBody = await response.json()
62+
return new Error(
63+
`HTTP error! status: ${response.status}, error: ${JSON.stringify(errorBody)}`
64+
)
6165
}
6266

6367
const data = await response.json()
@@ -77,16 +81,15 @@ export function constructMultiFileIterationRequestWithPromptHelpers({
7781
prompt,
7882
selections,
7983
projectFiles,
84+
applicationProjectDirectory,
8085
artifactGraph,
8186
projectName,
8287
currentFile,
8388
kclVersion,
8489
}: ConstructRequestArgs): PromptToEditRequest {
8590
const kclFilesMap: KclFileMetaMap = {}
8691
const files: KittyCadLibFile[] = []
87-
const currentFileMeta = projectFiles.find(
88-
(f) => f.type !== 'other' && f.absPath === currentFile.entry?.path
89-
)
92+
9093
projectFiles.forEach((file) => {
9194
let data: Blob
9295
if (file.type === 'other') {
@@ -104,21 +107,31 @@ export function constructMultiFileIterationRequestWithPromptHelpers({
104107

105108
// Way to patch in supplying the currently-opened file without updating the API.
106109
// TODO: update the API to support currently-opened files as other parts of the payload
107-
const currentFilePrompt: Models['SourceRangePrompt_type'] = {
108-
prompt: 'This is the active file',
109-
range: convertAppRangeToApiRange(
110-
[0, currentFile.content.length, 0],
111-
currentFile.content
112-
),
113-
file: currentFileMeta?.relPath,
114-
}
110+
const currentFilePrompt: Models['SourceRangePrompt_type'] | null =
111+
currentFile.entry
112+
? {
113+
prompt: 'This is the active file',
114+
range: convertAppRangeToApiRange(
115+
[0, currentFile.content.length, 0],
116+
currentFile.content
117+
),
118+
file: parentPathRelativeToProject(
119+
currentFile.entry?.path,
120+
applicationProjectDirectory
121+
),
122+
}
123+
: null
115124

116125
// If no selection, use whole file
117126
if (selections === null) {
127+
const rangePrompts: Models['SourceRangePrompt_type'][] = []
128+
if (currentFilePrompt !== null) {
129+
rangePrompts.push(currentFilePrompt)
130+
}
118131
return {
119132
body: {
120133
prompt,
121-
source_ranges: [currentFilePrompt],
134+
source_ranges: rangePrompts,
122135
project_name:
123136
projectName !== '' && projectName !== 'browser'
124137
? projectName
@@ -145,11 +158,9 @@ export function constructMultiFileIterationRequestWithPromptHelpers({
145158
prompts.push({
146159
prompt: `The users main selection is the end cap of a general-sweep (that is an extrusion, revolve, sweep or loft).
147160
The source range most likely refers to "startProfile" simply because this is the start of the profile that was swept.
148-
If you need to operate on this cap, for example for sketching on the face, you can use the special string ${
149-
artifact.subType === 'end' ? 'END' : 'START'
150-
} i.e. \`startSketchOn(someSweepVariable, face = ${
151-
artifact.subType === 'end' ? 'END' : 'START'
152-
})\`
161+
If you need to operate on this cap, for example for sketching on the face, you can use the special string ${artifact.subType === 'end' ? 'END' : 'START'
162+
} i.e. \`startSketchOn(someSweepVariable, face = ${artifact.subType === 'end' ? 'END' : 'START'
163+
})\`
153164
When they made this selection they main have intended this surface directly or meant something more general like the sweep body.
154165
See later source ranges for more context.`,
155166
range: convertAppRangeToApiRange(selection.codeRef.range, code),
@@ -190,14 +201,12 @@ But it's also worth bearing in mind that the user may have intended to select th
190201
if (artifact?.type === 'sweepEdge') {
191202
prompts.push({
192203
prompt: `The users main selection is the edge of a general-sweep (that is an extrusion, revolve, sweep or loft).
193-
it is an ${
194-
artifact.subType
195-
} edge, in order to refer to this edge you should add a tag to the segment function in this source range,
196-
and then use the function ${
197-
artifact.subType === 'adjacent'
204+
it is an ${artifact.subType
205+
} edge, in order to refer to this edge you should add a tag to the segment function in this source range,
206+
and then use the function ${artifact.subType === 'adjacent'
198207
? 'getAdjacentEdge'
199208
: 'getOppositeEdge'
200-
}
209+
}
201210
See later source ranges for more context. about the sweep`,
202211
range: convertAppRangeToApiRange(selection.codeRef.range, code),
203212
file: filePath,
@@ -261,7 +270,9 @@ See later source ranges for more context. about the sweep`,
261270
return prompts
262271
})
263272
// Push the current file prompt alongside the selection-based prompts
264-
ranges.push(currentFilePrompt)
273+
if (currentFilePrompt !== null) {
274+
ranges.push(currentFilePrompt)
275+
}
265276
let payload = {
266277
body: {
267278
prompt,

src/lib/promptToEditTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface PromptToEditRequest {
2727
export interface ConstructRequestArgs {
2828
conversationId?: string
2929
prompt: string
30+
applicationProjectDirectory: string
3031
selections: Selections | null
3132
projectFiles: FileMeta[]
3233
projectName: string

src/machines/mlEphantManagerMachine.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export type MlEphantManagerEvents =
8181
type: MlEphantManagerTransitions.PromptEditModel
8282
projectForPromptOutput: Project
8383
prompt: string
84-
fileSelectedDuringPrompting: { entry?: FileEntry; content: string }
84+
applicationProjectDirectory: string
85+
fileSelectedDuringPrompting: { entry: FileEntry; content: string }
8586
projectFiles: FileMeta[]
8687
selections: Selections
8788
artifactGraph: ArtifactGraph
@@ -377,6 +378,7 @@ export const mlEphantManagerMachine = setup({
377378
conversationId: context.conversationId,
378379
prompt: event.prompt,
379380
selections: event.selections,
381+
applicationProjectDirectory: event.applicationProjectDirectory,
380382
projectFiles: event.projectFiles,
381383
artifactGraph: event.artifactGraph,
382384
projectName: event.projectForPromptOutput.name,

src/machines/systemIO/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ export const determineProjectFilePathFromPrompt = (
227227
export const collectProjectFiles = async (args: {
228228
selectedFileContents: string
229229
fileNames: ExecState['filenames']
230-
targetFile?: FileEntry
231230
projectContext?: Project
232231
}) => {
233232
let projectFiles: FileMeta[] = [

0 commit comments

Comments
 (0)