Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 29, 2026

Related GitHub Issue

Closes: #11071

Description

This PR addresses the questions raised in issue #11071 about GLM model detection and sharing Z.ai optimizations with LM Studio and OpenAI-compatible endpoints.

Question 1: How do you detect whether LM Studio or the OpenAI-compatible endpoint is serving a GLM model?

This PR introduces an isGlmModel() utility function that detects GLM models by checking if the model ID contains common GLM patterns:

  • glm- prefix (official naming: glm-4, glm-4.5, glm-4.7)
  • glm4 (compact naming without dash)
  • chatglm (older ChatGLM models)

Question 2: Is there a way so that when improvements and fixes are made to how Roo Code communicates with GLM models on Z.ai, they will also become available to LM Studio and OpenAI-compatible endpoints running GLM models?

Yes! This PR enables automatic application of Z.ai optimizations for GLM models detected on LM Studio and OpenAI-compatible endpoints:

  1. mergeToolResultText: Merges text content after tool_results into the last tool message, preventing GLM models from dropping reasoning_content
  2. disableParallelToolCalls: Disables parallel_tool_calls by default for GLM models since they may not support it

Key Implementation Details

  • Created src/api/providers/utils/model-detection.ts with:
    • isGlmModel(modelId): Detects GLM models from model ID
    • getGlmModelOptions(modelId): Returns GLM-specific configuration options
  • Modified LmStudioHandler to detect GLM models and apply optimizations
  • Modified BaseOpenAiCompatibleProvider to detect GLM models and apply optimizations
  • Added comprehensive test coverage for model detection

Test Procedure

  1. Unit tests for model detection utility:

    • Run: cd src && npx vitest run api/providers/utils/__tests__/model-detection.spec.ts
    • Tests verify GLM detection for various model name formats
    • Tests verify non-GLM models are not falsely detected
  2. Manual testing (for users with GLM models):

    • Configure LM Studio or OpenAI-compatible endpoint with a GLM model
    • Start a task that uses tools
    • Verify the model no longer gets stuck in file read loops

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This implementation follows the same pattern used by Z.ai for GLM model handling. The key insight is that Z.ai uses convertToZAiFormat() with mergeToolResultText: true, which is functionally equivalent to using convertToOpenAiMessages() with the same option. This PR enables this option automatically when a GLM model is detected.

Future Z.ai improvements for GLM models can be easily extended to third-party providers by:

  1. Adding new detection patterns to isGlmModel() if needed
  2. Adding new configuration options to GlmModelOptions interface
  3. Updating providers to use the new options

Feedback and guidance are welcome!


Important

Adds GLM model detection and Z.ai optimizations for LM Studio and OpenAI-compatible providers, with tests for model detection.

  • Behavior:
    • Adds isGlmModel() and detectGlmModel() in glm-model-detection.ts to identify GLM models by model ID patterns.
    • Applies Z.ai optimizations for GLM models in BaseOpenAiCompatibleProvider and LmStudioHandler.
    • Disables parallel_tool_calls and enables mergeToolResultText for GLM models.
  • Implementation:
    • Modifies BaseOpenAiCompatibleProvider to detect GLM models and apply optimizations during construction and stream creation.
    • Modifies LmStudioHandler to detect GLM models and apply optimizations during message creation.
    • Adds logGlmDetection() for logging detection results.
  • Testing:
    • Adds tests in glm-model-detection.spec.ts to verify GLM model detection and configuration.

This description was created by Ellipsis for d010015. You can customize this summary. It will automatically update as commits are pushed.

@roomote
Copy link
Contributor Author

roomote bot commented Jan 29, 2026

Rooviewer Clock   See task on Roo Cloud

Reviewed all changes since last review. No issues found.

The GLM model detection implementation is clean, well-documented, and thoroughly tested:

  • detectGlmModel() correctly identifies GLM models from various ID formats (standard, prefixed paths, GGUF files, ChatGLM)
  • Version detection (4.5, 4.6, 4.7) and variant detection (base, air, flash, vision, etc.) work correctly
  • Both LmStudioHandler and BaseOpenAiCompatibleProvider properly integrate GLM-specific optimizations
  • mergeToolResultText and disableParallelToolCalls flags are correctly applied for GLM models
  • GLM-4.7 thinking mode is properly supported
  • Test coverage is comprehensive with tests for positive cases, negative cases, and edge cases
Previous reviews

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

…AI-compatible providers

This PR addresses Issue #11071 by implementing a comprehensive GLM model detection system:

1. Created glm-model-detection.ts utility that:
   - Detects GLM family models (GLM-4.5, 4.6, 4.7 and variants)
   - Supports various model ID formats (standard, MLX, GGUF, ChatGLM)
   - Identifies version (4.5, 4.6, 4.7) and variant (base, air, flash, v, etc.)
   - Returns appropriate configuration for each model

2. Updated LmStudioHandler to:
   - Detect GLM models and log detection results to console
   - Use convertToZAiFormat with mergeToolResultText for GLM models
   - Disable parallel_tool_calls for GLM models
   - Handle reasoning_content for GLM-4.7 models

3. Updated BaseOpenAiCompatibleProvider similarly

4. Added 33 comprehensive tests for the detection utility

The detection uses flexible regex patterns to match model IDs like:
- mlx-community/GLM-4.5-4bit
- GLM-4.5-UD-Q8_K_XL-00001-of-00008.gguf
- glm-4.5, glm-4.7-flash, etc.
@roomote roomote bot force-pushed the feature/glm-model-detection-11071 branch from 1379ba4 to d010015 Compare January 30, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] GLM4.5 via LMStudio as well as via an OpenAI-compatible endpoint stuck repeating file reads

1 participant