Skip to content
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

Download updates #594

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions packages/client-direct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { stringToUuid } from "@ai16z/eliza";
import { settings } from "@ai16z/eliza";
import { createApiRouter } from "./api.ts";
import * as fs from "fs";
import * as path from "path";
const upload = multer({ storage: multer.memoryStorage() });

export const messageHandlerTemplate =
Expand Down Expand Up @@ -295,27 +297,63 @@ export class DirectClient {
}
);
this.app.get(
"/fine-tune/:assetId",
"/fine-tune/:assetId",
async (req: express.Request, res: express.Response) => {
const assetId = req.params.assetId;
const downloadDir = path.join(process.cwd(), 'downloads', assetId);

console.log('Download directory:', downloadDir);

try {
const response = await fetch(`https://api.bageldb.ai/api/v1/asset/${assetId}/download`, {
console.log('Creating directory...');
await fs.promises.mkdir(downloadDir, { recursive: true });

console.log('Fetching file...');
const fileResponse = await fetch(`https://api.bageldb.ai/api/v1/asset/${assetId}/download`, {
headers: {
'X-API-KEY': `${process.env.BAGEL_API_KEY}`
}
});

if (!fileResponse.ok) {
throw new Error(`API responded with status ${fileResponse.status}: ${await fileResponse.text()}`);
}

console.log('Response headers:', fileResponse.headers);

const fileName = fileResponse.headers.get('content-disposition')
?.split('filename=')[1]
?.replace(/"/g, '') || 'default_name.txt';

// Forward the content-type header
res.set('Content-Type', response.headers.get('content-type'));
console.log('Saving as:', fileName);

// Convert ReadableStream to Buffer and send
const arrayBuffer = await response.arrayBuffer();
const arrayBuffer = await fileResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
res.send(buffer);

const filePath = path.join(downloadDir, fileName);
console.log('Full file path:', filePath);

await fs.promises.writeFile(filePath, buffer);

// Verify file was written
const stats = await fs.promises.stat(filePath);
console.log('File written successfully. Size:', stats.size, 'bytes');

res.json({
success: true,
message: 'Single file downloaded successfully',
downloadPath: downloadDir,
fileCount: 1,
fileName: fileName,
fileSize: stats.size
});

} catch (error) {
console.error('Detailed error:', error);
res.status(500).json({
error: 'Failed to forward request to BagelDB',
details: error.message
error: 'Failed to download files from BagelDB',
details: error.message,
stack: error.stack
});
}
}
Expand Down
Loading