Skip to content

Commit ec68856

Browse files
committed
wip: code suggestion #33
Signed-off-by: seven <[email protected]>
1 parent f09b2b2 commit ec68856

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/common/httpClient.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,66 @@ export const loadHttpClient = (con: {
101101
ssl: con.sslCertVerification,
102102
}),
103103
});
104+
105+
const MODEL = 'gpt-3.5-turbo-0125';
106+
let assistant = null;
107+
export const loadAiClient = async () => {
108+
const headers = {
109+
'Content-Type': 'application/json',
110+
'OpenAI-Beta': 'assistants=v1',
111+
Authorization: `Bearer ${OPENAI_API_KEY}`,
112+
};
113+
const { data, status, details } = await fetchApi.fetch('https://api.openai.com/v1/assistants', {
114+
method: 'POST',
115+
headers,
116+
body: JSON.stringify({
117+
instructions: 'You are a personal math tutor. Write and run code to answer math questions.',
118+
name: 'Math Tutor',
119+
tools: [{ type: 'code_interpreter' }],
120+
model: MODEL,
121+
}),
122+
});
123+
assistant = data as { id: string };
124+
const { data: thread, status: threadStatus } = await fetchApi.fetch(
125+
`https://api.openai.com/v1/assistants/${assistant.id}/threads`,
126+
{
127+
method: 'POST',
128+
headers,
129+
body: JSON.stringify({
130+
messages: [
131+
{
132+
role: 'system',
133+
content: 'You are a personal math tutor. Write and run code to answer math questions.',
134+
},
135+
],
136+
}),
137+
},
138+
);
139+
140+
console.log(`gpt assistant: ${assistant}, thread ${thread}`);
141+
if (status !== 200) {
142+
throw new CustomError(status, details);
143+
}
144+
return {
145+
suggest: async (fileContent: string, currentLineNumber: number) => {
146+
const { data, status, details } = await fetchApi.fetch(
147+
`https://api.openai.com/v1/threads/${(thread as { id: string }).id}/messages`,
148+
{
149+
method: 'POST',
150+
headers,
151+
body: JSON.stringify({
152+
messages: [
153+
{
154+
role: 'system',
155+
content: fileContent,
156+
current_line_number: currentLineNumber,
157+
},
158+
],
159+
}),
160+
},
161+
);
162+
console.log(`gpt suggest: ${data}, status: ${status}, details: ${details}`);
163+
return (data as { choices: Array<{ text: string }> }).choices[0].text.trim();
164+
},
165+
};
166+
};

src/views/editor/index.vue

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '../../common';
2020
import { useAppStore, useConnectionStore, useSourceFileStore } from '../../store';
2121
import { useLang } from '../../lang';
22+
import { loadAiClient } from '../../common/httpClient';
2223
2324
type Editor = ReturnType<typeof monaco.editor.create>;
2425
@@ -191,14 +192,23 @@ const executeQueryAction = async (
191192
}
192193
};
193194
194-
const setupQueryEditor = (code: string) => {
195+
const { fetchApi } = window;
196+
let aiClient: {
197+
suggest: (text: string, rangeLength: number) => Promise<string>;
198+
} | null = null;
199+
200+
const setupQueryEditor = async (code: string) => {
195201
queryEditor = monaco.editor.create(queryEditorRef.value, {
196202
automaticLayout: true,
197203
theme: getEditorTheme(),
198204
value: code ? code : defaultCodeSnippet,
199205
language: 'search',
200206
});
201207
208+
if (!aiClient) {
209+
aiClient = await loadAiClient();
210+
}
211+
202212
autoIndentCmdId = queryEditor.addCommand(
203213
0,
204214
(
@@ -263,6 +273,35 @@ const setupQueryEditor = (code: string) => {
263273
executeQueryAction(queryEditor, displayEditor, target.position);
264274
}
265275
});
276+
277+
// Event listener for user input
278+
queryEditor.onDidChangeModelContent(async ({ range, rangeLength, text }) => {
279+
const suggestion = await aiClient.suggest(text, 0);
280+
281+
const { status, data, details } = await fetchApi.fetch(
282+
'http://your-backend-service/api/suggest',
283+
{
284+
method: 'POST',
285+
headers: {
286+
'Content-Type': 'application/json',
287+
},
288+
body: JSON.stringify({ code: text }),
289+
},
290+
);
291+
if (status !== 200) {
292+
message.error(details, {
293+
closable: true,
294+
keepAliveOnHover: true,
295+
});
296+
return;
297+
}
298+
queryEditor.suggest(
299+
(data as Array<{ label: string; kind: number }>).map(suggestion => ({
300+
label: suggestion.label,
301+
kind: monaco.languages.CompletionItemKind[suggestion.kind],
302+
})),
303+
);
304+
});
266305
};
267306
const toggleEditor = (editorRef: Ref, display: string) => {
268307
editorRef.value.style.display = display;
@@ -280,6 +319,7 @@ const setupJsonEditor = () => {
280319
minimap: { enabled: false },
281320
});
282321
};
322+
283323
onMounted(async () => {
284324
await readSourceFromFile();
285325
const code = defaultFile.value;

0 commit comments

Comments
 (0)