-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Feat: ai integration [half-way to completion] #2767
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
Draft
Elitex07
wants to merge
10
commits into
Acode-Foundation:main
Choose a base branch
from
Elitex07:feat/ai-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,047
−324
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6bbfb5b
feat: Add initial AI Agent integration
Elitex07 7a4143b
fix: AI Settings not opening due to async import
Elitex07 c8a59e7
fix: Synchronous load for aiAutocomplete and fix AI sidebar icon
Elitex07 fb430ec
fix: App crash due to circular dependencies on launch
Elitex07 d19cebe
refactor: Remove AcodeProAdapter, simplify AIService to use direct pr…
Elitex07 dc6d47e
feat: Streaming responses, context-aware AI, per-file history, Apply-…
Elitex07 104bc17
feat: Agentic AI with Think->Act->Observe loop, tool calling, and liv…
Elitex07 f9128e7
fix: Rewrite AI Agent SCSS to follow Acode CSS variable conventions
Elitex07 33809cd
fix: AI settings page styling, add model config, remove unused providers
Elitex07 4a0495c
fix: Use valid icons for Agent mode toggle and send button
Elitex07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { ViewPlugin, Decoration, WidgetType, EditorView } from "@codemirror/view"; | ||
| import { StateField, StateEffect } from "@codemirror/state"; | ||
|
|
||
| class GhostTextWidget extends WidgetType { | ||
| constructor(public text: string) { | ||
| super(); | ||
| } | ||
|
|
||
| eq(other: GhostTextWidget) { | ||
| return this.text === other.text; | ||
| } | ||
|
|
||
| toDOM() { | ||
| const span = document.createElement("span"); | ||
| span.className = "cm-ai-ghost-text"; | ||
| span.textContent = this.text; | ||
| span.style.opacity = "0.5"; | ||
| span.style.fontStyle = "italic"; | ||
| return span; | ||
| } | ||
| } | ||
|
|
||
| const setGhostText = StateEffect.define<{ pos: number; text: string } | null>(); | ||
|
|
||
| export const ghostTextState = StateField.define<Decoration>({ | ||
| create() { | ||
| return Decoration.none; | ||
| }, | ||
| update(decorations, tr) { | ||
| decorations = decorations.map(tr.changes); | ||
| for (let e of tr.effects) { | ||
| if (e.is(setGhostText)) { | ||
| if (e.value === null) { | ||
| return Decoration.none; | ||
| } | ||
| return Decoration.set([ | ||
| Decoration.widget({ | ||
| widget: new GhostTextWidget(e.value.text), | ||
| side: 1, | ||
| }).range(e.value.pos), | ||
| ]); | ||
| } | ||
| } | ||
| // If document changed, clear ghost text | ||
| if (tr.docChanged) { | ||
| return Decoration.none; | ||
| } | ||
| return decorations; | ||
| }, | ||
| provide: (f) => EditorView.decorations.from(f), | ||
| }); | ||
|
|
||
| export const aiAutocompletePlugin = ViewPlugin.fromClass( | ||
| class { | ||
| timeout: any; | ||
| constructor(public view: any) {} | ||
|
|
||
| update(update: any) { | ||
| if (update.docChanged) { | ||
| import("lib/settings").then((mSettings) => { | ||
| const appSettings = mSettings.default; | ||
| if (appSettings.value.ai?.provider) { | ||
| clearTimeout(this.timeout); | ||
| this.view.dispatch({ effects: setGhostText.of(null) }); | ||
|
|
||
| this.timeout = setTimeout(async () => { | ||
| try { | ||
| const pos = this.view.state.selection.main.head; | ||
| const doc = this.view.state.doc.toString(); | ||
| const prefix = doc.slice(0, pos); | ||
| const suffix = doc.slice(pos); | ||
|
|
||
| const { default: aiService } = await import("utils/ai/AIService"); | ||
| const completion = await aiService.completeCode(prefix, suffix); | ||
| if (completion) { | ||
| this.view.dispatch({ | ||
| effects: setGhostText.of({ pos, text: completion }), | ||
| }); | ||
| } | ||
| } catch (e) { | ||
| // Ignore errors or log them | ||
| } | ||
| }, 1000); // 1s debounce | ||
| } | ||
| }).catch(() => {}); | ||
| } | ||
| } | ||
|
|
||
| destroy() { | ||
| clearTimeout(this.timeout); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| // We also need a keymap to accept the suggestion | ||
| import { keymap } from "@codemirror/view"; | ||
| export const acceptAiSuggestionKeymap = keymap.of([ | ||
| { | ||
| key: "Tab", | ||
| run: (view) => { | ||
| const ghost = view.state.field(ghostTextState, false); | ||
| if (ghost && ghost.size > 0) { | ||
| let textToInsert = ""; | ||
| ghost.between(0, view.state.doc.length, (from, to, deco) => { | ||
| textToInsert = (deco.spec.widget as GhostTextWidget).text; | ||
| }); | ||
| if (textToInsert) { | ||
| const pos = view.state.selection.main.head; | ||
| view.dispatch({ | ||
| changes: { from: pos, insert: textToInsert }, | ||
| effects: setGhostText.of(null), | ||
| }); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| export function aiAutocomplete() { | ||
| return [ghostTextState, aiAutocompletePlugin, acceptAiSuggestionKeymap]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a ghost completion is visible and the user moves the cursor or changes the selection without editing, the plugin retains the decoration but inserts its text at the current selection head, causing a suggestion generated for one location to modify a different part of the document.
Knowledge Base Used: Editor Core (CodeMirror)