Skip to content

Commit 6304aa5

Browse files
committed
fix(ai): multiple suggestions
1 parent a374a78 commit 6304aa5

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

addons/ai/src/main/index.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as commas from 'commas:api/main'
22
import { useAIStatus } from './chat'
33
import type { RuntimeInformation } from './prompt'
4-
import { AnswerSyntaxError, completeCommand, fixCommand, translateCommand } from './prompt'
4+
import { completeCommand, fixCommand, translateCommand } from './prompt'
55

66
declare module '@commas/types/settings' {
77
export interface Settings {
@@ -40,25 +40,18 @@ export default () => {
4040
}
4141
if (query) {
4242
status = true
43-
try {
44-
const generator = translateCommand(query, {
45-
cwd,
46-
extra: { columns },
47-
})
48-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
49-
while (true) {
50-
const { done, value } = await generator.next()
51-
if (done) {
52-
return commas.ipcMain.invoke(sender, 'ai-chat-fix', value)
53-
} else {
54-
yield value
55-
}
43+
const generator = translateCommand(query, {
44+
cwd,
45+
extra: { columns },
46+
})
47+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
48+
while (true) {
49+
const { done, value } = await generator.next()
50+
if (done) {
51+
return commas.ipcMain.invoke(sender, 'ai-chat-fix', value)
52+
} else {
53+
yield value
5654
}
57-
} catch (err) {
58-
if (err instanceof AnswerSyntaxError) {
59-
return `# ${err.message}`
60-
}
61-
throw err
6255
}
6356
}
6457
},
@@ -85,7 +78,7 @@ export default () => {
8578
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
8679
while (true) {
8780
const { done, value } = await generator.next()
88-
if (done) return value.value
81+
if (done && value[0]) return value[0].value
8982
}
9083
} catch {
9184
return ''
@@ -99,7 +92,7 @@ export default () => {
9992
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
10093
while (true) {
10194
const { done, value } = await generator.next()
102-
if (done) return value.value
95+
if (done && value[0]) return value[0].value
10396
}
10497
} catch {
10598
return ''

addons/ai/src/main/prompt.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,15 @@ Preferred Language: ${
108108
`
109109
}
110110

111-
class AnswerSyntaxError extends Error {}
112-
113-
async function* getAnswer(input: string, runtime: RuntimeInformation) {
111+
async function* getAnswers(input: string, runtime: RuntimeInformation) {
114112
const parser = new XMLParser()
115113
const prompt = getSystemPrompt(runtime)
116-
let answer: CommandSuggestion | undefined
114+
const answers: CommandSuggestion[] = []
117115
for await (const { tag, content } of paraphraseXML(chat(prompt, input))) {
118116
if (tag === 'command_suggestion') {
119117
const doc = parser.parse(content)
120118
const suggestion: CommandSuggestion = doc.command_suggestion
121-
answer ??= suggestion
119+
answers.push(suggestion)
122120
yield boxen(`${suggestion.label ?? suggestion.value}${suggestion.description ? '\n' + picocolors.dim(suggestion.description) : ''}`, {
123121
title: picocolors.inverse(picocolors.magenta(` ${startCase(tag)} `)),
124122
borderColor: 'magenta',
@@ -132,19 +130,18 @@ async function* getAnswer(input: string, runtime: RuntimeInformation) {
132130
yield content
133131
}
134132
}
135-
if (!answer) throw new AnswerSyntaxError(answer)
136-
return answer
133+
return answers
137134
}
138135

139136
function translateCommand(query: string, runtime: RuntimeInformation) {
140-
return getAnswer(`
137+
return getAnswers(`
141138
Translate the following task into a command:
142139
${query}
143140
`, runtime)
144141
}
145142

146143
function fixCommand(command: string, output: string, runtime: RuntimeInformation) {
147-
return getAnswer(`
144+
return getAnswers(`
148145
An error occurred when executing the command:
149146
${command}
150147
The error message is:
@@ -154,14 +151,13 @@ Provide a command that can be executed correctly.
154151
}
155152

156153
function completeCommand(query: string, runtime: RuntimeInformation) {
157-
return getAnswer(`
154+
return getAnswers(`
158155
Try to complete the following command:
159156
${query}
160157
`, runtime)
161158
}
162159

163160
export {
164-
AnswerSyntaxError,
165161
translateCommand,
166162
fixCommand,
167163
completeCommand,

addons/ai/src/main/stream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function parseXMLSlices(source: string, offset = 0): XMLSlice[] {
2525
{
2626
type: 'text',
2727
tag: '',
28-
start: 0,
29-
end: source.length,
28+
start: offset,
29+
end: source.length + offset,
3030
isClosed: true,
3131
},
3232
]
@@ -41,7 +41,7 @@ function parseXMLSlices(source: string, offset = 0): XMLSlice[] {
4141
{
4242
type: 'text',
4343
tag: '',
44-
start: 0,
44+
start: offset,
4545
end: tagIndex + offset,
4646
isClosed: true,
4747
} satisfies XMLSlice,

addons/ai/src/renderer/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useAIStatus } from './compositions'
66

77
declare module '@commas/electron-ipc' {
88
export interface RendererCommands {
9-
'ai-chat-fix': (suggestion: CommandSuggestion) => void,
9+
'ai-chat-fix': (suggestions: CommandSuggestion[]) => void,
1010
}
1111
}
1212

@@ -16,14 +16,16 @@ export default () => {
1616

1717
const terminal = $(commas.workspace.useCurrentTerminal())
1818

19-
commas.ipcRenderer.handle('ai-chat-fix', (event, suggestion) => {
19+
commas.ipcRenderer.handle('ai-chat-fix', (event, suggestions) => {
2020
if (!terminal) return
21-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
22-
terminal.addons?.shellIntegration?.addQuickFixAction(undefined, {
23-
value: suggestion.value,
24-
label: suggestion.label,
25-
description: suggestion.description,
26-
})
21+
for (const suggestion of suggestions) {
22+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
23+
terminal.addons?.shellIntegration?.addQuickFixAction(undefined, {
24+
value: suggestion.value,
25+
label: suggestion.label,
26+
description: suggestion.description,
27+
})
28+
}
2729
})
2830

2931
const status = $(useAIStatus())

0 commit comments

Comments
 (0)