Update content by source via API. Make web client use this API for config

This commit is contained in:
Debanjum Singh Solanky 2023-11-06 23:51:43 -08:00
parent 9ab327a2b6
commit d527b644f4
7 changed files with 32 additions and 29 deletions

View file

@ -9,6 +9,6 @@ The Github integration allows you to index as many repositories as you want. It'
## Use the Github plugin
1. Generate a [classic PAT (personal access token)](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) from [Github](https://github.com/settings/tokens) with `repo` and `admin:org` scopes at least.
2. Navigate to [http://localhost:42110/config/content_type/github](http://localhost:42110/config/content_type/github) to configure your Github settings. Enter in your PAT, along with details for each repository you want to index.
2. Navigate to [http://localhost:42110/config/content-source/github](http://localhost:42110/config/content-source/github) to configure your Github settings. Enter in your PAT, along with details for each repository you want to index.
3. Click `Save`. Go back to the settings page and click `Configure`.
4. Go to [http://localhost:42110/](http://localhost:42110/) and start searching!

View file

@ -8,7 +8,7 @@ We haven't setup a fancy integration with OAuth yet, so this integration still r
![setup_new_integration](https://github.com/khoj-ai/khoj/assets/65192171/b056e057-d4dc-47dc-aad3-57b59a22c68b)
3. Share all the workspaces that you want to integrate with the Khoj integration you just made in the previous step
![enable_workspace](https://github.com/khoj-ai/khoj/assets/65192171/98290303-b5b8-4cb0-b32c-f68c6923a3d0)
4. In the first step, you generated an API key. Use the newly generated API Key in your Khoj settings, by default at http://localhost:42110/config/content_type/notion. Click `Save`.
4. In the first step, you generated an API key. Use the newly generated API Key in your Khoj settings, by default at http://localhost:42110/config/content-source/notion. Click `Save`.
5. Click `Configure` in http://localhost:42110/config to index your Notion workspace(s).
That's it! You should be ready to start searching and chatting. Make sure you've configured your OpenAI API Key for chat.

View file

@ -19,7 +19,7 @@
<p class="card-description">Set repositories to index</p>
</div>
<div class="card-action-row">
<a class="card-button" href="/config/content_type/github">
<a class="card-button" href="/config/content-source/github">
{% if current_model_state.github %}
Update
{% else %}
@ -50,7 +50,7 @@
<p class="card-description">Configure your settings from Notion</p>
</div>
<div class="card-action-row">
<a class="card-button" href="/config/content_type/notion">
<a class="card-button" href="/config/content-source/notion">
{% if current_model_state.content %}
Update
{% else %}
@ -176,8 +176,9 @@
})
};
function clearContentType(content_type) {
fetch('/api/config/data/content_type/' + content_type, {
function clearContentType(content_source) {
fetch('/api/config/data/content-source/' + content_source, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
@ -186,15 +187,15 @@
.then(response => response.json())
.then(data => {
if (data.status == "ok") {
var contentTypeClearButton = document.getElementById("clear-" + content_type);
var contentTypeClearButton = document.getElementById("clear-" + content_source);
contentTypeClearButton.style.display = "none";
var configuredIcon = document.getElementById("configured-icon-" + content_type);
var configuredIcon = document.getElementById("configured-icon-" + content_source);
if (configuredIcon) {
configuredIcon.style.display = "none";
}
var misconfiguredIcon = document.getElementById("misconfigured-icon-" + content_type);
var misconfiguredIcon = document.getElementById("misconfigured-icon-" + content_source);
if (misconfiguredIcon) {
misconfiguredIcon.style.display = "none";
}

View file

@ -125,7 +125,7 @@
}
const csrfToken = document.cookie.split('; ').find(row => row.startsWith('csrftoken'))?.split('=')[1];
fetch('/api/config/data/content_type/github', {
fetch('/api/config/data/content-source/github', {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View file

@ -42,7 +42,7 @@
}
const csrfToken = document.cookie.split('; ').find(row => row.startsWith('csrftoken'))?.split('=')[1];
fetch('/api/config/data/content_type/notion', {
fetch('/api/config/data/content-source/notion', {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View file

@ -61,11 +61,13 @@ api = APIRouter()
logger = logging.getLogger(__name__)
def map_config_to_object(content_type: str):
if content_type == "github":
def map_config_to_object(content_source: str):
if content_source == "github":
return GithubConfig
if content_type == "notion":
if content_source == "notion":
return NotionConfig
if content_source == "computer":
return "Computer"
async def map_config_to_db(config: FullConfig, user: KhojUser):
@ -164,7 +166,7 @@ async def set_config_data(
return state.config
@api.post("/config/data/content_type/github", status_code=200)
@api.post("/config/data/content-source/github", status_code=200)
@requires(["authenticated"])
async def set_content_config_github_data(
request: Request,
@ -192,7 +194,7 @@ async def set_content_config_github_data(
return {"status": "ok"}
@api.post("/config/data/content_type/notion", status_code=200)
@api.post("/config/data/content-source/notion", status_code=200)
@requires(["authenticated"])
async def set_content_config_notion_data(
request: Request,
@ -219,11 +221,11 @@ async def set_content_config_notion_data(
return {"status": "ok"}
@api.delete("/config/data/content_type/{content_type}", status_code=200)
@api.delete("/config/data/content-source/{content_source}", status_code=200)
@requires(["authenticated"])
async def remove_content_config_data(
async def remove_content_source_data(
request: Request,
content_type: str,
content_source: str,
client: Optional[str] = None,
):
user = request.user.object
@ -233,15 +235,15 @@ async def remove_content_config_data(
telemetry_type="api",
api="delete_content_config",
client=client,
metadata={"content_type": content_type},
metadata={"content_source": content_source},
)
content_object = map_config_to_object(content_type)
content_object = map_config_to_object(content_source)
if content_object is None:
raise ValueError(f"Invalid content type: {content_type}")
await content_object.objects.filter(user=user).adelete()
await sync_to_async(EntryAdapters.delete_all_entries)(user, content_type)
raise ValueError(f"Invalid content source: {content_source}")
elif content_object != "Computer":
await content_object.objects.filter(user=user).adelete()
await sync_to_async(EntryAdapters.delete_all_entries_by_source)(user, content_source)
enabled_content = await sync_to_async(EntryAdapters.get_unique_file_types)(user)
return {"status": "ok"}

View file

@ -150,7 +150,7 @@ def config_page(request: Request):
)
@web_client.get("/config/content_type/github", response_class=HTMLResponse)
@web_client.get("/config/content-source/github", response_class=HTMLResponse)
@requires(["authenticated"], redirect="login_page")
def github_config_page(request: Request):
user = request.user.object
@ -177,7 +177,7 @@ def github_config_page(request: Request):
current_config = {} # type: ignore
return templates.TemplateResponse(
"content_type_github_input.html",
"content_source_github_input.html",
context={
"request": request,
"current_config": current_config,
@ -187,7 +187,7 @@ def github_config_page(request: Request):
)
@web_client.get("/config/content_type/notion", response_class=HTMLResponse)
@web_client.get("/config/content-source/notion", response_class=HTMLResponse)
@requires(["authenticated"], redirect="login_page")
def notion_config_page(request: Request):
user = request.user.object
@ -201,7 +201,7 @@ def notion_config_page(request: Request):
current_config = json.loads(current_config.json())
return templates.TemplateResponse(
"content_type_notion_input.html",
"content_source_notion_input.html",
context={
"request": request,
"current_config": current_config,