Skip to content

Commit

Permalink
wip: code suggestion #33
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Mar 29, 2024
1 parent f09b2b2 commit ec68856
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/common/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,66 @@ export const loadHttpClient = (con: {
ssl: con.sslCertVerification,
}),
});

const MODEL = 'gpt-3.5-turbo-0125';
let assistant = null;
export const loadAiClient = async () => {
const headers = {
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1',
Authorization: `Bearer ${OPENAI_API_KEY}`,
};
const { data, status, details } = await fetchApi.fetch('https://api.openai.com/v1/assistants', {
method: 'POST',
headers,
body: JSON.stringify({
instructions: 'You are a personal math tutor. Write and run code to answer math questions.',
name: 'Math Tutor',
tools: [{ type: 'code_interpreter' }],
model: MODEL,
}),
});
assistant = data as { id: string };
const { data: thread, status: threadStatus } = await fetchApi.fetch(
`https://api.openai.com/v1/assistants/${assistant.id}/threads`,
{
method: 'POST',
headers,
body: JSON.stringify({
messages: [
{
role: 'system',
content: 'You are a personal math tutor. Write and run code to answer math questions.',
},
],
}),
},
);

console.log(`gpt assistant: ${assistant}, thread ${thread}`);
if (status !== 200) {
throw new CustomError(status, details);
}
return {
suggest: async (fileContent: string, currentLineNumber: number) => {
const { data, status, details } = await fetchApi.fetch(
`https://api.openai.com/v1/threads/${(thread as { id: string }).id}/messages`,
{
method: 'POST',
headers,
body: JSON.stringify({
messages: [
{
role: 'system',
content: fileContent,
current_line_number: currentLineNumber,
},
],
}),
},
);
console.log(`gpt suggest: ${data}, status: ${status}, details: ${details}`);
return (data as { choices: Array<{ text: string }> }).choices[0].text.trim();
},
};
};
42 changes: 41 additions & 1 deletion src/views/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../common';
import { useAppStore, useConnectionStore, useSourceFileStore } from '../../store';
import { useLang } from '../../lang';
import { loadAiClient } from '../../common/httpClient';
type Editor = ReturnType<typeof monaco.editor.create>;
Expand Down Expand Up @@ -191,14 +192,23 @@ const executeQueryAction = async (
}
};
const setupQueryEditor = (code: string) => {
const { fetchApi } = window;
let aiClient: {
suggest: (text: string, rangeLength: number) => Promise<string>;
} | null = null;
const setupQueryEditor = async (code: string) => {
queryEditor = monaco.editor.create(queryEditorRef.value, {
automaticLayout: true,
theme: getEditorTheme(),
value: code ? code : defaultCodeSnippet,
language: 'search',
});
if (!aiClient) {
aiClient = await loadAiClient();
}
autoIndentCmdId = queryEditor.addCommand(
0,
(
Expand Down Expand Up @@ -263,6 +273,35 @@ const setupQueryEditor = (code: string) => {
executeQueryAction(queryEditor, displayEditor, target.position);
}
});
// Event listener for user input
queryEditor.onDidChangeModelContent(async ({ range, rangeLength, text }) => {
const suggestion = await aiClient.suggest(text, 0);
const { status, data, details } = await fetchApi.fetch(
'http://your-backend-service/api/suggest',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code: text }),
},
);
if (status !== 200) {
message.error(details, {
closable: true,
keepAliveOnHover: true,
});
return;
}
queryEditor.suggest(
(data as Array<{ label: string; kind: number }>).map(suggestion => ({
label: suggestion.label,
kind: monaco.languages.CompletionItemKind[suggestion.kind],
})),
);
});
};
const toggleEditor = (editorRef: Ref, display: string) => {
editorRef.value.style.display = display;
Expand All @@ -280,6 +319,7 @@ const setupJsonEditor = () => {
minimap: { enabled: false },
});
};
onMounted(async () => {
await readSourceFromFile();
const code = defaultFile.value;
Expand Down

0 comments on commit ec68856

Please # to comment.