Skip to content

feat: Add OpenAI provider support for AI assistance#1960

Open
vinzee wants to merge 9 commits intohyperdxio:mainfrom
vinzee:va/enable_ai_assistance
Open

feat: Add OpenAI provider support for AI assistance#1960
vinzee wants to merge 9 commits intohyperdxio:mainfrom
vinzee:va/enable_ai_assistance

Conversation

@vinzee
Copy link
Contributor

@vinzee vinzee commented Mar 21, 2026

Summary

Add OpenAI-compatible provider support for AI-powered chart assistance, enabling use of any OpenAI Chat Completions-compatible endpoint (Azure OpenAI, OpenRouter, LiteLLM proxies) as an alternative to Anthropic.

Key changes:

  • Implement getOpenAIModel() in controllers/ai.ts using @ai-sdk/openai
  • Support custom request headers via AI_CLIENT_ID / AI_USERNAME env vars
  • Support arbitrary request body fields via AI_EXTRA_BODY env var (JSON),
    useful for proxy/gateway compatibility (e.g. LiteLLM passthrough mode)
  • Add detailed error logging for AI API call failures (status code, response
    body, URL) to aid debugging provider issues
  • Adapt to sql-formatter's breaking rename of reservedPhrases to reservedKeywordPhrases in the
    TokenizerOptions interface

Screenshots or video

Before After

How to test locally or on Vercel

  1. Set env vars: AI_PROVIDER=openai, AI_API_KEY=<key>,
    AI_BASE_URL=<endpoint>, AI_MODEL_NAME=<model>
  2. Optionally set AI_CLIENT_ID, AI_USERNAME for custom headers
  3. Optionally set AI_EXTRA_BODY='{"passthrough":true}' for extra body fields
  4. Open Chart Explorer, select a source, use the AI assistant to generate a chart
  5. Verify the assistant returns a valid chart config

References

  • Linear Issue:
  • Related PRs:

@vercel
Copy link

vercel bot commented Mar 21, 2026

@vinzee is attempting to deploy a commit to the HyperDX Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link

changeset-bot bot commented Mar 21, 2026

🦋 Changeset detected

Latest commit: cea33b8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@hyperdx/api Patch
@hyperdx/app Patch
@hyperdx/otel-collector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 21, 2026

PR Review

  • ⚠️ Changeset marks a new feature as patch → Should be minor per semver (new openai provider is new functionality, not a bug fix)
  • ⚠️ AI_REQUEST_HEADERS only wired to OpenAI provider, not Anthropic → Either document this is OpenAI-only (e.g. rename to OPENAI_REQUEST_HEADERS) or also wire it up in getAnthropicModel(); the generic env var name implies it works for any provider
  • ℹ️ parseJSON<Record<string, string>> is a cast, not a runtime validation → Non-string header values (e.g. {"key": 123}) will silently pass type-check; acceptable for server-side env vars but worth noting

Otherwise the implementation is clean, tests are comprehensive, error messages are helpful, and the pattern follows existing conventions in the codebase.

@vinzee vinzee force-pushed the va/enable_ai_assistance branch from c9115f5 to 6506b05 Compare March 21, 2026 18:48
@brandon-pereira brandon-pereira self-requested a review March 23, 2026 17:36
vinzee added 6 commits March 23, 2026 10:51
Enables using OpenAI-compatible endpoints (OpenAI, Azure OpenAI, OpenRouter,
LiteLLM proxies) as an alternative to Anthropic for AI-powered query assistance.

Adds support for custom headers (X-Client-Id, X-Username) and request body
customization via AI_EXTRA_BODY for proxy/gateway compatibility. Enhances
error logging with detailed API call diagnostics.

Also upgrades sql-formatter to 15.7.0 to address breaking API changes.
- Require `AI_MODEL_NAME` explicitly for the OpenAI provider instead of
  falling back to an Anthropic model ID (`claude-sonnet-4-5-20250929`) which
  would fail against actual OpenAI endpoints
- Add error handling around `JSON.parse(AI_EXTRA_BODY)` so invalid JSON
  throws a descriptive config error instead of an unhandled exception
- Move `AI_CLIENT_ID`, `AI_USERNAME`, and `AI_EXTRA_BODY` into the config
  module for consistency with other AI env vars
- Truncate `responseBody` in error logs to 500 chars to avoid leaking
  sensitive data from provider error responses
- Remove accidentally committed package-lock.json (37k lines) and add it
  to .gitignore -- this is a Yarn 4.5.1 workspace project
- Log a warning when AI_EXTRA_BODY custom fetch cannot parse the request
  body as JSON, instead of silently forwarding it unmodified
This commit introduces a new test file to thoroughly validate the `getAIModel` function. The tests cover various scenarios including provider routing, API key handling, base URL configuration, model name selection, custom headers for OpenAI, and the `AI_EXTRA_BODY` functionality. This ensures robust and predictable behavior for AI model instantiation based on environment variables.
@vinzee vinzee force-pushed the va/enable_ai_assistance branch from abbc4df to 95c3723 Compare March 23, 2026 20:44
Replaced `AI_CLIENT_ID` and `AI_USERNAME` environment variables with a single `AI_REQUEST_HEADERS` variable. This new variable accepts a JSON string, allowing users to define arbitrary custom headers for AI provider requests. This change enhances the extensibility of AI provider integrations.

Additionally, improved error handling for AI API call failures by providing more concise error messages and removing excessive logging of truncated response bodies.
@vinzee vinzee force-pushed the va/enable_ai_assistance branch from a2f2d01 to cea33b8 Compare March 23, 2026 21:57
@brandon-pereira
Copy link
Member

@vinzee PR LGTM! Nice work! There is some linting issues, otherwise this is good to go :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants