Add OpenRouter quantization support and propagate model names through assistant messages#149
Merged
Merged
Conversation
- Add `/model <name> quantizations [int4, int8]` command - Pass quantizations as provider routing in OpenAI stream for OpenRouter models - Introduce `_parse_quantizations`, `set_model_quantizations`, `update_model_quantizations` - Update help text and tests accordingly - Fix error handling in fetch_context_phase by broadening exception catch
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.
Overview
This pull request introduces two complementary improvements to the codebase. First, it adds support for configuring quantization filters specifically for OpenRouter models, allowing users to control which quantized versions of a model the provider may serve. Second, it ensures that the model name used to generate each assistant response is recorded in the message thread and displayed in the UI, giving users immediate visibility into which model produced a given response. Along the way, several related changes improve error handling, maintainability, and test coverage.
What This Means for Users
/modelsubcommand for OpenRouter quantization – Users can now restrict an OpenRouter model to specific quantizations (e.g.,int4,int8) by running/model <model_name> quantizations [int4, int8]. This helps control inference speed, cost, and quality when using OpenRouter.json.JSONDecodeError,TypeError) when parsing JSON responses, reducing the chance of crashes from malformed output.A Closer Look at the Changes
Error Handling Improvements
fetch_context_phase.py– Movedjson.loads(json_content)inside thetryblock and extended the exception tuple to includejson.JSONDecodeErrorandTypeErrorin addition toAttributeError. This prevents crashes when the LLM returns non‑JSON content or unexpected data structures.Model Tracking in Threads
messages/thread.py– Theadd_response,add_response_partial, andfinalize_responsemethods now accept an optionalmodelparameter. When provided, the model name is stored alongside the message in the thread’s message list.messages/message_builder.py– Type annotations were updated to reflect that message dictionaries can now contain a"model"key alongside the standard"role"and"content".message_service.py– The streaming response path now consistently passes the active model name (response_model) to all thread update methods, ensuring the model is recorded during streaming and finalization.commands/compact.py– When compacting a thread, the new assistant message now includes a"model"field (taken from the original compacted data) to preserve model information across compaction.OpenRouter Quantization Support
commands/model.py– Added a new_parse_quantizationshelper and a_handle_quantizationsfunction that processes the/model <name> quantizations [...]subcommand. The command validates that the target model uses the OpenRouter provider and that the quantization list is well‑formed.models/model_list.py– Introducedupdate_model_quantizationsto modify the"quantizations"field of an existing model entry in the in‑memory list and persist it to disk.core/application.py– Addedset_model_quantizationsas a public method that delegates toModelListand triggers a UI refresh.services/streaming/openai_stream.py– When the model provider is"open_router"and the model entry contains a non‑empty"quantizations"list, the stream request now includes anextra_bodywith{"provider": {"quantizations": [...]}}. This tells the OpenRouter API to only use the specified quantization levels.services/llm_requests.py– Added_provider_messagesto strip out any non‑essential keys (such as"model") from messages before they are sent to the LLM provider. This ensures that internal metadata does not leak into provider‑side message formatting.UI Model Display
ui/ui_wrapper.py,ui/cli_events.py,ui/tui/textual_events.py– Thestream_chunkmethod now accepts an optionalmodelparameter, which is forwarded to the TUI event system.ui/tui/chat/chat_view_widget.py– TheStreamChunkevent handler uses the providedmodel(or falls back to the latest assistant message’s model) when creating or updatingMessageBubblewidgets.ui/tui/chat/message_bubble.py– TheMessageBubbleclass now stores amodelattribute and sets the bubble’s border title to the model name for assistant messages. Aset_modelmethod allows updating the model name after creation.Help Text & Documentation
commands/help.py– Updated the inline help text for/modelto include the quantizations subcommand, showing the full usage syntax.Test Coverage
tests/test_commands_model.py– Added test cases for setting quantizations on an OpenRouter model, rejecting invalid providers, and rejecting empty quantization lists. A new OpenRouter model entry was added to the test fixtures.tests/test_openai_stream.py– New test file that verifies the OpenRouter quantization parameter is correctly included in the API requestextra_bodywhen the model has a quantization list configured.Generated by JrDev AI using deepseek-v4-flash