Skip to content

Commit

Permalink
fix `Unable to submit request because it has an empty text parameter.…
Browse files Browse the repository at this point in the history
…' error

closes #83
  • Loading branch information
zuisong committed Nov 25, 2024
1 parent 42d8915 commit 62371b1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
58 changes: 34 additions & 24 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion dist/main_node.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// node_modules/.deno/@hono+node-server@1.13.5/node_modules/@hono/node-server/dist/index.mjs
// node_modules/.deno/@hono+node-server@1.13.7/node_modules/@hono/node-server/dist/index.mjs
import { createServer as createServerHTTP } from "http";
import { Http2ServerRequest } from "http2";
import { Readable } from "stream";
Expand Down Expand Up @@ -393,6 +393,8 @@ var getRequestListener = (fetchCallback, options = {}) => {
outgoing.on("close", () => {
if (incoming.errored) {
req[getAbortController]().abort(incoming.errored.toString());
} else if (!outgoing.writableFinished) {
req[getAbortController]().abort("Client connection prematurely closed.");
}
});
res = fetchCallback(req, { incoming, outgoing });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test-cov": "deno test --coverage -A --env && deno coverage coverage --lcov --output=coverage/lcov.info"
},
"dependencies": {
"@hono/node-server": "1.13.5",
"@hono/node-server": "1.13.7",
"eventsource-parser": "3.0.0",
"itty-router": "5.0.18"
},
Expand Down
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { IRequest, IttyRouterType } from "itty-router"
import type { IRequest } from "itty-router"
import { Router } from "itty-router/Router"
import { cors } from "itty-router/cors"
import { geminiProxy } from "./gemini-proxy.ts"
import { hello } from "./hello.ts"
import { Logger } from "./log.ts"
import { type Any, Logger } from "./log.ts"
import { chatProxyHandler } from "./openai/chat/completions/ChatProxyHandler.ts"
import { embeddingProxyHandler } from "./openai/embeddingProxyHandler.ts"
import { modelDetail, models } from "./openai/models.ts"

const { preflight, corsify } = cors({ allowHeaders: "*" })

const app: IttyRouterType = Router<IRequest>({
const app = Router<IRequest, Any[], Response>({
before: [
preflight,
(req) => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function openAiMessageToGeminiMessage(messages: OpenAI.Chat.ChatCompletio
if (role === "system") {
return [
{ role: "user", parts: typeof content !== "string" ? content : [{ text: content }] },
{ role: "model", parts: [{ text: "" }] },
{ role: "model", parts: [{ text: "OK" }] },
] satisfies Content[] as Content[]
}

Expand All @@ -62,7 +62,7 @@ export function openAiMessageToGeminiMessage(messages: OpenAI.Chat.ChatCompletio
: content.map((item) => {
if (item.type === "text") return { text: item.text }
if (item.type === "image_url") return parseBase64(item.image_url.url)
return { text: "" }
return { text: "OK" }
})

return [{ role: "user" === role ? "user" : "model", parts: parts }]
Expand Down
11 changes: 6 additions & 5 deletions test/chat-completion_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,8 @@ describe("openai to gemini test", () => {
}),
)

console.log(111)
const text = await res.text()
console.log(text)

const values = ReadableStream.from(text).pipeThrough(new EventSourceParserStream()).values()
const body = res.body ?? throws(new Error("no body"))
const values = body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).values()

for await (const e of values) {
if (e.data === "[DONE]") return
Expand All @@ -116,3 +113,7 @@ describe("openai to gemini test", () => {
}
})
})

function throws(e: Error): never {
throw e
}

0 comments on commit 62371b1

Please sign in to comment.