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 toolId = step.id_ref || step.id
|
||||||
const match = items.findLast(it => it.type === 'tool_call' && it.id === toolId)
|
const match = items.findLast(it => it.type === 'tool_call' && it.id === toolId)
|
||||||
if (match) {
|
if (match) {
|
||||||
const resultContent = step.content || ''
|
let resultContent = step.content || ''
|
||||||
match.resultSummary = resultContent.slice(0, 200)
|
let displayContent = resultContent
|
||||||
match.fullResult = resultContent
|
|
||||||
match.displayResult = resultContent.length > 1024 ? resultContent.slice(0, 1024) + '...' : 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.isSuccess = step.success !== false
|
||||||
match.loading = false
|
match.loading = false
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ def python_execute(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
timeout = arguments.get("timeout", 30)
|
timeout = arguments.get("timeout", 30)
|
||||||
|
|
||||||
if not code:
|
if not code:
|
||||||
return {"success": False, "error": "Code is required"}
|
return {"error": "Code is required"}
|
||||||
|
|
||||||
# Create execution environment
|
# Create execution environment
|
||||||
namespace = {
|
namespace = {
|
||||||
|
|
@ -64,23 +64,13 @@ def python_execute(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
if not k.startswith("_") and k != "__builtins__"}
|
if not k.startswith("_") and k != "__builtins__"}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"output": result,
|
"output": result,
|
||||||
"variables": {k: repr(v) for k, v in result_vars.items()},
|
"variables": {k: repr(v) for k, v in result_vars.items()}
|
||||||
"error": None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
except SyntaxError as e:
|
except SyntaxError as e:
|
||||||
return {
|
return {"error": f"Syntax error: {e}"}
|
||||||
"success": False,
|
|
||||||
"error": f"Syntax error: {e}"
|
|
||||||
}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {"error": f"Runtime error: {type(e).__name__}: {str(e)}"}
|
||||||
"success": False,
|
|
||||||
"error": f"Runtime error: {type(e).__name__}: {str(e)}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -103,19 +93,13 @@ def python_eval(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
expression = arguments.get("expression", "")
|
expression = arguments.get("expression", "")
|
||||||
|
|
||||||
if not expression:
|
if not expression:
|
||||||
return {"success": False, "error": "Expression is required"}
|
return {"error": "Expression is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = eval(expression)
|
result = eval(expression)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"result": repr(result),
|
"result": repr(result),
|
||||||
"type": type(result).__name__
|
"type": type(result).__name__
|
||||||
}
|
}
|
||||||
}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {"error": f"Evaluation error: {str(e)}"}
|
||||||
"success": False,
|
|
||||||
"error": f"Evaluation error: {str(e)}"
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ from luxx.tools.services import SearchService, FetchService
|
||||||
}, category="crawler")
|
}, category="crawler")
|
||||||
def web_search(arguments: dict) -> dict:
|
def web_search(arguments: dict) -> dict:
|
||||||
results = SearchService().search(arguments["query"], arguments.get("max_results", 5))
|
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={
|
@tool(name="web_fetch", description="Fetch content from a webpage.", parameters={
|
||||||
|
|
@ -26,9 +26,11 @@ def web_search(arguments: dict) -> dict:
|
||||||
}, category="crawler")
|
}, category="crawler")
|
||||||
def web_fetch(arguments: dict) -> dict:
|
def web_fetch(arguments: dict) -> dict:
|
||||||
if not arguments.get("url"):
|
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"))
|
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={
|
@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:
|
def batch_fetch(arguments: dict) -> dict:
|
||||||
urls = arguments.get("urls", [])
|
urls = arguments.get("urls", [])
|
||||||
if not urls:
|
if not urls:
|
||||||
return {"success": False, "error": "URLs list is required"}
|
return {"error": "URLs list is required"}
|
||||||
if len(urls) > 10:
|
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"))
|
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", "")
|
expression = arguments.get("expression", "")
|
||||||
|
|
||||||
if not expression:
|
if not expression:
|
||||||
return {"success": False, "error": "Expression is required"}
|
return {"error": "Expression is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Safe replacement for math functions
|
# 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})
|
result = eval(safe_expr, {"__builtins__": {}, **safe_dict})
|
||||||
|
|
||||||
return {
|
return {"result": result}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"expression": expression,
|
|
||||||
"result": result,
|
|
||||||
"formatted": f"{result}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {"error": f"Calculation error: {str(e)}"}
|
||||||
"success": False,
|
|
||||||
"error": f"Calculation error: {str(e)}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -93,7 +83,7 @@ def text_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
operation = arguments.get("operation", "")
|
operation = arguments.get("operation", "")
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
return {"success": False, "error": "Text is required"}
|
return {"error": "Text is required"}
|
||||||
|
|
||||||
operations = {
|
operations = {
|
||||||
"uppercase": text.upper(),
|
"uppercase": text.upper(),
|
||||||
|
|
@ -108,16 +98,9 @@ def text_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
result = operations.get(operation)
|
result = operations.get(operation)
|
||||||
|
|
||||||
if result is None:
|
if result is None:
|
||||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
return {"error": f"Unknown operation: {operation}"}
|
||||||
|
|
||||||
return {
|
return {"result": result}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"original": text,
|
|
||||||
"operation": operation,
|
|
||||||
"result": result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -146,7 +129,7 @@ def json_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
operation = arguments.get("operation", "")
|
operation = arguments.get("operation", "")
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return {"success": False, "error": "Data is required"}
|
return {"error": "Data is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
parsed = json.loads(data)
|
parsed = json.loads(data)
|
||||||
|
|
@ -158,17 +141,11 @@ def json_process(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
elif operation == "validate":
|
elif operation == "validate":
|
||||||
result = "Valid JSON"
|
result = "Valid JSON"
|
||||||
else:
|
else:
|
||||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
return {"error": f"Unknown operation: {operation}"}
|
||||||
|
|
||||||
return {
|
return {"result": result}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"result": result,
|
|
||||||
"operation": operation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
return {"success": False, "error": f"Invalid JSON: {str(e)}"}
|
return {"error": f"Invalid JSON: {str(e)}"}
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -199,23 +176,16 @@ def hash_text(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
algorithm = arguments.get("algorithm", "sha256")
|
algorithm = arguments.get("algorithm", "sha256")
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
return {"success": False, "error": "Text is required"}
|
return {"error": "Text is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hash_obj = hashlib.new(algorithm)
|
hash_obj = hashlib.new(algorithm)
|
||||||
hash_obj.update(text.encode('utf-8'))
|
hash_obj.update(text.encode('utf-8'))
|
||||||
hash_value = hash_obj.hexdigest()
|
hash_value = hash_obj.hexdigest()
|
||||||
|
|
||||||
return {
|
return {"hash": hash_value}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"text": text,
|
|
||||||
"algorithm": algorithm,
|
|
||||||
"hash": hash_value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"success": False, "error": f"Hash error: {str(e)}"}
|
return {"error": f"Hash error: {str(e)}"}
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -244,7 +214,7 @@ def url_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
operation = arguments.get("operation", "")
|
operation = arguments.get("operation", "")
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
return {"success": False, "error": "Text is required"}
|
return {"error": "Text is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if operation == "encode":
|
if operation == "encode":
|
||||||
|
|
@ -252,18 +222,11 @@ def url_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
elif operation == "decode":
|
elif operation == "decode":
|
||||||
result = unquote(text)
|
result = unquote(text)
|
||||||
else:
|
else:
|
||||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
return {"error": f"Unknown operation: {operation}"}
|
||||||
|
|
||||||
return {
|
return {"result": result}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"original": text,
|
|
||||||
"operation": operation,
|
|
||||||
"result": result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"success": False, "error": f"URL error: {str(e)}"}
|
return {"error": f"URL error: {str(e)}"}
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
|
|
@ -292,7 +255,7 @@ def base64_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
operation = arguments.get("operation", "")
|
operation = arguments.get("operation", "")
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
return {"success": False, "error": "Text is required"}
|
return {"error": "Text is required"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if operation == "encode":
|
if operation == "encode":
|
||||||
|
|
@ -300,15 +263,8 @@ def base64_encode_decode(arguments: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
elif operation == "decode":
|
elif operation == "decode":
|
||||||
result = base64.b64decode(text.encode()).decode()
|
result = base64.b64decode(text.encode()).decode()
|
||||||
else:
|
else:
|
||||||
return {"success": False, "error": f"Unknown operation: {operation}"}
|
return {"error": f"Unknown operation: {operation}"}
|
||||||
|
|
||||||
return {
|
return {"result": result}
|
||||||
"success": True,
|
|
||||||
"data": {
|
|
||||||
"original": text,
|
|
||||||
"operation": operation,
|
|
||||||
"result": result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
except Exception as e:
|
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:
|
with ThreadPoolExecutor(max_workers=max_concurrent) as pool:
|
||||||
futures = {pool.submit(self.fetch, url, extract_type): i for i, url in enumerate(urls)}
|
futures = {pool.submit(self.fetch, url, extract_type): i for i, url in enumerate(urls)}
|
||||||
for future in as_completed(futures):
|
for future in as_completed(futures):
|
||||||
|
try:
|
||||||
results[futures[future]] = future.result()
|
results[futures[future]] = future.result()
|
||||||
|
except Exception as e:
|
||||||
|
results[futures[future]] = {"error": str(e)}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue