Skip to content

OpenAI-powered search routing to endpoints#97

Open
seveibar wants to merge 1 commit intomainfrom
codex/add-/search-endpoint-with-openai-sdk
Open

OpenAI-powered search routing to endpoints#97
seveibar wants to merge 1 commit intomainfrom
codex/add-/search-endpoint-with-openai-sdk

Conversation

@seveibar
Copy link
Contributor

@seveibar seveibar commented Oct 8, 2025

Summary

  • remove the committed Bun lockfile and ignore future lockfile generation
  • add a reusable getOpenAiClient helper and update the /search route to build prompts from all GET endpoints, reuse the helper, and fail fast when no API key is configured
  • add focused route tests that stub an OpenAI server, assert the delegated request, and cover the missing API key error path

Testing

  • bun test tests/routes/search.test.ts
  • bunx tsc --noEmit
  • bun run format

https://chatgpt.com/codex/tasks/task_b_68e5aa749598832ea69db0fc554986f1

Comment on lines +80 to +118
test("GET /search delegates to internal endpoints returned by OpenAI", async () => {
const { axios } = await getTestServer()

const response = await axios.get("/search?q=leds")

expect(response.status).toBe(200)
expect(response.data.search_result.endpoint_used).toBe("/leds/list")
expect(response.data.search_result.filter_params).toEqual({ json: "true" })
expect(openAiRequests.length).toBe(1)

const [{ body }] = openAiRequests

expect(body.messages?.[1]?.content).toContain("GET /leds/list")
expect(body.messages?.[1]?.content).toContain("User query: leds")
})

test("GET /search returns error when OPENAI_API_KEY is missing", async () => {
delete process.env.OPENAI_API_KEY

const { axios } = await getTestServer()

let response: { status: number; data: any } | undefined

try {
response = await axios.get("/search?q=test")
} catch (error) {
response = error as { status: number; data: any }
}

expect(response?.status).toBe(500)
expect(response?.data).toEqual({
error: {
error_code: "missing_openai_api_key",
message: "OPENAI_API_KEY environment variable is not configured",
},
})

expect(openAiRequests.length).toBe(0)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test file contains 2 test() functions (lines 80-94 and 96-118), which violates the rule that a *.test.ts file may have AT MOST one test(...). After the first test, the user should split into multiple, numbered files. To fix this, split the tests into separate files like 'search1.test.ts' and 'search2.test.ts', with each file containing only one test() function.

Spotted by Diamond (based on custom rule: Custom rule)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment on lines +268 to +272
try {
internalResponse = await fetch(url.toString(), {
headers: {
Accept: "application/json",
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Ensure delegated requests trigger JSON responses

The /search handler forwards requests to internal GET endpoints but only sets an Accept: application/json header before calling fetch. Routes built with withWinterSpec treat requests as HTML unless a json query param or Content-Type: application/json is present (withIsApiRequest middleware). When OpenAI selects an endpoint whose json parameter is optional and omits it—which is plausible given the prompt only marks required params—the delegated call returns HTML and internalResponse.json() throws, so users receive invalid_internal_response despite a valid endpoint selection. Force JSON mode (e.g. always add json=true or set Content-Type on the forwarded request) to avoid this systematic failure.

Useful? React with 👍 / 👎.

@seveibar seveibar changed the title Refine OpenAI-powered search routing OpenAI-powered search routing to endpoints Oct 8, 2025
Pitrat-wav pushed a commit to Pitrat-wav/jlcsearch that referenced this pull request Feb 23, 2026
Pitrat-wav pushed a commit to Pitrat-wav/jlcsearch that referenced this pull request Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant