-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daishan Peng <daishan@acorn.io>
- Loading branch information
1 parent
c3edb5b
commit 0fec3f8
Showing
11 changed files
with
464 additions
and
85 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
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,45 @@ | ||
'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 }> | ||
> { | ||
const dir = path.join( | ||
WORKSPACE_DIR(), | ||
'knowledge', | ||
'integrations', | ||
'onedrive' | ||
); | ||
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 documentID in metadata) { | ||
result.set(path.join(dir, documentID, metadata[documentID].fileName), { | ||
url: metadata[documentID].url, | ||
fileName: metadata[documentID].fileName, | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
// 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 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,62 @@ | ||
'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@46a273e'; | ||
} else if (tool === 'onedrive') { | ||
toolUrl = 'github.com/gptscript-ai/knowledge-onedrive-integration@a85a498'; | ||
} | ||
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; | ||
} | ||
|
||
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 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; | ||
} |
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
Oops, something went wrong.