Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/routes/responses/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type { Context } from "hono"

import consola from "consola"
import { streamSSE } from "hono/streaming"

import { awaitApproval } from "~/lib/approval"
import { checkRateLimit } from "~/lib/rate-limit"
import { state } from "~/lib/state"
import { createResponse } from "~/services/copilot/create-response"

// Copilot only supports "function" tools — strip built-in OpenAI tools
// like web_search, file_search, code_interpreter, computer_use_preview, etc.
function sanitizePayload(
payload: Record<string, unknown>,
): Record<string, unknown> {
if (!Array.isArray(payload.tools)) return payload

const supportedTools = (payload.tools as Array<{ type?: string }>).filter(
(tool) => tool.type === "function",
)

const stripped = payload.tools.length - supportedTools.length
if (stripped > 0) {
consola.debug(`Stripped ${stripped} unsupported tool(s) from request`)
}

return {
...payload,
tools: supportedTools.length > 0 ? supportedTools : undefined,
}
}

export async function handleResponses(c: Context) {
await checkRateLimit(state)

const rawPayload = await c.req.json<Record<string, unknown>>()
consola.debug(
"Responses API request payload:",
JSON.stringify(rawPayload).slice(-400),
)

const payload = sanitizePayload(rawPayload)

if (state.manualApprove) {
await awaitApproval()
}

const response = await createResponse(payload)

// Non-streaming: forward the JSON response directly
if (response instanceof Response) {
const body = (await response.json()) as Record<string, unknown>
consola.debug(
"Non-streaming response from Copilot /responses:",
JSON.stringify(body).slice(-400),
)
return c.json(body)
}

// Streaming: forward SSE events directly
consola.debug("Streaming response from Copilot /responses")
return streamSSE(c, async (stream) => {
for await (const rawEvent of response) {
if (rawEvent.data === "[DONE]") {
break
}

if (!rawEvent.data) {
continue
}

consola.debug(
"Copilot /responses stream event:",
rawEvent.data.slice(-300),
)

const parsed = JSON.parse(rawEvent.data) as { type?: string }
const eventType = parsed.type ?? "unknown"

await stream.writeSSE({
event: eventType,
data: rawEvent.data,
})
}
})
}
15 changes: 15 additions & 0 deletions src/routes/responses/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Hono } from "hono"

import { forwardError } from "~/lib/error"

import { handleResponses } from "./handler"

export const responsesRoutes = new Hono()

responsesRoutes.post("/", async (c) => {
try {
return await handleResponses(c)
} catch (error) {
return await forwardError(c, error)
}
})
5 changes: 5 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { completionRoutes } from "./routes/chat-completions/route"
import { embeddingRoutes } from "./routes/embeddings/route"
import { messageRoutes } from "./routes/messages/route"
import { modelRoutes } from "./routes/models/route"
import { responsesRoutes } from "./routes/responses/route"
import { tokenRoute } from "./routes/token/route"
import { usageRoute } from "./routes/usage/route"

Expand All @@ -29,3 +30,7 @@ server.route("/v1/embeddings", embeddingRoutes)

// Anthropic compatible endpoints
server.route("/v1/messages", messageRoutes)

// OpenAI Responses API compatible endpoints
server.route("/responses", responsesRoutes)
server.route("/v1/responses", responsesRoutes)
39 changes: 39 additions & 0 deletions src/services/copilot/create-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import consola from "consola"
import { events } from "fetch-event-stream"

import { copilotBaseUrl, copilotHeaders } from "~/lib/api-config"
import { HTTPError } from "~/lib/error"
import { state } from "~/lib/state"

export const createResponse = async (
payload: Record<string, unknown>,
): Promise<Response | AsyncIterable<{ data?: string }>> => {
if (!state.copilotToken) throw new Error("Copilot token not found")

const headers: Record<string, string> = {
...copilotHeaders(state),
"X-Initiator": "user",
}

consola.debug(
"Forwarding to Copilot /responses:",
JSON.stringify(payload).slice(-400),
)

const response = await fetch(`${copilotBaseUrl(state)}/responses`, {
method: "POST",
headers,
body: JSON.stringify(payload),
})

if (!response.ok) {
consola.error("Failed to create response", response)
throw new HTTPError("Failed to create response", response)
}

if (payload.stream) {
return events(response)
}

return response
}