feat(modelnormalizer): gateway-edge middleware with declarative model alias rules - #33
Closed
weselben wants to merge 1 commit into
Closed
feat(modelnormalizer): gateway-edge middleware with declarative model alias rules#33weselben wants to merge 1 commit into
weselben wants to merge 1 commit into
Conversation
…es from declarative rules
Adds a data-driven normalizer that rewrites chat requests before
provider dispatch. Operators declare aliases in a new top-level
`model_normalizer:` section (with a JSON `MODEL_NORMALIZER` env var);
the normalizer rewrites the request `model` to a provider-qualified
target and injects a per-alias thinking policy. Runs as an Echo
middleware before workflow resolution so resolvers, failovers, budgets,
and caching all see the rewritten request. No provider package is
touched — works for kimicode and any future subscription provider.
Rule schema:
- alias: kimi-k2.6
target: kimicode/kimi-for-coding
thinking: disabled
modes: [chat]
context_window: 262144
/v1/models merges synthesized entries for each alias with the
advertised metadata; layers on top of any existing ExposedModelLister
via ChainedExposedModelLister.
Refs #27
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: trueThanks 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prototype D of 4 for #27 — a gateway-edge middleware approach. Compare: prototype A (provider-level static map +
KIMICODE_MODEL_MAP=offtoggle, PR #30), prototype B (provider-level map always-on, PR #29), prototype C (virtual-models + disable_reasoning, PR #31).Approach
A data-driven normalizer that runs as an Echo middleware before workflow resolution. Operators declare aliases in
config.yaml(orMODEL_NORMALIZERenv), and the normalizer rewrites the request body so every downstream stage (resolver, failover, budgets, cache) sees the canonical upstream target. No provider package is touched — works for kimicode and any future subscription provider.What's wired
internal/modelnormalizer— Rule + Normalizer (AdaptChatRequest) + config loading (config/modelnormalizer.go) +MODEL_NORMALIZERenv bindinginternal/server/model_normalizer_middleware.go— Echo middleware reading the body, calling the normalizer, rewriting the body, and stamping an audit revision when the audit log is enabledinternal/server/http.go— registers the middleware viae.Useand merges synthesized/v1/modelsentries (viaChainedExposedModelListerwhen another lister exists)internal/app/app.go— builds the normalizer from config and passes it throughTrade-offs vs A/B/C
Caveats
kimicode/kimi-for-coding) because the rewrite happens before routing. The canonical alias is preserved on the audit entry via the request-revision snapshot the middleware stamps, so dashboards can still see the client's original model.Tests
go build ./...— passgo test ./internal/modelnormalizer/... ./internal/server/... ./config/...— pass:internal/modelnormalizer/config_test.go— rule parsing, env layering, validationinternal/modelnormalizer/normalizer_test.go— adapt per rule, unknown IDs pass through, idempotenceinternal/server/modelnormalizer_server_test.go— chat-completion rewrites the model and injects thinking, unknown aliases pass through,/v1/modelsmerges synthesized entries with metadata, nil normalizer leaves the handler unchangedRefs #27