-
Notifications
You must be signed in to change notification settings - Fork 96
/
main_cloudflare-workers.mjs
754 lines (735 loc) · 23 KB
/
main_cloudflare-workers.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// node_modules/.deno/[email protected]/node_modules/itty-router/Router.mjs
var r = ({ base: r2 = "", routes: e2 = [], ...a } = {}) => ({ __proto__: new Proxy({}, { get: (a2, t, o, c) => (a3, ...l) => e2.push([t.toUpperCase?.(), RegExp(`^${(c = (r2 + a3).replace(/\/+(\/|$)/g, "$1")).replace(/(\/?\.?):(\w+)\+/g, "($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g, "($1(?<$2>[^$1/]+?))").replace(/\./g, "\\.").replace(/(\/?)\*/g, "($1.*)?")}/*$`), l, c]) && o }), routes: e2, ...a, async fetch(r3, ...t) {
let o, c, l = new URL(r3.url), p = r3.query = { __proto__: null };
for (let [r4, e3] of l.searchParams) p[r4] = p[r4] ? [].concat(p[r4], e3) : e3;
r: try {
for (let e3 of a.before || []) if (null != (o = await e3(r3.proxy ?? r3, ...t))) break r;
e: for (let [a2, p2, f, h] of e2) if ((a2 == r3.method || "ALL" == a2) && (c = l.pathname.match(p2))) {
r3.params = c.groups || {}, r3.route = h;
for (let e3 of f) if (null != (o = await e3(r3.proxy ?? r3, ...t))) break e;
}
} catch (e3) {
if (!a.catch) throw e3;
o = await a.catch(e3, r3.proxy ?? r3, ...t);
}
try {
for (let e3 of a.finally || []) o = await e3(o, r3.proxy ?? r3, ...t) ?? o;
} catch (e3) {
if (!a.catch) throw e3;
o = await a.catch(e3, r3.proxy ?? r3, ...t);
}
return o;
} });
// node_modules/.deno/[email protected]/node_modules/itty-router/cors.mjs
var e = (e2 = {}) => {
const { origin: o = "*", credentials: s = false, allowMethods: c = "*", allowHeaders: r2, exposeHeaders: n, maxAge: t } = e2, a = (e3) => {
const c2 = e3?.headers.get("origin");
return true === o ? c2 : o instanceof RegExp ? o.test(c2) ? c2 : void 0 : Array.isArray(o) ? o.includes(c2) ? c2 : void 0 : o instanceof Function ? o(c2) : "*" == o && s ? c2 : o;
}, l = (e3, o2) => {
for (const [s2, c2] of Object.entries(o2)) c2 && e3.headers.append(s2, c2);
return e3;
};
return { corsify: (e3, o2) => e3?.headers?.get("access-control-allow-origin") || 101 == e3.status ? e3 : l(e3.clone(), { "access-control-allow-origin": a(o2), "access-control-allow-credentials": s }), preflight: (e3) => {
if ("OPTIONS" == e3.method) {
const o2 = new Response(null, { status: 204 });
return l(o2, { "access-control-allow-origin": a(e3), "access-control-allow-methods": c?.join?.(",") ?? c, "access-control-expose-headers": n?.join?.(",") ?? n, "access-control-allow-headers": r2?.join?.(",") ?? r2 ?? e3.headers.get("access-control-request-headers"), "access-control-max-age": t, "access-control-allow-credentials": s });
}
} };
};
// src/gemini-proxy.ts
async function geminiProxy(rawReq) {
const url = new URL(rawReq.url);
url.host = "generativelanguage.googleapis.com";
url.port = "";
url.protocol = "https:";
const req = new Request(url, rawReq);
const resp = await fetch(req);
return new Response(resp.body, resp);
}
// src/utils.ts
function getToken(headers) {
for (const [k, v] of headers) {
if (k.toLowerCase() !== "authorization") continue;
const rawApikey = v.substring(v.indexOf(" ") + 1);
if (!rawApikey.includes("#")) {
return {
apikey: rawApikey,
useBeta: false
};
}
const apikey = rawApikey.substring(0, rawApikey.indexOf("#"));
const params = new URLSearchParams(rawApikey.substring(rawApikey.indexOf("#") + 1));
return {
apikey,
useBeta: params.has("useBeta")
};
}
return null;
}
function parseBase64(base64) {
if (!base64.startsWith("data:")) {
return { text: "" };
}
const [m, data, ..._arr] = base64.split(",");
const mimeType = m.match(/:(?<mime>.*?);/)?.groups?.mime ?? "img/png";
return {
inlineData: {
mimeType,
data
}
};
}
function openAiMessageToGeminiMessage(messages) {
const result = messages.flatMap(({ role, content }) => {
if (role === "system") {
return [
{ role: "user", parts: typeof content !== "string" ? content : [{ text: content }] }
];
}
const parts = content == null || typeof content === "string" ? [{ text: content?.toString() ?? "" }] : content.map((item) => {
if (item.type === "text") return { text: item.text };
if (item.type === "image_url") return parseBase64(item.image_url.url);
return { text: "OK" };
});
return [{ role: "user" === role ? "user" : "model", parts }];
});
return result;
}
function genModel(req) {
const defaultModel = (m) => {
if (m.startsWith("gemini")) {
return m;
}
return "gemini-1.5-flash-latest";
};
const model = ModelMapping[req.model] ?? defaultModel(req.model);
let functions = req.tools?.filter((it) => it.type === "function")?.map((it) => it.function) ?? [];
functions = functions.concat((req.functions ?? []).map((it) => ({ strict: null, ...it })));
const responseMimeType = req.response_format?.type === "json_object" ? "application/json" : "text/plain";
const generateContentRequest = {
contents: openAiMessageToGeminiMessage(req.messages),
generationConfig: {
maxOutputTokens: req.max_tokens ?? void 0,
temperature: req.temperature ?? void 0,
topP: req.top_p ?? void 0,
responseMimeType
},
tools: functions.length === 0 ? void 0 : [
{
functionDeclarations: functions
}
],
safetySettings: [
"HARM_CATEGORY_HATE_SPEECH",
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
"HARM_CATEGORY_DANGEROUS_CONTENT",
"HARM_CATEGORY_HARASSMENT"
].map((category) => ({
category,
threshold: "BLOCK_NONE"
}))
};
return [model, generateContentRequest];
}
var ModelMapping = {
"gpt-3.5-turbo": "gemini-1.0-pro-latest",
"gpt-4": "gemini-1.5-pro-latest",
"gpt-4o": "gemini-1.5-flash-latest",
"gpt-4o-mini": "gemini-1.5-flash-8b-exp-0827",
"gpt-4-vision-preview": "gemini-1.0-pro-vision-latest",
"gpt-4-turbo": "gemini-1.5-pro-latest",
"gpt-4-turbo-preview": "gemini-1.5-pro-latest"
};
function getRuntimeKey() {
const global = globalThis;
if (global?.Deno !== void 0) {
return "deno";
}
if (global?.Bun !== void 0) {
return "bun";
}
if (typeof global?.WebSocketPair === "function") {
return "workerd";
}
if (typeof global?.EdgeRuntime === "string") {
return "edge-light";
}
if (global?.fastly !== void 0) {
return "fastly";
}
if (global?.process?.release?.name === "node") {
return "node";
}
return "other";
}
// src/hello.ts
function hello(req) {
const origin = new URL(req.url).origin;
return new Response(`
Hello Gemini-OpenAI-Proxy from ${getRuntimeKey()}!
You can try it with:
curl ${origin}/v1/chat/completions \\
-H "Authorization: Bearer $YOUR_GEMINI_API_KEY" \\
-H "Content-Type: application/json" \\
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}],
"temperature": 0.7
}'
`);
}
// src/log.ts
var LEVEL = ["debug", "info", "warn", "error"];
var Logger = class {
config;
debug;
info;
warn;
error;
constructor(prefix, logLevel) {
const level = LEVEL.find((it) => it === logLevel) ?? "warn";
this.config = {
prefix: prefix ?? "",
level
};
for (const m of LEVEL) {
this[m] = (...data) => this.#write(m, ...data);
}
}
#write(level, ...data) {
const { level: configLevel, prefix } = this.config;
if (LEVEL.indexOf(level) < LEVEL.indexOf(configLevel)) {
return;
}
console[level](`${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase()}${prefix ? ` ${prefix}` : ""}`, ...data);
}
};
// node_modules/.deno/[email protected]/node_modules/eventsource-parser/dist/index.js
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
var ParseError = class extends Error {
constructor(message, options) {
super(message), __publicField(this, "type"), __publicField(this, "field"), __publicField(this, "value"), __publicField(this, "line"), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
}
};
function noop(_arg) {
}
function createParser(callbacks) {
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
function feed(newChunk) {
const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
for (const line of complete)
parseLine(line);
incompleteLine = incomplete, isFirstChunk = false;
}
function parseLine(line) {
if (line === "") {
dispatchEvent();
return;
}
if (line.startsWith(":")) {
onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
return;
}
const fieldSeparatorIndex = line.indexOf(":");
if (fieldSeparatorIndex !== -1) {
const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
processField(field, value, line);
return;
}
processField(line, "", line);
}
function processField(field, value, line) {
switch (field) {
case "event":
eventType = value;
break;
case "data":
data = `${data}${value}
`;
break;
case "id":
id = value.includes("\0") ? void 0 : value;
break;
case "retry":
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
new ParseError(`Invalid \`retry\` value: "${value}"`, {
type: "invalid-retry",
value,
line
})
);
break;
default:
onError(
new ParseError(
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
{ type: "unknown-field", field, value, line }
)
);
break;
}
}
function dispatchEvent() {
data.length > 0 && onEvent({
id,
event: eventType || void 0,
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
// then remove the last character from the data buffer.
data: data.endsWith(`
`) ? data.slice(0, -1) : data
}), id = void 0, data = "", eventType = "";
}
function reset(options = {}) {
incompleteLine && options.consume && parseLine(incompleteLine), id = void 0, data = "", eventType = "", incompleteLine = "";
}
return { feed, reset };
}
function splitLines(chunk) {
const lines = [];
let incompleteLine = "";
const totalLength = chunk.length;
for (let i = 0; i < totalLength; i++) {
const char = chunk[i];
char === "\r" && chunk[i + 1] === `
` ? (lines.push(incompleteLine), incompleteLine = "", i++) : char === "\r" || char === `
` ? (lines.push(incompleteLine), incompleteLine = "") : incompleteLine += char;
}
return [lines, incompleteLine];
}
// node_modules/.deno/[email protected]/node_modules/eventsource-parser/dist/stream.js
var EventSourceParserStream = class extends TransformStream {
constructor({ onError, onRetry, onComment } = {}) {
let parser;
super({
start(controller) {
parser = createParser({
onEvent: (event) => {
controller.enqueue(event);
},
onError(error) {
onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
},
onRetry,
onComment
});
},
transform(chunk) {
parser.feed(chunk);
}
});
}
};
// src/gemini-api-client/errors.ts
var GoogleGenerativeAIError = class extends Error {
constructor(message) {
super(`[GoogleGenerativeAI Error]: ${message}`);
}
};
var GoogleGenerativeAIResponseError = class extends GoogleGenerativeAIError {
response;
constructor(message, response) {
super(message);
this.response = response;
}
};
// src/gemini-api-client/gemini-api-client.ts
async function* generateContent(task, apiParam, model, params, requestOptions) {
const url = new RequestUrl(model, task, true, apiParam);
const response = await makeRequest(url, JSON.stringify(params), requestOptions);
const body = response.body;
if (body == null) {
return;
}
for await (const event of body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream())) {
const responseJson = JSON.parse(event.data);
yield responseJson;
}
}
async function makeRequest(url, body, requestOptions) {
let response;
try {
response = await fetch(url.toURL(), {
...buildFetchOptions(requestOptions),
method: "POST",
headers: {
"Content-Type": "application/json"
},
body
});
if (!response.ok) {
let message = "";
try {
const errResp = await response.json();
message = errResp.error?.message;
if (errResp?.error?.details) {
message += ` ${JSON.stringify(errResp.error.details)}`;
}
} catch (_e) {
}
throw new Error(`[${response.status} ${response.statusText}] ${message}`);
}
} catch (e2) {
console.log(e2);
const err = new GoogleGenerativeAIError(`Error fetching from google -> ${e2.message}`);
err.stack = e2.stack;
throw err;
}
return response;
}
var RequestUrl = class {
model;
task;
stream;
apiParam;
constructor(model, task, stream, apiParam) {
this.model = model;
this.task = task;
this.stream = stream;
this.apiParam = apiParam;
}
toURL() {
const api_version = "v1beta";
const url = new URL(`${BASE_URL}/${api_version}/models/${this.model}:${this.task}`);
url.searchParams.append("key", this.apiParam.apikey);
if (this.stream) {
url.searchParams.append("alt", "sse");
}
return url;
}
};
var BASE_URL = "https://generativelanguage.googleapis.com";
function buildFetchOptions(requestOptions) {
const fetchOptions = {};
if (requestOptions?.timeout) {
const abortController = new AbortController();
const signal = abortController.signal;
setTimeout(() => abortController.abort(), requestOptions.timeout);
fetchOptions.signal = signal;
}
return fetchOptions;
}
// src/gemini-api-client/response-helper.ts
function resultHelper(response) {
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) {
console.warn(
`This response had ${response.candidates.length} candidates. Returning text from the first candidate only. Access response.candidates directly to use the other candidates.`
);
}
if (hadBadFinishReason(response.candidates[0])) {
throw new GoogleGenerativeAIResponseError(
`${formatBlockErrorMessage(response)}`,
response
);
}
return getText(response);
}
if (response.promptFeedback) {
throw new GoogleGenerativeAIResponseError(
`Text not available. ${formatBlockErrorMessage(response)}`,
response
);
}
return "";
}
function getText(response) {
if (response.candidates?.[0].content?.parts?.[0]?.text) {
return response.candidates[0].content.parts[0].text;
}
if (response.candidates?.[0].content?.parts?.[0]?.functionCall) {
return response.candidates[0].content.parts[0].functionCall;
}
return "";
}
var badFinishReasons = ["RECITATION", "SAFETY"];
function hadBadFinishReason(candidate) {
return !!candidate.finishReason && badFinishReasons.includes(candidate.finishReason);
}
function formatBlockErrorMessage(response) {
let message = "";
if ((!response.candidates || response.candidates.length === 0) && response.promptFeedback) {
message += "Response was blocked";
if (response.promptFeedback?.blockReason) {
message += ` due to ${response.promptFeedback.blockReason}`;
}
if (response.promptFeedback?.blockReasonMessage) {
message += `: ${response.promptFeedback.blockReasonMessage}`;
}
} else if (response.candidates?.[0]) {
const firstCandidate = response.candidates[0];
if (hadBadFinishReason(firstCandidate)) {
message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
if (firstCandidate.finishMessage) {
message += `: ${firstCandidate.finishMessage}`;
}
}
}
return message;
}
// src/openai/chat/completions/NonStreamingChatProxyHandler.ts
async function nonStreamingChatProxyHandler(req, apiParam, log) {
const [model, geminiReq] = genModel(req);
let geminiResp = "";
try {
for await (const it of generateContent("streamGenerateContent", apiParam, model, geminiReq)) {
const data = resultHelper(it);
if (typeof data === "string") {
geminiResp += data;
} else {
geminiResp = data;
break;
}
}
} catch (err) {
log?.error(req);
log?.error(err?.message ?? err.toString());
geminiResp = err?.message ?? err.toString();
}
log?.debug(req);
log?.debug(geminiResp);
function genOpenAiResp(content) {
if (typeof content === "string") {
return {
id: "chatcmpl-abc123",
object: "chat.completion",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
message: { role: "assistant", content, refusal: null },
finish_reason: "stop",
index: 0,
logprobs: null
}
]
};
}
return {
id: "chatcmpl-abc123",
object: "chat.completion",
created: Math.floor(Date.now() / 1e3),
model: req.model,
choices: [
{
message: {
role: "assistant",
refusal: null,
content: null,
function_call: {
name: content.name ?? "",
arguments: JSON.stringify(content.args)
}
},
finish_reason: "function_call",
index: 0,
logprobs: null
}
]
};
}
return Response.json(genOpenAiResp(geminiResp));
}
// src/openai/chat/completions/StreamingChatProxyHandler.ts
function streamingChatProxyHandler(req, apiParam, log) {
const [model, geminiReq] = genModel(req);
log?.debug("streamGenerateContent request", req);
return sseResponse(
async function* () {
try {
for await (const it of generateContent("streamGenerateContent", apiParam, model, geminiReq)) {
log?.debug("streamGenerateContent resp", it);
const data = resultHelper(it);
yield genStreamResp({ model: req.model, content: data, stop: false });
}
} catch (error) {
yield genStreamResp({
model: req.model,
content: error?.message ?? error.toString(),
stop: true
});
}
yield genStreamResp({ model: req.model, content: "", stop: true });
yield "[DONE]";
return void 0;
}()
);
}
function genStreamResp({
model,
content,
stop
}) {
if (typeof content === "string") {
return {
id: "chatcmpl-abc123",
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model,
choices: [
{
delta: { role: "assistant", content },
finish_reason: stop ? "stop" : null,
index: 0
}
]
};
}
return {
id: "chatcmpl-abc123",
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model,
choices: [
{
delta: { role: "assistant", function_call: content },
finish_reason: stop ? "function_call" : null,
index: 0
}
]
};
}
var encoder = new TextEncoder();
function sseResponse(dataStream) {
const s = new ReadableStream({
async pull(controller) {
const { value, done } = await dataStream.next();
if (done) {
controller.close();
} else {
const data = typeof value === "string" ? value : JSON.stringify(value);
controller.enqueue(encoder.encode(toSseMsg({ data })));
}
}
});
const response = new Response(s, {
status: 200,
headers: new Headers({
"Content-Type": "text/event-stream"
})
});
return response;
}
function toSseMsg({ event, data, id }) {
let result = `data: ${data}
`;
if (event) {
result += `event: ${event ?? ""}
`;
}
if (id) {
result += `id: ${id ?? ""}
`;
}
return `${result}
`;
}
// src/openai/chat/completions/ChatProxyHandler.ts
async function chatProxyHandler(rawReq) {
const req = await rawReq.json();
const headers = rawReq.headers;
const apiParam = getToken(headers);
if (apiParam == null) {
return new Response("Unauthorized", { status: 401 });
}
if (req.stream !== true) {
return await nonStreamingChatProxyHandler(req, apiParam, rawReq.logger);
}
return streamingChatProxyHandler(req, apiParam, rawReq.logger);
}
// src/openai/embeddingProxyHandler.ts
async function embeddingProxyHandler(rawReq) {
const req = await rawReq.json();
const log = rawReq.logger;
const headers = rawReq.headers;
const apiParam = getToken(headers);
if (apiParam == null) {
return new Response("Unauthorized", { status: 401 });
}
const embedContentRequest = {
model: "models/text-embedding-004",
content: {
parts: [req.input].flat().map((it) => ({ text: it.toString() }))
}
};
log?.warn("request", embedContentRequest);
let geminiResp = [];
try {
for await (const it of generateContent("embedContent", apiParam, "text-embedding-004", embedContentRequest)) {
const data = it.embedding?.values;
geminiResp = data;
break;
}
} catch (err) {
log?.error(req);
log?.error(err?.message ?? err.toString());
geminiResp = err?.message ?? err.toString();
}
log?.debug(req);
log?.debug(geminiResp);
const resp = {
object: "list",
data: [
{
object: "embedding",
index: 0,
embedding: geminiResp ?? []
}
],
model: req.model,
usage: {
prompt_tokens: 5,
total_tokens: 5
}
};
return Response.json(resp);
}
// src/openai/models.ts
var modelData = Object.keys(ModelMapping).map((model) => ({
created: 1677610602,
object: "model",
owned_by: "openai",
id: model
}));
var models = () => {
return {
object: "list",
data: modelData
};
};
var modelDetail = (model) => {
return modelData.find((it) => it.id === model);
};
// src/app.ts
var { preflight, corsify } = e({ allowHeaders: "*" });
var app = r({
before: [
preflight,
(req) => {
req.logger = new Logger(crypto.randomUUID().toString());
req.logger.warn(`--> ${req.method} ${req.url}`);
}
],
finally: [
corsify,
(_, req) => {
req.logger?.warn(`<-- ${req.method} ${req.url}`);
}
]
});
app.get("/", hello);
app.post("/v1/chat/completions", chatProxyHandler);
app.post("/v1/embeddings", embeddingProxyHandler);
app.get("/v1/models", () => Response.json(models()));
app.get("/v1/models/:model", (c) => Response.json(modelDetail(c.params.model)));
app.post(":model_version/models/:model_and_action", geminiProxy);
app.all("*", () => new Response("Page Not Found", { status: 404 }));
// main_cloudflare-workers.ts
var main_cloudflare_workers_default = {
fetch: app.fetch
};
export {
main_cloudflare_workers_default as default
};