-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance external image hosting in agent configuration (#4565)
* Strengthen external image hosting in agent configuration * ✂️ * ✂️ * ✂️ * Apply new DFS logic to content fragment + refactor * Accept static hosted Dust Avatars
- Loading branch information
Showing
8 changed files
with
170 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { EnvironmentConfig } from "@dust-tt/types"; | ||
|
||
const config = { | ||
getServiceAccount: (): string => { | ||
return EnvironmentConfig.getEnvVariable("SERVICE_ACCOUNT"); | ||
}, | ||
getGcsPublicUploadBucket: (): string => { | ||
return EnvironmentConfig.getEnvVariable("DUST_UPLOAD_BUCKET"); | ||
}, | ||
getGcsPrivateUploadsBucket: (): string => { | ||
return EnvironmentConfig.getEnvVariable("DUST_PRIVATE_UPLOADS_BUCKET"); | ||
}, | ||
}; | ||
|
||
export default config; |
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,92 @@ | ||
import type { Bucket } from "@google-cloud/storage"; | ||
import { Storage } from "@google-cloud/storage"; | ||
import type formidable from "formidable"; | ||
import fs from "fs"; | ||
import { pipeline } from "stream/promises"; | ||
|
||
import config from "@app/lib/dfs/config"; | ||
|
||
type BucketKeyType = "PRIVATE_UPLOAD" | "PUBLIC_UPLOAD"; | ||
|
||
const storage = new Storage({ | ||
keyFilename: config.getServiceAccount(), | ||
}); | ||
|
||
const bucketKeysToBucket: Record<BucketKeyType, Bucket> = { | ||
PRIVATE_UPLOAD: storage.bucket(config.getGcsPrivateUploadsBucket()), | ||
PUBLIC_UPLOAD: storage.bucket(config.getGcsPublicUploadBucket()), | ||
}; | ||
|
||
class DFS { | ||
private readonly bucket: Bucket; | ||
|
||
constructor(bucketKey: BucketKeyType) { | ||
this.bucket = bucketKeysToBucket[bucketKey]; | ||
} | ||
|
||
/** | ||
* Upload functions. | ||
*/ | ||
|
||
async uploadFileToBucket(file: formidable.File, destPath: string) { | ||
const gcsFile = this.file(destPath); | ||
const fileStream = fs.createReadStream(file.filepath); | ||
|
||
await pipeline( | ||
fileStream, | ||
gcsFile.createWriteStream({ | ||
metadata: { | ||
contentType: file.mimetype, | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
async uploadRawContentToBucket({ | ||
content, | ||
contentType, | ||
filePath, | ||
}: { | ||
content: string; | ||
contentType: string; | ||
filePath: string; | ||
}) { | ||
const gcsFile = this.file(filePath); | ||
|
||
await gcsFile.save(content, { | ||
contentType, | ||
}); | ||
} | ||
|
||
/** | ||
* Download functions. | ||
*/ | ||
|
||
async fetchFileContent(filePath: string) { | ||
const gcsFile = this.file(filePath); | ||
|
||
const [content] = await gcsFile.download(); | ||
|
||
return content.toString(); | ||
} | ||
|
||
async getFileContentType(filename: string): Promise<string | null> { | ||
const gcsFile = this.file(filename); | ||
|
||
const [metadata] = await gcsFile.getMetadata(); | ||
|
||
return metadata.contentType; | ||
} | ||
|
||
file(filename: string) { | ||
return this.bucket.file(filename); | ||
} | ||
|
||
get name() { | ||
return this.bucket.name; | ||
} | ||
} | ||
|
||
export const getPrivateUploadBucket = () => new DFS("PRIVATE_UPLOAD"); | ||
|
||
export const getPublicUploadBucket = () => new DFS("PUBLIC_UPLOAD"); |
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
50 changes: 20 additions & 30 deletions
50
front/pages/api/w/[wId]/assistant/agent_configurations/avatar.ts
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