From 82965ae6e286b2599428a43c36ee87d82f735d24 Mon Sep 17 00:00:00 2001 From: ekko <152005280+EKKOLearnAI@users.noreply.github.com> Date: Fri, 24 Apr 2026 11:18:11 +0800 Subject: [PATCH] refactor: rewrite model-context to use js-yaml, add context_length to provider form (#177) * fix: context-length API returns 200K instead of actual model context Two bugs cause the /api/hermes/sessions/context-length endpoint to always return DEFAULT_CONTEXT_LENGTH (200K): 1. getModelContextLength ignores config.yaml model.context_length The function only checks models_dev_cache.json (which doesn't exist in default installations) and falls back to the hardcoded 200K default, completely ignoring the user's explicit model.context_length setting in config.yaml. 2. getDefaultModel regex fails when api_key/base_url come before default The regex /^model:\s*\n\s+default:\s*(.+)$/m assumes 'default' is the first child key under 'model:', but when api_key or base_url appear first in the YAML, the match fails. This causes getModelContextLength to short-circuit to DEFAULT_CONTEXT_LENGTH before even reaching the cache lookup. Fix: - Add getDefaultModelRobust() that extracts the entire model: block first, then searches for default: within it - Add getConfigContextLength() that reads model.context_length from config.yaml as a fallback (matching hermes-agent priority) - Update getModelContextLength() resolution order: 1. models_dev_cache.json (existing) 2. config.yaml model.context_length (new) 3. DEFAULT_CONTEXT_LENGTH (existing fallback) Closes #169 * refactor: rewrite model-context to use js-yaml, add context_length to provider form - Replace fragile regex-based YAML parsing with js-yaml for reliable config.yaml reads - Fix context_length resolution priority: config.yaml override > custom_providers > models_dev_cache > 200K default - Add context_length input field when adding custom providers in ProviderFormModal - Backend: persist context_length to custom_providers models..context_length in config.yaml - Add i18n keys (contextLength, contextLengthPlaceholder) to all 8 locales Co-Authored-By: Claude Opus 4.6 * fix: use NInputNumber instead of NInput type=number for context_length NInput does not support type="number" in Naive UI. Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: devilardis <53129661@qq.com> Co-authored-by: Claude Opus 4.6 --- packages/client/src/api/hermes/system.ts | 1 + .../hermes/models/ProviderFormModal.vue | 17 ++- packages/client/src/i18n/locales/de.ts | 2 + packages/client/src/i18n/locales/en.ts | 2 + packages/client/src/i18n/locales/es.ts | 2 + packages/client/src/i18n/locales/fr.ts | 2 + packages/client/src/i18n/locales/ja.ts | 2 + packages/client/src/i18n/locales/ko.ts | 2 + packages/client/src/i18n/locales/pt.ts | 2 + packages/client/src/i18n/locales/zh.ts | 2 + .../src/controllers/hermes/providers.ts | 26 ++++- .../src/services/hermes/model-context.ts | 101 +++++++++++++++--- 12 files changed, 143 insertions(+), 18 deletions(-) diff --git a/packages/client/src/api/hermes/system.ts b/packages/client/src/api/hermes/system.ts index 513a2b3..fc359be 100644 --- a/packages/client/src/api/hermes/system.ts +++ b/packages/client/src/api/hermes/system.ts @@ -45,6 +45,7 @@ export interface CustomProvider { base_url: string api_key: string model: string + context_length?: number providerKey?: string | null } diff --git a/packages/client/src/components/hermes/models/ProviderFormModal.vue b/packages/client/src/components/hermes/models/ProviderFormModal.vue index e9643d9..df7049d 100644 --- a/packages/client/src/components/hermes/models/ProviderFormModal.vue +++ b/packages/client/src/components/hermes/models/ProviderFormModal.vue @@ -1,6 +1,6 @@