Skip to content

fix: XunFei Spark provider API key verification and model listing - #17652

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/xunfei-spark-provider
Aug 3, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/xunfei-spark-provider

Conversation

@euvre

@euvre euvre commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

The XunFei Spark provider could not verify API keys or fetch the model list in Go mode:

  1. 400 "cannot unmarshal object into Go struct field CreateProviderInstanceRequest.api_key of type string" — the frontend submits XunFei Spark credentials as an object ({spark_api_password, spark_app_id, spark_api_secret, spark_api_key}), but the Go handler declared api_key as string.
  2. Verification always failed — the driver sent the whole JSON credential bundle as the Bearer token (the Spark HTTP API expects only spark_api_password), CheckConnection returned "no such method", and catalog model names (e.g. Spark-Max) were not mapped to the API version identifiers (e.g. generalv3.5) the Spark API requires.
  3. Model listing failed with HTTP 400 — same Bearer-token issue in ListModels.

Changes:

  • internal/handler/providers.go, internal/service/model_service.go: accept api_key as either a string or an object in create/alter/verify provider-instance requests and normalize it to the persisted JSON-string form.
  • internal/entity/models/xunfei.go:
    • Resolve the Bearer token from the credential bundle's spark_api_password for chat, streaming chat, and ListModels.
    • Map catalog model names to Spark API version identifiers, matching Python's SparkChat.model2version.
    • Implement CheckConnection via a minimal Spark-Lite chat request.
    • Surface upstream HTTP status/body on non-stream chat errors instead of a generic parse error.
  • internal/entity/models/xunfei_test.go: unit tests for the model-name mapping, bearer-token resolution, and the new CheckConnection behavior.

- Accept object-form api_key (credential bundle) in create/alter/verify
  provider instance requests, normalizing it to a JSON string
- Resolve the Bearer token from the bundle's spark_api_password for
  chat, streaming chat, and ListModels calls
- Map catalog model names (Spark-Max, etc.) to the API version
  identifiers (generalv3.5, etc.), matching Python's SparkChat
- Implement CheckConnection via a minimal Spark-Lite chat request
- Return the upstream status code/body on non-stream chat errors
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jul 31, 2026
@euvre euvre added the ci Continue Integration label Jul 31, 2026
@euvre
euvre requested a review from wangq8 July 31, 2026 11:16
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds structured credential handling for provider requests. XunFei resolves Spark model names and bearer tokens, reports failed HTTP responses, and validates connections with a minimal Spark-Lite chat request.

Changes

XunFei credential and connection flow

Layer / File(s) Summary
Normalize structured provider credentials
internal/handler/providers.go, internal/service/model_service.go
Provider and connection requests accept JSON strings or credential objects. Handlers normalize these values before provider creation, alteration, and connection checks.
Resolve Spark requests and validate connections
internal/entity/models/xunfei.go, internal/entity/models/xunfei_test.go
XunFei maps catalog model names, extracts bearer tokens, authenticates chat and model-list requests, reports non-streaming API errors, and validates connections with Spark-Lite. Tests cover mappings, token fallback, and connection behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested labels: 🧪 test

Poem

A rabbit packs the keys just right,
Then maps the Spark model in flight.
A tiny chat checks connection bright,
Errors return their body and bite.
“Hop!” says the rabbit, “tests unite!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary fix for XunFei Spark API key verification and model listing.
Description check ✅ Passed The description includes the required Summary section and clearly explains the background, failures, implementation, and tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
internal/handler/providers.go (1)

290-292: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove migration-history references from Go comments.

Keep comments focused on current Go behavior.

  • internal/handler/providers.go#L290-L292: Remove the Python parity reference.
  • internal/entity/models/xunfei.go#L30-L32: Remove the Python implementation reference.

As per coding guidelines, “keep package and doc comments aligned with the current runtime path rather than migration history.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/handler/providers.go` around lines 290 - 292, Remove the Python
parity reference from the comment in internal/handler/providers.go lines
290-292, leaving only the description of current Go behavior. Also remove the
Python implementation reference from the comment in
internal/entity/models/xunfei.go lines 30-32; no runtime code changes are
needed.

Source: Coding guidelines

internal/entity/models/xunfei_test.go (1)

49-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a request-level CheckConnection unit test.

This test only verifies the missing-key error. It does not test the new Spark-Lite chat request.

Use an httptest.Server to assert that CheckConnection sends the resolved spark_api_password Bearer token and the Spark-Lite model request. Keep the test local so it remains a default unit test.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/entity/models/xunfei_test.go` around lines 49 - 56, Extend
TestXunFeiCheckConnectionRequiresAPIKey or add a neighboring local unit test
using httptest.Server to exercise CheckConnection with valid configuration.
Assert the request targets the Spark-Lite chat endpoint, includes the resolved
spark_api_password as the Bearer authorization token, and sends the expected
Spark-Lite model request payload while preserving the existing missing-key
assertion.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/entity/models/xunfei_test.go`:
- Around line 49-56: Extend TestXunFeiCheckConnectionRequiresAPIKey or add a
neighboring local unit test using httptest.Server to exercise CheckConnection
with valid configuration. Assert the request targets the Spark-Lite chat
endpoint, includes the resolved spark_api_password as the Bearer authorization
token, and sends the expected Spark-Lite model request payload while preserving
the existing missing-key assertion.

In `@internal/handler/providers.go`:
- Around line 290-292: Remove the Python parity reference from the comment in
internal/handler/providers.go lines 290-292, leaving only the description of
current Go behavior. Also remove the Python implementation reference from the
comment in internal/entity/models/xunfei.go lines 30-32; no runtime code changes
are needed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b4b2c411-b77f-4c50-8c0a-35b3688f75bb

📥 Commits

Reviewing files that changed from the base of the PR and between f03a00c and 28cd2af.

📒 Files selected for processing (4)
  • internal/entity/models/xunfei.go
  • internal/entity/models/xunfei_test.go
  • internal/handler/providers.go
  • internal/service/model_service.go

@wangq8 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Solid fix for XunFei Spark in Go mode. Well-structured with tests.

Good:

  • normalizeAPIKey / json.RawMessage approach in the handler is clean and correctly handles both string and object api_key without breaking existing providers
  • resolveBearerToken correctly extracts spark_api_password from the credential bundle for Bearer auth
  • resolveSparkModel mapping matches Python's SparkChat.model2version — good parity
  • CheckConnection via minimal Spark-Lite request is a reasonable verification strategy
  • Better error surfacing (HTTP status + body) on non-200 responses instead of opaque parse errors
  • Tests cover model mapping, bearer token resolution, and CheckConnection edge cases

One minor note (non-blocking):

  • resolveBearerToken uses strings.HasPrefix(key, "{") as a heuristic to detect JSON bundles. While extremely unlikely in practice, a plain API key starting with "{" would trigger a failed unmarshal (falls back gracefully to raw key, so no breakage — just something to be aware of).

LGTM — approved.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 31, 2026
@wangq8 wangq8 removed the ci Continue Integration label Aug 3, 2026
@wangq8
wangq8 requested a review from Lynn-Inf August 3, 2026 02:05

@Lynn-Inf Lynn-Inf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@wangq8 wangq8 added the ci Continue Integration label Aug 3, 2026
@wangq8
wangq8 merged commit 8353fc7 into infiniflow:main Aug 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants