Repository navigation
fix: XunFei Spark provider API key verification and model listing - #17652
Conversation
- 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
📝 WalkthroughWalkthroughThe 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. ChangesXunFei credential and connection flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested labels: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
internal/handler/providers.go (1)
290-292: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove 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 winAdd a request-level
CheckConnectionunit test.This test only verifies the missing-key error. It does not test the new Spark-Lite chat request.
Use an
httptest.Serverto assert thatCheckConnectionsends the resolvedspark_api_passwordBearer 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
📒 Files selected for processing (4)
internal/entity/models/xunfei.gointernal/entity/models/xunfei_test.gointernal/handler/providers.gointernal/service/model_service.go
wangq8
left a comment
There was a problem hiding this comment.
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.
Summary
The XunFei Spark provider could not verify API keys or fetch the model list in Go mode:
{spark_api_password, spark_app_id, spark_api_secret, spark_api_key}), but the Go handler declaredapi_keyasstring.spark_api_password),CheckConnectionreturned "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.ListModels.Changes:
internal/handler/providers.go,internal/service/model_service.go: acceptapi_keyas 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:spark_api_passwordfor chat, streaming chat, andListModels.SparkChat.model2version.CheckConnectionvia a minimal Spark-Lite chat request.internal/entity/models/xunfei_test.go: unit tests for the model-name mapping, bearer-token resolution, and the newCheckConnectionbehavior.