fix: 统一前后端返回结果样式
This commit is contained in:
parent
61bf235a15
commit
772477607f
|
|
@ -99,10 +99,24 @@ const allItems = computed(() => {
|
|||
const toolId = step.id_ref || step.id
|
||||
const match = items.findLast(it => it.type === 'tool_call' && it.id === toolId)
|
||||
if (match) {
|
||||
const resultContent = step.content || ''
|
||||
match.resultSummary = resultContent.slice(0, 200)
|
||||
match.fullResult = resultContent
|
||||
match.displayResult = resultContent.length > 1024 ? resultContent.slice(0, 1024) + '...' : resultContent
|
||||
let resultContent = step.content || ''
|
||||
let displayContent = resultContent
|
||||
|
||||
// 尝试解析 JSON 并格式化显示
|
||||
try {
|
||||
const parsed = JSON.parse(resultContent)
|
||||
if (parsed.error) {
|
||||
displayContent = `错误: ${parsed.error}`
|
||||
} else {
|
||||
displayContent = JSON.stringify(parsed, null, 2)
|
||||
}
|
||||
} catch (e) {
|
||||
// 不是 JSON,保持原样
|
||||
}
|
||||
|
||||
match.resultSummary = displayContent.slice(0, 200)
|
||||
match.fullResult = displayContent
|
||||
match.displayResult = displayContent.length > 2048 ? displayContent.slice(0, 2048) + '...' : displayContent
|
||||
match.isSuccess = step.success !== false
|
||||
match.loading = false
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def python_execute(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
timeout = arguments.get("timeout", 30)
|
||||
|
||||
if not code:
|
||||
return {"success": False, "error": "Code is required"}
|
||||
return {"error": "Code is required"}
|
||||
|
||||
# Create execution environment
|
||||
namespace = {
|
||||
|
|
@ -64,23 +64,13 @@ def python_execute(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
if not k.startswith("_") and k != "__builtins__"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"output": result,
|
||||
"variables": {k: repr(v) for k, v in result_vars.items()},
|
||||
"error": None
|
||||
}
|
||||
"variables": {k: repr(v) for k, v in result_vars.items()}
|
||||
}
|
||||
except SyntaxError as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Syntax error: {e}"
|
||||
}
|
||||
return {"error": f"Syntax error: {e}"}
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Runtime error: {type(e).__name__}: {str(e)}"
|
||||
}
|
||||
return {"error": f"Runtime error: {type(e).__name__}: {str(e)}"}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -103,19 +93,13 @@ def python_eval(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
expression = arguments.get("expression", "")
|
||||
|
||||
if not expression:
|
||||
return {"success": False, "error": "Expression is required"}
|
||||
return {"error": "Expression is required"}
|
||||
|
||||
try:
|
||||
result = eval(expression)
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"result": repr(result),
|
||||
"type": type(result).__name__
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Evaluation error: {str(e)}"
|
||||
}
|
||||
return {"error": f"Evaluation error: {str(e)}"}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ from luxx.tools.services import SearchService, FetchService
|
|||
}, category="crawler")
|
||||
def web_search(arguments: dict) -> dict:
|
||||
results = SearchService().search(arguments["query"], arguments.get("max_results", 5))
|
||||
return {"success": True, "data": {"query": arguments["query"], "results": results or []}}
|
||||
return {"results": results or []}
|
||||
|
||||
|
||||
@tool(name="web_fetch", description="Fetch content from a webpage.", parameters={
|
||||
|
|
@ -26,9 +26,11 @@ def web_search(arguments: dict) -> dict:
|
|||
}, category="crawler")
|
||||
def web_fetch(arguments: dict) -> dict:
|
||||
if not arguments.get("url"):
|
||||
return {"success": False, "error": "URL is required"}
|
||||
return {"error": "URL is required"}
|
||||
result = FetchService().fetch(arguments["url"], arguments.get("extract_type", "text"))
|
||||
return {"success": "error" not in result, "data": result, "error": result.get("error")}
|
||||
if "error" in result:
|
||||
return {"error": result["error"]}
|
||||
return result
|
||||
|
||||
|
||||
@tool(name="batch_fetch", description="Batch fetch multiple webpages.", parameters={
|
||||
|
|
@ -42,8 +44,8 @@ def web_fetch(arguments: dict) -> dict:
|
|||
def batch_fetch(arguments: dict) -> dict:
|
||||
urls = arguments.get("urls", [])
|
||||
if not urls:
|
||||
return {"success": False, "error": "URLs list is required"}
|
||||
return {"error": "URLs list is required"}
|
||||
if len(urls) > 10:
|
||||
return {"success": False, "error": "Maximum 10 pages allowed"}
|
||||
return {"error": "Maximum 10 pages allowed"}
|
||||
results = FetchService().fetch_batch(urls, arguments.get("extract_type", "text"))
|
||||
return {"success": True, "data": {"results": results, "total": len(results)}}
|
||||
return {"results": results, "total": len(results)}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ def calculate(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
expression = arguments.get("expression", "")
|
||||
|
||||
if not expression:
|
||||
return {"success": False, "error": "Expression is required"}
|
||||
return {"error": "Expression is required"}
|
||||
|
||||
try:
|
||||
# Safe replacement for math functions
|
||||
|
|
@ -52,19 +52,9 @@ def calculate(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
|
||||
result = eval(safe_expr, {"__builtins__": {}, **safe_dict})
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"expression": expression,
|
||||
"result": result,
|
||||
"formatted": f"{result}"
|
||||
}
|
||||
}
|
||||
return {"result": result}
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Calculation error: {str(e)}"
|
||||
}
|
||||
return {"error": f"Calculation error: {str(e)}"}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -93,7 +83,7 @@ def text_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
operation = arguments.get("operation", "")
|
||||
|
||||
if not text:
|
||||
return {"success": False, "error": "Text is required"}
|
||||
return {"error": "Text is required"}
|
||||
|
||||
operations = {
|
||||
"uppercase": text.upper(),
|
||||
|
|
@ -108,16 +98,9 @@ def text_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
result = operations.get(operation)
|
||||
|
||||
if result is None:
|
||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
||||
return {"error": f"Unknown operation: {operation}"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"original": text,
|
||||
"operation": operation,
|
||||
"result": result
|
||||
}
|
||||
}
|
||||
return {"result": result}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -146,7 +129,7 @@ def json_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
operation = arguments.get("operation", "")
|
||||
|
||||
if not data:
|
||||
return {"success": False, "error": "Data is required"}
|
||||
return {"error": "Data is required"}
|
||||
|
||||
try:
|
||||
parsed = json.loads(data)
|
||||
|
|
@ -158,17 +141,11 @@ def json_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
elif operation == "validate":
|
||||
result = "Valid JSON"
|
||||
else:
|
||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
||||
return {"error": f"Unknown operation: {operation}"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"result": result,
|
||||
"operation": operation
|
||||
}
|
||||
}
|
||||
return {"result": result}
|
||||
except json.JSONDecodeError as e:
|
||||
return {"success": False, "error": f"Invalid JSON: {str(e)}"}
|
||||
return {"error": f"Invalid JSON: {str(e)}"}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -199,23 +176,16 @@ def hash_text(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
algorithm = arguments.get("algorithm", "sha256")
|
||||
|
||||
if not text:
|
||||
return {"success": False, "error": "Text is required"}
|
||||
return {"error": "Text is required"}
|
||||
|
||||
try:
|
||||
hash_obj = hashlib.new(algorithm)
|
||||
hash_obj.update(text.encode('utf-8'))
|
||||
hash_value = hash_obj.hexdigest()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"text": text,
|
||||
"algorithm": algorithm,
|
||||
"hash": hash_value
|
||||
}
|
||||
}
|
||||
return {"hash": hash_value}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"Hash error: {str(e)}"}
|
||||
return {"error": f"Hash error: {str(e)}"}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -244,7 +214,7 @@ def url_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
operation = arguments.get("operation", "")
|
||||
|
||||
if not text:
|
||||
return {"success": False, "error": "Text is required"}
|
||||
return {"error": "Text is required"}
|
||||
|
||||
try:
|
||||
if operation == "encode":
|
||||
|
|
@ -252,18 +222,11 @@ def url_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
elif operation == "decode":
|
||||
result = unquote(text)
|
||||
else:
|
||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
||||
return {"error": f"Unknown operation: {operation}"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"original": text,
|
||||
"operation": operation,
|
||||
"result": result
|
||||
}
|
||||
}
|
||||
return {"result": result}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"URL error: {str(e)}"}
|
||||
return {"error": f"URL error: {str(e)}"}
|
||||
|
||||
|
||||
@tool(
|
||||
|
|
@ -292,7 +255,7 @@ def base64_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
operation = arguments.get("operation", "")
|
||||
|
||||
if not text:
|
||||
return {"success": False, "error": "Text is required"}
|
||||
return {"error": "Text is required"}
|
||||
|
||||
try:
|
||||
if operation == "encode":
|
||||
|
|
@ -300,15 +263,8 @@ def base64_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|||
elif operation == "decode":
|
||||
result = base64.b64decode(text.encode()).decode()
|
||||
else:
|
||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
||||
return {"error": f"Unknown operation: {operation}"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"original": text,
|
||||
"operation": operation,
|
||||
"result": result
|
||||
}
|
||||
}
|
||||
return {"result": result}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"Base64 error: {str(e)}"}
|
||||
return {"error": f"Base64 error: {str(e)}"}
|
||||
|
|
|
|||
|
|
@ -72,6 +72,9 @@ class FetchService:
|
|||
with ThreadPoolExecutor(max_workers=max_concurrent) as pool:
|
||||
futures = {pool.submit(self.fetch, url, extract_type): i for i, url in enumerate(urls)}
|
||||
for future in as_completed(futures):
|
||||
try:
|
||||
results[futures[future]] = future.result()
|
||||
except Exception as e:
|
||||
results[futures[future]] = {"error": str(e)}
|
||||
|
||||
return results
|
||||
|
|
|
|||
Loading…
Reference in New Issue