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 3 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
18 changes: 17 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 { useCodeCompletionAiApi } 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 = useCodeCompletionAiApi()

const [cursorPosition, setCursorPosition] = useState<number | null>(null)
const [code, setCode] = useState(initialCode)
Expand Down Expand Up @@ -180,6 +182,20 @@ export const CodeEditor = ({
: javascript({ typescript: true, jsx: true }),
keymap.of([indentWithTab]),
EditorState.readOnly.of(readOnly),
copilotPlugin({
apiKey: codeCompletionApi.apiKey,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you enable only if the codeCompletionApi.apiKey is defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

language: Language.TYPESCRIPT,
}),
EditorView.theme({
".cm-ghostText, .cm-ghostText *": {
opacity: "0.6",
filter: "grayscale(20%)",
cursor: "pointer",
},
".cm-ghostText:hover": {
background: "#eee",
},
}),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
const newContent = update.state.doc.toString()
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/use-code-completion-ai-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useMemo } from "react"
import { useGlobalStore } from "./use-global-store"

export const useCodeCompletionAiApi = () => {
const sessionToken = useGlobalStore((state) => state.session?.token)
const codeium = useMemo(() => {
if (import.meta.env.VITE_USE_DIRECT_AI_REQUESTS === "true") {
console.warn(
"Direct AI requests are enabled. Do not use this in production.",
)
}
return import.meta.env.VITE_USE_DIRECT_AI_REQUESTS === "true"
? {
apiKey: import.meta.env.VITE_CODE_COMPLETION_API_KEY,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what service?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make the API_KEY indicate the service being used? Is it anthropic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seveibar it should be codium api key?
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then we should name the api key VITE_CODIUM_API_KEY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you test this? I'm not sure codium offers an api

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the user would have to provide a codium key? I'm a bit confused how we use this in practice

}
: {
apiKey: "{REPLACE_ON_SERVER}",
}
}, [])

return codeium
}
Loading