-
Notifications
You must be signed in to change notification settings - Fork 9
refactor: unify fallback next json proxies #408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
web/src/app/api/research/paperscool/sessions/[sessionId]/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it, vi } from "vitest" | ||
|
|
||
| const { apiBaseUrlMock, proxyJsonMock } = vi.hoisted(() => ({ | ||
| apiBaseUrlMock: vi.fn(() => "http://backend.example.com"), | ||
| proxyJsonMock: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock("@/app/api/_utils/backend-proxy", () => ({ | ||
| apiBaseUrl: apiBaseUrlMock, | ||
| proxyJson: proxyJsonMock, | ||
| })) | ||
|
|
||
| import { GET } from "./route" | ||
|
|
||
| describe("paperscool session route", () => { | ||
| it("proxies the backend request without caching", async () => { | ||
| const req = new Request("http://localhost/api/research/paperscool/sessions/some/session") | ||
| proxyJsonMock.mockResolvedValueOnce(Response.json({ ok: true })) | ||
|
|
||
| await GET(req, { params: Promise.resolve({ sessionId: "session/42" }) }) | ||
|
|
||
| expect(proxyJsonMock).toHaveBeenCalledWith( | ||
| req, | ||
| "http://backend.example.com/api/research/paperscool/sessions/session%2F42", | ||
| "GET", | ||
| { cache: "no-store" }, | ||
| ) | ||
| }) | ||
| }) |
16 changes: 5 additions & 11 deletions
16
web/src/app/api/research/paperscool/sessions/[sessionId]/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,11 @@ | ||
| import { NextResponse } from "next/server" | ||
| import { apiBaseUrl, proxyJson } from "@/app/api/_utils/backend-proxy" | ||
|
|
||
| import { apiBaseUrl } from "@/app/api/research/_base" | ||
|
|
||
| export async function GET(_req: Request, ctx: { params: Promise<{ sessionId: string }> }) { | ||
| export async function GET(req: Request, ctx: { params: Promise<{ sessionId: string }> }) { | ||
| const { sessionId } = await ctx.params | ||
| const upstream = await fetch( | ||
| return proxyJson( | ||
| req, | ||
| `${apiBaseUrl()}/api/research/paperscool/sessions/${encodeURIComponent(sessionId)}`, | ||
| "GET", | ||
| { cache: "no-store" }, | ||
| ) | ||
|
|
||
| const body = await upstream.text() | ||
| return new NextResponse(body, { | ||
| status: upstream.status, | ||
| headers: { "Content-Type": upstream.headers.get("content-type") || "application/json" }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, expect, it, vi } from "vitest" | ||
|
|
||
| const { apiBaseUrlMock, proxyJsonMock } = vi.hoisted(() => ({ | ||
| apiBaseUrlMock: vi.fn(() => "http://backend.example.com"), | ||
| proxyJsonMock: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock("@/app/api/_utils/backend-proxy", () => ({ | ||
| apiBaseUrl: apiBaseUrlMock, | ||
| proxyJson: proxyJsonMock, | ||
| })) | ||
|
|
||
| import { GET } from "./route" | ||
|
|
||
| describe("studio cwd route", () => { | ||
| it("proxies the backend request with a fallback response", async () => { | ||
| const req = new Request("http://localhost/api/studio/cwd") | ||
| proxyJsonMock.mockResolvedValueOnce(Response.json({ cwd: "/workspace" })) | ||
|
|
||
| await GET(req) | ||
|
|
||
| expect(proxyJsonMock).toHaveBeenCalledWith( | ||
| req, | ||
| "http://backend.example.com/api/studio/cwd", | ||
| "GET", | ||
| expect.objectContaining({ | ||
| onError: expect.any(Function), | ||
| }), | ||
| ) | ||
|
|
||
| const options = vi.mocked(proxyJsonMock).mock.calls[0][3] | ||
| expect(options).toBeDefined() | ||
|
|
||
| const fallback = options?.onError?.({ | ||
| error: new Error("offline"), | ||
| isTimeout: false, | ||
| upstreamUrl: "http://backend.example.com/api/studio/cwd", | ||
| }) | ||
|
|
||
| expect(fallback).toBeInstanceOf(Response) | ||
| expect(fallback?.status).toBe(200) | ||
| await expect(fallback?.json()).resolves.toEqual({ | ||
| cwd: process.env.HOME || "/tmp", | ||
| source: "fallback", | ||
| error: "Failed to get working directory from backend", | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,17 @@ | ||
| export const runtime = "nodejs" | ||
|
|
||
| function apiBaseUrl() { | ||
| return process.env.PAPERBOT_API_BASE_URL || "http://127.0.0.1:8000" | ||
| } | ||
|
|
||
| export async function GET() { | ||
| try { | ||
| const upstream = await fetch(`${apiBaseUrl()}/api/studio/cwd`, { | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }) | ||
|
|
||
| const data = await upstream.json() | ||
| import { apiBaseUrl, proxyJson } from "@/app/api/_utils/backend-proxy" | ||
|
|
||
| return Response.json(data, { | ||
| status: upstream.status, | ||
| }) | ||
| } catch (error) { | ||
| // Return a sensible default if backend is unavailable | ||
| return Response.json( | ||
| { | ||
| cwd: process.env.HOME || "/tmp", | ||
| source: "fallback", | ||
| error: "Failed to get working directory from backend", | ||
| }, | ||
| { status: 200 } | ||
| ) | ||
| } | ||
| export async function GET(req: Request) { | ||
| return proxyJson(req, `${apiBaseUrl()}/api/studio/cwd`, "GET", { | ||
| onError: () => | ||
| Response.json( | ||
| { | ||
| cwd: process.env.HOME || "/tmp", | ||
| source: "fallback", | ||
| error: "Failed to get working directory from backend", | ||
| }, | ||
| { status: 200 }, | ||
| ), | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, expect, it, vi } from "vitest" | ||
|
|
||
| const { apiBaseUrlMock, proxyJsonMock } = vi.hoisted(() => ({ | ||
| apiBaseUrlMock: vi.fn(() => "http://backend.example.com"), | ||
| proxyJsonMock: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock("@/app/api/_utils/backend-proxy", () => ({ | ||
| apiBaseUrl: apiBaseUrlMock, | ||
| proxyJson: proxyJsonMock, | ||
| })) | ||
|
|
||
| import { GET } from "./route" | ||
|
|
||
| describe("studio status route", () => { | ||
| it("proxies the backend request with a fallback response", async () => { | ||
| const req = new Request("http://localhost/api/studio/status") | ||
| proxyJsonMock.mockResolvedValueOnce(Response.json({ claude_cli: true })) | ||
|
|
||
| await GET(req) | ||
|
|
||
| expect(proxyJsonMock).toHaveBeenCalledWith( | ||
| req, | ||
| "http://backend.example.com/api/studio/status", | ||
| "GET", | ||
| expect.objectContaining({ | ||
| onError: expect.any(Function), | ||
| }), | ||
| ) | ||
|
|
||
| const options = vi.mocked(proxyJsonMock).mock.calls[0][3] | ||
| expect(options).toBeDefined() | ||
|
|
||
| const fallback = options?.onError?.({ | ||
| error: new Error("offline"), | ||
| isTimeout: false, | ||
| upstreamUrl: "http://backend.example.com/api/studio/status", | ||
| }) | ||
|
|
||
| expect(fallback).toBeInstanceOf(Response) | ||
| expect(fallback?.status).toBe(500) | ||
| await expect(fallback?.json()).resolves.toEqual({ | ||
| claude_cli: false, | ||
| error: "Failed to check Claude CLI status", | ||
| fallback: "anthropic_api", | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,17 @@ | ||
| export const runtime = "nodejs" | ||
|
|
||
| function apiBaseUrl() { | ||
| return process.env.PAPERBOT_API_BASE_URL || "http://127.0.0.1:8000" | ||
| } | ||
|
|
||
| export async function GET() { | ||
| try { | ||
| const upstream = await fetch(`${apiBaseUrl()}/api/studio/status`, { | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }) | ||
|
|
||
| const data = await upstream.json() | ||
| import { apiBaseUrl, proxyJson } from "@/app/api/_utils/backend-proxy" | ||
|
|
||
| return Response.json(data, { | ||
| status: upstream.status, | ||
| }) | ||
| } catch (error) { | ||
| return Response.json( | ||
| { | ||
| claude_cli: false, | ||
| error: "Failed to check Claude CLI status", | ||
| fallback: "anthropic_api" | ||
| }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| export async function GET(req: Request) { | ||
| return proxyJson(req, `${apiBaseUrl()}/api/studio/status`, "GET", { | ||
| onError: () => | ||
| Response.json( | ||
| { | ||
| claude_cli: false, | ||
| error: "Failed to check Claude CLI status", | ||
| fallback: "anthropic_api", | ||
| }, | ||
| { status: 500 }, | ||
| ), | ||
|
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the onError: (context) => {
console.error("Studio status proxy failed:", context.error)
return Response.json(
{
claude_cli: false,
error: "Failed to check Claude CLI status",
fallback: "anthropic_api",
},
{ status: 500 },
)
}, |
||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better observability and easier debugging, it would be beneficial to log the error details provided by the
onErrorcontext. The original implementation didn't log the error, but since you're refactoring to a more robust proxy helper, this is a good opportunity to add logging.