Skip to content

Commit 40c60d8

Browse files
committed
add handler for image edits
1 parent 4837c8f commit 40c60d8

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/handlers/imageEditsHandler.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { RouterError } from '../errors/RouterError';
2+
import {
3+
constructConfigFromRequestHeaders,
4+
tryTargetsRecursively,
5+
} from './handlerUtils';
6+
import { Context } from 'hono';
7+
8+
/**
9+
* Handles the '/images/edits' API request by selecting the appropriate provider(s) and making the request to them.
10+
*
11+
* @param {Context} c - The Cloudflare Worker context.
12+
* @returns {Promise<Response>} - The response from the provider.
13+
* @throws Will throw an error if no provider options can be determined or if the request to the provider(s) fails.
14+
* @throws Will throw an 500 error if the handler fails due to some reasons
15+
*/
16+
export async function imageEditsHandler(c: Context): Promise<Response> {
17+
try {
18+
let request = await c.req.json();
19+
let requestHeaders = Object.fromEntries(c.req.raw.headers);
20+
const camelCaseConfig = constructConfigFromRequestHeaders(requestHeaders);
21+
22+
const tryTargetsResponse = await tryTargetsRecursively(
23+
c,
24+
camelCaseConfig,
25+
request,
26+
requestHeaders,
27+
'imageEdit',
28+
'POST',
29+
'config'
30+
);
31+
32+
return tryTargetsResponse;
33+
} catch (err: any) {
34+
console.error('imageEdit error: ', err);
35+
let statusCode = 500;
36+
let errorMessage = 'Something went wrong';
37+
38+
if (err instanceof RouterError) {
39+
statusCode = 400;
40+
errorMessage = err.message;
41+
}
42+
return new Response(
43+
JSON.stringify({
44+
status: 'failure',
45+
message: 'Something went wrong',
46+
}),
47+
{
48+
status: 500,
49+
headers: {
50+
'content-type': 'application/json',
51+
},
52+
}
53+
);
54+
}
55+
}

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import filesHandler from './handlers/filesHandler';
3232
import batchesHandler from './handlers/batchesHandler';
3333
import finetuneHandler from './handlers/finetuneHandler';
3434
import { messagesHandler } from './handlers/messagesHandler';
35+
import { imageEditsHandler } from './handlers/imageEditsHandler';
3536

3637
// Config
3738
import conf from '../conf.json';
@@ -150,6 +151,12 @@ app.post('/v1/embeddings', requestValidator, embeddingsHandler);
150151
*/
151152
app.post('/v1/images/generations', requestValidator, imageGenerationsHandler);
152153

154+
/**
155+
* POST route for '/v1/images/edits'.
156+
* Handles requests by passing them to the imageGenerations handler.
157+
*/
158+
app.post('/v1/images/edits', requestValidator, imageEditsHandler);
159+
153160
/**
154161
* POST route for '/v1/audio/speech'.
155162
* Handles requests by passing them to the createSpeechHandler.

src/providers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export type endpointStrings =
9191
| 'stream-messages'
9292
| 'proxy'
9393
| 'imageGenerate'
94+
| 'imageEdit'
9495
| 'createSpeech'
9596
| 'createTranscription'
9697
| 'createTranslation'

0 commit comments

Comments
 (0)