Skip to content

Commit 060b0e2

Browse files
Prevent focus on ai assistant
1 parent a056d4c commit 060b0e2

File tree

2 files changed

+70
-68
lines changed

2 files changed

+70
-68
lines changed

js/ai.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
import theme from "./theme.js";
3+
import configuration from "./configuration.js";
34
import { sourceEditor } from "./ide.js";
45

56
const THREAD = [
@@ -124,6 +125,9 @@ document.addEventListener("keydown", function (e) {
124125
if (e.metaKey || e.ctrlKey) {
125126
switch (e.key) {
126127
case "p":
128+
if (!configuration.get("appOptions.showAIAssistant")) {
129+
break;
130+
}
127131
e.preventDefault();
128132
document.getElementById("judge0-chat-user-input").focus();
129133
break;

js/ide.js

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -575,77 +575,75 @@ document.addEventListener("DOMContentLoaded", async function () {
575575

576576
sourceEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, run);
577577

578-
if (configuration.get("appOptions.showAIAssistant")) {
579-
monaco.languages.registerInlineCompletionsProvider('*', {
580-
provideInlineCompletions: async (model, position) => {
581-
if (!puter.auth.isSignedIn() || !document.getElementById("judge0-inline-suggestions").checked) {
582-
return;
583-
}
578+
monaco.languages.registerInlineCompletionsProvider('*', {
579+
provideInlineCompletions: async (model, position) => {
580+
if (!puter.auth.isSignedIn() || !document.getElementById("judge0-inline-suggestions").checked || !configuration.get("appOptions.showAIAssistant")) {
581+
return;
582+
}
584583

585-
const textBeforeCursor = model.getValueInRange({
586-
startLineNumber: 1,
587-
startColumn: 1,
588-
endLineNumber: position.lineNumber,
589-
endColumn: position.column
590-
});
591-
592-
const textAfterCursor = model.getValueInRange({
593-
startLineNumber: position.lineNumber,
594-
startColumn: position.column,
595-
endLineNumber: model.getLineCount(),
596-
endColumn: model.getLineMaxColumn(model.getLineCount())
597-
});
598-
599-
const aiResponse = await puter.ai.chat([{
600-
role: "user",
601-
content: `You are a code completion assistant. Given the following context, generate the most likely code completion.
602-
603-
### Code Before Cursor:
604-
${textBeforeCursor}
605-
606-
### Code After Cursor:
607-
${textAfterCursor}
608-
609-
### Instructions:
610-
- Predict the next logical code segment.
611-
- Ensure the suggestion is syntactically and contextually correct.
612-
- Keep the completion concise and relevant.
613-
- Do not repeat existing code.
614-
- Provide only the missing code.
615-
- **Respond with only the code, without markdown formatting.**
616-
- **Do not include triple backticks (\`\`\`) or additional explanations.**
617-
618-
### Completion:`.trim()
619-
}], {
620-
model: document.getElementById("judge0-chat-model-select").value,
621-
});
622-
623-
let aiResponseValue = aiResponse?.toString().trim() || "";
624-
625-
if (Array.isArray(aiResponseValue)) {
626-
aiResponseValue = aiResponseValue.map(v => v.text).join("\n").trim();
627-
}
584+
const textBeforeCursor = model.getValueInRange({
585+
startLineNumber: 1,
586+
startColumn: 1,
587+
endLineNumber: position.lineNumber,
588+
endColumn: position.column
589+
});
590+
591+
const textAfterCursor = model.getValueInRange({
592+
startLineNumber: position.lineNumber,
593+
startColumn: position.column,
594+
endLineNumber: model.getLineCount(),
595+
endColumn: model.getLineMaxColumn(model.getLineCount())
596+
});
597+
598+
const aiResponse = await puter.ai.chat([{
599+
role: "user",
600+
content: `You are a code completion assistant. Given the following context, generate the most likely code completion.
601+
602+
### Code Before Cursor:
603+
${textBeforeCursor}
604+
605+
### Code After Cursor:
606+
${textAfterCursor}
607+
608+
### Instructions:
609+
- Predict the next logical code segment.
610+
- Ensure the suggestion is syntactically and contextually correct.
611+
- Keep the completion concise and relevant.
612+
- Do not repeat existing code.
613+
- Provide only the missing code.
614+
- **Respond with only the code, without markdown formatting.**
615+
- **Do not include triple backticks (\`\`\`) or additional explanations.**
616+
617+
### Completion:`.trim()
618+
}], {
619+
model: document.getElementById("judge0-chat-model-select").value,
620+
});
621+
622+
let aiResponseValue = aiResponse?.toString().trim() || "";
623+
624+
if (Array.isArray(aiResponseValue)) {
625+
aiResponseValue = aiResponseValue.map(v => v.text).join("\n").trim();
626+
}
628627

629-
if (!aiResponseValue || aiResponseValue.length === 0) {
630-
return;
631-
}
628+
if (!aiResponseValue || aiResponseValue.length === 0) {
629+
return;
630+
}
632631

633-
return {
634-
items: [{
635-
insertText: aiResponseValue,
636-
range: new monaco.Range(
637-
position.lineNumber,
638-
position.column,
639-
position.lineNumber,
640-
position.column
641-
)
642-
}]
643-
};
644-
},
645-
handleItemDidShow: () => { },
646-
freeInlineCompletions: () => { }
647-
});
648-
}
632+
return {
633+
items: [{
634+
insertText: aiResponseValue,
635+
range: new monaco.Range(
636+
position.lineNumber,
637+
position.column,
638+
position.lineNumber,
639+
position.column
640+
)
641+
}]
642+
};
643+
},
644+
handleItemDidShow: () => { },
645+
freeInlineCompletions: () => { }
646+
});
649647
});
650648

651649
layout.registerComponent("stdin", function (container, state) {

0 commit comments

Comments
 (0)