Skip to content

Commit 3c76b7d

Browse files
ai suggestions completed
1 parent d4d5ed9 commit 3c76b7d

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

API_Gateway/src/routes.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ const ROUTES = [
3737
},
3838
},
3939
},
40-
// Allow unauthenticated AI suggest if desired (optional). If you want auth, remove this route.
41-
{
42-
url: "/ai",
43-
auth: false,
44-
creditCheck: false,
45-
rateLimit: {
46-
windowMs: 60 * 1000,
47-
limit: 200,
48-
},
49-
proxy: {
50-
target:
51-
process.env.NODE_ENV === "production"
52-
? "http://codespace-service:5000/api"
53-
: "http://localhost:5000/api",
54-
changeOrigin: true,
55-
pathRewrite: {
56-
["^/ai/suggest"]: "/suggest",
57-
["^/ai"]: "",
58-
},
59-
},
60-
},
6140
{
6241
url: "/free",
6342
auth: false,

Frontend/src/App/CodeEditor/MonacoEditor/MonacoEditorModified.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export default function MonacoEditor({
5757
const cursorListenerDisposeRef = useRef<monaco.IDisposable | null>(null);
5858
const inlineCompletionDisposeRef = useRef<monaco.IDisposable | null>(null);
5959
const pendingAbortRef = useRef<AbortController | null>(null);
60+
const aiSuggestionsEnabledRef = useRef<boolean>(true);
61+
const [aiSuggestionsEnabled, setAiSuggestionsEnabled] =
62+
useState<boolean>(true);
6063

6164
const [language, setLanguage] = useState<string>("plaintext");
6265
const [fileUsers, setFileUsers] = useState<CollaborationUser[]>([]);
@@ -267,6 +270,13 @@ export default function MonacoEditor({
267270
token
268271
) => {
269272
try {
273+
// Exit early if AI suggestions are disabled
274+
if (!aiSuggestionsEnabledRef.current) {
275+
console.log("Returrrrrrrrrrrrrrrrrrrrrrrrrrrrrn");
276+
277+
return { items: [], dispose: () => {} };
278+
}
279+
270280
const range = new monaco.Range(
271281
1,
272282
1,
@@ -433,6 +443,41 @@ export default function MonacoEditor({
433443
</div>
434444

435445
<div className="flex items-center gap-2">
446+
<button
447+
onClick={() => {
448+
// Update both the ref and state
449+
aiSuggestionsEnabledRef.current =
450+
!aiSuggestionsEnabledRef.current;
451+
setAiSuggestionsEnabled(aiSuggestionsEnabledRef.current);
452+
console.log(aiSuggestionsEnabledRef.current);
453+
}}
454+
className={`px-2 py-1 text-xs rounded flex items-center gap-1
455+
${
456+
aiSuggestionsEnabled // Use state for rendering
457+
? `bg-blue-500 text-white`
458+
: `bg-gray-300 dark:bg-gray-600 text-gray-700 dark:text-gray-300`
459+
}`}
460+
title={
461+
aiSuggestionsEnabled // Use state for rendering
462+
? "Disable AI suggestions"
463+
: "Enable AI suggestions"
464+
}
465+
>
466+
<svg
467+
xmlns="http://www.w3.org/2000/svg"
468+
className="h-3 w-3"
469+
viewBox="0 0 20 20"
470+
fill="currentColor"
471+
>
472+
<path
473+
fillRule="evenodd"
474+
d="M11.3 1.046A1 1 0 0112 2v5h4a1 1 0 01.82 1.573l-7 10A1 1 0 018 18v-5H4a1 1 0 01-.82-1.573l7-10a1 1 0 011.12-.38z"
475+
clipRule="evenodd"
476+
/>
477+
</svg>
478+
AI {aiSuggestionsEnabled ? "On" : "Off"}{" "}
479+
{/* Use state for rendering */}
480+
</button>
436481
{/* Connection status */}
437482
<div
438483
className={`w-2 h-2 rounded-full ${

Frontend/src/lib/ai/completionClient.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getTokenFromStorage } from "../../utility/utility";
2+
13
export type SuggestPayload = {
24
prefix: string;
35
language?: string;
@@ -9,16 +11,16 @@ export type SuggestResponse = {
911
suggestion: string;
1012
};
1113

12-
// Default to API Gateway in dev; override via VITE_COMPLETION_API_URL when deploying behind same origin
13-
const API_BASE =
14-
(import.meta as any).env?.VITE_COMPLETION_API_URL ||
15-
"http://localhost:4000/ai";
14+
const API_AI = `${import.meta.env?.VITE_BACKEND_URL}/api/suggest`;
15+
16+
const getAuthHeader = () => {
17+
return { Authorization: getTokenFromStorage() };
18+
};
1619

1720
export async function fetchSuggestion(
1821
payload: SuggestPayload,
1922
signal?: AbortSignal
2023
): Promise<string> {
21-
const endpoint = `${API_BASE}/suggest`;
2224
// Apply a client-side timeout to avoid hanging UI. Defaults to 1.2s.
2325
const timeoutMs = 1200;
2426
const controller = new AbortController();
@@ -29,9 +31,12 @@ export async function fetchSuggestion(
2931
signal?.addEventListener("abort", onAbort, { once: true });
3032
timeoutId = setTimeout(() => controller.abort(), timeoutMs);
3133

32-
const res = await fetch(endpoint, {
34+
const res = await fetch(API_AI, {
3335
method: "POST",
34-
headers: { "Content-Type": "application/json" },
36+
headers: {
37+
"Content-Type": "application/json",
38+
...getAuthHeader(),
39+
},
3540
body: JSON.stringify(payload),
3641
signal: signal ?? controller.signal,
3742
});

0 commit comments

Comments
 (0)