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.<model>.context_length in config.yaml - Add i18n keys (contextLength, contextLengthPlaceholder) to all 8 locales Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: devilardis <53129661@qq.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,9 +5,17 @@ import * as hermesCli from '../../services/hermes/hermes-cli'
|
||||
import { readConfigYaml, writeConfigYaml, saveEnvValue, PROVIDER_ENV_MAP } from '../../services/config-helpers'
|
||||
import { logger } from '../../services/logger'
|
||||
|
||||
function buildProviderEntry(name: string, base_url: string, api_key: string, model: string, context_length?: number) {
|
||||
const entry: any = { name, base_url, api_key, model }
|
||||
if (context_length && context_length > 0) {
|
||||
entry.models = { [model]: { context_length } }
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
export async function create(ctx: any) {
|
||||
const { name, base_url, api_key, model, providerKey } = ctx.request.body as {
|
||||
name: string; base_url: string; api_key: string; model: string; providerKey?: string | null
|
||||
const { name, base_url, api_key, model, context_length, providerKey } = ctx.request.body as {
|
||||
name: string; base_url: string; api_key: string; model: string; context_length?: number; providerKey?: string | null
|
||||
}
|
||||
console.log(name, base_url, api_key, model, providerKey)
|
||||
if (!name || !base_url || !model) {
|
||||
@@ -30,8 +38,13 @@ export async function create(ctx: any) {
|
||||
existing.base_url = base_url
|
||||
existing.api_key = api_key
|
||||
existing.model = model
|
||||
if (context_length && context_length > 0) {
|
||||
if (!existing.models) existing.models = {}
|
||||
existing.models[model] = existing.models[model] || {}
|
||||
existing.models[model].context_length = context_length
|
||||
}
|
||||
} else {
|
||||
config.custom_providers.push({ name: name.trim().toLowerCase().replace(/ /g, '-'), base_url, api_key, model })
|
||||
config.custom_providers.push(buildProviderEntry(name.trim().toLowerCase().replace(/ /g, '-'), base_url, api_key, model, context_length))
|
||||
}
|
||||
config.model.default = model
|
||||
config.model.provider = poolKey
|
||||
@@ -51,8 +64,13 @@ export async function create(ctx: any) {
|
||||
existing.base_url = base_url
|
||||
existing.api_key = api_key
|
||||
existing.model = model
|
||||
if (context_length && context_length > 0) {
|
||||
if (!existing.models) existing.models = {}
|
||||
existing.models[model] = existing.models[model] || {}
|
||||
existing.models[model].context_length = context_length
|
||||
}
|
||||
} else {
|
||||
config.custom_providers.push({ name: poolKey, base_url, api_key, model })
|
||||
config.custom_providers.push(buildProviderEntry(poolKey, base_url, api_key, model, context_length))
|
||||
}
|
||||
config.model.default = model
|
||||
config.model.provider = `custom:${poolKey}`
|
||||
|
||||
Reference in New Issue
Block a user