11import 'dart:convert' ;
22
3+ import 'package:chatmcp/provider/provider_manager.dart' ;
4+
35class SystemPromptGenerator {
4- /// Default prompt template
5- final String template = '''
6+ /// Base prompt template without tool instructions
7+ final String baseTemplate = '''
68<system_prompt>
79You will select appropriate tools and call them to solve user queries
810
911**CRITICAL CONSTRAINT: You MUST call only ONE tool per response. Never call multiple tools simultaneously.**
10- </system_prompt>
12+ </system_prompt>''' ;
13+
14+ /// Tool-related template parts
15+ final String toolDefinitionsTemplate = '''
1116
1217**Tool Definitions:**
1318Here are the functions available, described in JSONSchema format:
1419<tool_definitions>
1520{{ TOOL DEFINITIONS IN JSON SCHEMA }}
16- </tool_definitions>
21+ </tool_definitions>''' ;
22+
23+ final String toolUsageTemplate = '''
1724
1825<tool_usage_instructions>
1926TOOL USE
@@ -40,12 +47,10 @@ For example:
4047</function>
4148
4249Always adhere to this format for the tool use to ensure proper parsing and execution.
43- </tool_usage_instructions>
44- ''' ;
50+ </tool_usage_instructions>''' ;
4551
4652 /// Default user system prompt
47- final String defaultUserSystemPrompt =
48- 'You are an intelligent assistant capable of using tools to solve user queries effectively.' ;
53+ final String defaultUserSystemPrompt = 'You are an intelligent assistant capable of using tools to solve user queries effectively.' ;
4954
5055 /// Default tool configuration
5156 final String defaultToolConfig = 'No additional configuration is required.' ;
@@ -58,16 +63,28 @@ Always adhere to this format for the tool use to ensure proper parsing and execu
5863 String generatePrompt ({
5964 required List <Map <String , dynamic >> tools,
6065 }) {
61- // Use provided values or defaults
62- final finalUserPrompt = defaultUserSystemPrompt;
66+ // Start with base template
67+
68+ var userPrompt = ProviderManager .settingsProvider.generalSetting.systemPrompt;
69+
70+ var language = ProviderManager .settingsProvider.generalSetting.locale;
71+
72+ var prompt = "$userPrompt \n $baseTemplate " ;
73+ if (language.isNotEmpty) {
74+ prompt += "\n\n Language: $language " ;
75+ }
76+
77+ // Only add tool-related sections if tools are available
78+ if (tools.isNotEmpty) {
79+ // Convert tools JSON to formatted string
80+ final toolsJsonSchema = const JsonEncoder .withIndent (' ' ).convert (tools);
6381
64- // Convert tools JSON to formatted string
65- final toolsJsonSchema = const JsonEncoder . withIndent ( ' ' ). convert (tools );
82+ // Add tool definitions section
83+ prompt += toolDefinitionsTemplate. replaceAll ( '{{ TOOL DEFINITIONS IN JSON SCHEMA }}' , toolsJsonSchema );
6684
67- // Replace placeholders in template
68- var prompt = template
69- .replaceAll ('{{ TOOL DEFINITIONS IN JSON SCHEMA }}' , toolsJsonSchema)
70- .replaceAll ('{{ USER SYSTEM PROMPT }}' , finalUserPrompt);
85+ // Add tool usage instructions
86+ prompt += toolUsageTemplate;
87+ }
7188
7289 return prompt;
7390 }
0 commit comments