Skip to content

Normalize Local AI Gateway base URL in dev proxy setups#23

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/implement-local-ai-gateway-feature
Draft

Normalize Local AI Gateway base URL in dev proxy setups#23
Copilot wants to merge 2 commits into
mainfrom
copilot/implement-local-ai-gateway-feature

Conversation

Copilot AI commented May 30, 2026

Copy link
Copy Markdown
Contributor

The Local AI Gateway status endpoint could return a frontend-origin base URL (http://localhost:5173/v1) when accessed through Vite, which caused downstream clients to hit invalid paths like /v1/v1/models and parse HTML error pages as JSON. This change normalizes the returned gateway base URL to the backend origin.

  • Gateway base URL normalization

    • Added origin normalization in src/routes/gateway.ts via gatewayBaseUrl(...).
    • When request origin is Vite dev server (:5173), base URL is rewritten to backend WEB_PORT (default 3000).
    • Applied consistently to both GET /api/gateway and POST /api/gateway responses.
  • Safer request-origin fallback

    • If ctx.origin is absent, origin is derived from ctx.protocol + ctx.host before normalization.
  • Regression coverage

    • Extended src/routes/__tests__/gateway.test.ts with a case asserting Host: localhost:5173 returns gateway.baseUrl === "http://localhost:3000/v1".
function gatewayBaseUrl(origin: string | null | undefined): string {
  const rawOrigin = typeof origin === 'string' ? origin : '';
  try {
    const url = new URL(rawOrigin);
    if (url.port === '5173') url.port = process.env.WEB_PORT ?? '3000';
    return `${url.origin}/v1`;
  } catch {
    return rawOrigin ? `${rawOrigin.replace(/\/$/, '')}/v1` : '/v1';
  }
}
Original prompt

message: Gateway /v1/models was unreachable: Unexpected token '<', "<!doctype "... is not valid JSON
requestUrl: http://localhost:5173/v1/v1/models
endpoint: http://localhost:5173/
checkedAt: 2026-05-30T14:30:25.391Z

[Chronological Review: The conversation began with the user requesting the implementation of a feature related to the Local AI Gateway. The user then sought clarification on how to use this feature. Following that, the user suggested adding a toggle switch and configuration capability for the gatewayToken in the settings, indicating a desire for improved usability.]

[Intent Mapping:

  • "实现这个feature" - The user explicitly requested the implementation of a feature related to the Local AI Gateway.
  • "具体怎么使用呢?" - The user asked for specific instructions on how to use the feature.
  • "gatewayToken 配置太极客了吧,或许可以在设置里面增加一个开关和配置能力?" - The user suggested enhancing the configuration of gatewayToken by adding a toggle switch in the settings.]

[Technical Inventory:

  • Technologies mentioned include Koa for backend HTTP services, Vercel AI SDK for LLM integration, and various AI models (Gemini, DeepSeek, OpenAI, Anthropic, Claude Code, Ollama).
  • Key files referenced include:
    • src/llm/client.ts: Handles AI SDK model calls, streaming, fallback, and usage recording.
    • src/config.ts: Manages model aliases, provider key access, and configuration.
    • src/routes/model.ts: Provides model lists, provider status, and routing configuration APIs.]

[Code Archaeology:

  • No specific code changes were discussed in detail during this segment, but the user is focused on enhancing the configuration capabilities related to gatewayToken.]

[Progress Assessment:

  • The user has outlined the existing capabilities of the Local AI Gateway and identified areas for improvement, particularly regarding user configuration options.
  • No specific implementation tasks have been completed yet, as the conversation is still in the planning and suggestion phase.]

[Context Validation: All critical information regarding the user's requests and suggestions for the Local AI Gateway feature is captured, ensuring continuity in the conversation.]

[Recent Commands Analysis:

  • Last Agent Commands: The user requested the implementation of a feature, asked for usage instructions, and suggested a configuration enhancement.
  • Tool Results Summary: No specific tool results were generated as the conversation primarily consisted of user requests and suggestions.
  • Pre-Summary State: The agent was actively discussing the implementation and usability of the Local AI Gateway feature when the token budget was exceeded.
  • Operation Context: The user's commands were aimed at improving the functionality and user experience of the Local AI Gateway, particularly regarding the gatewayToken configuration.]
1. Conversation Overview: - Primary Objectives: - "实现这个feature" - User requested implementation of a feature for the Local AI Gateway. - "具体怎么使用呢?" - User sought clarification on how to use the feature. - "gatewayToken 配置太极客了吧,或许可以在设置里面增加一个开关和配置能力?" - User suggested adding a toggle switch for gatewayToken configuration. - Session Context: The conversation has focused on the Local AI Gateway, discussing its current capabilities and potential enhancements. - User Intent Evolution: The user transitioned from requesting implementation to seeking usage instructions and suggesting usability improvements.
  1. Technical Foundation:
  • Koa: Backend HTTP service framework used for API management.
  • Vercel AI SDK: Framework for integrating various AI models.
  • Local AI Gateway: Aims to provide a unified model entry point for external clients.
  1. Codebase Status:
  • File Name: src/llm/client.ts
    • Purpose: Handles AI SDK model calls and usage recording.
    • Current State: No recent changes discussed.
  • File Name: src/config.ts
    • Purpose: Manages model aliases and provider key access.
    • Current State: No recent changes discussed.
  • File Name: src/routes/model.ts
    • Purpose: Provides model lists and routing configuration.
    • Current State: No recent changes discussed.
  1. Problem Resolution:
  • Issues Encountered: User expressed concerns about the complexity of gatewayToken configuration.
  • Solutions Implemented: User suggested adding a toggle switch for easier configuration.
  • Debugging Context: No ongoing troubleshooting efforts were mentioned.
  • Lessons Learned: User feedback indicates a need for improved usability in configuration settings.
  1. Progress Tracking:
  • Completed Tasks: None reported yet.
  • Partially Complete Work: User has outlined feature requests and suggestions for improvement.
  • Validated Outcomes: No features confirmed working through testing yet.
  1. Active Work State:
  • Current Focus: Discussing the implementation and usability of the Local AI Gateway feature.
  • Recent Context: User has been actively requesting feature implementation and usability enhancements.
  • Working Code: No speci...

Created from VS Code.

Copilot AI changed the title [WIP] Add local AI gateway feature implementation Normalize Local AI Gateway base URL in dev proxy setups May 30, 2026
Copilot AI requested a review from zane-byte-dev May 30, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants