-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(server): context awareness for copilot #9611
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
Merged
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| #![deny(clippy::all)] | ||
|
|
||
| mod utils; | ||
|
|
||
| pub mod doc_loader; | ||
| pub mod file_type; | ||
| pub mod hashcash; | ||
|
|
||
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,111 @@ | ||
| fn collapse_whitespace(s: &str) -> String { | ||
| let mut result = String::new(); | ||
| let mut prev_was_whitespace = false; | ||
| for c in s.chars() { | ||
| if c.is_whitespace() { | ||
| if !prev_was_whitespace { | ||
| result.push(' '); | ||
| prev_was_whitespace = true; | ||
| } | ||
| } else { | ||
| result.push(c); | ||
| prev_was_whitespace = false; | ||
| } | ||
| } | ||
| result | ||
| } | ||
|
|
||
| fn try_remove_label(s: &str, i: usize) -> Option<usize> { | ||
| let mut next_idx = match s[i..].to_ascii_lowercase() { | ||
| s if s.starts_with("figure") => i + 6, | ||
| s if s.starts_with("table") => i + 5, | ||
| _ => return None, | ||
| }; | ||
|
|
||
| if next_idx >= s.len() { | ||
| return None; | ||
| } | ||
|
|
||
| if let Some(ch) = s[next_idx..].chars().next() { | ||
| if !ch.is_whitespace() { | ||
| return None; | ||
| } | ||
| } else { | ||
| return None; | ||
| } | ||
|
|
||
| while next_idx < s.len() { | ||
| let ch = s[next_idx..].chars().next()?; | ||
| if ch.is_whitespace() { | ||
| next_idx += ch.len_utf8(); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| let start_digits = next_idx; | ||
| while next_idx < s.len() { | ||
| let ch = s[next_idx..].chars().next()?; | ||
| if ch.is_ascii_digit() { | ||
| next_idx += ch.len_utf8(); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if next_idx == start_digits { | ||
| return None; | ||
| } | ||
|
|
||
| if let Some(ch) = s[next_idx..].chars().next() { | ||
| if ch == '.' { | ||
| next_idx += ch.len_utf8(); | ||
| return Some(next_idx); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| fn remove_label(s: &str) -> String { | ||
| let mut result = String::with_capacity(s.len()); | ||
| let mut i = 0; | ||
| while i < s.len() { | ||
| if let Some(next_idx) = try_remove_label(s, i) { | ||
| i = next_idx; | ||
| continue; | ||
| } | ||
|
|
||
| let ch = s[i..].chars().next().unwrap(); | ||
| result.push(ch); | ||
| i += ch.len_utf8(); | ||
| } | ||
| result | ||
| } | ||
|
|
||
| pub fn clean_content(content: &str) -> String { | ||
| let content = content.replace("\x00", ""); | ||
| remove_label(&collapse_whitespace(&content)) | ||
| .trim() | ||
| .to_string() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_clean_input() { | ||
| let inputs = [ | ||
| "FIGURE 1. This is a\t test\n\nwith multiple lines", | ||
| "table 2. Another test\x00 with null", | ||
| "Some text \t\n without label", | ||
| ]; | ||
| let cleaned = [ | ||
| "This is a test with multiple lines", | ||
| "Another test with null", | ||
| "Some text without label", | ||
| ]; | ||
|
|
||
| assert_eq!(cleaned, inputs.map(clean_content)); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
packages/backend/server/migrations/20250210090228_ai_context_embedding/migration.sql
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,75 @@ | ||
| DO $$ | ||
| DECLARE error_message TEXT; | ||
| BEGIN -- check if pgvector extension is installed | ||
| IF NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector') THEN | ||
| BEGIN | ||
| -- CreateExtension | ||
| CREATE EXTENSION IF NOT EXISTS "vector"; | ||
| EXCEPTION | ||
| WHEN OTHERS THEN | ||
| -- if not found and cannot create extension, raise the exception | ||
| error_message := 'pgvector extension not found.' || E'\n' || | ||
| '****************************************************************************' || E'\n' || | ||
| '* *' || E'\n' || | ||
| '* NOTICE: From AFFiNE 0.20 onwards, the copilot module will depend *' || E'\n' || | ||
| '* on pgvector. *' || E'\n' || | ||
| '* *' || E'\n' || | ||
| '* 1. If you are using the official PostgreSQL Docker container, *' || E'\n' || | ||
| '* please switch to the pgvector/pgvector:pg${VERSION} container, *' || E'\n' || | ||
| '* where ${VERSION} is the major version of your PostgreSQL container. *' || E'\n' || | ||
| '* *' || E'\n' || | ||
| '* 2. If you are using a self-installed PostgreSQL, please follow the *' || E'\n' || | ||
| '* the official pgvector installation guide to install it into your *' || E'\n' || | ||
| '* database: https://github.com/pgvector/pgvector?tab=readme-ov- *' || E'\n' || | ||
| '* file#installation-notes---linux-and-mac *' || E'\n' || | ||
| '* *' || E'\n' || | ||
| '****************************************************************************'; | ||
|
|
||
| RAISE WARNING '%', error_message; | ||
| END; | ||
| END IF; | ||
| -- check again, initialize the tables if the extension is installed | ||
| IF EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector') THEN | ||
| -- CreateTable | ||
| CREATE TABLE "ai_context_embeddings" ( | ||
| "id" VARCHAR NOT NULL, | ||
| "context_id" VARCHAR NOT NULL, | ||
| "file_id" VARCHAR NOT NULL, | ||
| "chunk" INTEGER NOT NULL, | ||
| "content" VARCHAR NOT NULL, | ||
| "embedding" vector(512) NOT NULL, | ||
| "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updated_at" TIMESTAMPTZ(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "ai_context_embeddings_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "ai_workspace_embeddings" ( | ||
| "workspace_id" VARCHAR NOT NULL, | ||
| "doc_id" VARCHAR NOT NULL, | ||
| "chunk" INTEGER NOT NULL, | ||
| "content" VARCHAR NOT NULL, | ||
| "embedding" vector(512) NOT NULL, | ||
| "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updated_at" TIMESTAMPTZ(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "ai_workspace_embeddings_pkey" PRIMARY KEY ("workspace_id","doc_id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX IF NOT EXISTS "ai_context_embeddings_idx" ON ai_context_embeddings USING hnsw (embedding vector_cosine_ops); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "ai_context_embeddings_context_id_file_id_chunk_key" ON "ai_context_embeddings"("context_id", "file_id", "chunk"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX IF NOT EXISTS "ai_workspace_embeddings_idx" ON ai_workspace_embeddings USING hnsw (embedding vector_cosine_ops); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "ai_context_embeddings" ADD CONSTRAINT "ai_context_embeddings_context_id_fkey" FOREIGN KEY ("context_id") REFERENCES "ai_contexts"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "ai_workspace_embeddings" ADD CONSTRAINT "ai_workspace_embeddings_workspace_id_doc_id_fkey" FOREIGN KEY ("workspace_id", "doc_id") REFERENCES "snapshots"("workspace_id", "guid") ON DELETE CASCADE ON UPDATE CASCADE; | ||
| END IF; | ||
| END $$; | ||
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
Binary file modified
BIN
+92 Bytes
(120%)
packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.snap
Binary file not shown.
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.
Uh oh!
There was an error while loading. Please reload this page.