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,176 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import random
|
|
3
|
+
import re
|
|
4
|
+
from functools import lru_cache
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Tool call ID prefix for generating fallback IDs
|
|
8
|
+
TOOL_CALL_ID_PREFIX = "call_"
|
|
9
|
+
|
|
10
|
+
MSG_CONTINUOUS_CONTENT = "如果问题未被解决我们就继续执行\n"
|
|
11
|
+
ANSWER_CONTENT_PREFIX = "<answer>"
|
|
12
|
+
ANSWER_CONTENT_SUFFIX = "</answer>"
|
|
13
|
+
MAX_ANSWER_CONTENT_LENGTH = 12288 # 12k - increased to prevent SKILL.md truncation
|
|
14
|
+
|
|
15
|
+
# Duplicate output detection thresholds
|
|
16
|
+
# - MIN_LENGTH: Start detection early to catch loops quickly (default: 2KB)
|
|
17
|
+
# - COUNT: Set above max legitimate repetitions (default: 50 for 30-50 SVG cards with same CSS)
|
|
18
|
+
# - PATTERN_LENGTH: Length of pattern to check for repetition (default: 50 chars)
|
|
19
|
+
# - All can be overridden via environment variables for flexibility
|
|
20
|
+
MIN_LENGTH_TO_DETECT_DUPLICATE_OUTPUT = int(
|
|
21
|
+
os.environ.get("DOLPHIN_DUPLICATE_MIN_LENGTH", "2048")
|
|
22
|
+
) # 2KB - start checking early
|
|
23
|
+
COUNT_TO_PROVE_DUPLICATE_OUTPUT = int(
|
|
24
|
+
os.environ.get("DOLPHIN_DUPLICATE_COUNT_THRESHOLD", "50")
|
|
25
|
+
) # Allow up to 50 repetitions before triggering
|
|
26
|
+
DUPLICATE_PATTERN_LENGTH = int(
|
|
27
|
+
os.environ.get("DOLPHIN_DUPLICATE_PATTERN_LENGTH", "50")
|
|
28
|
+
) # Length of pattern to check for repetition
|
|
29
|
+
MAX_LOG_LENGTH = 2048
|
|
30
|
+
|
|
31
|
+
KEY_USER_ID = "_user_id"
|
|
32
|
+
KEY_SESSION_ID = "_session_id"
|
|
33
|
+
KEY_MAX_ANSWER_CONTENT_LENGTH = "_max_answer_len"
|
|
34
|
+
|
|
35
|
+
# Executor internal status variables (prefixed with underscore to avoid conflicts)
|
|
36
|
+
KEY_STATUS = "_status"
|
|
37
|
+
KEY_PREVIOUS_STATUS = "_previous_status"
|
|
38
|
+
|
|
39
|
+
MSG_DUPLICATE_SKILL_CALLS = [
|
|
40
|
+
"发现工具重复调用,请检查历史记录,重新思考问题、解决进展及下面计划,我的思考如下:",
|
|
41
|
+
"我发现存在重复调用的情况,重新思考吧,我新的思考如下:",
|
|
42
|
+
"duplicated skillcall, need to change my mind ...",
|
|
43
|
+
"检测到工具调用重复出现,请审视对话历史并调整思路,我的更新思考是:",
|
|
44
|
+
"重复的技能调用被发现,让我们重新评估情况,以下是我的新计划:",
|
|
45
|
+
"Noticed repeated tool invocations, time to rethink the approach. My updated thoughts:",
|
|
46
|
+
"Duplicate skill calls detected, checking history and reformulating. Here's my new reasoning:",
|
|
47
|
+
"重复工具调用警报!请允许我重新考虑问题,我的修订思考如下:",
|
|
48
|
+
"Found looping skill calls, need to break the cycle. New thoughts incoming:",
|
|
49
|
+
"技能调用出现冗余,历史审查中... 我的调整思路是:",
|
|
50
|
+
"Repeated invocations spotted, let's pivot. My fresh perspective:",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_msg_duplicate_skill_call():
|
|
55
|
+
return MSG_DUPLICATE_SKILL_CALLS[
|
|
56
|
+
random.randint(0, len(MSG_DUPLICATE_SKILL_CALLS) - 1)
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def is_msg_duplicate_skill_call(msg: str):
|
|
61
|
+
return any(
|
|
62
|
+
msg in msg_duplicate_skill_call
|
|
63
|
+
for msg_duplicate_skill_call in MSG_DUPLICATE_SKILL_CALLS
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
MSG_DUPLICATE_OUTPUT = [
|
|
68
|
+
"稍等,我发现了重复的生成内容,我要冷静一下重新思考,或许需要借助工具,来吧:",
|
|
69
|
+
"wait, i found duplicated output, i need to calm down and think again, maybe i need to use tools, let's go:",
|
|
70
|
+
"检测到内容重复生成,暂停一下让我重新整理思路,可能要调用工具了:",
|
|
71
|
+
"Oh no, repeating output detected. Taking a breath to rethink, tools might help:",
|
|
72
|
+
"内容出现循环复制,我需要停下来反思一下,工具或许是关键,来试试:",
|
|
73
|
+
"Duplicate content alert! Calming down for a fresh think, let's try some tools:",
|
|
74
|
+
"发现了输出冗余,让我调整心态重新规划,或许借助外部工具:",
|
|
75
|
+
"Spotted duplicated generation, need to reset my thoughts. Tools incoming?",
|
|
76
|
+
"重复内容生成中... 等下,我要冷静分析,可能需要工具支持:",
|
|
77
|
+
"Wait up, output looping. Time to reconsider, maybe with tool assistance:",
|
|
78
|
+
"生成结果有重复迹象,我将重新思考策略,工具调用准备中:",
|
|
79
|
+
"Repeated output found, rethinking now. Perhaps tools will break the loop:",
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def get_msg_duplicate_output():
|
|
84
|
+
return MSG_DUPLICATE_OUTPUT[random.randint(0, len(MSG_DUPLICATE_OUTPUT) - 1)]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@lru_cache(maxsize=128)
|
|
88
|
+
def _compile_duplicate_pattern(pattern: str):
|
|
89
|
+
"""
|
|
90
|
+
Cache compiled regex patterns for duplicate detection.
|
|
91
|
+
|
|
92
|
+
Uses LRU cache to avoid recompiling the same pattern repeatedly,
|
|
93
|
+
providing an additional 10-20% performance improvement on top of
|
|
94
|
+
the existing 6.8x speedup from the regex implementation.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
pattern: The pattern to compile (will be escaped for regex safety)
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Compiled regex pattern object
|
|
101
|
+
"""
|
|
102
|
+
return re.compile(f'(?={re.escape(pattern)})')
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def count_overlapping_occurrences(text: str, pattern: str) -> int:
|
|
106
|
+
"""
|
|
107
|
+
Count overlapping occurrences of pattern in text using regex lookahead.
|
|
108
|
+
|
|
109
|
+
This function is optimized for LLM duplicate output detection, achieving 6.8x average
|
|
110
|
+
speedup over the original loop-based implementation. See PERFORMANCE_OPTIMIZATION_REPORT.md
|
|
111
|
+
for detailed benchmarks and analysis.
|
|
112
|
+
|
|
113
|
+
Key Design Decisions:
|
|
114
|
+
- Uses regex lookahead (?=...) for overlapping matches (required for accurate loop detection)
|
|
115
|
+
- C-based regex vs Python loop reduces interpreter overhead significantly
|
|
116
|
+
- LRU-cached compilation provides additional 10-20% improvement on repeated patterns
|
|
117
|
+
|
|
118
|
+
Why Overlapping Matches:
|
|
119
|
+
- "XXXXX" contains 4 overlapping matches of "XX" (positions 0,1,2,3)
|
|
120
|
+
- Non-overlapping would find only 2, causing false negatives in loop detection
|
|
121
|
+
- Threshold set to allow legitimate repetitions (e.g., 30 SVG cards with same CSS)
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
text: The text to search in
|
|
125
|
+
pattern: The pattern to search for (auto-escaped for regex safety)
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
Number of overlapping occurrences found
|
|
129
|
+
|
|
130
|
+
Example:
|
|
131
|
+
>>> count_overlapping_occurrences("XXXXX", "XX")
|
|
132
|
+
4
|
|
133
|
+
|
|
134
|
+
Performance (typical 5-50KB input):
|
|
135
|
+
- First call: 0.05-0.5ms
|
|
136
|
+
- Cached pattern: 0.04-0.4ms
|
|
137
|
+
- Memory: O(matches), typically <1KB
|
|
138
|
+
"""
|
|
139
|
+
compiled_pattern = _compile_duplicate_pattern(pattern)
|
|
140
|
+
return len(compiled_pattern.findall(text))
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
SEARCH_TIMEOUT = 10 # seconds for search API calls
|
|
144
|
+
|
|
145
|
+
SEARCH_RETRY_COUNT = 2 # number of retries for failed search API calls
|
|
146
|
+
|
|
147
|
+
MAX_SKILL_CALL_TIMES = 100
|
|
148
|
+
|
|
149
|
+
# Compression constants
|
|
150
|
+
MAX_ANSWER_COMPRESSION_LENGTH = 100
|
|
151
|
+
|
|
152
|
+
# Chinese token estimation constants: character to token ratio
|
|
153
|
+
# Different models have different tokenization strategies:
|
|
154
|
+
# - OpenAI series: ~1 char = 2.0 tokens
|
|
155
|
+
# - DeepSeek series: ~1 char = 0.6 tokens
|
|
156
|
+
# - Qwen series: ~1 char = 1.0 tokens
|
|
157
|
+
# - General weighted average: ~1 char = 1.3 tokens (more accurate estimation)
|
|
158
|
+
CHINESE_CHAR_TO_TOKEN_RATIO = 1.3
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def estimate_tokens_from_chars(text: str) -> int:
|
|
162
|
+
"""Estimate the number of tokens in Chinese text"""
|
|
163
|
+
return int(len(text) * CHINESE_CHAR_TO_TOKEN_RATIO)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def estimate_chars_from_tokens(tokens: int) -> int:
|
|
167
|
+
"""Estimate the number of characters corresponding to the token count"""
|
|
168
|
+
return int(tokens / CHINESE_CHAR_TO_TOKEN_RATIO)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
# Dolphin variables output markers
|
|
172
|
+
DOLPHIN_VARIABLES_OUTPUT_START = "=== DOLPHIN_VARIABLES_OUTPUT_START ==="
|
|
173
|
+
DOLPHIN_VARIABLES_OUTPUT_END = "=== DOLPHIN_VARIABLES_OUTPUT_END ==="
|
|
174
|
+
|
|
175
|
+
# Marker: tool outputs containing this token will be persisted into conversation history (minimal pin-to-history)
|
|
176
|
+
PIN_MARKER = "[PIN]"
|