Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix hardcoded values part 2
perf3ct0d7cfe8061c7
diff --git a/src/services/llm/pipeline/chat_pipeline.ts b/src/services/llm/pipeline/chat_pipeline.tsindex 12be376a02..0136be93c1 100644--- a/src/services/llm/pipeline/chat_pipeline.ts+++ b/src/services/llm/pipeline/chat_pipeline.ts@@ -13,6 +13,7 @@ import toolRegistry from '../tools/tool_registry.js';import toolInitializer from '../tools/tool_initializer.js';import log from '../../log.js';import type { LLMServiceInterface } from '../interfaces/agent_tool_interfaces.js';+import { SEARCH_CONSTANTS } from '../constants/search_constants.js';/*** Pipeline for managing the entire chat flow@@ -56,7 +57,7 @@ export class ChatPipeline {this.config = {enableStreaming: true,enableMetrics: true,- maxToolCallIterations: 5,+ maxToolCallIterations: SEARCH_CONSTANTS.TOOL_EXECUTION.MAX_TOOL_CALL_ITERATIONS,...config};@@ -194,9 +195,9 @@ export class ChatPipeline {query: userQuery,noteId: input.noteId || 'global',options: {- maxResults: 5, // Can be adjusted+ maxResults: SEARCH_CONSTANTS.CONTEXT.MAX_SIMILAR_NOTES,useEnhancedQueries: true,- threshold: 0.6,+ threshold: SEARCH_CONSTANTS.VECTOR_SEARCH.DEFAULT_THRESHOLD,llmService: llmService || undefined}});diff --git a/src/services/llm/pipeline/stages/model_selection_stage.ts b/src/services/llm/pipeline/stages/model_selection_stage.tsindex 5245e3a4c9..35634c2106 100644--- a/src/services/llm/pipeline/stages/model_selection_stage.ts+++ b/src/services/llm/pipeline/stages/model_selection_stage.ts@@ -5,6 +5,7 @@ import type { ModelMetadata } from '../../providers/provider_options.js';import log from '../../../log.js';import options from '../../../options.js';import aiServiceManager from '../../ai_service_manager.js';+import { SEARCH_CONSTANTS, MODEL_CAPABILITIES } from "../../constants/search_constants.js";/*** Pipeline stage for selecting the appropriate LLM model*/@@ -28,7 +29,7 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,// Start with provided options or create a new objectconst updatedOptions: ChatCompletionOptions = { ...(inputOptions || {}) };-+// Preserve the stream option exactly as it was provided, including undefined state// This is critical for ensuring the stream option propagates correctly down the pipelinelog.info(`[ModelSelectionStage] After copy, stream: ${updatedOptions.stream}, type: ${typeof updatedOptions.stream}`);@@ -154,9 +155,9 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,}// Check content length if provided- if (contentLength && contentLength > 5000) {+ if (contentLength && contentLength > SEARCH_CONSTANTS.CONTEXT.CONTENT_LENGTH.MEDIUM_THRESHOLD) {// For large content, favor more powerful models- queryComplexity = contentLength > 10000 ? 'high' : 'medium';+ queryComplexity = contentLength > SEARCH_CONSTANTS.CONTEXT.CONTENT_LENGTH.HIGH_THRESHOLD ? 'high' : 'medium';}// Set the model and add provider metadata@@ -256,7 +257,7 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,// Get the first available provider and its default modelconst defaultProvider = availableProviders[0] as 'openai' | 'anthropic' | 'ollama' | 'local';- let defaultModel = 'gpt-3.5-turbo'; // Default fallback+ let defaultModel = 'gpt-3.5-turbo'; // Use model from our constants// Set provider metadataif (!input.options.providerMetadata) {@@ -274,17 +275,22 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,* Get estimated context window for Ollama models*/private getOllamaContextWindow(model: string): number {+ // Try to find exact matches in MODEL_CAPABILITIES+ if (model in MODEL_CAPABILITIES) {+ return MODEL_CAPABILITIES[model as keyof typeof MODEL_CAPABILITIES].contextWindowTokens;+ }+// Estimate based on model familyif (model.includes('llama3')) {- return 8192;+ return MODEL_CAPABILITIES['gpt-4'].contextWindowTokens;} else if (model.includes('llama2')) {- return 4096;+ return MODEL_CAPABILITIES['default'].contextWindowTokens;} else if (model.includes('mistral') || model.includes('mixtral')) {- return 8192;+ return MODEL_CAPABILITIES['gpt-4'].contextWindowTokens;} else if (model.includes('gemma')) {- return 8192;+ return MODEL_CAPABILITIES['gpt-4'].contextWindowTokens;} else {- return 4096; // Default fallback+ return MODEL_CAPABILITIES['default'].contextWindowTokens;}}}diff --git a/src/services/llm/pipeline/stages/semantic_context_extraction_stage.ts b/src/services/llm/pipeline/stages/semantic_context_extraction_stage.tsindex d386e38f24..bf2cc8fd7a 100644--- a/src/services/llm/pipeline/stages/semantic_context_extraction_stage.ts+++ b/src/services/llm/pipeline/stages/semantic_context_extraction_stage.ts@@ -7,6 +7,7 @@ import contextFormatter from '../../context/modules/context_formatter.js';import providerManager from '../../context/modules/provider_manager.js';import type { NoteSearchResult } from '../../interfaces/context_interfaces.js';import type { Message } from '../../ai_interface.js';+import { SEARCH_CONSTANTS } from "../../constants/search_constants.js";/*** Pipeline stage for extracting semantic context from notes@@ -35,7 +36,7 @@ export class SemanticContextExtractionStage extends BasePipelineStage<SemanticCooptions: {maxResults,useEnhancedQueries: true,- threshold: 0.6,+ threshold: SEARCH_CONSTANTS.VECTOR_SEARCH.DEFAULT_THRESHOLD,llmService: undefined // Let the vectorSearchStage use the default service}});diff --git a/src/services/llm/pipeline/stages/vector_search_stage.ts b/src/services/llm/pipeline/stages/vector_search_stage.tsindex f56fb74e1e..9766d55e3b 100644--- a/src/services/llm/pipeline/stages/vector_search_stage.ts+++ b/src/services/llm/pipeline/stages/vector_search_stage.ts@@ -9,6 +9,7 @@ import log from '../../../log.js';import vectorSearchService from '../../context/services/vector_search_service.js';import type { NoteSearchResult } from '../../interfaces/context_interfaces.js';import type { LLMServiceInterface } from '../../interfaces/agent_tool_interfaces.js';+import { SEARCH_CONSTANTS } from '../../constants/search_constants.js';export interface VectorSearchInput {query: string;@@ -46,8 +47,8 @@ export class VectorSearchStage {} = input;const {- maxResults = 10,- threshold = 0.6,+ maxResults = SEARCH_CONSTANTS.VECTOR_SEARCH.DEFAULT_MAX_RESULTS,+ threshold = SEARCH_CONSTANTS.VECTOR_SEARCH.DEFAULT_THRESHOLD,useEnhancedQueries = false,llmService = undefined} = options;