kweaver-dolphin 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- DolphinLanguageSDK/__init__.py +58 -0
- dolphin/__init__.py +62 -0
- dolphin/cli/__init__.py +20 -0
- dolphin/cli/args/__init__.py +9 -0
- dolphin/cli/args/parser.py +567 -0
- dolphin/cli/builtin_agents/__init__.py +22 -0
- dolphin/cli/commands/__init__.py +4 -0
- dolphin/cli/interrupt/__init__.py +8 -0
- dolphin/cli/interrupt/handler.py +205 -0
- dolphin/cli/interrupt/keyboard.py +82 -0
- dolphin/cli/main.py +49 -0
- dolphin/cli/multimodal/__init__.py +34 -0
- dolphin/cli/multimodal/clipboard.py +327 -0
- dolphin/cli/multimodal/handler.py +249 -0
- dolphin/cli/multimodal/image_processor.py +214 -0
- dolphin/cli/multimodal/input_parser.py +149 -0
- dolphin/cli/runner/__init__.py +8 -0
- dolphin/cli/runner/runner.py +989 -0
- dolphin/cli/ui/__init__.py +10 -0
- dolphin/cli/ui/console.py +2795 -0
- dolphin/cli/ui/input.py +340 -0
- dolphin/cli/ui/layout.py +425 -0
- dolphin/cli/ui/stream_renderer.py +302 -0
- dolphin/cli/utils/__init__.py +8 -0
- dolphin/cli/utils/helpers.py +135 -0
- dolphin/cli/utils/version.py +49 -0
- dolphin/core/__init__.py +107 -0
- dolphin/core/agent/__init__.py +10 -0
- dolphin/core/agent/agent_state.py +69 -0
- dolphin/core/agent/base_agent.py +970 -0
- dolphin/core/code_block/__init__.py +0 -0
- dolphin/core/code_block/agent_init_block.py +0 -0
- dolphin/core/code_block/assign_block.py +98 -0
- dolphin/core/code_block/basic_code_block.py +1865 -0
- dolphin/core/code_block/explore_block.py +1327 -0
- dolphin/core/code_block/explore_block_v2.py +712 -0
- dolphin/core/code_block/explore_strategy.py +672 -0
- dolphin/core/code_block/judge_block.py +220 -0
- dolphin/core/code_block/prompt_block.py +32 -0
- dolphin/core/code_block/skill_call_deduplicator.py +291 -0
- dolphin/core/code_block/tool_block.py +129 -0
- dolphin/core/common/__init__.py +17 -0
- dolphin/core/common/constants.py +176 -0
- dolphin/core/common/enums.py +1173 -0
- dolphin/core/common/exceptions.py +133 -0
- dolphin/core/common/multimodal.py +539 -0
- dolphin/core/common/object_type.py +165 -0
- dolphin/core/common/output_format.py +432 -0
- dolphin/core/common/types.py +36 -0
- dolphin/core/config/__init__.py +16 -0
- dolphin/core/config/global_config.py +1289 -0
- dolphin/core/config/ontology_config.py +133 -0
- dolphin/core/context/__init__.py +12 -0
- dolphin/core/context/context.py +1580 -0
- dolphin/core/context/context_manager.py +161 -0
- dolphin/core/context/var_output.py +82 -0
- dolphin/core/context/variable_pool.py +356 -0
- dolphin/core/context_engineer/__init__.py +41 -0
- dolphin/core/context_engineer/config/__init__.py +5 -0
- dolphin/core/context_engineer/config/settings.py +402 -0
- dolphin/core/context_engineer/core/__init__.py +7 -0
- dolphin/core/context_engineer/core/budget_manager.py +327 -0
- dolphin/core/context_engineer/core/context_assembler.py +583 -0
- dolphin/core/context_engineer/core/context_manager.py +637 -0
- dolphin/core/context_engineer/core/tokenizer_service.py +260 -0
- dolphin/core/context_engineer/example/incremental_example.py +267 -0
- dolphin/core/context_engineer/example/traditional_example.py +334 -0
- dolphin/core/context_engineer/services/__init__.py +5 -0
- dolphin/core/context_engineer/services/compressor.py +399 -0
- dolphin/core/context_engineer/utils/__init__.py +6 -0
- dolphin/core/context_engineer/utils/context_utils.py +441 -0
- dolphin/core/context_engineer/utils/message_formatter.py +270 -0
- dolphin/core/context_engineer/utils/token_utils.py +139 -0
- dolphin/core/coroutine/__init__.py +15 -0
- dolphin/core/coroutine/context_snapshot.py +154 -0
- dolphin/core/coroutine/context_snapshot_profile.py +922 -0
- dolphin/core/coroutine/context_snapshot_store.py +268 -0
- dolphin/core/coroutine/execution_frame.py +145 -0
- dolphin/core/coroutine/execution_state_registry.py +161 -0
- dolphin/core/coroutine/resume_handle.py +101 -0
- dolphin/core/coroutine/step_result.py +101 -0
- dolphin/core/executor/__init__.py +18 -0
- dolphin/core/executor/debug_controller.py +630 -0
- dolphin/core/executor/dolphin_executor.py +1063 -0
- dolphin/core/executor/executor.py +624 -0
- dolphin/core/flags/__init__.py +27 -0
- dolphin/core/flags/definitions.py +49 -0
- dolphin/core/flags/manager.py +113 -0
- dolphin/core/hook/__init__.py +95 -0
- dolphin/core/hook/expression_evaluator.py +499 -0
- dolphin/core/hook/hook_dispatcher.py +380 -0
- dolphin/core/hook/hook_types.py +248 -0
- dolphin/core/hook/isolated_variable_pool.py +284 -0
- dolphin/core/interfaces.py +53 -0
- dolphin/core/llm/__init__.py +0 -0
- dolphin/core/llm/llm.py +495 -0
- dolphin/core/llm/llm_call.py +100 -0
- dolphin/core/llm/llm_client.py +1285 -0
- dolphin/core/llm/message_sanitizer.py +120 -0
- dolphin/core/logging/__init__.py +20 -0
- dolphin/core/logging/logger.py +526 -0
- dolphin/core/message/__init__.py +8 -0
- dolphin/core/message/compressor.py +749 -0
- dolphin/core/parser/__init__.py +8 -0
- dolphin/core/parser/parser.py +405 -0
- dolphin/core/runtime/__init__.py +10 -0
- dolphin/core/runtime/runtime_graph.py +926 -0
- dolphin/core/runtime/runtime_instance.py +446 -0
- dolphin/core/skill/__init__.py +14 -0
- dolphin/core/skill/context_retention.py +157 -0
- dolphin/core/skill/skill_function.py +686 -0
- dolphin/core/skill/skill_matcher.py +282 -0
- dolphin/core/skill/skillkit.py +700 -0
- dolphin/core/skill/skillset.py +72 -0
- dolphin/core/trajectory/__init__.py +10 -0
- dolphin/core/trajectory/recorder.py +189 -0
- dolphin/core/trajectory/trajectory.py +522 -0
- dolphin/core/utils/__init__.py +9 -0
- dolphin/core/utils/cache_kv.py +212 -0
- dolphin/core/utils/tools.py +340 -0
- dolphin/lib/__init__.py +93 -0
- dolphin/lib/debug/__init__.py +8 -0
- dolphin/lib/debug/visualizer.py +409 -0
- dolphin/lib/memory/__init__.py +28 -0
- dolphin/lib/memory/async_processor.py +220 -0
- dolphin/lib/memory/llm_calls.py +195 -0
- dolphin/lib/memory/manager.py +78 -0
- dolphin/lib/memory/sandbox.py +46 -0
- dolphin/lib/memory/storage.py +245 -0
- dolphin/lib/memory/utils.py +51 -0
- dolphin/lib/ontology/__init__.py +12 -0
- dolphin/lib/ontology/basic/__init__.py +0 -0
- dolphin/lib/ontology/basic/base.py +102 -0
- dolphin/lib/ontology/basic/concept.py +130 -0
- dolphin/lib/ontology/basic/object.py +11 -0
- dolphin/lib/ontology/basic/relation.py +63 -0
- dolphin/lib/ontology/datasource/__init__.py +27 -0
- dolphin/lib/ontology/datasource/datasource.py +66 -0
- dolphin/lib/ontology/datasource/oracle_datasource.py +338 -0
- dolphin/lib/ontology/datasource/sql.py +845 -0
- dolphin/lib/ontology/mapping.py +177 -0
- dolphin/lib/ontology/ontology.py +733 -0
- dolphin/lib/ontology/ontology_context.py +16 -0
- dolphin/lib/ontology/ontology_manager.py +107 -0
- dolphin/lib/skill_results/__init__.py +31 -0
- dolphin/lib/skill_results/cache_backend.py +559 -0
- dolphin/lib/skill_results/result_processor.py +181 -0
- dolphin/lib/skill_results/result_reference.py +179 -0
- dolphin/lib/skill_results/skillkit_hook.py +324 -0
- dolphin/lib/skill_results/strategies.py +328 -0
- dolphin/lib/skill_results/strategy_registry.py +150 -0
- dolphin/lib/skillkits/__init__.py +44 -0
- dolphin/lib/skillkits/agent_skillkit.py +155 -0
- dolphin/lib/skillkits/cognitive_skillkit.py +82 -0
- dolphin/lib/skillkits/env_skillkit.py +250 -0
- dolphin/lib/skillkits/mcp_adapter.py +616 -0
- dolphin/lib/skillkits/mcp_skillkit.py +771 -0
- dolphin/lib/skillkits/memory_skillkit.py +650 -0
- dolphin/lib/skillkits/noop_skillkit.py +31 -0
- dolphin/lib/skillkits/ontology_skillkit.py +89 -0
- dolphin/lib/skillkits/plan_act_skillkit.py +452 -0
- dolphin/lib/skillkits/resource/__init__.py +52 -0
- dolphin/lib/skillkits/resource/models/__init__.py +6 -0
- dolphin/lib/skillkits/resource/models/skill_config.py +109 -0
- dolphin/lib/skillkits/resource/models/skill_meta.py +127 -0
- dolphin/lib/skillkits/resource/resource_skillkit.py +393 -0
- dolphin/lib/skillkits/resource/skill_cache.py +215 -0
- dolphin/lib/skillkits/resource/skill_loader.py +395 -0
- dolphin/lib/skillkits/resource/skill_validator.py +406 -0
- dolphin/lib/skillkits/resource_skillkit.py +11 -0
- dolphin/lib/skillkits/search_skillkit.py +163 -0
- dolphin/lib/skillkits/sql_skillkit.py +274 -0
- dolphin/lib/skillkits/system_skillkit.py +509 -0
- dolphin/lib/skillkits/vm_skillkit.py +65 -0
- dolphin/lib/utils/__init__.py +9 -0
- dolphin/lib/utils/data_process.py +207 -0
- dolphin/lib/utils/handle_progress.py +178 -0
- dolphin/lib/utils/security.py +139 -0
- dolphin/lib/utils/text_retrieval.py +462 -0
- dolphin/lib/vm/__init__.py +11 -0
- dolphin/lib/vm/env_executor.py +895 -0
- dolphin/lib/vm/python_session_manager.py +453 -0
- dolphin/lib/vm/vm.py +610 -0
- dolphin/sdk/__init__.py +60 -0
- dolphin/sdk/agent/__init__.py +12 -0
- dolphin/sdk/agent/agent_factory.py +236 -0
- dolphin/sdk/agent/dolphin_agent.py +1106 -0
- dolphin/sdk/api/__init__.py +4 -0
- dolphin/sdk/runtime/__init__.py +8 -0
- dolphin/sdk/runtime/env.py +363 -0
- dolphin/sdk/skill/__init__.py +10 -0
- dolphin/sdk/skill/global_skills.py +706 -0
- dolphin/sdk/skill/traditional_toolkit.py +260 -0
- kweaver_dolphin-0.1.0.dist-info/METADATA +521 -0
- kweaver_dolphin-0.1.0.dist-info/RECORD +199 -0
- kweaver_dolphin-0.1.0.dist-info/WHEEL +5 -0
- kweaver_dolphin-0.1.0.dist-info/entry_points.txt +27 -0
- kweaver_dolphin-0.1.0.dist-info/licenses/LICENSE.txt +201 -0
- kweaver_dolphin-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"""Configuration settings for context engineering."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, Any, Optional, List
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
import yaml
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from dolphin.core.common.enums import MessageRole
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BuildInBucket(Enum):
|
|
13
|
+
"""Enumeration type, defines context bucket names"""
|
|
14
|
+
|
|
15
|
+
SYSTEM = "_system"
|
|
16
|
+
QUERY = "_query"
|
|
17
|
+
SCRATCHPAD = "_scratchpad"
|
|
18
|
+
HISTORY = "_history"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class ModelConfig:
|
|
23
|
+
"""Model configuration."""
|
|
24
|
+
|
|
25
|
+
# Model Name
|
|
26
|
+
name: str
|
|
27
|
+
|
|
28
|
+
# Model context window limit (token count)
|
|
29
|
+
context_limit: int
|
|
30
|
+
|
|
31
|
+
# Expected output target (number of tokens)
|
|
32
|
+
output_target: int
|
|
33
|
+
|
|
34
|
+
# Reserved space (number of tokens)
|
|
35
|
+
output_headroom: int = 300
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def output_budget(self) -> int:
|
|
39
|
+
"""Total output budget including headroom.
|
|
40
|
+
|
|
41
|
+
Returns the model's total output budget, including expected output.
|
|
42
|
+
This ensures the model has enough space to generate a complete response.
|
|
43
|
+
"""
|
|
44
|
+
return self.output_target
|
|
45
|
+
|
|
46
|
+
def input_budget(self, system_overhead: int = 200) -> int:
|
|
47
|
+
"""Calculate available input budget.
|
|
48
|
+
|
|
49
|
+
Calculate the token budget available for input, which equals the model's context limit minus the output budget.
|
|
50
|
+
Does not include system overhead
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
The number of tokens available for input
|
|
54
|
+
"""
|
|
55
|
+
return self.context_limit - self.output_budget
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class BucketConfig:
|
|
60
|
+
"""Configuration for a context bucket.
|
|
61
|
+
|
|
62
|
+
Context bucket configuration, used to define how different types of messages are handled and their priority.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
# The name of the bucket, used to identify different context types
|
|
66
|
+
name: str
|
|
67
|
+
|
|
68
|
+
# The minimum number of tokens in a bucket to ensure that important context is not overly compressed
|
|
69
|
+
min_tokens: int
|
|
70
|
+
|
|
71
|
+
# The maximum number of tokens for the bucket, limiting the maximum space this type of context can occupy.
|
|
72
|
+
max_tokens: int
|
|
73
|
+
|
|
74
|
+
# The weight of the bucket, used to determine priority when allocating tokens (the higher the weight, the more tokens allocated)
|
|
75
|
+
weight: float
|
|
76
|
+
|
|
77
|
+
# Compression strategy, optional compression methods (such as truncation, summarization, etc.)
|
|
78
|
+
compress: Optional[str] = None
|
|
79
|
+
|
|
80
|
+
# Content scoring, used to evaluate the importance of content (0.0-1.0, with 1.0 indicating the most important)
|
|
81
|
+
content_score: float = 0.5
|
|
82
|
+
|
|
83
|
+
# Message role, specifying the type of message this bucket handles (system/user/tool/assistant)
|
|
84
|
+
message_role: MessageRole = MessageRole.SYSTEM
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass
|
|
88
|
+
class PolicyConfig:
|
|
89
|
+
"""Policy configuration.
|
|
90
|
+
|
|
91
|
+
Policy configuration, used to define the order of context buckets and their deletion priority.
|
|
92
|
+
|
|
93
|
+
Example configuration:
|
|
94
|
+
{
|
|
95
|
+
'drop_order': ['scratchpad', 'history', 'rag'], # Defines the priority order when content needs to be deleted, with decreasing priority from left to right
|
|
96
|
+
'bucket_order': ['system', 'task', 'tools', 'rag', 'history', 'fewshot', 'memory', 'scratchpad'] # Global order
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
bucket_order defines the global placement order of all buckets in the context, arranged sequentially according to the list order.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
# Delete order, defines the deletion priority of each bucket when space needs to be freed, arranged from highest to lowest priority
|
|
103
|
+
drop_order: List[str]
|
|
104
|
+
|
|
105
|
+
# Global order, defining the placement order of all buckets within the context
|
|
106
|
+
bucket_order: List[str]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@dataclass
|
|
110
|
+
class ContextConfig:
|
|
111
|
+
"""Main configuration for context engineering.
|
|
112
|
+
|
|
113
|
+
The main configuration class for context engineering, used to manage model configurations, bucket configurations, and policy configurations.
|
|
114
|
+
This class provides functionality to load configurations from dictionaries, YAML or JSON files, as well as methods to save and validate configurations.
|
|
115
|
+
|
|
116
|
+
Attributes:
|
|
117
|
+
model (ModelConfig): Model configuration information
|
|
118
|
+
buckets (Dict[str, BucketConfig]): Dictionary of bucket configurations, with bucket names as keys and corresponding bucket configurations as values
|
|
119
|
+
policies (Dict[str, PolicyConfig]): Dictionary of policy configurations, with policy names as keys and corresponding policy configurations as values
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
model: ModelConfig
|
|
123
|
+
buckets: Dict[str, BucketConfig]
|
|
124
|
+
policies: Dict[str, PolicyConfig]
|
|
125
|
+
|
|
126
|
+
@classmethod
|
|
127
|
+
def from_dict(cls, config_dict: Dict[str, Any]) -> "ContextConfig":
|
|
128
|
+
"""Create configuration from dictionary.
|
|
129
|
+
|
|
130
|
+
Create a context configuration instance from a dictionary. This method parses model configuration, bucket configuration, and policy configuration,
|
|
131
|
+
and adds a name field to buckets that are not explicitly named.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
config_dict (Dict[str, Any]): Dictionary containing configuration information
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
ContextConfig: The created context configuration instance
|
|
138
|
+
"""
|
|
139
|
+
# Parse model config
|
|
140
|
+
model_data = config_dict.get("model", {})
|
|
141
|
+
model = ModelConfig(
|
|
142
|
+
name=model_data.get("name", "gpt-4"),
|
|
143
|
+
context_limit=model_data.get("context_limit", 8192),
|
|
144
|
+
output_target=model_data.get("output_target", 1200),
|
|
145
|
+
output_headroom=model_data.get("output_headroom", 300),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# Parse bucket configs
|
|
149
|
+
buckets = {}
|
|
150
|
+
buckets_data = config_dict.get("buckets", {})
|
|
151
|
+
for name, bucket_data in buckets_data.items():
|
|
152
|
+
# Add name field to bucket_data if not present
|
|
153
|
+
bucket_data_with_name = bucket_data.copy()
|
|
154
|
+
if "name" not in bucket_data_with_name:
|
|
155
|
+
bucket_data_with_name["name"] = name
|
|
156
|
+
# Process the message_role enumeration
|
|
157
|
+
if "message_role" in bucket_data_with_name:
|
|
158
|
+
message_role_value = bucket_data_with_name["message_role"]
|
|
159
|
+
if isinstance(message_role_value, str):
|
|
160
|
+
try:
|
|
161
|
+
bucket_data_with_name["message_role"] = MessageRole(
|
|
162
|
+
message_role_value
|
|
163
|
+
)
|
|
164
|
+
except ValueError:
|
|
165
|
+
# If the string is not a valid MessageRole member, use the default value SYSTEM
|
|
166
|
+
bucket_data_with_name["message_role"] = MessageRole.SYSTEM
|
|
167
|
+
elif not isinstance(message_role_value, MessageRole):
|
|
168
|
+
bucket_data_with_name["message_role"] = MessageRole.SYSTEM
|
|
169
|
+
buckets[name] = BucketConfig(**bucket_data_with_name)
|
|
170
|
+
|
|
171
|
+
# Parse policy configs
|
|
172
|
+
policies = {}
|
|
173
|
+
policies_data = config_dict.get("policies", {})
|
|
174
|
+
for name, policy_data in policies_data.items():
|
|
175
|
+
policies[name] = PolicyConfig(**policy_data)
|
|
176
|
+
|
|
177
|
+
return cls(
|
|
178
|
+
model=model,
|
|
179
|
+
buckets=buckets,
|
|
180
|
+
policies=policies,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
@classmethod
|
|
184
|
+
def from_yaml(cls, file_path: str) -> "ContextConfig":
|
|
185
|
+
"""Load configuration from YAML file.
|
|
186
|
+
|
|
187
|
+
Load configuration information from a YAML file and create a context configuration instance.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
file_path (str): Path to the YAML configuration file
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
ContextConfig: Context configuration instance created from the YAML file
|
|
194
|
+
"""
|
|
195
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
196
|
+
config_dict = yaml.safe_load(f)
|
|
197
|
+
return cls.from_dict(config_dict)
|
|
198
|
+
|
|
199
|
+
@classmethod
|
|
200
|
+
def from_json(cls, file_path: str) -> "ContextConfig":
|
|
201
|
+
"""Load configuration from JSON file.
|
|
202
|
+
|
|
203
|
+
Load configuration information from a JSON file and create a context configuration instance.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
file_path (str): Path to the JSON configuration file
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
ContextConfig: Context configuration instance created from the JSON file
|
|
210
|
+
"""
|
|
211
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
212
|
+
config_dict = json.load(f)
|
|
213
|
+
return cls.from_dict(config_dict)
|
|
214
|
+
|
|
215
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
216
|
+
"""Convert configuration to dictionary.
|
|
217
|
+
|
|
218
|
+
Convert the current configuration instance to dictionary format for saving to file or transmission.
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
Dict[str, Any]: Dictionary containing all configuration information
|
|
222
|
+
"""
|
|
223
|
+
return {
|
|
224
|
+
"model": {
|
|
225
|
+
"name": self.model.name,
|
|
226
|
+
"context_limit": self.model.context_limit,
|
|
227
|
+
"output_target": self.model.output_target,
|
|
228
|
+
"output_headroom": self.model.output_headroom,
|
|
229
|
+
},
|
|
230
|
+
"buckets": {
|
|
231
|
+
name: {
|
|
232
|
+
"name": bucket.name,
|
|
233
|
+
"min_tokens": bucket.min_tokens,
|
|
234
|
+
"max_tokens": bucket.max_tokens,
|
|
235
|
+
"weight": bucket.weight,
|
|
236
|
+
"compress": bucket.compress,
|
|
237
|
+
"content_score": bucket.content_score,
|
|
238
|
+
"message_role": str(bucket.message_role), # Convert enum to string
|
|
239
|
+
}
|
|
240
|
+
for name, bucket in self.buckets.items()
|
|
241
|
+
},
|
|
242
|
+
"policies": {
|
|
243
|
+
name: {
|
|
244
|
+
"drop_order": policy.drop_order,
|
|
245
|
+
"bucket_order": policy.bucket_order,
|
|
246
|
+
}
|
|
247
|
+
for name, policy in self.policies.items()
|
|
248
|
+
},
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
def save_yaml(self, file_path: str):
|
|
252
|
+
"""Save configuration to YAML file.
|
|
253
|
+
|
|
254
|
+
Save the current configuration to a YAML file.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
file_path (str): Path to the YAML file to save to
|
|
258
|
+
"""
|
|
259
|
+
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
|
260
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
261
|
+
yaml.dump(self.to_dict(), f, default_flow_style=False, allow_unicode=True)
|
|
262
|
+
|
|
263
|
+
def save_json(self, file_path: str):
|
|
264
|
+
"""Save configuration to JSON file.
|
|
265
|
+
|
|
266
|
+
Save the current configuration to a JSON file.
|
|
267
|
+
|
|
268
|
+
Args:
|
|
269
|
+
file_path (str): The path to the JSON file to save.
|
|
270
|
+
"""
|
|
271
|
+
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
|
272
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
273
|
+
json.dump(self.to_dict(), f, indent=2, ensure_ascii=False)
|
|
274
|
+
|
|
275
|
+
def get_bucket_config(self, bucket_name: str) -> Optional[BucketConfig]:
|
|
276
|
+
"""Get configuration for a specific bucket.
|
|
277
|
+
|
|
278
|
+
Get the configuration for a bucket with a specified name.
|
|
279
|
+
|
|
280
|
+
Args:
|
|
281
|
+
bucket_name (str): The name of the bucket
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
Optional[BucketConfig]: The corresponding bucket configuration, or None if it does not exist
|
|
285
|
+
"""
|
|
286
|
+
return self.buckets.get(bucket_name)
|
|
287
|
+
|
|
288
|
+
def get_policy_config(self, policy_name: str) -> Optional[PolicyConfig]:
|
|
289
|
+
"""Get configuration for a specific policy.
|
|
290
|
+
|
|
291
|
+
Get the configuration for a policy with a specific name.
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
policy_name (str): The name of the policy
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
Optional[PolicyConfig]: The corresponding policy configuration, or None if it does not exist
|
|
298
|
+
"""
|
|
299
|
+
return self.policies.get(policy_name)
|
|
300
|
+
|
|
301
|
+
def get_default_policy(self) -> Optional[PolicyConfig]:
|
|
302
|
+
"""Get the default policy configuration.
|
|
303
|
+
|
|
304
|
+
Get the default policy configuration.
|
|
305
|
+
|
|
306
|
+
Returns:
|
|
307
|
+
Optional[PolicyConfig]: The default policy configuration, or None if it does not exist
|
|
308
|
+
"""
|
|
309
|
+
return self.policies.get("default")
|
|
310
|
+
|
|
311
|
+
def validate(self) -> List[str]:
|
|
312
|
+
"""Validate configuration and return list of issues.
|
|
313
|
+
|
|
314
|
+
Validate the validity of the configuration, checking whether the model configuration, bucket configuration, and policy configuration meet requirements.
|
|
315
|
+
|
|
316
|
+
Returns:
|
|
317
|
+
List[str]: List of validation issues; if the configuration is valid, returns an empty list
|
|
318
|
+
"""
|
|
319
|
+
issues = []
|
|
320
|
+
|
|
321
|
+
# Validate model config
|
|
322
|
+
if self.model.context_limit <= 0:
|
|
323
|
+
issues.append("Model context limit must be positive")
|
|
324
|
+
|
|
325
|
+
if self.model.output_budget >= self.model.context_limit:
|
|
326
|
+
issues.append("Output budget exceeds model context limit")
|
|
327
|
+
|
|
328
|
+
# Validate bucket configs
|
|
329
|
+
if not self.buckets:
|
|
330
|
+
issues.append("No buckets configured")
|
|
331
|
+
|
|
332
|
+
for name, bucket in self.buckets.items():
|
|
333
|
+
if bucket.min_tokens > bucket.max_tokens:
|
|
334
|
+
issues.append(f"Bucket '{name}': min_tokens > max_tokens")
|
|
335
|
+
|
|
336
|
+
if bucket.weight < 0:
|
|
337
|
+
issues.append(f"Bucket '{name}': weight must be non-negative")
|
|
338
|
+
|
|
339
|
+
# Validate policy configs
|
|
340
|
+
for name, policy in self.policies.items():
|
|
341
|
+
for bucket_name in policy.drop_order:
|
|
342
|
+
if bucket_name not in self.buckets:
|
|
343
|
+
issues.append(
|
|
344
|
+
f"Policy '{name}': unknown bucket in drop_order: '{bucket_name}'"
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
for bucket_name in policy.bucket_order:
|
|
348
|
+
if bucket_name not in self.buckets:
|
|
349
|
+
issues.append(
|
|
350
|
+
f"Policy '{name}': unknown bucket in bucket_order: '{bucket_name}'"
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
return issues
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def get_default_config() -> ContextConfig:
|
|
357
|
+
"""Get default configuration based on context_engineer.md specifications."""
|
|
358
|
+
return ContextConfig.from_dict(
|
|
359
|
+
{
|
|
360
|
+
"model": {
|
|
361
|
+
"name": "gpt-4",
|
|
362
|
+
"context_limit": 8192,
|
|
363
|
+
"output_target": 1200,
|
|
364
|
+
},
|
|
365
|
+
"buckets": {
|
|
366
|
+
"_system": {
|
|
367
|
+
"name": "_system",
|
|
368
|
+
"min_tokens": 300,
|
|
369
|
+
"max_tokens": 1024,
|
|
370
|
+
"weight": 2.0,
|
|
371
|
+
"message_role": "system",
|
|
372
|
+
},
|
|
373
|
+
"_history": {
|
|
374
|
+
"name": "_history",
|
|
375
|
+
"min_tokens": 120,
|
|
376
|
+
"max_tokens": 1024,
|
|
377
|
+
"weight": 0.8,
|
|
378
|
+
"message_role": "user",
|
|
379
|
+
},
|
|
380
|
+
"_query": {
|
|
381
|
+
"name": "_query",
|
|
382
|
+
"min_tokens": 120,
|
|
383
|
+
"max_tokens": 1024,
|
|
384
|
+
"weight": 0.8,
|
|
385
|
+
"message_role": "user",
|
|
386
|
+
},
|
|
387
|
+
"_scratchpad": {
|
|
388
|
+
"name": "_scratchpad",
|
|
389
|
+
"min_tokens": 0,
|
|
390
|
+
"max_tokens": 2048,
|
|
391
|
+
"weight": 1.2,
|
|
392
|
+
"message_role": "user",
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
"policies": {
|
|
396
|
+
"default": {
|
|
397
|
+
"drop_order": [],
|
|
398
|
+
"bucket_order": ["_system", "_history", "_query", "_scratchpad"],
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
}
|
|
402
|
+
)
|