-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2fedb5
commit 20c2968
Showing
7 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
const ScTranslations = require("./ScTranslations"); | ||
const { contains_folder_reference } = require("./contains_folder_reference"); | ||
const { contains_internal_link } = require("./contains_internal_link"); | ||
const { extract_internal_links } = require("./extract_internal_links"); | ||
const { extract_folder_references } = require("./extract_folder_references"); | ||
const { contains_system_prompt_ref, extract_system_prompt_ref } = require("./contains_system_prompt_ref"); | ||
|
||
// check if includes keywords referring to one's own notes | ||
async function contains_self_referential_keywords(env, user_input, language) { | ||
const language_settings = ScTranslations[language]; | ||
if (!language_settings) return false; | ||
let check_str = `${user_input}`; | ||
if(contains_internal_link(check_str)){ | ||
const extracted_links = extract_internal_links({}, check_str); | ||
for(const link of extracted_links){ | ||
check_str = check_str.replace(link, ''); | ||
} | ||
} | ||
if(contains_folder_reference(check_str)){ | ||
const folders = await env.plugin.get_folders(); // get folder references | ||
const extracted_folder_references = extract_folder_references(folders, check_str); | ||
for(const folder_reference of extracted_folder_references){ | ||
check_str = check_str.replace(folder_reference, ''); | ||
} | ||
} | ||
if(contains_system_prompt_ref(check_str)){ | ||
const {mention, mention_pattern} = extract_system_prompt_ref(check_str); | ||
check_str = check_str.replace(mention_pattern, ''); | ||
} | ||
|
||
if (check_str.match(new RegExp(`\\b(${language_settings.pronouns.join("|")})\\b`, "gi"))) return true; | ||
return false; | ||
} | ||
exports.contains_self_referential_keywords = contains_self_referential_keywords; |
This file contains 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,76 @@ | ||
const test = require('ava'); | ||
const { contains_self_referential_keywords } = require('./contains_self_referential_keywords'); | ||
const mock_env = { | ||
plugin: { | ||
get_folders: () => { | ||
return [ | ||
'/my notes/as context/', | ||
]; | ||
} | ||
} | ||
}; | ||
|
||
test('returns true for input containing self-referential pronouns in English', async t => { | ||
const user_input = 'I want to review my notes'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns true for input containing self-referential pronouns in Spanish', async t => { | ||
const user_input = 'Quiero revisar mis notas'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'es')); | ||
}); | ||
|
||
test('returns false for input without self-referential pronouns', async t => { | ||
const user_input = 'The sky is blue'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns true for input with uppercase self-referential pronouns', async t => { | ||
const user_input = 'MY notes are important to ME'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns true for input with mixed case self-referential pronouns', async t => { | ||
const user_input = 'Show Me My latest notes'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns false for input with partial matches of pronouns ("myself" contains "my")', async t => { | ||
const user_input = 'The word "myself" is not always self-referential'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns true for input with pronouns at the beginning or end of sentences', async t => { | ||
const user_input = 'My thoughts are clear. These ideas belong to me.'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('returns false for empty input', async t => { | ||
const user_input = ''; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('handles unsupported language gracefully', async t => { | ||
const user_input = 'This is a test'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'unsupported_language')); | ||
}); | ||
|
||
test('returns true for input containing multiple self-referential pronouns', async t => { | ||
const user_input = 'I need to organize my notes so I can find them easily'; | ||
t.true(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('excludes self-referential pronouns that are within links', async t => { | ||
const user_input = 'Should use [[my notes]] as context without lookup'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('excludes self-referential pronouns that are within /folder/paths/', async t => { | ||
const user_input = 'Should not match /my notes/as context/ as self-referential'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); | ||
|
||
test('excludes self-referential pronouns that are within @"system prompt" refs', async t => { | ||
const user_input = 'Should use @"my system prompt" in this query'; | ||
t.false(await contains_self_referential_keywords(mock_env, user_input, 'en')); | ||
}); |
This file contains 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,11 @@ | ||
function contains_system_prompt_ref(content) { | ||
return content.includes("@\""); | ||
} | ||
function extract_system_prompt_ref(content) { | ||
const mention_pattern = /@\"([^"]+)\"/; | ||
const mention = content.match(mention_pattern)[1]; | ||
return { mention, mention_pattern }; | ||
} | ||
|
||
exports.contains_system_prompt_ref = contains_system_prompt_ref; | ||
exports.extract_system_prompt_ref = extract_system_prompt_ref; |
This file contains 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,12 @@ | ||
function extract_internal_links(env, user_input) { | ||
const matches = user_input.match(/\[\[(.*?)\]\]/g); | ||
console.log(matches); | ||
// return array of TFile objects | ||
if (matches && env.plugin) return matches.map(match => { | ||
const tfile = env.plugin.app.metadataCache.getFirstLinkpathDest(match.replace("[[", "").replace("]]", ""), "/"); | ||
return tfile; | ||
}); | ||
if (matches) return matches; | ||
return []; | ||
} | ||
exports.extract_internal_links = extract_internal_links; |
This file contains 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 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