-
Notifications
You must be signed in to change notification settings - Fork 12
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: Adding file through Notion and onedrive #401
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6d769e8
Feat: Adding file through Notion
StrongMonkey 287bcfa
Address comments
StrongMonkey 7e8f366
Use external link
StrongMonkey d124712
Add onedrive integration
StrongMonkey d60a877
Address comments
StrongMonkey 69950f1
Set sync error when resyncing
StrongMonkey 8afa300
Address comments
StrongMonkey 1afc05a
Update nextui package
StrongMonkey 5b0f5c4
Sort table based on name, fix select with shift
StrongMonkey 24915cf
Address ryan's comment
StrongMonkey 5254c8f
Addressing craig's UX feedback
StrongMonkey 813a86b
Rebase
StrongMonkey 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 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
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,38 @@ | ||
'use server'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { WORKSPACE_DIR } from '@/config/env'; | ||
import { runSyncTool } from '@/actions/knowledge/tool'; | ||
|
||
export async function isNotionConfigured() { | ||
return fs.existsSync( | ||
path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'notion', | ||
'metadata.json' | ||
) | ||
); | ||
} | ||
|
||
export async function getNotionFiles() { | ||
const dir = path.join(WORKSPACE_DIR(), 'knowledge', 'integrations', 'notion'); | ||
const metadataFromFiles = fs.readFileSync(path.join(dir, 'metadata.json')); | ||
const metadata = JSON.parse(metadataFromFiles.toString()); | ||
const result = new Map<string, { url: string; fileName: string }>(); | ||
for (const pageID in metadata) { | ||
const filePath = path.join(dir, pageID, metadata[pageID].filename); | ||
result.set(filePath, { | ||
url: metadata[pageID].url, | ||
fileName: path.basename(filePath), | ||
}); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export async function runNotionSync(authed: boolean): Promise<void> { | ||
return runSyncTool(authed, 'notion'); | ||
} |
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,77 @@ | ||
'use server'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { WORKSPACE_DIR } from '@/config/env'; | ||
import { runSyncTool } from '@/actions/knowledge/tool'; | ||
|
||
export async function isOneDriveConfigured() { | ||
return fs.existsSync( | ||
path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'onedrive', | ||
'metadata.json' | ||
) | ||
); | ||
} | ||
|
||
export async function getOneDriveFiles(): Promise< | ||
Map<string, { url: string; fileName: string; displayName: string }> | ||
> { | ||
const dir = path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'onedrive' | ||
); | ||
const metadataFromFile = fs.readFileSync(path.join(dir, 'metadata.json')); | ||
const metadata = JSON.parse(metadataFromFile.toString()); | ||
const result = new Map< | ||
string, | ||
{ url: string; fileName: string; displayName: string } | ||
>(); | ||
for (const documentID in metadata) { | ||
result.set(path.join(dir, documentID, metadata[documentID].fileName), { | ||
url: metadata[documentID].url, | ||
fileName: metadata[documentID].fileName, | ||
displayName: metadata[documentID].displayName, | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
export async function syncSharedLink(link: string): Promise<void> { | ||
const dir = path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'onedrive' | ||
); | ||
const externalLinkFile = path.join(dir, 'externalLinks.json'); | ||
if (!fs.existsSync(externalLinkFile)) { | ||
fs.writeFileSync(externalLinkFile, '{}'); | ||
} | ||
|
||
const externalLink = JSON.parse(fs.readFileSync(externalLinkFile).toString()); | ||
externalLink[link] = 'true'; | ||
fs.writeFileSync(externalLinkFile, JSON.stringify(externalLink)); | ||
|
||
await runSyncTool(true, 'onedrive'); | ||
return; | ||
} | ||
|
||
export async function clearOneDriveFiles(): Promise<void> { | ||
const dir = path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'onedrive' | ||
); | ||
const externalLinkFile = path.join(dir, 'externalLinks.json'); | ||
fs.rmSync(externalLinkFile, { recursive: true, force: true }); | ||
} | ||
|
||
export async function runOneDriveSync(authed: boolean): Promise<void> { | ||
return runSyncTool(authed, 'onedrive'); | ||
} |
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,69 @@ | ||
'use server'; | ||
|
||
import { | ||
GPTScript, | ||
PromptFrame, | ||
Run, | ||
RunEventType, | ||
} from '@gptscript-ai/gptscript'; | ||
import path from 'path'; | ||
import { WORKSPACE_DIR } from '@/config/env'; | ||
import fs from 'fs'; | ||
|
||
export async function runSyncTool( | ||
authed: boolean, | ||
tool: 'notion' | 'onedrive' | ||
): Promise<void> { | ||
const gptscript = new GPTScript({ | ||
DefaultModelProvider: 'github.com/gptscript-ai/gateway-provider', | ||
}); | ||
|
||
let toolUrl = ''; | ||
if (tool === 'notion') { | ||
toolUrl = 'github.com/gptscript-ai/knowledge-notion-integration'; | ||
} else if (tool === 'onedrive') { | ||
toolUrl = 'github.com/gptscript-ai/knowledge-onedrive-integration'; | ||
} | ||
const runningTool = await gptscript.run(toolUrl, { | ||
prompt: true, | ||
}); | ||
if (!authed) { | ||
const handlePromptEvent = (runningTool: Run) => { | ||
return new Promise<string>((resolve) => { | ||
runningTool.on(RunEventType.Prompt, (data: PromptFrame) => { | ||
resolve(data.id); | ||
}); | ||
}); | ||
}; | ||
|
||
const id = await handlePromptEvent(runningTool); | ||
await gptscript.promptResponse({ id, responses: {} }); | ||
} | ||
await runningTool.text(); | ||
return; | ||
} | ||
|
||
/** | ||
* syncFiles syncs all files only when they are selected | ||
* todo: we can stop syncing once file is no longer used by any other script | ||
*/ | ||
export async function syncFiles( | ||
selectedFiles: string[], | ||
type: 'notion' | 'onedrive' | ||
): Promise<void> { | ||
const dir = path.join(WORKSPACE_DIR(), 'knowledge', 'integrations', type); | ||
const metadataFromFiles = fs.readFileSync(path.join(dir, 'metadata.json')); | ||
const metadata = JSON.parse(metadataFromFiles.toString()); | ||
for (const file of selectedFiles) { | ||
const baseDir = path.dirname(path.dirname(file)); | ||
if (baseDir === dir) { | ||
const documentID = path.basename(path.dirname(file)); | ||
const detail = metadata[documentID]; | ||
detail.sync = true; | ||
metadata[documentID] = detail; | ||
} | ||
} | ||
fs.writeFileSync(path.join(dir, 'metadata.json'), JSON.stringify(metadata)); | ||
await runSyncTool(true, type); | ||
return; | ||
} |
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.
Why are we calling path.dirname twice? What is the path we're retrieving? Whatever the answer, it could be a good comment.