|
@@ -6,7 +6,6 @@ import { describeRoute, generateSpecs, validator, resolver, openAPIRouteHandler
|
|
|
import { Hono } from "hono"
|
|
import { Hono } from "hono"
|
|
|
import { cors } from "hono/cors"
|
|
import { cors } from "hono/cors"
|
|
|
import { stream, streamSSE } from "hono/streaming"
|
|
import { stream, streamSSE } from "hono/streaming"
|
|
|
-import { proxy } from "hono/proxy"
|
|
|
|
|
import { Session } from "../session"
|
|
import { Session } from "../session"
|
|
|
import z from "zod"
|
|
import z from "zod"
|
|
|
import { Provider } from "../provider/provider"
|
|
import { Provider } from "../provider/provider"
|
|
@@ -54,6 +53,9 @@ globalThis.AI_SDK_LOG_WARNINGS = false
|
|
|
export namespace Server {
|
|
export namespace Server {
|
|
|
const log = Log.create({ service: "server" })
|
|
const log = Log.create({ service: "server" })
|
|
|
|
|
|
|
|
|
|
+ // Port that the server is running on, used to inject into frontend HTML
|
|
|
|
|
+ let serverPort: number = 4096
|
|
|
|
|
+
|
|
|
export const Event = {
|
|
export const Event = {
|
|
|
Connected: BusEvent.define("server.connected", z.object({})),
|
|
Connected: BusEvent.define("server.connected", z.object({})),
|
|
|
Disposed: BusEvent.define("global.disposed", z.object({})),
|
|
Disposed: BusEvent.define("global.disposed", z.object({})),
|
|
@@ -2578,12 +2580,25 @@ export namespace Server {
|
|
|
},
|
|
},
|
|
|
)
|
|
)
|
|
|
.all("/*", async (c) => {
|
|
.all("/*", async (c) => {
|
|
|
- return proxy(`https://desktop.opencode.ai${c.req.path}`, {
|
|
|
|
|
- ...c.req,
|
|
|
|
|
|
|
+ const response = await fetch(`https://desktop.opencode.ai${c.req.path}`, {
|
|
|
|
|
+ method: c.req.method,
|
|
|
headers: {
|
|
headers: {
|
|
|
host: "desktop.opencode.ai",
|
|
host: "desktop.opencode.ai",
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ const contentType = response.headers.get("content-type") || ""
|
|
|
|
|
+
|
|
|
|
|
+ // If this is an HTML response, inject the server port
|
|
|
|
|
+ if (contentType.includes("text/html")) {
|
|
|
|
|
+ const html = await response.text()
|
|
|
|
|
+ const portScript = `<script>window.__OPENCODE__ = window.__OPENCODE__ || {}; window.__OPENCODE__.port = ${serverPort};</script>`
|
|
|
|
|
+ // Inject the script right after the opening <head> tag
|
|
|
|
|
+ const modifiedHtml = html.replace("<head>", `<head>${portScript}`)
|
|
|
|
|
+ return c.html(modifiedHtml)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return response
|
|
|
}),
|
|
}),
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -2607,14 +2622,9 @@ export namespace Server {
|
|
|
idleTimeout: 0,
|
|
idleTimeout: 0,
|
|
|
fetch: App().fetch,
|
|
fetch: App().fetch,
|
|
|
websocket: websocket,
|
|
websocket: websocket,
|
|
|
- } as const
|
|
|
|
|
- if (opts.port === 0) {
|
|
|
|
|
- try {
|
|
|
|
|
- return Bun.serve({ ...args, port: 4096 })
|
|
|
|
|
- } catch {
|
|
|
|
|
- // port 4096 not available, fall through to use port 0
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return Bun.serve({ ...args, port: opts.port })
|
|
|
|
|
|
|
+ })
|
|
|
|
|
+ // Store the actual port for injection into frontend HTML
|
|
|
|
|
+ serverPort = server.port ?? opts.port
|
|
|
|
|
+ return server
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|