siada-cli 0.0.1__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.
- agent_config.yaml +34 -0
- siada/__init__.py +13 -0
- siada/agent_hub/__init__.py +10 -0
- siada/agent_hub/coder/bug_fix_agent.py +398 -0
- siada/agent_hub/coder/code_gen_agent.py +235 -0
- siada/agent_hub/coder/fe_gen_agent.py +37 -0
- siada/agent_hub/coder/issue_review_agent.py +68 -0
- siada/agent_hub/coder/prompt/__init__.py +0 -0
- siada/agent_hub/coder/prompt/base/__init__.py +0 -0
- siada/agent_hub/coder/prompt/base/capabilities.py +18 -0
- siada/agent_hub/coder/prompt/base/non_interactive.py +14 -0
- siada/agent_hub/coder/prompt/base/prompt_builder.py +33 -0
- siada/agent_hub/coder/prompt/base/rules.py +40 -0
- siada/agent_hub/coder/prompt/base/tool_use.py +14 -0
- siada/agent_hub/coder/prompt/bug_prompt/bug_fix_prompt.py +51 -0
- siada/agent_hub/coder/prompt/bug_prompt/rules.py +53 -0
- siada/agent_hub/coder/prompt/code_gen_prompt.py +45 -0
- siada/agent_hub/coder/prompt/fe_gen_prompt.py +45 -0
- siada/agent_hub/coder/prompt/issue_review_prompt.py +121 -0
- siada/agent_hub/coder/tracing/__init__.py +22 -0
- siada/agent_hub/coder/tracing/logger_tracing_processor.py +458 -0
- siada/agent_hub/hooks/__init__.py +0 -0
- siada/agent_hub/hooks/io_hooks.py +30 -0
- siada/agent_hub/siada_agent.py +214 -0
- siada/config/__init__.py +0 -0
- siada/config/config_loader.py +67 -0
- siada/entrypoint/__init__.py +3 -0
- siada/entrypoint/args_parser/__init__.py +0 -0
- siada/entrypoint/args_parser/args.py +195 -0
- siada/entrypoint/interaction/__init__.py +0 -0
- siada/entrypoint/interaction/base_turn.py +107 -0
- siada/entrypoint/interaction/config.py +30 -0
- siada/entrypoint/interaction/controller.py +122 -0
- siada/entrypoint/interaction/nointeractive_controller.py +27 -0
- siada/entrypoint/interaction/turn/interface.py +116 -0
- siada/entrypoint/interaction/turn/models.py +34 -0
- siada/entrypoint/interaction/turn/run_turn.py +641 -0
- siada/entrypoint/interaction/turn_factory.py +35 -0
- siada/entrypoint/interaction/turn_models.py +28 -0
- siada/entrypoint/siadahub.py +413 -0
- siada/foundation/__init__.py +0 -0
- siada/foundation/code_agent_context.py +48 -0
- siada/foundation/config.py +59 -0
- siada/foundation/constants.py +2 -0
- siada/foundation/context.py +76 -0
- siada/foundation/logging.py +341 -0
- siada/foundation/tools/__init__.py +0 -0
- siada/foundation/tools/get_git_diff.py +105 -0
- siada/io/__init__.py +0 -0
- siada/io/banner.py +148 -0
- siada/io/color_settings.py +207 -0
- siada/io/color_utils.py +16 -0
- siada/io/components/__init__.py +0 -0
- siada/io/components/mdstream.py +204 -0
- siada/io/console_printer.py +68 -0
- siada/io/io.py +661 -0
- siada/io/key_bindings.py +129 -0
- siada/io/notification_command.py +40 -0
- siada/io/test_banner.py +64 -0
- siada/models/__init__.py +0 -0
- siada/models/agent.py +47 -0
- siada/models/coder/__init__.py +0 -0
- siada/models/converter.py +27 -0
- siada/models/model_base_config.py +121 -0
- siada/models/model_run_config.py +153 -0
- siada/provider/__init__.py +0 -0
- siada/provider/client_factory.py +112 -0
- siada/provider/llm_client.py +11 -0
- siada/provider/openrouter/__init__.py +0 -0
- siada/provider/openrouter/coverter.py +17 -0
- siada/provider/openrouter/openroute_provider.py +33 -0
- siada/provider/provider_factory.py +53 -0
- siada/queries/tree-sitter-language-pack/README.md +7 -0
- siada/queries/tree-sitter-language-pack/arduino-tags.scm +5 -0
- siada/queries/tree-sitter-language-pack/c-tags.scm +9 -0
- siada/queries/tree-sitter-language-pack/chatito-tags.scm +16 -0
- siada/queries/tree-sitter-language-pack/commonlisp-tags.scm +122 -0
- siada/queries/tree-sitter-language-pack/cpp-tags.scm +15 -0
- siada/queries/tree-sitter-language-pack/csharp-tags.scm +26 -0
- siada/queries/tree-sitter-language-pack/d-tags.scm +26 -0
- siada/queries/tree-sitter-language-pack/dart-tags.scm +92 -0
- siada/queries/tree-sitter-language-pack/elisp-tags.scm +5 -0
- siada/queries/tree-sitter-language-pack/elixir-tags.scm +54 -0
- siada/queries/tree-sitter-language-pack/elm-tags.scm +19 -0
- siada/queries/tree-sitter-language-pack/gleam-tags.scm +41 -0
- siada/queries/tree-sitter-language-pack/go-tags.scm +42 -0
- siada/queries/tree-sitter-language-pack/java-tags.scm +20 -0
- siada/queries/tree-sitter-language-pack/javascript-tags.scm +88 -0
- siada/queries/tree-sitter-language-pack/lua-tags.scm +34 -0
- siada/queries/tree-sitter-language-pack/ocaml-tags.scm +115 -0
- siada/queries/tree-sitter-language-pack/ocaml_interface-tags.scm +98 -0
- siada/queries/tree-sitter-language-pack/pony-tags.scm +39 -0
- siada/queries/tree-sitter-language-pack/properties-tags.scm +5 -0
- siada/queries/tree-sitter-language-pack/python-tags.scm +14 -0
- siada/queries/tree-sitter-language-pack/r-tags.scm +21 -0
- siada/queries/tree-sitter-language-pack/racket-tags.scm +12 -0
- siada/queries/tree-sitter-language-pack/ruby-tags.scm +64 -0
- siada/queries/tree-sitter-language-pack/rust-tags.scm +60 -0
- siada/queries/tree-sitter-language-pack/solidity-tags.scm +43 -0
- siada/queries/tree-sitter-language-pack/swift-tags.scm +51 -0
- siada/queries/tree-sitter-language-pack/udev-tags.scm +20 -0
- siada/queries/tree-sitter-languages/README.md +23 -0
- siada/queries/tree-sitter-languages/c-tags.scm +9 -0
- siada/queries/tree-sitter-languages/c_sharp-tags.scm +46 -0
- siada/queries/tree-sitter-languages/cpp-tags.scm +15 -0
- siada/queries/tree-sitter-languages/dart-tags.scm +91 -0
- siada/queries/tree-sitter-languages/elisp-tags.scm +8 -0
- siada/queries/tree-sitter-languages/elixir-tags.scm +54 -0
- siada/queries/tree-sitter-languages/elm-tags.scm +19 -0
- siada/queries/tree-sitter-languages/go-tags.scm +30 -0
- siada/queries/tree-sitter-languages/hcl-tags.scm +77 -0
- siada/queries/tree-sitter-languages/java-tags.scm +20 -0
- siada/queries/tree-sitter-languages/javascript-tags.scm +88 -0
- siada/queries/tree-sitter-languages/kotlin-tags.scm +27 -0
- siada/queries/tree-sitter-languages/ocaml-tags.scm +115 -0
- siada/queries/tree-sitter-languages/ocaml_interface-tags.scm +98 -0
- siada/queries/tree-sitter-languages/php-tags.scm +26 -0
- siada/queries/tree-sitter-languages/python-tags.scm +12 -0
- siada/queries/tree-sitter-languages/ql-tags.scm +26 -0
- siada/queries/tree-sitter-languages/ruby-tags.scm +64 -0
- siada/queries/tree-sitter-languages/rust-tags.scm +60 -0
- siada/queries/tree-sitter-languages/scala-tags.scm +65 -0
- siada/queries/tree-sitter-languages/typescript-tags.scm +41 -0
- siada/services/__init__.py +30 -0
- siada/services/at_file_recommendation_design_document.md +1213 -0
- siada/services/code_context_manager.py +136 -0
- siada/services/enhanced_fix_result_check.py +546 -0
- siada/services/execution_trace_collector.py +207 -0
- siada/services/file_recommendation/__init__.py +136 -0
- siada/services/file_recommendation/core/__init__.py +17 -0
- siada/services/file_recommendation/core/completion_engine.py +351 -0
- siada/services/file_recommendation/core/config.py +62 -0
- siada/services/file_recommendation/core/file_discovery.py +353 -0
- siada/services/file_recommendation/core/suggestion.py +110 -0
- siada/services/file_recommendation/utils/__init__.py +22 -0
- siada/services/file_recommendation/utils/path_utils.py +138 -0
- siada/services/file_recommendation/utils/text_utils.py +253 -0
- siada/services/fix_result_check.py +377 -0
- siada/services/handle_at_command/README.md +247 -0
- siada/services/handle_at_command/__init__.py +115 -0
- siada/services/handle_at_command/exceptions.py +46 -0
- siada/services/handle_at_command/models.py +92 -0
- siada/services/handle_at_command/parser.py +189 -0
- siada/services/handle_at_command/processor.py +321 -0
- siada/services/handle_at_command/resolver.py +318 -0
- siada/services/handle_at_command/utils.py +345 -0
- siada/services/model_info_service.py +103 -0
- siada/services/siada_runner.py +170 -0
- siada/session/__init__.py +29 -0
- siada/session/session_manager.py +66 -0
- siada/session/session_models.py +43 -0
- siada/support/__init__.py +0 -0
- siada/support/completer.py +204 -0
- siada/support/editor.py +146 -0
- siada/support/envprocessor.py +54 -0
- siada/support/repo.py +51 -0
- siada/support/slash_commands.py +410 -0
- siada/support/spinner.py +320 -0
- siada/tools/__init__.py +0 -0
- siada/tools/ast/README.md +345 -0
- siada/tools/ast/ast_tool.py +341 -0
- siada/tools/ast/models.py +11 -0
- siada/tools/coder/__init__.py +0 -0
- siada/tools/coder/ask_followup_question.py +70 -0
- siada/tools/coder/cmd_runner.py +132 -0
- siada/tools/coder/file_operator.py +284 -0
- siada/tools/coder/file_search/README.md +223 -0
- siada/tools/coder/file_search/__init__.py +10 -0
- siada/tools/coder/file_search/bin/rg-linux-arm64 +0 -0
- siada/tools/coder/file_search/bin/rg-linux-x64 +0 -0
- siada/tools/coder/file_search/bin/rg-macos-arm64 +0 -0
- siada/tools/coder/file_search/bin/rg-macos-x64 +0 -0
- siada/tools/coder/file_search/bin/rg.exe +0 -0
- siada/tools/coder/file_search/search.py +437 -0
- siada/tools/coder/file_search/setup_package.py +150 -0
- siada/tools/coder/file_search/test_search.py +142 -0
- siada/tools/coder/files.py +147 -0
- siada/tools/coder/fix_attempt_completion.py +58 -0
- siada/tools/coder/issue_review_completion.py +29 -0
- siada/tools/coder/observation/__init__.py +0 -0
- siada/tools/coder/observation/error.py +22 -0
- siada/tools/coder/observation/file_observation.py +216 -0
- siada/tools/coder/observation/observation.py +71 -0
- siada/tools/coder/repo_map/__init__.py +157 -0
- siada/tools/coder/repo_map/dump.py +29 -0
- siada/tools/coder/repo_map/io.py +226 -0
- siada/tools/coder/repo_map/repo_map.py +881 -0
- siada/tools/coder/repo_map/special.py +203 -0
- siada/tools/coder/repo_map/token_counter.py +154 -0
- siada/tools/coder/repo_map/waiting.py +221 -0
- siada/tools/coder/reproduce_completion.py +39 -0
- siada/tools/coder/run_cmd.py +60 -0
- siada/tools/coder/test_completion.py +25 -0
- siada/tools/coder/tool_docs.py +44 -0
- siada/tools/compression_tool.py +200 -0
- siada/tools/read_many_files/README.md +220 -0
- siada/tools/read_many_files/__init__.py +25 -0
- siada/tools/read_many_files/file_processor.py +486 -0
- siada/tools/read_many_files/filters.py +326 -0
- siada/tools/read_many_files/formatters.py +204 -0
- siada/tools/read_many_files/models.py +189 -0
- siada/tools/read_many_files_tool.py +233 -0
- siada/tools/tool_call_format/__init__.py +55 -0
- siada/tools/tool_call_format/file_to_language.py +231 -0
- siada/tools/tool_call_format/formatter_factory.py +80 -0
- siada/tools/tool_call_format/formatters.py +282 -0
- siada/tools/tool_call_format/tool_call_formatter.py +54 -0
- siada/tools/web/__init__.py +0 -0
- siada/tools/web/web_crawl.py +208 -0
- siada/utils.py +194 -0
- siada_cli-0.0.1.dist-info/LICENSE +201 -0
- siada_cli-0.0.1.dist-info/METADATA +457 -0
- siada_cli-0.0.1.dist-info/RECORD +215 -0
- siada_cli-0.0.1.dist-info/WHEEL +4 -0
- siada_cli-0.0.1.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Web content crawling tool
|
|
3
|
+
|
|
4
|
+
Uses trafilatura library to fetch web content, supports multiple output formats
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
WEB_CRAWL_DOCS = """Web content crawling tool for fetching and extracting content from web pages
|
|
8
|
+
* Uses trafilatura library to intelligently extract clean content from web pages
|
|
9
|
+
* Supports multiple output formats: text, markdown, JSON, and raw HTML
|
|
10
|
+
* Handles various web page types including articles, blogs, documentation, and news sites
|
|
11
|
+
* Automatically validates URLs and provides detailed error messages for troubleshooting
|
|
12
|
+
* Returns structured observation objects with success status and extracted content
|
|
13
|
+
|
|
14
|
+
SUPPORTED OUTPUT FORMATS:
|
|
15
|
+
- `text`: Clean plain text content without HTML tags (default)
|
|
16
|
+
- `markdown`: Content formatted as Markdown with preserved structure
|
|
17
|
+
- `json`: Structured JSON format with metadata including title, author, date, etc.
|
|
18
|
+
- `html`: Raw HTML content as downloaded from the source
|
|
19
|
+
|
|
20
|
+
USAGE GUIDELINES:
|
|
21
|
+
|
|
22
|
+
Before using this tool:
|
|
23
|
+
1. Ensure the target URL is accessible and contains the content you need
|
|
24
|
+
2. Choose the appropriate output format based on your use case:
|
|
25
|
+
- Use `text` for simple content analysis or when you need clean readable text
|
|
26
|
+
- Use `markdown` when you need to preserve document structure and formatting
|
|
27
|
+
- Use `json` when you need metadata along with content (title, publication date, etc.)
|
|
28
|
+
- Use `html` when you need the raw source code for further processing
|
|
29
|
+
|
|
30
|
+
When using this tool:
|
|
31
|
+
1. Always provide a complete, valid URL including the protocol (http:// or https://)
|
|
32
|
+
2. The tool will automatically validate the URL format before attempting to fetch content
|
|
33
|
+
3. If the request fails, check the error message for specific details about the failure
|
|
34
|
+
4. Large pages may take some time to process, especially when extracting to JSON format
|
|
35
|
+
|
|
36
|
+
COMMON USE CASES:
|
|
37
|
+
- Research: Extract article content from news sites, blogs, or documentation
|
|
38
|
+
- Content Analysis: Get clean text for sentiment analysis or keyword extraction
|
|
39
|
+
- Documentation: Convert web pages to markdown for documentation purposes
|
|
40
|
+
- Data Collection: Extract structured data from web pages in JSON format
|
|
41
|
+
- Web Scraping: Get raw HTML for custom parsing and data extraction
|
|
42
|
+
|
|
43
|
+
ERROR HANDLING:
|
|
44
|
+
The tool provides detailed error messages for common issues:
|
|
45
|
+
- Invalid URL format: Check that the URL includes protocol and domain
|
|
46
|
+
- Network request failed: Check internet connection and URL accessibility
|
|
47
|
+
- Unable to download content: The target page may be protected or unavailable
|
|
48
|
+
- Unable to extract content: The page structure may not be supported by trafilatura
|
|
49
|
+
- JSON parsing failed: The extracted content could not be formatted as valid JSON
|
|
50
|
+
|
|
51
|
+
EXAMPLES:
|
|
52
|
+
- web_crawl(url="https://example.com/article", format="text")
|
|
53
|
+
- web_crawl(url="https://docs.example.com/guide", format="markdown")
|
|
54
|
+
- web_crawl(url="https://news.example.com/story", format="json")
|
|
55
|
+
- web_crawl(url="https://example.com/page", format="html")
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
context: Runtime context wrapper for the agent
|
|
59
|
+
url: Complete URL of the webpage to crawl (must include protocol)
|
|
60
|
+
format: Output format - "text", "markdown", "json", or "html" (default: "text")
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
WebCrawlObservation: Object containing the crawling results with success status and content
|
|
64
|
+
"""
|
|
65
|
+
import json
|
|
66
|
+
import logging
|
|
67
|
+
from typing import Literal
|
|
68
|
+
import trafilatura
|
|
69
|
+
import requests
|
|
70
|
+
from urllib.parse import urlparse
|
|
71
|
+
from agents import function_tool, RunContextWrapper
|
|
72
|
+
|
|
73
|
+
from siada.foundation.code_agent_context import CodeAgentContext
|
|
74
|
+
from siada.tools.coder.observation.observation import FunctionCallResult
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class WebCrawlObservation(FunctionCallResult):
|
|
78
|
+
"""Web crawling result observation"""
|
|
79
|
+
|
|
80
|
+
def __init__(self, url: str, content: str, format: str, success: bool = True, error: str = None):
|
|
81
|
+
self.url = url
|
|
82
|
+
self.content = content
|
|
83
|
+
self.format = format
|
|
84
|
+
self.success = success
|
|
85
|
+
self.error = error
|
|
86
|
+
|
|
87
|
+
def __str__(self) -> str:
|
|
88
|
+
if not self.success:
|
|
89
|
+
return f"Web crawling failed ({self.url}): {self.error}"
|
|
90
|
+
|
|
91
|
+
content_preview = self.content[:200] + "..." if len(self.content) > 200 else self.content
|
|
92
|
+
return f"Web crawling successful ({self.url}, format: {self.format}):\n{content_preview}"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@function_tool(name_override="web_crawl",
|
|
96
|
+
description_override="Complete the bug fix task and mark it as finished. This tool MUST be called to properly complete any bug fix task. Failure to call this tool means the bug fix task is incomplete and unacceptable."
|
|
97
|
+
)
|
|
98
|
+
def web_crawl(
|
|
99
|
+
context: RunContextWrapper[CodeAgentContext],
|
|
100
|
+
url: str,
|
|
101
|
+
format: Literal["markdown", "text", "json", "html"] = "text"
|
|
102
|
+
) -> WebCrawlObservation:
|
|
103
|
+
"""
|
|
104
|
+
Crawl web content and return content in specified format
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
context: Runtime context wrapper
|
|
108
|
+
url: URL of the webpage to crawl
|
|
109
|
+
format: Output format, supports "markdown", "text", "json", "html"
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
WebCrawlObservation: Observation object containing crawling results
|
|
113
|
+
"""
|
|
114
|
+
try:
|
|
115
|
+
# Validate URL format
|
|
116
|
+
parsed_url = urlparse(url)
|
|
117
|
+
if not parsed_url.scheme or not parsed_url.netloc:
|
|
118
|
+
return WebCrawlObservation(
|
|
119
|
+
url=url,
|
|
120
|
+
content="",
|
|
121
|
+
format=format,
|
|
122
|
+
success=False,
|
|
123
|
+
error=f"Invalid URL format: {url}"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
# Download web content
|
|
127
|
+
downloaded = trafilatura.fetch_url(url)
|
|
128
|
+
if not downloaded:
|
|
129
|
+
return WebCrawlObservation(
|
|
130
|
+
url=url,
|
|
131
|
+
content="",
|
|
132
|
+
format=format,
|
|
133
|
+
success=False,
|
|
134
|
+
error=f"Unable to download web content: {url}"
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
if format == "html":
|
|
138
|
+
extracted = downloaded
|
|
139
|
+
elif format == "text":
|
|
140
|
+
extracted = trafilatura.extract(downloaded, output_format='txt')
|
|
141
|
+
elif format == "markdown":
|
|
142
|
+
extracted = trafilatura.extract(downloaded, output_format='markdown')
|
|
143
|
+
elif format == "json":
|
|
144
|
+
extracted_data = trafilatura.extract(
|
|
145
|
+
downloaded,
|
|
146
|
+
output_format='json',
|
|
147
|
+
include_comments=False,
|
|
148
|
+
include_tables=True,
|
|
149
|
+
include_images=True
|
|
150
|
+
)
|
|
151
|
+
if extracted_data:
|
|
152
|
+
# Parse JSON and reformat
|
|
153
|
+
json_data = json.loads(extracted_data)
|
|
154
|
+
extracted = json.dumps(json_data, ensure_ascii=False, indent=2)
|
|
155
|
+
else:
|
|
156
|
+
extracted = None
|
|
157
|
+
else:
|
|
158
|
+
return WebCrawlObservation(
|
|
159
|
+
url=url,
|
|
160
|
+
content="",
|
|
161
|
+
format=format,
|
|
162
|
+
success=False,
|
|
163
|
+
error=f"Unsupported format: {format}"
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
if not extracted:
|
|
167
|
+
return WebCrawlObservation(
|
|
168
|
+
url=url,
|
|
169
|
+
content="",
|
|
170
|
+
format=format,
|
|
171
|
+
success=False,
|
|
172
|
+
error=f"Unable to extract content from webpage: {url}"
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return WebCrawlObservation(
|
|
176
|
+
url=url,
|
|
177
|
+
content=extracted,
|
|
178
|
+
format=format,
|
|
179
|
+
success=True
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
except requests.exceptions.RequestException as e:
|
|
183
|
+
error_msg = f"Network request failed: {str(e)}"
|
|
184
|
+
return WebCrawlObservation(
|
|
185
|
+
url=url,
|
|
186
|
+
content="",
|
|
187
|
+
format=format,
|
|
188
|
+
success=False,
|
|
189
|
+
error=error_msg
|
|
190
|
+
)
|
|
191
|
+
except json.JSONDecodeError as e:
|
|
192
|
+
error_msg = f"JSON parsing failed: {str(e)}"
|
|
193
|
+
return WebCrawlObservation(
|
|
194
|
+
url=url,
|
|
195
|
+
content="",
|
|
196
|
+
format=format,
|
|
197
|
+
success=False,
|
|
198
|
+
error=error_msg
|
|
199
|
+
)
|
|
200
|
+
except Exception as e:
|
|
201
|
+
error_msg = f"Web crawling failed: {str(e)}"
|
|
202
|
+
return WebCrawlObservation(
|
|
203
|
+
url=url,
|
|
204
|
+
content="",
|
|
205
|
+
format=format,
|
|
206
|
+
success=False,
|
|
207
|
+
error=error_msg
|
|
208
|
+
)
|
siada/utils.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""
|
|
2
|
+
工具函数模块
|
|
3
|
+
|
|
4
|
+
提供项目中通用的工具函数
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import traceback
|
|
8
|
+
from inspect import stack
|
|
9
|
+
from textwrap import indent
|
|
10
|
+
from typing import Any, Dict, Union, List
|
|
11
|
+
|
|
12
|
+
from siada.foundation.logging import logger
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class JsonUtils:
|
|
16
|
+
"""JSON相关工具类"""
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def format_json(json_data: Union[str, Dict[str, Any]]) -> str:
|
|
20
|
+
"""
|
|
21
|
+
美化JSON数据
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
json_data: JSON字符串或字典对象
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
格式化后的JSON字符串
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
# 如果是字符串,先解析为对象
|
|
31
|
+
if isinstance(json_data, str):
|
|
32
|
+
data_obj = json.loads(json_data)
|
|
33
|
+
else:
|
|
34
|
+
data_obj = json_data
|
|
35
|
+
|
|
36
|
+
# 格式化输出
|
|
37
|
+
return json.dumps(data_obj, indent=2, ensure_ascii=False)
|
|
38
|
+
except (json.JSONDecodeError, TypeError):
|
|
39
|
+
# 解析失败则返回原始内容
|
|
40
|
+
return str(json_data)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class SSEUtils:
|
|
44
|
+
"""Server-Sent Events (SSE)相关工具类"""
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
async def format_sse(data):
|
|
48
|
+
"""
|
|
49
|
+
将数据格式化为SSE格式
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
data: 要格式化的数据
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
SSE格式的数据
|
|
56
|
+
"""
|
|
57
|
+
if isinstance(data, dict):
|
|
58
|
+
event = data.get("event", "message")
|
|
59
|
+
# json.dumps已经会将换行符转为\n转义序列
|
|
60
|
+
json_data = json.dumps(data.get("data", {}), ensure_ascii=False)
|
|
61
|
+
return f"event: {event}\ndata: {json_data}\n\n"
|
|
62
|
+
|
|
63
|
+
json_str = json.dumps(data, ensure_ascii=False)
|
|
64
|
+
return f"data: {json_str}\n\n"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class AgentLogger:
|
|
68
|
+
"""Agent日志格式化助手类"""
|
|
69
|
+
|
|
70
|
+
@staticmethod
|
|
71
|
+
def format_start(agent_name: str) -> str:
|
|
72
|
+
"""格式化Agent启动日志"""
|
|
73
|
+
return f"Agent {agent_name} start running"
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def format_finished(agent_name: str) -> str:
|
|
77
|
+
"""格式化Agent完成日志"""
|
|
78
|
+
return f"Agent {agent_name} finished"
|
|
79
|
+
|
|
80
|
+
@staticmethod
|
|
81
|
+
def format_tool_call(tool_name: str, tool_args: Any) -> str:
|
|
82
|
+
"""格式化工具调用日志"""
|
|
83
|
+
formatted_args = JsonUtils.format_json(tool_args)
|
|
84
|
+
return f"**Tool Call**\nToolName:{tool_name}\nArguments: {formatted_args}"
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def format_tool_output(output: str) -> str:
|
|
88
|
+
"""格式化工具输出日志"""
|
|
89
|
+
return f"**Tool Call Output**\n{output}"
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
def format_action(thought: str, action: str) -> str:
|
|
93
|
+
"""格式化行动日志"""
|
|
94
|
+
if thought:
|
|
95
|
+
return f"**Thought**\n{thought} \n{action}"
|
|
96
|
+
return action
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def format_final_output(output: str) -> str:
|
|
100
|
+
"""格式化最终输出日志"""
|
|
101
|
+
return f"{output}"
|
|
102
|
+
|
|
103
|
+
@staticmethod
|
|
104
|
+
def log_user_input(input_text: str) -> None:
|
|
105
|
+
"""记录用户输入"""
|
|
106
|
+
logger.info(input_text, extra={'msg_type': 'USER_ACTION'})
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def log_tool_output(output: str) -> None:
|
|
110
|
+
"""记录工具输出"""
|
|
111
|
+
logger.info(output, extra={'msg_type': 'OBSERVATION'})
|
|
112
|
+
|
|
113
|
+
@staticmethod
|
|
114
|
+
def log_action(message: str) -> None:
|
|
115
|
+
"""记录行动"""
|
|
116
|
+
logger.info(message, extra={'msg_type': 'ACTION'})
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def log_final_output(message: str) -> None:
|
|
120
|
+
"""记录最终输出"""
|
|
121
|
+
logger.info(message.lstrip(), extra={"msg_type": "OUTPUT"})
|
|
122
|
+
|
|
123
|
+
@staticmethod
|
|
124
|
+
def format_observation(thought: str) -> str:
|
|
125
|
+
"""格式化观察日志"""
|
|
126
|
+
return f"**Observation**\n{thought}"
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def format_msg_action(thought: str) -> str:
|
|
130
|
+
"""格式化观察日志"""
|
|
131
|
+
return f"**Message**\n{thought}"
|
|
132
|
+
|
|
133
|
+
@staticmethod
|
|
134
|
+
def log_observation(message: str) -> None:
|
|
135
|
+
"""记录观察内容"""
|
|
136
|
+
logger.info(message, extra={'msg_type': 'MESSAGE'})
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class DebugUtils:
|
|
140
|
+
"Debug Utils"
|
|
141
|
+
|
|
142
|
+
@staticmethod
|
|
143
|
+
def _cvt(s: Any):
|
|
144
|
+
if isinstance(s, str):
|
|
145
|
+
return s
|
|
146
|
+
try:
|
|
147
|
+
return json.dumps(s, indent=4)
|
|
148
|
+
except TypeError:
|
|
149
|
+
return str(s)
|
|
150
|
+
|
|
151
|
+
def dump(*args):
|
|
152
|
+
stack = traceback.extract_stack()
|
|
153
|
+
vars = stack[-2][-3]
|
|
154
|
+
|
|
155
|
+
vars = "(".join(vars.split("(")[1:])
|
|
156
|
+
vars = ")".join(vars.split(")")[:-1])
|
|
157
|
+
|
|
158
|
+
args = [DebugUtils._cvt(v) for v in args]
|
|
159
|
+
has_newline = sum(1 for v in vars if "\n" in v)
|
|
160
|
+
if has_newline:
|
|
161
|
+
print("%s:" % vars)
|
|
162
|
+
print(",".join(args))
|
|
163
|
+
else:
|
|
164
|
+
print("%s:" % vars, ", ".join(args))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class ImageUtils:
|
|
168
|
+
"""Image Utils"""
|
|
169
|
+
IMAGE_EXTENSIONS = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp", ".pdf"]
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def is_image_file(filename: str) -> bool:
|
|
173
|
+
"""Check if a file is an image file"""
|
|
174
|
+
file_name = str(filename) # Convert file_name to string
|
|
175
|
+
return any(file_name.endswith(ext) for ext in ImageUtils.IMAGE_EXTENSIONS)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class SettingsUtils:
|
|
179
|
+
|
|
180
|
+
@staticmethod
|
|
181
|
+
def format_settings(parser, args):
|
|
182
|
+
"""Format settings"""
|
|
183
|
+
show = parser.format_values()
|
|
184
|
+
# clean up the headings for consistency w/ new lines
|
|
185
|
+
heading_env = "Environment Variables:"
|
|
186
|
+
heading_defaults = "Defaults:"
|
|
187
|
+
if heading_env in show:
|
|
188
|
+
show = show.replace(heading_env, "\n" + heading_env)
|
|
189
|
+
show = show.replace(heading_defaults, "\n" + heading_defaults)
|
|
190
|
+
show += "\n"
|
|
191
|
+
show += "Option settings:\n"
|
|
192
|
+
for arg, val in sorted(vars(args).items()):
|
|
193
|
+
show += f" - {arg}: {val}\n" # noqa: E221
|
|
194
|
+
return show
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) 2025 Li Auto Inc. and its affiliates
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|