Skip to content

Commit cde18b0

Browse files
authored
fix: generated image download path (#482)
1 parent d980fb1 commit cde18b0

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

images/src/analyze.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function analyzeImages(
5959
const supportedMimeTypes = ['image/jpeg', 'image/png', 'image/webp'];
6060
const threadId = process.env.OBOT_THREAD_ID
6161
const obotServerUrl = process.env.OBOT_SERVER_URL
62-
const imageGenBaseUrl = (threadId && obotServerUrl) ? `${obotServerUrl}/api/threads/${threadId}/files/` : null
62+
const imageGenBaseUrl = (threadId && obotServerUrl) ? `${obotServerUrl}/api/threads/${threadId}/file/` : null
6363

6464
async function resolveImageURL (image: string): Promise<string> {
6565
// If the image is a URL, return it as is
@@ -99,9 +99,9 @@ async function readImageFile(path: string): Promise<Buffer> {
9999

100100
// The Generate Images tool returns file paths with a special prefix
101101
// so that they can be rendered in the Obot UI.
102-
// e.g. /api/threads/<thread-id>/files/generated_image_<hash>.webp
102+
// e.g. /api/threads/<thread-id>/file/generated_image_<hash>.webp
103103
// It must be stripped before reading the file from the workspace
104-
path = path.replace(/^\/?api\/threads\/[a-z0-9]+\/files\//, '')
104+
path = path.replace(/^\/?api\/threads\/[a-z0-9]+\/file\//, '')
105105

106106
const client = new GPTScript()
107107
return Buffer.from(await client.readFileInWorkspace(`files/${path}`))

images/src/generate.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type ImageQuality = 'standard' | 'hd';
99

1010
const threadId = process.env.OBOT_THREAD_ID
1111
const obotServerUrl = process.env.OBOT_SERVER_URL
12-
const downloadBaseUrl = (threadId && obotServerUrl) ? `${obotServerUrl}/api/threads/${threadId}/files` : null
12+
const downloadBaseUrl = (threadId && obotServerUrl) ? `${obotServerUrl}/api/threads/${threadId}/file` : null
1313

1414
export async function generateImages(
1515
prompt: string = '',
@@ -81,14 +81,13 @@ async function download(client: gptscript.GPTScript, imageUrl: string): Promise<
8181
responseType: 'arraybuffer'
8282
})
8383
let content = Buffer.from(response.data, 'binary')
84-
8584
// Convert the image to webp format
86-
content = await sharp(content).webp({ quality: 100 }).toBuffer()
85+
content = Buffer.from(await sharp(content).webp({ quality: 100 }).toBuffer())
8786

8887
// Generate a SHA-256 hash of the imageURL to use as the filename
8988
const filePath = `generated_image_${createHash('sha256').update(imageUrl).digest('hex').substring(0, 8)}.webp`;
9089

91-
await client.writeFileInWorkspace(`${threadId ? 'files/' : ''}${filePath}`, content);
90+
await client.writeFileInWorkspace(`${threadId ? 'files/' : ''}${filePath}`, content.buffer);
9291

9392
return filePath
9493
}

0 commit comments

Comments
 (0)