Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CodeEditor AI driven auto completion #484

Merged
merged 5 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@types/file-saver": "^2.0.7",
"@types/ms": "^0.7.34",
"@typescript/ata": "^0.9.7",
"@valtown/codemirror-codeium": "^1.1.1",
"@valtown/codemirror-ts": "^2.2.0",
"@vercel/analytics": "^1.4.1",
"change-case": "^5.4.4",
Expand Down
22 changes: 21 additions & 1 deletion src/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { EditorView } from "codemirror"
import { useEffect, useMemo, useRef, useState } from "react"
import ts from "typescript"
import CodeEditorHeader from "./CodeEditorHeader"

import { copilotPlugin, Language } from "@valtown/codemirror-codeium"
import { useCodeCompletionApi } from "@/hooks/use-code-completion-ai-api"
const defaultImports = `
import React from "@types/react/jsx-runtime"
import { Circuit, createUseComponent } from "@tscircuit/core"
Expand Down Expand Up @@ -56,6 +57,7 @@ export const CodeEditor = ({
const viewRef = useRef<EditorView | null>(null)
const ataRef = useRef<ReturnType<typeof setupTypeAcquisition> | null>(null)
const apiUrl = useSnippetsBaseApiUrl()
const codeCompletionApi = useCodeCompletionApi()

const [cursorPosition, setCursorPosition] = useState<number | null>(null)
const [code, setCode] = useState(initialCode)
Expand Down Expand Up @@ -211,6 +213,24 @@ export const CodeEditor = ({
}
}),
]
if (codeCompletionApi?.apiKey) {
baseExtensions.push(
copilotPlugin({
apiKey: codeCompletionApi.apiKey,
language: Language.TYPESCRIPT,
}),
EditorView.theme({
".cm-ghostText, .cm-ghostText *": {
opacity: "0.6",
filter: "grayscale(20%)",
cursor: "pointer",
},
".cm-ghostText:hover": {
background: "#eee",
},
}),
)
}

// Add TypeScript-specific extensions and handlers
const tsExtensions =
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/use-code-completion-ai-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useMemo } from "react"

export const useCodeCompletionApi = () => {
const codeiumApiKey = useMemo(() => {
return {
apiKey: import.meta.env.VITE_CODIUM_API_KEY,
}
}, [])

return codeiumApiKey
Abse2001 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading